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