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