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