More meth updates.
[linux-2.6/linux-mips.git] / drivers / ide / probe.c
blob15c07ff80d61b51afb0221a88e74cb097a1962a2
1 /**** vi:set ts=8 sts=8 sw=8:************************************************
3 * Copyright (C) 1994-1998 Linus Torvalds & authors (see below)
5 * Mostly written by Mark Lord <mlord@pobox.com>
6 * and Gadi Oxman <gadio@netvision.net.il>
7 * and Andre Hedrick <andre@linux-ide.org>
9 * See linux/MAINTAINERS for address of current maintainer.
14 * This is roughly the code related to device detection and
15 * device id handling.
18 #include <linux/config.h>
19 #include <linux/module.h>
20 #include <linux/types.h>
21 #include <linux/string.h>
22 #include <linux/kernel.h>
23 #include <linux/timer.h>
24 #include <linux/mm.h>
25 #include <linux/interrupt.h>
26 #include <linux/major.h>
27 #include <linux/errno.h>
28 #include <linux/genhd.h>
29 #include <linux/slab.h>
30 #include <linux/delay.h>
31 #include <linux/spinlock.h>
32 #include <linux/pci.h>
33 #include <linux/hdreg.h>
34 #include <linux/ide.h>
36 #include <asm/byteorder.h>
37 #include <asm/irq.h>
38 #include <asm/uaccess.h>
39 #include <asm/io.h>
41 extern struct ata_device * get_info_ptr(kdev_t);
44 * This is called from the partition-table code in pt/msdos.c
45 * to invent a translated geometry.
47 * This is suppressed if the user specifies an explicit geometry.
49 * The ptheads parameter is either 0 or tells about the number of
50 * heads shown by the end of the first nonempty partition.
51 * If this is either 16, 32, 64, 128, 240 or 255 we'll believe it.
53 * The xparm parameter has the following meaning:
54 * 0 = convert to CHS with fewer than 1024 cyls
55 * using the same method as Ontrack DiskManager.
56 * 1 = same as "0", plus offset everything by 63 sectors.
57 * -1 = similar to "0", plus redirect sector 0 to sector 1.
58 * 2 = convert to a CHS geometry with "ptheads" heads.
60 * Returns 0 if the translation was not possible, if the device was not
61 * an IDE disk drive, or if a geometry was "forced" on the commandline.
62 * Returns 1 if the geometry translation was successful.
64 int ide_xlate_1024(kdev_t i_rdev, int xparm, int ptheads, const char *msg)
66 struct ata_device *drive;
67 const char *msg1 = "";
68 int heads = 0;
69 int c, h, s;
70 int transl = 1; /* try translation */
71 int ret = 0;
73 drive = get_info_ptr(i_rdev);
74 if (!drive)
75 return 0;
77 /* There used to be code here that assigned drive->id->CHS to
78 * drive->CHS and that to drive->bios_CHS. However, some disks have
79 * id->C/H/S = 4092/16/63 but are larger than 2.1 GB. In such cases
80 * that code was wrong. Moreover, there seems to be no reason to do
81 * any of these things.
83 * Please note that recent RedHat changes to the disk utils are bogous
84 * and will report spurious errors.
87 /* translate? */
88 if (drive->forced_geom)
89 transl = 0;
91 /* does ptheads look reasonable? */
92 if (ptheads == 32 || ptheads == 64 || ptheads == 128 ||
93 ptheads == 240 || ptheads == 255)
94 heads = ptheads;
96 if (xparm == 2) {
97 if (!heads ||
98 (drive->bios_head >= heads && drive->bios_sect == 63))
99 transl = 0;
101 if (xparm == -1) {
102 if (drive->bios_head > 16)
103 transl = 0; /* we already have a translation */
106 if (transl) {
107 static const u8 dm_head_vals[] = {4, 8, 16, 32, 64, 128, 255, 0};
108 const u8 *headp = dm_head_vals;
109 unsigned long total;
112 * If heads is nonzero: find a translation with this many heads
113 * and S=63. Otherwise: find out how OnTrack Disk Manager
114 * would translate the disk.
116 * The specs say: take geometry as obtained from Identify,
117 * compute total capacity C*H*S from that, and truncate to
118 * 1024*255*63. Now take S=63, H the first in the sequence 4,
119 * 8, 16, 32, 64, 128, 255 such that 63*H*1024 >= total.
120 * [Please tell aeb@cwi.nl in case this computes a geometry
121 * different from what OnTrack uses.]
124 total = ata_capacity(drive);
126 s = 63;
128 if (heads) {
129 h = heads;
130 c = total / (63 * heads);
131 } else {
132 while (63 * headp[0] * 1024 < total && headp[1] != 0)
133 headp++;
134 h = headp[0];
135 c = total / (63 * headp[0]);
138 drive->bios_cyl = c;
139 drive->bios_head = h;
140 drive->bios_sect = s;
141 ret = 1;
144 drive->part[0].nr_sects = ata_capacity(drive);
146 if (ret)
147 printk("%s%s [%d/%d/%d]", msg, msg1,
148 drive->bios_cyl, drive->bios_head, drive->bios_sect);
149 return ret;
153 * Drive ID data come as little endian, it needs to be converted on big endian
154 * machines.
156 void ata_fix_driveid(struct hd_driveid *id)
158 #ifndef __LITTLE_ENDIAN
159 # ifdef __BIG_ENDIAN
160 int i;
161 u16 *stringcast;
163 id->config = __le16_to_cpu(id->config);
164 id->cyls = __le16_to_cpu(id->cyls);
165 id->reserved2 = __le16_to_cpu(id->reserved2);
166 id->heads = __le16_to_cpu(id->heads);
167 id->track_bytes = __le16_to_cpu(id->track_bytes);
168 id->sector_bytes = __le16_to_cpu(id->sector_bytes);
169 id->sectors = __le16_to_cpu(id->sectors);
170 id->vendor0 = __le16_to_cpu(id->vendor0);
171 id->vendor1 = __le16_to_cpu(id->vendor1);
172 id->vendor2 = __le16_to_cpu(id->vendor2);
173 stringcast = (u16 *)&id->serial_no[0];
174 for (i = 0; i < (20/2); i++)
175 stringcast[i] = __le16_to_cpu(stringcast[i]);
176 id->buf_type = __le16_to_cpu(id->buf_type);
177 id->buf_size = __le16_to_cpu(id->buf_size);
178 id->ecc_bytes = __le16_to_cpu(id->ecc_bytes);
179 stringcast = (u16 *)&id->fw_rev[0];
180 for (i = 0; i < (8/2); i++)
181 stringcast[i] = __le16_to_cpu(stringcast[i]);
182 stringcast = (u16 *)&id->model[0];
183 for (i = 0; i < (40/2); i++)
184 stringcast[i] = __le16_to_cpu(stringcast[i]);
185 id->dword_io = __le16_to_cpu(id->dword_io);
186 id->reserved50 = __le16_to_cpu(id->reserved50);
187 id->field_valid = __le16_to_cpu(id->field_valid);
188 id->cur_cyls = __le16_to_cpu(id->cur_cyls);
189 id->cur_heads = __le16_to_cpu(id->cur_heads);
190 id->cur_sectors = __le16_to_cpu(id->cur_sectors);
191 id->cur_capacity0 = __le16_to_cpu(id->cur_capacity0);
192 id->cur_capacity1 = __le16_to_cpu(id->cur_capacity1);
193 id->lba_capacity = __le32_to_cpu(id->lba_capacity);
194 id->dma_1word = __le16_to_cpu(id->dma_1word);
195 id->dma_mword = __le16_to_cpu(id->dma_mword);
196 id->eide_pio_modes = __le16_to_cpu(id->eide_pio_modes);
197 id->eide_dma_min = __le16_to_cpu(id->eide_dma_min);
198 id->eide_dma_time = __le16_to_cpu(id->eide_dma_time);
199 id->eide_pio = __le16_to_cpu(id->eide_pio);
200 id->eide_pio_iordy = __le16_to_cpu(id->eide_pio_iordy);
201 for (i = 0; i < 2; ++i)
202 id->words69_70[i] = __le16_to_cpu(id->words69_70[i]);
203 for (i = 0; i < 4; ++i)
204 id->words71_74[i] = __le16_to_cpu(id->words71_74[i]);
205 id->queue_depth = __le16_to_cpu(id->queue_depth);
206 for (i = 0; i < 4; ++i)
207 id->words76_79[i] = __le16_to_cpu(id->words76_79[i]);
208 id->major_rev_num = __le16_to_cpu(id->major_rev_num);
209 id->minor_rev_num = __le16_to_cpu(id->minor_rev_num);
210 id->command_set_1 = __le16_to_cpu(id->command_set_1);
211 id->command_set_2 = __le16_to_cpu(id->command_set_2);
212 id->cfsse = __le16_to_cpu(id->cfsse);
213 id->cfs_enable_1 = __le16_to_cpu(id->cfs_enable_1);
214 id->cfs_enable_2 = __le16_to_cpu(id->cfs_enable_2);
215 id->csf_default = __le16_to_cpu(id->csf_default);
216 id->dma_ultra = __le16_to_cpu(id->dma_ultra);
217 id->word89 = __le16_to_cpu(id->word89);
218 id->word90 = __le16_to_cpu(id->word90);
219 id->CurAPMvalues = __le16_to_cpu(id->CurAPMvalues);
220 id->word92 = __le16_to_cpu(id->word92);
221 id->hw_config = __le16_to_cpu(id->hw_config);
222 id->acoustic = __le16_to_cpu(id->acoustic);
223 for (i = 0; i < 5; i++)
224 id->words95_99[i] = __le16_to_cpu(id->words95_99[i]);
225 id->lba_capacity_2 = __le64_to_cpu(id->lba_capacity_2);
226 for (i = 0; i < 22; i++)
227 id->words104_125[i] = __le16_to_cpu(id->words104_125[i]);
228 id->last_lun = __le16_to_cpu(id->last_lun);
229 id->word127 = __le16_to_cpu(id->word127);
230 id->dlf = __le16_to_cpu(id->dlf);
231 id->csfo = __le16_to_cpu(id->csfo);
232 for (i = 0; i < 26; i++)
233 id->words130_155[i] = __le16_to_cpu(id->words130_155[i]);
234 id->word156 = __le16_to_cpu(id->word156);
235 for (i = 0; i < 3; i++)
236 id->words157_159[i] = __le16_to_cpu(id->words157_159[i]);
237 id->cfa_power = __le16_to_cpu(id->cfa_power);
238 for (i = 0; i < 14; i++)
239 id->words161_175[i] = __le16_to_cpu(id->words161_175[i]);
240 for (i = 0; i < 31; i++)
241 id->words176_205[i] = __le16_to_cpu(id->words176_205[i]);
242 for (i = 0; i < 48; i++)
243 id->words206_254[i] = __le16_to_cpu(id->words206_254[i]);
244 id->integrity_word = __le16_to_cpu(id->integrity_word);
245 # else
246 # error "Please fix <asm/byteorder.h>"
247 # endif
248 #endif
251 void ide_fixstring(char *s, const int bytecount, const int byteswap)
253 char *p = s;
254 char *end = &s[bytecount & ~1]; /* bytecount must be even */
256 if (byteswap) {
257 /* convert from big-endian to host byte order */
258 for (p = end ; p != s;) {
259 unsigned short *pp = (unsigned short *) (p -= 2);
260 *pp = ntohs(*pp);
264 /* strip leading blanks */
265 while (s != end && *s == ' ')
266 ++s;
268 /* compress internal blanks and strip trailing blanks */
269 while (s != end && *s) {
270 if (*s++ != ' ' || (s != end && *s && *s != ' '))
271 *p++ = *(s-1);
274 /* wipe out trailing garbage */
275 while (p != end)
276 *p++ = '\0';
280 * All hosts that use the 80c ribbon must use this!
282 int eighty_ninty_three(struct ata_device *drive)
284 return ((drive->channel->udma_four) &&
285 #ifndef CONFIG_IDEDMA_IVB
286 (drive->id->hw_config & 0x4000) &&
287 #endif
288 (drive->id->hw_config & 0x6000)) ? 1 : 0;
291 /* FIXME: Channel lock should be held.
293 int ide_config_drive_speed(struct ata_device *drive, u8 speed)
295 struct ata_channel *ch = drive->channel;
296 int ret;
298 #if defined(CONFIG_BLK_DEV_IDEDMA) && !defined(__CRIS__)
299 u8 unit = (drive->select.b.unit & 0x01);
300 outb(inb(ch->dma_base + 2) & ~(1 << (5 + unit)), ch->dma_base + 2);
301 #endif
304 * Select the drive, and issue the SETFEATURES command.
306 disable_irq(ch->irq); /* disable_irq_nosync ?? */
307 udelay(1);
308 ata_select(drive, 0);
309 ata_mask(drive);
310 udelay(1);
311 ata_irq_enable(drive, 0);
312 OUT_BYTE(speed, IDE_NSECTOR_REG);
313 OUT_BYTE(SETFEATURES_XFER, IDE_FEATURE_REG);
314 OUT_BYTE(WIN_SETFEATURES, IDE_COMMAND_REG);
315 if (drive->quirk_list == 2)
316 ata_irq_enable(drive, 1);
317 udelay(1);
318 ret = ata_status_poll(drive, 0, BUSY_STAT, WAIT_CMD, NULL);
319 ata_mask(drive);
320 enable_irq(ch->irq);
322 if (ret != ATA_OP_READY) {
323 ata_dump(drive, NULL, "set drive speed");
324 return 1;
327 drive->id->dma_ultra &= ~0xFF00;
328 drive->id->dma_mword &= ~0x0F00;
329 drive->id->dma_1word &= ~0x0F00;
331 #if defined(CONFIG_BLK_DEV_IDEDMA) && !defined(__CRIS__)
332 if (speed > XFER_PIO_4) {
333 outb(inb(ch->dma_base + 2)|(1 << (5 + unit)), ch->dma_base + 2);
334 } else {
335 outb(inb(ch->dma_base + 2) & ~(1 << (5 + unit)), ch->dma_base + 2);
337 #endif
339 switch(speed) {
340 case XFER_UDMA_7: drive->id->dma_ultra |= 0x8080; break;
341 case XFER_UDMA_6: drive->id->dma_ultra |= 0x4040; break;
342 case XFER_UDMA_5: drive->id->dma_ultra |= 0x2020; break;
343 case XFER_UDMA_4: drive->id->dma_ultra |= 0x1010; break;
344 case XFER_UDMA_3: drive->id->dma_ultra |= 0x0808; break;
345 case XFER_UDMA_2: drive->id->dma_ultra |= 0x0404; break;
346 case XFER_UDMA_1: drive->id->dma_ultra |= 0x0202; break;
347 case XFER_UDMA_0: drive->id->dma_ultra |= 0x0101; break;
348 case XFER_MW_DMA_2: drive->id->dma_mword |= 0x0404; break;
349 case XFER_MW_DMA_1: drive->id->dma_mword |= 0x0202; break;
350 case XFER_MW_DMA_0: drive->id->dma_mword |= 0x0101; break;
351 case XFER_SW_DMA_2: drive->id->dma_1word |= 0x0404; break;
352 case XFER_SW_DMA_1: drive->id->dma_1word |= 0x0202; break;
353 case XFER_SW_DMA_0: drive->id->dma_1word |= 0x0101; break;
354 default: break;
357 drive->current_speed = speed;
359 return 0;
362 static inline void do_identify(struct ata_device *drive, u8 cmd)
364 int bswap = 1;
365 struct hd_driveid *id;
367 id = drive->id = kmalloc (SECTOR_WORDS*4, GFP_ATOMIC); /* called with interrupts disabled! */
368 if (!id) {
369 printk(KERN_WARNING "(ide-probe::do_identify) Out of memory.\n");
370 goto err_kmalloc;
373 /* Read 512 bytes of id info.
375 * Please note that it is well known that some *very* old drives are
376 * able to provide only 256 of them, since this was the amount read by
377 * DOS.
379 * However let's try to get away with this...
382 ata_read(drive, id, SECTOR_WORDS);
383 local_irq_enable();
384 ata_fix_driveid(id);
386 if (id->word156 == 0x4d42) {
387 printk("%s: drive->id->word156 == 0x%04x \n", drive->name, drive->id->word156);
390 if (!drive->forced_lun)
391 drive->last_lun = id->last_lun & 0x7;
392 #if defined (CONFIG_SCSI_EATA_DMA) || defined (CONFIG_SCSI_EATA_PIO) || defined (CONFIG_SCSI_EATA)
394 * EATA SCSI controllers do a hardware ATA emulation:
395 * Ignore them if there is a driver for them available.
397 if ((id->model[0] == 'P' && id->model[1] == 'M')
398 || (id->model[0] == 'S' && id->model[1] == 'K')) {
399 printk("%s: EATA SCSI HBA %.10s\n", drive->name, id->model);
400 goto err_misc;
402 #endif
405 * WIN_IDENTIFY returns little-endian info,
406 * WIN_PIDENTIFY *usually* returns little-endian info.
408 if (cmd == WIN_PIDENTIFY) {
409 if ((id->model[0] == 'N' && id->model[1] == 'E') /* NEC */
410 || (id->model[0] == 'F' && id->model[1] == 'X') /* Mitsumi */
411 || (id->model[0] == 'P' && id->model[1] == 'i'))/* Pioneer */
412 bswap ^= 1; /* Vertos drives may still be weird */
414 ide_fixstring(id->model, sizeof(id->model), bswap);
415 ide_fixstring(id->fw_rev, sizeof(id->fw_rev), bswap);
416 ide_fixstring(id->serial_no, sizeof(id->serial_no), bswap);
418 if (strstr(id->model, "E X A B Y T E N E S T"))
419 goto err_misc;
421 id->model[sizeof(id->model)-1] = '\0'; /* we depend on this a lot! */
422 printk("%s: %s, ", drive->name, id->model);
423 drive->present = 1;
426 * Check for an ATAPI device:
428 if (cmd == WIN_PIDENTIFY) {
429 u8 type = (id->config >> 8) & 0x1f;
430 printk("ATAPI ");
431 #ifdef CONFIG_BLK_DEV_PDC4030
432 if (drive->channel->unit == 1 && drive->channel->chipset == ide_pdc4030) {
433 printk(" -- not supported on 2nd Promise port\n");
434 goto err_misc;
436 #endif
437 switch (type) {
438 case ATA_FLOPPY:
439 if (!strstr(id->model, "CD-ROM")) {
440 if (!strstr(id->model, "oppy") && !strstr(id->model, "poyp") && !strstr(id->model, "ZIP"))
441 printk("cdrom or floppy?, assuming ");
442 if (drive->type != ATA_ROM) {
443 printk ("FLOPPY");
444 break;
447 type = ATA_ROM; /* Early cdrom models used zero */
448 case ATA_ROM:
449 drive->removable = 1;
450 #ifdef CONFIG_PPC
451 /* kludge for Apple PowerBook internal zip */
452 if (!strstr(id->model, "CD-ROM") && strstr(id->model, "ZIP")) {
453 printk ("FLOPPY");
454 type = ATA_FLOPPY;
455 break;
457 #endif
458 printk ("CD/DVD-ROM");
459 break;
460 case ATA_TAPE:
461 printk ("TAPE");
462 break;
463 case ATA_MOD:
464 printk ("OPTICAL");
465 drive->removable = 1;
466 break;
467 default:
468 printk("UNKNOWN (type %d)", type);
469 break;
471 printk (" drive\n");
472 drive->type = type;
473 return;
477 * Not an ATAPI device: looks like a "regular" hard disk:
479 if (id->config & (1<<7))
480 drive->removable = 1;
483 * FIXME: This is just plain ugly or plain unnecessary.
485 * Prevent long system lockup probing later for non-existant slave
486 * drive if the hwif is actually a flash memory card of some variety:
489 if (drive_is_flashcard(drive)) {
490 struct ata_device *mate = &drive->channel->drives[1 ^ drive->select.b.unit];
491 if (!mate->ata_flash) {
492 mate->present = 0;
493 mate->noprobe = 1;
496 drive->type = ATA_DISK;
497 printk("DISK drive\n");
499 /* Initialize our quirk list. */
500 if (drive->channel->quirkproc)
501 drive->quirk_list = drive->channel->quirkproc(drive);
503 /* Initialize queue depth settings */
504 drive->queue_depth = 1;
505 #ifdef CONFIG_BLK_DEV_IDE_TCQ_DEPTH
506 drive->queue_depth = CONFIG_BLK_DEV_IDE_TCQ_DEPTH;
507 #else
508 drive->queue_depth = drive->id->queue_depth + 1;
509 #endif
510 if (drive->queue_depth < 1 || drive->queue_depth > IDE_MAX_TAG)
511 drive->queue_depth = IDE_MAX_TAG;
513 return;
515 err_misc:
516 kfree(id);
517 err_kmalloc:
518 drive->present = 0;
520 return;
524 * Sends an ATA(PI) IDENTIFY request to a drive and wait for a response. It
525 * also monitor irqs while this is happening, in hope of automatically
526 * determining which one is being used by the interface.
528 * Returns: 0 device was identified
529 * 1 device timed-out (no response to identify request)
530 * 2 device aborted the command (refused to identify itself)
532 static int identify(struct ata_device *drive, u8 cmd)
534 struct ata_channel *ch = drive->channel;
535 int rc = 1;
536 int autoprobe = 0;
537 unsigned long cookie = 0;
538 ide_ioreg_t hd_status;
539 unsigned long timeout;
542 /* FIXME: perhaps we should be just using allways the status register,
543 * since it should simplify the code significantly.
545 if (ch->io_ports[IDE_CONTROL_OFFSET]) {
546 u8 s;
547 u8 a;
549 if (!drive->channel->irq) {
550 autoprobe = 1;
551 cookie = probe_irq_on();
552 ata_irq_enable(drive, 1); /* enable device irq */
555 /* take a deep breath */
556 mdelay(50);
557 a = IN_BYTE(ch->io_ports[IDE_ALTSTATUS_OFFSET]);
558 s = IN_BYTE(ch->io_ports[IDE_STATUS_OFFSET]);
559 if ((a ^ s) & ~INDEX_STAT) {
560 printk("%s: probing with STATUS(0x%02x) instead of ALTSTATUS(0x%02x)\n", drive->name, s, a);
561 hd_status = ch->io_ports[IDE_STATUS_OFFSET]; /* ancient Seagate drives, broken interfaces */
562 } else {
563 hd_status = ch->io_ports[IDE_ALTSTATUS_OFFSET]; /* use non-intrusive polling */
565 } else {
566 mdelay(50);
567 hd_status = ch->io_ports[IDE_STATUS_OFFSET];
570 /* set features register for atapi identify command to be sure of reply */
571 if ((cmd == WIN_PIDENTIFY))
572 OUT_BYTE(0,IDE_FEATURE_REG); /* disable dma & overlap */
574 #if CONFIG_BLK_DEV_PDC4030
575 if (drive->channel->chipset == ide_pdc4030) {
576 /* DC4030 hosted drives need their own identify... */
577 extern int pdc4030_identify(struct ata_device *);
579 if (pdc4030_identify(drive))
580 goto out;
581 } else
582 #endif
583 OUT_BYTE(cmd, IDE_COMMAND_REG); /* ask drive for ID */
584 timeout = ((cmd == WIN_IDENTIFY) ? WAIT_WORSTCASE : WAIT_PIDENTIFY) / 2;
585 timeout += jiffies;
586 do {
587 if (time_after(jiffies, timeout))
588 goto out; /* drive timed-out */
589 mdelay(50); /* give drive a breather */
590 } while (IN_BYTE(hd_status) & BUSY_STAT);
592 mdelay(50); /* wait for IRQ and DRQ_STAT */
594 if (ata_status(drive, DRQ_STAT, BAD_R_STAT)) {
595 unsigned long flags;
597 local_irq_save(flags); /* some systems need this */
598 do_identify(drive, cmd); /* drive returned ID */
599 rc = 0; /* drive responded with ID */
600 ata_status(drive, 0, 0); /* clear drive IRQ */
601 local_irq_restore(flags); /* local CPU only */
602 } else
603 rc = 2; /* drive refused ID */
605 out:
606 if (autoprobe) {
607 int irq;
609 ata_irq_enable(drive, 0); /* mask device irq */
610 ata_status(drive, 0, 0); /* clear drive IRQ */
611 udelay(5);
612 irq = probe_irq_off(cookie);
613 if (!drive->channel->irq) {
614 if (irq > 0)
615 drive->channel->irq = irq;
616 else /* Mmmm.. multiple IRQs.. don't know which was ours */
617 printk("%s: IRQ probe failed (0x%lx)\n", drive->name, cookie);
621 return rc;
626 * This has the difficult job of finding a drive if it exists, without getting
627 * hung up if it doesn't exist, without trampling on ethernet cards, and
628 * without leaving any IRQs dangling to haunt us later.
630 * If a drive is "known" to exist (from CMOS or kernel parameters), but does
631 * not respond right away, the probe will "hang in there" for the maximum wait
632 * time (about 30 seconds), otherwise it will exit much more quickly.
634 * Returns: 0 device was identified
635 * 1 device timed-out (no response to identify request)
636 * 2 device aborted the command (refused to identify itself)
637 * 3 bad status from device (possible for ATAPI drives)
638 * 4 probe was not attempted because failure was obvious
640 static int do_probe(struct ata_device *drive, u8 cmd)
642 int rc;
643 struct ata_channel *ch = drive->channel;
644 u8 select;
646 if (drive->present) { /* avoid waiting for inappropriate probes */
647 if ((drive->type != ATA_DISK) && (cmd == WIN_IDENTIFY))
648 return 4;
650 #ifdef DEBUG
651 printk("probing for %s: present=%d, type=%02x, probetype=%s\n",
652 drive->name, drive->present, drive->type,
653 (cmd == WIN_IDENTIFY) ? "ATA" : "ATAPI");
654 #endif
655 mdelay(50); /* needed for some systems (e.g. crw9624 as drive0 with disk as slave) */
656 ata_select(drive, 50000);
657 select = IN_BYTE(IDE_SELECT_REG);
658 if (select != drive->select.all && !drive->present) {
659 if (drive->select.b.unit != 0) {
660 ata_select(&ch->drives[0], 50000); /* exit with drive0 selected */
662 return 3; /* no i/f present: mmm.. this should be a 4 -ml */
665 if (ata_status(drive, READY_STAT, BUSY_STAT) || drive->present || cmd == WIN_PIDENTIFY) {
666 if ((rc = identify(drive,cmd))) /* send cmd and wait */
667 rc = identify(drive,cmd); /* failed: try again */
668 if (rc == 1 && cmd == WIN_PIDENTIFY && drive->autotune != 2) {
669 unsigned long timeout;
670 printk("%s: no response (status = 0x%02x), resetting drive\n",
671 drive->name, drive->status);
672 mdelay(50);
673 OUT_BYTE(drive->select.all, IDE_SELECT_REG);
674 mdelay(50);
675 OUT_BYTE(WIN_SRST, IDE_COMMAND_REG);
676 timeout = jiffies;
677 while (!ata_status(drive, 0, BUSY_STAT) && time_before(jiffies, timeout + WAIT_WORSTCASE))
678 mdelay(50);
679 rc = identify(drive, cmd);
681 if (rc == 1)
682 printk("%s: no response (status = 0x%02x)\n",
683 drive->name, drive->status);
684 ata_status(drive, 0, 0); /* ensure drive irq is clear */
685 } else
686 rc = 3; /* not present or maybe ATAPI */
688 if (drive->select.b.unit != 0) {
689 ata_select(&ch->drives[0], 50000); /* exit with drive0 selected */
690 ata_status(drive, 0, 0); /* ensure drive irq is clear */
693 return rc;
697 * Probe for drivers on a channel.
699 * This routine only knows how to look for drive units 0 and 1 on an interface,
700 * so any setting of MAX_DRIVES > 2 won't work here.
702 static void channel_probe(struct ata_channel *ch)
704 unsigned int i;
705 unsigned long flags;
706 int error;
708 if (ch->noprobe)
709 return;
711 ch->straight8 = 0;
713 local_save_flags(flags);
714 local_irq_enable(); /* needed for jiffies and irq probing */
717 * Check for the presence of a channel by probing for drives on it.
719 for (i = 0; i < MAX_DRIVES; ++i) {
720 struct ata_device *drive = &ch->drives[i];
722 if (drive->noprobe) /* don't look for this one */
723 continue;
725 if (do_probe(drive, WIN_IDENTIFY) >= 2) { /* if !(success||timed-out) */
726 do_probe(drive, WIN_PIDENTIFY); /* look for ATAPI device */
729 /* Special handling of EXABYTE controller cards. */
730 if (drive->id && strstr(drive->id->model, "E X A B Y T E N E S T")) {
731 unsigned long timeout;
733 printk("%s: enabling %s -- ", drive->channel->name, drive->id->model);
734 ata_select(drive, 50000);
735 OUT_BYTE(EXABYTE_ENABLE_NEST, IDE_COMMAND_REG);
736 timeout = jiffies + WAIT_WORSTCASE;
737 do {
738 if (time_after(jiffies, timeout)) {
739 printk("failed (timeout)\n");
740 return;
742 mdelay(50);
743 } while (!ata_status(drive, 0, BUSY_STAT));
744 mdelay(50);
745 if (!ata_status(drive, 0, BAD_STAT))
746 printk("failed (status = 0x%02x)\n", drive->status);
747 else
748 printk("success\n");
750 if (do_probe(drive, WIN_IDENTIFY) >= 2) { /* if !(success||timed-out) */
751 do_probe(drive, WIN_PIDENTIFY); /* look for ATAPI device */
755 if (!drive->present)
756 continue; /* drive not found */
758 if (!drive->id) { /* identification failed? */
759 if (drive->type == ATA_DISK)
760 printk ("%s: pre-ATA drive, CHS=%d/%d/%d\n",
761 drive->name, drive->cyl, drive->head, drive->sect);
762 else if (drive->type == ATA_ROM)
763 printk("%s: ATAPI cdrom (?)\n", drive->name);
764 else
765 drive->present = 0; /* nuke it */
768 /* drive found, there is a channel it is attached too. */
769 if (drive->present)
770 ch->present = 1;
773 if (!ch->present)
774 goto not_found;
776 error = 0;
778 if (((unsigned long)ch->io_ports[IDE_DATA_OFFSET] | 7) ==
779 ((unsigned long)ch->io_ports[IDE_STATUS_OFFSET])) {
780 error += !request_region(ch->io_ports[IDE_DATA_OFFSET], 8, ch->name);
781 ch->straight8 = 1;
782 } else {
783 for (i = 0; i < 8; i++)
784 error += !request_region(ch->io_ports[i], 1, ch->name);
786 if (ch->io_ports[IDE_CONTROL_OFFSET])
787 error += !request_region(ch->io_ports[IDE_CONTROL_OFFSET], 1, ch->name);
788 #if defined(CONFIG_AMIGA) || defined(CONFIG_MAC)
789 if (ch->io_ports[IDE_IRQ_OFFSET])
790 error += !request_region(ch->io_ports[IDE_IRQ_OFFSET], 1, ch->name);
791 #endif
793 /* Some neccessary register area was already used. Skip this device.
796 if (
797 #if CONFIG_BLK_DEV_PDC4030
798 (ch->chipset != ide_pdc4030 || ch->unit == 0) &&
799 #endif
800 error) {
802 /* FIXME: We should be dealing properly with partial IO region
803 * allocations here.
806 ch->present = 0;
807 printk("%s: error: ports already in use!\n", ch->name);
810 if (!ch->present)
811 goto not_found;
813 /* Register this hardware interface within the global device tree.
815 * FIXME: This should be handled as a pci subdevice in a generic way.
817 sprintf(ch->dev.bus_id, "ata@%02x", ch->unit);
818 strcpy(ch->dev.name, "ATA/ATAPI Host-Channel");
819 ch->dev.driver_data = ch;
820 #ifdef CONFIG_PCI
821 if (ch->pci_dev)
822 ch->dev.parent = &ch->pci_dev->dev;
823 else
824 #endif
825 ch->dev.parent = NULL; /* Would like to do = &device_legacy */
827 device_register(&ch->dev);
829 if (ch->reset)
830 ata_reset(ch);
832 local_irq_restore(flags);
835 * Now setup the PIO transfer modes of the drives on this channel.
837 for (i = 0; i < MAX_DRIVES; ++i) {
838 struct ata_device *drive = &ch->drives[i];
840 if (drive->present && (drive->autotune == 1)) {
841 if (drive->channel->tuneproc)
842 drive->channel->tuneproc(drive, 255); /* auto-tune PIO mode */
846 return;
848 not_found:
849 local_irq_restore(flags);
853 * This routine sets up the irq for an ide interface, and creates a new hwgroup
854 * for the irq/channel if none was previously assigned.
856 * Much of the code is for correctly detecting/handling irq sharing and irq
857 * serialization situations. This is somewhat complex because it handles
858 * static as well as dynamic (PCMCIA) IDE interfaces.
860 * The SA_INTERRUPT in sa_flags means ata_irq_request() is always entered with
861 * interrupts completely disabled. This can be bad for interrupt latency, but
862 * anything else has led to problems on some machines. We re-enable interrupts
863 * as much as we can safely do in most places.
865 static int init_irq(struct ata_channel *ch)
867 unsigned long flags;
868 int i;
869 spinlock_t *lock;
870 spinlock_t *new_lock;
871 unsigned long *active;
872 unsigned long *new_active;
873 struct ata_channel *match = NULL;
875 /* Spare allocation before sleep. */
876 new_lock = kmalloc(sizeof(*lock), GFP_KERNEL);
877 new_active = kmalloc(sizeof(*active), GFP_KERNEL);
878 *new_active = 0L;
880 spin_lock_irqsave(&ide_lock, flags);
881 ch->lock = NULL;
883 #if MAX_HWIFS > 1
885 * Group up with any other channels that share our irq(s).
887 for (i = 0; i < MAX_HWIFS; ++i) {
888 struct ata_channel *h = &ide_hwifs[i];
890 /* scan only initialized channels */
891 if (!h->lock)
892 continue;
894 if (ch->irq != h->irq)
895 continue;
897 ch->sharing_irq = h->sharing_irq = 1;
899 if (ch->chipset != ide_pci || h->chipset != ide_pci ||
900 ch->serialized || h->serialized) {
901 if (match && match->lock && match->lock != h->lock)
902 printk("%s: potential irq problem with %s and %s\n", ch->name, h->name, match->name);
903 /* don't undo a prior perfect match */
904 if (!match || match->irq != ch->irq)
905 match = h;
908 #endif
910 * If we are still without a lock group, then form a new one
912 if (!match) {
913 lock = new_lock;
914 active = new_active;
915 if (!lock) {
916 spin_unlock_irqrestore(&ide_lock, flags);
918 return 1;
920 spin_lock_init(lock);
921 } else {
922 lock = match->lock;
923 active = match->active;
924 if(new_lock)
925 kfree(new_lock);
929 * Allocate the irq, if not already obtained for another channel
931 if (!match || match->irq != ch->irq) {
932 struct ata_device tmp;
933 #ifdef CONFIG_IDEPCI_SHARE_IRQ
934 int sa = IDE_CHIPSET_IS_PCI(ch->chipset) ? SA_SHIRQ : SA_INTERRUPT;
935 #else
936 int sa = IDE_CHIPSET_IS_PCI(ch->chipset) ? SA_INTERRUPT|SA_SHIRQ : SA_INTERRUPT;
937 #endif
939 /* Enable interrupts triggered by the drive. We use a shallow
940 * device structure, just to use the generic function very
941 * early.
943 tmp.channel = ch;
944 ata_irq_enable(&tmp, 1);
946 if (request_irq(ch->irq, &ata_irq_request, sa, ch->name, ch)) {
947 if (!match) {
948 kfree(lock);
949 kfree(active);
952 spin_unlock_irqrestore(&ide_lock, flags);
954 return 1;
959 * Everything is okay. Tag us as member of this lock group.
961 ch->lock = lock;
962 ch->active = active;
964 init_timer(&ch->timer);
965 ch->timer.function = &ide_timer_expiry;
966 ch->timer.data = (unsigned long) ch;
968 for (i = 0; i < MAX_DRIVES; ++i) {
969 struct ata_device *drive = &ch->drives[i];
970 request_queue_t *q;
971 int max_sectors = 255;
973 if (!drive->present)
974 continue;
976 if (!ch->drive)
977 ch->drive = drive;
980 * Init the per device request queue.
983 q = &drive->queue;
984 q->queuedata = drive;
985 blk_init_queue(q, do_ide_request, drive->channel->lock);
986 blk_queue_segment_boundary(q, ch->seg_boundary_mask);
987 blk_queue_max_segment_size(q, ch->max_segment_size);
989 /* ATA can do up to 128K per request, pdc4030 needs smaller
990 * limit. */
991 #ifdef CONFIG_BLK_DEV_PDC4030
992 if (drive->channel->chipset == ide_pdc4030)
993 max_sectors = 127;
994 #endif
995 blk_queue_max_sectors(q, max_sectors);
997 /* ATA DMA can do PRD_ENTRIES number of segments. */
998 blk_queue_max_hw_segments(q, PRD_ENTRIES);
1000 /* FIXME: This is a driver limit and could be eliminated. */
1001 blk_queue_max_phys_segments(q, PRD_ENTRIES);
1003 spin_unlock_irqrestore(&ide_lock, flags);
1005 #if !defined(__mc68000__) && !defined(CONFIG_APUS) && !defined(__sparc__)
1006 printk("%s at 0x%03x-0x%03x,0x%03x on irq %d", ch->name,
1007 ch->io_ports[IDE_DATA_OFFSET],
1008 ch->io_ports[IDE_DATA_OFFSET]+7,
1009 ch->io_ports[IDE_CONTROL_OFFSET], ch->irq);
1010 #elif defined(__sparc__)
1011 printk("%s at 0x%03lx-0x%03lx,0x%03lx on irq %s", ch->name,
1012 ch->io_ports[IDE_DATA_OFFSET],
1013 ch->io_ports[IDE_DATA_OFFSET]+7,
1014 ch->io_ports[IDE_CONTROL_OFFSET], __irq_itoa(ch->irq));
1015 #else
1016 printk("%s at %p on irq 0x%08x", ch->name,
1017 ch->io_ports[IDE_DATA_OFFSET], ch->irq);
1018 #endif
1019 if (match)
1020 printk(" (%sed with %s)",
1021 ch->sharing_irq ? "shar" : "serializ", match->name);
1022 printk("\n");
1024 return 0;
1028 * Returns the queue which corresponds to a given device.
1030 * FIXME: this should take struct block_device * as argument in future.
1032 static request_queue_t *ata_get_queue(kdev_t dev)
1034 struct ata_channel *ch = (struct ata_channel *)blk_dev[major(dev)].data;
1036 /* FIXME: ALLERT: This discriminates between master and slave! */
1037 return &ch->drives[DEVICE_NR(dev) & 1].queue;
1040 /* Number of minor numbers we consume par channel. */
1041 #define ATA_MINORS (MAX_DRIVES * (1 << PARTN_BITS))
1043 static void channel_init(struct ata_channel *ch)
1045 struct gendisk *gd;
1046 struct hd_struct *part;
1047 devfs_handle_t *de_arr;
1048 char *flags;
1049 unsigned int unit;
1050 extern devfs_handle_t ide_devfs_handle;
1051 char *names;
1053 if (!ch->present)
1054 return;
1056 /* we set it back to 1 if all is ok below */
1057 ch->present = 0;
1059 if (!ch->irq) {
1060 if (!(ch->irq = ide_default_irq(ch->io_ports[IDE_DATA_OFFSET]))) {
1061 printk("%s: DISABLED, NO IRQ\n", ch->name);
1063 return;
1066 #ifdef CONFIG_BLK_DEV_HD
1068 /* The first "legacy" HD gets distinguished by the IRQ it is attached
1069 * to and the IO port it takes.
1072 if (ch->irq == 14 && ch->io_ports[IDE_DATA_OFFSET] != 0x1f0) {
1073 printk("%s: CANNOT SHARE IRQ WITH OLD HARDDISK DRIVER (hd.c)\n", ch->name);
1075 return;
1077 #endif
1079 if (register_blkdev(ch->major, ch->name, ide_fops)) {
1080 printk("%s: UNABLE TO GET MAJOR NUMBER %d\n", ch->name, ch->major);
1082 return;
1085 if (init_irq(ch)) {
1086 int irq = ch->irq;
1088 * It failed to initialise. Find the default IRQ for
1089 * this port and try that.
1091 if (!(ch->irq = ide_default_irq(ch->io_ports[IDE_DATA_OFFSET]))) {
1092 printk(KERN_INFO "%s: disabled; unable to get IRQ %d.\n", ch->name, irq);
1093 (void) unregister_blkdev (ch->major, ch->name);
1095 return;
1097 if (init_irq(ch)) {
1098 printk(KERN_INFO "%s: probed IRQ %d and default IRQ %d failed.\n",
1099 ch->name, irq, ch->irq);
1100 (void) unregister_blkdev(ch->major, ch->name);
1102 return;
1104 printk(KERN_INFO "%s: probed IRQ %d failed, using default.\n", ch->name, ch->irq);
1107 /* Initialize partition and global device data.
1110 gd = kmalloc (MAX_DRIVES * sizeof(struct gendisk), GFP_KERNEL);
1111 if (!gd)
1112 goto err_kmalloc_gd;
1114 memset(gd, 0, MAX_DRIVES * sizeof(struct gendisk));
1116 part = kmalloc(ATA_MINORS * sizeof(struct hd_struct), GFP_KERNEL);
1117 if (!part)
1118 goto err_kmalloc_gd_part;
1119 memset(part, 0, ATA_MINORS * sizeof(struct hd_struct));
1121 de_arr = kmalloc (sizeof(devfs_handle_t) * MAX_DRIVES, GFP_KERNEL);
1122 if (!de_arr)
1123 goto err_kmalloc_gd_de_arr;
1124 memset(de_arr, 0, sizeof(devfs_handle_t) * MAX_DRIVES);
1126 flags = kmalloc (sizeof(char) * MAX_DRIVES, GFP_KERNEL);
1127 if (!flags)
1128 goto err_kmalloc_gd_flags;
1129 memset(flags, 0, sizeof(char) * MAX_DRIVES);
1131 names = kmalloc (4 * MAX_DRIVES, GFP_KERNEL);
1132 if (!names)
1133 goto err_kmalloc_gd_names;
1134 memset(names, 0, 4 * MAX_DRIVES);
1136 for (unit = 0; unit < MAX_DRIVES; ++unit) {
1137 gd[unit].part = part + (unit << PARTN_BITS);
1138 gd[unit].de_arr = de_arr + unit;
1139 gd[unit].flags = flags + unit;
1140 ch->drives[unit].part = gd[unit].part;
1141 gd[unit].major = ch->major;
1142 gd[unit].first_minor = unit << PARTN_BITS;
1143 sprintf(names + 4*unit, "hd%c", 'a'+ch->index*MAX_DRIVES+unit);
1144 gd[unit].major_name = names + 4*unit;
1145 gd[unit].minor_shift = PARTN_BITS;
1146 gd[unit].nr_real = 1;
1147 gd[unit].fops = ide_fops;
1148 ch->gd[unit] = gd + unit;
1149 add_gendisk(gd + unit);
1152 for (unit = 0; unit < MAX_DRIVES; ++unit) {
1153 char name[80];
1155 ch->drives[unit].dn = ((ch->unit ? 2 : 0) + unit);
1156 sprintf(name, "host%d/bus%d/target%d/lun%d",
1157 ch->index, ch->unit, unit, ch->drives[unit].lun);
1158 if (ch->drives[unit].present)
1159 ch->drives[unit].de = devfs_mk_dir(ide_devfs_handle, name, NULL);
1162 blk_dev[ch->major].data = ch;
1163 blk_dev[ch->major].queue = ata_get_queue;
1165 /* All went well, flag this channel entry as valid again. */
1166 ch->present = 1;
1168 return;
1170 err_kmalloc_gd_names:
1171 kfree(names);
1172 err_kmalloc_gd_flags:
1173 kfree(de_arr);
1174 err_kmalloc_gd_de_arr:
1175 kfree(part);
1176 err_kmalloc_gd_part:
1177 kfree(gd);
1178 err_kmalloc_gd:
1179 printk(KERN_CRIT "(%s) Out of memory\n", __FUNCTION__);
1183 * FIXME: consider moving this to main.c, since this is the only place where
1184 * it's used.
1186 * Probe only for drives on channes which are not already present.
1188 int ideprobe_init(void)
1190 unsigned int i;
1191 int probe[MAX_HWIFS];
1193 for (i = 0; i < MAX_HWIFS; ++i)
1194 probe[i] = !ide_hwifs[i].present;
1197 * Probe for drives in the usual way.. CMOS/BIOS, then poke at ports
1199 for (i = 0; i < MAX_HWIFS; ++i) {
1200 if (!probe[i])
1201 continue;
1202 channel_probe(&ide_hwifs[i]);
1204 for (i = 0; i < MAX_HWIFS; ++i) {
1205 if (!probe[i])
1206 continue;
1207 channel_init(&ide_hwifs[i]);
1209 return 0;
1212 EXPORT_SYMBOL(ata_fix_driveid);
1213 EXPORT_SYMBOL(ide_fixstring);
1214 EXPORT_SYMBOL(eighty_ninty_three);
1215 EXPORT_SYMBOL(ide_config_drive_speed);