Import 2.3.9pre5
[davej-history.git] / drivers / block / ide-probe.c
blob3b5d24744ad0b9426c6358d9336340da19aded68
1 /*
2 * linux/drivers/block/ide-probe.c Version 1.04 March 10, 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
21 * fix for (hwif->chipset == ide_pdc4030)
22 * added ide6/7
25 #undef REALLY_SLOW_IO /* most systems can safely undef this */
27 #include <linux/config.h>
28 #include <linux/module.h>
29 #include <linux/types.h>
30 #include <linux/string.h>
31 #include <linux/kernel.h>
32 #include <linux/timer.h>
33 #include <linux/mm.h>
34 #include <linux/interrupt.h>
35 #include <linux/major.h>
36 #include <linux/errno.h>
37 #include <linux/genhd.h>
38 #include <linux/malloc.h>
39 #include <linux/delay.h>
40 #include <linux/ide.h>
43 #include <asm/byteorder.h>
44 #include <asm/irq.h>
45 #include <asm/uaccess.h>
46 #include <asm/io.h>
48 static inline void do_identify (ide_drive_t *drive, byte cmd)
50 int bswap = 1;
51 struct hd_driveid *id;
53 id = drive->id = kmalloc (SECTOR_WORDS*4, GFP_ATOMIC); /* called with interrupts disabled! */
54 ide_input_data(drive, id, SECTOR_WORDS); /* read 512 bytes of id info */
55 ide__sti(); /* local CPU only */
56 ide_fix_driveid(id);
58 #if defined (CONFIG_SCSI_EATA_DMA) || defined (CONFIG_SCSI_EATA_PIO) || defined (CONFIG_SCSI_EATA)
60 * EATA SCSI controllers do a hardware ATA emulation:
61 * Ignore them if there is a driver for them available.
63 if ((id->model[0] == 'P' && id->model[1] == 'M')
64 || (id->model[0] == 'S' && id->model[1] == 'K')) {
65 printk("%s: EATA SCSI HBA %.10s\n", drive->name, id->model);
66 drive->present = 0;
67 return;
69 #endif /* CONFIG_SCSI_EATA_DMA || CONFIG_SCSI_EATA_PIO */
72 * WIN_IDENTIFY returns little-endian info,
73 * WIN_PIDENTIFY *usually* returns little-endian info.
75 if (cmd == WIN_PIDENTIFY) {
76 if ((id->model[0] == 'N' && id->model[1] == 'E') /* NEC */
77 || (id->model[0] == 'F' && id->model[1] == 'X') /* Mitsumi */
78 || (id->model[0] == 'P' && id->model[1] == 'i'))/* Pioneer */
79 bswap ^= 1; /* Vertos drives may still be weird */
81 ide_fixstring (id->model, sizeof(id->model), bswap);
82 ide_fixstring (id->fw_rev, sizeof(id->fw_rev), bswap);
83 ide_fixstring (id->serial_no, sizeof(id->serial_no), bswap);
85 id->model[sizeof(id->model)-1] = '\0'; /* we depend on this a lot! */
86 printk("%s: %s, ", drive->name, id->model);
87 drive->present = 1;
90 * Check for an ATAPI device
92 if (cmd == WIN_PIDENTIFY) {
93 byte type = (id->config >> 8) & 0x1f;
94 printk("ATAPI ");
95 #ifdef CONFIG_BLK_DEV_PDC4030
96 if (HWIF(drive)->channel == 1 && HWIF(drive)->chipset == ide_pdc4030) {
97 printk(" -- not supported on 2nd Promise port\n");
98 drive->present = 0;
99 return;
101 #endif /* CONFIG_BLK_DEV_PDC4030 */
102 switch (type) {
103 case ide_floppy:
104 if (!strstr(id->model, "CD-ROM")) {
105 if (!strstr(id->model, "oppy") && !strstr(id->model, "poyp") && !strstr(id->model, "ZIP"))
106 printk("cdrom or floppy?, assuming ");
107 if (drive->media != ide_cdrom) {
108 printk ("FLOPPY");
109 break;
112 type = ide_cdrom; /* Early cdrom models used zero */
113 case ide_cdrom:
114 printk ("CDROM");
115 drive->removable = 1;
116 break;
117 case ide_tape:
118 printk ("TAPE");
119 break;
120 case ide_optical:
121 printk ("OPTICAL");
122 drive->removable = 1;
123 break;
124 default:
125 printk("UNKNOWN (type %d)", type);
126 break;
128 printk (" drive\n");
129 drive->media = type;
130 return;
134 * Not an ATAPI device: looks like a "regular" hard disk
136 if (id->config & (1<<7))
137 drive->removable = 1;
139 * Prevent long system lockup probing later for non-existant
140 * slave drive if the hwif is actually a flash memory card of some variety:
142 if (drive_is_flashcard(drive)) {
143 ide_drive_t *mate = &HWIF(drive)->drives[1^drive->select.b.unit];
144 mate->present = 0;
145 mate->noprobe = 1;
147 drive->media = ide_disk;
148 printk("ATA DISK drive\n");
149 return;
153 * try_to_identify() sends an ATA(PI) IDENTIFY request to a drive
154 * and waits for a response. It also monitors irqs while this is
155 * happening, in hope of automatically determining which one is
156 * being used by the interface.
158 * Returns: 0 device was identified
159 * 1 device timed-out (no response to identify request)
160 * 2 device aborted the command (refused to identify itself)
162 static int try_to_identify (ide_drive_t *drive, byte cmd)
164 int rc;
165 ide_ioreg_t hd_status;
166 unsigned long timeout;
167 unsigned long irqs = 0;
168 byte s, a;
170 if (IDE_CONTROL_REG) {
171 if (!HWIF(drive)->irq) { /* already got an IRQ? */
172 probe_irq_off(probe_irq_on()); /* clear dangling irqs */
173 irqs = probe_irq_on(); /* start monitoring irqs */
174 OUT_BYTE(drive->ctl,IDE_CONTROL_REG); /* enable device irq */
177 ide_delay_50ms(); /* take a deep breath */
178 a = IN_BYTE(IDE_ALTSTATUS_REG);
179 s = IN_BYTE(IDE_STATUS_REG);
180 if ((a ^ s) & ~INDEX_STAT) {
181 printk("%s: probing with STATUS(0x%02x) instead of ALTSTATUS(0x%02x)\n", drive->name, s, a);
182 hd_status = IDE_STATUS_REG; /* ancient Seagate drives, broken interfaces */
183 } else {
184 hd_status = IDE_ALTSTATUS_REG; /* use non-intrusive polling */
186 } else {
187 ide_delay_50ms();
188 hd_status = IDE_STATUS_REG;
191 #if CONFIG_BLK_DEV_PDC4030
192 if (HWIF(drive)->chipset == ide_pdc4030) {
193 /* DC4030 hosted drives need their own identify... */
194 extern int pdc4030_identify(ide_drive_t *);
195 if (pdc4030_identify(drive)) {
196 if (irqs)
197 (void) probe_irq_off(irqs);
198 return 1;
200 } else
201 #endif /* CONFIG_BLK_DEV_PDC4030 */
202 OUT_BYTE(cmd,IDE_COMMAND_REG); /* ask drive for ID */
203 timeout = ((cmd == WIN_IDENTIFY) ? WAIT_WORSTCASE : WAIT_PIDENTIFY) / 2;
204 timeout += jiffies;
205 do {
206 if (0 < (signed long)(jiffies - timeout)) {
207 if (irqs)
208 (void) probe_irq_off(irqs);
209 return 1; /* drive timed-out */
211 ide_delay_50ms(); /* give drive a breather */
212 } while (IN_BYTE(hd_status) & BUSY_STAT);
214 ide_delay_50ms(); /* wait for IRQ and DRQ_STAT */
215 if (OK_STAT(GET_STAT(),DRQ_STAT,BAD_R_STAT)) {
216 unsigned long flags;
217 __save_flags(flags); /* local CPU only */
218 __cli(); /* local CPU only; some systems need this */
219 do_identify(drive, cmd); /* drive returned ID */
220 rc = 0; /* drive responded with ID */
221 (void) GET_STAT(); /* clear drive IRQ */
222 __restore_flags(flags); /* local CPU only */
223 } else
224 rc = 2; /* drive refused ID */
225 if (IDE_CONTROL_REG && !HWIF(drive)->irq) {
226 irqs = probe_irq_off(irqs); /* get our irq number */
227 if (irqs > 0) {
228 HWIF(drive)->irq = irqs; /* save it for later */
229 irqs = probe_irq_on();
230 OUT_BYTE(drive->ctl|2,IDE_CONTROL_REG); /* mask device irq */
231 udelay(5);
232 (void) probe_irq_off(irqs);
233 (void) probe_irq_off(probe_irq_on()); /* clear self-inflicted irq */
234 (void) GET_STAT(); /* clear drive IRQ */
236 } else { /* Mmmm.. multiple IRQs.. don't know which was ours */
237 printk("%s: IRQ probe failed (%ld)\n", drive->name, irqs);
238 #ifdef CONFIG_BLK_DEV_CMD640
239 #ifdef CMD640_DUMP_REGS
240 if (HWIF(drive)->chipset == ide_cmd640) {
241 printk("%s: Hmmm.. probably a driver problem.\n", drive->name);
242 CMD640_DUMP_REGS;
244 #endif /* CMD640_DUMP_REGS */
245 #endif /* CONFIG_BLK_DEV_CMD640 */
248 return rc;
252 * do_probe() has the difficult job of finding a drive if it exists,
253 * without getting hung up if it doesn't exist, without trampling on
254 * ethernet cards, and without leaving any IRQs dangling to haunt us later.
256 * If a drive is "known" to exist (from CMOS or kernel parameters),
257 * but does not respond right away, the probe will "hang in there"
258 * for the maximum wait time (about 30 seconds), otherwise it will
259 * exit much more quickly.
261 * Returns: 0 device was identified
262 * 1 device timed-out (no response to identify request)
263 * 2 device aborted the command (refused to identify itself)
264 * 3 bad status from device (possible for ATAPI drives)
265 * 4 probe was not attempted because failure was obvious
267 static int do_probe (ide_drive_t *drive, byte cmd)
269 int rc;
270 ide_hwif_t *hwif = HWIF(drive);
271 if (drive->present) { /* avoid waiting for inappropriate probes */
272 if ((drive->media != ide_disk) && (cmd == WIN_IDENTIFY))
273 return 4;
275 #ifdef DEBUG
276 printk("probing for %s: present=%d, media=%d, probetype=%s\n",
277 drive->name, drive->present, drive->media,
278 (cmd == WIN_IDENTIFY) ? "ATA" : "ATAPI");
279 #endif
280 SELECT_DRIVE(hwif,drive);
281 ide_delay_50ms();
282 if (IN_BYTE(IDE_SELECT_REG) != drive->select.all && !drive->present) {
283 if (drive->select.b.unit != 0) {
284 SELECT_DRIVE(hwif,&hwif->drives[0]); /* exit with drive0 selected */
285 ide_delay_50ms(); /* allow BUSY_STAT to assert & clear */
287 return 3; /* no i/f present: mmm.. this should be a 4 -ml */
290 if (OK_STAT(GET_STAT(),READY_STAT,BUSY_STAT)
291 || drive->present || cmd == WIN_PIDENTIFY)
293 if ((rc = try_to_identify(drive,cmd))) /* send cmd and wait */
294 rc = try_to_identify(drive,cmd); /* failed: try again */
295 if (rc == 1 && cmd == WIN_PIDENTIFY && drive->autotune != 2) {
296 unsigned long timeout;
297 printk("%s: no response (status = 0x%02x), resetting drive\n", drive->name, GET_STAT());
298 ide_delay_50ms();
299 OUT_BYTE (drive->select.all, IDE_SELECT_REG);
300 ide_delay_50ms();
301 OUT_BYTE(WIN_SRST, IDE_COMMAND_REG);
302 timeout = jiffies;
303 while ((GET_STAT() & BUSY_STAT) && time_before(jiffies, timeout + WAIT_WORSTCASE))
304 ide_delay_50ms();
305 rc = try_to_identify(drive, cmd);
307 if (rc == 1)
308 printk("%s: no response (status = 0x%02x)\n", drive->name, GET_STAT());
309 (void) GET_STAT(); /* ensure drive irq is clear */
310 } else {
311 rc = 3; /* not present or maybe ATAPI */
313 if (drive->select.b.unit != 0) {
314 SELECT_DRIVE(hwif,&hwif->drives[0]); /* exit with drive0 selected */
315 ide_delay_50ms();
316 (void) GET_STAT(); /* ensure drive irq is clear */
318 return rc;
322 * probe_for_drive() tests for existence of a given drive using do_probe().
324 * Returns: 0 no device was found
325 * 1 device was found (note: drive->present might still be 0)
327 static inline byte probe_for_drive (ide_drive_t *drive)
329 if (drive->noprobe) /* skip probing? */
330 return drive->present;
331 if (do_probe(drive, WIN_IDENTIFY) >= 2) { /* if !(success||timed-out) */
332 (void) do_probe(drive, WIN_PIDENTIFY); /* look for ATAPI device */
334 if (!drive->present)
335 return 0; /* drive not found */
336 if (drive->id == NULL) { /* identification failed? */
337 if (drive->media == ide_disk) {
338 printk ("%s: non-IDE drive, CHS=%d/%d/%d\n",
339 drive->name, drive->cyl, drive->head, drive->sect);
340 } else if (drive->media == ide_cdrom) {
341 printk("%s: ATAPI cdrom (?)\n", drive->name);
342 } else {
343 drive->present = 0; /* nuke it */
346 return 1; /* drive was found */
350 * We query CMOS about hard disks : it could be that we have a SCSI/ESDI/etc
351 * controller that is BIOS compatible with ST-506, and thus showing up in our
352 * BIOS table, but not register compatible, and therefore not present in CMOS.
354 * Furthermore, we will assume that our ST-506 drives <if any> are the primary
355 * drives in the system -- the ones reflected as drive 1 or 2. The first
356 * drive is stored in the high nibble of CMOS byte 0x12, the second in the low
357 * nibble. This will be either a 4 bit drive type or 0xf indicating use byte
358 * 0x19 for an 8 bit type, drive 1, 0x1a for drive 2 in CMOS. A non-zero value
359 * means we have an AT controller hard disk for that drive.
361 * Of course, there is no guarantee that either drive is actually on the
362 * "primary" IDE interface, but we don't bother trying to sort that out here.
363 * If a drive is not actually on the primary interface, then these parameters
364 * will be ignored. This results in the user having to supply the logical
365 * drive geometry as a boot parameter for each drive not on the primary i/f.
367 * The only "perfect" way to handle this would be to modify the setup.[cS] code
368 * to do BIOS calls Int13h/Fn08h and Int13h/Fn48h to get all of the drive info
369 * for us during initialization. I have the necessary docs -- any takers? -ml
371 static void probe_cmos_for_drives (ide_hwif_t *hwif)
373 #ifdef __i386__
374 extern struct drive_info_struct drive_info;
375 byte cmos_disks, *BIOS = (byte *) &drive_info;
376 int unit;
378 #ifdef CONFIG_BLK_DEV_PDC4030
379 if (hwif->chipset == ide_pdc4030 && hwif->channel != 0)
380 return;
381 #endif /* CONFIG_BLK_DEV_PDC4030 */
382 outb_p(0x12,0x70); /* specify CMOS address 0x12 */
383 cmos_disks = inb_p(0x71); /* read the data from 0x12 */
384 /* Extract drive geometry from CMOS+BIOS if not already setup */
385 for (unit = 0; unit < MAX_DRIVES; ++unit) {
386 ide_drive_t *drive = &hwif->drives[unit];
387 if ((cmos_disks & (0xf0 >> (unit*4))) && !drive->present && !drive->nobios) {
388 drive->cyl = drive->bios_cyl = *(unsigned short *)BIOS;
389 drive->head = drive->bios_head = *(BIOS+2);
390 drive->sect = drive->bios_sect = *(BIOS+14);
391 drive->ctl = *(BIOS+8);
392 drive->present = 1;
394 BIOS += 16;
396 #endif
400 * This routine only knows how to look for drive units 0 and 1
401 * on an interface, so any setting of MAX_DRIVES > 2 won't work here.
403 static void probe_hwif (ide_hwif_t *hwif)
405 unsigned int unit;
406 unsigned long flags;
408 ide_ioreg_t ide_control_reg = hwif->io_ports[IDE_CONTROL_OFFSET];
409 ide_ioreg_t region_low = hwif->io_ports[IDE_DATA_OFFSET];
410 ide_ioreg_t region_high = region_low;
411 ide_ioreg_t region_request = 8;
412 int i;
414 if (hwif->noprobe)
415 return;
416 if (hwif->io_ports[IDE_DATA_OFFSET] == HD_DATA)
417 probe_cmos_for_drives (hwif);
420 * Calculate the region that this interface occupies,
421 * handling interfaces where the registers may not be
422 * ordered sanely. We deal with the CONTROL register
423 * separately.
425 for (i = IDE_DATA_OFFSET; i <= IDE_STATUS_OFFSET; i++) {
426 if (hwif->io_ports[i]) {
427 if (hwif->io_ports[i] < region_low)
428 region_low = hwif->io_ports[i];
429 if (hwif->io_ports[i] > region_high)
430 region_high = hwif->io_ports[i];
433 region_request = (region_high - region_low);
434 if (region_request == 0x0007)
435 region_request++;
436 if ((hwif->chipset != ide_4drives || !hwif->mate->present) &&
437 #if CONFIG_BLK_DEV_PDC4030
438 (hwif->chipset != ide_pdc4030 || hwif->channel == 0) &&
439 #endif /* CONFIG_BLK_DEV_PDC4030 */
440 (ide_check_region(region_low, region_request) ||
441 (ide_control_reg && ide_check_region(ide_control_reg,1))))
443 int msgout = 0;
444 for (unit = 0; unit < MAX_DRIVES; ++unit) {
445 ide_drive_t *drive = &hwif->drives[unit];
446 if (drive->present) {
447 drive->present = 0;
448 printk("%s: ERROR, PORTS ALREADY IN USE\n", drive->name);
449 msgout = 1;
452 if (!msgout)
453 printk("%s: ports already in use, skipping probe\n", hwif->name);
454 return;
457 __save_flags(flags); /* local CPU only */
458 __sti(); /* local CPU only; needed for jiffies and irq probing */
460 * Second drive should only exist if first drive was found,
461 * but a lot of cdrom drives are configured as single slaves.
463 for (unit = 0; unit < MAX_DRIVES; ++unit) {
464 ide_drive_t *drive = &hwif->drives[unit];
465 (void) probe_for_drive (drive);
466 if (drive->present && !hwif->present) {
467 hwif->present = 1;
468 if (hwif->chipset != ide_4drives || !hwif->mate->present) {
469 ide_request_region(region_low, region_request, hwif->name);
470 if (ide_control_reg)
471 ide_request_region(ide_control_reg, 1, hwif->name);
475 if (ide_control_reg && hwif->reset) {
476 unsigned long timeout = jiffies + WAIT_WORSTCASE;
477 byte stat;
479 printk("%s: reset\n", hwif->name);
480 OUT_BYTE(12, ide_control_reg);
481 udelay(10);
482 OUT_BYTE(8, ide_control_reg);
483 do {
484 ide_delay_50ms();
485 stat = IN_BYTE(hwif->io_ports[IDE_STATUS_OFFSET]);
486 } while ((stat & BUSY_STAT) && 0 < (signed long)(timeout - jiffies));
489 __restore_flags(flags); /* local CPU only */
490 for (unit = 0; unit < MAX_DRIVES; ++unit) {
491 ide_drive_t *drive = &hwif->drives[unit];
492 if (drive->present) {
493 ide_tuneproc_t *tuneproc = HWIF(drive)->tuneproc;
494 if (tuneproc != NULL && drive->autotune == 1)
495 tuneproc(drive, 255); /* auto-tune PIO mode */
500 #if MAX_HWIFS > 1
502 * save_match() is used to simplify logic in init_irq() below.
504 * A loophole here is that we may not know about a particular
505 * hwif's irq until after that hwif is actually probed/initialized..
506 * This could be a problem for the case where an hwif is on a
507 * dual interface that requires serialization (eg. cmd640) and another
508 * hwif using one of the same irqs is initialized beforehand.
510 * This routine detects and reports such situations, but does not fix them.
512 static void save_match (ide_hwif_t *hwif, ide_hwif_t *new, ide_hwif_t **match)
514 ide_hwif_t *m = *match;
516 if (m && m->hwgroup && m->hwgroup != new->hwgroup) {
517 if (!new->hwgroup)
518 return;
519 printk("%s: potential irq problem with %s and %s\n", hwif->name, new->name, m->name);
521 if (!m || m->irq != hwif->irq) /* don't undo a prior perfect match */
522 *match = new;
524 #endif /* MAX_HWIFS > 1 */
527 * This routine sets up the irq for an ide interface, and creates a new
528 * hwgroup for the irq/hwif if none was previously assigned.
530 * Much of the code is for correctly detecting/handling irq sharing
531 * and irq serialization situations. This is somewhat complex because
532 * it handles static as well as dynamic (PCMCIA) IDE interfaces.
534 * The SA_INTERRUPT in sa_flags means ide_intr() is always entered with
535 * interrupts completely disabled. This can be bad for interrupt latency,
536 * but anything else has led to problems on some machines. We re-enable
537 * interrupts as much as we can safely do in most places.
539 static int init_irq (ide_hwif_t *hwif)
541 unsigned long flags;
542 unsigned int index;
543 ide_hwgroup_t *hwgroup;
544 ide_hwif_t *match = NULL;
546 save_flags(flags); /* all CPUs */
547 cli(); /* all CPUs */
549 hwif->hwgroup = NULL;
550 #if MAX_HWIFS > 1
552 * Group up with any other hwifs that share our irq(s).
554 for (index = 0; index < MAX_HWIFS; index++) {
555 ide_hwif_t *h = &ide_hwifs[index];
556 if (h->hwgroup) { /* scan only initialized hwif's */
557 if (hwif->irq == h->irq) {
558 hwif->sharing_irq = h->sharing_irq = 1;
559 if (hwif->chipset != ide_pci || h->chipset != ide_pci) {
560 save_match(hwif, h, &match);
563 if (hwif->serialized) {
564 if (hwif->mate && hwif->mate->irq == h->irq)
565 save_match(hwif, h, &match);
567 if (h->serialized) {
568 if (h->mate && hwif->irq == h->mate->irq)
569 save_match(hwif, h, &match);
573 #endif /* MAX_HWIFS > 1 */
575 * If we are still without a hwgroup, then form a new one
577 if (match) {
578 hwgroup = match->hwgroup;
579 } else {
580 hwgroup = kmalloc(sizeof(ide_hwgroup_t), GFP_KERNEL);
581 memset(hwgroup, 0, sizeof(ide_hwgroup_t));
582 hwgroup->hwif = hwif->next = hwif;
583 hwgroup->rq = NULL;
584 hwgroup->handler = NULL;
585 hwgroup->drive = NULL;
586 hwgroup->busy = 0;
587 hwgroup->spinlock = (spinlock_t)SPIN_LOCK_UNLOCKED;
588 #if (DEBUG_SPINLOCK > 0)
589 printk("hwgroup(%s) spinlock is %p\n", hwif->name, &hwgroup->spinlock); /* FIXME */
590 #endif
591 init_timer(&hwgroup->timer);
592 hwgroup->timer.function = &ide_timer_expiry;
593 hwgroup->timer.data = (unsigned long) hwgroup;
597 * Allocate the irq, if not already obtained for another hwif
599 if (!match || match->irq != hwif->irq) {
600 int sa = (hwif->chipset == ide_pci) ? SA_INTERRUPT|SA_SHIRQ : SA_INTERRUPT;
601 if (ide_request_irq(hwif->irq, &ide_intr, sa, hwif->name, hwgroup)) {
602 if (!match)
603 kfree(hwgroup);
604 restore_flags(flags); /* all CPUs */
605 return 1;
610 * Everything is okay, so link us into the hwgroup
612 hwif->hwgroup = hwgroup;
613 hwif->next = hwgroup->hwif->next;
614 hwgroup->hwif->next = hwif;
616 for (index = 0; index < MAX_DRIVES; ++index) {
617 ide_drive_t *drive = &hwif->drives[index];
618 if (!drive->present)
619 continue;
620 if (!hwgroup->drive)
621 hwgroup->drive = drive;
622 drive->next = hwgroup->drive->next;
623 hwgroup->drive->next = drive;
625 if (!hwgroup->hwif) {
626 hwgroup->hwif = HWIF(hwgroup->drive);
627 #ifdef DEBUG
628 printk("%s : Adding missed hwif to hwgroup!!\n", hwif->name);
629 #endif
631 restore_flags(flags); /* all CPUs; safe now that hwif->hwgroup is set up */
633 #if !defined(__mc68000__) && !defined(CONFIG_APUS) && !defined(__sparc__)
634 printk("%s at 0x%03x-0x%03x,0x%03x on irq %d", hwif->name,
635 hwif->io_ports[IDE_DATA_OFFSET],
636 hwif->io_ports[IDE_DATA_OFFSET]+7,
637 hwif->io_ports[IDE_CONTROL_OFFSET], hwif->irq);
638 #elif defined(__sparc__)
639 printk("%s at 0x%03x-0x%03x,0x%03x on irq %s", hwif->name,
640 hwif->io_ports[IDE_DATA_OFFSET],
641 hwif->io_ports[IDE_DATA_OFFSET]+7,
642 hwif->io_ports[IDE_CONTROL_OFFSET], __irq_itoa(hwif->irq));
643 #else
644 printk("%s at %p on irq 0x%08x", hwif->name,
645 hwif->io_ports[IDE_DATA_OFFSET], hwif->irq);
646 #endif /* __mc68000__ && CONFIG_APUS */
647 if (match)
648 printk(" (%sed with %s)",
649 hwif->sharing_irq ? "shar" : "serializ", match->name);
650 printk("\n");
651 return 0;
655 * init_gendisk() (as opposed to ide_geninit) is called for each major device,
656 * after probing for drives, to allocate partition tables and other data
657 * structures needed for the routines in genhd.c. ide_geninit() gets called
658 * somewhat later, during the partition check.
660 static void init_gendisk (ide_hwif_t *hwif)
662 struct gendisk *gd, **gdp;
663 unsigned int unit, units, minors;
664 int *bs, *max_sect, *max_ra;
666 /* figure out maximum drive number on the interface */
667 for (units = MAX_DRIVES; units > 0; --units) {
668 if (hwif->drives[units-1].present)
669 break;
671 minors = units * (1<<PARTN_BITS);
672 gd = kmalloc (sizeof(struct gendisk), GFP_KERNEL);
673 gd->sizes = kmalloc (minors * sizeof(int), GFP_KERNEL);
674 gd->part = kmalloc (minors * sizeof(struct hd_struct), GFP_KERNEL);
675 bs = kmalloc (minors*sizeof(int), GFP_KERNEL);
676 max_sect = kmalloc (minors*sizeof(int), GFP_KERNEL);
677 max_ra = kmalloc (minors*sizeof(int), GFP_KERNEL);
679 memset(gd->part, 0, minors * sizeof(struct hd_struct));
681 /* cdroms and msdos f/s are examples of non-1024 blocksizes */
682 blksize_size[hwif->major] = bs;
683 max_sectors[hwif->major] = max_sect;
684 max_readahead[hwif->major] = max_ra;
685 for (unit = 0; unit < minors; ++unit) {
686 *bs++ = BLOCK_SIZE;
687 #ifdef CONFIG_BLK_DEV_PDC4030
688 *max_sect++ = ((hwif->chipset == ide_pdc4030) ? 127 : MAX_SECTORS);
689 #else
690 *max_sect++ = MAX_SECTORS;
691 #endif
692 *max_ra++ = MAX_READAHEAD;
695 for (unit = 0; unit < units; ++unit)
696 hwif->drives[unit].part = &gd->part[unit << PARTN_BITS];
698 gd->major = hwif->major; /* our major device number */
699 gd->major_name = IDE_MAJOR_NAME; /* treated special in genhd.c */
700 gd->minor_shift = PARTN_BITS; /* num bits for partitions */
701 gd->max_p = 1<<PARTN_BITS; /* 1 + max partitions / drive */
702 gd->max_nr = units; /* max num real drives */
703 gd->nr_real = units; /* current num real drives */
704 gd->init = &ide_geninit; /* initialization function */
705 gd->real_devices= hwif; /* ptr to internal data */
706 gd->next = NULL; /* linked list of major devs */
708 for (gdp = &gendisk_head; *gdp; gdp = &((*gdp)->next)) ;
709 hwif->gd = *gdp = gd; /* link onto tail of list */
711 for (unit = 0; unit < units; ++unit) {
712 if (hwif->drives[unit].present)
713 ide_add_generic_settings(hwif->drives + unit);
717 static int hwif_init (ide_hwif_t *hwif)
719 void (*rfn)(void);
721 if (!hwif->present)
722 return 0;
723 if (!hwif->irq) {
724 if (!(hwif->irq = ide_default_irq(hwif->io_ports[IDE_DATA_OFFSET])))
726 printk("%s: DISABLED, NO IRQ\n", hwif->name);
727 return (hwif->present = 0);
730 #ifdef CONFIG_BLK_DEV_HD
731 if (hwif->irq == HD_IRQ && hwif->io_ports[IDE_DATA_OFFSET] != HD_DATA) {
732 printk("%s: CANNOT SHARE IRQ WITH OLD HARDDISK DRIVER (hd.c)\n", hwif->name);
733 return (hwif->present = 0);
735 #endif /* CONFIG_BLK_DEV_HD */
737 hwif->present = 0; /* we set it back to 1 if all is ok below */
738 switch (hwif->major) {
739 case IDE0_MAJOR: rfn = &do_ide0_request; break;
740 #if MAX_HWIFS > 1
741 case IDE1_MAJOR: rfn = &do_ide1_request; break;
742 #endif
743 #if MAX_HWIFS > 2
744 case IDE2_MAJOR: rfn = &do_ide2_request; break;
745 #endif
746 #if MAX_HWIFS > 3
747 case IDE3_MAJOR: rfn = &do_ide3_request; break;
748 #endif
749 #if MAX_HWIFS > 4
750 case IDE4_MAJOR: rfn = &do_ide4_request; break;
751 #endif
752 #if MAX_HWIFS > 5
753 case IDE5_MAJOR: rfn = &do_ide5_request; break;
754 #endif
755 #if MAX_HWIFS > 6
756 case IDE6_MAJOR: rfn = &do_ide6_request; break;
757 #endif
758 #if MAX_HWIFS > 7
759 case IDE7_MAJOR: rfn = &do_ide7_request; break;
760 #endif
761 default:
762 printk("%s: request_fn NOT DEFINED\n", hwif->name);
763 return (hwif->present = 0);
765 if (register_blkdev (hwif->major, hwif->name, ide_fops)) {
766 printk("%s: UNABLE TO GET MAJOR NUMBER %d\n", hwif->name, hwif->major);
767 return (hwif->present = 0);
770 if (init_irq (hwif)) {
771 int i = hwif->irq;
773 * It failed to initialise. Find the default IRQ for
774 * this port and try that.
776 if (!(hwif->irq = ide_default_irq(hwif->io_ports[IDE_DATA_OFFSET])))
778 printk("%s: Disabled unable to get IRQ %d.\n", hwif->name, i);
779 (void) unregister_blkdev (hwif->major, hwif->name);
780 return (hwif->present = 0);
782 if(init_irq (hwif))
784 printk("%s: probed IRQ %d and default IRQ %d failed.\n",
785 hwif->name, i, hwif->irq);
786 (void) unregister_blkdev (hwif->major, hwif->name);
787 return (hwif->present = 0);
789 printk("%s: probed IRQ %d failed, using default.\n",
790 hwif->name, hwif->irq);
793 init_gendisk(hwif);
794 blk_dev[hwif->major].data = hwif;
795 blk_dev[hwif->major].request_fn = rfn;
796 blk_dev[hwif->major].queue = ide_get_queue;
797 read_ahead[hwif->major] = 8; /* (4kB) */
798 hwif->present = 1; /* success */
800 #if (DEBUG_SPINLOCK > 0)
802 static int done = 0;
803 if (!done++)
804 printk("io_request_lock is %p\n", &io_request_lock); /* FIXME */
806 #endif
807 return hwif->present;
810 int ideprobe_init (void);
811 static ide_module_t ideprobe_module = {
812 IDE_PROBE_MODULE,
813 ideprobe_init,
814 NULL
817 int ideprobe_init (void)
819 unsigned int index;
820 int probe[MAX_HWIFS];
822 MOD_INC_USE_COUNT;
823 memset(probe, 0, MAX_HWIFS * sizeof(int));
824 for (index = 0; index < MAX_HWIFS; ++index)
825 probe[index] = !ide_hwifs[index].present;
828 * Probe for drives in the usual way.. CMOS/BIOS, then poke at ports
830 for (index = 0; index < MAX_HWIFS; ++index)
831 if (probe[index])
832 probe_hwif(&ide_hwifs[index]);
833 for (index = 0; index < MAX_HWIFS; ++index)
834 if (probe[index])
835 hwif_init(&ide_hwifs[index]);
836 ide_register_module(&ideprobe_module);
837 MOD_DEC_USE_COUNT;
838 return 0;
841 #ifdef MODULE
842 int init_module (void)
844 unsigned int index;
846 for (index = 0; index < MAX_HWIFS; ++index)
847 ide_unregister(index);
848 ideprobe_init();
849 create_proc_ide_interfaces();
850 return 0;
853 void cleanup_module (void)
855 ide_unregister_module(&ideprobe_module);
857 #endif /* MODULE */