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