Merge with 2.4.0-test3-pre4.
[linux-2.6/linux-mips.git] / drivers / ide / ide-probe.c
blob168895fb59efdf49644d672e81911b0700abbd7e
1 /*
2 * linux/drivers/ide/ide-probe.c Version 1.06 June 9, 2000
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>
10 * and Andre Hedrick <andre@linux-ide.org>
12 * See linux/MAINTAINERS for address of current maintainer.
14 * This is the IDE probe module, as evolved from hd.c and ide.c.
16 * Version 1.00 move drive probing code from ide.c to ide-probe.c
17 * Version 1.01 fix compilation problem for m68k
18 * Version 1.02 increase WAIT_PIDENTIFY to avoid CD-ROM locking at boot
19 * by Andrea Arcangeli
20 * Version 1.03 fix for (hwif->chipset == ide_4drives)
21 * Version 1.04 fixed buggy treatments of known flash memory cards
23 * Version 1.05 fix for (hwif->chipset == ide_pdc4030)
24 * added ide6/7/8/9
25 * allowed for secondary flash card to be detectable
26 * with new flag : drive->ata_flash : 1;
27 * Version 1.06 stream line request queue and prep for cascade project.
30 #undef REALLY_SLOW_IO /* most systems can safely undef this */
32 #include <linux/config.h>
33 #include <linux/module.h>
34 #include <linux/types.h>
35 #include <linux/string.h>
36 #include <linux/kernel.h>
37 #include <linux/timer.h>
38 #include <linux/mm.h>
39 #include <linux/interrupt.h>
40 #include <linux/major.h>
41 #include <linux/errno.h>
42 #include <linux/genhd.h>
43 #include <linux/malloc.h>
44 #include <linux/delay.h>
45 #include <linux/ide.h>
46 #include <linux/spinlock.h>
48 #include <asm/byteorder.h>
49 #include <asm/irq.h>
50 #include <asm/uaccess.h>
51 #include <asm/io.h>
53 static inline void do_identify (ide_drive_t *drive, byte cmd)
55 int bswap = 1;
56 struct hd_driveid *id;
58 id = drive->id = kmalloc (SECTOR_WORDS*4, GFP_ATOMIC); /* called with interrupts disabled! */
59 ide_input_data(drive, id, SECTOR_WORDS); /* read 512 bytes of id info */
60 ide__sti(); /* local CPU only */
61 ide_fix_driveid(id);
63 if (id->word156 == 0x4d42) {
64 printk("%s: drive->id->word156 == 0x%04x \n", drive->name, drive->id->word156);
67 if (!drive->forced_lun)
68 drive->last_lun = id->last_lun & 0x7;
69 #if defined (CONFIG_SCSI_EATA_DMA) || defined (CONFIG_SCSI_EATA_PIO) || defined (CONFIG_SCSI_EATA)
71 * EATA SCSI controllers do a hardware ATA emulation:
72 * Ignore them if there is a driver for them available.
74 if ((id->model[0] == 'P' && id->model[1] == 'M')
75 || (id->model[0] == 'S' && id->model[1] == 'K')) {
76 printk("%s: EATA SCSI HBA %.10s\n", drive->name, id->model);
77 drive->present = 0;
78 return;
80 #endif /* CONFIG_SCSI_EATA_DMA || CONFIG_SCSI_EATA_PIO */
83 * WIN_IDENTIFY returns little-endian info,
84 * WIN_PIDENTIFY *usually* returns little-endian info.
86 if (cmd == WIN_PIDENTIFY) {
87 if ((id->model[0] == 'N' && id->model[1] == 'E') /* NEC */
88 || (id->model[0] == 'F' && id->model[1] == 'X') /* Mitsumi */
89 || (id->model[0] == 'P' && id->model[1] == 'i'))/* Pioneer */
90 bswap ^= 1; /* Vertos drives may still be weird */
92 ide_fixstring (id->model, sizeof(id->model), bswap);
93 ide_fixstring (id->fw_rev, sizeof(id->fw_rev), bswap);
94 ide_fixstring (id->serial_no, sizeof(id->serial_no), bswap);
96 if (strstr(id->model, "E X A B Y T E N E S T"))
97 return;
99 id->model[sizeof(id->model)-1] = '\0'; /* we depend on this a lot! */
100 printk("%s: %s, ", drive->name, id->model);
101 drive->present = 1;
104 * Check for an ATAPI device
106 if (cmd == WIN_PIDENTIFY) {
107 byte type = (id->config >> 8) & 0x1f;
108 printk("ATAPI ");
109 #ifdef CONFIG_BLK_DEV_PDC4030
110 if (HWIF(drive)->channel == 1 && HWIF(drive)->chipset == ide_pdc4030) {
111 printk(" -- not supported on 2nd Promise port\n");
112 drive->present = 0;
113 return;
115 #endif /* CONFIG_BLK_DEV_PDC4030 */
116 switch (type) {
117 case ide_floppy:
118 if (!strstr(id->model, "CD-ROM")) {
119 if (!strstr(id->model, "oppy") && !strstr(id->model, "poyp") && !strstr(id->model, "ZIP"))
120 printk("cdrom or floppy?, assuming ");
121 if (drive->media != ide_cdrom) {
122 printk ("FLOPPY");
123 break;
126 type = ide_cdrom; /* Early cdrom models used zero */
127 case ide_cdrom:
128 drive->removable = 1;
129 #ifdef CONFIG_PPC
130 /* kludge for Apple PowerBook internal zip */
131 if (!strstr(id->model, "CD-ROM") && strstr(id->model, "ZIP")) {
132 printk ("FLOPPY");
133 type = ide_floppy;
134 break;
136 #endif
137 printk ("CDROM");
138 break;
139 case ide_tape:
140 printk ("TAPE");
141 break;
142 case ide_optical:
143 printk ("OPTICAL");
144 drive->removable = 1;
145 break;
146 default:
147 printk("UNKNOWN (type %d)", type);
148 break;
150 printk (" drive\n");
151 drive->media = type;
152 return;
156 * Not an ATAPI device: looks like a "regular" hard disk
158 if (id->config & (1<<7))
159 drive->removable = 1;
161 * Prevent long system lockup probing later for non-existant
162 * slave drive if the hwif is actually a flash memory card of some variety:
164 if (drive_is_flashcard(drive)) {
165 ide_drive_t *mate = &HWIF(drive)->drives[1^drive->select.b.unit];
166 if (!mate->ata_flash) {
167 mate->present = 0;
168 mate->noprobe = 1;
171 drive->media = ide_disk;
172 printk("ATA DISK drive\n");
173 QUIRK_LIST(HWIF(drive),drive);
174 return;
178 * try_to_identify() sends an ATA(PI) IDENTIFY request to a drive
179 * and waits for a response. It also monitors irqs while this is
180 * happening, in hope of automatically determining which one is
181 * being used by the interface.
183 * Returns: 0 device was identified
184 * 1 device timed-out (no response to identify request)
185 * 2 device aborted the command (refused to identify itself)
187 static int try_to_identify (ide_drive_t *drive, byte cmd)
189 int rc;
190 ide_ioreg_t hd_status;
191 unsigned long timeout;
192 unsigned long irqs = 0;
193 byte s, a;
195 if (IDE_CONTROL_REG) {
196 if (!HWIF(drive)->irq) { /* already got an IRQ? */
197 probe_irq_off(probe_irq_on()); /* clear dangling irqs */
198 irqs = probe_irq_on(); /* start monitoring irqs */
199 OUT_BYTE(drive->ctl,IDE_CONTROL_REG); /* enable device irq */
202 ide_delay_50ms(); /* take a deep breath */
203 a = IN_BYTE(IDE_ALTSTATUS_REG);
204 s = IN_BYTE(IDE_STATUS_REG);
205 if ((a ^ s) & ~INDEX_STAT) {
206 printk("%s: probing with STATUS(0x%02x) instead of ALTSTATUS(0x%02x)\n", drive->name, s, a);
207 hd_status = IDE_STATUS_REG; /* ancient Seagate drives, broken interfaces */
208 } else {
209 hd_status = IDE_ALTSTATUS_REG; /* use non-intrusive polling */
211 } else {
212 ide_delay_50ms();
213 hd_status = IDE_STATUS_REG;
216 /* set features register for atapi identify command to be sure of reply */
217 if ((cmd == WIN_PIDENTIFY))
218 OUT_BYTE(0,IDE_FEATURE_REG); /* disable dma & overlap */
220 #if CONFIG_BLK_DEV_PDC4030
221 if (HWIF(drive)->chipset == ide_pdc4030) {
222 /* DC4030 hosted drives need their own identify... */
223 extern int pdc4030_identify(ide_drive_t *);
224 if (pdc4030_identify(drive)) {
225 if (irqs)
226 (void) probe_irq_off(irqs);
227 return 1;
229 } else
230 #endif /* CONFIG_BLK_DEV_PDC4030 */
231 OUT_BYTE(cmd,IDE_COMMAND_REG); /* ask drive for ID */
232 timeout = ((cmd == WIN_IDENTIFY) ? WAIT_WORSTCASE : WAIT_PIDENTIFY) / 2;
233 timeout += jiffies;
234 do {
235 if (0 < (signed long)(jiffies - timeout)) {
236 if (irqs)
237 (void) probe_irq_off(irqs);
238 return 1; /* drive timed-out */
240 ide_delay_50ms(); /* give drive a breather */
241 } while (IN_BYTE(hd_status) & BUSY_STAT);
243 ide_delay_50ms(); /* wait for IRQ and DRQ_STAT */
244 if (OK_STAT(GET_STAT(),DRQ_STAT,BAD_R_STAT)) {
245 unsigned long flags;
246 __save_flags(flags); /* local CPU only */
247 __cli(); /* local CPU only; some systems need this */
248 do_identify(drive, cmd); /* drive returned ID */
249 rc = 0; /* drive responded with ID */
250 (void) GET_STAT(); /* clear drive IRQ */
251 __restore_flags(flags); /* local CPU only */
252 } else
253 rc = 2; /* drive refused ID */
254 if (IDE_CONTROL_REG && !HWIF(drive)->irq) {
255 irqs = probe_irq_off(irqs); /* get our irq number */
256 if (irqs > 0) {
257 HWIF(drive)->irq = irqs; /* save it for later */
258 irqs = probe_irq_on();
259 OUT_BYTE(drive->ctl|2,IDE_CONTROL_REG); /* mask device irq */
260 udelay(5);
261 (void) probe_irq_off(irqs);
262 (void) probe_irq_off(probe_irq_on()); /* clear self-inflicted irq */
263 (void) GET_STAT(); /* clear drive IRQ */
265 } else { /* Mmmm.. multiple IRQs.. don't know which was ours */
266 printk("%s: IRQ probe failed (%ld)\n", drive->name, irqs);
267 #ifdef CONFIG_BLK_DEV_CMD640
268 #ifdef CMD640_DUMP_REGS
269 if (HWIF(drive)->chipset == ide_cmd640) {
270 printk("%s: Hmmm.. probably a driver problem.\n", drive->name);
271 CMD640_DUMP_REGS;
273 #endif /* CMD640_DUMP_REGS */
274 #endif /* CONFIG_BLK_DEV_CMD640 */
277 return rc;
281 * do_probe() has the difficult job of finding a drive if it exists,
282 * without getting hung up if it doesn't exist, without trampling on
283 * ethernet cards, and without leaving any IRQs dangling to haunt us later.
285 * If a drive is "known" to exist (from CMOS or kernel parameters),
286 * but does not respond right away, the probe will "hang in there"
287 * for the maximum wait time (about 30 seconds), otherwise it will
288 * exit much more quickly.
290 * Returns: 0 device was identified
291 * 1 device timed-out (no response to identify request)
292 * 2 device aborted the command (refused to identify itself)
293 * 3 bad status from device (possible for ATAPI drives)
294 * 4 probe was not attempted because failure was obvious
296 static int do_probe (ide_drive_t *drive, byte cmd)
298 int rc;
299 ide_hwif_t *hwif = HWIF(drive);
300 if (drive->present) { /* avoid waiting for inappropriate probes */
301 if ((drive->media != ide_disk) && (cmd == WIN_IDENTIFY))
302 return 4;
304 #ifdef DEBUG
305 printk("probing for %s: present=%d, media=%d, probetype=%s\n",
306 drive->name, drive->present, drive->media,
307 (cmd == WIN_IDENTIFY) ? "ATA" : "ATAPI");
308 #endif
309 ide_delay_50ms(); /* needed for some systems (e.g. crw9624 as drive0 with disk as slave) */
310 SELECT_DRIVE(hwif,drive);
311 ide_delay_50ms();
312 if (IN_BYTE(IDE_SELECT_REG) != drive->select.all && !drive->present) {
313 if (drive->select.b.unit != 0) {
314 SELECT_DRIVE(hwif,&hwif->drives[0]); /* exit with drive0 selected */
315 ide_delay_50ms(); /* allow BUSY_STAT to assert & clear */
317 return 3; /* no i/f present: mmm.. this should be a 4 -ml */
320 if (OK_STAT(GET_STAT(),READY_STAT,BUSY_STAT)
321 || drive->present || cmd == WIN_PIDENTIFY)
323 if ((rc = try_to_identify(drive,cmd))) /* send cmd and wait */
324 rc = try_to_identify(drive,cmd); /* failed: try again */
325 if (rc == 1 && cmd == WIN_PIDENTIFY && drive->autotune != 2) {
326 unsigned long timeout;
327 printk("%s: no response (status = 0x%02x), resetting drive\n", drive->name, GET_STAT());
328 ide_delay_50ms();
329 OUT_BYTE (drive->select.all, IDE_SELECT_REG);
330 ide_delay_50ms();
331 OUT_BYTE(WIN_SRST, IDE_COMMAND_REG);
332 timeout = jiffies;
333 while ((GET_STAT() & BUSY_STAT) && time_before(jiffies, timeout + WAIT_WORSTCASE))
334 ide_delay_50ms();
335 rc = try_to_identify(drive, cmd);
337 if (rc == 1)
338 printk("%s: no response (status = 0x%02x)\n", drive->name, GET_STAT());
339 (void) GET_STAT(); /* ensure drive irq is clear */
340 } else {
341 rc = 3; /* not present or maybe ATAPI */
343 if (drive->select.b.unit != 0) {
344 SELECT_DRIVE(hwif,&hwif->drives[0]); /* exit with drive0 selected */
345 ide_delay_50ms();
346 (void) GET_STAT(); /* ensure drive irq is clear */
348 return rc;
354 static void enable_nest (ide_drive_t *drive)
356 unsigned long timeout;
358 printk("%s: enabling %s -- ", HWIF(drive)->name, drive->id->model);
359 SELECT_DRIVE(HWIF(drive), drive);
360 ide_delay_50ms();
361 OUT_BYTE(EXABYTE_ENABLE_NEST, IDE_COMMAND_REG);
362 timeout = jiffies + WAIT_WORSTCASE;
363 do {
364 if (jiffies > timeout) {
365 printk("failed (timeout)\n");
366 return;
368 ide_delay_50ms();
369 } while (GET_STAT() & BUSY_STAT);
370 ide_delay_50ms();
371 if (!OK_STAT(GET_STAT(), 0, BAD_STAT))
372 printk("failed (status = 0x%02x)\n", GET_STAT());
373 else
374 printk("success\n");
375 if (do_probe(drive, WIN_IDENTIFY) >= 2) { /* if !(success||timed-out) */
376 (void) do_probe(drive, WIN_PIDENTIFY); /* look for ATAPI device */
381 * probe_for_drive() tests for existence of a given drive using do_probe().
383 * Returns: 0 no device was found
384 * 1 device was found (note: drive->present might still be 0)
386 static inline byte probe_for_drive (ide_drive_t *drive)
388 if (drive->noprobe) /* skip probing? */
389 return drive->present;
390 if (do_probe(drive, WIN_IDENTIFY) >= 2) { /* if !(success||timed-out) */
391 (void) do_probe(drive, WIN_PIDENTIFY); /* look for ATAPI device */
393 if (drive->id && strstr(drive->id->model, "E X A B Y T E N E S T"))
394 enable_nest(drive);
395 if (!drive->present)
396 return 0; /* drive not found */
397 if (drive->id == NULL) { /* identification failed? */
398 if (drive->media == ide_disk) {
399 printk ("%s: non-IDE drive, CHS=%d/%d/%d\n",
400 drive->name, drive->cyl, drive->head, drive->sect);
401 } else if (drive->media == ide_cdrom) {
402 printk("%s: ATAPI cdrom (?)\n", drive->name);
403 } else {
404 drive->present = 0; /* nuke it */
407 return 1; /* drive was found */
411 * Calculate the region that this interface occupies,
412 * handling interfaces where the registers may not be
413 * ordered sanely. We deal with the CONTROL register
414 * separately.
416 static int hwif_check_regions (ide_hwif_t *hwif)
418 int region_errors = 0;
420 hwif->straight8 = 0;
421 region_errors = ide_check_region(hwif->io_ports[IDE_DATA_OFFSET], 1);
422 region_errors += ide_check_region(hwif->io_ports[IDE_ERROR_OFFSET], 1);
423 region_errors += ide_check_region(hwif->io_ports[IDE_NSECTOR_OFFSET], 1);
424 region_errors += ide_check_region(hwif->io_ports[IDE_SECTOR_OFFSET], 1);
425 region_errors += ide_check_region(hwif->io_ports[IDE_LCYL_OFFSET], 1);
426 region_errors += ide_check_region(hwif->io_ports[IDE_HCYL_OFFSET], 1);
427 region_errors += ide_check_region(hwif->io_ports[IDE_SELECT_OFFSET], 1);
428 region_errors += ide_check_region(hwif->io_ports[IDE_STATUS_OFFSET], 1);
430 if (hwif->io_ports[IDE_CONTROL_OFFSET])
431 region_errors += ide_check_region(hwif->io_ports[IDE_CONTROL_OFFSET], 1);
432 #if defined(CONFIG_AMIGA) || defined(CONFIG_MAC)
433 if (hwif->io_ports[IDE_IRQ_OFFSET])
434 region_errors += ide_check_region(hwif->io_ports[IDE_IRQ_OFFSET], 1);
435 #endif /* (CONFIG_AMIGA) || (CONFIG_MAC) */
437 * If any errors are return, we drop the hwif interface.
439 return(region_errors);
442 static void hwif_register (ide_hwif_t *hwif)
444 if (((unsigned long)hwif->io_ports[IDE_DATA_OFFSET] | 7) ==
445 ((unsigned long)hwif->io_ports[IDE_STATUS_OFFSET])) {
446 ide_request_region(hwif->io_ports[IDE_DATA_OFFSET], 8, hwif->name);
447 hwif->straight8 = 1;
448 goto jump_straight8;
451 if (hwif->io_ports[IDE_DATA_OFFSET])
452 ide_request_region(hwif->io_ports[IDE_DATA_OFFSET], 1, hwif->name);
453 if (hwif->io_ports[IDE_ERROR_OFFSET])
454 ide_request_region(hwif->io_ports[IDE_ERROR_OFFSET], 1, hwif->name);
455 if (hwif->io_ports[IDE_NSECTOR_OFFSET])
456 ide_request_region(hwif->io_ports[IDE_NSECTOR_OFFSET], 1, hwif->name);
457 if (hwif->io_ports[IDE_SECTOR_OFFSET])
458 ide_request_region(hwif->io_ports[IDE_SECTOR_OFFSET], 1, hwif->name);
459 if (hwif->io_ports[IDE_LCYL_OFFSET])
460 ide_request_region(hwif->io_ports[IDE_LCYL_OFFSET], 1, hwif->name);
461 if (hwif->io_ports[IDE_HCYL_OFFSET])
462 ide_request_region(hwif->io_ports[IDE_HCYL_OFFSET], 1, hwif->name);
463 if (hwif->io_ports[IDE_SELECT_OFFSET])
464 ide_request_region(hwif->io_ports[IDE_SELECT_OFFSET], 1, hwif->name);
465 if (hwif->io_ports[IDE_STATUS_OFFSET])
466 ide_request_region(hwif->io_ports[IDE_STATUS_OFFSET], 1, hwif->name);
468 jump_straight8:
469 if (hwif->io_ports[IDE_CONTROL_OFFSET])
470 ide_request_region(hwif->io_ports[IDE_CONTROL_OFFSET], 1, hwif->name);
471 #if defined(CONFIG_AMIGA) || defined(CONFIG_MAC)
472 if (hwif->io_ports[IDE_IRQ_OFFSET])
473 ide_request_region(hwif->io_ports[IDE_IRQ_OFFSET], 1, hwif->name);
474 #endif /* (CONFIG_AMIGA) || (CONFIG_MAC) */
478 * This routine only knows how to look for drive units 0 and 1
479 * on an interface, so any setting of MAX_DRIVES > 2 won't work here.
481 static void probe_hwif (ide_hwif_t *hwif)
483 unsigned int unit;
484 unsigned long flags;
486 if (hwif->noprobe)
487 return;
488 #ifdef CONFIG_BLK_DEV_IDE
489 if (hwif->io_ports[IDE_DATA_OFFSET] == HD_DATA) {
490 extern void probe_cmos_for_drives(ide_hwif_t *);
492 probe_cmos_for_drives (hwif);
494 #endif
496 if ((hwif->chipset != ide_4drives || !hwif->mate->present) &&
497 #if CONFIG_BLK_DEV_PDC4030
498 (hwif->chipset != ide_pdc4030 || hwif->channel == 0) &&
499 #endif /* CONFIG_BLK_DEV_PDC4030 */
500 (hwif_check_regions(hwif))) {
501 int msgout = 0;
502 for (unit = 0; unit < MAX_DRIVES; ++unit) {
503 ide_drive_t *drive = &hwif->drives[unit];
504 if (drive->present) {
505 drive->present = 0;
506 printk("%s: ERROR, PORTS ALREADY IN USE\n", drive->name);
507 msgout = 1;
510 if (!msgout)
511 printk("%s: ports already in use, skipping probe\n", hwif->name);
512 return;
515 __save_flags(flags); /* local CPU only */
516 __sti(); /* local CPU only; needed for jiffies and irq probing */
518 * Second drive should only exist if first drive was found,
519 * but a lot of cdrom drives are configured as single slaves.
521 for (unit = 0; unit < MAX_DRIVES; ++unit) {
522 ide_drive_t *drive = &hwif->drives[unit];
523 (void) probe_for_drive (drive);
524 if (drive->present && !hwif->present) {
525 hwif->present = 1;
526 if (hwif->chipset != ide_4drives || !hwif->mate->present) {
527 hwif_register(hwif);
531 if (hwif->io_ports[IDE_CONTROL_OFFSET] && hwif->reset) {
532 unsigned long timeout = jiffies + WAIT_WORSTCASE;
533 byte stat;
535 printk("%s: reset\n", hwif->name);
536 OUT_BYTE(12, hwif->io_ports[IDE_CONTROL_OFFSET]);
537 udelay(10);
538 OUT_BYTE(8, hwif->io_ports[IDE_CONTROL_OFFSET]);
539 do {
540 ide_delay_50ms();
541 stat = IN_BYTE(hwif->io_ports[IDE_STATUS_OFFSET]);
542 } while ((stat & BUSY_STAT) && 0 < (signed long)(timeout - jiffies));
545 __restore_flags(flags); /* local CPU only */
546 for (unit = 0; unit < MAX_DRIVES; ++unit) {
547 ide_drive_t *drive = &hwif->drives[unit];
548 if (drive->present) {
549 ide_tuneproc_t *tuneproc = HWIF(drive)->tuneproc;
550 if (tuneproc != NULL && drive->autotune == 1)
551 tuneproc(drive, 255); /* auto-tune PIO mode */
556 #if MAX_HWIFS > 1
558 * save_match() is used to simplify logic in init_irq() below.
560 * A loophole here is that we may not know about a particular
561 * hwif's irq until after that hwif is actually probed/initialized..
562 * This could be a problem for the case where an hwif is on a
563 * dual interface that requires serialization (eg. cmd640) and another
564 * hwif using one of the same irqs is initialized beforehand.
566 * This routine detects and reports such situations, but does not fix them.
568 static void save_match (ide_hwif_t *hwif, ide_hwif_t *new, ide_hwif_t **match)
570 ide_hwif_t *m = *match;
572 if (m && m->hwgroup && m->hwgroup != new->hwgroup) {
573 if (!new->hwgroup)
574 return;
575 printk("%s: potential irq problem with %s and %s\n", hwif->name, new->name, m->name);
577 if (!m || m->irq != hwif->irq) /* don't undo a prior perfect match */
578 *match = new;
580 #endif /* MAX_HWIFS > 1 */
583 * init request queue
585 static void ide_init_queue(ide_drive_t *drive)
587 request_queue_t *q = &drive->queue;
589 q->queuedata = HWGROUP(drive);
590 blk_init_queue(q, do_ide_request);
594 * This routine sets up the irq for an ide interface, and creates a new
595 * hwgroup for the irq/hwif if none was previously assigned.
597 * Much of the code is for correctly detecting/handling irq sharing
598 * and irq serialization situations. This is somewhat complex because
599 * it handles static as well as dynamic (PCMCIA) IDE interfaces.
601 * The SA_INTERRUPT in sa_flags means ide_intr() is always entered with
602 * interrupts completely disabled. This can be bad for interrupt latency,
603 * but anything else has led to problems on some machines. We re-enable
604 * interrupts as much as we can safely do in most places.
606 static int init_irq (ide_hwif_t *hwif)
608 unsigned long flags;
609 unsigned int index;
610 ide_hwgroup_t *hwgroup;
611 ide_hwif_t *match = NULL;
613 save_flags(flags); /* all CPUs */
614 cli(); /* all CPUs */
616 hwif->hwgroup = NULL;
617 #if MAX_HWIFS > 1
619 * Group up with any other hwifs that share our irq(s).
621 for (index = 0; index < MAX_HWIFS; index++) {
622 ide_hwif_t *h = &ide_hwifs[index];
623 if (h->hwgroup) { /* scan only initialized hwif's */
624 if (hwif->irq == h->irq) {
625 hwif->sharing_irq = h->sharing_irq = 1;
626 if (hwif->chipset != ide_pci || h->chipset != ide_pci) {
627 save_match(hwif, h, &match);
630 if (hwif->serialized) {
631 if (hwif->mate && hwif->mate->irq == h->irq)
632 save_match(hwif, h, &match);
634 if (h->serialized) {
635 if (h->mate && hwif->irq == h->mate->irq)
636 save_match(hwif, h, &match);
640 #endif /* MAX_HWIFS > 1 */
642 * If we are still without a hwgroup, then form a new one
644 if (match) {
645 hwgroup = match->hwgroup;
646 } else {
647 hwgroup = kmalloc(sizeof(ide_hwgroup_t), GFP_KERNEL);
648 memset(hwgroup, 0, sizeof(ide_hwgroup_t));
649 hwgroup->hwif = hwif->next = hwif;
650 hwgroup->rq = NULL;
651 hwgroup->handler = NULL;
652 hwgroup->drive = NULL;
653 hwgroup->busy = 0;
654 init_timer(&hwgroup->timer);
655 hwgroup->timer.function = &ide_timer_expiry;
656 hwgroup->timer.data = (unsigned long) hwgroup;
660 * Allocate the irq, if not already obtained for another hwif
662 if (!match || match->irq != hwif->irq) {
663 #ifdef CONFIG_IDEPCI_SHARE_IRQ
664 int sa = (hwif->chipset == ide_pci) ? SA_SHIRQ : SA_INTERRUPT;
665 #else /* !CONFIG_IDEPCI_SHARE_IRQ */
666 int sa = (hwif->chipset == ide_pci) ? SA_INTERRUPT|SA_SHIRQ : SA_INTERRUPT;
667 #endif /* CONFIG_IDEPCI_SHARE_IRQ */
668 if (ide_request_irq(hwif->irq, &ide_intr, sa, hwif->name, hwgroup)) {
669 if (!match)
670 kfree(hwgroup);
671 restore_flags(flags); /* all CPUs */
672 return 1;
677 * Everything is okay, so link us into the hwgroup
679 hwif->hwgroup = hwgroup;
680 hwif->next = hwgroup->hwif->next;
681 hwgroup->hwif->next = hwif;
683 for (index = 0; index < MAX_DRIVES; ++index) {
684 ide_drive_t *drive = &hwif->drives[index];
685 if (!drive->present)
686 continue;
687 if (!hwgroup->drive)
688 hwgroup->drive = drive;
689 drive->next = hwgroup->drive->next;
690 hwgroup->drive->next = drive;
691 ide_init_queue(drive);
693 if (!hwgroup->hwif) {
694 hwgroup->hwif = HWIF(hwgroup->drive);
695 #ifdef DEBUG
696 printk("%s : Adding missed hwif to hwgroup!!\n", hwif->name);
697 #endif
699 restore_flags(flags); /* all CPUs; safe now that hwif->hwgroup is set up */
701 #if !defined(__mc68000__) && !defined(CONFIG_APUS) && !defined(__sparc__)
702 printk("%s at 0x%03x-0x%03x,0x%03x on irq %d", hwif->name,
703 hwif->io_ports[IDE_DATA_OFFSET],
704 hwif->io_ports[IDE_DATA_OFFSET]+7,
705 hwif->io_ports[IDE_CONTROL_OFFSET], hwif->irq);
706 #elif defined(__sparc__)
707 printk("%s at 0x%03lx-0x%03lx,0x%03lx on irq %s", hwif->name,
708 hwif->io_ports[IDE_DATA_OFFSET],
709 hwif->io_ports[IDE_DATA_OFFSET]+7,
710 hwif->io_ports[IDE_CONTROL_OFFSET], __irq_itoa(hwif->irq));
711 #else
712 printk("%s at %p on irq 0x%08x", hwif->name,
713 hwif->io_ports[IDE_DATA_OFFSET], hwif->irq);
714 #endif /* __mc68000__ && CONFIG_APUS */
715 if (match)
716 printk(" (%sed with %s)",
717 hwif->sharing_irq ? "shar" : "serializ", match->name);
718 printk("\n");
719 return 0;
723 * init_gendisk() (as opposed to ide_geninit) is called for each major device,
724 * after probing for drives, to allocate partition tables and other data
725 * structures needed for the routines in genhd.c. ide_geninit() gets called
726 * somewhat later, during the partition check.
728 static void init_gendisk (ide_hwif_t *hwif)
730 struct gendisk *gd, **gdp;
731 unsigned int unit, units, minors;
732 int *bs, *max_sect, *max_ra;
733 extern devfs_handle_t ide_devfs_handle;
735 /* figure out maximum drive number on the interface */
736 for (units = MAX_DRIVES; units > 0; --units) {
737 if (hwif->drives[units-1].present)
738 break;
740 minors = units * (1<<PARTN_BITS);
741 gd = kmalloc (sizeof(struct gendisk), GFP_KERNEL);
742 gd->sizes = kmalloc (minors * sizeof(int), GFP_KERNEL);
743 gd->part = kmalloc (minors * sizeof(struct hd_struct), GFP_KERNEL);
744 bs = kmalloc (minors*sizeof(int), GFP_KERNEL);
745 max_sect = kmalloc (minors*sizeof(int), GFP_KERNEL);
746 max_ra = kmalloc (minors*sizeof(int), GFP_KERNEL);
748 memset(gd->part, 0, minors * sizeof(struct hd_struct));
750 /* cdroms and msdos f/s are examples of non-1024 blocksizes */
751 blksize_size[hwif->major] = bs;
752 max_sectors[hwif->major] = max_sect;
753 max_readahead[hwif->major] = max_ra;
754 for (unit = 0; unit < minors; ++unit) {
755 *bs++ = BLOCK_SIZE;
756 #ifdef CONFIG_BLK_DEV_PDC4030
757 *max_sect++ = ((hwif->chipset == ide_pdc4030) ? 127 : MAX_SECTORS);
758 #else
759 *max_sect++ = MAX_SECTORS;
760 #endif
761 *max_ra++ = MAX_READAHEAD;
764 for (unit = 0; unit < units; ++unit)
765 hwif->drives[unit].part = &gd->part[unit << PARTN_BITS];
767 gd->major = hwif->major; /* our major device number */
768 gd->major_name = IDE_MAJOR_NAME; /* treated special in genhd.c */
769 gd->minor_shift = PARTN_BITS; /* num bits for partitions */
770 gd->max_p = 1<<PARTN_BITS; /* 1 + max partitions / drive */
771 gd->nr_real = units; /* current num real drives */
772 gd->real_devices= hwif; /* ptr to internal data */
773 gd->next = NULL; /* linked list of major devs */
774 gd->fops = ide_fops; /* file operations */
775 gd->de_arr = kmalloc (sizeof *gd->de_arr * units, GFP_KERNEL);
776 gd->flags = kmalloc (sizeof *gd->flags * units, GFP_KERNEL);
777 if (gd->de_arr)
778 memset (gd->de_arr, 0, sizeof *gd->de_arr * units);
779 if (gd->flags)
780 memset (gd->flags, 0, sizeof *gd->flags * units);
782 for (gdp = &gendisk_head; *gdp; gdp = &((*gdp)->next)) ;
783 hwif->gd = *gdp = gd; /* link onto tail of list */
785 for (unit = 0; unit < units; ++unit) {
786 if (hwif->drives[unit].present) {
787 char name[64];
789 ide_add_generic_settings(hwif->drives + unit);
790 hwif->drives[unit].dn = ((hwif->channel ? 2 : 0) + unit);
791 sprintf (name, "host%d/bus%d/target%d/lun%d",
792 (hwif->channel && hwif->mate) ? hwif->mate->index : hwif->index,
793 hwif->channel, unit, hwif->drives[unit].lun);
794 hwif->drives[unit].de =
795 devfs_mk_dir (ide_devfs_handle, name, NULL);
800 static int hwif_init (ide_hwif_t *hwif)
802 if (!hwif->present)
803 return 0;
804 if (!hwif->irq) {
805 if (!(hwif->irq = ide_default_irq(hwif->io_ports[IDE_DATA_OFFSET])))
807 printk("%s: DISABLED, NO IRQ\n", hwif->name);
808 return (hwif->present = 0);
811 #ifdef CONFIG_BLK_DEV_HD
812 if (hwif->irq == HD_IRQ && hwif->io_ports[IDE_DATA_OFFSET] != HD_DATA) {
813 printk("%s: CANNOT SHARE IRQ WITH OLD HARDDISK DRIVER (hd.c)\n", hwif->name);
814 return (hwif->present = 0);
816 #endif /* CONFIG_BLK_DEV_HD */
818 hwif->present = 0; /* we set it back to 1 if all is ok below */
820 if (devfs_register_blkdev (hwif->major, hwif->name, ide_fops)) {
821 printk("%s: UNABLE TO GET MAJOR NUMBER %d\n", hwif->name, hwif->major);
822 return (hwif->present = 0);
825 if (init_irq(hwif)) {
826 int i = hwif->irq;
828 * It failed to initialise. Find the default IRQ for
829 * this port and try that.
831 if (!(hwif->irq = ide_default_irq(hwif->io_ports[IDE_DATA_OFFSET]))) {
832 printk("%s: Disabled unable to get IRQ %d.\n", hwif->name, i);
833 (void) unregister_blkdev (hwif->major, hwif->name);
834 return (hwif->present = 0);
836 if (init_irq(hwif)) {
837 printk("%s: probed IRQ %d and default IRQ %d failed.\n",
838 hwif->name, i, hwif->irq);
839 (void) unregister_blkdev (hwif->major, hwif->name);
840 return (hwif->present = 0);
842 printk("%s: probed IRQ %d failed, using default.\n",
843 hwif->name, hwif->irq);
846 init_gendisk(hwif);
847 blk_dev[hwif->major].data = hwif;
848 blk_dev[hwif->major].queue = ide_get_queue;
849 read_ahead[hwif->major] = 8; /* (4kB) */
850 hwif->present = 1; /* success */
852 #if (DEBUG_SPINLOCK > 0)
854 static int done = 0;
855 if (!done++)
856 printk("io_request_lock is %p\n", &io_request_lock); /* FIXME */
858 #endif
859 return hwif->present;
862 int ideprobe_init (void);
863 static ide_module_t ideprobe_module = {
864 IDE_PROBE_MODULE,
865 ideprobe_init,
866 NULL
869 int ideprobe_init (void)
871 unsigned int index;
872 int probe[MAX_HWIFS];
874 MOD_INC_USE_COUNT;
875 memset(probe, 0, MAX_HWIFS * sizeof(int));
876 for (index = 0; index < MAX_HWIFS; ++index)
877 probe[index] = !ide_hwifs[index].present;
880 * Probe for drives in the usual way.. CMOS/BIOS, then poke at ports
882 for (index = 0; index < MAX_HWIFS; ++index)
883 if (probe[index])
884 probe_hwif(&ide_hwifs[index]);
885 for (index = 0; index < MAX_HWIFS; ++index)
886 if (probe[index])
887 hwif_init(&ide_hwifs[index]);
888 if (!ide_probe)
889 ide_probe = &ideprobe_module;
890 MOD_DEC_USE_COUNT;
891 return 0;
894 #ifdef MODULE
895 int init_module (void)
897 unsigned int index;
899 for (index = 0; index < MAX_HWIFS; ++index)
900 ide_unregister(index);
901 ideprobe_init();
902 create_proc_ide_interfaces();
903 return 0;
906 void cleanup_module (void)
908 ide_probe = NULL;
910 #endif /* MODULE */