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