blob: 9d9f1c6d602a53ffd91ed94423c232b28a7039e9 [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 *
Bartlomiej Zolnierkiewiczbbe4d6d2007-12-12 23:32:00 +010016 * -- increase WAIT_PIDENTIFY to avoid CD-ROM locking at boot
17 * by Andrea Arcangeli
Linus Torvalds1da177e2005-04-16 15:20:36 -070018 */
19
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/module.h>
21#include <linux/types.h>
22#include <linux/string.h>
23#include <linux/kernel.h>
24#include <linux/timer.h>
25#include <linux/mm.h>
26#include <linux/interrupt.h>
27#include <linux/major.h>
28#include <linux/errno.h>
29#include <linux/genhd.h>
30#include <linux/slab.h>
31#include <linux/delay.h>
32#include <linux/ide.h>
33#include <linux/spinlock.h>
34#include <linux/kmod.h>
35#include <linux/pci.h>
FUJITA Tomonoridc817852007-10-23 09:29:58 +020036#include <linux/scatterlist.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
38#include <asm/byteorder.h>
39#include <asm/irq.h>
40#include <asm/uaccess.h>
41#include <asm/io.h>
42
43/**
44 * generic_id - add a generic drive id
45 * @drive: drive to make an ID block for
46 *
47 * Add a fake id field to the drive we are passed. This allows
48 * use to skip a ton of NULL checks (which people always miss)
49 * and make drive properties unconditional outside of this file
50 */
51
52static void generic_id(ide_drive_t *drive)
53{
54 drive->id->cyls = drive->cyl;
55 drive->id->heads = drive->head;
56 drive->id->sectors = drive->sect;
57 drive->id->cur_cyls = drive->cyl;
58 drive->id->cur_heads = drive->head;
59 drive->id->cur_sectors = drive->sect;
60}
61
62static void ide_disk_init_chs(ide_drive_t *drive)
63{
64 struct hd_driveid *id = drive->id;
65
66 /* Extract geometry if we did not already have one for the drive */
67 if (!drive->cyl || !drive->head || !drive->sect) {
68 drive->cyl = drive->bios_cyl = id->cyls;
69 drive->head = drive->bios_head = id->heads;
70 drive->sect = drive->bios_sect = id->sectors;
71 }
72
73 /* Handle logical geometry translation by the drive */
74 if ((id->field_valid & 1) && id->cur_cyls &&
75 id->cur_heads && (id->cur_heads <= 16) && id->cur_sectors) {
76 drive->cyl = id->cur_cyls;
77 drive->head = id->cur_heads;
78 drive->sect = id->cur_sectors;
79 }
80
81 /* Use physical geometry if what we have still makes no sense */
82 if (drive->head > 16 && id->heads && id->heads <= 16) {
83 drive->cyl = id->cyls;
84 drive->head = id->heads;
85 drive->sect = id->sectors;
86 }
87}
88
89static void ide_disk_init_mult_count(ide_drive_t *drive)
90{
91 struct hd_driveid *id = drive->id;
92
93 drive->mult_count = 0;
94 if (id->max_multsect) {
95#ifdef CONFIG_IDEDISK_MULTI_MODE
96 id->multsect = ((id->max_multsect/2) > 1) ? id->max_multsect : 0;
97 id->multsect_valid = id->multsect ? 1 : 0;
Bartlomiej Zolnierkiewicz4ee06b72008-01-25 22:17:08 +010098 drive->mult_req = id->multsect_valid ? id->max_multsect : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 drive->special.b.set_multmode = drive->mult_req ? 1 : 0;
100#else /* original, pre IDE-NFG, per request of AC */
Bartlomiej Zolnierkiewicz4ee06b72008-01-25 22:17:08 +0100101 drive->mult_req = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 if (drive->mult_req > id->max_multsect)
103 drive->mult_req = id->max_multsect;
104 if (drive->mult_req || ((id->multsect_valid & 1) && id->multsect))
105 drive->special.b.set_multmode = 1;
106#endif
107 }
108}
109
110/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 * do_identify - identify a drive
112 * @drive: drive to identify
113 * @cmd: command used
114 *
115 * Called when we have issued a drive identify command to
116 * read and parse the results. This function is run with
117 * interrupts disabled.
118 */
119
120static inline void do_identify (ide_drive_t *drive, u8 cmd)
121{
122 ide_hwif_t *hwif = HWIF(drive);
123 int bswap = 1;
124 struct hd_driveid *id;
125
126 id = drive->id;
127 /* read 512 bytes of id info */
128 hwif->ata_input_data(drive, id, SECTOR_WORDS);
129
130 drive->id_read = 1;
131 local_irq_enable();
132 ide_fix_driveid(id);
133
Robert P. J. Daye71bc142007-07-09 23:17:57 +0200134#if defined (CONFIG_SCSI_EATA_PIO) || defined (CONFIG_SCSI_EATA)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 /*
136 * EATA SCSI controllers do a hardware ATA emulation:
137 * Ignore them if there is a driver for them available.
138 */
139 if ((id->model[0] == 'P' && id->model[1] == 'M') ||
140 (id->model[0] == 'S' && id->model[1] == 'K')) {
141 printk("%s: EATA SCSI HBA %.10s\n", drive->name, id->model);
142 goto err_misc;
143 }
Robert P. J. Daye71bc142007-07-09 23:17:57 +0200144#endif /* CONFIG_SCSI_EATA || CONFIG_SCSI_EATA_PIO */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
146 /*
147 * WIN_IDENTIFY returns little-endian info,
148 * WIN_PIDENTIFY *usually* returns little-endian info.
149 */
150 if (cmd == WIN_PIDENTIFY) {
151 if ((id->model[0] == 'N' && id->model[1] == 'E') /* NEC */
152 || (id->model[0] == 'F' && id->model[1] == 'X') /* Mitsumi */
153 || (id->model[0] == 'P' && id->model[1] == 'i'))/* Pioneer */
154 /* Vertos drives may still be weird */
155 bswap ^= 1;
156 }
157 ide_fixstring(id->model, sizeof(id->model), bswap);
158 ide_fixstring(id->fw_rev, sizeof(id->fw_rev), bswap);
159 ide_fixstring(id->serial_no, sizeof(id->serial_no), bswap);
160
Tejun Heo699b0522007-11-05 21:42:25 +0100161 /* we depend on this a lot! */
162 id->model[sizeof(id->model)-1] = '\0';
163
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 if (strstr(id->model, "E X A B Y T E N E S T"))
165 goto err_misc;
166
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 printk("%s: %s, ", drive->name, id->model);
168 drive->present = 1;
169 drive->dead = 0;
170
171 /*
172 * Check for an ATAPI device
173 */
174 if (cmd == WIN_PIDENTIFY) {
175 u8 type = (id->config >> 8) & 0x1f;
176 printk("ATAPI ");
177 switch (type) {
178 case ide_floppy:
179 if (!strstr(id->model, "CD-ROM")) {
180 if (!strstr(id->model, "oppy") &&
181 !strstr(id->model, "poyp") &&
182 !strstr(id->model, "ZIP"))
183 printk("cdrom or floppy?, assuming ");
184 if (drive->media != ide_cdrom) {
185 printk ("FLOPPY");
186 drive->removable = 1;
187 break;
188 }
189 }
190 /* Early cdrom models used zero */
191 type = ide_cdrom;
192 case ide_cdrom:
193 drive->removable = 1;
194#ifdef CONFIG_PPC
195 /* kludge for Apple PowerBook internal zip */
196 if (!strstr(id->model, "CD-ROM") &&
197 strstr(id->model, "ZIP")) {
198 printk ("FLOPPY");
199 type = ide_floppy;
200 break;
201 }
202#endif
203 printk ("CD/DVD-ROM");
204 break;
205 case ide_tape:
206 printk ("TAPE");
207 break;
208 case ide_optical:
209 printk ("OPTICAL");
210 drive->removable = 1;
211 break;
212 default:
213 printk("UNKNOWN (type %d)", type);
214 break;
215 }
216 printk (" drive\n");
217 drive->media = type;
218 /* an ATAPI device ignores DRDY */
219 drive->ready_stat = 0;
220 return;
221 }
222
223 /*
224 * Not an ATAPI device: looks like a "regular" hard disk
225 */
Richard Purdie98109332006-02-03 03:04:55 -0800226
227 /*
228 * 0x848a = CompactFlash device
229 * These are *not* removable in Linux definition of the term
230 */
231
232 if ((id->config != 0x848a) && (id->config & (1<<7)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 drive->removable = 1;
234
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 drive->media = ide_disk;
Richard Purdie98109332006-02-03 03:04:55 -0800236 printk("%s DISK drive\n", (id->config == 0x848a) ? "CFA" : "ATA" );
Bartlomiej Zolnierkiewiczcd3dbc92008-01-25 22:17:13 +0100237
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 return;
239
240err_misc:
241 kfree(id);
242 drive->present = 0;
243 return;
244}
245
246/**
247 * actual_try_to_identify - send ata/atapi identify
248 * @drive: drive to identify
249 * @cmd: command to use
250 *
251 * try_to_identify() sends an ATA(PI) IDENTIFY request to a drive
252 * and waits for a response. It also monitors irqs while this is
253 * happening, in hope of automatically determining which one is
254 * being used by the interface.
255 *
256 * Returns: 0 device was identified
257 * 1 device timed-out (no response to identify request)
258 * 2 device aborted the command (refused to identify itself)
259 */
260
261static int actual_try_to_identify (ide_drive_t *drive, u8 cmd)
262{
263 ide_hwif_t *hwif = HWIF(drive);
264 int rc;
265 unsigned long hd_status;
266 unsigned long timeout;
267 u8 s = 0, a = 0;
268
269 /* take a deep breath */
270 msleep(50);
271
272 if (IDE_CONTROL_REG) {
273 a = hwif->INB(IDE_ALTSTATUS_REG);
274 s = hwif->INB(IDE_STATUS_REG);
275 if ((a ^ s) & ~INDEX_STAT) {
276 printk(KERN_INFO "%s: probing with STATUS(0x%02x) instead of "
277 "ALTSTATUS(0x%02x)\n", drive->name, s, a);
278 /* ancient Seagate drives, broken interfaces */
279 hd_status = IDE_STATUS_REG;
280 } else {
281 /* use non-intrusive polling */
282 hd_status = IDE_ALTSTATUS_REG;
283 }
284 } else
285 hd_status = IDE_STATUS_REG;
286
287 /* set features register for atapi
288 * identify command to be sure of reply
289 */
290 if ((cmd == WIN_PIDENTIFY))
291 /* disable dma & overlap */
292 hwif->OUTB(0, IDE_FEATURE_REG);
293
294 /* ask drive for ID */
295 hwif->OUTB(cmd, IDE_COMMAND_REG);
296
297 timeout = ((cmd == WIN_IDENTIFY) ? WAIT_WORSTCASE : WAIT_PIDENTIFY) / 2;
298 timeout += jiffies;
299 do {
300 if (time_after(jiffies, timeout)) {
301 /* drive timed-out */
302 return 1;
303 }
304 /* give drive a breather */
305 msleep(50);
306 } while ((hwif->INB(hd_status)) & BUSY_STAT);
307
308 /* wait for IRQ and DRQ_STAT */
309 msleep(50);
310 if (OK_STAT((hwif->INB(IDE_STATUS_REG)), DRQ_STAT, BAD_R_STAT)) {
311 unsigned long flags;
312
313 /* local CPU only; some systems need this */
314 local_irq_save(flags);
315 /* drive returned ID */
316 do_identify(drive, cmd);
317 /* drive responded with ID */
318 rc = 0;
319 /* clear drive IRQ */
320 (void) hwif->INB(IDE_STATUS_REG);
321 local_irq_restore(flags);
322 } else {
323 /* drive refused ID */
324 rc = 2;
325 }
326 return rc;
327}
328
329/**
330 * try_to_identify - try to identify a drive
331 * @drive: drive to probe
332 * @cmd: command to use
333 *
334 * Issue the identify command and then do IRQ probing to
335 * complete the identification when needed by finding the
336 * IRQ the drive is attached to
337 */
338
339static int try_to_identify (ide_drive_t *drive, u8 cmd)
340{
341 ide_hwif_t *hwif = HWIF(drive);
342 int retval;
343 int autoprobe = 0;
344 unsigned long cookie = 0;
345
346 /*
347 * Disable device irq unless we need to
348 * probe for it. Otherwise we'll get spurious
349 * interrupts during the identify-phase that
350 * the irq handler isn't expecting.
351 */
352 if (IDE_CONTROL_REG) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 if (!hwif->irq) {
354 autoprobe = 1;
355 cookie = probe_irq_on();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 }
Bartlomiej Zolnierkiewicz81ca6912008-01-26 20:13:08 +0100357 ide_set_irq(drive, autoprobe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 }
359
360 retval = actual_try_to_identify(drive, cmd);
361
362 if (autoprobe) {
363 int irq;
Bartlomiej Zolnierkiewicz81ca6912008-01-26 20:13:08 +0100364
365 ide_set_irq(drive, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 /* clear drive IRQ */
367 (void) hwif->INB(IDE_STATUS_REG);
368 udelay(5);
369 irq = probe_irq_off(cookie);
370 if (!hwif->irq) {
371 if (irq > 0) {
372 hwif->irq = irq;
373 } else {
374 /* Mmmm.. multiple IRQs..
375 * don't know which was ours
376 */
377 printk("%s: IRQ probe failed (0x%lx)\n",
378 drive->name, cookie);
379 }
380 }
381 }
382 return retval;
383}
384
385
386/**
387 * do_probe - probe an IDE device
388 * @drive: drive to probe
389 * @cmd: command to use
390 *
391 * do_probe() has the difficult job of finding a drive if it exists,
392 * without getting hung up if it doesn't exist, without trampling on
393 * ethernet cards, and without leaving any IRQs dangling to haunt us later.
394 *
395 * If a drive is "known" to exist (from CMOS or kernel parameters),
396 * but does not respond right away, the probe will "hang in there"
397 * for the maximum wait time (about 30 seconds), otherwise it will
398 * exit much more quickly.
399 *
400 * Returns: 0 device was identified
401 * 1 device timed-out (no response to identify request)
402 * 2 device aborted the command (refused to identify itself)
403 * 3 bad status from device (possible for ATAPI drives)
404 * 4 probe was not attempted because failure was obvious
405 */
406
407static int do_probe (ide_drive_t *drive, u8 cmd)
408{
409 int rc;
410 ide_hwif_t *hwif = HWIF(drive);
411
412 if (drive->present) {
413 /* avoid waiting for inappropriate probes */
414 if ((drive->media != ide_disk) && (cmd == WIN_IDENTIFY))
415 return 4;
416 }
417#ifdef DEBUG
418 printk("probing for %s: present=%d, media=%d, probetype=%s\n",
419 drive->name, drive->present, drive->media,
420 (cmd == WIN_IDENTIFY) ? "ATA" : "ATAPI");
421#endif
422
423 /* needed for some systems
424 * (e.g. crw9624 as drive0 with disk as slave)
425 */
426 msleep(50);
427 SELECT_DRIVE(drive);
428 msleep(50);
429 if (hwif->INB(IDE_SELECT_REG) != drive->select.all && !drive->present) {
430 if (drive->select.b.unit != 0) {
431 /* exit with drive0 selected */
432 SELECT_DRIVE(&hwif->drives[0]);
433 /* allow BUSY_STAT to assert & clear */
434 msleep(50);
435 }
436 /* no i/f present: mmm.. this should be a 4 -ml */
437 return 3;
438 }
439
440 if (OK_STAT((hwif->INB(IDE_STATUS_REG)), READY_STAT, BUSY_STAT) ||
441 drive->present || cmd == WIN_PIDENTIFY) {
442 /* send cmd and wait */
443 if ((rc = try_to_identify(drive, cmd))) {
444 /* failed: try again */
445 rc = try_to_identify(drive,cmd);
446 }
447 if (hwif->INB(IDE_STATUS_REG) == (BUSY_STAT|READY_STAT))
448 return 4;
449
450 if ((rc == 1 && cmd == WIN_PIDENTIFY) &&
451 ((drive->autotune == IDE_TUNE_DEFAULT) ||
452 (drive->autotune == IDE_TUNE_AUTO))) {
453 unsigned long timeout;
454 printk("%s: no response (status = 0x%02x), "
455 "resetting drive\n", drive->name,
456 hwif->INB(IDE_STATUS_REG));
457 msleep(50);
458 hwif->OUTB(drive->select.all, IDE_SELECT_REG);
459 msleep(50);
460 hwif->OUTB(WIN_SRST, IDE_COMMAND_REG);
461 timeout = jiffies;
462 while (((hwif->INB(IDE_STATUS_REG)) & BUSY_STAT) &&
463 time_before(jiffies, timeout + WAIT_WORSTCASE))
464 msleep(50);
465 rc = try_to_identify(drive, cmd);
466 }
467 if (rc == 1)
468 printk("%s: no response (status = 0x%02x)\n",
469 drive->name, hwif->INB(IDE_STATUS_REG));
470 /* ensure drive irq is clear */
471 (void) hwif->INB(IDE_STATUS_REG);
472 } else {
473 /* not present or maybe ATAPI */
474 rc = 3;
475 }
476 if (drive->select.b.unit != 0) {
477 /* exit with drive0 selected */
478 SELECT_DRIVE(&hwif->drives[0]);
479 msleep(50);
480 /* ensure drive irq is clear */
481 (void) hwif->INB(IDE_STATUS_REG);
482 }
483 return rc;
484}
485
486/*
487 *
488 */
489static void enable_nest (ide_drive_t *drive)
490{
491 ide_hwif_t *hwif = HWIF(drive);
492 unsigned long timeout;
493
494 printk("%s: enabling %s -- ", hwif->name, drive->id->model);
495 SELECT_DRIVE(drive);
496 msleep(50);
497 hwif->OUTB(EXABYTE_ENABLE_NEST, IDE_COMMAND_REG);
498 timeout = jiffies + WAIT_WORSTCASE;
499 do {
500 if (time_after(jiffies, timeout)) {
501 printk("failed (timeout)\n");
502 return;
503 }
504 msleep(50);
505 } while ((hwif->INB(IDE_STATUS_REG)) & BUSY_STAT);
506
507 msleep(50);
508
509 if (!OK_STAT((hwif->INB(IDE_STATUS_REG)), 0, BAD_STAT)) {
510 printk("failed (status = 0x%02x)\n", hwif->INB(IDE_STATUS_REG));
511 } else {
512 printk("success\n");
513 }
514
515 /* if !(success||timed-out) */
516 if (do_probe(drive, WIN_IDENTIFY) >= 2) {
517 /* look for ATAPI device */
518 (void) do_probe(drive, WIN_PIDENTIFY);
519 }
520}
521
522/**
523 * probe_for_drives - upper level drive probe
524 * @drive: drive to probe for
525 *
526 * probe_for_drive() tests for existence of a given drive using do_probe()
527 * and presents things to the user as needed.
528 *
529 * Returns: 0 no device was found
530 * 1 device was found (note: drive->present might
531 * still be 0)
532 */
533
534static inline u8 probe_for_drive (ide_drive_t *drive)
535{
536 /*
537 * In order to keep things simple we have an id
538 * block for all drives at all times. If the device
539 * is pre ATA or refuses ATA/ATAPI identify we
540 * will add faked data to this.
541 *
542 * Also note that 0 everywhere means "can't do X"
543 */
544
Deepak Saxenaf5e3c2f2005-11-07 01:01:25 -0800545 drive->id = kzalloc(SECTOR_WORDS *4, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 drive->id_read = 0;
547 if(drive->id == NULL)
548 {
549 printk(KERN_ERR "ide: out of memory for id data.\n");
550 return 0;
551 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 strcpy(drive->id->model, "UNKNOWN");
553
554 /* skip probing? */
555 if (!drive->noprobe)
556 {
557 /* if !(success||timed-out) */
558 if (do_probe(drive, WIN_IDENTIFY) >= 2) {
559 /* look for ATAPI device */
560 (void) do_probe(drive, WIN_PIDENTIFY);
561 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 if (!drive->present)
563 /* drive not found */
564 return 0;
Alan Cox78595572007-07-03 22:28:35 +0200565 if (strstr(drive->id->model, "E X A B Y T E N E S T"))
566 enable_nest(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567
568 /* identification failed? */
569 if (!drive->id_read) {
570 if (drive->media == ide_disk) {
571 printk(KERN_INFO "%s: non-IDE drive, CHS=%d/%d/%d\n",
572 drive->name, drive->cyl,
573 drive->head, drive->sect);
574 } else if (drive->media == ide_cdrom) {
575 printk(KERN_INFO "%s: ATAPI cdrom (?)\n", drive->name);
576 } else {
577 /* nuke it */
578 printk(KERN_WARNING "%s: Unknown device on bus refused identification. Ignoring.\n", drive->name);
579 drive->present = 0;
580 }
581 }
582 /* drive was found */
583 }
584 if(!drive->present)
585 return 0;
586 /* The drive wasn't being helpful. Add generic info only */
587 if (drive->id_read == 0) {
588 generic_id(drive);
589 return 1;
590 }
591
592 if (drive->media == ide_disk) {
593 ide_disk_init_chs(drive);
594 ide_disk_init_mult_count(drive);
595 }
596
597 return drive->present;
598}
599
600static void hwif_release_dev (struct device *dev)
601{
602 ide_hwif_t *hwif = container_of(dev, ide_hwif_t, gendev);
603
Aleksey Makarovf36d4022006-01-09 15:59:27 -0800604 complete(&hwif->gendev_rel_comp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605}
606
607static void hwif_register (ide_hwif_t *hwif)
608{
Randy Dunlap349ae232006-10-03 01:14:23 -0700609 int ret;
610
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 /* register with global device tree */
612 strlcpy(hwif->gendev.bus_id,hwif->name,BUS_ID_SIZE);
613 hwif->gendev.driver_data = hwif;
614 if (hwif->gendev.parent == NULL) {
615 if (hwif->pci_dev)
616 hwif->gendev.parent = &hwif->pci_dev->dev;
617 else
618 /* Would like to do = &device_legacy */
619 hwif->gendev.parent = NULL;
620 }
621 hwif->gendev.release = hwif_release_dev;
Randy Dunlap349ae232006-10-03 01:14:23 -0700622 ret = device_register(&hwif->gendev);
623 if (ret < 0)
624 printk(KERN_WARNING "IDE: %s: device_register error: %d\n",
625 __FUNCTION__, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626}
627
628static int wait_hwif_ready(ide_hwif_t *hwif)
629{
Jonas Stare82661052007-11-27 21:35:53 +0100630 int unit, rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631
632 printk(KERN_DEBUG "Probing IDE interface %s...\n", hwif->name);
633
634 /* Let HW settle down a bit from whatever init state we
635 * come from */
636 mdelay(2);
637
638 /* Wait for BSY bit to go away, spec timeout is 30 seconds,
639 * I know of at least one disk who takes 31 seconds, I use 35
640 * here to be safe
641 */
642 rc = ide_wait_not_busy(hwif, 35000);
643 if (rc)
644 return rc;
645
646 /* Now make sure both master & slave are ready */
Jonas Stare82661052007-11-27 21:35:53 +0100647 for (unit = 0; unit < MAX_DRIVES; unit++) {
648 ide_drive_t *drive = &hwif->drives[unit];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649
Jonas Stare82661052007-11-27 21:35:53 +0100650 /* Ignore disks that we will not probe for later. */
651 if (!drive->noprobe || drive->present) {
652 SELECT_DRIVE(drive);
Bartlomiej Zolnierkiewicz81ca6912008-01-26 20:13:08 +0100653 ide_set_irq(drive, 1);
Jonas Stare82661052007-11-27 21:35:53 +0100654 mdelay(2);
655 rc = ide_wait_not_busy(hwif, 35000);
656 if (rc)
657 goto out;
658 } else
659 printk(KERN_DEBUG "%s: ide_wait_not_busy() skipped\n",
660 drive->name);
661 }
662out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 /* Exit function with master reselected (let's be sane) */
Jonas Stare82661052007-11-27 21:35:53 +0100664 if (unit)
665 SELECT_DRIVE(&hwif->drives[0]);
666
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 return rc;
668}
669
670/**
671 * ide_undecoded_slave - look for bad CF adapters
Bartlomiej Zolnierkiewiczf01393e2008-01-26 20:13:03 +0100672 * @drive1: drive
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 *
674 * Analyse the drives on the interface and attempt to decide if we
675 * have the same drive viewed twice. This occurs with crap CF adapters
676 * and PCMCIA sometimes.
677 */
678
Bartlomiej Zolnierkiewiczf01393e2008-01-26 20:13:03 +0100679void ide_undecoded_slave(ide_drive_t *drive1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680{
Bartlomiej Zolnierkiewiczf01393e2008-01-26 20:13:03 +0100681 ide_drive_t *drive0 = &drive1->hwif->drives[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682
Bartlomiej Zolnierkiewiczf01393e2008-01-26 20:13:03 +0100683 if ((drive1->dn & 1) == 0 || drive0->present == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 return;
685
686 /* If the models don't match they are not the same product */
687 if (strcmp(drive0->id->model, drive1->id->model))
688 return;
689
690 /* Serial numbers do not match */
691 if (strncmp(drive0->id->serial_no, drive1->id->serial_no, 20))
692 return;
693
694 /* No serial number, thankfully very rare for CF */
695 if (drive0->id->serial_no[0] == 0)
696 return;
697
698 /* Appears to be an IDE flash adapter with decode bugs */
699 printk(KERN_WARNING "ide-probe: ignoring undecoded slave\n");
700
701 drive1->present = 0;
702}
703
704EXPORT_SYMBOL_GPL(ide_undecoded_slave);
705
706/*
707 * This routine only knows how to look for drive units 0 and 1
708 * on an interface, so any setting of MAX_DRIVES > 2 won't work here.
709 */
Bartlomiej Zolnierkiewiczfd9bb532007-10-20 00:32:31 +0200710static void probe_hwif(ide_hwif_t *hwif)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 unsigned long flags;
713 unsigned int irqd;
Bartlomiej Zolnierkiewiczb140b992007-10-13 17:47:51 +0200714 int unit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715
716 if (hwif->noprobe)
717 return;
718
719 if ((hwif->chipset != ide_4drives || !hwif->mate || !hwif->mate->present) &&
720 (ide_hwif_request_regions(hwif))) {
721 u16 msgout = 0;
722 for (unit = 0; unit < MAX_DRIVES; ++unit) {
723 ide_drive_t *drive = &hwif->drives[unit];
724 if (drive->present) {
725 drive->present = 0;
726 printk(KERN_ERR "%s: ERROR, PORTS ALREADY IN USE\n",
727 drive->name);
728 msgout = 1;
729 }
730 }
731 if (!msgout)
732 printk(KERN_ERR "%s: ports already in use, skipping probe\n",
733 hwif->name);
734 return;
735 }
736
737 /*
738 * We must always disable IRQ, as probe_for_drive will assert IRQ, but
739 * we'll install our IRQ driver much later...
740 */
741 irqd = hwif->irq;
742 if (irqd)
743 disable_irq(hwif->irq);
744
745 local_irq_set(flags);
746
747 /* This is needed on some PPCs and a bunch of BIOS-less embedded
748 * platforms. Typical cases are:
749 *
750 * - The firmware hard reset the disk before booting the kernel,
751 * the drive is still doing it's poweron-reset sequence, that
752 * can take up to 30 seconds
753 * - The firmware does nothing (or no firmware), the device is
754 * still in POST state (same as above actually).
755 * - Some CD/DVD/Writer combo drives tend to drive the bus during
756 * their reset sequence even when they are non-selected slave
757 * devices, thus preventing discovery of the main HD
758 *
759 * Doing this wait-for-busy should not harm any existing configuration
760 * (at least things won't be worse than what current code does, that
761 * is blindly go & talk to the drive) and fix some issues like the
762 * above.
763 *
764 * BenH.
765 */
766 if (wait_hwif_ready(hwif) == -EBUSY)
767 printk(KERN_DEBUG "%s: Wait for ready failed before probe !\n", hwif->name);
768
769 /*
Bartlomiej Zolnierkiewiczb140b992007-10-13 17:47:51 +0200770 * Need to probe slave device first to make it release PDIAG-.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 */
Bartlomiej Zolnierkiewiczb140b992007-10-13 17:47:51 +0200772 for (unit = MAX_DRIVES - 1; unit >= 0; unit--) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 ide_drive_t *drive = &hwif->drives[unit];
774 drive->dn = (hwif->channel ? 2 : 0) + unit;
775 (void) probe_for_drive(drive);
776 if (drive->present && !hwif->present) {
777 hwif->present = 1;
778 if (hwif->chipset != ide_4drives ||
779 !hwif->mate ||
780 !hwif->mate->present) {
781 hwif_register(hwif);
782 }
783 }
784 }
785 if (hwif->io_ports[IDE_CONTROL_OFFSET] && hwif->reset) {
786 unsigned long timeout = jiffies + WAIT_WORSTCASE;
787 u8 stat;
788
789 printk(KERN_WARNING "%s: reset\n", hwif->name);
790 hwif->OUTB(12, hwif->io_ports[IDE_CONTROL_OFFSET]);
791 udelay(10);
792 hwif->OUTB(8, hwif->io_ports[IDE_CONTROL_OFFSET]);
793 do {
794 msleep(50);
795 stat = hwif->INB(hwif->io_ports[IDE_STATUS_OFFSET]);
796 } while ((stat & BUSY_STAT) && time_after(timeout, jiffies));
797
798 }
799 local_irq_restore(flags);
800 /*
801 * Use cached IRQ number. It might be (and is...) changed by probe
802 * code above
803 */
804 if (irqd)
805 enable_irq(irqd);
806
807 if (!hwif->present) {
808 ide_hwif_release_regions(hwif);
809 return;
810 }
811
Bartlomiej Zolnierkiewiczf01393e2008-01-26 20:13:03 +0100812 for (unit = 0; unit < MAX_DRIVES; unit++) {
813 ide_drive_t *drive = &hwif->drives[unit];
814
815 if (drive->present && hwif->quirkproc)
816 hwif->quirkproc(drive);
817 }
Bartlomiej Zolnierkiewicz0380dad2007-06-08 15:14:29 +0200818
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 for (unit = 0; unit < MAX_DRIVES; ++unit) {
820 ide_drive_t *drive = &hwif->drives[unit];
821
822 if (drive->present) {
Bartlomiej Zolnierkiewicz26bcb872007-10-11 23:54:00 +0200823 if (drive->autotune == IDE_TUNE_AUTO)
824 ide_set_max_pio(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825
826 if (drive->autotune != IDE_TUNE_DEFAULT &&
827 drive->autotune != IDE_TUNE_AUTO)
828 continue;
829
830 drive->nice1 = 1;
831
Bartlomiej Zolnierkiewicz15ce9262008-01-26 20:13:03 +0100832 if (hwif->dma_host_set)
Bartlomiej Zolnierkiewicz8c0697c2007-10-16 22:29:58 +0200833 ide_set_dma(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 }
835 }
Kumar Gala208a08f2006-03-24 03:18:21 -0800836
837 for (unit = 0; unit < MAX_DRIVES; ++unit) {
838 ide_drive_t *drive = &hwif->drives[unit];
839
840 if (hwif->no_io_32bit)
841 drive->no_io_32bit = 1;
842 else
843 drive->no_io_32bit = drive->id->dword_io ? 1 : 0;
844 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845}
846
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847#if MAX_HWIFS > 1
848/*
849 * save_match() is used to simplify logic in init_irq() below.
850 *
851 * A loophole here is that we may not know about a particular
852 * hwif's irq until after that hwif is actually probed/initialized..
853 * This could be a problem for the case where an hwif is on a
854 * dual interface that requires serialization (eg. cmd640) and another
855 * hwif using one of the same irqs is initialized beforehand.
856 *
857 * This routine detects and reports such situations, but does not fix them.
858 */
859static void save_match(ide_hwif_t *hwif, ide_hwif_t *new, ide_hwif_t **match)
860{
861 ide_hwif_t *m = *match;
862
863 if (m && m->hwgroup && m->hwgroup != new->hwgroup) {
864 if (!new->hwgroup)
865 return;
866 printk("%s: potential irq problem with %s and %s\n",
867 hwif->name, new->name, m->name);
868 }
869 if (!m || m->irq != hwif->irq) /* don't undo a prior perfect match */
870 *match = new;
871}
872#endif /* MAX_HWIFS > 1 */
873
874/*
875 * init request queue
876 */
877static int ide_init_queue(ide_drive_t *drive)
878{
Jens Axboe165125e2007-07-24 09:28:11 +0200879 struct request_queue *q;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 ide_hwif_t *hwif = HWIF(drive);
881 int max_sectors = 256;
882 int max_sg_entries = PRD_ENTRIES;
883
884 /*
885 * Our default set up assumes the normal IDE case,
886 * that is 64K segmenting, standard PRD setup
887 * and LBA28. Some drivers then impose their own
888 * limits and LBA48 we could raise it but as yet
889 * do not.
890 */
Christoph Lameter19460892005-06-23 00:08:19 -0700891
Ravikiran G Thirumalai556e58f2005-08-04 12:53:26 -0700892 q = blk_init_queue_node(do_ide_request, &ide_lock, hwif_to_node(hwif));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 if (!q)
894 return 1;
895
896 q->queuedata = drive;
897 blk_queue_segment_boundary(q, 0xffff);
898
899 if (!hwif->rqsize) {
Bartlomiej Zolnierkiewicz238e4f12007-10-19 00:30:07 +0200900 if ((hwif->host_flags & IDE_HFLAG_NO_LBA48) ||
901 (hwif->host_flags & IDE_HFLAG_NO_LBA48_DMA))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 hwif->rqsize = 256;
903 else
904 hwif->rqsize = 65536;
905 }
906 if (hwif->rqsize < max_sectors)
907 max_sectors = hwif->rqsize;
908 blk_queue_max_sectors(q, max_sectors);
909
910#ifdef CONFIG_PCI
911 /* When we have an IOMMU, we may have a problem where pci_map_sg()
912 * creates segments that don't completely match our boundary
913 * requirements and thus need to be broken up again. Because it
914 * doesn't align properly either, we may actually have to break up
915 * to more segments than what was we got in the first place, a max
916 * worst case is twice as many.
917 * This will be fixed once we teach pci_map_sg() about our boundary
918 * requirements, hopefully soon. *FIXME*
919 */
920 if (!PCI_DMA_BUS_IS_PHYS)
921 max_sg_entries >>= 1;
922#endif /* CONFIG_PCI */
923
924 blk_queue_max_hw_segments(q, max_sg_entries);
925 blk_queue_max_phys_segments(q, max_sg_entries);
926
927 /* assign drive queue */
928 drive->queue = q;
929
930 /* needs drive->queue to be set */
931 ide_toggle_bounce(drive, 1);
932
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 return 0;
934}
935
936/*
937 * This routine sets up the irq for an ide interface, and creates a new
938 * hwgroup for the irq/hwif if none was previously assigned.
939 *
940 * Much of the code is for correctly detecting/handling irq sharing
941 * and irq serialization situations. This is somewhat complex because
942 * it handles static as well as dynamic (PCMCIA) IDE interfaces.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 */
944static int init_irq (ide_hwif_t *hwif)
945{
946 unsigned int index;
947 ide_hwgroup_t *hwgroup;
948 ide_hwif_t *match = NULL;
949
950
951 BUG_ON(in_interrupt());
952 BUG_ON(irqs_disabled());
Ravikiran G Thirumalai556e58f2005-08-04 12:53:26 -0700953 BUG_ON(hwif == NULL);
954
Matthias Kaehlckeef298882007-07-09 23:17:55 +0200955 mutex_lock(&ide_cfg_mtx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 hwif->hwgroup = NULL;
957#if MAX_HWIFS > 1
958 /*
959 * Group up with any other hwifs that share our irq(s).
960 */
961 for (index = 0; index < MAX_HWIFS; index++) {
962 ide_hwif_t *h = &ide_hwifs[index];
963 if (h->hwgroup) { /* scan only initialized hwif's */
964 if (hwif->irq == h->irq) {
965 hwif->sharing_irq = h->sharing_irq = 1;
966 if (hwif->chipset != ide_pci ||
967 h->chipset != ide_pci) {
968 save_match(hwif, h, &match);
969 }
970 }
971 if (hwif->serialized) {
972 if (hwif->mate && hwif->mate->irq == h->irq)
973 save_match(hwif, h, &match);
974 }
975 if (h->serialized) {
976 if (h->mate && hwif->irq == h->mate->irq)
977 save_match(hwif, h, &match);
978 }
979 }
980 }
981#endif /* MAX_HWIFS > 1 */
982 /*
983 * If we are still without a hwgroup, then form a new one
984 */
985 if (match) {
986 hwgroup = match->hwgroup;
987 hwif->hwgroup = hwgroup;
988 /*
989 * Link us into the hwgroup.
990 * This must be done early, do ensure that unexpected_intr
991 * can find the hwif and prevent irq storms.
992 * No drives are attached to the new hwif, choose_drive
993 * can't do anything stupid (yet).
994 * Add ourself as the 2nd entry to the hwgroup->hwif
995 * linked list, the first entry is the hwif that owns
996 * hwgroup->handler - do not change that.
997 */
998 spin_lock_irq(&ide_lock);
999 hwif->next = hwgroup->hwif->next;
1000 hwgroup->hwif->next = hwif;
1001 spin_unlock_irq(&ide_lock);
1002 } else {
Christoph Lameter94f60302007-07-17 04:03:29 -07001003 hwgroup = kmalloc_node(sizeof(ide_hwgroup_t),
1004 GFP_KERNEL | __GFP_ZERO,
Ravikiran G Thirumalai556e58f2005-08-04 12:53:26 -07001005 hwif_to_node(hwif->drives[0].hwif));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 if (!hwgroup)
1007 goto out_up;
1008
1009 hwif->hwgroup = hwgroup;
1010
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 hwgroup->hwif = hwif->next = hwif;
1012 hwgroup->rq = NULL;
1013 hwgroup->handler = NULL;
1014 hwgroup->drive = NULL;
1015 hwgroup->busy = 0;
1016 init_timer(&hwgroup->timer);
1017 hwgroup->timer.function = &ide_timer_expiry;
1018 hwgroup->timer.data = (unsigned long) hwgroup;
1019 }
1020
1021 /*
1022 * Allocate the irq, if not already obtained for another hwif
1023 */
1024 if (!match || match->irq != hwif->irq) {
Bartlomiej Zolnierkiewicz7b5da4b2008-01-25 22:17:08 +01001025 int sa = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026#if defined(__mc68000__) || defined(CONFIG_APUS)
Thomas Gleixner362537b2006-07-01 19:29:34 -07001027 sa = IRQF_SHARED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028#endif /* __mc68000__ || CONFIG_APUS */
1029
Bartlomiej Zolnierkiewicz7b5da4b2008-01-25 22:17:08 +01001030 if (IDE_CHIPSET_IS_PCI(hwif->chipset))
Thomas Gleixner362537b2006-07-01 19:29:34 -07001031 sa = IRQF_SHARED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032
1033 if (hwif->io_ports[IDE_CONTROL_OFFSET])
1034 /* clear nIEN */
1035 hwif->OUTB(0x08, hwif->io_ports[IDE_CONTROL_OFFSET]);
1036
1037 if (request_irq(hwif->irq,&ide_intr,sa,hwif->name,hwgroup))
1038 goto out_unlink;
1039 }
1040
1041 /*
1042 * For any present drive:
1043 * - allocate the block device queue
1044 * - link drive into the hwgroup
1045 */
1046 for (index = 0; index < MAX_DRIVES; ++index) {
1047 ide_drive_t *drive = &hwif->drives[index];
1048 if (!drive->present)
1049 continue;
1050 if (ide_init_queue(drive)) {
1051 printk(KERN_ERR "ide: failed to init %s\n",drive->name);
1052 continue;
1053 }
1054 spin_lock_irq(&ide_lock);
1055 if (!hwgroup->drive) {
1056 /* first drive for hwgroup. */
1057 drive->next = drive;
1058 hwgroup->drive = drive;
1059 hwgroup->hwif = HWIF(hwgroup->drive);
1060 } else {
1061 drive->next = hwgroup->drive->next;
1062 hwgroup->drive->next = drive;
1063 }
1064 spin_unlock_irq(&ide_lock);
1065 }
1066
David S. Millerc6387a42006-06-20 01:21:29 -07001067#if !defined(__mc68000__) && !defined(CONFIG_APUS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 printk("%s at 0x%03lx-0x%03lx,0x%03lx on irq %d", hwif->name,
1069 hwif->io_ports[IDE_DATA_OFFSET],
1070 hwif->io_ports[IDE_DATA_OFFSET]+7,
1071 hwif->io_ports[IDE_CONTROL_OFFSET], hwif->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072#else
1073 printk("%s at 0x%08lx on irq %d", hwif->name,
1074 hwif->io_ports[IDE_DATA_OFFSET], hwif->irq);
1075#endif /* __mc68000__ && CONFIG_APUS */
1076 if (match)
1077 printk(" (%sed with %s)",
1078 hwif->sharing_irq ? "shar" : "serializ", match->name);
1079 printk("\n");
Matthias Kaehlckeef298882007-07-09 23:17:55 +02001080 mutex_unlock(&ide_cfg_mtx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 return 0;
1082out_unlink:
1083 spin_lock_irq(&ide_lock);
1084 if (hwif->next == hwif) {
1085 BUG_ON(match);
1086 BUG_ON(hwgroup->hwif != hwif);
1087 kfree(hwgroup);
1088 } else {
1089 ide_hwif_t *g;
1090 g = hwgroup->hwif;
1091 while (g->next != hwif)
1092 g = g->next;
1093 g->next = hwif->next;
1094 if (hwgroup->hwif == hwif) {
1095 /* Impossible. */
1096 printk(KERN_ERR "Duh. Uninitialized hwif listed as active hwif.\n");
1097 hwgroup->hwif = g;
1098 }
1099 BUG_ON(hwgroup->hwif == hwif);
1100 }
1101 spin_unlock_irq(&ide_lock);
1102out_up:
Matthias Kaehlckeef298882007-07-09 23:17:55 +02001103 mutex_unlock(&ide_cfg_mtx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 return 1;
1105}
1106
1107static int ata_lock(dev_t dev, void *data)
1108{
1109 /* FIXME: we want to pin hwif down */
1110 return 0;
1111}
1112
1113static struct kobject *ata_probe(dev_t dev, int *part, void *data)
1114{
1115 ide_hwif_t *hwif = data;
1116 int unit = *part >> PARTN_BITS;
1117 ide_drive_t *drive = &hwif->drives[unit];
1118 if (!drive->present)
1119 return NULL;
1120
1121 if (drive->media == ide_disk)
1122 request_module("ide-disk");
1123 if (drive->scsi)
1124 request_module("ide-scsi");
1125 if (drive->media == ide_cdrom || drive->media == ide_optical)
1126 request_module("ide-cd");
1127 if (drive->media == ide_tape)
1128 request_module("ide-tape");
1129 if (drive->media == ide_floppy)
1130 request_module("ide-floppy");
1131
1132 return NULL;
1133}
1134
1135static struct kobject *exact_match(dev_t dev, int *part, void *data)
1136{
1137 struct gendisk *p = data;
1138 *part &= (1 << PARTN_BITS) - 1;
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001139 return &p->dev.kobj;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140}
1141
1142static int exact_lock(dev_t dev, void *data)
1143{
1144 struct gendisk *p = data;
1145
1146 if (!get_disk(p))
1147 return -1;
1148 return 0;
1149}
1150
1151void ide_register_region(struct gendisk *disk)
1152{
1153 blk_register_region(MKDEV(disk->major, disk->first_minor),
1154 disk->minors, NULL, exact_match, exact_lock, disk);
1155}
1156
1157EXPORT_SYMBOL_GPL(ide_register_region);
1158
1159void ide_unregister_region(struct gendisk *disk)
1160{
1161 blk_unregister_region(MKDEV(disk->major, disk->first_minor),
1162 disk->minors);
1163}
1164
1165EXPORT_SYMBOL_GPL(ide_unregister_region);
1166
1167void ide_init_disk(struct gendisk *disk, ide_drive_t *drive)
1168{
1169 ide_hwif_t *hwif = drive->hwif;
1170 unsigned int unit = (drive->select.all >> 4) & 1;
1171
1172 disk->major = hwif->major;
1173 disk->first_minor = unit << PARTN_BITS;
1174 sprintf(disk->disk_name, "hd%c", 'a' + hwif->index * MAX_DRIVES + unit);
1175 disk->queue = drive->queue;
1176}
1177
1178EXPORT_SYMBOL_GPL(ide_init_disk);
1179
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02001180static void ide_remove_drive_from_hwgroup(ide_drive_t *drive)
1181{
1182 ide_hwgroup_t *hwgroup = drive->hwif->hwgroup;
1183
1184 if (drive == drive->next) {
1185 /* special case: last drive from hwgroup. */
1186 BUG_ON(hwgroup->drive != drive);
1187 hwgroup->drive = NULL;
1188 } else {
1189 ide_drive_t *walk;
1190
1191 walk = hwgroup->drive;
1192 while (walk->next != drive)
1193 walk = walk->next;
1194 walk->next = drive->next;
1195 if (hwgroup->drive == drive) {
1196 hwgroup->drive = drive->next;
1197 hwgroup->hwif = hwgroup->drive->hwif;
1198 }
1199 }
1200 BUG_ON(hwgroup->drive == drive);
1201}
1202
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203static void drive_release_dev (struct device *dev)
1204{
1205 ide_drive_t *drive = container_of(dev, ide_drive_t, gendev);
1206
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02001207 spin_lock_irq(&ide_lock);
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02001208 ide_remove_drive_from_hwgroup(drive);
Jesper Juhl6044ec82005-11-07 01:01:32 -08001209 kfree(drive->id);
1210 drive->id = NULL;
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02001211 drive->present = 0;
1212 /* Messed up locking ... */
1213 spin_unlock_irq(&ide_lock);
1214 blk_cleanup_queue(drive->queue);
1215 spin_lock_irq(&ide_lock);
1216 drive->queue = NULL;
1217 spin_unlock_irq(&ide_lock);
1218
Aleksey Makarovf36d4022006-01-09 15:59:27 -08001219 complete(&drive->gendev_rel_comp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220}
1221
1222/*
1223 * init_gendisk() (as opposed to ide_geninit) is called for each major device,
1224 * after probing for drives, to allocate partition tables and other data
1225 * structures needed for the routines in genhd.c. ide_geninit() gets called
1226 * somewhat later, during the partition check.
1227 */
1228static void init_gendisk (ide_hwif_t *hwif)
1229{
1230 unsigned int unit;
1231
1232 for (unit = 0; unit < MAX_DRIVES; ++unit) {
1233 ide_drive_t * drive = &hwif->drives[unit];
1234 ide_add_generic_settings(drive);
1235 snprintf(drive->gendev.bus_id,BUS_ID_SIZE,"%u.%u",
1236 hwif->index,unit);
1237 drive->gendev.parent = &hwif->gendev;
1238 drive->gendev.bus = &ide_bus_type;
1239 drive->gendev.driver_data = drive;
1240 drive->gendev.release = drive_release_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 }
1242 blk_register_region(MKDEV(hwif->major, 0), MAX_DRIVES << PARTN_BITS,
1243 THIS_MODULE, ata_probe, ata_lock, hwif);
1244}
1245
1246static int hwif_init(ide_hwif_t *hwif)
1247{
1248 int old_irq;
1249
1250 /* Return success if no device is connected */
1251 if (!hwif->present)
1252 return 1;
1253
1254 if (!hwif->irq) {
1255 if (!(hwif->irq = ide_default_irq(hwif->io_ports[IDE_DATA_OFFSET])))
1256 {
1257 printk("%s: DISABLED, NO IRQ\n", hwif->name);
1258 return (hwif->present = 0);
1259 }
1260 }
1261#ifdef CONFIG_BLK_DEV_HD
1262 if (hwif->irq == HD_IRQ && hwif->io_ports[IDE_DATA_OFFSET] != HD_DATA) {
1263 printk("%s: CANNOT SHARE IRQ WITH OLD "
1264 "HARDDISK DRIVER (hd.c)\n", hwif->name);
1265 return (hwif->present = 0);
1266 }
1267#endif /* CONFIG_BLK_DEV_HD */
1268
1269 /* we set it back to 1 if all is ok below */
1270 hwif->present = 0;
1271
1272 if (register_blkdev(hwif->major, hwif->name))
1273 return 0;
1274
1275 if (!hwif->sg_max_nents)
1276 hwif->sg_max_nents = PRD_ENTRIES;
1277
Jens Axboe45711f12007-10-22 21:19:53 +02001278 hwif->sg_table = kmalloc(sizeof(struct scatterlist)*hwif->sg_max_nents,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 GFP_KERNEL);
1280 if (!hwif->sg_table) {
1281 printk(KERN_ERR "%s: unable to allocate SG table.\n", hwif->name);
1282 goto out;
1283 }
Jens Axboe45711f12007-10-22 21:19:53 +02001284
1285 sg_init_table(hwif->sg_table, hwif->sg_max_nents);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286
1287 if (init_irq(hwif) == 0)
1288 goto done;
1289
1290 old_irq = hwif->irq;
1291 /*
1292 * It failed to initialise. Find the default IRQ for
1293 * this port and try that.
1294 */
1295 if (!(hwif->irq = ide_default_irq(hwif->io_ports[IDE_DATA_OFFSET]))) {
1296 printk("%s: Disabled unable to get IRQ %d.\n",
1297 hwif->name, old_irq);
1298 goto out;
1299 }
1300 if (init_irq(hwif)) {
1301 printk("%s: probed IRQ %d and default IRQ %d failed.\n",
1302 hwif->name, old_irq, hwif->irq);
1303 goto out;
1304 }
1305 printk("%s: probed IRQ %d failed, using default.\n",
1306 hwif->name, hwif->irq);
1307
1308done:
1309 init_gendisk(hwif);
Hannes Reineckee3a59b42007-02-07 18:19:37 +01001310
1311 ide_acpi_init(hwif);
1312
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 hwif->present = 1; /* success */
1314 return 1;
1315
1316out:
1317 unregister_blkdev(hwif->major, hwif->name);
1318 return 0;
1319}
1320
Bartlomiej Zolnierkiewicz9601a602007-10-20 00:32:29 +02001321static void hwif_register_devices(ide_hwif_t *hwif)
1322{
1323 unsigned int i;
1324
1325 for (i = 0; i < MAX_DRIVES; i++) {
1326 ide_drive_t *drive = &hwif->drives[i];
1327
1328 if (drive->present) {
1329 int ret = device_register(&drive->gendev);
1330
1331 if (ret < 0)
1332 printk(KERN_WARNING "IDE: %s: "
1333 "device_register error: %d\n",
1334 __FUNCTION__, ret);
1335 }
1336 }
1337}
1338
Olof Johanssonb0d5bc22008-01-26 20:13:05 +01001339int ide_device_add_all(u8 *idx)
Bartlomiej Zolnierkiewicz8447d9d2007-10-20 00:32:31 +02001340{
Bartlomiej Zolnierkiewiczba6560a2008-01-26 20:13:04 +01001341 ide_hwif_t *hwif;
Bartlomiej Zolnierkiewicz8447d9d2007-10-20 00:32:31 +02001342 int i, rc = 0;
1343
Bartlomiej Zolnierkiewicz151575e2008-01-26 20:13:05 +01001344 for (i = 0; i < MAX_HWIFS; i++) {
Bartlomiej Zolnierkiewiczba6560a2008-01-26 20:13:04 +01001345 if (idx[i] == 0xff)
1346 continue;
1347
Bartlomiej Zolnierkiewicz2e130932008-01-26 20:13:04 +01001348 probe_hwif(&ide_hwifs[idx[i]]);
1349 }
Bartlomiej Zolnierkiewiczba6560a2008-01-26 20:13:04 +01001350
Bartlomiej Zolnierkiewicz151575e2008-01-26 20:13:05 +01001351 for (i = 0; i < MAX_HWIFS; i++) {
Bartlomiej Zolnierkiewicz2e130932008-01-26 20:13:04 +01001352 if (idx[i] == 0xff)
1353 continue;
1354
1355 hwif = &ide_hwifs[idx[i]];
Bartlomiej Zolnierkiewiczba6560a2008-01-26 20:13:04 +01001356
1357 if (hwif_init(hwif) == 0) {
1358 printk(KERN_INFO "%s: failed to initialize IDE "
1359 "interface\n", hwif->name);
1360 rc = -1;
1361 continue;
1362 }
Bartlomiej Zolnierkiewicz2e130932008-01-26 20:13:04 +01001363 }
1364
Bartlomiej Zolnierkiewicz151575e2008-01-26 20:13:05 +01001365 for (i = 0; i < MAX_HWIFS; i++) {
Bartlomiej Zolnierkiewicz2e130932008-01-26 20:13:04 +01001366 if (idx[i] == 0xff)
1367 continue;
1368
1369 hwif = &ide_hwifs[idx[i]];
Bartlomiej Zolnierkiewiczba6560a2008-01-26 20:13:04 +01001370
Bartlomiej Zolnierkiewicz71518342008-01-26 20:13:04 +01001371 if (hwif->present) {
1372 if (hwif->chipset == ide_unknown ||
1373 hwif->chipset == ide_forced)
1374 hwif->chipset = ide_generic;
Bartlomiej Zolnierkiewiczba6560a2008-01-26 20:13:04 +01001375 hwif_register_devices(hwif);
Bartlomiej Zolnierkiewicz71518342008-01-26 20:13:04 +01001376 }
Bartlomiej Zolnierkiewicz8447d9d2007-10-20 00:32:31 +02001377 }
1378
Bartlomiej Zolnierkiewicz151575e2008-01-26 20:13:05 +01001379 for (i = 0; i < MAX_HWIFS; i++) {
Bartlomiej Zolnierkiewicz8447d9d2007-10-20 00:32:31 +02001380 if (idx[i] != 0xff)
1381 ide_proc_register_port(&ide_hwifs[idx[i]]);
1382 }
1383
1384 return rc;
1385}
Bartlomiej Zolnierkiewicz151575e2008-01-26 20:13:05 +01001386EXPORT_SYMBOL_GPL(ide_device_add_all);
Bartlomiej Zolnierkiewicz8447d9d2007-10-20 00:32:31 +02001387
Bartlomiej Zolnierkiewicz151575e2008-01-26 20:13:05 +01001388int ide_device_add(u8 idx[4])
1389{
1390 u8 idx_all[MAX_HWIFS];
1391 int i;
1392
1393 for (i = 0; i < MAX_HWIFS; i++)
1394 idx_all[i] = (i < 4) ? idx[i] : 0xff;
1395
1396 return ide_device_add_all(idx_all);
1397}
Bartlomiej Zolnierkiewicz8447d9d2007-10-20 00:32:31 +02001398EXPORT_SYMBOL_GPL(ide_device_add);