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