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