ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[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
1056 //      printk("IDE Taskfile ...\n");
1057
1058         req_task = kmalloc(tasksize, GFP_KERNEL);
1059         if (req_task == NULL) return -ENOMEM;
1060         memset(req_task, 0, tasksize);
1061         if (copy_from_user(req_task, (void *) arg, tasksize)) {
1062                 kfree(req_task);
1063                 return -EFAULT;
1064         }
1065
1066         taskout = (int) req_task->out_size;
1067         taskin  = (int) req_task->in_size;
1068
1069         if (taskout) {
1070                 int outtotal = tasksize;
1071                 outbuf = kmalloc(taskout, GFP_KERNEL);
1072                 if (outbuf == NULL) {
1073                         err = -ENOMEM;
1074                         goto abort;
1075                 }
1076                 memset(outbuf, 0, taskout);
1077                 if (copy_from_user(outbuf, (void *)arg + outtotal, taskout)) {
1078                         err = -EFAULT;
1079                         goto abort;
1080                 }
1081         }
1082
1083         if (taskin) {
1084                 int intotal = tasksize + taskout;
1085                 inbuf = kmalloc(taskin, GFP_KERNEL);
1086                 if (inbuf == NULL) {
1087                         err = -ENOMEM;
1088                         goto abort;
1089                 }
1090                 memset(inbuf, 0, taskin);
1091                 if (copy_from_user(inbuf, (void *)arg + intotal , taskin)) {
1092                         err = -EFAULT;
1093                         goto abort;
1094                 }
1095         }
1096
1097         memset(&args, 0, sizeof(ide_task_t));
1098         memcpy(argsptr, req_task->io_ports, HDIO_DRIVE_TASK_HDR_SIZE);
1099         memcpy(hobsptr, req_task->hob_ports, HDIO_DRIVE_HOB_HDR_SIZE);
1100
1101         args.tf_in_flags  = req_task->in_flags;
1102         args.tf_out_flags = req_task->out_flags;
1103         args.data_phase   = req_task->data_phase;
1104         args.command_type = req_task->req_cmd;
1105
1106         drive->io_32bit = 0;
1107         switch(req_task->data_phase) {
1108                 case TASKFILE_OUT_DMAQ:
1109                 case TASKFILE_OUT_DMA:
1110                         err = ide_diag_taskfile(drive, &args, taskout, outbuf);
1111                         break;
1112                 case TASKFILE_IN_DMAQ:
1113                 case TASKFILE_IN_DMA:
1114                         err = ide_diag_taskfile(drive, &args, taskin, inbuf);
1115                         break;
1116                 case TASKFILE_IN_OUT:
1117 #if 0
1118                         args.prehandler = &pre_task_out_intr;
1119                         args.handler = &task_out_intr;
1120                         err = ide_diag_taskfile(drive, &args, taskout, outbuf);
1121                         args.prehandler = NULL;
1122                         args.handler = &task_in_intr;
1123                         err = ide_diag_taskfile(drive, &args, taskin, inbuf);
1124                         break;
1125 #else
1126                         err = -EFAULT;
1127                         goto abort;
1128 #endif
1129                 case TASKFILE_MULTI_OUT:
1130                         if (!drive->mult_count) {
1131                                 /* (hs): give up if multcount is not set */
1132                                 printk(KERN_ERR "%s: %s Multimode Write " \
1133                                         "multcount is not set\n",
1134                                         drive->name, __FUNCTION__);
1135                                 err = -EPERM;
1136                                 goto abort;
1137                         }
1138                         if (args.tf_out_flags.all != 0) {
1139                                 args.prehandler = &flagged_pre_task_mulout_intr;
1140                                 args.handler = &flagged_task_mulout_intr;
1141                         } else {
1142                                 args.prehandler = &pre_task_mulout_intr;
1143                                 args.handler = &task_mulout_intr;
1144                         }
1145                         err = ide_diag_taskfile(drive, &args, taskout, outbuf);
1146                         break;
1147                 case TASKFILE_OUT:
1148                         if (args.tf_out_flags.all != 0) {
1149                                 args.prehandler = &flagged_pre_task_out_intr;
1150                                 args.handler    = &flagged_task_out_intr;
1151                         } else {
1152                                 args.prehandler = &pre_task_out_intr;
1153                                 args.handler = &task_out_intr;
1154                         }
1155                         err = ide_diag_taskfile(drive, &args, taskout, outbuf);
1156                         break;
1157                 case TASKFILE_MULTI_IN:
1158                         if (!drive->mult_count) {
1159                                 /* (hs): give up if multcount is not set */
1160                                 printk(KERN_ERR "%s: %s Multimode Read failure " \
1161                                         "multcount is not set\n",
1162                                         drive->name, __FUNCTION__);
1163                                 err = -EPERM;
1164                                 goto abort;
1165                         }
1166                         if (args.tf_out_flags.all != 0) {
1167                                 args.handler = &flagged_task_mulin_intr;
1168                         } else {
1169                                 args.handler = &task_mulin_intr;
1170                         }
1171                         err = ide_diag_taskfile(drive, &args, taskin, inbuf);
1172                         break;
1173                 case TASKFILE_IN:
1174                         if (args.tf_out_flags.all != 0) {
1175                                 args.handler = &flagged_task_in_intr;
1176                         } else {
1177                                 args.handler = &task_in_intr;
1178                         }
1179                         err = ide_diag_taskfile(drive, &args, taskin, inbuf);
1180                         break;
1181                 case TASKFILE_NO_DATA:
1182                         if (args.tf_out_flags.all != 0) {
1183                                 args.handler = &flagged_task_no_data_intr;
1184                         } else {
1185                                 args.handler = &task_no_data_intr;
1186                         }
1187                         err = ide_diag_taskfile(drive, &args, 0, NULL);
1188                         break;
1189                 default:
1190                         err = -EFAULT;
1191                         goto abort;
1192         }
1193
1194         memcpy(req_task->io_ports, &(args.tfRegister), HDIO_DRIVE_TASK_HDR_SIZE);
1195         memcpy(req_task->hob_ports, &(args.hobRegister), HDIO_DRIVE_HOB_HDR_SIZE);
1196         req_task->in_flags  = args.tf_in_flags;
1197         req_task->out_flags = args.tf_out_flags;
1198
1199         if (copy_to_user((void *)arg, req_task, tasksize)) {
1200                 err = -EFAULT;
1201                 goto abort;
1202         }
1203         if (taskout) {
1204                 int outtotal = tasksize;
1205                 if (copy_to_user((void *)arg+outtotal, outbuf, taskout)) {
1206                         err = -EFAULT;
1207                         goto abort;
1208                 }
1209         }
1210         if (taskin) {
1211                 int intotal = tasksize + taskout;
1212                 if (copy_to_user((void *)arg+intotal, inbuf, taskin)) {
1213                         err = -EFAULT;
1214                         goto abort;
1215                 }
1216         }
1217 abort:
1218         kfree(req_task);
1219         if (outbuf != NULL)
1220                 kfree(outbuf);
1221         if (inbuf != NULL)
1222                 kfree(inbuf);
1223
1224 //      printk("IDE Taskfile ioctl ended. rc = %i\n", err);
1225
1226         drive->io_32bit = io_32bit;
1227
1228         return err;
1229 }
1230
1231 EXPORT_SYMBOL(ide_taskfile_ioctl);
1232
1233 int ide_wait_cmd (ide_drive_t *drive, u8 cmd, u8 nsect, u8 feature, u8 sectors, u8 *buf)
1234 {
1235         struct request rq;
1236         u8 buffer[4];
1237
1238         if (!buf)
1239                 buf = buffer;
1240         memset(buf, 0, 4 + SECTOR_WORDS * 4 * sectors);
1241         ide_init_drive_cmd(&rq);
1242         rq.buffer = buf;
1243         *buf++ = cmd;
1244         *buf++ = nsect;
1245         *buf++ = feature;
1246         *buf++ = sectors;
1247         return ide_do_drive_cmd(drive, &rq, ide_wait);
1248 }
1249
1250 EXPORT_SYMBOL(ide_wait_cmd);
1251
1252 /*
1253  * FIXME : this needs to map into at taskfile. <andre@linux-ide.org>
1254  */
1255 int ide_cmd_ioctl (ide_drive_t *drive, unsigned int cmd, unsigned long arg)
1256 {
1257         int err = 0;
1258         u8 args[4], *argbuf = args;
1259         u8 xfer_rate = 0;
1260         int argsize = 4;
1261         ide_task_t tfargs;
1262
1263         if (NULL == (void *) arg) {
1264                 struct request rq;
1265                 ide_init_drive_cmd(&rq);
1266                 return ide_do_drive_cmd(drive, &rq, ide_wait);
1267         }
1268
1269         if (copy_from_user(args, (void *)arg, 4))
1270                 return -EFAULT;
1271
1272         memset(&tfargs, 0, sizeof(ide_task_t));
1273         tfargs.tfRegister[IDE_FEATURE_OFFSET] = args[2];
1274         tfargs.tfRegister[IDE_NSECTOR_OFFSET] = args[3];
1275         tfargs.tfRegister[IDE_SECTOR_OFFSET]  = args[1];
1276         tfargs.tfRegister[IDE_LCYL_OFFSET]    = 0x00;
1277         tfargs.tfRegister[IDE_HCYL_OFFSET]    = 0x00;
1278         tfargs.tfRegister[IDE_SELECT_OFFSET]  = 0x00;
1279         tfargs.tfRegister[IDE_COMMAND_OFFSET] = args[0];
1280
1281         if (args[3]) {
1282                 argsize = 4 + (SECTOR_WORDS * 4 * args[3]);
1283                 argbuf = kmalloc(argsize, GFP_KERNEL);
1284                 if (argbuf == NULL)
1285                         return -ENOMEM;
1286                 memcpy(argbuf, args, 4);
1287         }
1288         if (set_transfer(drive, &tfargs)) {
1289                 xfer_rate = args[1];
1290                 if (ide_ata66_check(drive, &tfargs))
1291                         goto abort;
1292         }
1293
1294         err = ide_wait_cmd(drive, args[0], args[1], args[2], args[3], argbuf);
1295
1296         if (!err && xfer_rate) {
1297                 /* active-retuning-calls future */
1298                 ide_set_xfer_rate(drive, xfer_rate);
1299                 ide_driveid_update(drive);
1300         }
1301 abort:
1302         if (copy_to_user((void *)arg, argbuf, argsize))
1303                 err = -EFAULT;
1304         if (argsize > 4)
1305                 kfree(argbuf);
1306         return err;
1307 }
1308
1309 EXPORT_SYMBOL(ide_cmd_ioctl);
1310
1311 int ide_wait_cmd_task (ide_drive_t *drive, u8 *buf)
1312 {
1313         struct request rq;
1314
1315         ide_init_drive_cmd(&rq);
1316         rq.flags = REQ_DRIVE_TASK;
1317         rq.buffer = buf;
1318         return ide_do_drive_cmd(drive, &rq, ide_wait);
1319 }
1320
1321 EXPORT_SYMBOL(ide_wait_cmd_task);
1322
1323 /*
1324  * FIXME : this needs to map into at taskfile. <andre@linux-ide.org>
1325  */
1326 int ide_task_ioctl (ide_drive_t *drive, unsigned int cmd, unsigned long arg)
1327 {
1328         int err = 0;
1329         u8 args[7], *argbuf = args;
1330         int argsize = 7;
1331
1332         if (copy_from_user(args, (void *)arg, 7))
1333                 return -EFAULT;
1334         err = ide_wait_cmd_task(drive, argbuf);
1335         if (copy_to_user((void *)arg, argbuf, argsize))
1336                 err = -EFAULT;
1337         return err;
1338 }
1339
1340 EXPORT_SYMBOL(ide_task_ioctl);
1341
1342 /*
1343  * NOTICE: This is additions from IBM to provide a discrete interface,
1344  * for selective taskregister access operations.  Nice JOB Klaus!!!
1345  * Glad to be able to work and co-develop this with you and IBM.
1346  */
1347 ide_startstop_t flagged_taskfile (ide_drive_t *drive, ide_task_t *task)
1348 {
1349         ide_hwif_t *hwif        = HWIF(drive);
1350         task_struct_t *taskfile = (task_struct_t *) task->tfRegister;
1351         hob_struct_t *hobfile   = (hob_struct_t *) task->hobRegister;
1352 #if DEBUG_TASKFILE
1353         u8 status;
1354 #endif
1355
1356
1357 #ifdef CONFIG_IDE_TASK_IOCTL_DEBUG
1358         void debug_taskfile(drive, task);
1359 #endif /* CONFIG_IDE_TASK_IOCTL_DEBUG */
1360
1361         /*
1362          * (ks) Check taskfile in/out flags.
1363          * If set, then execute as it is defined.
1364          * If not set, then define default settings.
1365          * The default values are:
1366          *      write and read all taskfile registers (except data) 
1367          *      write and read the hob registers (sector,nsector,lcyl,hcyl)
1368          */
1369         if (task->tf_out_flags.all == 0) {
1370                 task->tf_out_flags.all = IDE_TASKFILE_STD_OUT_FLAGS;
1371                 if (drive->addressing == 1)
1372                         task->tf_out_flags.all |= (IDE_HOB_STD_OUT_FLAGS << 8);
1373         }
1374
1375         if (task->tf_in_flags.all == 0) {
1376                 task->tf_in_flags.all = IDE_TASKFILE_STD_IN_FLAGS;
1377                 if (drive->addressing == 1)
1378                         task->tf_in_flags.all |= (IDE_HOB_STD_IN_FLAGS  << 8);
1379         }
1380
1381         /* ALL Command Block Executions SHALL clear nIEN, unless otherwise */
1382         if (IDE_CONTROL_REG)
1383                 /* clear nIEN */
1384                 hwif->OUTB(drive->ctl, IDE_CONTROL_REG);
1385         SELECT_MASK(drive, 0);
1386
1387 #if DEBUG_TASKFILE
1388         status = hwif->INB(IDE_STATUS_REG);
1389         if (status & 0x80) {
1390                 printk("flagged_taskfile -> Bad status. Status = %02x. wait 100 usec ...\n", status);
1391                 udelay(100);
1392                 status = hwif->INB(IDE_STATUS_REG);
1393                 printk("flagged_taskfile -> Status = %02x\n", status);
1394         }
1395 #endif
1396
1397         if (task->tf_out_flags.b.data) {
1398                 u16 data =  taskfile->data + (hobfile->data << 8);
1399                 hwif->OUTW(data, IDE_DATA_REG);
1400         }
1401
1402         /* (ks) send hob registers first */
1403         if (task->tf_out_flags.b.nsector_hob)
1404                 hwif->OUTB(hobfile->sector_count, IDE_NSECTOR_REG);
1405         if (task->tf_out_flags.b.sector_hob)
1406                 hwif->OUTB(hobfile->sector_number, IDE_SECTOR_REG);
1407         if (task->tf_out_flags.b.lcyl_hob)
1408                 hwif->OUTB(hobfile->low_cylinder, IDE_LCYL_REG);
1409         if (task->tf_out_flags.b.hcyl_hob)
1410                 hwif->OUTB(hobfile->high_cylinder, IDE_HCYL_REG);
1411
1412         /* (ks) Send now the standard registers */
1413         if (task->tf_out_flags.b.error_feature)
1414                 hwif->OUTB(taskfile->feature, IDE_FEATURE_REG);
1415         /* refers to number of sectors to transfer */
1416         if (task->tf_out_flags.b.nsector)
1417                 hwif->OUTB(taskfile->sector_count, IDE_NSECTOR_REG);
1418         /* refers to sector offset or start sector */
1419         if (task->tf_out_flags.b.sector)
1420                 hwif->OUTB(taskfile->sector_number, IDE_SECTOR_REG);
1421         if (task->tf_out_flags.b.lcyl)
1422                 hwif->OUTB(taskfile->low_cylinder, IDE_LCYL_REG);
1423         if (task->tf_out_flags.b.hcyl)
1424                 hwif->OUTB(taskfile->high_cylinder, IDE_HCYL_REG);
1425
1426         /*
1427          * (ks) In the flagged taskfile approch, we will used all specified
1428          * registers and the register value will not be changed. Except the
1429          * select bit (master/slave) in the drive_head register. We must make
1430          * sure that the desired drive is selected.
1431          */
1432         hwif->OUTB(taskfile->device_head | drive->select.all, IDE_SELECT_REG);
1433         switch(task->data_phase) {
1434
1435                 case TASKFILE_OUT_DMAQ:
1436                 case TASKFILE_OUT_DMA:
1437                         hwif->ide_dma_write(drive);
1438                         break;
1439
1440                 case TASKFILE_IN_DMAQ:
1441                 case TASKFILE_IN_DMA:
1442                         hwif->ide_dma_read(drive);
1443                         break;
1444
1445                 default:
1446                         if (task->handler == NULL)
1447                                 return ide_stopped;
1448
1449                         /* Issue the command */
1450                         ide_execute_command(drive, taskfile->command, task->handler, WAIT_WORSTCASE, NULL);
1451                         if (task->prehandler != NULL)
1452                                 return task->prehandler(drive, HWGROUP(drive)->rq);
1453         }
1454
1455         return ide_started;
1456 }
1457
1458 EXPORT_SYMBOL(flagged_taskfile);
1459
1460 ide_startstop_t flagged_task_no_data_intr (ide_drive_t *drive)
1461 {
1462         ide_hwif_t *hwif = HWIF(drive);
1463         u8 stat;
1464
1465         local_irq_enable();
1466
1467         if (!OK_STAT(stat = hwif->INB(IDE_STATUS_REG), READY_STAT, BAD_STAT)) {
1468                 if (stat & ERR_STAT) {
1469                         return DRIVER(drive)->error(drive, "flagged_task_no_data_intr", stat);
1470                 }
1471                 /*
1472                  * (ks) Unexpected ATA data phase detected.
1473                  * This should not happen. But, it can !
1474                  * I am not sure, which function is best to clean up
1475                  * this situation.  I choose: ide_error(...)
1476                  */
1477                 return DRIVER(drive)->error(drive, "flagged_task_no_data_intr (unexpected phase)", stat); 
1478         }
1479
1480         ide_end_drive_cmd(drive, stat, hwif->INB(IDE_ERROR_REG));
1481
1482         return ide_stopped;
1483 }
1484
1485 /*
1486  * Handler for command with PIO data-in phase
1487  */
1488 ide_startstop_t flagged_task_in_intr (ide_drive_t *drive)
1489 {
1490         ide_hwif_t *hwif        = HWIF(drive);
1491         u8 stat                 = hwif->INB(IDE_STATUS_REG);
1492         struct request *rq      = HWGROUP(drive)->rq;
1493         char *pBuf              = NULL;
1494         int retries             = 5;
1495
1496         if (rq->current_nr_sectors == 0) 
1497                 return DRIVER(drive)->error(drive, "flagged_task_in_intr (no data requested)", stat); 
1498
1499         if (!OK_STAT(stat, DATA_READY, BAD_R_STAT)) {
1500                 if (stat & ERR_STAT) {
1501                         return DRIVER(drive)->error(drive, "flagged_task_in_intr", stat);
1502                 }
1503                 /*
1504                  * (ks) Unexpected ATA data phase detected.
1505                  * This should not happen. But, it can !
1506                  * I am not sure, which function is best to clean up
1507                  * this situation.  I choose: ide_error(...)
1508                  */
1509                 return DRIVER(drive)->error(drive, "flagged_task_in_intr (unexpected data phase)", stat); 
1510         }
1511
1512         pBuf = rq->buffer + ((rq->nr_sectors - rq->current_nr_sectors) * SECTOR_SIZE);
1513         DTF("Read - rq->current_nr_sectors: %d, status: %02x\n", (int) rq->current_nr_sectors, stat);
1514
1515         taskfile_input_data(drive, pBuf, SECTOR_WORDS);
1516
1517         if (--rq->current_nr_sectors != 0) {
1518                 /*
1519                  * (ks) We don't know which command was executed. 
1520                  * So, we wait the 'WORSTCASE' value.
1521                  */
1522                 ide_set_handler(drive, &flagged_task_in_intr,  WAIT_WORSTCASE, NULL);
1523                 return ide_started;
1524         }
1525         /*
1526          * (ks) Last sector was transfered, wait until drive is ready. 
1527          * This can take up to 10 usec. We willl wait max 50 us.
1528          */
1529         while (((stat = hwif->INB(IDE_STATUS_REG)) & BUSY_STAT) && retries--)
1530                 udelay(10);
1531         ide_end_drive_cmd (drive, stat, hwif->INB(IDE_ERROR_REG));
1532
1533         return ide_stopped;
1534 }
1535
1536 ide_startstop_t flagged_task_mulin_intr (ide_drive_t *drive)
1537 {
1538         ide_hwif_t *hwif        = HWIF(drive);
1539         u8 stat                 = hwif->INB(IDE_STATUS_REG);
1540         struct request *rq      = HWGROUP(drive)->rq;
1541         char *pBuf              = NULL;
1542         int retries             = 5;
1543         unsigned int msect, nsect;
1544
1545         if (rq->current_nr_sectors == 0) 
1546                 return DRIVER(drive)->error(drive, "flagged_task_mulin_intr (no data requested)", stat); 
1547
1548         msect = drive->mult_count;
1549         if (msect == 0) 
1550                 return DRIVER(drive)->error(drive, "flagged_task_mulin_intr (multimode not set)", stat); 
1551
1552         if (!OK_STAT(stat, DATA_READY, BAD_R_STAT)) {
1553                 if (stat & ERR_STAT) {
1554                         return DRIVER(drive)->error(drive, "flagged_task_mulin_intr", stat);
1555                 }
1556                 /*
1557                  * (ks) Unexpected ATA data phase detected.
1558                  * This should not happen. But, it can !
1559                  * I am not sure, which function is best to clean up
1560                  * this situation.  I choose: ide_error(...)
1561                  */
1562                 return DRIVER(drive)->error(drive, "flagged_task_mulin_intr (unexpected data phase)", stat); 
1563         }
1564
1565         nsect = (rq->current_nr_sectors > msect) ? msect : rq->current_nr_sectors;
1566         pBuf = rq->buffer + ((rq->nr_sectors - rq->current_nr_sectors) * SECTOR_SIZE);
1567
1568         DTF("Multiread: %p, nsect: %d , rq->current_nr_sectors: %ld\n",
1569             pBuf, nsect, rq->current_nr_sectors);
1570
1571         taskfile_input_data(drive, pBuf, nsect * SECTOR_WORDS);
1572
1573         rq->current_nr_sectors -= nsect;
1574         if (rq->current_nr_sectors != 0) {
1575                 /*
1576                  * (ks) We don't know which command was executed. 
1577                  * So, we wait the 'WORSTCASE' value.
1578                  */
1579                 ide_set_handler(drive, &flagged_task_mulin_intr,  WAIT_WORSTCASE, NULL);
1580                 return ide_started;
1581         }
1582
1583         /*
1584          * (ks) Last sector was transfered, wait until drive is ready. 
1585          * This can take up to 10 usec. We willl wait max 50 us.
1586          */
1587         while (((stat = hwif->INB(IDE_STATUS_REG)) & BUSY_STAT) && retries--)
1588                 udelay(10);
1589         ide_end_drive_cmd (drive, stat, hwif->INB(IDE_ERROR_REG));
1590
1591         return ide_stopped;
1592 }
1593
1594 /*
1595  * Pre handler for command with PIO data-out phase
1596  */
1597 ide_startstop_t flagged_pre_task_out_intr (ide_drive_t *drive, struct request *rq)
1598 {
1599         ide_hwif_t *hwif        = HWIF(drive);
1600         u8 stat                 = hwif->INB(IDE_STATUS_REG);
1601         ide_startstop_t startstop;
1602
1603         if (!rq->current_nr_sectors) {
1604                 return DRIVER(drive)->error(drive, "flagged_pre_task_out_intr (write data not specified)", stat);
1605         }
1606
1607         if (ide_wait_stat(&startstop, drive, DATA_READY,
1608                         BAD_W_STAT, WAIT_DRQ)) {
1609                 printk(KERN_ERR "%s: No DRQ bit after issuing write command.\n", drive->name);
1610                 return startstop;
1611         }
1612
1613         taskfile_output_data(drive, rq->buffer, SECTOR_WORDS);
1614         --rq->current_nr_sectors;
1615
1616         return ide_started;
1617 }
1618
1619 ide_startstop_t flagged_task_out_intr (ide_drive_t *drive)
1620 {
1621         ide_hwif_t *hwif        = HWIF(drive);
1622         u8 stat                 = hwif->INB(IDE_STATUS_REG);
1623         struct request *rq      = HWGROUP(drive)->rq;
1624         char *pBuf              = NULL;
1625
1626         if (!OK_STAT(stat, DRIVE_READY, BAD_W_STAT)) 
1627                 return DRIVER(drive)->error(drive, "flagged_task_out_intr", stat);
1628         
1629         if (!rq->current_nr_sectors) { 
1630                 ide_end_drive_cmd (drive, stat, hwif->INB(IDE_ERROR_REG));
1631                 return ide_stopped;
1632         }
1633
1634         if (!OK_STAT(stat, DATA_READY, BAD_W_STAT)) {
1635                 /*
1636                  * (ks) Unexpected ATA data phase detected.
1637                  * This should not happen. But, it can !
1638                  * I am not sure, which function is best to clean up
1639                  * this situation.  I choose: ide_error(...)
1640                  */
1641                 return DRIVER(drive)->error(drive, "flagged_task_out_intr (unexpected data phase)", stat); 
1642         }
1643
1644         pBuf = rq->buffer + ((rq->nr_sectors - rq->current_nr_sectors) * SECTOR_SIZE);
1645         DTF("Write - rq->current_nr_sectors: %d, status: %02x\n",
1646                 (int) rq->current_nr_sectors, stat);
1647
1648         taskfile_output_data(drive, pBuf, SECTOR_WORDS);
1649         --rq->current_nr_sectors;
1650
1651         /*
1652          * (ks) We don't know which command was executed. 
1653          * So, we wait the 'WORSTCASE' value.
1654          */
1655         ide_set_handler(drive, &flagged_task_out_intr, WAIT_WORSTCASE, NULL);
1656
1657         return ide_started;
1658 }
1659
1660 ide_startstop_t flagged_pre_task_mulout_intr (ide_drive_t *drive, struct request *rq)
1661 {
1662         ide_hwif_t *hwif        = HWIF(drive);
1663         u8 stat                 = hwif->INB(IDE_STATUS_REG);
1664         char *pBuf              = NULL;
1665         ide_startstop_t startstop;
1666         unsigned int msect, nsect;
1667
1668         if (!rq->current_nr_sectors) 
1669                 return DRIVER(drive)->error(drive, "flagged_pre_task_mulout_intr (write data not specified)", stat);
1670
1671         msect = drive->mult_count;
1672         if (msect == 0)
1673                 return DRIVER(drive)->error(drive, "flagged_pre_task_mulout_intr (multimode not set)", stat);
1674
1675         if (ide_wait_stat(&startstop, drive, DATA_READY,
1676                         BAD_W_STAT, WAIT_DRQ)) {
1677                 printk(KERN_ERR "%s: No DRQ bit after issuing write command.\n", drive->name);
1678                 return startstop;
1679         }
1680
1681         nsect = (rq->current_nr_sectors > msect) ? msect : rq->current_nr_sectors;
1682         pBuf = rq->buffer + ((rq->nr_sectors - rq->current_nr_sectors) * SECTOR_SIZE);
1683         DTF("Multiwrite: %p, nsect: %d , rq->current_nr_sectors: %ld\n",
1684             pBuf, nsect, rq->current_nr_sectors);
1685
1686         taskfile_output_data(drive, pBuf, nsect * SECTOR_WORDS);
1687
1688         rq->current_nr_sectors -= nsect;
1689
1690         return ide_started;
1691 }
1692
1693 ide_startstop_t flagged_task_mulout_intr (ide_drive_t *drive)
1694 {
1695         ide_hwif_t *hwif        = HWIF(drive);
1696         u8 stat                 = hwif->INB(IDE_STATUS_REG);
1697         struct request *rq      = HWGROUP(drive)->rq;
1698         char *pBuf              = NULL;
1699         unsigned int msect, nsect;
1700
1701         msect = drive->mult_count;
1702         if (msect == 0)
1703                 return DRIVER(drive)->error(drive, "flagged_task_mulout_intr (multimode not set)", stat);
1704
1705         if (!OK_STAT(stat, DRIVE_READY, BAD_W_STAT)) 
1706                 return DRIVER(drive)->error(drive, "flagged_task_mulout_intr", stat);
1707         
1708         if (!rq->current_nr_sectors) { 
1709                 ide_end_drive_cmd (drive, stat, hwif->INB(IDE_ERROR_REG));
1710                 return ide_stopped;
1711         }
1712
1713         if (!OK_STAT(stat, DATA_READY, BAD_W_STAT)) {
1714                 /*
1715                  * (ks) Unexpected ATA data phase detected.
1716                  * This should not happen. But, it can !
1717                  * I am not sure, which function is best to clean up
1718                  * this situation.  I choose: ide_error(...)
1719                  */
1720                 return DRIVER(drive)->error(drive, "flagged_task_mulout_intr (unexpected data phase)", stat); 
1721         }
1722
1723         nsect = (rq->current_nr_sectors > msect) ? msect : rq->current_nr_sectors;
1724         pBuf = rq->buffer + ((rq->nr_sectors - rq->current_nr_sectors) * SECTOR_SIZE);
1725         DTF("Multiwrite: %p, nsect: %d , rq->current_nr_sectors: %ld\n",
1726             pBuf, nsect, rq->current_nr_sectors);
1727
1728         taskfile_output_data(drive, pBuf, nsect * SECTOR_WORDS);
1729         rq->current_nr_sectors -= nsect;
1730
1731         /*
1732          * (ks) We don't know which command was executed. 
1733          * So, we wait the 'WORSTCASE' value.
1734          */
1735         ide_set_handler(drive, &flagged_task_mulout_intr, WAIT_WORSTCASE, NULL);
1736
1737         return ide_started;
1738 }