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