File: /usr/src/linux/drivers/ide/hd.c
1 /*
2 * linux/drivers/ide/hd.c
3 *
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 */
6
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.
12 *
13 * modified by Drew Eckhardt to check nr of hd's from the CMOS.
14 *
15 * Thanks to Branko Lankester, lankeste@fwi.uva.nl, who found a bug
16 * in the early extended-partition checks and added DM partitions
17 *
18 * IRQ-unmask, drive-id, multiple-mode, support for ">16 heads",
19 * and general streamlining by Mark Lord.
20 *
21 * Removed 99% of above. Use Mark's ide driver for those options.
22 * This is now a lightweight ST-506 driver. (Paul Gortmaker)
23 *
24 * Modified 1995 Russell King for ARM processor.
25 *
26 * Bugfix: max_sectors must be <= 255 or the wheels tend to come
27 * off in a hurry once you queue things up - Paul G. 02/2001
28 */
29
30 /* Uncomment the following if you want verbose error reports. */
31 /* #define VERBOSE_ERRORS */
32
33 #include <linux/errno.h>
34 #include <linux/signal.h>
35 #include <linux/sched.h>
36 #include <linux/timer.h>
37 #include <linux/fs.h>
38 #include <linux/devfs_fs_kernel.h>
39 #include <linux/kernel.h>
40 #include <linux/hdreg.h>
41 #include <linux/genhd.h>
42 #include <linux/slab.h>
43 #include <linux/string.h>
44 #include <linux/ioport.h>
45 #include <linux/mc146818rtc.h> /* CMOS defines */
46 #include <linux/init.h>
47 #include <linux/blkpg.h>
48
49 #define REALLY_SLOW_IO
50 #include <asm/system.h>
51 #include <asm/io.h>
52 #include <asm/uaccess.h>
53
54 #define MAJOR_NR HD_MAJOR
55 #include <linux/blk.h>
56
57 #ifdef __arm__
58 #undef HD_IRQ
59 #endif
60 #include <asm/irq.h>
61 #ifdef __arm__
62 #define HD_IRQ IRQ_HARDDISK
63 #endif
64
65 static int revalidate_hddisk(kdev_t, int);
66
67 #define HD_DELAY 0
68
69 #define MAX_ERRORS 16 /* Max read/write errors/sector */
70 #define RESET_FREQ 8 /* Reset controller every 8th retry */
71 #define RECAL_FREQ 4 /* Recalibrate every 4th retry */
72 #define MAX_HD 2
73
74 #define STAT_OK (READY_STAT|SEEK_STAT)
75 #define OK_STATUS(s) (((s)&(STAT_OK|(BUSY_STAT|WRERR_STAT|ERR_STAT)))==STAT_OK)
76
77 static void recal_intr(void);
78 static void bad_rw_intr(void);
79
80 static char recalibrate[MAX_HD];
81 static char special_op[MAX_HD];
82 static int access_count[MAX_HD];
83 static char busy[MAX_HD];
84 static DECLARE_WAIT_QUEUE_HEAD(busy_wait);
85
86 static int reset;
87 static int hd_error;
88
89 #define SUBSECTOR(block) (CURRENT->current_nr_sectors > 0)
90
91 /*
92 * This struct defines the HD's and their types.
93 */
94 struct hd_i_struct {
95 unsigned int head,sect,cyl,wpcom,lzone,ctl;
96 };
97
98 #ifdef HD_TYPE
99 static struct hd_i_struct hd_info[] = { HD_TYPE };
100 static int NR_HD = ((sizeof (hd_info))/(sizeof (struct hd_i_struct)));
101 #else
102 static struct hd_i_struct hd_info[MAX_HD];
103 static int NR_HD;
104 #endif
105
106 static struct hd_struct hd[MAX_HD<<6];
107 static int hd_sizes[MAX_HD<<6];
108 static int hd_blocksizes[MAX_HD<<6];
109 static int hd_hardsectsizes[MAX_HD<<6];
110 static int hd_maxsect[MAX_HD<<6];
111
112 static struct timer_list device_timer;
113
114 #define SET_TIMER \
115 do { \
116 mod_timer(&device_timer, jiffies + TIMEOUT_VALUE); \
117 } while (0)
118
119 #define CLEAR_TIMER del_timer(&device_timer);
120
121 #undef SET_INTR
122
123 #define SET_INTR(x) \
124 if ((DEVICE_INTR = (x)) != NULL) \
125 SET_TIMER; \
126 else \
127 CLEAR_TIMER;
128
129
130 #if (HD_DELAY > 0)
131 unsigned long last_req;
132
133 unsigned long read_timer(void)
134 {
135 unsigned long t, flags;
136 int i;
137
138 save_flags(flags);
139 cli();
140 t = jiffies * 11932;
141 outb_p(0, 0x43);
142 i = inb_p(0x40);
143 i |= inb(0x40) << 8;
144 restore_flags(flags);
145 return(t - i);
146 }
147 #endif
148
149 void __init hd_setup(char *str, int *ints)
150 {
151 int hdind = 0;
152
153 if (ints[0] != 3)
154 return;
155 if (hd_info[0].head != 0)
156 hdind=1;
157 hd_info[hdind].head = ints[2];
158 hd_info[hdind].sect = ints[3];
159 hd_info[hdind].cyl = ints[1];
160 hd_info[hdind].wpcom = 0;
161 hd_info[hdind].lzone = ints[1];
162 hd_info[hdind].ctl = (ints[2] > 8 ? 8 : 0);
163 NR_HD = hdind+1;
164 }
165
166 static void dump_status (const char *msg, unsigned int stat)
167 {
168 unsigned long flags;
169 char devc;
170
171 devc = !QUEUE_EMPTY ? 'a' + DEVICE_NR(CURRENT->rq_dev) : '?';
172 save_flags (flags);
173 sti();
174 #ifdef VERBOSE_ERRORS
175 printk("hd%c: %s: status=0x%02x { ", devc, msg, stat & 0xff);
176 if (stat & BUSY_STAT) printk("Busy ");
177 if (stat & READY_STAT) printk("DriveReady ");
178 if (stat & WRERR_STAT) printk("WriteFault ");
179 if (stat & SEEK_STAT) printk("SeekComplete ");
180 if (stat & DRQ_STAT) printk("DataRequest ");
181 if (stat & ECC_STAT) printk("CorrectedError ");
182 if (stat & INDEX_STAT) printk("Index ");
183 if (stat & ERR_STAT) printk("Error ");
184 printk("}\n");
185 if ((stat & ERR_STAT) == 0) {
186 hd_error = 0;
187 } else {
188 hd_error = inb(HD_ERROR);
189 printk("hd%c: %s: error=0x%02x { ", devc, msg, hd_error & 0xff);
190 if (hd_error & BBD_ERR) printk("BadSector ");
191 if (hd_error & ECC_ERR) printk("UncorrectableError ");
192 if (hd_error & ID_ERR) printk("SectorIdNotFound ");
193 if (hd_error & ABRT_ERR) printk("DriveStatusError ");
194 if (hd_error & TRK0_ERR) printk("TrackZeroNotFound ");
195 if (hd_error & MARK_ERR) printk("AddrMarkNotFound ");
196 printk("}");
197 if (hd_error & (BBD_ERR|ECC_ERR|ID_ERR|MARK_ERR)) {
198 printk(", CHS=%d/%d/%d", (inb(HD_HCYL)<<8) + inb(HD_LCYL),
199 inb(HD_CURRENT) & 0xf, inb(HD_SECTOR));
200 if (!QUEUE_EMPTY)
201 printk(", sector=%ld", CURRENT->sector);
202 }
203 printk("\n");
204 }
205 #else
206 printk("hd%c: %s: status=0x%02x.\n", devc, msg, stat & 0xff);
207 if ((stat & ERR_STAT) == 0) {
208 hd_error = 0;
209 } else {
210 hd_error = inb(HD_ERROR);
211 printk("hd%c: %s: error=0x%02x.\n", devc, msg, hd_error & 0xff);
212 }
213 #endif /* verbose errors */
214 restore_flags (flags);
215 }
216
217 void check_status(void)
218 {
219 int i = inb_p(HD_STATUS);
220
221 if (!OK_STATUS(i)) {
222 dump_status("check_status", i);
223 bad_rw_intr();
224 }
225 }
226
227 static int controller_busy(void)
228 {
229 int retries = 100000;
230 unsigned char status;
231
232 do {
233 status = inb_p(HD_STATUS);
234 } while ((status & BUSY_STAT) && --retries);
235 return status;
236 }
237
238 static int status_ok(void)
239 {
240 unsigned char status = inb_p(HD_STATUS);
241
242 if (status & BUSY_STAT)
243 return 1; /* Ancient, but does it make sense??? */
244 if (status & WRERR_STAT)
245 return 0;
246 if (!(status & READY_STAT))
247 return 0;
248 if (!(status & SEEK_STAT))
249 return 0;
250 return 1;
251 }
252
253 static int controller_ready(unsigned int drive, unsigned int head)
254 {
255 int retry = 100;
256
257 do {
258 if (controller_busy() & BUSY_STAT)
259 return 0;
260 outb_p(0xA0 | (drive<<4) | head, HD_CURRENT);
261 if (status_ok())
262 return 1;
263 } while (--retry);
264 return 0;
265 }
266
267 static void hd_out(unsigned int drive,unsigned int nsect,unsigned int sect,
268 unsigned int head,unsigned int cyl,unsigned int cmd,
269 void (*intr_addr)(void))
270 {
271 unsigned short port;
272
273 #if (HD_DELAY > 0)
274 while (read_timer() - last_req < HD_DELAY)
275 /* nothing */;
276 #endif
277 if (reset)
278 return;
279 if (!controller_ready(drive, head)) {
280 reset = 1;
281 return;
282 }
283 SET_INTR(intr_addr);
284 outb_p(hd_info[drive].ctl,HD_CMD);
285 port=HD_DATA;
286 outb_p(hd_info[drive].wpcom>>2,++port);
287 outb_p(nsect,++port);
288 outb_p(sect,++port);
289 outb_p(cyl,++port);
290 outb_p(cyl>>8,++port);
291 outb_p(0xA0|(drive<<4)|head,++port);
292 outb_p(cmd,++port);
293 }
294
295 static void hd_request (void);
296
297 static int drive_busy(void)
298 {
299 unsigned int i;
300 unsigned char c;
301
302 for (i = 0; i < 500000 ; i++) {
303 c = inb_p(HD_STATUS);
304 if ((c & (BUSY_STAT | READY_STAT | SEEK_STAT)) == STAT_OK)
305 return 0;
306 }
307 dump_status("reset timed out", c);
308 return 1;
309 }
310
311 static void reset_controller(void)
312 {
313 int i;
314
315 outb_p(4,HD_CMD);
316 for(i = 0; i < 1000; i++) barrier();
317 outb_p(hd_info[0].ctl & 0x0f,HD_CMD);
318 for(i = 0; i < 1000; i++) barrier();
319 if (drive_busy())
320 printk("hd: controller still busy\n");
321 else if ((hd_error = inb(HD_ERROR)) != 1)
322 printk("hd: controller reset failed: %02x\n",hd_error);
323 }
324
325 static void reset_hd(void)
326 {
327 static int i;
328
329 repeat:
330 if (reset) {
331 reset = 0;
332 i = -1;
333 reset_controller();
334 } else {
335 check_status();
336 if (reset)
337 goto repeat;
338 }
339 if (++i < NR_HD) {
340 special_op[i] = recalibrate[i] = 1;
341 hd_out(i,hd_info[i].sect,hd_info[i].sect,hd_info[i].head-1,
342 hd_info[i].cyl,WIN_SPECIFY,&reset_hd);
343 if (reset)
344 goto repeat;
345 } else
346 hd_request();
347 }
348
349 /*
350 * Ok, don't know what to do with the unexpected interrupts: on some machines
351 * doing a reset and a retry seems to result in an eternal loop. Right now I
352 * ignore it, and just set the timeout.
353 *
354 * On laptops (and "green" PCs), an unexpected interrupt occurs whenever the
355 * drive enters "idle", "standby", or "sleep" mode, so if the status looks
356 * "good", we just ignore the interrupt completely.
357 */
358 void unexpected_hd_interrupt(void)
359 {
360 unsigned int stat = inb_p(HD_STATUS);
361
362 if (stat & (BUSY_STAT|DRQ_STAT|ECC_STAT|ERR_STAT)) {
363 dump_status ("unexpected interrupt", stat);
364 SET_TIMER;
365 }
366 }
367
368 /*
369 * bad_rw_intr() now tries to be a bit smarter and does things
370 * according to the error returned by the controller.
371 * -Mika Liljeberg (liljeber@cs.Helsinki.FI)
372 */
373 static void bad_rw_intr(void)
374 {
375 int dev;
376
377 if (QUEUE_EMPTY)
378 return;
379 dev = DEVICE_NR(CURRENT->rq_dev);
380 if (++CURRENT->errors >= MAX_ERRORS || (hd_error & BBD_ERR)) {
381 end_request(0);
382 special_op[dev] = recalibrate[dev] = 1;
383 } else if (CURRENT->errors % RESET_FREQ == 0)
384 reset = 1;
385 else if ((hd_error & TRK0_ERR) || CURRENT->errors % RECAL_FREQ == 0)
386 special_op[dev] = recalibrate[dev] = 1;
387 /* Otherwise just retry */
388 }
389
390 static inline int wait_DRQ(void)
391 {
392 int retries = 100000, stat;
393
394 while (--retries > 0)
395 if ((stat = inb_p(HD_STATUS)) & DRQ_STAT)
396 return 0;
397 dump_status("wait_DRQ", stat);
398 return -1;
399 }
400
401 static void read_intr(void)
402 {
403 int i, retries = 100000;
404
405 do {
406 i = (unsigned) inb_p(HD_STATUS);
407 if (i & BUSY_STAT)
408 continue;
409 if (!OK_STATUS(i))
410 break;
411 if (i & DRQ_STAT)
412 goto ok_to_read;
413 } while (--retries > 0);
414 dump_status("read_intr", i);
415 bad_rw_intr();
416 hd_request();
417 return;
418 ok_to_read:
419 insw(HD_DATA,CURRENT->buffer,256);
420 CURRENT->sector++;
421 CURRENT->buffer += 512;
422 CURRENT->errors = 0;
423 i = --CURRENT->nr_sectors;
424 --CURRENT->current_nr_sectors;
425 #ifdef DEBUG
426 printk("hd%c: read: sector %ld, remaining = %ld, buffer=0x%08lx\n",
427 dev+'a', CURRENT->sector, CURRENT->nr_sectors,
428 (unsigned long) CURRENT->buffer+512));
429 #endif
430 if (CURRENT->current_nr_sectors <= 0)
431 end_request(1);
432 if (i > 0) {
433 SET_INTR(&read_intr);
434 return;
435 }
436 (void) inb_p(HD_STATUS);
437 #if (HD_DELAY > 0)
438 last_req = read_timer();
439 #endif
440 if (!QUEUE_EMPTY)
441 hd_request();
442 return;
443 }
444
445 static void write_intr(void)
446 {
447 int i;
448 int retries = 100000;
449
450 do {
451 i = (unsigned) inb_p(HD_STATUS);
452 if (i & BUSY_STAT)
453 continue;
454 if (!OK_STATUS(i))
455 break;
456 if ((CURRENT->nr_sectors <= 1) || (i & DRQ_STAT))
457 goto ok_to_write;
458 } while (--retries > 0);
459 dump_status("write_intr", i);
460 bad_rw_intr();
461 hd_request();
462 return;
463 ok_to_write:
464 CURRENT->sector++;
465 i = --CURRENT->nr_sectors;
466 --CURRENT->current_nr_sectors;
467 CURRENT->buffer += 512;
468 if (!i || (CURRENT->bh && !SUBSECTOR(i)))
469 end_request(1);
470 if (i > 0) {
471 SET_INTR(&write_intr);
472 outsw(HD_DATA,CURRENT->buffer,256);
473 sti();
474 } else {
475 #if (HD_DELAY > 0)
476 last_req = read_timer();
477 #endif
478 hd_request();
479 }
480 return;
481 }
482
483 static void recal_intr(void)
484 {
485 check_status();
486 #if (HD_DELAY > 0)
487 last_req = read_timer();
488 #endif
489 hd_request();
490 }
491
492 /*
493 * This is another of the error-routines I don't know what to do with. The
494 * best idea seems to just set reset, and start all over again.
495 */
496 static void hd_times_out(unsigned long dummy)
497 {
498 unsigned int dev;
499
500 DEVICE_INTR = NULL;
501 if (QUEUE_EMPTY)
502 return;
503 disable_irq(HD_IRQ);
504 sti();
505 reset = 1;
506 dev = DEVICE_NR(CURRENT->rq_dev);
507 printk("hd%c: timeout\n", dev+'a');
508 if (++CURRENT->errors >= MAX_ERRORS) {
509 #ifdef DEBUG
510 printk("hd%c: too many errors\n", dev+'a');
511 #endif
512 end_request(0);
513 }
514 cli();
515 hd_request();
516 enable_irq(HD_IRQ);
517 }
518
519 int do_special_op (unsigned int dev)
520 {
521 if (recalibrate[dev]) {
522 recalibrate[dev] = 0;
523 hd_out(dev,hd_info[dev].sect,0,0,0,WIN_RESTORE,&recal_intr);
524 return reset;
525 }
526 if (hd_info[dev].head > 16) {
527 printk ("hd%c: cannot handle device with more than 16 heads - giving up\n", dev+'a');
528 end_request(0);
529 }
530 special_op[dev] = 0;
531 return 1;
532 }
533
534 /*
535 * The driver enables interrupts as much as possible. In order to do this,
536 * (a) the device-interrupt is disabled before entering hd_request(),
537 * and (b) the timeout-interrupt is disabled before the sti().
538 *
539 * Interrupts are still masked (by default) whenever we are exchanging
540 * data/cmds with a drive, because some drives seem to have very poor
541 * tolerance for latency during I/O. The IDE driver has support to unmask
542 * interrupts for non-broken hardware, so use that driver if required.
543 */
544 static void hd_request(void)
545 {
546 unsigned int dev, block, nsect, sec, track, head, cyl;
547
548 if (!QUEUE_EMPTY && CURRENT->rq_status == RQ_INACTIVE) return;
549 if (DEVICE_INTR)
550 return;
551 repeat:
552 del_timer(&device_timer);
553 sti();
554 INIT_REQUEST;
555 if (reset) {
556 cli();
557 reset_hd();
558 return;
559 }
560 dev = MINOR(CURRENT->rq_dev);
561 block = CURRENT->sector;
562 nsect = CURRENT->nr_sectors;
563 if (dev >= (NR_HD<<6) || block >= hd[dev].nr_sects || ((block+nsect) > hd[dev].nr_sects)) {
564 #ifdef DEBUG
565 if (dev >= (NR_HD<<6))
566 printk("hd: bad minor number: device=%s\n",
567 kdevname(CURRENT->rq_dev));
568 else
569 printk("hd%c: bad access: block=%d, count=%d\n",
570 (MINOR(CURRENT->rq_dev)>>6)+'a', block, nsect);
571 #endif
572 end_request(0);
573 goto repeat;
574 }
575 block += hd[dev].start_sect;
576 dev >>= 6;
577 if (special_op[dev]) {
578 if (do_special_op(dev))
579 goto repeat;
580 return;
581 }
582 sec = block % hd_info[dev].sect + 1;
583 track = block / hd_info[dev].sect;
584 head = track % hd_info[dev].head;
585 cyl = track / hd_info[dev].head;
586 #ifdef DEBUG
587 printk("hd%c: %sing: CHS=%d/%d/%d, sectors=%d, buffer=0x%08lx\n",
588 dev+'a', (CURRENT->cmd == READ)?"read":"writ",
589 cyl, head, sec, nsect, (unsigned long) CURRENT->buffer);
590 #endif
591 if (CURRENT->cmd == READ) {
592 hd_out(dev,nsect,sec,head,cyl,WIN_READ,&read_intr);
593 if (reset)
594 goto repeat;
595 return;
596 }
597 if (CURRENT->cmd == WRITE) {
598 hd_out(dev,nsect,sec,head,cyl,WIN_WRITE,&write_intr);
599 if (reset)
600 goto repeat;
601 if (wait_DRQ()) {
602 bad_rw_intr();
603 goto repeat;
604 }
605 outsw(HD_DATA,CURRENT->buffer,256);
606 return;
607 }
608 panic("unknown hd-command");
609 }
610
611 static void do_hd_request (request_queue_t * q)
612 {
613 disable_irq(HD_IRQ);
614 hd_request();
615 enable_irq(HD_IRQ);
616 }
617
618 static int hd_ioctl(struct inode * inode, struct file * file,
619 unsigned int cmd, unsigned long arg)
620 {
621 struct hd_geometry *loc = (struct hd_geometry *) arg;
622 int dev;
623
624 if ((!inode) || !(inode->i_rdev))
625 return -EINVAL;
626 dev = DEVICE_NR(inode->i_rdev);
627 if (dev >= NR_HD)
628 return -EINVAL;
629 switch (cmd) {
630 case HDIO_GETGEO:
631 {
632 struct hd_geometry g;
633 if (!loc) return -EINVAL;
634 g.heads = hd_info[dev].head;
635 g.sectors = hd_info[dev].sect;
636 g.cylinders = hd_info[dev].cyl;
637 g.start = hd[MINOR(inode->i_rdev)].start_sect;
638 return copy_to_user(loc, &g, sizeof g) ? -EFAULT : 0;
639 }
640
641 case BLKGETSIZE: /* Return device size */
642 return put_user(hd[MINOR(inode->i_rdev)].nr_sects,
643 (long *) arg);
644 case BLKGETSIZE64:
645 return put_user((u64)hd[MINOR(inode->i_rdev)].nr_sects << 9,
646 (u64 *) arg);
647
648 case BLKRRPART: /* Re-read partition tables */
649 if (!capable(CAP_SYS_ADMIN))
650 return -EACCES;
651 return revalidate_hddisk(inode->i_rdev, 1);
652
653 case BLKROSET:
654 case BLKROGET:
655 case BLKRASET:
656 case BLKRAGET:
657 case BLKFLSBUF:
658 case BLKPG:
659 return blk_ioctl(inode->i_rdev, cmd, arg);
660
661 default:
662 return -EINVAL;
663 }
664 }
665
666 static int hd_open(struct inode * inode, struct file * filp)
667 {
668 int target;
669 target = DEVICE_NR(inode->i_rdev);
670
671 if (target >= NR_HD)
672 return -ENODEV;
673 while (busy[target])
674 sleep_on(&busy_wait);
675 access_count[target]++;
676 return 0;
677 }
678
679 /*
680 * Releasing a block device means we sync() it, so that it can safely
681 * be forgotten about...
682 */
683 static int hd_release(struct inode * inode, struct file * file)
684 {
685 int target = DEVICE_NR(inode->i_rdev);
686 access_count[target]--;
687 return 0;
688 }
689
690 extern struct block_device_operations hd_fops;
691
692 static struct gendisk hd_gendisk = {
693 major: MAJOR_NR,
694 major_name: "hd",
695 minor_shift: 6,
696 max_p: 1 << 6,
697 part: hd,
698 sizes: hd_sizes,
699 fops: &hd_fops,
700 };
701
702 static void hd_interrupt(int irq, void *dev_id, struct pt_regs *regs)
703 {
704 void (*handler)(void) = DEVICE_INTR;
705
706 DEVICE_INTR = NULL;
707 del_timer(&device_timer);
708 if (!handler)
709 handler = unexpected_hd_interrupt;
710 handler();
711 sti();
712 }
713
714 static struct block_device_operations hd_fops = {
715 open: hd_open,
716 release: hd_release,
717 ioctl: hd_ioctl,
718 };
719
720 /*
721 * This is the hard disk IRQ description. The SA_INTERRUPT in sa_flags
722 * means we run the IRQ-handler with interrupts disabled: this is bad for
723 * interrupt latency, but anything else has led to problems on some
724 * machines.
725 *
726 * We enable interrupts in some of the routines after making sure it's
727 * safe.
728 */
729 static void __init hd_geninit(void)
730 {
731 int drive;
732
733 for(drive=0; drive < (MAX_HD << 6); drive++) {
734 hd_blocksizes[drive] = 1024;
735 hd_hardsectsizes[drive] = 512;
736 hd_maxsect[drive]=255;
737 }
738 blksize_size[MAJOR_NR] = hd_blocksizes;
739 hardsect_size[MAJOR_NR] = hd_hardsectsizes;
740 max_sectors[MAJOR_NR] = hd_maxsect;
741
742 #ifdef __i386__
743 if (!NR_HD) {
744 extern struct drive_info drive_info;
745 unsigned char *BIOS = (unsigned char *) &drive_info;
746 unsigned long flags;
747 int cmos_disks;
748
749 for (drive=0 ; drive<2 ; drive++) {
750 hd_info[drive].cyl = *(unsigned short *) BIOS;
751 hd_info[drive].head = *(2+BIOS);
752 hd_info[drive].wpcom = *(unsigned short *) (5+BIOS);
753 hd_info[drive].ctl = *(8+BIOS);
754 hd_info[drive].lzone = *(unsigned short *) (12+BIOS);
755 hd_info[drive].sect = *(14+BIOS);
756 #ifdef does_not_work_for_everybody_with_scsi_but_helps_ibm_vp
757 if (hd_info[drive].cyl && NR_HD == drive)
758 NR_HD++;
759 #endif
760 BIOS += 16;
761 }
762
763 /*
764 We query CMOS about hard disks : it could be that
765 we have a SCSI/ESDI/etc controller that is BIOS
766 compatible with ST-506, and thus showing up in our
767 BIOS table, but not register compatible, and therefore
768 not present in CMOS.
769
770 Furthermore, we will assume that our ST-506 drives
771 <if any> are the primary drives in the system, and
772 the ones reflected as drive 1 or 2.
773
774 The first drive is stored in the high nibble of CMOS
775 byte 0x12, the second in the low nibble. This will be
776 either a 4 bit drive type or 0xf indicating use byte 0x19
777 for an 8 bit type, drive 1, 0x1a for drive 2 in CMOS.
778
779 Needless to say, a non-zero value means we have
780 an AT controller hard disk for that drive.
781
782 Currently the rtc_lock is a bit academic since this
783 driver is non-modular, but someday... ? Paul G.
784 */
785
786 spin_lock_irqsave(&rtc_lock, flags);
787 cmos_disks = CMOS_READ(0x12);
788 spin_unlock_irqrestore(&rtc_lock, flags);
789
790 if (cmos_disks & 0xf0) {
791 if (cmos_disks & 0x0f)
792 NR_HD = 2;
793 else
794 NR_HD = 1;
795 }
796 }
797 #endif /* __i386__ */
798 #ifdef __arm__
799 if (!NR_HD) {
800 /* We don't know anything about the drive. This means
801 * that you *MUST* specify the drive parameters to the
802 * kernel yourself.
803 */
804 printk("hd: no drives specified - use hd=cyl,head,sectors"
805 " on kernel command line\n");
806 }
807 #endif
808
809 for (drive=0 ; drive < NR_HD ; drive++) {
810 hd[drive<<6].nr_sects = hd_info[drive].head *
811 hd_info[drive].sect * hd_info[drive].cyl;
812 printk ("hd%c: %ldMB, CHS=%d/%d/%d\n", drive+'a',
813 hd[drive<<6].nr_sects / 2048, hd_info[drive].cyl,
814 hd_info[drive].head, hd_info[drive].sect);
815 }
816 if (!NR_HD)
817 return;
818
819 if (request_irq(HD_IRQ, hd_interrupt, SA_INTERRUPT, "hd", NULL)) {
820 printk("hd: unable to get IRQ%d for the hard disk driver\n",
821 HD_IRQ);
822 NR_HD = 0;
823 return;
824 }
825 request_region(HD_DATA, 8, "hd");
826 request_region(HD_CMD, 1, "hd(cmd)");
827
828 hd_gendisk.nr_real = NR_HD;
829
830 for(drive=0; drive < NR_HD; drive++)
831 register_disk(&hd_gendisk, MKDEV(MAJOR_NR,drive<<6), 1<<6,
832 &hd_fops, hd_info[drive].head * hd_info[drive].sect *
833 hd_info[drive].cyl);
834 }
835
836 int __init hd_init(void)
837 {
838 if (devfs_register_blkdev(MAJOR_NR,"hd",&hd_fops)) {
839 printk("hd: unable to get major %d for hard disk\n",MAJOR_NR);
840 return -1;
841 }
842 blk_init_queue(BLK_DEFAULT_QUEUE(MAJOR_NR), DEVICE_REQUEST);
843 read_ahead[MAJOR_NR] = 8; /* 8 sector (4kB) read-ahead */
844 add_gendisk(&hd_gendisk);
845 init_timer(&device_timer);
846 device_timer.function = hd_times_out;
847 hd_geninit();
848 return 0;
849 }
850
851 #define DEVICE_BUSY busy[target]
852 #define USAGE access_count[target]
853 #define CAPACITY (hd_info[target].head*hd_info[target].sect*hd_info[target].cyl)
854 /* We assume that the BIOS parameters do not change, so the disk capacity
855 will not change */
856 #undef MAYBE_REINIT
857 #define GENDISK_STRUCT hd_gendisk
858
859 /*
860 * This routine is called to flush all partitions and partition tables
861 * for a changed disk, and then re-read the new partition table.
862 * If we are revalidating a disk because of a media change, then we
863 * enter with usage == 0. If we are using an ioctl, we automatically have
864 * usage == 1 (we need an open channel to use an ioctl :-), so this
865 * is our limit.
866 */
867 static int revalidate_hddisk(kdev_t dev, int maxusage)
868 {
869 int target;
870 struct gendisk * gdev;
871 int max_p;
872 int start;
873 int i;
874 long flags;
875
876 target = DEVICE_NR(dev);
877 gdev = &GENDISK_STRUCT;
878
879 save_flags(flags);
880 cli();
881 if (DEVICE_BUSY || USAGE > maxusage) {
882 restore_flags(flags);
883 return -EBUSY;
884 }
885 DEVICE_BUSY = 1;
886 restore_flags(flags);
887
888 max_p = gdev->max_p;
889 start = target << gdev->minor_shift;
890
891 for (i=max_p - 1; i >=0 ; i--) {
892 int minor = start + i;
893 invalidate_device(MKDEV(MAJOR_NR, minor), 1);
894 gdev->part[minor].start_sect = 0;
895 gdev->part[minor].nr_sects = 0;
896 }
897
898 #ifdef MAYBE_REINIT
899 MAYBE_REINIT;
900 #endif
901
902 grok_partitions(gdev, target, 1<<6, CAPACITY);
903
904 DEVICE_BUSY = 0;
905 wake_up(&busy_wait);
906 return 0;
907 }
908
909 static int parse_hd_setup (char *line) {
910 int ints[6];
911
912 (void) get_options(line, ARRAY_SIZE(ints), ints);
913 hd_setup(NULL, ints);
914
915 return 1;
916 }
917 __setup("hd=", parse_hd_setup);
918
919