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