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