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