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