~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

Linux Cross Reference
Linux/drivers/ide/ide-probe.c

Version: ~ [ 2.2.5 ] ~ [ 2.4.1 ] ~ [ 2.4.9 ] ~ [ 2.6.17.10 ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 /*
  2  *  linux/drivers/ide/ide-probe.c       Version 1.06    June 9, 2000
  3  *
  4  *  Copyright (C) 1994-1998  Linus Torvalds & authors (see below)
  5  */
  6 
  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>
 11  *
 12  *  See linux/MAINTAINERS for address of current maintainer.
 13  *
 14  * This is the IDE probe module, as evolved from hd.c and ide.c.
 15  *
 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
 22  *
 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.
 28  */
 29 
 30 #undef REALLY_SLOW_IO           /* most systems can safely undef this */
 31 
 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>
 47 
 48 #include <asm/byteorder.h>
 49 #include <asm/irq.h>
 50 #include <asm/uaccess.h>
 51 #include <asm/io.h>
 52 
 53 static inline void do_identify (ide_drive_t *drive, byte cmd)
 54 {
 55         int bswap = 1;
 56         struct hd_driveid *id;
 57 
 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);
 62 
 63         if (id->word156 == 0x4d42) {
 64                 printk("%s: drive->id->word156 == 0x%04x \n", drive->name, drive->id->word156);
 65         }
 66 
 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)
 70         /*
 71          * EATA SCSI controllers do a hardware ATA emulation:
 72          * Ignore them if there is a driver for them available.
 73          */
 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;
 79         }
 80 #endif /* CONFIG_SCSI_EATA_DMA || CONFIG_SCSI_EATA_PIO */
 81 
 82         /*
 83          *  WIN_IDENTIFY returns little-endian info,
 84          *  WIN_PIDENTIFY *usually* returns little-endian info.
 85          */
 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 */
 91         }
 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);
 95 
 96         if (strstr(id->model, "E X A B Y T E N E S T"))
 97                 return;
 98 
 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;
