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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124#define REVISION "Revision: 7.00beta4-2.4"
125#define VERSION "Id: ide.c 7.00b4 20030520"
126
127#undef REALLY_SLOW_IO
128
129#define _IDE_C
130
131#include <linux/config.h>
132#include <linux/module.h>
133#include <linux/types.h>
134#include <linux/string.h>
135#include <linux/kernel.h>
136#include <linux/timer.h>
137#include <linux/mm.h>
138#include <linux/interrupt.h>
139#include <linux/major.h>
140#include <linux/errno.h>
141#include <linux/genhd.h>
142#include <linux/blkpg.h>
143#include <linux/slab.h>
144#include <linux/init.h>
145#include <linux/pci.h>
146#include <linux/delay.h>
147#include <linux/ide.h>
148#include <linux/devfs_fs_kernel.h>
149#include <linux/completion.h>
150#include <linux/reboot.h>
151
152#include <asm/byteorder.h>
153#include <asm/irq.h>
154#include <asm/uaccess.h>
155#include <asm/io.h>
156#include <asm/bitops.h>
157
158#include "ide_modes.h"
159
160#include <linux/kmod.h>
161
162
163#define IDE_DEFAULT_MAX_FAILURES 1
164
165static const u8 ide_hwif_to_major[] = { IDE0_MAJOR, IDE1_MAJOR,
166 IDE2_MAJOR, IDE3_MAJOR,
167 IDE4_MAJOR, IDE5_MAJOR,
168 IDE6_MAJOR, IDE7_MAJOR,
169 IDE8_MAJOR, IDE9_MAJOR };
170
171static int idebus_parameter;
172static int system_bus_speed;
173static int initializing;
174
175static int ide_scan_direction;
176
177#ifdef CONFIG_IDEDMA_AUTO
178int noautodma = 0;
179#else
180int noautodma = 1;
181#endif
182
183EXPORT_SYMBOL(noautodma);
184
185
186
187
188
189ide_module_t *ide_chipsets;
190ide_module_t *ide_modules;
191ide_module_t *ide_probe;
192
193
194
195
196ide_hwif_t ide_hwifs[MAX_HWIFS];
197
198EXPORT_SYMBOL(ide_hwifs);
199
200ide_devices_t *idedisk;
201ide_devices_t *idecd;
202ide_devices_t *idefloppy;
203ide_devices_t *idetape;
204ide_devices_t *idescsi;
205
206EXPORT_SYMBOL(idedisk);
207EXPORT_SYMBOL(idecd);
208EXPORT_SYMBOL(idefloppy);
209EXPORT_SYMBOL(idetape);
210EXPORT_SYMBOL(idescsi);
211
212extern ide_driver_t idedefault_driver;
213static void setup_driver_defaults (ide_drive_t *drive);
214
215
216
217
218static void init_hwif_data (unsigned int index)
219{
220 unsigned int unit;
221 hw_regs_t hw;
222 ide_hwif_t *hwif = &ide_hwifs[index];
223
224
225 memset(hwif, 0, sizeof(ide_hwif_t));
226 memset(&hw, 0, sizeof(hw_regs_t));
227
228
229 hwif->index = index;
230 ide_init_hwif_ports(&hw, ide_default_io_base(index), 0, &hwif->irq);
231 memcpy(&hwif->hw, &hw, sizeof(hw));
232 memcpy(hwif->io_ports, hw.io_ports, sizeof(hw.io_ports));
233 hwif->noprobe = !hwif->io_ports[IDE_DATA_OFFSET];
234#ifdef CONFIG_BLK_DEV_HD
235 if (hwif->io_ports[IDE_DATA_OFFSET] == HD_DATA)
236 hwif->noprobe = 1;
237#endif
238 hwif->major = ide_hwif_to_major[index];
239 hwif->name[0] = 'i';
240 hwif->name[1] = 'd';
241 hwif->name[2] = 'e';
242 hwif->name[3] = '0' + index;
243 hwif->bus_state = BUSSTATE_ON;
244 hwif->reset_poll= NULL;
245 hwif->pre_reset = NULL;
246
247 hwif->atapi_dma = 0;
248 hwif->ultra_mask = 0x80;
249 hwif->mwdma_mask = 0x80;
250 hwif->swdma_mask = 0x80;
251 hwif->sata = 0;
252
253 default_hwif_iops(hwif);
254 default_hwif_transport(hwif);
255 for (unit = 0; unit < MAX_DRIVES; ++unit) {
256 ide_drive_t *drive = &hwif->drives[unit];
257
258 drive->media = ide_disk;
259 drive->select.all = (unit<<4)|0xa0;
260 drive->hwif = hwif;
261 drive->ctl = 0x08;
262 drive->ready_stat = READY_STAT;
263 drive->bad_wstat = BAD_W_STAT;
264 drive->special.b.recalibrate = 1;
265 drive->special.b.set_geometry = 1;
266 drive->name[0] = 'h';
267 drive->name[1] = 'd';
268 drive->name[2] = 'a' + (index * MAX_DRIVES) + unit;
269 drive->max_failures = IDE_DEFAULT_MAX_FAILURES;
270 drive->using_dma = 0;
271 drive->is_flash = 0;
272 drive->driver = &idedefault_driver;
273 setup_driver_defaults(drive);
274 init_waitqueue_head(&drive->wqueue);
275 }
276}
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295#define MAGIC_COOKIE 0x12345678
296static void __init init_ide_data (void)
297{
298 unsigned int index;
299 static unsigned long magic_cookie = MAGIC_COOKIE;
300
301 if (magic_cookie != MAGIC_COOKIE)
302 return;
303 magic_cookie = 0;
304
305
306 for (index = 0; index < MAX_HWIFS; ++index)
307 init_hwif_data(index);
308
309
310 ide_init_default_hwifs();
311
312 idebus_parameter = 0;
313 system_bus_speed = 0;
314}
315
316
317
318
319
320
321
322
323int ide_system_bus_speed (void)
324{
325 if (!system_bus_speed) {
326 if (idebus_parameter) {
327
328 system_bus_speed = idebus_parameter;
329 } else if (pci_present()) {
330
331 system_bus_speed = 33;
332 } else {
333
334 system_bus_speed = 50;
335 }
336 printk(KERN_INFO "ide: Assuming %dMHz system bus speed "
337 "for PIO modes%s\n", system_bus_speed,
338 idebus_parameter ? "" : "; override with idebus=xx");
339 }
340 return system_bus_speed;
341}
342
343
344
345
346
347unsigned long current_capacity (ide_drive_t *drive)
348{
349 if (!drive->present)
350 return 0;
351 return DRIVER(drive)->capacity(drive);
352}
353
354EXPORT_SYMBOL(current_capacity);
355
356static inline u32 read_24 (ide_drive_t *drive)
357{
358 return (HWIF(drive)->INB(IDE_HCYL_REG)<<16) |
359 (HWIF(drive)->INB(IDE_LCYL_REG)<<8) |
360 HWIF(drive)->INB(IDE_SECTOR_REG);
361}
362
363
364
365
366u8 ide_dump_status (ide_drive_t *drive, const char *msg, u8 stat)
367{
368 ide_hwif_t *hwif = HWIF(drive);
369 unsigned long flags;
370 u8 err = 0;
371
372 local_irq_set(flags);
373 printk(KERN_WARNING "%s: %s: status=0x%02x", drive->name, msg, stat);
374#if FANCY_STATUS_DUMPS
375 printk(" { ");
376 if (stat & BUSY_STAT) {
377 printk("Busy ");
378 } else {
379 if (stat & READY_STAT) printk("DriveReady ");
380 if (stat & WRERR_STAT) printk("DeviceFault ");
381 if (stat & SEEK_STAT) printk("SeekComplete ");
382 if (stat & DRQ_STAT) printk("DataRequest ");
383 if (stat & ECC_STAT) printk("CorrectedError ");
384 if (stat & INDEX_STAT) printk("Index ");
385 if (stat & ERR_STAT) printk("Error ");
386 }
387 printk("}");
388#endif
389 printk("\n");
390 if ((stat & (BUSY_STAT|ERR_STAT)) == ERR_STAT) {
391 err = hwif->INB(IDE_ERROR_REG);
392 printk("%s: %s: error=0x%02x", drive->name, msg, err);
393#if FANCY_STATUS_DUMPS
394 if (drive->media == ide_disk) {
395 printk(" { ");
396 if (err & ABRT_ERR) printk("DriveStatusError ");
397 if (err & ICRC_ERR) printk("Bad%s ", (err & ABRT_ERR) ? "CRC" : "Sector");
398 if (err & ECC_ERR) printk("UncorrectableError ");
399 if (err & ID_ERR) printk("SectorIdNotFound ");
400 if (err & TRK0_ERR) printk("TrackZeroNotFound ");
401 if (err & MARK_ERR) printk("AddrMarkNotFound ");
402 printk("}");
403 if ((err & (BBD_ERR | ABRT_ERR)) == BBD_ERR || (err & (ECC_ERR|ID_ERR|MARK_ERR))) {
404 if ((drive->id->command_set_2 & 0x0400) &&
405 (drive->id->cfs_enable_2 & 0x0400) &&
406 (drive->addressing == 1)) {
407 u64 sectors = 0;
408 u32 high = 0;
409 u32 low = read_24(drive);
410 hwif->OUTB(drive->ctl|0x80, IDE_CONTROL_REG);
411 high = read_24(drive);
412
413 sectors = ((u64)high << 24) | low;
414 printk(", LBAsect=%llu, high=%d, low=%d",
415 (u64) sectors,
416 high, low);
417 } else {
418 u8 cur = hwif->INB(IDE_SELECT_REG);
419 if (cur & 0x40) {
420 printk(", LBAsect=%ld", (unsigned long)
421 ((cur&0xf)<<24)
422 |(hwif->INB(IDE_HCYL_REG)<<16)
423 |(hwif->INB(IDE_LCYL_REG)<<8)
424 | hwif->INB(IDE_SECTOR_REG));
425 } else {
426 printk(", CHS=%d/%d/%d",
427 (hwif->INB(IDE_HCYL_REG)<<8) +
428 hwif->INB(IDE_LCYL_REG),
429 cur & 0xf,
430 hwif->INB(IDE_SECTOR_REG));
431 }
432 }
433 if (HWGROUP(drive) && HWGROUP(drive)->rq)
434 printk(", sector=%ld", HWGROUP(drive)->rq->sector);
435 }
436 }
437#endif
438 printk("\n");
439 }
440 local_irq_restore(flags);
441 return err;
442}
443
444EXPORT_SYMBOL(ide_dump_status);
445
446
447
448
449
450
451
452
453
454
455int ide_revalidate_disk (kdev_t i_rdev)
456{
457 ide_drive_t *drive;
458 ide_hwgroup_t *hwgroup;
459 unsigned int p, major, minor;
460 unsigned long flags;
461
462 if ((drive = ide_info_ptr(i_rdev, 0)) == NULL)
463 return -ENODEV;
464 major = MAJOR(i_rdev);
465 minor = drive->select.b.unit << PARTN_BITS;
466 hwgroup = HWGROUP(drive);
467 spin_lock_irqsave(&io_request_lock, flags);
468 if (drive->busy || (drive->usage > 1)) {
469 spin_unlock_irqrestore(&io_request_lock, flags);
470 return -EBUSY;
471 };
472 drive->busy = 1;
473 MOD_INC_USE_COUNT;
474 spin_unlock_irqrestore(&io_request_lock, flags);
475
476 for (p = 0; p < (1<<PARTN_BITS); ++p) {
477 if (drive->part[p].nr_sects > 0) {
478 kdev_t devp = MKDEV(major, minor+p);
479 invalidate_device(devp, 1);
480 }
481 drive->part[p].start_sect = 0;
482 drive->part[p].nr_sects = 0;
483 };
484
485 if (DRIVER(drive)->revalidate)
486 DRIVER(drive)->revalidate(drive);
487
488 drive->busy = 0;
489 wake_up(&drive->wqueue);
490 MOD_DEC_USE_COUNT;
491 return 0;
492}
493
494EXPORT_SYMBOL(ide_revalidate_disk);
495
496static void revalidate_drives (int revaldiate)
497{
498 ide_hwif_t *hwif;
499 ide_drive_t *drive;
500 int index, unit;
501
502 for (index = 0; index < MAX_HWIFS; ++index) {
503 hwif = &ide_hwifs[index];
504 for (unit = 0; unit < MAX_DRIVES; ++unit) {
505 drive = &ide_hwifs[index].drives[unit];
506 if (drive->revalidate) {
507 drive->revalidate = 0;
508 if ((!initializing) && (revaldiate))
509 (void) ide_revalidate_disk(MKDEV(hwif->major, unit<<PARTN_BITS));
510 }
511 }
512 }
513}
514
515void ide_probe_module (int revaldiate)
516{
517 if (!ide_probe) {
518#if defined(CONFIG_BLK_DEV_IDE_MODULE)
519 (void) request_module("ide-probe-mod");
520#endif
521 } else {
522 (void) ide_probe->init();
523 }
524 revalidate_drives(revaldiate);
525}
526
527EXPORT_SYMBOL(ide_probe_module);
528
529void ide_driver_module (int revaldiate)
530{
531 int index;
532 ide_module_t *module = ide_modules;
533
534 for (index = 0; index < MAX_HWIFS; ++index)
535 if (ide_hwifs[index].present)
536 goto search;
537 ide_probe_module(revaldiate);
538search:
539 while (module) {
540 (void) module->init();
541 module = module->next;
542 }
543 revalidate_drives(revaldiate);
544}
545
546EXPORT_SYMBOL(ide_driver_module);
547
548static int ide_open (struct inode * inode, struct file * filp)
549{
550 ide_drive_t *drive;
551 int force = 1;
552
553 if(capable(CAP_SYS_ADMIN) && (filp->f_flags & O_NDELAY))
554 force = 1;
555
556 if ((drive = ide_info_ptr(inode->i_rdev, force)) == NULL)
557 return -ENXIO;
558
559
560
561
562
563
564 if (drive->present)
565 {
566 if (drive->driver == &idedefault_driver)
567 ide_driver_module(1);
568 if (drive->driver == &idedefault_driver) {
569 if (drive->media == ide_disk)
570 (void) request_module("ide-disk");
571 if (drive->scsi)
572 (void) request_module("ide-scsi");
573 if (drive->media == ide_cdrom)
574 (void) request_module("ide-cd");
575 if (drive->media == ide_tape)
576 (void) request_module("ide-tape");
577 if (drive->media == ide_floppy)
578 (void) request_module("ide-floppy");
579 }
580
581
582
583 while (drive->busy)
584 sleep_on(&drive->wqueue);
585 }
586
587
588
589
590
591 drive->usage++;
592 if (!drive->dead || force)
593 return DRIVER(drive)->open(inode, filp, drive);
594 printk(KERN_WARNING "%s: driver not present\n", drive->name);
595 drive->usage--;
596 return -ENXIO;
597}
598
599
600
601
602
603static int ide_release (struct inode * inode, struct file * file)
604{
605 ide_drive_t *drive;
606
607 if ((drive = ide_info_ptr(inode->i_rdev, 1)) != NULL) {
608 drive->usage--;
609 DRIVER(drive)->release(inode, file, drive);
610 }
611 return 0;
612}
613
614#ifdef CONFIG_PROC_FS
615ide_proc_entry_t generic_subdriver_entries[] = {
616 { "capacity", S_IFREG|S_IRUGO, proc_ide_read_capacity, NULL },
617 { NULL, 0, NULL, NULL }
618};
619#endif
620
621
622#define hwif_release_region(addr, num) \
623 ((hwif->mmio) ? release_mem_region((addr),(num)) : release_region((addr),(num)))
624
625
626
627
628
629
630void hwif_unregister (ide_hwif_t *hwif)
631{
632 u32 i = 0;
633
634 if (hwif->mmio == 2)
635 return;
636 if (hwif->io_ports[IDE_CONTROL_OFFSET])
637 hwif_release_region(hwif->io_ports[IDE_CONTROL_OFFSET], 1);
638#if defined(CONFIG_AMIGA) || defined(CONFIG_MAC)
639 if (hwif->io_ports[IDE_IRQ_OFFSET])
640 hwif_release_region(hwif->io_ports[IDE_IRQ_OFFSET], 1);
641#endif
642
643 if (hwif->straight8) {
644 hwif_release_region(hwif->io_ports[IDE_DATA_OFFSET], 8);
645 return;
646 }
647 for (i = IDE_DATA_OFFSET; i <= IDE_STATUS_OFFSET; i++) {
648 if (hwif->io_ports[i]) {
649 hwif_release_region(hwif->io_ports[i], 1);
650 }
651 }
652}
653
654EXPORT_SYMBOL(hwif_unregister);
655
656extern void init_hwif_data(unsigned int index);
657
658
659
660
661
662
663
664
665
666static int ide_prepare_tristate(ide_drive_t *our_drive)
667{
668 ide_drive_t *drive;
669 int unit;
670 unsigned long flags;
671 int minor;
672 int p;
673 int i;
674 ide_hwif_t *hwif = HWIF(our_drive);
675
676 if(our_drive->busy)
677 printk("HUH? We are busy.\n");
678
679 if (!hwif->present)
680 BUG();
681 spin_lock_irqsave(&io_request_lock, flags);
682
683
684 for (unit = 0; unit < MAX_DRIVES; ++unit) {
685 drive = &hwif->drives[unit];
686 if (!drive->present)
687 continue;
688 if (drive == our_drive && drive->usage != 1)
689 goto abort;
690 if (drive != our_drive && drive->usage)
691 goto abort;
692 if (drive->busy)
693 goto abort;
694 }
695
696 for (unit = 0; unit < MAX_DRIVES; ++unit) {
697 drive = &hwif->drives[unit];
698 if (!drive->present)
699 continue;
700 if (drive != our_drive && DRIVER(drive)->shutdown(drive))
701 goto abort;
702 }
703
704
705
706 our_drive->usage--;
707 i = DRIVER(our_drive)->shutdown(our_drive);
708 if(i)
709 goto abort_fix;
710
711
712 spin_unlock_irqrestore(&io_request_lock, flags);
713
714
715
716 spin_unlock_irqrestore(&io_request_lock, flags);
717 for (unit = 0; unit < MAX_DRIVES; ++unit) {
718 drive = &hwif->drives[unit];
719 if (!drive->present)
720 continue;
721 DRIVER(drive)->cleanup(drive);
722 minor = drive->select.b.unit << PARTN_BITS;
723 for (p = 0; p < (1<<PARTN_BITS); ++p) {
724 if (drive->part[p].nr_sects > 0) {
725 kdev_t devp = MKDEV(hwif->major, minor+p);
726 invalidate_device(devp, 0);
727 }
728 }
729#ifdef CONFIG_PROC_FS
730 destroy_proc_ide_drives(hwif);
731#endif
732 }
733 spin_lock_irqsave(&io_request_lock, flags);
734 our_drive->usage++;
735 for (i = 0; i < MAX_DRIVES; ++i) {
736 drive = &hwif->drives[i];
737 if (drive->de) {
738 devfs_unregister(drive->de);
739 drive->de = NULL;
740 }
741 if (!drive->present)
742 continue;
743 if (drive->id != NULL) {
744 kfree(drive->id);
745 drive->id = NULL;
746 }
747 drive->present = 0;
748
749 drive->dead = 0;
750 }
751 spin_unlock_irqrestore(&io_request_lock, flags);
752 return 0;
753
754abort_fix:
755 our_drive->usage++;
756abort:
757 spin_unlock_irqrestore(&io_request_lock, flags);
758 return -EBUSY;
759}
760
761
762
763
764
765
766
767
768
769
770
771static int ide_resume_hwif(ide_drive_t *our_drive)
772{
773 ide_hwif_t *hwif = HWIF(our_drive);
774 int err = ide_wait_hwif_ready(hwif);
775 int irqd;
776 int present = 0;
777 int unit;
778
779 if(err)
780 {
781 printk(KERN_ERR "%s: drives not ready.\n", our_drive->name);
782 return err;
783 }
784
785
786
787 irqd = hwif->irq;
788 if(irqd)
789 disable_irq(irqd);
790
791
792
793 for (unit = 0; unit < MAX_DRIVES; ++unit) {
794 ide_drive_t *drive = &hwif->drives[unit];
795 drive->dn = ((hwif->channel ? 2 : 0) + unit);
796 drive->usage = 0;
797 drive->busy = 0;
798 hwif->drives[unit].dn = ((hwif->channel ? 2 : 0) + unit);
799 (void) ide_probe_for_drive(drive);
800 if (drive->present)
801 present = 1;
802 }
803 ide_probe_reset(hwif);
804 if(irqd)
805 enable_irq(irqd);
806
807 if(present)
808 printk(KERN_INFO "ide: drives found on hot-added interface.\n");
809
810
811
812
813
814
815 ide_tune_drives(hwif);
816 if(present)
817 hwif->present = 1;
818
819
820
821
822 for (unit = 0; unit < MAX_DRIVES; ++unit) {
823 ide_drive_t *drive = &hwif->drives[unit];
824 if(drive->present && !drive->dead)
825 ide_attach_drive(drive);
826 }
827 our_drive->usage++;
828 return 0;
829}
830
831int ide_unregister (unsigned int index)
832{
833 struct gendisk *gd;
834 ide_drive_t *drive, *d;
835 ide_hwif_t *hwif, *g;
836 ide_hwgroup_t *hwgroup;
837 int irq_count = 0, unit, i;
838 unsigned long flags;
839 unsigned int p, minor;
840 ide_hwif_t old_hwif;
841
842 if (index >= MAX_HWIFS)
843 BUG();
844
845 spin_lock_irqsave(&io_request_lock, flags);
846 hwif = &ide_hwifs[index];
847 if (!hwif->present)
848 goto abort;
849 for (unit = 0; unit < MAX_DRIVES; ++unit) {
850 drive = &hwif->drives[unit];
851 if (!drive->present)
852 continue;
853 if (drive->busy || drive->usage)
854 goto abort;
855 if (DRIVER(drive)->shutdown(drive))
856 goto abort;
857 }
858 hwif->present = 0;
859
860
861
862
863 spin_unlock_irqrestore(&io_request_lock, flags);
864 for (unit = 0; unit < MAX_DRIVES; ++unit) {
865 drive = &hwif->drives[unit];
866 if (!drive->present)
867 continue;
868 DRIVER(drive)->cleanup(drive);
869 minor = drive->select.b.unit << PARTN_BITS;
870 for (p = 0; p < (1<<PARTN_BITS); ++p) {
871 if (drive->part[p].nr_sects > 0) {
872 kdev_t devp = MKDEV(hwif->major, minor+p);
873 invalidate_device(devp, 0);
874 }
875 }
876#ifdef CONFIG_PROC_FS
877 destroy_proc_ide_drives(hwif);
878#endif
879 }
880
881 spin_lock_irqsave(&io_request_lock, flags);
882 hwgroup = hwif->hwgroup;
883
884
885
886
887 g = hwgroup->hwif;
888 do {
889 if (g->irq == hwif->irq)
890 ++irq_count;
891 g = g->next;
892 } while (g != hwgroup->hwif);
893 if (irq_count == 1)
894 free_irq(hwif->irq, hwgroup);
895
896
897
898
899
900
901 hwif_unregister(hwif);
902
903
904
905
906
907 d = hwgroup->drive;
908 for (i = 0; i < MAX_DRIVES; ++i) {
909 drive = &hwif->drives[i];
910 if (drive->de) {
911 devfs_unregister(drive->de);
912 drive->de = NULL;
913 }
914 if (!drive->present)
915 continue;
916 while (hwgroup->drive->next != drive)
917 hwgroup->drive = hwgroup->drive->next;
918 hwgroup->drive->next = drive->next;
919 if (hwgroup->drive == drive)
920 hwgroup->drive = NULL;
921 if (drive->id != NULL) {
922 kfree(drive->id);
923 drive->id = NULL;
924 }
925 drive->present = 0;
926 blk_cleanup_queue(&drive->queue);
927 }
928 if (d->present)
929 hwgroup->drive = d;
930 while (hwgroup->hwif->next != hwif)
931 hwgroup->hwif = hwgroup->hwif->next;
932 hwgroup->hwif->next = hwif->next;
933 if (hwgroup->hwif == hwif)
934 kfree(hwgroup);
935 else
936 hwgroup->hwif = HWIF(hwgroup->drive);
937
938#if !defined(CONFIG_DMA_NONPCI)
939 if (hwif->dma_base) {
940 (void) ide_release_dma(hwif);
941
942 hwif->dma_base = 0;
943 hwif->dma_master = 0;
944 hwif->dma_command = 0;
945 hwif->dma_vendor1 = 0;
946 hwif->dma_status = 0;
947 hwif->dma_vendor3 = 0;
948 hwif->dma_prdtable = 0;
949 }
950#endif
951
952
953
954
955 unregister_blkdev(hwif->major, hwif->name);
956 kfree(blksize_size[hwif->major]);
957 kfree(max_sectors[hwif->major]);
958 kfree(max_readahead[hwif->major]);
959 blk_dev[hwif->major].data = NULL;
960 blk_dev[hwif->major].queue = NULL;
961 blksize_size[hwif->major] = NULL;
962 gd = hwif->gd;
963 if (gd) {
964 del_gendisk(gd);
965 kfree(gd->sizes);
966 kfree(gd->part);
967 if (gd->de_arr)
968 kfree(gd->de_arr);
969 if (gd->flags)
970 kfree(gd->flags);
971 kfree(gd);
972 hwif->gd = NULL;
973 }
974
975 old_hwif = *hwif;
976 init_hwif_data(index);
977 hwif->hwgroup = old_hwif.hwgroup;
978
979 hwif->proc = old_hwif.proc;
980
981 hwif->major = old_hwif.major;
982
983
984 hwif->straight8 = old_hwif.straight8;
985 hwif->bus_state = old_hwif.bus_state;
986
987 hwif->atapi_dma = old_hwif.atapi_dma;
988 hwif->ultra_mask = old_hwif.ultra_mask;
989 hwif->mwdma_mask = old_hwif.mwdma_mask;
990 hwif->swdma_mask = old_hwif.swdma_mask;
991
992 hwif->chipset = old_hwif.chipset;
993 hwif->hold = old_hwif.hold;
994
995#ifdef CONFIG_BLK_DEV_IDEPCI
996 hwif->pci_dev = old_hwif.pci_dev;
997 hwif->cds = old_hwif.cds;
998#endif
999
1000#if 0
1001 hwif->hwifops = old_hwif.hwifops;
1002#else
1003 hwif->identify = old_hwif.identify;
1004 hwif->tuneproc = old_hwif.tuneproc;
1005 hwif->speedproc = old_hwif.speedproc;
1006 hwif->selectproc = old_hwif.selectproc;
1007 hwif->reset_poll = old_hwif.reset_poll;
1008 hwif->pre_reset = old_hwif.pre_reset;
1009 hwif->resetproc = old_hwif.resetproc;
1010 hwif->intrproc = old_hwif.intrproc;
1011 hwif->maskproc = old_hwif.maskproc;
1012 hwif->quirkproc = old_hwif.quirkproc;
1013 hwif->busproc = old_hwif.busproc;
1014#endif
1015
1016#if 0
1017 hwif->pioops = old_hwif.pioops;
1018#else
1019 hwif->ata_input_data = old_hwif.ata_input_data;
1020 hwif->ata_output_data = old_hwif.ata_output_data;
1021 hwif->atapi_input_bytes = old_hwif.atapi_input_bytes;
1022 hwif->atapi_output_bytes = old_hwif.atapi_output_bytes;
1023#endif
1024
1025#if 0
1026 hwif->dmaops = old_hwif.dmaops;
1027#else
1028 hwif->ide_dma_read = old_hwif.ide_dma_read;
1029 hwif->ide_dma_write = old_hwif.ide_dma_write;
1030 hwif->ide_dma_begin = old_hwif.ide_dma_begin;
1031 hwif->ide_dma_end = old_hwif.ide_dma_end;
1032 hwif->ide_dma_check = old_hwif.ide_dma_check;
1033 hwif->ide_dma_on = old_hwif.ide_dma_on;
1034 hwif->ide_dma_off = old_hwif.ide_dma_off;
1035 hwif->ide_dma_off_quietly = old_hwif.ide_dma_off_quietly;
1036 hwif->ide_dma_test_irq = old_hwif.ide_dma_test_irq;
1037 hwif->ide_dma_host_on = old_hwif.ide_dma_host_on;
1038 hwif->ide_dma_host_off = old_hwif.ide_dma_host_off;
1039 hwif->ide_dma_bad_drive = old_hwif.ide_dma_bad_drive;
1040 hwif->ide_dma_good_drive = old_hwif.ide_dma_good_drive;
1041 hwif->ide_dma_count = old_hwif.ide_dma_count;
1042 hwif->ide_dma_verbose = old_hwif.ide_dma_verbose;
1043 hwif->ide_dma_retune = old_hwif.ide_dma_retune;
1044 hwif->ide_dma_lostirq = old_hwif.ide_dma_lostirq;
1045 hwif->ide_dma_timeout = old_hwif.ide_dma_timeout;
1046#endif
1047
1048#if 0
1049 hwif->iops = old_hwif.iops;
1050#else
1051 hwif->OUTB = old_hwif.OUTB;
1052 hwif->OUTBSYNC = old_hwif.OUTBSYNC;
1053 hwif->OUTW = old_hwif.OUTW;
1054 hwif->OUTL = old_hwif.OUTL;
1055 hwif->OUTSW = old_hwif.OUTSW;
1056 hwif->OUTSL = old_hwif.OUTSL;
1057
1058 hwif->INB = old_hwif.INB;
1059 hwif->INW = old_hwif.INW;
1060 hwif->INL = old_hwif.INL;
1061 hwif->INSW = old_hwif.INSW;
1062 hwif->INSL = old_hwif.INSL;
1063#endif
1064
1065 hwif->mmio = old_hwif.mmio;
1066 hwif->rqsize = old_hwif.rqsize;
1067 hwif->addressing = old_hwif.addressing;
1068#ifndef CONFIG_BLK_DEV_IDECS
1069 hwif->irq = old_hwif.irq;
1070#endif
1071 hwif->initializing = old_hwif.initializing;
1072
1073 hwif->dma_base = old_hwif.dma_base;
1074 hwif->dma_master = old_hwif.dma_master;
1075 hwif->dma_command = old_hwif.dma_command;
1076 hwif->dma_vendor1 = old_hwif.dma_vendor1;
1077 hwif->dma_status = old_hwif.dma_status;
1078 hwif->dma_vendor3 = old_hwif.dma_vendor3;
1079 hwif->dma_prdtable = old_hwif.dma_prdtable;
1080
1081 hwif->dma_extra = old_hwif.dma_extra;
1082 hwif->config_data = old_hwif.config_data;
1083 hwif->select_data = old_hwif.select_data;
1084 hwif->autodma = old_hwif.autodma;
1085 hwif->udma_four = old_hwif.udma_four;
1086 hwif->no_dsc = old_hwif.no_dsc;
1087
1088 hwif->hwif_data = old_hwif.hwif_data;
1089 spin_unlock_irqrestore(&io_request_lock, flags);
1090 return 0;
1091
1092abort:
1093 spin_unlock_irqrestore(&io_request_lock, flags);
1094 return 1;
1095
1096}
1097
1098EXPORT_SYMBOL(ide_unregister);
1099
1100
1101
1102
1103
1104
1105void ide_setup_ports ( hw_regs_t *hw,
1106 ide_ioreg_t base, int *offsets,
1107 ide_ioreg_t ctrl, ide_ioreg_t intr,
1108 ide_ack_intr_t *ack_intr,
1109
1110
1111
1112 int irq)
1113{
1114 int i;
1115
1116 for (i = 0; i < IDE_NR_PORTS; i++) {
1117 if (offsets[i] == -1) {
1118 switch(i) {
1119 case IDE_CONTROL_OFFSET:
1120 hw->io_ports[i] = ctrl;
1121 break;
1122#if defined(CONFIG_AMIGA) || defined(CONFIG_MAC)
1123 case IDE_IRQ_OFFSET:
1124 hw->io_ports[i] = intr;
1125 break;
1126#endif
1127 default:
1128 hw->io_ports[i] = 0;
1129 break;
1130 }
1131 } else {
1132 hw->io_ports[i] = base + offsets[i];
1133 }
1134 }
1135 hw->irq = irq;
1136 hw->dma = NO_DMA;
1137 hw->ack_intr = ack_intr;
1138
1139
1140
1141}
1142
1143EXPORT_SYMBOL(ide_setup_ports);
1144
1145
1146
1147
1148
1149int ide_register_hw (hw_regs_t *hw, ide_hwif_t **hwifp)
1150{
1151 int index, retry = 1;
1152 ide_hwif_t *hwif;
1153
1154 do {
1155 for (index = 0; index < MAX_HWIFS; ++index) {
1156 hwif = &ide_hwifs[index];
1157 if (hwif->hw.io_ports[IDE_DATA_OFFSET] == hw->io_ports[IDE_DATA_OFFSET])
1158 goto found;
1159 }
1160 for (index = 0; index < MAX_HWIFS; ++index) {
1161 hwif = &ide_hwifs[index];
1162 if (!hwif->hold && ((!hwif->present && !hwif->mate && !initializing) ||
1163 (!hwif->hw.io_ports[IDE_DATA_OFFSET] && initializing)))
1164 goto found;
1165 }
1166 for (index = 0; index < MAX_HWIFS; index++)
1167 ide_unregister(index);
1168 } while (retry--);
1169 return -1;
1170found:
1171 if (hwif->present)
1172 ide_unregister(index);
1173 else if (!hwif->hold)
1174 init_hwif_data(index);
1175 if (hwif->present)
1176 return -1;
1177 memcpy(&hwif->hw, hw, sizeof(*hw));
1178 memcpy(hwif->io_ports, hwif->hw.io_ports, sizeof(hwif->hw.io_ports));
1179 hwif->irq = hw->irq;
1180 hwif->noprobe = 0;
1181 hwif->chipset = hw->chipset;
1182
1183 if (!initializing) {
1184 ide_probe_module(1);
1185#ifdef CONFIG_PROC_FS
1186 create_proc_ide_interfaces();
1187#endif
1188 ide_driver_module(1);
1189 }
1190
1191 if (hwifp)
1192 *hwifp = hwif;
1193
1194 return (initializing || hwif->present) ? index : -1;
1195}
1196
1197EXPORT_SYMBOL(ide_register_hw);
1198
1199
1200
1201
1202
1203int ide_register (int arg1, int arg2, int irq)
1204{
1205 hw_regs_t hw;
1206 ide_init_hwif_ports(&hw, (ide_ioreg_t) arg1, (ide_ioreg_t) arg2, NULL);
1207 hw.irq = irq;
1208 return ide_register_hw(&hw, NULL);
1209}
1210
1211EXPORT_SYMBOL(ide_register);
1212
1213
1214
1215
1216
1217
1218DECLARE_MUTEX(ide_setting_sem);
1219EXPORT_SYMBOL(ide_setting_sem);
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247int ide_add_setting (ide_drive_t *drive, const char *name, int rw, int read_ioctl, int write_ioctl, int data_type, int min, int max, int mul_factor, int div_factor, void *data, ide_procset_t *set)
1248{
1249 ide_settings_t **p = (ide_settings_t **) &drive->settings, *setting = NULL;
1250
1251 down(&ide_setting_sem);
1252 while ((*p) && strcmp((*p)->name, name) < 0)
1253 p = &((*p)->next);
1254 if ((setting = kmalloc(sizeof(*setting), GFP_KERNEL)) == NULL)
1255 goto abort;
1256 memset(setting, 0, sizeof(*setting));
1257 if ((setting->name = kmalloc(strlen(name) + 1, GFP_KERNEL)) == NULL)
1258 goto abort;
1259 strcpy(setting->name, name);
1260 setting->rw = rw;
1261 setting->read_ioctl = read_ioctl;
1262 setting->write_ioctl = write_ioctl;
1263 setting->data_type = data_type;
1264 setting->min = min;
1265 setting->max = max;
1266 setting->mul_factor = mul_factor;
1267 setting->div_factor = div_factor;
1268 setting->data = data;
1269 setting->set = set;
1270
1271 setting->next = *p;
1272 if (drive->driver != &idedefault_driver)
1273 setting->auto_remove = 1;
1274 *p = setting;
1275 up(&ide_setting_sem);
1276 return 0;
1277abort:
1278 up(&ide_setting_sem);
1279 if (setting)
1280 kfree(setting);
1281 return -1;
1282}
1283
1284EXPORT_SYMBOL(ide_add_setting);
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295static void __ide_remove_setting (ide_drive_t *drive, char *name)
1296{
1297 ide_settings_t **p, *setting;
1298
1299 p = (ide_settings_t **) &drive->settings;
1300
1301 while ((*p) && strcmp((*p)->name, name))
1302 p = &((*p)->next);
1303 if ((setting = (*p)) == NULL)
1304 return;
1305
1306 (*p) = setting->next;
1307
1308 kfree(setting->name);
1309 kfree(setting);
1310}
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323void ide_remove_setting (ide_drive_t *drive, char *name)
1324{
1325 down(&ide_setting_sem);
1326 __ide_remove_setting(drive, name);
1327 up(&ide_setting_sem);
1328}
1329
1330EXPORT_SYMBOL(ide_remove_setting);
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342static ide_settings_t *ide_find_setting_by_ioctl (ide_drive_t *drive, int cmd)
1343{
1344 ide_settings_t *setting = drive->settings;
1345
1346 while (setting) {
1347 if (setting->read_ioctl == cmd || setting->write_ioctl == cmd)
1348 break;
1349 setting = setting->next;
1350 }
1351
1352 return setting;
1353}
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365ide_settings_t *ide_find_setting_by_name (ide_drive_t *drive, char *name)
1366{
1367 ide_settings_t *setting = drive->settings;
1368
1369 while (setting) {
1370 if (strcmp(setting->name, name) == 0)
1371 break;
1372 setting = setting->next;
1373 }
1374 return setting;
1375}
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386static void auto_remove_settings (ide_drive_t *drive)
1387{
1388 ide_settings_t *setting;
1389repeat:
1390 setting = drive->settings;
1391 while (setting) {
1392 if (setting->auto_remove) {
1393 __ide_remove_setting(drive, setting->name);
1394 goto repeat;
1395 }
1396 setting = setting->next;
1397 }
1398}
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413int ide_read_setting (ide_drive_t *drive, ide_settings_t *setting)
1414{
1415 int val = -EINVAL;
1416 unsigned long flags;
1417
1418 if ((setting->rw & SETTING_READ)) {
1419 spin_lock_irqsave(&io_request_lock, flags);
1420 switch(setting->data_type) {
1421 case TYPE_BYTE:
1422 val = *((u8 *) setting->data);
1423 break;
1424 case TYPE_SHORT:
1425 val = *((u16 *) setting->data);
1426 break;
1427 case TYPE_INT:
1428 case TYPE_INTA:
1429 val = *((u32 *) setting->data);
1430 break;
1431 }
1432 spin_unlock_irqrestore(&io_request_lock, flags);
1433 }
1434 return val;
1435}
1436
1437int ide_spin_wait_hwgroup (ide_drive_t *drive)
1438{
1439 ide_hwgroup_t *hwgroup = HWGROUP(drive);
1440 unsigned long timeout = jiffies + (3 * HZ);
1441
1442 spin_lock_irq(&io_request_lock);
1443
1444 while (hwgroup->busy) {
1445 unsigned long lflags;
1446 spin_unlock_irq(&io_request_lock);
1447 local_irq_set(lflags);
1448 if (time_after(jiffies, timeout)) {
1449 local_irq_restore(lflags);
1450 printk(KERN_ERR "%s: channel busy\n", drive->name);
1451 return -EBUSY;
1452 }
1453 local_irq_restore(lflags);
1454 spin_lock_irq(&io_request_lock);
1455 }
1456 return 0;
1457}
1458
1459EXPORT_SYMBOL(ide_spin_wait_hwgroup);
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478int ide_write_setting (ide_drive_t *drive, ide_settings_t *setting, int val)
1479{
1480 int i;
1481 u32 *p;
1482
1483 if (!capable(CAP_SYS_ADMIN))
1484 return -EACCES;
1485 if (!(setting->rw & SETTING_WRITE))
1486 return -EPERM;
1487 if (val < setting->min || val > setting->max)
1488 return -EINVAL;
1489 if (setting->set)
1490 return setting->set(drive, val);
1491 if (ide_spin_wait_hwgroup(drive))
1492 return -EBUSY;
1493 switch (setting->data_type) {
1494 case TYPE_BYTE:
1495 *((u8 *) setting->data) = val;
1496 break;
1497 case TYPE_SHORT:
1498 *((u16 *) setting->data) = val;
1499 break;
1500 case TYPE_INT:
1501 *((u32 *) setting->data) = val;
1502 break;
1503 case TYPE_INTA:
1504 p = (u32 *) setting->data;
1505 for (i = 0; i < 1 << PARTN_BITS; i++, p++)
1506 *p = val;
1507 break;
1508 }
1509 spin_unlock_irq(&io_request_lock);
1510 return 0;
1511}
1512
1513EXPORT_SYMBOL(ide_write_setting);
1514
1515static int set_io_32bit(ide_drive_t *drive, int arg)
1516{
1517 drive->io_32bit = arg;
1518#ifdef CONFIG_BLK_DEV_DTC2278
1519 if (HWIF(drive)->chipset == ide_dtc2278)
1520 HWIF(drive)->drives[!drive->select.b.unit].io_32bit = arg;
1521#endif
1522 return 0;
1523}
1524
1525static int set_using_dma (ide_drive_t *drive, int arg)
1526{
1527 if (!DRIVER(drive)->supports_dma)
1528 return -EPERM;
1529 if (!(drive->id->capability & 1))
1530 return -EPERM;
1531 if (HWIF(drive)->ide_dma_check == NULL)
1532 return -EPERM;
1533 if (HWIF(drive)->ide_dma_check(drive) != 0)
1534 return -EIO;
1535 if (arg) {
1536 if (HWIF(drive)->ide_dma_check(drive) != 0)
1537 return -EIO;
1538 if (HWIF(drive)->ide_dma_on(drive)) return -EIO;
1539 } else {
1540 if (HWIF(drive)->ide_dma_off(drive)) return -EIO;
1541 }
1542 return 0;
1543}
1544
1545static int set_pio_mode (ide_drive_t *drive, int arg)
1546{
1547 struct request rq;
1548
1549 if (!HWIF(drive)->tuneproc)
1550 return -ENOSYS;
1551 if (drive->special.b.set_tune)
1552 return -EBUSY;
1553 ide_init_drive_cmd(&rq);
1554 drive->tune_req = (u8) arg;
1555 drive->special.b.set_tune = 1;
1556 (void) ide_do_drive_cmd(drive, &rq, ide_wait);
1557 return 0;
1558}
1559
1560static int set_xfer_rate (ide_drive_t *drive, int arg)
1561{
1562 int err = ide_wait_cmd(drive,
1563 WIN_SETFEATURES, (u8) arg,
1564 SETFEATURES_XFER, 0, NULL);
1565
1566 if (!err && arg) {
1567 ide_set_xfer_rate(drive, (u8) arg);
1568 ide_driveid_update(drive);
1569 }
1570 return err;
1571}
1572
1573int ide_atapi_to_scsi (ide_drive_t *drive, int arg)
1574{
1575 if (drive->media == ide_disk) {
1576 drive->scsi = 0;
1577 return 0;
1578 }
1579 if (DRIVER(drive)->cleanup(drive)) {
1580 drive->scsi = 0;
1581 return 0;
1582 }
1583 drive->scsi = (u8) arg;
1584 ide_attach_drive(drive);
1585 return 0;
1586}
1587
1588void ide_add_generic_settings (ide_drive_t *drive)
1589{
1590
1591
1592
1593 ide_add_setting(drive, "io_32bit", drive->no_io_32bit ? SETTING_READ : SETTING_RW, HDIO_GET_32BIT, HDIO_SET_32BIT, TYPE_BYTE, 0, 1 + (SUPPORT_VLB_SYNC << 1), 1, 1, &drive->io_32bit, set_io_32bit);
1594 ide_add_setting(drive, "keepsettings", SETTING_RW, HDIO_GET_KEEPSETTINGS, HDIO_SET_KEEPSETTINGS, TYPE_BYTE, 0, 1, 1, 1, &drive->keep_settings, NULL);
1595 ide_add_setting(drive, "nice1", SETTING_RW, -1, -1, TYPE_BYTE, 0, 1, 1, 1, &drive->nice1, NULL);
1596 ide_add_setting(drive, "pio_mode", SETTING_WRITE, -1, HDIO_SET_PIO_MODE, TYPE_BYTE, 0, 255, 1, 1, NULL, set_pio_mode);
1597 ide_add_setting(drive, "slow", SETTING_RW, -1, -1, TYPE_BYTE, 0, 1, 1, 1, &drive->slow, NULL);
1598 ide_add_setting(drive, "unmaskirq", drive->no_unmask ? SETTING_READ : SETTING_RW, HDIO_GET_UNMASKINTR, HDIO_SET_UNMASKINTR, TYPE_BYTE, 0, 1, 1, 1, &drive->unmask, NULL);
1599 ide_add_setting(drive, "using_dma", SETTING_RW, HDIO_GET_DMA, HDIO_SET_DMA, TYPE_BYTE, 0, 1, 1, 1, &drive->using_dma, set_using_dma);
1600 ide_add_setting(drive, "init_speed", SETTING_RW, -1, -1, TYPE_BYTE, 0, 70, 1, 1, &drive->init_speed, NULL);
1601 ide_add_setting(drive, "current_speed", SETTING_RW, -1, -1, TYPE_BYTE, 0, 70, 1, 1, &drive->current_speed, set_xfer_rate);
1602 ide_add_setting(drive, "number", SETTING_RW, -1, -1, TYPE_BYTE, 0, 3, 1, 1, &drive->dn, NULL);
1603#if 0
1604
1605 if (drive->media != ide_disk)
1606 ide_add_setting(drive, "ide-scsi", SETTING_RW, -1, HDIO_SET_IDE_SCSI, TYPE_BYTE, 0, 1, 1, 1, &drive->scsi, ide_atapi_to_scsi);
1607#endif
1608}
1609
1610EXPORT_SYMBOL_GPL(ide_add_generic_settings);
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620void ide_delay_50ms (void)
1621{
1622#ifndef CONFIG_BLK_DEV_IDECS
1623 mdelay(50);
1624#else
1625 __set_current_state(TASK_UNINTERRUPTIBLE);
1626 schedule_timeout(1+HZ/20);
1627#endif
1628}
1629
1630EXPORT_SYMBOL(ide_delay_50ms);
1631
1632int system_bus_clock (void)
1633{
1634 return((int) ((!system_bus_speed) ? ide_system_bus_speed() : system_bus_speed ));
1635}
1636
1637EXPORT_SYMBOL(system_bus_clock);
1638
1639int ide_replace_subdriver (ide_drive_t *drive, const char *driver)
1640{
1641 if (!drive->present || drive->busy || drive->usage || drive->dead)
1642 goto abort;
1643 if (DRIVER(drive)->cleanup(drive))
1644 goto abort;
1645 strncpy(drive->driver_req, driver, 9);
1646 ide_driver_module(0);
1647 drive->driver_req[0] = 0;
1648 ide_driver_module(0);
1649 if (!strcmp(DRIVER(drive)->name, driver))
1650 return 0;
1651abort:
1652 return 1;
1653}
1654
1655EXPORT_SYMBOL(ide_replace_subdriver);
1656
1657int ide_attach_drive (ide_drive_t *drive)
1658{
1659
1660 if(drive->dead)
1661 return 1;
1662
1663#ifdef CONFIG_BLK_DEV_IDESCSI
1664 if (drive->scsi) {
1665 extern int idescsi_attach(ide_drive_t *drive);
1666 if (idescsi_attach(drive))
1667 return 0;
1668 }
1669#endif
1670
1671 switch (drive->media) {
1672#ifdef CONFIG_BLK_DEV_IDECD
1673 case ide_cdrom:
1674 {
1675 extern int ide_cdrom_attach(ide_drive_t *drive);
1676 if (ide_cdrom_attach(drive))
1677 return 1;
1678 break;
1679 }
1680#endif
1681#ifdef CONFIG_BLK_DEV_IDEDISK
1682 case ide_disk:
1683 {
1684 extern int idedisk_attach(ide_drive_t *drive);
1685 if (idedisk_attach(drive))
1686 return 1;
1687 break;
1688 }
1689#endif
1690#ifdef CONFIG_BLK_DEV_IDEFLOPPY
1691 case ide_floppy:
1692 {
1693 extern int idefloppy_attach(ide_drive_t *drive);
1694 if (idefloppy_attach(drive))
1695 return 1;
1696 break;
1697 }
1698#endif
1699#ifdef CONFIG_BLK_DEV_IDETAPE
1700 case ide_tape:
1701 {
1702 extern int idetape_attach(ide_drive_t *drive);
1703 if (idetape_attach(drive))
1704 return 1;
1705 break;
1706 }
1707#endif
1708 default:
1709 {
1710 extern int idedefault_attach(ide_drive_t *drive);
1711 if(idedefault_attach(drive))
1712 printk(KERN_CRIT "ide: failed to attach default driver.\n");
1713 return 1;
1714 }
1715 }
1716 return 0;
1717}
1718
1719EXPORT_SYMBOL(ide_attach_drive);
1720
1721static int ide_ioctl (struct inode *inode, struct file *file,
1722 unsigned int cmd, unsigned long arg)
1723{
1724 int err = 0, major, minor;
1725 ide_drive_t *drive;
1726 struct request rq;
1727 kdev_t dev;
1728 ide_settings_t *setting;
1729 int force = 0;
1730
1731 if (!inode || !(dev = inode->i_rdev))
1732 return -EINVAL;
1733
1734 switch(cmd)
1735 {
1736 case HDIO_GET_BUSSTATE:
1737 case HDIO_SET_BUSSTATE:
1738 case HDIO_SCAN_HWIF:
1739 case HDIO_UNREGISTER_HWIF:
1740 force = 1;
1741 }
1742
1743 major = MAJOR(dev); minor = MINOR(dev);
1744 if ((drive = ide_info_ptr(inode->i_rdev, force)) == NULL)
1745 return -ENODEV;
1746
1747 down(&ide_setting_sem);
1748 if ((setting = ide_find_setting_by_ioctl(drive, cmd)) != NULL) {
1749 if (cmd == setting->read_ioctl) {
1750 err = ide_read_setting(drive, setting);
1751 up(&ide_setting_sem);
1752 return err >= 0 ? put_user(err, (long *) arg) : err;
1753 } else {
1754 if ((MINOR(inode->i_rdev) & PARTN_MASK))
1755 err = -EINVAL;
1756 else
1757 err = ide_write_setting(drive, setting, arg);
1758 up(&ide_setting_sem);
1759 return err;
1760 }
1761 }
1762 up(&ide_setting_sem);
1763
1764 ide_init_drive_cmd (&rq);
1765 switch (cmd) {
1766 case HDIO_GETGEO:
1767 {
1768 struct hd_geometry *loc = (struct hd_geometry *) arg;
1769 u16 bios_cyl = drive->bios_cyl;
1770 if (!loc || (drive->media != ide_disk && drive->media != ide_floppy)) return -EINVAL;
1771 if (put_user(drive->bios_head, (u8 *) &loc->heads)) return -EFAULT;
1772 if (put_user(drive->bios_sect, (u8 *) &loc->sectors)) return -EFAULT;
1773 if (put_user(bios_cyl, (u16 *) &loc->cylinders)) return -EFAULT;
1774 if (put_user((unsigned)drive->part[MINOR(inode->i_rdev)&PARTN_MASK].start_sect,
1775 (unsigned long *) &loc->start)) return -EFAULT;
1776 return 0;
1777 }
1778
1779 case HDIO_GETGEO_BIG:
1780 {
1781 struct hd_big_geometry *loc = (struct hd_big_geometry *) arg;
1782 if (!loc || (drive->media != ide_disk && drive->media != ide_floppy)) return -EINVAL;
1783 if (put_user(drive->bios_head, (u8 *) &loc->heads)) return -EFAULT;
1784 if (put_user(drive->bios_sect, (u8 *) &loc->sectors)) return -EFAULT;
1785 if (put_user(drive->bios_cyl, (unsigned int *) &loc->cylinders)) return -EFAULT;
1786 if (put_user((unsigned)drive->part[MINOR(inode->i_rdev)&PARTN_MASK].start_sect,
1787 (unsigned long *) &loc->start)) return -EFAULT;
1788 return 0;
1789 }
1790
1791 case HDIO_GETGEO_BIG_RAW:
1792 {
1793 struct hd_big_geometry *loc = (struct hd_big_geometry *) arg;
1794 if (!loc || (drive->media != ide_disk && drive->media != ide_floppy)) return -EINVAL;
1795 if (put_user(drive->head, (u8 *) &loc->heads)) return -EFAULT;
1796 if (put_user(drive->sect, (u8 *) &loc->sectors)) return -EFAULT;
1797 if (put_user(drive->cyl, (unsigned int *) &loc->cylinders)) return -EFAULT;
1798 if (put_user((unsigned)drive->part[MINOR(inode->i_rdev)&PARTN_MASK].start_sect,
1799 (unsigned long *) &loc->start)) return -EFAULT;
1800 return 0;
1801 }
1802
1803 case BLKGETSIZE:
1804 return put_user(drive->part[MINOR(inode->i_rdev)&PARTN_MASK].nr_sects, (unsigned long *) arg);
1805 case BLKGETSIZE64:
1806 return put_user((u64)drive->part[MINOR(inode->i_rdev)&PARTN_MASK].nr_sects << 9, (u64 *) arg);
1807
1808 case BLKRRPART:
1809 if (!capable(CAP_SYS_ADMIN)) return -EACCES;
1810 return ide_revalidate_disk(inode->i_rdev);
1811
1812 case HDIO_OBSOLETE_IDENTITY:
1813 case HDIO_GET_IDENTITY:
1814 if (MINOR(inode->i_rdev) & PARTN_MASK)
1815 return -EINVAL;
1816 if (drive->id_read == 0)
1817 return -ENOMSG;
1818 if (copy_to_user((char *)arg, (char *)drive->id, (cmd == HDIO_GET_IDENTITY) ? sizeof(*drive->id) : 142))
1819 return -EFAULT;
1820 return 0;
1821
1822 case HDIO_GET_NICE:
1823 return put_user(drive->dsc_overlap << IDE_NICE_DSC_OVERLAP |
1824 drive->atapi_overlap << IDE_NICE_ATAPI_OVERLAP |
1825 drive->nice0 << IDE_NICE_0 |
1826 drive->nice1 << IDE_NICE_1 |
1827 drive->nice2 << IDE_NICE_2,
1828 (long *) arg);
1829
1830#ifdef CONFIG_IDE_TASK_IOCTL
1831 case HDIO_DRIVE_TASKFILE:
1832 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
1833 return -EACCES;
1834 switch(drive->media) {
1835 case ide_disk:
1836 return ide_taskfile_ioctl(drive, inode, file, cmd, arg);
1837#ifdef CONFIG_PKT_TASK_IOCTL
1838 case ide_cdrom:
1839 case ide_tape:
1840 case ide_floppy:
1841 return pkt_taskfile_ioctl(drive, inode, file, cmd, arg);
1842#endif
1843 default:
1844 return -ENOMSG;
1845 }
1846#endif
1847
1848 case HDIO_DRIVE_CMD:
1849 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
1850 return -EACCES;
1851 return ide_cmd_ioctl(drive, inode, file, cmd, arg);
1852
1853 case HDIO_DRIVE_TASK:
1854 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
1855 return -EACCES;
1856 return ide_task_ioctl(drive, inode, file, cmd, arg);
1857
1858 case HDIO_SCAN_HWIF:
1859 {
1860 int args[3];
1861 if (!capable(CAP_SYS_RAWIO)) return -EACCES;
1862 if (copy_from_user(args, (void *)arg, 3 * sizeof(int)))
1863 return -EFAULT;
1864 if (ide_register(args[0], args[1], args[2]) == -1)
1865 return -EIO;
1866 return 0;
1867 }
1868 case HDIO_UNREGISTER_HWIF:
1869 if (!capable(CAP_SYS_RAWIO)) return -EACCES;
1870
1871 ide_unregister(arg);
1872 return 0;
1873 case HDIO_SET_NICE:
1874 if (!capable(CAP_SYS_ADMIN)) return -EACCES;
1875 if (arg != (arg & ((1 << IDE_NICE_DSC_OVERLAP) | (1 << IDE_NICE_1))))
1876 return -EPERM;
1877 drive->dsc_overlap = (arg >> IDE_NICE_DSC_OVERLAP) & 1;
1878 if (drive->dsc_overlap && !DRIVER(drive)->supports_dsc_overlap) {
1879 drive->dsc_overlap = 0;
1880 return -EPERM;
1881 }
1882 drive->nice1 = (arg >> IDE_NICE_1) & 1;
1883 return 0;
1884 case HDIO_DRIVE_RESET:
1885 {
1886 unsigned long flags;
1887 if (!capable(CAP_SYS_ADMIN)) return -EACCES;
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897 spin_lock_irqsave(&io_request_lock, flags);
1898
1899 DRIVER(drive)->abort(drive, "drive reset");
1900 if(HWGROUP(drive)->handler)
1901 BUG();
1902
1903
1904
1905
1906 HWGROUP(drive)->busy = 1;
1907 spin_unlock_irqrestore(&io_request_lock, flags);
1908
1909 (void) ide_do_reset(drive);
1910 if (drive->suspend_reset) {
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920 return ide_revalidate_disk(inode->i_rdev);
1921 }
1922 return 0;
1923 }
1924 case BLKROSET:
1925 case BLKROGET:
1926 case BLKFLSBUF:
1927 case BLKSSZGET:
1928 case BLKPG:
1929 case BLKELVGET:
1930 case BLKELVSET:
1931 case BLKBSZGET:
1932 case BLKBSZSET:
1933 return blk_ioctl(inode->i_rdev, cmd, arg);
1934
1935 case HDIO_GET_BUSSTATE:
1936 if (!capable(CAP_SYS_ADMIN))
1937 return -EACCES;
1938 if (put_user(HWIF(drive)->bus_state, (long *)arg))
1939 return -EFAULT;
1940 return 0;
1941
1942 case HDIO_SET_BUSSTATE:
1943 {
1944 ide_hwif_t *hwif = HWIF(drive);
1945
1946 if (!capable(CAP_SYS_ADMIN))
1947 return -EACCES;
1948#ifdef OLD_STUFF
1949 if (hwif->busproc)
1950 return HWIF(drive)->busproc(drive, (int)arg);
1951 return -EOPNOTSUPP;
1952#else
1953 if(hwif->bus_state == arg)
1954 return 0;
1955
1956 if(hwif->bus_state == BUSSTATE_ON)
1957 {
1958
1959 if((err = ide_prepare_tristate(drive)) != 0)
1960 return err;
1961 hwif->bus_state = arg;
1962 }
1963 if (hwif->busproc)
1964 {
1965 err = hwif->busproc(drive, (int)arg);
1966 if(err)
1967 return err;
1968 }
1969 if(arg != BUSSTATE_OFF)
1970 {
1971 err = ide_resume_hwif(drive);
1972 hwif->bus_state = arg;
1973 if(err)
1974 return err;
1975 }
1976 return 0;
1977#endif
1978 }
1979
1980 default:
1981 return DRIVER(drive)->ioctl(drive, inode, file, cmd, arg);
1982 return -EPERM;
1983 }
1984}
1985
1986static int ide_check_media_change (kdev_t i_rdev)
1987{
1988 ide_drive_t *drive;
1989
1990 if ((drive = ide_info_ptr(i_rdev, 0)) == NULL)
1991 return -ENODEV;
1992 return DRIVER(drive)->media_change(drive);
1993}
1994
1995
1996
1997
1998
1999static int __init stridx (const char *s, char c)
2000{
2001 char *i = strchr(s, c);
2002 return (i && c) ? i - s : -1;
2003}
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017static int __init match_parm (char *s, const char *keywords[], int vals[], int max_vals)
2018{
2019 static const char *decimal = "0123456789";
2020 static const char *hex = "0123456789abcdef";
2021 int i, n;
2022
2023 if (*s++ == '=') {
2024
2025
2026
2027
2028 if (keywords != NULL) {
2029 for (i = 0; *keywords != NULL; ++i) {
2030 if (!strcmp(s, *keywords++))
2031 return -(i+1);
2032 }
2033 }
2034
2035
2036
2037
2038
2039
2040 for (n = 0; (i = stridx(decimal, *s)) >= 0;) {
2041 vals[n] = i;
2042 while ((i = stridx(decimal, *++s)) >= 0)
2043 vals[n] = (vals[n] * 10) + i;
2044 if (*s == 'x' && !vals[n]) {
2045 while ((i = stridx(hex, *++s)) >= 0)
2046 vals[n] = (vals[n] * 0x10) + i;
2047 }
2048 if (++n == max_vals)
2049 break;
2050 if (*s == ',' || *s == ';')
2051 ++s;
2052 }
2053 if (!*s)
2054 return n;
2055 }
2056 return 0;
2057}
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139int __init ide_setup (char *s)
2140{
2141 int i, vals[3];
2142 ide_hwif_t *hwif;
2143 ide_drive_t *drive;
2144 unsigned int hw, unit;
2145 const char max_drive = 'a' + ((MAX_HWIFS * MAX_DRIVES) - 1);
2146 const char max_hwif = '0' + (MAX_HWIFS - 1);
2147
2148
2149 if (strncmp(s,"hd",2) == 0 && s[2] == '=')
2150 return 0;
2151
2152 if (strncmp(s,"ide",3) &&
2153 strncmp(s,"idebus",6) &&
2154 strncmp(s,"hd",2))
2155 return 0;
2156
2157 printk(KERN_INFO "ide_setup: %s", s);
2158 init_ide_data ();
2159
2160#ifdef CONFIG_BLK_DEV_IDEDOUBLER
2161 if (!strcmp(s, "ide=doubler")) {
2162 extern int ide_doubler;
2163
2164 printk(" : Enabled support for IDE doublers\n");
2165 ide_doubler = 1;
2166 return 1;
2167 }
2168#endif
2169
2170 if (!strcmp(s, "ide=nodma")) {
2171 printk(" : Prevented DMA\n");
2172 noautodma = 1;
2173 return 1;
2174 }
2175
2176#ifdef CONFIG_BLK_DEV_IDEPCI
2177 if (!strcmp(s, "ide=reverse")) {
2178 ide_scan_direction = 1;
2179 printk(" : Enabled support for IDE inverse scan order.\n");
2180 return 1;
2181 }
2182#endif
2183
2184
2185
2186
2187 if (s[0] == 'h' && s[1] == 'd' && s[2] >= 'a' && s[2] <= max_drive) {
2188 const char *hd_words[] = {"none", "noprobe", "nowerr", "cdrom",
2189 "serialize", "autotune", "noautotune",
2190 "slow", "swapdata", "bswap", "flash",
2191 "remap", "noremap", "scsi", NULL};
2192 unit = s[2] - 'a';
2193 hw = unit / MAX_DRIVES;
2194 unit = unit % MAX_DRIVES;
2195 hwif = &ide_hwifs[hw];
2196 drive = &hwif->drives[unit];
2197 if (strncmp(s + 4, "ide-", 4) == 0) {
2198 strncpy(drive->driver_req, s + 4, 9);
2199 goto done;
2200 }
2201
2202
2203
2204 if (s[3] == 'l' && s[4] == 'u' && s[5] == 'n') {
2205 if (match_parm(&s[6], NULL, vals, 1) != 1)
2206 goto bad_option;
2207 if (vals[0] >= 0 && vals[0] <= 7) {
2208 drive->last_lun = vals[0];
2209 drive->forced_lun = 1;
2210 } else
2211 printk(" -- BAD LAST LUN! Expected value from 0 to 7");
2212 goto done;
2213 }
2214 switch (match_parm(&s[3], hd_words, vals, 3)) {
2215 case -1:
2216 drive->nobios = 1;
2217 case -2:
2218 drive->noprobe = 1;
2219 goto done;
2220 case -3:
2221 drive->bad_wstat = BAD_R_STAT;
2222 hwif->noprobe = 0;
2223 goto done;
2224 case -4:
2225 drive->present = 1;
2226 drive->media = ide_cdrom;
2227 hwif->noprobe = 0;
2228 goto done;
2229 case -5:
2230 printk(" -- USE \"ide%d=serialize\" INSTEAD", hw);
2231 goto do_serialize;
2232 case -6:
2233 drive->autotune = 1;
2234 goto done;
2235 case -7:
2236 drive->autotune = 2;
2237 goto done;
2238 case -8:
2239 drive->slow = 1;
2240 goto done;
2241 case -9:
2242 case -10:
2243 drive->bswap = 1;
2244 goto done;
2245 case -11:
2246 drive->ata_flash = 1;
2247 goto done;
2248 case -12:
2249 drive->remap_0_to_1 = 1;
2250 goto done;
2251 case -13:
2252 drive->remap_0_to_1 = 2;
2253 goto done;
2254 case -14:
2255 drive->scsi = 1;
2256 goto done;
2257 case 3:
2258 drive->media = ide_disk;
2259 drive->cyl = drive->bios_cyl = vals[0];
2260 drive->head = drive->bios_head = vals[1];
2261 drive->sect = drive->bios_sect = vals[2];
2262 drive->present = 1;
2263 drive->forced_geom = 1;
2264 hwif->noprobe = 0;
2265 goto done;
2266 default:
2267 goto bad_option;
2268 }
2269 }
2270
2271 if (s[0] != 'i' || s[1] != 'd' || s[2] != 'e')
2272 goto bad_option;
2273
2274
2275
2276 if (s[3] == 'b' && s[4] == 'u' && s[5] == 's') {
2277 if (match_parm(&s[6], NULL, vals, 1) != 1)
2278 goto bad_option;
2279 if (vals[0] >= 20 && vals[0] <= 66) {
2280 idebus_parameter = vals[0];
2281 } else
2282 printk(" -- BAD BUS SPEED! Expected value from 20 to 66");
2283 goto done;
2284 }
2285
2286
2287
2288 if (s[3] >= '0' && s[3] <= max_hwif) {
2289
2290
2291
2292
2293 const char *ide_words[] = {
2294 "noprobe", "serialize", "autotune", "noautotune", "reset", "dma", "ata66",
2295 "minus8", "minus9", "minus10",
2296 "four", "qd65xx", "ht6560b", "cmd640_vlb", "dtc2278", "umc8672", "ali14xx", "dc4030", NULL };
2297 hw = s[3] - '0';
2298 hwif = &ide_hwifs[hw];
2299 i = match_parm(&s[4], ide_words, vals, 3);
2300
2301
2302
2303
2304 if (i > 0 || i <= -11) {
2305 if (hwif->chipset != ide_unknown)
2306 goto bad_option;
2307 if (i <= -11 && i != -18 && hw != 0)
2308 goto bad_hwif;
2309 if (i <= -11 && i != -18 && ide_hwifs[hw+1].chipset != ide_unknown)
2310 goto bad_option;
2311 printk("\n");
2312 }
2313
2314 switch (i) {
2315#ifdef CONFIG_BLK_DEV_PDC4030
2316 case -18:
2317 {
2318 extern void init_pdc4030(void);
2319 init_pdc4030();
2320 goto done;
2321 }
2322#endif
2323#ifdef CONFIG_BLK_DEV_ALI14XX
2324 case -17:
2325 {
2326 extern void init_ali14xx (void);
2327 init_ali14xx();
2328 goto done;
2329 }
2330#endif
2331#ifdef CONFIG_BLK_DEV_UMC8672
2332 case -16:
2333 {
2334 extern void init_umc8672 (void);
2335 init_umc8672();
2336 goto done;
2337 }
2338#endif
2339#ifdef CONFIG_BLK_DEV_DTC2278
2340 case -15:
2341 {
2342 extern void init_dtc2278 (void);
2343 init_dtc2278();
2344 goto done;
2345 }
2346#endif
2347#ifdef CONFIG_BLK_DEV_CMD640
2348 case -14:
2349 {
2350 extern void init_cmd640_vlb (void);
2351 init_cmd640_vlb();
2352 goto done;
2353 }
2354#endif
2355#ifdef CONFIG_BLK_DEV_HT6560B
2356 case -13:
2357 {
2358 extern void init_ht6560b (void);
2359 init_ht6560b();
2360 goto done;
2361 }
2362#endif
2363#if CONFIG_BLK_DEV_QD65XX
2364 case -12:
2365 {
2366 extern void init_qd65xx (void);
2367 init_qd65xx();
2368 goto done;
2369 }
2370#endif
2371#ifdef CONFIG_BLK_DEV_4DRIVES
2372 case -11:
2373 {
2374 ide_hwif_t *mate = &ide_hwifs[hw^1];
2375 mate->drives[0].select.all ^= 0x20;
2376 mate->drives[1].select.all ^= 0x20;
2377 hwif->chipset = mate->chipset = ide_4drives;
2378 mate->irq = hwif->irq;
2379 memcpy(mate->io_ports, hwif->io_ports, sizeof(hwif->io_ports));
2380 goto do_serialize;
2381 }
2382#endif
2383 case -10:
2384 case -9:
2385 case -8:
2386 goto bad_option;
2387 case -7:
2388#ifdef CONFIG_BLK_DEV_IDEPCI
2389 hwif->udma_four = 1;
2390 goto done;
2391#else
2392 hwif->udma_four = 0;
2393 goto bad_hwif;
2394#endif
2395 case -6:
2396 hwif->autodma = 1;
2397 goto done;
2398 case -5:
2399 hwif->reset = 1;
2400 goto done;
2401 case -4:
2402 hwif->drives[0].autotune = 2;
2403 hwif->drives[1].autotune = 2;
2404 goto done;
2405 case -3:
2406 hwif->drives[0].autotune = 1;
2407 hwif->drives[1].autotune = 1;
2408 goto done;
2409 case -2:
2410 do_serialize:
2411 hwif->mate = &ide_hwifs[hw^1];
2412 hwif->mate->mate = hwif;
2413 hwif->serialized = hwif->mate->serialized = 1;
2414 goto done;
2415
2416 case -1:
2417 hwif->noprobe = 1;
2418 goto done;
2419
2420 case 1:
2421 vals[1] = vals[0] + 0x206;
2422 case 2:
2423 vals[2] = 0;
2424 case 3:
2425 hwif->hw.irq = vals[2];
2426 ide_init_hwif_ports(&hwif->hw, (ide_ioreg_t) vals[0], (ide_ioreg_t) vals[1], &hwif->irq);
2427 memcpy(hwif->io_ports, hwif->hw.io_ports, sizeof(hwif->io_ports));
2428 hwif->irq = vals[2];
2429 hwif->noprobe = 0;
2430 hwif->chipset = ide_generic;
2431 goto done;
2432
2433 case 0: goto bad_option;
2434 default:
2435 printk(" -- SUPPORT NOT CONFIGURED IN THIS KERNEL\n");
2436 return 1;
2437 }
2438 }
2439bad_option:
2440 printk(" -- BAD OPTION\n");
2441 return 1;
2442bad_hwif:
2443 printk("-- NOT SUPPORTED ON ide%d", hw);
2444done:
2445 printk("\n");
2446 return 1;
2447}
2448
2449
2450
2451
2452#define NUM_DRIVER 32
2453
2454
2455
2456
2457
2458static __initdata ide_driver_call ide_scan[NUM_DRIVER];
2459static int drivers_run = 0;
2460
2461void __init ide_register_driver(ide_driver_call scan)
2462{
2463 static int ide_scans = 0;
2464 if(ide_scans == NUM_DRIVER)
2465 panic("Too many IDE drivers");
2466 ide_scan[ide_scans++]=scan;
2467 if(drivers_run)
2468 {
2469 printk(KERN_ERR "ide: late registration of driver.\n");
2470 scan();
2471 }
2472}
2473
2474EXPORT_SYMBOL(ide_register_driver);
2475
2476static void __init ide_scan_drivers(void)
2477{
2478 int i = 0;
2479 while(ide_scan[i])
2480 {
2481 (ide_scan[i])();
2482 i++;
2483 }
2484 drivers_run = 1;
2485}
2486
2487
2488
2489
2490static void __init probe_for_hwifs (void)
2491{
2492#ifdef CONFIG_BLK_DEV_IDEPCI
2493 if (pci_present())
2494 {
2495 ide_scan_pcibus(ide_scan_direction);
2496 }
2497#endif
2498 ide_scan_drivers();
2499
2500
2501
2502
2503#ifdef CONFIG_ETRAX_IDE
2504 {
2505 extern void init_e100_ide(void);
2506 init_e100_ide();
2507 }
2508#endif
2509#ifdef CONFIG_BLK_DEV_IDE_PMAC
2510 {
2511 extern void pmac_ide_probe(void);
2512 pmac_ide_probe();
2513 }
2514#endif
2515#ifdef CONFIG_BLK_DEV_IDE_SIBYTE
2516 {
2517 extern void sibyte_ide_probe(void);
2518 sibyte_ide_probe();
2519 }
2520#endif
2521#ifdef CONFIG_BLK_DEV_IDE_ICSIDE
2522 {
2523 extern void icside_init(void);
2524 icside_init();
2525 }
2526#endif
2527#ifdef CONFIG_BLK_DEV_IDE_RAPIDE
2528 {
2529 extern void rapide_init(void);
2530 rapide_init();
2531 }
2532#endif
2533#ifdef CONFIG_BLK_DEV_GAYLE
2534 {
2535 extern void gayle_init(void);
2536 gayle_init();
2537 }
2538#endif
2539#ifdef CONFIG_BLK_DEV_FALCON_IDE
2540 {
2541 extern void falconide_init(void);
2542 falconide_init();
2543 }
2544#endif
2545#ifdef CONFIG_BLK_DEV_MAC_IDE
2546 {
2547 extern void macide_init(void);
2548 macide_init();
2549 }
2550#endif
2551#ifdef CONFIG_BLK_DEV_CPCI405_IDE
2552 {
2553 extern void cpci405ide_init(void);
2554 cpci405ide_init();
2555 }
2556#endif
2557#ifdef CONFIG_BLK_DEV_Q40IDE
2558 {
2559 extern void q40ide_init(void);
2560 q40ide_init();
2561 }
2562#endif
2563#ifdef CONFIG_BLK_DEV_BUDDHA
2564 {
2565 extern void buddha_init(void);
2566 buddha_init();
2567 }
2568#endif
2569}
2570
2571void __init ide_init_builtin_subdrivers (void)
2572{
2573 idedefault_init();
2574#ifdef CONFIG_BLK_DEV_IDEDISK
2575 (void) idedisk_init();
2576#endif
2577#ifdef CONFIG_BLK_DEV_IDECD
2578 (void) ide_cdrom_init();
2579#endif
2580#ifdef CONFIG_BLK_DEV_IDETAPE
2581 (void) idetape_init();
2582#endif
2583#ifdef CONFIG_BLK_DEV_IDEFLOPPY
2584 (void) idefloppy_init();
2585#endif
2586#ifdef CONFIG_BLK_DEV_IDESCSI
2587 #ifdef CONFIG_SCSI
2588 (void) idescsi_init();
2589 #else
2590 #warning ide scsi-emulation selected but no SCSI-subsystem in kernel
2591 #endif
2592#endif
2593}
2594
2595#ifndef CLASSIC_BUILTINS_METHOD
2596# ifndef DIRECT_HWIF_PROBE_ATTACH_METHOD
2597# ifdef FAKE_CLASSIC_ATTACH_METHOD
2598
2599
2600
2601void ide_attach_devices (void)
2602{
2603 ide_hwif_t *hwif;
2604 ide_drive_t *drive;
2605 int i, unit;
2606
2607 for (i = 0; i < MAX_HWIFS; i++) {
2608 hwif = &ide_hwifs[i];
2609 if (!hwif->present)
2610 continue;
2611 for (unit = 0; unit < MAX_DRIVES; ++unit) {
2612 drive = &hwif->drives[unit];
2613 if (drive->present && !drive->dead)
2614 ide_attach_drive(drive);
2615 }
2616 }
2617}
2618# endif
2619# endif
2620#endif
2621
2622void __init ide_init_builtin_drivers (void)
2623{
2624
2625
2626
2627 idedefault_init();
2628
2629
2630
2631 probe_for_hwifs ();
2632
2633#ifdef CONFIG_BLK_DEV_IDE
2634 if (ide_hwifs[0].io_ports[IDE_DATA_OFFSET])
2635 ide_get_lock(NULL, NULL);
2636
2637 (void) ideprobe_init();
2638
2639 if (ide_hwifs[0].io_ports[IDE_DATA_OFFSET])
2640 ide_release_lock();
2641#endif
2642
2643#ifdef CONFIG_PROC_FS
2644 proc_ide_create();
2645#endif
2646
2647#ifdef CLASSIC_BUILTINS_METHOD
2648 ide_init_builtin_subdrivers();
2649#else
2650# ifndef DIRECT_HWIF_PROBE_ATTACH_METHOD
2651# ifdef FAKE_CLASSIC_ATTACH_METHOD
2652 ide_attach_devices();
2653# endif
2654# endif
2655#endif
2656}
2657
2658
2659
2660
2661
2662
2663static int default_cleanup (ide_drive_t *drive)
2664{
2665 return ide_unregister_subdriver(drive);
2666}
2667
2668
2669
2670
2671
2672
2673static int default_shutdown(ide_drive_t *drive)
2674{
2675 if (drive->usage || drive->busy || DRIVER(drive)->busy) {
2676 return 1;
2677 }
2678 drive->dead = 1;
2679 return 0;
2680}
2681
2682static int default_standby (ide_drive_t *drive)
2683{
2684 return 0;
2685}
2686
2687static int default_suspend (ide_drive_t *drive)
2688{
2689 return 0;
2690}
2691
2692static int default_resume (ide_drive_t *drive)
2693{
2694 return 0;
2695}
2696
2697static int default_flushcache (ide_drive_t *drive)
2698{
2699 return 0;
2700}
2701
2702static ide_startstop_t default_do_request (ide_drive_t *drive, struct request *rq, unsigned long block)
2703{
2704 ide_end_request(drive, 0);
2705 return ide_stopped;
2706}
2707
2708static int default_end_request (ide_drive_t *drive, int uptodate)
2709{
2710 return ide_end_request(drive, uptodate);
2711}
2712
2713static u8 default_sense (ide_drive_t *drive, const char *msg, u8 stat)
2714{
2715 return ide_dump_status(drive, msg, stat);
2716}
2717
2718static ide_startstop_t default_error (ide_drive_t *drive, const char *msg, u8 stat)
2719{
2720 return ide_error(drive, msg, stat);
2721}
2722
2723static ide_startstop_t default_abort (ide_drive_t *drive, const char *msg)
2724{
2725 return ide_abort(drive, msg);
2726}
2727
2728static int default_ioctl (ide_drive_t *drive, struct inode *inode, struct file *file,
2729 unsigned int cmd, unsigned long arg)
2730{
2731 return -EIO;
2732}
2733
2734static int default_open (struct inode *inode, struct file *filp, ide_drive_t *drive)
2735{
2736 drive->usage--;
2737 return -EIO;
2738}
2739
2740static void default_release (struct inode *inode, struct file *filp, ide_drive_t *drive)
2741{
2742}
2743
2744static int default_check_media_change (ide_drive_t *drive)
2745{
2746 return 1;
2747}
2748
2749static void default_pre_reset (ide_drive_t *drive)
2750{
2751}
2752
2753static unsigned long default_capacity (ide_drive_t *drive)
2754{
2755 return 0x7fffffff;
2756}
2757
2758static ide_startstop_t default_special (ide_drive_t *drive)
2759{
2760 special_t *s = &drive->special;
2761
2762 s->all = 0;
2763 drive->mult_req = 0;
2764 return ide_stopped;
2765}
2766
2767static int default_init (void)
2768{
2769 return 0;
2770}
2771
2772static int default_attach (ide_drive_t *drive)
2773{
2774 printk(KERN_ERR "%s: does not support hotswap of device class !\n",
2775 drive->name);
2776
2777 return 0;
2778}
2779
2780static void setup_driver_defaults (ide_drive_t *drive)
2781{
2782 ide_driver_t *d = drive->driver;
2783
2784 if (d->cleanup == NULL) d->cleanup = default_cleanup;
2785 if (d->shutdown == NULL) d->shutdown = default_shutdown;
2786 if (d->standby == NULL) d->standby = default_standby;
2787 if (d->suspend == NULL) d->suspend = default_suspend;
2788 if (d->resume == NULL) d->resume = default_resume;
2789 if (d->flushcache == NULL) d->flushcache = default_flushcache;
2790 if (d->do_request == NULL) d->do_request = default_do_request;
2791 if (d->end_request == NULL) d->end_request = default_end_request;
2792 if (d->sense == NULL) d->sense = default_sense;
2793 if (d->error == NULL) d->error = default_error;
2794 if (d->error == NULL) d->abort = default_abort;
2795 if (d->ioctl == NULL) d->ioctl = default_ioctl;
2796 if (d->open == NULL) d->open = default_open;
2797 if (d->release == NULL) d->release = default_release;
2798 if (d->media_change == NULL) d->media_change = default_check_media_change;
2799 if (d->pre_reset == NULL) d->pre_reset = default_pre_reset;
2800 if (d->capacity == NULL) d->capacity = default_capacity;
2801 if (d->special == NULL) d->special = default_special;
2802 if (d->init == NULL) d->init = default_init;
2803 if (d->attach == NULL) d->attach = default_attach;
2804}
2805
2806ide_drive_t *ide_scan_devices (u8 media, const char *name, ide_driver_t *driver, int n)
2807{
2808 unsigned int unit, index, i;
2809
2810 if(driver == NULL)
2811 driver = &idedefault_driver;
2812
2813 for (index = 0, i = 0; index < MAX_HWIFS; ++index) {
2814 ide_hwif_t *hwif = &ide_hwifs[index];
2815 if (!hwif->present)
2816 continue;
2817 for (unit = 0; unit < MAX_DRIVES; ++unit) {
2818 ide_drive_t *drive = &hwif->drives[unit];
2819 char *req = drive->driver_req;
2820 if (*req && !strstr(name, req))
2821 continue;
2822 if (drive->present && drive->media == media &&
2823 drive->driver == driver && ++i > n)
2824 return drive;
2825 }
2826 }
2827 return NULL;
2828}
2829
2830EXPORT_SYMBOL(ide_scan_devices);
2831
2832int ide_register_subdriver (ide_drive_t *drive, ide_driver_t *driver, int version)
2833{
2834 unsigned long flags;
2835
2836 BUG_ON(drive->driver == NULL);
2837
2838 spin_lock_irqsave(&io_request_lock, flags);
2839 if (version != IDE_SUBDRIVER_VERSION || !drive->present ||
2840 drive->driver != &idedefault_driver || drive->busy || drive->usage) {
2841 spin_unlock_irqrestore(&io_request_lock, flags);
2842 return 1;
2843 }
2844 drive->driver = driver;
2845 setup_driver_defaults(drive);
2846 printk("%s: attached %s driver.\n", drive->name, driver->name);
2847 spin_unlock_irqrestore(&io_request_lock, flags);
2848 if (drive->autotune != 2) {
2849
2850 if (!driver->supports_dma && HWIF(drive)->ide_dma_off_quietly)
2851
2852 HWIF(drive)->ide_dma_off(drive);
2853 drive->dsc_overlap = (drive->next != drive && driver->supports_dsc_overlap);
2854 drive->nice1 = 1;
2855 }
2856 drive->revalidate = 1;
2857 drive->suspend_reset = 0;
2858#ifdef CONFIG_PROC_FS
2859 if (drive->driver != &idedefault_driver) {
2860 ide_add_proc_entries(drive->proc, generic_subdriver_entries, drive);
2861 ide_add_proc_entries(drive->proc, driver->proc, drive);
2862 }
2863#endif
2864 return 0;
2865}
2866
2867EXPORT_SYMBOL(ide_register_subdriver);
2868
2869int ide_unregister_subdriver (ide_drive_t *drive)
2870{
2871 unsigned long flags;
2872
2873 down(&ide_setting_sem);
2874 spin_lock_irqsave(&io_request_lock, flags);
2875 if (drive->usage || drive->busy || DRIVER(drive)->busy) {
2876 spin_unlock_irqrestore(&io_request_lock, flags);
2877 up(&ide_setting_sem);
2878 return 1;
2879 }
2880#ifdef CONFIG_PROC_FS
2881 ide_remove_proc_entries(drive->proc, DRIVER(drive)->proc);
2882 ide_remove_proc_entries(drive->proc, generic_subdriver_entries);
2883#endif
2884 drive->driver = &idedefault_driver;
2885 setup_driver_defaults(drive);
2886 auto_remove_settings(drive);
2887 spin_unlock_irqrestore(&io_request_lock, flags);
2888 up(&ide_setting_sem);
2889 return 0;
2890}
2891
2892EXPORT_SYMBOL(ide_unregister_subdriver);
2893
2894int ide_register_module (ide_module_t *module)
2895{
2896 ide_module_t *p = ide_modules;
2897
2898 while (p) {
2899 if (p == module)
2900 return 1;
2901 p = p->next;
2902 }
2903 module->next = ide_modules;
2904 ide_modules = module;
2905 revalidate_drives(1);
2906 return 0;
2907}
2908
2909EXPORT_SYMBOL(ide_register_module);
2910
2911void ide_unregister_module (ide_module_t *module)
2912{
2913 ide_module_t **p;
2914
2915 for (p = &ide_modules; (*p) && (*p) != module; p = &((*p)->next));
2916 if (*p)
2917 *p = (*p)->next;
2918}
2919
2920EXPORT_SYMBOL(ide_unregister_module);
2921
2922struct block_device_operations ide_fops[] = {{
2923 owner: THIS_MODULE,
2924 open: ide_open,
2925 release: ide_release,
2926 ioctl: ide_ioctl,
2927 check_media_change: ide_check_media_change,
2928 revalidate: ide_revalidate_disk
2929}};
2930
2931EXPORT_SYMBOL(ide_fops);
2932
2933
2934
2935
2936void ide_geninit (ide_hwif_t *hwif)
2937{
2938 unsigned int unit;
2939 struct gendisk *gd = hwif->gd;
2940
2941 for (unit = 0; unit < MAX_DRIVES; ++unit) {
2942 ide_drive_t *drive = &hwif->drives[unit];
2943
2944 if (!drive->present)
2945 continue;
2946 if (drive->media!=ide_disk && drive->media!=ide_floppy)
2947 continue;
2948 register_disk(gd,MKDEV(hwif->major,unit<<PARTN_BITS),
2949#ifdef CONFIG_BLK_DEV_ISAPNP
2950 (drive->forced_geom && drive->noprobe) ? 1 :
2951#endif
2952 1<<PARTN_BITS, ide_fops,
2953 current_capacity(drive));
2954 }
2955}
2956
2957EXPORT_SYMBOL(ide_geninit);
2958
2959
2960
2961
2962
2963devfs_handle_t ide_devfs_handle;
2964
2965EXPORT_SYMBOL(ide_probe);
2966EXPORT_SYMBOL(ide_devfs_handle);
2967
2968static int ide_notify_reboot (struct notifier_block *this, unsigned long event, void *x)
2969{
2970 ide_hwif_t *hwif;
2971 ide_drive_t *drive;
2972 int i, unit;
2973
2974 switch (event) {
2975 case SYS_HALT:
2976 case SYS_POWER_OFF:
2977 case SYS_RESTART:
2978 break;
2979 default:
2980 return NOTIFY_DONE;
2981 }
2982
2983 printk(KERN_INFO "flushing ide devices: ");
2984
2985 for (i = 0; i < MAX_HWIFS; i++) {
2986 hwif = &ide_hwifs[i];
2987 if (!hwif->present)
2988 continue;
2989 for (unit = 0; unit < MAX_DRIVES; ++unit) {
2990 drive = &hwif->drives[unit];
2991 if (!drive->present)
2992 continue;
2993
2994
2995 printk("%s ", drive->name);
2996#ifdef CONFIG_ALPHA
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010 if (event == SYS_POWER_OFF)
3011#else
3012 if (event != SYS_RESTART)
3013#endif
3014 if (DRIVER(drive)->standby(drive))
3015 continue;
3016
3017 DRIVER(drive)->cleanup(drive);
3018 continue;
3019 }
3020 }
3021 printk("\n");
3022 return NOTIFY_DONE;
3023}
3024
3025static struct notifier_block ide_notifier = {
3026 ide_notify_reboot,
3027 NULL,
3028 5
3029};
3030
3031
3032
3033
3034int __init ide_init (void)
3035{
3036 static char banner_printed;
3037 int i;
3038
3039 if (!banner_printed) {
3040 printk(KERN_INFO "Uniform Multi-Platform E-IDE driver " REVISION "\n");
3041 ide_devfs_handle = devfs_mk_dir(NULL, "ide", NULL);
3042 system_bus_speed = ide_system_bus_speed();
3043 banner_printed = 1;
3044 }
3045
3046 init_ide_data();
3047
3048#ifndef CLASSIC_BUILTINS_METHOD
3049 ide_init_builtin_subdrivers();
3050#endif
3051
3052 initializing = 1;
3053 ide_init_builtin_drivers();
3054 initializing = 0;
3055
3056 for (i = 0; i < MAX_HWIFS; ++i) {
3057 ide_hwif_t *hwif = &ide_hwifs[i];
3058 if (hwif->present)
3059 ide_geninit(hwif);
3060 }
3061
3062 register_reboot_notifier(&ide_notifier);
3063 return 0;
3064}
3065
3066#ifdef MODULE
3067char *options = NULL;
3068MODULE_PARM(options,"s");
3069MODULE_LICENSE("GPL");
3070
3071static void __init parse_options (char *line)
3072{
3073 char *next = line;
3074
3075 if (line == NULL || !*line)
3076 return;
3077 while ((line = next) != NULL) {
3078 if ((next = strchr(line,' ')) != NULL)
3079 *next++ = 0;
3080 if (!ide_setup(line))
3081 printk ("Unknown option '%s'\n", line);
3082 }
3083}
3084
3085int init_module (void)
3086{
3087 parse_options(options);
3088 return ide_init();
3089}
3090
3091void cleanup_module (void)
3092{
3093 int index;
3094
3095 unregister_reboot_notifier(&ide_notifier);
3096 for (index = 0; index < MAX_HWIFS; ++index) {
3097 ide_unregister(index);
3098#if !defined(CONFIG_DMA_NONPCI)
3099 if (ide_hwifs[index].dma_base)
3100 (void) ide_release_dma(&ide_hwifs[index]);
3101#endif
3102 }
3103
3104#ifdef CONFIG_PROC_FS
3105 proc_ide_destroy();
3106#endif
3107 devfs_unregister (ide_devfs_handle);
3108}
3109
3110#else
3111
3112__setup("", ide_setup);
3113module_init(ide_init);
3114
3115#endif
3116