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