blob: 249a707f88a47f9e5c3dd08442a04afe5e132b7d [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Bartlomiej Zolnierkiewicz59bca8c2008-02-01 23:09:33 +01002 * 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
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * IBM Storage Technology Division
Bartlomiej Zolnierkiewicz59bca8c2008-02-01 23:09:33 +01006 * Copyright (C) 2003-2004, 2007 Bartlomiej Zolnierkiewicz
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
8 * The big the bad and the ugly.
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 */
10
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/types.h>
12#include <linux/string.h>
13#include <linux/kernel.h>
Andrew Morton651c29a2006-02-15 15:17:37 -080014#include <linux/sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/interrupt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/errno.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/delay.h>
19#include <linux/hdreg.h>
20#include <linux/ide.h>
Jens Axboe55c16a72007-07-25 08:13:56 +020021#include <linux/scatterlist.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <asm/uaccess.h>
24#include <asm/io.h>
25
Bartlomiej Zolnierkiewicz089c5c72008-04-28 23:44:39 +020026void ide_tf_dump(const char *s, struct ide_taskfile *tf)
27{
28#ifdef DEBUG
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);
37#endif
38}
39
Linus Torvalds1da177e2005-04-16 15:20:36 -070040int taskfile_lib_get_identify (ide_drive_t *drive, u8 *buf)
41{
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +010042 struct ide_cmd cmd;
Bartlomiej Zolnierkiewicz650d8412008-01-25 22:17:06 +010043
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +010044 memset(&cmd, 0, sizeof(cmd));
45 cmd.tf.nsect = 0x01;
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 if (drive->media == ide_disk)
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +010047 cmd.tf.command = ATA_CMD_ID_ATA;
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 else
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +010049 cmd.tf.command = ATA_CMD_ID_ATAPI;
50 cmd.tf_flags = IDE_TFLAG_TF | IDE_TFLAG_DEVICE;
51 cmd.data_phase = TASKFILE_IN;
52
53 return ide_raw_taskfile(drive, &cmd, buf, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070054}
55
Bartlomiej Zolnierkiewicz1192e522008-01-25 22:17:16 +010056static ide_startstop_t task_no_data_intr(ide_drive_t *);
Bartlomiej Zolnierkiewiczadb1af92009-03-27 12:46:38 +010057static ide_startstop_t pre_task_out_intr(ide_drive_t *, struct ide_cmd *);
Bartlomiej Zolnierkiewiczf6e29e32008-01-25 22:17:16 +010058static ide_startstop_t task_in_intr(ide_drive_t *);
Bartlomiej Zolnierkiewicz1192e522008-01-25 22:17:16 +010059
Bartlomiej Zolnierkiewiczadb1af92009-03-27 12:46:38 +010060ide_startstop_t do_rw_taskfile(ide_drive_t *drive, struct ide_cmd *orig_cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -070061{
Bartlomiej Zolnierkiewicz898ec222009-01-06 17:20:52 +010062 ide_hwif_t *hwif = drive->hwif;
Bartlomiej Zolnierkiewiczadb1af92009-03-27 12:46:38 +010063 struct ide_cmd *cmd = &hwif->cmd;
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +010064 struct ide_taskfile *tf = &cmd->tf;
Bartlomiej Zolnierkiewicz57d73662008-01-25 22:17:16 +010065 ide_handler_t *handler = NULL;
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +020066 const struct ide_tp_ops *tp_ops = hwif->tp_ops;
Bartlomiej Zolnierkiewiczf37afda2008-04-26 22:25:24 +020067 const struct ide_dma_ops *dma_ops = hwif->dma_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
Bartlomiej Zolnierkiewiczadb1af92009-03-27 12:46:38 +010069 if (orig_cmd->data_phase == TASKFILE_MULTI_IN ||
70 orig_cmd->data_phase == TASKFILE_MULTI_OUT) {
Bartlomiej Zolnierkiewicz1edee602008-01-25 22:17:15 +010071 if (!drive->mult_count) {
72 printk(KERN_ERR "%s: multimode not set!\n",
73 drive->name);
74 return ide_stopped;
75 }
76 }
77
Bartlomiej Zolnierkiewiczadb1af92009-03-27 12:46:38 +010078 if (orig_cmd->ftf_flags & IDE_FTFLAG_FLAGGED)
79 orig_cmd->ftf_flags |= IDE_FTFLAG_SET_IN_FLAGS;
Bartlomiej Zolnierkiewicz1edee602008-01-25 22:17:15 +010080
Bartlomiej Zolnierkiewiczadb1af92009-03-27 12:46:38 +010081 memcpy(cmd, orig_cmd, sizeof(*cmd));
Bartlomiej Zolnierkiewiczd6ff9f62008-10-13 21:39:41 +020082
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +010083 if ((cmd->tf_flags & IDE_TFLAG_DMA_PIO_FALLBACK) == 0) {
Bartlomiej Zolnierkiewicz089c5c72008-04-28 23:44:39 +020084 ide_tf_dump(drive->name, tf);
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +020085 tp_ops->set_irq(hwif, 1);
Bartlomiej Zolnierkiewiczed4af482008-07-15 21:21:48 +020086 SELECT_MASK(drive, 0);
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +010087 tp_ops->tf_load(drive, cmd);
Bartlomiej Zolnierkiewicz089c5c72008-04-28 23:44:39 +020088 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +010090 switch (cmd->data_phase) {
Bartlomiej Zolnierkiewicz10d90152008-01-25 22:17:16 +010091 case TASKFILE_MULTI_OUT:
92 case TASKFILE_OUT:
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +020093 tp_ops->exec_command(hwif, tf->command);
Bartlomiej Zolnierkiewicz10d90152008-01-25 22:17:16 +010094 ndelay(400); /* FIXME */
Bartlomiej Zolnierkiewiczadb1af92009-03-27 12:46:38 +010095 return pre_task_out_intr(drive, cmd);
Bartlomiej Zolnierkiewicz10d90152008-01-25 22:17:16 +010096 case TASKFILE_MULTI_IN:
97 case TASKFILE_IN:
Bartlomiej Zolnierkiewicz57d73662008-01-25 22:17:16 +010098 handler = task_in_intr;
Bartlomiej Zolnierkiewicz1192e522008-01-25 22:17:16 +010099 /* fall-through */
Bartlomiej Zolnierkiewicz10d90152008-01-25 22:17:16 +0100100 case TASKFILE_NO_DATA:
Bartlomiej Zolnierkiewicz57d73662008-01-25 22:17:16 +0100101 if (handler == NULL)
102 handler = task_no_data_intr;
Bartlomiej Zolnierkiewicz57d73662008-01-25 22:17:16 +0100103 ide_execute_command(drive, tf->command, handler,
104 WAIT_WORSTCASE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 return ide_started;
Bartlomiej Zolnierkiewicz10d90152008-01-25 22:17:16 +0100106 default:
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200107 if ((drive->dev_flags & IDE_DFLAG_USING_DMA) == 0 ||
Bartlomiej Zolnierkiewicze6830a82009-03-27 12:46:37 +0100108 ide_build_sglist(drive, hwif->rq) == 0 ||
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200109 dma_ops->dma_setup(drive))
Bartlomiej Zolnierkiewicz10d90152008-01-25 22:17:16 +0100110 return ide_stopped;
Bartlomiej Zolnierkiewicz5e37bdc2008-04-26 22:25:24 +0200111 dma_ops->dma_exec_cmd(drive, tf->command);
112 dma_ops->dma_start(drive);
Bartlomiej Zolnierkiewicz74095a92008-01-25 22:17:07 +0100113 return ide_started;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115}
Bartlomiej Zolnierkiewiczf6e29e32008-01-25 22:17:16 +0100116EXPORT_SYMBOL_GPL(do_rw_taskfile);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118/*
Bartlomiej Zolnierkiewiczd6ff9f62008-10-13 21:39:41 +0200119 * Handler for commands without a data phase
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 */
Bartlomiej Zolnierkiewiczd6ff9f62008-10-13 21:39:41 +0200121static ide_startstop_t task_no_data_intr(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122{
Bartlomiej Zolnierkiewiczb73c7ee2008-07-23 19:55:52 +0200123 ide_hwif_t *hwif = drive->hwif;
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100124 struct ide_cmd *cmd = &hwif->cmd;
125 struct ide_taskfile *tf = &cmd->tf;
126 int custom = (cmd->tf_flags & IDE_TFLAG_CUSTOM_HANDLER) ? 1 : 0;
Bartlomiej Zolnierkiewiczd6ff9f62008-10-13 21:39:41 +0200127 int retries = (custom && tf->command == ATA_CMD_INIT_DEV_PARAMS) ? 5 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 u8 stat;
129
Bartlomiej Zolnierkiewicz90d2c6b2008-07-24 22:53:36 +0200130 local_irq_enable_in_hardirq();
131
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200132 while (1) {
133 stat = hwif->tp_ops->read_status(hwif);
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +0200134 if ((stat & ATA_BUSY) == 0 || retries-- == 0)
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200135 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 udelay(10);
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200137 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
Bartlomiej Zolnierkiewiczd6ff9f62008-10-13 21:39:41 +0200139 if (!OK_STAT(stat, ATA_DRDY, BAD_STAT)) {
140 if (custom && tf->command == ATA_CMD_SET_MULTI) {
141 drive->mult_req = drive->mult_count = 0;
142 drive->special.b.recalibrate = 1;
143 (void)ide_dump_status(drive, __func__, stat);
144 return ide_stopped;
145 } else if (custom && tf->command == ATA_CMD_INIT_DEV_PARAMS) {
146 if ((stat & (ATA_ERR | ATA_DRQ)) == 0) {
147 ide_set_handler(drive, &task_no_data_intr,
148 WAIT_WORSTCASE, NULL);
149 return ide_started;
150 }
151 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 return ide_error(drive, "task_no_data_intr", stat);
Bartlomiej Zolnierkiewiczd6ff9f62008-10-13 21:39:41 +0200153 }
Bartlomiej Zolnierkiewiczc47137a2008-02-06 02:57:51 +0100154
Bartlomiej Zolnierkiewicza09485d2009-03-27 12:46:31 +0100155 if (custom && tf->command == ATA_CMD_IDLEIMMEDIATE) {
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100156 hwif->tp_ops->tf_read(drive, cmd);
Elias Oltmanns4abdc6e2008-10-13 21:39:50 +0200157 if (tf->lbal != 0xc4) {
158 printk(KERN_ERR "%s: head unload failed!\n",
159 drive->name);
160 ide_tf_dump(drive->name, tf);
161 } else
162 drive->dev_flags |= IDE_DFLAG_PARKED;
Bartlomiej Zolnierkiewicza09485d2009-03-27 12:46:31 +0100163 } else if (custom && tf->command == ATA_CMD_SET_MULTI)
Bartlomiej Zolnierkiewiczd6ff9f62008-10-13 21:39:41 +0200164 drive->mult_count = drive->mult_req;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
Bartlomiej Zolnierkiewicza09485d2009-03-27 12:46:31 +0100166 if (custom == 0 || tf->command == ATA_CMD_IDLEIMMEDIATE) {
167 struct request *rq = hwif->rq;
168 u8 err = ide_read_error(drive);
169
170 if (blk_pm_request(rq))
171 ide_complete_pm_rq(drive, rq);
172 else {
173 if (rq->cmd_type == REQ_TYPE_ATA_TASKFILE)
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100174 ide_complete_cmd(drive, cmd, stat, err);
Bartlomiej Zolnierkiewicza09485d2009-03-27 12:46:31 +0100175 ide_complete_rq(drive, err);
176 }
177 }
178
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 return ide_stopped;
180}
181
Adrian Bunkda6f4c72008-02-01 23:09:16 +0100182static u8 wait_drive_not_busy(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183{
Bartlomiej Zolnierkiewiczb73c7ee2008-07-23 19:55:52 +0200184 ide_hwif_t *hwif = drive->hwif;
Masatake YAMATOb42fa132007-07-03 22:28:34 +0200185 int retries;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 u8 stat;
187
188 /*
Bartlomiej Zolnierkiewiczf54feaf2008-06-20 20:53:33 +0200189 * Last sector was transfered, wait until device is ready. This can
190 * take up to 6 ms on some ATAPI devices, so we will wait max 10 ms.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 */
Bartlomiej Zolnierkiewiczf54feaf2008-06-20 20:53:33 +0200192 for (retries = 0; retries < 1000; retries++) {
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200193 stat = hwif->tp_ops->read_status(hwif);
Bartlomiej Zolnierkiewiczc47137a2008-02-06 02:57:51 +0100194
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +0200195 if (stat & ATA_BUSY)
Masatake YAMATOb42fa132007-07-03 22:28:34 +0200196 udelay(10);
197 else
198 break;
199 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +0200201 if (stat & ATA_BUSY)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 printk(KERN_ERR "%s: drive still BUSY!\n", drive->name);
203
204 return stat;
205}
206
Bartlomiej Zolnierkiewiczadb1af92009-03-27 12:46:38 +0100207static void ide_pio_sector(ide_drive_t *drive, struct ide_cmd *cmd,
Bartlomiej Zolnierkiewicz92d3ab22008-04-28 23:44:36 +0200208 unsigned int write)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209{
210 ide_hwif_t *hwif = drive->hwif;
211 struct scatterlist *sg = hwif->sg_table;
Bartlomiej Zolnierkiewiczb6308ee2009-03-27 12:46:38 +0100212 struct scatterlist *cursg = cmd->cursg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 struct page *page;
214#ifdef CONFIG_HIGHMEM
215 unsigned long flags;
216#endif
217 unsigned int offset;
218 u8 *buf;
219
Bartlomiej Zolnierkiewiczb6308ee2009-03-27 12:46:38 +0100220 cursg = cmd->cursg;
Jens Axboe55c16a72007-07-25 08:13:56 +0200221 if (!cursg) {
222 cursg = sg;
Bartlomiej Zolnierkiewiczb6308ee2009-03-27 12:46:38 +0100223 cmd->cursg = sg;
Jens Axboe55c16a72007-07-25 08:13:56 +0200224 }
225
Jens Axboe45711f12007-10-22 21:19:53 +0200226 page = sg_page(cursg);
Bartlomiej Zolnierkiewiczb6308ee2009-03-27 12:46:38 +0100227 offset = cursg->offset + cmd->cursg_ofs * SECTOR_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228
229 /* get the current page and offset */
230 page = nth_page(page, (offset >> PAGE_SHIFT));
231 offset %= PAGE_SIZE;
232
233#ifdef CONFIG_HIGHMEM
234 local_irq_save(flags);
235#endif
236 buf = kmap_atomic(page, KM_BIO_SRC_IRQ) + offset;
237
Bartlomiej Zolnierkiewiczb6308ee2009-03-27 12:46:38 +0100238 cmd->nleft--;
239 cmd->cursg_ofs++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
Bartlomiej Zolnierkiewiczb6308ee2009-03-27 12:46:38 +0100241 if ((cmd->cursg_ofs * SECTOR_SIZE) == cursg->length) {
242 cmd->cursg = sg_next(cmd->cursg);
243 cmd->cursg_ofs = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 }
245
246 /* do the actual data transfer */
247 if (write)
Bartlomiej Zolnierkiewiczadb1af92009-03-27 12:46:38 +0100248 hwif->tp_ops->output_data(drive, cmd, buf, SECTOR_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 else
Bartlomiej Zolnierkiewiczadb1af92009-03-27 12:46:38 +0100250 hwif->tp_ops->input_data(drive, cmd, buf, SECTOR_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
252 kunmap_atomic(buf, KM_BIO_SRC_IRQ);
253#ifdef CONFIG_HIGHMEM
254 local_irq_restore(flags);
255#endif
256}
257
Bartlomiej Zolnierkiewiczadb1af92009-03-27 12:46:38 +0100258static void ide_pio_multi(ide_drive_t *drive, struct ide_cmd *cmd,
Bartlomiej Zolnierkiewicz92d3ab22008-04-28 23:44:36 +0200259 unsigned int write)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260{
261 unsigned int nsect;
262
Bartlomiej Zolnierkiewiczb6308ee2009-03-27 12:46:38 +0100263 nsect = min_t(unsigned int, cmd->nleft, drive->mult_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 while (nsect--)
Bartlomiej Zolnierkiewiczadb1af92009-03-27 12:46:38 +0100265 ide_pio_sector(drive, cmd, write);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266}
267
Bartlomiej Zolnierkiewiczadb1af92009-03-27 12:46:38 +0100268static void ide_pio_datablock(ide_drive_t *drive, struct ide_cmd *cmd,
269 unsigned int write)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270{
Tejun Heo35cf2b92008-01-26 20:13:10 +0100271 u8 saved_io_32bit = drive->io_32bit;
272
Bartlomiej Zolnierkiewiczadb1af92009-03-27 12:46:38 +0100273 if (cmd->tf_flags & IDE_TFLAG_FS)
274 cmd->rq->errors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100276 if (cmd->tf_flags & IDE_TFLAG_IO_16BIT)
Bartlomiej Zolnierkiewicze3d9a732009-03-27 12:46:32 +0100277 drive->io_32bit = 0;
Tejun Heo35cf2b92008-01-26 20:13:10 +0100278
Andrew Morton651c29a2006-02-15 15:17:37 -0800279 touch_softlockup_watchdog();
280
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100281 switch (cmd->data_phase) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 case TASKFILE_MULTI_IN:
283 case TASKFILE_MULTI_OUT:
Bartlomiej Zolnierkiewiczadb1af92009-03-27 12:46:38 +0100284 ide_pio_multi(drive, cmd, write);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 break;
286 default:
Bartlomiej Zolnierkiewiczadb1af92009-03-27 12:46:38 +0100287 ide_pio_sector(drive, cmd, write);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 break;
289 }
Tejun Heo35cf2b92008-01-26 20:13:10 +0100290
291 drive->io_32bit = saved_io_32bit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292}
293
Bartlomiej Zolnierkiewiczadb1af92009-03-27 12:46:38 +0100294static ide_startstop_t task_error(ide_drive_t *drive, struct ide_cmd *cmd,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 const char *s, u8 stat)
296{
Bartlomiej Zolnierkiewiczadb1af92009-03-27 12:46:38 +0100297 if (cmd->tf_flags & IDE_TFLAG_FS) {
Bartlomiej Zolnierkiewiczb6308ee2009-03-27 12:46:38 +0100298 int sectors = cmd->nsect - cmd->nleft;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100300 switch (cmd->data_phase) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 case TASKFILE_IN:
Bartlomiej Zolnierkiewiczb6308ee2009-03-27 12:46:38 +0100302 if (cmd->nleft)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 break;
304 /* fall through */
305 case TASKFILE_OUT:
306 sectors--;
307 break;
308 case TASKFILE_MULTI_IN:
Bartlomiej Zolnierkiewiczb6308ee2009-03-27 12:46:38 +0100309 if (cmd->nleft)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 break;
311 /* fall through */
312 case TASKFILE_MULTI_OUT:
313 sectors -= drive->mult_count;
314 default:
315 break;
316 }
317
Bartlomiej Zolnierkiewiczc152cc12009-03-27 12:46:34 +0100318 if (sectors > 0)
319 ide_end_request(drive, 1, sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 }
321 return ide_error(drive, s, stat);
322}
323
Bartlomiej Zolnierkiewiczadb1af92009-03-27 12:46:38 +0100324void ide_finish_cmd(ide_drive_t *drive, struct ide_cmd *cmd, u8 stat)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325{
Bartlomiej Zolnierkiewiczadb1af92009-03-27 12:46:38 +0100326 if ((cmd->tf_flags & IDE_TFLAG_FS) == 0) {
Bartlomiej Zolnierkiewicz64a57fe2008-02-06 02:57:51 +0100327 u8 err = ide_read_error(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328
Bartlomiej Zolnierkiewiczadb1af92009-03-27 12:46:38 +0100329 ide_complete_cmd(drive, cmd, stat, err);
Bartlomiej Zolnierkiewicza09485d2009-03-27 12:46:31 +0100330 ide_complete_rq(drive, err);
Tejun Heo4d7a9842008-01-26 20:13:11 +0100331 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 }
333
Bartlomiej Zolnierkiewiczadb1af92009-03-27 12:46:38 +0100334 ide_end_request(drive, 1, cmd->rq->nr_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335}
336
337/*
Linus Torvalds6c3c3152008-03-18 21:26:24 -0700338 * We got an interrupt on a task_in case, but no errors and no DRQ.
339 *
340 * It might be a spurious irq (shared irq), but it might be a
341 * command that had no output.
342 */
Bartlomiej Zolnierkiewiczadb1af92009-03-27 12:46:38 +0100343static ide_startstop_t task_in_unexpected(ide_drive_t *drive,
344 struct ide_cmd *cmd, u8 stat)
Linus Torvalds6c3c3152008-03-18 21:26:24 -0700345{
346 /* Command all done? */
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +0200347 if (OK_STAT(stat, ATA_DRDY, ATA_BUSY)) {
Bartlomiej Zolnierkiewiczadb1af92009-03-27 12:46:38 +0100348 ide_finish_cmd(drive, cmd, stat);
Linus Torvalds6c3c3152008-03-18 21:26:24 -0700349 return ide_stopped;
350 }
351
352 /* Assume it was a spurious irq */
353 ide_set_handler(drive, &task_in_intr, WAIT_WORSTCASE, NULL);
354 return ide_started;
355}
356
357/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 * Handler for command with PIO data-in phase (Read/Read Multiple).
359 */
Bartlomiej Zolnierkiewiczf6e29e32008-01-25 22:17:16 +0100360static ide_startstop_t task_in_intr(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361{
362 ide_hwif_t *hwif = drive->hwif;
Bartlomiej Zolnierkiewiczadb1af92009-03-27 12:46:38 +0100363 struct ide_cmd *cmd = &drive->hwif->cmd;
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200364 u8 stat = hwif->tp_ops->read_status(hwif);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365
Linus Torvalds6c3c3152008-03-18 21:26:24 -0700366 /* Error? */
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +0200367 if (stat & ATA_ERR)
Bartlomiej Zolnierkiewiczadb1af92009-03-27 12:46:38 +0100368 return task_error(drive, cmd, __func__, stat);
Linus Torvalds6c3c3152008-03-18 21:26:24 -0700369
370 /* Didn't want any data? Odd. */
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +0200371 if ((stat & ATA_DRQ) == 0)
Bartlomiej Zolnierkiewiczadb1af92009-03-27 12:46:38 +0100372 return task_in_unexpected(drive, cmd, stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
Bartlomiej Zolnierkiewiczadb1af92009-03-27 12:46:38 +0100374 ide_pio_datablock(drive, cmd, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
Linus Torvalds6c3c3152008-03-18 21:26:24 -0700376 /* Are we done? Check status and finish transfer. */
Bartlomiej Zolnierkiewiczb6308ee2009-03-27 12:46:38 +0100377 if (cmd->nleft == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 stat = wait_drive_not_busy(drive);
Bartlomiej Zolnierkiewicz73d7de02008-01-26 20:13:10 +0100379 if (!OK_STAT(stat, 0, BAD_STAT))
Bartlomiej Zolnierkiewiczadb1af92009-03-27 12:46:38 +0100380 return task_error(drive, cmd, __func__, stat);
381 ide_finish_cmd(drive, cmd, stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 return ide_stopped;
383 }
384
385 /* Still data left to transfer. */
386 ide_set_handler(drive, &task_in_intr, WAIT_WORSTCASE, NULL);
387
388 return ide_started;
389}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390
391/*
392 * Handler for command with PIO data-out phase (Write/Write Multiple).
393 */
394static ide_startstop_t task_out_intr (ide_drive_t *drive)
395{
396 ide_hwif_t *hwif = drive->hwif;
Bartlomiej Zolnierkiewiczadb1af92009-03-27 12:46:38 +0100397 struct ide_cmd *cmd = &drive->hwif->cmd;
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200398 u8 stat = hwif->tp_ops->read_status(hwif);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
400 if (!OK_STAT(stat, DRIVE_READY, drive->bad_wstat))
Bartlomiej Zolnierkiewiczadb1af92009-03-27 12:46:38 +0100401 return task_error(drive, cmd, __func__, stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
403 /* Deal with unexpected ATA data phase. */
Bartlomiej Zolnierkiewiczb6308ee2009-03-27 12:46:38 +0100404 if (((stat & ATA_DRQ) == 0) ^ (cmd->nleft == 0))
Bartlomiej Zolnierkiewiczadb1af92009-03-27 12:46:38 +0100405 return task_error(drive, cmd, __func__, stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406
Bartlomiej Zolnierkiewiczb6308ee2009-03-27 12:46:38 +0100407 if (cmd->nleft == 0) {
Bartlomiej Zolnierkiewiczadb1af92009-03-27 12:46:38 +0100408 ide_finish_cmd(drive, cmd, stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 return ide_stopped;
410 }
411
412 /* Still data left to transfer. */
Bartlomiej Zolnierkiewiczadb1af92009-03-27 12:46:38 +0100413 ide_pio_datablock(drive, cmd, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 ide_set_handler(drive, &task_out_intr, WAIT_WORSTCASE, NULL);
415
416 return ide_started;
417}
418
Bartlomiej Zolnierkiewiczadb1af92009-03-27 12:46:38 +0100419static ide_startstop_t pre_task_out_intr(ide_drive_t *drive,
420 struct ide_cmd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421{
422 ide_startstop_t startstop;
423
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +0200424 if (ide_wait_stat(&startstop, drive, ATA_DRQ,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 drive->bad_wstat, WAIT_DRQ)) {
426 printk(KERN_ERR "%s: no DRQ after issuing %sWRITE%s\n",
Bartlomiej Zolnierkiewiczc7db9662009-03-27 12:46:27 +0100427 drive->name,
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100428 cmd->data_phase == TASKFILE_MULTI_OUT ? "MULT" : "",
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200429 (drive->dev_flags & IDE_DFLAG_LBA48) ? "_EXT" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 return startstop;
431 }
432
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200433 if ((drive->dev_flags & IDE_DFLAG_UNMASK) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 local_irq_disable();
435
436 ide_set_handler(drive, &task_out_intr, WAIT_WORSTCASE, NULL);
Bartlomiej Zolnierkiewiczadb1af92009-03-27 12:46:38 +0100437 ide_pio_datablock(drive, cmd, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438
439 return ide_started;
440}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100442int ide_raw_taskfile(ide_drive_t *drive, struct ide_cmd *cmd, u8 *buf,
443 u16 nsect)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444{
FUJITA Tomonori154ed282008-07-15 21:21:43 +0200445 struct request *rq;
446 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447
FUJITA Tomonori154ed282008-07-15 21:21:43 +0200448 rq = blk_get_request(drive->queue, READ, __GFP_WAIT);
449 rq->cmd_type = REQ_TYPE_ATA_TASKFILE;
450 rq->buffer = buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451
452 /*
453 * (ks) We transfer currently only whole sectors.
454 * This is suffient for now. But, it would be great,
455 * if we would find a solution to transfer any size.
456 * To support special commands like READ LONG.
457 */
FUJITA Tomonori154ed282008-07-15 21:21:43 +0200458 rq->hard_nr_sectors = rq->nr_sectors = nsect;
459 rq->hard_cur_sectors = rq->current_nr_sectors = nsect;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100461 if (cmd->tf_flags & IDE_TFLAG_WRITE)
FUJITA Tomonori154ed282008-07-15 21:21:43 +0200462 rq->cmd_flags |= REQ_RW;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100464 rq->special = cmd;
465 cmd->rq = rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466
FUJITA Tomonori154ed282008-07-15 21:21:43 +0200467 error = blk_execute_rq(drive->queue, NULL, rq, 0);
468 blk_put_request(rq);
469
470 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471}
472
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473EXPORT_SYMBOL(ide_raw_taskfile);
474
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100475int ide_no_data_taskfile(ide_drive_t *drive, struct ide_cmd *cmd)
Bartlomiej Zolnierkiewicz9a3c49b2008-01-25 22:17:07 +0100476{
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100477 cmd->data_phase = TASKFILE_NO_DATA;
Bartlomiej Zolnierkiewicz9a3c49b2008-01-25 22:17:07 +0100478
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100479 return ide_raw_taskfile(drive, cmd, NULL, 0);
Bartlomiej Zolnierkiewicz9a3c49b2008-01-25 22:17:07 +0100480}
481EXPORT_SYMBOL_GPL(ide_no_data_taskfile);
482
Bartlomiej Zolnierkiewicz26a5b042007-11-05 21:42:27 +0100483#ifdef CONFIG_IDE_TASK_IOCTL
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100484int ide_taskfile_ioctl(ide_drive_t *drive, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485{
486 ide_task_request_t *req_task;
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100487 struct ide_cmd cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 u8 *outbuf = NULL;
489 u8 *inbuf = NULL;
Bartlomiej Zolnierkiewiczac026ff2008-01-25 22:17:14 +0100490 u8 *data_buf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 int err = 0;
492 int tasksize = sizeof(struct ide_task_request_s);
Alan Cox3a42bb22006-10-16 16:31:02 +0100493 unsigned int taskin = 0;
494 unsigned int taskout = 0;
Bartlomiej Zolnierkiewiczac026ff2008-01-25 22:17:14 +0100495 u16 nsect = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 char __user *buf = (char __user *)arg;
497
498// printk("IDE Taskfile ...\n");
499
Deepak Saxenaf5e3c2f2005-11-07 01:01:25 -0800500 req_task = kzalloc(tasksize, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 if (req_task == NULL) return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 if (copy_from_user(req_task, buf, tasksize)) {
503 kfree(req_task);
504 return -EFAULT;
505 }
506
Alan Cox3a42bb22006-10-16 16:31:02 +0100507 taskout = req_task->out_size;
508 taskin = req_task->in_size;
509
510 if (taskin > 65536 || taskout > 65536) {
511 err = -EINVAL;
512 goto abort;
513 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514
515 if (taskout) {
516 int outtotal = tasksize;
Deepak Saxenaf5e3c2f2005-11-07 01:01:25 -0800517 outbuf = kzalloc(taskout, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 if (outbuf == NULL) {
519 err = -ENOMEM;
520 goto abort;
521 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 if (copy_from_user(outbuf, buf + outtotal, taskout)) {
523 err = -EFAULT;
524 goto abort;
525 }
526 }
527
528 if (taskin) {
529 int intotal = tasksize + taskout;
Deepak Saxenaf5e3c2f2005-11-07 01:01:25 -0800530 inbuf = kzalloc(taskin, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 if (inbuf == NULL) {
532 err = -ENOMEM;
533 goto abort;
534 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 if (copy_from_user(inbuf, buf + intotal, taskin)) {
536 err = -EFAULT;
537 goto abort;
538 }
539 }
540
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100541 memset(&cmd, 0, sizeof(cmd));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100543 memcpy(&cmd.tf_array[0], req_task->hob_ports,
544 HDIO_DRIVE_HOB_HDR_SIZE - 2);
545 memcpy(&cmd.tf_array[6], req_task->io_ports,
546 HDIO_DRIVE_TASK_HDR_SIZE);
Bartlomiej Zolnierkiewicz866e2ec2008-01-25 22:17:14 +0100547
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100548 cmd.data_phase = req_task->data_phase;
549 cmd.tf_flags = IDE_TFLAG_IO_16BIT | IDE_TFLAG_DEVICE |
550 IDE_TFLAG_IN_TF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200552 if (drive->dev_flags & IDE_DFLAG_LBA48)
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100553 cmd.tf_flags |= (IDE_TFLAG_LBA48 | IDE_TFLAG_IN_HOB);
Bartlomiej Zolnierkiewicza3bbb9d2008-01-25 22:17:10 +0100554
Bartlomiej Zolnierkiewicz74095a92008-01-25 22:17:07 +0100555 if (req_task->out_flags.all) {
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100556 cmd.ftf_flags |= IDE_FTFLAG_FLAGGED;
Bartlomiej Zolnierkiewicz74095a92008-01-25 22:17:07 +0100557
558 if (req_task->out_flags.b.data)
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100559 cmd.ftf_flags |= IDE_FTFLAG_OUT_DATA;
Bartlomiej Zolnierkiewicz74095a92008-01-25 22:17:07 +0100560
561 if (req_task->out_flags.b.nsector_hob)
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100562 cmd.tf_flags |= IDE_TFLAG_OUT_HOB_NSECT;
Bartlomiej Zolnierkiewicz74095a92008-01-25 22:17:07 +0100563 if (req_task->out_flags.b.sector_hob)
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100564 cmd.tf_flags |= IDE_TFLAG_OUT_HOB_LBAL;
Bartlomiej Zolnierkiewicz74095a92008-01-25 22:17:07 +0100565 if (req_task->out_flags.b.lcyl_hob)
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100566 cmd.tf_flags |= IDE_TFLAG_OUT_HOB_LBAM;
Bartlomiej Zolnierkiewicz74095a92008-01-25 22:17:07 +0100567 if (req_task->out_flags.b.hcyl_hob)
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100568 cmd.tf_flags |= IDE_TFLAG_OUT_HOB_LBAH;
Bartlomiej Zolnierkiewicz74095a92008-01-25 22:17:07 +0100569
570 if (req_task->out_flags.b.error_feature)
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100571 cmd.tf_flags |= IDE_TFLAG_OUT_FEATURE;
Bartlomiej Zolnierkiewicz74095a92008-01-25 22:17:07 +0100572 if (req_task->out_flags.b.nsector)
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100573 cmd.tf_flags |= IDE_TFLAG_OUT_NSECT;
Bartlomiej Zolnierkiewicz74095a92008-01-25 22:17:07 +0100574 if (req_task->out_flags.b.sector)
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100575 cmd.tf_flags |= IDE_TFLAG_OUT_LBAL;
Bartlomiej Zolnierkiewicz74095a92008-01-25 22:17:07 +0100576 if (req_task->out_flags.b.lcyl)
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100577 cmd.tf_flags |= IDE_TFLAG_OUT_LBAM;
Bartlomiej Zolnierkiewicz74095a92008-01-25 22:17:07 +0100578 if (req_task->out_flags.b.hcyl)
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100579 cmd.tf_flags |= IDE_TFLAG_OUT_LBAH;
Bartlomiej Zolnierkiewicza3bbb9d2008-01-25 22:17:10 +0100580 } else {
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100581 cmd.tf_flags |= IDE_TFLAG_OUT_TF;
582 if (cmd.tf_flags & IDE_TFLAG_LBA48)
583 cmd.tf_flags |= IDE_TFLAG_OUT_HOB;
Bartlomiej Zolnierkiewicz74095a92008-01-25 22:17:07 +0100584 }
585
Bartlomiej Zolnierkiewicz866e2ec2008-01-25 22:17:14 +0100586 if (req_task->in_flags.b.data)
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100587 cmd.ftf_flags |= IDE_FTFLAG_IN_DATA;
Bartlomiej Zolnierkiewicz866e2ec2008-01-25 22:17:14 +0100588
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 switch(req_task->data_phase) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 case TASKFILE_MULTI_OUT:
591 if (!drive->mult_count) {
592 /* (hs): give up if multcount is not set */
593 printk(KERN_ERR "%s: %s Multimode Write " \
594 "multcount is not set\n",
Harvey Harrisoneb639632008-04-26 22:25:20 +0200595 drive->name, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 err = -EPERM;
597 goto abort;
598 }
599 /* fall through */
600 case TASKFILE_OUT:
Bartlomiej Zolnierkiewiczac026ff2008-01-25 22:17:14 +0100601 /* fall through */
602 case TASKFILE_OUT_DMAQ:
603 case TASKFILE_OUT_DMA:
604 nsect = taskout / SECTOR_SIZE;
605 data_buf = outbuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 break;
607 case TASKFILE_MULTI_IN:
608 if (!drive->mult_count) {
609 /* (hs): give up if multcount is not set */
610 printk(KERN_ERR "%s: %s Multimode Read failure " \
611 "multcount is not set\n",
Harvey Harrisoneb639632008-04-26 22:25:20 +0200612 drive->name, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 err = -EPERM;
614 goto abort;
615 }
616 /* fall through */
617 case TASKFILE_IN:
Bartlomiej Zolnierkiewiczac026ff2008-01-25 22:17:14 +0100618 /* fall through */
619 case TASKFILE_IN_DMAQ:
620 case TASKFILE_IN_DMA:
621 nsect = taskin / SECTOR_SIZE;
622 data_buf = inbuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 break;
624 case TASKFILE_NO_DATA:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 break;
626 default:
627 err = -EFAULT;
628 goto abort;
629 }
630
Bartlomiej Zolnierkiewiczac026ff2008-01-25 22:17:14 +0100631 if (req_task->req_cmd == IDE_DRIVE_TASK_NO_DATA)
632 nsect = 0;
633 else if (!nsect) {
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100634 nsect = (cmd.tf.hob_nsect << 8) | cmd.tf.nsect;
Bartlomiej Zolnierkiewiczac026ff2008-01-25 22:17:14 +0100635
636 if (!nsect) {
637 printk(KERN_ERR "%s: in/out command without data\n",
638 drive->name);
639 err = -EFAULT;
640 goto abort;
641 }
642 }
643
644 if (req_task->req_cmd == IDE_DRIVE_TASK_RAW_WRITE)
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100645 cmd.tf_flags |= IDE_TFLAG_WRITE;
Bartlomiej Zolnierkiewiczac026ff2008-01-25 22:17:14 +0100646
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100647 err = ide_raw_taskfile(drive, &cmd, data_buf, nsect);
Bartlomiej Zolnierkiewiczac026ff2008-01-25 22:17:14 +0100648
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100649 memcpy(req_task->hob_ports, &cmd.tf_array[0],
650 HDIO_DRIVE_HOB_HDR_SIZE - 2);
651 memcpy(req_task->io_ports, &cmd.tf_array[6],
652 HDIO_DRIVE_TASK_HDR_SIZE);
Bartlomiej Zolnierkiewicz866e2ec2008-01-25 22:17:14 +0100653
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100654 if ((cmd.ftf_flags & IDE_FTFLAG_SET_IN_FLAGS) &&
Bartlomiej Zolnierkiewicz866e2ec2008-01-25 22:17:14 +0100655 req_task->in_flags.all == 0) {
656 req_task->in_flags.all = IDE_TASKFILE_STD_IN_FLAGS;
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200657 if (drive->dev_flags & IDE_DFLAG_LBA48)
Bartlomiej Zolnierkiewicz866e2ec2008-01-25 22:17:14 +0100658 req_task->in_flags.all |= (IDE_HOB_STD_IN_FLAGS << 8);
659 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660
661 if (copy_to_user(buf, req_task, tasksize)) {
662 err = -EFAULT;
663 goto abort;
664 }
665 if (taskout) {
666 int outtotal = tasksize;
667 if (copy_to_user(buf + outtotal, outbuf, taskout)) {
668 err = -EFAULT;
669 goto abort;
670 }
671 }
672 if (taskin) {
673 int intotal = tasksize + taskout;
674 if (copy_to_user(buf + intotal, inbuf, taskin)) {
675 err = -EFAULT;
676 goto abort;
677 }
678 }
679abort:
680 kfree(req_task);
Jesper Juhl6044ec82005-11-07 01:01:32 -0800681 kfree(outbuf);
682 kfree(inbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683
684// printk("IDE Taskfile ioctl ended. rc = %i\n", err);
685
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 return err;
687}
Bartlomiej Zolnierkiewicz26a5b042007-11-05 21:42:27 +0100688#endif