patch-2_6_7-vs1_9_1_12
[linux-2.6.git] / drivers / ide / legacy / pdc4030.c
1 /*  -*- linux-c -*-
2  *  linux/drivers/ide/legacy/pdc4030.c          Version 0.90  May 27, 1999
3  *
4  *  Copyright (C) 1995-2002  Linus Torvalds & authors (see below)
5  */
6
7 /*
8  *  Principal Author/Maintainer:  Peter Denison <promise@pnd-pc.demon.co.uk>
9  *
10  *  This file provides support for the second port and cache of Promise
11  *  IDE interfaces, e.g. DC4030VL, DC4030VL-1 and DC4030VL-2.
12  *
13  *  Thanks are due to Mark Lord for advice and patiently answering stupid
14  *  questions, and all those mugs^H^H^H^Hbrave souls who've tested this,
15  *  especially Andre Hedrick.
16  *
17  *  Version 0.01        Initial version, #include'd in ide.c rather than
18  *                      compiled separately.
19  *                      Reads use Promise commands, writes as before. Drives
20  *                      on second channel are read-only.
21  *  Version 0.02        Writes working on second channel, reads on both
22  *                      channels. Writes fail under high load. Suspect
23  *                      transfers of >127 sectors don't work.
24  *  Version 0.03        Brought into line with ide.c version 5.27.
25  *                      Other minor changes.
26  *  Version 0.04        Updated for ide.c version 5.30
27  *                      Changed initialization strategy
28  *  Version 0.05        Kernel integration.  -ml
29  *  Version 0.06        Ooops. Add hwgroup to direct call of ide_intr() -ml
30  *  Version 0.07        Added support for DC4030 variants
31  *                      Secondary interface autodetection
32  *  Version 0.08        Renamed to pdc4030.c
33  *  Version 0.09        Obsolete - never released - did manual write request
34  *                      splitting before max_sectors[major][minor] available.
35  *  Version 0.10        Updated for 2.1 series of kernels
36  *  Version 0.11        Updated for 2.3 series of kernels
37  *                      Autodetection code added.
38  *
39  *  Version 0.90        Transition to BETA code. No lost/unexpected interrupts
40  */
41
42 /*
43  * Once you've compiled it in, you'll have to also enable the interface
44  * setup routine from the kernel command line, as in 
45  *
46  *      'linux ide0=dc4030' or 'linux ide1=dc4030'
47  *
48  * It should now work as a second controller also ('ide1=dc4030') but only
49  * if you DON'T have BIOS V4.44, which has a bug. If you have this version
50  * and EPROM programming facilities, you need to fix 4 bytes:
51  *      2496:   81      81
52  *      2497:   3E      3E
53  *      2498:   22      98      *
54  *      2499:   06      05      *
55  *      249A:   F0      F0
56  *      249B:   01      01
57  *      ...
58  *      24A7:   81      81
59  *      24A8:   3E      3E
60  *      24A9:   22      98      *
61  *      24AA:   06      05      *
62  *      24AB:   70      70
63  *      24AC:   01      01
64  *
65  * As of January 1999, Promise Technology Inc. have finally supplied me with
66  * some technical information which has shed a glimmer of light on some of the
67  * problems I was having, especially with writes. 
68  *
69  * There are still potential problems with the robustness and efficiency of
70  * this driver because I still don't understand what the card is doing with
71  * interrupts, however, it has been stable for a while with no reports of ill
72  * effects.
73  */
74
75 #define DEBUG_READ
76 #define DEBUG_WRITE
77 #define __PROMISE_4030
78
79 #include <linux/module.h>
80 #include <linux/config.h>
81 #include <linux/types.h>
82 #include <linux/kernel.h>
83 #include <linux/delay.h>
84 #include <linux/timer.h>
85 #include <linux/mm.h>
86 #include <linux/ioport.h>
87 #include <linux/blkdev.h>
88 #include <linux/hdreg.h>
89 #include <linux/ide.h>
90 #include <linux/init.h>
91
92 #include <asm/io.h>
93 #include <asm/irq.h>
94
95 #include "pdc4030.h"
96
97 static ide_startstop_t promise_rw_disk (ide_drive_t *drive, struct request *rq, sector_t block);
98
99 /*
100  * promise_selectproc() is invoked by ide.c
101  * in preparation for access to the specified drive.
102  */
103 static void promise_selectproc (ide_drive_t *drive)
104 {
105         unsigned int number;
106
107         number = (HWIF(drive)->channel << 1) + drive->select.b.unit;
108         HWIF(drive)->OUTB(number, IDE_FEATURE_REG);
109 }
110
111 /*
112  * pdc4030_cmd handles the set of vendor specific commands that are initiated
113  * by command F0. They all have the same success/failure notification -
114  * 'P' (=0x50) on success, 'p' (=0x70) on failure.
115  */
116 int pdc4030_cmd(ide_drive_t *drive, u8 cmd)
117 {
118         unsigned long timeout;
119         u8 status_val;
120
121         promise_selectproc(drive);      /* redundant? */
122         HWIF(drive)->OUTB(0xF3, IDE_SECTOR_REG);
123         HWIF(drive)->OUTB(cmd, IDE_SELECT_REG);
124         HWIF(drive)->OUTB(PROMISE_EXTENDED_COMMAND, IDE_COMMAND_REG);
125         timeout = HZ * 10;
126         timeout += jiffies;
127         do {
128                 if(time_after(jiffies, timeout)) {
129                         return 2; /* device timed out */
130                 }
131                 /* Delays at least 10ms to give interface a chance */
132                 mdelay(10);
133                 status_val = HWIF(drive)->INB(IDE_SECTOR_REG);
134         } while (status_val != 0x50 && status_val != 0x70);
135
136         if(status_val == 0x50)
137                 return 0; /* device returned success */
138         else
139                 return 1; /* device returned failure */
140 }
141
142 /*
143  * pdc4030_identify sends a vendor-specific IDENTIFY command to the drive
144  */
145 int pdc4030_identify(ide_drive_t *drive)
146 {
147         return pdc4030_cmd(drive, PROMISE_IDENTIFY);
148 }
149
150 /*
151  * setup_pdc4030()
152  * Completes the setup of a Promise DC4030 controller card, once found.
153  */
154 int __init setup_pdc4030(ide_hwif_t *hwif)
155 {
156         ide_drive_t *drive;
157         ide_hwif_t *hwif2;
158         struct dc_ident ident;
159         int i;
160         ide_startstop_t startstop;
161         
162         if (!hwif) return 0;
163
164         drive = &hwif->drives[0];
165         hwif2 = &ide_hwifs[hwif->index+1];
166         if (hwif->chipset == ide_pdc4030) /* we've already been found ! */
167                 return 1;
168
169         if (hwif->INB(IDE_NSECTOR_REG) == 0xFF ||
170             hwif->INB(IDE_SECTOR_REG) == 0xFF) {
171                 return 0;
172         }
173         if (IDE_CONTROL_REG)
174                 hwif->OUTB(0x08, IDE_CONTROL_REG);
175         if (pdc4030_cmd(drive,PROMISE_GET_CONFIG)) {
176                 return 0;
177         }
178         if (ide_wait_stat(&startstop, drive,DATA_READY,BAD_W_STAT,WAIT_DRQ)) {
179                 printk(KERN_INFO
180                         "%s: Failed Promise read config!\n",hwif->name);
181                 return 0;
182         }
183         hwif->ata_input_data(drive, &ident, SECTOR_WORDS);
184         if (ident.id[1] != 'P' || ident.id[0] != 'T') {
185                 return 0;
186         }
187         printk(KERN_INFO "%s: Promise caching controller, ",hwif->name);
188         switch(ident.type) {
189                 case 0x43:      printk("DC4030VL-2, "); break;
190                 case 0x41:      printk("DC4030VL-1, "); break;
191                 case 0x40:      printk("DC4030VL, "); break;
192                 default:
193                         printk("unknown - type 0x%02x - please report!\n"
194                                ,ident.type);
195                         printk("Please e-mail the following data to "
196                                "promise@pnd-pc.demon.co.uk along with\n"
197                                "a description of your card and drives:\n");
198                         for (i=0; i < 0x90; i++) {
199                                 printk("%02x ", ((unsigned char *)&ident)[i]);
200                                 if ((i & 0x0f) == 0x0f) printk("\n");
201                         }
202                         return 0;
203         }
204         printk("%dKB cache, ",(int)ident.cache_mem);
205         switch(ident.irq) {
206             case 0x00: hwif->irq = 14; break;
207             case 0x01: hwif->irq = 12; break;
208             default:   hwif->irq = 15; break;
209         }
210         printk("on IRQ %d\n",hwif->irq);
211
212         /*
213          * Once found and identified, we set up the next hwif in the array
214          * (hwif2 = ide_hwifs[hwif->index+1]) with the same io ports, irq
215          * and other settings as the main hwif. This gives us two "mated"
216          * hwifs pointing to the Promise card.
217          *
218          * We also have to shift the default values for the remaining
219          * interfaces "up by one" to make room for the second interface on the
220          * same set of values.
221          */
222
223         hwif->chipset   = hwif2->chipset = ide_pdc4030;
224         hwif->mate      = hwif2;
225         hwif2->mate     = hwif;
226         hwif2->channel  = 1;
227         hwif->rqsize    = hwif2->rqsize = 127;
228         hwif->no_lba48 = hwif2->no_lba48 = 1;
229         hwif->selectproc = hwif2->selectproc = &promise_selectproc;
230         hwif->serialized = hwif2->serialized = 1;
231         /* DC4030 hosted drives need their own identify... */
232         hwif->identify = hwif2->identify = &pdc4030_identify;
233
234         /* Override the normal ide disk read/write. */
235         hwif->rw_disk = promise_rw_disk;
236         hwif2->rw_disk = promise_rw_disk;
237
238         /* Shift the remaining interfaces up by one */
239         for (i=MAX_HWIFS-1 ; i > hwif->index+1 ; i--) {
240                 ide_hwif_t *h = &ide_hwifs[i];
241
242 #ifdef DEBUG
243                 printk(KERN_DEBUG "pdc4030: Shifting i/f %d values to i/f %d\n",i-1,i);
244 #endif /* DEBUG */
245                 ide_init_hwif_ports(&h->hw, (h-1)->io_ports[IDE_DATA_OFFSET], 0, NULL);
246                 memcpy(h->io_ports, h->hw.io_ports, sizeof(h->io_ports));
247                 h->noprobe = (h-1)->noprobe;
248         }
249         ide_init_hwif_ports(&hwif2->hw, hwif->io_ports[IDE_DATA_OFFSET], 0, NULL);
250         memcpy(hwif2->io_ports, hwif->hw.io_ports, sizeof(hwif2->io_ports));
251         hwif2->irq = hwif->irq;
252         hwif2->hw.irq = hwif->hw.irq = hwif->irq;
253         for (i=0; i<2 ; i++) {
254                 hwif->drives[i].io_32bit = 3;
255                 hwif2->drives[i].io_32bit = 3;
256                 hwif->drives[i].keep_settings = 1;
257                 hwif2->drives[i].keep_settings = 1;
258                 if (!ident.current_tm[i].cyl)
259                         hwif->drives[i].noprobe = 1;
260                 if (!ident.current_tm[i+2].cyl)
261                         hwif2->drives[i].noprobe = 1;
262         }
263
264         probe_hwif_init(&ide_hwifs[hwif->index]);
265         probe_hwif_init(&ide_hwifs[hwif2->index]);
266
267         return 1;
268 }
269
270 /*
271  * detect_pdc4030()
272  * Tests for the presence of a DC4030 Promise card on this interface
273  * Returns: 1 if found, 0 if not found
274  */
275 int __init detect_pdc4030(ide_hwif_t *hwif)
276 {
277         ide_drive_t *drive = &hwif->drives[0];
278
279         if (IDE_DATA_REG == 0) { /* Skip test for non-existent interface */
280                 return 0;
281         }
282         hwif->OUTB(0xF3, IDE_SECTOR_REG);
283         hwif->OUTB(0x14, IDE_SELECT_REG);
284         hwif->OUTB(PROMISE_EXTENDED_COMMAND, IDE_COMMAND_REG);
285
286         msleep(50);
287
288         if (hwif->INB(IDE_ERROR_REG) == 'P' &&
289             hwif->INB(IDE_NSECTOR_REG) == 'T' &&
290             hwif->INB(IDE_SECTOR_REG) == 'I') {
291                 return 1;
292         } else {
293                 return 0;
294         }
295 }
296
297 int __init pdc4030_init(void)
298 {
299         unsigned int    index;
300         ide_hwif_t      *hwif;
301
302         for (index = 0; index < MAX_HWIFS; index++) {
303                 hwif = &ide_hwifs[index];
304                 if (hwif->chipset == ide_unknown && detect_pdc4030(hwif)) {
305                         if (!setup_pdc4030(hwif))
306                                 return -ENODEV;
307                         return 0;
308                 }
309         }
310         return -ENODEV;
311 }
312
313 #ifdef MODULE
314 module_init(pdc4030_init);
315 #endif
316
317 MODULE_AUTHOR("Peter Denison");
318 MODULE_DESCRIPTION("Support of Promise 4030 VLB series IDE chipsets");
319 MODULE_LICENSE("GPL");
320
321 /*
322  * promise_read_intr() is the handler for disk read/multread interrupts
323  */
324 static ide_startstop_t promise_read_intr (ide_drive_t *drive)
325 {
326         unsigned int sectors_left, sectors_avail, nsect;
327         struct request *rq = HWGROUP(drive)->rq;
328         ata_status_t status;
329
330         status.all = HWIF(drive)->INB(IDE_STATUS_REG);
331         if (!OK_STAT(status.all, DATA_READY, BAD_R_STAT))
332                 return DRIVER(drive)->error(drive, __FUNCTION__, status.all);
333
334 read_again:
335         do {
336                 sectors_left = HWIF(drive)->INB(IDE_NSECTOR_REG);
337                 HWIF(drive)->INB(IDE_SECTOR_REG);
338         } while (HWIF(drive)->INB(IDE_NSECTOR_REG) != sectors_left);
339         sectors_avail = rq->nr_sectors - sectors_left;
340         if (!sectors_avail)
341                 goto read_again;
342
343 read_next:
344         nsect = rq->current_nr_sectors;
345         if (nsect > sectors_avail)
346                 nsect = sectors_avail;
347         sectors_avail -= nsect;
348
349 #ifdef DEBUG_READ
350         printk(KERN_DEBUG "%s: %s: sectors(%lu-%lu), rem=%lu\n",
351                           drive->name, __FUNCTION__,
352                           (unsigned long)rq->sector,
353                           (unsigned long)rq->sector + nsect - 1,
354                           (unsigned long)rq->nr_sectors - nsect);
355 #endif /* DEBUG_READ */
356
357 #ifdef CONFIG_IDE_TASKFILE_IO
358         task_sectors(drive, rq, nsect, IDE_PIO_IN);
359
360         /* FIXME: can we check status after transfer on pdc4030? */
361         /* Complete previously submitted bios. */
362         while (rq->bio != rq->cbio)
363                 if (!DRIVER(drive)->end_request(drive, 1, bio_sectors(rq->bio)))
364                         return ide_stopped;
365 #else /* CONFIG_IDE_TASKFILE_IO */
366         HWIF(drive)->ata_input_data(drive, rq->buffer, nsect * SECTOR_WORDS);
367         rq->buffer += nsect<<9;
368         rq->sector += nsect;
369         rq->errors = 0;
370         rq->nr_sectors -= nsect;
371         if (!rq->current_nr_sectors)
372                 DRIVER(drive)->end_request(drive, 1, 0);
373 #endif /* CONFIG_IDE_TASKFILE_IO */
374
375 /*
376  * Now the data has been read in, do the following:
377  * 
378  * if there are still sectors left in the request, 
379  *   if we know there are still sectors available from the interface,
380  *     go back and read the next bit of the request.
381  *   else if DRQ is asserted, there are more sectors available, so
382  *     go back and find out how many, then read them in.
383  *   else if BUSY is asserted, we are going to get an interrupt, so
384  *     set the handler for the interrupt and just return
385  */
386         if (rq->nr_sectors > 0) {
387                 if (sectors_avail)
388                         goto read_next;
389                 status.all = HWIF(drive)->INB(IDE_STATUS_REG);
390                 if (status.b.drq)
391                         goto read_again;
392                 if (status.b.bsy) {
393                         if (HWGROUP(drive)->handler != NULL)
394                                 BUG();
395                         ide_set_handler(drive,
396                                         &promise_read_intr,
397                                         WAIT_CMD,
398                                         NULL);
399 #ifdef DEBUG_READ
400                         printk(KERN_DEBUG "%s: promise_read: waiting for"
401                                "interrupt\n", drive->name);
402 #endif /* DEBUG_READ */
403                         return ide_started;
404                 }
405                 printk(KERN_ERR "%s: Eeek! promise_read_intr: sectors left "
406                        "!DRQ !BUSY\n", drive->name);
407                 return DRIVER(drive)->error(drive,
408                                 "promise read intr", status.all);
409         }
410         return ide_stopped;
411 }
412
413 /*
414  * promise_complete_pollfunc()
415  * This is the polling function for waiting (nicely!) until drive stops
416  * being busy. It is invoked at the end of a write, after the previous poll
417  * has finished.
418  *
419  * Once not busy, the end request is called.
420  */
421 static ide_startstop_t promise_complete_pollfunc(ide_drive_t *drive)
422 {
423         ide_hwgroup_t *hwgroup = HWGROUP(drive);
424 #ifdef CONFIG_IDE_TASKFILE_IO
425         struct request *rq = hwgroup->rq;
426 #else
427         struct request *rq = &hwgroup->wrq;
428         struct bio *bio = rq->bio;
429 #endif
430
431         if ((HWIF(drive)->INB(IDE_STATUS_REG)) & BUSY_STAT) {
432                 if (time_before(jiffies, hwgroup->poll_timeout)) {
433                         if (hwgroup->handler != NULL)
434                                 BUG();
435                         ide_set_handler(drive,
436                                         &promise_complete_pollfunc,
437                                         HZ/100,
438                                         NULL);
439                         return ide_started; /* continue polling... */
440                 }
441                 hwgroup->poll_timeout = 0;
442                 printk(KERN_ERR "%s: completion timeout - still busy!\n",
443                        drive->name);
444                 return DRIVER(drive)->error(drive, "busy timeout",
445                                 HWIF(drive)->INB(IDE_STATUS_REG));
446         }
447
448         hwgroup->poll_timeout = 0;
449 #ifdef DEBUG_WRITE
450         printk(KERN_DEBUG "%s: Write complete - end_request\n", drive->name);
451 #endif /* DEBUG_WRITE */
452
453 #ifdef CONFIG_IDE_TASKFILE_IO
454         /* Complete previously submitted bios. */
455         while (rq->bio != rq->cbio)
456                 (void) DRIVER(drive)->end_request(drive, 1, bio_sectors(rq->bio));
457 #else
458         bio->bi_idx = bio->bi_vcnt - rq->nr_cbio_segments;
459         rq = hwgroup->rq;
460         DRIVER(drive)->end_request(drive, 1, rq->hard_nr_sectors);
461 #endif
462         return ide_stopped;
463 }
464
465 /*
466  * promise_multwrite() transfers a block of up to mcount sectors of data
467  * to a drive as part of a disk multiple-sector write operation.
468  */
469 #ifdef CONFIG_IDE_TASKFILE_IO
470 static void promise_multwrite (ide_drive_t *drive, unsigned int msect)
471 {
472         struct request* rq = HWGROUP(drive)->rq;
473         unsigned int nsect;
474
475         rq->errors = 0;
476         do {
477                 nsect = rq->current_nr_sectors;
478                 if (nsect > msect)
479                         nsect = msect;
480
481                 task_sectors(drive, rq, nsect, IDE_PIO_OUT);
482
483                 if (!rq->nr_sectors)
484                         msect = 0;
485                 else
486                         msect -= nsect;
487         } while (msect);
488 }
489 #else /* CONFIG_IDE_TASKFILE_IO */
490 static void promise_multwrite (ide_drive_t *drive, unsigned int mcount)
491 {
492         ide_hwgroup_t *hwgroup  = HWGROUP(drive);
493         struct request *rq      = &hwgroup->wrq;
494
495         do {
496                 char *buffer;
497                 int nsect = rq->current_nr_sectors;
498
499                 if (nsect > mcount)
500                         nsect = mcount;
501                 mcount -= nsect;
502                 buffer = rq->buffer;
503
504                 rq->sector += nsect;
505                 rq->buffer += nsect << 9;
506                 rq->nr_sectors -= nsect;
507                 rq->current_nr_sectors -= nsect;
508
509                 /* Do we move to the next bh after this? */
510                 if (!rq->current_nr_sectors) {
511                         struct bio *bio = rq->bio;
512
513                         /*
514                          * only move to next bio, when we have processed
515                          * all bvecs in this one.
516                          */
517                         if (++bio->bi_idx >= bio->bi_vcnt) {
518                                 bio->bi_idx = bio->bi_vcnt - rq->nr_cbio_segments;
519                                 bio = bio->bi_next;
520                         }
521
522                         /* end early early we ran out of requests */
523                         if (!bio) {
524                                 mcount = 0;
525                         } else {
526                                 rq->bio = bio;
527                                 rq->nr_cbio_segments = bio_segments(bio);
528                                 rq->current_nr_sectors = bio_cur_sectors(bio);
529                                 rq->hard_cur_sectors = rq->current_nr_sectors;
530                         }
531                 }
532
533                 /*
534                  * Ok, we're all setup for the interrupt
535                  * re-entering us on the last transfer.
536                  */
537                 taskfile_output_data(drive, buffer, nsect<<7);
538         } while (mcount);
539 }
540 #endif
541
542 /*
543  * promise_write_pollfunc() is the handler for disk write completion polling.
544  */
545 static ide_startstop_t promise_write_pollfunc (ide_drive_t *drive)
546 {
547         ide_hwgroup_t *hwgroup = HWGROUP(drive);
548 #ifdef CONFIG_IDE_TASKFILE_IO
549         struct request *rq = hwgroup->rq;
550 #else
551         struct request *rq = &hwgroup->wrq;
552         struct bio *bio = rq->bio;
553 #endif
554
555         if (HWIF(drive)->INB(IDE_NSECTOR_REG) != 0) {
556                 if (time_before(jiffies, hwgroup->poll_timeout)) {
557                         if (hwgroup->handler != NULL)
558                                 BUG();
559                         ide_set_handler(drive,
560                                         &promise_write_pollfunc,
561                                         HZ/100,
562                                         NULL);
563                         return ide_started; /* continue polling... */
564                 }
565                 hwgroup->poll_timeout = 0;
566                 printk(KERN_ERR "%s: write timed-out!\n",drive->name);
567 #ifndef CONFIG_IDE_TASKFILE_IO
568                 bio->bi_idx = bio->bi_vcnt - rq->nr_cbio_segments;
569 #endif
570                 return DRIVER(drive)->error(drive, "write timeout",
571                                 HWIF(drive)->INB(IDE_STATUS_REG));
572         }
573
574 #ifdef CONFIG_IDE_TASKFILE_IO
575         /* Complete previously submitted bios. */
576         while (rq->bio != rq->cbio)
577                 (void) DRIVER(drive)->end_request(drive, 1, bio_sectors(rq->bio));
578 #endif
579
580         /*
581          * Now write out last 4 sectors and poll for not BUSY
582          */
583         promise_multwrite(drive, 4);
584         hwgroup->poll_timeout = jiffies + WAIT_WORSTCASE;
585         if (hwgroup->handler != NULL)
586                 BUG();
587         ide_set_handler(drive, &promise_complete_pollfunc, HZ/100, NULL);
588 #ifdef DEBUG_WRITE
589         printk(KERN_DEBUG "%s: Done last 4 sectors - status = %02x\n",
590                 drive->name, HWIF(drive)->INB(IDE_STATUS_REG));
591 #endif /* DEBUG_WRITE */
592         return ide_started;
593 }
594
595 /*
596  * promise_write() transfers a block of one or more sectors of data to a
597  * drive as part of a disk write operation. All but 4 sectors are transferred
598  * in the first attempt, then the interface is polled (nicely!) for completion
599  * before the final 4 sectors are transferred. There is no interrupt generated
600  * on writes (at least on the DC4030VL-2), we just have to poll for NOT BUSY.
601  */
602 static ide_startstop_t promise_write (ide_drive_t *drive)
603 {
604         ide_hwgroup_t *hwgroup = HWGROUP(drive);
605 #ifdef CONFIG_IDE_TASKFILE_IO
606         struct request *rq = hwgroup->rq;
607 #else
608         struct request *rq = &hwgroup->wrq;
609 #endif
610
611 #ifdef DEBUG_WRITE
612         printk(KERN_DEBUG "%s: %s: sectors(%lu-%lu)\n",
613                           drive->name, __FUNCTION__,
614                           (unsigned long)rq->sector,
615                           (unsigned long)rq->sector + rq->nr_sectors - 1);
616 #endif /* DEBUG_WRITE */
617
618         /*
619          * If there are more than 4 sectors to transfer, do n-4 then go into
620          * the polling strategy as defined above.
621          */
622         if (rq->nr_sectors > 4) {
623                 promise_multwrite(drive, rq->nr_sectors - 4);
624                 hwgroup->poll_timeout = jiffies + WAIT_WORSTCASE;
625                 if (hwgroup->handler != NULL)   /* paranoia check */
626                         BUG();
627                 ide_set_handler (drive, &promise_write_pollfunc, HZ/100, NULL);
628                 return ide_started;
629         } else {
630         /*
631          * There are 4 or fewer sectors to transfer, do them all in one go
632          * and wait for NOT BUSY.
633          */
634                 promise_multwrite(drive, rq->nr_sectors);
635                 hwgroup->poll_timeout = jiffies + WAIT_WORSTCASE;
636                 if (hwgroup->handler != NULL)
637                         BUG();
638                 ide_set_handler(drive,
639                                 &promise_complete_pollfunc,
640                                 HZ/100,
641                                 NULL);
642
643 #ifdef DEBUG_WRITE
644                 printk(KERN_DEBUG "%s: promise_write: <= 4 sectors, "
645                         "status = %02x\n", drive->name,
646                         HWIF(drive)->INB(IDE_STATUS_REG));
647 #endif /* DEBUG_WRITE */
648                 return ide_started;
649         }
650 }
651
652 /*
653  * do_pdc4030_io() is called from promise_rw_disk, having had the block number
654  * already set up. It issues a READ or WRITE command to the Promise
655  * controller, assuming LBA has been used to set up the block number.
656  */
657 #ifndef CONFIG_IDE_TASKFILE_IO
658 ide_startstop_t do_pdc4030_io (ide_drive_t *drive, struct request *rq)
659 {
660         ide_startstop_t startstop;
661         unsigned long timeout;
662         u8 stat = 0;
663 #else
664 static ide_startstop_t do_pdc4030_io (ide_drive_t *drive, ide_task_t *task)
665 {
666         struct request *rq      = HWGROUP(drive)->rq;
667         task_struct_t *taskfile = (task_struct_t *) task->tfRegister;
668         ide_startstop_t startstop;
669         unsigned long timeout;
670         u8 stat = 0;
671
672         if (IDE_CONTROL_REG)
673                 HWIF(drive)->OUTB(drive->ctl, IDE_CONTROL_REG); /* clear nIEN */
674         SELECT_MASK(drive, 0);
675         HWIF(drive)->OUTB(taskfile->feature, IDE_FEATURE_REG);
676         HWIF(drive)->OUTB(taskfile->sector_count, IDE_NSECTOR_REG);
677         /* refers to number of sectors to transfer */
678         HWIF(drive)->OUTB(taskfile->sector_number, IDE_SECTOR_REG);
679         /* refers to sector offset or start sector */
680         HWIF(drive)->OUTB(taskfile->low_cylinder, IDE_LCYL_REG);
681         HWIF(drive)->OUTB(taskfile->high_cylinder, IDE_HCYL_REG);
682         HWIF(drive)->OUTB(taskfile->device_head, IDE_SELECT_REG);
683         HWIF(drive)->OUTB(taskfile->command, IDE_COMMAND_REG);
684 #endif
685
686         if (rq_data_dir(rq) == READ) {
687 #ifndef CONFIG_IDE_TASKFILE_IO
688                 HWIF(drive)->OUTB(PROMISE_READ, IDE_COMMAND_REG);
689 #endif
690 /*
691  * The card's behaviour is odd at this point. If the data is
692  * available, DRQ will be true, and no interrupt will be
693  * generated by the card. If this is the case, we need to call the 
694  * "interrupt" handler (promise_read_intr) directly. Otherwise, if
695  * an interrupt is going to occur, bit0 of the SELECT register will
696  * be high, so we can set the handler the just return and be interrupted.
697  * If neither of these is the case, we wait for up to 50ms (badly I'm
698  * afraid!) until one of them is.
699  */
700                 timeout = jiffies + HZ/20; /* 50ms wait */
701                 do {
702                         stat = HWIF(drive)->INB(IDE_STATUS_REG);
703                         if (stat & DRQ_STAT) {
704                                 udelay(1);
705                                 return promise_read_intr(drive);
706                         }
707                         if (HWIF(drive)->INB(IDE_SELECT_REG) & 0x01) {
708 #ifdef DEBUG_READ
709                                 printk(KERN_DEBUG "%s: read: waiting for "
710                                                 "interrupt\n", drive->name);
711 #endif /* DEBUG_READ */
712                                 ide_set_handler(drive,
713                                                 &promise_read_intr,
714                                                 WAIT_CMD,
715                                                 NULL);
716                                 return ide_started;
717                         }
718                         udelay(1);
719                 } while (time_before(jiffies, timeout));
720
721                 printk(KERN_ERR "%s: reading: No DRQ and not "
722                                 "waiting - Odd!\n", drive->name);
723                 return ide_stopped;
724         } else {
725 #ifndef CONFIG_IDE_TASKFILE_IO
726                 HWIF(drive)->OUTB(PROMISE_WRITE, IDE_COMMAND_REG);
727 #endif
728                 if (ide_wait_stat(&startstop, drive, DATA_READY,
729                                 drive->bad_wstat, WAIT_DRQ)) {
730                         printk(KERN_ERR "%s: no DRQ after issuing "
731                                 "PROMISE_WRITE\n", drive->name);
732                         return startstop;
733                 }
734                 if (!drive->unmask)
735                         local_irq_disable();
736 #ifndef CONFIG_IDE_TASKFILE_IO
737                 HWGROUP(drive)->wrq = *rq; /* scratchpad */
738 #endif
739                 return promise_write(drive);
740         }
741 }
742
743 static ide_startstop_t promise_rw_disk (ide_drive_t *drive, struct request *rq, sector_t block)
744 {
745         /* The four drives on the two logical (one physical) interfaces
746            are distinguished by writing the drive number (0-3) to the
747            Feature register.
748            FIXME: Is promise_selectproc now redundant??
749         */
750         ide_hwif_t *hwif = HWIF(drive);
751         int drive_number = (hwif->channel << 1) + drive->select.b.unit;
752 #ifdef CONFIG_IDE_TASKFILE_IO
753         struct hd_drive_task_hdr taskfile;
754         ide_task_t args;
755 #endif
756
757         BUG_ON(rq->nr_sectors > 127);
758
759 #ifndef CONFIG_IDE_TASKFILE_IO
760         if (IDE_CONTROL_REG)
761                 hwif->OUTB(drive->ctl, IDE_CONTROL_REG);
762         hwif->OUTB(drive_number, IDE_FEATURE_REG);
763         hwif->OUTB(rq->nr_sectors, IDE_NSECTOR_REG);
764         hwif->OUTB(block,IDE_SECTOR_REG);
765         hwif->OUTB(block>>=8,IDE_LCYL_REG);
766         hwif->OUTB(block>>=8,IDE_HCYL_REG);
767         hwif->OUTB(((block>>8)&0x0f)|drive->select.all,IDE_SELECT_REG);
768
769         return do_pdc4030_io(drive, rq);
770 #else /* !CONFIG_IDE_TASKFILE_IO */
771         memset(&taskfile, 0, sizeof(struct hd_drive_task_hdr));
772
773         taskfile.feature        = drive_number;
774         taskfile.sector_count   = rq->nr_sectors;
775         taskfile.sector_number  = block;
776         taskfile.low_cylinder   = (block>>=8);
777         taskfile.high_cylinder  = (block>>=8);
778         taskfile.device_head    = ((block>>8)&0x0f)|drive->select.all;
779         taskfile.command        = (rq->cmd==READ)?PROMISE_READ:PROMISE_WRITE;
780
781         memcpy(args.tfRegister, &taskfile, sizeof(struct hd_drive_task_hdr));
782         memset(args.hobRegister, 0, sizeof(struct hd_drive_hob_hdr));
783         /*
784          * Setup the bits of args that we do need.
785          * Note that we don't use the generic interrupt handlers.
786          */
787         args.handler            = NULL;
788         args.rq                 = (struct request *) rq;
789         rq->special             = (ide_task_t *)&args;
790
791         return do_pdc4030_io(drive, &args);
792 #endif /* !CONFIG_IDE_TASKFILE_IO */
793 }