Import 2.3.9pre5
[davej-history.git] / drivers / block / hd.c
blobf01d7475d5bd4ae7b23b80ea1887ec138eee376f
1 /*
2 * linux/drivers/block/hd.c
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 */
7 /*
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 */
29 #include <asm/irq.h>
30 #include <linux/errno.h>
31 #include <linux/signal.h>
32 #include <linux/sched.h>
33 #include <linux/timer.h>
34 #include <linux/fs.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>
46 #include <asm/io.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);
54 #define HD_DELAY 0
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 */
59 #define MAX_HD 2
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);
73 static int reset = 0;
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.
81 struct hd_i_struct {
82 unsigned int head,sect,cyl,wpcom,lzone,ctl;
85 #ifdef HD_TYPE
86 static struct hd_i_struct hd_info[] = { HD_TYPE };
87 static int NR_HD = ((sizeof (hd_info))/(sizeof (struct hd_i_struct)));
88 #else
89 static struct hd_i_struct hd_info[] = { {0,0,0,0,0,0},{0,0,0,0,0,0} };
90 static int NR_HD = 0;
91 #endif
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, };
97 #if (HD_DELAY > 0)
98 unsigned long last_req;
100 unsigned long read_timer(void)
102 unsigned long t, flags;
103 int i;
105 save_flags(flags);
106 cli();
107 t = jiffies * 11932;
108 outb_p(0, 0x43);
109 i = inb_p(0x40);
110 i |= inb(0x40) << 8;
111 restore_flags(flags);
112 return(t - i);
114 #endif
116 __initfunc(void hd_setup(char *str, int *ints))
118 int hdind = 0;
120 if (ints[0] != 3)
121 return;
122 if (hd_info[0].head != 0)
123 hdind=1;
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);
130 NR_HD = hdind+1;
133 static void dump_status (const char *msg, unsigned int stat)
135 unsigned long flags;
136 char devc;
138 devc = CURRENT ? 'a' + DEVICE_NR(CURRENT->rq_dev) : '?';
139 save_flags (flags);
140 sti();
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 ");
151 printk("}\n");
152 if ((stat & ERR_STAT) == 0) {
153 hd_error = 0;
154 } else {
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 ");
163 printk("}");
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));
167 if (CURRENT)
168 printk(", sector=%ld", CURRENT->sector);
170 printk("\n");
172 #else
173 printk("hd%c: %s: status=0x%02x.\n", devc, msg, stat & 0xff);
174 if ((stat & ERR_STAT) == 0) {
175 hd_error = 0;
176 } else {
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);
188 if (!OK_STATUS(i)) {
189 dump_status("check_status", i);
190 bad_rw_intr();
194 static int controller_busy(void)
196 int retries = 100000;
197 unsigned char status;
199 do {
200 status = inb_p(HD_STATUS);
201 } while ((status & BUSY_STAT) && --retries);
202 return status;
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)
212 return 0;
213 if (!(status & READY_STAT))
214 return 0;
215 if (!(status & SEEK_STAT))
216 return 0;
217 return 1;
220 static int controller_ready(unsigned int drive, unsigned int head)
222 int retry = 100;
224 do {
225 if (controller_busy() & BUSY_STAT)
226 return 0;
227 outb_p(0xA0 | (drive<<4) | head, HD_CURRENT);
228 if (status_ok())
229 return 1;
230 } while (--retry);
231 return 0;
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))
238 unsigned short port;
240 #if (HD_DELAY > 0)
241 while (read_timer() - last_req < HD_DELAY)
242 /* nothing */;
243 #endif
244 if (reset)
245 return;
246 if (!controller_ready(drive, head)) {
247 reset = 1;
248 return;
250 SET_INTR(intr_addr);
251 outb_p(hd_info[drive].ctl,HD_CMD);
252 port=HD_DATA;
253 outb_p(hd_info[drive].wpcom>>2,++port);
254 outb_p(nsect,++port);
255 outb_p(sect,++port);
256 outb_p(cyl,++port);
257 outb_p(cyl>>8,++port);
258 outb_p(0xA0|(drive<<4)|head,++port);
259 outb_p(cmd,++port);
262 static void hd_request (void);
264 static int drive_busy(void)
266 unsigned int i;
267 unsigned char c;
269 for (i = 0; i < 500000 ; i++) {
270 c = inb_p(HD_STATUS);
271 if ((c & (BUSY_STAT | READY_STAT | SEEK_STAT)) == STAT_OK)
272 return 0;
274 dump_status("reset timed out", c);
275 return 1;
278 static void reset_controller(void)
280 int i;
282 outb_p(4,HD_CMD);
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();
286 if (drive_busy())
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)
294 static int i;
296 repeat:
297 if (reset) {
298 reset = 0;
299 i = -1;
300 reset_controller();
301 } else {
302 check_status();
303 if (reset)
304 goto repeat;
306 if (++i < NR_HD) {
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);
310 if (reset)
311 goto repeat;
312 } else
313 hd_request();
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);
331 SET_TIMER;
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)
342 int dev;
344 if (!CURRENT)
345 return;
346 dev = DEVICE_NR(CURRENT->rq_dev);
347 if (++CURRENT->errors >= MAX_ERRORS || (hd_error & BBD_ERR)) {
348 end_request(0);
349 special_op[dev] = recalibrate[dev] = 1;
350 } else if (CURRENT->errors % RESET_FREQ == 0)
351 reset = 1;
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)
363 return 0;
364 dump_status("wait_DRQ", stat);
365 return -1;
368 static void read_intr(void)
370 int i, retries = 100000;
372 do {
373 i = (unsigned) inb_p(HD_STATUS);
374 if (i & BUSY_STAT)
375 continue;
376 if (!OK_STATUS(i))
377 break;
378 if (i & DRQ_STAT)
379 goto ok_to_read;
380 } while (--retries > 0);
381 dump_status("read_intr", i);
382 bad_rw_intr();
383 hd_request();
384 return;
385 ok_to_read:
386 insw(HD_DATA,CURRENT->buffer,256);
387 CURRENT->sector++;
388 CURRENT->buffer += 512;
389 CURRENT->errors = 0;
390 i = --CURRENT->nr_sectors;
391 --CURRENT->current_nr_sectors;
392 #ifdef DEBUG
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));
396 #endif
397 if (CURRENT->current_nr_sectors <= 0)
398 end_request(1);
399 if (i > 0) {
400 SET_INTR(&read_intr);
401 return;
403 (void) inb_p(HD_STATUS);
404 #if (HD_DELAY > 0)
405 last_req = read_timer();
406 #endif
407 if (CURRENT)
408 hd_request();
409 return;
412 static void write_intr(void)
414 int i;
415 int retries = 100000;
417 do {
418 i = (unsigned) inb_p(HD_STATUS);
419 if (i & BUSY_STAT)
420 continue;
421 if (!OK_STATUS(i))
422 break;
423 if ((CURRENT->nr_sectors <= 1) || (i & DRQ_STAT))
424 goto ok_to_write;
425 } while (--retries > 0);
426 dump_status("write_intr", i);
427 bad_rw_intr();
428 hd_request();
429 return;
430 ok_to_write:
431 CURRENT->sector++;
432 i = --CURRENT->nr_sectors;
433 --CURRENT->current_nr_sectors;
434 CURRENT->buffer += 512;
435 if (!i || (CURRENT->bh && !SUBSECTOR(i)))
436 end_request(1);
437 if (i > 0) {
438 SET_INTR(&write_intr);
439 outsw(HD_DATA,CURRENT->buffer,256);
440 sti();
441 } else {
442 #if (HD_DELAY > 0)
443 last_req = read_timer();
444 #endif
445 hd_request();
447 return;
450 static void recal_intr(void)
452 check_status();
453 #if (HD_DELAY > 0)
454 last_req = read_timer();
455 #endif
456 hd_request();
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)
465 unsigned int dev;
467 DEVICE_INTR = NULL;
468 if (!CURRENT)
469 return;
470 disable_irq(HD_IRQ);
471 sti();
472 reset = 1;
473 dev = DEVICE_NR(CURRENT->rq_dev);
474 printk("hd%c: timeout\n", dev+'a');
475 if (++CURRENT->errors >= MAX_ERRORS) {
476 #ifdef DEBUG
477 printk("hd%c: too many errors\n", dev+'a');
478 #endif
479 end_request(0);
481 cli();
482 hd_request();
483 enable_irq(HD_IRQ);
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);
491 return reset;
493 if (hd_info[dev].head > 16) {
494 printk ("hd%c: cannot handle device with more than 16 heads - giving up\n", dev+'a');
495 end_request(0);
497 special_op[dev] = 0;
498 return 1;
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;
516 if (DEVICE_INTR)
517 return;
518 repeat:
519 timer_active &= ~(1<<HD_TIMER);
520 sti();
521 INIT_REQUEST;
522 if (reset) {
523 cli();
524 reset_hd();
525 return;
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)) {
531 #ifdef DEBUG
532 if (dev >= (NR_HD<<6))
533 printk("hd: bad minor number: device=%s\n",
534 kdevname(CURRENT->rq_dev));
535 else
536 printk("hd%c: bad access: block=%d, count=%d\n",
537 (MINOR(CURRENT->rq_dev)>>6)+'a', block, nsect);
538 #endif
539 end_request(0);
540 goto repeat;
542 block += hd[dev].start_sect;
543 dev >>= 6;
544 if (special_op[dev]) {
545 if (do_special_op(dev))
546 goto repeat;
547 return;
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;
553 #ifdef DEBUG
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);
557 #endif
558 if (CURRENT->cmd == READ) {
559 hd_out(dev,nsect,sec,head,cyl,WIN_READ,&read_intr);
560 if (reset)
561 goto repeat;
562 return;
564 if (CURRENT->cmd == WRITE) {
565 hd_out(dev,nsect,sec,head,cyl,WIN_WRITE,&write_intr);
566 if (reset)
567 goto repeat;
568 if (wait_DRQ()) {
569 bad_rw_intr();
570 goto repeat;
572 outsw(HD_DATA,CURRENT->buffer,256);
573 return;
575 panic("unknown hd-command");
578 static void do_hd_request (void)
580 disable_irq(HD_IRQ);
581 hd_request();
582 enable_irq(HD_IRQ);
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;
589 int dev;
591 if ((!inode) || !(inode->i_rdev))
592 return -EINVAL;
593 dev = DEVICE_NR(inode->i_rdev);
594 if (dev >= NR_HD)
595 return -EINVAL;
596 switch (cmd) {
597 case HDIO_GETGEO:
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,
611 (long *) arg);
613 case BLKRRPART: /* Re-read partition tables */
614 if (!capable(CAP_SYS_ADMIN))
615 return -EACCES;
616 return revalidate_hddisk(inode->i_rdev, 1);
618 case BLKROSET:
619 case BLKROGET:
620 case BLKRASET:
621 case BLKRAGET:
622 case BLKFLSBUF:
623 case BLKPG:
624 return blk_ioctl(inode->i_rdev, cmd, arg);
626 default:
627 return -EINVAL;
631 static int hd_open(struct inode * inode, struct file * filp)
633 int target;
634 target = DEVICE_NR(inode->i_rdev);
636 if (target >= NR_HD)
637 return -ENODEV;
638 while (busy[target])
639 sleep_on(&busy_wait);
640 access_count[target]++;
641 return 0;
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)
650 int target;
651 sync_dev(inode->i_rdev);
653 target = DEVICE_NR(inode->i_rdev);
654 access_count[target]--;
655 return 0;
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 */
667 hd, /* hd struct */
668 hd_sizes, /* block sizes */
669 0, /* number */
670 NULL, /* internal use, not presently used */
671 NULL /* next */
674 static void hd_interrupt(int irq, void *dev_id, struct pt_regs *regs)
676 void (*handler)(void) = DEVICE_INTR;
678 DEVICE_INTR = NULL;
679 timer_active &= ~(1<<HD_TIMER);
680 if (!handler)
681 handler = unexpected_hd_interrupt;
682 handler();
683 sti();
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
690 * machines.
692 * We enable interrupts in some of the routines after making sure it's
693 * safe.
695 static void hd_geninit(struct gendisk *ignored)
697 int drive;
699 #ifdef __i386__
700 if (!NR_HD) {
701 extern struct drive_info drive_info;
702 unsigned char *BIOS = (unsigned char *) &drive_info;
703 int cmos_disks;
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)
714 NR_HD++;
715 #endif
716 BIOS += 16;
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
724 not present in CMOS.
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)
743 NR_HD = 2;
744 else
745 NR_HD = 1;
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);
756 if (NR_HD) {
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);
759 NR_HD = 0;
760 } else {
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 */
777 NULL, /* poll */
778 hd_ioctl, /* ioctl */
779 NULL, /* mmap */
780 hd_open, /* open */
781 NULL, /* flush */
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);
790 return -1;
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;
797 return 0;
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
804 will not change */
805 #undef MAYBE_REINIT
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
814 * is our limit.
816 static int revalidate_hddisk(kdev_t dev, int maxusage)
818 int target;
819 struct gendisk * gdev;
820 int max_p;
821 int start;
822 int i;
823 long flags;
825 target = DEVICE_NR(dev);
826 gdev = &GENDISK_STRUCT;
828 save_flags(flags);
829 cli();
830 if (DEVICE_BUSY || USAGE > maxusage) {
831 restore_flags(flags);
832 return -EBUSY;
834 DEVICE_BUSY = 1;
835 restore_flags(flags);
837 max_p = gdev->max_p;
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);
845 sync_dev(devi);
846 if (sb)
847 invalidate_inodes(sb);
848 invalidate_buffers(devi);
849 gdev->part[minor].start_sect = 0;
850 gdev->part[minor].nr_sects = 0;
853 #ifdef MAYBE_REINIT
854 MAYBE_REINIT;
855 #endif
857 gdev->part[start].nr_sects = CAPACITY;
858 resetup_one_dev(gdev, target);
860 DEVICE_BUSY = 0;
861 wake_up(&busy_wait);
862 return 0;