blob: 6a6f2e066b4679b7c1a2ff16704af8a67bbc7673 [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
175 if (strstr(id->model, "E X A B Y T E N E S T"))
176 goto err_misc;
177
178 /* we depend on this a lot! */
179 id->model[sizeof(id->model)-1] = '\0';
180 printk("%s: %s, ", drive->name, id->model);
181 drive->present = 1;
182 drive->dead = 0;
183
184 /*
185 * Check for an ATAPI device
186 */
187 if (cmd == WIN_PIDENTIFY) {
188 u8 type = (id->config >> 8) & 0x1f;
189 printk("ATAPI ");
190 switch (type) {
191 case ide_floppy:
192 if (!strstr(id->model, "CD-ROM")) {
193 if (!strstr(id->model, "oppy") &&
194 !strstr(id->model, "poyp") &&
195 !strstr(id->model, "ZIP"))
196 printk("cdrom or floppy?, assuming ");
197 if (drive->media != ide_cdrom) {
198 printk ("FLOPPY");
199 drive->removable = 1;
200 break;
201 }
202 }
203 /* Early cdrom models used zero */
204 type = ide_cdrom;
205 case ide_cdrom:
206 drive->removable = 1;
207#ifdef CONFIG_PPC
208 /* kludge for Apple PowerBook internal zip */
209 if (!strstr(id->model, "CD-ROM") &&
210 strstr(id->model, "ZIP")) {
211 printk ("FLOPPY");
212 type = ide_floppy;
213 break;
214 }
215#endif
216 printk ("CD/DVD-ROM");
217 break;
218 case ide_tape:
219 printk ("TAPE");
220 break;
221 case ide_optical:
222 printk ("OPTICAL");
223 drive->removable = 1;
224 break;
225 default:
226 printk("UNKNOWN (type %d)", type);
227 break;
228 }
229 printk (" drive\n");
230 drive->media = type;
231 /* an ATAPI device ignores DRDY */
232 drive->ready_stat = 0;
233 return;
234 }
235
236 /*
237 * Not an ATAPI device: looks like a "regular" hard disk
238 */
Richard Purdie98109332006-02-03 03:04:55 -0800239
240 /*
241 * 0x848a = CompactFlash device
242 * These are *not* removable in Linux definition of the term
243 */
244
245 if ((id->config != 0x848a) && (id->config & (1<<7)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 drive->removable = 1;
247
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 drive->media = ide_disk;
Richard Purdie98109332006-02-03 03:04:55 -0800249 printk("%s DISK drive\n", (id->config == 0x848a) ? "CFA" : "ATA" );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 QUIRK_LIST(drive);
251 return;
252
253err_misc:
254 kfree(id);
255 drive->present = 0;
256 return;
257}
258
259/**
260 * actual_try_to_identify - send ata/atapi identify
261 * @drive: drive to identify
262 * @cmd: command to use
263 *
264 * try_to_identify() sends an ATA(PI) IDENTIFY request to a drive
265 * and waits for a response. It also monitors irqs while this is
266 * happening, in hope of automatically determining which one is
267 * being used by the interface.
268 *
269 * Returns: 0 device was identified
270 * 1 device timed-out (no response to identify request)
271 * 2 device aborted the command (refused to identify itself)
272 */
273
274static int actual_try_to_identify (ide_drive_t *drive, u8 cmd)
275{
276 ide_hwif_t *hwif = HWIF(drive);
277 int rc;
278 unsigned long hd_status;
279 unsigned long timeout;
280 u8 s = 0, a = 0;
281
282 /* take a deep breath */
283 msleep(50);
284
285 if (IDE_CONTROL_REG) {
286 a = hwif->INB(IDE_ALTSTATUS_REG);
287 s = hwif->INB(IDE_STATUS_REG);
288 if ((a ^ s) & ~INDEX_STAT) {
289 printk(KERN_INFO "%s: probing with STATUS(0x%02x) instead of "
290 "ALTSTATUS(0x%02x)\n", drive->name, s, a);
291 /* ancient Seagate drives, broken interfaces */
292 hd_status = IDE_STATUS_REG;
293 } else {
294 /* use non-intrusive polling */
295 hd_status = IDE_ALTSTATUS_REG;
296 }
297 } else
298 hd_status = IDE_STATUS_REG;
299
300 /* set features register for atapi
301 * identify command to be sure of reply
302 */
303 if ((cmd == WIN_PIDENTIFY))
304 /* disable dma & overlap */
305 hwif->OUTB(0, IDE_FEATURE_REG);
306
307 /* ask drive for ID */
308 hwif->OUTB(cmd, IDE_COMMAND_REG);
309
310 timeout = ((cmd == WIN_IDENTIFY) ? WAIT_WORSTCASE : WAIT_PIDENTIFY) / 2;
311 timeout += jiffies;
312 do {
313 if (time_after(jiffies, timeout)) {
314 /* drive timed-out */
315 return 1;
316 }
317 /* give drive a breather */
318 msleep(50);
319 } while ((hwif->INB(hd_status)) & BUSY_STAT);
320
321 /* wait for IRQ and DRQ_STAT */
322 msleep(50);
323 if (OK_STAT((hwif->INB(IDE_STATUS_REG)), DRQ_STAT, BAD_R_STAT)) {
324 unsigned long flags;
325
326 /* local CPU only; some systems need this */
327 local_irq_save(flags);
328 /* drive returned ID */
329 do_identify(drive, cmd);
330 /* drive responded with ID */
331 rc = 0;
332 /* clear drive IRQ */
333 (void) hwif->INB(IDE_STATUS_REG);
334 local_irq_restore(flags);
335 } else {
336 /* drive refused ID */
337 rc = 2;
338 }
339 return rc;
340}
341
342/**
343 * try_to_identify - try to identify a drive
344 * @drive: drive to probe
345 * @cmd: command to use
346 *
347 * Issue the identify command and then do IRQ probing to
348 * complete the identification when needed by finding the
349 * IRQ the drive is attached to
350 */
351
352static int try_to_identify (ide_drive_t *drive, u8 cmd)
353{
354 ide_hwif_t *hwif = HWIF(drive);
355 int retval;
356 int autoprobe = 0;
357 unsigned long cookie = 0;
358
359 /*
360 * Disable device irq unless we need to
361 * probe for it. Otherwise we'll get spurious
362 * interrupts during the identify-phase that
363 * the irq handler isn't expecting.
364 */
365 if (IDE_CONTROL_REG) {
366 u8 ctl = drive->ctl | 2;
367 if (!hwif->irq) {
368 autoprobe = 1;
369 cookie = probe_irq_on();
370 /* enable device irq */
371 ctl &= ~2;
372 }
373 hwif->OUTB(ctl, IDE_CONTROL_REG);
374 }
375
376 retval = actual_try_to_identify(drive, cmd);
377
378 if (autoprobe) {
379 int irq;
380 /* mask device irq */
381 hwif->OUTB(drive->ctl|2, IDE_CONTROL_REG);
382 /* clear drive IRQ */
383 (void) hwif->INB(IDE_STATUS_REG);
384 udelay(5);
385 irq = probe_irq_off(cookie);
386 if (!hwif->irq) {
387 if (irq > 0) {
388 hwif->irq = irq;
389 } else {
390 /* Mmmm.. multiple IRQs..
391 * don't know which was ours
392 */
393 printk("%s: IRQ probe failed (0x%lx)\n",
394 drive->name, cookie);
395 }
396 }
397 }
398 return retval;
399}
400
401
402/**
403 * do_probe - probe an IDE device
404 * @drive: drive to probe
405 * @cmd: command to use
406 *
407 * do_probe() has the difficult job of finding a drive if it exists,
408 * without getting hung up if it doesn't exist, without trampling on
409 * ethernet cards, and without leaving any IRQs dangling to haunt us later.
410 *
411 * If a drive is "known" to exist (from CMOS or kernel parameters),
412 * but does not respond right away, the probe will "hang in there"
413 * for the maximum wait time (about 30 seconds), otherwise it will
414 * exit much more quickly.
415 *
416 * Returns: 0 device was identified
417 * 1 device timed-out (no response to identify request)
418 * 2 device aborted the command (refused to identify itself)
419 * 3 bad status from device (possible for ATAPI drives)
420 * 4 probe was not attempted because failure was obvious
421 */
422
423static int do_probe (ide_drive_t *drive, u8 cmd)
424{
425 int rc;
426 ide_hwif_t *hwif = HWIF(drive);
427
428 if (drive->present) {
429 /* avoid waiting for inappropriate probes */
430 if ((drive->media != ide_disk) && (cmd == WIN_IDENTIFY))
431 return 4;
432 }
433#ifdef DEBUG
434 printk("probing for %s: present=%d, media=%d, probetype=%s\n",
435 drive->name, drive->present, drive->media,
436 (cmd == WIN_IDENTIFY) ? "ATA" : "ATAPI");
437#endif
438
439 /* needed for some systems
440 * (e.g. crw9624 as drive0 with disk as slave)
441 */
442 msleep(50);
443 SELECT_DRIVE(drive);
444 msleep(50);
445 if (hwif->INB(IDE_SELECT_REG) != drive->select.all && !drive->present) {
446 if (drive->select.b.unit != 0) {
447 /* exit with drive0 selected */
448 SELECT_DRIVE(&hwif->drives[0]);
449 /* allow BUSY_STAT to assert & clear */
450 msleep(50);
451 }
452 /* no i/f present: mmm.. this should be a 4 -ml */
453 return 3;
454 }
455
456 if (OK_STAT((hwif->INB(IDE_STATUS_REG)), READY_STAT, BUSY_STAT) ||
457 drive->present || cmd == WIN_PIDENTIFY) {
458 /* send cmd and wait */
459 if ((rc = try_to_identify(drive, cmd))) {
460 /* failed: try again */
461 rc = try_to_identify(drive,cmd);
462 }
463 if (hwif->INB(IDE_STATUS_REG) == (BUSY_STAT|READY_STAT))
464 return 4;
465
466 if ((rc == 1 && cmd == WIN_PIDENTIFY) &&
467 ((drive->autotune == IDE_TUNE_DEFAULT) ||
468 (drive->autotune == IDE_TUNE_AUTO))) {
469 unsigned long timeout;
470 printk("%s: no response (status = 0x%02x), "
471 "resetting drive\n", drive->name,
472 hwif->INB(IDE_STATUS_REG));
473 msleep(50);
474 hwif->OUTB(drive->select.all, IDE_SELECT_REG);
475 msleep(50);
476 hwif->OUTB(WIN_SRST, IDE_COMMAND_REG);
477 timeout = jiffies;
478 while (((hwif->INB(IDE_STATUS_REG)) & BUSY_STAT) &&
479 time_before(jiffies, timeout + WAIT_WORSTCASE))
480 msleep(50);
481 rc = try_to_identify(drive, cmd);
482 }
483 if (rc == 1)
484 printk("%s: no response (status = 0x%02x)\n",
485 drive->name, hwif->INB(IDE_STATUS_REG));
486 /* ensure drive irq is clear */
487 (void) hwif->INB(IDE_STATUS_REG);
488 } else {
489 /* not present or maybe ATAPI */
490 rc = 3;
491 }
492 if (drive->select.b.unit != 0) {
493 /* exit with drive0 selected */
494 SELECT_DRIVE(&hwif->drives[0]);
495 msleep(50);
496 /* ensure drive irq is clear */
497 (void) hwif->INB(IDE_STATUS_REG);
498 }
499 return rc;
500}
501
502/*
503 *
504 */
505static void enable_nest (ide_drive_t *drive)
506{
507 ide_hwif_t *hwif = HWIF(drive);
508 unsigned long timeout;
509
510 printk("%s: enabling %s -- ", hwif->name, drive->id->model);
511 SELECT_DRIVE(drive);
512 msleep(50);
513 hwif->OUTB(EXABYTE_ENABLE_NEST, IDE_COMMAND_REG);
514 timeout = jiffies + WAIT_WORSTCASE;
515 do {
516 if (time_after(jiffies, timeout)) {
517 printk("failed (timeout)\n");
518 return;
519 }
520 msleep(50);
521 } while ((hwif->INB(IDE_STATUS_REG)) & BUSY_STAT);
522
523 msleep(50);
524
525 if (!OK_STAT((hwif->INB(IDE_STATUS_REG)), 0, BAD_STAT)) {
526 printk("failed (status = 0x%02x)\n", hwif->INB(IDE_STATUS_REG));
527 } else {
528 printk("success\n");
529 }
530
531 /* if !(success||timed-out) */
532 if (do_probe(drive, WIN_IDENTIFY) >= 2) {
533 /* look for ATAPI device */
534 (void) do_probe(drive, WIN_PIDENTIFY);
535 }
536}
537
538/**
539 * probe_for_drives - upper level drive probe
540 * @drive: drive to probe for
541 *
542 * probe_for_drive() tests for existence of a given drive using do_probe()
543 * and presents things to the user as needed.
544 *
545 * Returns: 0 no device was found
546 * 1 device was found (note: drive->present might
547 * still be 0)
548 */
549
550static inline u8 probe_for_drive (ide_drive_t *drive)
551{
552 /*
553 * In order to keep things simple we have an id
554 * block for all drives at all times. If the device
555 * is pre ATA or refuses ATA/ATAPI identify we
556 * will add faked data to this.
557 *
558 * Also note that 0 everywhere means "can't do X"
559 */
560
Deepak Saxenaf5e3c2f2005-11-07 01:01:25 -0800561 drive->id = kzalloc(SECTOR_WORDS *4, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 drive->id_read = 0;
563 if(drive->id == NULL)
564 {
565 printk(KERN_ERR "ide: out of memory for id data.\n");
566 return 0;
567 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 strcpy(drive->id->model, "UNKNOWN");
569
570 /* skip probing? */
571 if (!drive->noprobe)
572 {
573 /* if !(success||timed-out) */
574 if (do_probe(drive, WIN_IDENTIFY) >= 2) {
575 /* look for ATAPI device */
576 (void) do_probe(drive, WIN_PIDENTIFY);
577 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 if (!drive->present)
579 /* drive not found */
580 return 0;
Alan Cox78595572007-07-03 22:28:35 +0200581 if (strstr(drive->id->model, "E X A B Y T E N E S T"))
582 enable_nest(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583
584 /* identification failed? */
585 if (!drive->id_read) {
586 if (drive->media == ide_disk) {
587 printk(KERN_INFO "%s: non-IDE drive, CHS=%d/%d/%d\n",
588 drive->name, drive->cyl,
589 drive->head, drive->sect);
590 } else if (drive->media == ide_cdrom) {
591 printk(KERN_INFO "%s: ATAPI cdrom (?)\n", drive->name);
592 } else {
593 /* nuke it */
594 printk(KERN_WARNING "%s: Unknown device on bus refused identification. Ignoring.\n", drive->name);
595 drive->present = 0;
596 }
597 }
598 /* drive was found */
599 }
600 if(!drive->present)
601 return 0;
602 /* The drive wasn't being helpful. Add generic info only */
603 if (drive->id_read == 0) {
604 generic_id(drive);
605 return 1;
606 }
607
608 if (drive->media == ide_disk) {
609 ide_disk_init_chs(drive);
610 ide_disk_init_mult_count(drive);
611 }
612
613 return drive->present;
614}
615
616static void hwif_release_dev (struct device *dev)
617{
618 ide_hwif_t *hwif = container_of(dev, ide_hwif_t, gendev);
619
Aleksey Makarovf36d4022006-01-09 15:59:27 -0800620 complete(&hwif->gendev_rel_comp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621}
622
623static void hwif_register (ide_hwif_t *hwif)
624{
Randy Dunlap349ae232006-10-03 01:14:23 -0700625 int ret;
626
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 /* register with global device tree */
628 strlcpy(hwif->gendev.bus_id,hwif->name,BUS_ID_SIZE);
629 hwif->gendev.driver_data = hwif;
630 if (hwif->gendev.parent == NULL) {
631 if (hwif->pci_dev)
632 hwif->gendev.parent = &hwif->pci_dev->dev;
633 else
634 /* Would like to do = &device_legacy */
635 hwif->gendev.parent = NULL;
636 }
637 hwif->gendev.release = hwif_release_dev;
Randy Dunlap349ae232006-10-03 01:14:23 -0700638 ret = device_register(&hwif->gendev);
639 if (ret < 0)
640 printk(KERN_WARNING "IDE: %s: device_register error: %d\n",
641 __FUNCTION__, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642}
643
644static int wait_hwif_ready(ide_hwif_t *hwif)
645{
646 int rc;
647
648 printk(KERN_DEBUG "Probing IDE interface %s...\n", hwif->name);
649
650 /* Let HW settle down a bit from whatever init state we
651 * come from */
652 mdelay(2);
653
654 /* Wait for BSY bit to go away, spec timeout is 30 seconds,
655 * I know of at least one disk who takes 31 seconds, I use 35
656 * here to be safe
657 */
658 rc = ide_wait_not_busy(hwif, 35000);
659 if (rc)
660 return rc;
661
662 /* Now make sure both master & slave are ready */
663 SELECT_DRIVE(&hwif->drives[0]);
664 hwif->OUTB(8, hwif->io_ports[IDE_CONTROL_OFFSET]);
665 mdelay(2);
Marcello Maggioni284e4232005-05-26 15:47:35 +0200666 rc = ide_wait_not_busy(hwif, 35000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 if (rc)
668 return rc;
669 SELECT_DRIVE(&hwif->drives[1]);
670 hwif->OUTB(8, hwif->io_ports[IDE_CONTROL_OFFSET]);
671 mdelay(2);
Marcello Maggioni284e4232005-05-26 15:47:35 +0200672 rc = ide_wait_not_busy(hwif, 35000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673
674 /* Exit function with master reselected (let's be sane) */
675 SELECT_DRIVE(&hwif->drives[0]);
676
677 return rc;
678}
679
680/**
681 * ide_undecoded_slave - look for bad CF adapters
682 * @hwif: interface
683 *
684 * Analyse the drives on the interface and attempt to decide if we
685 * have the same drive viewed twice. This occurs with crap CF adapters
686 * and PCMCIA sometimes.
687 */
688
689void ide_undecoded_slave(ide_hwif_t *hwif)
690{
691 ide_drive_t *drive0 = &hwif->drives[0];
692 ide_drive_t *drive1 = &hwif->drives[1];
693
694 if (drive0->present == 0 || drive1->present == 0)
695 return;
696
697 /* If the models don't match they are not the same product */
698 if (strcmp(drive0->id->model, drive1->id->model))
699 return;
700
701 /* Serial numbers do not match */
702 if (strncmp(drive0->id->serial_no, drive1->id->serial_no, 20))
703 return;
704
705 /* No serial number, thankfully very rare for CF */
706 if (drive0->id->serial_no[0] == 0)
707 return;
708
709 /* Appears to be an IDE flash adapter with decode bugs */
710 printk(KERN_WARNING "ide-probe: ignoring undecoded slave\n");
711
712 drive1->present = 0;
713}
714
715EXPORT_SYMBOL_GPL(ide_undecoded_slave);
716
717/*
718 * This routine only knows how to look for drive units 0 and 1
719 * on an interface, so any setting of MAX_DRIVES > 2 won't work here.
720 */
Bartlomiej Zolnierkiewiczfd9bb532007-10-20 00:32:31 +0200721static void probe_hwif(ide_hwif_t *hwif)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 unsigned long flags;
724 unsigned int irqd;
Bartlomiej Zolnierkiewiczb140b992007-10-13 17:47:51 +0200725 int unit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726
727 if (hwif->noprobe)
728 return;
729
730 if ((hwif->chipset != ide_4drives || !hwif->mate || !hwif->mate->present) &&
731 (ide_hwif_request_regions(hwif))) {
732 u16 msgout = 0;
733 for (unit = 0; unit < MAX_DRIVES; ++unit) {
734 ide_drive_t *drive = &hwif->drives[unit];
735 if (drive->present) {
736 drive->present = 0;
737 printk(KERN_ERR "%s: ERROR, PORTS ALREADY IN USE\n",
738 drive->name);
739 msgout = 1;
740 }
741 }
742 if (!msgout)
743 printk(KERN_ERR "%s: ports already in use, skipping probe\n",
744 hwif->name);
745 return;
746 }
747
748 /*
749 * We must always disable IRQ, as probe_for_drive will assert IRQ, but
750 * we'll install our IRQ driver much later...
751 */
752 irqd = hwif->irq;
753 if (irqd)
754 disable_irq(hwif->irq);
755
756 local_irq_set(flags);
757
758 /* This is needed on some PPCs and a bunch of BIOS-less embedded
759 * platforms. Typical cases are:
760 *
761 * - The firmware hard reset the disk before booting the kernel,
762 * the drive is still doing it's poweron-reset sequence, that
763 * can take up to 30 seconds
764 * - The firmware does nothing (or no firmware), the device is
765 * still in POST state (same as above actually).
766 * - Some CD/DVD/Writer combo drives tend to drive the bus during
767 * their reset sequence even when they are non-selected slave
768 * devices, thus preventing discovery of the main HD
769 *
770 * Doing this wait-for-busy should not harm any existing configuration
771 * (at least things won't be worse than what current code does, that
772 * is blindly go & talk to the drive) and fix some issues like the
773 * above.
774 *
775 * BenH.
776 */
777 if (wait_hwif_ready(hwif) == -EBUSY)
778 printk(KERN_DEBUG "%s: Wait for ready failed before probe !\n", hwif->name);
779
780 /*
Bartlomiej Zolnierkiewiczb140b992007-10-13 17:47:51 +0200781 * Need to probe slave device first to make it release PDIAG-.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 */
Bartlomiej Zolnierkiewiczb140b992007-10-13 17:47:51 +0200783 for (unit = MAX_DRIVES - 1; unit >= 0; unit--) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 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 Zolnierkiewiczfd9bb532007-10-20 00:32:31 +0200823 if (hwif->fixup)
824 hwif->fixup(hwif);
Bartlomiej Zolnierkiewicz0380dad2007-06-08 15:14:29 +0200825
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) {
Bartlomiej Zolnierkiewicz26bcb872007-10-11 23:54:00 +0200830 if (drive->autotune == IDE_TUNE_AUTO)
831 ide_set_max_pio(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832
833 if (drive->autotune != IDE_TUNE_DEFAULT &&
834 drive->autotune != IDE_TUNE_AUTO)
835 continue;
836
837 drive->nice1 = 1;
838
Bartlomiej Zolnierkiewicz0ae2e172007-10-16 22:29:55 +0200839 if (hwif->ide_dma_on) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 /*
841 * Force DMAing for the beginning of the check.
842 * Some chipsets appear to do interesting
843 * things, if not checked and cleared.
844 * PARANOIA!!!
845 */
Bartlomiej Zolnierkiewicz7469aaf2007-02-17 02:40:26 +0100846 hwif->dma_off_quietly(drive);
Bartlomiej Zolnierkiewicz8c0697c2007-10-16 22:29:58 +0200847 ide_set_dma(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 }
849 }
850 }
Kumar Gala208a08f2006-03-24 03:18:21 -0800851
852 for (unit = 0; unit < MAX_DRIVES; ++unit) {
853 ide_drive_t *drive = &hwif->drives[unit];
854
855 if (hwif->no_io_32bit)
856 drive->no_io_32bit = 1;
857 else
858 drive->no_io_32bit = drive->id->dword_io ? 1 : 0;
859 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860}
861
862static int hwif_init(ide_hwif_t *hwif);
Bartlomiej Zolnierkiewicz9601a602007-10-20 00:32:29 +0200863static void hwif_register_devices(ide_hwif_t *hwif);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864
Bartlomiej Zolnierkiewicz8447d9d2007-10-20 00:32:31 +0200865static int probe_hwif_init(ide_hwif_t *hwif)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866{
Bartlomiej Zolnierkiewiczfd9bb532007-10-20 00:32:31 +0200867 probe_hwif(hwif);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868
869 if (!hwif_init(hwif)) {
870 printk(KERN_INFO "%s: failed to initialize IDE interface\n",
871 hwif->name);
872 return -1;
873 }
874
Bartlomiej Zolnierkiewicz9601a602007-10-20 00:32:29 +0200875 if (hwif->present)
876 hwif_register_devices(hwif);
Randy Dunlap349ae232006-10-03 01:14:23 -0700877
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 return 0;
879}
880
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881#if MAX_HWIFS > 1
882/*
883 * save_match() is used to simplify logic in init_irq() below.
884 *
885 * A loophole here is that we may not know about a particular
886 * hwif's irq until after that hwif is actually probed/initialized..
887 * This could be a problem for the case where an hwif is on a
888 * dual interface that requires serialization (eg. cmd640) and another
889 * hwif using one of the same irqs is initialized beforehand.
890 *
891 * This routine detects and reports such situations, but does not fix them.
892 */
893static void save_match(ide_hwif_t *hwif, ide_hwif_t *new, ide_hwif_t **match)
894{
895 ide_hwif_t *m = *match;
896
897 if (m && m->hwgroup && m->hwgroup != new->hwgroup) {
898 if (!new->hwgroup)
899 return;
900 printk("%s: potential irq problem with %s and %s\n",
901 hwif->name, new->name, m->name);
902 }
903 if (!m || m->irq != hwif->irq) /* don't undo a prior perfect match */
904 *match = new;
905}
906#endif /* MAX_HWIFS > 1 */
907
908/*
909 * init request queue
910 */
911static int ide_init_queue(ide_drive_t *drive)
912{
Jens Axboe165125e2007-07-24 09:28:11 +0200913 struct request_queue *q;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 ide_hwif_t *hwif = HWIF(drive);
915 int max_sectors = 256;
916 int max_sg_entries = PRD_ENTRIES;
917
918 /*
919 * Our default set up assumes the normal IDE case,
920 * that is 64K segmenting, standard PRD setup
921 * and LBA28. Some drivers then impose their own
922 * limits and LBA48 we could raise it but as yet
923 * do not.
924 */
Christoph Lameter19460892005-06-23 00:08:19 -0700925
Ravikiran G Thirumalai556e58f2005-08-04 12:53:26 -0700926 q = blk_init_queue_node(do_ide_request, &ide_lock, hwif_to_node(hwif));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 if (!q)
928 return 1;
929
930 q->queuedata = drive;
931 blk_queue_segment_boundary(q, 0xffff);
932
933 if (!hwif->rqsize) {
Bartlomiej Zolnierkiewicz238e4f12007-10-19 00:30:07 +0200934 if ((hwif->host_flags & IDE_HFLAG_NO_LBA48) ||
935 (hwif->host_flags & IDE_HFLAG_NO_LBA48_DMA))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 hwif->rqsize = 256;
937 else
938 hwif->rqsize = 65536;
939 }
940 if (hwif->rqsize < max_sectors)
941 max_sectors = hwif->rqsize;
942 blk_queue_max_sectors(q, max_sectors);
943
944#ifdef CONFIG_PCI
945 /* When we have an IOMMU, we may have a problem where pci_map_sg()
946 * creates segments that don't completely match our boundary
947 * requirements and thus need to be broken up again. Because it
948 * doesn't align properly either, we may actually have to break up
949 * to more segments than what was we got in the first place, a max
950 * worst case is twice as many.
951 * This will be fixed once we teach pci_map_sg() about our boundary
952 * requirements, hopefully soon. *FIXME*
953 */
954 if (!PCI_DMA_BUS_IS_PHYS)
955 max_sg_entries >>= 1;
956#endif /* CONFIG_PCI */
957
958 blk_queue_max_hw_segments(q, max_sg_entries);
959 blk_queue_max_phys_segments(q, max_sg_entries);
960
961 /* assign drive queue */
962 drive->queue = q;
963
964 /* needs drive->queue to be set */
965 ide_toggle_bounce(drive, 1);
966
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 return 0;
968}
969
970/*
971 * This routine sets up the irq for an ide interface, and creates a new
972 * hwgroup for the irq/hwif if none was previously assigned.
973 *
974 * Much of the code is for correctly detecting/handling irq sharing
975 * and irq serialization situations. This is somewhat complex because
976 * it handles static as well as dynamic (PCMCIA) IDE interfaces.
977 *
Thomas Gleixner362537b2006-07-01 19:29:34 -0700978 * The IRQF_DISABLED in sa_flags means ide_intr() is always entered with
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 * interrupts completely disabled. This can be bad for interrupt latency,
980 * but anything else has led to problems on some machines. We re-enable
981 * interrupts as much as we can safely do in most places.
982 */
983static int init_irq (ide_hwif_t *hwif)
984{
985 unsigned int index;
986 ide_hwgroup_t *hwgroup;
987 ide_hwif_t *match = NULL;
988
989
990 BUG_ON(in_interrupt());
991 BUG_ON(irqs_disabled());
Ravikiran G Thirumalai556e58f2005-08-04 12:53:26 -0700992 BUG_ON(hwif == NULL);
993
Matthias Kaehlckeef298882007-07-09 23:17:55 +0200994 mutex_lock(&ide_cfg_mtx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 hwif->hwgroup = NULL;
996#if MAX_HWIFS > 1
997 /*
998 * Group up with any other hwifs that share our irq(s).
999 */
1000 for (index = 0; index < MAX_HWIFS; index++) {
1001 ide_hwif_t *h = &ide_hwifs[index];
1002 if (h->hwgroup) { /* scan only initialized hwif's */
1003 if (hwif->irq == h->irq) {
1004 hwif->sharing_irq = h->sharing_irq = 1;
1005 if (hwif->chipset != ide_pci ||
1006 h->chipset != ide_pci) {
1007 save_match(hwif, h, &match);
1008 }
1009 }
1010 if (hwif->serialized) {
1011 if (hwif->mate && hwif->mate->irq == h->irq)
1012 save_match(hwif, h, &match);
1013 }
1014 if (h->serialized) {
1015 if (h->mate && hwif->irq == h->mate->irq)
1016 save_match(hwif, h, &match);
1017 }
1018 }
1019 }
1020#endif /* MAX_HWIFS > 1 */
1021 /*
1022 * If we are still without a hwgroup, then form a new one
1023 */
1024 if (match) {
1025 hwgroup = match->hwgroup;
1026 hwif->hwgroup = hwgroup;
1027 /*
1028 * Link us into the hwgroup.
1029 * This must be done early, do ensure that unexpected_intr
1030 * can find the hwif and prevent irq storms.
1031 * No drives are attached to the new hwif, choose_drive
1032 * can't do anything stupid (yet).
1033 * Add ourself as the 2nd entry to the hwgroup->hwif
1034 * linked list, the first entry is the hwif that owns
1035 * hwgroup->handler - do not change that.
1036 */
1037 spin_lock_irq(&ide_lock);
1038 hwif->next = hwgroup->hwif->next;
1039 hwgroup->hwif->next = hwif;
1040 spin_unlock_irq(&ide_lock);
1041 } else {
Christoph Lameter94f60302007-07-17 04:03:29 -07001042 hwgroup = kmalloc_node(sizeof(ide_hwgroup_t),
1043 GFP_KERNEL | __GFP_ZERO,
Ravikiran G Thirumalai556e58f2005-08-04 12:53:26 -07001044 hwif_to_node(hwif->drives[0].hwif));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 if (!hwgroup)
1046 goto out_up;
1047
1048 hwif->hwgroup = hwgroup;
1049
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 hwgroup->hwif = hwif->next = hwif;
1051 hwgroup->rq = NULL;
1052 hwgroup->handler = NULL;
1053 hwgroup->drive = NULL;
1054 hwgroup->busy = 0;
1055 init_timer(&hwgroup->timer);
1056 hwgroup->timer.function = &ide_timer_expiry;
1057 hwgroup->timer.data = (unsigned long) hwgroup;
1058 }
1059
1060 /*
1061 * Allocate the irq, if not already obtained for another hwif
1062 */
1063 if (!match || match->irq != hwif->irq) {
Thomas Gleixner362537b2006-07-01 19:29:34 -07001064 int sa = IRQF_DISABLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065#if defined(__mc68000__) || defined(CONFIG_APUS)
Thomas Gleixner362537b2006-07-01 19:29:34 -07001066 sa = IRQF_SHARED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067#endif /* __mc68000__ || CONFIG_APUS */
1068
1069 if (IDE_CHIPSET_IS_PCI(hwif->chipset)) {
Thomas Gleixner362537b2006-07-01 19:29:34 -07001070 sa = IRQF_SHARED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071#ifndef CONFIG_IDEPCI_SHARE_IRQ
Thomas Gleixner362537b2006-07-01 19:29:34 -07001072 sa |= IRQF_DISABLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073#endif /* CONFIG_IDEPCI_SHARE_IRQ */
1074 }
1075
1076 if (hwif->io_ports[IDE_CONTROL_OFFSET])
1077 /* clear nIEN */
1078 hwif->OUTB(0x08, hwif->io_ports[IDE_CONTROL_OFFSET]);
1079
1080 if (request_irq(hwif->irq,&ide_intr,sa,hwif->name,hwgroup))
1081 goto out_unlink;
1082 }
1083
1084 /*
1085 * For any present drive:
1086 * - allocate the block device queue
1087 * - link drive into the hwgroup
1088 */
1089 for (index = 0; index < MAX_DRIVES; ++index) {
1090 ide_drive_t *drive = &hwif->drives[index];
1091 if (!drive->present)
1092 continue;
1093 if (ide_init_queue(drive)) {
1094 printk(KERN_ERR "ide: failed to init %s\n",drive->name);
1095 continue;
1096 }
1097 spin_lock_irq(&ide_lock);
1098 if (!hwgroup->drive) {
1099 /* first drive for hwgroup. */
1100 drive->next = drive;
1101 hwgroup->drive = drive;
1102 hwgroup->hwif = HWIF(hwgroup->drive);
1103 } else {
1104 drive->next = hwgroup->drive->next;
1105 hwgroup->drive->next = drive;
1106 }
1107 spin_unlock_irq(&ide_lock);
1108 }
1109
David S. Millerc6387a42006-06-20 01:21:29 -07001110#if !defined(__mc68000__) && !defined(CONFIG_APUS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 printk("%s at 0x%03lx-0x%03lx,0x%03lx on irq %d", hwif->name,
1112 hwif->io_ports[IDE_DATA_OFFSET],
1113 hwif->io_ports[IDE_DATA_OFFSET]+7,
1114 hwif->io_ports[IDE_CONTROL_OFFSET], hwif->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115#else
1116 printk("%s at 0x%08lx on irq %d", hwif->name,
1117 hwif->io_ports[IDE_DATA_OFFSET], hwif->irq);
1118#endif /* __mc68000__ && CONFIG_APUS */
1119 if (match)
1120 printk(" (%sed with %s)",
1121 hwif->sharing_irq ? "shar" : "serializ", match->name);
1122 printk("\n");
Matthias Kaehlckeef298882007-07-09 23:17:55 +02001123 mutex_unlock(&ide_cfg_mtx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 return 0;
1125out_unlink:
1126 spin_lock_irq(&ide_lock);
1127 if (hwif->next == hwif) {
1128 BUG_ON(match);
1129 BUG_ON(hwgroup->hwif != hwif);
1130 kfree(hwgroup);
1131 } else {
1132 ide_hwif_t *g;
1133 g = hwgroup->hwif;
1134 while (g->next != hwif)
1135 g = g->next;
1136 g->next = hwif->next;
1137 if (hwgroup->hwif == hwif) {
1138 /* Impossible. */
1139 printk(KERN_ERR "Duh. Uninitialized hwif listed as active hwif.\n");
1140 hwgroup->hwif = g;
1141 }
1142 BUG_ON(hwgroup->hwif == hwif);
1143 }
1144 spin_unlock_irq(&ide_lock);
1145out_up:
Matthias Kaehlckeef298882007-07-09 23:17:55 +02001146 mutex_unlock(&ide_cfg_mtx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 return 1;
1148}
1149
1150static int ata_lock(dev_t dev, void *data)
1151{
1152 /* FIXME: we want to pin hwif down */
1153 return 0;
1154}
1155
1156static struct kobject *ata_probe(dev_t dev, int *part, void *data)
1157{
1158 ide_hwif_t *hwif = data;
1159 int unit = *part >> PARTN_BITS;
1160 ide_drive_t *drive = &hwif->drives[unit];
1161 if (!drive->present)
1162 return NULL;
1163
1164 if (drive->media == ide_disk)
1165 request_module("ide-disk");
1166 if (drive->scsi)
1167 request_module("ide-scsi");
1168 if (drive->media == ide_cdrom || drive->media == ide_optical)
1169 request_module("ide-cd");
1170 if (drive->media == ide_tape)
1171 request_module("ide-tape");
1172 if (drive->media == ide_floppy)
1173 request_module("ide-floppy");
1174
1175 return NULL;
1176}
1177
1178static struct kobject *exact_match(dev_t dev, int *part, void *data)
1179{
1180 struct gendisk *p = data;
1181 *part &= (1 << PARTN_BITS) - 1;
1182 return &p->kobj;
1183}
1184
1185static int exact_lock(dev_t dev, void *data)
1186{
1187 struct gendisk *p = data;
1188
1189 if (!get_disk(p))
1190 return -1;
1191 return 0;
1192}
1193
1194void ide_register_region(struct gendisk *disk)
1195{
1196 blk_register_region(MKDEV(disk->major, disk->first_minor),
1197 disk->minors, NULL, exact_match, exact_lock, disk);
1198}
1199
1200EXPORT_SYMBOL_GPL(ide_register_region);
1201
1202void ide_unregister_region(struct gendisk *disk)
1203{
1204 blk_unregister_region(MKDEV(disk->major, disk->first_minor),
1205 disk->minors);
1206}
1207
1208EXPORT_SYMBOL_GPL(ide_unregister_region);
1209
1210void ide_init_disk(struct gendisk *disk, ide_drive_t *drive)
1211{
1212 ide_hwif_t *hwif = drive->hwif;
1213 unsigned int unit = (drive->select.all >> 4) & 1;
1214
1215 disk->major = hwif->major;
1216 disk->first_minor = unit << PARTN_BITS;
1217 sprintf(disk->disk_name, "hd%c", 'a' + hwif->index * MAX_DRIVES + unit);
1218 disk->queue = drive->queue;
1219}
1220
1221EXPORT_SYMBOL_GPL(ide_init_disk);
1222
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02001223static void ide_remove_drive_from_hwgroup(ide_drive_t *drive)
1224{
1225 ide_hwgroup_t *hwgroup = drive->hwif->hwgroup;
1226
1227 if (drive == drive->next) {
1228 /* special case: last drive from hwgroup. */
1229 BUG_ON(hwgroup->drive != drive);
1230 hwgroup->drive = NULL;
1231 } else {
1232 ide_drive_t *walk;
1233
1234 walk = hwgroup->drive;
1235 while (walk->next != drive)
1236 walk = walk->next;
1237 walk->next = drive->next;
1238 if (hwgroup->drive == drive) {
1239 hwgroup->drive = drive->next;
1240 hwgroup->hwif = hwgroup->drive->hwif;
1241 }
1242 }
1243 BUG_ON(hwgroup->drive == drive);
1244}
1245
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246static void drive_release_dev (struct device *dev)
1247{
1248 ide_drive_t *drive = container_of(dev, ide_drive_t, gendev);
1249
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02001250 spin_lock_irq(&ide_lock);
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02001251 ide_remove_drive_from_hwgroup(drive);
Jesper Juhl6044ec82005-11-07 01:01:32 -08001252 kfree(drive->id);
1253 drive->id = NULL;
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02001254 drive->present = 0;
1255 /* Messed up locking ... */
1256 spin_unlock_irq(&ide_lock);
1257 blk_cleanup_queue(drive->queue);
1258 spin_lock_irq(&ide_lock);
1259 drive->queue = NULL;
1260 spin_unlock_irq(&ide_lock);
1261
Aleksey Makarovf36d4022006-01-09 15:59:27 -08001262 complete(&drive->gendev_rel_comp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263}
1264
1265/*
1266 * init_gendisk() (as opposed to ide_geninit) is called for each major device,
1267 * after probing for drives, to allocate partition tables and other data
1268 * structures needed for the routines in genhd.c. ide_geninit() gets called
1269 * somewhat later, during the partition check.
1270 */
1271static void init_gendisk (ide_hwif_t *hwif)
1272{
1273 unsigned int unit;
1274
1275 for (unit = 0; unit < MAX_DRIVES; ++unit) {
1276 ide_drive_t * drive = &hwif->drives[unit];
1277 ide_add_generic_settings(drive);
1278 snprintf(drive->gendev.bus_id,BUS_ID_SIZE,"%u.%u",
1279 hwif->index,unit);
1280 drive->gendev.parent = &hwif->gendev;
1281 drive->gendev.bus = &ide_bus_type;
1282 drive->gendev.driver_data = drive;
1283 drive->gendev.release = drive_release_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 }
1285 blk_register_region(MKDEV(hwif->major, 0), MAX_DRIVES << PARTN_BITS,
1286 THIS_MODULE, ata_probe, ata_lock, hwif);
1287}
1288
1289static int hwif_init(ide_hwif_t *hwif)
1290{
1291 int old_irq;
1292
1293 /* Return success if no device is connected */
1294 if (!hwif->present)
1295 return 1;
1296
1297 if (!hwif->irq) {
1298 if (!(hwif->irq = ide_default_irq(hwif->io_ports[IDE_DATA_OFFSET])))
1299 {
1300 printk("%s: DISABLED, NO IRQ\n", hwif->name);
1301 return (hwif->present = 0);
1302 }
1303 }
1304#ifdef CONFIG_BLK_DEV_HD
1305 if (hwif->irq == HD_IRQ && hwif->io_ports[IDE_DATA_OFFSET] != HD_DATA) {
1306 printk("%s: CANNOT SHARE IRQ WITH OLD "
1307 "HARDDISK DRIVER (hd.c)\n", hwif->name);
1308 return (hwif->present = 0);
1309 }
1310#endif /* CONFIG_BLK_DEV_HD */
1311
1312 /* we set it back to 1 if all is ok below */
1313 hwif->present = 0;
1314
1315 if (register_blkdev(hwif->major, hwif->name))
1316 return 0;
1317
1318 if (!hwif->sg_max_nents)
1319 hwif->sg_max_nents = PRD_ENTRIES;
1320
Jens Axboe45711f12007-10-22 21:19:53 +02001321 hwif->sg_table = kmalloc(sizeof(struct scatterlist)*hwif->sg_max_nents,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 GFP_KERNEL);
1323 if (!hwif->sg_table) {
1324 printk(KERN_ERR "%s: unable to allocate SG table.\n", hwif->name);
1325 goto out;
1326 }
Jens Axboe45711f12007-10-22 21:19:53 +02001327
1328 sg_init_table(hwif->sg_table, hwif->sg_max_nents);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329
1330 if (init_irq(hwif) == 0)
1331 goto done;
1332
1333 old_irq = hwif->irq;
1334 /*
1335 * It failed to initialise. Find the default IRQ for
1336 * this port and try that.
1337 */
1338 if (!(hwif->irq = ide_default_irq(hwif->io_ports[IDE_DATA_OFFSET]))) {
1339 printk("%s: Disabled unable to get IRQ %d.\n",
1340 hwif->name, old_irq);
1341 goto out;
1342 }
1343 if (init_irq(hwif)) {
1344 printk("%s: probed IRQ %d and default IRQ %d failed.\n",
1345 hwif->name, old_irq, hwif->irq);
1346 goto out;
1347 }
1348 printk("%s: probed IRQ %d failed, using default.\n",
1349 hwif->name, hwif->irq);
1350
1351done:
1352 init_gendisk(hwif);
Hannes Reineckee3a59b42007-02-07 18:19:37 +01001353
1354 ide_acpi_init(hwif);
1355
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 hwif->present = 1; /* success */
1357 return 1;
1358
1359out:
1360 unregister_blkdev(hwif->major, hwif->name);
1361 return 0;
1362}
1363
Bartlomiej Zolnierkiewicz9601a602007-10-20 00:32:29 +02001364static void hwif_register_devices(ide_hwif_t *hwif)
1365{
1366 unsigned int i;
1367
1368 for (i = 0; i < MAX_DRIVES; i++) {
1369 ide_drive_t *drive = &hwif->drives[i];
1370
1371 if (drive->present) {
1372 int ret = device_register(&drive->gendev);
1373
1374 if (ret < 0)
1375 printk(KERN_WARNING "IDE: %s: "
1376 "device_register error: %d\n",
1377 __FUNCTION__, ret);
1378 }
1379 }
1380}
1381
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382int ideprobe_init (void)
1383{
1384 unsigned int index;
1385 int probe[MAX_HWIFS];
1386
1387 memset(probe, 0, MAX_HWIFS * sizeof(int));
1388 for (index = 0; index < MAX_HWIFS; ++index)
1389 probe[index] = !ide_hwifs[index].present;
1390
1391 for (index = 0; index < MAX_HWIFS; ++index)
1392 if (probe[index])
Bartlomiej Zolnierkiewiczfd9bb532007-10-20 00:32:31 +02001393 probe_hwif(&ide_hwifs[index]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 for (index = 0; index < MAX_HWIFS; ++index)
1395 if (probe[index])
1396 hwif_init(&ide_hwifs[index]);
1397 for (index = 0; index < MAX_HWIFS; ++index) {
1398 if (probe[index]) {
1399 ide_hwif_t *hwif = &ide_hwifs[index];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 if (!hwif->present)
1401 continue;
1402 if (hwif->chipset == ide_unknown || hwif->chipset == ide_forced)
1403 hwif->chipset = ide_generic;
Bartlomiej Zolnierkiewicz9601a602007-10-20 00:32:29 +02001404 hwif_register_devices(hwif);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 }
1406 }
Bartlomiej Zolnierkiewicz5cbf79c2007-05-10 00:01:11 +02001407 for (index = 0; index < MAX_HWIFS; ++index)
1408 if (probe[index])
1409 ide_proc_register_port(&ide_hwifs[index]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 return 0;
1411}
1412
1413EXPORT_SYMBOL_GPL(ideprobe_init);
Bartlomiej Zolnierkiewicz8447d9d2007-10-20 00:32:31 +02001414
1415int ide_device_add(u8 idx[4])
1416{
1417 int i, rc = 0;
1418
1419 for (i = 0; i < 4; i++) {
1420 if (idx[i] != 0xff)
1421 rc |= probe_hwif_init(&ide_hwifs[idx[i]]);
1422 }
1423
1424 for (i = 0; i < 4; i++) {
1425 if (idx[i] != 0xff)
1426 ide_proc_register_port(&ide_hwifs[idx[i]]);
1427 }
1428
1429 return rc;
1430}
1431
1432EXPORT_SYMBOL_GPL(ide_device_add);