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