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