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