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