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