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