patch-2_6_7-vs1_9_1_12
[linux-2.6.git] / drivers / ide / ide-taskfile.c
1 /*
2  * linux/drivers/ide/ide-taskfile.c     Version 0.38    March 05, 2003
3  *
4  *  Copyright (C) 2000-2002     Michael Cornwell <cornwell@acm.org>
5  *  Copyright (C) 2000-2002     Andre Hedrick <andre@linux-ide.org>
6  *  Copyright (C) 2001-2002     Klaus Smolin
7  *                                      IBM Storage Technology Division
8  *  Copyright (C) 2003          Bartlomiej Zolnierkiewicz
9  *
10  *  The big the bad and the ugly.
11  *
12  *  Problems to be fixed because of BH interface or the lack therefore.
13  *
14  *  Fill me in stupid !!!
15  *
16  *  HOST:
17  *      General refers to the Controller and Driver "pair".
18  *  DATA HANDLER:
19  *      Under the context of Linux it generally refers to an interrupt handler.
20  *      However, it correctly describes the 'HOST'
21  *  DATA BLOCK:
22  *      The amount of data needed to be transfered as predefined in the
23  *      setup of the device.
24  *  STORAGE ATOMIC:
25  *      The 'DATA BLOCK' associated to the 'DATA HANDLER', and can be as
26  *      small as a single sector or as large as the entire command block
27  *      request.
28  */
29
30 #include <linux/config.h>
31 #include <linux/module.h>
32 #include <linux/types.h>
33 #include <linux/string.h>
34 #include <linux/kernel.h>
35 #include <linux/timer.h>
36 #include <linux/mm.h>
37 #include <linux/interrupt.h>
38 #include <linux/major.h>
39 #include <linux/errno.h>
40 #include <linux/genhd.h>
41 #include <linux/blkpg.h>
42 #include <linux/slab.h>
43 #include <linux/pci.h>
44 #include <linux/delay.h>
45 #include <linux/hdreg.h>
46 #include <linux/ide.h>
47
48 #include <asm/byteorder.h>
49 #include <asm/irq.h>
50 #include <asm/uaccess.h>
51 #include <asm/io.h>
52 #include <asm/bitops.h>
53
54 #define DEBUG_TASKFILE  0       /* unset when fixed */
55
56 #if DEBUG_TASKFILE
57 #define DTF(x...) printk(x)
58 #else
59 #define DTF(x...)
60 #endif
61
62 static void ata_bswap_data (void *buffer, int wcount)
63 {
64         u16 *p = buffer;
65
66         while (wcount--) {
67                 *p = *p << 8 | *p >> 8; p++;
68                 *p = *p << 8 | *p >> 8; p++;
69         }
70 }
71
72
73 void taskfile_input_data (ide_drive_t *drive, void *buffer, u32 wcount)
74 {
75         HWIF(drive)->ata_input_data(drive, buffer, wcount);
76         if (drive->bswap)
77                 ata_bswap_data(buffer, wcount);
78 }
79
80 EXPORT_SYMBOL(taskfile_input_data);
81
82 void taskfile_output_data (ide_drive_t *drive, void *buffer, u32 wcount)
83 {
84         if (drive->bswap) {
85                 ata_bswap_data(buffer, wcount);
86                 HWIF(drive)->ata_output_data(drive, buffer, wcount);
87                 ata_bswap_data(buffer, wcount);
88         } else {
89                 HWIF(drive)->ata_output_data(drive, buffer, wcount);
90         }
91 }
92
93 EXPORT_SYMBOL(taskfile_output_data);
94
95 int taskfile_lib_get_identify (ide_drive_t *drive, u8 *buf)
96 {
97         ide_task_t args;
98         memset(&args, 0, sizeof(ide_task_t));
99         args.tfRegister[IDE_NSECTOR_OFFSET]     = 0x01;
100         if (drive->media == ide_disk)
101                 args.tfRegister[IDE_COMMAND_OFFSET]     = WIN_IDENTIFY;
102         else
103                 args.tfRegister[IDE_COMMAND_OFFSET]     = WIN_PIDENTIFY;
104         args.command_type = IDE_DRIVE_TASK_IN;
105         args.handler      = &task_in_intr;
106         return ide_raw_taskfile(drive, &args, buf);
107 }
108
109 EXPORT_SYMBOL(taskfile_lib_get_identify);
110
111 #ifdef CONFIG_IDE_TASK_IOCTL_DEBUG
112 void debug_taskfile (ide_drive_t *drive, ide_task_t *args)
113 {
114         printk(KERN_INFO "%s: ", drive->name);
115 //      printk("TF.0=x%02x ", args->tfRegister[IDE_DATA_OFFSET]);
116         printk("TF.1=x%02x ", args->tfRegister[IDE_FEATURE_OFFSET]);
117         printk("TF.2=x%02x ", args->tfRegister[IDE_NSECTOR_OFFSET]);
118         printk("TF.3=x%02x ", args->tfRegister[IDE_SECTOR_OFFSET]);
119         printk("TF.4=x%02x ", args->tfRegister[IDE_LCYL_OFFSET]);
120         printk("TF.5=x%02x ", args->tfRegister[IDE_HCYL_OFFSET]);
121         printk("TF.6=x%02x ", args->tfRegister[IDE_SELECT_OFFSET]);
122         printk("TF.7=x%02x\n", args->tfRegister[IDE_COMMAND_OFFSET]);
123         printk(KERN_INFO "%s: ", drive->name);
124 //      printk("HTF.0=x%02x ", args->hobRegister[IDE_DATA_OFFSET]);
125         printk("HTF.1=x%02x ", args->hobRegister[IDE_FEATURE_OFFSET]);
126         printk("HTF.2=x%02x ", args->hobRegister[IDE_NSECTOR_OFFSET]);
127         printk("HTF.3=x%02x ", args->hobRegister[IDE_SECTOR_OFFSET]);
128         printk("HTF.4=x%02x ", args->hobRegister[IDE_LCYL_OFFSET]);
129         printk("HTF.5=x%02x ", args->hobRegister[IDE_HCYL_OFFSET]);
130         printk("HTF.6=x%02x ", args->hobRegister[IDE_SELECT_OFFSET]);
131         printk("HTF.7=x%02x\n", args->hobRegister[IDE_CONTROL_OFFSET_HOB]);
132 }
133 #endif /* CONFIG_IDE_TASK_IOCTL_DEBUG */
134
135 ide_startstop_t do_rw_taskfile (ide_drive_t *drive, ide_task_t *task)
136 {
137         ide_hwif_t *hwif        = HWIF(drive);
138         task_struct_t *taskfile = (task_struct_t *) task->tfRegister;
139         hob_struct_t *hobfile   = (hob_struct_t *) task->hobRegister;
140         u8 HIHI                 = (drive->addressing == 1) ? 0xE0 : 0xEF;
141
142 #ifdef CONFIG_IDE_TASK_IOCTL_DEBUG
143         void debug_taskfile(drive, task);
144 #endif /* CONFIG_IDE_TASK_IOCTL_DEBUG */
145
146         /* ALL Command Block Executions SHALL clear nIEN, unless otherwise */
147         if (IDE_CONTROL_REG) {
148                 /* clear nIEN */
149                 hwif->OUTB(drive->ctl, IDE_CONTROL_REG);
150         }
151         SELECT_MASK(drive, 0);
152
153         if (drive->addressing == 1) {
154                 hwif->OUTB(hobfile->feature, IDE_FEATURE_REG);
155                 hwif->OUTB(hobfile->sector_count, IDE_NSECTOR_REG);
156                 hwif->OUTB(hobfile->sector_number, IDE_SECTOR_REG);
157                 hwif->OUTB(hobfile->low_cylinder, IDE_LCYL_REG);
158                 hwif->OUTB(hobfile->high_cylinder, IDE_HCYL_REG);
159         }
160
161         hwif->OUTB(taskfile->feature, IDE_FEATURE_REG);
162         hwif->OUTB(taskfile->sector_count, IDE_NSECTOR_REG);
163         hwif->OUTB(taskfile->sector_number, IDE_SECTOR_REG);
164         hwif->OUTB(taskfile->low_cylinder, IDE_LCYL_REG);
165         hwif->OUTB(taskfile->high_cylinder, IDE_HCYL_REG);
166
167         hwif->OUTB((taskfile->device_head & HIHI) | drive->select.all, IDE_SELECT_REG);
168 #ifdef CONFIG_IDE_TASKFILE_IO
169         if (task->handler != NULL) {
170                 if (task->prehandler != NULL) {
171                         hwif->OUTBSYNC(drive, taskfile->command, IDE_COMMAND_REG);
172                         ndelay(400);    /* FIXME */
173                         return task->prehandler(drive, task->rq);
174                 }
175                 ide_execute_command(drive, taskfile->command, task->handler, WAIT_WORSTCASE, NULL);
176                 return ide_started;
177         }
178 #else
179         if (task->handler != NULL) {
180                 ide_execute_command(drive, taskfile->command, task->handler, WAIT_WORSTCASE, NULL);
181                 if (task->prehandler != NULL)
182                         return task->prehandler(drive, task->rq);
183                 return ide_started;
184         }
185 #endif
186
187         if (!drive->using_dma)
188                 return ide_stopped;
189
190         switch (taskfile->command) {
191                 case WIN_WRITEDMA_ONCE:
192                 case WIN_WRITEDMA:
193                 case WIN_WRITEDMA_EXT:
194                         if (!hwif->ide_dma_write(drive))
195                                 return ide_started;
196                         break;
197                 case WIN_READDMA_ONCE:
198                 case WIN_READDMA:
199                 case WIN_READDMA_EXT:
200                 case WIN_IDENTIFY_DMA:
201                         if (!hwif->ide_dma_read(drive))
202                                 return ide_started;
203                         break;
204                 default:
205                         if (task->handler == NULL)
206                                 return ide_stopped;
207         }
208
209         return ide_stopped;
210 }
211
212 EXPORT_SYMBOL(do_rw_taskfile);
213
214 /*
215  * set_multmode_intr() is invoked on completion of a WIN_SETMULT cmd.
216  */
217 ide_startstop_t set_multmode_intr (ide_drive_t *drive)
218 {
219         ide_hwif_t *hwif = HWIF(drive);
220         u8 stat;
221
222         if (OK_STAT(stat = hwif->INB(IDE_STATUS_REG),READY_STAT,BAD_STAT)) {
223                 drive->mult_count = drive->mult_req;
224         } else {
225                 drive->mult_req = drive->mult_count = 0;
226                 drive->special.b.recalibrate = 1;
227                 (void) ide_dump_status(drive, "set_multmode", stat);
228         }
229         return ide_stopped;
230 }
231
232 EXPORT_SYMBOL(set_multmode_intr);
233
234 /*
235  * set_geometry_intr() is invoked on completion of a WIN_SPECIFY cmd.
236  */
237 ide_startstop_t set_geometry_intr (ide_drive_t *drive)
238 {
239         ide_hwif_t *hwif = HWIF(drive);
240         int retries = 5;
241         u8 stat;
242
243         while (((stat = hwif->INB(IDE_STATUS_REG)) & BUSY_STAT) && retries--)
244                 udelay(10);
245
246         if (OK_STAT(stat, READY_STAT, BAD_STAT))
247                 return ide_stopped;
248
249         if (stat & (ERR_STAT|DRQ_STAT))
250                 return DRIVER(drive)->error(drive, "set_geometry_intr", stat);
251
252         if (HWGROUP(drive)->handler != NULL)
253                 BUG();
254         ide_set_handler(drive, &set_geometry_intr, WAIT_WORSTCASE, NULL);
255         return ide_started;
256 }
257
258 EXPORT_SYMBOL(set_geometry_intr);
259
260 /*
261  * recal_intr() is invoked on completion of a WIN_RESTORE (recalibrate) cmd.
262  */
263 ide_startstop_t recal_intr (ide_drive_t *drive)
264 {
265         ide_hwif_t *hwif = HWIF(drive);
266         u8 stat;
267
268         if (!OK_STAT(stat = hwif->INB(IDE_STATUS_REG), READY_STAT, BAD_STAT))
269                 return DRIVER(drive)->error(drive, "recal_intr", stat);
270         return ide_stopped;
271 }
272
273 EXPORT_SYMBOL(recal_intr);
274
275 /*
276  * Handler for commands without a data phase
277  */
278 ide_startstop_t task_no_data_intr (ide_drive_t *drive)
279 {
280         ide_task_t *args        = HWGROUP(drive)->rq->special;
281         ide_hwif_t *hwif        = HWIF(drive);
282         u8 stat;
283
284         local_irq_enable();
285         if (!OK_STAT(stat = hwif->INB(IDE_STATUS_REG),READY_STAT,BAD_STAT)) {
286                 DTF("%s: command opcode 0x%02x\n", drive->name,
287                         args->tfRegister[IDE_COMMAND_OFFSET]);
288                 return DRIVER(drive)->error(drive, "task_no_data_intr", stat);
289                 /* calls ide_end_drive_cmd */
290         }
291         if (args)
292                 ide_end_drive_cmd(drive, stat, hwif->INB(IDE_ERROR_REG));
293
294         return ide_stopped;
295 }
296
297 EXPORT_SYMBOL(task_no_data_intr);
298
299 /*
300  * old taskfile PIO handlers, to be killed as soon as possible.
301  */
302 #ifndef CONFIG_IDE_TASKFILE_IO
303
304 /*
305  * Handler for command with PIO data-in phase, READ
306  */
307 ide_startstop_t task_in_intr (ide_drive_t *drive)
308 {
309         struct request *rq      = HWGROUP(drive)->rq;
310         ide_hwif_t *hwif        = HWIF(drive);
311         char *pBuf              = NULL;
312         u8 stat;
313
314         if (!OK_STAT(stat = hwif->INB(IDE_STATUS_REG),DATA_READY,BAD_R_STAT)) {
315                 if (stat & (ERR_STAT|DRQ_STAT)) {
316                         return DRIVER(drive)->error(drive, "task_in_intr", stat);
317                 }
318                 if (!(stat & BUSY_STAT)) {
319                         DTF("task_in_intr to Soon wait for next interrupt\n");
320                         if (HWGROUP(drive)->handler == NULL)
321                                 ide_set_handler(drive, &task_in_intr, WAIT_WORSTCASE, NULL);
322                         return ide_started;  
323                 }
324         }
325
326         pBuf = rq->buffer + task_rq_offset(rq);
327         DTF("Read: %p, rq->current_nr_sectors: %d, stat: %02x\n",
328                 pBuf, (int) rq->current_nr_sectors, stat);
329         taskfile_input_data(drive, pBuf, SECTOR_WORDS);
330
331         /* FIXME: check drive status */
332         if (--rq->current_nr_sectors <= 0)
333                 if (!DRIVER(drive)->end_request(drive, 1, 0))
334                         return ide_stopped;
335         /*
336          * ERM, it is techincally legal to leave/exit here but it makes
337          * a mess of the code ...
338          */
339         if (HWGROUP(drive)->handler == NULL)
340                 ide_set_handler(drive, &task_in_intr, WAIT_WORSTCASE, NULL);
341         return ide_started;
342 }
343
344 EXPORT_SYMBOL(task_in_intr);
345
346 /*
347  * Handler for command with Read Multiple
348  */
349 ide_startstop_t task_mulin_intr (ide_drive_t *drive)
350 {
351         ide_hwif_t *hwif        = HWIF(drive);
352         struct request *rq      = HWGROUP(drive)->rq;
353         char *pBuf              = NULL;
354         unsigned int msect      = drive->mult_count;
355         unsigned int nsect;
356         u8 stat;
357
358         if (!OK_STAT(stat = hwif->INB(IDE_STATUS_REG),DATA_READY,BAD_R_STAT)) {
359                 if (stat & (ERR_STAT|DRQ_STAT)) {
360                         return DRIVER(drive)->error(drive, "task_mulin_intr", stat);
361                 }
362                 /* no data yet, so wait for another interrupt */
363                 if (HWGROUP(drive)->handler == NULL)
364                         ide_set_handler(drive, &task_mulin_intr, WAIT_WORSTCASE, NULL);
365                 return ide_started;
366         }
367
368         do {
369                 nsect = rq->current_nr_sectors;
370                 if (nsect > msect)
371                         nsect = msect;
372                 pBuf = rq->buffer + task_rq_offset(rq);
373                 DTF("Multiread: %p, nsect: %d, msect: %d, " \
374                         " rq->current_nr_sectors: %d\n",
375                         pBuf, nsect, msect, rq->current_nr_sectors);
376                 taskfile_input_data(drive, pBuf, nsect * SECTOR_WORDS);
377                 rq->errors = 0;
378                 rq->current_nr_sectors -= nsect;
379                 msect -= nsect;
380
381                 /* FIXME: check drive status */
382                 if (!rq->current_nr_sectors) {
383                         if (!DRIVER(drive)->end_request(drive, 1, 0))
384                                 return ide_stopped;
385                 }
386         } while (msect);
387         if (HWGROUP(drive)->handler == NULL)
388                 ide_set_handler(drive, &task_mulin_intr, WAIT_WORSTCASE, NULL);
389         return ide_started;
390 }
391
392 EXPORT_SYMBOL(task_mulin_intr);
393
394 /*
395  * VERIFY ME before 2.4 ... unexpected race is possible based on details
396  * RMK with 74LS245/373/374 TTL buffer logic because of passthrough.
397  */
398 ide_startstop_t pre_task_out_intr (ide_drive_t *drive, struct request *rq)
399 {
400         ide_startstop_t startstop;
401
402         if (ide_wait_stat(&startstop, drive, DATA_READY,
403                         drive->bad_wstat, WAIT_DRQ)) {
404                 printk(KERN_ERR "%s: no DRQ after issuing WRITE%s\n",
405                         drive->name,
406                         drive->addressing ? "_EXT" : "");
407                 return startstop;
408         }
409         /* For Write_sectors we need to stuff the first sector */
410         taskfile_output_data(drive, rq->buffer + task_rq_offset(rq), SECTOR_WORDS);
411         rq->current_nr_sectors--;
412         return ide_started;
413 }
414
415 EXPORT_SYMBOL(pre_task_out_intr);
416
417 /*
418  * Handler for command with PIO data-out phase WRITE
419  *
420  * WOOHOO this is a CORRECT STATE DIAGRAM NOW, <andre@linux-ide.org>
421  */
422 ide_startstop_t task_out_intr (ide_drive_t *drive)
423 {
424         ide_hwif_t *hwif        = HWIF(drive);
425         struct request *rq      = HWGROUP(drive)->rq;
426         char *pBuf              = NULL;
427         u8 stat;
428
429         if (!OK_STAT(stat = hwif->INB(IDE_STATUS_REG), DRIVE_READY, drive->bad_wstat)) {
430                 return DRIVER(drive)->error(drive, "task_out_intr", stat);
431         }
432         /*
433          * Safe to update request for partial completions.
434          * We have a good STATUS CHECK!!!
435          */
436         if (!rq->current_nr_sectors)
437                 if (!DRIVER(drive)->end_request(drive, 1, 0))
438                         return ide_stopped;
439         if ((rq->current_nr_sectors==1) ^ (stat & DRQ_STAT)) {
440                 rq = HWGROUP(drive)->rq;
441                 pBuf = rq->buffer + task_rq_offset(rq);
442                 DTF("write: %p, rq->current_nr_sectors: %d\n",
443                         pBuf, (int) rq->current_nr_sectors);
444                 taskfile_output_data(drive, pBuf, SECTOR_WORDS);
445                 rq->errors = 0;
446                 rq->current_nr_sectors--;
447         }
448         if (HWGROUP(drive)->handler == NULL)
449                 ide_set_handler(drive, &task_out_intr, WAIT_WORSTCASE, NULL);
450         return ide_started;
451 }
452
453 EXPORT_SYMBOL(task_out_intr);
454
455 ide_startstop_t pre_task_mulout_intr (ide_drive_t *drive, struct request *rq)
456 {
457         ide_task_t *args = rq->special;
458         ide_startstop_t startstop;
459
460         if (ide_wait_stat(&startstop, drive, DATA_READY,
461                         drive->bad_wstat, WAIT_DRQ)) {
462                 printk(KERN_ERR "%s: no DRQ after issuing %s\n",
463                         drive->name,
464                         drive->addressing ? "MULTWRITE_EXT" : "MULTWRITE");
465                 return startstop;
466         }
467         if (!(drive_is_ready(drive))) {
468                 int i;
469                 for (i=0; i<100; i++) {
470                         if (drive_is_ready(drive))
471                                 break;
472                 }
473         }
474
475         /*
476          * WARNING :: if the drive as not acked good status we may not
477          * move the DATA-TRANSFER T-Bar as BSY != 0. <andre@linux-ide.org>
478          */
479         return args->handler(drive);
480 }
481
482 EXPORT_SYMBOL(pre_task_mulout_intr);
483
484 /*
485  * Handler for command write multiple
486  * Called directly from execute_drive_cmd for the first bunch of sectors,
487  * afterwards only by the ISR
488  */
489 ide_startstop_t task_mulout_intr (ide_drive_t *drive)
490 {
491         ide_hwif_t *hwif                = HWIF(drive);
492         u8 stat                         = hwif->INB(IDE_STATUS_REG);
493         struct request *rq              = HWGROUP(drive)->rq;
494         char *pBuf                      = NULL;
495         unsigned int msect              = drive->mult_count;
496         unsigned int nsect;
497
498         if (!OK_STAT(stat, DATA_READY, BAD_R_STAT) || !rq->current_nr_sectors) {
499                 if (stat & (ERR_STAT|DRQ_STAT)) {
500                         return DRIVER(drive)->error(drive, "task_mulout_intr", stat);
501                 }
502                 /* Handle last IRQ, occurs after all data was sent. */
503                 if (!rq->current_nr_sectors) {
504                         DRIVER(drive)->end_request(drive, 1, 0);
505                         return ide_stopped;
506                 }
507                 /* no data yet, so wait for another interrupt */
508                 if (HWGROUP(drive)->handler == NULL)
509                         ide_set_handler(drive, &task_mulout_intr, WAIT_WORSTCASE, NULL);
510                 return ide_started;
511         }
512
513         if (HWGROUP(drive)->handler != NULL) {
514                 unsigned long lflags;
515                 spin_lock_irqsave(&ide_lock, lflags);
516                 HWGROUP(drive)->handler = NULL;
517                 del_timer(&HWGROUP(drive)->timer);
518                 spin_unlock_irqrestore(&ide_lock, lflags);
519         }
520
521         do {
522                 nsect = rq->current_nr_sectors;
523                 if (nsect > msect)
524                         nsect = msect;
525                 pBuf = rq->buffer + task_rq_offset(rq);
526                 DTF("Multiwrite: %p, nsect: %d, msect: %d, " \
527                         "rq->current_nr_sectors: %ld\n",
528                         pBuf, nsect, msect, rq->current_nr_sectors);
529                 msect -= nsect;
530                 taskfile_output_data(drive, pBuf, nsect * SECTOR_WORDS);
531                 rq->current_nr_sectors -= nsect;
532
533                 /* FIXME: check drive status */
534                 if (!rq->current_nr_sectors) {
535                         if (!DRIVER(drive)->end_request(drive, 1, 0))
536                                 if (!rq->bio)
537                                         return ide_stopped;
538                 }
539         } while (msect);
540         rq->errors = 0;
541         if (HWGROUP(drive)->handler == NULL)
542                 ide_set_handler(drive, &task_mulout_intr, WAIT_WORSTCASE, NULL);
543         return ide_started;
544 }
545
546 EXPORT_SYMBOL(task_mulout_intr);
547
548 #else /* !CONFIG_IDE_TASKFILE_IO */
549
550 static u8 wait_drive_not_busy(ide_drive_t *drive)
551 {
552         ide_hwif_t *hwif = HWIF(drive);
553         int retries = 100;
554         u8 stat;
555
556         /*
557          * Last sector was transfered, wait until drive is ready.
558          * This can take up to 10 usec, but we will wait max 1 ms
559          * (drive_cmd_intr() waits that long).
560          */
561         while (((stat = hwif->INB(IDE_STATUS_REG)) & BUSY_STAT) && retries--)
562                 udelay(10);
563
564         if (!retries)
565                 printk(KERN_ERR "%s: drive still BUSY!\n", drive->name);
566
567         return stat;
568 }
569
570 /*
571  * Handler for command with PIO data-in phase (Read).
572  */
573 ide_startstop_t task_in_intr (ide_drive_t *drive)
574 {
575         struct request *rq = HWGROUP(drive)->rq;
576         u8 stat, good_stat;
577
578         good_stat = DATA_READY;
579         stat = HWIF(drive)->INB(IDE_STATUS_REG);
580 check_status:
581         if (!OK_STAT(stat, good_stat, BAD_R_STAT)) {
582                 if (stat & (ERR_STAT | DRQ_STAT))
583                         return DRIVER(drive)->error(drive, __FUNCTION__, stat);
584                 /* BUSY_STAT: No data yet, so wait for another IRQ. */
585                 ide_set_handler(drive, &task_in_intr, WAIT_WORSTCASE, NULL);
586                 return ide_started;
587         }
588
589         /*
590          * Complete previously submitted bios (if any).
591          * Status was already verifyied.
592          */
593         while (rq->bio != rq->cbio)
594                 if (!DRIVER(drive)->end_request(drive, 1, bio_sectors(rq->bio)))
595                         return ide_stopped;
596         /* Complete rq->buffer based request (ioctls). */
597         if (!rq->bio && !rq->nr_sectors) {
598                 ide_end_drive_cmd(drive, stat, HWIF(drive)->INB(IDE_ERROR_REG));
599                 return ide_stopped;
600         }
601
602         rq->errors = 0;
603         task_sectors(drive, rq, 1, IDE_PIO_IN);
604
605         /* If it was the last datablock check status and finish transfer. */
606         if (!rq->nr_sectors) {
607                 good_stat = 0;
608                 stat = wait_drive_not_busy(drive);
609                 goto check_status;
610         }
611
612         /* Still data left to transfer. */
613         ide_set_handler(drive, &task_in_intr, WAIT_WORSTCASE, NULL);
614
615         return ide_started;
616 }
617 EXPORT_SYMBOL(task_in_intr);
618
619 /*
620  * Handler for command with PIO data-in phase (Read Multiple).
621  */
622 ide_startstop_t task_mulin_intr (ide_drive_t *drive)
623 {
624         struct request *rq = HWGROUP(drive)->rq;
625         unsigned int msect = drive->mult_count;
626         unsigned int nsect;
627         u8 stat, good_stat;
628
629         good_stat = DATA_READY;
630         stat = HWIF(drive)->INB(IDE_STATUS_REG);
631 check_status:
632         if (!OK_STAT(stat, good_stat, BAD_R_STAT)) {
633                 if (stat & (ERR_STAT | DRQ_STAT))
634                         return DRIVER(drive)->error(drive, __FUNCTION__, stat);
635                 /* BUSY_STAT: No data yet, so wait for another IRQ. */
636                 ide_set_handler(drive, &task_mulin_intr, WAIT_WORSTCASE, NULL);
637                 return ide_started;
638         }
639
640         /*
641          * Complete previously submitted bios (if any).
642          * Status was already verifyied.
643          */
644         while (rq->bio != rq->cbio)
645                 if (!DRIVER(drive)->end_request(drive, 1, bio_sectors(rq->bio)))
646                         return ide_stopped;
647         /* Complete rq->buffer based request (ioctls). */
648         if (!rq->bio && !rq->nr_sectors) {
649                 ide_end_drive_cmd(drive, stat, HWIF(drive)->INB(IDE_ERROR_REG));
650                 return ide_stopped;
651         }
652
653         rq->errors = 0;
654         do {
655                 nsect = rq->current_nr_sectors;
656                 if (nsect > msect)
657                         nsect = msect;
658
659                 task_sectors(drive, rq, nsect, IDE_PIO_IN);
660
661                 if (!rq->nr_sectors)
662                         msect = 0;
663                 else
664                         msect -= nsect;
665         } while (msect);
666
667         /* If it was the last datablock check status and finish transfer. */
668         if (!rq->nr_sectors) {
669                 good_stat = 0;
670                 stat = wait_drive_not_busy(drive);
671                 goto check_status;
672         }
673
674         /* Still data left to transfer. */
675         ide_set_handler(drive, &task_mulin_intr, WAIT_WORSTCASE, NULL);
676
677         return ide_started;
678 }
679 EXPORT_SYMBOL(task_mulin_intr);
680
681 /*
682  * Handler for command with PIO data-out phase (Write).
683  */
684 ide_startstop_t task_out_intr (ide_drive_t *drive)
685 {
686         struct request *rq = HWGROUP(drive)->rq;
687         u8 stat;
688
689         stat = HWIF(drive)->INB(IDE_STATUS_REG);
690         if (!OK_STAT(stat, DRIVE_READY, drive->bad_wstat)) {
691                 if ((stat & (ERR_STAT | DRQ_STAT)) ||
692                     ((stat & WRERR_STAT) && !drive->nowerr))
693                         return DRIVER(drive)->error(drive, __FUNCTION__, stat);
694                 if (stat & BUSY_STAT) {
695                         /* Not ready yet, so wait for another IRQ. */
696                         ide_set_handler(drive, &task_out_intr, WAIT_WORSTCASE, NULL);
697                         return ide_started;
698                 }
699         }
700
701         /* Deal with unexpected ATA data phase. */
702         if ((!(stat & DATA_READY) && rq->nr_sectors) ||
703             ((stat & DATA_READY) && !rq->nr_sectors))
704                 return DRIVER(drive)->error(drive, __FUNCTION__, stat);
705
706         /* 
707          * Complete previously submitted bios (if any).
708          * Status was already verifyied.
709          */
710         while (rq->bio != rq->cbio)
711                 if (!DRIVER(drive)->end_request(drive, 1, bio_sectors(rq->bio)))
712                         return ide_stopped;
713         /* Complete rq->buffer based request (ioctls). */
714         if (!rq->bio && !rq->nr_sectors) {
715                 ide_end_drive_cmd(drive, stat, HWIF(drive)->INB(IDE_ERROR_REG));
716                 return ide_stopped;
717         }
718
719         /* Still data left to transfer. */
720         ide_set_handler(drive, &task_out_intr, WAIT_WORSTCASE, NULL);
721
722         rq->errors = 0;
723         task_sectors(drive, rq, 1, IDE_PIO_OUT);
724
725         return ide_started;
726 }
727
728 EXPORT_SYMBOL(task_out_intr);
729
730 ide_startstop_t pre_task_out_intr (ide_drive_t *drive, struct request *rq)
731 {
732         ide_startstop_t startstop;
733
734         if (ide_wait_stat(&startstop, drive, DATA_READY,
735                           drive->bad_wstat, WAIT_DRQ)) {
736                 printk(KERN_ERR "%s: no DRQ after issuing WRITE%s\n",
737                                 drive->name, drive->addressing ? "_EXT" : "");
738                 return startstop;
739         }
740
741         if (!drive->unmask)
742                 local_irq_disable();
743
744         return task_out_intr(drive);
745 }
746 EXPORT_SYMBOL(pre_task_out_intr);
747
748 /*
749  * Handler for command with PIO data-out phase (Write Multiple).
750  */
751 ide_startstop_t task_mulout_intr (ide_drive_t *drive)
752 {
753         struct request *rq = HWGROUP(drive)->rq;
754         unsigned int msect = drive->mult_count;
755         unsigned int nsect;
756         u8 stat;
757
758         stat = HWIF(drive)->INB(IDE_STATUS_REG);
759         if (!OK_STAT(stat, DRIVE_READY, drive->bad_wstat)) {
760                 if ((stat & (ERR_STAT | DRQ_STAT)) ||
761                     ((stat & WRERR_STAT) && !drive->nowerr))
762                         return DRIVER(drive)->error(drive, __FUNCTION__, stat);
763                 if (stat & BUSY_STAT) {
764                         /* Not ready yet, so wait for another IRQ. */
765                         ide_set_handler(drive, &task_mulout_intr, WAIT_WORSTCASE, NULL);
766                         return ide_started;
767                 }
768         }
769
770         /* Deal with unexpected ATA data phase. */
771         if ((!(stat & DATA_READY) && rq->nr_sectors) ||
772             ((stat & DATA_READY) && !rq->nr_sectors))
773                 return DRIVER(drive)->error(drive, __FUNCTION__, stat);
774
775         /* 
776          * Complete previously submitted bios (if any).
777          * Status was already verifyied.
778          */
779         while (rq->bio != rq->cbio)
780                 if (!DRIVER(drive)->end_request(drive, 1, bio_sectors(rq->bio)))
781                         return ide_stopped;
782         /* Complete rq->buffer based request (ioctls). */
783         if (!rq->bio && !rq->nr_sectors) {
784                 ide_end_drive_cmd(drive, stat, HWIF(drive)->INB(IDE_ERROR_REG));
785                 return ide_stopped;
786         }
787
788         /* Still data left to transfer. */
789         ide_set_handler(drive, &task_mulout_intr, WAIT_WORSTCASE, NULL);
790
791         rq->errors = 0;
792         do {
793                 nsect = rq->current_nr_sectors;
794                 if (nsect > msect)
795                         nsect = msect;
796
797                 task_sectors(drive, rq, nsect, IDE_PIO_OUT);
798
799                 if (!rq->nr_sectors)
800                         msect = 0;
801                 else
802                         msect -= nsect;
803         } while (msect);
804
805         return ide_started;
806 }
807 EXPORT_SYMBOL(task_mulout_intr);
808
809 ide_startstop_t pre_task_mulout_intr (ide_drive_t *drive, struct request *rq)
810 {
811         ide_startstop_t startstop;
812
813         if (ide_wait_stat(&startstop, drive, DATA_READY,
814                           drive->bad_wstat, WAIT_DRQ)) {
815                 printk(KERN_ERR "%s: no DRQ after issuing MULTWRITE%s\n",
816                                 drive->name, drive->addressing ? "_EXT" : "");
817                 return startstop;
818         }
819
820         if (!drive->unmask)
821                 local_irq_disable();
822
823         return task_mulout_intr(drive);
824 }
825 EXPORT_SYMBOL(pre_task_mulout_intr);
826
827 #endif /* !CONFIG_IDE_TASKFILE_IO */
828
829 int ide_diag_taskfile (ide_drive_t *drive, ide_task_t *args, unsigned long data_size, u8 *buf)
830 {
831         struct request rq;
832
833         memset(&rq, 0, sizeof(rq));
834         rq.flags = REQ_DRIVE_TASKFILE;
835         rq.buffer = buf;
836
837         /*
838          * (ks) We transfer currently only whole sectors.
839          * This is suffient for now.  But, it would be great,
840          * if we would find a solution to transfer any size.
841          * To support special commands like READ LONG.
842          */
843         if (args->command_type != IDE_DRIVE_TASK_NO_DATA) {
844                 if (data_size == 0)
845                         rq.nr_sectors = (args->hobRegister[IDE_NSECTOR_OFFSET] << 8) | args->tfRegister[IDE_NSECTOR_OFFSET];
846                 else
847                         rq.nr_sectors = data_size / SECTOR_SIZE;
848
849                 if (!rq.nr_sectors) {
850                         printk(KERN_ERR "%s: in/out command without data\n",
851                                         drive->name);
852                         return -EFAULT;
853                 }
854
855                 rq.hard_nr_sectors = rq.nr_sectors;
856                 rq.hard_cur_sectors = rq.current_nr_sectors = rq.nr_sectors;
857         }
858
859         rq.special = args;
860         return ide_do_drive_cmd(drive, &rq, ide_wait);
861 }
862
863 EXPORT_SYMBOL(ide_diag_taskfile);
864
865 int ide_raw_taskfile (ide_drive_t *drive, ide_task_t *args, u8 *buf)
866 {
867         return ide_diag_taskfile(drive, args, 0, buf);
868 }
869
870 EXPORT_SYMBOL(ide_raw_taskfile);
871
872 #define MAX_DMA         (256*SECTOR_WORDS)
873
874 ide_startstop_t flagged_taskfile(ide_drive_t *, ide_task_t *);
875 ide_startstop_t flagged_task_no_data_intr(ide_drive_t *);
876 ide_startstop_t flagged_task_in_intr(ide_drive_t *);
877 ide_startstop_t flagged_task_mulin_intr(ide_drive_t *);
878 ide_startstop_t flagged_pre_task_out_intr(ide_drive_t *, struct request *);
879 ide_startstop_t flagged_task_out_intr(ide_drive_t *);
880 ide_startstop_t flagged_pre_task_mulout_intr(ide_drive_t *, struct request *);
881 ide_startstop_t flagged_task_mulout_intr(ide_drive_t *);
882
883 int ide_taskfile_ioctl (ide_drive_t *drive, unsigned int cmd, unsigned long arg)
884 {
885         ide_task_request_t      *req_task;
886         ide_task_t              args;
887         u8 *outbuf              = NULL;
888         u8 *inbuf               = NULL;
889         task_ioreg_t *argsptr   = args.tfRegister;
890         task_ioreg_t *hobsptr   = args.hobRegister;
891         int err                 = 0;
892         int tasksize            = sizeof(struct ide_task_request_s);
893         int taskin              = 0;
894         int taskout             = 0;
895         u8 io_32bit             = drive->io_32bit;
896         char __user *buf = (char __user *)arg;
897
898 //      printk("IDE Taskfile ...\n");
899
900         req_task = kmalloc(tasksize, GFP_KERNEL);
901         if (req_task == NULL) return -ENOMEM;
902         memset(req_task, 0, tasksize);
903         if (copy_from_user(req_task, buf, tasksize)) {
904                 kfree(req_task);
905                 return -EFAULT;
906         }
907
908         taskout = (int) req_task->out_size;
909         taskin  = (int) req_task->in_size;
910
911         if (taskout) {
912                 int outtotal = tasksize;
913                 outbuf = kmalloc(taskout, GFP_KERNEL);
914                 if (outbuf == NULL) {
915                         err = -ENOMEM;
916                         goto abort;
917                 }
918                 memset(outbuf, 0, taskout);
919                 if (copy_from_user(outbuf, buf + outtotal, taskout)) {
920                         err = -EFAULT;
921                         goto abort;
922                 }
923         }
924
925         if (taskin) {
926                 int intotal = tasksize + taskout;
927                 inbuf = kmalloc(taskin, GFP_KERNEL);
928                 if (inbuf == NULL) {
929                         err = -ENOMEM;
930                         goto abort;
931                 }
932                 memset(inbuf, 0, taskin);
933                 if (copy_from_user(inbuf, buf + intotal, taskin)) {
934                         err = -EFAULT;
935                         goto abort;
936                 }
937         }
938
939         memset(&args, 0, sizeof(ide_task_t));
940         memcpy(argsptr, req_task->io_ports, HDIO_DRIVE_TASK_HDR_SIZE);
941         memcpy(hobsptr, req_task->hob_ports, HDIO_DRIVE_HOB_HDR_SIZE);
942
943         args.tf_in_flags  = req_task->in_flags;
944         args.tf_out_flags = req_task->out_flags;
945         args.data_phase   = req_task->data_phase;
946         args.command_type = req_task->req_cmd;
947
948         drive->io_32bit = 0;
949         switch(req_task->data_phase) {
950                 case TASKFILE_OUT_DMAQ:
951                 case TASKFILE_OUT_DMA:
952                         err = ide_diag_taskfile(drive, &args, taskout, outbuf);
953                         break;
954                 case TASKFILE_IN_DMAQ:
955                 case TASKFILE_IN_DMA:
956                         err = ide_diag_taskfile(drive, &args, taskin, inbuf);
957                         break;
958                 case TASKFILE_IN_OUT:
959 #if 0
960                         args.prehandler = &pre_task_out_intr;
961                         args.handler = &task_out_intr;
962                         err = ide_diag_taskfile(drive, &args, taskout, outbuf);
963                         args.prehandler = NULL;
964                         args.handler = &task_in_intr;
965                         err = ide_diag_taskfile(drive, &args, taskin, inbuf);
966                         break;
967 #else
968                         err = -EFAULT;
969                         goto abort;
970 #endif
971                 case TASKFILE_MULTI_OUT:
972                         if (!drive->mult_count) {
973                                 /* (hs): give up if multcount is not set */
974                                 printk(KERN_ERR "%s: %s Multimode Write " \
975                                         "multcount is not set\n",
976                                         drive->name, __FUNCTION__);
977                                 err = -EPERM;
978                                 goto abort;
979                         }
980                         if (args.tf_out_flags.all != 0) {
981                                 args.prehandler = &flagged_pre_task_mulout_intr;
982                                 args.handler = &flagged_task_mulout_intr;
983                         } else {
984                                 args.prehandler = &pre_task_mulout_intr;
985                                 args.handler = &task_mulout_intr;
986                         }
987                         err = ide_diag_taskfile(drive, &args, taskout, outbuf);
988                         break;
989                 case TASKFILE_OUT:
990                         if (args.tf_out_flags.all != 0) {
991                                 args.prehandler = &flagged_pre_task_out_intr;
992                                 args.handler    = &flagged_task_out_intr;
993                         } else {
994                                 args.prehandler = &pre_task_out_intr;
995                                 args.handler = &task_out_intr;
996                         }
997                         err = ide_diag_taskfile(drive, &args, taskout, outbuf);
998                         break;
999                 case TASKFILE_MULTI_IN:
1000                         if (!drive->mult_count) {
1001                                 /* (hs): give up if multcount is not set */
1002                                 printk(KERN_ERR "%s: %s Multimode Read failure " \
1003                                         "multcount is not set\n",
1004                                         drive->name, __FUNCTION__);
1005                                 err = -EPERM;
1006                                 goto abort;
1007                         }
1008                         if (args.tf_out_flags.all != 0) {
1009                                 args.handler = &flagged_task_mulin_intr;
1010                         } else {
1011                                 args.handler = &task_mulin_intr;
1012                         }
1013                         err = ide_diag_taskfile(drive, &args, taskin, inbuf);
1014                         break;
1015                 case TASKFILE_IN:
1016                         if (args.tf_out_flags.all != 0) {
1017                                 args.handler = &flagged_task_in_intr;
1018                         } else {
1019                                 args.handler = &task_in_intr;
1020                         }
1021                         err = ide_diag_taskfile(drive, &args, taskin, inbuf);
1022                         break;
1023                 case TASKFILE_NO_DATA:
1024                         if (args.tf_out_flags.all != 0) {
1025                                 args.handler = &flagged_task_no_data_intr;
1026                         } else {
1027                                 args.handler = &task_no_data_intr;
1028                         }
1029                         err = ide_diag_taskfile(drive, &args, 0, NULL);
1030                         break;
1031                 default:
1032                         err = -EFAULT;
1033                         goto abort;
1034         }
1035
1036         memcpy(req_task->io_ports, &(args.tfRegister), HDIO_DRIVE_TASK_HDR_SIZE);
1037         memcpy(req_task->hob_ports, &(args.hobRegister), HDIO_DRIVE_HOB_HDR_SIZE);
1038         req_task->in_flags  = args.tf_in_flags;
1039         req_task->out_flags = args.tf_out_flags;
1040
1041         if (copy_to_user(buf, req_task, tasksize)) {
1042                 err = -EFAULT;
1043                 goto abort;
1044         }
1045         if (taskout) {
1046                 int outtotal = tasksize;
1047                 if (copy_to_user(buf + outtotal, outbuf, taskout)) {
1048                         err = -EFAULT;
1049                         goto abort;
1050                 }
1051         }
1052         if (taskin) {
1053                 int intotal = tasksize + taskout;
1054                 if (copy_to_user(buf + intotal, inbuf, taskin)) {
1055                         err = -EFAULT;
1056                         goto abort;
1057                 }
1058         }
1059 abort:
1060         kfree(req_task);
1061         if (outbuf != NULL)
1062                 kfree(outbuf);
1063         if (inbuf != NULL)
1064                 kfree(inbuf);
1065
1066 //      printk("IDE Taskfile ioctl ended. rc = %i\n", err);
1067
1068         drive->io_32bit = io_32bit;
1069
1070         return err;
1071 }
1072
1073 EXPORT_SYMBOL(ide_taskfile_ioctl);
1074
1075 int ide_wait_cmd (ide_drive_t *drive, u8 cmd, u8 nsect, u8 feature, u8 sectors, u8 *buf)
1076 {
1077         struct request rq;
1078         u8 buffer[4];
1079
1080         if (!buf)
1081                 buf = buffer;
1082         memset(buf, 0, 4 + SECTOR_WORDS * 4 * sectors);
1083         ide_init_drive_cmd(&rq);
1084         rq.buffer = buf;
1085         *buf++ = cmd;
1086         *buf++ = nsect;
1087         *buf++ = feature;
1088         *buf++ = sectors;
1089         return ide_do_drive_cmd(drive, &rq, ide_wait);
1090 }
1091
1092 EXPORT_SYMBOL(ide_wait_cmd);
1093
1094 /*
1095  * FIXME : this needs to map into at taskfile. <andre@linux-ide.org>
1096  */
1097 int ide_cmd_ioctl (ide_drive_t *drive, unsigned int cmd, unsigned long arg)
1098 {
1099         int err = 0;
1100         u8 args[4], *argbuf = args;
1101         u8 xfer_rate = 0;
1102         int argsize = 4;
1103         ide_task_t tfargs;
1104
1105         if (NULL == (void *) arg) {
1106                 struct request rq;
1107                 ide_init_drive_cmd(&rq);
1108                 return ide_do_drive_cmd(drive, &rq, ide_wait);
1109         }
1110
1111         if (copy_from_user(args, (void __user *)arg, 4))
1112                 return -EFAULT;
1113
1114         memset(&tfargs, 0, sizeof(ide_task_t));
1115         tfargs.tfRegister[IDE_FEATURE_OFFSET] = args[2];
1116         tfargs.tfRegister[IDE_NSECTOR_OFFSET] = args[3];
1117         tfargs.tfRegister[IDE_SECTOR_OFFSET]  = args[1];
1118         tfargs.tfRegister[IDE_LCYL_OFFSET]    = 0x00;
1119         tfargs.tfRegister[IDE_HCYL_OFFSET]    = 0x00;
1120         tfargs.tfRegister[IDE_SELECT_OFFSET]  = 0x00;
1121         tfargs.tfRegister[IDE_COMMAND_OFFSET] = args[0];
1122
1123         if (args[3]) {
1124                 argsize = 4 + (SECTOR_WORDS * 4 * args[3]);
1125                 argbuf = kmalloc(argsize, GFP_KERNEL);
1126                 if (argbuf == NULL)
1127                         return -ENOMEM;
1128                 memcpy(argbuf, args, 4);
1129         }
1130         if (set_transfer(drive, &tfargs)) {
1131                 xfer_rate = args[1];
1132                 if (ide_ata66_check(drive, &tfargs))
1133                         goto abort;
1134         }
1135
1136         err = ide_wait_cmd(drive, args[0], args[1], args[2], args[3], argbuf);
1137
1138         if (!err && xfer_rate) {
1139                 /* active-retuning-calls future */
1140                 ide_set_xfer_rate(drive, xfer_rate);
1141                 ide_driveid_update(drive);
1142         }
1143 abort:
1144         if (copy_to_user((void __user *)arg, argbuf, argsize))
1145                 err = -EFAULT;
1146         if (argsize > 4)
1147                 kfree(argbuf);
1148         return err;
1149 }
1150
1151 EXPORT_SYMBOL(ide_cmd_ioctl);
1152
1153 int ide_wait_cmd_task (ide_drive_t *drive, u8 *buf)
1154 {
1155         struct request rq;
1156
1157         ide_init_drive_cmd(&rq);
1158         rq.flags = REQ_DRIVE_TASK;
1159         rq.buffer = buf;
1160         return ide_do_drive_cmd(drive, &rq, ide_wait);
1161 }
1162
1163 EXPORT_SYMBOL(ide_wait_cmd_task);
1164
1165 /*
1166  * FIXME : this needs to map into at taskfile. <andre@linux-ide.org>
1167  */
1168 int ide_task_ioctl (ide_drive_t *drive, unsigned int cmd, unsigned long arg)
1169 {
1170         void __user *p = (void __user *)arg;
1171         int err = 0;
1172         u8 args[7], *argbuf = args;
1173         int argsize = 7;
1174
1175         if (copy_from_user(args, p, 7))
1176                 return -EFAULT;
1177         err = ide_wait_cmd_task(drive, argbuf);
1178         if (copy_to_user(p, argbuf, argsize))
1179                 err = -EFAULT;
1180         return err;
1181 }
1182
1183 EXPORT_SYMBOL(ide_task_ioctl);
1184
1185 /*
1186  * NOTICE: This is additions from IBM to provide a discrete interface,
1187  * for selective taskregister access operations.  Nice JOB Klaus!!!
1188  * Glad to be able to work and co-develop this with you and IBM.
1189  */
1190 ide_startstop_t flagged_taskfile (ide_drive_t *drive, ide_task_t *task)
1191 {
1192         ide_hwif_t *hwif        = HWIF(drive);
1193         task_struct_t *taskfile = (task_struct_t *) task->tfRegister;
1194         hob_struct_t *hobfile   = (hob_struct_t *) task->hobRegister;
1195 #if DEBUG_TASKFILE
1196         u8 status;
1197 #endif
1198
1199
1200 #ifdef CONFIG_IDE_TASK_IOCTL_DEBUG
1201         void debug_taskfile(drive, task);
1202 #endif /* CONFIG_IDE_TASK_IOCTL_DEBUG */
1203
1204         /*
1205          * (ks) Check taskfile in/out flags.
1206          * If set, then execute as it is defined.
1207          * If not set, then define default settings.
1208          * The default values are:
1209          *      write and read all taskfile registers (except data) 
1210          *      write and read the hob registers (sector,nsector,lcyl,hcyl)
1211          */
1212         if (task->tf_out_flags.all == 0) {
1213                 task->tf_out_flags.all = IDE_TASKFILE_STD_OUT_FLAGS;
1214                 if (drive->addressing == 1)
1215                         task->tf_out_flags.all |= (IDE_HOB_STD_OUT_FLAGS << 8);
1216         }
1217
1218         if (task->tf_in_flags.all == 0) {
1219                 task->tf_in_flags.all = IDE_TASKFILE_STD_IN_FLAGS;
1220                 if (drive->addressing == 1)
1221                         task->tf_in_flags.all |= (IDE_HOB_STD_IN_FLAGS  << 8);
1222         }
1223
1224         /* ALL Command Block Executions SHALL clear nIEN, unless otherwise */
1225         if (IDE_CONTROL_REG)
1226                 /* clear nIEN */
1227                 hwif->OUTB(drive->ctl, IDE_CONTROL_REG);
1228         SELECT_MASK(drive, 0);
1229
1230 #if DEBUG_TASKFILE
1231         status = hwif->INB(IDE_STATUS_REG);
1232         if (status & 0x80) {
1233                 printk("flagged_taskfile -> Bad status. Status = %02x. wait 100 usec ...\n", status);
1234                 udelay(100);
1235                 status = hwif->INB(IDE_STATUS_REG);
1236                 printk("flagged_taskfile -> Status = %02x\n", status);
1237         }
1238 #endif
1239
1240         if (task->tf_out_flags.b.data) {
1241                 u16 data =  taskfile->data + (hobfile->data << 8);
1242                 hwif->OUTW(data, IDE_DATA_REG);
1243         }
1244
1245         /* (ks) send hob registers first */
1246         if (task->tf_out_flags.b.nsector_hob)
1247                 hwif->OUTB(hobfile->sector_count, IDE_NSECTOR_REG);
1248         if (task->tf_out_flags.b.sector_hob)
1249                 hwif->OUTB(hobfile->sector_number, IDE_SECTOR_REG);
1250         if (task->tf_out_flags.b.lcyl_hob)
1251                 hwif->OUTB(hobfile->low_cylinder, IDE_LCYL_REG);
1252         if (task->tf_out_flags.b.hcyl_hob)
1253                 hwif->OUTB(hobfile->high_cylinder, IDE_HCYL_REG);
1254
1255         /* (ks) Send now the standard registers */
1256         if (task->tf_out_flags.b.error_feature)
1257                 hwif->OUTB(taskfile->feature, IDE_FEATURE_REG);
1258         /* refers to number of sectors to transfer */
1259         if (task->tf_out_flags.b.nsector)
1260                 hwif->OUTB(taskfile->sector_count, IDE_NSECTOR_REG);
1261         /* refers to sector offset or start sector */
1262         if (task->tf_out_flags.b.sector)
1263                 hwif->OUTB(taskfile->sector_number, IDE_SECTOR_REG);
1264         if (task->tf_out_flags.b.lcyl)
1265                 hwif->OUTB(taskfile->low_cylinder, IDE_LCYL_REG);
1266         if (task->tf_out_flags.b.hcyl)
1267                 hwif->OUTB(taskfile->high_cylinder, IDE_HCYL_REG);
1268
1269         /*
1270          * (ks) In the flagged taskfile approch, we will used all specified
1271          * registers and the register value will not be changed. Except the
1272          * select bit (master/slave) in the drive_head register. We must make
1273          * sure that the desired drive is selected.
1274          */
1275         hwif->OUTB(taskfile->device_head | drive->select.all, IDE_SELECT_REG);
1276         switch(task->data_phase) {
1277
1278                 case TASKFILE_OUT_DMAQ:
1279                 case TASKFILE_OUT_DMA:
1280                         hwif->ide_dma_write(drive);
1281                         break;
1282
1283                 case TASKFILE_IN_DMAQ:
1284                 case TASKFILE_IN_DMA:
1285                         hwif->ide_dma_read(drive);
1286                         break;
1287
1288                 default:
1289                         if (task->handler == NULL)
1290                                 return ide_stopped;
1291
1292                         /* Issue the command */
1293                         ide_execute_command(drive, taskfile->command, task->handler, WAIT_WORSTCASE, NULL);
1294                         if (task->prehandler != NULL)
1295                                 return task->prehandler(drive, HWGROUP(drive)->rq);
1296         }
1297
1298         return ide_started;
1299 }
1300
1301 EXPORT_SYMBOL(flagged_taskfile);
1302
1303 ide_startstop_t flagged_task_no_data_intr (ide_drive_t *drive)
1304 {
1305         ide_hwif_t *hwif = HWIF(drive);
1306         u8 stat;
1307
1308         local_irq_enable();
1309
1310         if (!OK_STAT(stat = hwif->INB(IDE_STATUS_REG), READY_STAT, BAD_STAT)) {
1311                 if (stat & ERR_STAT) {
1312                         return DRIVER(drive)->error(drive, "flagged_task_no_data_intr", stat);
1313                 }
1314                 /*
1315                  * (ks) Unexpected ATA data phase detected.
1316                  * This should not happen. But, it can !
1317                  * I am not sure, which function is best to clean up
1318                  * this situation.  I choose: ide_error(...)
1319                  */
1320                 return DRIVER(drive)->error(drive, "flagged_task_no_data_intr (unexpected phase)", stat); 
1321         }
1322
1323         ide_end_drive_cmd(drive, stat, hwif->INB(IDE_ERROR_REG));
1324
1325         return ide_stopped;
1326 }
1327
1328 /*
1329  * Handler for command with PIO data-in phase
1330  */
1331 ide_startstop_t flagged_task_in_intr (ide_drive_t *drive)
1332 {
1333         ide_hwif_t *hwif        = HWIF(drive);
1334         u8 stat                 = hwif->INB(IDE_STATUS_REG);
1335         struct request *rq      = HWGROUP(drive)->rq;
1336         char *pBuf              = NULL;
1337         int retries             = 5;
1338
1339         if (!OK_STAT(stat, DATA_READY, BAD_R_STAT)) {
1340                 if (stat & ERR_STAT) {
1341                         return DRIVER(drive)->error(drive, "flagged_task_in_intr", stat);
1342                 }
1343                 /*
1344                  * (ks) Unexpected ATA data phase detected.
1345                  * This should not happen. But, it can !
1346                  * I am not sure, which function is best to clean up
1347                  * this situation.  I choose: ide_error(...)
1348                  */
1349                 return DRIVER(drive)->error(drive, "flagged_task_in_intr (unexpected data phase)", stat); 
1350         }
1351
1352         pBuf = rq->buffer + ((rq->nr_sectors - rq->current_nr_sectors) * SECTOR_SIZE);
1353         DTF("Read - rq->current_nr_sectors: %d, status: %02x\n", (int) rq->current_nr_sectors, stat);
1354
1355         taskfile_input_data(drive, pBuf, SECTOR_WORDS);
1356
1357         if (--rq->current_nr_sectors != 0) {
1358                 /*
1359                  * (ks) We don't know which command was executed. 
1360                  * So, we wait the 'WORSTCASE' value.
1361                  */
1362                 ide_set_handler(drive, &flagged_task_in_intr,  WAIT_WORSTCASE, NULL);
1363                 return ide_started;
1364         }
1365         /*
1366          * (ks) Last sector was transfered, wait until drive is ready. 
1367          * This can take up to 10 usec. We willl wait max 50 us.
1368          */
1369         while (((stat = hwif->INB(IDE_STATUS_REG)) & BUSY_STAT) && retries--)
1370                 udelay(10);
1371         ide_end_drive_cmd (drive, stat, hwif->INB(IDE_ERROR_REG));
1372
1373         return ide_stopped;
1374 }
1375
1376 ide_startstop_t flagged_task_mulin_intr (ide_drive_t *drive)
1377 {
1378         ide_hwif_t *hwif        = HWIF(drive);
1379         u8 stat                 = hwif->INB(IDE_STATUS_REG);
1380         struct request *rq      = HWGROUP(drive)->rq;
1381         char *pBuf              = NULL;
1382         int retries             = 5;
1383         unsigned int msect, nsect;
1384
1385         msect = drive->mult_count;
1386         if (msect == 0) 
1387                 return DRIVER(drive)->error(drive, "flagged_task_mulin_intr (multimode not set)", stat); 
1388
1389         if (!OK_STAT(stat, DATA_READY, BAD_R_STAT)) {
1390                 if (stat & ERR_STAT) {
1391                         return DRIVER(drive)->error(drive, "flagged_task_mulin_intr", stat);
1392                 }
1393                 /*
1394                  * (ks) Unexpected ATA data phase detected.
1395                  * This should not happen. But, it can !
1396                  * I am not sure, which function is best to clean up
1397                  * this situation.  I choose: ide_error(...)
1398                  */
1399                 return DRIVER(drive)->error(drive, "flagged_task_mulin_intr (unexpected data phase)", stat); 
1400         }
1401
1402         nsect = (rq->current_nr_sectors > msect) ? msect : rq->current_nr_sectors;
1403         pBuf = rq->buffer + ((rq->nr_sectors - rq->current_nr_sectors) * SECTOR_SIZE);
1404
1405         DTF("Multiread: %p, nsect: %d , rq->current_nr_sectors: %ld\n",
1406             pBuf, nsect, rq->current_nr_sectors);
1407
1408         taskfile_input_data(drive, pBuf, nsect * SECTOR_WORDS);
1409
1410         rq->current_nr_sectors -= nsect;
1411         if (rq->current_nr_sectors != 0) {
1412                 /*
1413                  * (ks) We don't know which command was executed. 
1414                  * So, we wait the 'WORSTCASE' value.
1415                  */
1416                 ide_set_handler(drive, &flagged_task_mulin_intr,  WAIT_WORSTCASE, NULL);
1417                 return ide_started;
1418         }
1419
1420         /*
1421          * (ks) Last sector was transfered, wait until drive is ready. 
1422          * This can take up to 10 usec. We willl wait max 50 us.
1423          */
1424         while (((stat = hwif->INB(IDE_STATUS_REG)) & BUSY_STAT) && retries--)
1425                 udelay(10);
1426         ide_end_drive_cmd (drive, stat, hwif->INB(IDE_ERROR_REG));
1427
1428         return ide_stopped;
1429 }
1430
1431 /*
1432  * Pre handler for command with PIO data-out phase
1433  */
1434 ide_startstop_t flagged_pre_task_out_intr (ide_drive_t *drive, struct request *rq)
1435 {
1436         ide_startstop_t startstop;
1437
1438         if (ide_wait_stat(&startstop, drive, DATA_READY,
1439                         BAD_W_STAT, WAIT_DRQ)) {
1440                 printk(KERN_ERR "%s: No DRQ bit after issuing write command.\n", drive->name);
1441                 return startstop;
1442         }
1443
1444         taskfile_output_data(drive, rq->buffer, SECTOR_WORDS);
1445         --rq->current_nr_sectors;
1446
1447         return ide_started;
1448 }
1449
1450 ide_startstop_t flagged_task_out_intr (ide_drive_t *drive)
1451 {
1452         ide_hwif_t *hwif        = HWIF(drive);
1453         u8 stat                 = hwif->INB(IDE_STATUS_REG);
1454         struct request *rq      = HWGROUP(drive)->rq;
1455         char *pBuf              = NULL;
1456
1457         if (!OK_STAT(stat, DRIVE_READY, BAD_W_STAT)) 
1458                 return DRIVER(drive)->error(drive, "flagged_task_out_intr", stat);
1459         
1460         if (!rq->current_nr_sectors) { 
1461                 ide_end_drive_cmd (drive, stat, hwif->INB(IDE_ERROR_REG));
1462                 return ide_stopped;
1463         }
1464
1465         if (!OK_STAT(stat, DATA_READY, BAD_W_STAT)) {
1466                 /*
1467                  * (ks) Unexpected ATA data phase detected.
1468                  * This should not happen. But, it can !
1469                  * I am not sure, which function is best to clean up
1470                  * this situation.  I choose: ide_error(...)
1471                  */
1472                 return DRIVER(drive)->error(drive, "flagged_task_out_intr (unexpected data phase)", stat); 
1473         }
1474
1475         pBuf = rq->buffer + ((rq->nr_sectors - rq->current_nr_sectors) * SECTOR_SIZE);
1476         DTF("Write - rq->current_nr_sectors: %d, status: %02x\n",
1477                 (int) rq->current_nr_sectors, stat);
1478
1479         taskfile_output_data(drive, pBuf, SECTOR_WORDS);
1480         --rq->current_nr_sectors;
1481
1482         /*
1483          * (ks) We don't know which command was executed. 
1484          * So, we wait the 'WORSTCASE' value.
1485          */
1486         ide_set_handler(drive, &flagged_task_out_intr, WAIT_WORSTCASE, NULL);
1487
1488         return ide_started;
1489 }
1490
1491 ide_startstop_t flagged_pre_task_mulout_intr (ide_drive_t *drive, struct request *rq)
1492 {
1493         ide_hwif_t *hwif        = HWIF(drive);
1494         u8 stat                 = hwif->INB(IDE_STATUS_REG);
1495         char *pBuf              = NULL;
1496         ide_startstop_t startstop;
1497         unsigned int msect, nsect;
1498
1499         msect = drive->mult_count;
1500         if (msect == 0)
1501                 return DRIVER(drive)->error(drive, "flagged_pre_task_mulout_intr (multimode not set)", stat);
1502
1503         if (ide_wait_stat(&startstop, drive, DATA_READY,
1504                         BAD_W_STAT, WAIT_DRQ)) {
1505                 printk(KERN_ERR "%s: No DRQ bit after issuing write command.\n", drive->name);
1506                 return startstop;
1507         }
1508
1509         nsect = (rq->current_nr_sectors > msect) ? msect : rq->current_nr_sectors;
1510         pBuf = rq->buffer + ((rq->nr_sectors - rq->current_nr_sectors) * SECTOR_SIZE);
1511         DTF("Multiwrite: %p, nsect: %d , rq->current_nr_sectors: %ld\n",
1512             pBuf, nsect, rq->current_nr_sectors);
1513
1514         taskfile_output_data(drive, pBuf, nsect * SECTOR_WORDS);
1515
1516         rq->current_nr_sectors -= nsect;
1517
1518         return ide_started;
1519 }
1520
1521 ide_startstop_t flagged_task_mulout_intr (ide_drive_t *drive)
1522 {
1523         ide_hwif_t *hwif        = HWIF(drive);
1524         u8 stat                 = hwif->INB(IDE_STATUS_REG);
1525         struct request *rq      = HWGROUP(drive)->rq;
1526         char *pBuf              = NULL;
1527         unsigned int msect, nsect;
1528
1529         msect = drive->mult_count;
1530         if (msect == 0)
1531                 return DRIVER(drive)->error(drive, "flagged_task_mulout_intr (multimode not set)", stat);
1532
1533         if (!OK_STAT(stat, DRIVE_READY, BAD_W_STAT)) 
1534                 return DRIVER(drive)->error(drive, "flagged_task_mulout_intr", stat);
1535         
1536         if (!rq->current_nr_sectors) { 
1537                 ide_end_drive_cmd (drive, stat, hwif->INB(IDE_ERROR_REG));
1538                 return ide_stopped;
1539         }
1540
1541         if (!OK_STAT(stat, DATA_READY, BAD_W_STAT)) {
1542                 /*
1543                  * (ks) Unexpected ATA data phase detected.
1544                  * This should not happen. But, it can !
1545                  * I am not sure, which function is best to clean up
1546                  * this situation.  I choose: ide_error(...)
1547                  */
1548                 return DRIVER(drive)->error(drive, "flagged_task_mulout_intr (unexpected data phase)", stat); 
1549         }
1550
1551         nsect = (rq->current_nr_sectors > msect) ? msect : rq->current_nr_sectors;
1552         pBuf = rq->buffer + ((rq->nr_sectors - rq->current_nr_sectors) * SECTOR_SIZE);
1553         DTF("Multiwrite: %p, nsect: %d , rq->current_nr_sectors: %ld\n",
1554             pBuf, nsect, rq->current_nr_sectors);
1555
1556         taskfile_output_data(drive, pBuf, nsect * SECTOR_WORDS);
1557         rq->current_nr_sectors -= nsect;
1558
1559         /*
1560          * (ks) We don't know which command was executed. 
1561          * So, we wait the 'WORSTCASE' value.
1562          */
1563         ide_set_handler(drive, &flagged_task_mulout_intr, WAIT_WORSTCASE, NULL);
1564
1565         return ide_started;
1566 }