102 
103         /*
104          * Check for an ATAPI device
105          */
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;
114                 }
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;
124                                         }
125                                 }
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;
135                                 }
136 #endif
137                                 printk ("CD/DVD-ROM");
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;
149                 }
150                 printk (" drive\n");
151                 drive->media = type;
152                 return;
153         }
154 
155         /*
156          * Not an ATAPI device: looks like a "regular" hard disk
157          */
158         if (id->config & (1<<7))
159                 drive->removable = 1;
160         /*
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:
163          */
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;
169                 }
170         }
171         drive->media = ide_disk;
172         printk("ATA DISK drive\n");
173         QUIRK_LIST(HWIF(drive),drive);
174         return;
175 }
176 
177 /*
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.
182  *
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)
186  */
187 static int actual_try_to_identify (ide_drive_t *drive, byte cmd)
188 {
189         int rc;
190         ide_ioreg_t hd_status;
191         unsigned long timeout;
192         byte s, a;
193 
194         if (IDE_CONTROL_REG) {
195                 /* take a deep breath */
196                 ide_delay_50ms();
197                 a = IN_BYTE(IDE_ALTSTATUS_REG);
198                 s = IN_BYTE(IDE_STATUS_REG);
199                 if ((a ^ s) & ~INDEX_STAT) {
200                         printk("%s: probing with STATUS(0x%02x) instead of ALTSTATUS(0x%02x)\n", drive->name, s, a);
201                         hd_status = IDE_STATUS_REG;     /* ancient Seagate drives, broken interfaces */
202                 } else {
203                         hd_status = IDE_ALTSTATUS_REG;  /* use non-intrusive polling */
204                 }
205         } else {
206                 ide_delay_50ms();
207                 hd_status = IDE_STATUS_REG;
208         }
209 
210         /* set features register for atapi identify command to be sure of reply */
211         if ((cmd == WIN_PIDENTIFY))
212                 OUT_BYTE(0,IDE_FEATURE_REG);    /* disable dma & overlap */
213 
214 #if CONFIG_BLK_DEV_PDC4030
215         if (HWIF(drive)->chipset == ide_pdc4030) {
216                 /* DC4030 hosted drives need their own identify... */
217                 extern int pdc4030_identify(ide_drive_t *);
218                 if (pdc4030_identify(drive)) {
219                         return 1;
220                 }
221         } else
222 #endif /* CONFIG_BLK_DEV_PDC4030 */
223                 OUT_BYTE(cmd,IDE_COMMAND_REG);          /* ask drive for ID */
224         timeout = ((cmd == WIN_IDENTIFY) ? WAIT_WORSTCASE : WAIT_PIDENTIFY) / 2;
225         timeout += jiffies;
226         do {
227                 if (0 < (signed long)(jiffies - timeout)) {
228                         return 1;       /* drive timed-out */
229                 }
230                 ide_delay_50ms();               /* give drive a breather */
231         } while (IN_BYTE(hd_status) & BUSY_STAT);
232 
233         ide_delay_50ms();               /* wait for IRQ and DRQ_STAT */
234         if (OK_STAT(GET_STAT(),DRQ_STAT,BAD_R_STAT)) {
235                 unsigned long flags;
236                 __save_flags(flags);    /* local CPU only */
237                 __cli();                /* local CPU only; some systems need this */
238                 do_identify(drive, cmd); /* drive returned ID */
239                 rc = 0;                 /* drive responded with ID */
240                 (void) GET_STAT();      /* clear drive IRQ */
241                 __restore_flags(flags); /* local CPU only */
242         } else
243                 rc = 2;                 /* drive refused ID */
244         return rc;
245 }
246 
247 static int try_to_identify (ide_drive_t *drive, byte cmd)
248 {
249         int retval;
250         int autoprobe = 0;
251         unsigned long cookie = 0;
252 
253         if (IDE_CONTROL_REG && !HWIF(drive)->irq) {
254                 autoprobe = 1;
255                 cookie = probe_irq_on();
256                 OUT_BYTE(drive->ctl,IDE_CONTROL_REG);   /* enable device irq */
257         }
258 
259         retval = actual_try_to_identify(drive, cmd);
260 
261         if (autoprobe) {
262                 int irq;
263                 OUT_BYTE(drive->ctl|2,IDE_CONTROL_REG); /* mask device irq */
264                 (void) GET_STAT();                      /* clear drive IRQ */
265                 udelay(5);
266                 irq = probe_irq_off(cookie);
267                 if (!HWIF(drive)->irq) {
268                         if (irq > 0) {
269                                 HWIF(drive)->irq = irq;
270                         } else {        /* Mmmm.. multiple IRQs.. don't know which was ours */
271                                 printk("%s: IRQ probe failed (0x%lx)\n", drive->name, cookie);
272 #ifdef CONFIG_BLK_DEV_CMD640
273 #ifdef CMD640_DUMP_REGS
274                                 if (HWIF(drive)->chipset == ide_cmd640) {
275                                         printk("%s: Hmmm.. probably a driver problem.\n", drive->name);
276                                         CMD640_DUMP_REGS;
277                                 }
278 #endif /* CMD640_DUMP_REGS */
279 #endif /* CONFIG_BLK_DEV_CMD640 */
280                         }
281                 }
282         }
283         return retval;
284 }
285 
286 
287 /*
288  * do_probe() has the difficult job of finding a drive if it exists,
289  * without getting hung up if it doesn't exist, without trampling on
290  * ethernet cards, and without leaving any IRQs dangling to haunt us later.
291  *
292  * If a drive is "known" to exist (from CMOS or kernel parameters),
293  * but does not respond right away, the probe will "hang in there"
294  * for the maximum wait time (about 30 seconds), otherwise it will
295  * exit much more quickly.
296  *
297  * Returns:     0  device was identified
298  *              1  device timed-out (no response to identify request)
299  *              2  device aborted the command (refused to identify itself)
300  *              3  bad status from device (possible for ATAPI drives)
301  *              4  probe was not attempted because failure was obvious
302  */
303 static int do_probe (ide_drive_t *drive, byte cmd)
304 {
305         int rc;
306         ide_hwif_t *hwif = HWIF(drive);
307         if (drive->present) {   /* avoid waiting for inappropriate probes */
308                 if ((drive->media != ide_disk) && (cmd == WIN_IDENTIFY))
309                         return 4;
310         }
311 #ifdef DEBUG
312         printk("probing for %s: present=%d, media=%d, probetype=%s\n",
313                 drive->name, drive->present, drive->media,
314                 (cmd == WIN_IDENTIFY) ? "ATA" : "ATAPI");
315 #endif
316         ide_delay_50ms();       /* needed for some systems (e.g. crw9624 as drive0 with disk as slave) */
317         SELECT_DRIVE(hwif,drive);
318         ide_delay_50ms();
319         if (IN_BYTE(IDE_SELECT_REG) != drive->select.all && !drive->present) {
320                 if (drive->select.b.unit != 0) {
321                         SELECT_DRIVE(hwif,&hwif->drives[0]);    /* exit with drive0 selected */
322                         ide_delay_50ms();               /* allow BUSY_STAT to assert & clear */
323                 }
324                 return 3;    /* no i/f present: mmm.. this should be a 4 -ml */
325         }
326 
327         if (OK_STAT(GET_STAT(),READY_STAT,BUSY_STAT)
328          || drive->present || cmd == WIN_PIDENTIFY)
329         {
330                 if ((rc = try_to_identify(drive,cmd)))   /* send cmd and wait */
331                         rc = try_to_identify(drive,cmd); /* failed: try again */
332                 if (rc == 1 && cmd == WIN_PIDENTIFY && drive->autotune != 2) {
333                         unsigned long timeout;
334                         printk("%s: no response (status = 0x%02x), resetting drive\n", drive->name, GET_STAT());
335                         ide_delay_50ms();
336                         OUT_BYTE (drive->select.all, IDE_SELECT_REG);
337                         ide_delay_50ms();
338                         OUT_BYTE(WIN_SRST, IDE_COMMAND_REG);
339                         timeout = jiffies;
340                         while ((GET_STAT() & BUSY_STAT) && time_before(jiffies, timeout + WAIT_WORSTCASE))
341                                 ide_delay_50ms();
342                         rc = try_to_identify(drive, cmd);
343                 }
344                 if (rc == 1)
345                         printk("%s: no response (status = 0x%02x)\n", drive->name, GET_STAT());
346                 (void) GET_STAT();              /* ensure drive irq is clear */
347         } else {
348                 rc = 3;                         /* not present or maybe ATAPI */
349         }
350         if (drive->select.b.unit != 0) {
351                 SELECT_DRIVE(hwif,&hwif->drives[0]);    /* exit with drive0 selected */
352                 ide_delay_50ms();
353                 (void) GET_STAT();              /* ensure drive irq is clear */
354         }
355         return rc;
356 }
357 
358 /*
359  *
360  */
361 static void enable_nest (ide_drive_t *drive)
362 {
363         unsigned long timeout;
364 
365         printk("%s: enabling %s -- ", HWIF(drive)->name, drive->id->model);
366         SELECT_DRIVE(HWIF(drive), drive);
367         ide_delay_50ms();
368         OUT_BYTE(EXABYTE_ENABLE_NEST, IDE_COMMAND_REG);
369         timeout = jiffies + WAIT_WORSTCASE;
370         do {
371                 if (jiffies > timeout) {
372                         printk("failed (timeout)\n");
373                         return;
374                 }
375                 ide_delay_50ms();
376         } while (GET_STAT() & BUSY_STAT);
377         ide_delay_50ms();
378         if (!OK_STAT(GET_STAT(), 0, BAD_STAT))
379                 printk("failed (status = 0x%02x)\n", GET_STAT());
380         else
381                 printk("success\n");
382         if (do_probe(drive, WIN_IDENTIFY) >= 2) {       /* if !(success||timed-out) */
383                 (void) do_probe(drive, WIN_PIDENTIFY);  /* look for ATAPI device */
384         }
385 }
386 
387 /*
388  * probe_for_drive() tests for existence of a given drive using do_probe().
389  *
390  * Returns:     0  no device was found
391  *              1  device was found (note: drive->present might still be 0)
392  */
393 static inline byte probe_for_drive (ide_drive_t *drive)
394 {
395         if (drive->noprobe)                     /* skip probing? */
396                 return drive->present;
397         if (do_probe(drive, WIN_IDENTIFY) >= 2) { /* if !(success||timed-out) */
398                 (void) do_probe(drive, WIN_PIDENTIFY); /* look for ATAPI device */
399         }
400         if (drive->id && strstr(drive->id->model, "E X A B Y T E N E S T"))
401                 enable_nest(drive);
402         if (!drive->present)
403                 return 0;                       /* drive not found */
404         if (drive->id == NULL) {                /* identification failed? */
405                 if (drive->media == ide_disk) {
406                         printk ("%s: non-IDE drive, CHS=%d/%d/%d\n",
407                          drive->name, drive->cyl, drive->head, drive->sect);
408                 } else if (drive->media == ide_cdrom) {
409                         printk("%s: ATAPI cdrom (?)\n", drive->name);
410                 } else {
411                         drive->present = 0;     /* nuke it */
412                 }
413         }
414         return 1;       /* drive was found */
415 }
416 
417 /*
418  * Calculate the region that this interface occupies,
419  * handling interfaces where the registers may not be
420  * ordered sanely.  We deal with the CONTROL register
421  * separately.
422  */
423 static int hwif_check_regions (ide_hwif_t *hwif)
424 {
425         int region_errors = 0;
426 
427         hwif->straight8 = 0;
428         region_errors  = ide_check_region(hwif->io_ports[IDE_DATA_OFFSET], 1);
429         region_errors += ide_check_region(hwif->io_ports[IDE_ERROR_OFFSET], 1);
430         region_errors += ide_check_region(hwif->io_ports[IDE_NSECTOR_OFFSET], 1);
431         region_errors += ide_check_region(hwif->io_ports[IDE_SECTOR_OFFSET], 1);
432         region_errors += ide_check_region(hwif->io_ports[IDE_LCYL_OFFSET], 1);
433         region_errors += ide_check_region(hwif->io_ports[IDE_HCYL_OFFSET], 1);
434         region_errors += ide_check_region(hwif->io_ports[IDE_SELECT_OFFSET], 1);
435         region_errors += ide_check_region(hwif->io_ports[IDE_STATUS_OFFSET], 1);
436 
437         if (hwif->io_ports[IDE_CONTROL_OFFSET])
438                 region_errors += ide_check_region(hwif->io_ports[IDE_CONTROL_OFFSET], 1);
439 #if defined(CONFIG_AMIGA) || defined(CONFIG_MAC)
440         if (hwif->io_ports[IDE_IRQ_OFFSET])
441                 region_errors += ide_check_region(hwif->io_ports[IDE_IRQ_OFFSET], 1);
442 #endif /* (CONFIG_AMIGA) || (CONFIG_MAC) */
443         /*
444          * If any errors are return, we drop the hwif interface.
445          */
446         return(region_errors);
447 }
448 
449 static void hwif_register (ide_hwif_t *hwif)
450 {
451         if (((unsigned long)hwif->io_ports[IDE_DATA_OFFSET] | 7) ==
452             ((unsigned long)hwif->io_ports[IDE_STATUS_OFFSET])) {
453                 ide_request_region(hwif->io_ports[IDE_DATA_OFFSET], 8, hwif->name);
454                 hwif->straight8 = 1;
455                 goto jump_straight8;
456         }
457 
458         if (hwif->io_ports[IDE_DATA_OFFSET])
459                 ide_request_region(hwif->io_ports[IDE_DATA_OFFSET], 1, hwif->name);
460         if (hwif->io_ports[IDE_ERROR_OFFSET])
461                 ide_request_region(hwif->io_ports[IDE_ERROR_OFFSET], 1, hwif->name);
462         if (hwif->io_ports[IDE_NSECTOR_OFFSET])
463                 ide_request_region(hwif->io_ports[IDE_NSECTOR_OFFSET], 1, hwif->name);
464         if (hwif->io_ports[IDE_SECTOR_OFFSET])
465                 ide_request_region(hwif->io_ports[IDE_SECTOR_OFFSET], 1, hwif->name);
466         if (hwif->io_ports[IDE_LCYL_OFFSET])
467                 ide_request_region(hwif->io_ports[IDE_LCYL_OFFSET], 1, hwif->name);
468         if (hwif->io_ports[IDE_HCYL_OFFSET])
469                 ide_request_region(hwif->io_ports[IDE_HCYL_OFFSET], 1, hwif->name);
470         if (hwif->io_ports[IDE_SELECT_OFFSET])
471                 ide_request_region(hwif->io_ports[IDE_SELECT_OFFSET], 1, hwif->name);
472         if (hwif->io_ports[IDE_STATUS_OFFSET])
473                 ide_request_region(hwif->io_ports[IDE_STATUS_OFFSET], 1, hwif->name);
474 
475 jump_straight8:
476         if (hwif->io_ports[IDE_CONTROL_OFFSET])
477                 ide_request_region(hwif->io_ports[IDE_CONTROL_OFFSET], 1, hwif->name);
478 #if defined(CONFIG_AMIGA) || defined(CONFIG_MAC)
479         if (hwif->io_ports[IDE_IRQ_OFFSET])
480                 ide_request_region(hwif->io_ports[IDE_IRQ_OFFSET], 1, hwif->name);
481 #endif /* (CONFIG_AMIGA) || (CONFIG_MAC) */
482 }
483 
484 /*
485  * This routine only knows how to look for drive units 0 and 1
486  * on an interface, so any setting of MAX_DRIVES > 2 won't work here.
487  */
488 static void probe_hwif (ide_hwif_t *hwif)
489 {
490         unsigned int unit;
491         unsigned long flags;
492 
493         if (hwif->noprobe)
494                 return;
495 #ifdef CONFIG_BLK_DEV_IDE
496         if (hwif->io_ports[IDE_DATA_OFFSET] == HD_DATA) {
497                 extern void probe_cmos_for_drives(ide_hwif_t *);
498 
499                 probe_cmos_for_drives (hwif);
500         }
501 #endif
502 
503         if ((hwif->chipset != ide_4drives || !hwif->mate->present) &&
504 #if CONFIG_BLK_DEV_PDC4030
505             (hwif->chipset != ide_pdc4030 || hwif->channel == 0) &&
506 #endif /* CONFIG_BLK_DEV_PDC4030 */
507             (hwif_check_regions(hwif))) {
508                 int msgout = 0;
509                 for (unit = 0; unit < MAX_DRIVES; ++unit) {
510                         ide_drive_t *drive = &hwif->drives[unit];
511                         if (drive->present) {
512                                 drive->present = 0;
513                                 printk("%s: ERROR, PORTS ALREADY IN USE\n", drive->name);
514                                 msgout = 1;
515                         }
516                 }
517                 if (!msgout)
518                         printk("%s: ports already in use, skipping probe\n", hwif->name);
519                 return; 
520         }
521 
522         __save_flags(flags);    /* local CPU only */
523         __sti();                /* local CPU only; needed for jiffies and irq probing */
524         /*
525          * Second drive should only exist if first drive was found,
526          * but a lot of cdrom drives are configured as single slaves.
527          */
528         for (unit = 0; unit < MAX_DRIVES; ++unit) {
529                 ide_drive_t *drive = &hwif->drives[unit];
530                 (void) probe_for_drive (drive);
531                 if (drive->present && !hwif->present) {
532                         hwif->present = 1;
533                         if (hwif->chipset != ide_4drives || !hwif->mate->present) {
534                                 hwif_register(hwif);
535                         }
536                 }
537         }
538         if (hwif->io_ports[IDE_CONTROL_OFFSET] && hwif->reset) {
539                 unsigned long timeout = jiffies + WAIT_WORSTCASE;
540                 byte stat;
541 
542                 printk("%s: reset\n", hwif->name);
543                 OUT_BYTE(12, hwif->io_ports[IDE_CONTROL_OFFSET]);
544                 udelay(10);
545                 OUT_BYTE(8, hwif->io_ports[IDE_CONTROL_OFFSET]);
546                 do {
547                         ide_delay_50ms();
548                         stat = IN_BYTE(hwif->io_ports[IDE_STATUS_OFFSET]);
549                 } while ((stat & BUSY_STAT) && 0 < (signed long)(timeout - jiffies));
550 
551         }
552         __restore_flags(flags); /* local CPU only */
553         for (unit = 0; unit < MAX_DRIVES; ++unit) {
554                 ide_drive_t *drive = &hwif->drives[unit];
555                 if (drive->present) {
556                         ide_tuneproc_t *tuneproc = HWIF(drive)->tuneproc;
557                         if (tuneproc != NULL && drive->autotune == 1)
558                                 tuneproc(drive, 255);   /* auto-tune PIO mode */
559                 }
560         }
561 }
562 
563 #if MAX_HWIFS > 1
564 /*
565  * save_match() is used to simplify logic in init_irq() below.
566  *
567  * A loophole here is that we may not know about a particular
568  * hwif's irq until after that hwif is actually probed/initialized..
569  * This could be a problem for the case where an hwif is on a
570  * dual interface that requires serialization (eg. cmd640) and another
571  * hwif using one of the same irqs is initialized beforehand.
572  *
573  * This routine detects and reports such situations, but does not fix them.
574  */
575 static void save_match (ide_hwif_t *hwif, ide_hwif_t *new, ide_hwif_t **match)
576 {
577         ide_hwif_t *m = *match;
578 
579         if (m && m->hwgroup && m->hwgroup != new->hwgroup) {
580                 if (!new->hwgroup)
581                         return;
582                 printk("%s: potential irq problem with %s and %s\n", hwif->name, new->name, m->name);
583         }
584         if (!m || m->irq != hwif->irq) /* don't undo a prior perfect match */
585                 *match = new;
586 }
587 #endif /* MAX_HWIFS > 1 */
588 
589 /*
590  * init request queue
591  */
592 static void ide_init_queue(ide_drive_t *drive)
593 {
594         request_queue_t *q = &drive->queue;
595 
596         q->queuedata = HWGROUP(drive);
597         blk_init_queue(q, do_ide_request);
598 }
599 
600 /*
601  * This routine sets up the irq for an ide interface, and creates a new
602  * hwgroup for the irq/hwif if none was previously assigned.
603  *
604  * Much of the code is for correctly detecting/handling irq sharing
605  * and irq serialization situations.  This is somewhat complex because
606  * it handles static as well as dynamic (PCMCIA) IDE interfaces.
607  *
608  * The SA_INTERRUPT in sa_flags means ide_intr() is always entered with
609  * interrupts completely disabled.  This can be bad for interrupt latency,
610  * but anything else has led to problems on some machines.  We re-enable
611  * interrupts as much as we can safely do in most places.
612  */
613 static int init_irq (ide_hwif_t *hwif)
614 {
615         unsigned long flags;
616         unsigned int index;
617         ide_hwgroup_t *hwgroup;
618         ide_hwif_t *match = NULL;
619 
620         save_flags(flags);      /* all CPUs */
621         cli();                  /* all CPUs */
622 
623         hwif->hwgroup = NULL;
624 #if MAX_HWIFS > 1
625         /*
626          * Group up with any other hwifs that share our irq(s).
627          */
628         for (index = 0; index < MAX_HWIFS; index++) {
629                 ide_hwif_t *h = &ide_hwifs[index];
630                 if (h->hwgroup) {  /* scan only initialized hwif's */
631                         if (hwif->irq == h->irq) {
632                                 hwif->sharing_irq = h->sharing_irq = 1;
633                                 if (hwif->chipset != ide_pci || h->chipset != ide_pci) {
634                                         save_match(hwif, h, &match);
635                                 }
636                         }
637                         if (hwif->serialized) {
638                                 if (hwif->mate && hwif->mate->irq == h->irq)
639                                         save_match(hwif, h, &match);
640                         }
641                         if (h->serialized) {
642                                 if (h->mate && hwif->irq == h->mate->irq)
643                                         save_match(hwif, h, &match);
644                         }
645                 }
646         }
647 #endif /* MAX_HWIFS > 1 */
648         /*
649          * If we are still without a hwgroup, then form a new one
650          */
651         if (match) {
652                 hwgroup = match->hwgroup;
653         } else {
654                 hwgroup = kmalloc(sizeof(ide_hwgroup_t), GFP_KERNEL);
655                 memset(hwgroup, 0, sizeof(ide_hwgroup_t));
656                 hwgroup->hwif     = hwif->next = hwif;
657                 hwgroup->rq       = NULL;
658                 hwgroup->handler  = NULL;
659                 hwgroup->drive    = NULL;
660                 hwgroup->busy     = 0;
661                 init_timer(&hwgroup->timer);
662                 hwgroup->timer.function = &ide_timer_expiry;
663                 hwgroup->timer.data = (unsigned long) hwgroup;
664         }
665 
666         /*
667          * Allocate the irq, if not already obtained for another hwif
668          */
669         if (!match || match->irq != hwif->irq) {
670 #ifdef CONFIG_IDEPCI_SHARE_IRQ
671                 int sa = (hwif->chipset == ide_pci) ? SA_SHIRQ : SA_INTERRUPT;
672 #else /* !CONFIG_IDEPCI_SHARE_IRQ */
673                 int sa = (hwif->chipset == ide_pci) ? SA_INTERRUPT|SA_SHIRQ : SA_INTERRUPT;
674 #endif /* CONFIG_IDEPCI_SHARE_IRQ */
675                 if (ide_request_irq(hwif->irq, &ide_intr, sa, hwif->name, hwgroup)) {
676                         if (!match)
677                                 kfree(hwgroup);
678                         restore_flags(flags);   /* all CPUs */
679                         return 1;
680                 }
681         }
682 
683         /*
684          * Everything is okay, so link us into the hwgroup
685          */
686         hwif->hwgroup = hwgroup;
687         hwif->next = hwgroup->hwif->next;
688         hwgroup->hwif->next = hwif;
689 
690         for (index = 0; index < MAX_DRIVES; ++index) {
691                 ide_drive_t *drive = &hwif->drives[index];
692                 if (!drive->present)
693                         continue;
694                 if (!hwgroup->drive)
695                         hwgroup->drive = drive;
696                 drive->next = hwgroup->drive->next;
697                 hwgroup->drive->next = drive;
698                 ide_init_queue(drive);
699         }
700         if (!hwgroup->hwif) {
701                 hwgroup->hwif = HWIF(hwgroup->drive);
702 #ifdef DEBUG
703                 printk("%s : Adding missed hwif to hwgroup!!\n", hwif->name);
704 #endif
705         }
706         restore_flags(flags);   /* all CPUs; safe now that hwif->hwgroup is set up */
707 
708 #if !defined(__mc68000__) && !defined(CONFIG_APUS) && !defined(__sparc__)
709         printk("%s at 0x%03x-0x%03x,0x%03x on irq %d", hwif->name,
710                 hwif->io_ports[IDE_DATA_OFFSET],
711                 hwif->io_ports[IDE_DATA_OFFSET]+7,
712                 hwif->io_ports[IDE_CONTROL_OFFSET], hwif->irq);
713 #elif defined(__sparc__)
714         printk("%s at 0x%03lx-0x%03lx,0x%03lx on irq %s", hwif->name,
715                 hwif->io_ports[IDE_DATA_OFFSET],
716                 hwif->io_ports[IDE_DATA_OFFSET]+7,
717                 hwif->io_ports[IDE_CONTROL_OFFSET], __irq_itoa(hwif->irq));
718 #else
719         printk("%s at %p on irq 0x%08x", hwif->name,
720                 hwif->io_ports[IDE_DATA_OFFSET], hwif->irq);
721 #endif /* __mc68000__ && CONFIG_APUS */
722         if (match)
723                 printk(" (%sed with %s)",
724                         hwif->sharing_irq ? "shar" : "serializ", match->name);
725         printk("\n");
726         return 0;
727 }
728 
729 /*
730  * init_gendisk() (as opposed to ide_geninit) is called for each major device,
731  * after probing for drives, to allocate partition tables and other data
732  * structures needed for the routines in genhd.c.  ide_geninit() gets called
733  * somewhat later, during the partition check.
734  */
735 static void init_gendisk (ide_hwif_t *hwif)
736 {
737         struct gendisk *gd, **gdp;
738         unsigned int unit, units, minors;
739         int *bs, *max_sect, *max_ra;
740         extern devfs_handle_t ide_devfs_handle;
741 
742         /* figure out maximum drive number on the interface */
743         for (units = MAX_DRIVES; units > 0; --units) {
744                 if (hwif->drives[units-1].present)
745                         break;
746         }
747         minors    = units * (1<<PARTN_BITS);
748         gd        = kmalloc (sizeof(struct gendisk), GFP_KERNEL);
749         gd->sizes = kmalloc (minors * sizeof(int), GFP_KERNEL);
750         gd->part  = kmalloc (minors * sizeof(struct hd_struct), GFP_KERNEL);
751         bs        = kmalloc (minors*sizeof(int), GFP_KERNEL);
752         max_sect  = kmalloc (minors*sizeof(int), GFP_KERNEL);
753         max_ra    = kmalloc (minors*sizeof(int), GFP_KERNEL);
754 
755         memset(gd->part, 0, minors * sizeof(struct hd_struct));
756 
757         /* cdroms and msdos f/s are examples of non-1024 blocksizes */
758         blksize_size[hwif->major] = bs;
759         max_sectors[hwif->major] = max_sect;
760         max_readahead[hwif->major] = max_ra;
761         for (unit = 0; unit < minors; ++unit) {
762                 *bs++ = BLOCK_SIZE;
763 #ifdef CONFIG_BLK_DEV_PDC4030
764                 *max_sect++ = ((hwif->chipset == ide_pdc4030) ? 127 : 256);
765 #else
766                 /* IDE can do up to 128K per request. */
767                 *max_sect++ = 256;
768 #endif
769                 *max_ra++ = MAX_READAHEAD;
770         }
771 
772         for (unit = 0; unit < units; ++unit)
773                 hwif->drives[unit].part = &gd->part[unit << PARTN_BITS];
774 
775         gd->major       = hwif->major;          /* our major device number */
776         gd->major_name  = IDE_MAJOR_NAME;       /* treated special in genhd.c */
777         gd->minor_shift = PARTN_BITS;           /* num bits for partitions */
778         gd->max_p       = 1<<PARTN_BITS;        /* 1 + max partitions / drive */
779         gd->nr_real     = units;                /* current num real drives */
780         gd->real_devices= hwif;                 /* ptr to internal data */
781         gd->next        = NULL;                 /* linked list of major devs */
782         gd->fops        = ide_fops;             /* file operations */
783         gd->de_arr      = kmalloc (sizeof *gd->de_arr * units, GFP_KERNEL);
784         gd->flags       = kmalloc (sizeof *gd->flags * units, GFP_KERNEL);
785         if (gd->de_arr)
786                 memset (gd->de_arr, 0, sizeof *gd->de_arr * units);
787         if (gd->flags)
788                 memset (gd->flags, 0, sizeof *gd->flags * units);
789 
790         for (gdp = &gendisk_head; *gdp; gdp = &((*gdp)->next)) ;
791         hwif->gd = *gdp = gd;                   /* link onto tail of list */
792 
793         for (unit = 0; unit < units; ++unit) {
794                 if (hwif->drives[unit].present) {
795                         char name[64];
796 
797                         ide_add_generic_settings(hwif->drives + unit);
798                         hwif->drives[unit].dn = ((hwif->channel ? 2 : 0) + unit);
799                         sprintf (name, "host%d/bus%d/target%d/lun%d",
800                                  (hwif->channel && hwif->mate) ? hwif->mate->index : hwif->index,
801                                  hwif->channel, unit, hwif->drives[unit].lun);
802                         hwif->drives[unit].de =
803                                 devfs_mk_dir (ide_devfs_handle, name, NULL);
804                 }
805         }
806 }
807 
808 static int hwif_init (ide_hwif_t *hwif)
809 {
810         if (!hwif->present)
811                 return 0;
812         if (!hwif->irq) {
813                 if (!(hwif->irq = ide_default_irq(hwif->io_ports[IDE_DATA_OFFSET])))
814                 {
815                         printk("%s: DISABLED, NO IRQ\n", hwif->name);
816                         return (hwif->present = 0);
817                 }
818         }
819 #ifdef CONFIG_BLK_DEV_HD
820         if (hwif->irq == HD_IRQ && hwif->io_ports[IDE_DATA_OFFSET] != HD_DATA) {
821                 printk("%s: CANNOT SHARE IRQ WITH OLD HARDDISK DRIVER (hd.c)\n", hwif->name);
822                 return (hwif->present = 0);
823         }
824 #endif /* CONFIG_BLK_DEV_HD */
825         
826         hwif->present = 0; /* we set it back to 1 if all is ok below */
827 
828         if (devfs_register_blkdev (hwif->major, hwif->name, ide_fops)) {
829                 printk("%s: UNABLE TO GET MAJOR NUMBER %d\n", hwif->name, hwif->major);
830                 return (hwif->present = 0);
831         }
832         
833         if (init_irq(hwif)) {
834                 int i = hwif->irq;
835                 /*
836                  *      It failed to initialise. Find the default IRQ for 
837                  *      this port and try that.
838                  */
839                 if (!(hwif->irq = ide_default_irq(hwif->io_ports[IDE_DATA_OFFSET]))) {
840                         printk("%s: Disabled unable to get IRQ %d.\n", hwif->name, i);
841                         (void) unregister_blkdev (hwif->major, hwif->name);
842                         return (hwif->present = 0);
843                 }
844                 if (init_irq(hwif)) {
845                         printk("%s: probed IRQ %d and default IRQ %d failed.\n",
846                                 hwif->name, i, hwif->irq);
847                         (void) unregister_blkdev (hwif->major, hwif->name);
848                         return (hwif->present = 0);
849                 }
850                 printk("%s: probed IRQ %d failed, using default.\n",
851                         hwif->name, hwif->irq);
852         }
853         
854         init_gendisk(hwif);
855         blk_dev[hwif->major].data = hwif;
856         blk_dev[hwif->major].queue = ide_get_queue;
857         read_ahead[hwif->major] = 8;    /* (4kB) */
858         hwif->present = 1;      /* success */
859 
860 #if (DEBUG_SPINLOCK > 0)
861 {
862         static int done = 0;
863         if (!done++)
864                 printk("io_request_lock is %p\n", &io_request_lock);    /* FIXME */
865 }
866 #endif
867         return hwif->present;
868 }
869 
870 int ideprobe_init (void);
871 static ide_module_t ideprobe_module = {
872         IDE_PROBE_MODULE,
873         ideprobe_init,
874         NULL
875 };
876 
877 int ideprobe_init (void)
878 {
879         unsigned int index;
880         int probe[MAX_HWIFS];
881         
882         MOD_INC_USE_COUNT;
883         memset(probe, 0, MAX_HWIFS * sizeof(int));
884         for (index = 0; index < MAX_HWIFS; ++index)
885                 probe[index] = !ide_hwifs[index].present;
886 
887         /*
888          * Probe for drives in the usual way.. CMOS/BIOS, then poke at ports
889          */
890         for (index = 0; index < MAX_HWIFS; ++index)
891                 if (probe[index])
892                         probe_hwif(&ide_hwifs[index]);
893         for (index = 0; index < MAX_HWIFS; ++index)
894                 if (probe[index])
895                         hwif_init(&ide_hwifs[index]);
896         if (!ide_probe)
897                 ide_probe = &ideprobe_module;
898         MOD_DEC_USE_COUNT;
899         return 0;
900 }
901 
902 #ifdef MODULE
903 int init_module (void)
904 {
905         unsigned int index;
906         
907         for (index = 0; index < MAX_HWIFS; ++index)
908                 ide_unregister(index);
909         ideprobe_init();
910         create_proc_ide_interfaces();
911         return 0;
912 }
913 
914 void cleanup_module (void)
915 {
916         ide_probe = NULL;
917 }
918 #endif /* MODULE */
919 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

This page was automatically generated by the LXR engine.
Visit the LXR main site for more information.