Import 2.3.15pre2
[davej-history.git] / drivers / block / ide-probe.c
blob07cc729654937329d0813f4f703b50659f3f7754
1 /*
2 * linux/drivers/block/ide-probe.c Version 1.05 July 3, 1999
4 * Copyright (C) 1994-1998 Linus Torvalds & authors (see below)
5 */
7 /*
8 * Mostly written by Mark Lord <mlord@pobox.com>
9 * and Gadi Oxman <gadio@netvision.net.il>
11 * See linux/MAINTAINERS for address of current maintainer.
13 * This is the IDE probe module, as evolved from hd.c and ide.c.
15 * Version 1.00 move drive probing code from ide.c to ide-probe.c
16 * Version 1.01 fix compilation problem for m68k
17 * Version 1.02 increase WAIT_PIDENTIFY to avoid CD-ROM locking at boot
18 * by Andrea Arcangeli
19 * Version 1.03 fix for (hwif->chipset == ide_4drives)
20 * Version 1.04 fixed buggy treatments of known flash memory cards
22 * Version 1.05 fix for (hwif->chipset == ide_pdc4030)
23 * added ide6/7/8/9
24 * allowed for secondary flash card to be detectable
25 * with new flag : drive->ata_flash : 1;
28 #undef REALLY_SLOW_IO /* most systems can safely undef this */
30 #include <linux/config.h>
31 #include <linux/module.h>
32 #include <linux/types.h>
33 #include <linux/string.h>
34 #include <linux/kernel.h>
35 #include <linux/timer.h>
36 #include <linux/mm.h>
37 #include <linux/interrupt.h>
38 #include <linux/major.h>
39 #include <linux/errno.h>
40 #include <linux/genhd.h>
41 #include <linux/malloc.h>
42 #include <linux/delay.h>
43 #include <linux/ide.h>
46 #include <asm/byteorder.h>
47 #include <asm/irq.h>
48 #include <asm/uaccess.h>
49 #include <asm/io.h>
51 static inline void do_identify (ide_drive_t *drive, byte cmd)
53 int bswap = 1;
54 struct hd_driveid *id;
56 id = drive->id = kmalloc (SECTOR_WORDS*4, GFP_ATOMIC); /* called with interrupts disabled! */
57 ide_input_data(drive, id, SECTOR_WORDS); /* read 512 bytes of id info */
58 ide__sti(); /* local CPU only */
59 ide_fix_driveid(id);
61 #if defined (CONFIG_SCSI_EATA_DMA) || defined (CONFIG_SCSI_EATA_PIO) || defined (CONFIG_SCSI_EATA)
63 * EATA SCSI controllers do a hardware ATA emulation:
64 * Ignore them if there is a driver for them available.
66 if ((id->model[0] == 'P' && id->model[1] == 'M')
67 || (id->model[0] == 'S' && id->model[1] == 'K')) {
68 printk("%s: EATA SCSI HBA %.10s\n", drive->name, id->model);
69 drive->present = 0;
70 return;
72 #endif /* CONFIG_SCSI_EATA_DMA || CONFIG_SCSI_EATA_PIO */
75 * WIN_IDENTIFY returns little-endian info,
76 * WIN_PIDENTIFY *usually* returns little-endian info.
78 if (cmd == WIN_PIDENTIFY) {
79 if ((id->model[0] == 'N' && id->model[1] == 'E') /* NEC */
80 || (id->model[0] == 'F' && id->model[1] == 'X') /* Mitsumi */
81 || (id->model[0] == 'P' && id->model[1] == 'i'))/* Pioneer */
82 bswap ^= 1; /* Vertos drives may still be weird */
84 ide_fixstring (id->model, sizeof(id->model), bswap);
85 ide_fixstring (id->fw_rev, sizeof(id->fw_rev), bswap);
86 ide_fixstring (id->serial_no, sizeof(id->serial_no), bswap);
88 id->model[sizeof(id->model)-1] = '\0'; /* we depend on this a lot! */
89 printk("%s: %s, ", drive->name, id->model);
90 drive->present = 1;
93 * Check for an ATAPI device
95 if (cmd == WIN_PIDENTIFY) {
96 byte type = (id->config >> 8) & 0x1f;
97 printk("ATAPI ");
98 #ifdef CONFIG_BLK_DEV_PDC4030
99 if (HWIF(drive)->channel == 1 && HWIF(drive)->chipset == ide_pdc4030) {
100 printk(" -- not supported on 2nd Promise port\n");
101 drive->present = 0;
102 return;
104 #endif /* CONFIG_BLK_DEV_PDC4030 */
105 switch (type) {
106 case ide_floppy:
107 if (!strstr(id->model, "CD-ROM")) {
108 if (!strstr(id->model, "oppy") && !strstr(id->model, "poyp") && !strstr(id->model, "ZIP"))
109 printk("cdrom or floppy?, assuming ");
110 if (drive->media != ide_cdrom) {
111 printk ("FLOPPY");
112 break;
115 type = ide_cdrom; /* Early cdrom models used zero */
116 case ide_cdrom:
117 printk ("CDROM");
118 drive->removable = 1;
119 break;
120 case ide_tape:
121 printk ("TAPE");
122 break;
123 case ide_optical:
124 printk ("OPTICAL");
125 drive->removable = 1;
126 break;
127 default:
128 printk("UNKNOWN (type %d)", type);
129 break;
131 printk (" drive\n");
132 drive->media = type;
133 return;
137 * Not an ATAPI device: looks like a "regular" hard disk
139 if (id->config & (1<<7))
140 drive->removable = 1;
142 * Prevent long system lockup probing later for non-existant
143 * slave drive if the hwif is actually a flash memory card of some variety:
145 if (drive_is_flashcard(drive)) {
146 ide_drive_t *mate = &HWIF(drive)->drives[1^drive->select.b.unit];
147 if (!mate->ata_flash) {
148 mate->present = 0;
149 mate->noprobe = 1;
152 drive->media = ide_disk;
153 printk("ATA DISK drive\n");
154 return;
158 * try_to_identify() sends an ATA(PI) IDENTIFY request to a drive
159 * and waits for a response. It also monitors irqs while this is
160 * happening, in hope of automatically determining which one is
161 * being used by the interface.
163 * Returns: 0 device was identified
164 * 1 device timed-out (no response to identify request)
165 * 2 device aborted the command (refused to identify itself)
167 static int try_to_identify (ide_drive_t *drive, byte cmd)
169 int rc;
170 ide_ioreg_t hd_status;
171 unsigned long timeout;
172 unsigned long irqs = 0;
173 byte s, a;
175 if (IDE_CONTROL_REG) {
176 if (!HWIF(drive)->irq) { /* already got an IRQ? */
177 probe_irq_off(probe_irq_on()); /* clear dangling irqs */
178 irqs = probe_irq_on(); /* start monitoring irqs */
179 OUT_BYTE(drive->ctl,IDE_CONTROL_REG); /* enable device irq */
182 ide_delay_50ms(); /* take a deep breath */
183 a = IN_BYTE(IDE_ALTSTATUS_REG);
184 s = IN_BYTE(IDE_STATUS_REG);
185 if ((a ^ s) & ~INDEX_STAT) {
186 printk("%s: probing with STATUS(0x%02x) instead of ALTSTATUS(0x%02x)\n", drive->name, s, a);
187 hd_status = IDE_STATUS_REG; /* ancient Seagate drives, broken interfaces */
188 } else {
189 hd_status = IDE_ALTSTATUS_REG; /* use non-intrusive polling */
191 } else {
192 ide_delay_50ms();
193 hd_status = IDE_STATUS_REG;
196 #if CONFIG_BLK_DEV_PDC4030
197 if (HWIF(drive)->chipset == ide_pdc4030) {
198 /* DC4030 hosted drives need their own identify... */
199 extern int pdc4030_identify(ide_drive_t *);
200 if (pdc4030_identify(drive)) {
201 if (irqs)
202 (void) probe_irq_off(irqs);
203 return 1;
205 } else
206 #endif /* CONFIG_BLK_DEV_PDC4030 */
207 OUT_BYTE(cmd,IDE_COMMAND_REG); /* ask drive for ID */
208 timeout = ((cmd == WIN_IDENTIFY) ? WAIT_WORSTCASE : WAIT_PIDENTIFY) / 2;
209 timeout += jiffies;
210 do {
211 if (0 < (signed long)(jiffies - timeout)) {
212 if (irqs)
213 (void) probe_irq_off(irqs);
214 return 1; /* drive timed-out */
216 ide_delay_50ms(); /* give drive a breather */
217 } while (IN_BYTE(hd_status) & BUSY_STAT);
219 ide_delay_50ms(); /* wait for IRQ and DRQ_STAT */
220 if (OK_STAT(GET_STAT(),DRQ_STAT,BAD_R_STAT)) {
221 unsigned long flags;
222 __save_flags(flags); /* local CPU only */
223 __cli(); /* local CPU only; some systems need this */
224 do_identify(drive, cmd); /* drive returned ID */
225 rc = 0; /* drive responded with ID */
226 (void) GET_STAT(); /* clear drive IRQ */
227 __restore_flags(flags); /* local CPU only */
228 } else
229 rc = 2; /* drive refused ID */
230 if (IDE_CONTROL_REG && !HWIF(drive)->irq) {
231 irqs = probe_irq_off(irqs); /* get our irq number */
232 if (irqs > 0) {
233 HWIF(drive)->irq = irqs; /* save it for later */
234 irqs = probe_irq_on();
235 OUT_BYTE(drive->ctl|2,IDE_CONTROL_REG); /* mask device irq */
236 udelay(5);
237 (void) probe_irq_off(irqs);
238 (void) probe_irq_off(probe_irq_on()); /* clear self-inflicted irq */
239 (void) GET_STAT(); /* clear drive IRQ */
241 } else { /* Mmmm.. multiple IRQs.. don't know which was ours */
242 printk("%s: IRQ probe failed (%ld)\n", drive->name, irqs);
243 #ifdef CONFIG_BLK_DEV_CMD640
244 #ifdef CMD640_DUMP_REGS
245 if (HWIF(drive)->chipset == ide_cmd640) {
246 printk("%s: Hmmm.. probably a driver problem.\n", drive->name);
247 CMD640_DUMP_REGS;
249 #endif /* CMD640_DUMP_REGS */
250 #endif /* CONFIG_BLK_DEV_CMD640 */
253 return rc;
257 * do_probe() has the difficult job of finding a drive if it exists,
258 * without getting hung up if it doesn't exist, without trampling on
259 * ethernet cards, and without leaving any IRQs dangling to haunt us later.
261 * If a drive is "known" to exist (from CMOS or kernel parameters),
262 * but does not respond right away, the probe will "hang in there"
263 * for the maximum wait time (about 30 seconds), otherwise it will
264 * exit much more quickly.
266 * Returns: 0 device was identified
267 * 1 device timed-out (no response to identify request)
268 * 2 device aborted the command (refused to identify itself)
269 * 3 bad status from device (possible for ATAPI drives)
270 * 4 probe was not attempted because failure was obvious
272 static int do_probe (ide_drive_t *drive, byte cmd)
274 int rc;
275 ide_hwif_t *hwif = HWIF(drive);
276 if (drive->present) { /* avoid waiting for inappropriate probes */
277 if ((drive->media != ide_disk) && (cmd == WIN_IDENTIFY))
278 return 4;
280 #ifdef DEBUG
281 printk("probing for %s: present=%d, media=%d, probetype=%s\n",
282 drive->name, drive->present, drive->media,
283 (cmd == WIN_IDENTIFY) ? "ATA" : "ATAPI");
284 #endif
285 SELECT_DRIVE(hwif,drive);
286 ide_delay_50ms();
287 if (IN_BYTE(IDE_SELECT_REG) != drive->select.all && !drive->present) {
288 if (drive->select.b.unit != 0) {
289 SELECT_DRIVE(hwif,&hwif->drives[0]); /* exit with drive0 selected */
290 ide_delay_50ms(); /* allow BUSY_STAT to assert & clear */
292 return 3; /* no i/f present: mmm.. this should be a 4 -ml */
295 if (OK_STAT(GET_STAT(),READY_STAT,BUSY_STAT)
296 || drive->present || cmd == WIN_PIDENTIFY)
298 if ((rc = try_to_identify(drive,cmd))) /* send cmd and wait */
299 rc = try_to_identify(drive,cmd); /* failed: try again */
300 if (rc == 1 && cmd == WIN_PIDENTIFY && drive->autotune != 2) {
301 unsigned long timeout;
302 printk("%s: no response (status = 0x%02x), resetting drive\n", drive->name, GET_STAT());
303 ide_delay_50ms();
304 OUT_BYTE (drive->select.all, IDE_SELECT_REG);
305 ide_delay_50ms();
306 OUT_BYTE(WIN_SRST, IDE_COMMAND_REG);
307 timeout = jiffies;
308 while ((GET_STAT() & BUSY_STAT) && time_before(jiffies, timeout + WAIT_WORSTCASE))
309 ide_delay_50ms();
310 rc = try_to_identify(drive, cmd);
312 if (rc == 1)
313 printk("%s: no response (status = 0x%02x)\n", drive->name, GET_STAT());
314 (void) GET_STAT(); /* ensure drive irq is clear */
315 } else {
316 rc = 3; /* not present or maybe ATAPI */
318 if (drive->select.b.unit != 0) {
319 SELECT_DRIVE(hwif,&hwif->drives[0]); /* exit with drive0 selected */
320 ide_delay_50ms();
321 (void) GET_STAT(); /* ensure drive irq is clear */
323 return rc;
327 * probe_for_drive() tests for existence of a given drive using do_probe().
329 * Returns: 0 no device was found
330 * 1 device was found (note: drive->present might still be 0)
332 static inline byte probe_for_drive (ide_drive_t *drive)
334 if (drive->noprobe) /* skip probing? */
335 return drive->present;
336 if (do_probe(drive, WIN_IDENTIFY) >= 2) { /* if !(success||timed-out) */
337 (void) do_probe(drive, WIN_PIDENTIFY); /* look for ATAPI device */
339 if (!drive->present)
340 return 0; /* drive not found */
341 if (drive->id == NULL) { /* identification failed? */
342 if (drive->media == ide_disk) {
343 printk ("%s: non-IDE drive, CHS=%d/%d/%d\n",
344 drive->name, drive->cyl, drive->head, drive->sect);
345 } else if (drive->media == ide_cdrom) {
346 printk("%s: ATAPI cdrom (?)\n", drive->name);
347 } else {
348 drive->present = 0; /* nuke it */
351 return 1; /* drive was found */
355 * We query CMOS about hard disks : it could be that we have a SCSI/ESDI/etc
356 * controller that is BIOS compatible with ST-506, and thus showing up in our
357 * BIOS table, but not register compatible, and therefore not present in CMOS.
359 * Furthermore, we will assume that our ST-506 drives <if any> are the primary
360 * drives in the system -- the ones reflected as drive 1 or 2. The first
361 * drive is stored in the high nibble of CMOS byte 0x12, the second in the low
362 * nibble. This will be either a 4 bit drive type or 0xf indicating use byte
363 * 0x19 for an 8 bit type, drive 1, 0x1a for drive 2 in CMOS. A non-zero value
364 * means we have an AT controller hard disk for that drive.
366 * Of course, there is no guarantee that either drive is actually on the
367 * "primary" IDE interface, but we don't bother trying to sort that out here.
368 * If a drive is not actually on the primary interface, then these parameters
369 * will be ignored. This results in the user having to supply the logical
370 * drive geometry as a boot parameter for each drive not on the primary i/f.
372 * The only "perfect" way to handle this would be to modify the setup.[cS] code
373 * to do BIOS calls Int13h/Fn08h and Int13h/Fn48h to get all of the drive info
374 * for us during initialization. I have the necessary docs -- any takers? -ml
376 static void probe_cmos_for_drives (ide_hwif_t *hwif)
378 #ifdef __i386__
379 extern struct drive_info_struct drive_info;
380 byte cmos_disks, *BIOS = (byte *) &drive_info;
381 int unit;
383 #ifdef CONFIG_BLK_DEV_PDC4030
384 if (hwif->chipset == ide_pdc4030 && hwif->channel != 0)
385 return;
386 #endif /* CONFIG_BLK_DEV_PDC4030 */
387 outb_p(0x12,0x70); /* specify CMOS address 0x12 */
388 cmos_disks = inb_p(0x71); /* read the data from 0x12 */
389 /* Extract drive geometry from CMOS+BIOS if not already setup */
390 for (unit = 0; unit < MAX_DRIVES; ++unit) {
391 ide_drive_t *drive = &hwif->drives[unit];
392 if ((cmos_disks & (0xf0 >> (unit*4))) && !drive->present && !drive->nobios) {
393 drive->cyl = drive->bios_cyl = *(unsigned short *)BIOS;
394 drive->head = drive->bios_head = *(BIOS+2);
395 drive->sect = drive->bios_sect = *(BIOS+14);
396 drive->ctl = *(BIOS+8);
397 drive->present = 1;
399 BIOS += 16;
401 #endif
405 * This routine only knows how to look for drive units 0 and 1
406 * on an interface, so any setting of MAX_DRIVES > 2 won't work here.
408 static void probe_hwif (ide_hwif_t *hwif)
410 unsigned int unit;
411 unsigned long flags;
413 ide_ioreg_t ide_control_reg = hwif->io_ports[IDE_CONTROL_OFFSET];
414 ide_ioreg_t region_low = hwif->io_ports[IDE_DATA_OFFSET];
415 ide_ioreg_t region_high = region_low;
416 ide_ioreg_t region_request = 8;
417 int i;
419 if (hwif->noprobe)
420 return;
421 if (hwif->io_ports[IDE_DATA_OFFSET] == HD_DATA)
422 probe_cmos_for_drives (hwif);
425 * Calculate the region that this interface occupies,
426 * handling interfaces where the registers may not be
427 * ordered sanely. We deal with the CONTROL register
428 * separately.
430 for (i = IDE_DATA_OFFSET; i <= IDE_STATUS_OFFSET; i++) {
431 if (hwif->io_ports[i]) {
432 if (hwif->io_ports[i] < region_low)
433 region_low = hwif->io_ports[i];
434 if (hwif->io_ports[i] > region_high)
435 region_high = hwif->io_ports[i];
438 region_request = (region_high - region_low);
439 if (region_request == 0x0007)
440 region_request++;
441 if ((hwif->chipset != ide_4drives || !hwif->mate->present) &&
442 #if CONFIG_BLK_DEV_PDC4030
443 (hwif->chipset != ide_pdc4030 || hwif->channel == 0) &&
444 #endif /* CONFIG_BLK_DEV_PDC4030 */
445 (ide_check_region(region_low, region_request) ||
446 (ide_control_reg && ide_check_region(ide_control_reg,1))))
448 int msgout = 0;
449 for (unit = 0; unit < MAX_DRIVES; ++unit) {
450 ide_drive_t *drive = &hwif->drives[unit];
451 if (drive->present) {
452 drive->present = 0;
453 printk("%s: ERROR, PORTS ALREADY IN USE\n", drive->name);
454 msgout = 1;
457 if (!msgout)
458 printk("%s: ports already in use, skipping probe\n", hwif->name);
459 return;
462 __save_flags(flags); /* local CPU only */
463 __sti(); /* local CPU only; needed for jiffies and irq probing */
465 * Second drive should only exist if first drive was found,
466 * but a lot of cdrom drives are configured as single slaves.
468 for (unit = 0; unit < MAX_DRIVES; ++unit) {
469 ide_drive_t *drive = &hwif->drives[unit];
470 (void) probe_for_drive (drive);
471 if (drive->present && !hwif->present) {
472 hwif->present = 1;
473 if (hwif->chipset != ide_4drives || !hwif->mate->present) {
474 ide_request_region(region_low, region_request, hwif->name);
475 if (ide_control_reg)
476 ide_request_region(ide_control_reg, 1, hwif->name);
480 if (ide_control_reg && hwif->reset) {
481 unsigned long timeout = jiffies + WAIT_WORSTCASE;
482 byte stat;
484 printk("%s: reset\n", hwif->name);
485 OUT_BYTE(12, ide_control_reg);
486 udelay(10);
487 OUT_BYTE(8, ide_control_reg);
488 do {
489 ide_delay_50ms();
490 stat = IN_BYTE(hwif->io_ports[IDE_STATUS_OFFSET]);
491 } while ((stat & BUSY_STAT) && 0 < (signed long)(timeout - jiffies));
494 __restore_flags(flags); /* local CPU only */
495 for (unit = 0; unit < MAX_DRIVES; ++unit) {
496 ide_drive_t *drive = &hwif->drives[unit];
497 if (drive->present) {
498 ide_tuneproc_t *tuneproc = HWIF(drive)->tuneproc;
499 if (tuneproc != NULL && drive->autotune == 1)
500 tuneproc(drive, 255); /* auto-tune PIO mode */
505 #if MAX_HWIFS > 1
507 * save_match() is used to simplify logic in init_irq() below.
509 * A loophole here is that we may not know about a particular
510 * hwif's irq until after that hwif is actually probed/initialized..
511 * This could be a problem for the case where an hwif is on a
512 * dual interface that requires serialization (eg. cmd640) and another
513 * hwif using one of the same irqs is initialized beforehand.
515 * This routine detects and reports such situations, but does not fix them.
517 static void save_match (ide_hwif_t *hwif, ide_hwif_t *new, ide_hwif_t **match)
519 ide_hwif_t *m = *match;
521 if (m && m->hwgroup && m->hwgroup != new->hwgroup) {
522 if (!new->hwgroup)
523 return;
524 printk("%s: potential irq problem with %s and %s\n", hwif->name, new->name, m->name);
526 if (!m || m->irq != hwif->irq) /* don't undo a prior perfect match */
527 *match = new;
529 #endif /* MAX_HWIFS > 1 */
532 * This routine sets up the irq for an ide interface, and creates a new
533 * hwgroup for the irq/hwif if none was previously assigned.
535 * Much of the code is for correctly detecting/handling irq sharing
536 * and irq serialization situations. This is somewhat complex because
537 * it handles static as well as dynamic (PCMCIA) IDE interfaces.
539 * The SA_INTERRUPT in sa_flags means ide_intr() is always entered with
540 * interrupts completely disabled. This can be bad for interrupt latency,
541 * but anything else has led to problems on some machines. We re-enable
542 * interrupts as much as we can safely do in most places.
544 static int init_irq (ide_hwif_t *hwif)
546 unsigned long flags;
547 unsigned int index;
548 ide_hwgroup_t *hwgroup;
549 ide_hwif_t *match = NULL;
551 save_flags(flags); /* all CPUs */
552 cli(); /* all CPUs */
554 hwif->hwgroup = NULL;
555 #if MAX_HWIFS > 1
557 * Group up with any other hwifs that share our irq(s).
559 for (index = 0; index < MAX_HWIFS; index++) {
560 ide_hwif_t *h = &ide_hwifs[index];
561 if (h->hwgroup) { /* scan only initialized hwif's */
562 if (hwif->irq == h->irq) {
563 hwif->sharing_irq = h->sharing_irq = 1;
564 if (hwif->chipset != ide_pci || h->chipset != ide_pci) {
565 save_match(hwif, h, &match);
568 if (hwif->serialized) {
569 if (hwif->mate && hwif->mate->irq == h->irq)
570 save_match(hwif, h, &match);
572 if (h->serialized) {
573 if (h->mate && hwif->irq == h->mate->irq)
574 save_match(hwif, h, &match);
578 #endif /* MAX_HWIFS > 1 */
580 * If we are still without a hwgroup, then form a new one
582 if (match) {
583 hwgroup = match->hwgroup;
584 } else {
585 hwgroup = kmalloc(sizeof(ide_hwgroup_t), GFP_KERNEL);
586 memset(hwgroup, 0, sizeof(ide_hwgroup_t));
587 hwgroup->hwif = hwif->next = hwif;
588 hwgroup->rq = NULL;
589 hwgroup->handler = NULL;
590 hwgroup->drive = NULL;
591 hwgroup->busy = 0;
592 hwgroup->spinlock = (spinlock_t)SPIN_LOCK_UNLOCKED;
593 #if (DEBUG_SPINLOCK > 0)
594 printk("hwgroup(%s) spinlock is %p\n", hwif->name, &hwgroup->spinlock); /* FIXME */
595 #endif
596 init_timer(&hwgroup->timer);
597 hwgroup->timer.function = &ide_timer_expiry;
598 hwgroup->timer.data = (unsigned long) hwgroup;
602 * Allocate the irq, if not already obtained for another hwif
604 if (!match || match->irq != hwif->irq) {
605 int sa = (hwif->chipset == ide_pci) ? SA_INTERRUPT|SA_SHIRQ : SA_INTERRUPT;
606 if (ide_request_irq(hwif->irq, &ide_intr, sa, hwif->name, hwgroup)) {
607 if (!match)
608 kfree(hwgroup);
609 restore_flags(flags); /* all CPUs */
610 return 1;
615 * Everything is okay, so link us into the hwgroup
617 hwif->hwgroup = hwgroup;
618 hwif->next = hwgroup->hwif->next;
619 hwgroup->hwif->next = hwif;
621 for (index = 0; index < MAX_DRIVES; ++index) {
622 ide_drive_t *drive = &hwif->drives[index];
623 if (!drive->present)
624 continue;
625 if (!hwgroup->drive)
626 hwgroup->drive = drive;
627 drive->next = hwgroup->drive->next;
628 hwgroup->drive->next = drive;
630 if (!hwgroup->hwif) {
631 hwgroup->hwif = HWIF(hwgroup->drive);
632 #ifdef DEBUG
633 printk("%s : Adding missed hwif to hwgroup!!\n", hwif->name);
634 #endif
636 restore_flags(flags); /* all CPUs; safe now that hwif->hwgroup is set up */
638 #if !defined(__mc68000__) && !defined(CONFIG_APUS) && !defined(__sparc__)
639 printk("%s at 0x%03x-0x%03x,0x%03x on irq %d", hwif->name,
640 hwif->io_ports[IDE_DATA_OFFSET],
641 hwif->io_ports[IDE_DATA_OFFSET]+7,
642 hwif->io_ports[IDE_CONTROL_OFFSET], hwif->irq);
643 #elif defined(__sparc__)
644 printk("%s at 0x%03x-0x%03x,0x%03x on irq %s", hwif->name,
645 hwif->io_ports[IDE_DATA_OFFSET],
646 hwif->io_ports[IDE_DATA_OFFSET]+7,
647 hwif->io_ports[IDE_CONTROL_OFFSET], __irq_itoa(hwif->irq));
648 #else
649 printk("%s at %p on irq 0x%08x", hwif->name,
650 hwif->io_ports[IDE_DATA_OFFSET], hwif->irq);
651 #endif /* __mc68000__ && CONFIG_APUS */
652 if (match)
653 printk(" (%sed with %s)",
654 hwif->sharing_irq ? "shar" : "serializ", match->name);
655 printk("\n");
656 return 0;
660 * init_gendisk() (as opposed to ide_geninit) is called for each major device,
661 * after probing for drives, to allocate partition tables and other data
662 * structures needed for the routines in genhd.c. ide_geninit() gets called
663 * somewhat later, during the partition check.
665 static void init_gendisk (ide_hwif_t *hwif)
667 struct gendisk *gd, **gdp;
668 unsigned int unit, units, minors;
669 int *bs, *max_sect, *max_ra;
671 /* figure out maximum drive number on the interface */
672 for (units = MAX_DRIVES; units > 0; --units) {
673 if (hwif->drives[units-1].present)
674 break;
676 minors = units * (1<<PARTN_BITS);
677 gd = kmalloc (sizeof(struct gendisk), GFP_KERNEL);
678 gd->sizes = kmalloc (minors * sizeof(int), GFP_KERNEL);
679 gd->part = kmalloc (minors * sizeof(struct hd_struct), GFP_KERNEL);
680 bs = kmalloc (minors*sizeof(int), GFP_KERNEL);
681 max_sect = kmalloc (minors*sizeof(int), GFP_KERNEL);
682 max_ra = kmalloc (minors*sizeof(int), GFP_KERNEL);
684 memset(gd->part, 0, minors * sizeof(struct hd_struct));
686 /* cdroms and msdos f/s are examples of non-1024 blocksizes */
687 blksize_size[hwif->major] = bs;
688 max_sectors[hwif->major] = max_sect;
689 max_readahead[hwif->major] = max_ra;
690 for (unit = 0; unit < minors; ++unit) {
691 *bs++ = BLOCK_SIZE;
692 #ifdef CONFIG_BLK_DEV_PDC4030
693 *max_sect++ = ((hwif->chipset == ide_pdc4030) ? 127 : MAX_SECTORS);
694 #else
695 *max_sect++ = MAX_SECTORS;
696 #endif
697 *max_ra++ = MAX_READAHEAD;
700 for (unit = 0; unit < units; ++unit)
701 hwif->drives[unit].part = &gd->part[unit << PARTN_BITS];
703 gd->major = hwif->major; /* our major device number */
704 gd->major_name = IDE_MAJOR_NAME; /* treated special in genhd.c */
705 gd->minor_shift = PARTN_BITS; /* num bits for partitions */
706 gd->max_p = 1<<PARTN_BITS; /* 1 + max partitions / drive */
707 gd->max_nr = units; /* max num real drives */
708 gd->nr_real = units; /* current num real drives */
709 gd->init = &ide_geninit; /* initialization function */
710 gd->real_devices= hwif; /* ptr to internal data */
711 gd->next = NULL; /* linked list of major devs */
713 for (gdp = &gendisk_head; *gdp; gdp = &((*gdp)->next)) ;
714 hwif->gd = *gdp = gd; /* link onto tail of list */
716 for (unit = 0; unit < units; ++unit) {
717 if (hwif->drives[unit].present)
718 ide_add_generic_settings(hwif->drives + unit);
722 static int hwif_init (ide_hwif_t *hwif)
724 void (*rfn)(void);
726 if (!hwif->present)
727 return 0;
728 if (!hwif->irq) {
729 if (!(hwif->irq = ide_default_irq(hwif->io_ports[IDE_DATA_OFFSET])))
731 printk("%s: DISABLED, NO IRQ\n", hwif->name);
732 return (hwif->present = 0);
735 #ifdef CONFIG_BLK_DEV_HD
736 if (hwif->irq == HD_IRQ && hwif->io_ports[IDE_DATA_OFFSET] != HD_DATA) {
737 printk("%s: CANNOT SHARE IRQ WITH OLD HARDDISK DRIVER (hd.c)\n", hwif->name);
738 return (hwif->present = 0);
740 #endif /* CONFIG_BLK_DEV_HD */
742 hwif->present = 0; /* we set it back to 1 if all is ok below */
743 switch (hwif->major) {
744 case IDE0_MAJOR: rfn = &do_ide0_request; break;
745 #if MAX_HWIFS > 1
746 case IDE1_MAJOR: rfn = &do_ide1_request; break;
747 #endif
748 #if MAX_HWIFS > 2
749 case IDE2_MAJOR: rfn = &do_ide2_request; break;
750 #endif
751 #if MAX_HWIFS > 3
752 case IDE3_MAJOR: rfn = &do_ide3_request; break;
753 #endif
754 #if MAX_HWIFS > 4
755 case IDE4_MAJOR: rfn = &do_ide4_request; break;
756 #endif
757 #if MAX_HWIFS > 5
758 case IDE5_MAJOR: rfn = &do_ide5_request; break;
759 #endif
760 #if MAX_HWIFS > 6
761 case IDE6_MAJOR: rfn = &do_ide6_request; break;
762 #endif
763 #if MAX_HWIFS > 7
764 case IDE7_MAJOR: rfn = &do_ide7_request; break;
765 #endif
766 #if MAX_HWIFS > 8
767 case IDE8_MAJOR: rfn = &do_ide8_request; break;
768 #endif
769 #if MAX_HWIFS > 9
770 case IDE9_MAJOR: rfn = &do_ide9_request; break;
771 #endif
772 default:
773 printk("%s: request_fn NOT DEFINED\n", hwif->name);
774 return (hwif->present = 0);
776 if (register_blkdev (hwif->major, hwif->name, ide_fops)) {
777 printk("%s: UNABLE TO GET MAJOR NUMBER %d\n", hwif->name, hwif->major);
778 return (hwif->present = 0);
781 if (init_irq (hwif)) {
782 int i = hwif->irq;
784 * It failed to initialise. Find the default IRQ for
785 * this port and try that.
787 if (!(hwif->irq = ide_default_irq(hwif->io_ports[IDE_DATA_OFFSET])))
789 printk("%s: Disabled unable to get IRQ %d.\n", hwif->name, i);
790 (void) unregister_blkdev (hwif->major, hwif->name);
791 return (hwif->present = 0);
793 if(init_irq (hwif))
795 printk("%s: probed IRQ %d and default IRQ %d failed.\n",
796 hwif->name, i, hwif->irq);
797 (void) unregister_blkdev (hwif->major, hwif->name);
798 return (hwif->present = 0);
800 printk("%s: probed IRQ %d failed, using default.\n",
801 hwif->name, hwif->irq);
804 init_gendisk(hwif);
805 blk_dev[hwif->major].data = hwif;
806 blk_dev[hwif->major].request_fn = rfn;
807 blk_dev[hwif->major].queue = ide_get_queue;
808 read_ahead[hwif->major] = 8; /* (4kB) */
809 hwif->present = 1; /* success */
811 #if (DEBUG_SPINLOCK > 0)
813 static int done = 0;
814 if (!done++)
815 printk("io_request_lock is %p\n", &io_request_lock); /* FIXME */
817 #endif
818 return hwif->present;
821 int ideprobe_init (void);
822 static ide_module_t ideprobe_module = {
823 IDE_PROBE_MODULE,
824 ideprobe_init,
825 NULL
828 int ideprobe_init (void)
830 unsigned int index;
831 int probe[MAX_HWIFS];
833 MOD_INC_USE_COUNT;
834 memset(probe, 0, MAX_HWIFS * sizeof(int));
835 for (index = 0; index < MAX_HWIFS; ++index)
836 probe[index] = !ide_hwifs[index].present;
839 * Probe for drives in the usual way.. CMOS/BIOS, then poke at ports
841 for (index = 0; index < MAX_HWIFS; ++index)
842 if (probe[index])
843 probe_hwif(&ide_hwifs[index]);
844 for (index = 0; index < MAX_HWIFS; ++index)
845 if (probe[index])
846 hwif_init(&ide_hwifs[index]);
847 ide_register_module(&ideprobe_module);
848 MOD_DEC_USE_COUNT;
849 return 0;
852 #ifdef MODULE
853 int init_module (void)
855 unsigned int index;
857 for (index = 0; index < MAX_HWIFS; ++index)
858 ide_unregister(index);
859 ideprobe_init();
860 create_proc_ide_interfaces();
861 return 0;
864 void cleanup_module (void)
866 ide_unregister_module(&ideprobe_module);
868 #endif /* MODULE */