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