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