ide: fix for EATA SCSI HBA in ATA emulating mode
[deliverable/linux.git] / drivers / ide / ide-probe.c
CommitLineData
1da177e4 1/*
59bca8cc
BZ
2 * Copyright (C) 1994-1998 Linus Torvalds & authors (see below)
3 * Copyright (C) 2005, 2007 Bartlomiej Zolnierkiewicz
1da177e4
LT
4 */
5
6/*
7 * Mostly written by Mark Lord <mlord@pobox.com>
8 * and Gadi Oxman <gadio@netvision.net.il>
9 * and Andre Hedrick <andre@linux-ide.org>
10 *
11 * See linux/MAINTAINERS for address of current maintainer.
12 *
13 * This is the IDE probe module, as evolved from hd.c and ide.c.
14 *
bbe4d6d8
BZ
15 * -- increase WAIT_PIDENTIFY to avoid CD-ROM locking at boot
16 * by Andrea Arcangeli
1da177e4
LT
17 */
18
1da177e4
LT
19#include <linux/module.h>
20#include <linux/types.h>
21#include <linux/string.h>
22#include <linux/kernel.h>
23#include <linux/timer.h>
24#include <linux/mm.h>
25#include <linux/interrupt.h>
26#include <linux/major.h>
27#include <linux/errno.h>
28#include <linux/genhd.h>
29#include <linux/slab.h>
30#include <linux/delay.h>
31#include <linux/ide.h>
32#include <linux/spinlock.h>
33#include <linux/kmod.h>
34#include <linux/pci.h>
dc81785d 35#include <linux/scatterlist.h>
1da177e4
LT
36
37#include <asm/byteorder.h>
38#include <asm/irq.h>
39#include <asm/uaccess.h>
40#include <asm/io.h>
41
42/**
43 * generic_id - add a generic drive id
44 * @drive: drive to make an ID block for
45 *
46 * Add a fake id field to the drive we are passed. This allows
47 * use to skip a ton of NULL checks (which people always miss)
48 * and make drive properties unconditional outside of this file
49 */
50
51static void generic_id(ide_drive_t *drive)
52{
53 drive->id->cyls = drive->cyl;
54 drive->id->heads = drive->head;
55 drive->id->sectors = drive->sect;
56 drive->id->cur_cyls = drive->cyl;
57 drive->id->cur_heads = drive->head;
58 drive->id->cur_sectors = drive->sect;
59}
60
61static void ide_disk_init_chs(ide_drive_t *drive)
62{
63 struct hd_driveid *id = drive->id;
64
65 /* Extract geometry if we did not already have one for the drive */
66 if (!drive->cyl || !drive->head || !drive->sect) {
67 drive->cyl = drive->bios_cyl = id->cyls;
68 drive->head = drive->bios_head = id->heads;
69 drive->sect = drive->bios_sect = id->sectors;
70 }
71
72 /* Handle logical geometry translation by the drive */
73 if ((id->field_valid & 1) && id->cur_cyls &&
74 id->cur_heads && (id->cur_heads <= 16) && id->cur_sectors) {
75 drive->cyl = id->cur_cyls;
76 drive->head = id->cur_heads;
77 drive->sect = id->cur_sectors;
78 }
79
80 /* Use physical geometry if what we have still makes no sense */
81 if (drive->head > 16 && id->heads && id->heads <= 16) {
82 drive->cyl = id->cyls;
83 drive->head = id->heads;
84 drive->sect = id->sectors;
85 }
86}
87
88static void ide_disk_init_mult_count(ide_drive_t *drive)
89{
90 struct hd_driveid *id = drive->id;
91
92 drive->mult_count = 0;
93 if (id->max_multsect) {
94#ifdef CONFIG_IDEDISK_MULTI_MODE
95 id->multsect = ((id->max_multsect/2) > 1) ? id->max_multsect : 0;
96 id->multsect_valid = id->multsect ? 1 : 0;
4ee06b7e 97 drive->mult_req = id->multsect_valid ? id->max_multsect : 0;
1da177e4
LT
98 drive->special.b.set_multmode = drive->mult_req ? 1 : 0;
99#else /* original, pre IDE-NFG, per request of AC */
4ee06b7e 100 drive->mult_req = 0;
1da177e4
LT
101 if (drive->mult_req > id->max_multsect)
102 drive->mult_req = id->max_multsect;
103 if (drive->mult_req || ((id->multsect_valid & 1) && id->multsect))
104 drive->special.b.set_multmode = 1;
105#endif
106 }
107}
108
1da177e4
LT
109/**
110 * do_identify - identify a drive
111 * @drive: drive to identify
112 * @cmd: command used
113 *
114 * Called when we have issued a drive identify command to
115 * read and parse the results. This function is run with
116 * interrupts disabled.
117 */
118
119static inline void do_identify (ide_drive_t *drive, u8 cmd)
120{
121 ide_hwif_t *hwif = HWIF(drive);
122 int bswap = 1;
123 struct hd_driveid *id;
124
125 id = drive->id;
126 /* read 512 bytes of id info */
374e042c 127 hwif->tp_ops->input_data(drive, NULL, id, SECTOR_SIZE);
1da177e4
LT
128
129 drive->id_read = 1;
130 local_irq_enable();
7b9f25b5
BZ
131#ifdef DEBUG
132 printk(KERN_INFO "%s: dumping identify data\n", drive->name);
133 ide_dump_identify((u8 *)id);
134#endif
1da177e4
LT
135 ide_fix_driveid(id);
136
1da177e4
LT
137 /*
138 * WIN_IDENTIFY returns little-endian info,
139 * WIN_PIDENTIFY *usually* returns little-endian info.
140 */
141 if (cmd == WIN_PIDENTIFY) {
142 if ((id->model[0] == 'N' && id->model[1] == 'E') /* NEC */
143 || (id->model[0] == 'F' && id->model[1] == 'X') /* Mitsumi */
144 || (id->model[0] == 'P' && id->model[1] == 'i'))/* Pioneer */
145 /* Vertos drives may still be weird */
146 bswap ^= 1;
147 }
148 ide_fixstring(id->model, sizeof(id->model), bswap);
149 ide_fixstring(id->fw_rev, sizeof(id->fw_rev), bswap);
150 ide_fixstring(id->serial_no, sizeof(id->serial_no), bswap);
151
699b052a
TH
152 /* we depend on this a lot! */
153 id->model[sizeof(id->model)-1] = '\0';
154
1da177e4
LT
155 if (strstr(id->model, "E X A B Y T E N E S T"))
156 goto err_misc;
157
1da177e4
LT
158 printk("%s: %s, ", drive->name, id->model);
159 drive->present = 1;
160 drive->dead = 0;
161
162 /*
163 * Check for an ATAPI device
164 */
165 if (cmd == WIN_PIDENTIFY) {
166 u8 type = (id->config >> 8) & 0x1f;
167 printk("ATAPI ");
168 switch (type) {
169 case ide_floppy:
170 if (!strstr(id->model, "CD-ROM")) {
171 if (!strstr(id->model, "oppy") &&
172 !strstr(id->model, "poyp") &&
173 !strstr(id->model, "ZIP"))
174 printk("cdrom or floppy?, assuming ");
175 if (drive->media != ide_cdrom) {
176 printk ("FLOPPY");
177 drive->removable = 1;
178 break;
179 }
180 }
181 /* Early cdrom models used zero */
182 type = ide_cdrom;
183 case ide_cdrom:
184 drive->removable = 1;
185#ifdef CONFIG_PPC
186 /* kludge for Apple PowerBook internal zip */
187 if (!strstr(id->model, "CD-ROM") &&
188 strstr(id->model, "ZIP")) {
189 printk ("FLOPPY");
190 type = ide_floppy;
191 break;
192 }
193#endif
194 printk ("CD/DVD-ROM");
195 break;
196 case ide_tape:
197 printk ("TAPE");
198 break;
199 case ide_optical:
200 printk ("OPTICAL");
201 drive->removable = 1;
202 break;
203 default:
204 printk("UNKNOWN (type %d)", type);
205 break;
206 }
207 printk (" drive\n");
208 drive->media = type;
209 /* an ATAPI device ignores DRDY */
210 drive->ready_stat = 0;
211 return;
212 }
213
214 /*
215 * Not an ATAPI device: looks like a "regular" hard disk
216 */
98109337
RP
217
218 /*
219 * 0x848a = CompactFlash device
220 * These are *not* removable in Linux definition of the term
221 */
222
223 if ((id->config != 0x848a) && (id->config & (1<<7)))
1da177e4
LT
224 drive->removable = 1;
225
1da177e4 226 drive->media = ide_disk;
98109337 227 printk("%s DISK drive\n", (id->config == 0x848a) ? "CFA" : "ATA" );
cd3dbc99 228
1da177e4
LT
229 return;
230
231err_misc:
232 kfree(id);
233 drive->present = 0;
234 return;
235}
236
237/**
238 * actual_try_to_identify - send ata/atapi identify
239 * @drive: drive to identify
240 * @cmd: command to use
241 *
242 * try_to_identify() sends an ATA(PI) IDENTIFY request to a drive
243 * and waits for a response. It also monitors irqs while this is
244 * happening, in hope of automatically determining which one is
245 * being used by the interface.
246 *
247 * Returns: 0 device was identified
248 * 1 device timed-out (no response to identify request)
249 * 2 device aborted the command (refused to identify itself)
250 */
251
252static int actual_try_to_identify (ide_drive_t *drive, u8 cmd)
253{
254 ide_hwif_t *hwif = HWIF(drive);
4c3032d8 255 struct ide_io_ports *io_ports = &hwif->io_ports;
374e042c 256 const struct ide_tp_ops *tp_ops = hwif->tp_ops;
c47137a9 257 int use_altstatus = 0, rc;
1da177e4
LT
258 unsigned long timeout;
259 u8 s = 0, a = 0;
260
261 /* take a deep breath */
262 msleep(50);
263
4c3032d8 264 if (io_ports->ctl_addr) {
374e042c
BZ
265 a = tp_ops->read_altstatus(hwif);
266 s = tp_ops->read_status(hwif);
c47137a9 267 if ((a ^ s) & ~INDEX_STAT)
1da177e4 268 /* ancient Seagate drives, broken interfaces */
c47137a9
BZ
269 printk(KERN_INFO "%s: probing with STATUS(0x%02x) "
270 "instead of ALTSTATUS(0x%02x)\n",
271 drive->name, s, a);
272 else
1da177e4 273 /* use non-intrusive polling */
c47137a9
BZ
274 use_altstatus = 1;
275 }
1da177e4
LT
276
277 /* set features register for atapi
278 * identify command to be sure of reply
279 */
4e65837b
BZ
280 if (cmd == WIN_PIDENTIFY) {
281 ide_task_t task;
282
283 memset(&task, 0, sizeof(task));
284 /* disable DMA & overlap */
285 task.tf_flags = IDE_TFLAG_OUT_FEATURE;
286
374e042c 287 tp_ops->tf_load(drive, &task);
4e65837b 288 }
1da177e4
LT
289
290 /* ask drive for ID */
374e042c 291 tp_ops->exec_command(hwif, cmd);
1da177e4
LT
292
293 timeout = ((cmd == WIN_IDENTIFY) ? WAIT_WORSTCASE : WAIT_PIDENTIFY) / 2;
294 timeout += jiffies;
295 do {
296 if (time_after(jiffies, timeout)) {
297 /* drive timed-out */
298 return 1;
299 }
300 /* give drive a breather */
301 msleep(50);
374e042c
BZ
302 s = use_altstatus ? tp_ops->read_altstatus(hwif)
303 : tp_ops->read_status(hwif);
c47137a9 304 } while (s & BUSY_STAT);
1da177e4
LT
305
306 /* wait for IRQ and DRQ_STAT */
307 msleep(50);
374e042c 308 s = tp_ops->read_status(hwif);
c47137a9
BZ
309
310 if (OK_STAT(s, DRQ_STAT, BAD_R_STAT)) {
1da177e4
LT
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 */
374e042c 320 (void)tp_ops->read_status(hwif);
1da177e4
LT
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);
374e042c 342 const struct ide_tp_ops *tp_ops = hwif->tp_ops;
1da177e4
LT
343 int retval;
344 int autoprobe = 0;
345 unsigned long cookie = 0;
346
347 /*
348 * Disable device irq unless we need to
349 * probe for it. Otherwise we'll get spurious
350 * interrupts during the identify-phase that
351 * the irq handler isn't expecting.
352 */
4c3032d8 353 if (hwif->io_ports.ctl_addr) {
1da177e4
LT
354 if (!hwif->irq) {
355 autoprobe = 1;
356 cookie = probe_irq_on();
1da177e4 357 }
374e042c 358 tp_ops->set_irq(hwif, autoprobe);
1da177e4
LT
359 }
360
361 retval = actual_try_to_identify(drive, cmd);
362
363 if (autoprobe) {
364 int irq;
81ca6919 365
374e042c 366 tp_ops->set_irq(hwif, 0);
1da177e4 367 /* clear drive IRQ */
374e042c 368 (void)tp_ops->read_status(hwif);
1da177e4
LT
369 udelay(5);
370 irq = probe_irq_off(cookie);
371 if (!hwif->irq) {
372 if (irq > 0) {
373 hwif->irq = irq;
374 } else {
375 /* Mmmm.. multiple IRQs..
376 * don't know which was ours
377 */
378 printk("%s: IRQ probe failed (0x%lx)\n",
379 drive->name, cookie);
380 }
381 }
382 }
383 return retval;
384}
385
3a5015cc
BZ
386static int ide_busy_sleep(ide_hwif_t *hwif)
387{
388 unsigned long timeout = jiffies + WAIT_WORSTCASE;
389 u8 stat;
390
391 do {
392 msleep(50);
374e042c 393 stat = hwif->tp_ops->read_status(hwif);
3a5015cc
BZ
394 if ((stat & BUSY_STAT) == 0)
395 return 0;
396 } while (time_before(jiffies, timeout));
397
398 return 1;
399}
1da177e4 400
1f2efb82
BZ
401static u8 ide_read_device(ide_drive_t *drive)
402{
403 ide_task_t task;
404
405 memset(&task, 0, sizeof(task));
406 task.tf_flags = IDE_TFLAG_IN_DEVICE;
407
374e042c 408 drive->hwif->tp_ops->tf_read(drive, &task);
1f2efb82
BZ
409
410 return task.tf.device;
411}
412
1da177e4
LT
413/**
414 * do_probe - probe an IDE device
415 * @drive: drive to probe
416 * @cmd: command to use
417 *
418 * do_probe() has the difficult job of finding a drive if it exists,
419 * without getting hung up if it doesn't exist, without trampling on
420 * ethernet cards, and without leaving any IRQs dangling to haunt us later.
421 *
422 * If a drive is "known" to exist (from CMOS or kernel parameters),
423 * but does not respond right away, the probe will "hang in there"
424 * for the maximum wait time (about 30 seconds), otherwise it will
425 * exit much more quickly.
426 *
427 * Returns: 0 device was identified
428 * 1 device timed-out (no response to identify request)
429 * 2 device aborted the command (refused to identify itself)
430 * 3 bad status from device (possible for ATAPI drives)
431 * 4 probe was not attempted because failure was obvious
432 */
433
434static int do_probe (ide_drive_t *drive, u8 cmd)
435{
1da177e4 436 ide_hwif_t *hwif = HWIF(drive);
374e042c 437 const struct ide_tp_ops *tp_ops = hwif->tp_ops;
57b55275
BZ
438 int rc;
439 u8 stat;
1da177e4
LT
440
441 if (drive->present) {
442 /* avoid waiting for inappropriate probes */
443 if ((drive->media != ide_disk) && (cmd == WIN_IDENTIFY))
444 return 4;
445 }
446#ifdef DEBUG
447 printk("probing for %s: present=%d, media=%d, probetype=%s\n",
448 drive->name, drive->present, drive->media,
449 (cmd == WIN_IDENTIFY) ? "ATA" : "ATAPI");
450#endif
451
452 /* needed for some systems
453 * (e.g. crw9624 as drive0 with disk as slave)
454 */
455 msleep(50);
456 SELECT_DRIVE(drive);
457 msleep(50);
1f2efb82
BZ
458
459 if (ide_read_device(drive) != drive->select.all && !drive->present) {
1da177e4
LT
460 if (drive->select.b.unit != 0) {
461 /* exit with drive0 selected */
462 SELECT_DRIVE(&hwif->drives[0]);
463 /* allow BUSY_STAT to assert & clear */
464 msleep(50);
465 }
466 /* no i/f present: mmm.. this should be a 4 -ml */
467 return 3;
468 }
469
374e042c 470 stat = tp_ops->read_status(hwif);
c47137a9
BZ
471
472 if (OK_STAT(stat, READY_STAT, BUSY_STAT) ||
1da177e4
LT
473 drive->present || cmd == WIN_PIDENTIFY) {
474 /* send cmd and wait */
475 if ((rc = try_to_identify(drive, cmd))) {
476 /* failed: try again */
477 rc = try_to_identify(drive,cmd);
478 }
57b55275 479
374e042c 480 stat = tp_ops->read_status(hwif);
57b55275
BZ
481
482 if (stat == (BUSY_STAT | READY_STAT))
1da177e4
LT
483 return 4;
484
cc12175f 485 if (rc == 1 && cmd == WIN_PIDENTIFY) {
57b55275
BZ
486 printk(KERN_ERR "%s: no response (status = 0x%02x), "
487 "resetting drive\n", drive->name, stat);
1da177e4 488 msleep(50);
e81a3bde 489 SELECT_DRIVE(drive);
1da177e4 490 msleep(50);
374e042c 491 tp_ops->exec_command(hwif, WIN_SRST);
3a5015cc 492 (void)ide_busy_sleep(hwif);
1da177e4
LT
493 rc = try_to_identify(drive, cmd);
494 }
57b55275
BZ
495
496 /* ensure drive IRQ is clear */
374e042c 497 stat = tp_ops->read_status(hwif);
57b55275 498
1da177e4 499 if (rc == 1)
57b55275
BZ
500 printk(KERN_ERR "%s: no response (status = 0x%02x)\n",
501 drive->name, stat);
1da177e4
LT
502 } else {
503 /* not present or maybe ATAPI */
504 rc = 3;
505 }
506 if (drive->select.b.unit != 0) {
507 /* exit with drive0 selected */
508 SELECT_DRIVE(&hwif->drives[0]);
509 msleep(50);
510 /* ensure drive irq is clear */
374e042c 511 (void)tp_ops->read_status(hwif);
1da177e4
LT
512 }
513 return rc;
514}
515
516/*
517 *
518 */
519static void enable_nest (ide_drive_t *drive)
520{
521 ide_hwif_t *hwif = HWIF(drive);
374e042c 522 const struct ide_tp_ops *tp_ops = hwif->tp_ops;
57b55275 523 u8 stat;
1da177e4
LT
524
525 printk("%s: enabling %s -- ", hwif->name, drive->id->model);
526 SELECT_DRIVE(drive);
527 msleep(50);
374e042c 528 tp_ops->exec_command(hwif, EXABYTE_ENABLE_NEST);
3a5015cc
BZ
529
530 if (ide_busy_sleep(hwif)) {
531 printk(KERN_CONT "failed (timeout)\n");
532 return;
533 }
1da177e4
LT
534
535 msleep(50);
536
374e042c 537 stat = tp_ops->read_status(hwif);
57b55275
BZ
538
539 if (!OK_STAT(stat, 0, BAD_STAT))
540 printk(KERN_CONT "failed (status = 0x%02x)\n", stat);
541 else
542 printk(KERN_CONT "success\n");
1da177e4
LT
543
544 /* if !(success||timed-out) */
545 if (do_probe(drive, WIN_IDENTIFY) >= 2) {
546 /* look for ATAPI device */
547 (void) do_probe(drive, WIN_PIDENTIFY);
548 }
549}
550
551/**
552 * probe_for_drives - upper level drive probe
553 * @drive: drive to probe for
554 *
555 * probe_for_drive() tests for existence of a given drive using do_probe()
556 * and presents things to the user as needed.
557 *
558 * Returns: 0 no device was found
559 * 1 device was found (note: drive->present might
560 * still be 0)
561 */
562
563static inline u8 probe_for_drive (ide_drive_t *drive)
564{
565 /*
566 * In order to keep things simple we have an id
567 * block for all drives at all times. If the device
568 * is pre ATA or refuses ATA/ATAPI identify we
569 * will add faked data to this.
570 *
571 * Also note that 0 everywhere means "can't do X"
572 */
573
f5e3c2fa 574 drive->id = kzalloc(SECTOR_WORDS *4, GFP_KERNEL);
1da177e4
LT
575 drive->id_read = 0;
576 if(drive->id == NULL)
577 {
578 printk(KERN_ERR "ide: out of memory for id data.\n");
579 return 0;
580 }
1da177e4
LT
581 strcpy(drive->id->model, "UNKNOWN");
582
583 /* skip probing? */
584 if (!drive->noprobe)
585 {
586 /* if !(success||timed-out) */
587 if (do_probe(drive, WIN_IDENTIFY) >= 2) {
588 /* look for ATAPI device */
589 (void) do_probe(drive, WIN_PIDENTIFY);
590 }
1da177e4
LT
591 if (!drive->present)
592 /* drive not found */
593 return 0;
78595575
AC
594 if (strstr(drive->id->model, "E X A B Y T E N E S T"))
595 enable_nest(drive);
1da177e4
LT
596
597 /* identification failed? */
598 if (!drive->id_read) {
599 if (drive->media == ide_disk) {
600 printk(KERN_INFO "%s: non-IDE drive, CHS=%d/%d/%d\n",
601 drive->name, drive->cyl,
602 drive->head, drive->sect);
603 } else if (drive->media == ide_cdrom) {
604 printk(KERN_INFO "%s: ATAPI cdrom (?)\n", drive->name);
605 } else {
606 /* nuke it */
607 printk(KERN_WARNING "%s: Unknown device on bus refused identification. Ignoring.\n", drive->name);
608 drive->present = 0;
609 }
610 }
611 /* drive was found */
612 }
613 if(!drive->present)
614 return 0;
615 /* The drive wasn't being helpful. Add generic info only */
616 if (drive->id_read == 0) {
617 generic_id(drive);
618 return 1;
619 }
620
621 if (drive->media == ide_disk) {
622 ide_disk_init_chs(drive);
623 ide_disk_init_mult_count(drive);
624 }
625
626 return drive->present;
627}
628
fc410698 629static void hwif_release_dev(struct device *dev)
1da177e4
LT
630{
631 ide_hwif_t *hwif = container_of(dev, ide_hwif_t, gendev);
632
f36d4024 633 complete(&hwif->gendev_rel_comp);
1da177e4
LT
634}
635
f74c9141 636static int ide_register_port(ide_hwif_t *hwif)
1da177e4 637{
349ae23f
RD
638 int ret;
639
1da177e4
LT
640 /* register with global device tree */
641 strlcpy(hwif->gendev.bus_id,hwif->name,BUS_ID_SIZE);
642 hwif->gendev.driver_data = hwif;
643 if (hwif->gendev.parent == NULL) {
36501650
BZ
644 if (hwif->dev)
645 hwif->gendev.parent = hwif->dev;
1da177e4
LT
646 else
647 /* Would like to do = &device_legacy */
648 hwif->gendev.parent = NULL;
649 }
650 hwif->gendev.release = hwif_release_dev;
349ae23f 651 ret = device_register(&hwif->gendev);
f74c9141 652 if (ret < 0) {
349ae23f 653 printk(KERN_WARNING "IDE: %s: device_register error: %d\n",
eb63963a 654 __func__, ret);
f74c9141
BZ
655 goto out;
656 }
657
716ad875
GKH
658 hwif->portdev = device_create_drvdata(ide_port_class, &hwif->gendev,
659 MKDEV(0, 0), hwif, hwif->name);
f74c9141
BZ
660 if (IS_ERR(hwif->portdev)) {
661 ret = PTR_ERR(hwif->portdev);
662 device_unregister(&hwif->gendev);
663 }
f74c9141
BZ
664out:
665 return ret;
1da177e4
LT
666}
667
c860a8f2
BZ
668/**
669 * ide_port_wait_ready - wait for port to become ready
670 * @hwif: IDE port
671 *
672 * This is needed on some PPCs and a bunch of BIOS-less embedded
673 * platforms. Typical cases are:
674 *
675 * - The firmware hard reset the disk before booting the kernel,
676 * the drive is still doing it's poweron-reset sequence, that
677 * can take up to 30 seconds.
678 *
679 * - The firmware does nothing (or no firmware), the device is
680 * still in POST state (same as above actually).
681 *
682 * - Some CD/DVD/Writer combo drives tend to drive the bus during
683 * their reset sequence even when they are non-selected slave
684 * devices, thus preventing discovery of the main HD.
685 *
686 * Doing this wait-for-non-busy should not harm any existing
687 * configuration and fix some issues like the above.
688 *
689 * BenH.
690 *
691 * Returns 0 on success, error code (< 0) otherwise.
692 */
693
694static int ide_port_wait_ready(ide_hwif_t *hwif)
1da177e4 695{
8266105b 696 int unit, rc;
1da177e4
LT
697
698 printk(KERN_DEBUG "Probing IDE interface %s...\n", hwif->name);
699
700 /* Let HW settle down a bit from whatever init state we
701 * come from */
702 mdelay(2);
703
704 /* Wait for BSY bit to go away, spec timeout is 30 seconds,
705 * I know of at least one disk who takes 31 seconds, I use 35
706 * here to be safe
707 */
708 rc = ide_wait_not_busy(hwif, 35000);
709 if (rc)
710 return rc;
711
712 /* Now make sure both master & slave are ready */
8266105b
JS
713 for (unit = 0; unit < MAX_DRIVES; unit++) {
714 ide_drive_t *drive = &hwif->drives[unit];
715
716 /* Ignore disks that we will not probe for later. */
717 if (!drive->noprobe || drive->present) {
718 SELECT_DRIVE(drive);
374e042c 719 hwif->tp_ops->set_irq(hwif, 1);
8266105b
JS
720 mdelay(2);
721 rc = ide_wait_not_busy(hwif, 35000);
722 if (rc)
723 goto out;
724 } else
725 printk(KERN_DEBUG "%s: ide_wait_not_busy() skipped\n",
726 drive->name);
727 }
728out:
1da177e4 729 /* Exit function with master reselected (let's be sane) */
8266105b
JS
730 if (unit)
731 SELECT_DRIVE(&hwif->drives[0]);
732
1da177e4
LT
733 return rc;
734}
735
736/**
737 * ide_undecoded_slave - look for bad CF adapters
f01393e4 738 * @drive1: drive
1da177e4
LT
739 *
740 * Analyse the drives on the interface and attempt to decide if we
741 * have the same drive viewed twice. This occurs with crap CF adapters
742 * and PCMCIA sometimes.
743 */
744
f01393e4 745void ide_undecoded_slave(ide_drive_t *drive1)
1da177e4 746{
f01393e4 747 ide_drive_t *drive0 = &drive1->hwif->drives[0];
1da177e4 748
f01393e4 749 if ((drive1->dn & 1) == 0 || drive0->present == 0)
1da177e4
LT
750 return;
751
752 /* If the models don't match they are not the same product */
753 if (strcmp(drive0->id->model, drive1->id->model))
754 return;
755
756 /* Serial numbers do not match */
757 if (strncmp(drive0->id->serial_no, drive1->id->serial_no, 20))
758 return;
759
760 /* No serial number, thankfully very rare for CF */
761 if (drive0->id->serial_no[0] == 0)
762 return;
763
764 /* Appears to be an IDE flash adapter with decode bugs */
765 printk(KERN_WARNING "ide-probe: ignoring undecoded slave\n");
766
767 drive1->present = 0;
768}
769
770EXPORT_SYMBOL_GPL(ide_undecoded_slave);
771
9d501529 772static int ide_probe_port(ide_hwif_t *hwif)
1da177e4 773{
1da177e4
LT
774 unsigned long flags;
775 unsigned int irqd;
a14dc574
BZ
776 int unit, rc = -ENODEV;
777
778 BUG_ON(hwif->present);
1da177e4 779
e53cd458 780 if (hwif->drives[0].noprobe && hwif->drives[1].noprobe)
9d501529 781 return -EACCES;
1da177e4 782
1da177e4
LT
783 /*
784 * We must always disable IRQ, as probe_for_drive will assert IRQ, but
785 * we'll install our IRQ driver much later...
786 */
787 irqd = hwif->irq;
788 if (irqd)
789 disable_irq(hwif->irq);
790
791 local_irq_set(flags);
792
c860a8f2 793 if (ide_port_wait_ready(hwif) == -EBUSY)
1da177e4
LT
794 printk(KERN_DEBUG "%s: Wait for ready failed before probe !\n", hwif->name);
795
796 /*
f367bed0
BZ
797 * Second drive should only exist if first drive was found,
798 * but a lot of cdrom drives are configured as single slaves.
1da177e4 799 */
f367bed0 800 for (unit = 0; unit < MAX_DRIVES; ++unit) {
1da177e4
LT
801 ide_drive_t *drive = &hwif->drives[unit];
802 drive->dn = (hwif->channel ? 2 : 0) + unit;
803 (void) probe_for_drive(drive);
a14dc574
BZ
804 if (drive->present)
805 rc = 0;
1da177e4 806 }
e460a597 807
1da177e4 808 local_irq_restore(flags);
e460a597 809
1da177e4
LT
810 /*
811 * Use cached IRQ number. It might be (and is...) changed by probe
812 * code above
813 */
814 if (irqd)
815 enable_irq(irqd);
816
a14dc574 817 return rc;
e84e7ea7
BZ
818}
819
820static void ide_port_tune_devices(ide_hwif_t *hwif)
821{
ac95beed 822 const struct ide_port_ops *port_ops = hwif->port_ops;
e84e7ea7
BZ
823 int unit;
824
f01393e4
BZ
825 for (unit = 0; unit < MAX_DRIVES; unit++) {
826 ide_drive_t *drive = &hwif->drives[unit];
827
ac95beed
BZ
828 if (drive->present && port_ops && port_ops->quirkproc)
829 port_ops->quirkproc(drive);
f01393e4 830 }
0380dad4 831
1da177e4
LT
832 for (unit = 0; unit < MAX_DRIVES; ++unit) {
833 ide_drive_t *drive = &hwif->drives[unit];
834
835 if (drive->present) {
207daeaa 836 ide_set_max_pio(drive);
1da177e4 837
1da177e4
LT
838 drive->nice1 = 1;
839
5e37bdc0 840 if (hwif->dma_ops)
8c0697cc 841 ide_set_dma(drive);
1da177e4
LT
842 }
843 }
208a08f7
KG
844
845 for (unit = 0; unit < MAX_DRIVES; ++unit) {
846 ide_drive_t *drive = &hwif->drives[unit];
847
807b90d0 848 if (hwif->host_flags & IDE_HFLAG_NO_IO_32BIT)
208a08f7
KG
849 drive->no_io_32bit = 1;
850 else
851 drive->no_io_32bit = drive->id->dword_io ? 1 : 0;
852 }
1da177e4
LT
853}
854
1da177e4
LT
855#if MAX_HWIFS > 1
856/*
857 * save_match() is used to simplify logic in init_irq() below.
858 *
859 * A loophole here is that we may not know about a particular
860 * hwif's irq until after that hwif is actually probed/initialized..
861 * This could be a problem for the case where an hwif is on a
862 * dual interface that requires serialization (eg. cmd640) and another
863 * hwif using one of the same irqs is initialized beforehand.
864 *
865 * This routine detects and reports such situations, but does not fix them.
866 */
867static void save_match(ide_hwif_t *hwif, ide_hwif_t *new, ide_hwif_t **match)
868{
869 ide_hwif_t *m = *match;
870
871 if (m && m->hwgroup && m->hwgroup != new->hwgroup) {
872 if (!new->hwgroup)
873 return;
874 printk("%s: potential irq problem with %s and %s\n",
875 hwif->name, new->name, m->name);
876 }
877 if (!m || m->irq != hwif->irq) /* don't undo a prior perfect match */
878 *match = new;
879}
880#endif /* MAX_HWIFS > 1 */
881
882/*
883 * init request queue
884 */
885static int ide_init_queue(ide_drive_t *drive)
886{
165125e1 887 struct request_queue *q;
1da177e4
LT
888 ide_hwif_t *hwif = HWIF(drive);
889 int max_sectors = 256;
890 int max_sg_entries = PRD_ENTRIES;
891
892 /*
893 * Our default set up assumes the normal IDE case,
894 * that is 64K segmenting, standard PRD setup
895 * and LBA28. Some drivers then impose their own
896 * limits and LBA48 we could raise it but as yet
897 * do not.
898 */
1946089a 899
556e58fe 900 q = blk_init_queue_node(do_ide_request, &ide_lock, hwif_to_node(hwif));
1da177e4
LT
901 if (!q)
902 return 1;
903
904 q->queuedata = drive;
905 blk_queue_segment_boundary(q, 0xffff);
906
1da177e4
LT
907 if (hwif->rqsize < max_sectors)
908 max_sectors = hwif->rqsize;
909 blk_queue_max_sectors(q, max_sectors);
910
911#ifdef CONFIG_PCI
912 /* When we have an IOMMU, we may have a problem where pci_map_sg()
913 * creates segments that don't completely match our boundary
914 * requirements and thus need to be broken up again. Because it
915 * doesn't align properly either, we may actually have to break up
916 * to more segments than what was we got in the first place, a max
917 * worst case is twice as many.
918 * This will be fixed once we teach pci_map_sg() about our boundary
919 * requirements, hopefully soon. *FIXME*
920 */
921 if (!PCI_DMA_BUS_IS_PHYS)
922 max_sg_entries >>= 1;
923#endif /* CONFIG_PCI */
924
925 blk_queue_max_hw_segments(q, max_sg_entries);
926 blk_queue_max_phys_segments(q, max_sg_entries);
927
928 /* assign drive queue */
929 drive->queue = q;
930
931 /* needs drive->queue to be set */
932 ide_toggle_bounce(drive, 1);
933
1da177e4
LT
934 return 0;
935}
936
0947e0dc
BZ
937static void ide_add_drive_to_hwgroup(ide_drive_t *drive)
938{
939 ide_hwgroup_t *hwgroup = drive->hwif->hwgroup;
940
941 spin_lock_irq(&ide_lock);
942 if (!hwgroup->drive) {
943 /* first drive for hwgroup. */
944 drive->next = drive;
945 hwgroup->drive = drive;
946 hwgroup->hwif = HWIF(hwgroup->drive);
947 } else {
948 drive->next = hwgroup->drive->next;
949 hwgroup->drive->next = drive;
950 }
951 spin_unlock_irq(&ide_lock);
952}
953
d5bc6592
BZ
954/*
955 * For any present drive:
956 * - allocate the block device queue
957 * - link drive into the hwgroup
958 */
959static void ide_port_setup_devices(ide_hwif_t *hwif)
960{
961 int i;
962
26042d05 963 mutex_lock(&ide_cfg_mtx);
d5bc6592
BZ
964 for (i = 0; i < MAX_DRIVES; i++) {
965 ide_drive_t *drive = &hwif->drives[i];
966
967 if (!drive->present)
968 continue;
969
970 if (ide_init_queue(drive)) {
971 printk(KERN_ERR "ide: failed to init %s\n",
972 drive->name);
973 continue;
974 }
975
976 ide_add_drive_to_hwgroup(drive);
977 }
26042d05 978 mutex_unlock(&ide_cfg_mtx);
d5bc6592
BZ
979}
980
af1cbba3
BZ
981static ide_hwif_t *ide_ports[MAX_HWIFS];
982
6059143a
BZ
983void ide_remove_port_from_hwgroup(ide_hwif_t *hwif)
984{
985 ide_hwgroup_t *hwgroup = hwif->hwgroup;
986
af1cbba3
BZ
987 ide_ports[hwif->index] = NULL;
988
6059143a
BZ
989 spin_lock_irq(&ide_lock);
990 /*
991 * Remove us from the hwgroup, and free
992 * the hwgroup if we were the only member
993 */
994 if (hwif->next == hwif) {
995 BUG_ON(hwgroup->hwif != hwif);
996 kfree(hwgroup);
997 } else {
998 /* There is another interface in hwgroup.
999 * Unlink us, and set hwgroup->drive and ->hwif to
1000 * something sane.
1001 */
1002 ide_hwif_t *g = hwgroup->hwif;
1003
1004 while (g->next != hwif)
1005 g = g->next;
1006 g->next = hwif->next;
1007 if (hwgroup->hwif == hwif) {
1008 /* Chose a random hwif for hwgroup->hwif.
1009 * It's guaranteed that there are no drives
1010 * left in the hwgroup.
1011 */
1012 BUG_ON(hwgroup->drive != NULL);
1013 hwgroup->hwif = g;
1014 }
1015 BUG_ON(hwgroup->hwif == hwif);
1016 }
1017 spin_unlock_irq(&ide_lock);
1018}
1019
1da177e4
LT
1020/*
1021 * This routine sets up the irq for an ide interface, and creates a new
1022 * hwgroup for the irq/hwif if none was previously assigned.
1023 *
1024 * Much of the code is for correctly detecting/handling irq sharing
1025 * and irq serialization situations. This is somewhat complex because
1026 * it handles static as well as dynamic (PCMCIA) IDE interfaces.
1da177e4
LT
1027 */
1028static int init_irq (ide_hwif_t *hwif)
1029{
4c3032d8 1030 struct ide_io_ports *io_ports = &hwif->io_ports;
1da177e4
LT
1031 unsigned int index;
1032 ide_hwgroup_t *hwgroup;
1033 ide_hwif_t *match = NULL;
1034
1035
1036 BUG_ON(in_interrupt());
1037 BUG_ON(irqs_disabled());
556e58fe
RT
1038 BUG_ON(hwif == NULL);
1039
ef29888e 1040 mutex_lock(&ide_cfg_mtx);
1da177e4
LT
1041 hwif->hwgroup = NULL;
1042#if MAX_HWIFS > 1
1043 /*
1044 * Group up with any other hwifs that share our irq(s).
1045 */
1046 for (index = 0; index < MAX_HWIFS; index++) {
af1cbba3
BZ
1047 ide_hwif_t *h = ide_ports[index];
1048
1049 if (h && h->hwgroup) { /* scan only initialized ports */
1da177e4
LT
1050 if (hwif->irq == h->irq) {
1051 hwif->sharing_irq = h->sharing_irq = 1;
1052 if (hwif->chipset != ide_pci ||
1053 h->chipset != ide_pci) {
1054 save_match(hwif, h, &match);
1055 }
1056 }
1057 if (hwif->serialized) {
1058 if (hwif->mate && hwif->mate->irq == h->irq)
1059 save_match(hwif, h, &match);
1060 }
1061 if (h->serialized) {
1062 if (h->mate && hwif->irq == h->mate->irq)
1063 save_match(hwif, h, &match);
1064 }
1065 }
1066 }
1067#endif /* MAX_HWIFS > 1 */
1068 /*
1069 * If we are still without a hwgroup, then form a new one
1070 */
1071 if (match) {
1072 hwgroup = match->hwgroup;
1073 hwif->hwgroup = hwgroup;
1074 /*
1075 * Link us into the hwgroup.
1076 * This must be done early, do ensure that unexpected_intr
1077 * can find the hwif and prevent irq storms.
1078 * No drives are attached to the new hwif, choose_drive
1079 * can't do anything stupid (yet).
1080 * Add ourself as the 2nd entry to the hwgroup->hwif
1081 * linked list, the first entry is the hwif that owns
1082 * hwgroup->handler - do not change that.
1083 */
1084 spin_lock_irq(&ide_lock);
1085 hwif->next = hwgroup->hwif->next;
1086 hwgroup->hwif->next = hwif;
cae5c820 1087 BUG_ON(hwif->next == hwif);
1da177e4
LT
1088 spin_unlock_irq(&ide_lock);
1089 } else {
422278ef
BZ
1090 hwgroup = kmalloc_node(sizeof(*hwgroup), GFP_KERNEL|__GFP_ZERO,
1091 hwif_to_node(hwif));
1092 if (hwgroup == NULL)
1093 goto out_up;
1da177e4
LT
1094
1095 hwif->hwgroup = hwgroup;
422278ef 1096 hwgroup->hwif = hwif->next = hwif;
1da177e4 1097
1da177e4
LT
1098 init_timer(&hwgroup->timer);
1099 hwgroup->timer.function = &ide_timer_expiry;
1100 hwgroup->timer.data = (unsigned long) hwgroup;
1101 }
1102
af1cbba3
BZ
1103 ide_ports[hwif->index] = hwif;
1104
1da177e4
LT
1105 /*
1106 * Allocate the irq, if not already obtained for another hwif
1107 */
1108 if (!match || match->irq != hwif->irq) {
7b5da4be 1109 int sa = 0;
7b892806 1110#if defined(__mc68000__)
362537b9 1111 sa = IRQF_SHARED;
e1771e20 1112#endif /* __mc68000__ */
1da177e4 1113
7b5da4be 1114 if (IDE_CHIPSET_IS_PCI(hwif->chipset))
362537b9 1115 sa = IRQF_SHARED;
1da177e4 1116
4c3032d8 1117 if (io_ports->ctl_addr)
374e042c 1118 hwif->tp_ops->set_irq(hwif, 1);
1da177e4
LT
1119
1120 if (request_irq(hwif->irq,&ide_intr,sa,hwif->name,hwgroup))
1121 goto out_unlink;
1122 }
1123
8a0e7e14
BZ
1124 if (!hwif->rqsize) {
1125 if ((hwif->host_flags & IDE_HFLAG_NO_LBA48) ||
1126 (hwif->host_flags & IDE_HFLAG_NO_LBA48_DMA))
1127 hwif->rqsize = 256;
1128 else
1129 hwif->rqsize = 65536;
1130 }
1131
7b892806 1132#if !defined(__mc68000__)
1da177e4 1133 printk("%s at 0x%03lx-0x%03lx,0x%03lx on irq %d", hwif->name,
4c3032d8
BZ
1134 io_ports->data_addr, io_ports->status_addr,
1135 io_ports->ctl_addr, hwif->irq);
1da177e4
LT
1136#else
1137 printk("%s at 0x%08lx on irq %d", hwif->name,
4c3032d8 1138 io_ports->data_addr, hwif->irq);
7b892806 1139#endif /* __mc68000__ */
1da177e4
LT
1140 if (match)
1141 printk(" (%sed with %s)",
1142 hwif->sharing_irq ? "shar" : "serializ", match->name);
1143 printk("\n");
d5bc6592 1144
ef29888e 1145 mutex_unlock(&ide_cfg_mtx);
1da177e4
LT
1146 return 0;
1147out_unlink:
fbd13088 1148 ide_remove_port_from_hwgroup(hwif);
1da177e4 1149out_up:
ef29888e 1150 mutex_unlock(&ide_cfg_mtx);
1da177e4
LT
1151 return 1;
1152}
1153
1154static int ata_lock(dev_t dev, void *data)
1155{
1156 /* FIXME: we want to pin hwif down */
1157 return 0;
1158}
1159
1160static struct kobject *ata_probe(dev_t dev, int *part, void *data)
1161{
1162 ide_hwif_t *hwif = data;
1163 int unit = *part >> PARTN_BITS;
1164 ide_drive_t *drive = &hwif->drives[unit];
1165 if (!drive->present)
1166 return NULL;
1167
1168 if (drive->media == ide_disk)
1169 request_module("ide-disk");
1170 if (drive->scsi)
1171 request_module("ide-scsi");
1172 if (drive->media == ide_cdrom || drive->media == ide_optical)
1173 request_module("ide-cd");
1174 if (drive->media == ide_tape)
1175 request_module("ide-tape");
1176 if (drive->media == ide_floppy)
1177 request_module("ide-floppy");
1178
1179 return NULL;
1180}
1181
1182static struct kobject *exact_match(dev_t dev, int *part, void *data)
1183{
1184 struct gendisk *p = data;
1185 *part &= (1 << PARTN_BITS) - 1;
edfaa7c3 1186 return &p->dev.kobj;
1da177e4
LT
1187}
1188
1189static int exact_lock(dev_t dev, void *data)
1190{
1191 struct gendisk *p = data;
1192
1193 if (!get_disk(p))
1194 return -1;
1195 return 0;
1196}
1197
1198void ide_register_region(struct gendisk *disk)
1199{
1200 blk_register_region(MKDEV(disk->major, disk->first_minor),
1201 disk->minors, NULL, exact_match, exact_lock, disk);
1202}
1203
1204EXPORT_SYMBOL_GPL(ide_register_region);
1205
1206void ide_unregister_region(struct gendisk *disk)
1207{
1208 blk_unregister_region(MKDEV(disk->major, disk->first_minor),
1209 disk->minors);
1210}
1211
1212EXPORT_SYMBOL_GPL(ide_unregister_region);
1213
1214void ide_init_disk(struct gendisk *disk, ide_drive_t *drive)
1215{
1216 ide_hwif_t *hwif = drive->hwif;
1217 unsigned int unit = (drive->select.all >> 4) & 1;
1218
1219 disk->major = hwif->major;
1220 disk->first_minor = unit << PARTN_BITS;
1221 sprintf(disk->disk_name, "hd%c", 'a' + hwif->index * MAX_DRIVES + unit);
1222 disk->queue = drive->queue;
1223}
1224
1225EXPORT_SYMBOL_GPL(ide_init_disk);
1226
8604affd
BZ
1227static void ide_remove_drive_from_hwgroup(ide_drive_t *drive)
1228{
1229 ide_hwgroup_t *hwgroup = drive->hwif->hwgroup;
1230
1231 if (drive == drive->next) {
1232 /* special case: last drive from hwgroup. */
1233 BUG_ON(hwgroup->drive != drive);
1234 hwgroup->drive = NULL;
1235 } else {
1236 ide_drive_t *walk;
1237
1238 walk = hwgroup->drive;
1239 while (walk->next != drive)
1240 walk = walk->next;
1241 walk->next = drive->next;
1242 if (hwgroup->drive == drive) {
1243 hwgroup->drive = drive->next;
1244 hwgroup->hwif = hwgroup->drive->hwif;
1245 }
1246 }
1247 BUG_ON(hwgroup->drive == drive);
1248}
1249
1da177e4
LT
1250static void drive_release_dev (struct device *dev)
1251{
1252 ide_drive_t *drive = container_of(dev, ide_drive_t, gendev);
1253
5b0c4b30
BZ
1254 ide_proc_unregister_device(drive);
1255
8604affd 1256 spin_lock_irq(&ide_lock);
8604affd 1257 ide_remove_drive_from_hwgroup(drive);
6044ec88
JJ
1258 kfree(drive->id);
1259 drive->id = NULL;
8604affd
BZ
1260 drive->present = 0;
1261 /* Messed up locking ... */
1262 spin_unlock_irq(&ide_lock);
1263 blk_cleanup_queue(drive->queue);
1264 spin_lock_irq(&ide_lock);
1265 drive->queue = NULL;
1266 spin_unlock_irq(&ide_lock);
1267
f36d4024 1268 complete(&drive->gendev_rel_comp);
1da177e4
LT
1269}
1270
1da177e4
LT
1271static int hwif_init(ide_hwif_t *hwif)
1272{
1273 int old_irq;
1274
1da177e4 1275 if (!hwif->irq) {
a861beb1 1276 hwif->irq = __ide_default_irq(hwif->io_ports.data_addr);
4c3032d8 1277 if (!hwif->irq) {
1da177e4 1278 printk("%s: DISABLED, NO IRQ\n", hwif->name);
48535651 1279 return 0;
1da177e4
LT
1280 }
1281 }
1da177e4 1282
1da177e4
LT
1283 if (register_blkdev(hwif->major, hwif->name))
1284 return 0;
1285
1286 if (!hwif->sg_max_nents)
1287 hwif->sg_max_nents = PRD_ENTRIES;
1288
45711f1a 1289 hwif->sg_table = kmalloc(sizeof(struct scatterlist)*hwif->sg_max_nents,
1da177e4
LT
1290 GFP_KERNEL);
1291 if (!hwif->sg_table) {
1292 printk(KERN_ERR "%s: unable to allocate SG table.\n", hwif->name);
1293 goto out;
1294 }
45711f1a
JA
1295
1296 sg_init_table(hwif->sg_table, hwif->sg_max_nents);
1da177e4
LT
1297
1298 if (init_irq(hwif) == 0)
1299 goto done;
1300
1301 old_irq = hwif->irq;
1302 /*
1303 * It failed to initialise. Find the default IRQ for
1304 * this port and try that.
1305 */
a861beb1 1306 hwif->irq = __ide_default_irq(hwif->io_ports.data_addr);
4c3032d8 1307 if (!hwif->irq) {
1da177e4
LT
1308 printk("%s: Disabled unable to get IRQ %d.\n",
1309 hwif->name, old_irq);
1310 goto out;
1311 }
1312 if (init_irq(hwif)) {
1313 printk("%s: probed IRQ %d and default IRQ %d failed.\n",
1314 hwif->name, old_irq, hwif->irq);
1315 goto out;
1316 }
1317 printk("%s: probed IRQ %d failed, using default.\n",
1318 hwif->name, hwif->irq);
1319
1320done:
3a4e7c96
BZ
1321 blk_register_region(MKDEV(hwif->major, 0), MAX_DRIVES << PARTN_BITS,
1322 THIS_MODULE, ata_probe, ata_lock, hwif);
1da177e4
LT
1323 return 1;
1324
1325out:
1326 unregister_blkdev(hwif->major, hwif->name);
1327 return 0;
1328}
1329
9601a607
BZ
1330static void hwif_register_devices(ide_hwif_t *hwif)
1331{
1332 unsigned int i;
1333
1334 for (i = 0; i < MAX_DRIVES; i++) {
1335 ide_drive_t *drive = &hwif->drives[i];
c5d70cc7
BZ
1336 struct device *dev = &drive->gendev;
1337 int ret;
9601a607 1338
c5d70cc7
BZ
1339 if (!drive->present)
1340 continue;
9601a607 1341
c5d70cc7
BZ
1342 ide_add_generic_settings(drive);
1343
1344 snprintf(dev->bus_id, BUS_ID_SIZE, "%u.%u", hwif->index, i);
1345 dev->parent = &hwif->gendev;
1346 dev->bus = &ide_bus_type;
1347 dev->driver_data = drive;
1348 dev->release = drive_release_dev;
1349
1350 ret = device_register(dev);
1351 if (ret < 0)
1352 printk(KERN_WARNING "IDE: %s: device_register error: "
1353 "%d\n", __func__, ret);
9601a607
BZ
1354 }
1355}
1356
7704ca2a
BZ
1357static void ide_port_init_devices(ide_hwif_t *hwif)
1358{
ac95beed 1359 const struct ide_port_ops *port_ops = hwif->port_ops;
7704ca2a
BZ
1360 int i;
1361
1362 for (i = 0; i < MAX_DRIVES; i++) {
1363 ide_drive_t *drive = &hwif->drives[i];
1364
1365 if (hwif->host_flags & IDE_HFLAG_IO_32BIT)
1366 drive->io_32bit = 1;
1367 if (hwif->host_flags & IDE_HFLAG_UNMASK_IRQS)
1368 drive->unmask = 1;
807b90d0
BZ
1369 if (hwif->host_flags & IDE_HFLAG_NO_UNMASK_IRQS)
1370 drive->no_unmask = 1;
1f2cf8b0 1371
e6d95bd1
BZ
1372 if (port_ops && port_ops->init_dev)
1373 port_ops->init_dev(drive);
1374 }
7704ca2a
BZ
1375}
1376
c413b9b9
BZ
1377static void ide_init_port(ide_hwif_t *hwif, unsigned int port,
1378 const struct ide_port_info *d)
8447d9d5 1379{
b7691646 1380 hwif->channel = port;
c413b9b9
BZ
1381
1382 if (d->chipset)
1383 hwif->chipset = d->chipset;
1384
1385 if (d->init_iops)
1386 d->init_iops(hwif);
1387
c413b9b9
BZ
1388 if ((!hwif->irq && (d->host_flags & IDE_HFLAG_LEGACY_IRQS)) ||
1389 (d->host_flags & IDE_HFLAG_FORCE_LEGACY_IRQS))
1390 hwif->irq = port ? 15 : 14;
1391
23f8e4bf
BZ
1392 /* ->host_flags may be set by ->init_iops (or even earlier...) */
1393 hwif->host_flags |= d->host_flags;
c413b9b9
BZ
1394 hwif->pio_mask = d->pio_mask;
1395
374e042c
BZ
1396 if (d->tp_ops)
1397 hwif->tp_ops = d->tp_ops;
1398
ac95beed
BZ
1399 /* ->set_pio_mode for DTC2278 is currently limited to port 0 */
1400 if (hwif->chipset != ide_dtc2278 || hwif->channel == 0)
1401 hwif->port_ops = d->port_ops;
1402
c413b9b9
BZ
1403 hwif->swdma_mask = d->swdma_mask;
1404 hwif->mwdma_mask = d->mwdma_mask;
1405 hwif->ultra_mask = d->udma_mask;
1406
b123f56e
BZ
1407 if ((d->host_flags & IDE_HFLAG_NO_DMA) == 0) {
1408 int rc;
1409
1410 if (d->init_dma)
1411 rc = d->init_dma(hwif, d);
1412 else
1413 rc = ide_hwif_setup_dma(hwif, d);
1414
1415 if (rc < 0) {
1416 printk(KERN_INFO "%s: DMA disabled\n", hwif->name);
ebb00fb5 1417 hwif->dma_base = 0;
b123f56e
BZ
1418 hwif->swdma_mask = 0;
1419 hwif->mwdma_mask = 0;
1420 hwif->ultra_mask = 0;
5e37bdc0
BZ
1421 } else if (d->dma_ops)
1422 hwif->dma_ops = d->dma_ops;
b123f56e 1423 }
c413b9b9 1424
1024c5f4
BZ
1425 if ((d->host_flags & IDE_HFLAG_SERIALIZE) ||
1426 ((d->host_flags & IDE_HFLAG_SERIALIZE_DMA) && hwif->dma_base)) {
1427 if (hwif->mate)
1428 hwif->mate->serialized = hwif->serialized = 1;
1429 }
1430
c413b9b9
BZ
1431 if (d->host_flags & IDE_HFLAG_RQSIZE_256)
1432 hwif->rqsize = 256;
1433
1434 /* call chipset specific routine for each enabled port */
1435 if (d->init_hwif)
1436 d->init_hwif(hwif);
c7f6f21a 1437}
bfa14b42 1438
c7f6f21a
BZ
1439static void ide_port_cable_detect(ide_hwif_t *hwif)
1440{
ac95beed
BZ
1441 const struct ide_port_ops *port_ops = hwif->port_ops;
1442
1443 if (port_ops && port_ops->cable_detect && (hwif->ultra_mask & 0x78)) {
bfa14b42 1444 if (hwif->cbl != ATA_CBL_PATA40_SHORT)
ac95beed 1445 hwif->cbl = port_ops->cable_detect(hwif);
bfa14b42 1446 }
c413b9b9
BZ
1447}
1448
f74c9141
BZ
1449static ssize_t store_delete_devices(struct device *portdev,
1450 struct device_attribute *attr,
1451 const char *buf, size_t n)
1452{
1453 ide_hwif_t *hwif = dev_get_drvdata(portdev);
1454
1455 if (strncmp(buf, "1", n))
1456 return -EINVAL;
1457
1458 ide_port_unregister_devices(hwif);
1459
1460 return n;
1461};
1462
1463static DEVICE_ATTR(delete_devices, S_IWUSR, NULL, store_delete_devices);
1464
1465static ssize_t store_scan(struct device *portdev,
1466 struct device_attribute *attr,
1467 const char *buf, size_t n)
1468{
1469 ide_hwif_t *hwif = dev_get_drvdata(portdev);
1470
1471 if (strncmp(buf, "1", n))
1472 return -EINVAL;
1473
1474 ide_port_unregister_devices(hwif);
1475 ide_port_scan(hwif);
1476
1477 return n;
1478};
1479
1480static DEVICE_ATTR(scan, S_IWUSR, NULL, store_scan);
1481
1482static struct device_attribute *ide_port_attrs[] = {
1483 &dev_attr_delete_devices,
1484 &dev_attr_scan,
1485 NULL
1486};
1487
1488static int ide_sysfs_register_port(ide_hwif_t *hwif)
1489{
1490 int i, rc;
1491
1492 for (i = 0; ide_port_attrs[i]; i++) {
1493 rc = device_create_file(hwif->portdev, ide_port_attrs[i]);
1494 if (rc)
1495 break;
1496 }
1497
1498 return rc;
1499}
1500
8cdf3100
BZ
1501static unsigned int ide_indexes;
1502
fe80b937 1503/**
8cdf3100 1504 * ide_find_port_slot - find free port slot
fe80b937
BZ
1505 * @d: IDE port info
1506 *
8cdf3100 1507 * Return the new port slot index or -ENOENT if we are out of free slots.
fe80b937
BZ
1508 */
1509
8cdf3100 1510static int ide_find_port_slot(const struct ide_port_info *d)
fe80b937 1511{
8cdf3100 1512 int idx = -ENOENT;
fe80b937 1513 u8 bootable = (d && (d->host_flags & IDE_HFLAG_NON_BOOTABLE)) ? 0 : 1;
8cdf3100 1514 u8 i = (d && (d->host_flags & IDE_HFLAG_QD_2ND_PORT)) ? 1 : 0;;
fe80b937
BZ
1515
1516 /*
1517 * Claim an unassigned slot.
1518 *
1519 * Give preference to claiming other slots before claiming ide0/ide1,
1520 * just in case there's another interface yet-to-be-scanned
1521 * which uses ports 0x1f0/0x170 (the ide0/ide1 defaults).
1522 *
1523 * Unless there is a bootable card that does not use the standard
1524 * ports 0x1f0/0x170 (the ide0/ide1 defaults).
1525 */
8cdf3100
BZ
1526 mutex_lock(&ide_cfg_mtx);
1527 if (MAX_HWIFS == 1) {
1528 if (ide_indexes == 0 && i == 0)
1529 idx = 1;
fe80b937 1530 } else {
8cdf3100
BZ
1531 if (bootable) {
1532 if ((ide_indexes | i) != (1 << MAX_HWIFS) - 1)
1533 idx = ffz(ide_indexes | i);
1534 } else {
1535 if ((ide_indexes | 3) != (1 << MAX_HWIFS) - 1)
1536 idx = ffz(ide_indexes | 3);
1537 else if ((ide_indexes & 3) != 3)
1538 idx = ffz(ide_indexes);
fe80b937
BZ
1539 }
1540 }
8cdf3100
BZ
1541 if (idx >= 0)
1542 ide_indexes |= (1 << idx);
1543 mutex_unlock(&ide_cfg_mtx);
fe80b937 1544
8cdf3100
BZ
1545 return idx;
1546}
256c5f8e 1547
8cdf3100
BZ
1548static void ide_free_port_slot(int idx)
1549{
1550 mutex_lock(&ide_cfg_mtx);
1551 ide_indexes &= ~(1 << idx);
1552 mutex_unlock(&ide_cfg_mtx);
fe80b937 1553}
fe80b937 1554
48c3c107
BZ
1555struct ide_host *ide_host_alloc_all(const struct ide_port_info *d,
1556 hw_regs_t **hws)
1557{
1558 struct ide_host *host;
1559 int i;
1560
1561 host = kzalloc(sizeof(*host), GFP_KERNEL);
1562 if (host == NULL)
1563 return NULL;
1564
1565 for (i = 0; i < MAX_HWIFS; i++) {
1566 ide_hwif_t *hwif;
8cdf3100 1567 int idx;
48c3c107
BZ
1568
1569 if (hws[i] == NULL)
1570 continue;
1571
18de1017
BZ
1572 hwif = kzalloc(sizeof(*hwif), GFP_KERNEL);
1573 if (hwif == NULL)
1574 continue;
1575
8cdf3100
BZ
1576 idx = ide_find_port_slot(d);
1577 if (idx < 0) {
1578 printk(KERN_ERR "%s: no free slot for interface\n",
1579 d ? d->name : "ide");
18de1017 1580 kfree(hwif);
8cdf3100 1581 continue;
48c3c107 1582 }
8cdf3100 1583
8cdf3100
BZ
1584 ide_init_port_data(hwif, idx);
1585
08da591e
BZ
1586 hwif->host = host;
1587
8cdf3100
BZ
1588 host->ports[i] = hwif;
1589 host->n_ports++;
48c3c107
BZ
1590 }
1591
1592 if (host->n_ports == 0) {
1593 kfree(host);
1594 return NULL;
1595 }
1596
6cdf6eb3
BZ
1597 if (hws[0])
1598 host->dev[0] = hws[0]->dev;
1599
ef0b0427
BZ
1600 if (d)
1601 host->host_flags = d->host_flags;
1602
48c3c107
BZ
1603 return host;
1604}
1605EXPORT_SYMBOL_GPL(ide_host_alloc_all);
1606
1607struct ide_host *ide_host_alloc(const struct ide_port_info *d, hw_regs_t **hws)
1608{
1609 hw_regs_t *hws_all[MAX_HWIFS];
1610 int i;
1611
1612 for (i = 0; i < MAX_HWIFS; i++)
1613 hws_all[i] = (i < 4) ? hws[i] : NULL;
1614
1615 return ide_host_alloc_all(d, hws_all);
1616}
1617EXPORT_SYMBOL_GPL(ide_host_alloc);
1618
1619int ide_host_register(struct ide_host *host, const struct ide_port_info *d,
1620 hw_regs_t **hws)
c413b9b9
BZ
1621{
1622 ide_hwif_t *hwif, *mate = NULL;
e0d00207 1623 int i, j = 0;
8447d9d5 1624
c413b9b9 1625 for (i = 0; i < MAX_HWIFS; i++) {
e0d00207 1626 hwif = host->ports[i];
48c3c107 1627
e0d00207 1628 if (hwif == NULL) {
c413b9b9
BZ
1629 mate = NULL;
1630 continue;
1631 }
1632
c97c6aca 1633 ide_init_port_hw(hwif, hws[i]);
9fd91d95
BZ
1634 ide_port_apply_params(hwif);
1635
1636 if (d == NULL) {
1637 mate = NULL;
1638 continue;
1639 }
1640
b7691646 1641 if ((i & 1) && mate) {
c413b9b9
BZ
1642 hwif->mate = mate;
1643 mate->mate = hwif;
1644 }
1645
1646 mate = (i & 1) ? NULL : hwif;
1647
1648 ide_init_port(hwif, i & 1, d);
c7f6f21a 1649 ide_port_cable_detect(hwif);
7704ca2a 1650 ide_port_init_devices(hwif);
c413b9b9
BZ
1651 }
1652
151575e4 1653 for (i = 0; i < MAX_HWIFS; i++) {
e0d00207 1654 hwif = host->ports[i];
ba6560aa 1655
e0d00207
BZ
1656 if (hwif == NULL)
1657 continue;
139ddfca 1658
eb716beb
BZ
1659 if (ide_probe_port(hwif) == 0)
1660 hwif->present = 1;
a14dc574
BZ
1661
1662 if (hwif->chipset != ide_4drives || !hwif->mate ||
1663 !hwif->mate->present)
1664 ide_register_port(hwif);
1665
eb716beb
BZ
1666 if (hwif->present)
1667 ide_port_tune_devices(hwif);
2e13093a 1668 }
ba6560aa 1669
151575e4 1670 for (i = 0; i < MAX_HWIFS; i++) {
e0d00207 1671 hwif = host->ports[i];
2e13093a 1672
e0d00207
BZ
1673 if (hwif == NULL)
1674 continue;
ba6560aa
BZ
1675
1676 if (hwif_init(hwif) == 0) {
1677 printk(KERN_INFO "%s: failed to initialize IDE "
1678 "interface\n", hwif->name);
48535651 1679 hwif->present = 0;
ba6560aa
BZ
1680 continue;
1681 }
decdc3f0 1682
e0d00207
BZ
1683 j++;
1684
eb716beb
BZ
1685 if (hwif->present)
1686 ide_port_setup_devices(hwif);
26042d05 1687
decdc3f0 1688 ide_acpi_init(hwif);
eb716beb
BZ
1689
1690 if (hwif->present)
1691 ide_acpi_port_init_devices(hwif);
2e13093a
BZ
1692 }
1693
151575e4 1694 for (i = 0; i < MAX_HWIFS; i++) {
e0d00207 1695 hwif = host->ports[i];
2e13093a 1696
e0d00207
BZ
1697 if (hwif == NULL)
1698 continue;
ba6560aa 1699
eb716beb
BZ
1700 if (hwif->chipset == ide_unknown)
1701 hwif->chipset = ide_generic;
1702
1703 if (hwif->present)
ba6560aa 1704 hwif_register_devices(hwif);
8447d9d5
BZ
1705 }
1706
151575e4 1707 for (i = 0; i < MAX_HWIFS; i++) {
e0d00207 1708 hwif = host->ports[i];
327617e1 1709
e0d00207
BZ
1710 if (hwif == NULL)
1711 continue;
327617e1 1712
eb716beb
BZ
1713 ide_sysfs_register_port(hwif);
1714 ide_proc_register_port(hwif);
1715
1716 if (hwif->present)
d9270a3f 1717 ide_proc_port_register_devices(hwif);
8447d9d5
BZ
1718 }
1719
e0d00207 1720 return j ? 0 : -1;
8447d9d5 1721}
48c3c107 1722EXPORT_SYMBOL_GPL(ide_host_register);
151575e4 1723
6f904d01
BZ
1724int ide_host_add(const struct ide_port_info *d, hw_regs_t **hws,
1725 struct ide_host **hostp)
1726{
1727 struct ide_host *host;
8a69580e 1728 int rc;
6f904d01
BZ
1729
1730 host = ide_host_alloc(d, hws);
1731 if (host == NULL)
1732 return -ENOMEM;
1733
8a69580e
BZ
1734 rc = ide_host_register(host, d, hws);
1735 if (rc) {
1736 ide_host_free(host);
1737 return rc;
1738 }
6f904d01
BZ
1739
1740 if (hostp)
1741 *hostp = host;
1742
1743 return 0;
1744}
1745EXPORT_SYMBOL_GPL(ide_host_add);
1746
8a69580e 1747void ide_host_free(struct ide_host *host)
151575e4 1748{
8cdf3100 1749 ide_hwif_t *hwif;
151575e4 1750 int i;
8447d9d5 1751
c97c6aca 1752 for (i = 0; i < MAX_HWIFS; i++) {
8cdf3100
BZ
1753 hwif = host->ports[i];
1754
1755 if (hwif == NULL)
1756 continue;
1757
8cdf3100 1758 ide_free_port_slot(hwif->index);
18de1017 1759 kfree(hwif);
c97c6aca 1760 }
151575e4 1761
48c3c107 1762 kfree(host);
151575e4 1763}
8a69580e
BZ
1764EXPORT_SYMBOL_GPL(ide_host_free);
1765
1766void ide_host_remove(struct ide_host *host)
1767{
1768 int i;
1769
1770 for (i = 0; i < MAX_HWIFS; i++) {
1771 if (host->ports[i])
1772 ide_unregister(host->ports[i]);
1773 }
1774
1775 ide_host_free(host);
1776}
48c3c107 1777EXPORT_SYMBOL_GPL(ide_host_remove);
2dde7861
BZ
1778
1779void ide_port_scan(ide_hwif_t *hwif)
1780{
9fd91d95 1781 ide_port_apply_params(hwif);
2dde7861
BZ
1782 ide_port_cable_detect(hwif);
1783 ide_port_init_devices(hwif);
1784
1785 if (ide_probe_port(hwif) < 0)
1786 return;
1787
1788 hwif->present = 1;
1789
1790 ide_port_tune_devices(hwif);
1791 ide_acpi_port_init_devices(hwif);
1792 ide_port_setup_devices(hwif);
1793 hwif_register_devices(hwif);
1794 ide_proc_port_register_devices(hwif);
1795}
1796EXPORT_SYMBOL_GPL(ide_port_scan);
3b36f66b 1797
48c3c107 1798static void ide_legacy_init_one(hw_regs_t **hws, hw_regs_t *hw,
c97c6aca 1799 u8 port_no, const struct ide_port_info *d,
d9b819a0 1800 unsigned long config)
3b36f66b 1801{
d9b819a0
BZ
1802 unsigned long base, ctl;
1803 int irq;
3b36f66b 1804
d9b819a0
BZ
1805 if (port_no == 0) {
1806 base = 0x1f0;
1807 ctl = 0x3f6;
1808 irq = 14;
1809 } else {
1810 base = 0x170;
1811 ctl = 0x376;
1812 irq = 15;
1813 }
3b36f66b 1814
d92f1a28
BZ
1815 if (!request_region(base, 8, d->name)) {
1816 printk(KERN_ERR "%s: I/O resource 0x%lX-0x%lX not free.\n",
1817 d->name, base, base + 7);
1818 return;
1819 }
1820
1821 if (!request_region(ctl, 1, d->name)) {
1822 printk(KERN_ERR "%s: I/O resource 0x%lX not free.\n",
1823 d->name, ctl);
1824 release_region(base, 8);
1825 return;
1826 }
1827
d9b819a0
BZ
1828 ide_std_init_ports(hw, base, ctl);
1829 hw->irq = irq;
d427e836 1830 hw->chipset = d->chipset;
d6276b5f 1831 hw->config = config;
3b36f66b 1832
48c3c107 1833 hws[port_no] = hw;
d9b819a0 1834}
3b36f66b 1835
d9b819a0
BZ
1836int ide_legacy_device_add(const struct ide_port_info *d, unsigned long config)
1837{
c97c6aca 1838 hw_regs_t hw[2], *hws[] = { NULL, NULL, NULL, NULL };
0bfeee7d 1839
d9b819a0
BZ
1840 memset(&hw, 0, sizeof(hw));
1841
1842 if ((d->host_flags & IDE_HFLAG_QD_2ND_PORT) == 0)
48c3c107
BZ
1843 ide_legacy_init_one(hws, &hw[0], 0, d, config);
1844 ide_legacy_init_one(hws, &hw[1], 1, d, config);
d9b819a0 1845
48c3c107 1846 if (hws[0] == NULL && hws[1] == NULL &&
d9b819a0
BZ
1847 (d->host_flags & IDE_HFLAG_SINGLE))
1848 return -ENOENT;
3b36f66b 1849
6f904d01 1850 return ide_host_add(d, hws, NULL);
3b36f66b
BZ
1851}
1852EXPORT_SYMBOL_GPL(ide_legacy_device_add);
This page took 0.471751 seconds and 5 git commands to generate.