This source file includes following definitions.
- read_timer
- ide_set_recovery_timer
- init_ide_data
- do_vlb_sync
- ide_input_data
- ide_output_data
- ide_hwif_select
- ide_set_handler
- lba_capacity_is_ok
- current_capacity
- ide_geninit
- init_gendisk
- reset_ihandler
- start_reset_timer
- atapi_reset_handler
- reset_handler
- ide_do_reset
- end_drive_cmd
- ide_dump_status
- try_to_flush_leftover_data
- ide_error
- read_intr
- write_intr
- multwrite
- multwrite_intr
- ide_cmd
- set_multmode_intr
- set_geometry_intr
- recal_intr
- drive_cmd_intr
- do_special
- ide_wait_stat
- do_rw_disk
- do_request
- ide_do_request
- do_hwgroup_request
- do_ide0_request
- do_ide1_request
- do_ide2_request
- do_ide3_request
- timer_expiry
- unexpected_intr
- ide_intr
- get_info_ptr
- ide_do_drive_cmd
- ide_open
- ide_release
- revalidate_disk
- write_fs_long
- ide_ioctl
- ide_check_media_change
- fixstring
- do_identify
- delay_10ms
- try_to_identify
- do_probe
- probe_for_drive
- probe_for_drives
- sub22
- init_dtc2278
- init_qd6580
- stridx
- match_parm
- ide_setup
- ide_xlate_1024
- probe_cmos_for_drives
- init_irq
- ide_pci_access_error
- buggy_interface_fallback
- init_rz1000
- ide_probe_pci
- ide_init_pci
- ide_init
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185 #undef REALLY_SLOW_IO
186
187 #include <linux/config.h>
188 #include <linux/types.h>
189 #include <linux/string.h>
190 #include <linux/kernel.h>
191 #include <linux/delay.h>
192 #include <linux/timer.h>
193 #include <linux/mm.h>
194 #include <linux/ioport.h>
195 #include <linux/interrupt.h>
196 #include <linux/major.h>
197 #include <linux/blkdev.h>
198 #include <linux/errno.h>
199 #include <linux/hdreg.h>
200 #include <linux/genhd.h>
201 #include <linux/malloc.h>
202
203 #include <asm/byteorder.h>
204 #include <asm/irq.h>
205 #include <asm/segment.h>
206 #include <asm/io.h>
207
208 #ifdef CONFIG_PCI
209 #include <linux/bios32.h>
210 #include <linux/pci.h>
211 #endif
212
213 #include "ide.h"
214
215 #ifdef SUPPORT_CMD640
216 void cmd640_tune_drive(ide_drive_t *);
217 static int cmd640_vlb = 0;
218 #endif
219
220 ide_hwif_t ide_hwifs[MAX_HWIFS];
221 static ide_hwgroup_t *irq_to_hwgroup [16];
222 static const byte ide_hwif_to_major[MAX_HWIFS] = {IDE0_MAJOR, IDE1_MAJOR, IDE2_MAJOR, IDE3_MAJOR};
223
224 static const unsigned short default_io_base[MAX_HWIFS] = {0x1f0, 0x170, 0x1e8, 0x168};
225 static const byte default_irqs[MAX_HWIFS] = {14, 15, 11, 10};
226 static int serialized = 0;
227 static int disallow_unmask = 0;
228
229 #if (DISK_RECOVERY_TIME > 0)
230
231
232
233
234 static unsigned long read_timer(void)
235 {
236 unsigned long t, flags;
237 int i;
238
239 save_flags(flags);
240 cli();
241 t = jiffies * 11932;
242 outb_p(0, 0x43);
243 i = inb_p(0x40);
244 i |= inb(0x40) << 8;
245 restore_flags(flags);
246 return (t - i);
247 }
248
249 void ide_set_recovery_timer (ide_hwif_t *hwif)
250 {
251 hwif->last_time = read_timer();
252 }
253 #endif
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269 #define MAGIC_COOKIE 0x12345678
270 static void init_ide_data (void)
271 {
272 byte *p;
273 unsigned int h, unit;
274 static unsigned long magic_cookie = MAGIC_COOKIE;
275
276 if (magic_cookie != MAGIC_COOKIE)
277 return;
278 magic_cookie = 0;
279
280 for (h = 0; h < 16; ++h)
281 irq_to_hwgroup[h] = NULL;
282
283
284 p = ((byte *) ide_hwifs) + sizeof(ide_hwifs);
285 do {
286 *--p = 0;
287 } while (p > (byte *) ide_hwifs);
288
289 for (h = 0; h < MAX_HWIFS; ++h) {
290 ide_hwif_t *hwif = &ide_hwifs[h];
291
292
293 hwif->noprobe = (h > 1);
294 hwif->io_base = default_io_base[h];
295 hwif->ctl_port = hwif->io_base ? hwif->io_base+0x206 : 0x000;
296 #ifdef CONFIG_BLK_DEV_HD
297 if (hwif->io_base == HD_DATA)
298 hwif->noprobe = 1;
299 #endif
300 hwif->major = ide_hwif_to_major[h];
301 hwif->name[0] = 'i';
302 hwif->name[1] = 'd';
303 hwif->name[2] = 'e';
304 hwif->name[3] = '0' + h;
305
306 for (unit = 0; unit < MAX_DRIVES; ++unit) {
307 ide_drive_t *drive = &hwif->drives[unit];
308
309
310 drive->select.all = (unit<<4)|0xa0;
311 drive->hwif = hwif;
312 drive->ctl = 0x08;
313 drive->ready_stat = READY_STAT;
314 drive->bad_wstat = BAD_W_STAT;
315 drive->special.b.recalibrate = 1;
316 drive->special.b.set_geometry = 1;
317 drive->name[0] = 'h';
318 drive->name[1] = 'd';
319 drive->name[2] = 'a' + (h * MAX_DRIVES) + unit;
320 }
321 }
322 }
323
324 #define VLB_SYNC 1
325
326
327
328
329
330
331
332 static inline void do_vlb_sync (unsigned short port) {
333 (void) inb (port);
334 (void) inb (port);
335 (void) inb (port);
336 }
337
338
339
340
341 void ide_input_data (ide_drive_t *drive, void *buffer, unsigned int wcount)
342 {
343 unsigned short io_base = HWIF(drive)->io_base;
344 unsigned short data_reg = io_base+IDE_DATA_OFFSET;
345
346 if (drive->vlb_32bit) {
347 #ifdef VLB_SYNC
348 if (drive->vlb_sync) {
349 cli();
350 do_vlb_sync(io_base+IDE_NSECTOR_OFFSET);
351 insl(data_reg, buffer, wcount);
352 if (drive->unmask)
353 sti();
354 } else
355 #endif
356 insl(data_reg, buffer, wcount);
357 } else
358 insw(data_reg, buffer, wcount<<1);
359 }
360
361
362
363
364 void ide_output_data (ide_drive_t *drive, void *buffer, unsigned int wcount)
365 {
366 unsigned short io_base = HWIF(drive)->io_base;
367 unsigned short data_reg = io_base+IDE_DATA_OFFSET;
368
369 if (drive->vlb_32bit) {
370 #ifdef VLB_SYNC
371 if (drive->vlb_sync) {
372 cli();
373 do_vlb_sync(io_base+IDE_NSECTOR_OFFSET);
374 outsl(data_reg, buffer, wcount);
375 if (drive->unmask)
376 sti();
377 } else
378 #endif
379 outsl(data_reg, buffer, wcount);
380 } else
381 outsw(data_reg, buffer, wcount<<1);
382 }
383
384 #if SUPPORT_HT6560B
385
386
387
388
389
390
391
392
393
394
395
396 void ide_hwif_select (ide_hwif_t *hwif)
397 {
398 static byte current_select = 0;
399
400 if (hwif->select != current_select) {
401 unsigned long flags;
402 save_flags (flags);
403 cli();
404 current_select = hwif->select;
405 (void) inb(0x3e6);
406 (void) inb(0x3e6);
407 (void) inb(0x3e6);
408 (void) inb(0x3e6);
409 outb(current_select,0x3e6);
410 restore_flags (flags);
411 }
412 }
413 #endif
414
415
416
417
418
419
420
421
422 void ide_set_handler (ide_drive_t *drive, ide_handler_t *handler)
423 {
424 ide_hwgroup_t *hwgroup = HWGROUP(drive);
425 #ifdef DEBUG
426 if (hwgroup->handler != NULL)
427 printk("%s: ide_set_handler: old handler not null\n", drive->name);
428 #endif
429 hwgroup->handler = handler;
430 hwgroup->timer.expires = jiffies + WAIT_CMD;
431 add_timer(&(hwgroup->timer));
432 }
433
434
435
436
437
438
439
440
441 static int lba_capacity_is_ok (struct hd_driveid *id)
442 {
443 unsigned long lba_sects = id->lba_capacity;
444 unsigned long chs_sects = id->cyls * id->heads * id->sectors;
445 unsigned long _10_percent = chs_sects / 10;
446
447
448 if ((lba_sects - chs_sects) < _10_percent)
449 return 1;
450
451
452 lba_sects = (lba_sects << 16) | (lba_sects >> 16);
453 if ((lba_sects - chs_sects) < _10_percent) {
454 id->lba_capacity = lba_sects;
455 return 1;
456 }
457 return 0;
458 }
459
460
461
462
463
464 static unsigned long current_capacity (ide_drive_t *drive)
465 {
466 struct hd_driveid *id = drive->id;
467 unsigned long capacity;
468
469 if (!drive->present)
470 return 0;
471 if (drive->media != disk)
472 return 0x1fffff;
473
474 if (id != NULL && (id->capability & 2) && lba_capacity_is_ok(id)) {
475 drive->select.b.lba = 1;
476 capacity = id->lba_capacity;
477 } else {
478 drive->select.b.lba = 0;
479 capacity = drive->cyl * drive->head * drive->sect;
480 }
481 return (capacity - drive->sect0);
482 }
483
484
485
486
487
488 static void ide_geninit (struct gendisk *gd)
489 {
490 unsigned int unit;
491 ide_hwif_t *hwif = gd->real_devices;
492
493 for (unit = 0; unit < gd->nr_real; ++unit) {
494 ide_drive_t *drive = &hwif->drives[unit];
495 #ifdef CONFIG_BLK_DEV_IDECD
496 if (drive->present && drive->media == cdrom)
497 ide_cdrom_setup(drive);
498 #endif
499 drive->part[0].nr_sects = current_capacity(drive);
500 if (!drive->present || drive->media != disk) {
501 drive->part[0].start_sect = -1;
502 }
503 }
504
505
506
507
508
509
510 gd->real_devices = hwif->drives[0].name;
511 }
512
513
514
515
516
517
518
519 static void init_gendisk (ide_hwif_t *hwif)
520 {
521 struct gendisk *gd;
522 unsigned int unit, units, minors;
523 int *bs;
524
525
526 for (units = MAX_DRIVES; units > 0; --units) {
527 if (hwif->drives[units-1].present)
528 break;
529 }
530 minors = units * (1<<PARTN_BITS);
531 gd = kmalloc (sizeof(struct gendisk), GFP_KERNEL);
532 gd->sizes = kmalloc (minors * sizeof(int), GFP_KERNEL);
533 gd->part = kmalloc (minors * sizeof(struct hd_struct), GFP_KERNEL);
534 bs = kmalloc (minors*sizeof(int), GFP_KERNEL);
535
536
537 blksize_size[hwif->major] = bs;
538 for (unit = 0; unit < minors; ++unit)
539 *bs++ = BLOCK_SIZE;
540
541 for (unit = 0; unit < units; ++unit)
542 hwif->drives[unit].part = &gd->part[unit << PARTN_BITS];
543
544 gd->major = hwif->major;
545 gd->major_name = IDE_MAJOR_NAME;
546 gd->minor_shift = PARTN_BITS;
547 gd->max_p = 1<<PARTN_BITS;
548 gd->max_nr = units;
549 gd->nr_real = units;
550 gd->init = ide_geninit;
551 gd->real_devices= hwif;
552
553 gd->next = gendisk_head;
554 hwif->gd = gendisk_head = gd;
555 }
556
557 static void unexpected_intr (int, ide_hwgroup_t *);
558
559
560
561
562
563
564
565 static void reset_ihandler (ide_drive_t *drive)
566 {
567 unsigned long flags;
568
569 save_flags(flags);
570 cli();
571 unexpected_intr (HWIF(drive)->irq, HWGROUP(drive));
572 restore_flags(flags);
573 }
574
575
576
577
578
579 static void start_reset_timer (ide_hwif_t *hwif)
580 {
581 ide_hwgroup_t *hwgroup = hwif->hwgroup;
582
583 hwgroup->reset_timeout = jiffies + WAIT_WORSTCASE;
584 hwgroup->handler = &reset_ihandler;
585 hwgroup->timer.expires = jiffies + (HZ/20);
586 add_timer(&(hwgroup->timer));
587 }
588
589 #ifdef CONFIG_BLK_DEV_IDECD
590
591
592
593
594
595
596
597
598 static int atapi_reset_handler (ide_hwgroup_t *hwgroup)
599 {
600 ide_hwif_t *hwif = hwgroup->hwif;
601 ide_drive_t *drive = hwgroup->drive;
602 byte stat;
603
604 OUT_BYTE (drive->select.all, IDE_SELECT_REG);
605 udelay (10);
606
607 if (!OK_STAT(stat=GET_STAT(), 0, BUSY_STAT)) {
608 if (jiffies < hwgroup->reset_timeout) {
609 start_reset_timer (hwif);
610 return 1;
611 }
612 printk("%s: ATAPI reset timed-out, status=0x%02x\n", drive->name, stat);
613 return ide_do_reset (drive);
614 }
615 hwgroup->doing_atapi_reset = 0;
616 hwgroup->handler = NULL;
617 hwgroup->reset_timeout = 0;
618 printk("%s: ATAPI reset complete\n", drive->name);
619 return 0;
620 }
621 #endif
622
623
624
625
626
627
628
629
630
631 static int reset_handler (ide_hwgroup_t *hwgroup)
632 {
633 ide_hwif_t *hwif = hwgroup->hwif;
634 ide_drive_t *drive = hwgroup->drive;
635 byte tmp;
636
637 #ifdef CONFIG_BLK_DEV_IDECD
638 if (hwgroup->doing_atapi_reset)
639 return atapi_reset_handler(hwgroup);
640 #endif
641
642 if (!OK_STAT(tmp=GET_STAT(), 0, BUSY_STAT)) {
643 if (jiffies < hwgroup->reset_timeout) {
644 start_reset_timer (hwif);
645 return 1;
646 }
647 printk("%s: reset timed-out, status=0x%02x\n", hwif->name, tmp);
648 } else {
649 printk("%s: reset: ", hwif->name);
650 if ((tmp = GET_ERR()) == 1)
651 printk("success\n");
652 else {
653 printk("master: ");
654 switch (tmp & 0x7f) {
655 case 1: printk("passed");
656 break;
657 case 2: printk("formatter device error");
658 break;
659 case 3: printk("sector buffer error");
660 break;
661 case 4: printk("ECC circuitry error");
662 break;
663 case 5: printk("controlling MPU error");
664 break;
665 default:printk("error (0x%02x?)", tmp);
666 }
667 if (tmp & 0x80)
668 printk("; slave: failed");
669 printk("\n");
670 }
671 }
672 hwgroup->handler = NULL;
673 hwgroup->reset_timeout = 0;
674 return 0;
675 }
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692 int ide_do_reset (ide_drive_t *drive)
693 {
694 unsigned int unit;
695 unsigned long flags;
696 ide_hwif_t *hwif = HWIF(drive);
697 ide_hwgroup_t *hwgroup = HWGROUP(drive);
698
699 save_flags(flags);
700 cli();
701
702 #ifdef CONFIG_BLK_DEV_IDECD
703
704 if (drive->media == cdrom) {
705 if (!hwgroup->doing_atapi_reset) {
706 hwgroup->doing_atapi_reset = 1;
707 if (!drive->keep_settings)
708 drive->unmask = 0;
709 OUT_BYTE (drive->select.all, IDE_SELECT_REG);
710 udelay (20);
711 OUT_BYTE (WIN_SRST, IDE_COMMAND_REG);
712 hwgroup->reset_timeout = jiffies + WAIT_WORSTCASE;
713 start_reset_timer (hwif);
714 restore_flags (flags);
715 return 1;
716 }
717 }
718 hwgroup->doing_atapi_reset = 0;
719 #endif
720
721
722
723
724
725 for (unit = 0; unit < MAX_DRIVES; ++unit) {
726 ide_drive_t *rdrive = &hwif->drives[unit];
727 rdrive->special.b.set_geometry = 1;
728 rdrive->special.b.recalibrate = 1;
729 rdrive->special.b.set_multmode = 0;
730 if (OK_TO_RESET_CONTROLLER)
731 rdrive->mult_count = 0;
732 if (!rdrive->keep_settings) {
733 rdrive->mult_req = 0;
734 rdrive->unmask = 0;
735 }
736 if (rdrive->mult_req != rdrive->mult_count)
737 rdrive->special.b.set_multmode = 1;
738 }
739
740 #if OK_TO_RESET_CONTROLLER
741
742
743
744
745
746
747
748
749 OUT_BYTE(drive->ctl|6,IDE_CONTROL_REG);
750 udelay(5);
751 OUT_BYTE(drive->ctl|2,IDE_CONTROL_REG);
752 hwgroup->reset_timeout = jiffies + WAIT_WORSTCASE;
753 start_reset_timer (hwif);
754 #endif
755
756 restore_flags (flags);
757 return OK_TO_RESET_CONTROLLER;
758 }
759
760
761
762
763 static void end_drive_cmd (ide_drive_t *drive, byte stat, byte err)
764 {
765 unsigned long flags;
766 struct request *rq = HWGROUP(drive)->rq;
767 byte *args = (byte *) rq->buffer;
768
769 rq->errors = !OK_STAT(stat,READY_STAT,BAD_STAT);
770 if (args) {
771 args[0] = stat;
772 args[1] = err;
773 args[2] = IN_BYTE(IDE_NSECTOR_REG);
774 }
775 save_flags(flags);
776 cli();
777 up(rq->sem);
778 HWGROUP(drive)->rq = NULL;
779 restore_flags(flags);
780 }
781
782
783
784
785 byte ide_dump_status (ide_drive_t *drive, const char *msg, byte stat)
786 {
787 unsigned long flags;
788 byte err = 0;
789
790 save_flags (flags);
791 sti();
792 printk("%s: %s: status=0x%02x", drive->name, msg, stat);
793 #if FANCY_STATUS_DUMPS
794 if (drive->media == disk) {
795 printk(" { ");
796 if (stat & BUSY_STAT)
797 printk("Busy ");
798 else {
799 if (stat & READY_STAT) printk("DriveReady ");
800 if (stat & WRERR_STAT) printk("DeviceFault ");
801 if (stat & SEEK_STAT) printk("SeekComplete ");
802 if (stat & DRQ_STAT) printk("DataRequest ");
803 if (stat & ECC_STAT) printk("CorrectedError ");
804 if (stat & INDEX_STAT) printk("Index ");
805 if (stat & ERR_STAT) printk("Error ");
806 }
807 printk("}");
808 }
809 #endif
810 printk("\n");
811 if ((stat & (BUSY_STAT|ERR_STAT)) == ERR_STAT) {
812 err = GET_ERR();
813 printk("%s: %s: error=0x%02x", drive->name, msg, err);
814 #if FANCY_STATUS_DUMPS
815 if (drive->media == disk) {
816 printk(" { ");
817 if (err & BBD_ERR) printk("BadSector ");
818 if (err & ECC_ERR) printk("UncorrectableError ");
819 if (err & ID_ERR) printk("SectorIdNotFound ");
820 if (err & ABRT_ERR) printk("DriveStatusError ");
821 if (err & TRK0_ERR) printk("TrackZeroNotFound ");
822 if (err & MARK_ERR) printk("AddrMarkNotFound ");
823 printk("}");
824 if (err & (BBD_ERR|ECC_ERR|ID_ERR|MARK_ERR)) {
825 byte cur = IN_BYTE(IDE_SELECT_REG);
826 if (cur & 0x40) {
827 printk(", LBAsect=%ld", (unsigned long)
828 ((cur&0xf)<<24)
829 |(IN_BYTE(IDE_HCYL_REG)<<16)
830 |(IN_BYTE(IDE_LCYL_REG)<<8)
831 | IN_BYTE(IDE_SECTOR_REG));
832 } else {
833 printk(", CHS=%d/%d/%d",
834 (IN_BYTE(IDE_HCYL_REG)<<8) +
835 IN_BYTE(IDE_LCYL_REG),
836 cur & 0xf,
837 IN_BYTE(IDE_SECTOR_REG));
838 }
839 if (HWGROUP(drive)->rq)
840 printk(", sector=%ld", HWGROUP(drive)->rq->sector);
841 }
842 }
843 #endif
844 printk("\n");
845 }
846 restore_flags (flags);
847 return err;
848 }
849
850
851
852
853
854
855
856
857 static void try_to_flush_leftover_data (ide_drive_t *drive)
858 {
859 int i = (drive->mult_count ? drive->mult_count : 1) * SECTOR_WORDS;
860
861 while (i > 0) {
862 unsigned long buffer[16];
863 unsigned int wcount = (i > 16) ? 16 : i;
864 i -= wcount;
865 ide_input_data (drive, buffer, wcount);
866 }
867 }
868
869
870
871
872
873
874
875
876 int ide_error (ide_drive_t *drive, const char *msg, byte stat)
877 {
878 struct request *rq;
879 byte err;
880
881 err = ide_dump_status(drive, msg, stat);
882 if ((rq = HWGROUP(drive)->rq) == NULL || drive == NULL)
883 return 0;
884 if (rq->cmd == IDE_DRIVE_CMD) {
885 end_drive_cmd(drive, stat, err);
886 return 0;
887 }
888 if (stat & BUSY_STAT) {
889 rq->errors |= ERROR_RESET;
890 } else {
891 if (drive->media == disk && (stat & ERR_STAT)) {
892
893 if (err & BBD_ERR)
894 rq->errors = ERROR_MAX;
895 else if (err & TRK0_ERR)
896 rq->errors |= ERROR_RECAL;
897 }
898 if ((stat & DRQ_STAT) && rq->cmd != WRITE)
899 try_to_flush_leftover_data(drive);
900 }
901 if (GET_STAT() & (BUSY_STAT|DRQ_STAT))
902 rq->errors |= ERROR_RESET;
903
904 #ifdef CONFIG_BLK_DEV_TRITON
905 if (rq->errors > 3 && drive->using_dma) {
906 drive->using_dma = 0;
907 printk("%s: DMA disabled\n", drive->name);
908 --rq->errors;
909 return 0;
910 }
911 #endif
912 if (rq->errors >= ERROR_MAX)
913 ide_end_request(0, HWGROUP(drive));
914 else {
915 if ((rq->errors & ERROR_RESET) == ERROR_RESET) {
916 ++rq->errors;
917 return ide_do_reset(drive);
918 } else if ((rq->errors & ERROR_RECAL) == ERROR_RECAL)
919 drive->special.b.recalibrate = 1;
920 ++rq->errors;
921 }
922 return 0;
923 }
924
925
926
927
928 static void read_intr (ide_drive_t *drive)
929 {
930 byte stat;
931 int i;
932 unsigned int msect, nsect;
933 struct request *rq;
934
935 if (!OK_STAT(stat=GET_STAT(),DATA_READY,BAD_R_STAT)) {
936 sti();
937 if (!ide_error(drive, "read_intr", stat))
938 IDE_DO_REQUEST;
939 return;
940 }
941 msect = drive->mult_count;
942 read_next:
943 rq = HWGROUP(drive)->rq;
944 if (msect) {
945 if ((nsect = rq->current_nr_sectors) > msect)
946 nsect = msect;
947 msect -= nsect;
948 } else
949 nsect = 1;
950 ide_input_data(drive, rq->buffer, nsect * SECTOR_WORDS);
951 #ifdef DEBUG
952 printk("%s: read: sectors(%ld-%ld), buffer=0x%08lx, remaining=%ld\n",
953 drive->name, rq->sector, rq->sector+nsect-1,
954 (unsigned long) rq->buffer+(nsect<<9), rq->nr_sectors-nsect);
955 #endif
956 rq->sector += nsect;
957 rq->buffer += nsect<<9;
958 rq->errors = 0;
959 i = (rq->nr_sectors -= nsect);
960 if ((rq->current_nr_sectors -= nsect) <= 0)
961 ide_end_request(1, HWGROUP(drive));
962 if (i > 0) {
963 if (msect)
964 goto read_next;
965 ide_set_handler (drive, &read_intr);
966 return;
967 }
968 IDE_DO_REQUEST;
969 }
970
971
972
973
974 static void write_intr (ide_drive_t *drive)
975 {
976 byte stat;
977 int i;
978 struct request *rq = HWGROUP(drive)->rq;
979
980 if (OK_STAT(stat=GET_STAT(),DRIVE_READY,drive->bad_wstat)) {
981 #ifdef DEBUG
982 printk("%s: write: sector %ld, buffer=0x%08lx, remaining=%ld\n",
983 drive->name, rq->sector, (unsigned long) rq->buffer,
984 rq->nr_sectors-1);
985 #endif
986 if ((rq->nr_sectors == 1) ^ ((stat & DRQ_STAT) != 0)) {
987 rq->sector++;
988 rq->buffer += 512;
989 rq->errors = 0;
990 i = --rq->nr_sectors;
991 --rq->current_nr_sectors;
992 if (rq->current_nr_sectors <= 0)
993 ide_end_request(1, HWGROUP(drive));
994 if (i > 0) {
995 ide_output_data (drive, rq->buffer, SECTOR_WORDS);
996 ide_set_handler (drive, &write_intr);
997 return;
998 }
999 IDE_DO_REQUEST;
1000 return;
1001 }
1002 }
1003 sti();
1004 if (!ide_error(drive, "write_intr", stat))
1005 IDE_DO_REQUEST;
1006 }
1007
1008
1009
1010
1011
1012 static void multwrite (ide_drive_t *drive)
1013 {
1014 struct request *rq = &HWGROUP(drive)->wrq;
1015 unsigned int mcount = drive->mult_count;
1016
1017 do {
1018 unsigned int nsect = rq->current_nr_sectors;
1019 if (nsect > mcount)
1020 nsect = mcount;
1021 mcount -= nsect;
1022
1023 ide_output_data(drive, rq->buffer, nsect<<7);
1024 #ifdef DEBUG
1025 printk("%s: multwrite: sector %ld, buffer=0x%08lx, count=%d, remaining=%ld\n",
1026 drive->name, rq->sector, (unsigned long) rq->buffer,
1027 nsect, rq->nr_sectors - nsect);
1028 #endif
1029 if ((rq->nr_sectors -= nsect) <= 0)
1030 break;
1031 if ((rq->current_nr_sectors -= nsect) == 0) {
1032 if ((rq->bh = rq->bh->b_reqnext) != NULL) {
1033 rq->current_nr_sectors = rq->bh->b_size>>9;
1034 rq->buffer = rq->bh->b_data;
1035 } else {
1036 panic("%s: buffer list corrupted\n", drive->name);
1037 break;
1038 }
1039 } else {
1040 rq->buffer += nsect << 9;
1041 }
1042 } while (mcount);
1043 }
1044
1045
1046
1047
1048 static void multwrite_intr (ide_drive_t *drive)
1049 {
1050 byte stat;
1051 int i;
1052 struct request *rq = &HWGROUP(drive)->wrq;
1053
1054 if (OK_STAT(stat=GET_STAT(),DRIVE_READY,drive->bad_wstat)) {
1055 if (stat & DRQ_STAT) {
1056 if (rq->nr_sectors) {
1057 multwrite(drive);
1058 ide_set_handler (drive, &multwrite_intr);
1059 return;
1060 }
1061 } else {
1062 if (!rq->nr_sectors) {
1063 rq = HWGROUP(drive)->rq;
1064 for (i = rq->nr_sectors; i > 0;){
1065 i -= rq->current_nr_sectors;
1066 ide_end_request(1, HWGROUP(drive));
1067 }
1068 IDE_DO_REQUEST;
1069 return;
1070 }
1071 }
1072 }
1073 sti();
1074 if (!ide_error(drive, "multwrite_intr", stat))
1075 IDE_DO_REQUEST;
1076 }
1077
1078
1079
1080
1081
1082 static void ide_cmd(ide_drive_t *drive, byte cmd, byte nsect, ide_handler_t *handler)
1083 {
1084 ide_set_handler (drive, handler);
1085 OUT_BYTE(drive->ctl,IDE_CONTROL_REG);
1086 OUT_BYTE(nsect,IDE_NSECTOR_REG);
1087 OUT_BYTE(cmd,IDE_COMMAND_REG);
1088 }
1089
1090
1091
1092
1093 static void set_multmode_intr (ide_drive_t *drive)
1094 {
1095 byte stat = GET_STAT();
1096
1097 sti();
1098 if (OK_STAT(stat,READY_STAT,BAD_STAT)) {
1099 drive->mult_count = drive->mult_req;
1100 } else {
1101 drive->mult_req = drive->mult_count = 0;
1102 drive->special.b.recalibrate = 1;
1103 (void) ide_dump_status(drive, "set_multmode", stat);
1104 }
1105 IDE_DO_REQUEST;
1106 }
1107
1108
1109
1110
1111 static void set_geometry_intr (ide_drive_t *drive)
1112 {
1113 byte stat = GET_STAT();
1114
1115 sti();
1116 if (!OK_STAT(stat,READY_STAT,BAD_STAT))
1117 if (ide_error(drive, "set_geometry_intr", stat))
1118 return;
1119 IDE_DO_REQUEST;
1120 }
1121
1122
1123
1124
1125 static void recal_intr (ide_drive_t *drive)
1126 {
1127 byte stat = GET_STAT();
1128
1129 sti();
1130 if (!OK_STAT(stat,READY_STAT,BAD_STAT))
1131 if (ide_error(drive, "recal_intr", stat))
1132 return;
1133 IDE_DO_REQUEST;
1134 }
1135
1136
1137
1138
1139 static void drive_cmd_intr (ide_drive_t *drive)
1140 {
1141 byte stat = GET_STAT();
1142
1143 sti();
1144 if (OK_STAT(stat,READY_STAT,BAD_STAT))
1145 end_drive_cmd (drive, stat, GET_ERR());
1146 else if (ide_error(drive, "drive_cmd", stat))
1147 return;
1148 IDE_DO_REQUEST;
1149 }
1150
1151
1152
1153
1154
1155
1156 static inline void do_special (ide_drive_t *drive)
1157 {
1158 special_t *s = &drive->special;
1159 #ifdef DEBUG
1160 printk("%s: do_special: 0x%02x\n", drive->name, s->all);
1161 #endif
1162 if (s->b.set_geometry) {
1163 s->b.set_geometry = 0;
1164 if (drive->media == disk) {
1165 OUT_BYTE(drive->sect,IDE_SECTOR_REG);
1166 OUT_BYTE(drive->cyl,IDE_LCYL_REG);
1167 OUT_BYTE(drive->cyl>>8,IDE_HCYL_REG);
1168 OUT_BYTE(((drive->head-1)|drive->select.all)&0xBF,IDE_SELECT_REG);
1169 ide_cmd(drive, WIN_SPECIFY, drive->sect, &set_geometry_intr);
1170 }
1171 } else if (s->b.recalibrate) {
1172 s->b.recalibrate = 0;
1173 if (drive->media == disk) {
1174 ide_cmd(drive, WIN_RESTORE, drive->sect, &recal_intr);
1175 }
1176 } else if (s->b.set_multmode) {
1177 s->b.set_multmode = 0;
1178 if (drive->media == disk) {
1179 if (drive->id && drive->mult_req > drive->id->max_multsect)
1180 drive->mult_req = drive->id->max_multsect;
1181 ide_cmd(drive, WIN_SETMULT, drive->mult_req, &set_multmode_intr);
1182 } else
1183 drive->mult_req = 0;
1184 } else if (s->all) {
1185 s->all = 0;
1186 printk("%s: bad special flag: 0x%02x\n", drive->name, s->all);
1187 }
1188 }
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201 int ide_wait_stat (ide_drive_t *drive, byte good, byte bad, unsigned long timeout)
1202 {
1203 byte stat;
1204 unsigned long flags;
1205 test:
1206 udelay(1);
1207 if (OK_STAT((stat = GET_STAT()), good, bad))
1208 return 0;
1209 if (!(stat & BUSY_STAT)) {
1210 (void) ide_error(drive, "status error", stat);
1211 return 1;
1212 }
1213
1214 save_flags(flags);
1215 sti();
1216 timeout += jiffies;
1217 do {
1218 if (!((stat = GET_STAT()) & BUSY_STAT)) {
1219 restore_flags(flags);
1220 goto test;
1221 }
1222 } while (jiffies <= timeout);
1223
1224 restore_flags(flags);
1225 (void) ide_error(drive, "status timeout", GET_STAT());
1226 return 1;
1227 }
1228
1229
1230
1231
1232
1233
1234 static inline void do_rw_disk (ide_drive_t *drive, struct request *rq, unsigned long block)
1235 {
1236 unsigned short io_base = HWIF(drive)->io_base;
1237
1238 OUT_BYTE(drive->ctl,IDE_CONTROL_REG);
1239 OUT_BYTE(rq->nr_sectors,io_base+IDE_NSECTOR_OFFSET);
1240 if (drive->select.b.lba) {
1241 #ifdef DEBUG
1242 printk("%s: %sing: LBAsect=%ld, sectors=%ld, buffer=0x%08lx\n",
1243 drive->name, (rq->cmd==READ)?"read":"writ",
1244 block, rq->nr_sectors, (unsigned long) rq->buffer);
1245 #endif
1246 OUT_BYTE(block,io_base+IDE_SECTOR_OFFSET);
1247 OUT_BYTE(block>>=8,io_base+IDE_LCYL_OFFSET);
1248 OUT_BYTE(block>>=8,io_base+IDE_HCYL_OFFSET);
1249 OUT_BYTE(((block>>8)&0x0f)|drive->select.all,io_base+IDE_SELECT_OFFSET);
1250 } else {
1251 unsigned int sect,head,cyl,track;
1252 track = block / drive->sect;
1253 sect = block % drive->sect + 1;
1254 OUT_BYTE(sect,io_base+IDE_SECTOR_OFFSET);
1255 head = track % drive->head;
1256 cyl = track / drive->head;
1257 OUT_BYTE(cyl,io_base+IDE_LCYL_OFFSET);
1258 OUT_BYTE(cyl>>8,io_base+IDE_HCYL_OFFSET);
1259 OUT_BYTE(head|drive->select.all,io_base+IDE_SELECT_OFFSET);
1260 #ifdef DEBUG
1261 printk("%s: %sing: CHS=%d/%d/%d, sectors=%ld, buffer=0x%08lx\n",
1262 drive->name, (rq->cmd==READ)?"read":"writ", cyl,
1263 head, sect, rq->nr_sectors, (unsigned long) rq->buffer);
1264 #endif
1265 }
1266 if (rq->cmd == READ) {
1267 #ifdef CONFIG_BLK_DEV_TRITON
1268 if (drive->using_dma && !(HWIF(drive)->dmaproc(ide_dma_read, drive)))
1269 return;
1270 #endif
1271 ide_set_handler(drive, &read_intr);
1272 OUT_BYTE(drive->mult_count ? WIN_MULTREAD : WIN_READ, io_base+IDE_COMMAND_OFFSET);
1273 return;
1274 }
1275 if (rq->cmd == WRITE) {
1276 #ifdef CONFIG_BLK_DEV_TRITON
1277 if (drive->using_dma && !(HWIF(drive)->dmaproc(ide_dma_write, drive)))
1278 return;
1279 #endif
1280 OUT_BYTE(drive->mult_count ? WIN_MULTWRITE : WIN_WRITE, io_base+IDE_COMMAND_OFFSET);
1281 if (ide_wait_stat(drive, DATA_READY, drive->bad_wstat, WAIT_DRQ)) {
1282 printk("%s: no DRQ after issuing %s\n", drive->name,
1283 drive->mult_count ? "MULTWRITE" : "WRITE");
1284 return;
1285 }
1286 if (!drive->unmask)
1287 cli();
1288 if (drive->mult_count) {
1289 HWGROUP(drive)->wrq = *rq;
1290 ide_set_handler (drive, &multwrite_intr);
1291 multwrite(drive);
1292 } else {
1293 ide_set_handler (drive, &write_intr);
1294 ide_output_data(drive, rq->buffer, SECTOR_WORDS);
1295 }
1296 return;
1297 }
1298 if (rq->cmd == IDE_DRIVE_CMD) {
1299 byte *args = rq->buffer;
1300 if (args) {
1301 printk("%s: DRIVE_CMD cmd=0x%02x sc=0x%02x fr=0x%02x\n",
1302 drive->name, args[0], args[1], args[2]);
1303 OUT_BYTE(args[2],io_base+IDE_FEATURE_OFFSET);
1304 ide_cmd(drive, args[0], args[1], &drive_cmd_intr);
1305 return;
1306 } else {
1307
1308
1309
1310
1311 #ifdef DEBUG
1312 printk("%s: DRIVE_CMD (null)\n", drive->name);
1313 #endif
1314 end_drive_cmd(drive, GET_STAT(), GET_ERR());
1315 return;
1316 }
1317 }
1318 printk("%s: bad command: %d\n", drive->name, rq->cmd);
1319 ide_end_request(0, HWGROUP(drive));
1320 }
1321
1322
1323
1324
1325 static inline void do_request (ide_hwif_t *hwif, struct request *rq)
1326 {
1327 unsigned int minor, unit;
1328 unsigned long block, blockend;
1329 ide_drive_t *drive;
1330
1331 sti();
1332 #ifdef DEBUG
1333 printk("%s: ide_do_request: current=0x%08lx\n", hwif->name, (unsigned long) rq);
1334 #endif
1335 minor = MINOR(rq->rq_dev);
1336 unit = minor >> PARTN_BITS;
1337 if (MAJOR(rq->rq_dev) != hwif->major || unit >= MAX_DRIVES) {
1338 printk("%s: bad device number: %s\n",
1339 hwif->name, kdevname(rq->rq_dev));
1340 goto kill_rq;
1341 }
1342 drive = &hwif->drives[unit];
1343 #ifdef DEBUG
1344 if (rq->bh && !rq->bh->b_lock) {
1345 printk("%s: block not locked\n", drive->name);
1346 goto kill_rq;
1347 }
1348 #endif
1349 block = rq->sector;
1350 blockend = block + rq->nr_sectors;
1351 if ((blockend < block) || (blockend > drive->part[minor&PARTN_MASK].nr_sects)) {
1352 printk("%s%c: bad access: block=%ld, count=%ld\n", drive->name,
1353 (minor&PARTN_MASK)?'0'+(minor&PARTN_MASK):' ', block, rq->nr_sectors);
1354 goto kill_rq;
1355 }
1356 block += drive->part[minor&PARTN_MASK].start_sect + drive->sect0;
1357 #if FAKE_FDISK_FOR_EZDRIVE
1358 if (block == 0 && drive->ezdrive) {
1359 block = 1;
1360 printk("%s: [EZD] accessing sector 1 instead of sector 0\n", drive->name);
1361 }
1362 #endif
1363 ((ide_hwgroup_t *)hwif->hwgroup)->drive = drive;
1364 #if (DISK_RECOVERY_TIME > 0)
1365 while ((read_timer() - hwif->last_time) < DISK_RECOVERY_TIME);
1366 #endif
1367 #if SUPPORT_HT6560B
1368 if (hwif->select)
1369 ide_hwif_select (hwif);
1370 #endif
1371 OUT_BYTE(drive->select.all,IDE_SELECT_REG);
1372 if (ide_wait_stat(drive, drive->ready_stat, BUSY_STAT|DRQ_STAT, WAIT_READY)) {
1373 printk("%s: drive not ready for command\n", drive->name);
1374 return;
1375
1376 }
1377 if (!drive->special.all) {
1378 #ifdef CONFIG_BLK_DEV_IDECD
1379 switch (drive->media) {
1380 case disk:
1381 do_rw_disk (drive, rq, block);
1382 return;
1383 case cdrom:
1384 ide_do_rw_cdrom (drive, block);
1385 return;
1386 default:
1387 printk("%s: media type %d not supported\n",
1388 drive->name, drive->media);
1389 goto kill_rq;
1390 }
1391 #else
1392 do_rw_disk (drive, rq, block);
1393 return;
1394 #endif;
1395 }
1396 do_special(drive);
1397 return;
1398 kill_rq:
1399 ide_end_request(0, hwif->hwgroup);
1400 }
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417 void ide_do_request (ide_hwgroup_t *hwgroup)
1418 {
1419 cli();
1420 if (hwgroup->handler != NULL) {
1421 printk("%s: EEeekk!! handler not NULL in ide_do_request()\n", hwgroup->hwif->name);
1422 return;
1423 }
1424 do {
1425 ide_hwif_t *hwif = hwgroup->hwif;
1426 struct request *rq;
1427 if ((rq = hwgroup->rq) == NULL) {
1428 hwgroup->drive = NULL;
1429 do {
1430 rq = blk_dev[hwif->major].current_request;
1431 if (rq != NULL && rq->rq_status != RQ_INACTIVE)
1432 goto got_rq;
1433 } while ((hwif = hwif->next) != hwgroup->hwif);
1434 return;
1435 got_rq:
1436 blk_dev[hwif->major].current_request = rq->next;
1437 }
1438 do_request(hwgroup->hwif = hwif, hwgroup->rq = rq);
1439 cli();
1440 } while (hwgroup->handler == NULL);
1441 }
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454 static void do_hwgroup_request (ide_hwgroup_t *hwgroup)
1455 {
1456 if (hwgroup->handler == NULL) {
1457 ide_hwif_t *hgif = hwgroup->hwif;
1458 ide_hwif_t *hwif = hgif;
1459 do {
1460 disable_irq(hwif->irq);
1461 } while ((hwif = hwif->next) != hgif);
1462 ide_do_request (hwgroup);
1463 do {
1464 enable_irq(hwif->irq);
1465 } while ((hwif = hwif->next) != hgif);
1466 }
1467 }
1468
1469 static void do_ide0_request (void)
1470 {
1471 do_hwgroup_request (ide_hwifs[0].hwgroup);
1472 }
1473
1474 static void do_ide1_request (void)
1475 {
1476 do_hwgroup_request (ide_hwifs[1].hwgroup);
1477 }
1478
1479 static void do_ide2_request (void)
1480 {
1481 do_hwgroup_request (ide_hwifs[2].hwgroup);
1482 }
1483
1484 static void do_ide3_request (void)
1485 {
1486 do_hwgroup_request (ide_hwifs[3].hwgroup);
1487 }
1488
1489 static void timer_expiry (unsigned long data)
1490 {
1491 ide_hwgroup_t *hwgroup = (ide_hwgroup_t *) data;
1492 ide_drive_t *drive = hwgroup->drive;
1493 unsigned long flags;
1494
1495 save_flags(flags);
1496 cli();
1497
1498 if (hwgroup->reset_timeout != 0) {
1499 if (!reset_handler(hwgroup))
1500 do_hwgroup_request (hwgroup);
1501 } else if (hwgroup->handler == NULL) {
1502 sti();
1503 printk("%s: marginal timeout\n", drive->name);
1504 } else {
1505 hwgroup->handler = NULL;
1506 if (hwgroup->hwif->dmaproc)
1507 (void) hwgroup->hwif->dmaproc (ide_dma_abort, drive);
1508 if (!ide_error(drive, "irq timeout", GET_STAT()))
1509 do_hwgroup_request (hwgroup);
1510 }
1511 restore_flags(flags);
1512 }
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537 static void unexpected_intr (int irq, ide_hwgroup_t *hwgroup)
1538 {
1539 byte stat;
1540 unsigned int unit;
1541 ide_hwif_t *hwif = hwgroup->hwif;
1542
1543
1544
1545
1546 if (hwgroup->reset_timeout != 0) {
1547 if (!reset_handler(hwgroup))
1548 do_hwgroup_request (hwgroup);
1549 return;
1550 }
1551
1552
1553
1554
1555 do {
1556 if (hwif->irq == irq) {
1557 #if SUPPORT_HT6560B
1558 if (hwif->select)
1559 ide_hwif_select (hwif);
1560 #endif
1561 for (unit = 0; unit < MAX_DRIVES; ++unit) {
1562 ide_drive_t *drive = &hwif->drives[unit];
1563 if (!drive->present)
1564 continue;
1565 if (!OK_STAT(stat=GET_STAT(), drive->ready_stat, BAD_STAT))
1566 (void) ide_dump_status(drive, "unexpected_intr", stat);
1567 if ((stat & DRQ_STAT))
1568 try_to_flush_leftover_data(drive);
1569 }
1570 }
1571 } while ((hwif = hwif->next) != hwgroup->hwif);
1572 }
1573
1574
1575
1576
1577 static void ide_intr (int irq, struct pt_regs *regs)
1578 {
1579 ide_hwgroup_t *hwgroup = irq_to_hwgroup[irq];
1580 ide_handler_t *handler;
1581
1582 if (irq == hwgroup->hwif->irq && (handler = hwgroup->handler) != NULL) {
1583 ide_drive_t *drive = hwgroup->drive;
1584 hwgroup->handler = NULL;
1585 del_timer(&(hwgroup->timer));
1586 if (drive->unmask)
1587 sti();
1588 handler(drive);
1589 } else {
1590 unexpected_intr(irq, hwgroup);
1591 }
1592 cli();
1593 }
1594
1595
1596
1597
1598
1599 static ide_drive_t *get_info_ptr (kdev_t i_rdev)
1600 {
1601 int major = MAJOR(i_rdev);
1602 unsigned int h;
1603
1604 for (h = 0; h < MAX_HWIFS; ++h) {
1605 ide_hwif_t *hwif = &ide_hwifs[h];
1606 if (hwif->present && major == hwif->major) {
1607 unsigned unit = DEVICE_NR(i_rdev);
1608 if (unit < MAX_DRIVES) {
1609 ide_drive_t *drive = &hwif->drives[unit];
1610 if (drive->present)
1611 return drive;
1612 } else if (major == IDE0_MAJOR && unit < 4) {
1613 printk("ide: probable bad entry for /dev/hd%c%d\n",
1614 'a' + unit, MINOR(i_rdev) & PARTN_MASK);
1615 printk("ide: to fix it, run: /usr/src/linux/drivers/block/MAKEDEV.ide\n");
1616 }
1617 break;
1618 }
1619 }
1620 return NULL;
1621 }
1622
1623
1624
1625
1626
1627
1628
1629 int ide_do_drive_cmd(kdev_t rdev, char *args)
1630 {
1631 unsigned long flags;
1632 unsigned int major = MAJOR(rdev);
1633 struct request rq, *cur_rq;
1634 struct blk_dev_struct *bdev;
1635 struct semaphore sem = MUTEX_LOCKED;
1636
1637
1638 rq.buffer = args;
1639 rq.cmd = IDE_DRIVE_CMD;
1640 rq.errors = 0;
1641 rq.sector = 0;
1642 rq.nr_sectors = 0;
1643 rq.current_nr_sectors = 0;
1644 rq.sem = &sem;
1645 rq.bh = NULL;
1646 rq.bhtail = NULL;
1647 rq.next = NULL;
1648 rq.rq_status = RQ_ACTIVE;
1649 rq.rq_dev = rdev;
1650 bdev = &blk_dev[major];
1651
1652 save_flags(flags);
1653 cli();
1654 cur_rq = bdev->current_request;
1655 if (cur_rq == NULL) {
1656 bdev->current_request = &rq;
1657 bdev->request_fn();
1658 } else {
1659 while (cur_rq->next != NULL)
1660 cur_rq = cur_rq->next;
1661 cur_rq->next = &rq;
1662 }
1663
1664 down(&sem);
1665 restore_flags(flags);
1666 return rq.errors ? -EIO : 0;
1667 }
1668
1669 static int ide_open(struct inode * inode, struct file * filp)
1670 {
1671 ide_drive_t *drive;
1672 unsigned long flags;
1673
1674 if ((drive = get_info_ptr(inode->i_rdev)) == NULL)
1675 return -ENODEV;
1676 save_flags(flags);
1677 cli();
1678 while (drive->busy)
1679 sleep_on(&drive->wqueue);
1680 drive->usage++;
1681 restore_flags(flags);
1682 #ifdef CONFIG_BLK_DEV_IDECD
1683 if (drive->media == cdrom)
1684 return ide_cdrom_open (inode, filp, drive);
1685 #endif
1686 if (drive->removeable) {
1687 byte door_lock[] = {WIN_DOORLOCK,0,0,0};
1688 check_disk_change(inode->i_rdev);
1689 ide_do_drive_cmd(inode->i_rdev, door_lock);
1690 }
1691 return 0;
1692 }
1693
1694
1695
1696
1697
1698 static void ide_release(struct inode * inode, struct file * file)
1699 {
1700 ide_drive_t *drive;
1701
1702 if ((drive = get_info_ptr(inode->i_rdev)) != NULL) {
1703 sync_dev(inode->i_rdev);
1704 drive->usage--;
1705 #ifdef CONFIG_BLK_DEV_IDECD
1706 if (drive->media == cdrom)
1707 ide_cdrom_release (inode, file, drive);
1708 else
1709 #endif
1710 if (drive->removeable) {
1711 byte door_unlock[] = {WIN_DOORUNLOCK,0,0,0};
1712 invalidate_buffers(inode->i_rdev);
1713 ide_do_drive_cmd(inode->i_rdev, door_unlock);
1714 }
1715 }
1716 }
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726 static int revalidate_disk(kdev_t i_rdev)
1727 {
1728 ide_drive_t *drive;
1729 unsigned int p, major, minor;
1730 long flags;
1731
1732 if ((drive = get_info_ptr(i_rdev)) == NULL)
1733 return -ENODEV;
1734
1735 major = MAJOR(i_rdev);
1736 minor = drive->select.b.unit << PARTN_BITS;
1737 save_flags(flags);
1738 cli();
1739 if (drive->busy || (drive->usage > 1)) {
1740 restore_flags(flags);
1741 return -EBUSY;
1742 };
1743 drive->busy = 1;
1744 restore_flags(flags);
1745
1746 for (p = 0; p < (1<<PARTN_BITS); ++p) {
1747 if (drive->part[p].nr_sects > 0) {
1748 kdev_t devp = MKDEV(major, minor+p);
1749 sync_dev (devp);
1750 invalidate_inodes (devp);
1751 invalidate_buffers (devp);
1752 }
1753 drive->part[p].start_sect = 0;
1754 drive->part[p].nr_sects = 0;
1755 };
1756
1757 drive->part[0].nr_sects = current_capacity(drive);
1758 if (drive->media == disk)
1759 resetup_one_dev(HWIF(drive)->gd, drive->select.b.unit);
1760
1761 drive->busy = 0;
1762 wake_up(&drive->wqueue);
1763 return 0;
1764 }
1765
1766 static int write_fs_long (unsigned long useraddr, long value)
1767 {
1768 int err;
1769
1770 if (NULL == (long *)useraddr)
1771 return -EINVAL;
1772 if ((err = verify_area(VERIFY_WRITE, (long *)useraddr, sizeof(long))))
1773 return err;
1774 put_user((unsigned)value, (long *) useraddr);
1775 return 0;
1776 }
1777
1778 static int ide_ioctl (struct inode *inode, struct file *file,
1779 unsigned int cmd, unsigned long arg)
1780 {
1781 struct hd_geometry *loc = (struct hd_geometry *) arg;
1782 int err;
1783 ide_drive_t *drive;
1784 unsigned long flags;
1785
1786 if (!inode || !(inode->i_rdev))
1787 return -EINVAL;
1788 if ((drive = get_info_ptr(inode->i_rdev)) == NULL)
1789 return -ENODEV;
1790 switch (cmd) {
1791 case HDIO_GETGEO:
1792 if (!loc || drive->media != disk) return -EINVAL;
1793 err = verify_area(VERIFY_WRITE, loc, sizeof(*loc));
1794 if (err) return err;
1795 put_user(drive->bios_head, (byte *) &loc->heads);
1796 put_user(drive->bios_sect, (byte *) &loc->sectors);
1797 put_user(drive->bios_cyl, (unsigned short *) &loc->cylinders);
1798 put_user((unsigned)drive->part[MINOR(inode->i_rdev)&PARTN_MASK].start_sect,
1799 (unsigned long *) &loc->start);
1800 return 0;
1801
1802 case BLKFLSBUF:
1803 if(!suser()) return -EACCES;
1804 fsync_dev(inode->i_rdev);
1805 invalidate_buffers(inode->i_rdev);
1806 return 0;
1807
1808 case BLKRASET:
1809 if(!suser()) return -EACCES;
1810 if(arg > 0xff) return -EINVAL;
1811 read_ahead[MAJOR(inode->i_rdev)] = arg;
1812 return 0;
1813
1814 case BLKRAGET:
1815 return write_fs_long(arg, read_ahead[MAJOR(inode->i_rdev)]);
1816
1817 case BLKGETSIZE:
1818 return write_fs_long(arg, drive->part[MINOR(inode->i_rdev)&PARTN_MASK].nr_sects);
1819 case BLKRRPART:
1820 return revalidate_disk(inode->i_rdev);
1821
1822 case HDIO_GET_KEEPSETTINGS:
1823 return write_fs_long(arg, drive->keep_settings);
1824
1825 case HDIO_GET_UNMASKINTR:
1826 return write_fs_long(arg, drive->unmask);
1827
1828 case HDIO_GET_DMA:
1829 return write_fs_long(arg, drive->using_dma);
1830
1831 case HDIO_GET_CHIPSET:
1832 return write_fs_long(arg, drive->chipset);
1833
1834 case HDIO_GET_MULTCOUNT:
1835 return write_fs_long(arg, drive->mult_count);
1836
1837 case HDIO_GET_IDENTITY:
1838 if (!arg || (MINOR(inode->i_rdev) & PARTN_MASK))
1839 return -EINVAL;
1840 if (drive->id == NULL)
1841 return -ENOMSG;
1842 err = verify_area(VERIFY_WRITE, (char *)arg, sizeof(*drive->id));
1843 if (!err)
1844 memcpy_tofs((char *)arg, (char *)drive->id, sizeof(*drive->id));
1845 return err;
1846
1847 case HDIO_GET_NOWERR:
1848 return write_fs_long(arg, drive->bad_wstat == BAD_R_STAT);
1849
1850 case HDIO_SET_DMA:
1851 if (drive->media != disk)
1852 return -EPERM;
1853 if (!drive->id || !(drive->id->capability & 1) || !HWIF(drive)->dmaproc)
1854 return -EPERM;
1855 case HDIO_SET_KEEPSETTINGS:
1856 case HDIO_SET_UNMASKINTR:
1857 case HDIO_SET_NOWERR:
1858 if (arg > 1)
1859 return -EINVAL;
1860 case HDIO_SET_CHIPSET:
1861 if (!suser())
1862 return -EACCES;
1863 if ((MINOR(inode->i_rdev) & PARTN_MASK))
1864 return -EINVAL;
1865 save_flags(flags);
1866 cli();
1867 switch (cmd) {
1868 case HDIO_SET_DMA:
1869 if (!(HWIF(drive)->dmaproc)) {
1870 restore_flags(flags);
1871 return -EPERM;
1872 }
1873 drive->using_dma = arg;
1874 break;
1875 case HDIO_SET_KEEPSETTINGS:
1876 drive->keep_settings = arg;
1877 break;
1878 case HDIO_SET_UNMASKINTR:
1879 if (arg && disallow_unmask) {
1880 restore_flags(flags);
1881 return -EPERM;
1882 }
1883 drive->unmask = arg;
1884 break;
1885 case HDIO_SET_NOWERR:
1886 drive->bad_wstat = arg ? BAD_R_STAT : BAD_W_STAT;
1887 break;
1888 case HDIO_SET_CHIPSET:
1889 drive->chipset = arg;
1890 drive->vlb_32bit = (arg & 1);
1891 drive->vlb_sync = (arg & 2) >> 1;
1892 #ifndef VLB_SYNC
1893 if (drive->vlb_sync)
1894 printk("%s: VLB_SYNC not supported by this kernel\n", drive->name);
1895 #endif
1896 break;
1897 }
1898 restore_flags(flags);
1899 return 0;
1900
1901 case HDIO_SET_MULTCOUNT:
1902 if (!suser())
1903 return -EACCES;
1904 if (MINOR(inode->i_rdev) & PARTN_MASK)
1905 return -EINVAL;
1906 if ((drive->id != NULL) && (arg > drive->id->max_multsect))
1907 return -EINVAL;
1908 save_flags(flags);
1909 cli();
1910 if (drive->special.b.set_multmode) {
1911 restore_flags(flags);
1912 return -EBUSY;
1913 }
1914 drive->mult_req = arg;
1915 drive->special.b.set_multmode = 1;
1916 restore_flags(flags);
1917 ide_do_drive_cmd (inode->i_rdev, NULL);
1918 return (drive->mult_count == arg) ? 0 : -EIO;
1919
1920 case HDIO_DRIVE_CMD:
1921 {
1922 unsigned long args;
1923
1924 if (NULL == (long *) arg)
1925 err = ide_do_drive_cmd(inode->i_rdev,NULL);
1926 else {
1927 if (!(err = verify_area(VERIFY_READ,(long *)arg,sizeof(long))))
1928 {
1929 args = get_user((long *)arg);
1930 if (!(err = verify_area(VERIFY_WRITE,(long *)arg,sizeof(long)))) {
1931 err = ide_do_drive_cmd(inode->i_rdev,(char *)&args);
1932 put_user(args,(long *)arg);
1933 }
1934 }
1935 }
1936 return err;
1937 }
1938
1939 RO_IOCTLS(inode->i_rdev, arg);
1940
1941 default:
1942 #ifdef CONFIG_BLK_DEV_IDECD
1943 if (drive->media == cdrom)
1944 return ide_cdrom_ioctl(drive, inode, file, cmd, arg);
1945 #endif
1946 return -EPERM;
1947 }
1948 }
1949
1950 static int ide_check_media_change (kdev_t i_rdev)
1951 {
1952 ide_drive_t *drive;
1953
1954 if ((drive = get_info_ptr(i_rdev)) == NULL)
1955 return -ENODEV;
1956 #ifdef CONFIG_BLK_DEV_IDECD
1957 if (drive->media == cdrom)
1958 return ide_cdrom_check_media_change (drive);
1959 #endif
1960 if (drive->removeable)
1961 return 1;
1962 return 0;
1963 }
1964
1965 static void fixstring (byte *s, const int bytecount, const int byteswap)
1966 {
1967 byte *p = s, *end = &s[bytecount & ~1];
1968
1969 if (byteswap) {
1970
1971 for (p = end ; p != s;) {
1972 unsigned short *pp = (unsigned short *) (p -= 2);
1973 *pp = ntohs(*pp);
1974 }
1975 }
1976
1977
1978 while (s != end && *s == ' ')
1979 ++s;
1980
1981
1982 while (s != end && *s) {
1983 if (*s++ != ' ' || (s != end && *s && *s != ' '))
1984 *p++ = *(s-1);
1985 }
1986
1987
1988 while (p != end)
1989 *p++ = '\0';
1990 }
1991
1992 static inline void do_identify (ide_drive_t *drive, byte cmd)
1993 {
1994 int bswap;
1995 struct hd_driveid *id;
1996 unsigned long capacity, check;
1997
1998 id = drive->id = kmalloc (SECTOR_WORDS*4, GFP_KERNEL);
1999 ide_input_data(drive, id, SECTOR_WORDS);
2000 sti();
2001
2002
2003
2004
2005 if ((id->model[0] == 'P' && id->model[1] == 'M')
2006 || (id->model[0] == 'S' && id->model[1] == 'K')) {
2007 printk("%s: EATA SCSI HBA %.10s\n", drive->name, id->model);
2008 drive->present = 0;
2009 return;
2010 }
2011
2012
2013
2014
2015
2016 bswap = 1;
2017 if (cmd == WIN_PIDENTIFY) {
2018 if ((id->model[0] == 'N' && id->model[1] == 'E')
2019 || (id->model[0] == 'F' && id->model[1] == 'X')
2020 || (id->model[0] == 'P' && id->model[1] == 'i'))
2021 bswap = 0;
2022 }
2023 fixstring (id->model, sizeof(id->model), bswap);
2024 fixstring (id->fw_rev, sizeof(id->fw_rev), bswap);
2025 fixstring (id->serial_no, sizeof(id->serial_no), bswap);
2026
2027
2028
2029
2030 if (cmd == WIN_PIDENTIFY) {
2031 #ifdef CONFIG_BLK_DEV_IDECD
2032 byte type = (id->config >> 8) & 0x0f;
2033 #endif
2034 printk("%s: %s, ATAPI, ", drive->name, id->model);
2035 drive->media = cdrom;
2036 #ifdef CONFIG_BLK_DEV_IDECD
2037 if (type == 0 || type == 5)
2038 printk("CDROM drive\n");
2039 else
2040 printk("UNKNOWN device\n");
2041 drive->present = 1;
2042 drive->removeable = 1;
2043 #else
2044 printk("not supported by this kernel\n");
2045 #endif
2046 return;
2047 }
2048
2049
2050 if (id->config & (1<<7)) {
2051 if (id->model[0] != 'W' || id->model[1] != 'D')
2052 drive->removeable = 1;
2053 }
2054
2055 drive->media = disk;
2056
2057 if (!drive->present) {
2058 drive->present = 1;
2059 drive->cyl = drive->bios_cyl = id->cyls;
2060 drive->head = drive->bios_head = id->heads;
2061 drive->sect = drive->bios_sect = id->sectors;
2062 }
2063
2064 if ((id->field_valid & 1) && id->cur_cyls && id->cur_heads
2065 && (id->cur_heads <= 16) && id->cur_sectors)
2066 {
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077 drive->cyl = id->cur_cyls;
2078 drive->head = id->cur_heads;
2079 drive->sect = id->cur_sectors;
2080
2081
2082 capacity = drive->cyl * drive->head * drive->sect;
2083 check = (id->cur_capacity0 << 16) | id->cur_capacity1;
2084 if (check == capacity) {
2085
2086 id->cur_capacity0 = (capacity >> 0) & 0xffff;
2087 id->cur_capacity1 = (capacity >> 16) & 0xffff;
2088 }
2089 }
2090
2091 if ((!drive->head || drive->head > 16) && id->heads && id->heads <= 16) {
2092 drive->cyl = id->cyls;
2093 drive->head = id->heads;
2094 drive->sect = id->sectors;
2095 }
2096
2097 if (drive->sect == drive->bios_sect && drive->head == drive->bios_head) {
2098 if (drive->cyl > drive->bios_cyl)
2099 drive->bios_cyl = drive->cyl;
2100 }
2101
2102 (void) current_capacity (drive);
2103
2104 printk ("%s: %.40s, %ldMB w/%dKB Cache, %sCHS=%d/%d/%d",
2105 drive->name, id->model, current_capacity(drive)/2048L, id->buf_size/2,
2106 drive->select.b.lba ? "LBA, " : "",
2107 drive->bios_cyl, drive->bios_head, drive->bios_sect);
2108
2109 drive->mult_count = 0;
2110 if (id->max_multsect) {
2111 drive->mult_req = INITIAL_MULT_COUNT;
2112 if (drive->mult_req > id->max_multsect)
2113 drive->mult_req = id->max_multsect;
2114 if (drive->mult_req || ((id->multsect_valid & 1) && id->multsect))
2115 drive->special.b.set_multmode = 1;
2116 }
2117 if (HWIF(drive)->dmaproc != NULL) {
2118 if (!(HWIF(drive)->dmaproc(ide_dma_check, drive)))
2119 printk(", DMA");
2120 }
2121 printk("\n");
2122 #ifdef SUPPORT_CMD640
2123 cmd640_tune_drive(drive);
2124 #endif
2125 }
2126
2127
2128
2129
2130
2131 static void delay_10ms (void)
2132 {
2133 unsigned long timer = jiffies + (HZ + 99)/100 + 1;
2134 while (timer > jiffies);
2135 }
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147 static int try_to_identify (ide_drive_t *drive, byte cmd)
2148 {
2149 int hd_status, rc;
2150 unsigned long timeout;
2151 int irqs = 0;
2152
2153 if (!HWIF(drive)->irq) {
2154 probe_irq_off(probe_irq_on());
2155 irqs = probe_irq_on();
2156 OUT_BYTE(drive->ctl,IDE_CONTROL_REG);
2157 }
2158
2159 delay_10ms();
2160 if ((IN_BYTE(IDE_ALTSTATUS_REG) ^ IN_BYTE(IDE_STATUS_REG)) & ~INDEX_STAT) {
2161 printk("%s: probing with STATUS instead of ALTSTATUS\n", drive->name);
2162 hd_status = IDE_STATUS_REG;
2163 } else
2164 hd_status = IDE_ALTSTATUS_REG;
2165
2166 OUT_BYTE(cmd,IDE_COMMAND_REG);
2167 timeout = ((cmd == WIN_IDENTIFY) ? WAIT_WORSTCASE : WAIT_PIDENTIFY) / 2;
2168 timeout += jiffies;
2169 do {
2170 if (jiffies > timeout) {
2171 if (!HWIF(drive)->irq)
2172 (void) probe_irq_off(irqs);
2173 return 1;
2174 }
2175 delay_10ms();
2176 } while (IN_BYTE(hd_status) & BUSY_STAT);
2177
2178 delay_10ms();
2179 if (OK_STAT(GET_STAT(),DRQ_STAT,BAD_R_STAT)) {
2180 cli();
2181 do_identify(drive, cmd);
2182 rc = 0;
2183 } else
2184 rc = 2;
2185 if (!HWIF(drive)->irq) {
2186 irqs = probe_irq_off(irqs);
2187 if (irqs > 0)
2188 HWIF(drive)->irq = irqs;
2189 else
2190 printk("%s: IRQ probe failed (%d)\n", drive->name, irqs);
2191 }
2192 return rc;
2193 }
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211 static int do_probe (ide_drive_t *drive, byte cmd)
2212 {
2213 int rc;
2214 #ifdef CONFIG_BLK_DEV_IDECD
2215 if (drive->present) {
2216 if ((drive->media == cdrom) && (cmd == WIN_IDENTIFY))
2217 return 4;
2218 }
2219 #endif
2220 #ifdef DEBUG
2221 printk("probing for %s: present=%d, type=%s, probetype=%s\n",
2222 drive->name, drive->present, drive->media ? "cdrom":"disk",
2223 (cmd == WIN_IDENTIFY) ? "ATA" : "ATAPI");
2224 #endif
2225 #if SUPPORT_HT6560B
2226 if (HWIF(drive)->select)
2227 ide_hwif_select (HWIF(drive));
2228 #endif
2229 OUT_BYTE(drive->select.all,IDE_SELECT_REG);
2230 delay_10ms();
2231 if (IN_BYTE(IDE_SELECT_REG) != drive->select.all && !drive->present) {
2232 OUT_BYTE(0xa0,IDE_SELECT_REG);
2233 return 3;
2234 }
2235
2236 if (OK_STAT(GET_STAT(),READY_STAT,BUSY_STAT)
2237 || drive->present || cmd == WIN_PIDENTIFY)
2238 {
2239 if ((rc = try_to_identify(drive,cmd)))
2240 rc = try_to_identify(drive,cmd);
2241 if (rc == 1)
2242 printk("%s: no response (status = 0x%02x)\n", drive->name, GET_STAT());
2243 (void) GET_STAT();
2244 } else {
2245 rc = 3;
2246 }
2247 if (drive->select.b.unit != 0) {
2248 OUT_BYTE(0xa0,IDE_SELECT_REG);
2249 delay_10ms();
2250 (void) GET_STAT();
2251 }
2252 return rc;
2253 }
2254
2255
2256
2257
2258
2259
2260
2261 static inline byte probe_for_drive (ide_drive_t *drive)
2262 {
2263 if (drive->noprobe)
2264 return drive->present;
2265 if (do_probe(drive, WIN_IDENTIFY) >= 2) {
2266 #ifdef CONFIG_BLK_DEV_IDECD
2267 (void) do_probe(drive, WIN_PIDENTIFY);
2268 #endif
2269 }
2270 if (!drive->present)
2271 return 0;
2272 if (drive->id == NULL) {
2273 if (drive->media == disk) {
2274 printk ("%s: non-IDE drive, CHS=%d/%d/%d\n",
2275 drive->name, drive->cyl, drive->head, drive->sect);
2276 }
2277 #ifdef CONFIG_BLK_DEV_IDECD
2278 else if (drive->media == cdrom) {
2279 printk("%s: ATAPI cdrom (?)\n", drive->name);
2280 }
2281 #endif
2282 else {
2283 drive->present = 0;
2284 return 1;
2285 }
2286 }
2287 if (drive->media == disk && !drive->select.b.lba) {
2288 if (!drive->head || drive->head > 16) {
2289 printk("%s: INVALID GEOMETRY: %d PHYSICAL HEADS?\n",
2290 drive->name, drive->head);
2291 drive->present = 0;
2292 }
2293 }
2294 return 1;
2295 }
2296
2297
2298
2299
2300
2301 static void probe_for_drives (ide_hwif_t *hwif)
2302 {
2303 unsigned int unit;
2304
2305 if (check_region(hwif->io_base,8) || check_region(hwif->ctl_port,1)) {
2306 int msgout = 0;
2307 for (unit = 0; unit < MAX_DRIVES; ++unit) {
2308 ide_drive_t *drive = &hwif->drives[unit];
2309 if (drive->present) {
2310 drive->present = 0;
2311 printk("%s: ERROR, PORTS ALREADY IN USE\n", drive->name);
2312 msgout = 1;
2313 }
2314 }
2315 if (!msgout)
2316 printk("%s: ports already in use, skipping probe\n", hwif->name);
2317 } else {
2318 unsigned long flags;
2319 save_flags(flags);
2320
2321 #if (MAX_DRIVES > 2)
2322 printk("%s: probing for first 2 of %d possible drives\n", hwif->name, MAX_DRIVES);
2323 #endif
2324 sti();
2325
2326
2327
2328
2329 for (unit = 0; unit < 2; ++unit) {
2330 ide_drive_t *drive = &hwif->drives[unit];
2331 (void) probe_for_drive (drive);
2332 }
2333 for (unit = 0; unit < MAX_DRIVES; ++unit) {
2334 ide_drive_t *drive = &hwif->drives[unit];
2335 if (drive->present) {
2336 hwif->present = 1;
2337 request_region(hwif->io_base, 8, hwif->name);
2338 request_region(hwif->ctl_port, 1, hwif->name);
2339 break;
2340 }
2341 }
2342 restore_flags(flags);
2343 }
2344 }
2345
2346 #if SUPPORT_DTC2278
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362 #if SET_DTC2278_MODE4
2363 static void sub22 (char b, char c)
2364 {
2365 int i;
2366
2367 for(i = 0; i < 3; ++i) {
2368 inb(0x3f6);
2369 outb_p(b,0xb0);
2370 inb(0x3f6);
2371 outb_p(c,0xb4);
2372 inb(0x3f6);
2373 if(inb(0xb4) == c) {
2374 outb_p(7,0xb0);
2375 inb(0x3f6);
2376 return;
2377 }
2378 }
2379 }
2380 #endif
2381
2382 static void init_dtc2278 (void)
2383 {
2384 unsigned long flags;
2385
2386 save_flags(flags);
2387 cli();
2388 #if SET_DTC2278_MODE4
2389
2390
2391
2392 sub22(1,0xc3);
2393 sub22(0,0xa0);
2394 #endif
2395
2396
2397
2398 outb_p(4,0xb0);
2399 inb(0x3f6);
2400 outb_p(0x20,0xb4);
2401 inb(0x3f6);
2402 restore_flags(flags);
2403 }
2404 #endif
2405
2406 #ifdef SUPPORT_QD6580
2407
2408
2409
2410
2411
2412
2413
2414 static void init_qd6580 (void)
2415 {
2416 unsigned long flags;
2417
2418
2419
2420
2421
2422
2423
2424
2425 save_flags(flags);
2426 cli();
2427 outb_p(0x8d,0xb0);
2428 outb_p(0x0 ,0xb2);
2429 outb_p(0x4f,0xb3);
2430 inb(0x3f6);
2431 restore_flags(flags);
2432 }
2433 #endif
2434
2435 #ifdef SUPPORT_UMC8672
2436 #include "umc8672.c"
2437 #endif
2438
2439 #ifdef SUPPORT_CMD640
2440 #include "cmd640.c"
2441 #endif
2442
2443
2444
2445
2446
2447 static int stridx (const char *s, char c)
2448 {
2449 char *i = strchr(s, c);
2450 return (i && c) ? i - s : -1;
2451 }
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465 static int match_parm (char *s, const char *keywords[], int vals[], int max_vals)
2466 {
2467 static const char *decimal = "0123456789";
2468 static const char *hex = "0123456789abcdef";
2469 int i, n;
2470
2471 if (*s++ == '=') {
2472
2473
2474
2475
2476 for (i = 0; *keywords != NULL; ++i) {
2477 if (!strcmp(s, *keywords++))
2478 return -(i+1);
2479 }
2480
2481
2482
2483
2484
2485
2486 for (n = 0; (i = stridx(decimal, *s)) >= 0;) {
2487 vals[n] = i;
2488 while ((i = stridx(decimal, *++s)) >= 0)
2489 vals[n] = (vals[n] * 10) + i;
2490 if (*s == 'x' && !vals[n]) {
2491 while ((i = stridx(hex, *++s)) >= 0)
2492 vals[n] = (vals[n] * 0x10) + i;
2493 }
2494 if (++n == max_vals)
2495 break;
2496 if (*s == ',')
2497 ++s;
2498 }
2499 if (!*s)
2500 return n;
2501 }
2502 return 0;
2503 }
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539 void ide_setup (char *s)
2540 {
2541 int vals[3];
2542 ide_hwif_t *hwif;
2543 ide_drive_t *drive;
2544 unsigned int hw, unit;
2545 const char max_drive = 'a' + ((MAX_HWIFS * MAX_DRIVES) - 1);
2546 const char max_hwif = '0' + (MAX_HWIFS - 1);
2547
2548 printk("ide_setup: %s", s);
2549 init_ide_data ();
2550
2551
2552
2553
2554 if (s[0] == 'h' && s[1] == 'd' && s[2] >= 'a' && s[2] <= max_drive) {
2555 const char *hd_words[] = {"noprobe", "nowerr", "cdrom", "serialize", NULL};
2556 unit = s[2] - 'a';
2557 hw = unit / MAX_DRIVES;
2558 unit = unit % MAX_DRIVES;
2559 hwif = &ide_hwifs[hw];
2560 drive = &hwif->drives[unit];
2561 switch (match_parm(&s[3], hd_words, vals, 3)) {
2562 case -1:
2563 drive->noprobe = 1;
2564 goto done;
2565 case -2:
2566 drive->bad_wstat = BAD_R_STAT;
2567 hwif->noprobe = 0;
2568 goto done;
2569 case -3:
2570 drive->present = 1;
2571 drive->media = cdrom;
2572 hwif->noprobe = 0;
2573 goto done;
2574 case -4:
2575 printk(" -- USE \"ide%c=serialize\" INSTEAD", '0'+hw);
2576 goto do_serialize;
2577 case 3:
2578 drive->media = disk;
2579 drive->cyl = drive->bios_cyl = vals[0];
2580 drive->head = drive->bios_head = vals[1];
2581 drive->sect = drive->bios_sect = vals[2];
2582 drive->present = 1;
2583 hwif->noprobe = 0;
2584 goto done;
2585 default:
2586 goto bad_option;
2587 }
2588 }
2589
2590
2591
2592 if (s[0] == 'i' && s[1] == 'd' && s[2] == 'e' && s[3] >= '0' && s[3] <= max_hwif) {
2593 const char *ide_words[] = {"noprobe", "serialize", "dtc2278", "ht6560b",
2594 "cmd640_vlb", "qd6580", "umc8672", NULL};
2595 hw = s[3] - '0';
2596 hwif = &ide_hwifs[hw];
2597
2598 switch (match_parm(&s[4], ide_words, vals, 3)) {
2599 #if SUPPORT_UMC8672
2600 case -7:
2601 if (hw != 0) goto bad_hwif;
2602 init_umc8672();
2603 goto done;
2604 #endif
2605 #if SUPPORT_QD6580
2606 case -6:
2607 if (hw != 0) goto bad_hwif;
2608 init_qd6580();
2609 goto done;
2610 #endif
2611 #if SUPPORT_CMD640
2612 case -5:
2613 if (hw > 1) goto bad_hwif;
2614 cmd640_vlb = 1;
2615 break;
2616 #endif
2617 #if SUPPORT_HT6560B
2618 case -4:
2619 if (hw > 1) goto bad_hwif;
2620
2621
2622
2623
2624
2625
2626 if (check_region(0x3e6,1)) {
2627 printk(" -- HT6560 PORT 0x3e6 ALREADY IN USE");
2628 goto done;
2629 }
2630 request_region(0x3e6, 1, hwif->name);
2631 ide_hwifs[0].select = 0x3c;
2632 ide_hwifs[1].select = 0x3d;
2633 goto do_serialize;
2634 #endif
2635 #if SUPPORT_DTC2278
2636 case -3:
2637 if (hw > 1) goto bad_hwif;
2638 init_dtc2278();
2639 goto do_serialize;
2640 #endif
2641 case -2:
2642 do_serialize:
2643 if (hw > 1) goto bad_hwif;
2644 serialized = 1;
2645 goto done;
2646 case -1:
2647 hwif->noprobe = 1;
2648 goto done;
2649 case 1:
2650 vals[1] = vals[0] + 0x206;
2651 case 2:
2652 vals[2] = 0;
2653 case 3:
2654 hwif->io_base = vals[0];
2655 hwif->ctl_port = vals[1];
2656 hwif->irq = vals[2];
2657 hwif->noprobe = 0;
2658 goto done;
2659 }
2660 }
2661 bad_option:
2662 printk(" -- BAD OPTION\n");
2663 return;
2664 bad_hwif:
2665 printk("-- NOT SUPPORTED ON ide%d", hw);
2666 done:
2667 printk("\n");
2668 }
2669
2670
2671
2672
2673
2674
2675 int ide_xlate_1024 (kdev_t i_rdev, int offset, const char *msg)
2676 {
2677 ide_drive_t *drive;
2678 static const byte head_vals[] = {4, 8, 16, 32, 64, 128, 255, 0};
2679 const byte *heads = head_vals;
2680 unsigned long tracks;
2681
2682 if ((drive = get_info_ptr(i_rdev)) == NULL || drive->id == NULL)
2683 return 0;
2684
2685 drive->cyl = drive->bios_cyl = drive->id->cyls;
2686 drive->head = drive->bios_head = drive->id->heads;
2687 drive->sect = drive->bios_sect = drive->id->sectors;
2688 drive->special.b.set_geometry = 1;
2689
2690 tracks = drive->bios_cyl * drive->bios_head * drive->bios_sect / 63;
2691 drive->bios_sect = 63;
2692 while (drive->bios_cyl >= 1024) {
2693 drive->bios_head = *heads;
2694 drive->bios_cyl = tracks / drive->bios_head;
2695 if (0 == *++heads)
2696 break;
2697 }
2698 if (offset) {
2699 #if FAKE_FDISK_FOR_EZDRIVE
2700 if (offset == -1)
2701 drive->ezdrive = 1;
2702 else
2703 #endif
2704 {
2705 drive->sect0 = 63;
2706 drive->bios_cyl = (tracks - 1) / drive->bios_head;
2707 }
2708 }
2709 drive->part[0].nr_sects = current_capacity(drive);
2710 printk("%s [%d/%d/%d]", msg, drive->bios_cyl, drive->bios_head, drive->bios_sect);
2711 return 1;
2712 }
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737 static void probe_cmos_for_drives (ide_hwif_t *hwif)
2738 {
2739 #ifdef __i386__
2740 extern struct drive_info_struct drive_info;
2741 byte cmos_disks, *BIOS = (byte *) &drive_info;
2742 int unit;
2743
2744 outb_p(0x12,0x70);
2745 cmos_disks = inb_p(0x71);
2746
2747 for (unit = 0; unit < MAX_DRIVES; ++unit) {
2748 ide_drive_t *drive = &hwif->drives[unit];
2749 if ((cmos_disks & (0xf0 >> (unit*4))) && !drive->present) {
2750 drive->cyl = drive->bios_cyl = *(unsigned short *)BIOS;
2751 drive->head = drive->bios_head = *(BIOS+2);
2752 drive->sect = drive->bios_sect = *(BIOS+14);
2753 drive->ctl = *(BIOS+8);
2754 drive->present = 1;
2755 }
2756 BIOS += 16;
2757 }
2758 #endif
2759 }
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770 static int init_irq (ide_hwif_t *hwif)
2771 {
2772 unsigned long flags;
2773 int irq = hwif->irq;
2774 ide_hwgroup_t *hwgroup = irq_to_hwgroup[irq];
2775
2776 save_flags(flags);
2777 cli();
2778
2779
2780
2781
2782 if (hwgroup == NULL) {
2783 if (request_irq(irq, ide_intr, SA_INTERRUPT|SA_SAMPLE_RANDOM, hwif->name)) {
2784 restore_flags(flags);
2785 printk(" -- FAILED!");
2786 return 1;
2787 }
2788 }
2789
2790
2791
2792
2793 if (serialized && hwif->name[3] == '0' && ide_hwifs[1].present)
2794 hwgroup = ide_hwifs[1].hwgroup;
2795
2796
2797
2798
2799 if (hwgroup == NULL) {
2800 hwgroup = kmalloc (sizeof(ide_hwgroup_t), GFP_KERNEL);
2801 hwgroup->hwif = hwif->next = hwif;
2802 hwgroup->rq = NULL;
2803 hwgroup->handler = NULL;
2804 hwgroup->drive = NULL;
2805 hwgroup->reset_timeout = 0;
2806 #ifdef CONFIG_BLK_DEV_IDECD
2807 hwgroup->doing_atapi_reset = 0;
2808 #endif
2809 init_timer(&hwgroup->timer);
2810 hwgroup->timer.function = &timer_expiry;
2811 hwgroup->timer.data = (unsigned long) hwgroup;
2812 } else {
2813 hwif->next = hwgroup->hwif->next;
2814 hwgroup->hwif->next = hwif;
2815 }
2816 hwif->hwgroup = hwgroup;
2817 irq_to_hwgroup[irq] = hwgroup;
2818
2819 restore_flags(flags);
2820
2821 printk("%s at 0x%03x-0x%03x,0x%03x on irq %d", hwif->name,
2822 hwif->io_base, hwif->io_base+7, hwif->ctl_port, irq);
2823 if (hwgroup->hwif != hwif)
2824 printk(" (serialized with %s)", hwgroup->hwif->name);
2825 printk("\n");
2826 return 0;
2827 }
2828
2829 static struct file_operations ide_fops = {
2830 NULL,
2831 block_read,
2832 block_write,
2833 NULL,
2834 NULL,
2835 ide_ioctl,
2836 NULL,
2837 ide_open,
2838 ide_release,
2839 block_fsync
2840 ,NULL,
2841 ide_check_media_change,
2842 revalidate_disk
2843 };
2844
2845 #ifdef CONFIG_PCI
2846
2847 void ide_pci_access_error (int rc)
2848 {
2849 printk("ide: pcibios access failed - %s\n", pcibios_strerror(rc));
2850 }
2851
2852 #if SUPPORT_RZ1000 || SUPPORT_CMD640
2853 void buggy_interface_fallback (int rc)
2854 {
2855 ide_pci_access_error (rc);
2856 serialized = 1;
2857 disallow_unmask = 1;
2858 printk("serialized, disabled unmasking\n");
2859 }
2860 #endif
2861
2862 #if SUPPORT_RZ1000
2863 void init_rz1000 (byte bus, byte fn)
2864 {
2865 int rc;
2866 unsigned short reg;
2867
2868 printk("ide: buggy RZ1000 interface: ");
2869 if ((rc = pcibios_read_config_word (bus, fn, PCI_COMMAND, ®))) {
2870 ide_pci_access_error (rc);
2871 } else if (!(reg & 1)) {
2872 printk("not enabled\n");
2873 } else {
2874 if ((rc = pcibios_read_config_word(bus, fn, 0x40, ®))
2875 || (rc = pcibios_write_config_word(bus, fn, 0x40, reg & 0xdfff)))
2876 buggy_interface_fallback (rc);
2877 else
2878 printk("disabled read-ahead\n");
2879 }
2880 }
2881 #endif
2882
2883 typedef void (ide_pci_init_proc_t)(byte, byte);
2884
2885
2886
2887
2888
2889 static void ide_probe_pci (unsigned short vendor, unsigned short device, ide_pci_init_proc_t *init, int func_adj)
2890 {
2891 unsigned long flags;
2892 unsigned index;
2893 byte fn, bus;
2894
2895 save_flags(flags);
2896 cli();
2897 for (index = 0; !pcibios_find_device (vendor, device, index, &bus, &fn); ++index) {
2898 init (bus, fn + func_adj);
2899 }
2900 restore_flags(flags);
2901 }
2902
2903
2904
2905
2906
2907
2908
2909 static void ide_init_pci (void)
2910 {
2911 #if SUPPORT_RZ1000
2912 ide_probe_pci (PCI_VENDOR_ID_PCTECH, PCI_DEVICE_ID_PCTECH_RZ1000, &init_rz1000, 0);
2913 #endif
2914 #ifdef CONFIG_BLK_DEV_TRITON
2915
2916
2917
2918
2919
2920 ide_probe_pci (PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82371_0, &ide_init_triton, 1);
2921 #endif
2922 }
2923 #endif
2924
2925
2926
2927
2928 int ide_init (void)
2929 {
2930 int h;
2931
2932 init_ide_data ();
2933
2934
2935
2936
2937 #ifdef CONFIG_PCI
2938
2939
2940
2941 if (pcibios_present())
2942 ide_init_pci ();
2943 #endif
2944 #ifdef SUPPORT_CMD640
2945 ide_probe_for_cmd640x();
2946 #endif
2947
2948
2949
2950
2951 for (h = 0; h < MAX_HWIFS; ++h) {
2952 ide_hwif_t *hwif = &ide_hwifs[h];
2953 if (!hwif->noprobe) {
2954 if (hwif->io_base == HD_DATA)
2955 probe_cmos_for_drives (hwif);
2956 probe_for_drives (hwif);
2957 }
2958 if (hwif->present) {
2959 if (!hwif->irq) {
2960 if (!(hwif->irq = default_irqs[h])) {
2961 printk("%s: DISABLED, NO IRQ\n", hwif->name);
2962 hwif->present = 0;
2963 continue;
2964 }
2965 }
2966 #ifdef CONFIG_BLK_DEV_HD
2967 if (hwif->irq == HD_IRQ && hwif->io_base != HD_DATA) {
2968 printk("%s: CANNOT SHARE IRQ WITH OLD HARDDISK DRIVER (hd.c)\n", hwif->name);
2969 hwif->present = 0;
2970 }
2971 #endif
2972 }
2973 }
2974
2975
2976
2977
2978 for (h = MAX_HWIFS-1; h >= 0; --h) {
2979 void (*rfn)(void);
2980 ide_hwif_t *hwif = &ide_hwifs[h];
2981 if (!hwif->present)
2982 continue;
2983 hwif->present = 0;
2984 switch (hwif->major) {
2985 case IDE0_MAJOR: rfn = &do_ide0_request; break;
2986 case IDE1_MAJOR: rfn = &do_ide1_request; break;
2987 case IDE2_MAJOR: rfn = &do_ide2_request; break;
2988 case IDE3_MAJOR: rfn = &do_ide3_request; break;
2989 default:
2990 printk("%s: request_fn NOT DEFINED\n", hwif->name);
2991 continue;
2992 }
2993 if (register_blkdev (hwif->major, hwif->name, &ide_fops)) {
2994 printk("%s: UNABLE TO GET MAJOR NUMBER %d\n", hwif->name, hwif->major);
2995 } else if (init_irq (hwif)) {
2996 printk("%s: UNABLE TO GET IRQ %d\n", hwif->name, hwif->irq);
2997 (void) unregister_blkdev (hwif->major, hwif->name);
2998 } else {
2999 init_gendisk(hwif);
3000 blk_dev[hwif->major].request_fn = rfn;
3001 read_ahead[hwif->major] = 8;
3002 hwif->present = 1;
3003 }
3004 }
3005 return 0;
3006 }