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