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