sync hh.org
[hh.org.git] / drivers / ide / ide-taskfile.c
blob9265fdefde2a335417a259d8d0e112514602d724
1 /*
2 * linux/drivers/ide/ide-taskfile.c Version 0.38 March 05, 2003
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
10 * The big the bad and the ugly.
12 * Problems to be fixed because of BH interface or the lack therefore.
14 * Fill me in stupid !!!
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.
30 #include <linux/module.h>
31 #include <linux/types.h>
32 #include <linux/string.h>
33 #include <linux/kernel.h>
34 #include <linux/timer.h>
35 #include <linux/mm.h>
36 #include <linux/sched.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>
49 #include <asm/byteorder.h>
50 #include <asm/irq.h>
51 #include <asm/uaccess.h>
52 #include <asm/io.h>
54 static void ata_bswap_data (void *buffer, int wcount)
56 u16 *p = buffer;
58 while (wcount--) {
59 *p = *p << 8 | *p >> 8; p++;
60 *p = *p << 8 | *p >> 8; p++;
64 static void taskfile_input_data(ide_drive_t *drive, void *buffer, u32 wcount)
66 HWIF(drive)->ata_input_data(drive, buffer, wcount);
67 if (drive->bswap)
68 ata_bswap_data(buffer, wcount);
71 static void taskfile_output_data(ide_drive_t *drive, void *buffer, u32 wcount)
73 if (drive->bswap) {
74 ata_bswap_data(buffer, wcount);
75 HWIF(drive)->ata_output_data(drive, buffer, wcount);
76 ata_bswap_data(buffer, wcount);
77 } else {
78 HWIF(drive)->ata_output_data(drive, buffer, wcount);
82 int taskfile_lib_get_identify (ide_drive_t *drive, u8 *buf)
84 ide_task_t args;
85 memset(&args, 0, sizeof(ide_task_t));
86 args.tfRegister[IDE_NSECTOR_OFFSET] = 0x01;
87 if (drive->media == ide_disk)
88 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_IDENTIFY;
89 else
90 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_PIDENTIFY;
91 args.command_type = IDE_DRIVE_TASK_IN;
92 args.data_phase = TASKFILE_IN;
93 args.handler = &task_in_intr;
94 return ide_raw_taskfile(drive, &args, buf);
97 ide_startstop_t do_rw_taskfile (ide_drive_t *drive, ide_task_t *task)
99 ide_hwif_t *hwif = HWIF(drive);
100 task_struct_t *taskfile = (task_struct_t *) task->tfRegister;
101 hob_struct_t *hobfile = (hob_struct_t *) task->hobRegister;
102 u8 HIHI = (drive->addressing == 1) ? 0xE0 : 0xEF;
104 /* ALL Command Block Executions SHALL clear nIEN, unless otherwise */
105 if (IDE_CONTROL_REG) {
106 /* clear nIEN */
107 hwif->OUTB(drive->ctl, IDE_CONTROL_REG);
109 SELECT_MASK(drive, 0);
111 if (drive->addressing == 1) {
112 hwif->OUTB(hobfile->feature, IDE_FEATURE_REG);
113 hwif->OUTB(hobfile->sector_count, IDE_NSECTOR_REG);
114 hwif->OUTB(hobfile->sector_number, IDE_SECTOR_REG);
115 hwif->OUTB(hobfile->low_cylinder, IDE_LCYL_REG);
116 hwif->OUTB(hobfile->high_cylinder, IDE_HCYL_REG);
119 hwif->OUTB(taskfile->feature, IDE_FEATURE_REG);
120 hwif->OUTB(taskfile->sector_count, IDE_NSECTOR_REG);
121 hwif->OUTB(taskfile->sector_number, IDE_SECTOR_REG);
122 hwif->OUTB(taskfile->low_cylinder, IDE_LCYL_REG);
123 hwif->OUTB(taskfile->high_cylinder, IDE_HCYL_REG);
125 hwif->OUTB((taskfile->device_head & HIHI) | drive->select.all, IDE_SELECT_REG);
127 if (task->handler != NULL) {
128 if (task->prehandler != NULL) {
129 hwif->OUTBSYNC(drive, taskfile->command, IDE_COMMAND_REG);
130 ndelay(400); /* FIXME */
131 return task->prehandler(drive, task->rq);
133 ide_execute_command(drive, taskfile->command, task->handler, WAIT_WORSTCASE, NULL);
134 return ide_started;
137 if (!drive->using_dma)
138 return ide_stopped;
140 switch (taskfile->command) {
141 case WIN_WRITEDMA_ONCE:
142 case WIN_WRITEDMA:
143 case WIN_WRITEDMA_EXT:
144 case WIN_READDMA_ONCE:
145 case WIN_READDMA:
146 case WIN_READDMA_EXT:
147 case WIN_IDENTIFY_DMA:
148 if (!hwif->dma_setup(drive)) {
149 hwif->dma_exec_cmd(drive, taskfile->command);
150 hwif->dma_start(drive);
151 return ide_started;
153 break;
154 default:
155 if (task->handler == NULL)
156 return ide_stopped;
159 return ide_stopped;
163 * set_multmode_intr() is invoked on completion of a WIN_SETMULT cmd.
165 ide_startstop_t set_multmode_intr (ide_drive_t *drive)
167 ide_hwif_t *hwif = HWIF(drive);
168 u8 stat;
170 if (OK_STAT(stat = hwif->INB(IDE_STATUS_REG),READY_STAT,BAD_STAT)) {
171 drive->mult_count = drive->mult_req;
172 } else {
173 drive->mult_req = drive->mult_count = 0;
174 drive->special.b.recalibrate = 1;
175 (void) ide_dump_status(drive, "set_multmode", stat);
177 return ide_stopped;
181 * set_geometry_intr() is invoked on completion of a WIN_SPECIFY cmd.
183 ide_startstop_t set_geometry_intr (ide_drive_t *drive)
185 ide_hwif_t *hwif = HWIF(drive);
186 int retries = 5;
187 u8 stat;
189 while (((stat = hwif->INB(IDE_STATUS_REG)) & BUSY_STAT) && retries--)
190 udelay(10);
192 if (OK_STAT(stat, READY_STAT, BAD_STAT))
193 return ide_stopped;
195 if (stat & (ERR_STAT|DRQ_STAT))
196 return ide_error(drive, "set_geometry_intr", stat);
198 BUG_ON(HWGROUP(drive)->handler != NULL);
199 ide_set_handler(drive, &set_geometry_intr, WAIT_WORSTCASE, NULL);
200 return ide_started;
204 * recal_intr() is invoked on completion of a WIN_RESTORE (recalibrate) cmd.
206 ide_startstop_t recal_intr (ide_drive_t *drive)
208 ide_hwif_t *hwif = HWIF(drive);
209 u8 stat;
211 if (!OK_STAT(stat = hwif->INB(IDE_STATUS_REG), READY_STAT, BAD_STAT))
212 return ide_error(drive, "recal_intr", stat);
213 return ide_stopped;
217 * Handler for commands without a data phase
219 ide_startstop_t task_no_data_intr (ide_drive_t *drive)
221 ide_task_t *args = HWGROUP(drive)->rq->special;
222 ide_hwif_t *hwif = HWIF(drive);
223 u8 stat;
225 local_irq_enable_in_hardirq();
226 if (!OK_STAT(stat = hwif->INB(IDE_STATUS_REG),READY_STAT,BAD_STAT)) {
227 return ide_error(drive, "task_no_data_intr", stat);
228 /* calls ide_end_drive_cmd */
230 if (args)
231 ide_end_drive_cmd(drive, stat, hwif->INB(IDE_ERROR_REG));
233 return ide_stopped;
236 EXPORT_SYMBOL(task_no_data_intr);
238 static u8 wait_drive_not_busy(ide_drive_t *drive)
240 ide_hwif_t *hwif = HWIF(drive);
241 int retries = 100;
242 u8 stat;
245 * Last sector was transfered, wait until drive is ready.
246 * This can take up to 10 usec, but we will wait max 1 ms
247 * (drive_cmd_intr() waits that long).
249 while (((stat = hwif->INB(IDE_STATUS_REG)) & BUSY_STAT) && retries--)
250 udelay(10);
252 if (!retries)
253 printk(KERN_ERR "%s: drive still BUSY!\n", drive->name);
255 return stat;
258 static void ide_pio_sector(ide_drive_t *drive, unsigned int write)
260 ide_hwif_t *hwif = drive->hwif;
261 struct scatterlist *sg = hwif->sg_table;
262 enum dma_data_direction dir = write ? DMA_TO_DEVICE : DMA_FROM_DEVICE;
263 struct page *page;
264 #ifdef CONFIG_HIGHMEM
265 unsigned long flags;
266 #endif
267 unsigned int offset;
268 u8 *buf;
270 page = sg[hwif->cursg].page;
271 offset = sg[hwif->cursg].offset + hwif->cursg_ofs * SECTOR_SIZE;
273 /* get the current page and offset */
274 page = nth_page(page, (offset >> PAGE_SHIFT));
275 offset %= PAGE_SIZE;
277 #ifdef CONFIG_HIGHMEM
278 local_irq_save(flags);
279 #endif
280 buf = blk_kmap_atomic(page, KM_BIO_SRC_IRQ, dir) + offset;
282 hwif->nleft--;
283 hwif->cursg_ofs++;
285 if ((hwif->cursg_ofs * SECTOR_SIZE) == sg[hwif->cursg].length) {
286 hwif->cursg++;
287 hwif->cursg_ofs = 0;
290 /* do the actual data transfer */
291 if (write)
292 taskfile_output_data(drive, buf, SECTOR_WORDS);
293 else
294 taskfile_input_data(drive, buf, SECTOR_WORDS);
296 blk_kunmap_atomic(buf, KM_BIO_SRC_IRQ, dir);
297 #ifdef CONFIG_HIGHMEM
298 local_irq_restore(flags);
299 #endif
302 static void ide_pio_multi(ide_drive_t *drive, unsigned int write)
304 unsigned int nsect;
306 nsect = min_t(unsigned int, drive->hwif->nleft, drive->mult_count);
307 while (nsect--)
308 ide_pio_sector(drive, write);
311 static void ide_pio_datablock(ide_drive_t *drive, struct request *rq,
312 unsigned int write)
314 if (rq->bio) /* fs request */
315 rq->errors = 0;
317 touch_softlockup_watchdog();
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;
330 static ide_startstop_t task_error(ide_drive_t *drive, struct request *rq,
331 const char *s, u8 stat)
333 if (rq->bio) {
334 ide_hwif_t *hwif = drive->hwif;
335 int sectors = hwif->nsect - hwif->nleft;
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;
355 if (sectors > 0) {
356 ide_driver_t *drv;
358 drv = *(ide_driver_t **)rq->rq_disk->private_data;
359 drv->end_request(drive, 1, sectors);
362 return ide_error(drive, s, stat);
365 static void task_end_request(ide_drive_t *drive, struct request *rq, u8 stat)
367 if (rq->cmd_type == REQ_TYPE_ATA_TASKFILE) {
368 ide_task_t *task = rq->special;
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;
377 if (rq->rq_disk) {
378 ide_driver_t *drv;
380 drv = *(ide_driver_t **)rq->rq_disk->private_data;;
381 drv->end_request(drive, 1, rq->hard_nr_sectors);
382 } else
383 ide_end_request(drive, 1, rq->hard_nr_sectors);
387 * Handler for command with PIO data-in phase (Read/Read Multiple).
389 ide_startstop_t task_in_intr (ide_drive_t *drive)
391 ide_hwif_t *hwif = drive->hwif;
392 struct request *rq = HWGROUP(drive)->rq;
393 u8 stat = hwif->INB(IDE_STATUS_REG);
395 /* new way for dealing with premature shared PCI interrupts */
396 if (!OK_STAT(stat, DATA_READY, BAD_R_STAT)) {
397 if (stat & (ERR_STAT | DRQ_STAT))
398 return task_error(drive, rq, __FUNCTION__, stat);
399 /* No data yet, so wait for another IRQ. */
400 ide_set_handler(drive, &task_in_intr, WAIT_WORSTCASE, NULL);
401 return ide_started;
404 ide_pio_datablock(drive, rq, 0);
406 /* If it was the last datablock check status and finish transfer. */
407 if (!hwif->nleft) {
408 stat = wait_drive_not_busy(drive);
409 if (!OK_STAT(stat, 0, BAD_R_STAT))
410 return task_error(drive, rq, __FUNCTION__, stat);
411 task_end_request(drive, rq, stat);
412 return ide_stopped;
415 /* Still data left to transfer. */
416 ide_set_handler(drive, &task_in_intr, WAIT_WORSTCASE, NULL);
418 return ide_started;
420 EXPORT_SYMBOL(task_in_intr);
423 * Handler for command with PIO data-out phase (Write/Write Multiple).
425 static ide_startstop_t task_out_intr (ide_drive_t *drive)
427 ide_hwif_t *hwif = drive->hwif;
428 struct request *rq = HWGROUP(drive)->rq;
429 u8 stat = hwif->INB(IDE_STATUS_REG);
431 if (!OK_STAT(stat, DRIVE_READY, drive->bad_wstat))
432 return task_error(drive, rq, __FUNCTION__, stat);
434 /* Deal with unexpected ATA data phase. */
435 if (((stat & DRQ_STAT) == 0) ^ !hwif->nleft)
436 return task_error(drive, rq, __FUNCTION__, stat);
438 if (!hwif->nleft) {
439 task_end_request(drive, rq, stat);
440 return ide_stopped;
443 /* Still data left to transfer. */
444 ide_pio_datablock(drive, rq, 1);
445 ide_set_handler(drive, &task_out_intr, WAIT_WORSTCASE, NULL);
447 return ide_started;
450 ide_startstop_t pre_task_out_intr (ide_drive_t *drive, struct request *rq)
452 ide_startstop_t startstop;
454 if (ide_wait_stat(&startstop, drive, DATA_READY,
455 drive->bad_wstat, WAIT_DRQ)) {
456 printk(KERN_ERR "%s: no DRQ after issuing %sWRITE%s\n",
457 drive->name,
458 drive->hwif->data_phase ? "MULT" : "",
459 drive->addressing ? "_EXT" : "");
460 return startstop;
463 if (!drive->unmask)
464 local_irq_disable();
466 ide_set_handler(drive, &task_out_intr, WAIT_WORSTCASE, NULL);
467 ide_pio_datablock(drive, rq, 1);
469 return ide_started;
471 EXPORT_SYMBOL(pre_task_out_intr);
473 static int ide_diag_taskfile(ide_drive_t *drive, ide_task_t *args, unsigned long data_size, u8 *buf)
475 struct request rq;
477 memset(&rq, 0, sizeof(rq));
478 rq.cmd_type = REQ_TYPE_ATA_TASKFILE;
479 rq.buffer = buf;
482 * (ks) We transfer currently only whole sectors.
483 * This is suffient for now. But, it would be great,
484 * if we would find a solution to transfer any size.
485 * To support special commands like READ LONG.
487 if (args->command_type != IDE_DRIVE_TASK_NO_DATA) {
488 if (data_size == 0)
489 rq.nr_sectors = (args->hobRegister[IDE_NSECTOR_OFFSET] << 8) | args->tfRegister[IDE_NSECTOR_OFFSET];
490 else
491 rq.nr_sectors = data_size / SECTOR_SIZE;
493 if (!rq.nr_sectors) {
494 printk(KERN_ERR "%s: in/out command without data\n",
495 drive->name);
496 return -EFAULT;
499 rq.hard_nr_sectors = rq.nr_sectors;
500 rq.hard_cur_sectors = rq.current_nr_sectors = rq.nr_sectors;
502 if (args->command_type == IDE_DRIVE_TASK_RAW_WRITE)
503 rq.cmd_flags |= REQ_RW;
506 rq.special = args;
507 args->rq = &rq;
508 return ide_do_drive_cmd(drive, &rq, ide_wait);
511 int ide_raw_taskfile (ide_drive_t *drive, ide_task_t *args, u8 *buf)
513 return ide_diag_taskfile(drive, args, 0, buf);
516 EXPORT_SYMBOL(ide_raw_taskfile);
518 int ide_taskfile_ioctl (ide_drive_t *drive, unsigned int cmd, unsigned long arg)
520 ide_task_request_t *req_task;
521 ide_task_t args;
522 u8 *outbuf = NULL;
523 u8 *inbuf = NULL;
524 task_ioreg_t *argsptr = args.tfRegister;
525 task_ioreg_t *hobsptr = args.hobRegister;
526 int err = 0;
527 int tasksize = sizeof(struct ide_task_request_s);
528 unsigned int taskin = 0;
529 unsigned int taskout = 0;
530 u8 io_32bit = drive->io_32bit;
531 char __user *buf = (char __user *)arg;
533 // printk("IDE Taskfile ...\n");
535 req_task = kzalloc(tasksize, GFP_KERNEL);
536 if (req_task == NULL) return -ENOMEM;
537 if (copy_from_user(req_task, buf, tasksize)) {
538 kfree(req_task);
539 return -EFAULT;
542 taskout = req_task->out_size;
543 taskin = req_task->in_size;
545 if (taskin > 65536 || taskout > 65536) {
546 err = -EINVAL;
547 goto abort;
550 if (taskout) {
551 int outtotal = tasksize;
552 outbuf = kzalloc(taskout, GFP_KERNEL);
553 if (outbuf == NULL) {
554 err = -ENOMEM;
555 goto abort;
557 if (copy_from_user(outbuf, buf + outtotal, taskout)) {
558 err = -EFAULT;
559 goto abort;
563 if (taskin) {
564 int intotal = tasksize + taskout;
565 inbuf = kzalloc(taskin, GFP_KERNEL);
566 if (inbuf == NULL) {
567 err = -ENOMEM;
568 goto abort;
570 if (copy_from_user(inbuf, buf + intotal, taskin)) {
571 err = -EFAULT;
572 goto abort;
576 memset(&args, 0, sizeof(ide_task_t));
577 memcpy(argsptr, req_task->io_ports, HDIO_DRIVE_TASK_HDR_SIZE);
578 memcpy(hobsptr, req_task->hob_ports, HDIO_DRIVE_HOB_HDR_SIZE);
580 args.tf_in_flags = req_task->in_flags;
581 args.tf_out_flags = req_task->out_flags;
582 args.data_phase = req_task->data_phase;
583 args.command_type = req_task->req_cmd;
585 drive->io_32bit = 0;
586 switch(req_task->data_phase) {
587 case TASKFILE_OUT_DMAQ:
588 case TASKFILE_OUT_DMA:
589 err = ide_diag_taskfile(drive, &args, taskout, outbuf);
590 break;
591 case TASKFILE_IN_DMAQ:
592 case TASKFILE_IN_DMA:
593 err = ide_diag_taskfile(drive, &args, taskin, inbuf);
594 break;
595 case TASKFILE_MULTI_OUT:
596 if (!drive->mult_count) {
597 /* (hs): give up if multcount is not set */
598 printk(KERN_ERR "%s: %s Multimode Write " \
599 "multcount is not set\n",
600 drive->name, __FUNCTION__);
601 err = -EPERM;
602 goto abort;
604 /* fall through */
605 case TASKFILE_OUT:
606 args.prehandler = &pre_task_out_intr;
607 args.handler = &task_out_intr;
608 err = ide_diag_taskfile(drive, &args, taskout, outbuf);
609 break;
610 case TASKFILE_MULTI_IN:
611 if (!drive->mult_count) {
612 /* (hs): give up if multcount is not set */
613 printk(KERN_ERR "%s: %s Multimode Read failure " \
614 "multcount is not set\n",
615 drive->name, __FUNCTION__);
616 err = -EPERM;
617 goto abort;
619 /* fall through */
620 case TASKFILE_IN:
621 args.handler = &task_in_intr;
622 err = ide_diag_taskfile(drive, &args, taskin, inbuf);
623 break;
624 case TASKFILE_NO_DATA:
625 args.handler = &task_no_data_intr;
626 err = ide_diag_taskfile(drive, &args, 0, NULL);
627 break;
628 default:
629 err = -EFAULT;
630 goto abort;
633 memcpy(req_task->io_ports, &(args.tfRegister), HDIO_DRIVE_TASK_HDR_SIZE);
634 memcpy(req_task->hob_ports, &(args.hobRegister), HDIO_DRIVE_HOB_HDR_SIZE);
635 req_task->in_flags = args.tf_in_flags;
636 req_task->out_flags = args.tf_out_flags;
638 if (copy_to_user(buf, req_task, tasksize)) {
639 err = -EFAULT;
640 goto abort;
642 if (taskout) {
643 int outtotal = tasksize;
644 if (copy_to_user(buf + outtotal, outbuf, taskout)) {
645 err = -EFAULT;
646 goto abort;
649 if (taskin) {
650 int intotal = tasksize + taskout;
651 if (copy_to_user(buf + intotal, inbuf, taskin)) {
652 err = -EFAULT;
653 goto abort;
656 abort:
657 kfree(req_task);
658 kfree(outbuf);
659 kfree(inbuf);
661 // printk("IDE Taskfile ioctl ended. rc = %i\n", err);
663 drive->io_32bit = io_32bit;
665 return err;
668 int ide_wait_cmd (ide_drive_t *drive, u8 cmd, u8 nsect, u8 feature, u8 sectors, u8 *buf)
670 struct request rq;
671 u8 buffer[4];
673 if (!buf)
674 buf = buffer;
675 memset(buf, 0, 4 + SECTOR_WORDS * 4 * sectors);
676 ide_init_drive_cmd(&rq);
677 rq.buffer = buf;
678 *buf++ = cmd;
679 *buf++ = nsect;
680 *buf++ = feature;
681 *buf++ = sectors;
682 return ide_do_drive_cmd(drive, &rq, ide_wait);
686 * FIXME : this needs to map into at taskfile. <andre@linux-ide.org>
688 int ide_cmd_ioctl (ide_drive_t *drive, unsigned int cmd, unsigned long arg)
690 int err = 0;
691 u8 args[4], *argbuf = args;
692 u8 xfer_rate = 0;
693 int argsize = 4;
694 ide_task_t tfargs;
696 if (NULL == (void *) arg) {
697 struct request rq;
698 ide_init_drive_cmd(&rq);
699 return ide_do_drive_cmd(drive, &rq, ide_wait);
702 if (copy_from_user(args, (void __user *)arg, 4))
703 return -EFAULT;
705 memset(&tfargs, 0, sizeof(ide_task_t));
706 tfargs.tfRegister[IDE_FEATURE_OFFSET] = args[2];
707 tfargs.tfRegister[IDE_NSECTOR_OFFSET] = args[3];
708 tfargs.tfRegister[IDE_SECTOR_OFFSET] = args[1];
709 tfargs.tfRegister[IDE_LCYL_OFFSET] = 0x00;
710 tfargs.tfRegister[IDE_HCYL_OFFSET] = 0x00;
711 tfargs.tfRegister[IDE_SELECT_OFFSET] = 0x00;
712 tfargs.tfRegister[IDE_COMMAND_OFFSET] = args[0];
714 if (args[3]) {
715 argsize = 4 + (SECTOR_WORDS * 4 * args[3]);
716 argbuf = kzalloc(argsize, GFP_KERNEL);
717 if (argbuf == NULL)
718 return -ENOMEM;
720 if (set_transfer(drive, &tfargs)) {
721 xfer_rate = args[1];
722 if (ide_ata66_check(drive, &tfargs))
723 goto abort;
726 err = ide_wait_cmd(drive, args[0], args[1], args[2], args[3], argbuf);
728 if (!err && xfer_rate) {
729 /* active-retuning-calls future */
730 ide_set_xfer_rate(drive, xfer_rate);
731 ide_driveid_update(drive);
733 abort:
734 if (copy_to_user((void __user *)arg, argbuf, argsize))
735 err = -EFAULT;
736 if (argsize > 4)
737 kfree(argbuf);
738 return err;
741 static int ide_wait_cmd_task(ide_drive_t *drive, u8 *buf)
743 struct request rq;
745 ide_init_drive_cmd(&rq);
746 rq.cmd_type = REQ_TYPE_ATA_TASK;
747 rq.buffer = buf;
748 return ide_do_drive_cmd(drive, &rq, ide_wait);
752 * FIXME : this needs to map into at taskfile. <andre@linux-ide.org>
754 int ide_task_ioctl (ide_drive_t *drive, unsigned int cmd, unsigned long arg)
756 void __user *p = (void __user *)arg;
757 int err = 0;
758 u8 args[7], *argbuf = args;
759 int argsize = 7;
761 if (copy_from_user(args, p, 7))
762 return -EFAULT;
763 err = ide_wait_cmd_task(drive, argbuf);
764 if (copy_to_user(p, argbuf, argsize))
765 err = -EFAULT;
766 return err;
770 * NOTICE: This is additions from IBM to provide a discrete interface,
771 * for selective taskregister access operations. Nice JOB Klaus!!!
772 * Glad to be able to work and co-develop this with you and IBM.
774 ide_startstop_t flagged_taskfile (ide_drive_t *drive, ide_task_t *task)
776 ide_hwif_t *hwif = HWIF(drive);
777 task_struct_t *taskfile = (task_struct_t *) task->tfRegister;
778 hob_struct_t *hobfile = (hob_struct_t *) task->hobRegister;
780 if (task->data_phase == TASKFILE_MULTI_IN ||
781 task->data_phase == TASKFILE_MULTI_OUT) {
782 if (!drive->mult_count) {
783 printk(KERN_ERR "%s: multimode not set!\n", drive->name);
784 return ide_stopped;
789 * (ks) Check taskfile in flags.
790 * If set, then execute as it is defined.
791 * If not set, then define default settings.
792 * The default values are:
793 * read all taskfile registers (except data)
794 * read the hob registers (sector, nsector, lcyl, hcyl)
796 if (task->tf_in_flags.all == 0) {
797 task->tf_in_flags.all = IDE_TASKFILE_STD_IN_FLAGS;
798 if (drive->addressing == 1)
799 task->tf_in_flags.all |= (IDE_HOB_STD_IN_FLAGS << 8);
802 /* ALL Command Block Executions SHALL clear nIEN, unless otherwise */
803 if (IDE_CONTROL_REG)
804 /* clear nIEN */
805 hwif->OUTB(drive->ctl, IDE_CONTROL_REG);
806 SELECT_MASK(drive, 0);
808 if (task->tf_out_flags.b.data) {
809 u16 data = taskfile->data + (hobfile->data << 8);
810 hwif->OUTW(data, IDE_DATA_REG);
813 /* (ks) send hob registers first */
814 if (task->tf_out_flags.b.nsector_hob)
815 hwif->OUTB(hobfile->sector_count, IDE_NSECTOR_REG);
816 if (task->tf_out_flags.b.sector_hob)
817 hwif->OUTB(hobfile->sector_number, IDE_SECTOR_REG);
818 if (task->tf_out_flags.b.lcyl_hob)
819 hwif->OUTB(hobfile->low_cylinder, IDE_LCYL_REG);
820 if (task->tf_out_flags.b.hcyl_hob)
821 hwif->OUTB(hobfile->high_cylinder, IDE_HCYL_REG);
823 /* (ks) Send now the standard registers */
824 if (task->tf_out_flags.b.error_feature)
825 hwif->OUTB(taskfile->feature, IDE_FEATURE_REG);
826 /* refers to number of sectors to transfer */
827 if (task->tf_out_flags.b.nsector)
828 hwif->OUTB(taskfile->sector_count, IDE_NSECTOR_REG);
829 /* refers to sector offset or start sector */
830 if (task->tf_out_flags.b.sector)
831 hwif->OUTB(taskfile->sector_number, IDE_SECTOR_REG);
832 if (task->tf_out_flags.b.lcyl)
833 hwif->OUTB(taskfile->low_cylinder, IDE_LCYL_REG);
834 if (task->tf_out_flags.b.hcyl)
835 hwif->OUTB(taskfile->high_cylinder, IDE_HCYL_REG);
838 * (ks) In the flagged taskfile approch, we will use all specified
839 * registers and the register value will not be changed, except the
840 * select bit (master/slave) in the drive_head register. We must make
841 * sure that the desired drive is selected.
843 hwif->OUTB(taskfile->device_head | drive->select.all, IDE_SELECT_REG);
844 switch(task->data_phase) {
846 case TASKFILE_OUT_DMAQ:
847 case TASKFILE_OUT_DMA:
848 case TASKFILE_IN_DMAQ:
849 case TASKFILE_IN_DMA:
850 hwif->dma_setup(drive);
851 hwif->dma_exec_cmd(drive, taskfile->command);
852 hwif->dma_start(drive);
853 break;
855 default:
856 if (task->handler == NULL)
857 return ide_stopped;
859 /* Issue the command */
860 if (task->prehandler) {
861 hwif->OUTBSYNC(drive, taskfile->command, IDE_COMMAND_REG);
862 ndelay(400); /* FIXME */
863 return task->prehandler(drive, task->rq);
865 ide_execute_command(drive, taskfile->command, task->handler, WAIT_WORSTCASE, NULL);
868 return ide_started;