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