1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20#include <linux/module.h>
21#include <linux/types.h>
22#include <linux/string.h>
23#include <linux/kernel.h>
24#include <linux/timer.h>
25#include <linux/mm.h>
26#include <linux/interrupt.h>
27#include <linux/major.h>
28#include <linux/errno.h>
29#include <linux/genhd.h>
30#include <linux/slab.h>
31#include <linux/delay.h>
32#include <linux/ide.h>
33#include <linux/spinlock.h>
34#include <linux/kmod.h>
35#include <linux/pci.h>
36#include <linux/scatterlist.h>
37
38#include <asm/byteorder.h>
39#include <asm/irq.h>
40#include <asm/uaccess.h>
41#include <asm/io.h>
42
43
44
45
46
47
48
49
50
51
52static void generic_id(ide_drive_t *drive)
53{
54 drive->id->cyls = drive->cyl;
55 drive->id->heads = drive->head;
56 drive->id->sectors = drive->sect;
57 drive->id->cur_cyls = drive->cyl;
58 drive->id->cur_heads = drive->head;
59 drive->id->cur_sectors = drive->sect;
60}
61
62static void ide_disk_init_chs(ide_drive_t *drive)
63{
64 struct hd_driveid *id = drive->id;
65
66
67 if (!drive->cyl || !drive->head || !drive->sect) {
68 drive->cyl = drive->bios_cyl = id->cyls;
69 drive->head = drive->bios_head = id->heads;
70 drive->sect = drive->bios_sect = id->sectors;
71 }
72
73
74 if ((id->field_valid & 1) && id->cur_cyls &&
75 id->cur_heads && (id->cur_heads <= 16) && id->cur_sectors) {
76 drive->cyl = id->cur_cyls;
77 drive->head = id->cur_heads;
78 drive->sect = id->cur_sectors;
79 }
80
81
82 if (drive->head > 16 && id->heads && id->heads <= 16) {
83 drive->cyl = id->cyls;
84 drive->head = id->heads;
85 drive->sect = id->sectors;
86 }
87}
88
89static void ide_disk_init_mult_count(ide_drive_t *drive)
90{
91 struct hd_driveid *id = drive->id;
92
93 drive->mult_count = 0;
94 if (id->max_multsect) {
95#ifdef CONFIG_IDEDISK_MULTI_MODE
96 id->multsect = ((id->max_multsect/2) > 1) ? id->max_multsect : 0;
97 id->multsect_valid = id->multsect ? 1 : 0;
98 drive->mult_req = id->multsect_valid ? id->max_multsect : INITIAL_MULT_COUNT;
99 drive->special.b.set_multmode = drive->mult_req ? 1 : 0;
100#else
101 drive->mult_req = INITIAL_MULT_COUNT;
102 if (drive->mult_req > id->max_multsect)
103 drive->mult_req = id->max_multsect;
104 if (drive->mult_req || ((id->multsect_valid & 1) && id->multsect))
105 drive->special.b.set_multmode = 1;
106#endif
107 }
108}
109
110
111
112
113
114
115
116
117
118
119
120static inline void do_identify (ide_drive_t *drive, u8 cmd)
121{
122 ide_hwif_t *hwif = HWIF(drive);
123 int bswap = 1;
124 struct hd_driveid *id;
125
126 id = drive->id;
127
128 hwif->ata_input_data(drive, id, SECTOR_WORDS);
129
130 drive->id_read = 1;
131 local_irq_enable();
132 ide_fix_driveid(id);
133
134#if defined (CONFIG_SCSI_EATA_PIO) || defined (CONFIG_SCSI_EATA)
135
136
137
138
139 if ((id->model[0] == 'P' && id->model[1] == 'M') ||
140 (id->model[0] == 'S' && id->model[1] == 'K')) {
141 printk("%s: EATA SCSI HBA %.10s\n", drive->name, id->model);
142 goto err_misc;
143 }
144#endif
145
146
147
148
149
150 if (cmd == WIN_PIDENTIFY) {
151 if ((id->model[0] == 'N' && id->model[1] == 'E')
152 || (id->model[0] == 'F' && id->model[1] == 'X')
153 || (id->model[0] == 'P' && id->model[1] == 'i'))
154
155 bswap ^= 1;
156 }
157 ide_fixstring(id->model, sizeof(id->model), bswap);
158 ide_fixstring(id->fw_rev, sizeof(id->fw_rev), bswap);
159 ide_fixstring(id->serial_no, sizeof(id->serial_no), bswap);
160
161
162 id->model[sizeof(id->model)-1] = '\0';
163
164 if (strstr(id->model, "E X A B Y T E N E S T"))
165 goto err_misc;
166
167 printk("%s: %s, ", drive->name, id->model);
168 drive->present = 1;
169 drive->dead = 0;
170
171
172
173
174 if (cmd == WIN_PIDENTIFY) {
175 u8 type = (id->config >> 8) & 0x1f;
176 printk("ATAPI ");
177 switch (type) {
178 case ide_floppy:
179 if (!strstr(id->model, "CD-ROM")) {
180 if (!strstr(id->model, "oppy") &&
181 !strstr(id->model, "poyp") &&
182 !strstr(id->model, "ZIP"))
183 printk("cdrom or floppy?, assuming ");
184 if (drive->media != ide_cdrom) {
185 printk ("FLOPPY");
186 drive->removable = 1;
187 break;
188 }
189 }
190
191 type = ide_cdrom;
192 case ide_cdrom:
193 drive->removable = 1;
194#ifdef CONFIG_PPC
195
196 if (!strstr(id->model, "CD-ROM") &&
197 strstr(id->model, "ZIP")) {
198 printk ("FLOPPY");
199 type = ide_floppy;
200 break;
201 }
202#endif
203 printk ("CD/DVD-ROM");
204 break;
205 case ide_tape:
206 printk ("TAPE");
207 break;
208 case ide_optical:
209 printk ("OPTICAL");
210 drive->removable = 1;
211 break;
212 default:
213 printk("UNKNOWN (type %d)", type);
214 break;
215 }
216 printk (" drive\n");
217 drive->media = type;
218
219 drive->ready_stat = 0;
220 return;
221 }
222
223
224
225
226
227
228
229
230
231
232 if ((id->config != 0x848a) && (id->config & (1<<7)))
233 drive->removable = 1;
234
235 drive->media = ide_disk;
236 printk("%s DISK drive\n", (id->config == 0x848a) ? "CFA" : "ATA" );
237 QUIRK_LIST(drive);
238 return;
239
240err_misc:
241 kfree(id);
242 drive->present = 0;
243 return;
244}
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261static int actual_try_to_identify (ide_drive_t *drive, u8 cmd)
262{
263 ide_hwif_t *hwif = HWIF(drive);
264 int rc;
265 unsigned long hd_status;
266 unsigned long timeout;
267 u8 s = 0, a = 0;
268
269
270 msleep(50);
271
272 if (IDE_CONTROL_REG) {
273 a = hwif->INB(IDE_ALTSTATUS_REG);
274 s = hwif->INB(IDE_STATUS_REG);
275 if ((a ^ s) & ~INDEX_STAT) {
276 printk(KERN_INFO "%s: probing with STATUS(0x%02x) instead of "
277 "ALTSTATUS(0x%02x)\n", drive->name, s, a);
278
279 hd_status = IDE_STATUS_REG;
280 } else {
281
282 hd_status = IDE_ALTSTATUS_REG;
283 }
284 } else
285 hd_status = IDE_STATUS_REG;
286
287
288
289
290 if ((cmd == WIN_PIDENTIFY))
291
292 hwif->OUTB(0, IDE_FEATURE_REG);
293
294
295 hwif->OUTB(cmd, IDE_COMMAND_REG);
296
297 timeout = ((cmd == WIN_IDENTIFY) ? WAIT_WORSTCASE : WAIT_PIDENTIFY) / 2;
298 timeout += jiffies;
299 do {
300 if (time_after(jiffies, timeout)) {
301
302 return 1;
303 }
304
305 msleep(50);
306 } while ((hwif->INB(hd_status)) & BUSY_STAT);
307
308
309 msleep(50);
310 if (OK_STAT((hwif->INB(IDE_STATUS_REG)), DRQ_STAT, BAD_R_STAT)) {
311 unsigned long flags;
312
313
314 local_irq_save(flags);
315
316 do_identify(drive, cmd);
317
318 rc = 0;
319
320 (void) hwif->INB(IDE_STATUS_REG);
321 local_irq_restore(flags);
322 } else {
323
324 rc = 2;
325 }
326 return rc;
327}
328
329
330
331
332
333
334
335
336
337
338
339static int try_to_identify (ide_drive_t *drive, u8 cmd)
340{
341 ide_hwif_t *hwif = HWIF(drive);
342 int retval;
343 int autoprobe = 0;
344 unsigned long cookie = 0;
345
346
347
348
349
350
351
352 if (IDE_CONTROL_REG) {
353 u8 ctl = drive->ctl | 2;
354 if (!hwif->irq) {
355 autoprobe = 1;
356 cookie = probe_irq_on();
357
358 ctl &= ~2;
359 }
360 hwif->OUTB(ctl, IDE_CONTROL_REG);
361 }
362
363 retval = actual_try_to_identify(drive, cmd);
364
365 if (autoprobe) {
366 int irq;
367
368 hwif->OUTB(drive->ctl|2, IDE_CONTROL_REG);
369
370 (void) hwif->INB(IDE_STATUS_REG);
371 udelay(5);
372 irq = probe_irq_off(cookie);
373 if (!hwif->irq) {
374 if (irq > 0) {
375 hwif->irq = irq;
376 } else {
377
378
379
380 printk("%s: IRQ probe failed (0x%lx)\n",
381 drive->name, cookie);
382 }
383 }
384 }
385 return retval;
386}
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410static int do_probe (ide_drive_t *drive, u8 cmd)
411{
412 int rc;
413 ide_hwif_t *hwif = HWIF(drive);
414
415 if (drive->present) {
416
417 if ((drive->media != ide_disk) && (cmd == WIN_IDENTIFY))
418 return 4;
419 }
420#ifdef DEBUG
421 printk("probing for %s: present=%d, media=%d, probetype=%s\n",
422 drive->name, drive->present, drive->media,
423 (cmd == WIN_IDENTIFY) ? "ATA" : "ATAPI");
424#endif
425
426
427
428
429 msleep(50);
430 SELECT_DRIVE(drive);
431 msleep(50);
432 if (hwif->INB(IDE_SELECT_REG) != drive->select.all && !drive->present) {
433 if (drive->select.b.unit != 0) {
434
435 SELECT_DRIVE(&hwif->drives[0]);
436
437 msleep(50);
438 }
439
440 return 3;
441 }
442
443 if (OK_STAT((hwif->INB(IDE_STATUS_REG)), READY_STAT, BUSY_STAT) ||
444 drive->present || cmd == WIN_PIDENTIFY) {
445
446 if ((rc = try_to_identify(drive, cmd))) {
447
448 rc = try_to_identify(drive,cmd);
449 }
450 if (hwif->INB(IDE_STATUS_REG) == (BUSY_STAT|READY_STAT))
451 return 4;
452
453 if ((rc == 1 && cmd == WIN_PIDENTIFY) &&
454 ((drive->autotune == IDE_TUNE_DEFAULT) ||
455 (drive->autotune == IDE_TUNE_AUTO))) {
456 unsigned long timeout;
457 printk("%s: no response (status = 0x%02x), "
458 "resetting drive\n", drive->name,
459 hwif->INB(IDE_STATUS_REG));
460 msleep(50);
461 hwif->OUTB(drive->select.all, IDE_SELECT_REG);
462 msleep(50);
463 hwif->OUTB(WIN_SRST, IDE_COMMAND_REG);
464 timeout = jiffies;
465 while (((hwif->INB(IDE_STATUS_REG)) & BUSY_STAT) &&
466 time_before(jiffies, timeout + WAIT_WORSTCASE))
467 msleep(50);
468 rc = try_to_identify(drive, cmd);
469 }
470 if (rc == 1)
471 printk("%s: no response (status = 0x%02x)\n",
472 drive->name, hwif->INB(IDE_STATUS_REG));
473
474 (void) hwif->INB(IDE_STATUS_REG);
475 } else {
476
477 rc = 3;
478 }
479 if (drive->select.b.unit != 0) {
480
481 SELECT_DRIVE(&hwif->drives[0]);
482 msleep(50);
483
484 (void) hwif->INB(IDE_STATUS_REG);
485 }
486 return rc;
487}
488
489
490
491
492static void enable_nest (ide_drive_t *drive)
493{
494 ide_hwif_t *hwif = HWIF(drive);
495 unsigned long timeout;
496
497 printk("%s: enabling %s -- ", hwif->name, drive->id->model);
498 SELECT_DRIVE(drive);
499 msleep(50);
500 hwif->OUTB(EXABYTE_ENABLE_NEST, IDE_COMMAND_REG);
501 timeout = jiffies + WAIT_WORSTCASE;
502 do {
503 if (time_after(jiffies, timeout)) {
504 printk("failed (timeout)\n");
505 return;
506 }
507 msleep(50);
508 } while ((hwif->INB(IDE_STATUS_REG)) & BUSY_STAT);
509
510 msleep(50);
511
512 if (!OK_STAT((hwif->INB(IDE_STATUS_REG)), 0, BAD_STAT)) {
513 printk("failed (status = 0x%02x)\n", hwif->INB(IDE_STATUS_REG));
514 } else {
515 printk("success\n");
516 }
517
518
519 if (do_probe(drive, WIN_IDENTIFY) >= 2) {
520
521 (void) do_probe(drive, WIN_PIDENTIFY);
522 }
523}
524
525
526
527
528
529
530
531
532
533
534
535
536
537static inline u8 probe_for_drive (ide_drive_t *drive)
538{
539
540
541
542
543
544
545
546
547
548 drive->id = kzalloc(SECTOR_WORDS *4, GFP_KERNEL);
549 drive->id_read = 0;
550 if(drive->id == NULL)
551 {
552 printk(KERN_ERR "ide: out of memory for id data.\n");
553 return 0;
554 }
555 strcpy(drive->id->model, "UNKNOWN");
556
557
558 if (!drive->noprobe)
559 {
560
561 if (do_probe(drive, WIN_IDENTIFY) >= 2) {
562
563 (void) do_probe(drive, WIN_PIDENTIFY);
564 }
565 if (!drive->present)
566
567 return 0;
568 if (strstr(drive->id->model, "E X A B Y T E N E S T"))
569 enable_nest(drive);
570
571
572 if (!drive->id_read) {
573 if (drive->media == ide_disk) {
574 printk(KERN_INFO "%s: non-IDE drive, CHS=%d/%d/%d\n",
575 drive->name, drive->cyl,
576 drive->head, drive->sect);
577 } else if (drive->media == ide_cdrom) {
578 printk(KERN_INFO "%s: ATAPI cdrom (?)\n", drive->name);
579 } else {
580
581 printk(KERN_WARNING "%s: Unknown device on bus refused identification. Ignoring.\n", drive->name);
582 drive->present = 0;
583 }
584 }
585
586 }
587 if(!drive->present)
588 return 0;
589
590 if (drive->id_read == 0) {
591 generic_id(drive);
592 return 1;
593 }
594
595 if (drive->media == ide_disk) {
596 ide_disk_init_chs(drive);
597 ide_disk_init_mult_count(drive);
598 }
599
600 return drive->present;
601}
602
603static void hwif_release_dev (struct device *dev)
604{
605 ide_hwif_t *hwif = container_of(dev, ide_hwif_t, gendev);
606
607 complete(&hwif->gendev_rel_comp);
608}
609
610static void hwif_register (ide_hwif_t *hwif)
611{
612 int ret;
613
614
615 strlcpy(hwif->gendev.bus_id,hwif->name,BUS_ID_SIZE);
616 hwif->gendev.driver_data = hwif;
617 if (hwif->gendev.parent == NULL) {
618 if (hwif->pci_dev)
619 hwif->gendev.parent = &hwif->pci_dev->dev;
620 else
621
622 hwif->gendev.parent = NULL;
623 }
624 hwif->gendev.release = hwif_release_dev;
625 ret = device_register(&hwif->gendev);
626 if (ret < 0)
627 printk(KERN_WARNING "IDE: %s: device_register error: %d\n",
628 __FUNCTION__, ret);
629}
630
631static int wait_hwif_ready(ide_hwif_t *hwif)
632{
633 int unit, rc;
634
635 printk(KERN_DEBUG "Probing IDE interface %s...\n", hwif->name);
636
637
638
639 mdelay(2);
640
641
642
643
644
645 rc = ide_wait_not_busy(hwif, 35000);
646 if (rc)
647 return rc;
648
649
650 for (unit = 0; unit < MAX_DRIVES; unit++) {
651 ide_drive_t *drive = &hwif->drives[unit];
652
653
654 if (!drive->noprobe || drive->present) {
655 SELECT_DRIVE(drive);
656 if (IDE_CONTROL_REG)
657 hwif->OUTB(drive->ctl, IDE_CONTROL_REG);
658 mdelay(2);
659 rc = ide_wait_not_busy(hwif, 35000);
660 if (rc)
661 goto out;
662 } else
663 printk(KERN_DEBUG "%s: ide_wait_not_busy() skipped\n",
664 drive->name);
665 }
666out:
667
668 if (unit)
669 SELECT_DRIVE(&hwif->drives[0]);
670
671 return rc;
672}
673
674
675
676
677
678
679
680
681
682
683void ide_undecoded_slave(ide_hwif_t *hwif)
684{
685 ide_drive_t *drive0 = &hwif->drives[0];
686 ide_drive_t *drive1 = &hwif->drives[1];
687
688 if (drive0->present == 0 || drive1->present == 0)
689 return;
690
691
692 if (strcmp(drive0->id->model, drive1->id->model))
693 return;
694
695
696 if (strncmp(drive0->id->serial_no, drive1->id->serial_no, 20))
697 return;
698
699
700 if (drive0->id->serial_no[0] == 0)
701 return;
702
703
704 printk(KERN_WARNING "ide-probe: ignoring undecoded slave\n");
705
706 drive1->present = 0;
707}
708
709EXPORT_SYMBOL_GPL(ide_undecoded_slave);
710
711
712
713
714
715static void probe_hwif(ide_hwif_t *hwif)
716{
717 unsigned long flags;
718 unsigned int irqd;
719 int unit;
720
721 if (hwif->noprobe)
722 return;
723
724 if ((hwif->chipset != ide_4drives || !hwif->mate || !hwif->mate->present) &&
725 (ide_hwif_request_regions(hwif))) {
726 u16 msgout = 0;
727 for (unit = 0; unit < MAX_DRIVES; ++unit) {
728 ide_drive_t *drive = &hwif->drives[unit];
729 if (drive->present) {
730 drive->present = 0;
731 printk(KERN_ERR "%s: ERROR, PORTS ALREADY IN USE\n",
732 drive->name);
733 msgout = 1;
734 }
735 }
736 if (!msgout)
737 printk(KERN_ERR "%s: ports already in use, skipping probe\n",
738 hwif->name);
739 return;
740 }
741
742
743
744
745
746 irqd = hwif->irq;
747 if (irqd)
748 disable_irq(hwif->irq);
749
750 local_irq_set(flags);
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771 if (wait_hwif_ready(hwif) == -EBUSY)
772 printk(KERN_DEBUG "%s: Wait for ready failed before probe !\n", hwif->name);
773
774
775
776
777 for (unit = MAX_DRIVES - 1; unit >= 0; unit--) {
778 ide_drive_t *drive = &hwif->drives[unit];
779 drive->dn = (hwif->channel ? 2 : 0) + unit;
780 (void) probe_for_drive(drive);
781 if (drive->present && !hwif->present) {
782 hwif->present = 1;
783 if (hwif->chipset != ide_4drives ||
784 !hwif->mate ||
785 !hwif->mate->present) {
786 hwif_register(hwif);
787 }
788 }
789 }
790 if (hwif->io_ports[IDE_CONTROL_OFFSET] && hwif->reset) {
791 unsigned long timeout = jiffies + WAIT_WORSTCASE;
792 u8 stat;
793
794 printk(KERN_WARNING "%s: reset\n", hwif->name);
795 hwif->OUTB(12, hwif->io_ports[IDE_CONTROL_OFFSET]);
796 udelay(10);
797 hwif->OUTB(8, hwif->io_ports[IDE_CONTROL_OFFSET]);
798 do {
799 msleep(50);
800 stat = hwif->INB(hwif->io_ports[IDE_STATUS_OFFSET]);
801 } while ((stat & BUSY_STAT) && time_after(timeout, jiffies));
802
803 }
804 local_irq_restore(flags);
805
806
807
808
809 if (irqd)
810 enable_irq(irqd);
811
812 if (!hwif->present) {
813 ide_hwif_release_regions(hwif);
814 return;
815 }
816
817 if (hwif->fixup)
818 hwif->fixup(hwif);
819
820 for (unit = 0; unit < MAX_DRIVES; ++unit) {
821 ide_drive_t *drive = &hwif->drives[unit];
822
823 if (drive->present) {
824 if (drive->autotune == IDE_TUNE_AUTO)
825 ide_set_max_pio(drive);
826
827 if (drive->autotune != IDE_TUNE_DEFAULT &&
828 drive->autotune != IDE_TUNE_AUTO)
829 continue;
830
831 drive->nice1 = 1;
832
833 if (hwif->ide_dma_on) {
834
835
836
837
838
839
840 hwif->dma_off_quietly(drive);
841 ide_set_dma(drive);
842 }
843 }
844 }
845
846 for (unit = 0; unit < MAX_DRIVES; ++unit) {
847 ide_drive_t *drive = &hwif->drives[unit];
848
849 if (hwif->no_io_32bit)
850 drive->no_io_32bit = 1;
851 else
852 drive->no_io_32bit = drive->id->dword_io ? 1 : 0;
853 }
854}
855
856static int hwif_init(ide_hwif_t *hwif);
857static void hwif_register_devices(ide_hwif_t *hwif);
858
859static int probe_hwif_init(ide_hwif_t *hwif)
860{
861 probe_hwif(hwif);
862
863 if (!hwif_init(hwif)) {
864 printk(KERN_INFO "%s: failed to initialize IDE interface\n",
865 hwif->name);
866 return -1;
867 }
868
869 if (hwif->present)
870 hwif_register_devices(hwif);
871
872 return 0;
873}
874
875#if MAX_HWIFS > 1
876
877
878
879
880
881
882
883
884
885
886
887static void save_match(ide_hwif_t *hwif, ide_hwif_t *new, ide_hwif_t **match)
888{
889 ide_hwif_t *m = *match;
890
891 if (m && m->hwgroup && m->hwgroup != new->hwgroup) {
892 if (!new->hwgroup)
893 return;
894 printk("%s: potential irq problem with %s and %s\n",
895 hwif->name, new->name, m->name);
896 }
897 if (!m || m->irq != hwif->irq)
898 *match = new;
899}
900#endif
901
902
903
904
905static int ide_init_queue(ide_drive_t *drive)
906{
907 struct request_queue *q;
908 ide_hwif_t *hwif = HWIF(drive);
909 int max_sectors = 256;
910 int max_sg_entries = PRD_ENTRIES;
911
912
913
914
915
916
917
918
919
920 q = blk_init_queue_node(do_ide_request, &ide_lock, hwif_to_node(hwif));
921 if (!q)
922 return 1;
923
924 q->queuedata = drive;
925 blk_queue_segment_boundary(q, 0xffff);
926
927 if (!hwif->rqsize) {
928 if ((hwif->host_flags & IDE_HFLAG_NO_LBA48) ||
929 (hwif->host_flags & IDE_HFLAG_NO_LBA48_DMA))
930 hwif->rqsize = 256;
931 else
932 hwif->rqsize = 65536;
933 }
934 if (hwif->rqsize < max_sectors)
935 max_sectors = hwif->rqsize;
936 blk_queue_max_sectors(q, max_sectors);
937
938#ifdef CONFIG_PCI
939
940
941
942
943
944
945
946
947
948 if (!PCI_DMA_BUS_IS_PHYS)
949 max_sg_entries >>= 1;
950#endif
951
952 blk_queue_max_hw_segments(q, max_sg_entries);
953 blk_queue_max_phys_segments(q, max_sg_entries);
954
955
956 drive->queue = q;
957
958
959 ide_toggle_bounce(drive, 1);
960
961 return 0;
962}
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977static int init_irq (ide_hwif_t *hwif)
978{
979 unsigned int index;
980 ide_hwgroup_t *hwgroup;
981 ide_hwif_t *match = NULL;
982
983
984 BUG_ON(in_interrupt());
985 BUG_ON(irqs_disabled());
986 BUG_ON(hwif == NULL);
987
988 mutex_lock(&ide_cfg_mtx);
989 hwif->hwgroup = NULL;
990#if MAX_HWIFS > 1
991
992
993
994 for (index = 0; index < MAX_HWIFS; index++) {
995 ide_hwif_t *h = &ide_hwifs[index];
996 if (h->hwgroup) {
997 if (hwif->irq == h->irq) {
998 hwif->sharing_irq = h->sharing_irq = 1;
999 if (hwif->chipset != ide_pci ||
1000 h->chipset != ide_pci) {
1001 save_match(hwif, h, &match);
1002 }
1003 }
1004 if (hwif->serialized) {
1005 if (hwif->mate && hwif->mate->irq == h->irq)
1006 save_match(hwif, h, &match);
1007 }
1008 if (h->serialized) {
1009 if (h->mate && hwif->irq == h->mate->irq)
1010 save_match(hwif, h, &match);
1011 }
1012 }
1013 }
1014#endif
1015
1016
1017
1018 if (match) {
1019 hwgroup = match->hwgroup;
1020 hwif->hwgroup = hwgroup;
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031 spin_lock_irq(&ide_lock);
1032 hwif->next = hwgroup->hwif->next;
1033 hwgroup->hwif->next = hwif;
1034 spin_unlock_irq(&ide_lock);
1035 } else {
1036 hwgroup = kmalloc_node(sizeof(ide_hwgroup_t),
1037 GFP_KERNEL | __GFP_ZERO,
1038 hwif_to_node(hwif->drives[0].hwif));
1039 if (!hwgroup)
1040 goto out_up;
1041
1042 hwif->hwgroup = hwgroup;
1043
1044 hwgroup->hwif = hwif->next = hwif;
1045 hwgroup->rq = NULL;
1046 hwgroup->handler = NULL;
1047 hwgroup->drive = NULL;
1048 hwgroup->busy = 0;
1049 init_timer(&hwgroup->timer);
1050 hwgroup->timer.function = &ide_timer_expiry;
1051 hwgroup->timer.data = (unsigned long) hwgroup;
1052 }
1053
1054
1055
1056
1057 if (!match || match->irq != hwif->irq) {
1058 int sa = IRQF_DISABLED;
1059#if defined(__mc68000__) || defined(CONFIG_APUS)
1060 sa = IRQF_SHARED;
1061#endif
1062
1063 if (IDE_CHIPSET_IS_PCI(hwif->chipset)) {
1064 sa = IRQF_SHARED;
1065#ifndef CONFIG_IDEPCI_SHARE_IRQ
1066 sa |= IRQF_DISABLED;
1067#endif
1068 }
1069
1070 if (hwif->io_ports[IDE_CONTROL_OFFSET])
1071
1072 hwif->OUTB(0x08, hwif->io_ports[IDE_CONTROL_OFFSET]);
1073
1074 if (request_irq(hwif->irq,&ide_intr,sa,hwif->name,hwgroup))
1075 goto out_unlink;
1076 }
1077
1078
1079
1080
1081
1082
1083 for (index = 0; index < MAX_DRIVES; ++index) {
1084 ide_drive_t *drive = &hwif->drives[index];
1085 if (!drive->present)
1086 continue;
1087 if (ide_init_queue(drive)) {
1088 printk(KERN_ERR "ide: failed to init %s\n",drive->name);
1089 continue;
1090 }
1091 spin_lock_irq(&ide_lock);
1092 if (!hwgroup->drive) {
1093
1094 drive->next = drive;
1095 hwgroup->drive = drive;
1096 hwgroup->hwif = HWIF(hwgroup->drive);
1097 } else {
1098 drive->next = hwgroup->drive->next;
1099 hwgroup->drive->next = drive;
1100 }
1101 spin_unlock_irq(&ide_lock);
1102 }
1103
1104#if !defined(__mc68000__) && !defined(CONFIG_APUS)
1105 printk("%s at 0x%03lx-0x%03lx,0x%03lx on irq %d", hwif->name,
1106 hwif->io_ports[IDE_DATA_OFFSET],
1107 hwif->io_ports[IDE_DATA_OFFSET]+7,
1108 hwif->io_ports[IDE_CONTROL_OFFSET], hwif->irq);
1109#else
1110 printk("%s at 0x%08lx on irq %d", hwif->name,
1111 hwif->io_ports[IDE_DATA_OFFSET], hwif->irq);
1112#endif
1113 if (match)
1114 printk(" (%sed with %s)",
1115 hwif->sharing_irq ? "shar" : "serializ", match->name);
1116 printk("\n");
1117 mutex_unlock(&ide_cfg_mtx);
1118 return 0;
1119out_unlink:
1120 spin_lock_irq(&ide_lock);
1121 if (hwif->next == hwif) {
1122 BUG_ON(match);
1123 BUG_ON(hwgroup->hwif != hwif);
1124 kfree(hwgroup);
1125 } else {
1126 ide_hwif_t *g;
1127 g = hwgroup->hwif;
1128 while (g->next != hwif)
1129 g = g->next;
1130 g->next = hwif->next;
1131 if (hwgroup->hwif == hwif) {
1132
1133 printk(KERN_ERR "Duh. Uninitialized hwif listed as active hwif.\n");
1134 hwgroup->hwif = g;
1135 }
1136 BUG_ON(hwgroup->hwif == hwif);
1137 }
1138 spin_unlock_irq(&ide_lock);
1139out_up:
1140 mutex_unlock(&ide_cfg_mtx);
1141 return 1;
1142}
1143
1144static int ata_lock(dev_t dev, void *data)
1145{
1146
1147 return 0;
1148}
1149
1150static struct kobject *ata_probe(dev_t dev, int *part, void *data)
1151{
1152 ide_hwif_t *hwif = data;
1153 int unit = *part >> PARTN_BITS;
1154 ide_drive_t *drive = &hwif->drives[unit];
1155 if (!drive->present)
1156 return NULL;
1157
1158 if (drive->media == ide_disk)
1159 request_module("ide-disk");
1160 if (drive->scsi)
1161 request_module("ide-scsi");
1162 if (drive->media == ide_cdrom || drive->media == ide_optical)
1163 request_module("ide-cd");
1164 if (drive->media == ide_tape)
1165 request_module("ide-tape");
1166 if (drive->media == ide_floppy)
1167 request_module("ide-floppy");
1168
1169 return NULL;
1170}
1171
1172static struct kobject *exact_match(dev_t dev, int *part, void *data)
1173{
1174 struct gendisk *p = data;
1175 *part &= (1 << PARTN_BITS) - 1;
1176 return &p->kobj;
1177}
1178
1179static int exact_lock(dev_t dev, void *data)
1180{
1181 struct gendisk *p = data;
1182
1183 if (!get_disk(p))
1184 return -1;
1185 return 0;
1186}
1187
1188void ide_register_region(struct gendisk *disk)
1189{
1190 blk_register_region(MKDEV(disk->major, disk->first_minor),
1191 disk->minors, NULL, exact_match, exact_lock, disk);
1192}
1193
1194EXPORT_SYMBOL_GPL(ide_register_region);
1195
1196void ide_unregister_region(struct gendisk *disk)
1197{
1198 blk_unregister_region(MKDEV(disk->major, disk->first_minor),
1199 disk->minors);
1200}
1201
1202EXPORT_SYMBOL_GPL(ide_unregister_region);
1203
1204void ide_init_disk(struct gendisk *disk, ide_drive_t *drive)
1205{
1206 ide_hwif_t *hwif = drive->hwif;
1207 unsigned int unit = (drive->select.all >> 4) & 1;
1208
1209 disk->major = hwif->major;
1210 disk->first_minor = unit << PARTN_BITS;
1211 sprintf(disk->disk_name, "hd%c", 'a' + hwif->index * MAX_DRIVES + unit);
1212 disk->queue = drive->queue;
1213}
1214
1215EXPORT_SYMBOL_GPL(ide_init_disk);
1216
1217static void ide_remove_drive_from_hwgroup(ide_drive_t *drive)
1218{
1219 ide_hwgroup_t *hwgroup = drive->hwif->hwgroup;
1220
1221 if (drive == drive->next) {
1222
1223 BUG_ON(hwgroup->drive != drive);
1224 hwgroup->drive = NULL;
1225 } else {
1226 ide_drive_t *walk;
1227
1228 walk = hwgroup->drive;
1229 while (walk->next != drive)
1230 walk = walk->next;
1231 walk->next = drive->next;
1232 if (hwgroup->drive == drive) {
1233 hwgroup->drive = drive->next;
1234 hwgroup->hwif = hwgroup->drive->hwif;
1235 }
1236 }
1237 BUG_ON(hwgroup->drive == drive);
1238}
1239
1240static void drive_release_dev (struct device *dev)
1241{
1242 ide_drive_t *drive = container_of(dev, ide_drive_t, gendev);
1243
1244 spin_lock_irq(&ide_lock);
1245 ide_remove_drive_from_hwgroup(drive);
1246 kfree(drive->id);
1247 drive->id = NULL;
1248 drive->present = 0;
1249
1250 spin_unlock_irq(&ide_lock);
1251 blk_cleanup_queue(drive->queue);
1252 spin_lock_irq(&ide_lock);
1253 drive->queue = NULL;
1254 spin_unlock_irq(&ide_lock);
1255
1256 complete(&drive->gendev_rel_comp);
1257}
1258
1259
1260
1261
1262
1263
1264
1265static void init_gendisk (ide_hwif_t *hwif)
1266{
1267 unsigned int unit;
1268
1269 for (unit = 0; unit < MAX_DRIVES; ++unit) {
1270 ide_drive_t * drive = &hwif->drives[unit];
1271 ide_add_generic_settings(drive);
1272 snprintf(drive->gendev.bus_id,BUS_ID_SIZE,"%u.%u",
1273 hwif->index,unit);
1274 drive->gendev.parent = &hwif->gendev;
1275 drive->gendev.bus = &ide_bus_type;
1276 drive->gendev.driver_data = drive;
1277 drive->gendev.release = drive_release_dev;
1278 }
1279 blk_register_region(MKDEV(hwif->major, 0), MAX_DRIVES << PARTN_BITS,
1280 THIS_MODULE, ata_probe, ata_lock, hwif);
1281}
1282
1283static int hwif_init(ide_hwif_t *hwif)
1284{
1285 int old_irq;
1286
1287
1288 if (!hwif->present)
1289 return 1;
1290
1291 if (!hwif->irq) {
1292 if (!(hwif->irq = ide_default_irq(hwif->io_ports[IDE_DATA_OFFSET])))
1293 {
1294 printk("%s: DISABLED, NO IRQ\n", hwif->name);
1295 return (hwif->present = 0);
1296 }
1297 }
1298#ifdef CONFIG_BLK_DEV_HD
1299 if (hwif->irq == HD_IRQ && hwif->io_ports[IDE_DATA_OFFSET] != HD_DATA) {
1300 printk("%s: CANNOT SHARE IRQ WITH OLD "
1301 "HARDDISK DRIVER (hd.c)\n", hwif->name);
1302 return (hwif->present = 0);
1303 }
1304#endif
1305
1306
1307 hwif->present = 0;
1308
1309 if (register_blkdev(hwif->major, hwif->name))
1310 return 0;
1311
1312 if (!hwif->sg_max_nents)
1313 hwif->sg_max_nents = PRD_ENTRIES;
1314
1315 hwif->sg_table = kmalloc(sizeof(struct scatterlist)*hwif->sg_max_nents,
1316 GFP_KERNEL);
1317 if (!hwif->sg_table) {
1318 printk(KERN_ERR "%s: unable to allocate SG table.\n", hwif->name);
1319 goto out;
1320 }
1321
1322 sg_init_table(hwif->sg_table, hwif->sg_max_nents);
1323
1324 if (init_irq(hwif) == 0)
1325 goto done;
1326
1327 old_irq = hwif->irq;
1328
1329
1330
1331
1332 if (!(hwif->irq = ide_default_irq(hwif->io_ports[IDE_DATA_OFFSET]))) {
1333 printk("%s: Disabled unable to get IRQ %d.\n",
1334 hwif->name, old_irq);
1335 goto out;
1336 }
1337 if (init_irq(hwif)) {
1338 printk("%s: probed IRQ %d and default IRQ %d failed.\n",
1339 hwif->name, old_irq, hwif->irq);
1340 goto out;
1341 }
1342 printk("%s: probed IRQ %d failed, using default.\n",
1343 hwif->name, hwif->irq);
1344
1345done:
1346 init_gendisk(hwif);
1347
1348 ide_acpi_init(hwif);
1349
1350 hwif->present = 1;
1351 return 1;
1352
1353out:
1354 unregister_blkdev(hwif->major, hwif->name);
1355 return 0;
1356}
1357
1358static void hwif_register_devices(ide_hwif_t *hwif)
1359{
1360 unsigned int i;
1361
1362 for (i = 0; i < MAX_DRIVES; i++) {
1363 ide_drive_t *drive = &hwif->drives[i];
1364
1365 if (drive->present) {
1366 int ret = device_register(&drive->gendev);
1367
1368 if (ret < 0)
1369 printk(KERN_WARNING "IDE: %s: "
1370 "device_register error: %d\n",
1371 __FUNCTION__, ret);
1372 }
1373 }
1374}
1375
1376int ideprobe_init (void)
1377{
1378 unsigned int index;
1379 int probe[MAX_HWIFS];
1380
1381 memset(probe, 0, MAX_HWIFS * sizeof(int));
1382 for (index = 0; index < MAX_HWIFS; ++index)
1383 probe[index] = !ide_hwifs[index].present;
1384
1385 for (index = 0; index < MAX_HWIFS; ++index)
1386 if (probe[index])
1387 probe_hwif(&ide_hwifs[index]);
1388 for (index = 0; index < MAX_HWIFS; ++index)
1389 if (probe[index])
1390 hwif_init(&ide_hwifs[index]);
1391 for (index = 0; index < MAX_HWIFS; ++index) {
1392 if (probe[index]) {
1393 ide_hwif_t *hwif = &ide_hwifs[index];
1394 if (!hwif->present)
1395 continue;
1396 if (hwif->chipset == ide_unknown || hwif->chipset == ide_forced)
1397 hwif->chipset = ide_generic;
1398 hwif_register_devices(hwif);
1399 }
1400 }
1401 for (index = 0; index < MAX_HWIFS; ++index)
1402 if (probe[index])
1403 ide_proc_register_port(&ide_hwifs[index]);
1404 return 0;
1405}
1406
1407EXPORT_SYMBOL_GPL(ideprobe_init);
1408
1409int ide_device_add(u8 idx[4])
1410{
1411 int i, rc = 0;
1412
1413 for (i = 0; i < 4; i++) {
1414 if (idx[i] != 0xff)
1415 rc |= probe_hwif_init(&ide_hwifs[idx[i]]);
1416 }
1417
1418 for (i = 0; i < 4; i++) {
1419 if (idx[i] != 0xff)
1420 ide_proc_register_port(&ide_hwifs[idx[i]]);
1421 }
1422
1423 return rc;
1424}
1425
1426EXPORT_SYMBOL_GPL(ide_device_add);
1427