]> pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/ide/ide-taskfile.c
ide: use ->data_phase to set ->handler in do_rw_taskfile()
[linux-2.6-omap-h63xx.git] / drivers / ide / ide-taskfile.c
1 /*
2  * linux/drivers/ide/ide-taskfile.c     Version 0.38    March 05, 2003
3  *
4  *  Copyright (C) 2000-2002     Michael Cornwell <cornwell@acm.org>
5  *  Copyright (C) 2000-2002     Andre Hedrick <andre@linux-ide.org>
6  *  Copyright (C) 2001-2002     Klaus Smolin
7  *                                      IBM Storage Technology Division
8  *  Copyright (C) 2003-2004     Bartlomiej Zolnierkiewicz
9  *
10  *  The big the bad and the ugly.
11  */
12
13 #include <linux/module.h>
14 #include <linux/types.h>
15 #include <linux/string.h>
16 #include <linux/kernel.h>
17 #include <linux/timer.h>
18 #include <linux/mm.h>
19 #include <linux/sched.h>
20 #include <linux/interrupt.h>
21 #include <linux/major.h>
22 #include <linux/errno.h>
23 #include <linux/genhd.h>
24 #include <linux/blkpg.h>
25 #include <linux/slab.h>
26 #include <linux/pci.h>
27 #include <linux/delay.h>
28 #include <linux/hdreg.h>
29 #include <linux/ide.h>
30 #include <linux/bitops.h>
31 #include <linux/scatterlist.h>
32
33 #include <asm/byteorder.h>
34 #include <asm/irq.h>
35 #include <asm/uaccess.h>
36 #include <asm/io.h>
37
38 static void ata_bswap_data (void *buffer, int wcount)
39 {
40         u16 *p = buffer;
41
42         while (wcount--) {
43                 *p = *p << 8 | *p >> 8; p++;
44                 *p = *p << 8 | *p >> 8; p++;
45         }
46 }
47
48 static void taskfile_input_data(ide_drive_t *drive, void *buffer, u32 wcount)
49 {
50         HWIF(drive)->ata_input_data(drive, buffer, wcount);
51         if (drive->bswap)
52                 ata_bswap_data(buffer, wcount);
53 }
54
55 static void taskfile_output_data(ide_drive_t *drive, void *buffer, u32 wcount)
56 {
57         if (drive->bswap) {
58                 ata_bswap_data(buffer, wcount);
59                 HWIF(drive)->ata_output_data(drive, buffer, wcount);
60                 ata_bswap_data(buffer, wcount);
61         } else {
62                 HWIF(drive)->ata_output_data(drive, buffer, wcount);
63         }
64 }
65
66 void ide_tf_load(ide_drive_t *drive, ide_task_t *task)
67 {
68         ide_hwif_t *hwif = drive->hwif;
69         struct ide_taskfile *tf = &task->tf;
70         u8 HIHI = (task->tf_flags & IDE_TFLAG_LBA48) ? 0xE0 : 0xEF;
71
72         if (task->tf_flags & IDE_TFLAG_FLAGGED)
73                 HIHI = 0xFF;
74
75 #ifdef DEBUG
76         printk("%s: tf: feat 0x%02x nsect 0x%02x lbal 0x%02x "
77                 "lbam 0x%02x lbah 0x%02x dev 0x%02x cmd 0x%02x\n",
78                 drive->name, tf->feature, tf->nsect, tf->lbal,
79                 tf->lbam, tf->lbah, tf->device, tf->command);
80 #endif
81
82         if (IDE_CONTROL_REG)
83                 hwif->OUTB(drive->ctl, IDE_CONTROL_REG); /* clear nIEN */
84
85         if ((task->tf_flags & IDE_TFLAG_NO_SELECT_MASK) == 0)
86                 SELECT_MASK(drive, 0);
87
88         if (task->tf_flags & IDE_TFLAG_OUT_DATA)
89                 hwif->OUTW((tf->hob_data << 8) | tf->data, IDE_DATA_REG);
90
91         if (task->tf_flags & IDE_TFLAG_OUT_HOB_FEATURE)
92                 hwif->OUTB(tf->hob_feature, IDE_FEATURE_REG);
93         if (task->tf_flags & IDE_TFLAG_OUT_HOB_NSECT)
94                 hwif->OUTB(tf->hob_nsect, IDE_NSECTOR_REG);
95         if (task->tf_flags & IDE_TFLAG_OUT_HOB_LBAL)
96                 hwif->OUTB(tf->hob_lbal, IDE_SECTOR_REG);
97         if (task->tf_flags & IDE_TFLAG_OUT_HOB_LBAM)
98                 hwif->OUTB(tf->hob_lbam, IDE_LCYL_REG);
99         if (task->tf_flags & IDE_TFLAG_OUT_HOB_LBAH)
100                 hwif->OUTB(tf->hob_lbah, IDE_HCYL_REG);
101
102         if (task->tf_flags & IDE_TFLAG_OUT_FEATURE)
103                 hwif->OUTB(tf->feature, IDE_FEATURE_REG);
104         if (task->tf_flags & IDE_TFLAG_OUT_NSECT)
105                 hwif->OUTB(tf->nsect, IDE_NSECTOR_REG);
106         if (task->tf_flags & IDE_TFLAG_OUT_LBAL)
107                 hwif->OUTB(tf->lbal, IDE_SECTOR_REG);
108         if (task->tf_flags & IDE_TFLAG_OUT_LBAM)
109                 hwif->OUTB(tf->lbam, IDE_LCYL_REG);
110         if (task->tf_flags & IDE_TFLAG_OUT_LBAH)
111                 hwif->OUTB(tf->lbah, IDE_HCYL_REG);
112
113         if (task->tf_flags & IDE_TFLAG_OUT_DEVICE)
114                 hwif->OUTB((tf->device & HIHI) | drive->select.all, IDE_SELECT_REG);
115 }
116
117 EXPORT_SYMBOL_GPL(ide_tf_load);
118
119 int taskfile_lib_get_identify (ide_drive_t *drive, u8 *buf)
120 {
121         ide_task_t args;
122
123         memset(&args, 0, sizeof(ide_task_t));
124         args.tf.nsect = 0x01;
125         if (drive->media == ide_disk)
126                 args.tf.command = WIN_IDENTIFY;
127         else
128                 args.tf.command = WIN_PIDENTIFY;
129         args.tf_flags   = IDE_TFLAG_OUT_TF | IDE_TFLAG_OUT_DEVICE;
130         args.data_phase = TASKFILE_IN;
131         return ide_raw_taskfile(drive, &args, buf, 1);
132 }
133
134 static int inline task_dma_ok(ide_task_t *task)
135 {
136         if (task->tf_flags & IDE_TFLAG_FLAGGED)
137                 return 1;
138
139         switch (task->tf.command) {
140                 case WIN_WRITEDMA_ONCE:
141                 case WIN_WRITEDMA:
142                 case WIN_WRITEDMA_EXT:
143                 case WIN_READDMA_ONCE:
144                 case WIN_READDMA:
145                 case WIN_READDMA_EXT:
146                 case WIN_IDENTIFY_DMA:
147                         return 1;
148         }
149
150         return 0;
151 }
152
153 static ide_startstop_t task_no_data_intr(ide_drive_t *);
154 static ide_startstop_t task_out_intr(ide_drive_t *);
155
156 ide_startstop_t do_rw_taskfile (ide_drive_t *drive, ide_task_t *task)
157 {
158         ide_hwif_t *hwif        = HWIF(drive);
159         struct ide_taskfile *tf = &task->tf;
160
161         if (task->data_phase == TASKFILE_MULTI_IN ||
162             task->data_phase == TASKFILE_MULTI_OUT) {
163                 if (!drive->mult_count) {
164                         printk(KERN_ERR "%s: multimode not set!\n",
165                                         drive->name);
166                         return ide_stopped;
167                 }
168         }
169
170         if (task->tf_flags & IDE_TFLAG_FLAGGED)
171                 task->tf_flags |= IDE_TFLAG_FLAGGED_SET_IN_FLAGS;
172
173         ide_tf_load(drive, task);
174
175         switch (task->data_phase) {
176         case TASKFILE_MULTI_OUT:
177         case TASKFILE_OUT:
178                 task->handler = task_out_intr;
179                 hwif->OUTBSYNC(drive, tf->command, IDE_COMMAND_REG);
180                 ndelay(400);    /* FIXME */
181                 return pre_task_out_intr(drive, task->rq);
182         case TASKFILE_MULTI_IN:
183         case TASKFILE_IN:
184                 task->handler = task_in_intr;
185                 /* fall-through */
186         case TASKFILE_NO_DATA:
187                 /* WIN_{SPECIFY,RESTORE,SETMULT} use custom handlers */
188                 if (task->handler == NULL)
189                         task->handler = task_no_data_intr;
190                 ide_execute_command(drive, tf->command, task->handler, WAIT_WORSTCASE, NULL);
191                 return ide_started;
192         default:
193                 if (task_dma_ok(task) == 0 || drive->using_dma == 0 ||
194                     hwif->dma_setup(drive))
195                         return ide_stopped;
196                 hwif->dma_exec_cmd(drive, tf->command);
197                 hwif->dma_start(drive);
198                 return ide_started;
199         }
200 }
201
202 /*
203  * set_multmode_intr() is invoked on completion of a WIN_SETMULT cmd.
204  */
205 ide_startstop_t set_multmode_intr (ide_drive_t *drive)
206 {
207         ide_hwif_t *hwif = HWIF(drive);
208         u8 stat;
209
210         if (OK_STAT(stat = hwif->INB(IDE_STATUS_REG),READY_STAT,BAD_STAT)) {
211                 drive->mult_count = drive->mult_req;
212         } else {
213                 drive->mult_req = drive->mult_count = 0;
214                 drive->special.b.recalibrate = 1;
215                 (void) ide_dump_status(drive, "set_multmode", stat);
216         }
217         return ide_stopped;
218 }
219
220 /*
221  * set_geometry_intr() is invoked on completion of a WIN_SPECIFY cmd.
222  */
223 ide_startstop_t set_geometry_intr (ide_drive_t *drive)
224 {
225         ide_hwif_t *hwif = HWIF(drive);
226         int retries = 5;
227         u8 stat;
228
229         while (((stat = hwif->INB(IDE_STATUS_REG)) & BUSY_STAT) && retries--)
230                 udelay(10);
231
232         if (OK_STAT(stat, READY_STAT, BAD_STAT))
233                 return ide_stopped;
234
235         if (stat & (ERR_STAT|DRQ_STAT))
236                 return ide_error(drive, "set_geometry_intr", stat);
237
238         BUG_ON(HWGROUP(drive)->handler != NULL);
239         ide_set_handler(drive, &set_geometry_intr, WAIT_WORSTCASE, NULL);
240         return ide_started;
241 }
242
243 /*
244  * recal_intr() is invoked on completion of a WIN_RESTORE (recalibrate) cmd.
245  */
246 ide_startstop_t recal_intr (ide_drive_t *drive)
247 {
248         ide_hwif_t *hwif = HWIF(drive);
249         u8 stat;
250
251         if (!OK_STAT(stat = hwif->INB(IDE_STATUS_REG), READY_STAT, BAD_STAT))
252                 return ide_error(drive, "recal_intr", stat);
253         return ide_stopped;
254 }
255
256 /*
257  * Handler for commands without a data phase
258  */
259 static ide_startstop_t task_no_data_intr(ide_drive_t *drive)
260 {
261         ide_task_t *args        = HWGROUP(drive)->rq->special;
262         ide_hwif_t *hwif        = HWIF(drive);
263         u8 stat;
264
265         local_irq_enable_in_hardirq();
266         if (!OK_STAT(stat = hwif->INB(IDE_STATUS_REG),READY_STAT,BAD_STAT)) {
267                 return ide_error(drive, "task_no_data_intr", stat);
268                 /* calls ide_end_drive_cmd */
269         }
270         if (args)
271                 ide_end_drive_cmd(drive, stat, hwif->INB(IDE_ERROR_REG));
272
273         return ide_stopped;
274 }
275
276 static u8 wait_drive_not_busy(ide_drive_t *drive)
277 {
278         ide_hwif_t *hwif = HWIF(drive);
279         int retries;
280         u8 stat;
281
282         /*
283          * Last sector was transfered, wait until drive is ready.
284          * This can take up to 10 usec, but we will wait max 1 ms
285          * (drive_cmd_intr() waits that long).
286          */
287         for (retries = 0; retries < 100; retries++) {
288                 if ((stat = hwif->INB(IDE_STATUS_REG)) & BUSY_STAT)
289                         udelay(10);
290                 else
291                         break;
292         }
293
294         if (stat & BUSY_STAT)
295                 printk(KERN_ERR "%s: drive still BUSY!\n", drive->name);
296
297         return stat;
298 }
299
300 static void ide_pio_sector(ide_drive_t *drive, unsigned int write)
301 {
302         ide_hwif_t *hwif = drive->hwif;
303         struct scatterlist *sg = hwif->sg_table;
304         struct scatterlist *cursg = hwif->cursg;
305         struct page *page;
306 #ifdef CONFIG_HIGHMEM
307         unsigned long flags;
308 #endif
309         unsigned int offset;
310         u8 *buf;
311
312         cursg = hwif->cursg;
313         if (!cursg) {
314                 cursg = sg;
315                 hwif->cursg = sg;
316         }
317
318         page = sg_page(cursg);
319         offset = cursg->offset + hwif->cursg_ofs * SECTOR_SIZE;
320
321         /* get the current page and offset */
322         page = nth_page(page, (offset >> PAGE_SHIFT));
323         offset %= PAGE_SIZE;
324
325 #ifdef CONFIG_HIGHMEM
326         local_irq_save(flags);
327 #endif
328         buf = kmap_atomic(page, KM_BIO_SRC_IRQ) + offset;
329
330         hwif->nleft--;
331         hwif->cursg_ofs++;
332
333         if ((hwif->cursg_ofs * SECTOR_SIZE) == cursg->length) {
334                 hwif->cursg = sg_next(hwif->cursg);
335                 hwif->cursg_ofs = 0;
336         }
337
338         /* do the actual data transfer */
339         if (write)
340                 taskfile_output_data(drive, buf, SECTOR_WORDS);
341         else
342                 taskfile_input_data(drive, buf, SECTOR_WORDS);
343
344         kunmap_atomic(buf, KM_BIO_SRC_IRQ);
345 #ifdef CONFIG_HIGHMEM
346         local_irq_restore(flags);
347 #endif
348 }
349
350 static void ide_pio_multi(ide_drive_t *drive, unsigned int write)
351 {
352         unsigned int nsect;
353
354         nsect = min_t(unsigned int, drive->hwif->nleft, drive->mult_count);
355         while (nsect--)
356                 ide_pio_sector(drive, write);
357 }
358
359 static void ide_pio_datablock(ide_drive_t *drive, struct request *rq,
360                                      unsigned int write)
361 {
362         if (rq->bio)    /* fs request */
363                 rq->errors = 0;
364
365         touch_softlockup_watchdog();
366
367         switch (drive->hwif->data_phase) {
368         case TASKFILE_MULTI_IN:
369         case TASKFILE_MULTI_OUT:
370                 ide_pio_multi(drive, write);
371                 break;
372         default:
373                 ide_pio_sector(drive, write);
374                 break;
375         }
376 }
377
378 static ide_startstop_t task_error(ide_drive_t *drive, struct request *rq,
379                                   const char *s, u8 stat)
380 {
381         if (rq->bio) {
382                 ide_hwif_t *hwif = drive->hwif;
383                 int sectors = hwif->nsect - hwif->nleft;
384
385                 switch (hwif->data_phase) {
386                 case TASKFILE_IN:
387                         if (hwif->nleft)
388                                 break;
389                         /* fall through */
390                 case TASKFILE_OUT:
391                         sectors--;
392                         break;
393                 case TASKFILE_MULTI_IN:
394                         if (hwif->nleft)
395                                 break;
396                         /* fall through */
397                 case TASKFILE_MULTI_OUT:
398                         sectors -= drive->mult_count;
399                 default:
400                         break;
401                 }
402
403                 if (sectors > 0) {
404                         ide_driver_t *drv;
405
406                         drv = *(ide_driver_t **)rq->rq_disk->private_data;
407                         drv->end_request(drive, 1, sectors);
408                 }
409         }
410         return ide_error(drive, s, stat);
411 }
412
413 static void task_end_request(ide_drive_t *drive, struct request *rq, u8 stat)
414 {
415         HWIF(drive)->cursg = NULL;
416
417         if (rq->cmd_type == REQ_TYPE_ATA_TASKFILE) {
418                 ide_task_t *task = rq->special;
419
420                 if (task->tf_flags & IDE_TFLAG_FLAGGED) {
421                         u8 err = drive->hwif->INB(IDE_ERROR_REG);
422                         ide_end_drive_cmd(drive, stat, err);
423                         return;
424                 }
425         }
426
427         if (rq->rq_disk) {
428                 ide_driver_t *drv;
429
430                 drv = *(ide_driver_t **)rq->rq_disk->private_data;;
431                 drv->end_request(drive, 1, rq->hard_nr_sectors);
432         } else
433                 ide_end_request(drive, 1, rq->hard_nr_sectors);
434 }
435
436 /*
437  * Handler for command with PIO data-in phase (Read/Read Multiple).
438  */
439 ide_startstop_t task_in_intr (ide_drive_t *drive)
440 {
441         ide_hwif_t *hwif = drive->hwif;
442         struct request *rq = HWGROUP(drive)->rq;
443         u8 stat = hwif->INB(IDE_STATUS_REG);
444
445         /* new way for dealing with premature shared PCI interrupts */
446         if (!OK_STAT(stat, DATA_READY, BAD_R_STAT)) {
447                 if (stat & (ERR_STAT | DRQ_STAT))
448                         return task_error(drive, rq, __FUNCTION__, stat);
449                 /* No data yet, so wait for another IRQ. */
450                 ide_set_handler(drive, &task_in_intr, WAIT_WORSTCASE, NULL);
451                 return ide_started;
452         }
453
454         ide_pio_datablock(drive, rq, 0);
455
456         /* If it was the last datablock check status and finish transfer. */
457         if (!hwif->nleft) {
458                 stat = wait_drive_not_busy(drive);
459                 if (!OK_STAT(stat, 0, BAD_R_STAT))
460                         return task_error(drive, rq, __FUNCTION__, stat);
461                 task_end_request(drive, rq, stat);
462                 return ide_stopped;
463         }
464
465         /* Still data left to transfer. */
466         ide_set_handler(drive, &task_in_intr, WAIT_WORSTCASE, NULL);
467
468         return ide_started;
469 }
470 EXPORT_SYMBOL(task_in_intr);
471
472 /*
473  * Handler for command with PIO data-out phase (Write/Write Multiple).
474  */
475 static ide_startstop_t task_out_intr (ide_drive_t *drive)
476 {
477         ide_hwif_t *hwif = drive->hwif;
478         struct request *rq = HWGROUP(drive)->rq;
479         u8 stat = hwif->INB(IDE_STATUS_REG);
480
481         if (!OK_STAT(stat, DRIVE_READY, drive->bad_wstat))
482                 return task_error(drive, rq, __FUNCTION__, stat);
483
484         /* Deal with unexpected ATA data phase. */
485         if (((stat & DRQ_STAT) == 0) ^ !hwif->nleft)
486                 return task_error(drive, rq, __FUNCTION__, stat);
487
488         if (!hwif->nleft) {
489                 task_end_request(drive, rq, stat);
490                 return ide_stopped;
491         }
492
493         /* Still data left to transfer. */
494         ide_pio_datablock(drive, rq, 1);
495         ide_set_handler(drive, &task_out_intr, WAIT_WORSTCASE, NULL);
496
497         return ide_started;
498 }
499
500 ide_startstop_t pre_task_out_intr (ide_drive_t *drive, struct request *rq)
501 {
502         ide_startstop_t startstop;
503
504         if (ide_wait_stat(&startstop, drive, DATA_READY,
505                           drive->bad_wstat, WAIT_DRQ)) {
506                 printk(KERN_ERR "%s: no DRQ after issuing %sWRITE%s\n",
507                                 drive->name,
508                                 drive->hwif->data_phase ? "MULT" : "",
509                                 drive->addressing ? "_EXT" : "");
510                 return startstop;
511         }
512
513         if (!drive->unmask)
514                 local_irq_disable();
515
516         ide_set_handler(drive, &task_out_intr, WAIT_WORSTCASE, NULL);
517         ide_pio_datablock(drive, rq, 1);
518
519         return ide_started;
520 }
521 EXPORT_SYMBOL(pre_task_out_intr);
522
523 int ide_raw_taskfile(ide_drive_t *drive, ide_task_t *task, u8 *buf, u16 nsect)
524 {
525         struct request rq;
526
527         memset(&rq, 0, sizeof(rq));
528         rq.ref_count = 1;
529         rq.cmd_type = REQ_TYPE_ATA_TASKFILE;
530         rq.buffer = buf;
531
532         /*
533          * (ks) We transfer currently only whole sectors.
534          * This is suffient for now.  But, it would be great,
535          * if we would find a solution to transfer any size.
536          * To support special commands like READ LONG.
537          */
538         rq.hard_nr_sectors = rq.nr_sectors = nsect;
539         rq.hard_cur_sectors = rq.current_nr_sectors = nsect;
540
541         if (task->tf_flags & IDE_TFLAG_WRITE)
542                 rq.cmd_flags |= REQ_RW;
543
544         rq.special = task;
545         task->rq = &rq;
546
547         return ide_do_drive_cmd(drive, &rq, ide_wait);
548 }
549
550 EXPORT_SYMBOL(ide_raw_taskfile);
551
552 int ide_no_data_taskfile(ide_drive_t *drive, ide_task_t *task)
553 {
554         task->data_phase = TASKFILE_NO_DATA;
555
556         return ide_raw_taskfile(drive, task, NULL, 0);
557 }
558 EXPORT_SYMBOL_GPL(ide_no_data_taskfile);
559
560 #ifdef CONFIG_IDE_TASK_IOCTL
561 int ide_taskfile_ioctl (ide_drive_t *drive, unsigned int cmd, unsigned long arg)
562 {
563         ide_task_request_t      *req_task;
564         ide_task_t              args;
565         u8 *outbuf              = NULL;
566         u8 *inbuf               = NULL;
567         u8 *data_buf            = NULL;
568         int err                 = 0;
569         int tasksize            = sizeof(struct ide_task_request_s);
570         unsigned int taskin     = 0;
571         unsigned int taskout    = 0;
572         u16 nsect               = 0;
573         u8 io_32bit             = drive->io_32bit;
574         char __user *buf = (char __user *)arg;
575
576 //      printk("IDE Taskfile ...\n");
577
578         req_task = kzalloc(tasksize, GFP_KERNEL);
579         if (req_task == NULL) return -ENOMEM;
580         if (copy_from_user(req_task, buf, tasksize)) {
581                 kfree(req_task);
582                 return -EFAULT;
583         }
584
585         taskout = req_task->out_size;
586         taskin  = req_task->in_size;
587         
588         if (taskin > 65536 || taskout > 65536) {
589                 err = -EINVAL;
590                 goto abort;
591         }
592
593         if (taskout) {
594                 int outtotal = tasksize;
595                 outbuf = kzalloc(taskout, GFP_KERNEL);
596                 if (outbuf == NULL) {
597                         err = -ENOMEM;
598                         goto abort;
599                 }
600                 if (copy_from_user(outbuf, buf + outtotal, taskout)) {
601                         err = -EFAULT;
602                         goto abort;
603                 }
604         }
605
606         if (taskin) {
607                 int intotal = tasksize + taskout;
608                 inbuf = kzalloc(taskin, GFP_KERNEL);
609                 if (inbuf == NULL) {
610                         err = -ENOMEM;
611                         goto abort;
612                 }
613                 if (copy_from_user(inbuf, buf + intotal, taskin)) {
614                         err = -EFAULT;
615                         goto abort;
616                 }
617         }
618
619         memset(&args, 0, sizeof(ide_task_t));
620
621         memcpy(&args.tf_array[0], req_task->hob_ports, HDIO_DRIVE_HOB_HDR_SIZE - 2);
622         memcpy(&args.tf_array[6], req_task->io_ports, HDIO_DRIVE_TASK_HDR_SIZE);
623
624         args.data_phase = req_task->data_phase;
625
626         args.tf_flags = IDE_TFLAG_OUT_DEVICE;
627         if (drive->addressing == 1)
628                 args.tf_flags |= IDE_TFLAG_LBA48;
629
630         if (req_task->out_flags.all) {
631                 args.tf_flags |= IDE_TFLAG_FLAGGED;
632
633                 if (req_task->out_flags.b.data)
634                         args.tf_flags |= IDE_TFLAG_OUT_DATA;
635
636                 if (req_task->out_flags.b.nsector_hob)
637                         args.tf_flags |= IDE_TFLAG_OUT_HOB_NSECT;
638                 if (req_task->out_flags.b.sector_hob)
639                         args.tf_flags |= IDE_TFLAG_OUT_HOB_LBAL;
640                 if (req_task->out_flags.b.lcyl_hob)
641                         args.tf_flags |= IDE_TFLAG_OUT_HOB_LBAM;
642                 if (req_task->out_flags.b.hcyl_hob)
643                         args.tf_flags |= IDE_TFLAG_OUT_HOB_LBAH;
644
645                 if (req_task->out_flags.b.error_feature)
646                         args.tf_flags |= IDE_TFLAG_OUT_FEATURE;
647                 if (req_task->out_flags.b.nsector)
648                         args.tf_flags |= IDE_TFLAG_OUT_NSECT;
649                 if (req_task->out_flags.b.sector)
650                         args.tf_flags |= IDE_TFLAG_OUT_LBAL;
651                 if (req_task->out_flags.b.lcyl)
652                         args.tf_flags |= IDE_TFLAG_OUT_LBAM;
653                 if (req_task->out_flags.b.hcyl)
654                         args.tf_flags |= IDE_TFLAG_OUT_LBAH;
655         } else {
656                 args.tf_flags |= IDE_TFLAG_OUT_TF;
657                 if (args.tf_flags & IDE_TFLAG_LBA48)
658                         args.tf_flags |= IDE_TFLAG_OUT_HOB;
659         }
660
661         if (req_task->in_flags.b.data)
662                 args.tf_flags |= IDE_TFLAG_IN_DATA;
663
664         drive->io_32bit = 0;
665         switch(req_task->data_phase) {
666                 case TASKFILE_MULTI_OUT:
667                         if (!drive->mult_count) {
668                                 /* (hs): give up if multcount is not set */
669                                 printk(KERN_ERR "%s: %s Multimode Write " \
670                                         "multcount is not set\n",
671                                         drive->name, __FUNCTION__);
672                                 err = -EPERM;
673                                 goto abort;
674                         }
675                         /* fall through */
676                 case TASKFILE_OUT:
677                         /* fall through */
678                 case TASKFILE_OUT_DMAQ:
679                 case TASKFILE_OUT_DMA:
680                         nsect = taskout / SECTOR_SIZE;
681                         data_buf = outbuf;
682                         break;
683                 case TASKFILE_MULTI_IN:
684                         if (!drive->mult_count) {
685                                 /* (hs): give up if multcount is not set */
686                                 printk(KERN_ERR "%s: %s Multimode Read failure " \
687                                         "multcount is not set\n",
688                                         drive->name, __FUNCTION__);
689                                 err = -EPERM;
690                                 goto abort;
691                         }
692                         /* fall through */
693                 case TASKFILE_IN:
694                         /* fall through */
695                 case TASKFILE_IN_DMAQ:
696                 case TASKFILE_IN_DMA:
697                         nsect = taskin / SECTOR_SIZE;
698                         data_buf = inbuf;
699                         break;
700                 case TASKFILE_NO_DATA:
701                         break;
702                 default:
703                         err = -EFAULT;
704                         goto abort;
705         }
706
707         if (req_task->req_cmd == IDE_DRIVE_TASK_NO_DATA)
708                 nsect = 0;
709         else if (!nsect) {
710                 nsect = (args.tf.hob_nsect << 8) | args.tf.nsect;
711
712                 if (!nsect) {
713                         printk(KERN_ERR "%s: in/out command without data\n",
714                                         drive->name);
715                         err = -EFAULT;
716                         goto abort;
717                 }
718         }
719
720         if (req_task->req_cmd == IDE_DRIVE_TASK_RAW_WRITE)
721                 args.tf_flags |= IDE_TFLAG_WRITE;
722
723         err = ide_raw_taskfile(drive, &args, data_buf, nsect);
724
725         memcpy(req_task->hob_ports, &args.tf_array[0], HDIO_DRIVE_HOB_HDR_SIZE - 2);
726         memcpy(req_task->io_ports, &args.tf_array[6], HDIO_DRIVE_TASK_HDR_SIZE);
727
728         if ((args.tf_flags & IDE_TFLAG_FLAGGED_SET_IN_FLAGS) &&
729             req_task->in_flags.all == 0) {
730                 req_task->in_flags.all = IDE_TASKFILE_STD_IN_FLAGS;
731                 if (drive->addressing == 1)
732                         req_task->in_flags.all |= (IDE_HOB_STD_IN_FLAGS << 8);
733         }
734
735         if (copy_to_user(buf, req_task, tasksize)) {
736                 err = -EFAULT;
737                 goto abort;
738         }
739         if (taskout) {
740                 int outtotal = tasksize;
741                 if (copy_to_user(buf + outtotal, outbuf, taskout)) {
742                         err = -EFAULT;
743                         goto abort;
744                 }
745         }
746         if (taskin) {
747                 int intotal = tasksize + taskout;
748                 if (copy_to_user(buf + intotal, inbuf, taskin)) {
749                         err = -EFAULT;
750                         goto abort;
751                 }
752         }
753 abort:
754         kfree(req_task);
755         kfree(outbuf);
756         kfree(inbuf);
757
758 //      printk("IDE Taskfile ioctl ended. rc = %i\n", err);
759
760         drive->io_32bit = io_32bit;
761
762         return err;
763 }
764 #endif
765
766 int ide_wait_cmd (ide_drive_t *drive, u8 cmd, u8 nsect, u8 feature, u8 sectors, u8 *buf)
767 {
768         struct request rq;
769         u8 buffer[4];
770
771         if (!buf)
772                 buf = buffer;
773         memset(buf, 0, 4 + SECTOR_WORDS * 4 * sectors);
774         ide_init_drive_cmd(&rq);
775         rq.buffer = buf;
776         *buf++ = cmd;
777         *buf++ = nsect;
778         *buf++ = feature;
779         *buf++ = sectors;
780         return ide_do_drive_cmd(drive, &rq, ide_wait);
781 }
782
783 int ide_cmd_ioctl (ide_drive_t *drive, unsigned int cmd, unsigned long arg)
784 {
785         int err = 0;
786         u8 args[4], *argbuf = args;
787         u8 xfer_rate = 0;
788         int argsize = 4;
789         ide_task_t tfargs;
790         struct ide_taskfile *tf = &tfargs.tf;
791
792         if (NULL == (void *) arg) {
793                 struct request rq;
794                 ide_init_drive_cmd(&rq);
795                 return ide_do_drive_cmd(drive, &rq, ide_wait);
796         }
797
798         if (copy_from_user(args, (void __user *)arg, 4))
799                 return -EFAULT;
800
801         memset(&tfargs, 0, sizeof(ide_task_t));
802         tf->feature = args[2];
803         tf->nsect   = args[3];
804         tf->lbal    = args[1];
805         tf->command = args[0];
806
807         if (args[3]) {
808                 argsize = 4 + (SECTOR_WORDS * 4 * args[3]);
809                 argbuf = kzalloc(argsize, GFP_KERNEL);
810                 if (argbuf == NULL)
811                         return -ENOMEM;
812         }
813         if (set_transfer(drive, &tfargs)) {
814                 xfer_rate = args[1];
815                 if (ide_ata66_check(drive, &tfargs))
816                         goto abort;
817         }
818
819         err = ide_wait_cmd(drive, args[0], args[1], args[2], args[3], argbuf);
820
821         if (!err && xfer_rate) {
822                 /* active-retuning-calls future */
823                 ide_set_xfer_rate(drive, xfer_rate);
824                 ide_driveid_update(drive);
825         }
826 abort:
827         if (copy_to_user((void __user *)arg, argbuf, argsize))
828                 err = -EFAULT;
829         if (argsize > 4)
830                 kfree(argbuf);
831         return err;
832 }
833
834 int ide_task_ioctl (ide_drive_t *drive, unsigned int cmd, unsigned long arg)
835 {
836         void __user *p = (void __user *)arg;
837         int err = 0;
838         u8 args[7];
839         ide_task_t task;
840
841         if (copy_from_user(args, p, 7))
842                 return -EFAULT;
843
844         memset(&task, 0, sizeof(task));
845         memcpy(&task.tf_array[7], &args[1], 6);
846         task.tf.command = args[0];
847         task.tf_flags = IDE_TFLAG_OUT_TF | IDE_TFLAG_OUT_DEVICE;
848
849         err = ide_no_data_taskfile(drive, &task);
850
851         args[0] = task.tf.command;
852         memcpy(&args[1], &task.tf_array[7], 6);
853
854         if (copy_to_user(p, args, 7))
855                 err = -EFAULT;
856
857         return err;
858 }