2 * Copyright (C) 2000-2002 Michael Cornwell <cornwell@acm.org>
3 * Copyright (C) 2000-2002 Andre Hedrick <andre@linux-ide.org>
4 * Copyright (C) 2001-2002 Klaus Smolin
5 * IBM Storage Technology Division
6 * Copyright (C) 2003-2004, 2007 Bartlomiej Zolnierkiewicz
8 * The big the bad and the ugly.
11 #include <linux/types.h>
12 #include <linux/string.h>
13 #include <linux/kernel.h>
14 #include <linux/sched.h>
15 #include <linux/interrupt.h>
16 #include <linux/errno.h>
17 #include <linux/slab.h>
18 #include <linux/delay.h>
19 #include <linux/hdreg.h>
20 #include <linux/ide.h>
21 #include <linux/scatterlist.h>
23 #include <asm/uaccess.h>
26 void ide_tf_dump(const char *s, struct ide_taskfile *tf)
29 printk("%s: tf: feat 0x%02x nsect 0x%02x lbal 0x%02x "
30 "lbam 0x%02x lbah 0x%02x dev 0x%02x cmd 0x%02x\n",
31 s, tf->feature, tf->nsect, tf->lbal,
32 tf->lbam, tf->lbah, tf->device, tf->command);
33 printk("%s: hob: nsect 0x%02x lbal 0x%02x "
34 "lbam 0x%02x lbah 0x%02x\n",
35 s, tf->hob_nsect, tf->hob_lbal,
36 tf->hob_lbam, tf->hob_lbah);
40 int taskfile_lib_get_identify (ide_drive_t *drive, u8 *buf)
44 memset(&args, 0, sizeof(ide_task_t));
46 if (drive->media == ide_disk)
47 args.tf.command = WIN_IDENTIFY;
49 args.tf.command = WIN_PIDENTIFY;
50 args.tf_flags = IDE_TFLAG_TF | IDE_TFLAG_DEVICE;
51 args.data_phase = TASKFILE_IN;
52 return ide_raw_taskfile(drive, &args, buf, 1);
55 static ide_startstop_t task_no_data_intr(ide_drive_t *);
56 static ide_startstop_t set_geometry_intr(ide_drive_t *);
57 static ide_startstop_t recal_intr(ide_drive_t *);
58 static ide_startstop_t set_multmode_intr(ide_drive_t *);
59 static ide_startstop_t pre_task_out_intr(ide_drive_t *, struct request *);
60 static ide_startstop_t task_in_intr(ide_drive_t *);
62 ide_startstop_t do_rw_taskfile (ide_drive_t *drive, ide_task_t *task)
64 ide_hwif_t *hwif = HWIF(drive);
65 struct ide_taskfile *tf = &task->tf;
66 ide_handler_t *handler = NULL;
67 const struct ide_tp_ops *tp_ops = hwif->tp_ops;
68 const struct ide_dma_ops *dma_ops = hwif->dma_ops;
70 if (task->data_phase == TASKFILE_MULTI_IN ||
71 task->data_phase == TASKFILE_MULTI_OUT) {
72 if (!drive->mult_count) {
73 printk(KERN_ERR "%s: multimode not set!\n",
79 if (task->tf_flags & IDE_TFLAG_FLAGGED)
80 task->tf_flags |= IDE_TFLAG_FLAGGED_SET_IN_FLAGS;
82 if ((task->tf_flags & IDE_TFLAG_DMA_PIO_FALLBACK) == 0) {
83 ide_tf_dump(drive->name, tf);
84 tp_ops->set_irq(hwif, 1);
85 SELECT_MASK(drive, 0);
86 tp_ops->tf_load(drive, task);
89 switch (task->data_phase) {
90 case TASKFILE_MULTI_OUT:
92 tp_ops->exec_command(hwif, tf->command);
93 ndelay(400); /* FIXME */
94 return pre_task_out_intr(drive, task->rq);
95 case TASKFILE_MULTI_IN:
97 handler = task_in_intr;
99 case TASKFILE_NO_DATA:
101 handler = task_no_data_intr;
102 /* WIN_{SPECIFY,RESTORE,SETMULT} use custom handlers */
103 if (task->tf_flags & IDE_TFLAG_CUSTOM_HANDLER) {
104 switch (tf->command) {
105 case WIN_SPECIFY: handler = set_geometry_intr; break;
106 case WIN_RESTORE: handler = recal_intr; break;
107 case WIN_SETMULT: handler = set_multmode_intr; break;
110 ide_execute_command(drive, tf->command, handler,
111 WAIT_WORSTCASE, NULL);
114 if (drive->using_dma == 0 || dma_ops->dma_setup(drive))
116 dma_ops->dma_exec_cmd(drive, tf->command);
117 dma_ops->dma_start(drive);
121 EXPORT_SYMBOL_GPL(do_rw_taskfile);
124 * set_multmode_intr() is invoked on completion of a WIN_SETMULT cmd.
126 static ide_startstop_t set_multmode_intr(ide_drive_t *drive)
128 ide_hwif_t *hwif = drive->hwif;
129 u8 stat = hwif->tp_ops->read_status(hwif);
131 if (OK_STAT(stat, READY_STAT, BAD_STAT))
132 drive->mult_count = drive->mult_req;
134 drive->mult_req = drive->mult_count = 0;
135 drive->special.b.recalibrate = 1;
136 (void) ide_dump_status(drive, "set_multmode", stat);
142 * set_geometry_intr() is invoked on completion of a WIN_SPECIFY cmd.
144 static ide_startstop_t set_geometry_intr(ide_drive_t *drive)
146 ide_hwif_t *hwif = drive->hwif;
151 stat = hwif->tp_ops->read_status(hwif);
152 if ((stat & BUSY_STAT) == 0 || retries-- == 0)
157 if (OK_STAT(stat, READY_STAT, BAD_STAT))
160 if (stat & (ERR_STAT|DRQ_STAT))
161 return ide_error(drive, "set_geometry_intr", stat);
163 ide_set_handler(drive, &set_geometry_intr, WAIT_WORSTCASE, NULL);
168 * recal_intr() is invoked on completion of a WIN_RESTORE (recalibrate) cmd.
170 static ide_startstop_t recal_intr(ide_drive_t *drive)
172 ide_hwif_t *hwif = drive->hwif;
173 u8 stat = hwif->tp_ops->read_status(hwif);
175 if (!OK_STAT(stat, READY_STAT, BAD_STAT))
176 return ide_error(drive, "recal_intr", stat);
181 * Handler for commands without a data phase
183 static ide_startstop_t task_no_data_intr(ide_drive_t *drive)
185 ide_hwif_t *hwif = drive->hwif;
186 ide_task_t *args = hwif->hwgroup->rq->special;
189 local_irq_enable_in_hardirq();
190 stat = hwif->tp_ops->read_status(hwif);
192 if (!OK_STAT(stat, READY_STAT, BAD_STAT))
193 return ide_error(drive, "task_no_data_intr", stat);
194 /* calls ide_end_drive_cmd */
197 ide_end_drive_cmd(drive, stat, ide_read_error(drive));
202 static u8 wait_drive_not_busy(ide_drive_t *drive)
204 ide_hwif_t *hwif = drive->hwif;
209 * Last sector was transfered, wait until device is ready. This can
210 * take up to 6 ms on some ATAPI devices, so we will wait max 10 ms.
212 for (retries = 0; retries < 1000; retries++) {
213 stat = hwif->tp_ops->read_status(hwif);
215 if (stat & BUSY_STAT)
221 if (stat & BUSY_STAT)
222 printk(KERN_ERR "%s: drive still BUSY!\n", drive->name);
227 static void ide_pio_sector(ide_drive_t *drive, struct request *rq,
230 ide_hwif_t *hwif = drive->hwif;
231 struct scatterlist *sg = hwif->sg_table;
232 struct scatterlist *cursg = hwif->cursg;
234 #ifdef CONFIG_HIGHMEM
246 page = sg_page(cursg);
247 offset = cursg->offset + hwif->cursg_ofs * SECTOR_SIZE;
249 /* get the current page and offset */
250 page = nth_page(page, (offset >> PAGE_SHIFT));
253 #ifdef CONFIG_HIGHMEM
254 local_irq_save(flags);
256 buf = kmap_atomic(page, KM_BIO_SRC_IRQ) + offset;
261 if ((hwif->cursg_ofs * SECTOR_SIZE) == cursg->length) {
262 hwif->cursg = sg_next(hwif->cursg);
266 /* do the actual data transfer */
268 hwif->tp_ops->output_data(drive, rq, buf, SECTOR_SIZE);
270 hwif->tp_ops->input_data(drive, rq, buf, SECTOR_SIZE);
272 kunmap_atomic(buf, KM_BIO_SRC_IRQ);
273 #ifdef CONFIG_HIGHMEM
274 local_irq_restore(flags);
278 static void ide_pio_multi(ide_drive_t *drive, struct request *rq,
283 nsect = min_t(unsigned int, drive->hwif->nleft, drive->mult_count);
285 ide_pio_sector(drive, rq, write);
288 static void ide_pio_datablock(ide_drive_t *drive, struct request *rq,
291 u8 saved_io_32bit = drive->io_32bit;
293 if (rq->bio) /* fs request */
296 if (rq->cmd_type == REQ_TYPE_ATA_TASKFILE) {
297 ide_task_t *task = rq->special;
299 if (task->tf_flags & IDE_TFLAG_IO_16BIT)
303 touch_softlockup_watchdog();
305 switch (drive->hwif->data_phase) {
306 case TASKFILE_MULTI_IN:
307 case TASKFILE_MULTI_OUT:
308 ide_pio_multi(drive, rq, write);
311 ide_pio_sector(drive, rq, write);
315 drive->io_32bit = saved_io_32bit;
318 static ide_startstop_t task_error(ide_drive_t *drive, struct request *rq,
319 const char *s, u8 stat)
322 ide_hwif_t *hwif = drive->hwif;
323 int sectors = hwif->nsect - hwif->nleft;
325 switch (hwif->data_phase) {
333 case TASKFILE_MULTI_IN:
337 case TASKFILE_MULTI_OUT:
338 sectors -= drive->mult_count;
346 drv = *(ide_driver_t **)rq->rq_disk->private_data;
347 drv->end_request(drive, 1, sectors);
350 return ide_error(drive, s, stat);
353 void task_end_request(ide_drive_t *drive, struct request *rq, u8 stat)
355 if (rq->cmd_type == REQ_TYPE_ATA_TASKFILE) {
356 u8 err = ide_read_error(drive);
358 ide_end_drive_cmd(drive, stat, err);
365 drv = *(ide_driver_t **)rq->rq_disk->private_data;;
366 drv->end_request(drive, 1, rq->nr_sectors);
368 ide_end_request(drive, 1, rq->nr_sectors);
372 * We got an interrupt on a task_in case, but no errors and no DRQ.
374 * It might be a spurious irq (shared irq), but it might be a
375 * command that had no output.
377 static ide_startstop_t task_in_unexpected(ide_drive_t *drive, struct request *rq, u8 stat)
379 /* Command all done? */
380 if (OK_STAT(stat, READY_STAT, BUSY_STAT)) {
381 task_end_request(drive, rq, stat);
385 /* Assume it was a spurious irq */
386 ide_set_handler(drive, &task_in_intr, WAIT_WORSTCASE, NULL);
391 * Handler for command with PIO data-in phase (Read/Read Multiple).
393 static ide_startstop_t task_in_intr(ide_drive_t *drive)
395 ide_hwif_t *hwif = drive->hwif;
396 struct request *rq = hwif->hwgroup->rq;
397 u8 stat = hwif->tp_ops->read_status(hwif);
401 return task_error(drive, rq, __func__, stat);
403 /* Didn't want any data? Odd. */
404 if (!(stat & DRQ_STAT))
405 return task_in_unexpected(drive, rq, stat);
407 ide_pio_datablock(drive, rq, 0);
409 /* Are we done? Check status and finish transfer. */
411 stat = wait_drive_not_busy(drive);
412 if (!OK_STAT(stat, 0, BAD_STAT))
413 return task_error(drive, rq, __func__, stat);
414 task_end_request(drive, rq, stat);
418 /* Still data left to transfer. */
419 ide_set_handler(drive, &task_in_intr, WAIT_WORSTCASE, NULL);
425 * Handler for command with PIO data-out phase (Write/Write Multiple).
427 static ide_startstop_t task_out_intr (ide_drive_t *drive)
429 ide_hwif_t *hwif = drive->hwif;
430 struct request *rq = HWGROUP(drive)->rq;
431 u8 stat = hwif->tp_ops->read_status(hwif);
433 if (!OK_STAT(stat, DRIVE_READY, drive->bad_wstat))
434 return task_error(drive, rq, __func__, stat);
436 /* Deal with unexpected ATA data phase. */
437 if (((stat & DRQ_STAT) == 0) ^ !hwif->nleft)
438 return task_error(drive, rq, __func__, stat);
441 task_end_request(drive, rq, stat);
445 /* Still data left to transfer. */
446 ide_pio_datablock(drive, rq, 1);
447 ide_set_handler(drive, &task_out_intr, WAIT_WORSTCASE, NULL);
452 static ide_startstop_t pre_task_out_intr(ide_drive_t *drive, struct request *rq)
454 ide_startstop_t startstop;
456 if (ide_wait_stat(&startstop, drive, DRQ_STAT,
457 drive->bad_wstat, WAIT_DRQ)) {
458 printk(KERN_ERR "%s: no DRQ after issuing %sWRITE%s\n",
460 drive->hwif->data_phase ? "MULT" : "",
461 drive->addressing ? "_EXT" : "");
468 ide_set_handler(drive, &task_out_intr, WAIT_WORSTCASE, NULL);
469 ide_pio_datablock(drive, rq, 1);
474 int ide_raw_taskfile(ide_drive_t *drive, ide_task_t *task, u8 *buf, u16 nsect)
479 rq = blk_get_request(drive->queue, READ, __GFP_WAIT);
480 rq->cmd_type = REQ_TYPE_ATA_TASKFILE;
484 * (ks) We transfer currently only whole sectors.
485 * This is suffient for now. But, it would be great,
486 * if we would find a solution to transfer any size.
487 * To support special commands like READ LONG.
489 rq->hard_nr_sectors = rq->nr_sectors = nsect;
490 rq->hard_cur_sectors = rq->current_nr_sectors = nsect;
492 if (task->tf_flags & IDE_TFLAG_WRITE)
493 rq->cmd_flags |= REQ_RW;
498 error = blk_execute_rq(drive->queue, NULL, rq, 0);
504 EXPORT_SYMBOL(ide_raw_taskfile);
506 int ide_no_data_taskfile(ide_drive_t *drive, ide_task_t *task)
508 task->data_phase = TASKFILE_NO_DATA;
510 return ide_raw_taskfile(drive, task, NULL, 0);
512 EXPORT_SYMBOL_GPL(ide_no_data_taskfile);
514 #ifdef CONFIG_IDE_TASK_IOCTL
515 int ide_taskfile_ioctl (ide_drive_t *drive, unsigned int cmd, unsigned long arg)
517 ide_task_request_t *req_task;
523 int tasksize = sizeof(struct ide_task_request_s);
524 unsigned int taskin = 0;
525 unsigned int taskout = 0;
527 char __user *buf = (char __user *)arg;
529 // printk("IDE Taskfile ...\n");
531 req_task = kzalloc(tasksize, GFP_KERNEL);
532 if (req_task == NULL) return -ENOMEM;
533 if (copy_from_user(req_task, buf, tasksize)) {
538 taskout = req_task->out_size;
539 taskin = req_task->in_size;
541 if (taskin > 65536 || taskout > 65536) {
547 int outtotal = tasksize;
548 outbuf = kzalloc(taskout, GFP_KERNEL);
549 if (outbuf == NULL) {
553 if (copy_from_user(outbuf, buf + outtotal, taskout)) {
560 int intotal = tasksize + taskout;
561 inbuf = kzalloc(taskin, GFP_KERNEL);
566 if (copy_from_user(inbuf, buf + intotal, taskin)) {
572 memset(&args, 0, sizeof(ide_task_t));
574 memcpy(&args.tf_array[0], req_task->hob_ports, HDIO_DRIVE_HOB_HDR_SIZE - 2);
575 memcpy(&args.tf_array[6], req_task->io_ports, HDIO_DRIVE_TASK_HDR_SIZE);
577 args.data_phase = req_task->data_phase;
579 args.tf_flags = IDE_TFLAG_IO_16BIT | IDE_TFLAG_DEVICE |
581 if (drive->addressing == 1)
582 args.tf_flags |= (IDE_TFLAG_LBA48 | IDE_TFLAG_IN_HOB);
584 if (req_task->out_flags.all) {
585 args.tf_flags |= IDE_TFLAG_FLAGGED;
587 if (req_task->out_flags.b.data)
588 args.tf_flags |= IDE_TFLAG_OUT_DATA;
590 if (req_task->out_flags.b.nsector_hob)
591 args.tf_flags |= IDE_TFLAG_OUT_HOB_NSECT;
592 if (req_task->out_flags.b.sector_hob)
593 args.tf_flags |= IDE_TFLAG_OUT_HOB_LBAL;
594 if (req_task->out_flags.b.lcyl_hob)
595 args.tf_flags |= IDE_TFLAG_OUT_HOB_LBAM;
596 if (req_task->out_flags.b.hcyl_hob)
597 args.tf_flags |= IDE_TFLAG_OUT_HOB_LBAH;
599 if (req_task->out_flags.b.error_feature)
600 args.tf_flags |= IDE_TFLAG_OUT_FEATURE;
601 if (req_task->out_flags.b.nsector)
602 args.tf_flags |= IDE_TFLAG_OUT_NSECT;
603 if (req_task->out_flags.b.sector)
604 args.tf_flags |= IDE_TFLAG_OUT_LBAL;
605 if (req_task->out_flags.b.lcyl)
606 args.tf_flags |= IDE_TFLAG_OUT_LBAM;
607 if (req_task->out_flags.b.hcyl)
608 args.tf_flags |= IDE_TFLAG_OUT_LBAH;
610 args.tf_flags |= IDE_TFLAG_OUT_TF;
611 if (args.tf_flags & IDE_TFLAG_LBA48)
612 args.tf_flags |= IDE_TFLAG_OUT_HOB;
615 if (req_task->in_flags.b.data)
616 args.tf_flags |= IDE_TFLAG_IN_DATA;
618 switch(req_task->data_phase) {
619 case TASKFILE_MULTI_OUT:
620 if (!drive->mult_count) {
621 /* (hs): give up if multcount is not set */
622 printk(KERN_ERR "%s: %s Multimode Write " \
623 "multcount is not set\n",
624 drive->name, __func__);
631 case TASKFILE_OUT_DMAQ:
632 case TASKFILE_OUT_DMA:
633 nsect = taskout / SECTOR_SIZE;
636 case TASKFILE_MULTI_IN:
637 if (!drive->mult_count) {
638 /* (hs): give up if multcount is not set */
639 printk(KERN_ERR "%s: %s Multimode Read failure " \
640 "multcount is not set\n",
641 drive->name, __func__);
648 case TASKFILE_IN_DMAQ:
649 case TASKFILE_IN_DMA:
650 nsect = taskin / SECTOR_SIZE;
653 case TASKFILE_NO_DATA:
660 if (req_task->req_cmd == IDE_DRIVE_TASK_NO_DATA)
663 nsect = (args.tf.hob_nsect << 8) | args.tf.nsect;
666 printk(KERN_ERR "%s: in/out command without data\n",
673 if (req_task->req_cmd == IDE_DRIVE_TASK_RAW_WRITE)
674 args.tf_flags |= IDE_TFLAG_WRITE;
676 err = ide_raw_taskfile(drive, &args, data_buf, nsect);
678 memcpy(req_task->hob_ports, &args.tf_array[0], HDIO_DRIVE_HOB_HDR_SIZE - 2);
679 memcpy(req_task->io_ports, &args.tf_array[6], HDIO_DRIVE_TASK_HDR_SIZE);
681 if ((args.tf_flags & IDE_TFLAG_FLAGGED_SET_IN_FLAGS) &&
682 req_task->in_flags.all == 0) {
683 req_task->in_flags.all = IDE_TASKFILE_STD_IN_FLAGS;
684 if (drive->addressing == 1)
685 req_task->in_flags.all |= (IDE_HOB_STD_IN_FLAGS << 8);
688 if (copy_to_user(buf, req_task, tasksize)) {
693 int outtotal = tasksize;
694 if (copy_to_user(buf + outtotal, outbuf, taskout)) {
700 int intotal = tasksize + taskout;
701 if (copy_to_user(buf + intotal, inbuf, taskin)) {
711 // printk("IDE Taskfile ioctl ended. rc = %i\n", err);
717 int ide_cmd_ioctl (ide_drive_t *drive, unsigned int cmd, unsigned long arg)
720 int bufsize = 0, err = 0;
721 u8 args[4], xfer_rate = 0;
723 struct ide_taskfile *tf = &tfargs.tf;
724 struct hd_driveid *id = drive->id;
726 if (NULL == (void *) arg) {
729 rq = blk_get_request(drive->queue, READ, __GFP_WAIT);
730 rq->cmd_type = REQ_TYPE_ATA_TASKFILE;
731 err = blk_execute_rq(drive->queue, NULL, rq, 0);
737 if (copy_from_user(args, (void __user *)arg, 4))
740 memset(&tfargs, 0, sizeof(ide_task_t));
741 tf->feature = args[2];
742 if (args[0] == WIN_SMART) {
747 tfargs.tf_flags = IDE_TFLAG_OUT_TF | IDE_TFLAG_IN_NSECT;
750 tfargs.tf_flags = IDE_TFLAG_OUT_FEATURE |
751 IDE_TFLAG_OUT_NSECT | IDE_TFLAG_IN_NSECT;
753 tf->command = args[0];
754 tfargs.data_phase = args[3] ? TASKFILE_IN : TASKFILE_NO_DATA;
757 tfargs.tf_flags |= IDE_TFLAG_IO_16BIT;
758 bufsize = SECTOR_WORDS * 4 * args[3];
759 buf = kzalloc(bufsize, GFP_KERNEL);
764 if (tf->command == WIN_SETFEATURES &&
765 tf->feature == SETFEATURES_XFER &&
766 tf->nsect >= XFER_SW_DMA_0 &&
767 (id->dma_ultra || id->dma_mword || id->dma_1word)) {
769 if (tf->nsect > XFER_UDMA_2 && !eighty_ninty_three(drive)) {
770 printk(KERN_WARNING "%s: UDMA speeds >UDMA33 cannot "
771 "be set\n", drive->name);
776 err = ide_raw_taskfile(drive, &tfargs, buf, args[3]);
778 args[0] = tf->status;
782 if (!err && xfer_rate) {
783 /* active-retuning-calls future */
784 ide_set_xfer_rate(drive, xfer_rate);
785 ide_driveid_update(drive);
788 if (copy_to_user((void __user *)arg, &args, 4))
791 if (copy_to_user((void __user *)(arg + 4), buf, bufsize))
798 int ide_task_ioctl (ide_drive_t *drive, unsigned int cmd, unsigned long arg)
800 void __user *p = (void __user *)arg;
805 if (copy_from_user(args, p, 7))
808 memset(&task, 0, sizeof(task));
809 memcpy(&task.tf_array[7], &args[1], 6);
810 task.tf.command = args[0];
811 task.tf_flags = IDE_TFLAG_TF | IDE_TFLAG_DEVICE;
813 err = ide_no_data_taskfile(drive, &task);
815 args[0] = task.tf.command;
816 memcpy(&args[1], &task.tf_array[7], 6);
818 if (copy_to_user(p, args, 7))