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