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