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