Import 2.1.116pre2
[davej-history.git] / drivers / block / ide-probe.c
blobffe446afee1b598479f3fdec256b672038f9c899
1 /*
2 * linux/drivers/block/ide-probe.c Version 1.03 Dec 5, 1997
4 * Copyright (C) 1994-1998 Linus Torvalds & authors (see below)
5 */
7 /*
8 * Maintained by Mark Lord <mlord@pobox.com>
9 * and Gadi Oxman <gadio@netvision.net.il>
11 * This is the IDE probe module, as evolved from hd.c and ide.c.
13 * Version 1.00 move drive probing code from ide.c to ide-probe.c
14 * Version 1.01 fix compilation problem for m68k
15 * Version 1.02 increase WAIT_PIDENTIFY to avoid CD-ROM locking at boot
16 * by Andrea Arcangeli <arcangeli@mbox.queen.it>
17 * Version 1.03 fix for (hwif->chipset == ide_4drives)
20 #undef REALLY_SLOW_IO /* most systems can safely undef this */
22 #include <linux/config.h>
23 #include <linux/module.h>
24 #include <linux/types.h>
25 #include <linux/string.h>
26 #include <linux/kernel.h>
27 #include <linux/timer.h>
28 #include <linux/mm.h>
29 #include <linux/interrupt.h>
30 #include <linux/major.h>
31 #include <linux/errno.h>
32 #include <linux/genhd.h>
33 #include <linux/malloc.h>
34 #include <linux/delay.h>
36 #include <asm/byteorder.h>
37 #include <asm/irq.h>
38 #include <asm/uaccess.h>
39 #include <asm/io.h>
41 #include "ide.h"
43 static inline void do_identify (ide_drive_t *drive, byte cmd)
45 int bswap = 1;
46 struct hd_driveid *id;
48 id = drive->id = kmalloc (SECTOR_WORDS*4, GFP_ATOMIC); /* called with interrupts disabled! */
49 ide_input_data(drive, id, SECTOR_WORDS); /* read 512 bytes of id info */
50 ide__sti(); /* local CPU only */
51 ide_fix_driveid(id);
53 #if defined (CONFIG_SCSI_EATA_DMA) || defined (CONFIG_SCSI_EATA_PIO) || defined (CONFIG_SCSI_EATA)
55 * EATA SCSI controllers do a hardware ATA emulation:
56 * Ignore them if there is a driver for them available.
58 if ((id->model[0] == 'P' && id->model[1] == 'M')
59 || (id->model[0] == 'S' && id->model[1] == 'K')) {
60 printk("%s: EATA SCSI HBA %.10s\n", drive->name, id->model);
61 drive->present = 0;
62 return;
64 #endif /* CONFIG_SCSI_EATA_DMA || CONFIG_SCSI_EATA_PIO */
67 * WIN_IDENTIFY returns little-endian info,
68 * WIN_PIDENTIFY *usually* returns little-endian info.
70 if (cmd == WIN_PIDENTIFY) {
71 if ((id->model[0] == 'N' && id->model[1] == 'E') /* NEC */
72 || (id->model[0] == 'F' && id->model[1] == 'X') /* Mitsumi */
73 || (id->model[0] == 'P' && id->model[1] == 'i'))/* Pioneer */
74 bswap ^= 1; /* Vertos drives may still be weird */
76 ide_fixstring (id->model, sizeof(id->model), bswap);
77 ide_fixstring (id->fw_rev, sizeof(id->fw_rev), bswap);
78 ide_fixstring (id->serial_no, sizeof(id->serial_no), bswap);
80 id->model[sizeof(id->model)-1] = '\0'; /* we depend on this a lot! */
81 drive->present = 1;
82 printk("%s: %s, ", drive->name, id->model);
85 * Check for an ATAPI device
87 if (cmd == WIN_PIDENTIFY) {
88 byte type = (id->config >> 8) & 0x1f;
89 printk("ATAPI ");
90 #ifdef CONFIG_BLK_DEV_PDC4030
91 if (HWIF(drive)->channel == 1 && HWIF(drive)->chipset == ide_pdc4030) {
92 printk(" -- not supported on 2nd Promise port\n");
93 drive->present = 0;
94 return;
96 #endif /* CONFIG_BLK_DEV_PDC4030 */
97 switch (type) {
98 case ide_floppy:
99 if (!strstr(id->model, "CD-ROM")) {
100 if (!strstr(id->model, "oppy") && !strstr(id->model, "poyp") && !strstr(id->model, "ZIP"))
101 printk("cdrom or floppy?, assuming ");
102 if (drive->media != ide_cdrom) {
103 printk ("FLOPPY");
104 break;
107 type = ide_cdrom; /* Early cdrom models used zero */
108 case ide_cdrom:
109 printk ("CDROM");
110 drive->removable = 1;
111 break;
112 case ide_tape:
113 printk ("TAPE");
114 break;
115 default:
116 printk("UNKNOWN (type %d)", type);
117 break;
119 printk (" drive\n");
120 drive->media = type;
121 return;
124 drive->media = ide_disk;
125 printk("ATA DISK drive\n");
126 return;
130 * Delay for *at least* 50ms. As we don't know how much time is left
131 * until the next tick occurs, we wait an extra tick to be safe.
132 * This is used only during the probing/polling for drives at boot time.
134 static void delay_50ms (void)
136 unsigned long timeout = jiffies + ((HZ + 19)/20) + 1;
137 while (0 < (signed long)(timeout - jiffies));
141 * try_to_identify() sends an ATA(PI) IDENTIFY request to a drive
142 * and waits for a response. It also monitors irqs while this is
143 * happening, in hope of automatically determining which one is
144 * being used by the interface.
146 * Returns: 0 device was identified
147 * 1 device timed-out (no response to identify request)
148 * 2 device aborted the command (refused to identify itself)
150 static int try_to_identify (ide_drive_t *drive, byte cmd)
152 int rc;
153 ide_ioreg_t hd_status;
154 unsigned long timeout;
155 unsigned long irqs = 0;
156 byte s, a;
158 if (!HWIF(drive)->irq) { /* already got an IRQ? */
159 probe_irq_off(probe_irq_on()); /* clear dangling irqs */
160 irqs = probe_irq_on(); /* start monitoring irqs */
161 OUT_BYTE(drive->ctl,IDE_CONTROL_REG); /* enable device irq */
164 delay_50ms(); /* take a deep breath */
165 a = IN_BYTE(IDE_ALTSTATUS_REG);
166 s = IN_BYTE(IDE_STATUS_REG);
167 if ((a ^ s) & ~INDEX_STAT) {
168 printk("%s: probing with STATUS(0x%02x) instead of ALTSTATUS(0x%02x)\n", drive->name, s, a);
169 hd_status = IDE_STATUS_REG; /* ancient Seagate drives, broken interfaces */
170 } else
171 hd_status = IDE_ALTSTATUS_REG; /* use non-intrusive polling */
173 #if CONFIG_BLK_DEV_PDC4030
174 if (IS_PDC4030_DRIVE) {
175 extern int pdc4030_cmd(ide_drive_t *, byte);
176 if (pdc4030_cmd(drive,PROMISE_IDENTIFY)) {
177 if (irqs)
178 (void) probe_irq_off(irqs);
179 return 1;
181 } else
182 #endif /* CONFIG_BLK_DEV_PDC4030 */
183 OUT_BYTE(cmd,IDE_COMMAND_REG); /* ask drive for ID */
184 timeout = ((cmd == WIN_IDENTIFY) ? WAIT_WORSTCASE : WAIT_PIDENTIFY) / 2;
185 timeout += jiffies;
186 do {
187 if (0 < (signed long)(jiffies - timeout)) {
188 if (irqs)
189 (void) probe_irq_off(irqs);
190 return 1; /* drive timed-out */
192 delay_50ms(); /* give drive a breather */
193 } while (IN_BYTE(hd_status) & BUSY_STAT);
195 delay_50ms(); /* wait for IRQ and DRQ_STAT */
196 if (OK_STAT(GET_STAT(),DRQ_STAT,BAD_R_STAT)) {
197 unsigned long flags;
198 __save_flags(flags); /* local CPU only */
199 __cli(); /* local CPU only; some systems need this */
200 do_identify(drive, cmd); /* drive returned ID */
201 rc = 0; /* drive responded with ID */
202 (void) GET_STAT(); /* clear drive IRQ */
203 __restore_flags(flags); /* local CPU only */
204 } else
205 rc = 2; /* drive refused ID */
206 if (!HWIF(drive)->irq) {
207 irqs = probe_irq_off(irqs); /* get our irq number */
208 if (irqs > 0) {
209 HWIF(drive)->irq = irqs; /* save it for later */
210 irqs = probe_irq_on();
211 OUT_BYTE(drive->ctl|2,IDE_CONTROL_REG); /* mask device irq */
212 udelay(5);
213 (void) probe_irq_off(irqs);
214 (void) probe_irq_off(probe_irq_on()); /* clear self-inflicted irq */
215 (void) GET_STAT(); /* clear drive IRQ */
217 } else { /* Mmmm.. multiple IRQs.. don't know which was ours */
218 printk("%s: IRQ probe failed (%ld)\n", drive->name, irqs);
219 #ifdef CONFIG_BLK_DEV_CMD640
220 #ifdef CMD640_DUMP_REGS
221 if (HWIF(drive)->chipset == ide_cmd640) {
222 printk("%s: Hmmm.. probably a driver problem.\n", drive->name);
223 CMD640_DUMP_REGS;
225 #endif /* CMD640_DUMP_REGS */
226 #endif /* CONFIG_BLK_DEV_CMD640 */
229 return rc;
233 * do_probe() has the difficult job of finding a drive if it exists,
234 * without getting hung up if it doesn't exist, without trampling on
235 * ethernet cards, and without leaving any IRQs dangling to haunt us later.
237 * If a drive is "known" to exist (from CMOS or kernel parameters),
238 * but does not respond right away, the probe will "hang in there"
239 * for the maximum wait time (about 30 seconds), otherwise it will
240 * exit much more quickly.
242 * Returns: 0 device was identified
243 * 1 device timed-out (no response to identify request)
244 * 2 device aborted the command (refused to identify itself)
245 * 3 bad status from device (possible for ATAPI drives)
246 * 4 probe was not attempted because failure was obvious
248 static int do_probe (ide_drive_t *drive, byte cmd)
250 int rc;
251 ide_hwif_t *hwif = HWIF(drive);
252 if (drive->present) { /* avoid waiting for inappropriate probes */
253 if ((drive->media != ide_disk) && (cmd == WIN_IDENTIFY))
254 return 4;
256 #ifdef DEBUG
257 printk("probing for %s: present=%d, media=%d, probetype=%s\n",
258 drive->name, drive->present, drive->media,
259 (cmd == WIN_IDENTIFY) ? "ATA" : "ATAPI");
260 #endif
261 SELECT_DRIVE(hwif,drive);
262 delay_50ms();
263 if (IN_BYTE(IDE_SELECT_REG) != drive->select.all && !drive->present) {
264 if (drive->select.b.unit != 0) {
265 SELECT_DRIVE(hwif,&hwif->drives[0]); /* exit with drive0 selected */
266 delay_50ms(); /* allow BUSY_STAT to assert & clear */
268 return 3; /* no i/f present: mmm.. this should be a 4 -ml */
271 if (OK_STAT(GET_STAT(),READY_STAT,BUSY_STAT)
272 || drive->present || cmd == WIN_PIDENTIFY)
274 if ((rc = try_to_identify(drive,cmd))) /* send cmd and wait */
275 rc = try_to_identify(drive,cmd); /* failed: try again */
276 if (rc == 1)
277 printk("%s: no response (status = 0x%02x)\n", drive->name, GET_STAT());
278 (void) GET_STAT(); /* ensure drive irq is clear */
279 } else {
280 rc = 3; /* not present or maybe ATAPI */
282 if (drive->select.b.unit != 0) {
283 SELECT_DRIVE(hwif,&hwif->drives[0]); /* exit with drive0 selected */
284 delay_50ms();
285 (void) GET_STAT(); /* ensure drive irq is clear */
287 return rc;
291 * probe_for_drive() tests for existence of a given drive using do_probe().
293 * Returns: 0 no device was found
294 * 1 device was found (note: drive->present might still be 0)
296 static inline byte probe_for_drive (ide_drive_t *drive)
298 if (drive->noprobe) /* skip probing? */
299 return drive->present;
300 if (do_probe(drive, WIN_IDENTIFY) >= 2) { /* if !(success||timed-out) */
301 (void) do_probe(drive, WIN_PIDENTIFY); /* look for ATAPI device */
303 if (!drive->present)
304 return 0; /* drive not found */
305 if (drive->id == NULL) { /* identification failed? */
306 if (drive->media == ide_disk) {
307 printk ("%s: non-IDE drive, CHS=%d/%d/%d\n",
308 drive->name, drive->cyl, drive->head, drive->sect);
309 } else if (drive->media == ide_cdrom) {
310 printk("%s: ATAPI cdrom (?)\n", drive->name);
311 } else {
312 drive->present = 0; /* nuke it */
315 return 1; /* drive was found */
319 * We query CMOS about hard disks : it could be that we have a SCSI/ESDI/etc
320 * controller that is BIOS compatible with ST-506, and thus showing up in our
321 * BIOS table, but not register compatible, and therefore not present in CMOS.
323 * Furthermore, we will assume that our ST-506 drives <if any> are the primary
324 * drives in the system -- the ones reflected as drive 1 or 2. The first
325 * drive is stored in the high nibble of CMOS byte 0x12, the second in the low
326 * nibble. This will be either a 4 bit drive type or 0xf indicating use byte
327 * 0x19 for an 8 bit type, drive 1, 0x1a for drive 2 in CMOS. A non-zero value
328 * means we have an AT controller hard disk for that drive.
330 * Of course, there is no guarantee that either drive is actually on the
331 * "primary" IDE interface, but we don't bother trying to sort that out here.
332 * If a drive is not actually on the primary interface, then these parameters
333 * will be ignored. This results in the user having to supply the logical
334 * drive geometry as a boot parameter for each drive not on the primary i/f.
336 * The only "perfect" way to handle this would be to modify the setup.[cS] code
337 * to do BIOS calls Int13h/Fn08h and Int13h/Fn48h to get all of the drive info
338 * for us during initialization. I have the necessary docs -- any takers? -ml
340 static void probe_cmos_for_drives (ide_hwif_t *hwif)
342 #ifdef __i386__
343 extern struct drive_info_struct drive_info;
344 byte cmos_disks, *BIOS = (byte *) &drive_info;
345 int unit;
347 #ifdef CONFIG_BLK_DEV_PDC4030
348 if (hwif->chipset == ide_pdc4030 && hwif->channel != 0)
349 return;
350 #endif /* CONFIG_BLK_DEV_PDC4030 */
351 outb_p(0x12,0x70); /* specify CMOS address 0x12 */
352 cmos_disks = inb_p(0x71); /* read the data from 0x12 */
353 /* Extract drive geometry from CMOS+BIOS if not already setup */
354 for (unit = 0; unit < MAX_DRIVES; ++unit) {
355 ide_drive_t *drive = &hwif->drives[unit];
356 if ((cmos_disks & (0xf0 >> (unit*4))) && !drive->present && !drive->nobios) {
357 drive->cyl = drive->bios_cyl = *(unsigned short *)BIOS;
358 drive->head = drive->bios_head = *(BIOS+2);
359 drive->sect = drive->bios_sect = *(BIOS+14);
360 drive->ctl = *(BIOS+8);
361 drive->present = 1;
363 BIOS += 16;
365 #endif
369 * This routine only knows how to look for drive units 0 and 1
370 * on an interface, so any setting of MAX_DRIVES > 2 won't work here.
372 static void probe_hwif (ide_hwif_t *hwif)
374 unsigned int unit;
375 unsigned long flags;
377 if (hwif->noprobe)
378 return;
379 if (hwif->io_ports[IDE_DATA_OFFSET] == HD_DATA)
380 probe_cmos_for_drives (hwif);
381 if ((hwif->chipset != ide_4drives || !hwif->mate->present)
382 #if CONFIG_BLK_DEV_PDC4030
383 && (hwif->chipset != ide_pdc4030 || hwif->channel == 0)
384 #endif /* CONFIG_BLK_DEV_PDC4030 */
385 && (ide_check_region(hwif->io_ports[IDE_DATA_OFFSET],8) || ide_check_region(hwif->io_ports[IDE_CONTROL_OFFSET],1)))
387 int msgout = 0;
388 for (unit = 0; unit < MAX_DRIVES; ++unit) {
389 ide_drive_t *drive = &hwif->drives[unit];
390 if (drive->present) {
391 drive->present = 0;
392 printk("%s: ERROR, PORTS ALREADY IN USE\n", drive->name);
393 msgout = 1;
396 if (!msgout)
397 printk("%s: ports already in use, skipping probe\n", hwif->name);
398 return;
401 __save_flags(flags); /* local CPU only */
402 __sti(); /* local CPU only; needed for jiffies and irq probing */
404 * Second drive should only exist if first drive was found,
405 * but a lot of cdrom drives are configured as single slaves.
407 for (unit = 0; unit < MAX_DRIVES; ++unit) {
408 ide_drive_t *drive = &hwif->drives[unit];
409 (void) probe_for_drive (drive);
410 if (drive->present && !hwif->present) {
411 hwif->present = 1;
412 if (hwif->chipset != ide_4drives || !hwif->mate->present) {
413 ide_request_region(hwif->io_ports[IDE_DATA_OFFSET], 8, hwif->name);
414 ide_request_region(hwif->io_ports[IDE_CONTROL_OFFSET], 1, hwif->name);
418 if (hwif->reset) {
419 unsigned long timeout = jiffies + WAIT_WORSTCASE;
420 byte stat;
422 printk("%s: reset\n", hwif->name);
423 OUT_BYTE(12, hwif->io_ports[IDE_CONTROL_OFFSET]);
424 udelay(10);
425 OUT_BYTE(8, hwif->io_ports[IDE_CONTROL_OFFSET]);
426 do {
427 delay_50ms();
428 stat = IN_BYTE(hwif->io_ports[IDE_STATUS_OFFSET]);
429 } while ((stat & BUSY_STAT) && 0 < (signed long)(timeout - jiffies));
432 __restore_flags(flags); /* local CPU only */
433 for (unit = 0; unit < MAX_DRIVES; ++unit) {
434 ide_drive_t *drive = &hwif->drives[unit];
435 if (drive->present) {
436 ide_tuneproc_t *tuneproc = HWIF(drive)->tuneproc;
437 if (tuneproc != NULL && drive->autotune == 1)
438 tuneproc(drive, 255); /* auto-tune PIO mode */
443 #if MAX_HWIFS > 1
445 * save_match() is used to simplify logic in init_irq() below.
447 * A loophole here is that we may not know about a particular
448 * hwif's irq until after that hwif is actually probed/initialized..
449 * This could be a problem for the case where an hwif is on a
450 * dual interface that requires serialization (eg. cmd640) and another
451 * hwif using one of the same irqs is initialized beforehand.
453 * This routine detects and reports such situations, but does not fix them.
455 static void save_match (ide_hwif_t *hwif, ide_hwif_t *new, ide_hwif_t **match)
457 ide_hwif_t *m = *match;
459 if (m && m->hwgroup && m->hwgroup != new->hwgroup) {
460 if (!new->hwgroup)
461 return;
462 printk("%s: potential irq problem with %s and %s\n", hwif->name, new->name, m->name);
464 if (!m || m->irq != hwif->irq) /* don't undo a prior perfect match */
465 *match = new;
467 #endif /* MAX_HWIFS > 1 */
470 * This routine sets up the irq for an ide interface, and creates a new
471 * hwgroup for the irq/hwif if none was previously assigned.
473 * Much of the code is for correctly detecting/handling irq sharing
474 * and irq serialization situations. This is somewhat complex because
475 * it handles static as well as dynamic (PCMCIA) IDE interfaces.
477 * The SA_INTERRUPT in sa_flags means ide_intr() is always entered with
478 * interrupts completely disabled. This can be bad for interrupt latency,
479 * but anything else has led to problems on some machines. We re-enable
480 * interrupts as much as we can safely do in most places.
482 static int init_irq (ide_hwif_t *hwif)
484 unsigned long flags;
485 unsigned int index;
486 ide_hwgroup_t *hwgroup;
487 ide_hwif_t *match = NULL;
489 save_flags(flags); /* all CPUs */
490 cli(); /* all CPUs */
492 hwif->hwgroup = NULL;
493 #if MAX_HWIFS > 1
495 * Group up with any other hwifs that share our irq(s).
497 for (index = 0; index < MAX_HWIFS; index++) {
498 ide_hwif_t *h = &ide_hwifs[index];
499 if (h->hwgroup) { /* scan only initialized hwif's */
500 if (hwif->irq == h->irq) {
501 hwif->sharing_irq = h->sharing_irq = 1;
502 if (hwif->chipset != ide_pci || h->chipset != ide_pci) {
503 save_match(hwif, h, &match);
506 if (hwif->serialized) {
507 if (hwif->mate && hwif->mate->irq == h->irq)
508 save_match(hwif, h, &match);
510 if (h->serialized) {
511 if (h->mate && hwif->irq == h->mate->irq)
512 save_match(hwif, h, &match);
516 #endif /* MAX_HWIFS > 1 */
518 * If we are still without a hwgroup, then form a new one
520 if (match) {
521 hwgroup = match->hwgroup;
522 } else {
523 hwgroup = kmalloc(sizeof(ide_hwgroup_t), GFP_KERNEL);
524 memset(hwgroup, 0, sizeof(ide_hwgroup_t));
525 hwgroup->hwif = hwif->next = hwif;
526 hwgroup->rq = NULL;
527 hwgroup->handler = NULL;
528 hwgroup->drive = NULL;
529 hwgroup->busy = 0;
530 hwgroup->spinlock = (spinlock_t)SPIN_LOCK_UNLOCKED;
531 #if (DEBUG_SPINLOCK > 0)
532 printk("hwgroup(%s) spinlock is %p\n", hwif->name, &hwgroup->spinlock); /* FIXME */
533 #endif
534 init_timer(&hwgroup->timer);
535 hwgroup->timer.function = &ide_timer_expiry;
536 hwgroup->timer.data = (unsigned long) hwgroup;
540 * Allocate the irq, if not already obtained for another hwif
542 if (!match || match->irq != hwif->irq) {
543 int sa = (hwif->chipset == ide_pci) ? SA_INTERRUPT|SA_SHIRQ : SA_INTERRUPT;
544 if (ide_request_irq(hwif->irq, &ide_intr, sa, hwif->name, hwgroup)) {
545 if (!match)
546 kfree(hwgroup);
547 restore_flags(flags); /* all CPUs */
548 return 1;
553 * Everything is okay, so link us into the hwgroup
555 hwif->hwgroup = hwgroup;
556 hwif->next = hwgroup->hwif->next;
557 hwgroup->hwif->next = hwif;
559 for (index = 0; index < MAX_DRIVES; ++index) {
560 ide_drive_t *drive = &hwif->drives[index];
561 if (!drive->present)
562 continue;
563 if (!hwgroup->drive)
564 hwgroup->drive = drive;
565 drive->next = hwgroup->drive->next;
566 hwgroup->drive->next = drive;
568 hwgroup->hwif = HWIF(hwgroup->drive);
569 restore_flags(flags); /* all CPUs; safe now that hwif->hwgroup is set up */
571 #if !defined(__mc68000__) && !defined(CONFIG_APUS) && !defined(__sparc__)
572 printk("%s at 0x%03x-0x%03x,0x%03x on irq %d", hwif->name,
573 hwif->io_ports[IDE_DATA_OFFSET],
574 hwif->io_ports[IDE_DATA_OFFSET]+7,
575 hwif->io_ports[IDE_CONTROL_OFFSET], hwif->irq);
576 #elif defined(__sparc__)
577 printk("%s at 0x%03x-0x%03x,0x%03x on irq %s", hwif->name,
578 hwif->io_ports[IDE_DATA_OFFSET],
579 hwif->io_ports[IDE_DATA_OFFSET]+7,
580 hwif->io_ports[IDE_CONTROL_OFFSET], __irq_itoa(hwif->irq));
581 #else
582 printk("%s at %p on irq 0x%08x", hwif->name,
583 hwif->io_ports[IDE_DATA_OFFSET], hwif->irq);
584 #endif /* __mc68000__ && CONFIG_APUS */
585 if (match)
586 printk(" (%sed with %s)",
587 hwif->sharing_irq ? "shar" : "serializ", match->name);
588 printk("\n");
589 return 0;
593 * init_gendisk() (as opposed to ide_geninit) is called for each major device,
594 * after probing for drives, to allocate partition tables and other data
595 * structures needed for the routines in genhd.c. ide_geninit() gets called
596 * somewhat later, during the partition check.
598 static void init_gendisk (ide_hwif_t *hwif)
600 struct gendisk *gd, **gdp;
601 unsigned int unit, units, minors;
602 int *bs, *max_sect, *max_ra;
604 /* figure out maximum drive number on the interface */
605 for (units = MAX_DRIVES; units > 0; --units) {
606 if (hwif->drives[units-1].present)
607 break;
609 minors = units * (1<<PARTN_BITS);
610 gd = kmalloc (sizeof(struct gendisk), GFP_KERNEL);
611 gd->sizes = kmalloc (minors * sizeof(int), GFP_KERNEL);
612 gd->part = kmalloc (minors * sizeof(struct hd_struct), GFP_KERNEL);
613 bs = kmalloc (minors*sizeof(int), GFP_KERNEL);
614 max_sect = kmalloc (minors*sizeof(int), GFP_KERNEL);
615 max_ra = kmalloc (minors*sizeof(int), GFP_KERNEL);
617 memset(gd->part, 0, minors * sizeof(struct hd_struct));
619 /* cdroms and msdos f/s are examples of non-1024 blocksizes */
620 blksize_size[hwif->major] = bs;
621 max_sectors[hwif->major] = max_sect;
622 max_readahead[hwif->major] = max_ra;
623 for (unit = 0; unit < minors; ++unit) {
624 *bs++ = BLOCK_SIZE;
625 *max_sect++ = MAX_SECTORS;
626 *max_ra++ = MAX_READAHEAD;
629 for (unit = 0; unit < units; ++unit)
630 hwif->drives[unit].part = &gd->part[unit << PARTN_BITS];
632 gd->major = hwif->major; /* our major device number */
633 gd->major_name = IDE_MAJOR_NAME; /* treated special in genhd.c */
634 gd->minor_shift = PARTN_BITS; /* num bits for partitions */
635 gd->max_p = 1<<PARTN_BITS; /* 1 + max partitions / drive */
636 gd->max_nr = units; /* max num real drives */
637 gd->nr_real = units; /* current num real drives */
638 gd->init = &ide_geninit; /* initialization function */
639 gd->real_devices= hwif; /* ptr to internal data */
640 gd->next = NULL; /* linked list of major devs */
642 for (gdp = &gendisk_head; *gdp; gdp = &((*gdp)->next)) ;
643 hwif->gd = *gdp = gd; /* link onto tail of list */
645 for (unit = 0; unit < units; ++unit) {
646 if (hwif->drives[unit].present)
647 ide_add_generic_settings(hwif->drives + unit);
651 static int hwif_init (ide_hwif_t *hwif)
653 void (*rfn)(void);
655 if (!hwif->present)
656 return 0;
657 if (!hwif->irq) {
658 if (!(hwif->irq = ide_default_irq(hwif->io_ports[IDE_DATA_OFFSET]))) {
659 printk("%s: DISABLED, NO IRQ\n", hwif->name);
660 return (hwif->present = 0);
663 #ifdef CONFIG_BLK_DEV_HD
664 if (hwif->irq == HD_IRQ && hwif->io_ports[IDE_DATA_OFFSET] != HD_DATA) {
665 printk("%s: CANNOT SHARE IRQ WITH OLD HARDDISK DRIVER (hd.c)\n", hwif->name);
666 return (hwif->present = 0);
668 #endif /* CONFIG_BLK_DEV_HD */
670 hwif->present = 0; /* we set it back to 1 if all is ok below */
671 switch (hwif->major) {
672 case IDE0_MAJOR: rfn = &do_ide0_request; break;
673 #if MAX_HWIFS > 1
674 case IDE1_MAJOR: rfn = &do_ide1_request; break;
675 #endif
676 #if MAX_HWIFS > 2
677 case IDE2_MAJOR: rfn = &do_ide2_request; break;
678 #endif
679 #if MAX_HWIFS > 3
680 case IDE3_MAJOR: rfn = &do_ide3_request; break;
681 #endif
682 #if MAX_HWIFS > 4
683 case IDE4_MAJOR: rfn = &do_ide4_request; break;
684 #endif
685 #if MAX_HWIFS > 5
686 case IDE5_MAJOR: rfn = &do_ide5_request; break;
687 #endif
688 default:
689 printk("%s: request_fn NOT DEFINED\n", hwif->name);
690 return (hwif->present = 0);
692 if (register_blkdev (hwif->major, hwif->name, ide_fops)) {
693 printk("%s: UNABLE TO GET MAJOR NUMBER %d\n", hwif->name, hwif->major);
694 } else if (init_irq (hwif)) {
695 printk("%s: UNABLE TO GET IRQ %d\n", hwif->name, hwif->irq);
696 (void) unregister_blkdev (hwif->major, hwif->name);
697 } else {
698 init_gendisk(hwif);
699 blk_dev[hwif->major].data = hwif;
700 blk_dev[hwif->major].request_fn = rfn;
701 blk_dev[hwif->major].queue = ide_get_queue;
702 read_ahead[hwif->major] = 8; /* (4kB) */
703 hwif->present = 1; /* success */
705 #if (DEBUG_SPINLOCK > 0)
707 static int done = 0;
708 if (!done++)
709 printk("io_request_lock is %p\n", &io_request_lock); /* FIXME */
711 #endif
712 return hwif->present;
715 int ideprobe_init (void);
716 static ide_module_t ideprobe_module = {
717 IDE_PROBE_MODULE,
718 ideprobe_init,
719 NULL
722 int ideprobe_init (void)
724 unsigned int index;
725 int probe[MAX_HWIFS];
727 MOD_INC_USE_COUNT;
728 memset(probe, 0, MAX_HWIFS * sizeof(int));
729 for (index = 0; index < MAX_HWIFS; ++index)
730 probe[index] = !ide_hwifs[index].present;
733 * Probe for drives in the usual way.. CMOS/BIOS, then poke at ports
735 for (index = 0; index < MAX_HWIFS; ++index)
736 if (probe[index])
737 probe_hwif(&ide_hwifs[index]);
738 for (index = 0; index < MAX_HWIFS; ++index)
739 if (probe[index])
740 hwif_init(&ide_hwifs[index]);
741 ide_register_module(&ideprobe_module);
742 MOD_DEC_USE_COUNT;
743 return 0;
746 #ifdef MODULE
747 int init_module (void)
749 unsigned int index;
751 for (index = 0; index < MAX_HWIFS; ++index)
752 ide_unregister(index);
753 return ideprobe_init();
756 void cleanup_module (void)
758 ide_unregister_module(&ideprobe_module);
760 #endif /* MODULE */