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