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