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