This commit was manufactured by cvs2svn to create tag
[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 #ifdef CONFIG_BLK_DEV_IDE_TCQ
205                 case WIN_READDMA_QUEUED:
206                 case WIN_READDMA_QUEUED_EXT:
207                         return __ide_dma_queued_read(drive);
208                 case WIN_WRITEDMA_QUEUED:
209                 case WIN_WRITEDMA_QUEUED_EXT:
210                         return __ide_dma_queued_write(drive);
211 #endif
212                 default:
213                         if (task->handler == NULL)
214                                 return ide_stopped;
215         }
216
217         return ide_stopped;
218 }
219
220 EXPORT_SYMBOL(do_rw_taskfile);
221
222 /*
223  * set_multmode_intr() is invoked on completion of a WIN_SETMULT cmd.
224  */
225 ide_startstop_t set_multmode_intr (ide_drive_t *drive)
226 {
227         ide_hwif_t *hwif = HWIF(drive);
228         u8 stat;
229
230         if (OK_STAT(stat = hwif->INB(IDE_STATUS_REG),READY_STAT,BAD_STAT)) {
231                 drive->mult_count = drive->mult_req;
232         } else {
233                 drive->mult_req = drive->mult_count = 0;
234                 drive->special.b.recalibrate = 1;
235                 (void) ide_dump_status(drive, "set_multmode", stat);
236         }
237         return ide_stopped;
238 }
239
240 EXPORT_SYMBOL(set_multmode_intr);
241
242 /*
243  * set_geometry_intr() is invoked on completion of a WIN_SPECIFY cmd.
244  */
245 ide_startstop_t set_geometry_intr (ide_drive_t *drive)
246 {
247         ide_hwif_t *hwif = HWIF(drive);
248         int retries = 5;
249         u8 stat;
250
251         while (((stat = hwif->INB(IDE_STATUS_REG)) & BUSY_STAT) && retries--)
252                 udelay(10);
253
254         if (OK_STAT(stat, READY_STAT, BAD_STAT))
255                 return ide_stopped;
256
257         if (stat & (ERR_STAT|DRQ_STAT))
258                 return DRIVER(drive)->error(drive, "set_geometry_intr", stat);
259
260         if (HWGROUP(drive)->handler != NULL)
261                 BUG();
262         ide_set_handler(drive, &set_geometry_intr, WAIT_WORSTCASE, NULL);
263         return ide_started;
264 }
265
266 EXPORT_SYMBOL(set_geometry_intr);
267
268 /*
269  * recal_intr() is invoked on completion of a WIN_RESTORE (recalibrate) cmd.
270  */
271 ide_startstop_t recal_intr (ide_drive_t *drive)
272 {
273         ide_hwif_t *hwif = HWIF(drive);
274         u8 stat;
275
276         if (!OK_STAT(stat = hwif->INB(IDE_STATUS_REG), READY_STAT, BAD_STAT))
277                 return DRIVER(drive)->error(drive, "recal_intr", stat);
278         return ide_stopped;
279 }
280
281 EXPORT_SYMBOL(recal_intr);
282
283 /*
284  * Handler for commands without a data phase
285  */
286 ide_startstop_t task_no_data_intr (ide_drive_t *drive)
287 {
288         ide_task_t *args        = HWGROUP(drive)->rq->special;
289         ide_hwif_t *hwif        = HWIF(drive);
290         u8 stat;
291
292         local_irq_enable();
293         if (!OK_STAT(stat = hwif->INB(IDE_STATUS_REG),READY_STAT,BAD_STAT)) {
294                 DTF("%s: command opcode 0x%02x\n", drive->name,
295                         args->tfRegister[IDE_COMMAND_OFFSET]);
296                 return DRIVER(drive)->error(drive, "task_no_data_intr", stat);
297                 /* calls ide_end_drive_cmd */
298         }
299         if (args)
300                 ide_end_drive_cmd(drive, stat, hwif->INB(IDE_ERROR_REG));
301
302         return ide_stopped;
303 }
304
305 EXPORT_SYMBOL(task_no_data_intr);
306
307 /*
308  * old taskfile PIO handlers, to be killed as soon as possible.
309  */
310 #ifndef CONFIG_IDE_TASKFILE_IO
311
312 #define task_map_rq(rq, flags)          ide_map_buffer((rq), (flags))
313 #define task_unmap_rq(rq, buf, flags)   ide_unmap_buffer((rq), (buf), (flags))
314
315 /*
316  * Handler for command with PIO data-in phase, READ
317  */
318 /*
319  * FIXME before 2.4 enable ...
320  *      DATA integrity issue upon error. <andre@linux-ide.org>
321  */
322 ide_startstop_t task_in_intr (ide_drive_t *drive)
323 {
324         struct request *rq      = HWGROUP(drive)->rq;
325         ide_hwif_t *hwif        = HWIF(drive);
326         char *pBuf              = NULL;
327         u8 stat;
328         unsigned long flags;
329
330         if (!OK_STAT(stat = hwif->INB(IDE_STATUS_REG),DATA_READY,BAD_R_STAT)) {
331                 if (stat & (ERR_STAT|DRQ_STAT)) {
332 #if 0
333                         DTF("%s: attempting to recover last " \
334                                 "sector counter status=0x%02x\n",
335                                 drive->name, stat);
336                         /*
337                          * Expect a BUG BOMB if we attempt to rewind the
338                          * offset in the BH aka PAGE in the current BLOCK
339                          * segment.  This is different than the HOST segment.
340                          */
341 #endif
342                         if (!rq->bio)
343                                 rq->current_nr_sectors++;
344                         return DRIVER(drive)->error(drive, "task_in_intr", stat);
345                 }
346                 if (!(stat & BUSY_STAT)) {
347                         DTF("task_in_intr to Soon wait for next interrupt\n");
348                         if (HWGROUP(drive)->handler == NULL)
349                                 ide_set_handler(drive, &task_in_intr, WAIT_WORSTCASE, NULL);
350                         return ide_started;  
351                 }
352         }
353 #if 0
354
355         /*
356          * Holding point for a brain dump of a thought :-/
357          */
358
359         if (!OK_STAT(stat,DRIVE_READY,drive->bad_wstat)) {
360                 DTF("%s: READ attempting to recover last " \
361                         "sector counter status=0x%02x\n",
362                         drive->name, stat);
363                 rq->current_nr_sectors++;
364                 return DRIVER(drive)->error(drive, "task_in_intr", stat);
365         }
366         if (!rq->current_nr_sectors)
367                 if (!DRIVER(drive)->end_request(drive, 1, 0))
368                         return ide_stopped;
369
370         if (--rq->current_nr_sectors <= 0)
371                 if (!DRIVER(drive)->end_request(drive, 1, 0))
372                         return ide_stopped;
373 #endif
374
375         pBuf = task_map_rq(rq, &flags);
376         DTF("Read: %p, rq->current_nr_sectors: %d, stat: %02x\n",
377                 pBuf, (int) rq->current_nr_sectors, stat);
378         taskfile_input_data(drive, pBuf, SECTOR_WORDS);
379         task_unmap_rq(rq, pBuf, &flags);
380         /*
381          * FIXME :: We really can not legally get a new page/bh
382          * regardless, if this is the end of our segment.
383          * BH walking or segment can only be updated after we have a good
384          * hwif->INB(IDE_STATUS_REG); return.
385          */
386         if (--rq->current_nr_sectors <= 0)
387                 if (!DRIVER(drive)->end_request(drive, 1, 0))
388                         return ide_stopped;
389         /*
390          * ERM, it is techincally legal to leave/exit here but it makes
391          * a mess of the code ...
392          */
393         if (HWGROUP(drive)->handler == NULL)
394                 ide_set_handler(drive, &task_in_intr, WAIT_WORSTCASE, NULL);
395         return ide_started;
396 }
397
398 EXPORT_SYMBOL(task_in_intr);
399
400 /*
401  * Handler for command with Read Multiple
402  */
403 ide_startstop_t task_mulin_intr (ide_drive_t *drive)
404 {
405         ide_hwif_t *hwif        = HWIF(drive);
406         struct request *rq      = HWGROUP(drive)->rq;
407         char *pBuf              = NULL;
408         unsigned int msect      = drive->mult_count;
409         unsigned int nsect;
410         unsigned long flags;
411         u8 stat;
412
413         if (!OK_STAT(stat = hwif->INB(IDE_STATUS_REG),DATA_READY,BAD_R_STAT)) {
414                 if (stat & (ERR_STAT|DRQ_STAT)) {
415                         if (!rq->bio) {
416                                 rq->current_nr_sectors += drive->mult_count;
417                                 /*
418                                  * NOTE: could rewind beyond beginning :-/
419                                  */
420                         } else {
421                                 printk(KERN_ERR "%s: MULTI-READ assume all data " \
422                                         "transfered is bad status=0x%02x\n",
423                                         drive->name, stat);
424                         }
425                         return DRIVER(drive)->error(drive, "task_mulin_intr", stat);
426                 }
427                 /* no data yet, so wait for another interrupt */
428                 if (HWGROUP(drive)->handler == NULL)
429                         ide_set_handler(drive, &task_mulin_intr, WAIT_WORSTCASE, NULL);
430                 return ide_started;
431         }
432
433         do {
434                 nsect = rq->current_nr_sectors;
435                 if (nsect > msect)
436                         nsect = msect;
437                 pBuf = task_map_rq(rq, &flags);
438                 DTF("Multiread: %p, nsect: %d, msect: %d, " \
439                         " rq->current_nr_sectors: %d\n",
440                         pBuf, nsect, msect, rq->current_nr_sectors);
441                 taskfile_input_data(drive, pBuf, nsect * SECTOR_WORDS);
442                 task_unmap_rq(rq, pBuf, &flags);
443                 rq->errors = 0;
444                 rq->current_nr_sectors -= nsect;
445                 msect -= nsect;
446                 /*
447                  * FIXME :: We really can not legally get a new page/bh
448                  * regardless, if this is the end of our segment.
449                  * BH walking or segment can only be updated after we have a
450                  * good hwif->INB(IDE_STATUS_REG); return.
451                  */
452                 if (!rq->current_nr_sectors) {
453                         if (!DRIVER(drive)->end_request(drive, 1, 0))
454                                 return ide_stopped;
455                 }
456         } while (msect);
457         if (HWGROUP(drive)->handler == NULL)
458                 ide_set_handler(drive, &task_mulin_intr, WAIT_WORSTCASE, NULL);
459         return ide_started;
460 }
461
462 EXPORT_SYMBOL(task_mulin_intr);
463
464 /*
465  * VERIFY ME before 2.4 ... unexpected race is possible based on details
466  * RMK with 74LS245/373/374 TTL buffer logic because of passthrough.
467  */
468 ide_startstop_t pre_task_out_intr (ide_drive_t *drive, struct request *rq)
469 {
470         char *pBuf              = NULL;
471         unsigned long flags;
472         ide_startstop_t startstop;
473
474         if (ide_wait_stat(&startstop, drive, DATA_READY,
475                         drive->bad_wstat, WAIT_DRQ)) {
476                 printk(KERN_ERR "%s: no DRQ after issuing WRITE%s\n",
477                         drive->name,
478                         drive->addressing ? "_EXT" : "");
479                 return startstop;
480         }
481         /* For Write_sectors we need to stuff the first sector */
482         pBuf = task_map_rq(rq, &flags);
483         taskfile_output_data(drive, pBuf, SECTOR_WORDS);
484         rq->current_nr_sectors--;
485         task_unmap_rq(rq, pBuf, &flags);
486         return ide_started;
487 }
488
489 EXPORT_SYMBOL(pre_task_out_intr);
490
491 /*
492  * Handler for command with PIO data-out phase WRITE
493  *
494  * WOOHOO this is a CORRECT STATE DIAGRAM NOW, <andre@linux-ide.org>
495  */
496 ide_startstop_t task_out_intr (ide_drive_t *drive)
497 {
498         ide_hwif_t *hwif        = HWIF(drive);
499         struct request *rq      = HWGROUP(drive)->rq;
500         char *pBuf              = NULL;
501         unsigned long flags;
502         u8 stat;
503
504         if (!OK_STAT(stat = hwif->INB(IDE_STATUS_REG), DRIVE_READY, drive->bad_wstat)) {
505                 DTF("%s: WRITE attempting to recover last " \
506                         "sector counter status=0x%02x\n",
507                         drive->name, stat);
508                 rq->current_nr_sectors++;
509                 return DRIVER(drive)->error(drive, "task_out_intr", stat);
510         }
511         /*
512          * Safe to update request for partial completions.
513          * We have a good STATUS CHECK!!!
514          */
515         if (!rq->current_nr_sectors)
516                 if (!DRIVER(drive)->end_request(drive, 1, 0))
517                         return ide_stopped;
518         if ((rq->current_nr_sectors==1) ^ (stat & DRQ_STAT)) {
519                 rq = HWGROUP(drive)->rq;
520                 pBuf = task_map_rq(rq, &flags);
521                 DTF("write: %p, rq->current_nr_sectors: %d\n",
522                         pBuf, (int) rq->current_nr_sectors);
523                 taskfile_output_data(drive, pBuf, SECTOR_WORDS);
524                 task_unmap_rq(rq, pBuf, &flags);
525                 rq->errors = 0;
526                 rq->current_nr_sectors--;
527         }
528         if (HWGROUP(drive)->handler == NULL)
529                 ide_set_handler(drive, &task_out_intr, WAIT_WORSTCASE, NULL);
530         return ide_started;
531 }
532
533 EXPORT_SYMBOL(task_out_intr);
534
535 #undef ALTERNATE_STATE_DIAGRAM_MULTI_OUT
536
537 ide_startstop_t pre_task_mulout_intr (ide_drive_t *drive, struct request *rq)
538 {
539 #ifdef ALTERNATE_STATE_DIAGRAM_MULTI_OUT
540         ide_hwif_t *hwif                = HWIF(drive);
541         char *pBuf                      = NULL;
542         unsigned int nsect = 0, msect   = drive->mult_count;
543         u8 stat;
544         unsigned long flags;
545 #endif /* ALTERNATE_STATE_DIAGRAM_MULTI_OUT */
546
547         ide_task_t *args = rq->special;
548         ide_startstop_t startstop;
549
550 #if 0
551         /*
552          * assign private copy for multi-write
553          */
554         memcpy(&HWGROUP(drive)->wrq, rq, sizeof(struct request));
555 #endif
556
557         if (ide_wait_stat(&startstop, drive, DATA_READY,
558                         drive->bad_wstat, WAIT_DRQ)) {
559                 printk(KERN_ERR "%s: no DRQ after issuing %s\n",
560                         drive->name,
561                         drive->addressing ? "MULTWRITE_EXT" : "MULTWRITE");
562                 return startstop;
563         }
564 #ifdef ALTERNATE_STATE_DIAGRAM_MULTI_OUT
565
566         do {
567                 nsect = rq->current_nr_sectors;
568                 if (nsect > msect)
569                         nsect = msect;
570                 pBuf = task_map_rq(rq, &flags);
571                 DTF("Pre-Multiwrite: %p, nsect: %d, msect: %d, " \
572                         "rq->current_nr_sectors: %ld\n",
573                         pBuf, nsect, msect, rq->current_nr_sectors);
574                 msect -= nsect;
575                 taskfile_output_data(drive, pBuf, nsect * SECTOR_WORDS);
576                 task_unmap_rq(rq, pBuf, &flags);
577                 rq->current_nr_sectors -= nsect;
578                 if (!rq->current_nr_sectors) {
579                         if (!DRIVER(drive)->end_request(drive, 1, 0))
580                                 if (!rq->bio) {
581                                         stat = hwif->INB(IDE_STATUS_REG);
582                                         return ide_stopped;
583                                 }
584                 }
585         } while (msect);
586         rq->errors = 0;
587         return ide_started;
588 #else /* ! ALTERNATE_STATE_DIAGRAM_MULTI_OUT */
589         if (!(drive_is_ready(drive))) {
590                 int i;
591                 for (i=0; i<100; i++) {
592                         if (drive_is_ready(drive))
593                                 break;
594                 }
595         }
596
597         /*
598          * WARNING :: if the drive as not acked good status we may not
599          * move the DATA-TRANSFER T-Bar as BSY != 0. <andre@linux-ide.org>
600          */
601         return args->handler(drive);
602 #endif /* ALTERNATE_STATE_DIAGRAM_MULTI_OUT */
603 }
604
605 EXPORT_SYMBOL(pre_task_mulout_intr);
606
607 /*
608  * FIXME before enabling in 2.4 ... DATA integrity issue upon error.
609  */
610 /*
611  * Handler for command write multiple
612  * Called directly from execute_drive_cmd for the first bunch of sectors,
613  * afterwards only by the ISR
614  */
615 ide_startstop_t task_mulout_intr (ide_drive_t *drive)
616 {
617         ide_hwif_t *hwif                = HWIF(drive);
618         u8 stat                         = hwif->INB(IDE_STATUS_REG);
619         struct request *rq              = HWGROUP(drive)->rq;
620         char *pBuf                      = NULL;
621         ide_startstop_t startstop       = ide_stopped;
622         unsigned int msect              = drive->mult_count;
623         unsigned int nsect;
624         unsigned long flags;
625
626         /*
627          * (ks/hs): Handle last IRQ on multi-sector transfer,
628          * occurs after all data was sent in this chunk
629          */
630         if (rq->current_nr_sectors == 0) {
631                 if (stat & (ERR_STAT|DRQ_STAT)) {
632                         if (!rq->bio) {
633                                 rq->current_nr_sectors += drive->mult_count;
634                                 /*
635                                  * NOTE: could rewind beyond beginning :-/
636                                  */
637                         } else {
638                                 printk(KERN_ERR "%s: MULTI-WRITE assume all data " \
639                                         "transfered is bad status=0x%02x\n",
640                                         drive->name, stat);
641                         }
642                         return DRIVER(drive)->error(drive, "task_mulout_intr", stat);
643                 }
644                 if (!rq->bio)
645                         DRIVER(drive)->end_request(drive, 1, 0);
646                 return startstop;
647         }
648         /*
649          * DON'T be lazy code the above and below togather !!!
650          */
651         if (!OK_STAT(stat,DATA_READY,BAD_R_STAT)) {
652                 if (stat & (ERR_STAT|DRQ_STAT)) {
653                         if (!rq->bio) {
654                                 rq->current_nr_sectors += drive->mult_count;
655                                 /*
656                                  * NOTE: could rewind beyond beginning :-/
657                                  */
658                         } else {
659                                 printk("%s: MULTI-WRITE assume all data " \
660                                         "transfered is bad status=0x%02x\n",
661                                         drive->name, stat);
662                         }
663                         return DRIVER(drive)->error(drive, "task_mulout_intr", stat);
664                 }
665                 /* no data yet, so wait for another interrupt */
666                 if (HWGROUP(drive)->handler == NULL)
667                         ide_set_handler(drive, &task_mulout_intr, WAIT_WORSTCASE, NULL);
668                 return ide_started;
669         }
670
671 #ifndef ALTERNATE_STATE_DIAGRAM_MULTI_OUT
672         if (HWGROUP(drive)->handler != NULL) {
673                 unsigned long lflags;
674                 spin_lock_irqsave(&ide_lock, lflags);
675                 HWGROUP(drive)->handler = NULL;
676                 del_timer(&HWGROUP(drive)->timer);
677                 spin_unlock_irqrestore(&ide_lock, lflags);
678         }
679 #endif /* ALTERNATE_STATE_DIAGRAM_MULTI_OUT */
680
681         do {
682                 nsect = rq->current_nr_sectors;
683                 if (nsect > msect)
684                         nsect = msect;
685                 pBuf = task_map_rq(rq, &flags);
686                 DTF("Multiwrite: %p, nsect: %d, msect: %d, " \
687                         "rq->current_nr_sectors: %ld\n",
688                         pBuf, nsect, msect, rq->current_nr_sectors);
689                 msect -= nsect;
690                 taskfile_output_data(drive, pBuf, nsect * SECTOR_WORDS);
691                 task_unmap_rq(rq, pBuf, &flags);
692                 rq->current_nr_sectors -= nsect;
693                 /*
694                  * FIXME :: We really can not legally get a new page/bh
695                  * regardless, if this is the end of our segment.
696                  * BH walking or segment can only be updated after we
697                  * have a good  hwif->INB(IDE_STATUS_REG); return.
698                  */
699                 if (!rq->current_nr_sectors) {
700                         if (!DRIVER(drive)->end_request(drive, 1, 0))
701                                 if (!rq->bio)
702                                         return ide_stopped;
703                 }
704         } while (msect);
705         rq->errors = 0;
706         if (HWGROUP(drive)->handler == NULL)
707                 ide_set_handler(drive, &task_mulout_intr, WAIT_WORSTCASE, NULL);
708         return ide_started;
709 }
710
711 EXPORT_SYMBOL(task_mulout_intr);
712
713 #else /* !CONFIG_IDE_TASKFILE_IO */
714
715 static u8 wait_drive_not_busy(ide_drive_t *drive)
716 {
717         ide_hwif_t *hwif = HWIF(drive);
718         int retries = 100;
719         u8 stat;
720
721         /*
722          * Last sector was transfered, wait until drive is ready.
723          * This can take up to 10 usec, but we will wait max 1 ms
724          * (drive_cmd_intr() waits that long).
725          */
726         while (((stat = hwif->INB(IDE_STATUS_REG)) & BUSY_STAT) && retries--)
727                 udelay(10);
728
729         if (!retries)
730                 printk(KERN_ERR "%s: drive still BUSY!\n", drive->name);
731
732         return stat;
733 }
734
735 /*
736  * Handler for command with PIO data-in phase (Read).
737  */
738 ide_startstop_t task_in_intr (ide_drive_t *drive)
739 {
740         struct request *rq = HWGROUP(drive)->rq;
741         u8 stat, good_stat;
742
743         good_stat = DATA_READY;
744         stat = HWIF(drive)->INB(IDE_STATUS_REG);
745 check_status:
746         if (!OK_STAT(stat, good_stat, BAD_R_STAT)) {
747                 if (stat & (ERR_STAT | DRQ_STAT))
748                         return DRIVER(drive)->error(drive, __FUNCTION__, stat);
749                 /* BUSY_STAT: No data yet, so wait for another IRQ. */
750                 ide_set_handler(drive, &task_in_intr, WAIT_WORSTCASE, NULL);
751                 return ide_started;
752         }
753
754         /*
755          * Complete previously submitted bios (if any).
756          * Status was already verifyied.
757          */
758         while (rq->bio != rq->cbio)
759                 if (!DRIVER(drive)->end_request(drive, 1, bio_sectors(rq->bio)))
760                         return ide_stopped;
761         /* Complete rq->buffer based request (ioctls). */
762         if (!rq->bio && !rq->nr_sectors) {
763                 ide_end_drive_cmd(drive, stat, HWIF(drive)->INB(IDE_ERROR_REG));
764                 return ide_stopped;
765         }
766
767         rq->errors = 0;
768         task_sectors(drive, rq, 1, IDE_PIO_IN);
769
770         /* If it was the last datablock check status and finish transfer. */
771         if (!rq->nr_sectors) {
772                 good_stat = 0;
773                 stat = wait_drive_not_busy(drive);
774                 goto check_status;
775         }
776
777         /* Still data left to transfer. */
778         ide_set_handler(drive, &task_in_intr, WAIT_WORSTCASE, NULL);
779
780         return ide_started;
781 }
782 EXPORT_SYMBOL(task_in_intr);
783
784 /*
785  * Handler for command with PIO data-in phase (Read Multiple).
786  */
787 ide_startstop_t task_mulin_intr (ide_drive_t *drive)
788 {
789         struct request *rq = HWGROUP(drive)->rq;
790         unsigned int msect = drive->mult_count;
791         unsigned int nsect;
792         u8 stat, good_stat;
793
794         good_stat = DATA_READY;
795         stat = HWIF(drive)->INB(IDE_STATUS_REG);
796 check_status:
797         if (!OK_STAT(stat, good_stat, BAD_R_STAT)) {
798                 if (stat & (ERR_STAT | DRQ_STAT))
799                         return DRIVER(drive)->error(drive, __FUNCTION__, stat);
800                 /* BUSY_STAT: No data yet, so wait for another IRQ. */
801                 ide_set_handler(drive, &task_mulin_intr, WAIT_WORSTCASE, NULL);
802                 return ide_started;
803         }
804
805         /*
806          * Complete previously submitted bios (if any).
807          * Status was already verifyied.
808          */
809         while (rq->bio != rq->cbio)
810                 if (!DRIVER(drive)->end_request(drive, 1, bio_sectors(rq->bio)))
811                         return ide_stopped;
812         /* Complete rq->buffer based request (ioctls). */
813         if (!rq->bio && !rq->nr_sectors) {
814                 ide_end_drive_cmd(drive, stat, HWIF(drive)->INB(IDE_ERROR_REG));
815                 return ide_stopped;
816         }
817
818         rq->errors = 0;
819         do {
820                 nsect = rq->current_nr_sectors;
821                 if (nsect > msect)
822                         nsect = msect;
823
824                 task_sectors(drive, rq, nsect, IDE_PIO_IN);
825
826                 if (!rq->nr_sectors)
827                         msect = 0;
828                 else
829                         msect -= nsect;
830         } while (msect);
831
832         /* If it was the last datablock check status and finish transfer. */
833         if (!rq->nr_sectors) {
834                 good_stat = 0;
835                 stat = wait_drive_not_busy(drive);
836                 goto check_status;
837         }
838
839         /* Still data left to transfer. */
840         ide_set_handler(drive, &task_mulin_intr, WAIT_WORSTCASE, NULL);
841
842         return ide_started;
843 }
844 EXPORT_SYMBOL(task_mulin_intr);
845
846 /*
847  * Handler for command with PIO data-out phase (Write).
848  */
849 ide_startstop_t task_out_intr (ide_drive_t *drive)
850 {
851         struct request *rq = HWGROUP(drive)->rq;
852         u8 stat;
853
854         stat = HWIF(drive)->INB(IDE_STATUS_REG);
855         if (!OK_STAT(stat, DRIVE_READY, drive->bad_wstat)) {
856                 if ((stat & (ERR_STAT | DRQ_STAT)) ||
857                     ((stat & WRERR_STAT) && !drive->nowerr))
858                         return DRIVER(drive)->error(drive, __FUNCTION__, stat);
859                 if (stat & BUSY_STAT) {
860                         /* Not ready yet, so wait for another IRQ. */
861                         ide_set_handler(drive, &task_out_intr, WAIT_WORSTCASE, NULL);
862                         return ide_started;
863                 }
864         }
865
866         /* Deal with unexpected ATA data phase. */
867         if ((!(stat & DATA_READY) && rq->nr_sectors) ||
868             ((stat & DATA_READY) && !rq->nr_sectors))
869                 return DRIVER(drive)->error(drive, __FUNCTION__, stat);
870
871         /* 
872          * Complete previously submitted bios (if any).
873          * Status was already verifyied.
874          */
875         while (rq->bio != rq->cbio)
876                 if (!DRIVER(drive)->end_request(drive, 1, bio_sectors(rq->bio)))
877                         return ide_stopped;
878         /* Complete rq->buffer based request (ioctls). */
879         if (!rq->bio && !rq->nr_sectors) {
880                 ide_end_drive_cmd(drive, stat, HWIF(drive)->INB(IDE_ERROR_REG));
881                 return ide_stopped;
882         }
883
884         /* Still data left to transfer. */
885         ide_set_handler(drive, &task_out_intr, WAIT_WORSTCASE, NULL);
886
887         rq->errors = 0;
888         task_sectors(drive, rq, 1, IDE_PIO_OUT);
889
890         return ide_started;
891 }
892
893 EXPORT_SYMBOL(task_out_intr);
894
895 ide_startstop_t pre_task_out_intr (ide_drive_t *drive, struct request *rq)
896 {
897         ide_startstop_t startstop;
898
899         if (ide_wait_stat(&startstop, drive, DATA_READY,
900                           drive->bad_wstat, WAIT_DRQ)) {
901                 printk(KERN_ERR "%s: no DRQ after issuing WRITE%s\n",
902                                 drive->name, drive->addressing ? "_EXT" : "");
903                 return startstop;
904         }
905
906         if (!drive->unmask)
907                 local_irq_disable();
908
909         return task_out_intr(drive);
910 }
911 EXPORT_SYMBOL(pre_task_out_intr);
912
913 /*
914  * Handler for command with PIO data-out phase (Write Multiple).
915  */
916 ide_startstop_t task_mulout_intr (ide_drive_t *drive)
917 {
918         struct request *rq = HWGROUP(drive)->rq;
919         unsigned int msect = drive->mult_count;
920         unsigned int nsect;
921         u8 stat;
922
923         stat = HWIF(drive)->INB(IDE_STATUS_REG);
924         if (!OK_STAT(stat, DRIVE_READY, drive->bad_wstat)) {
925                 if ((stat & (ERR_STAT | DRQ_STAT)) ||
926                     ((stat & WRERR_STAT) && !drive->nowerr))
927                         return DRIVER(drive)->error(drive, __FUNCTION__, stat);
928                 if (stat & BUSY_STAT) {
929                         /* Not ready yet, so wait for another IRQ. */
930                         ide_set_handler(drive, &task_mulout_intr, WAIT_WORSTCASE, NULL);
931                         return ide_started;
932                 }
933         }
934
935         /* Deal with unexpected ATA data phase. */
936         if ((!(stat & DATA_READY) && rq->nr_sectors) ||
937             ((stat & DATA_READY) && !rq->nr_sectors))
938                 return DRIVER(drive)->error(drive, __FUNCTION__, stat);
939
940         /* 
941          * Complete previously submitted bios (if any).
942          * Status was already verifyied.
943          */
944         while (rq->bio != rq->cbio)
945                 if (!DRIVER(drive)->end_request(drive, 1, bio_sectors(rq->bio)))
946                         return ide_stopped;
947         /* Complete rq->buffer based request (ioctls). */
948         if (!rq->bio && !rq->nr_sectors) {
949                 ide_end_drive_cmd(drive, stat, HWIF(drive)->INB(IDE_ERROR_REG));
950                 return ide_stopped;
951         }
952
953         /* Still data left to transfer. */
954         ide_set_handler(drive, &task_mulout_intr, WAIT_WORSTCASE, NULL);
955
956         rq->errors = 0;
957         do {
958                 nsect = rq->current_nr_sectors;
959                 if (nsect > msect)
960                         nsect = msect;
961
962                 task_sectors(drive, rq, nsect, IDE_PIO_OUT);
963
964                 if (!rq->nr_sectors)
965                         msect = 0;
966                 else
967                         msect -= nsect;
968         } while (msect);
969
970         return ide_started;
971 }
972 EXPORT_SYMBOL(task_mulout_intr);
973
974 ide_startstop_t pre_task_mulout_intr (ide_drive_t *drive, struct request *rq)
975 {
976         ide_startstop_t startstop;
977
978         if (ide_wait_stat(&startstop, drive, DATA_READY,
979                           drive->bad_wstat, WAIT_DRQ)) {
980                 printk(KERN_ERR "%s: no DRQ after issuing MULTWRITE%s\n",
981                                 drive->name, drive->addressing ? "_EXT" : "");
982                 return startstop;
983         }
984
985         if (!drive->unmask)
986                 local_irq_disable();
987
988         return task_mulout_intr(drive);
989 }
990 EXPORT_SYMBOL(pre_task_mulout_intr);
991
992 #endif /* !CONFIG_IDE_TASKFILE_IO */
993
994 int ide_diag_taskfile (ide_drive_t *drive, ide_task_t *args, unsigned long data_size, u8 *buf)
995 {
996         struct request rq;
997
998         memset(&rq, 0, sizeof(rq));
999         rq.flags = REQ_DRIVE_TASKFILE;
1000         rq.buffer = buf;
1001
1002         /*
1003          * (ks) We transfer currently only whole sectors.
1004          * This is suffient for now.  But, it would be great,
1005          * if we would find a solution to transfer any size.
1006          * To support special commands like READ LONG.
1007          */
1008         if (args->command_type != IDE_DRIVE_TASK_NO_DATA) {
1009                 if (data_size == 0)
1010                         rq.nr_sectors = (args->hobRegister[IDE_NSECTOR_OFFSET] << 8) | args->tfRegister[IDE_NSECTOR_OFFSET];
1011                 else
1012                         rq.nr_sectors = data_size / SECTOR_SIZE;
1013
1014                 rq.hard_nr_sectors = rq.nr_sectors;
1015                 rq.hard_cur_sectors = rq.current_nr_sectors = rq.nr_sectors;
1016         }
1017
1018         rq.special = args;
1019         return ide_do_drive_cmd(drive, &rq, ide_wait);
1020 }
1021
1022 EXPORT_SYMBOL(ide_diag_taskfile);
1023
1024 int ide_raw_taskfile (ide_drive_t *drive, ide_task_t *args, u8 *buf)
1025 {
1026         return ide_diag_taskfile(drive, args, 0, buf);
1027 }
1028
1029 EXPORT_SYMBOL(ide_raw_taskfile);
1030
1031 #define MAX_DMA         (256*SECTOR_WORDS)
1032
1033 ide_startstop_t flagged_taskfile(ide_drive_t *, ide_task_t *);
1034 ide_startstop_t flagged_task_no_data_intr(ide_drive_t *);
1035 ide_startstop_t flagged_task_in_intr(ide_drive_t *);
1036 ide_startstop_t flagged_task_mulin_intr(ide_drive_t *);
1037 ide_startstop_t flagged_pre_task_out_intr(ide_drive_t *, struct request *);
1038 ide_startstop_t flagged_task_out_intr(ide_drive_t *);
1039 ide_startstop_t flagged_pre_task_mulout_intr(ide_drive_t *, struct request *);
1040 ide_startstop_t flagged_task_mulout_intr(ide_drive_t *);
1041
1042 int ide_taskfile_ioctl (ide_drive_t *drive, unsigned int cmd, unsigned long arg)
1043 {
1044         ide_task_request_t      *req_task;
1045         ide_task_t              args;
1046         u8 *outbuf              = NULL;
1047         u8 *inbuf               = NULL;
1048         task_ioreg_t *argsptr   = args.tfRegister;
1049         task_ioreg_t *hobsptr   = args.hobRegister;
1050         int err                 = 0;
1051         int tasksize            = sizeof(struct ide_task_request_s);
1052         int taskin              = 0;
1053         int taskout             = 0;
1054         u8 io_32bit             = drive->io_32bit;
1055         char __user *buf = (char __user *)arg;
1056
1057 //      printk("IDE Taskfile ...\n");
1058
1059         req_task = kmalloc(tasksize, GFP_KERNEL);
1060         if (req_task == NULL) return -ENOMEM;
1061         memset(req_task, 0, tasksize);
1062         if (copy_from_user(req_task, buf, tasksize)) {
1063                 kfree(req_task);
1064                 return -EFAULT;
1065         }
1066
1067         taskout = (int) req_task->out_size;
1068         taskin  = (int) req_task->in_size;
1069
1070         if (taskout) {
1071                 int outtotal = tasksize;
1072                 outbuf = kmalloc(taskout, GFP_KERNEL);
1073                 if (outbuf == NULL) {
1074                         err = -ENOMEM;
1075                         goto abort;
1076                 }
1077                 memset(outbuf, 0, taskout);
1078                 if (copy_from_user(outbuf, buf + outtotal, taskout)) {
1079                         err = -EFAULT;
1080                         goto abort;
1081                 }
1082         }
1083
1084         if (taskin) {
1085                 int intotal = tasksize + taskout;
1086                 inbuf = kmalloc(taskin, GFP_KERNEL);
1087                 if (inbuf == NULL) {
1088                         err = -ENOMEM;
1089                         goto abort;
1090                 }
1091                 memset(inbuf, 0, taskin);
1092                 if (copy_from_user(inbuf, buf + intotal, taskin)) {
1093                         err = -EFAULT;
1094                         goto abort;
1095                 }
1096         }
1097
1098         memset(&args, 0, sizeof(ide_task_t));
1099         memcpy(argsptr, req_task->io_ports, HDIO_DRIVE_TASK_HDR_SIZE);
1100         memcpy(hobsptr, req_task->hob_ports, HDIO_DRIVE_HOB_HDR_SIZE);
1101
1102         args.tf_in_flags  = req_task->in_flags;
1103         args.tf_out_flags = req_task->out_flags;
1104         args.data_phase   = req_task->data_phase;
1105         args.command_type = req_task->req_cmd;
1106
1107         drive->io_32bit = 0;
1108         switch(req_task->data_phase) {
1109                 case TASKFILE_OUT_DMAQ:
1110                 case TASKFILE_OUT_DMA:
1111                         err = ide_diag_taskfile(drive, &args, taskout, outbuf);
1112                         break;
1113                 case TASKFILE_IN_DMAQ:
1114                 case TASKFILE_IN_DMA:
1115                         err = ide_diag_taskfile(drive, &args, taskin, inbuf);
1116                         break;
1117                 case TASKFILE_IN_OUT:
1118 #if 0
1119                         args.prehandler = &pre_task_out_intr;
1120                         args.handler = &task_out_intr;
1121                         err = ide_diag_taskfile(drive, &args, taskout, outbuf);
1122                         args.prehandler = NULL;
1123                         args.handler = &task_in_intr;
1124                         err = ide_diag_taskfile(drive, &args, taskin, inbuf);
1125                         break;
1126 #else
1127                         err = -EFAULT;
1128                         goto abort;
1129 #endif
1130                 case TASKFILE_MULTI_OUT:
1131                         if (!drive->mult_count) {
1132                                 /* (hs): give up if multcount is not set */
1133                                 printk(KERN_ERR "%s: %s Multimode Write " \
1134                                         "multcount is not set\n",
1135                                         drive->name, __FUNCTION__);
1136                                 err = -EPERM;
1137                                 goto abort;
1138                         }
1139                         if (args.tf_out_flags.all != 0) {
1140                                 args.prehandler = &flagged_pre_task_mulout_intr;
1141                                 args.handler = &flagged_task_mulout_intr;
1142                         } else {
1143                                 args.prehandler = &pre_task_mulout_intr;
1144                                 args.handler = &task_mulout_intr;
1145                         }
1146                         err = ide_diag_taskfile(drive, &args, taskout, outbuf);
1147                         break;
1148                 case TASKFILE_OUT:
1149                         if (args.tf_out_flags.all != 0) {
1150                                 args.prehandler = &flagged_pre_task_out_intr;
1151                                 args.handler    = &flagged_task_out_intr;
1152                         } else {
1153                                 args.prehandler = &pre_task_out_intr;
1154                                 args.handler = &task_out_intr;
1155                         }
1156                         err = ide_diag_taskfile(drive, &args, taskout, outbuf);
1157                         break;
1158                 case TASKFILE_MULTI_IN:
1159                         if (!drive->mult_count) {
1160                                 /* (hs): give up if multcount is not set */
1161                                 printk(KERN_ERR "%s: %s Multimode Read failure " \
1162                                         "multcount is not set\n",
1163                                         drive->name, __FUNCTION__);
1164                                 err = -EPERM;
1165                                 goto abort;
1166                         }
1167                         if (args.tf_out_flags.all != 0) {
1168                                 args.handler = &flagged_task_mulin_intr;
1169                         } else {
1170                                 args.handler = &task_mulin_intr;
1171                         }
1172                         err = ide_diag_taskfile(drive, &args, taskin, inbuf);
1173                         break;
1174                 case TASKFILE_IN:
1175                         if (args.tf_out_flags.all != 0) {
1176                                 args.handler = &flagged_task_in_intr;
1177                         } else {
1178                                 args.handler = &task_in_intr;
1179                         }
1180                         err = ide_diag_taskfile(drive, &args, taskin, inbuf);
1181                         break;
1182                 case TASKFILE_NO_DATA:
1183                         if (args.tf_out_flags.all != 0) {
1184                                 args.handler = &flagged_task_no_data_intr;
1185                         } else {
1186                                 args.handler = &task_no_data_intr;
1187                         }
1188                         err = ide_diag_taskfile(drive, &args, 0, NULL);
1189                         break;
1190                 default:
1191                         err = -EFAULT;
1192                         goto abort;
1193         }
1194
1195         memcpy(req_task->io_ports, &(args.tfRegister), HDIO_DRIVE_TASK_HDR_SIZE);
1196         memcpy(req_task->hob_ports, &(args.hobRegister), HDIO_DRIVE_HOB_HDR_SIZE);
1197         req_task->in_flags  = args.tf_in_flags;
1198         req_task->out_flags = args.tf_out_flags;
1199
1200         if (copy_to_user(buf, req_task, tasksize)) {
1201                 err = -EFAULT;
1202                 goto abort;
1203         }
1204         if (taskout) {
1205                 int outtotal = tasksize;
1206                 if (copy_to_user(buf + outtotal, outbuf, taskout)) {
1207                         err = -EFAULT;
1208                         goto abort;
1209                 }
1210         }
1211         if (taskin) {
1212                 int intotal = tasksize + taskout;
1213                 if (copy_to_user(buf + intotal, inbuf, taskin)) {
1214                         err = -EFAULT;
1215                         goto abort;
1216                 }
1217         }
1218 abort:
1219         kfree(req_task);
1220         if (outbuf != NULL)
1221                 kfree(outbuf);
1222         if (inbuf != NULL)
1223                 kfree(inbuf);
1224
1225 //      printk("IDE Taskfile ioctl ended. rc = %i\n", err);
1226
1227         drive->io_32bit = io_32bit;
1228
1229         return err;
1230 }
1231
1232 EXPORT_SYMBOL(ide_taskfile_ioctl);
1233
1234 int ide_wait_cmd (ide_drive_t *drive, u8 cmd, u8 nsect, u8 feature, u8 sectors, u8 *buf)
1235 {
1236         struct request rq;
1237         u8 buffer[4];
1238
1239         if (!buf)
1240                 buf = buffer;
1241         memset(buf, 0, 4 + SECTOR_WORDS * 4 * sectors);
1242         ide_init_drive_cmd(&rq);
1243         rq.buffer = buf;
1244         *buf++ = cmd;
1245         *buf++ = nsect;
1246         *buf++ = feature;
1247         *buf++ = sectors;
1248         return ide_do_drive_cmd(drive, &rq, ide_wait);
1249 }
1250
1251 EXPORT_SYMBOL(ide_wait_cmd);
1252
1253 /*
1254  * FIXME : this needs to map into at taskfile. <andre@linux-ide.org>
1255  */
1256 int ide_cmd_ioctl (ide_drive_t *drive, unsigned int cmd, unsigned long arg)
1257 {
1258         int err = 0;
1259         u8 args[4], *argbuf = args;
1260         u8 xfer_rate = 0;
1261         int argsize = 4;
1262         ide_task_t tfargs;
1263
1264         if (NULL == (void *) arg) {
1265                 struct request rq;
1266                 ide_init_drive_cmd(&rq);
1267                 return ide_do_drive_cmd(drive, &rq, ide_wait);
1268         }
1269
1270         if (copy_from_user(args, (void __user *)arg, 4))
1271                 return -EFAULT;
1272
1273         memset(&tfargs, 0, sizeof(ide_task_t));
1274         tfargs.tfRegister[IDE_FEATURE_OFFSET] = args[2];
1275         tfargs.tfRegister[IDE_NSECTOR_OFFSET] = args[3];
1276         tfargs.tfRegister[IDE_SECTOR_OFFSET]  = args[1];
1277         tfargs.tfRegister[IDE_LCYL_OFFSET]    = 0x00;
1278         tfargs.tfRegister[IDE_HCYL_OFFSET]    = 0x00;
1279         tfargs.tfRegister[IDE_SELECT_OFFSET]  = 0x00;
1280         tfargs.tfRegister[IDE_COMMAND_OFFSET] = args[0];
1281
1282         if (args[3]) {
1283                 argsize = 4 + (SECTOR_WORDS * 4 * args[3]);
1284                 argbuf = kmalloc(argsize, GFP_KERNEL);
1285                 if (argbuf == NULL)
1286                         return -ENOMEM;
1287                 memcpy(argbuf, args, 4);
1288         }
1289         if (set_transfer(drive, &tfargs)) {
1290                 xfer_rate = args[1];
1291                 if (ide_ata66_check(drive, &tfargs))
1292                         goto abort;
1293         }
1294
1295         err = ide_wait_cmd(drive, args[0], args[1], args[2], args[3], argbuf);
1296
1297         if (!err && xfer_rate) {
1298                 /* active-retuning-calls future */
1299                 ide_set_xfer_rate(drive, xfer_rate);
1300                 ide_driveid_update(drive);
1301         }
1302 abort:
1303         if (copy_to_user((void __user *)arg, argbuf, argsize))
1304                 err = -EFAULT;
1305         if (argsize > 4)
1306                 kfree(argbuf);
1307         return err;
1308 }
1309
1310 EXPORT_SYMBOL(ide_cmd_ioctl);
1311
1312 int ide_wait_cmd_task (ide_drive_t *drive, u8 *buf)
1313 {
1314         struct request rq;
1315
1316         ide_init_drive_cmd(&rq);
1317         rq.flags = REQ_DRIVE_TASK;
1318         rq.buffer = buf;
1319         return ide_do_drive_cmd(drive, &rq, ide_wait);
1320 }
1321
1322 EXPORT_SYMBOL(ide_wait_cmd_task);
1323
1324 /*
1325  * FIXME : this needs to map into at taskfile. <andre@linux-ide.org>
1326  */
1327 int ide_task_ioctl (ide_drive_t *drive, unsigned int cmd, unsigned long arg)
1328 {
1329         void __user *p = (void __user *)arg;
1330         int err = 0;
1331         u8 args[7], *argbuf = args;
1332         int argsize = 7;
1333
1334         if (copy_from_user(args, p, 7))
1335                 return -EFAULT;
1336         err = ide_wait_cmd_task(drive, argbuf);
1337         if (copy_to_user(p, argbuf, argsize))
1338                 err = -EFAULT;
1339         return err;
1340 }
1341
1342 EXPORT_SYMBOL(ide_task_ioctl);
1343
1344 /*
1345  * NOTICE: This is additions from IBM to provide a discrete interface,
1346  * for selective taskregister access operations.  Nice JOB Klaus!!!
1347  * Glad to be able to work and co-develop this with you and IBM.
1348  */
1349 ide_startstop_t flagged_taskfile (ide_drive_t *drive, ide_task_t *task)
1350 {
1351         ide_hwif_t *hwif        = HWIF(drive);
1352         task_struct_t *taskfile = (task_struct_t *) task->tfRegister;
1353         hob_struct_t *hobfile   = (hob_struct_t *) task->hobRegister;
1354 #if DEBUG_TASKFILE
1355         u8 status;
1356 #endif
1357
1358
1359 #ifdef CONFIG_IDE_TASK_IOCTL_DEBUG
1360         void debug_taskfile(drive, task);
1361 #endif /* CONFIG_IDE_TASK_IOCTL_DEBUG */
1362
1363         /*
1364          * (ks) Check taskfile in/out flags.
1365          * If set, then execute as it is defined.
1366          * If not set, then define default settings.
1367          * The default values are:
1368          *      write and read all taskfile registers (except data) 
1369          *      write and read the hob registers (sector,nsector,lcyl,hcyl)
1370          */
1371         if (task->tf_out_flags.all == 0) {
1372                 task->tf_out_flags.all = IDE_TASKFILE_STD_OUT_FLAGS;
1373                 if (drive->addressing == 1)
1374                         task->tf_out_flags.all |= (IDE_HOB_STD_OUT_FLAGS << 8);
1375         }
1376
1377         if (task->tf_in_flags.all == 0) {
1378                 task->tf_in_flags.all = IDE_TASKFILE_STD_IN_FLAGS;
1379                 if (drive->addressing == 1)
1380                         task->tf_in_flags.all |= (IDE_HOB_STD_IN_FLAGS  << 8);
1381         }
1382
1383         /* ALL Command Block Executions SHALL clear nIEN, unless otherwise */
1384         if (IDE_CONTROL_REG)
1385                 /* clear nIEN */
1386                 hwif->OUTB(drive->ctl, IDE_CONTROL_REG);
1387         SELECT_MASK(drive, 0);
1388
1389 #if DEBUG_TASKFILE
1390         status = hwif->INB(IDE_STATUS_REG);
1391         if (status & 0x80) {
1392                 printk("flagged_taskfile -> Bad status. Status = %02x. wait 100 usec ...\n", status);
1393                 udelay(100);
1394                 status = hwif->INB(IDE_STATUS_REG);
1395                 printk("flagged_taskfile -> Status = %02x\n", status);
1396         }
1397 #endif
1398
1399         if (task->tf_out_flags.b.data) {
1400                 u16 data =  taskfile->data + (hobfile->data << 8);
1401                 hwif->OUTW(data, IDE_DATA_REG);
1402         }
1403
1404         /* (ks) send hob registers first */
1405         if (task->tf_out_flags.b.nsector_hob)
1406                 hwif->OUTB(hobfile->sector_count, IDE_NSECTOR_REG);
1407         if (task->tf_out_flags.b.sector_hob)
1408                 hwif->OUTB(hobfile->sector_number, IDE_SECTOR_REG);
1409         if (task->tf_out_flags.b.lcyl_hob)
1410                 hwif->OUTB(hobfile->low_cylinder, IDE_LCYL_REG);
1411         if (task->tf_out_flags.b.hcyl_hob)
1412                 hwif->OUTB(hobfile->high_cylinder, IDE_HCYL_REG);
1413
1414         /* (ks) Send now the standard registers */
1415         if (task->tf_out_flags.b.error_feature)
1416                 hwif->OUTB(taskfile->feature, IDE_FEATURE_REG);
1417         /* refers to number of sectors to transfer */
1418         if (task->tf_out_flags.b.nsector)
1419                 hwif->OUTB(taskfile->sector_count, IDE_NSECTOR_REG);
1420         /* refers to sector offset or start sector */
1421         if (task->tf_out_flags.b.sector)
1422                 hwif->OUTB(taskfile->sector_number, IDE_SECTOR_REG);
1423         if (task->tf_out_flags.b.lcyl)
1424                 hwif->OUTB(taskfile->low_cylinder, IDE_LCYL_REG);
1425         if (task->tf_out_flags.b.hcyl)
1426                 hwif->OUTB(taskfile->high_cylinder, IDE_HCYL_REG);
1427
1428         /*
1429          * (ks) In the flagged taskfile approch, we will used all specified
1430          * registers and the register value will not be changed. Except the
1431          * select bit (master/slave) in the drive_head register. We must make
1432          * sure that the desired drive is selected.
1433          */
1434         hwif->OUTB(taskfile->device_head | drive->select.all, IDE_SELECT_REG);
1435         switch(task->data_phase) {
1436
1437                 case TASKFILE_OUT_DMAQ:
1438                 case TASKFILE_OUT_DMA:
1439                         hwif->ide_dma_write(drive);
1440                         break;
1441
1442                 case TASKFILE_IN_DMAQ:
1443                 case TASKFILE_IN_DMA:
1444                         hwif->ide_dma_read(drive);
1445                         break;
1446
1447                 default:
1448                         if (task->handler == NULL)
1449                                 return ide_stopped;
1450
1451                         /* Issue the command */
1452                         ide_execute_command(drive, taskfile->command, task->handler, WAIT_WORSTCASE, NULL);
1453                         if (task->prehandler != NULL)
1454                                 return task->prehandler(drive, HWGROUP(drive)->rq);
1455         }
1456
1457         return ide_started;
1458 }
1459
1460 EXPORT_SYMBOL(flagged_taskfile);
1461
1462 ide_startstop_t flagged_task_no_data_intr (ide_drive_t *drive)
1463 {
1464         ide_hwif_t *hwif = HWIF(drive);
1465         u8 stat;
1466
1467         local_irq_enable();
1468
1469         if (!OK_STAT(stat = hwif->INB(IDE_STATUS_REG), READY_STAT, BAD_STAT)) {
1470                 if (stat & ERR_STAT) {
1471                         return DRIVER(drive)->error(drive, "flagged_task_no_data_intr", stat);
1472                 }
1473                 /*
1474                  * (ks) Unexpected ATA data phase detected.
1475                  * This should not happen. But, it can !
1476                  * I am not sure, which function is best to clean up
1477                  * this situation.  I choose: ide_error(...)
1478                  */
1479                 return DRIVER(drive)->error(drive, "flagged_task_no_data_intr (unexpected phase)", stat); 
1480         }
1481
1482         ide_end_drive_cmd(drive, stat, hwif->INB(IDE_ERROR_REG));
1483
1484         return ide_stopped;
1485 }
1486
1487 /*
1488  * Handler for command with PIO data-in phase
1489  */
1490 ide_startstop_t flagged_task_in_intr (ide_drive_t *drive)
1491 {
1492         ide_hwif_t *hwif        = HWIF(drive);
1493         u8 stat                 = hwif->INB(IDE_STATUS_REG);
1494         struct request *rq      = HWGROUP(drive)->rq;
1495         char *pBuf              = NULL;
1496         int retries             = 5;
1497
1498         if (rq->current_nr_sectors == 0) 
1499                 return DRIVER(drive)->error(drive, "flagged_task_in_intr (no data requested)", stat); 
1500
1501         if (!OK_STAT(stat, DATA_READY, BAD_R_STAT)) {
1502                 if (stat & ERR_STAT) {
1503                         return DRIVER(drive)->error(drive, "flagged_task_in_intr", stat);
1504                 }
1505                 /*
1506                  * (ks) Unexpected ATA data phase detected.
1507                  * This should not happen. But, it can !
1508                  * I am not sure, which function is best to clean up
1509                  * this situation.  I choose: ide_error(...)
1510                  */
1511                 return DRIVER(drive)->error(drive, "flagged_task_in_intr (unexpected data phase)", stat); 
1512         }
1513
1514         pBuf = rq->buffer + ((rq->nr_sectors - rq->current_nr_sectors) * SECTOR_SIZE);
1515         DTF("Read - rq->current_nr_sectors: %d, status: %02x\n", (int) rq->current_nr_sectors, stat);
1516
1517         taskfile_input_data(drive, pBuf, SECTOR_WORDS);
1518
1519         if (--rq->current_nr_sectors != 0) {
1520                 /*
1521                  * (ks) We don't know which command was executed. 
1522                  * So, we wait the 'WORSTCASE' value.
1523                  */
1524                 ide_set_handler(drive, &flagged_task_in_intr,  WAIT_WORSTCASE, NULL);
1525                 return ide_started;
1526         }
1527         /*
1528          * (ks) Last sector was transfered, wait until drive is ready. 
1529          * This can take up to 10 usec. We willl wait max 50 us.
1530          */
1531         while (((stat = hwif->INB(IDE_STATUS_REG)) & BUSY_STAT) && retries--)
1532                 udelay(10);
1533         ide_end_drive_cmd (drive, stat, hwif->INB(IDE_ERROR_REG));
1534
1535         return ide_stopped;
1536 }
1537
1538 ide_startstop_t flagged_task_mulin_intr (ide_drive_t *drive)
1539 {
1540         ide_hwif_t *hwif        = HWIF(drive);
1541         u8 stat                 = hwif->INB(IDE_STATUS_REG);
1542         struct request *rq      = HWGROUP(drive)->rq;
1543         char *pBuf              = NULL;
1544         int retries             = 5;
1545         unsigned int msect, nsect;
1546
1547         if (rq->current_nr_sectors == 0) 
1548                 return DRIVER(drive)->error(drive, "flagged_task_mulin_intr (no data requested)", stat); 
1549
1550         msect = drive->mult_count;
1551         if (msect == 0) 
1552                 return DRIVER(drive)->error(drive, "flagged_task_mulin_intr (multimode not set)", stat); 
1553
1554         if (!OK_STAT(stat, DATA_READY, BAD_R_STAT)) {
1555                 if (stat & ERR_STAT) {
1556                         return DRIVER(drive)->error(drive, "flagged_task_mulin_intr", stat);
1557                 }
1558                 /*
1559                  * (ks) Unexpected ATA data phase detected.
1560                  * This should not happen. But, it can !
1561                  * I am not sure, which function is best to clean up
1562                  * this situation.  I choose: ide_error(...)
1563                  */
1564                 return DRIVER(drive)->error(drive, "flagged_task_mulin_intr (unexpected data phase)", stat); 
1565         }
1566
1567         nsect = (rq->current_nr_sectors > msect) ? msect : rq->current_nr_sectors;
1568         pBuf = rq->buffer + ((rq->nr_sectors - rq->current_nr_sectors) * SECTOR_SIZE);
1569
1570         DTF("Multiread: %p, nsect: %d , rq->current_nr_sectors: %ld\n",
1571             pBuf, nsect, rq->current_nr_sectors);
1572
1573         taskfile_input_data(drive, pBuf, nsect * SECTOR_WORDS);
1574
1575         rq->current_nr_sectors -= nsect;
1576         if (rq->current_nr_sectors != 0) {
1577                 /*
1578                  * (ks) We don't know which command was executed. 
1579                  * So, we wait the 'WORSTCASE' value.
1580                  */
1581                 ide_set_handler(drive, &flagged_task_mulin_intr,  WAIT_WORSTCASE, NULL);
1582                 return ide_started;
1583         }
1584
1585         /*
1586          * (ks) Last sector was transfered, wait until drive is ready. 
1587          * This can take up to 10 usec. We willl wait max 50 us.
1588          */
1589         while (((stat = hwif->INB(IDE_STATUS_REG)) & BUSY_STAT) && retries--)
1590                 udelay(10);
1591         ide_end_drive_cmd (drive, stat, hwif->INB(IDE_ERROR_REG));
1592
1593         return ide_stopped;
1594 }
1595
1596 /*
1597  * Pre handler for command with PIO data-out phase
1598  */
1599 ide_startstop_t flagged_pre_task_out_intr (ide_drive_t *drive, struct request *rq)
1600 {
1601         ide_hwif_t *hwif        = HWIF(drive);
1602         u8 stat                 = hwif->INB(IDE_STATUS_REG);
1603         ide_startstop_t startstop;
1604
1605         if (!rq->current_nr_sectors) {
1606                 return DRIVER(drive)->error(drive, "flagged_pre_task_out_intr (write data not specified)", stat);
1607         }
1608
1609         if (ide_wait_stat(&startstop, drive, DATA_READY,
1610                         BAD_W_STAT, WAIT_DRQ)) {
1611                 printk(KERN_ERR "%s: No DRQ bit after issuing write command.\n", drive->name);
1612                 return startstop;
1613         }
1614
1615         taskfile_output_data(drive, rq->buffer, SECTOR_WORDS);
1616         --rq->current_nr_sectors;
1617
1618         return ide_started;
1619 }
1620
1621 ide_startstop_t flagged_task_out_intr (ide_drive_t *drive)
1622 {
1623         ide_hwif_t *hwif        = HWIF(drive);
1624         u8 stat                 = hwif->INB(IDE_STATUS_REG);
1625         struct request *rq      = HWGROUP(drive)->rq;
1626         char *pBuf              = NULL;
1627
1628         if (!OK_STAT(stat, DRIVE_READY, BAD_W_STAT)) 
1629                 return DRIVER(drive)->error(drive, "flagged_task_out_intr", stat);
1630         
1631         if (!rq->current_nr_sectors) { 
1632                 ide_end_drive_cmd (drive, stat, hwif->INB(IDE_ERROR_REG));
1633                 return ide_stopped;
1634         }
1635
1636         if (!OK_STAT(stat, DATA_READY, BAD_W_STAT)) {
1637                 /*
1638                  * (ks) Unexpected ATA data phase detected.
1639                  * This should not happen. But, it can !
1640                  * I am not sure, which function is best to clean up
1641                  * this situation.  I choose: ide_error(...)
1642                  */
1643                 return DRIVER(drive)->error(drive, "flagged_task_out_intr (unexpected data phase)", stat); 
1644         }
1645
1646         pBuf = rq->buffer + ((rq->nr_sectors - rq->current_nr_sectors) * SECTOR_SIZE);
1647         DTF("Write - rq->current_nr_sectors: %d, status: %02x\n",
1648                 (int) rq->current_nr_sectors, stat);
1649
1650         taskfile_output_data(drive, pBuf, SECTOR_WORDS);
1651         --rq->current_nr_sectors;
1652
1653         /*
1654          * (ks) We don't know which command was executed. 
1655          * So, we wait the 'WORSTCASE' value.
1656          */
1657         ide_set_handler(drive, &flagged_task_out_intr, WAIT_WORSTCASE, NULL);
1658
1659         return ide_started;
1660 }
1661
1662 ide_startstop_t flagged_pre_task_mulout_intr (ide_drive_t *drive, struct request *rq)
1663 {
1664         ide_hwif_t *hwif        = HWIF(drive);
1665         u8 stat                 = hwif->INB(IDE_STATUS_REG);
1666         char *pBuf              = NULL;
1667         ide_startstop_t startstop;
1668         unsigned int msect, nsect;
1669
1670         if (!rq->current_nr_sectors) 
1671                 return DRIVER(drive)->error(drive, "flagged_pre_task_mulout_intr (write data not specified)", stat);
1672
1673         msect = drive->mult_count;
1674         if (msect == 0)
1675                 return DRIVER(drive)->error(drive, "flagged_pre_task_mulout_intr (multimode not set)", stat);
1676
1677         if (ide_wait_stat(&startstop, drive, DATA_READY,
1678                         BAD_W_STAT, WAIT_DRQ)) {
1679                 printk(KERN_ERR "%s: No DRQ bit after issuing write command.\n", drive->name);
1680                 return startstop;
1681         }
1682
1683         nsect = (rq->current_nr_sectors > msect) ? msect : rq->current_nr_sectors;
1684         pBuf = rq->buffer + ((rq->nr_sectors - rq->current_nr_sectors) * SECTOR_SIZE);
1685         DTF("Multiwrite: %p, nsect: %d , rq->current_nr_sectors: %ld\n",
1686             pBuf, nsect, rq->current_nr_sectors);
1687
1688         taskfile_output_data(drive, pBuf, nsect * SECTOR_WORDS);
1689
1690         rq->current_nr_sectors -= nsect;
1691
1692         return ide_started;
1693 }
1694
1695 ide_startstop_t flagged_task_mulout_intr (ide_drive_t *drive)
1696 {
1697         ide_hwif_t *hwif        = HWIF(drive);
1698         u8 stat                 = hwif->INB(IDE_STATUS_REG);
1699         struct request *rq      = HWGROUP(drive)->rq;
1700         char *pBuf              = NULL;
1701         unsigned int msect, nsect;
1702
1703         msect = drive->mult_count;
1704         if (msect == 0)
1705                 return DRIVER(drive)->error(drive, "flagged_task_mulout_intr (multimode not set)", stat);
1706
1707         if (!OK_STAT(stat, DRIVE_READY, BAD_W_STAT)) 
1708                 return DRIVER(drive)->error(drive, "flagged_task_mulout_intr", stat);
1709         
1710         if (!rq->current_nr_sectors) { 
1711                 ide_end_drive_cmd (drive, stat, hwif->INB(IDE_ERROR_REG));
1712                 return ide_stopped;
1713         }
1714
1715         if (!OK_STAT(stat, DATA_READY, BAD_W_STAT)) {
1716                 /*
1717                  * (ks) Unexpected ATA data phase detected.
1718                  * This should not happen. But, it can !
1719                  * I am not sure, which function is best to clean up
1720                  * this situation.  I choose: ide_error(...)
1721                  */
1722                 return DRIVER(drive)->error(drive, "flagged_task_mulout_intr (unexpected data phase)", stat); 
1723         }
1724
1725         nsect = (rq->current_nr_sectors > msect) ? msect : rq->current_nr_sectors;
1726         pBuf = rq->buffer + ((rq->nr_sectors - rq->current_nr_sectors) * SECTOR_SIZE);
1727         DTF("Multiwrite: %p, nsect: %d , rq->current_nr_sectors: %ld\n",
1728             pBuf, nsect, rq->current_nr_sectors);
1729
1730         taskfile_output_data(drive, pBuf, nsect * SECTOR_WORDS);
1731         rq->current_nr_sectors -= nsect;
1732
1733         /*
1734          * (ks) We don't know which command was executed. 
1735          * So, we wait the 'WORSTCASE' value.
1736          */
1737         ide_set_handler(drive, &flagged_task_mulout_intr, WAIT_WORSTCASE, NULL);
1738
1739         return ide_started;
1740 }