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