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