blob: cc5801399467360bbd1097a5aa36801a487a6a36 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/drivers/ide/ide-probe.c Version 1.11 Mar 05, 2003
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 * Version 1.07 max_sect <= 255; slower disks would get behind and
29 * then fall over when they get to 256. Paul G.
30 * Version 1.10 Update set for new IDE. drive->id is now always
31 * valid after probe time even with noprobe
32 */
33
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <linux/module.h>
35#include <linux/types.h>
36#include <linux/string.h>
37#include <linux/kernel.h>
38#include <linux/timer.h>
39#include <linux/mm.h>
40#include <linux/interrupt.h>
41#include <linux/major.h>
42#include <linux/errno.h>
43#include <linux/genhd.h>
44#include <linux/slab.h>
45#include <linux/delay.h>
46#include <linux/ide.h>
47#include <linux/spinlock.h>
48#include <linux/kmod.h>
49#include <linux/pci.h>
50
51#include <asm/byteorder.h>
52#include <asm/irq.h>
53#include <asm/uaccess.h>
54#include <asm/io.h>
55
56/**
57 * generic_id - add a generic drive id
58 * @drive: drive to make an ID block for
59 *
60 * Add a fake id field to the drive we are passed. This allows
61 * use to skip a ton of NULL checks (which people always miss)
62 * and make drive properties unconditional outside of this file
63 */
64
65static void generic_id(ide_drive_t *drive)
66{
67 drive->id->cyls = drive->cyl;
68 drive->id->heads = drive->head;
69 drive->id->sectors = drive->sect;
70 drive->id->cur_cyls = drive->cyl;
71 drive->id->cur_heads = drive->head;
72 drive->id->cur_sectors = drive->sect;
73}
74
75static void ide_disk_init_chs(ide_drive_t *drive)
76{
77 struct hd_driveid *id = drive->id;
78
79 /* Extract geometry if we did not already have one for the drive */
80 if (!drive->cyl || !drive->head || !drive->sect) {
81 drive->cyl = drive->bios_cyl = id->cyls;
82 drive->head = drive->bios_head = id->heads;
83 drive->sect = drive->bios_sect = id->sectors;
84 }
85
86 /* Handle logical geometry translation by the drive */
87 if ((id->field_valid & 1) && id->cur_cyls &&
88 id->cur_heads && (id->cur_heads <= 16) && id->cur_sectors) {
89 drive->cyl = id->cur_cyls;
90 drive->head = id->cur_heads;
91 drive->sect = id->cur_sectors;
92 }
93
94 /* Use physical geometry if what we have still makes no sense */
95 if (drive->head > 16 && id->heads && id->heads <= 16) {
96 drive->cyl = id->cyls;
97 drive->head = id->heads;
98 drive->sect = id->sectors;
99 }
100}
101
102static void ide_disk_init_mult_count(ide_drive_t *drive)
103{
104 struct hd_driveid *id = drive->id;
105
106 drive->mult_count = 0;
107 if (id->max_multsect) {
108#ifdef CONFIG_IDEDISK_MULTI_MODE
109 id->multsect = ((id->max_multsect/2) > 1) ? id->max_multsect : 0;
110 id->multsect_valid = id->multsect ? 1 : 0;
111 drive->mult_req = id->multsect_valid ? id->max_multsect : INITIAL_MULT_COUNT;
112 drive->special.b.set_multmode = drive->mult_req ? 1 : 0;
113#else /* original, pre IDE-NFG, per request of AC */
114 drive->mult_req = INITIAL_MULT_COUNT;
115 if (drive->mult_req > id->max_multsect)
116 drive->mult_req = id->max_multsect;
117 if (drive->mult_req || ((id->multsect_valid & 1) && id->multsect))
118 drive->special.b.set_multmode = 1;
119#endif
120 }
121}
122
123/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 * do_identify - identify a drive
125 * @drive: drive to identify
126 * @cmd: command used
127 *
128 * Called when we have issued a drive identify command to
129 * read and parse the results. This function is run with
130 * interrupts disabled.
131 */
132
133static inline void do_identify (ide_drive_t *drive, u8 cmd)
134{
135 ide_hwif_t *hwif = HWIF(drive);
136 int bswap = 1;
137 struct hd_driveid *id;
138
139 id = drive->id;
140 /* read 512 bytes of id info */
141 hwif->ata_input_data(drive, id, SECTOR_WORDS);
142
143 drive->id_read = 1;
144 local_irq_enable();
145 ide_fix_driveid(id);
146
Robert P. J. Daye71bc142007-07-09 23:17:57 +0200147#if defined (CONFIG_SCSI_EATA_PIO) || defined (CONFIG_SCSI_EATA)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 /*
149 * EATA SCSI controllers do a hardware ATA emulation:
150 * Ignore them if there is a driver for them available.
151 */
152 if ((id->model[0] == 'P' && id->model[1] == 'M') ||
153 (id->model[0] == 'S' && id->model[1] == 'K')) {
154 printk("%s: EATA SCSI HBA %.10s\n", drive->name, id->model);
155 goto err_misc;
156 }
Robert P. J. Daye71bc142007-07-09 23:17:57 +0200157#endif /* CONFIG_SCSI_EATA || CONFIG_SCSI_EATA_PIO */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
159 /*
160 * WIN_IDENTIFY returns little-endian info,
161 * WIN_PIDENTIFY *usually* returns little-endian info.
162 */
163 if (cmd == WIN_PIDENTIFY) {
164 if ((id->model[0] == 'N' && id->model[1] == 'E') /* NEC */
165 || (id->model[0] == 'F' && id->model[1] == 'X') /* Mitsumi */
166 || (id->model[0] == 'P' && id->model[1] == 'i'))/* Pioneer */
167 /* Vertos drives may still be weird */
168 bswap ^= 1;
169 }
170 ide_fixstring(id->model, sizeof(id->model), bswap);
171 ide_fixstring(id->fw_rev, sizeof(id->fw_rev), bswap);
172 ide_fixstring(id->serial_no, sizeof(id->serial_no), bswap);
173
174 if (strstr(id->model, "E X A B Y T E N E S T"))
175 goto err_misc;
176
177 /* we depend on this a lot! */
178 id->model[sizeof(id->model)-1] = '\0';
179 printk("%s: %s, ", drive->name, id->model);
180 drive->present = 1;
181 drive->dead = 0;
182
183 /*
184 * Check for an ATAPI device
185 */
186 if (cmd == WIN_PIDENTIFY) {
187 u8 type = (id->config >> 8) & 0x1f;
188 printk("ATAPI ");
189 switch (type) {
190 case ide_floppy:
191 if (!strstr(id->model, "CD-ROM")) {
192 if (!strstr(id->model, "oppy") &&
193 !strstr(id->model, "poyp") &&
194 !strstr(id->model, "ZIP"))
195 printk("cdrom or floppy?, assuming ");
196 if (drive->media != ide_cdrom) {
197 printk ("FLOPPY");
198 drive->removable = 1;
199 break;
200 }
201 }
202 /* Early cdrom models used zero */
203 type = ide_cdrom;
204 case ide_cdrom:
205 drive->removable = 1;
206#ifdef CONFIG_PPC
207 /* kludge for Apple PowerBook internal zip */
208 if (!strstr(id->model, "CD-ROM") &&
209 strstr(id->model, "ZIP")) {
210 printk ("FLOPPY");
211 type = ide_floppy;
212 break;
213 }
214#endif
215 printk ("CD/DVD-ROM");
216 break;
217 case ide_tape:
218 printk ("TAPE");
219 break;
220 case ide_optical:
221 printk ("OPTICAL");
222 drive->removable = 1;
223 break;
224 default:
225 printk("UNKNOWN (type %d)", type);
226 break;
227 }
228 printk (" drive\n");
229 drive->media = type;
230 /* an ATAPI device ignores DRDY */
231 drive->ready_stat = 0;
232 return;
233 }
234
235 /*
236 * Not an ATAPI device: looks like a "regular" hard disk
237 */
Richard Purdie98109332006-02-03 03:04:55 -0800238
239 /*
240 * 0x848a = CompactFlash device
241 * These are *not* removable in Linux definition of the term
242 */
243
244 if ((id->config != 0x848a) && (id->config & (1<<7)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 drive->removable = 1;
246
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 drive->media = ide_disk;
Richard Purdie98109332006-02-03 03:04:55 -0800248 printk("%s DISK drive\n", (id->config == 0x848a) ? "CFA" : "ATA" );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 QUIRK_LIST(drive);
250 return;
251
252err_misc:
253 kfree(id);
254 drive->present = 0;
255 return;
256}
257
258/**
259 * actual_try_to_identify - send ata/atapi identify
260 * @drive: drive to identify
261 * @cmd: command to use
262 *
263 * try_to_identify() sends an ATA(PI) IDENTIFY request to a drive
264 * and waits for a response. It also monitors irqs while this is
265 * happening, in hope of automatically determining which one is
266 * being used by the interface.
267 *
268 * Returns: 0 device was identified
269 * 1 device timed-out (no response to identify request)
270 * 2 device aborted the command (refused to identify itself)
271 */
272
273static int actual_try_to_identify (ide_drive_t *drive, u8 cmd)
274{
275 ide_hwif_t *hwif = HWIF(drive);
276 int rc;
277 unsigned long hd_status;
278 unsigned long timeout;
279 u8 s = 0, a = 0;
280
281 /* take a deep breath */
282 msleep(50);
283
284 if (IDE_CONTROL_REG) {
285 a = hwif->INB(IDE_ALTSTATUS_REG);
286 s = hwif->INB(IDE_STATUS_REG);
287 if ((a ^ s) & ~INDEX_STAT) {
288 printk(KERN_INFO "%s: probing with STATUS(0x%02x) instead of "
289 "ALTSTATUS(0x%02x)\n", drive->name, s, a);
290 /* ancient Seagate drives, broken interfaces */
291 hd_status = IDE_STATUS_REG;
292 } else {
293 /* use non-intrusive polling */
294 hd_status = IDE_ALTSTATUS_REG;
295 }
296 } else
297 hd_status = IDE_STATUS_REG;
298
299 /* set features register for atapi
300 * identify command to be sure of reply
301 */
302 if ((cmd == WIN_PIDENTIFY))
303 /* disable dma & overlap */
304 hwif->OUTB(0, IDE_FEATURE_REG);
305
306 /* ask drive for ID */
307 hwif->OUTB(cmd, IDE_COMMAND_REG);
308
309 timeout = ((cmd == WIN_IDENTIFY) ? WAIT_WORSTCASE : WAIT_PIDENTIFY) / 2;
310 timeout += jiffies;
311 do {
312 if (time_after(jiffies, timeout)) {
313 /* drive timed-out */
314 return 1;
315 }
316 /* give drive a breather */
317 msleep(50);
318 } while ((hwif->INB(hd_status)) & BUSY_STAT);
319
320 /* wait for IRQ and DRQ_STAT */
321 msleep(50);
322 if (OK_STAT((hwif->INB(IDE_STATUS_REG)), DRQ_STAT, BAD_R_STAT)) {
323 unsigned long flags;
324
325 /* local CPU only; some systems need this */
326 local_irq_save(flags);
327 /* drive returned ID */
328 do_identify(drive, cmd);
329 /* drive responded with ID */
330 rc = 0;
331 /* clear drive IRQ */
332 (void) hwif->INB(IDE_STATUS_REG);
333 local_irq_restore(flags);
334 } else {
335 /* drive refused ID */
336 rc = 2;
337 }
338 return rc;
339}
340
341/**
342 * try_to_identify - try to identify a drive
343 * @drive: drive to probe
344 * @cmd: command to use
345 *
346 * Issue the identify command and then do IRQ probing to
347 * complete the identification when needed by finding the
348 * IRQ the drive is attached to
349 */
350
351static int try_to_identify (ide_drive_t *drive, u8 cmd)
352{
353 ide_hwif_t *hwif = HWIF(drive);
354 int retval;
355 int autoprobe = 0;
356 unsigned long cookie = 0;
357
358 /*
359 * Disable device irq unless we need to
360 * probe for it. Otherwise we'll get spurious
361 * interrupts during the identify-phase that
362 * the irq handler isn't expecting.
363 */
364 if (IDE_CONTROL_REG) {
365 u8 ctl = drive->ctl | 2;
366 if (!hwif->irq) {
367 autoprobe = 1;
368 cookie = probe_irq_on();
369 /* enable device irq */
370 ctl &= ~2;
371 }
372 hwif->OUTB(ctl, IDE_CONTROL_REG);
373 }
374
375 retval = actual_try_to_identify(drive, cmd);
376
377 if (autoprobe) {
378 int irq;
379 /* mask device irq */
380 hwif->OUTB(drive->ctl|2, IDE_CONTROL_REG);
381 /* clear drive IRQ */
382 (void) hwif->INB(IDE_STATUS_REG);
383 udelay(5);
384 irq = probe_irq_off(cookie);
385 if (!hwif->irq) {
386 if (irq > 0) {
387 hwif->irq = irq;
388 } else {
389 /* Mmmm.. multiple IRQs..
390 * don't know which was ours
391 */
392 printk("%s: IRQ probe failed (0x%lx)\n",
393 drive->name, cookie);
394 }
395 }
396 }
397 return retval;
398}
399
400
401/**
402 * do_probe - probe an IDE device
403 * @drive: drive to probe
404 * @cmd: command to use
405 *
406 * do_probe() has the difficult job of finding a drive if it exists,
407 * without getting hung up if it doesn't exist, without trampling on
408 * ethernet cards, and without leaving any IRQs dangling to haunt us later.
409 *
410 * If a drive is "known" to exist (from CMOS or kernel parameters),
411 * but does not respond right away, the probe will "hang in there"
412 * for the maximum wait time (about 30 seconds), otherwise it will
413 * exit much more quickly.
414 *
415 * Returns: 0 device was identified
416 * 1 device timed-out (no response to identify request)
417 * 2 device aborted the command (refused to identify itself)
418 * 3 bad status from device (possible for ATAPI drives)
419 * 4 probe was not attempted because failure was obvious
420 */
421
422static int do_probe (ide_drive_t *drive, u8 cmd)
423{
424 int rc;
425 ide_hwif_t *hwif = HWIF(drive);
426
427 if (drive->present) {
428 /* avoid waiting for inappropriate probes */
429 if ((drive->media != ide_disk) && (cmd == WIN_IDENTIFY))
430 return 4;
431 }
432#ifdef DEBUG
433 printk("probing for %s: present=%d, media=%d, probetype=%s\n",
434 drive->name, drive->present, drive->media,
435 (cmd == WIN_IDENTIFY) ? "ATA" : "ATAPI");
436#endif
437
438 /* needed for some systems
439 * (e.g. crw9624 as drive0 with disk as slave)
440 */
441 msleep(50);
442 SELECT_DRIVE(drive);
443 msleep(50);
444 if (hwif->INB(IDE_SELECT_REG) != drive->select.all && !drive->present) {
445 if (drive->select.b.unit != 0) {
446 /* exit with drive0 selected */
447 SELECT_DRIVE(&hwif->drives[0]);
448 /* allow BUSY_STAT to assert & clear */
449 msleep(50);
450 }
451 /* no i/f present: mmm.. this should be a 4 -ml */
452 return 3;
453 }
454
455 if (OK_STAT((hwif->INB(IDE_STATUS_REG)), READY_STAT, BUSY_STAT) ||
456 drive->present || cmd == WIN_PIDENTIFY) {
457 /* send cmd and wait */
458 if ((rc = try_to_identify(drive, cmd))) {
459 /* failed: try again */
460 rc = try_to_identify(drive,cmd);
461 }
462 if (hwif->INB(IDE_STATUS_REG) == (BUSY_STAT|READY_STAT))
463 return 4;
464
465 if ((rc == 1 && cmd == WIN_PIDENTIFY) &&
466 ((drive->autotune == IDE_TUNE_DEFAULT) ||
467 (drive->autotune == IDE_TUNE_AUTO))) {
468 unsigned long timeout;
469 printk("%s: no response (status = 0x%02x), "
470 "resetting drive\n", drive->name,
471 hwif->INB(IDE_STATUS_REG));
472 msleep(50);
473 hwif->OUTB(drive->select.all, IDE_SELECT_REG);
474 msleep(50);
475 hwif->OUTB(WIN_SRST, IDE_COMMAND_REG);
476 timeout = jiffies;
477 while (((hwif->INB(IDE_STATUS_REG)) & BUSY_STAT) &&
478 time_before(jiffies, timeout + WAIT_WORSTCASE))
479 msleep(50);
480 rc = try_to_identify(drive, cmd);
481 }
482 if (rc == 1)
483 printk("%s: no response (status = 0x%02x)\n",
484 drive->name, hwif->INB(IDE_STATUS_REG));
485 /* ensure drive irq is clear */
486 (void) hwif->INB(IDE_STATUS_REG);
487 } else {
488 /* not present or maybe ATAPI */
489 rc = 3;
490 }
491 if (drive->select.b.unit != 0) {
492 /* exit with drive0 selected */
493 SELECT_DRIVE(&hwif->drives[0]);
494 msleep(50);
495 /* ensure drive irq is clear */
496 (void) hwif->INB(IDE_STATUS_REG);
497 }
498 return rc;
499}
500
501/*
502 *
503 */
504static void enable_nest (ide_drive_t *drive)
505{
506 ide_hwif_t *hwif = HWIF(drive);
507 unsigned long timeout;
508
509 printk("%s: enabling %s -- ", hwif->name, drive->id->model);
510 SELECT_DRIVE(drive);
511 msleep(50);
512 hwif->OUTB(EXABYTE_ENABLE_NEST, IDE_COMMAND_REG);
513 timeout = jiffies + WAIT_WORSTCASE;
514 do {
515 if (time_after(jiffies, timeout)) {
516 printk("failed (timeout)\n");
517 return;
518 }
519 msleep(50);
520 } while ((hwif->INB(IDE_STATUS_REG)) & BUSY_STAT);
521
522 msleep(50);
523
524 if (!OK_STAT((hwif->INB(IDE_STATUS_REG)), 0, BAD_STAT)) {
525 printk("failed (status = 0x%02x)\n", hwif->INB(IDE_STATUS_REG));
526 } else {
527 printk("success\n");
528 }
529
530 /* if !(success||timed-out) */
531 if (do_probe(drive, WIN_IDENTIFY) >= 2) {
532 /* look for ATAPI device */
533 (void) do_probe(drive, WIN_PIDENTIFY);
534 }
535}
536
537/**
538 * probe_for_drives - upper level drive probe
539 * @drive: drive to probe for
540 *
541 * probe_for_drive() tests for existence of a given drive using do_probe()
542 * and presents things to the user as needed.
543 *
544 * Returns: 0 no device was found
545 * 1 device was found (note: drive->present might
546 * still be 0)
547 */
548
549static inline u8 probe_for_drive (ide_drive_t *drive)
550{
551 /*
552 * In order to keep things simple we have an id
553 * block for all drives at all times. If the device
554 * is pre ATA or refuses ATA/ATAPI identify we
555 * will add faked data to this.
556 *
557 * Also note that 0 everywhere means "can't do X"
558 */
559
Deepak Saxenaf5e3c2f2005-11-07 01:01:25 -0800560 drive->id = kzalloc(SECTOR_WORDS *4, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 drive->id_read = 0;
562 if(drive->id == NULL)
563 {
564 printk(KERN_ERR "ide: out of memory for id data.\n");
565 return 0;
566 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 strcpy(drive->id->model, "UNKNOWN");
568
569 /* skip probing? */
570 if (!drive->noprobe)
571 {
572 /* if !(success||timed-out) */
573 if (do_probe(drive, WIN_IDENTIFY) >= 2) {
574 /* look for ATAPI device */
575 (void) do_probe(drive, WIN_PIDENTIFY);
576 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 if (!drive->present)
578 /* drive not found */
579 return 0;
Alan Cox78595572007-07-03 22:28:35 +0200580 if (strstr(drive->id->model, "E X A B Y T E N E S T"))
581 enable_nest(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582
583 /* identification failed? */
584 if (!drive->id_read) {
585 if (drive->media == ide_disk) {
586 printk(KERN_INFO "%s: non-IDE drive, CHS=%d/%d/%d\n",
587 drive->name, drive->cyl,
588 drive->head, drive->sect);
589 } else if (drive->media == ide_cdrom) {
590 printk(KERN_INFO "%s: ATAPI cdrom (?)\n", drive->name);
591 } else {
592 /* nuke it */
593 printk(KERN_WARNING "%s: Unknown device on bus refused identification. Ignoring.\n", drive->name);
594 drive->present = 0;
595 }
596 }
597 /* drive was found */
598 }
599 if(!drive->present)
600 return 0;
601 /* The drive wasn't being helpful. Add generic info only */
602 if (drive->id_read == 0) {
603 generic_id(drive);
604 return 1;
605 }
606
607 if (drive->media == ide_disk) {
608 ide_disk_init_chs(drive);
609 ide_disk_init_mult_count(drive);
610 }
611
612 return drive->present;
613}
614
615static void hwif_release_dev (struct device *dev)
616{
617 ide_hwif_t *hwif = container_of(dev, ide_hwif_t, gendev);
618
Aleksey Makarovf36d4022006-01-09 15:59:27 -0800619 complete(&hwif->gendev_rel_comp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620}
621
622static void hwif_register (ide_hwif_t *hwif)
623{
Randy Dunlap349ae232006-10-03 01:14:23 -0700624 int ret;
625
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 /* register with global device tree */
627 strlcpy(hwif->gendev.bus_id,hwif->name,BUS_ID_SIZE);
628 hwif->gendev.driver_data = hwif;
629 if (hwif->gendev.parent == NULL) {
630 if (hwif->pci_dev)
631 hwif->gendev.parent = &hwif->pci_dev->dev;
632 else
633 /* Would like to do = &device_legacy */
634 hwif->gendev.parent = NULL;
635 }
636 hwif->gendev.release = hwif_release_dev;
Randy Dunlap349ae232006-10-03 01:14:23 -0700637 ret = device_register(&hwif->gendev);
638 if (ret < 0)
639 printk(KERN_WARNING "IDE: %s: device_register error: %d\n",
640 __FUNCTION__, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641}
642
643static int wait_hwif_ready(ide_hwif_t *hwif)
644{
645 int rc;
646
647 printk(KERN_DEBUG "Probing IDE interface %s...\n", hwif->name);
648
649 /* Let HW settle down a bit from whatever init state we
650 * come from */
651 mdelay(2);
652
653 /* Wait for BSY bit to go away, spec timeout is 30 seconds,
654 * I know of at least one disk who takes 31 seconds, I use 35
655 * here to be safe
656 */
657 rc = ide_wait_not_busy(hwif, 35000);
658 if (rc)
659 return rc;
660
661 /* Now make sure both master & slave are ready */
662 SELECT_DRIVE(&hwif->drives[0]);
663 hwif->OUTB(8, hwif->io_ports[IDE_CONTROL_OFFSET]);
664 mdelay(2);
Marcello Maggioni284e4232005-05-26 15:47:35 +0200665 rc = ide_wait_not_busy(hwif, 35000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 if (rc)
667 return rc;
668 SELECT_DRIVE(&hwif->drives[1]);
669 hwif->OUTB(8, hwif->io_ports[IDE_CONTROL_OFFSET]);
670 mdelay(2);
Marcello Maggioni284e4232005-05-26 15:47:35 +0200671 rc = ide_wait_not_busy(hwif, 35000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672
673 /* Exit function with master reselected (let's be sane) */
674 SELECT_DRIVE(&hwif->drives[0]);
675
676 return rc;
677}
678
679/**
680 * ide_undecoded_slave - look for bad CF adapters
681 * @hwif: interface
682 *
683 * Analyse the drives on the interface and attempt to decide if we
684 * have the same drive viewed twice. This occurs with crap CF adapters
685 * and PCMCIA sometimes.
686 */
687
688void ide_undecoded_slave(ide_hwif_t *hwif)
689{
690 ide_drive_t *drive0 = &hwif->drives[0];
691 ide_drive_t *drive1 = &hwif->drives[1];
692
693 if (drive0->present == 0 || drive1->present == 0)
694 return;
695
696 /* If the models don't match they are not the same product */
697 if (strcmp(drive0->id->model, drive1->id->model))
698 return;
699
700 /* Serial numbers do not match */
701 if (strncmp(drive0->id->serial_no, drive1->id->serial_no, 20))
702 return;
703
704 /* No serial number, thankfully very rare for CF */
705 if (drive0->id->serial_no[0] == 0)
706 return;
707
708 /* Appears to be an IDE flash adapter with decode bugs */
709 printk(KERN_WARNING "ide-probe: ignoring undecoded slave\n");
710
711 drive1->present = 0;
712}
713
714EXPORT_SYMBOL_GPL(ide_undecoded_slave);
715
716/*
717 * This routine only knows how to look for drive units 0 and 1
718 * on an interface, so any setting of MAX_DRIVES > 2 won't work here.
719 */
Bartlomiej Zolnierkiewicz0380dad2007-06-08 15:14:29 +0200720static void probe_hwif(ide_hwif_t *hwif, void (*fixup)(ide_hwif_t *hwif))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721{
722 unsigned int unit;
723 unsigned long flags;
724 unsigned int irqd;
725
726 if (hwif->noprobe)
727 return;
728
729 if ((hwif->chipset != ide_4drives || !hwif->mate || !hwif->mate->present) &&
730 (ide_hwif_request_regions(hwif))) {
731 u16 msgout = 0;
732 for (unit = 0; unit < MAX_DRIVES; ++unit) {
733 ide_drive_t *drive = &hwif->drives[unit];
734 if (drive->present) {
735 drive->present = 0;
736 printk(KERN_ERR "%s: ERROR, PORTS ALREADY IN USE\n",
737 drive->name);
738 msgout = 1;
739 }
740 }
741 if (!msgout)
742 printk(KERN_ERR "%s: ports already in use, skipping probe\n",
743 hwif->name);
744 return;
745 }
746
747 /*
748 * We must always disable IRQ, as probe_for_drive will assert IRQ, but
749 * we'll install our IRQ driver much later...
750 */
751 irqd = hwif->irq;
752 if (irqd)
753 disable_irq(hwif->irq);
754
755 local_irq_set(flags);
756
757 /* This is needed on some PPCs and a bunch of BIOS-less embedded
758 * platforms. Typical cases are:
759 *
760 * - The firmware hard reset the disk before booting the kernel,
761 * the drive is still doing it's poweron-reset sequence, that
762 * can take up to 30 seconds
763 * - The firmware does nothing (or no firmware), the device is
764 * still in POST state (same as above actually).
765 * - Some CD/DVD/Writer combo drives tend to drive the bus during
766 * their reset sequence even when they are non-selected slave
767 * devices, thus preventing discovery of the main HD
768 *
769 * Doing this wait-for-busy should not harm any existing configuration
770 * (at least things won't be worse than what current code does, that
771 * is blindly go & talk to the drive) and fix some issues like the
772 * above.
773 *
774 * BenH.
775 */
776 if (wait_hwif_ready(hwif) == -EBUSY)
777 printk(KERN_DEBUG "%s: Wait for ready failed before probe !\n", hwif->name);
778
779 /*
780 * Second drive should only exist if first drive was found,
781 * but a lot of cdrom drives are configured as single slaves.
782 */
783 for (unit = 0; unit < MAX_DRIVES; ++unit) {
784 ide_drive_t *drive = &hwif->drives[unit];
785 drive->dn = (hwif->channel ? 2 : 0) + unit;
786 (void) probe_for_drive(drive);
787 if (drive->present && !hwif->present) {
788 hwif->present = 1;
789 if (hwif->chipset != ide_4drives ||
790 !hwif->mate ||
791 !hwif->mate->present) {
792 hwif_register(hwif);
793 }
794 }
795 }
796 if (hwif->io_ports[IDE_CONTROL_OFFSET] && hwif->reset) {
797 unsigned long timeout = jiffies + WAIT_WORSTCASE;
798 u8 stat;
799
800 printk(KERN_WARNING "%s: reset\n", hwif->name);
801 hwif->OUTB(12, hwif->io_ports[IDE_CONTROL_OFFSET]);
802 udelay(10);
803 hwif->OUTB(8, hwif->io_ports[IDE_CONTROL_OFFSET]);
804 do {
805 msleep(50);
806 stat = hwif->INB(hwif->io_ports[IDE_STATUS_OFFSET]);
807 } while ((stat & BUSY_STAT) && time_after(timeout, jiffies));
808
809 }
810 local_irq_restore(flags);
811 /*
812 * Use cached IRQ number. It might be (and is...) changed by probe
813 * code above
814 */
815 if (irqd)
816 enable_irq(irqd);
817
818 if (!hwif->present) {
819 ide_hwif_release_regions(hwif);
820 return;
821 }
822
Bartlomiej Zolnierkiewicz0380dad2007-06-08 15:14:29 +0200823 if (fixup)
824 fixup(hwif);
825
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 for (unit = 0; unit < MAX_DRIVES; ++unit) {
827 ide_drive_t *drive = &hwif->drives[unit];
828
829 if (drive->present) {
830 if (hwif->tuneproc != NULL &&
831 drive->autotune == IDE_TUNE_AUTO)
832 /* auto-tune PIO mode */
833 hwif->tuneproc(drive, 255);
834
835 if (drive->autotune != IDE_TUNE_DEFAULT &&
836 drive->autotune != IDE_TUNE_AUTO)
837 continue;
838
839 drive->nice1 = 1;
840
841 /*
842 * MAJOR HACK BARF :-/
843 *
844 * FIXME: chipsets own this cruft!
845 */
846 /*
847 * Move here to prevent module loading clashing.
848 */
849 // drive->autodma = hwif->autodma;
850 if (hwif->ide_dma_check) {
851 /*
852 * Force DMAing for the beginning of the check.
853 * Some chipsets appear to do interesting
854 * things, if not checked and cleared.
855 * PARANOIA!!!
856 */
Bartlomiej Zolnierkiewicz7469aaf2007-02-17 02:40:26 +0100857 hwif->dma_off_quietly(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858#ifdef CONFIG_IDEDMA_ONLYDISK
859 if (drive->media == ide_disk)
860#endif
Bartlomiej Zolnierkiewicz3608b5d2007-02-17 02:40:26 +0100861 ide_set_dma(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 }
863 }
864 }
Kumar Gala208a08f2006-03-24 03:18:21 -0800865
866 for (unit = 0; unit < MAX_DRIVES; ++unit) {
867 ide_drive_t *drive = &hwif->drives[unit];
868
869 if (hwif->no_io_32bit)
870 drive->no_io_32bit = 1;
871 else
872 drive->no_io_32bit = drive->id->dword_io ? 1 : 0;
873 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874}
875
876static int hwif_init(ide_hwif_t *hwif);
877
878int probe_hwif_init_with_fixup(ide_hwif_t *hwif, void (*fixup)(ide_hwif_t *hwif))
879{
Bartlomiej Zolnierkiewicz0380dad2007-06-08 15:14:29 +0200880 probe_hwif(hwif, fixup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881
882 if (!hwif_init(hwif)) {
883 printk(KERN_INFO "%s: failed to initialize IDE interface\n",
884 hwif->name);
885 return -1;
886 }
887
888 if (hwif->present) {
889 u16 unit = 0;
Randy Dunlap349ae232006-10-03 01:14:23 -0700890 int ret;
891
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 for (unit = 0; unit < MAX_DRIVES; ++unit) {
893 ide_drive_t *drive = &hwif->drives[unit];
894 /* For now don't attach absent drives, we may
895 want them on default or a new "empty" class
896 for hotplug reprobing ? */
897 if (drive->present) {
Randy Dunlap349ae232006-10-03 01:14:23 -0700898 ret = device_register(&drive->gendev);
899 if (ret < 0)
900 printk(KERN_WARNING "IDE: %s: "
901 "device_register error: %d\n",
902 __FUNCTION__, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 }
904 }
905 }
906 return 0;
907}
908
909int probe_hwif_init(ide_hwif_t *hwif)
910{
911 return probe_hwif_init_with_fixup(hwif, NULL);
912}
913
914EXPORT_SYMBOL(probe_hwif_init);
915
916#if MAX_HWIFS > 1
917/*
918 * save_match() is used to simplify logic in init_irq() below.
919 *
920 * A loophole here is that we may not know about a particular
921 * hwif's irq until after that hwif is actually probed/initialized..
922 * This could be a problem for the case where an hwif is on a
923 * dual interface that requires serialization (eg. cmd640) and another
924 * hwif using one of the same irqs is initialized beforehand.
925 *
926 * This routine detects and reports such situations, but does not fix them.
927 */
928static void save_match(ide_hwif_t *hwif, ide_hwif_t *new, ide_hwif_t **match)
929{
930 ide_hwif_t *m = *match;
931
932 if (m && m->hwgroup && m->hwgroup != new->hwgroup) {
933 if (!new->hwgroup)
934 return;
935 printk("%s: potential irq problem with %s and %s\n",
936 hwif->name, new->name, m->name);
937 }
938 if (!m || m->irq != hwif->irq) /* don't undo a prior perfect match */
939 *match = new;
940}
941#endif /* MAX_HWIFS > 1 */
942
943/*
944 * init request queue
945 */
946static int ide_init_queue(ide_drive_t *drive)
947{
948 request_queue_t *q;
949 ide_hwif_t *hwif = HWIF(drive);
950 int max_sectors = 256;
951 int max_sg_entries = PRD_ENTRIES;
952
953 /*
954 * Our default set up assumes the normal IDE case,
955 * that is 64K segmenting, standard PRD setup
956 * and LBA28. Some drivers then impose their own
957 * limits and LBA48 we could raise it but as yet
958 * do not.
959 */
Christoph Lameter19460892005-06-23 00:08:19 -0700960
Ravikiran G Thirumalai556e58f2005-08-04 12:53:26 -0700961 q = blk_init_queue_node(do_ide_request, &ide_lock, hwif_to_node(hwif));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 if (!q)
963 return 1;
964
965 q->queuedata = drive;
966 blk_queue_segment_boundary(q, 0xffff);
967
968 if (!hwif->rqsize) {
969 if (hwif->no_lba48 || hwif->no_lba48_dma)
970 hwif->rqsize = 256;
971 else
972 hwif->rqsize = 65536;
973 }
974 if (hwif->rqsize < max_sectors)
975 max_sectors = hwif->rqsize;
976 blk_queue_max_sectors(q, max_sectors);
977
978#ifdef CONFIG_PCI
979 /* When we have an IOMMU, we may have a problem where pci_map_sg()
980 * creates segments that don't completely match our boundary
981 * requirements and thus need to be broken up again. Because it
982 * doesn't align properly either, we may actually have to break up
983 * to more segments than what was we got in the first place, a max
984 * worst case is twice as many.
985 * This will be fixed once we teach pci_map_sg() about our boundary
986 * requirements, hopefully soon. *FIXME*
987 */
988 if (!PCI_DMA_BUS_IS_PHYS)
989 max_sg_entries >>= 1;
990#endif /* CONFIG_PCI */
991
992 blk_queue_max_hw_segments(q, max_sg_entries);
993 blk_queue_max_phys_segments(q, max_sg_entries);
994
995 /* assign drive queue */
996 drive->queue = q;
997
998 /* needs drive->queue to be set */
999 ide_toggle_bounce(drive, 1);
1000
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 return 0;
1002}
1003
1004/*
1005 * This routine sets up the irq for an ide interface, and creates a new
1006 * hwgroup for the irq/hwif if none was previously assigned.
1007 *
1008 * Much of the code is for correctly detecting/handling irq sharing
1009 * and irq serialization situations. This is somewhat complex because
1010 * it handles static as well as dynamic (PCMCIA) IDE interfaces.
1011 *
Thomas Gleixner362537b2006-07-01 19:29:34 -07001012 * The IRQF_DISABLED in sa_flags means ide_intr() is always entered with
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 * interrupts completely disabled. This can be bad for interrupt latency,
1014 * but anything else has led to problems on some machines. We re-enable
1015 * interrupts as much as we can safely do in most places.
1016 */
1017static int init_irq (ide_hwif_t *hwif)
1018{
1019 unsigned int index;
1020 ide_hwgroup_t *hwgroup;
1021 ide_hwif_t *match = NULL;
1022
1023
1024 BUG_ON(in_interrupt());
1025 BUG_ON(irqs_disabled());
Ravikiran G Thirumalai556e58f2005-08-04 12:53:26 -07001026 BUG_ON(hwif == NULL);
1027
Matthias Kaehlckeef298882007-07-09 23:17:55 +02001028 mutex_lock(&ide_cfg_mtx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 hwif->hwgroup = NULL;
1030#if MAX_HWIFS > 1
1031 /*
1032 * Group up with any other hwifs that share our irq(s).
1033 */
1034 for (index = 0; index < MAX_HWIFS; index++) {
1035 ide_hwif_t *h = &ide_hwifs[index];
1036 if (h->hwgroup) { /* scan only initialized hwif's */
1037 if (hwif->irq == h->irq) {
1038 hwif->sharing_irq = h->sharing_irq = 1;
1039 if (hwif->chipset != ide_pci ||
1040 h->chipset != ide_pci) {
1041 save_match(hwif, h, &match);
1042 }
1043 }
1044 if (hwif->serialized) {
1045 if (hwif->mate && hwif->mate->irq == h->irq)
1046 save_match(hwif, h, &match);
1047 }
1048 if (h->serialized) {
1049 if (h->mate && hwif->irq == h->mate->irq)
1050 save_match(hwif, h, &match);
1051 }
1052 }
1053 }
1054#endif /* MAX_HWIFS > 1 */
1055 /*
1056 * If we are still without a hwgroup, then form a new one
1057 */
1058 if (match) {
1059 hwgroup = match->hwgroup;
1060 hwif->hwgroup = hwgroup;
1061 /*
1062 * Link us into the hwgroup.
1063 * This must be done early, do ensure that unexpected_intr
1064 * can find the hwif and prevent irq storms.
1065 * No drives are attached to the new hwif, choose_drive
1066 * can't do anything stupid (yet).
1067 * Add ourself as the 2nd entry to the hwgroup->hwif
1068 * linked list, the first entry is the hwif that owns
1069 * hwgroup->handler - do not change that.
1070 */
1071 spin_lock_irq(&ide_lock);
1072 hwif->next = hwgroup->hwif->next;
1073 hwgroup->hwif->next = hwif;
1074 spin_unlock_irq(&ide_lock);
1075 } else {
Christoph Lameter19460892005-06-23 00:08:19 -07001076 hwgroup = kmalloc_node(sizeof(ide_hwgroup_t), GFP_KERNEL,
Ravikiran G Thirumalai556e58f2005-08-04 12:53:26 -07001077 hwif_to_node(hwif->drives[0].hwif));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 if (!hwgroup)
1079 goto out_up;
1080
1081 hwif->hwgroup = hwgroup;
1082
1083 memset(hwgroup, 0, sizeof(ide_hwgroup_t));
1084 hwgroup->hwif = hwif->next = hwif;
1085 hwgroup->rq = NULL;
1086 hwgroup->handler = NULL;
1087 hwgroup->drive = NULL;
1088 hwgroup->busy = 0;
1089 init_timer(&hwgroup->timer);
1090 hwgroup->timer.function = &ide_timer_expiry;
1091 hwgroup->timer.data = (unsigned long) hwgroup;
1092 }
1093
1094 /*
1095 * Allocate the irq, if not already obtained for another hwif
1096 */
1097 if (!match || match->irq != hwif->irq) {
Thomas Gleixner362537b2006-07-01 19:29:34 -07001098 int sa = IRQF_DISABLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099#if defined(__mc68000__) || defined(CONFIG_APUS)
Thomas Gleixner362537b2006-07-01 19:29:34 -07001100 sa = IRQF_SHARED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101#endif /* __mc68000__ || CONFIG_APUS */
1102
1103 if (IDE_CHIPSET_IS_PCI(hwif->chipset)) {
Thomas Gleixner362537b2006-07-01 19:29:34 -07001104 sa = IRQF_SHARED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105#ifndef CONFIG_IDEPCI_SHARE_IRQ
Thomas Gleixner362537b2006-07-01 19:29:34 -07001106 sa |= IRQF_DISABLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107#endif /* CONFIG_IDEPCI_SHARE_IRQ */
1108 }
1109
1110 if (hwif->io_ports[IDE_CONTROL_OFFSET])
1111 /* clear nIEN */
1112 hwif->OUTB(0x08, hwif->io_ports[IDE_CONTROL_OFFSET]);
1113
1114 if (request_irq(hwif->irq,&ide_intr,sa,hwif->name,hwgroup))
1115 goto out_unlink;
1116 }
1117
1118 /*
1119 * For any present drive:
1120 * - allocate the block device queue
1121 * - link drive into the hwgroup
1122 */
1123 for (index = 0; index < MAX_DRIVES; ++index) {
1124 ide_drive_t *drive = &hwif->drives[index];
1125 if (!drive->present)
1126 continue;
1127 if (ide_init_queue(drive)) {
1128 printk(KERN_ERR "ide: failed to init %s\n",drive->name);
1129 continue;
1130 }
1131 spin_lock_irq(&ide_lock);
1132 if (!hwgroup->drive) {
1133 /* first drive for hwgroup. */
1134 drive->next = drive;
1135 hwgroup->drive = drive;
1136 hwgroup->hwif = HWIF(hwgroup->drive);
1137 } else {
1138 drive->next = hwgroup->drive->next;
1139 hwgroup->drive->next = drive;
1140 }
1141 spin_unlock_irq(&ide_lock);
1142 }
1143
David S. Millerc6387a42006-06-20 01:21:29 -07001144#if !defined(__mc68000__) && !defined(CONFIG_APUS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 printk("%s at 0x%03lx-0x%03lx,0x%03lx on irq %d", hwif->name,
1146 hwif->io_ports[IDE_DATA_OFFSET],
1147 hwif->io_ports[IDE_DATA_OFFSET]+7,
1148 hwif->io_ports[IDE_CONTROL_OFFSET], hwif->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149#else
1150 printk("%s at 0x%08lx on irq %d", hwif->name,
1151 hwif->io_ports[IDE_DATA_OFFSET], hwif->irq);
1152#endif /* __mc68000__ && CONFIG_APUS */
1153 if (match)
1154 printk(" (%sed with %s)",
1155 hwif->sharing_irq ? "shar" : "serializ", match->name);
1156 printk("\n");
Matthias Kaehlckeef298882007-07-09 23:17:55 +02001157 mutex_unlock(&ide_cfg_mtx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 return 0;
1159out_unlink:
1160 spin_lock_irq(&ide_lock);
1161 if (hwif->next == hwif) {
1162 BUG_ON(match);
1163 BUG_ON(hwgroup->hwif != hwif);
1164 kfree(hwgroup);
1165 } else {
1166 ide_hwif_t *g;
1167 g = hwgroup->hwif;
1168 while (g->next != hwif)
1169 g = g->next;
1170 g->next = hwif->next;
1171 if (hwgroup->hwif == hwif) {
1172 /* Impossible. */
1173 printk(KERN_ERR "Duh. Uninitialized hwif listed as active hwif.\n");
1174 hwgroup->hwif = g;
1175 }
1176 BUG_ON(hwgroup->hwif == hwif);
1177 }
1178 spin_unlock_irq(&ide_lock);
1179out_up:
Matthias Kaehlckeef298882007-07-09 23:17:55 +02001180 mutex_unlock(&ide_cfg_mtx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 return 1;
1182}
1183
1184static int ata_lock(dev_t dev, void *data)
1185{
1186 /* FIXME: we want to pin hwif down */
1187 return 0;
1188}
1189
1190static struct kobject *ata_probe(dev_t dev, int *part, void *data)
1191{
1192 ide_hwif_t *hwif = data;
1193 int unit = *part >> PARTN_BITS;
1194 ide_drive_t *drive = &hwif->drives[unit];
1195 if (!drive->present)
1196 return NULL;
1197
1198 if (drive->media == ide_disk)
1199 request_module("ide-disk");
1200 if (drive->scsi)
1201 request_module("ide-scsi");
1202 if (drive->media == ide_cdrom || drive->media == ide_optical)
1203 request_module("ide-cd");
1204 if (drive->media == ide_tape)
1205 request_module("ide-tape");
1206 if (drive->media == ide_floppy)
1207 request_module("ide-floppy");
1208
1209 return NULL;
1210}
1211
1212static struct kobject *exact_match(dev_t dev, int *part, void *data)
1213{
1214 struct gendisk *p = data;
1215 *part &= (1 << PARTN_BITS) - 1;
1216 return &p->kobj;
1217}
1218
1219static int exact_lock(dev_t dev, void *data)
1220{
1221 struct gendisk *p = data;
1222
1223 if (!get_disk(p))
1224 return -1;
1225 return 0;
1226}
1227
1228void ide_register_region(struct gendisk *disk)
1229{
1230 blk_register_region(MKDEV(disk->major, disk->first_minor),
1231 disk->minors, NULL, exact_match, exact_lock, disk);
1232}
1233
1234EXPORT_SYMBOL_GPL(ide_register_region);
1235
1236void ide_unregister_region(struct gendisk *disk)
1237{
1238 blk_unregister_region(MKDEV(disk->major, disk->first_minor),
1239 disk->minors);
1240}
1241
1242EXPORT_SYMBOL_GPL(ide_unregister_region);
1243
1244void ide_init_disk(struct gendisk *disk, ide_drive_t *drive)
1245{
1246 ide_hwif_t *hwif = drive->hwif;
1247 unsigned int unit = (drive->select.all >> 4) & 1;
1248
1249 disk->major = hwif->major;
1250 disk->first_minor = unit << PARTN_BITS;
1251 sprintf(disk->disk_name, "hd%c", 'a' + hwif->index * MAX_DRIVES + unit);
1252 disk->queue = drive->queue;
1253}
1254
1255EXPORT_SYMBOL_GPL(ide_init_disk);
1256
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02001257static void ide_remove_drive_from_hwgroup(ide_drive_t *drive)
1258{
1259 ide_hwgroup_t *hwgroup = drive->hwif->hwgroup;
1260
1261 if (drive == drive->next) {
1262 /* special case: last drive from hwgroup. */
1263 BUG_ON(hwgroup->drive != drive);
1264 hwgroup->drive = NULL;
1265 } else {
1266 ide_drive_t *walk;
1267
1268 walk = hwgroup->drive;
1269 while (walk->next != drive)
1270 walk = walk->next;
1271 walk->next = drive->next;
1272 if (hwgroup->drive == drive) {
1273 hwgroup->drive = drive->next;
1274 hwgroup->hwif = hwgroup->drive->hwif;
1275 }
1276 }
1277 BUG_ON(hwgroup->drive == drive);
1278}
1279
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280static void drive_release_dev (struct device *dev)
1281{
1282 ide_drive_t *drive = container_of(dev, ide_drive_t, gendev);
1283
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02001284 spin_lock_irq(&ide_lock);
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02001285 ide_remove_drive_from_hwgroup(drive);
Jesper Juhl6044ec82005-11-07 01:01:32 -08001286 kfree(drive->id);
1287 drive->id = NULL;
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02001288 drive->present = 0;
1289 /* Messed up locking ... */
1290 spin_unlock_irq(&ide_lock);
1291 blk_cleanup_queue(drive->queue);
1292 spin_lock_irq(&ide_lock);
1293 drive->queue = NULL;
1294 spin_unlock_irq(&ide_lock);
1295
Aleksey Makarovf36d4022006-01-09 15:59:27 -08001296 complete(&drive->gendev_rel_comp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297}
1298
1299/*
1300 * init_gendisk() (as opposed to ide_geninit) is called for each major device,
1301 * after probing for drives, to allocate partition tables and other data
1302 * structures needed for the routines in genhd.c. ide_geninit() gets called
1303 * somewhat later, during the partition check.
1304 */
1305static void init_gendisk (ide_hwif_t *hwif)
1306{
1307 unsigned int unit;
1308
1309 for (unit = 0; unit < MAX_DRIVES; ++unit) {
1310 ide_drive_t * drive = &hwif->drives[unit];
1311 ide_add_generic_settings(drive);
1312 snprintf(drive->gendev.bus_id,BUS_ID_SIZE,"%u.%u",
1313 hwif->index,unit);
1314 drive->gendev.parent = &hwif->gendev;
1315 drive->gendev.bus = &ide_bus_type;
1316 drive->gendev.driver_data = drive;
1317 drive->gendev.release = drive_release_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 }
1319 blk_register_region(MKDEV(hwif->major, 0), MAX_DRIVES << PARTN_BITS,
1320 THIS_MODULE, ata_probe, ata_lock, hwif);
1321}
1322
1323static int hwif_init(ide_hwif_t *hwif)
1324{
1325 int old_irq;
1326
1327 /* Return success if no device is connected */
1328 if (!hwif->present)
1329 return 1;
1330
1331 if (!hwif->irq) {
1332 if (!(hwif->irq = ide_default_irq(hwif->io_ports[IDE_DATA_OFFSET])))
1333 {
1334 printk("%s: DISABLED, NO IRQ\n", hwif->name);
1335 return (hwif->present = 0);
1336 }
1337 }
1338#ifdef CONFIG_BLK_DEV_HD
1339 if (hwif->irq == HD_IRQ && hwif->io_ports[IDE_DATA_OFFSET] != HD_DATA) {
1340 printk("%s: CANNOT SHARE IRQ WITH OLD "
1341 "HARDDISK DRIVER (hd.c)\n", hwif->name);
1342 return (hwif->present = 0);
1343 }
1344#endif /* CONFIG_BLK_DEV_HD */
1345
1346 /* we set it back to 1 if all is ok below */
1347 hwif->present = 0;
1348
1349 if (register_blkdev(hwif->major, hwif->name))
1350 return 0;
1351
1352 if (!hwif->sg_max_nents)
1353 hwif->sg_max_nents = PRD_ENTRIES;
1354
1355 hwif->sg_table = kmalloc(sizeof(struct scatterlist)*hwif->sg_max_nents,
1356 GFP_KERNEL);
1357 if (!hwif->sg_table) {
1358 printk(KERN_ERR "%s: unable to allocate SG table.\n", hwif->name);
1359 goto out;
1360 }
1361
1362 if (init_irq(hwif) == 0)
1363 goto done;
1364
1365 old_irq = hwif->irq;
1366 /*
1367 * It failed to initialise. Find the default IRQ for
1368 * this port and try that.
1369 */
1370 if (!(hwif->irq = ide_default_irq(hwif->io_ports[IDE_DATA_OFFSET]))) {
1371 printk("%s: Disabled unable to get IRQ %d.\n",
1372 hwif->name, old_irq);
1373 goto out;
1374 }
1375 if (init_irq(hwif)) {
1376 printk("%s: probed IRQ %d and default IRQ %d failed.\n",
1377 hwif->name, old_irq, hwif->irq);
1378 goto out;
1379 }
1380 printk("%s: probed IRQ %d failed, using default.\n",
1381 hwif->name, hwif->irq);
1382
1383done:
1384 init_gendisk(hwif);
Hannes Reineckee3a59b42007-02-07 18:19:37 +01001385
1386 ide_acpi_init(hwif);
1387
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 hwif->present = 1; /* success */
1389 return 1;
1390
1391out:
1392 unregister_blkdev(hwif->major, hwif->name);
1393 return 0;
1394}
1395
1396int ideprobe_init (void)
1397{
1398 unsigned int index;
1399 int probe[MAX_HWIFS];
1400
1401 memset(probe, 0, MAX_HWIFS * sizeof(int));
1402 for (index = 0; index < MAX_HWIFS; ++index)
1403 probe[index] = !ide_hwifs[index].present;
1404
1405 for (index = 0; index < MAX_HWIFS; ++index)
1406 if (probe[index])
Bartlomiej Zolnierkiewicz0380dad2007-06-08 15:14:29 +02001407 probe_hwif(&ide_hwifs[index], NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 for (index = 0; index < MAX_HWIFS; ++index)
1409 if (probe[index])
1410 hwif_init(&ide_hwifs[index]);
1411 for (index = 0; index < MAX_HWIFS; ++index) {
1412 if (probe[index]) {
1413 ide_hwif_t *hwif = &ide_hwifs[index];
1414 int unit;
1415 if (!hwif->present)
1416 continue;
1417 if (hwif->chipset == ide_unknown || hwif->chipset == ide_forced)
1418 hwif->chipset = ide_generic;
1419 for (unit = 0; unit < MAX_DRIVES; ++unit)
Randy Dunlap349ae232006-10-03 01:14:23 -07001420 if (hwif->drives[unit].present) {
1421 int ret = device_register(
1422 &hwif->drives[unit].gendev);
1423 if (ret < 0)
1424 printk(KERN_WARNING "IDE: %s: "
1425 "device_register error: %d\n",
1426 __FUNCTION__, ret);
1427 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 }
1429 }
Bartlomiej Zolnierkiewicz5cbf79c2007-05-10 00:01:11 +02001430 for (index = 0; index < MAX_HWIFS; ++index)
1431 if (probe[index])
1432 ide_proc_register_port(&ide_hwifs[index]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 return 0;
1434}
1435
1436EXPORT_SYMBOL_GPL(ideprobe_init);