This source file includes following definitions.
- read_timer
- input_ide_data
- output_ide_data
- start_ide_timer
- do_ide_reset
- end_drive_cmd
- dump_status
- ide_error
- read_intr
- write_intr
- multwrite
- multwrite_intr
- ide_cmd
- set_multmode_intr
- set_geometry_intr
- recal_intr
- drive_cmd_intr
- timer_expiry
- ide0_timer_expiry
- ide1_timer_expiry
- do_special
- wait_stat
- do_rw_disk
- do_request
- do_ide0_request
- do_ide1_request
- do_ide0_request
- do_shared_request
- unexpected_intr
- ide_seq_intr
- ide0_intr
- ide1_intr
- ide_intr
- ide_shared_intr
- get_info_ptr
- ide_open
- ide_release
- revalidate_disk
- do_drive_cmd
- write_fs_long
- ide_ioctl
- ide_check_media_change
- fixstring
- lba_capacity_is_ok
- do_identify
- delay_10ms
- try_to_identify
- do_probe
- probe_for_drive
- probe_for_drives
- ide_setup
- hda_setup
- hdb_setup
- hdc_setup
- hdd_setup
- probe_cmos_for_drives
- init_ide_data
- setup_irq
- ide_geninit
- ide0_geninit
- ide1_geninit
- sub22
- try_to_init_dtc2278
- ide_init
1
2
3
4
5
6
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 #include <linux/config.h>
132 #include <linux/errno.h>
133 #include <linux/signal.h>
134 #include <linux/sched.h>
135 #include <linux/timer.h>
136 #include <linux/fs.h>
137 #include <linux/kernel.h>
138 #include <linux/hdreg.h>
139 #include <linux/genhd.h>
140 #include <linux/malloc.h>
141 #include <linux/string.h>
142 #include <linux/delay.h>
143 #include <linux/major.h>
144 #include <linux/ioport.h>
145 #include <linux/interrupt.h>
146 #include <linux/blkdev.h>
147 #include <asm/bitops.h>
148 #include <asm/irq.h>
149 #include <asm/segment.h>
150 #include <asm/system.h>
151
152
153
154
155 #define REALLY_SLOW_IO
156 #include <asm/io.h>
157
158 #ifdef __alpha__
159 # ifndef SUPPORT_VLB_SYNC
160 # define SUPPORT_VLB_SYNC 0
161 # endif
162 #endif
163
164 #undef REALLY_FAST_IO
165 #define INITIAL_MULT_COUNT 0
166 #ifndef SUPPORT_VLB_32BIT
167 #define SUPPORT_VLB_32BIT 1
168 #endif
169 #ifndef SUPPORT_VLB_SYNC
170 #define SUPPORT_VLB_SYNC 1
171 #endif
172 #ifndef DISK_RECOVERY_TIME
173 #define DISK_RECOVERY_TIME 0
174 #endif
175 #ifndef OK_TO_RESET_CONTROLLER
176 #define OK_TO_RESET_CONTROLLER 1
177 #endif
178 #ifndef SUPPORT_TWO_INTERFACES
179 #define SUPPORT_TWO_INTERFACES 1
180 #endif
181 #ifndef OPTIMIZE_IRQS
182 #define OPTIMIZE_IRQS 1
183 #endif
184 #ifndef SUPPORT_SERIALIZE
185 #define SUPPORT_SERIALIZE 1
186 #endif
187 #ifndef SUPPORT_SHARING_IRQ
188 #define SUPPORT_SHARING_IRQ 1
189 #endif
190 #ifndef SUPPORT_DTC2278
191 #define SUPPORT_DTC2278 1
192 #endif
193 #ifndef FANCY_STATUS_DUMPS
194 #define FANCY_STATUS_DUMPS 1
195 #endif
196 #define PROBE_FOR_IRQS 1
197 #define DEFAULT_IDE0_IRQ 14
198 #define DEFAULT_IDE1_IRQ 15
199
200
201 #define IDE_DRIVE_CMD 99
202
203
204
205
206
207
208
209
210
211 #ifndef IDE0_TIMER
212 #define IDE0_TIMER HD_TIMER
213 #define IDE1_TIMER HD_TIMER2
214 #endif
215
216
217
218
219 #ifdef REALLY_SLOW_IO
220 #undef REALLY_FAST_IO
221 #endif
222 #ifdef CONFIG_BLK_DEV_HD
223 #undef SUPPORT_TWO_INTERFACES
224 #define SUPPORT_TWO_INTERFACES 0
225 #endif
226
227 #if SUPPORT_TWO_INTERFACES
228 #define HWIF hwif
229 #define DEV_HWIF (dev->hwif)
230 #if SUPPORT_SERIALIZE
231 #undef SUPPORT_SHARING_IRQ
232 #define SUPPORT_SHARING_IRQ 1
233 #endif
234 #else
235 #undef SUPPORT_SERIALIZE
236 #define SUPPORT_SERIALIZE 0
237 #undef OPTIMIZE_IRQS
238 #define OPTIMIZE_IRQS 0
239 #undef SUPPORT_SHARING_IRQ
240 #define SUPPORT_SHARING_IRQ 0
241 #ifdef CONFIG_BLK_DEV_HD
242 #define HWIF 1
243 #else
244 #define HWIF 0
245 #endif
246 #define DEV_HWIF HWIF
247 #endif
248
249
250
251
252 typedef unsigned char byte;
253 #define IDE_PORT(p,hwif) ((p)^((hwif)<<7))
254
255 #ifdef REALLY_FAST_IO
256 #define OUT_BYTE(b,p) outb((b),IDE_PORT(p,DEV_HWIF))
257 #define IN_BYTE(p,hwif) (byte)inb(IDE_PORT(p,hwif))
258 #else
259 #define OUT_BYTE(b,p) outb_p((b),IDE_PORT(p,DEV_HWIF))
260 #define IN_BYTE(p,hwif) (byte)inb_p(IDE_PORT(p,hwif))
261 #endif
262
263 #if SUPPORT_VLB_32BIT
264 #if SUPPORT_VLB_SYNC
265 #define VLB_SYNC __asm__ __volatile__ ("pusha\n movl $0x01f2,%edx\n inb (%dx),%al\n inb (%dx),%al\n inb (%dx),%al\n popa\n")
266 #endif
267 #endif
268
269 #if SUPPORT_DTC2278
270 static uint probe_dtc2278 = 0;
271 #endif
272
273 #define GET_ERR(hwif) IN_BYTE(HD_ERROR,hwif)
274 #define GET_STAT(hwif) IN_BYTE(HD_STATUS,hwif)
275 #define OK_STAT(stat,good,bad) (((stat)&((good)|(bad)))==(good))
276 #define BAD_R_STAT (BUSY_STAT | ERR_STAT)
277 #define BAD_W_STAT (BUSY_STAT | ERR_STAT | WRERR_STAT)
278 #define BAD_STAT (BAD_R_STAT | DRQ_STAT)
279 #define DRIVE_READY (READY_STAT | SEEK_STAT)
280 #define DATA_READY (DRIVE_READY | DRQ_STAT)
281
282
283
284
285 #define BIOS_SECTORS(dev) (dev->bios_head*dev->bios_sect*dev->bios_cyl)
286 #define HD_NAME "hd"
287 #define PARTN_BITS 6
288 #define PARTN_MASK ((1<<PARTN_BITS)-1)
289 #define MAX_DRIVES 2
290 #define SECTOR_WORDS (512 / 4)
291
292
293
294
295 #define WAIT_DRQ (5*HZ/100)
296 #define WAIT_READY (3*HZ/100)
297 #define WAIT_PIDENTIFY (1*HZ)
298 #define WAIT_WORSTCASE (30*HZ)
299 #define WAIT_CMD (10*HZ)
300
301
302
303
304
305
306
307
308 typedef enum {disk, cdrom} dev_type;
309
310 typedef union {
311 unsigned all : 8;
312 struct {
313 unsigned set_geometry : 1;
314 unsigned recalibrate : 1;
315 unsigned set_multmode : 1;
316 unsigned reserved : 5;
317 } b;
318 } special_t;
319
320 typedef union {
321 unsigned all : 8;
322 struct {
323 unsigned head : 4;
324 unsigned drive : 1;
325 unsigned bit5 : 1;
326 unsigned lba : 1;
327 unsigned bit7 : 1;
328 } b;
329 } select_t;
330
331 typedef struct {
332 byte hwif;
333 byte unmask;
334 dev_type type : 1;
335 unsigned present : 1;
336 unsigned dont_probe : 1;
337 unsigned keep_settings : 1;
338 unsigned busy : 1;
339 unsigned vlb_32bit : 1;
340 unsigned vlb_sync : 1;
341 unsigned reserved0 : 1;
342 special_t special;
343 select_t select;
344 byte mult_count, chipset, reserved2;
345 byte usage, mult_req, wpcom, ctl;
346 byte head, sect, bios_head, bios_sect;
347 unsigned short cyl, bios_cyl;
348 const char *name;
349 struct hd_driveid *id;
350 struct wait_queue *wqueue;
351 } ide_dev_t;
352
353
354
355
356 static const byte ide_major [2] = {IDE0_MAJOR,IDE1_MAJOR};
357 static byte ide_irq [2] = {DEFAULT_IDE0_IRQ,DEFAULT_IDE1_IRQ};
358 static struct hd_struct ide_hd [2][MAX_DRIVES<<PARTN_BITS] = {{{0,0},},};
359 static int ide_sizes [2][MAX_DRIVES<<PARTN_BITS] = {{0,},};
360 static int ide_blksizes [2][MAX_DRIVES<<PARTN_BITS] = {{0,},};
361 static unsigned long ide_capacity [2][MAX_DRIVES] = {{0,},};
362 static ide_dev_t ide_dev [2][MAX_DRIVES] = {{{0,},},};
363 static ide_dev_t *ide_cur_dev [2] = {NULL,NULL};
364 static void (*ide_handler[2])(ide_dev_t *) = {NULL,NULL};
365 static struct request *ide_cur_rq [2] = {NULL,NULL};
366 static struct request ide_write_rq [2];
367 static const int ide_timer [2] = {IDE0_TIMER,IDE1_TIMER};
368 static const int ide_timerbit[2] = {(1<<IDE0_TIMER),(1<<IDE1_TIMER)};
369 static const char *ide_name [2] = {"ide0", "ide1"};
370 static const char *ide_devname [2][MAX_DRIVES] =
371 {{HD_NAME "a", HD_NAME "b"}, {HD_NAME "c", HD_NAME "d"}};
372 static const char *unsupported = " not supported by this kernel\n";
373
374 static byte single_threaded = 0;
375 #if SUPPORT_SHARING_IRQ
376 static byte sharing_single_irq = 0;
377 static volatile byte current_hwif = 0;
378 #endif
379
380
381
382
383 static void ide0_geninit(void), ide1_geninit(void);
384 static struct gendisk ide_gendisk [2] =
385 {{
386 IDE0_MAJOR,
387 HD_NAME,
388 PARTN_BITS,
389 1 << PARTN_BITS,
390 MAX_DRIVES,
391 ide0_geninit,
392 ide_hd[0],
393 ide_sizes[0],
394 0,
395 ide_dev[0],
396 NULL
397 },{
398 IDE1_MAJOR,
399 HD_NAME,
400 PARTN_BITS,
401 1 << PARTN_BITS,
402 MAX_DRIVES,
403 ide1_geninit,
404 ide_hd[1],
405 ide_sizes[1],
406 0,
407 ide_dev[1],
408 NULL
409 }};
410
411
412
413
414 #define IDE_DRIVER
415 #include "blk.h"
416
417
418
419
420 #if (DISK_RECOVERY_TIME > 0)
421 static unsigned long ide_lastreq[] = {0,0};
422 #define SET_DISK_RECOVERY_TIMER ide_lastreq[DEV_HWIF] = read_timer();
423 static unsigned long read_timer(void)
424 {
425 unsigned long t, flags;
426 int i;
427
428 save_flags(flags);
429 cli();
430 t = jiffies * 11932;
431 outb_p(0, 0x43);
432 i = inb_p(0x40);
433 i |= inb(0x40) << 8;
434 restore_flags(flags);
435 return (t - i);
436 }
437 #else
438 #define SET_DISK_RECOVERY_TIMER
439 #endif
440
441
442
443
444 static void do_request (byte hwif);
445 #define DO_REQUEST {SET_DISK_RECOVERY_TIMER do_request(DEV_HWIF);}
446
447
448
449
450
451
452
453
454
455
456 #define WAIT_STAT(dev,good,bad,timeout,msg,label) \
457 { \
458 byte stat; \
459 udelay(1); \
460 if (GET_STAT(DEV_HWIF) & BUSY_STAT) { \
461 unsigned long timer = jiffies + timeout; \
462 do { \
463 if ((GET_STAT(DEV_HWIF) & BUSY_STAT) == 0) \
464 break; \
465 } while (timer > jiffies); \
466 } \
467 udelay(1); \
468 if (!OK_STAT(stat=GET_STAT(DEV_HWIF), good, bad)) { \
469 ide_error(dev, msg " error", stat); \
470 goto label; \
471 } \
472 }
473
474
475
476
477 void input_ide_data (ide_dev_t *dev, void *buffer, uint wcount)
478 {
479 #if SUPPORT_VLB_32BIT
480 if (dev->vlb_32bit) {
481 #if SUPPORT_VLB_SYNC
482 if (dev->vlb_sync) {
483 cli();
484 VLB_SYNC;
485 insl(IDE_PORT(HD_DATA,DEV_HWIF), buffer, wcount);
486 if (dev->unmask)
487 sti();
488 } else
489 #endif
490 insl(IDE_PORT(HD_DATA,DEV_HWIF), buffer, wcount);
491 } else
492 #endif
493 insw(IDE_PORT(HD_DATA,DEV_HWIF), buffer, wcount<<1);
494 }
495
496
497
498
499 void output_ide_data (ide_dev_t *dev, void *buffer, uint wcount)
500 {
501 #if SUPPORT_VLB_32BIT
502 if (dev->vlb_32bit) {
503 #if SUPPORT_VLB_SYNC
504 if (dev->vlb_sync) {
505 cli();
506 VLB_SYNC;
507 outsl(IDE_PORT(HD_DATA,DEV_HWIF), buffer, wcount);
508 if (dev->unmask)
509 sti();
510 } else
511 outsl(IDE_PORT(HD_DATA,DEV_HWIF), buffer, wcount);
512 #endif
513 } else
514 #endif
515 outsw(IDE_PORT(HD_DATA,DEV_HWIF), buffer, wcount<<1);
516 }
517
518
519
520
521 static inline void start_ide_timer (byte hwif)
522 {
523 if (ide_handler[HWIF] != NULL) {
524 timer_table[ide_timer[HWIF]].expires = jiffies + WAIT_CMD;
525 timer_active |= ide_timerbit[HWIF];
526 }
527 }
528
529 static void do_ide_reset (ide_dev_t *dev)
530 {
531 byte tmp;
532 unsigned long timer, flags;
533
534 save_flags(flags);
535 sti();
536 for (tmp = 0; tmp < MAX_DRIVES; tmp++) {
537 ide_dev_t *rdev = &ide_dev[DEV_HWIF][tmp];
538 rdev->special.b.set_geometry = 1;
539 rdev->special.b.recalibrate = 1;
540 rdev->special.b.set_multmode = 0;
541 if (OK_TO_RESET_CONTROLLER)
542 rdev->mult_count = 0;
543 if (!rdev->keep_settings) {
544 rdev->mult_req = 0;
545 rdev->unmask = 0;
546 }
547 if (rdev->mult_req != rdev->mult_count)
548 rdev->special.b.set_multmode = 1;
549 }
550
551 #if OK_TO_RESET_CONTROLLER
552 cli();
553 OUT_BYTE(dev->ctl|6,HD_CMD);
554 udelay(10);
555 OUT_BYTE(dev->ctl|2,HD_CMD);
556 udelay(10);
557 sti();
558 for (timer = jiffies + WAIT_WORSTCASE; timer > jiffies;) {
559 if ((GET_STAT(DEV_HWIF) & BUSY_STAT) == 0)
560 break;
561 }
562 printk("%s: do_ide_reset: ", ide_name[DEV_HWIF]);
563
564 if (!OK_STAT(tmp=GET_STAT(DEV_HWIF), 0, BUSY_STAT)) {
565 printk("timed-out, status=0x%02x\n", tmp);
566 } else {
567 if ((tmp = GET_ERR(DEV_HWIF)) == 1)
568 printk("success\n");
569 else {
570 printk("%s: ", ide_devname[DEV_HWIF][0]);
571 switch (tmp & 0x7f) {
572 case 1: printk("passed");
573 break;
574 case 2: printk("formatter device error");
575 break;
576 case 3: printk("sector buffer error");
577 break;
578 case 4: printk("ECC circuitry error");
579 break;
580 case 5: printk("controlling MPU error");
581 break;
582 default:printk("error (0x%02x?)", tmp);
583 }
584 if (tmp & 0x80)
585 printk("; %s: error", ide_devname[DEV_HWIF][1]);
586 printk("\n");
587 }
588 }
589 #endif
590 restore_flags(flags);
591 }
592
593
594
595
596 static void end_drive_cmd (ide_dev_t *dev, byte stat, byte err)
597 {
598 unsigned long flags;
599 struct request *rq = ide_cur_rq[DEV_HWIF];
600 byte *args = (byte *) rq->buffer;
601
602 rq->errors = !OK_STAT(stat,READY_STAT,BAD_STAT);
603 if (args) {
604 args[0] = stat;
605 args[1] = err;
606 args[2] = IN_BYTE(HD_NSECTOR,DEV_HWIF);
607 }
608 save_flags(flags);
609 cli();
610 up(rq->sem);
611 ide_cur_rq[DEV_HWIF] = NULL;
612 restore_flags(flags);
613 }
614
615
616
617
618 static byte dump_status (byte hwif, const char *msg, byte stat)
619 {
620 unsigned long flags;
621 byte err = 0;
622 ide_dev_t *dev = ide_cur_dev[HWIF];
623 const char *name = dev ? dev->name : ide_name[HWIF];
624
625 save_flags (flags);
626 sti();
627 printk("%s: %s: status=0x%02x", name, msg, stat);
628 #if FANCY_STATUS_DUMPS
629 if (dev && dev->type == disk) {
630 printk(" { ");
631 if (stat & BUSY_STAT)
632 printk("Busy ");
633 else {
634 if (stat & READY_STAT) printk("DriveReady ");
635 if (stat & WRERR_STAT) printk("WriteFault ");
636 if (stat & SEEK_STAT) printk("SeekComplete ");
637 if (stat & DRQ_STAT) printk("DataRequest ");
638 if (stat & ECC_STAT) printk("CorrectedError ");
639 if (stat & INDEX_STAT) printk("Index ");
640 if (stat & ERR_STAT) printk("Error ");
641 }
642 printk("}");
643 }
644 #endif
645 printk("\n");
646 if ((stat & (BUSY_STAT|ERR_STAT)) == ERR_STAT) {
647 err = GET_ERR(HWIF);
648 printk("%s: %s: error=0x%02x", name, msg, err);
649 #if FANCY_STATUS_DUMPS
650 if (dev && dev->type == disk) {
651 printk(" { ");
652 if (err & BBD_ERR) printk("BadSector ");
653 if (err & ECC_ERR) printk("UncorrectableError ");
654 if (err & ID_ERR) printk("SectorIdNotFound ");
655 if (err & ABRT_ERR) printk("DriveStatusError ");
656 if (err & TRK0_ERR) printk("TrackZeroNotFound ");
657 if (err & MARK_ERR) printk("AddrMarkNotFound ");
658 printk("}");
659 if (err & (BBD_ERR|ECC_ERR|ID_ERR|MARK_ERR)) {
660 byte cur = IN_BYTE(HD_CURRENT,HWIF);
661 if (cur & 0x40) {
662 printk(", LBAsect=%ld", (unsigned long)
663 ((cur&0xf)<<24)
664 |(IN_BYTE(HD_HCYL,HWIF)<<16)
665 |(IN_BYTE(HD_LCYL,HWIF)<<8)
666 | IN_BYTE(HD_SECTOR,HWIF));
667 } else {
668 printk(", CHS=%d/%d/%d",
669 (IN_BYTE(HD_HCYL,HWIF)<<8) +
670 IN_BYTE(HD_LCYL,HWIF),
671 cur & 0xf,
672 IN_BYTE(HD_SECTOR,HWIF));
673 }
674 if (ide_cur_rq[HWIF])
675 printk(", sector=%ld", ide_cur_rq[HWIF]->sector);
676 }
677 }
678 #endif
679 printk("\n");
680 }
681 restore_flags (flags);
682 return err;
683 }
684
685
686
687
688 #define ERROR_MAX 8
689 #define ERROR_RESET 3
690 #define ERROR_RECAL 1
691 static void ide_error (ide_dev_t *dev, const char *msg, byte stat)
692 {
693 struct request *rq;
694 byte err;
695
696 err = dump_status(DEV_HWIF, msg, stat);
697 if ((rq = ide_cur_rq[DEV_HWIF]) == NULL || dev == NULL)
698 return;
699 #ifdef IDE_DRIVE_CMD
700 if (rq->cmd == IDE_DRIVE_CMD) {
701 end_drive_cmd(dev, stat, err);
702 return;
703 }
704 #endif
705 if (stat & BUSY_STAT) {
706 rq->errors |= ERROR_RESET;
707 } else {
708 if (dev->type == disk && (stat & ERR_STAT)) {
709
710 if (err & BBD_ERR)
711 rq->errors = ERROR_MAX;
712 else if (err & TRK0_ERR)
713 rq->errors |= ERROR_RECAL;
714 }
715 if ((stat & DRQ_STAT) && rq->cmd == READ) {
716 int i = dev->mult_count ? dev->mult_count<<8 : 1<<8;
717 while (i-- > 0)
718 (void) IN_BYTE(HD_DATA, dev->hwif);
719 }
720 }
721 if (GET_STAT(dev->hwif) & (BUSY_STAT|DRQ_STAT))
722 rq->errors |= ERROR_RESET;
723
724 if (rq->errors >= ERROR_MAX)
725 end_request(0, DEV_HWIF);
726 else {
727 if ((rq->errors & ERROR_RESET) == ERROR_RESET)
728 do_ide_reset(dev);
729 else if ((rq->errors & ERROR_RECAL) == ERROR_RECAL)
730 dev->special.b.recalibrate = 1;
731 ++rq->errors;
732 }
733 }
734
735 static void read_intr (ide_dev_t *dev)
736 {
737 byte stat;
738 int i;
739 unsigned int msect, nsect;
740 struct request *rq;
741
742 if (!OK_STAT(stat=GET_STAT(DEV_HWIF),DATA_READY,BAD_R_STAT)) {
743 sti();
744 ide_error(dev, "read_intr", stat);
745 DO_REQUEST;
746 return;
747 }
748 msect = dev->mult_count;
749 read_next:
750 rq = ide_cur_rq[DEV_HWIF];
751 if (msect) {
752 if ((nsect = rq->current_nr_sectors) > msect)
753 nsect = msect;
754 msect -= nsect;
755 } else
756 nsect = 1;
757 input_ide_data(dev, rq->buffer, nsect * SECTOR_WORDS);
758 #ifdef DEBUG
759 printk("%s: read: sectors(%ld-%ld), buffer=0x%08lx, remaining=%ld\n",
760 dev->name, rq->sector, rq->sector+nsect-1,
761 (unsigned long) rq->buffer+(nsect<<9), rq->nr_sectors-nsect);
762 #endif
763 rq->sector += nsect;
764 rq->buffer += nsect<<9;
765 rq->errors = 0;
766 i = (rq->nr_sectors -= nsect);
767 if ((rq->current_nr_sectors -= nsect) <= 0)
768 end_request(1, DEV_HWIF);
769 if (i > 0) {
770 if (msect)
771 goto read_next;
772 ide_handler[DEV_HWIF] = &read_intr;
773 return;
774 }
775
776 DO_REQUEST;
777 }
778
779 static void write_intr (ide_dev_t *dev)
780 {
781 byte stat;
782 int i;
783 struct request *rq = ide_cur_rq[DEV_HWIF];
784
785 if (OK_STAT(stat=GET_STAT(DEV_HWIF),DRIVE_READY,BAD_W_STAT)) {
786 #ifdef DEBUG
787 printk("%s: write: sector %ld, buffer=0x%08lx, remaining=%ld\n",
788 dev->name, rq->sector, (unsigned long) rq->buffer,
789 rq->nr_sectors-1);
790 #endif
791 if ((rq->nr_sectors == 1) ^ ((stat & DRQ_STAT) != 0)) {
792 rq->sector++;
793 rq->buffer += 512;
794 rq->errors = 0;
795 i = --rq->nr_sectors;
796 --rq->current_nr_sectors;
797 if (rq->current_nr_sectors <= 0)
798 end_request(1, DEV_HWIF);
799 if (i > 0) {
800 ide_handler[DEV_HWIF] = &write_intr;
801 output_ide_data(dev, rq->buffer, SECTOR_WORDS);
802 return;
803 }
804 DO_REQUEST;
805 return;
806 }
807 }
808 sti();
809 ide_error(dev, "write_intr", stat);
810 DO_REQUEST;
811 }
812
813 static void multwrite (ide_dev_t *dev)
814 {
815 struct request *rq = &ide_write_rq[DEV_HWIF];
816 unsigned int mcount = dev->mult_count;
817
818 do {
819 unsigned int nsect = rq->current_nr_sectors;
820 if (nsect > mcount)
821 nsect = mcount;
822 mcount -= nsect;
823
824 output_ide_data(dev, rq->buffer, nsect<<7);
825 #ifdef DEBUG
826 printk("%s: multwrite: sector %ld, buffer=0x%08lx, count=%d, remaining=%ld\n",
827 dev->name, rq->sector, (unsigned long) rq->buffer,
828 nsect, rq->nr_sectors - nsect);
829 #endif
830 if ((rq->nr_sectors -= nsect) <= 0)
831 break;
832 if ((rq->current_nr_sectors -= nsect) == 0) {
833 if ((rq->bh = rq->bh->b_reqnext) != NULL) {
834 rq->current_nr_sectors = rq->bh->b_size>>9;
835 rq->buffer = rq->bh->b_data;
836 } else {
837 panic("%s: buffer list corrupted\n", dev->name);
838 break;
839 }
840 } else {
841 rq->buffer += nsect << 9;
842 }
843 } while (mcount);
844 }
845
846 static void multwrite_intr (ide_dev_t *dev)
847 {
848 byte stat;
849 int i;
850 struct request *rq = &ide_write_rq[DEV_HWIF];
851
852 if (OK_STAT(stat=GET_STAT(DEV_HWIF),DRIVE_READY,BAD_W_STAT)) {
853 if (stat & DRQ_STAT) {
854 if (rq->nr_sectors) {
855 if (dev->mult_count)
856 multwrite(dev);
857 ide_handler[DEV_HWIF] = &multwrite_intr;
858 return;
859 }
860 } else {
861 if (!rq->nr_sectors) {
862 rq = ide_cur_rq[DEV_HWIF];
863 for (i = rq->nr_sectors; i > 0;){
864 i -= rq->current_nr_sectors;
865 end_request(1, DEV_HWIF);
866 }
867 DO_REQUEST;
868 return;
869 }
870 }
871 }
872 sti();
873 ide_error(dev, "multwrite_intr", stat);
874 DO_REQUEST;
875 }
876
877
878
879
880
881 static inline void ide_cmd(ide_dev_t *dev, byte cmd, byte nsect,
882 void (*handler)(ide_dev_t *dev))
883 {
884 OUT_BYTE(dev->ctl,HD_CMD);
885 OUT_BYTE(nsect,HD_NSECTOR);
886 OUT_BYTE(cmd,HD_COMMAND);
887 ide_handler[DEV_HWIF] = handler;
888 }
889
890 static void set_multmode_intr (ide_dev_t *dev)
891 {
892 byte stat = GET_STAT(DEV_HWIF);
893
894 sti();
895 if (!OK_STAT(stat,READY_STAT,BAD_STAT)) {
896 dev->mult_req = dev->mult_count = 0;
897 dev->special.b.recalibrate = 1;
898 (void) dump_status(DEV_HWIF, "set_multmode", stat);
899 } else {
900 if ((dev->mult_count = dev->mult_req))
901 printk (" %s: enabled %d-sector multiple mode\n",
902 dev->name, dev->mult_count);
903 else
904 printk (" %s: multiple mode turned off\n", dev->name);
905 }
906 DO_REQUEST;
907 }
908
909 static void set_geometry_intr (ide_dev_t *dev)
910 {
911 byte stat = GET_STAT(DEV_HWIF);
912
913 sti();
914 if (!OK_STAT(stat,READY_STAT,BAD_STAT))
915 ide_error(dev, "set_geometry_intr", stat);
916 DO_REQUEST;
917 }
918
919 static void recal_intr (ide_dev_t *dev)
920 {
921 byte stat = GET_STAT(DEV_HWIF);
922
923 sti();
924 if (!OK_STAT(stat,READY_STAT,BAD_STAT))
925 ide_error(dev, "recal_intr", stat);
926 DO_REQUEST;
927 }
928
929 static void drive_cmd_intr (ide_dev_t *dev)
930 {
931 byte stat = GET_STAT(DEV_HWIF);
932
933 sti();
934 if (!OK_STAT(stat,READY_STAT,BAD_STAT))
935 ide_error(dev, "drive_cmd", stat);
936 else
937 end_drive_cmd (dev, stat, GET_ERR(DEV_HWIF));
938 DO_REQUEST;
939 }
940
941 static void timer_expiry (byte hwif)
942 {
943 unsigned long flags;
944
945 save_flags(flags);
946 cli();
947
948 if (ide_handler[HWIF] == NULL || (timer_active & ide_timerbit[HWIF])) {
949
950 sti();
951 printk("%s: marginal timeout\n", ide_name[HWIF]);
952 } else {
953 ide_handler[HWIF] = NULL;
954 disable_irq(ide_irq[HWIF]);
955 #if SUPPORT_SERIALIZE
956 if (single_threaded && ide_irq[HWIF] != ide_irq[HWIF^1])
957 disable_irq(ide_irq[HWIF^1]);
958 #endif
959 sti();
960 ide_error(ide_cur_dev[HWIF], "timeout", GET_STAT(HWIF));
961 do_request(HWIF);
962 #if SUPPORT_SHARING_IRQ
963 if (single_threaded)
964 hwif = current_hwif;
965 #endif
966 cli();
967 start_ide_timer(HWIF);
968 enable_irq(ide_irq[HWIF]);
969 #if SUPPORT_SERIALIZE
970 if (single_threaded && ide_irq[HWIF] != ide_irq[HWIF^1])
971 enable_irq(ide_irq[HWIF^1]);
972 #endif
973 }
974 restore_flags(flags);
975 }
976
977 static void ide0_timer_expiry (void)
978 {
979 timer_expiry (0);
980 }
981
982 static void ide1_timer_expiry (void)
983 {
984 timer_expiry (1);
985 }
986
987 static int do_special (ide_dev_t *dev)
988 {
989 special_t *s = &dev->special;
990 #ifdef DEBUG
991 printk("%s: do_special: 0x%02x\n", dev->name, s->all);
992 #endif
993 if (s->b.set_geometry) {
994 s->b.set_geometry = 0;
995 if (dev->type == disk) {
996 OUT_BYTE(dev->sect,HD_SECTOR);
997 OUT_BYTE(dev->cyl,HD_LCYL);
998 OUT_BYTE(dev->cyl>>8,HD_HCYL);
999 OUT_BYTE(((dev->head-1)|dev->select.all)&0xBF,HD_CURRENT);
1000 ide_cmd(dev, WIN_SPECIFY, dev->sect, &set_geometry_intr);
1001 }
1002 } else if (s->b.recalibrate) {
1003 s->b.recalibrate = 0;
1004 if (dev->type == disk)
1005 ide_cmd(dev,WIN_RESTORE,dev->sect,&recal_intr);
1006 } else if (s->b.set_multmode) {
1007 if (dev->type == disk) {
1008 if (dev->id && dev->mult_req > dev->id->max_multsect)
1009 dev->mult_req = dev->id->max_multsect;
1010 ide_cmd(dev,WIN_SETMULT,dev->mult_req,&set_multmode_intr);
1011 } else {
1012 dev->mult_req = 0;
1013 printk("%s: multmode not supported by this device\n", dev->name);
1014 }
1015 s->b.set_multmode = 0;
1016 } else {
1017 if (s->all) {
1018 printk("%s: bad special flag: 0x%02x\n", dev->name, s->all);
1019 s->all = 0;
1020 }
1021 }
1022 return (ide_handler[DEV_HWIF] == NULL) ? 1 : 0;
1023 }
1024
1025 #ifdef CONFIG_BLK_DEV_IDECD
1026 static byte wait_stat (ide_dev_t *dev, byte good, byte bad, unsigned long timeout)
1027 {
1028 unsigned long flags;
1029
1030 save_flags(flags);
1031 sti();
1032 WAIT_STAT(dev, good, bad, timeout, "status", error);
1033 restore_flags(flags);
1034 return 0;
1035 error:
1036 restore_flags(flags);
1037 return 1;
1038 }
1039
1040 #include "ide-cd.c"
1041 #endif
1042
1043 static inline int do_rw_disk (ide_dev_t *dev, struct request *rq, unsigned long block)
1044 {
1045 OUT_BYTE(dev->ctl,HD_CMD);
1046 OUT_BYTE(rq->nr_sectors,HD_NSECTOR);
1047 if (dev->select.b.lba) {
1048 #ifdef DEBUG
1049 printk("%s: %sing: LBAsect=%ld, sectors=%ld, buffer=0x%08lx\n",
1050 dev->name, (rq->cmd==READ)?"read":"writ",
1051 block, rq->nr_sectors, (unsigned long) rq->buffer);
1052 #endif
1053 OUT_BYTE(block,HD_SECTOR);
1054 OUT_BYTE(block>>=8,HD_LCYL);
1055 OUT_BYTE(block>>=8,HD_HCYL);
1056 OUT_BYTE(((block>>8)&0x0f)|dev->select.all,HD_CURRENT);
1057 } else {
1058 unsigned int sect,head,cyl,track;
1059 track = block / dev->sect;
1060 sect = block % dev->sect + 1;
1061 OUT_BYTE(sect,HD_SECTOR);
1062 head = track % dev->head;
1063 cyl = track / dev->head;
1064 OUT_BYTE(cyl,HD_LCYL);
1065 OUT_BYTE(cyl>>8,HD_HCYL);
1066 OUT_BYTE(head|dev->select.all,HD_CURRENT);
1067 #ifdef DEBUG
1068 printk("%s: %sing: CHS=%d/%d/%d, sectors=%ld, buffer=0x%08lx\n",
1069 dev->name, (rq->cmd==READ)?"read":"writ", cyl,
1070 head, sect, rq->nr_sectors, (unsigned long) rq->buffer);
1071 #endif
1072 }
1073 if (rq->cmd == READ) {
1074 OUT_BYTE(dev->mult_count ? WIN_MULTREAD : WIN_READ, HD_COMMAND);
1075 ide_handler[DEV_HWIF] = &read_intr;
1076 return 0;
1077 }
1078 if (rq->cmd == WRITE) {
1079 OUT_BYTE(dev->wpcom,HD_PRECOMP);
1080 OUT_BYTE(dev->mult_count ? WIN_MULTWRITE : WIN_WRITE, HD_COMMAND);
1081 WAIT_STAT(dev, DATA_READY, BAD_W_STAT, WAIT_DRQ, "DRQ", error);
1082 if (!dev->unmask)
1083 cli();
1084 if (dev->mult_count) {
1085 ide_write_rq[DEV_HWIF] = *rq;
1086 multwrite(dev);
1087 ide_handler[DEV_HWIF] = &multwrite_intr;
1088 } else {
1089 output_ide_data(dev, rq->buffer, SECTOR_WORDS);
1090 ide_handler[DEV_HWIF] = &write_intr;
1091 }
1092 return 0;
1093 }
1094 #ifdef IDE_DRIVE_CMD
1095 if (rq->cmd == IDE_DRIVE_CMD) {
1096 byte *args = rq->buffer;
1097 if (args) {
1098 OUT_BYTE(args[2],HD_FEATURE);
1099 ide_cmd(dev, args[0], args[1], &drive_cmd_intr);
1100 printk("%s: DRIVE_CMD cmd=0x%02x sc=0x%02x fr=0x%02x\n",
1101 dev->name, args[0], args[1], args[2]);
1102 return 0;
1103 } else {
1104 #ifdef DEBUG
1105 printk("%s: DRIVE_CMD (null)\n", dev->name);
1106 #endif
1107 end_drive_cmd(dev,GET_STAT(DEV_HWIF),GET_ERR(DEV_HWIF));
1108 return 1;
1109 }
1110 }
1111 #endif
1112 printk("%s: bad command: %d\n", dev->name, rq->cmd);
1113 end_request(0, DEV_HWIF);
1114 error:
1115 return 1;
1116 }
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129 static void do_request (byte hwif)
1130 {
1131 unsigned int minor, drive;
1132 unsigned long block, blockend;
1133 struct request *rq;
1134 ide_dev_t *dev;
1135 repeat:
1136 sti();
1137 #if SUPPORT_SHARING_IRQ
1138 current_hwif = hwif;
1139 #endif
1140 if ((rq = ide_cur_rq[HWIF]) == NULL) {
1141 rq = blk_dev[ide_major[HWIF]].current_request;
1142 if ((rq == NULL) || (rq->dev < 0)) {
1143 #if SUPPORT_SHARING_IRQ
1144 if (single_threaded) {
1145 if (sharing_single_irq && (dev = ide_cur_dev[hwif]))
1146 OUT_BYTE(dev->ctl|2,HD_CMD);
1147 rq = blk_dev[ide_major[hwif^=1]].current_request;
1148 if ((rq != NULL) && (rq->dev >= 0))
1149 goto repeat;
1150 }
1151 #endif
1152 return;
1153 }
1154 blk_dev[ide_major[HWIF]].current_request = rq->next;
1155 ide_cur_rq[HWIF] = rq;
1156 }
1157 #ifdef DEBUG
1158 printk("%s: do_request: current=0x%08lx\n",ide_name[HWIF],(unsigned long)rq);
1159 #endif
1160 minor = MINOR(rq->dev);
1161 drive = minor >> PARTN_BITS;
1162 ide_cur_dev[HWIF] = dev = &ide_dev[HWIF][drive];
1163 if ((MAJOR(rq->dev) != ide_major[HWIF]) || (drive >= MAX_DRIVES)) {
1164 printk("%s: bad device number: 0x%04x\n", ide_name[HWIF], rq->dev);
1165 end_request(0, HWIF);
1166 goto repeat;
1167 }
1168 if (rq->bh && !rq->bh->b_lock) {
1169 printk("%s: block not locked\n", ide_name[HWIF]);
1170 end_request(0, HWIF);
1171 goto repeat;
1172 }
1173 block = rq->sector;
1174 blockend = block + rq->nr_sectors;
1175 if ((blockend < block) || (blockend > ide_hd[HWIF][minor].nr_sects)) {
1176 printk("%s: bad access: block=%ld, count=%ld\n",
1177 dev->name, block, rq->nr_sectors);
1178 end_request(0, HWIF);
1179 goto repeat;
1180 }
1181 block += ide_hd[HWIF][minor].start_sect;
1182 #if (DISK_RECOVERY_TIME > 0)
1183 while ((read_timer() - ide_lastreq[HWIF]) < DISK_RECOVERY_TIME);
1184 #endif
1185 OUT_BYTE(dev->select.all,HD_CURRENT);
1186 #ifdef CONFIG_BLK_DEV_IDECD
1187 WAIT_STAT(dev, (dev->type == cdrom) ? 0 : READY_STAT,
1188 BUSY_STAT|DRQ_STAT, WAIT_READY, "DRDY", repeat);
1189 #else
1190 WAIT_STAT(dev, READY_STAT, BUSY_STAT|DRQ_STAT, WAIT_READY, "DRDY", repeat);
1191 #endif
1192 if (!dev->special.all) {
1193 #ifdef CONFIG_BLK_DEV_IDECD
1194 if (dev->type == disk) {
1195 #endif
1196 if (do_rw_disk(dev, rq, block))
1197 goto repeat;
1198 #ifdef CONFIG_BLK_DEV_IDECD
1199 } else {
1200 if (do_rw_cdrom(dev, block))
1201 goto repeat;
1202 }
1203 #endif
1204 } else {
1205 if (do_special(dev))
1206 goto repeat;
1207 }
1208 }
1209
1210
1211
1212
1213
1214
1215 #define DO_IDE_REQUEST(hwif) \
1216 { \
1217 if (ide_handler[hwif] == NULL) { \
1218 disable_irq(ide_irq[hwif]); \
1219 if (single_threaded && ide_irq[hwif] != ide_irq[hwif^1]) \
1220 disable_irq(ide_irq[hwif^1]); \
1221 do_request(hwif); \
1222 cli(); \
1223 start_ide_timer(hwif); \
1224 enable_irq(ide_irq[hwif]); \
1225 if (single_threaded && ide_irq[hwif] != ide_irq[hwif^1]) \
1226 enable_irq(ide_irq[hwif^1]); \
1227 } \
1228 }
1229
1230 #if SUPPORT_TWO_INTERFACES
1231 static void do_ide0_request (void)
1232 {
1233 DO_IDE_REQUEST(0);
1234 }
1235
1236 static void do_ide1_request (void)
1237 {
1238 DO_IDE_REQUEST(1);
1239 }
1240 #else
1241 #define do_ide1_request do_ide0_request
1242 static void do_ide0_request (void)
1243 {
1244 DO_IDE_REQUEST(HWIF);
1245 }
1246 #endif
1247
1248 #if SUPPORT_SHARING_IRQ
1249 static void do_shared_request (void)
1250 {
1251 DO_IDE_REQUEST(current_hwif);
1252 }
1253 #endif
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265 static void unexpected_intr (byte hwif)
1266 {
1267 byte stat;
1268
1269 if (!OK_STAT(stat=GET_STAT(HWIF), DRIVE_READY, BAD_STAT))
1270 (void) dump_status(HWIF, "unexpected_intr", stat);
1271 outb_p(2,IDE_PORT(HD_CMD,hwif));
1272 #if SUPPORT_SHARING_IRQ
1273 if (single_threaded && ide_irq[hwif] == ide_irq[hwif^1]) {
1274 if (!OK_STAT(stat=GET_STAT(hwif^1), DRIVE_READY, BAD_STAT))
1275 (void) dump_status(hwif^1, "unexpected_intr", stat);
1276 outb_p(2,IDE_PORT(HD_CMD,hwif^1));
1277 }
1278 #endif
1279 }
1280
1281
1282
1283
1284
1285
1286 #define IDE_INTR(hwif) \
1287 { \
1288 ide_dev_t *dev; \
1289 void (*handler)(ide_dev_t *); \
1290 \
1291 timer_active &= ~ide_timerbit[hwif]; \
1292 if ((handler = ide_handler[hwif]) != NULL) { \
1293 ide_handler[hwif] = NULL; \
1294 dev = ide_cur_dev[hwif]; \
1295 if (dev->unmask) \
1296 sti(); \
1297 handler(dev); \
1298 } else \
1299 unexpected_intr(hwif); \
1300 cli(); \
1301 }
1302
1303 #if SUPPORT_SERIALIZE
1304
1305 static void ide_seq_intr (int irq, struct pt_regs *regs)
1306 {
1307 byte hwif = (irq != ide_irq[0]);
1308 IDE_INTR(HWIF);
1309 start_ide_timer(current_hwif);
1310 }
1311 #endif
1312
1313 #if OPTIMIZE_IRQS
1314
1315
1316 static void ide0_intr (int irq, struct pt_regs *regs)
1317 {
1318 IDE_INTR(0);
1319 start_ide_timer(0);
1320 }
1321
1322
1323 static void ide1_intr (int irq, struct pt_regs *regs)
1324 {
1325 IDE_INTR(1);
1326 start_ide_timer(1);
1327 }
1328
1329 #else
1330
1331 #define ide0_intr ide_intr
1332 #define ide1_intr ide_intr
1333
1334
1335 static void ide_intr (int irq, struct pt_regs *regs)
1336 {
1337 #if SUPPORT_TWO_INTERFACES
1338 byte hwif = (irq != ide_irq[0]);
1339 #endif
1340 IDE_INTR(HWIF);
1341 start_ide_timer(HWIF);
1342 }
1343
1344 #endif
1345
1346 #if SUPPORT_SHARING_IRQ
1347
1348 static void ide_shared_intr (int irq, struct pt_regs * regs)
1349 {
1350 IDE_INTR(current_hwif);
1351 start_ide_timer(current_hwif);
1352 }
1353 #endif
1354
1355 static ide_dev_t *get_info_ptr (int i_rdev)
1356 {
1357 unsigned int drive = DEVICE_NR(i_rdev);
1358 ide_dev_t *dev;
1359
1360 if (drive < MAX_DRIVES) {
1361 switch (MAJOR(i_rdev)) {
1362 case IDE0_MAJOR: dev = &ide_dev[0][drive];
1363 if (dev->present) return dev;
1364 break;
1365 case IDE1_MAJOR: dev = &ide_dev[1][drive];
1366 if (dev->present) return dev;
1367 break;
1368 }
1369 }
1370 return NULL;
1371 }
1372
1373 static int ide_open(struct inode * inode, struct file * filp)
1374 {
1375 ide_dev_t *dev;
1376 unsigned long flags;
1377
1378 if ((dev = get_info_ptr(inode->i_rdev)) == NULL)
1379 return -ENODEV;
1380 save_flags(flags);
1381 cli();
1382 while (dev->busy)
1383 sleep_on(&dev->wqueue);
1384 dev->usage++;
1385 restore_flags(flags);
1386 #ifdef CONFIG_BLK_DEV_IDECD
1387 if (dev->type == cdrom)
1388 return cdrom_open (inode, filp, dev);
1389 #endif
1390 return 0;
1391 }
1392
1393
1394
1395
1396
1397 static void ide_release(struct inode * inode, struct file * file)
1398 {
1399 ide_dev_t *dev;
1400
1401 if ((dev = get_info_ptr(inode->i_rdev)) != NULL) {
1402 sync_dev(inode->i_rdev);
1403 dev->usage--;
1404 #ifdef CONFIG_BLK_DEV_IDECD
1405 if (dev->type == cdrom)
1406 cdrom_release (inode, file, dev);
1407 #endif
1408 }
1409 }
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419 static int revalidate_disk(int i_rdev)
1420 {
1421 unsigned int i, major, start, drive = DEVICE_NR(i_rdev);
1422 ide_dev_t *dev;
1423 struct gendisk *gd;
1424 long flags;
1425
1426 if ((dev = get_info_ptr(i_rdev)) == NULL)
1427 return -ENODEV;
1428
1429 save_flags(flags);
1430 cli();
1431 if (dev->busy || (dev->usage > 1)) {
1432 restore_flags(flags);
1433 return -EBUSY;
1434 };
1435 dev->busy = 1;
1436 restore_flags(flags);
1437
1438 gd = &ide_gendisk[DEV_HWIF];
1439 major = ide_major[DEV_HWIF] << 8;
1440 start = drive << PARTN_BITS;
1441
1442 for (i = 0; i < (1<<PARTN_BITS); i++) {
1443 unsigned int minor = start + i;
1444 sync_dev (major | minor);
1445 invalidate_inodes (major | minor);
1446 invalidate_buffers (major | minor);
1447 gd->part[minor].start_sect = 0;
1448 gd->part[minor].nr_sects = 0;
1449 };
1450
1451 gd->part[start].nr_sects = ide_capacity[DEV_HWIF][drive];
1452 resetup_one_dev(gd, drive);
1453
1454 dev->busy = 0;
1455 wake_up(&dev->wqueue);
1456 return 0;
1457 }
1458
1459 #ifdef IDE_DRIVE_CMD
1460
1461
1462
1463
1464
1465
1466 static int do_drive_cmd(int dev, char *args)
1467 {
1468 unsigned long flags;
1469 unsigned int major = MAJOR(dev);
1470 struct request rq, *cur_rq;
1471 struct blk_dev_struct *bdev;
1472 struct semaphore sem = MUTEX_LOCKED;
1473
1474
1475 rq.buffer = args;
1476 rq.cmd = IDE_DRIVE_CMD;
1477 rq.errors = 0;
1478 rq.sector = 0;
1479 rq.nr_sectors = 0;
1480 rq.current_nr_sectors = 0;
1481 rq.sem = &sem;
1482 rq.bh = NULL;
1483 rq.bhtail = NULL;
1484 rq.next = NULL;
1485 rq.dev = dev;
1486 bdev = &blk_dev[major];
1487
1488 save_flags(flags);
1489 cli();
1490 cur_rq = bdev->current_request;
1491 if (cur_rq == NULL) {
1492 bdev->current_request = &rq;
1493 bdev->request_fn();
1494 } else {
1495 while (cur_rq->next != NULL)
1496 cur_rq = cur_rq->next;
1497 cur_rq->next = &rq;
1498 }
1499
1500 down(&sem);
1501 restore_flags(flags);
1502 return rq.errors ? -EIO : 0;
1503 }
1504 #endif
1505
1506 static int write_fs_long (unsigned long useraddr, long value)
1507 {
1508 int err;
1509
1510 if (NULL == (long *)useraddr)
1511 return -EINVAL;
1512 if ((err = verify_area(VERIFY_WRITE, (long *)useraddr, sizeof(long))))
1513 return err;
1514 put_user((unsigned)value, (long *) useraddr);
1515 return 0;
1516 }
1517
1518 static int ide_ioctl (struct inode *inode, struct file *file,
1519 unsigned int cmd, unsigned long arg)
1520 {
1521 struct hd_geometry *loc = (struct hd_geometry *) arg;
1522 int err;
1523 ide_dev_t *dev;
1524 unsigned long flags;
1525
1526 if (!inode || !inode->i_rdev)
1527 return -EINVAL;
1528 if ((dev = get_info_ptr(inode->i_rdev)) == NULL)
1529 return -ENODEV;
1530 switch (cmd) {
1531 case HDIO_GETGEO:
1532 if (!loc || dev->type != disk) return -EINVAL;
1533 err = verify_area(VERIFY_WRITE, loc, sizeof(*loc));
1534 if (err) return err;
1535 put_user(dev->bios_head,
1536 (char *) &loc->heads);
1537 put_user(dev->bios_sect,
1538 (char *) &loc->sectors);
1539 put_user(dev->bios_cyl,
1540 (short *) &loc->cylinders);
1541 put_user((unsigned)ide_hd[DEV_HWIF][MINOR(inode->i_rdev)].start_sect,
1542 (long *) &loc->start);
1543 return 0;
1544
1545 case BLKFLSBUF:
1546 if(!suser()) return -EACCES;
1547 fsync_dev(inode->i_rdev);
1548 invalidate_buffers(inode->i_rdev);
1549 return 0;
1550
1551 case BLKRASET:
1552 if(!suser()) return -EACCES;
1553 if(arg > 0xff) return -EINVAL;
1554 read_ahead[MAJOR(inode->i_rdev)] = arg;
1555 return 0;
1556
1557 case BLKRAGET:
1558 return write_fs_long(arg, read_ahead[MAJOR(inode->i_rdev)]);
1559
1560 case BLKGETSIZE:
1561 return write_fs_long(arg, ide_hd[DEV_HWIF][MINOR(inode->i_rdev)].nr_sects);
1562 case BLKRRPART:
1563 return revalidate_disk(inode->i_rdev);
1564
1565 case HDIO_GET_KEEPSETTINGS:
1566 return write_fs_long(arg, dev->keep_settings);
1567
1568 case HDIO_GET_UNMASKINTR:
1569 return write_fs_long(arg, dev->unmask);
1570
1571 case HDIO_GET_CHIPSET:
1572 return write_fs_long(arg, dev->chipset);
1573
1574 case HDIO_GET_MULTCOUNT:
1575 return write_fs_long(arg, dev->mult_count);
1576
1577 case HDIO_GET_IDENTITY:
1578 if (!arg || (MINOR(inode->i_rdev) & PARTN_MASK))
1579 return -EINVAL;
1580 if (dev->id == NULL)
1581 return -ENOMSG;
1582 err = verify_area(VERIFY_WRITE, (char *)arg, sizeof(*dev->id));
1583 if (err) return err;
1584 memcpy_tofs((char *)arg, (char *)dev->id, sizeof(*dev->id));
1585 return 0;
1586
1587 case HDIO_SET_KEEPSETTINGS:
1588 case HDIO_SET_UNMASKINTR:
1589 if (!suser()) return -EACCES;
1590 if ((arg > 1) || (MINOR(inode->i_rdev) & PARTN_MASK))
1591 return -EINVAL;
1592 save_flags(flags);
1593 cli();
1594 if (cmd == HDIO_SET_KEEPSETTINGS)
1595 dev->keep_settings = arg;
1596 else
1597 dev->unmask = arg;
1598 restore_flags(flags);
1599 return 0;
1600
1601 case HDIO_SET_CHIPSET:
1602 if (!suser()) return -EACCES;
1603 if ((arg > 3) || (MINOR(inode->i_rdev) & PARTN_MASK))
1604 return -EINVAL;
1605 save_flags(flags);
1606 cli();
1607 dev->chipset = arg;
1608 dev->vlb_sync = (arg & 2) >> 1;
1609 dev->vlb_32bit = (arg & 1);
1610 restore_flags(flags);
1611 return 0;
1612
1613 case HDIO_SET_MULTCOUNT:
1614 if (!suser()) return -EACCES;
1615 if (MINOR(inode->i_rdev) & PARTN_MASK)
1616 return -EINVAL;
1617 if ((dev->id != NULL) && (arg > dev->id->max_multsect))
1618 return -EINVAL;
1619 save_flags(flags);
1620 cli();
1621 if (dev->special.b.set_multmode) {
1622 restore_flags(flags);
1623 return -EBUSY;
1624 }
1625 dev->mult_req = arg;
1626 dev->special.b.set_multmode = 1;
1627 restore_flags(flags);
1628 #ifdef IDE_DRIVE_CMD
1629 do_drive_cmd (inode->i_rdev, NULL);
1630 return (dev->mult_count == arg) ? 0 : -EIO;
1631 #else
1632 return 0;
1633 #endif
1634
1635 #ifdef IDE_DRIVE_CMD
1636 case HDIO_DRIVE_CMD:
1637 {
1638 unsigned long args;
1639
1640 if (NULL == (long *) arg)
1641 err = do_drive_cmd(inode->i_rdev,NULL);
1642 else {
1643 if (!(err = verify_area(VERIFY_WRITE,(long *)arg,sizeof(long))))
1644 {
1645 args = get_user((long *)arg);
1646 err = do_drive_cmd(inode->i_rdev,(char *)&args);
1647 put_user(args,(long *)arg);
1648 }
1649 }
1650 return err;
1651 }
1652 #endif
1653
1654 RO_IOCTLS(inode->i_rdev, arg);
1655
1656 default:
1657 #ifdef CONFIG_BLK_DEV_IDECD
1658 if (dev->type == cdrom)
1659 return ide_cdrom_ioctl(dev, inode, file, cmd, arg);
1660 #endif
1661 return -EPERM;
1662 }
1663 }
1664
1665 #ifdef CONFIG_BLK_DEV_IDECD
1666 static int ide_check_media_change (dev_t full_dev)
1667 {
1668 ide_dev_t *dev;
1669
1670 if ((dev = get_info_ptr(full_dev)) == NULL)
1671 return -ENODEV;
1672 if (dev->type != cdrom)
1673 return 0;
1674 return cdrom_check_media_change (dev);
1675 }
1676 #endif
1677
1678
1679 static void fixstring (byte *s, int bytecount, int byteswap)
1680 {
1681 byte *p, *end = &s[bytecount &= ~1];
1682
1683 if (byteswap) {
1684
1685 for (p = end ; p != s;) {
1686 unsigned short *pp = (unsigned short *) (p -= 2);
1687 *pp = (*pp >> 8) | (*pp << 8);
1688 }
1689 }
1690 p = s;
1691
1692
1693 while (s != end && *s == ' ')
1694 ++s;
1695
1696
1697 while (s != end && *s) {
1698 if (*s++ != ' ' || (s != end && *s && *s != ' '))
1699 *p++ = *(s-1);
1700 }
1701
1702
1703 while (p != end)
1704 *p++ = '\0';
1705 }
1706
1707 static int lba_capacity_is_ok (struct hd_driveid *id)
1708
1709
1710
1711
1712 {
1713 unsigned long lba_sects = id->lba_capacity;
1714 unsigned long chs_sects = id->cyls * id->heads * id->sectors;
1715 unsigned long _10_percent = chs_sects / 10;
1716
1717
1718 if ((lba_sects - chs_sects) < _10_percent)
1719 return 1;
1720
1721
1722 lba_sects = (lba_sects << 16) | (lba_sects >> 16);
1723 if ((lba_sects - chs_sects) < _10_percent) {
1724 id->lba_capacity = lba_sects;
1725 return 1;
1726 }
1727 return 0;
1728 }
1729
1730 static unsigned long probe_mem_start;
1731
1732 static void do_identify (ide_dev_t *dev, byte cmd)
1733 {
1734 int bswap;
1735 struct hd_driveid *id;
1736 unsigned long capacity, check;
1737
1738 id = dev->id = (struct hd_driveid *) probe_mem_start;
1739 probe_mem_start += 512;
1740 input_ide_data(dev, id, SECTOR_WORDS);
1741 sti();
1742
1743
1744
1745
1746 if ((id->model[0] == 'P' && id->model[1] == 'M')
1747 || (id->model[0] == 'S' && id->model[1] == 'K')) {
1748 printk("%s: EATA SCSI HBA %.10s\n", dev->name, id->model);
1749 dev->present = 0;
1750 return;
1751 }
1752
1753
1754
1755
1756
1757 bswap = 1;
1758 if (cmd == WIN_PIDENTIFY) {
1759 if ((id->model[0] == 'N' && id->model[1] == 'E')
1760 || (id->model[0] == 'F' && id->model[1] == 'X'))
1761 bswap = 0;
1762 }
1763 fixstring (id->model, sizeof(id->model), bswap);
1764 fixstring (id->fw_rev, sizeof(id->fw_rev), bswap);
1765 fixstring (id->serial_no, sizeof(id->serial_no), bswap);
1766
1767
1768
1769
1770 if (cmd == WIN_PIDENTIFY) {
1771 #ifdef CONFIG_BLK_DEV_IDECD
1772 byte type = (id->config >> 8) & 0x0f;
1773 #endif
1774 printk("%s: %s, ATAPI,", dev->name, id->model);
1775 #ifdef CONFIG_BLK_DEV_IDECD
1776 if (type == 0 || type == 5)
1777 printk(" CDROM drive\n");
1778 else
1779 printk(" UNKNOWN device\n");
1780 dev->type = cdrom;
1781 dev->present = 1;
1782 #else
1783 printk(unsupported);
1784 #endif
1785 return;
1786 }
1787
1788 dev->type = disk;
1789
1790 if (!dev->present) {
1791 dev->present = 1;
1792 dev->cyl = dev->bios_cyl = id->cyls;
1793 dev->head = dev->bios_head = id->heads;
1794 dev->sect = dev->bios_sect = id->sectors;
1795 }
1796
1797 if ((id->field_valid & 1) && id->cur_cyls && id->cur_heads
1798 && (id->cur_heads <= 16) && id->cur_sectors)
1799 {
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810 dev->cyl = id->cur_cyls;
1811 dev->head = id->cur_heads;
1812 dev->sect = id->cur_sectors;
1813 capacity = dev->cyl * dev->head * dev->sect;
1814
1815
1816 check = (id->cur_capacity0 << 16) | id->cur_capacity1;
1817 if (check == capacity) {
1818
1819 id->cur_capacity0 = (capacity >> 0) & 0xffff;
1820 id->cur_capacity1 = (capacity >> 16) & 0xffff;
1821 }
1822 }
1823
1824 if ((!dev->head || dev->head > 16) && id->heads && id->heads <= 16) {
1825 dev->cyl = id->cyls;
1826 dev->head = id->heads;
1827 dev->sect = id->sectors;
1828 }
1829
1830 if (dev->sect == dev->bios_sect && dev->head == dev->bios_head) {
1831 if (dev->cyl > dev->bios_cyl)
1832 dev->bios_cyl = dev->cyl;
1833 }
1834
1835 if ((id->capability & 2) && lba_capacity_is_ok(id)) {
1836 dev->select.b.lba = 1;
1837 capacity = id->lba_capacity;
1838 } else {
1839 capacity = dev->cyl * dev->head * dev->sect;
1840 }
1841
1842 ide_capacity[DEV_HWIF][dev->select.b.drive] = capacity;
1843 printk ("%s: %.40s, %ldMB w/%dKB Cache, %sCHS=%d/%d/%d",
1844 dev->name, id->model, capacity/2048L, id->buf_size/2,
1845 dev->select.b.lba ? "LBA, " : "",
1846 dev->bios_cyl, dev->bios_head, dev->bios_sect);
1847
1848 dev->mult_count = 0;
1849 if (id->max_multsect) {
1850 dev->mult_req = INITIAL_MULT_COUNT;
1851 if (dev->mult_req > id->max_multsect)
1852 dev->mult_req = id->max_multsect;
1853 if (dev->mult_req || ((id->multsect_valid & 1) && id->multsect))
1854 dev->special.b.set_multmode = 1;
1855 printk(", MaxMult=%d", id->max_multsect);
1856 }
1857 printk("\n");
1858 }
1859
1860
1861
1862
1863
1864 static void delay_10ms (void)
1865 {
1866 unsigned long timer = jiffies + (HZ + 99)/100 + 1;
1867 while (timer > jiffies);
1868 }
1869
1870
1871 static int try_to_identify (ide_dev_t *dev, byte cmd)
1872
1873
1874
1875
1876
1877 {
1878 int hd_status, rc;
1879 unsigned long timeout;
1880 #if PROBE_FOR_IRQS
1881 int irqs = 0;
1882 static byte irq_probed[2] = {0,0};
1883 #endif
1884
1885 OUT_BYTE(dev->ctl|2,HD_CMD);
1886 #if PROBE_FOR_IRQS
1887 if (!irq_probed[DEV_HWIF]) {
1888 probe_irq_off(probe_irq_on());
1889 irqs = probe_irq_on();
1890 OUT_BYTE(dev->ctl,HD_CMD);
1891 }
1892 #endif
1893 delay_10ms();
1894 if ((IN_BYTE(HD_ALTSTATUS,DEV_HWIF) ^ IN_BYTE(HD_STATUS,DEV_HWIF)) & ~INDEX_STAT) {
1895 hd_status = HD_STATUS;
1896 printk("%s: probing with STATUS instead of ALTSTATUS\n", dev->name);
1897 } else
1898 hd_status = HD_ALTSTATUS;
1899 OUT_BYTE(cmd,HD_COMMAND);
1900 timeout = ((cmd == WIN_IDENTIFY) ? WAIT_WORSTCASE : WAIT_PIDENTIFY) / 2;
1901 timeout += jiffies;
1902 do {
1903 if (jiffies > timeout) {
1904 #if PROBE_FOR_IRQS
1905 if (!irq_probed[DEV_HWIF])
1906 (void) probe_irq_off(irqs);
1907 #endif
1908 return 1;
1909 }
1910 delay_10ms();
1911 } while (IN_BYTE(hd_status,DEV_HWIF) & BUSY_STAT);
1912 delay_10ms();
1913 if (OK_STAT(GET_STAT(DEV_HWIF),DRQ_STAT,BAD_R_STAT)) {
1914 cli();
1915 do_identify(dev, cmd);
1916 rc = 0;
1917 } else
1918 rc = 2;
1919 #if PROBE_FOR_IRQS
1920 if (!irq_probed[DEV_HWIF]) {
1921 irqs = probe_irq_off(irqs);
1922 if (irqs > 0) {
1923 irq_probed[DEV_HWIF] = 1;
1924 ide_irq[DEV_HWIF] = irqs;
1925 } else
1926 printk("%s: IRQ probe failed (%d)\n", dev->name, irqs);
1927 }
1928 #endif
1929 return rc;
1930 }
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942 static int do_probe (ide_dev_t *dev, byte cmd)
1943
1944
1945
1946
1947
1948
1949
1950 {
1951 int rc;
1952
1953 #ifdef CONFIG_BLK_DEV_IDECD
1954 if (dev->present) {
1955 if ((dev->type == disk) ^ (cmd == WIN_IDENTIFY))
1956 return 4;
1957 }
1958 #endif
1959 #if DEBUG
1960 printk("probing for %s: present=%d, type=%s, probetype=%s\n",
1961 dev->name, dev->present, dev->type ? "cdrom":"disk",
1962 (cmd == WIN_IDENTIFY) ? "ATA" : "ATAPI");
1963 #endif
1964 OUT_BYTE(dev->select.all,HD_CURRENT);
1965 delay_10ms();
1966 if (IN_BYTE(HD_CURRENT,DEV_HWIF) != dev->select.all && !dev->present) {
1967 OUT_BYTE(0xa0,HD_CURRENT);
1968 return 3;
1969 }
1970
1971 if (OK_STAT(GET_STAT(DEV_HWIF),READY_STAT,BUSY_STAT)
1972 || dev->present || cmd == WIN_PIDENTIFY)
1973 {
1974 if ((rc = try_to_identify(dev, cmd)))
1975 rc = try_to_identify(dev, cmd);
1976 if (rc == 1)
1977 printk("%s: no response (status = 0x%02x)\n",
1978 dev->name, GET_STAT(DEV_HWIF));
1979 OUT_BYTE(dev->ctl|2,HD_CMD);
1980 delay_10ms();
1981 (void) GET_STAT(DEV_HWIF);
1982 } else {
1983 rc = 3;
1984 }
1985 if (dev->select.b.drive == 1) {
1986 OUT_BYTE(0xa0,HD_CURRENT);
1987 delay_10ms();
1988 OUT_BYTE(dev->ctl|2,HD_CMD);
1989 delay_10ms();
1990 (void) GET_STAT(DEV_HWIF);
1991 }
1992 return rc;
1993 }
1994
1995 static byte probe_for_drive (ide_dev_t *dev)
1996
1997
1998
1999
2000 {
2001 if (dev->dont_probe)
2002 return dev->present;
2003 if (do_probe(dev, WIN_IDENTIFY) >= 2) {
2004 #ifdef CONFIG_BLK_DEV_IDECD
2005 (void) do_probe(dev, WIN_PIDENTIFY);
2006 #endif
2007 }
2008 if (!dev->present)
2009 return 0;
2010 if (dev->id == NULL) {
2011 if (dev->type == disk) {
2012 printk ("%s: non-IDE device, CHS=%d/%d/%d\n",
2013 dev->name, dev->cyl, dev->head, dev->sect);
2014 }
2015 #ifdef CONFIG_BLK_DEV_IDECD
2016 else if (dev->type == cdrom) {
2017 printk("%s: ATAPI cdrom (?)\n", dev->name);
2018 }
2019 #endif
2020 else {
2021 dev->present = 0;
2022 return 1;
2023 }
2024 }
2025 #ifdef CONFIG_BLK_DEV_IDECD
2026 if (dev->type == cdrom)
2027 cdrom_setup(dev);
2028 #endif
2029 if (dev->type == disk && !dev->select.b.lba) {
2030 if (!dev->head || dev->head > 16) {
2031 printk("%s: cannot handle disk with %d physical heads\n",
2032 dev->name, dev->head);
2033 dev->present = 0;
2034 }
2035 }
2036 return 1;
2037 }
2038
2039 static void probe_for_drives (byte hwif)
2040 {
2041 ide_dev_t *devs = &ide_dev[HWIF][0];
2042
2043 if (check_region(IDE_PORT(HD_DATA,HWIF),8)
2044 || check_region(IDE_PORT(HD_CMD,HWIF),1))
2045 {
2046 if (devs[0].present || devs[1].present)
2047 printk("ERROR: ");
2048 printk("%s: port(s) already in use\n", ide_name[HWIF]);
2049 devs[0].present = 0;
2050 devs[1].present = 0;
2051 } else {
2052 unsigned long flags;
2053 save_flags(flags);
2054 sti();
2055
2056
2057 if (probe_for_drive(&devs[0]) || devs[1].present)
2058 (void) probe_for_drive(&devs[1]);
2059 #if PROBE_FOR_IRQS
2060 (void) probe_irq_off(probe_irq_on());
2061 #endif
2062 if (devs[0].present || devs[1].present) {
2063 request_region(IDE_PORT(HD_DATA,HWIF),8,ide_name[HWIF]);
2064 request_region(IDE_PORT(HD_CMD,HWIF),1,ide_name[HWIF]);
2065 }
2066 restore_flags(flags);
2067 }
2068 }
2069
2070 static int next_drive = 0;
2071
2072 void ide_setup(char *str, int *ints)
2073 {
2074 ide_dev_t *dev;
2075 const char *p[] = {"cyls","heads","sects","wpcom","irq"};
2076 int i, hwif, drive = next_drive++;
2077 #ifdef CONFIG_BLK_DEV_HD
2078 extern void hd_setup(char *, int *);
2079
2080 if (drive < 2) {
2081 hd_setup (str, ints);
2082 return;
2083 }
2084 #endif
2085 hwif = (drive > 1);
2086 printk("%s: ", ide_name[hwif]);
2087 if (drive > 3) {
2088 printk("too many drives defined\n");
2089 return;
2090 }
2091 drive = drive & 1;
2092 printk("%s: ", ide_devname[hwif][drive]);
2093 if (!SUPPORT_TWO_INTERFACES && hwif != HWIF) {
2094 printk(unsupported);
2095 return;
2096 }
2097 dev = &ide_dev[hwif][drive];
2098 if (dev->present)
2099 printk("(redefined) ");
2100 if (ints[0] == 0) {
2101 #if SUPPORT_DTC2278
2102 if (!strcmp(str,"dtc2278")) {
2103 printk("%s\n",str);
2104 probe_dtc2278 = 1;
2105 return;
2106 }
2107 #endif
2108 #if SUPPORT_SERIALIZE
2109 if (!strcmp(str,"serialize") || !strcmp(str,"cmd")) {
2110 printk("%s\n",str);
2111 single_threaded = 1;
2112 return;
2113 }
2114 #endif
2115 if (!strcmp(str,"noprobe")) {
2116 printk("%s\n",str);
2117 dev->dont_probe = 1;
2118 return;
2119 }
2120 #ifdef CONFIG_BLK_DEV_IDECD
2121 if (!strcmp(str,"cdrom")) {
2122 printk("cdrom\n");
2123 dev->present = 1;
2124 dev->type = cdrom;
2125 return;
2126 }
2127 #endif
2128 }
2129 if (ints[0] < 3 || ints[0] > 5) {
2130 printk("bad parms, expected: cyls,heads,sects[,wpcom[,irq]]\n");
2131 } else {
2132 for (i=0; i++ < ints[0];)
2133 printk("%s=%d%c",p[i-1],ints[i],i<ints[0]?',':'\n');
2134 dev->type = disk;
2135 dev->cyl = dev->bios_cyl = ints[1];
2136 dev->head = dev->bios_head = ints[2];
2137 dev->ctl = (ints[2] > 8 ? 8 : 0);
2138 dev->sect = dev->bios_sect = ints[3];
2139 dev->wpcom = (ints[0] >= 4) ? ints[4] : 0;
2140 if (ints[0] >= 5)
2141 ide_irq[HWIF] = ints[5];
2142 ide_capacity[HWIF][drive] = BIOS_SECTORS(dev);
2143 dev->present = 1;
2144 }
2145 }
2146
2147 void hda_setup(char *str, int *ints)
2148 {
2149 next_drive = 0;
2150 ide_setup (str, ints);
2151 }
2152
2153 void hdb_setup(char *str, int *ints)
2154 {
2155 next_drive = 1;
2156 ide_setup (str, ints);
2157 }
2158
2159 void hdc_setup(char *str, int *ints)
2160 {
2161 next_drive = 2;
2162 ide_setup (str, ints);
2163 }
2164
2165 void hdd_setup(char *str, int *ints)
2166 {
2167 next_drive = 3;
2168 ide_setup (str, ints);
2169 }
2170
2171 #ifndef CONFIG_BLK_DEV_HD
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184 static void probe_cmos_for_drives (void)
2185 {
2186 #ifdef __i386__
2187 extern struct drive_info_struct drive_info;
2188 byte drive, cmos_disks, *BIOS = (byte *) &drive_info;
2189
2190 outb_p(0x12,0x70);
2191 cmos_disks = inb_p(0x71);
2192
2193 for (drive = 0; drive < MAX_DRIVES; drive++) {
2194 ide_dev_t *dev = &ide_dev[0][drive];
2195 if ((cmos_disks & (0xf0 >> (drive*4))) && !dev->present) {
2196 dev->cyl = dev->bios_cyl = *(unsigned short *)BIOS;
2197 dev->head = dev->bios_head = * (BIOS+2);
2198 dev->sect = dev->bios_sect = * (BIOS+14);
2199 dev->wpcom = (*(unsigned short *)(BIOS+5))>>2;
2200 dev->ctl = *(BIOS+8);
2201 dev->wpcom = 0;
2202 dev->type = disk;
2203 dev->present = 1;
2204 ide_capacity[0][drive] = BIOS_SECTORS(dev);
2205 }
2206 BIOS += 16;
2207 }
2208 #endif
2209 }
2210 #endif
2211
2212 static void init_ide_data (byte hwif)
2213 {
2214 int drive;
2215
2216 for (drive = 0; drive < (MAX_DRIVES<<PARTN_BITS); drive++)
2217 ide_blksizes[hwif][drive] = 1024;
2218 blksize_size[ide_major[hwif]] = ide_blksizes[hwif];
2219
2220
2221 for (drive = 0; drive < MAX_DRIVES; drive++) {
2222 ide_dev_t *dev = &ide_dev[hwif][drive];
2223 dev->select.all = (drive<<4)|0xa0;
2224 dev->hwif = hwif;
2225 dev->unmask = 0;
2226 dev->busy = 0;
2227 dev->mult_count = 0;
2228 dev->mult_req = 0;
2229 dev->usage = 0;
2230 dev->vlb_32bit = 0;
2231 dev->vlb_sync = 0;
2232 dev->id = NULL;
2233 dev->ctl = 0x08;
2234 dev->wqueue = NULL;
2235 dev->special.all = 0;
2236 dev->special.b.recalibrate = 1;
2237 dev->special.b.set_geometry = 1;
2238 dev->keep_settings = 0;
2239 ide_hd[hwif][drive<<PARTN_BITS].start_sect = 0;
2240 dev->name = ide_devname[hwif][drive];
2241 }
2242 }
2243
2244
2245
2246
2247
2248
2249
2250 static byte setup_irq (byte hwif)
2251 {
2252 static byte rc = 0;
2253 unsigned long flags;
2254 const char *msg = "", *primary_secondary[] = {"primary", "secondary"};
2255 void (*handler)(int, struct pt_regs *) = HWIF ? &ide1_intr : &ide0_intr;
2256
2257 #if SUPPORT_SHARING_IRQ
2258 if (sharing_single_irq) {
2259 if (HWIF != 0 && !rc) {
2260 msg = " (shared with ide0)";
2261 goto done;
2262 }
2263 handler = &ide_shared_intr;
2264 }
2265 #if SUPPORT_SERIALIZE
2266 else if (single_threaded) {
2267 handler = &ide_seq_intr;
2268 if (HWIF != 0)
2269 msg = " (single-threaded with ide0)";
2270 }
2271 #endif
2272 #endif
2273 save_flags(flags);
2274 cli();
2275 if ((rc = request_irq(ide_irq[HWIF],handler,SA_INTERRUPT,ide_name[HWIF])))
2276 msg = ": FAILED! unable to allocate IRQ";
2277 restore_flags(flags);
2278 #if SUPPORT_SHARING_IRQ
2279 done:
2280 #endif
2281 printk("%s: %s interface on irq %d%s\n",
2282 ide_name[HWIF], primary_secondary[HWIF], ide_irq[HWIF], msg);
2283 return rc;
2284 }
2285
2286 static void ide_geninit(byte hwif)
2287 {
2288 static int drive;
2289
2290 for (drive = 0; drive < MAX_DRIVES; drive++) {
2291 ide_dev_t *dev = &ide_dev[HWIF][drive];
2292 if (dev->present) {
2293 ide_hd[HWIF][drive<<PARTN_BITS].nr_sects = ide_capacity[HWIF][drive];
2294
2295 if (dev->type == cdrom)
2296 ide_hd[HWIF][drive<<PARTN_BITS].start_sect = -1;
2297 }
2298 }
2299 }
2300
2301 static void ide0_geninit(void)
2302 {
2303 ide_geninit(0);
2304 }
2305
2306 static void ide1_geninit(void)
2307 {
2308 ide_geninit(1);
2309 }
2310
2311 static struct file_operations ide_fops = {
2312 NULL,
2313 block_read,
2314 block_write,
2315 NULL,
2316 NULL,
2317 ide_ioctl,
2318 NULL,
2319 ide_open,
2320 ide_release,
2321 block_fsync
2322 #ifdef CONFIG_BLK_DEV_IDECD
2323 ,NULL,
2324 ide_check_media_change,
2325 NULL
2326 #endif CONFIG_BLK_DEV_IDECD
2327 };
2328
2329
2330 #if SUPPORT_DTC2278
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342 static void sub22 (char b, char c)
2343 {
2344 int i;
2345
2346 for(i = 0; i < 3; i++) {
2347 __inb(0x3f6);
2348 outb_p(b,0xb0);
2349 __inb(0x3f6);
2350 outb_p(c,0xb4);
2351 __inb(0x3f6);
2352 if(__inb(0xb4) == c) {
2353 outb_p(7,0xb0);
2354 __inb(0x3f6);
2355 return;
2356 }
2357 }
2358 }
2359
2360 static void try_to_init_dtc2278 (void)
2361 {
2362
2363 cli();
2364 sub22(1,0xc3);
2365 sub22(0,0xa0);
2366 sti();
2367
2368
2369
2370 outb_p(4,0xb0);
2371 __inb(0x3f6);
2372 outb_p(0x20,0xb4);
2373 __inb(0x3f6);
2374 }
2375 #endif
2376
2377
2378
2379
2380 unsigned long ide_init (unsigned long mem_start, unsigned long mem_end)
2381 {
2382 byte hwif;
2383
2384 #if SUPPORT_DTC2278
2385 if (probe_dtc2278)
2386 try_to_init_dtc2278();
2387 #endif
2388
2389 for (hwif = 0; hwif < 2; hwif++) {
2390 init_ide_data (hwif);
2391 if (SUPPORT_TWO_INTERFACES || hwif == HWIF) {
2392 if (hwif == 0)
2393 #ifdef CONFIG_BLK_DEV_HD
2394 continue;
2395 #else
2396 probe_cmos_for_drives ();
2397 #endif
2398 probe_mem_start = (mem_start + 3uL) & ~3uL;
2399 probe_for_drives (hwif);
2400 mem_start = probe_mem_start;
2401 }
2402 }
2403
2404
2405 ide_gendisk[0].nr_real = ide_dev[0][0].present + ide_dev[0][1].present;
2406 ide_gendisk[1].nr_real = ide_dev[1][0].present + ide_dev[1][1].present;
2407 if (ide_gendisk[1].nr_real && (ide_irq[0] == ide_irq[1])) {
2408 if (!ide_gendisk[0].nr_real) {
2409 ide_irq[0] = 0;
2410 } else {
2411 #if SUPPORT_SHARING_IRQ
2412 sharing_single_irq = 1;
2413 single_threaded = 1;
2414 #else
2415 printk("%s: ide irq-sharing%s", ide_name[1], unsupported);
2416 return mem_start;
2417 #endif
2418 }
2419 }
2420 #ifdef CONFIG_BLK_DEV_HD
2421 #if SUPPORT_SHARING_IRQ
2422 if (ide_irq[1] == 14 || sharing_single_irq) {
2423 #else
2424 if (ide_irq[1] == 14) {
2425 #endif
2426 printk("%s: irq-sharing not possible with old harddisk driver (hd.c)\n", ide_name[1]);
2427 return mem_start;
2428 }
2429 #endif
2430
2431 for (hwif = 2; hwif-- > 0;) {
2432 if (ide_gendisk[hwif].nr_real != 0 && !setup_irq(hwif)) {
2433 const char *name = ide_name[HWIF];
2434 unsigned int major = ide_major[HWIF];
2435 if (register_blkdev(major, name, &ide_fops)) {
2436 printk("%s: unable to get major number %d\n", name, major);
2437 } else {
2438 timer_table[ide_timer[HWIF]].fn
2439 = HWIF ? ide1_timer_expiry : ide0_timer_expiry;
2440 #if SUPPORT_SHARING_IRQ
2441 if (single_threaded)
2442 blk_dev[major].request_fn = &do_shared_request;
2443 else
2444 #endif
2445 blk_dev[major].request_fn =
2446 HWIF ? &do_ide1_request : &do_ide0_request;
2447 read_ahead[major] = 8;
2448 ide_gendisk[HWIF].next = gendisk_head;
2449 gendisk_head = &ide_gendisk[HWIF];
2450 }
2451 }
2452 }
2453 return mem_start;
2454 }