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