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