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