+- add patches.fixes/linux-post-2.6.3-20040220
[linux-flexiantxendom0-3.2.10.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_cmd_type_parser(&args);
105         return ide_raw_taskfile(drive, &args, buf);
106 }
107
108 EXPORT_SYMBOL(taskfile_lib_get_identify);
109
110 #ifdef CONFIG_IDE_TASK_IOCTL_DEBUG
111 void debug_taskfile (ide_drive_t *drive, ide_task_t *args)
112 {
113         printk(KERN_INFO "%s: ", drive->name);
114 //      printk("TF.0=x%02x ", args->tfRegister[IDE_DATA_OFFSET]);
115         printk("TF.1=x%02x ", args->tfRegister[IDE_FEATURE_OFFSET]);
116         printk("TF.2=x%02x ", args->tfRegister[IDE_NSECTOR_OFFSET]);
117         printk("TF.3=x%02x ", args->tfRegister[IDE_SECTOR_OFFSET]);
118         printk("TF.4=x%02x ", args->tfRegister[IDE_LCYL_OFFSET]);
119         printk("TF.5=x%02x ", args->tfRegister[IDE_HCYL_OFFSET]);
120         printk("TF.6=x%02x ", args->tfRegister[IDE_SELECT_OFFSET]);
121         printk("TF.7=x%02x\n", args->tfRegister[IDE_COMMAND_OFFSET]);
122         printk(KERN_INFO "%s: ", drive->name);
123 //      printk("HTF.0=x%02x ", args->hobRegister[IDE_DATA_OFFSET_HOB]);
124         printk("HTF.1=x%02x ", args->hobRegister[IDE_FEATURE_OFFSET_HOB]);
125         printk("HTF.2=x%02x ", args->hobRegister[IDE_NSECTOR_OFFSET_HOB]);
126         printk("HTF.3=x%02x ", args->hobRegister[IDE_SECTOR_OFFSET_HOB]);
127         printk("HTF.4=x%02x ", args->hobRegister[IDE_LCYL_OFFSET_HOB]);
128         printk("HTF.5=x%02x ", args->hobRegister[IDE_HCYL_OFFSET_HOB]);
129         printk("HTF.6=x%02x ", args->hobRegister[IDE_SELECT_OFFSET_HOB]);
130         printk("HTF.7=x%02x\n", args->hobRegister[IDE_CONTROL_OFFSET_HOB]);
131 }
132 #endif /* CONFIG_IDE_TASK_IOCTL_DEBUG */
133
134 ide_startstop_t do_rw_taskfile (ide_drive_t *drive, ide_task_t *task)
135 {
136         ide_hwif_t *hwif        = HWIF(drive);
137         task_struct_t *taskfile = (task_struct_t *) task->tfRegister;
138         hob_struct_t *hobfile   = (hob_struct_t *) task->hobRegister;
139         u8 HIHI                 = (drive->addressing == 1) ? 0xE0 : 0xEF;
140
141 #ifdef CONFIG_IDE_TASK_IOCTL_DEBUG
142         void debug_taskfile(drive, task);
143 #endif /* CONFIG_IDE_TASK_IOCTL_DEBUG */
144
145         /* ALL Command Block Executions SHALL clear nIEN, unless otherwise */
146         if (IDE_CONTROL_REG) {
147                 /* clear nIEN */
148                 hwif->OUTB(drive->ctl, IDE_CONTROL_REG);
149         }
150         SELECT_MASK(drive, 0);
151
152         if (drive->addressing == 1) {
153                 hwif->OUTB(hobfile->feature, IDE_FEATURE_REG);
154                 hwif->OUTB(hobfile->sector_count, IDE_NSECTOR_REG);
155                 hwif->OUTB(hobfile->sector_number, IDE_SECTOR_REG);
156                 hwif->OUTB(hobfile->low_cylinder, IDE_LCYL_REG);
157                 hwif->OUTB(hobfile->high_cylinder, IDE_HCYL_REG);
158         }
159
160         hwif->OUTB(taskfile->feature, IDE_FEATURE_REG);
161         hwif->OUTB(taskfile->sector_count, IDE_NSECTOR_REG);
162         hwif->OUTB(taskfile->sector_number, IDE_SECTOR_REG);
163         hwif->OUTB(taskfile->low_cylinder, IDE_LCYL_REG);
164         hwif->OUTB(taskfile->high_cylinder, IDE_HCYL_REG);
165
166         hwif->OUTB((taskfile->device_head & HIHI) | drive->select.all, IDE_SELECT_REG);
167 #ifdef CONFIG_IDE_TASKFILE_IO
168         if (task->handler != NULL) {
169                 if (task->prehandler != NULL) {
170                         hwif->OUTBSYNC(drive, taskfile->command, IDE_COMMAND_REG);
171                         ndelay(400);    /* FIXME */
172                         return task->prehandler(drive, task->rq);
173                 }
174                 ide_execute_command(drive, taskfile->command, task->handler, WAIT_WORSTCASE, NULL);
175                 return ide_started;
176         }
177 #else
178         if (task->handler != NULL) {
179                 ide_execute_command(drive, taskfile->command, task->handler, WAIT_WORSTCASE, NULL);
180                 if (task->prehandler != NULL)
181                         return task->prehandler(drive, task->rq);
182                 return ide_started;
183         }
184 #endif
185
186         if (!drive->using_dma)
187                 return ide_stopped;
188
189         switch (taskfile->command) {
190                 case WIN_WRITEDMA_ONCE:
191                 case WIN_WRITEDMA:
192                 case WIN_WRITEDMA_EXT:
193                         if (!hwif->ide_dma_write(drive))
194                                 return ide_started;
195                         break;
196                 case WIN_READDMA_ONCE:
197                 case WIN_READDMA:
198                 case WIN_READDMA_EXT:
199                 case WIN_IDENTIFY_DMA:
200                         if (!hwif->ide_dma_read(drive))
201                                 return ide_started;
202                         break;
203 #ifdef CONFIG_BLK_DEV_IDE_TCQ
204                 case WIN_READDMA_QUEUED:
205                 case WIN_READDMA_QUEUED_EXT:
206                         return __ide_dma_queued_read(drive);
207                 case WIN_WRITEDMA_QUEUED:
208                 case WIN_WRITEDMA_QUEUED_EXT:
209                         return __ide_dma_queued_write(drive);
210 #endif
211                 default:
212                         if (task->handler == NULL)
213                                 return ide_stopped;
214         }
215
216         return ide_stopped;
217 }
218
219 EXPORT_SYMBOL(do_rw_taskfile);
220
221 /*
222  * set_multmode_intr() is invoked on completion of a WIN_SETMULT cmd.
223  */
224 ide_startstop_t set_multmode_intr (ide_drive_t *drive)
225 {
226         ide_hwif_t *hwif = HWIF(drive);
227         u8 stat;
228
229         if (OK_STAT(stat = hwif->INB(IDE_STATUS_REG),READY_STAT,BAD_STAT)) {
230                 drive->mult_count = drive->mult_req;
231         } else {
232                 drive->mult_req = drive->mult_count = 0;
233                 drive->special.b.recalibrate = 1;
234                 (void) ide_dump_status(drive, "set_multmode", stat);
235         }
236         return ide_stopped;
237 }
238
239 EXPORT_SYMBOL(set_multmode_intr);
240
241 /*
242  * set_geometry_intr() is invoked on completion of a WIN_SPECIFY cmd.
243  */
244 ide_startstop_t set_geometry_intr (ide_drive_t *drive)
245 {
246         ide_hwif_t *hwif = HWIF(drive);
247         int retries = 5;
248         u8 stat;
249
250         while (((stat = hwif->INB(IDE_STATUS_REG)) & BUSY_STAT) && retries--)
251                 udelay(10);
252
253         if (OK_STAT(stat, READY_STAT, BAD_STAT))
254                 return ide_stopped;
255
256         if (stat & (ERR_STAT|DRQ_STAT))
257                 return DRIVER(drive)->error(drive, "set_geometry_intr", stat);
258
259         if (HWGROUP(drive)->handler != NULL)
260                 BUG();
261         ide_set_handler(drive, &set_geometry_intr, WAIT_WORSTCASE, NULL);
262         return ide_started;
263 }
264
265 EXPORT_SYMBOL(set_geometry_intr);
266
267 /*
268  * recal_intr() is invoked on completion of a WIN_RESTORE (recalibrate) cmd.
269  */
270 ide_startstop_t recal_intr (ide_drive_t *drive)
271 {
272         ide_hwif_t *hwif = HWIF(drive);
273         u8 stat;
274
275         if (!OK_STAT(stat = hwif->INB(IDE_STATUS_REG), READY_STAT, BAD_STAT))
276                 return DRIVER(drive)->error(drive, "recal_intr", stat);
277         return ide_stopped;
278 }
279
280 EXPORT_SYMBOL(recal_intr);
281
282 /*
283  * Handler for commands without a data phase
284  */
285 ide_startstop_t task_no_data_intr (ide_drive_t *drive)
286 {
287         ide_task_t *args        = HWGROUP(drive)->rq->special;
288         ide_hwif_t *hwif        = HWIF(drive);
289         u8 stat;
290
291         local_irq_enable();
292         if (!OK_STAT(stat = hwif->INB(IDE_STATUS_REG),READY_STAT,BAD_STAT)) {
293                 DTF("%s: command opcode 0x%02x\n", drive->name,
294                         args->tfRegister[IDE_COMMAND_OFFSET]);
295                 return DRIVER(drive)->error(drive, "task_no_data_intr", stat);
296                 /* calls ide_end_drive_cmd */
297         }
298         if (args)
299                 ide_end_drive_cmd(drive, stat, hwif->INB(IDE_ERROR_REG));
300
301         return ide_stopped;
302 }
303
304 EXPORT_SYMBOL(task_no_data_intr);
305
306 /*
307  * old taskfile PIO handlers, to be killed as soon as possible.
308  */
309 #ifndef CONFIG_IDE_TASKFILE_IO
310
311 #define task_map_rq(rq, flags)          ide_map_buffer((rq), (flags))
312 #define task_unmap_rq(rq, buf, flags)   ide_unmap_buffer((rq), (buf), (flags))
313
314 /*
315  * Handler for command with PIO data-in phase, READ
316  */
317 /*
318  * FIXME before 2.4 enable ...
319  *      DATA integrity issue upon error. <andre@linux-ide.org>
320  */
321 ide_startstop_t task_in_intr (ide_drive_t *drive)
322 {
323         struct request *rq      = HWGROUP(drive)->rq;
324         ide_hwif_t *hwif        = HWIF(drive);
325         char *pBuf              = NULL;
326         u8 stat;
327         unsigned long flags;
328
329         if (!OK_STAT(stat = hwif->INB(IDE_STATUS_REG),DATA_READY,BAD_R_STAT)) {
330                 if (stat & (ERR_STAT|DRQ_STAT)) {
331 #if 0
332                         DTF("%s: attempting to recover last " \
333                                 "sector counter status=0x%02x\n",
334                                 drive->name, stat);
335                         /*
336                          * Expect a BUG BOMB if we attempt to rewind the
337                          * offset in the BH aka PAGE in the current BLOCK
338                          * segment.  This is different than the HOST segment.
339                          */
340 #endif
341                         if (!rq->bio)
342                                 rq->current_nr_sectors++;
343                         return DRIVER(drive)->error(drive, "task_in_intr", stat);
344                 }
345                 if (!(stat & BUSY_STAT)) {
346                         DTF("task_in_intr to Soon wait for next interrupt\n");
347                         if (HWGROUP(drive)->handler == NULL)
348                                 ide_set_handler(drive, &task_in_intr, WAIT_WORSTCASE, NULL);
349                         return ide_started;  
350                 }
351         }
352 #if 0
353
354         /*
355          * Holding point for a brain dump of a thought :-/
356          */
357
358         if (!OK_STAT(stat,DRIVE_READY,drive->bad_wstat)) {
359                 DTF("%s: READ attempting to recover last " \
360                         "sector counter status=0x%02x\n",
361                         drive->name, stat);
362                 rq->current_nr_sectors++;
363                 return DRIVER(drive)->error(drive, "task_in_intr", stat);
364         }
365         if (!rq->current_nr_sectors)
366                 if (!DRIVER(drive)->end_request(drive, 1, 0))
367                         return ide_stopped;
368
369         if (--rq->current_nr_sectors <= 0)
370                 if (!DRIVER(drive)->end_request(drive, 1, 0))
371                         return ide_stopped;
372 #endif
373
374         pBuf = task_map_rq(rq, &flags);
375         DTF("Read: %p, rq->current_nr_sectors: %d, stat: %02x\n",
376                 pBuf, (int) rq->current_nr_sectors, stat);
377         taskfile_input_data(drive, pBuf, SECTOR_WORDS);
378         task_unmap_rq(rq, pBuf, &flags);
379         /*
380          * FIXME :: We really can not legally get a new page/bh
381          * regardless, if this is the end of our segment.
382          * BH walking or segment can only be updated after we have a good
383          * hwif->INB(IDE_STATUS_REG); return.
384          */
385         if (--rq->current_nr_sectors <= 0)
386                 if (!DRIVER(drive)->end_request(drive, 1, 0))
387                         return ide_stopped;
388         /*
389          * ERM, it is techincally legal to leave/exit here but it makes
390          * a mess of the code ...
391          */
392         if (HWGROUP(drive)->handler == NULL)
393                 ide_set_handler(drive, &task_in_intr, WAIT_WORSTCASE, NULL);
394         return ide_started;
395 }
396
397 EXPORT_SYMBOL(task_in_intr);
398
399 /*
400  * Handler for command with Read Multiple
401  */
402 ide_startstop_t task_mulin_intr (ide_drive_t *drive)
403 {
404         ide_hwif_t *hwif        = HWIF(drive);
405         struct request *rq      = HWGROUP(drive)->rq;
406         char *pBuf              = NULL;
407         unsigned int msect      = drive->mult_count;
408         unsigned int nsect;
409         unsigned long flags;
410         u8 stat;
411
412         if (!OK_STAT(stat = hwif->INB(IDE_STATUS_REG),DATA_READY,BAD_R_STAT)) {
413                 if (stat & (ERR_STAT|DRQ_STAT)) {
414                         if (!rq->bio) {
415                                 rq->current_nr_sectors += drive->mult_count;
416                                 /*
417                                  * NOTE: could rewind beyond beginning :-/
418                                  */
419                         } else {
420                                 printk(KERN_ERR "%s: MULTI-READ assume all data " \
421                                         "transfered is bad status=0x%02x\n",
422                                         drive->name, stat);
423                         }
424                         return DRIVER(drive)->error(drive, "task_mulin_intr", stat);
425                 }
426                 /* no data yet, so wait for another interrupt */
427                 if (HWGROUP(drive)->handler == NULL)
428                         ide_set_handler(drive, &task_mulin_intr, WAIT_WORSTCASE, NULL);
429                 return ide_started;
430         }
431
432         do {
433                 nsect = rq->current_nr_sectors;
434                 if (nsect > msect)
435                         nsect = msect;
436                 pBuf = task_map_rq(rq, &flags);
437                 DTF("Multiread: %p, nsect: %d, msect: %d, " \
438                         " rq->current_nr_sectors: %d\n",
439                         pBuf, nsect, msect, rq->current_nr_sectors);
440                 taskfile_input_data(drive, pBuf, nsect * SECTOR_WORDS);
441                 task_unmap_rq(rq, pBuf, &flags);
442                 rq->errors = 0;
443                 rq->current_nr_sectors -= nsect;
444                 msect -= nsect;
445                 /*
446                  * FIXME :: We really can not legally get a new page/bh
447                  * regardless, if this is the end of our segment.
448                  * BH walking or segment can only be updated after we have a
449                  * good hwif->INB(IDE_STATUS_REG); return.
450                  */
451                 if (!rq->current_nr_sectors) {
452                         if (!DRIVER(drive)->end_request(drive, 1, 0))
453                                 return ide_stopped;
454                 }
455         } while (msect);
456         if (HWGROUP(drive)->handler == NULL)
457                 ide_set_handler(drive, &task_mulin_intr, WAIT_WORSTCASE, NULL);
458         return ide_started;
459 }
460
461 EXPORT_SYMBOL(task_mulin_intr);
462
463 /*
464  * VERIFY ME before 2.4 ... unexpected race is possible based on details
465  * RMK with 74LS245/373/374 TTL buffer logic because of passthrough.
466  */
467 ide_startstop_t pre_task_out_intr (ide_drive_t *drive, struct request *rq)
468 {
469         char *pBuf              = NULL;
470         unsigned long flags;
471         ide_startstop_t startstop;
472
473         if (ide_wait_stat(&startstop, drive, DATA_READY,
474                         drive->bad_wstat, WAIT_DRQ)) {
475                 printk(KERN_ERR "%s: no DRQ after issuing WRITE%s\n",
476                         drive->name,
477                         drive->addressing ? "_EXT" : "");
478                 return startstop;
479         }
480         /* For Write_sectors we need to stuff the first sector */
481         pBuf = task_map_rq(rq, &flags);
482         taskfile_output_data(drive, pBuf, SECTOR_WORDS);
483         rq->current_nr_sectors--;
484         task_unmap_rq(rq, pBuf, &flags);
485         return ide_started;
486 }
487
488 EXPORT_SYMBOL(pre_task_out_intr);
489
490 /*
491  * Handler for command with PIO data-out phase WRITE
492  *
493  * WOOHOO this is a CORRECT STATE DIAGRAM NOW, <andre@linux-ide.org>
494  */
495 ide_startstop_t task_out_intr (ide_drive_t *drive)
496 {
497         ide_hwif_t *hwif        = HWIF(drive);
498         struct request *rq      = HWGROUP(drive)->rq;
499         char *pBuf              = NULL;
500         unsigned long flags;
501         u8 stat;
502
503         if (!OK_STAT(stat = hwif->INB(IDE_STATUS_REG), DRIVE_READY, drive->bad_wstat)) {
504                 DTF("%s: WRITE attempting to recover last " \
505                         "sector counter status=0x%02x\n",
506                         drive->name, stat);
507                 rq->current_nr_sectors++;
508                 return DRIVER(drive)->error(drive, "task_out_intr", stat);
509         }
510         /*
511          * Safe to update request for partial completions.
512          * We have a good STATUS CHECK!!!
513          */
514         if (!rq->current_nr_sectors)
515                 if (!DRIVER(drive)->end_request(drive, 1, 0))
516                         return ide_stopped;
517         if ((rq->current_nr_sectors==1) ^ (stat & DRQ_STAT)) {
518                 rq = HWGROUP(drive)->rq;
519                 pBuf = task_map_rq(rq, &flags);
520                 DTF("write: %p, rq->current_nr_sectors: %d\n",
521                         pBuf, (int) rq->current_nr_sectors);
522                 taskfile_output_data(drive, pBuf, SECTOR_WORDS);
523                 task_unmap_rq(rq, pBuf, &flags);
524                 rq->errors = 0;
525                 rq->current_nr_sectors--;
526         }
527         if (HWGROUP(drive)->handler == NULL)
528                 ide_set_handler(drive, &task_out_intr, WAIT_WORSTCASE, NULL);
529         return ide_started;
530 }
531
532 EXPORT_SYMBOL(task_out_intr);
533
534 #undef ALTERNATE_STATE_DIAGRAM_MULTI_OUT
535
536 ide_startstop_t pre_task_mulout_intr (ide_drive_t *drive, struct request *rq)
537 {
538 #ifdef ALTERNATE_STATE_DIAGRAM_MULTI_OUT
539         ide_hwif_t *hwif                = HWIF(drive);
540         char *pBuf                      = NULL;
541         unsigned int nsect = 0, msect   = drive->mult_count;
542         u8 stat;
543         unsigned long flags;
544 #endif /* ALTERNATE_STATE_DIAGRAM_MULTI_OUT */
545
546         ide_task_t *args = rq->special;
547         ide_startstop_t startstop;
548
549 #if 0
550         /*
551          * assign private copy for multi-write
552          */
553         memcpy(&HWGROUP(drive)->wrq, rq, sizeof(struct request));
554 #endif
555
556         if (ide_wait_stat(&startstop, drive, DATA_READY,
557                         drive->bad_wstat, WAIT_DRQ)) {
558                 printk(KERN_ERR "%s: no DRQ after issuing %s\n",
559                         drive->name,
560                         drive->addressing ? "MULTWRITE_EXT" : "MULTWRITE");
561                 return startstop;
562         }
563 #ifdef ALTERNATE_STATE_DIAGRAM_MULTI_OUT
564
565         do {
566                 nsect = rq->current_nr_sectors;
567                 if (nsect > msect)
568                         nsect = msect;
569                 pBuf = task_map_rq(rq, &flags);
570                 DTF("Pre-Multiwrite: %p, nsect: %d, msect: %d, " \
571                         "rq->current_nr_sectors: %ld\n",
572                         pBuf, nsect, msect, rq->current_nr_sectors);
573                 msect -= nsect;
574                 taskfile_output_data(drive, pBuf, nsect * SECTOR_WORDS);
575                 task_unmap_rq(rq, pBuf, &flags);
576                 rq->current_nr_sectors -= nsect;
577                 if (!rq->current_nr_sectors) {
578                         if (!DRIVER(drive)->end_request(drive, 1, 0))
579                                 if (!rq->bio) {
580                                         stat = hwif->INB(IDE_STATUS_REG);
581                                         return ide_stopped;
582                                 }
583                 }
584         } while (msect);
585         rq->errors = 0;
586         return ide_started;
587 #else /* ! ALTERNATE_STATE_DIAGRAM_MULTI_OUT */
588         if (!(drive_is_ready(drive))) {
589                 int i;
590                 for (i=0; i<100; i++) {
591                         if (drive_is_ready(drive))
592                                 break;
593                 }
594         }
595
596         /*
597          * WARNING :: if the drive as not acked good status we may not
598          * move the DATA-TRANSFER T-Bar as BSY != 0. <andre@linux-ide.org>
599          */
600         return args->handler(drive);
601 #endif /* ALTERNATE_STATE_DIAGRAM_MULTI_OUT */
602 }
603
604 EXPORT_SYMBOL(pre_task_mulout_intr);
605
606 /*
607  * FIXME before enabling in 2.4 ... DATA integrity issue upon error.
608  */
609 /*
610  * Handler for command write multiple
611  * Called directly from execute_drive_cmd for the first bunch of sectors,
612  * afterwards only by the ISR
613  */
614 ide_startstop_t task_mulout_intr (ide_drive_t *drive)
615 {
616         ide_hwif_t *hwif                = HWIF(drive);
617         u8 stat                         = hwif->INB(IDE_STATUS_REG);
618         struct request *rq              = HWGROUP(drive)->rq;
619         char *pBuf                      = NULL;
620         ide_startstop_t startstop       = ide_stopped;
621         unsigned int msect              = drive->mult_count;
622         unsigned int nsect;
623         unsigned long flags;
624
625         /*
626          * (ks/hs): Handle last IRQ on multi-sector transfer,
627          * occurs after all data was sent in this chunk
628          */
629         if (rq->current_nr_sectors == 0) {
630                 if (stat & (ERR_STAT|DRQ_STAT)) {
631                         if (!rq->bio) {
632                                 rq->current_nr_sectors += drive->mult_count;
633                                 /*
634                                  * NOTE: could rewind beyond beginning :-/
635                                  */
636                         } else {
637                                 printk(KERN_ERR "%s: MULTI-WRITE assume all data " \
638                                         "transfered is bad status=0x%02x\n",
639                                         drive->name, stat);
640                         }
641                         return DRIVER(drive)->error(drive, "task_mulout_intr", stat);
642                 }
643                 if (!rq->bio)
644                         DRIVER(drive)->end_request(drive, 1, 0);
645                 return startstop;
646         }
647         /*
648          * DON'T be lazy code the above and below togather !!!
649          */
650         if (!OK_STAT(stat,DATA_READY,BAD_R_STAT)) {
651                 if (stat & (ERR_STAT|DRQ_STAT)) {
652                         if (!rq->bio) {
653                                 rq->current_nr_sectors += drive->mult_count;
654                                 /*
655                                  * NOTE: could rewind beyond beginning :-/
656                                  */
657                         } else {
658                                 printk("%s: MULTI-WRITE assume all data " \
659                                         "transfered is bad status=0x%02x\n",
660                                         drive->name, stat);
661                         }
662                         return DRIVER(drive)->error(drive, "task_mulout_intr", stat);
663                 }
664                 /* no data yet, so wait for another interrupt */
665                 if (HWGROUP(drive)->handler == NULL)
666                         ide_set_handler(drive, &task_mulout_intr, WAIT_WORSTCASE, NULL);
667                 return ide_started;
668         }
669
670 #ifndef ALTERNATE_STATE_DIAGRAM_MULTI_OUT
671         if (HWGROUP(drive)->handler != NULL) {
672                 unsigned long lflags;
673                 spin_lock_irqsave(&ide_lock, lflags);
674                 HWGROUP(drive)->handler = NULL;
675                 del_timer(&HWGROUP(drive)->timer);
676                 spin_unlock_irqrestore(&ide_lock, lflags);
677         }
678 #endif /* ALTERNATE_STATE_DIAGRAM_MULTI_OUT */
679
680         do {
681                 nsect = rq->current_nr_sectors;
682                 if (nsect > msect)
683                         nsect = msect;
684                 pBuf = task_map_rq(rq, &flags);
685                 DTF("Multiwrite: %p, nsect: %d, msect: %d, " \
686                         "rq->current_nr_sectors: %ld\n",
687                         pBuf, nsect, msect, rq->current_nr_sectors);
688                 msect -= nsect;
689                 taskfile_output_data(drive, pBuf, nsect * SECTOR_WORDS);
690                 task_unmap_rq(rq, pBuf, &flags);
691                 rq->current_nr_sectors -= nsect;
692                 /*
693                  * FIXME :: We really can not legally get a new page/bh
694                  * regardless, if this is the end of our segment.
695                  * BH walking or segment can only be updated after we
696                  * have a good  hwif->INB(IDE_STATUS_REG); return.
697                  */
698                 if (!rq->current_nr_sectors) {
699                         if (!DRIVER(drive)->end_request(drive, 1, 0))
700                                 if (!rq->bio)
701                                         return ide_stopped;
702                 }
703         } while (msect);
704         rq->errors = 0;
705         if (HWGROUP(drive)->handler == NULL)
706                 ide_set_handler(drive, &task_mulout_intr, WAIT_WORSTCASE, NULL);
707         return ide_started;
708 }
709
710 EXPORT_SYMBOL(task_mulout_intr);
711
712 #else /* !CONFIG_IDE_TASKFILE_IO */
713
714 static u8 wait_drive_not_busy(ide_drive_t *drive)
715 {
716         ide_hwif_t *hwif = HWIF(drive);
717         int retries = 100;
718         u8 stat;
719
720         /*
721          * Last sector was transfered, wait until drive is ready.
722          * This can take up to 10 usec, but we will wait max 1 ms
723          * (drive_cmd_intr() waits that long).
724          */
725         while (((stat = hwif->INB(IDE_STATUS_REG)) & BUSY_STAT) && retries--)
726                 udelay(10);
727
728         if (!retries)
729                 printk(KERN_ERR "%s: drive still BUSY!\n", drive->name);
730
731         return stat;
732 }
733
734 /*
735  * Handler for command with PIO data-in phase (Read).
736  */
737 ide_startstop_t task_in_intr (ide_drive_t *drive)
738 {
739         struct request *rq = HWGROUP(drive)->rq;
740         u8 stat, good_stat;
741
742         good_stat = DATA_READY;
743         stat = HWIF(drive)->INB(IDE_STATUS_REG);
744 check_status:
745         if (!OK_STAT(stat, good_stat, BAD_R_STAT)) {
746                 if (stat & (ERR_STAT | DRQ_STAT))
747                         return DRIVER(drive)->error(drive, __FUNCTION__, stat);
748                 /* BUSY_STAT: No data yet, so wait for another IRQ. */
749                 ide_set_handler(drive, &task_in_intr, WAIT_WORSTCASE, NULL);
750                 return ide_started;
751         }
752
753         /*
754          * Complete previously submitted bios (if any).
755          * Status was already verifyied.
756          */
757         while (rq->bio != rq->cbio)
758                 if (!DRIVER(drive)->end_request(drive, 1, bio_sectors(rq->bio)))
759                         return ide_stopped;
760         /* Complete rq->buffer based request (ioctls). */
761         if (!rq->bio && !rq->nr_sectors) {
762                 ide_end_drive_cmd(drive, stat, HWIF(drive)->INB(IDE_ERROR_REG));
763                 return ide_stopped;
764         }
765
766         rq->errors = 0;
767         task_sectors(drive, rq, 1, IDE_PIO_IN);
768
769         /* If it was the last datablock check status and finish transfer. */
770         if (!rq->nr_sectors) {
771                 good_stat = 0;
772                 stat = wait_drive_not_busy(drive);
773                 goto check_status;
774         }
775
776         /* Still data left to transfer. */
777         ide_set_handler(drive, &task_in_intr, WAIT_WORSTCASE, NULL);
778
779         return ide_started;
780 }
781 EXPORT_SYMBOL(task_in_intr);
782
783 /*
784  * Handler for command with PIO data-in phase (Read Multiple).
785  */
786 ide_startstop_t task_mulin_intr (ide_drive_t *drive)
787 {
788         struct request *rq = HWGROUP(drive)->rq;
789         unsigned int msect = drive->mult_count;
790         unsigned int nsect;
791         u8 stat, good_stat;
792
793         good_stat = DATA_READY;
794         stat = HWIF(drive)->INB(IDE_STATUS_REG);
795 check_status:
796         if (!OK_STAT(stat, good_stat, BAD_R_STAT)) {
797                 if (stat & (ERR_STAT | DRQ_STAT))
798                         return DRIVER(drive)->error(drive, __FUNCTION__, stat);
799                 /* BUSY_STAT: No data yet, so wait for another IRQ. */
800                 ide_set_handler(drive, &task_mulin_intr, WAIT_WORSTCASE, NULL);
801                 return ide_started;
802         }
803
804         /*
805          * Complete previously submitted bios (if any).
806          * Status was already verifyied.
807          */
808         while (rq->bio != rq->cbio)
809                 if (!DRIVER(drive)->end_request(drive, 1, bio_sectors(rq->bio)))
810                         return ide_stopped;
811         /* Complete rq->buffer based request (ioctls). */
812         if (!rq->bio && !rq->nr_sectors) {
813                 ide_end_drive_cmd(drive, stat, HWIF(drive)->INB(IDE_ERROR_REG));
814                 return ide_stopped;
815         }
816
817         rq->errors = 0;
818         do {
819                 nsect = rq->current_nr_sectors;
820                 if (nsect > msect)
821                         nsect = msect;
822
823                 task_sectors(drive, rq, nsect, IDE_PIO_IN);
824
825                 if (!rq->nr_sectors)
826                         msect = 0;
827                 else
828                         msect -= nsect;
829         } while (msect);
830
831         /* If it was the last datablock check status and finish transfer. */
832         if (!rq->nr_sectors) {
833                 good_stat = 0;
834                 stat = wait_drive_not_busy(drive);
835                 goto check_status;
836         }
837
838         /* Still data left to transfer. */
839         ide_set_handler(drive, &task_mulin_intr, WAIT_WORSTCASE, NULL);
840
841         return ide_started;
842 }
843 EXPORT_SYMBOL(task_mulin_intr);
844
845 /*
846  * Handler for command with PIO data-out phase (Write).
847  */
848 ide_startstop_t task_out_intr (ide_drive_t *drive)
849 {
850         struct request *rq = HWGROUP(drive)->rq;
851         u8 stat;
852
853         stat = HWIF(drive)->INB(IDE_STATUS_REG);
854         if (!OK_STAT(stat, DRIVE_READY, drive->bad_wstat)) {
855                 if ((stat & (ERR_STAT | DRQ_STAT)) ||
856                     ((stat & WRERR_STAT) && !drive->nowerr))
857                         return DRIVER(drive)->error(drive, __FUNCTION__, stat);
858                 if (stat & BUSY_STAT) {
859                         /* Not ready yet, so wait for another IRQ. */
860                         ide_set_handler(drive, &task_out_intr, WAIT_WORSTCASE, NULL);
861                         return ide_started;
862                 }
863         }
864
865         /* Deal with unexpected ATA data phase. */
866         if ((!(stat & DATA_READY) && rq->nr_sectors) ||
867             ((stat & DATA_READY) && !rq->nr_sectors))
868                 return DRIVER(drive)->error(drive, __FUNCTION__, stat);
869
870         /* 
871          * Complete previously submitted bios (if any).
872          * Status was already verifyied.
873          */
874         while (rq->bio != rq->cbio)
875                 if (!DRIVER(drive)->end_request(drive, 1, bio_sectors(rq->bio)))
876                         return ide_stopped;
877         /* Complete rq->buffer based request (ioctls). */
878         if (!rq->bio && !rq->nr_sectors) {
879                 ide_end_drive_cmd(drive, stat, HWIF(drive)->INB(IDE_ERROR_REG));
880                 return ide_stopped;
881         }
882
883         /* Still data left to transfer. */
884         ide_set_handler(drive, &task_out_intr, WAIT_WORSTCASE, NULL);
885
886         rq->errors = 0;
887         task_sectors(drive, rq, 1, IDE_PIO_OUT);
888
889         return ide_started;
890 }
891
892 EXPORT_SYMBOL(task_out_intr);
893
894 ide_startstop_t pre_task_out_intr (ide_drive_t *drive, struct request *rq)
895 {
896         ide_startstop_t startstop;
897
898         if (ide_wait_stat(&startstop, drive, DATA_READY,
899                           drive->bad_wstat, WAIT_DRQ)) {
900                 printk(KERN_ERR "%s: no DRQ after issuing WRITE%s\n",
901                                 drive->name, drive->addressing ? "_EXT" : "");
902                 return startstop;
903         }
904
905         if (!drive->unmask)
906                 local_irq_disable();
907
908         return task_out_intr(drive);
909 }
910 EXPORT_SYMBOL(pre_task_out_intr);
911
912 /*
913  * Handler for command with PIO data-out phase (Write Multiple).
914  */
915 ide_startstop_t task_mulout_intr (ide_drive_t *drive)
916 {
917         struct request *rq = HWGROUP(drive)->rq;
918         unsigned int msect = drive->mult_count;
919         unsigned int nsect;
920         u8 stat;
921
922         stat = HWIF(drive)->INB(IDE_STATUS_REG);
923         if (!OK_STAT(stat, DRIVE_READY, drive->bad_wstat)) {
924                 if ((stat & (ERR_STAT | DRQ_STAT)) ||
925                     ((stat & WRERR_STAT) && !drive->nowerr))
926                         return DRIVER(drive)->error(drive, __FUNCTION__, stat);
927                 if (stat & BUSY_STAT) {
928                         /* Not ready yet, so wait for another IRQ. */
929                         ide_set_handler(drive, &task_mulout_intr, WAIT_WORSTCASE, NULL);
930                         return ide_started;
931                 }
932         }
933
934         /* Deal with unexpected ATA data phase. */
935         if ((!(stat & DATA_READY) && rq->nr_sectors) ||
936             ((stat & DATA_READY) && !rq->nr_sectors))
937                 return DRIVER(drive)->error(drive, __FUNCTION__, stat);
938
939         /* 
940          * Complete previously submitted bios (if any).
941          * Status was already verifyied.
942          */
943         while (rq->bio != rq->cbio)
944                 if (!DRIVER(drive)->end_request(drive, 1, bio_sectors(rq->bio)))
945                         return ide_stopped;
946         /* Complete rq->buffer based request (ioctls). */
947         if (!rq->bio && !rq->nr_sectors) {
948                 ide_end_drive_cmd(drive, stat, HWIF(drive)->INB(IDE_ERROR_REG));
949                 return ide_stopped;
950         }
951
952         /* Still data left to transfer. */
953         ide_set_handler(drive, &task_mulout_intr, WAIT_WORSTCASE, NULL);
954
955         rq->errors = 0;
956         do {
957                 nsect = rq->current_nr_sectors;
958                 if (nsect > msect)
959                         nsect = msect;
960
961                 task_sectors(drive, rq, nsect, IDE_PIO_OUT);
962
963                 if (!rq->nr_sectors)
964                         msect = 0;
965                 else
966                         msect -= nsect;
967         } while (msect);
968
969         return ide_started;
970 }
971 EXPORT_SYMBOL(task_mulout_intr);
972
973 ide_startstop_t pre_task_mulout_intr (ide_drive_t *drive, struct request *rq)
974 {
975         ide_startstop_t startstop;
976
977         if (ide_wait_stat(&startstop, drive, DATA_READY,
978                           drive->bad_wstat, WAIT_DRQ)) {
979                 printk(KERN_ERR "%s: no DRQ after issuing MULTWRITE%s\n",
980                                 drive->name, drive->addressing ? "_EXT" : "");
981                 return startstop;
982         }
983
984         if (!drive->unmask)
985                 local_irq_disable();
986
987         return task_mulout_intr(drive);
988 }
989 EXPORT_SYMBOL(pre_task_mulout_intr);
990
991 #endif /* !CONFIG_IDE_TASKFILE_IO */
992
993 /* Called by internal to feature out type of command being called */
994 //ide_pre_handler_t * ide_pre_handler_parser (task_struct_t *taskfile, hob_struct_t *hobfile)
995 ide_pre_handler_t * ide_pre_handler_parser (struct hd_drive_task_hdr *taskfile, struct hd_drive_hob_hdr *hobfile)
996 {
997         switch(taskfile->command) {
998                                 /* IDE_DRIVE_TASK_RAW_WRITE */
999                 case CFA_WRITE_MULTI_WO_ERASE:
1000         //      case WIN_WRITE_LONG:
1001         //      case WIN_WRITE_LONG_ONCE:
1002                 case WIN_MULTWRITE:
1003                 case WIN_MULTWRITE_EXT:
1004                         return &pre_task_mulout_intr;
1005                         
1006                                 /* IDE_DRIVE_TASK_OUT */
1007                 case WIN_WRITE:
1008         //      case WIN_WRITE_ONCE:
1009                 case WIN_WRITE_EXT:
1010                 case WIN_WRITE_VERIFY:
1011                 case WIN_WRITE_BUFFER:
1012                 case CFA_WRITE_SECT_WO_ERASE:
1013                 case WIN_DOWNLOAD_MICROCODE:
1014                         return &pre_task_out_intr;
1015                                 /* IDE_DRIVE_TASK_OUT */
1016                 case WIN_SMART:
1017                         if (taskfile->feature == SMART_WRITE_LOG_SECTOR)
1018                                 return &pre_task_out_intr;
1019                 case WIN_WRITEDMA:
1020         //      case WIN_WRITEDMA_ONCE:
1021                 case WIN_WRITEDMA_QUEUED:
1022                 case WIN_WRITEDMA_EXT:
1023                 case WIN_WRITEDMA_QUEUED_EXT:
1024                                 /* IDE_DRIVE_TASK_OUT */
1025                 default:
1026                         break;
1027         }
1028         return(NULL);
1029 }
1030
1031 EXPORT_SYMBOL(ide_pre_handler_parser);
1032
1033 /* Called by internal to feature out type of command being called */
1034 //ide_handler_t * ide_handler_parser (task_struct_t *taskfile, hob_struct_t *hobfile)
1035 ide_handler_t * ide_handler_parser (struct hd_drive_task_hdr *taskfile, struct hd_drive_hob_hdr *hobfile)
1036 {
1037         switch(taskfile->command) {
1038                 case WIN_IDENTIFY:
1039                 case WIN_PIDENTIFY:
1040                 case CFA_TRANSLATE_SECTOR:
1041                 case WIN_READ_BUFFER:
1042                 case WIN_READ:
1043         //      case WIN_READ_ONCE:
1044                 case WIN_READ_EXT:
1045                         return &task_in_intr;
1046                 case WIN_SECURITY_DISABLE:
1047                 case WIN_SECURITY_ERASE_UNIT:
1048                 case WIN_SECURITY_SET_PASS:
1049                 case WIN_SECURITY_UNLOCK:
1050                 case WIN_DOWNLOAD_MICROCODE:
1051                 case CFA_WRITE_SECT_WO_ERASE:
1052                 case WIN_WRITE_BUFFER:
1053                 case WIN_WRITE_VERIFY:
1054                 case WIN_WRITE:
1055         //      case WIN_WRITE_ONCE:    
1056                 case WIN_WRITE_EXT:
1057                         return &task_out_intr;
1058         //      case WIN_READ_LONG:
1059         //      case WIN_READ_LONG_ONCE:
1060                 case WIN_MULTREAD:
1061                 case WIN_MULTREAD_EXT:
1062                         return &task_mulin_intr;
1063         //      case WIN_WRITE_LONG:
1064         //      case WIN_WRITE_LONG_ONCE:
1065                 case CFA_WRITE_MULTI_WO_ERASE:
1066                 case WIN_MULTWRITE:
1067                 case WIN_MULTWRITE_EXT:
1068                         return &task_mulout_intr;
1069                 case WIN_SMART:
1070                         switch(taskfile->feature) {
1071                                 case SMART_READ_VALUES:
1072                                 case SMART_READ_THRESHOLDS:
1073                                 case SMART_READ_LOG_SECTOR:
1074                                         return &task_in_intr;
1075                                 case SMART_WRITE_LOG_SECTOR:
1076                                         return &task_out_intr;
1077                                 default:
1078                                         return &task_no_data_intr;
1079                         }
1080                 case CFA_REQ_EXT_ERROR_CODE:
1081                 case CFA_ERASE_SECTORS:
1082                 case WIN_VERIFY:
1083         //      case WIN_VERIFY_ONCE:
1084                 case WIN_VERIFY_EXT:
1085                 case WIN_SEEK:
1086                         return &task_no_data_intr;
1087                 case WIN_SPECIFY:
1088                         return &set_geometry_intr;
1089                 case WIN_RECAL:
1090         //      case WIN_RESTORE:
1091                         return &recal_intr;
1092                 case WIN_NOP:
1093                 case WIN_DIAGNOSE:
1094                 case WIN_FLUSH_CACHE:
1095                 case WIN_FLUSH_CACHE_EXT:
1096                 case WIN_STANDBYNOW1:
1097                 case WIN_STANDBYNOW2:
1098                 case WIN_SLEEPNOW1:
1099                 case WIN_SLEEPNOW2:
1100                 case WIN_SETIDLE1:
1101                 case WIN_CHECKPOWERMODE1:
1102                 case WIN_CHECKPOWERMODE2:
1103                 case WIN_GETMEDIASTATUS:
1104                 case WIN_MEDIAEJECT:
1105                         return &task_no_data_intr;
1106                 case WIN_SETMULT:
1107                         return &set_multmode_intr;
1108                 case WIN_READ_NATIVE_MAX:
1109                 case WIN_SET_MAX:
1110                 case WIN_READ_NATIVE_MAX_EXT:
1111                 case WIN_SET_MAX_EXT:
1112                 case WIN_SECURITY_ERASE_PREPARE:
1113                 case WIN_SECURITY_FREEZE_LOCK:
1114                 case WIN_DOORLOCK:
1115                 case WIN_DOORUNLOCK:
1116                 case WIN_SETFEATURES:
1117                         return &task_no_data_intr;
1118                 case DISABLE_SEAGATE:
1119                 case EXABYTE_ENABLE_NEST:
1120                         return &task_no_data_intr;
1121                 case WIN_READDMA:
1122         //      case WIN_READDMA_ONCE:
1123                 case WIN_IDENTIFY_DMA:
1124                 case WIN_READDMA_QUEUED:
1125                 case WIN_READDMA_EXT:
1126                 case WIN_READDMA_QUEUED_EXT:
1127                 case WIN_WRITEDMA:
1128         //      case WIN_WRITEDMA_ONCE:
1129                 case WIN_WRITEDMA_QUEUED:
1130                 case WIN_WRITEDMA_EXT:
1131                 case WIN_WRITEDMA_QUEUED_EXT:
1132                 case WIN_FORMAT:
1133                 case WIN_INIT:
1134                 case WIN_DEVICE_RESET:
1135                 case WIN_QUEUED_SERVICE:
1136                 case WIN_PACKETCMD:
1137                 default:
1138                         return(NULL);
1139         }       
1140 }
1141
1142 EXPORT_SYMBOL(ide_handler_parser);
1143
1144 ide_post_handler_t * ide_post_handler_parser (struct hd_drive_task_hdr *taskfile, struct hd_drive_hob_hdr *hobfile)
1145 {
1146         switch(taskfile->command) {
1147                 case WIN_SPECIFY:       /* set_geometry_intr */
1148                 case WIN_RESTORE:       /* recal_intr */
1149                 case WIN_SETMULT:       /* set_multmode_intr */
1150                 default:
1151                         return(NULL);
1152         }
1153 }
1154
1155 EXPORT_SYMBOL(ide_post_handler_parser);
1156
1157 /* Called by ioctl to feature out type of command being called */
1158 int ide_cmd_type_parser (ide_task_t *args)
1159 {
1160
1161         task_struct_t *taskfile = (task_struct_t *) args->tfRegister;
1162         hob_struct_t *hobfile   = (hob_struct_t *) args->hobRegister;
1163
1164         args->prehandler        = ide_pre_handler_parser(taskfile, hobfile);
1165         args->handler           = ide_handler_parser(taskfile, hobfile);
1166         args->posthandler       = ide_post_handler_parser(taskfile, hobfile);
1167
1168         switch(args->tfRegister[IDE_COMMAND_OFFSET]) {
1169                 case WIN_IDENTIFY:
1170                 case WIN_PIDENTIFY:
1171                         return IDE_DRIVE_TASK_IN;
1172                 case CFA_TRANSLATE_SECTOR:
1173                 case WIN_READ:
1174         //      case WIN_READ_ONCE:
1175                 case WIN_READ_EXT:
1176                 case WIN_READ_BUFFER:
1177                         return IDE_DRIVE_TASK_IN;
1178                 case WIN_WRITE:
1179         //      case WIN_WRITE_ONCE:
1180                 case WIN_WRITE_EXT:
1181                 case WIN_WRITE_VERIFY:
1182                 case WIN_WRITE_BUFFER:
1183                 case CFA_WRITE_SECT_WO_ERASE:
1184                 case WIN_DOWNLOAD_MICROCODE:
1185                         return IDE_DRIVE_TASK_RAW_WRITE;
1186         //      case WIN_READ_LONG:
1187         //      case WIN_READ_LONG_ONCE:
1188                 case WIN_MULTREAD:
1189                 case WIN_MULTREAD_EXT:
1190                         return IDE_DRIVE_TASK_IN;
1191         //      case WIN_WRITE_LONG:
1192         //      case WIN_WRITE_LONG_ONCE:
1193                 case CFA_WRITE_MULTI_WO_ERASE:
1194                 case WIN_MULTWRITE:
1195                 case WIN_MULTWRITE_EXT:
1196                         return IDE_DRIVE_TASK_RAW_WRITE;
1197                 case WIN_SECURITY_DISABLE:
1198                 case WIN_SECURITY_ERASE_UNIT:
1199                 case WIN_SECURITY_SET_PASS:
1200                 case WIN_SECURITY_UNLOCK:
1201                         return IDE_DRIVE_TASK_OUT;
1202                 case WIN_SMART:
1203                         args->tfRegister[IDE_LCYL_OFFSET] = SMART_LCYL_PASS;
1204                         args->tfRegister[IDE_HCYL_OFFSET] = SMART_HCYL_PASS;
1205                         switch(args->tfRegister[IDE_FEATURE_OFFSET]) {
1206                                 case SMART_READ_VALUES:
1207                                 case SMART_READ_THRESHOLDS:
1208                                 case SMART_READ_LOG_SECTOR:
1209                                         return IDE_DRIVE_TASK_IN;
1210                                 case SMART_WRITE_LOG_SECTOR:
1211                                         return IDE_DRIVE_TASK_OUT;
1212                                 default:
1213                                         return IDE_DRIVE_TASK_NO_DATA;
1214                         }
1215                 case WIN_READDMA:
1216         //      case WIN_READDMA_ONCE:
1217                 case WIN_IDENTIFY_DMA:
1218                 case WIN_READDMA_QUEUED:
1219                 case WIN_READDMA_EXT:
1220                 case WIN_READDMA_QUEUED_EXT:
1221                         return IDE_DRIVE_TASK_IN;
1222                 case WIN_WRITEDMA:
1223         //      case WIN_WRITEDMA_ONCE:
1224                 case WIN_WRITEDMA_QUEUED:
1225                 case WIN_WRITEDMA_EXT:
1226                 case WIN_WRITEDMA_QUEUED_EXT:
1227                         return IDE_DRIVE_TASK_RAW_WRITE;
1228                 case WIN_SETFEATURES:
1229                         switch(args->tfRegister[IDE_FEATURE_OFFSET]) {
1230                                 case SETFEATURES_EN_8BIT:
1231                                 case SETFEATURES_EN_WCACHE:
1232                                         return IDE_DRIVE_TASK_NO_DATA;
1233                                 case SETFEATURES_XFER:
1234                                         return IDE_DRIVE_TASK_SET_XFER;
1235                                 case SETFEATURES_DIS_DEFECT:
1236                                 case SETFEATURES_EN_APM:
1237                                 case SETFEATURES_DIS_MSN:
1238                                 case SETFEATURES_DIS_RETRY:
1239                                 case SETFEATURES_EN_AAM:
1240                                 case SETFEATURES_RW_LONG:
1241                                 case SETFEATURES_SET_CACHE:
1242                                 case SETFEATURES_DIS_RLA:
1243                                 case SETFEATURES_EN_RI:
1244                                 case SETFEATURES_EN_SI:
1245                                 case SETFEATURES_DIS_RPOD:
1246                                 case SETFEATURES_DIS_WCACHE:
1247                                 case SETFEATURES_EN_DEFECT:
1248                                 case SETFEATURES_DIS_APM:
1249                                 case SETFEATURES_EN_ECC:
1250                                 case SETFEATURES_EN_MSN:
1251                                 case SETFEATURES_EN_RETRY:
1252                                 case SETFEATURES_EN_RLA:
1253                                 case SETFEATURES_PREFETCH:
1254                                 case SETFEATURES_4B_RW_LONG:
1255                                 case SETFEATURES_DIS_AAM:
1256                                 case SETFEATURES_EN_RPOD:
1257                                 case SETFEATURES_DIS_RI:
1258                                 case SETFEATURES_DIS_SI:
1259                                 default:
1260                                         return IDE_DRIVE_TASK_NO_DATA;
1261                         }
1262                 case WIN_NOP:
1263                 case CFA_REQ_EXT_ERROR_CODE:
1264                 case CFA_ERASE_SECTORS:
1265                 case WIN_VERIFY:
1266         //      case WIN_VERIFY_ONCE:
1267                 case WIN_VERIFY_EXT:
1268                 case WIN_SEEK:
1269                 case WIN_SPECIFY:
1270                 case WIN_RESTORE:
1271                 case WIN_DIAGNOSE:
1272                 case WIN_FLUSH_CACHE:
1273                 case WIN_FLUSH_CACHE_EXT:
1274                 case WIN_STANDBYNOW1:
1275                 case WIN_STANDBYNOW2:
1276                 case WIN_SLEEPNOW1:
1277                 case WIN_SLEEPNOW2:
1278                 case WIN_SETIDLE1:
1279                 case DISABLE_SEAGATE:
1280                 case WIN_CHECKPOWERMODE1:
1281                 case WIN_CHECKPOWERMODE2:
1282                 case WIN_GETMEDIASTATUS:
1283                 case WIN_MEDIAEJECT:
1284                 case WIN_SETMULT:
1285                 case WIN_READ_NATIVE_MAX:
1286                 case WIN_SET_MAX:
1287                 case WIN_READ_NATIVE_MAX_EXT:
1288                 case WIN_SET_MAX_EXT:
1289                 case WIN_SECURITY_ERASE_PREPARE:
1290                 case WIN_SECURITY_FREEZE_LOCK:
1291                 case EXABYTE_ENABLE_NEST:
1292                 case WIN_DOORLOCK:
1293                 case WIN_DOORUNLOCK:
1294                         return IDE_DRIVE_TASK_NO_DATA;
1295                 case WIN_FORMAT:
1296                 case WIN_INIT:
1297                 case WIN_DEVICE_RESET:
1298                 case WIN_QUEUED_SERVICE:
1299                 case WIN_PACKETCMD:
1300                 default:
1301                         return IDE_DRIVE_TASK_INVALID;
1302         }
1303 }
1304
1305 EXPORT_SYMBOL(ide_cmd_type_parser);
1306
1307 /*
1308  * This function is intended to be used prior to invoking ide_do_drive_cmd().
1309  */
1310 void ide_init_drive_taskfile (struct request *rq)
1311 {
1312         memset(rq, 0, sizeof(*rq));
1313         rq->flags = REQ_DRIVE_TASKFILE;
1314 }
1315
1316 EXPORT_SYMBOL(ide_init_drive_taskfile);
1317
1318 int ide_diag_taskfile (ide_drive_t *drive, ide_task_t *args, unsigned long data_size, u8 *buf)
1319 {
1320         struct request rq;
1321
1322         ide_init_drive_taskfile(&rq);
1323         rq.flags = REQ_DRIVE_TASKFILE;
1324         rq.buffer = buf;
1325
1326         /*
1327          * (ks) We transfer currently only whole sectors.
1328          * This is suffient for now.  But, it would be great,
1329          * if we would find a solution to transfer any size.
1330          * To support special commands like READ LONG.
1331          */
1332         if (args->command_type != IDE_DRIVE_TASK_NO_DATA) {
1333                 if (data_size == 0)
1334                         rq.nr_sectors = (args->hobRegister[IDE_NSECTOR_OFFSET_HOB] << 8) | args->tfRegister[IDE_NSECTOR_OFFSET];
1335                 else
1336                         rq.nr_sectors = data_size / SECTOR_SIZE;
1337
1338                 rq.hard_nr_sectors = rq.nr_sectors;
1339                 rq.hard_cur_sectors = rq.current_nr_sectors = rq.nr_sectors;
1340         }
1341
1342         if (args->tf_out_flags.all == 0) {
1343                 /*
1344                  * clean up kernel settings for driver sanity, regardless.
1345                  * except for discrete diag services.
1346                  */
1347                 args->posthandler = ide_post_handler_parser(
1348                                 (struct hd_drive_task_hdr *) args->tfRegister,
1349                                 (struct hd_drive_hob_hdr *) args->hobRegister);
1350
1351         }
1352         rq.special = args;
1353         return ide_do_drive_cmd(drive, &rq, ide_wait);
1354 }
1355
1356 EXPORT_SYMBOL(ide_diag_taskfile);
1357
1358 int ide_raw_taskfile (ide_drive_t *drive, ide_task_t *args, u8 *buf)
1359 {
1360         return ide_diag_taskfile(drive, args, 0, buf);
1361 }
1362
1363 EXPORT_SYMBOL(ide_raw_taskfile);
1364         
1365 #ifdef CONFIG_IDE_TASK_IOCTL_DEBUG
1366 char * ide_ioctl_verbose (unsigned int cmd)
1367 {
1368         return("unknown");
1369 }
1370
1371 char * ide_task_cmd_verbose (u8 task)
1372 {
1373         return("unknown");
1374 }
1375 #endif /* CONFIG_IDE_TASK_IOCTL_DEBUG */
1376
1377 #define MAX_DMA         (256*SECTOR_WORDS)
1378
1379 ide_startstop_t flagged_taskfile(ide_drive_t *, ide_task_t *);
1380 ide_startstop_t flagged_task_no_data_intr(ide_drive_t *);
1381 ide_startstop_t flagged_task_in_intr(ide_drive_t *);
1382 ide_startstop_t flagged_task_mulin_intr(ide_drive_t *);
1383 ide_startstop_t flagged_pre_task_out_intr(ide_drive_t *, struct request *);
1384 ide_startstop_t flagged_task_out_intr(ide_drive_t *);
1385 ide_startstop_t flagged_pre_task_mulout_intr(ide_drive_t *, struct request *);
1386 ide_startstop_t flagged_task_mulout_intr(ide_drive_t *);
1387
1388 int ide_taskfile_ioctl (ide_drive_t *drive, unsigned int cmd, unsigned long arg)
1389 {
1390         ide_task_request_t      *req_task;
1391         ide_task_t              args;
1392         u8 *outbuf              = NULL;
1393         u8 *inbuf               = NULL;
1394         task_ioreg_t *argsptr   = args.tfRegister;
1395         task_ioreg_t *hobsptr   = args.hobRegister;
1396         int err                 = 0;
1397         int tasksize            = sizeof(struct ide_task_request_s);
1398         int taskin              = 0;
1399         int taskout             = 0;
1400         u8 io_32bit             = drive->io_32bit;
1401
1402 //      printk("IDE Taskfile ...\n");
1403
1404         req_task = kmalloc(tasksize, GFP_KERNEL);
1405         if (req_task == NULL) return -ENOMEM;
1406         memset(req_task, 0, tasksize);
1407         if (copy_from_user(req_task, (void *) arg, tasksize)) {
1408                 kfree(req_task);
1409                 return -EFAULT;
1410         }
1411
1412         taskout = (int) req_task->out_size;
1413         taskin  = (int) req_task->in_size;
1414
1415         if (taskout) {
1416                 int outtotal = tasksize;
1417                 outbuf = kmalloc(taskout, GFP_KERNEL);
1418                 if (outbuf == NULL) {
1419                         err = -ENOMEM;
1420                         goto abort;
1421                 }
1422                 memset(outbuf, 0, taskout);
1423                 if (copy_from_user(outbuf, (void *)arg + outtotal, taskout)) {
1424                         err = -EFAULT;
1425                         goto abort;
1426                 }
1427         }
1428
1429         if (taskin) {
1430                 int intotal = tasksize + taskout;
1431                 inbuf = kmalloc(taskin, GFP_KERNEL);
1432                 if (inbuf == NULL) {
1433                         err = -ENOMEM;
1434                         goto abort;
1435                 }
1436                 memset(inbuf, 0, taskin);
1437                 if (copy_from_user(inbuf, (void *)arg + intotal , taskin)) {
1438                         err = -EFAULT;
1439                         goto abort;
1440                 }
1441         }
1442
1443         memset(&args, 0, sizeof(ide_task_t));
1444         memcpy(argsptr, req_task->io_ports, HDIO_DRIVE_TASK_HDR_SIZE);
1445         memcpy(hobsptr, req_task->hob_ports, HDIO_DRIVE_HOB_HDR_SIZE);
1446
1447         args.tf_in_flags  = req_task->in_flags;
1448         args.tf_out_flags = req_task->out_flags;
1449         args.data_phase   = req_task->data_phase;
1450         args.command_type = req_task->req_cmd;
1451
1452 #ifdef CONFIG_IDE_TASK_IOCTL_DEBUG
1453         DTF("%s: ide_ioctl_cmd %s:  ide_task_cmd %s\n",
1454                 drive->name,
1455                 ide_ioctl_verbose(cmd),
1456                 ide_task_cmd_verbose(args.tfRegister[IDE_COMMAND_OFFSET]));
1457 #endif /* CONFIG_IDE_TASK_IOCTL_DEBUG */
1458
1459         drive->io_32bit = 0;
1460         switch(req_task->data_phase) {
1461                 case TASKFILE_OUT_DMAQ:
1462                 case TASKFILE_OUT_DMA:
1463                         err = ide_diag_taskfile(drive, &args, taskout, outbuf);
1464                         break;
1465                 case TASKFILE_IN_DMAQ:
1466                 case TASKFILE_IN_DMA:
1467                         err = ide_diag_taskfile(drive, &args, taskin, inbuf);
1468                         break;
1469                 case TASKFILE_IN_OUT:
1470 #if 0
1471                         args.prehandler = &pre_task_out_intr;
1472                         args.handler = &task_out_intr;
1473                         args.posthandler = NULL;
1474                         err = ide_diag_taskfile(drive, &args, taskout, outbuf);
1475                         args.prehandler = NULL;
1476                         args.handler = &task_in_intr;
1477                         args.posthandler = NULL;
1478                         err = ide_diag_taskfile(drive, &args, taskin, inbuf);
1479                         break;
1480 #else
1481                         err = -EFAULT;
1482                         goto abort;
1483 #endif
1484                 case TASKFILE_MULTI_OUT:
1485                         if (!drive->mult_count) {
1486                                 /* (hs): give up if multcount is not set */
1487                                 printk(KERN_ERR "%s: %s Multimode Write " \
1488                                         "multcount is not set\n",
1489                                         drive->name, __FUNCTION__);
1490                                 err = -EPERM;
1491                                 goto abort;
1492                         }
1493                         if (args.tf_out_flags.all != 0) {
1494                                 args.prehandler = &flagged_pre_task_mulout_intr;
1495                                 args.handler = &flagged_task_mulout_intr;
1496                         } else {
1497                                 args.prehandler = &pre_task_mulout_intr;
1498                                 args.handler = &task_mulout_intr;
1499                         }
1500                         err = ide_diag_taskfile(drive, &args, taskout, outbuf);
1501                         break;
1502                 case TASKFILE_OUT:
1503                         if (args.tf_out_flags.all != 0) {
1504                                 args.prehandler = &flagged_pre_task_out_intr;
1505                                 args.handler    = &flagged_task_out_intr;
1506                         } else {
1507                                 args.prehandler = &pre_task_out_intr;
1508                                 args.handler = &task_out_intr;
1509                         }
1510                         err = ide_diag_taskfile(drive, &args, taskout, outbuf);
1511                         break;
1512                 case TASKFILE_MULTI_IN:
1513                         if (!drive->mult_count) {
1514                                 /* (hs): give up if multcount is not set */
1515                                 printk(KERN_ERR "%s: %s Multimode Read failure " \
1516                                         "multcount is not set\n",
1517                                         drive->name, __FUNCTION__);
1518                                 err = -EPERM;
1519                                 goto abort;
1520                         }
1521                         if (args.tf_out_flags.all != 0) {
1522                                 args.handler = &flagged_task_mulin_intr;
1523                         } else {
1524                                 args.handler = &task_mulin_intr;
1525                         }
1526                         err = ide_diag_taskfile(drive, &args, taskin, inbuf);
1527                         break;
1528                 case TASKFILE_IN:
1529                         if (args.tf_out_flags.all != 0) {
1530                                 args.handler = &flagged_task_in_intr;
1531                         } else {
1532                                 args.handler = &task_in_intr;
1533                         }
1534                         err = ide_diag_taskfile(drive, &args, taskin, inbuf);
1535                         break;
1536                 case TASKFILE_NO_DATA:
1537                         if (args.tf_out_flags.all != 0) {
1538                                 args.handler = &flagged_task_no_data_intr;
1539                         } else {
1540                                 args.handler = &task_no_data_intr;
1541                         }
1542                         err = ide_diag_taskfile(drive, &args, 0, NULL);
1543                         break;
1544                 default:
1545                         err = -EFAULT;
1546                         goto abort;
1547         }
1548
1549         memcpy(req_task->io_ports, &(args.tfRegister), HDIO_DRIVE_TASK_HDR_SIZE);
1550         memcpy(req_task->hob_ports, &(args.hobRegister), HDIO_DRIVE_HOB_HDR_SIZE);
1551         req_task->in_flags  = args.tf_in_flags;
1552         req_task->out_flags = args.tf_out_flags;
1553
1554         if (copy_to_user((void *)arg, req_task, tasksize)) {
1555                 err = -EFAULT;
1556                 goto abort;
1557         }
1558         if (taskout) {
1559                 int outtotal = tasksize;
1560                 if (copy_to_user((void *)arg+outtotal, outbuf, taskout)) {
1561                         err = -EFAULT;
1562                         goto abort;
1563                 }
1564         }
1565         if (taskin) {
1566                 int intotal = tasksize + taskout;
1567                 if (copy_to_user((void *)arg+intotal, inbuf, taskin)) {
1568                         err = -EFAULT;
1569                         goto abort;
1570                 }
1571         }
1572 abort:
1573         kfree(req_task);
1574         if (outbuf != NULL)
1575                 kfree(outbuf);
1576         if (inbuf != NULL)
1577                 kfree(inbuf);
1578
1579 //      printk("IDE Taskfile ioctl ended. rc = %i\n", err);
1580
1581         drive->io_32bit = io_32bit;
1582
1583         return err;
1584 }
1585
1586 EXPORT_SYMBOL(ide_taskfile_ioctl);
1587
1588 int ide_wait_cmd (ide_drive_t *drive, u8 cmd, u8 nsect, u8 feature, u8 sectors, u8 *buf)
1589 {
1590         struct request rq;
1591         u8 buffer[4];
1592
1593         if (!buf)
1594                 buf = buffer;
1595         memset(buf, 0, 4 + SECTOR_WORDS * 4 * sectors);
1596         ide_init_drive_cmd(&rq);
1597         rq.buffer = buf;
1598         *buf++ = cmd;
1599         *buf++ = nsect;
1600         *buf++ = feature;
1601         *buf++ = sectors;
1602         return ide_do_drive_cmd(drive, &rq, ide_wait);
1603 }
1604
1605 EXPORT_SYMBOL(ide_wait_cmd);
1606
1607 /*
1608  * FIXME : this needs to map into at taskfile. <andre@linux-ide.org>
1609  */
1610 int ide_cmd_ioctl (ide_drive_t *drive, unsigned int cmd, unsigned long arg)
1611 {
1612         int err = 0;
1613         u8 args[4], *argbuf = args;
1614         u8 xfer_rate = 0;
1615         int argsize = 4;
1616         ide_task_t tfargs;
1617
1618         if (NULL == (void *) arg) {
1619                 struct request rq;
1620                 ide_init_drive_cmd(&rq);
1621                 return ide_do_drive_cmd(drive, &rq, ide_wait);
1622         }
1623
1624         if (copy_from_user(args, (void *)arg, 4))
1625                 return -EFAULT;
1626
1627         memset(&tfargs, 0, sizeof(ide_task_t));
1628         tfargs.tfRegister[IDE_FEATURE_OFFSET] = args[2];
1629         tfargs.tfRegister[IDE_NSECTOR_OFFSET] = args[3];
1630         tfargs.tfRegister[IDE_SECTOR_OFFSET]  = args[1];
1631         tfargs.tfRegister[IDE_LCYL_OFFSET]    = 0x00;
1632         tfargs.tfRegister[IDE_HCYL_OFFSET]    = 0x00;
1633         tfargs.tfRegister[IDE_SELECT_OFFSET]  = 0x00;
1634         tfargs.tfRegister[IDE_COMMAND_OFFSET] = args[0];
1635
1636         if (args[3]) {
1637                 argsize = 4 + (SECTOR_WORDS * 4 * args[3]);
1638                 argbuf = kmalloc(argsize, GFP_KERNEL);
1639                 if (argbuf == NULL)
1640                         return -ENOMEM;
1641                 memcpy(argbuf, args, 4);
1642         }
1643         if (set_transfer(drive, &tfargs)) {
1644                 xfer_rate = args[1];
1645                 if (ide_ata66_check(drive, &tfargs))
1646                         goto abort;
1647         }
1648
1649         err = ide_wait_cmd(drive, args[0], args[1], args[2], args[3], argbuf);
1650
1651         if (!err && xfer_rate) {
1652                 /* active-retuning-calls future */
1653                 ide_set_xfer_rate(drive, xfer_rate);
1654                 ide_driveid_update(drive);
1655         }
1656 abort:
1657         if (copy_to_user((void *)arg, argbuf, argsize))
1658                 err = -EFAULT;
1659         if (argsize > 4)
1660                 kfree(argbuf);
1661         return err;
1662 }
1663
1664 EXPORT_SYMBOL(ide_cmd_ioctl);
1665
1666 int ide_wait_cmd_task (ide_drive_t *drive, u8 *buf)
1667 {
1668         struct request rq;
1669
1670         ide_init_drive_cmd(&rq);
1671         rq.flags = REQ_DRIVE_TASK;
1672         rq.buffer = buf;
1673         return ide_do_drive_cmd(drive, &rq, ide_wait);
1674 }
1675
1676 EXPORT_SYMBOL(ide_wait_cmd_task);
1677
1678 /*
1679  * FIXME : this needs to map into at taskfile. <andre@linux-ide.org>
1680  */
1681 int ide_task_ioctl (ide_drive_t *drive, unsigned int cmd, unsigned long arg)
1682 {
1683         int err = 0;
1684         u8 args[7], *argbuf = args;
1685         int argsize = 7;
1686
1687         if (copy_from_user(args, (void *)arg, 7))
1688                 return -EFAULT;
1689         err = ide_wait_cmd_task(drive, argbuf);
1690         if (copy_to_user((void *)arg, argbuf, argsize))
1691                 err = -EFAULT;
1692         return err;
1693 }
1694
1695 EXPORT_SYMBOL(ide_task_ioctl);
1696
1697 /*
1698  * NOTICE: This is additions from IBM to provide a discrete interface,
1699  * for selective taskregister access operations.  Nice JOB Klaus!!!
1700  * Glad to be able to work and co-develop this with you and IBM.
1701  */
1702 ide_startstop_t flagged_taskfile (ide_drive_t *drive, ide_task_t *task)
1703 {
1704         ide_hwif_t *hwif        = HWIF(drive);
1705         task_struct_t *taskfile = (task_struct_t *) task->tfRegister;
1706         hob_struct_t *hobfile   = (hob_struct_t *) task->hobRegister;
1707 #if DEBUG_TASKFILE
1708         u8 status;
1709 #endif
1710
1711
1712 #ifdef CONFIG_IDE_TASK_IOCTL_DEBUG
1713         void debug_taskfile(drive, task);
1714 #endif /* CONFIG_IDE_TASK_IOCTL_DEBUG */
1715
1716         /*
1717          * (ks) Check taskfile in/out flags.
1718          * If set, then execute as it is defined.
1719          * If not set, then define default settings.
1720          * The default values are:
1721          *      write and read all taskfile registers (except data) 
1722          *      write and read the hob registers (sector,nsector,lcyl,hcyl)
1723          */
1724         if (task->tf_out_flags.all == 0) {
1725                 task->tf_out_flags.all = IDE_TASKFILE_STD_OUT_FLAGS;
1726                 if (drive->addressing == 1)
1727                         task->tf_out_flags.all |= (IDE_HOB_STD_OUT_FLAGS << 8);
1728         }
1729
1730         if (task->tf_in_flags.all == 0) {
1731                 task->tf_in_flags.all = IDE_TASKFILE_STD_IN_FLAGS;
1732                 if (drive->addressing == 1)
1733                         task->tf_in_flags.all |= (IDE_HOB_STD_IN_FLAGS  << 8);
1734         }
1735
1736         /* ALL Command Block Executions SHALL clear nIEN, unless otherwise */
1737         if (IDE_CONTROL_REG)
1738                 /* clear nIEN */
1739                 hwif->OUTB(drive->ctl, IDE_CONTROL_REG);
1740         SELECT_MASK(drive, 0);
1741
1742 #if DEBUG_TASKFILE
1743         status = hwif->INB(IDE_STATUS_REG);
1744         if (status & 0x80) {
1745                 printk("flagged_taskfile -> Bad status. Status = %02x. wait 100 usec ...\n", status);
1746                 udelay(100);
1747                 status = hwif->INB(IDE_STATUS_REG);
1748                 printk("flagged_taskfile -> Status = %02x\n", status);
1749         }
1750 #endif
1751
1752         if (task->tf_out_flags.b.data) {
1753                 u16 data =  taskfile->data + (hobfile->data << 8);
1754                 hwif->OUTW(data, IDE_DATA_REG);
1755         }
1756
1757         /* (ks) send hob registers first */
1758         if (task->tf_out_flags.b.nsector_hob)
1759                 hwif->OUTB(hobfile->sector_count, IDE_NSECTOR_REG);
1760         if (task->tf_out_flags.b.sector_hob)
1761                 hwif->OUTB(hobfile->sector_number, IDE_SECTOR_REG);
1762         if (task->tf_out_flags.b.lcyl_hob)
1763                 hwif->OUTB(hobfile->low_cylinder, IDE_LCYL_REG);
1764         if (task->tf_out_flags.b.hcyl_hob)
1765                 hwif->OUTB(hobfile->high_cylinder, IDE_HCYL_REG);
1766
1767         /* (ks) Send now the standard registers */
1768         if (task->tf_out_flags.b.error_feature)
1769                 hwif->OUTB(taskfile->feature, IDE_FEATURE_REG);
1770         /* refers to number of sectors to transfer */
1771         if (task->tf_out_flags.b.nsector)
1772                 hwif->OUTB(taskfile->sector_count, IDE_NSECTOR_REG);
1773         /* refers to sector offset or start sector */
1774         if (task->tf_out_flags.b.sector)
1775                 hwif->OUTB(taskfile->sector_number, IDE_SECTOR_REG);
1776         if (task->tf_out_flags.b.lcyl)
1777                 hwif->OUTB(taskfile->low_cylinder, IDE_LCYL_REG);
1778         if (task->tf_out_flags.b.hcyl)
1779                 hwif->OUTB(taskfile->high_cylinder, IDE_HCYL_REG);
1780
1781         /*
1782          * (ks) In the flagged taskfile approch, we will used all specified
1783          * registers and the register value will not be changed. Except the
1784          * select bit (master/slave) in the drive_head register. We must make
1785          * sure that the desired drive is selected.
1786          */
1787         hwif->OUTB(taskfile->device_head | drive->select.all, IDE_SELECT_REG);
1788         switch(task->data_phase) {
1789
1790                 case TASKFILE_OUT_DMAQ:
1791                 case TASKFILE_OUT_DMA:
1792                         hwif->ide_dma_write(drive);
1793                         break;
1794
1795                 case TASKFILE_IN_DMAQ:
1796                 case TASKFILE_IN_DMA:
1797                         hwif->ide_dma_read(drive);
1798                         break;
1799
1800                 default:
1801                         if (task->handler == NULL)
1802                                 return ide_stopped;
1803
1804                         /* Issue the command */
1805                         ide_execute_command(drive, taskfile->command, task->handler, WAIT_WORSTCASE, NULL);
1806                         if (task->prehandler != NULL)
1807                                 return task->prehandler(drive, HWGROUP(drive)->rq);
1808         }
1809
1810         return ide_started;
1811 }
1812
1813 EXPORT_SYMBOL(flagged_taskfile);
1814
1815 ide_startstop_t flagged_task_no_data_intr (ide_drive_t *drive)
1816 {
1817         ide_hwif_t *hwif = HWIF(drive);
1818         u8 stat;
1819
1820         local_irq_enable();
1821
1822         if (!OK_STAT(stat = hwif->INB(IDE_STATUS_REG), READY_STAT, BAD_STAT)) {
1823                 if (stat & ERR_STAT) {
1824                         return DRIVER(drive)->error(drive, "flagged_task_no_data_intr", stat);
1825                 }
1826                 /*
1827                  * (ks) Unexpected ATA data phase detected.
1828                  * This should not happen. But, it can !
1829                  * I am not sure, which function is best to clean up
1830                  * this situation.  I choose: ide_error(...)
1831                  */
1832                 return DRIVER(drive)->error(drive, "flagged_task_no_data_intr (unexpected phase)", stat); 
1833         }
1834
1835         ide_end_drive_cmd(drive, stat, hwif->INB(IDE_ERROR_REG));
1836
1837         return ide_stopped;
1838 }
1839
1840 /*
1841  * Handler for command with PIO data-in phase
1842  */
1843 ide_startstop_t flagged_task_in_intr (ide_drive_t *drive)
1844 {
1845         ide_hwif_t *hwif        = HWIF(drive);
1846         u8 stat                 = hwif->INB(IDE_STATUS_REG);
1847         struct request *rq      = HWGROUP(drive)->rq;
1848         char *pBuf              = NULL;
1849         int retries             = 5;
1850
1851         if (rq->current_nr_sectors == 0) 
1852                 return DRIVER(drive)->error(drive, "flagged_task_in_intr (no data requested)", stat); 
1853
1854         if (!OK_STAT(stat, DATA_READY, BAD_R_STAT)) {
1855                 if (stat & ERR_STAT) {
1856                         return DRIVER(drive)->error(drive, "flagged_task_in_intr", stat);
1857                 }
1858                 /*
1859                  * (ks) Unexpected ATA data phase detected.
1860                  * This should not happen. But, it can !
1861                  * I am not sure, which function is best to clean up
1862                  * this situation.  I choose: ide_error(...)
1863                  */
1864                 return DRIVER(drive)->error(drive, "flagged_task_in_intr (unexpected data phase)", stat); 
1865         }
1866
1867         pBuf = rq->buffer + ((rq->nr_sectors - rq->current_nr_sectors) * SECTOR_SIZE);
1868         DTF("Read - rq->current_nr_sectors: %d, status: %02x\n", (int) rq->current_nr_sectors, stat);
1869
1870         taskfile_input_data(drive, pBuf, SECTOR_WORDS);
1871
1872         if (--rq->current_nr_sectors != 0) {
1873                 /*
1874                  * (ks) We don't know which command was executed. 
1875                  * So, we wait the 'WORSTCASE' value.
1876                  */
1877                 ide_set_handler(drive, &flagged_task_in_intr,  WAIT_WORSTCASE, NULL);
1878                 return ide_started;
1879         }
1880         /*
1881          * (ks) Last sector was transfered, wait until drive is ready. 
1882          * This can take up to 10 usec. We willl wait max 50 us.
1883          */
1884         while (((stat = hwif->INB(IDE_STATUS_REG)) & BUSY_STAT) && retries--)
1885                 udelay(10);
1886         ide_end_drive_cmd (drive, stat, hwif->INB(IDE_ERROR_REG));
1887
1888         return ide_stopped;
1889 }
1890
1891 ide_startstop_t flagged_task_mulin_intr (ide_drive_t *drive)
1892 {
1893         ide_hwif_t *hwif        = HWIF(drive);
1894         u8 stat                 = hwif->INB(IDE_STATUS_REG);
1895         struct request *rq      = HWGROUP(drive)->rq;
1896         char *pBuf              = NULL;
1897         int retries             = 5;
1898         unsigned int msect, nsect;
1899
1900         if (rq->current_nr_sectors == 0) 
1901                 return DRIVER(drive)->error(drive, "flagged_task_mulin_intr (no data requested)", stat); 
1902
1903         msect = drive->mult_count;
1904         if (msect == 0) 
1905                 return DRIVER(drive)->error(drive, "flagged_task_mulin_intr (multimode not set)", stat); 
1906
1907         if (!OK_STAT(stat, DATA_READY, BAD_R_STAT)) {
1908                 if (stat & ERR_STAT) {
1909                         return DRIVER(drive)->error(drive, "flagged_task_mulin_intr", stat);
1910                 }
1911                 /*
1912                  * (ks) Unexpected ATA data phase detected.
1913                  * This should not happen. But, it can !
1914                  * I am not sure, which function is best to clean up
1915                  * this situation.  I choose: ide_error(...)
1916                  */
1917                 return DRIVER(drive)->error(drive, "flagged_task_mulin_intr (unexpected data phase)", stat); 
1918         }
1919
1920         nsect = (rq->current_nr_sectors > msect) ? msect : rq->current_nr_sectors;
1921         pBuf = rq->buffer + ((rq->nr_sectors - rq->current_nr_sectors) * SECTOR_SIZE);
1922
1923         DTF("Multiread: %p, nsect: %d , rq->current_nr_sectors: %ld\n",
1924             pBuf, nsect, rq->current_nr_sectors);
1925
1926         taskfile_input_data(drive, pBuf, nsect * SECTOR_WORDS);
1927
1928         rq->current_nr_sectors -= nsect;
1929         if (rq->current_nr_sectors != 0) {
1930                 /*
1931                  * (ks) We don't know which command was executed. 
1932                  * So, we wait the 'WORSTCASE' value.
1933                  */
1934                 ide_set_handler(drive, &flagged_task_mulin_intr,  WAIT_WORSTCASE, NULL);
1935                 return ide_started;
1936         }
1937
1938         /*
1939          * (ks) Last sector was transfered, wait until drive is ready. 
1940          * This can take up to 10 usec. We willl wait max 50 us.
1941          */
1942         while (((stat = hwif->INB(IDE_STATUS_REG)) & BUSY_STAT) && retries--)
1943                 udelay(10);
1944         ide_end_drive_cmd (drive, stat, hwif->INB(IDE_ERROR_REG));
1945
1946         return ide_stopped;
1947 }
1948
1949 /*
1950  * Pre handler for command with PIO data-out phase
1951  */
1952 ide_startstop_t flagged_pre_task_out_intr (ide_drive_t *drive, struct request *rq)
1953 {
1954         ide_hwif_t *hwif        = HWIF(drive);
1955         u8 stat                 = hwif->INB(IDE_STATUS_REG);
1956         ide_startstop_t startstop;
1957
1958         if (!rq->current_nr_sectors) {
1959                 return DRIVER(drive)->error(drive, "flagged_pre_task_out_intr (write data not specified)", stat);
1960         }
1961
1962         if (ide_wait_stat(&startstop, drive, DATA_READY,
1963                         BAD_W_STAT, WAIT_DRQ)) {
1964                 printk(KERN_ERR "%s: No DRQ bit after issuing write command.\n", drive->name);
1965                 return startstop;
1966         }
1967
1968         taskfile_output_data(drive, rq->buffer, SECTOR_WORDS);
1969         --rq->current_nr_sectors;
1970
1971         return ide_started;
1972 }
1973
1974 ide_startstop_t flagged_task_out_intr (ide_drive_t *drive)
1975 {
1976         ide_hwif_t *hwif        = HWIF(drive);
1977         u8 stat                 = hwif->INB(IDE_STATUS_REG);
1978         struct request *rq      = HWGROUP(drive)->rq;
1979         char *pBuf              = NULL;
1980
1981         if (!OK_STAT(stat, DRIVE_READY, BAD_W_STAT)) 
1982                 return DRIVER(drive)->error(drive, "flagged_task_out_intr", stat);
1983         
1984         if (!rq->current_nr_sectors) { 
1985                 ide_end_drive_cmd (drive, stat, hwif->INB(IDE_ERROR_REG));
1986                 return ide_stopped;
1987         }
1988
1989         if (!OK_STAT(stat, DATA_READY, BAD_W_STAT)) {
1990                 /*
1991                  * (ks) Unexpected ATA data phase detected.
1992                  * This should not happen. But, it can !
1993                  * I am not sure, which function is best to clean up
1994                  * this situation.  I choose: ide_error(...)
1995                  */
1996                 return DRIVER(drive)->error(drive, "flagged_task_out_intr (unexpected data phase)", stat); 
1997         }
1998
1999         pBuf = rq->buffer + ((rq->nr_sectors - rq->current_nr_sectors) * SECTOR_SIZE);
2000         DTF("Write - rq->current_nr_sectors: %d, status: %02x\n",
2001                 (int) rq->current_nr_sectors, stat);
2002
2003         taskfile_output_data(drive, pBuf, SECTOR_WORDS);
2004         --rq->current_nr_sectors;
2005
2006         /*
2007          * (ks) We don't know which command was executed. 
2008          * So, we wait the 'WORSTCASE' value.
2009          */
2010         ide_set_handler(drive, &flagged_task_out_intr, WAIT_WORSTCASE, NULL);
2011
2012         return ide_started;
2013 }
2014
2015 ide_startstop_t flagged_pre_task_mulout_intr (ide_drive_t *drive, struct request *rq)
2016 {
2017         ide_hwif_t *hwif        = HWIF(drive);
2018         u8 stat                 = hwif->INB(IDE_STATUS_REG);
2019         char *pBuf              = NULL;
2020         ide_startstop_t startstop;
2021         unsigned int msect, nsect;
2022
2023         if (!rq->current_nr_sectors) 
2024                 return DRIVER(drive)->error(drive, "flagged_pre_task_mulout_intr (write data not specified)", stat);
2025
2026         msect = drive->mult_count;
2027         if (msect == 0)
2028                 return DRIVER(drive)->error(drive, "flagged_pre_task_mulout_intr (multimode not set)", stat);
2029
2030         if (ide_wait_stat(&startstop, drive, DATA_READY,
2031                         BAD_W_STAT, WAIT_DRQ)) {
2032                 printk(KERN_ERR "%s: No DRQ bit after issuing write command.\n", drive->name);
2033                 return startstop;
2034         }
2035
2036         nsect = (rq->current_nr_sectors > msect) ? msect : rq->current_nr_sectors;
2037         pBuf = rq->buffer + ((rq->nr_sectors - rq->current_nr_sectors) * SECTOR_SIZE);
2038         DTF("Multiwrite: %p, nsect: %d , rq->current_nr_sectors: %ld\n",
2039             pBuf, nsect, rq->current_nr_sectors);
2040
2041         taskfile_output_data(drive, pBuf, nsect * SECTOR_WORDS);
2042
2043         rq->current_nr_sectors -= nsect;
2044
2045         return ide_started;
2046 }
2047
2048 ide_startstop_t flagged_task_mulout_intr (ide_drive_t *drive)
2049 {
2050         ide_hwif_t *hwif        = HWIF(drive);
2051         u8 stat                 = hwif->INB(IDE_STATUS_REG);
2052         struct request *rq      = HWGROUP(drive)->rq;
2053         char *pBuf              = NULL;
2054         unsigned int msect, nsect;
2055
2056         msect = drive->mult_count;
2057         if (msect == 0)
2058                 return DRIVER(drive)->error(drive, "flagged_task_mulout_intr (multimode not set)", stat);
2059
2060         if (!OK_STAT(stat, DRIVE_READY, BAD_W_STAT)) 
2061                 return DRIVER(drive)->error(drive, "flagged_task_mulout_intr", stat);
2062         
2063         if (!rq->current_nr_sectors) { 
2064                 ide_end_drive_cmd (drive, stat, hwif->INB(IDE_ERROR_REG));
2065                 return ide_stopped;
2066         }
2067
2068         if (!OK_STAT(stat, DATA_READY, BAD_W_STAT)) {
2069                 /*
2070                  * (ks) Unexpected ATA data phase detected.
2071                  * This should not happen. But, it can !
2072                  * I am not sure, which function is best to clean up
2073                  * this situation.  I choose: ide_error(...)
2074                  */
2075                 return DRIVER(drive)->error(drive, "flagged_task_mulout_intr (unexpected data phase)", stat); 
2076         }
2077
2078         nsect = (rq->current_nr_sectors > msect) ? msect : rq->current_nr_sectors;
2079         pBuf = rq->buffer + ((rq->nr_sectors - rq->current_nr_sectors) * SECTOR_SIZE);
2080         DTF("Multiwrite: %p, nsect: %d , rq->current_nr_sectors: %ld\n",
2081             pBuf, nsect, rq->current_nr_sectors);
2082
2083         taskfile_output_data(drive, pBuf, nsect * SECTOR_WORDS);
2084         rq->current_nr_sectors -= nsect;
2085
2086         /*
2087          * (ks) We don't know which command was executed. 
2088          * So, we wait the 'WORSTCASE' value.
2089          */
2090         ide_set_handler(drive, &flagged_task_mulout_intr, WAIT_WORSTCASE, NULL);
2091
2092         return ide_started;
2093 }