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