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