blob: aab6a10435b65203e8550d556f6401f681ae07fd [file] [log] [blame]
Thomas Gleixner457c8992019-05-19 13:08:55 +01001// SPDX-License-Identifier: GPL-2.0-only
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
Bartlomiej Zolnierkiewicz59bca8c2008-02-01 23:09:33 +01003 * Copyright (C) 2000-2002 Michael Cornwell <cornwell@acm.org>
4 * Copyright (C) 2000-2002 Andre Hedrick <andre@linux-ide.org>
5 * Copyright (C) 2001-2002 Klaus Smolin
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 * IBM Storage Technology Division
Bartlomiej Zolnierkiewicz59bca8c2008-02-01 23:09:33 +01007 * Copyright (C) 2003-2004, 2007 Bartlomiej Zolnierkiewicz
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 *
9 * The big the bad and the ugly.
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 */
11
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/types.h>
13#include <linux/string.h>
14#include <linux/kernel.h>
Paul Gortmaker38789fd2011-07-17 15:33:58 -040015#include <linux/export.h>
Andrew Morton651c29a2006-02-15 15:17:37 -080016#include <linux/sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/interrupt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/errno.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/delay.h>
21#include <linux/hdreg.h>
22#include <linux/ide.h>
Ingo Molnar38b8d202017-02-08 18:51:31 +010023#include <linux/nmi.h>
Jens Axboe55c16a72007-07-25 08:13:56 +020024#include <linux/scatterlist.h>
Jaswinder Singh Rajput2d5abce2009-08-02 20:17:34 -070025#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <asm/io.h>
28
Sergei Shtylyov3153c262009-04-08 14:13:03 +020029void ide_tf_readback(ide_drive_t *drive, struct ide_cmd *cmd)
30{
31 ide_hwif_t *hwif = drive->hwif;
32 const struct ide_tp_ops *tp_ops = hwif->tp_ops;
33
34 /* Be sure we're looking at the low order bytes */
35 tp_ops->write_devctl(hwif, ATA_DEVCTL_OBS);
36
37 tp_ops->tf_read(drive, &cmd->tf, cmd->valid.in.tf);
38
39 if (cmd->tf_flags & IDE_TFLAG_LBA48) {
40 tp_ops->write_devctl(hwif, ATA_HOB | ATA_DEVCTL_OBS);
41
42 tp_ops->tf_read(drive, &cmd->hob, cmd->valid.in.hob);
43 }
44}
45
Sergei Shtylyov745483f2009-04-08 14:13:02 +020046void ide_tf_dump(const char *s, struct ide_cmd *cmd)
Bartlomiej Zolnierkiewicz089c5c72008-04-28 23:44:39 +020047{
48#ifdef DEBUG
49 printk("%s: tf: feat 0x%02x nsect 0x%02x lbal 0x%02x "
50 "lbam 0x%02x lbah 0x%02x dev 0x%02x cmd 0x%02x\n",
Sergei Shtylyov745483f2009-04-08 14:13:02 +020051 s, cmd->tf.feature, cmd->tf.nsect,
52 cmd->tf.lbal, cmd->tf.lbam, cmd->tf.lbah,
53 cmd->tf.device, cmd->tf.command);
54 printk("%s: hob: nsect 0x%02x lbal 0x%02x lbam 0x%02x lbah 0x%02x\n",
55 s, cmd->hob.nsect, cmd->hob.lbal, cmd->hob.lbam, cmd->hob.lbah);
Bartlomiej Zolnierkiewicz089c5c72008-04-28 23:44:39 +020056#endif
57}
58
Jaswinder Singh Rajput2d5abce2009-08-02 20:17:34 -070059int taskfile_lib_get_identify(ide_drive_t *drive, u8 *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070060{
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +010061 struct ide_cmd cmd;
Bartlomiej Zolnierkiewicz650d8412008-01-25 22:17:06 +010062
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +010063 memset(&cmd, 0, sizeof(cmd));
64 cmd.tf.nsect = 0x01;
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 if (drive->media == ide_disk)
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +010066 cmd.tf.command = ATA_CMD_ID_ATA;
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 else
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +010068 cmd.tf.command = ATA_CMD_ID_ATAPI;
Sergei Shtylyov60f85012009-04-08 14:13:01 +020069 cmd.valid.out.tf = IDE_VALID_OUT_TF | IDE_VALID_DEVICE;
70 cmd.valid.in.tf = IDE_VALID_IN_TF | IDE_VALID_DEVICE;
Bartlomiej Zolnierkiewicz0dfb9912009-03-27 12:46:39 +010071 cmd.protocol = ATA_PROT_PIO;
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +010072
73 return ide_raw_taskfile(drive, &cmd, buf, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070074}
75
Bartlomiej Zolnierkiewicz1192e522008-01-25 22:17:16 +010076static ide_startstop_t task_no_data_intr(ide_drive_t *);
Bartlomiej Zolnierkiewiczadb1af92009-03-27 12:46:38 +010077static ide_startstop_t pre_task_out_intr(ide_drive_t *, struct ide_cmd *);
Bartlomiej Zolnierkiewicz901bd08a2009-03-27 12:46:39 +010078static ide_startstop_t task_pio_intr(ide_drive_t *);
Bartlomiej Zolnierkiewicz1192e522008-01-25 22:17:16 +010079
Bartlomiej Zolnierkiewiczadb1af92009-03-27 12:46:38 +010080ide_startstop_t do_rw_taskfile(ide_drive_t *drive, struct ide_cmd *orig_cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -070081{
Bartlomiej Zolnierkiewicz898ec222009-01-06 17:20:52 +010082 ide_hwif_t *hwif = drive->hwif;
Bartlomiej Zolnierkiewiczadb1af92009-03-27 12:46:38 +010083 struct ide_cmd *cmd = &hwif->cmd;
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +010084 struct ide_taskfile *tf = &cmd->tf;
Bartlomiej Zolnierkiewicz57d73662008-01-25 22:17:16 +010085 ide_handler_t *handler = NULL;
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +020086 const struct ide_tp_ops *tp_ops = hwif->tp_ops;
Bartlomiej Zolnierkiewiczf37afda2008-04-26 22:25:24 +020087 const struct ide_dma_ops *dma_ops = hwif->dma_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
Bartlomiej Zolnierkiewicz0dfb9912009-03-27 12:46:39 +010089 if (orig_cmd->protocol == ATA_PROT_PIO &&
90 (orig_cmd->tf_flags & IDE_TFLAG_MULTI_PIO) &&
91 drive->mult_count == 0) {
Jaswinder Singh Rajput2d5abce2009-08-02 20:17:34 -070092 pr_err("%s: multimode not set!\n", drive->name);
Bartlomiej Zolnierkiewicz0dfb9912009-03-27 12:46:39 +010093 return ide_stopped;
Bartlomiej Zolnierkiewicz1edee602008-01-25 22:17:15 +010094 }
95
Bartlomiej Zolnierkiewiczadb1af92009-03-27 12:46:38 +010096 if (orig_cmd->ftf_flags & IDE_FTFLAG_FLAGGED)
97 orig_cmd->ftf_flags |= IDE_FTFLAG_SET_IN_FLAGS;
Bartlomiej Zolnierkiewicz1edee602008-01-25 22:17:15 +010098
Bartlomiej Zolnierkiewiczadb1af92009-03-27 12:46:38 +010099 memcpy(cmd, orig_cmd, sizeof(*cmd));
Bartlomiej Zolnierkiewiczd6ff9f62008-10-13 21:39:41 +0200100
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100101 if ((cmd->tf_flags & IDE_TFLAG_DMA_PIO_FALLBACK) == 0) {
Sergei Shtylyov745483f2009-04-08 14:13:02 +0200102 ide_tf_dump(drive->name, cmd);
Sergei Shtylyovecf3a312009-03-31 20:15:30 +0200103 tp_ops->write_devctl(hwif, ATA_DEVCTL_OBS);
Sergei Shtylyov35218d12009-03-31 20:15:31 +0200104
105 if (cmd->ftf_flags & IDE_FTFLAG_OUT_DATA) {
Sergei Shtylyov745483f2009-04-08 14:13:02 +0200106 u8 data[2] = { cmd->tf.data, cmd->hob.data };
Sergei Shtylyov35218d12009-03-31 20:15:31 +0200107
108 tp_ops->output_data(drive, cmd, data, 2);
109 }
Sergei Shtylyov4109d192009-04-08 14:13:02 +0200110
111 if (cmd->valid.out.tf & IDE_VALID_DEVICE) {
112 u8 HIHI = (cmd->tf_flags & IDE_TFLAG_LBA48) ?
113 0xE0 : 0xEF;
114
115 if (!(cmd->ftf_flags & IDE_FTFLAG_FLAGGED))
116 cmd->tf.device &= HIHI;
117 cmd->tf.device |= drive->select;
118 }
119
Sergei Shtylyovc9ff9e72009-04-08 14:13:03 +0200120 tp_ops->tf_load(drive, &cmd->hob, cmd->valid.out.hob);
121 tp_ops->tf_load(drive, &cmd->tf, cmd->valid.out.tf);
Bartlomiej Zolnierkiewicz089c5c72008-04-28 23:44:39 +0200122 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
Bartlomiej Zolnierkiewicz0dfb9912009-03-27 12:46:39 +0100124 switch (cmd->protocol) {
125 case ATA_PROT_PIO:
126 if (cmd->tf_flags & IDE_TFLAG_WRITE) {
127 tp_ops->exec_command(hwif, tf->command);
128 ndelay(400); /* FIXME */
129 return pre_task_out_intr(drive, cmd);
130 }
Bartlomiej Zolnierkiewicz901bd08a2009-03-27 12:46:39 +0100131 handler = task_pio_intr;
Gustavo A. R. Silvaa2eed332018-07-03 14:23:05 -0500132 /* fall through */
Bartlomiej Zolnierkiewicz0dfb9912009-03-27 12:46:39 +0100133 case ATA_PROT_NODATA:
Bartlomiej Zolnierkiewicz57d73662008-01-25 22:17:16 +0100134 if (handler == NULL)
135 handler = task_no_data_intr;
Bartlomiej Zolnierkiewicz35b5d0b2009-03-27 12:46:47 +0100136 ide_execute_command(drive, cmd, handler, WAIT_WORSTCASE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 return ide_started;
Bartlomiej Zolnierkiewiczb788ee92009-03-27 12:46:46 +0100138 case ATA_PROT_DMA:
Bartlomiej Zolnierkiewicz5ae54122009-03-31 20:15:20 +0200139 if (ide_dma_prepare(drive, cmd))
Bartlomiej Zolnierkiewicz10d90152008-01-25 22:17:16 +0100140 return ide_stopped;
Bartlomiej Zolnierkiewicz22117d62009-03-27 12:46:47 +0100141 hwif->expiry = dma_ops->dma_timer_expiry;
Bartlomiej Zolnierkiewicz35b5d0b2009-03-27 12:46:47 +0100142 ide_execute_command(drive, cmd, ide_dma_intr, 2 * WAIT_CMD);
Bartlomiej Zolnierkiewicz5e37bdc2008-04-26 22:25:24 +0200143 dma_ops->dma_start(drive);
Gustavo A. R. Silvaa2eed332018-07-03 14:23:05 -0500144 /* fall through */
Bartlomiej Zolnierkiewiczb788ee92009-03-27 12:46:46 +0100145 default:
Bartlomiej Zolnierkiewicz74095a92008-01-25 22:17:07 +0100146 return ide_started;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148}
Bartlomiej Zolnierkiewiczf6e29e32008-01-25 22:17:16 +0100149EXPORT_SYMBOL_GPL(do_rw_taskfile);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
Bartlomiej Zolnierkiewiczd6ff9f62008-10-13 21:39:41 +0200151static ide_startstop_t task_no_data_intr(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152{
Bartlomiej Zolnierkiewiczb73c7ee2008-07-23 19:55:52 +0200153 ide_hwif_t *hwif = drive->hwif;
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100154 struct ide_cmd *cmd = &hwif->cmd;
155 struct ide_taskfile *tf = &cmd->tf;
156 int custom = (cmd->tf_flags & IDE_TFLAG_CUSTOM_HANDLER) ? 1 : 0;
Bartlomiej Zolnierkiewiczd6ff9f62008-10-13 21:39:41 +0200157 int retries = (custom && tf->command == ATA_CMD_INIT_DEV_PARAMS) ? 5 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 u8 stat;
159
Bartlomiej Zolnierkiewicz90d2c6b2008-07-24 22:53:36 +0200160 local_irq_enable_in_hardirq();
161
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200162 while (1) {
163 stat = hwif->tp_ops->read_status(hwif);
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +0200164 if ((stat & ATA_BUSY) == 0 || retries-- == 0)
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200165 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 udelay(10);
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200167 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168
Bartlomiej Zolnierkiewiczd6ff9f62008-10-13 21:39:41 +0200169 if (!OK_STAT(stat, ATA_DRDY, BAD_STAT)) {
170 if (custom && tf->command == ATA_CMD_SET_MULTI) {
171 drive->mult_req = drive->mult_count = 0;
Bartlomiej Zolnierkiewiczca1b96e2009-05-17 19:12:21 +0200172 drive->special_flags |= IDE_SFLAG_RECALIBRATE;
Bartlomiej Zolnierkiewiczd6ff9f62008-10-13 21:39:41 +0200173 (void)ide_dump_status(drive, __func__, stat);
174 return ide_stopped;
175 } else if (custom && tf->command == ATA_CMD_INIT_DEV_PARAMS) {
176 if ((stat & (ATA_ERR | ATA_DRQ)) == 0) {
177 ide_set_handler(drive, &task_no_data_intr,
Bartlomiej Zolnierkiewicz60c0cd02009-03-27 12:46:46 +0100178 WAIT_WORSTCASE);
Bartlomiej Zolnierkiewiczd6ff9f62008-10-13 21:39:41 +0200179 return ide_started;
180 }
181 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 return ide_error(drive, "task_no_data_intr", stat);
Bartlomiej Zolnierkiewiczd6ff9f62008-10-13 21:39:41 +0200183 }
Bartlomiej Zolnierkiewiczc47137a2008-02-06 02:57:51 +0100184
Bartlomiej Zolnierkiewicze7fedc32009-03-27 12:46:41 +0100185 if (custom && tf->command == ATA_CMD_SET_MULTI)
Bartlomiej Zolnierkiewiczd6ff9f62008-10-13 21:39:41 +0200186 drive->mult_count = drive->mult_req;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
Bartlomiej Zolnierkiewiczd364c7f2009-03-27 12:46:42 +0100188 if (custom == 0 || tf->command == ATA_CMD_IDLEIMMEDIATE ||
189 tf->command == ATA_CMD_CHK_POWER) {
Bartlomiej Zolnierkiewicza09485d2009-03-27 12:46:31 +0100190 struct request *rq = hwif->rq;
Bartlomiej Zolnierkiewicza09485d2009-03-27 12:46:31 +0100191
Christoph Hellwiga7928c12015-04-17 22:37:20 +0200192 if (ata_pm_request(rq))
Bartlomiej Zolnierkiewicza09485d2009-03-27 12:46:31 +0100193 ide_complete_pm_rq(drive, rq);
Bartlomiej Zolnierkiewicz2230d90d2009-03-27 12:46:42 +0100194 else
195 ide_finish_cmd(drive, cmd, stat);
Bartlomiej Zolnierkiewicza09485d2009-03-27 12:46:31 +0100196 }
197
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 return ide_stopped;
199}
200
Adrian Bunkda6f4c72008-02-01 23:09:16 +0100201static u8 wait_drive_not_busy(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202{
Bartlomiej Zolnierkiewiczb73c7ee2008-07-23 19:55:52 +0200203 ide_hwif_t *hwif = drive->hwif;
Masatake YAMATOb42fa132007-07-03 22:28:34 +0200204 int retries;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 u8 stat;
206
207 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300208 * Last sector was transferred, wait until device is ready. This can
Bartlomiej Zolnierkiewiczf54feaf2008-06-20 20:53:33 +0200209 * take up to 6 ms on some ATAPI devices, so we will wait max 10 ms.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 */
Bartlomiej Zolnierkiewiczf54feaf2008-06-20 20:53:33 +0200211 for (retries = 0; retries < 1000; retries++) {
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200212 stat = hwif->tp_ops->read_status(hwif);
Bartlomiej Zolnierkiewiczc47137a2008-02-06 02:57:51 +0100213
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +0200214 if (stat & ATA_BUSY)
Masatake YAMATOb42fa132007-07-03 22:28:34 +0200215 udelay(10);
216 else
217 break;
218 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +0200220 if (stat & ATA_BUSY)
Jaswinder Singh Rajput2d5abce2009-08-02 20:17:34 -0700221 pr_err("%s: drive still BUSY!\n", drive->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222
223 return stat;
224}
225
Bartlomiej Zolnierkiewicza08915b2009-03-31 20:15:13 +0200226void ide_pio_bytes(ide_drive_t *drive, struct ide_cmd *cmd,
227 unsigned int write, unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228{
229 ide_hwif_t *hwif = drive->hwif;
230 struct scatterlist *sg = hwif->sg_table;
Bartlomiej Zolnierkiewiczb6308ee2009-03-27 12:46:38 +0100231 struct scatterlist *cursg = cmd->cursg;
David S. Miller7fa350b2009-06-23 04:16:04 -0700232 unsigned long uninitialized_var(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 unsigned int offset;
235 u8 *buf;
236
Bartlomiej Zolnierkiewicz7a007982009-03-31 20:14:59 +0200237 if (cursg == NULL)
238 cursg = cmd->cursg = sg;
Jens Axboe55c16a72007-07-25 08:13:56 +0200239
Bartlomiej Zolnierkiewicz7a007982009-03-31 20:14:59 +0200240 while (len) {
241 unsigned nr_bytes = min(len, cursg->length - cmd->cursg_ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242
Bartlomiej Zolnierkiewicz7a007982009-03-31 20:14:59 +0200243 page = sg_page(cursg);
244 offset = cursg->offset + cmd->cursg_ofs;
245
246 /* get the current page and offset */
247 page = nth_page(page, (offset >> PAGE_SHIFT));
248 offset %= PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249
Steven J. Hill15812082013-06-20 10:30:37 -0500250 nr_bytes = min_t(unsigned, nr_bytes, (PAGE_SIZE - offset));
251
Cong Wang45b408d2011-11-25 23:14:20 +0800252 buf = kmap_atomic(page) + offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253
Bartlomiej Zolnierkiewicz7a007982009-03-31 20:14:59 +0200254 cmd->nleft -= nr_bytes;
255 cmd->cursg_ofs += nr_bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
Bartlomiej Zolnierkiewicz7a007982009-03-31 20:14:59 +0200257 if (cmd->cursg_ofs == cursg->length) {
258 cursg = cmd->cursg = sg_next(cmd->cursg);
259 cmd->cursg_ofs = 0;
260 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261
Bartlomiej Zolnierkiewicz7a007982009-03-31 20:14:59 +0200262 /* do the actual data transfer */
263 if (write)
264 hwif->tp_ops->output_data(drive, cmd, buf, nr_bytes);
265 else
266 hwif->tp_ops->input_data(drive, cmd, buf, nr_bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
Cong Wang45b408d2011-11-25 23:14:20 +0800268 kunmap_atomic(buf);
Bartlomiej Zolnierkiewiczf2bc3162009-03-31 20:14:59 +0200269
Bartlomiej Zolnierkiewicz7a007982009-03-31 20:14:59 +0200270 len -= nr_bytes;
271 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272}
Bartlomiej Zolnierkiewicza08915b2009-03-31 20:15:13 +0200273EXPORT_SYMBOL_GPL(ide_pio_bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274
Bartlomiej Zolnierkiewiczadb1af92009-03-27 12:46:38 +0100275static void ide_pio_datablock(ide_drive_t *drive, struct ide_cmd *cmd,
276 unsigned int write)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277{
Bartlomiej Zolnierkiewicz7a007982009-03-31 20:14:59 +0200278 unsigned int nr_bytes;
279
Tejun Heo35cf2b92008-01-26 20:13:10 +0100280 u8 saved_io_32bit = drive->io_32bit;
281
Bartlomiej Zolnierkiewiczadb1af92009-03-27 12:46:38 +0100282 if (cmd->tf_flags & IDE_TFLAG_FS)
Christoph Hellwig17d53632017-04-20 16:03:01 +0200283 scsi_req(cmd->rq)->result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100285 if (cmd->tf_flags & IDE_TFLAG_IO_16BIT)
Bartlomiej Zolnierkiewicze3d9a732009-03-27 12:46:32 +0100286 drive->io_32bit = 0;
Tejun Heo35cf2b92008-01-26 20:13:10 +0100287
Andrew Morton651c29a2006-02-15 15:17:37 -0800288 touch_softlockup_watchdog();
289
Bartlomiej Zolnierkiewicz0dfb9912009-03-27 12:46:39 +0100290 if (cmd->tf_flags & IDE_TFLAG_MULTI_PIO)
Bartlomiej Zolnierkiewicz7a007982009-03-31 20:14:59 +0200291 nr_bytes = min_t(unsigned, cmd->nleft, drive->mult_count << 9);
Bartlomiej Zolnierkiewicz0dfb9912009-03-27 12:46:39 +0100292 else
Bartlomiej Zolnierkiewicz7a007982009-03-31 20:14:59 +0200293 nr_bytes = SECTOR_SIZE;
294
295 ide_pio_bytes(drive, cmd, write, nr_bytes);
Tejun Heo35cf2b92008-01-26 20:13:10 +0100296
297 drive->io_32bit = saved_io_32bit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298}
299
Bartlomiej Zolnierkiewicz041cea12009-03-27 12:46:41 +0100300static void ide_error_cmd(ide_drive_t *drive, struct ide_cmd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301{
Bartlomiej Zolnierkiewiczadb1af92009-03-27 12:46:38 +0100302 if (cmd->tf_flags & IDE_TFLAG_FS) {
Bartlomiej Zolnierkiewiczbf717c02009-03-27 12:46:47 +0100303 int nr_bytes = cmd->nbytes - cmd->nleft;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304
Bartlomiej Zolnierkiewicz0dfb9912009-03-27 12:46:39 +0100305 if (cmd->protocol == ATA_PROT_PIO &&
306 ((cmd->tf_flags & IDE_TFLAG_WRITE) || cmd->nleft == 0)) {
307 if (cmd->tf_flags & IDE_TFLAG_MULTI_PIO)
Bartlomiej Zolnierkiewiczbf717c02009-03-27 12:46:47 +0100308 nr_bytes -= drive->mult_count << 9;
Bartlomiej Zolnierkiewicz0dfb9912009-03-27 12:46:39 +0100309 else
Bartlomiej Zolnierkiewiczbf717c02009-03-27 12:46:47 +0100310 nr_bytes -= SECTOR_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 }
312
Bartlomiej Zolnierkiewiczbf717c02009-03-27 12:46:47 +0100313 if (nr_bytes > 0)
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200314 ide_complete_rq(drive, BLK_STS_OK, nr_bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316}
317
Bartlomiej Zolnierkiewiczadb1af92009-03-27 12:46:38 +0100318void ide_finish_cmd(ide_drive_t *drive, struct ide_cmd *cmd, u8 stat)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319{
Bartlomiej Zolnierkiewicz6902a532009-03-27 12:46:43 +0100320 struct request *rq = drive->hwif->rq;
Bartlomiej Zolnierkiewicz665d66e2009-06-23 11:35:51 +0000321 u8 err = ide_read_error(drive), nsect = cmd->tf.nsect;
322 u8 set_xfer = !!(cmd->tf_flags & IDE_TFLAG_SET_XFER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
Bartlomiej Zolnierkiewicz2230d90d2009-03-27 12:46:42 +0100324 ide_complete_cmd(drive, cmd, stat, err);
Christoph Hellwig17d53632017-04-20 16:03:01 +0200325 scsi_req(rq)->result = err;
Bartlomiej Zolnierkiewicz665d66e2009-06-23 11:35:51 +0000326
327 if (err == 0 && set_xfer) {
328 ide_set_xfer_rate(drive, nsect);
329 ide_driveid_update(drive);
330 }
331
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200332 ide_complete_rq(drive, err ? BLK_STS_IOERR : BLK_STS_OK, blk_rq_bytes(rq));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333}
334
335/*
Bartlomiej Zolnierkiewicz901bd08a2009-03-27 12:46:39 +0100336 * Handler for command with PIO data phase.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 */
Bartlomiej Zolnierkiewicz901bd08a2009-03-27 12:46:39 +0100338static ide_startstop_t task_pio_intr(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339{
340 ide_hwif_t *hwif = drive->hwif;
Bartlomiej Zolnierkiewiczadb1af92009-03-27 12:46:38 +0100341 struct ide_cmd *cmd = &drive->hwif->cmd;
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200342 u8 stat = hwif->tp_ops->read_status(hwif);
Bartlomiej Zolnierkiewicz901bd08a2009-03-27 12:46:39 +0100343 u8 write = !!(cmd->tf_flags & IDE_TFLAG_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344
Bartlomiej Zolnierkiewicz901bd08a2009-03-27 12:46:39 +0100345 if (write == 0) {
346 /* Error? */
347 if (stat & ATA_ERR)
Bartlomiej Zolnierkiewicz0a1248c2009-03-27 12:46:41 +0100348 goto out_err;
Linus Torvalds6c3c3152008-03-18 21:26:24 -0700349
Bartlomiej Zolnierkiewicz901bd08a2009-03-27 12:46:39 +0100350 /* Didn't want any data? Odd. */
Bartlomiej Zolnierkiewicz151055e2009-03-27 12:46:39 +0100351 if ((stat & ATA_DRQ) == 0) {
352 /* Command all done? */
Bartlomiej Zolnierkiewicz0a1248c2009-03-27 12:46:41 +0100353 if (OK_STAT(stat, ATA_DRDY, ATA_BUSY))
354 goto out_end;
Bartlomiej Zolnierkiewicz151055e2009-03-27 12:46:39 +0100355
356 /* Assume it was a spurious irq */
Bartlomiej Zolnierkiewicz0a1248c2009-03-27 12:46:41 +0100357 goto out_wait;
Bartlomiej Zolnierkiewicz151055e2009-03-27 12:46:39 +0100358 }
Bartlomiej Zolnierkiewicz901bd08a2009-03-27 12:46:39 +0100359 } else {
360 if (!OK_STAT(stat, DRIVE_READY, drive->bad_wstat))
Bartlomiej Zolnierkiewicz0a1248c2009-03-27 12:46:41 +0100361 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362
Bartlomiej Zolnierkiewicz901bd08a2009-03-27 12:46:39 +0100363 /* Deal with unexpected ATA data phase. */
364 if (((stat & ATA_DRQ) == 0) ^ (cmd->nleft == 0))
Bartlomiej Zolnierkiewicz0a1248c2009-03-27 12:46:41 +0100365 goto out_err;
Bartlomiej Zolnierkiewicz901bd08a2009-03-27 12:46:39 +0100366 }
367
Bartlomiej Zolnierkiewicz0a1248c2009-03-27 12:46:41 +0100368 if (write && cmd->nleft == 0)
369 goto out_end;
Bartlomiej Zolnierkiewicz901bd08a2009-03-27 12:46:39 +0100370
371 /* Still data left to transfer. */
372 ide_pio_datablock(drive, cmd, write);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
Linus Torvalds6c3c3152008-03-18 21:26:24 -0700374 /* Are we done? Check status and finish transfer. */
Bartlomiej Zolnierkiewicz901bd08a2009-03-27 12:46:39 +0100375 if (write == 0 && cmd->nleft == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 stat = wait_drive_not_busy(drive);
Bartlomiej Zolnierkiewicz73d7de02008-01-26 20:13:10 +0100377 if (!OK_STAT(stat, 0, BAD_STAT))
Bartlomiej Zolnierkiewicz0a1248c2009-03-27 12:46:41 +0100378 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379
Bartlomiej Zolnierkiewicz0a1248c2009-03-27 12:46:41 +0100380 goto out_end;
381 }
382out_wait:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 /* Still data left to transfer. */
Bartlomiej Zolnierkiewicz60c0cd02009-03-27 12:46:46 +0100384 ide_set_handler(drive, &task_pio_intr, WAIT_WORSTCASE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 return ide_started;
Bartlomiej Zolnierkiewicz0a1248c2009-03-27 12:46:41 +0100386out_end:
Bartlomiej Zolnierkiewicz2230d90d2009-03-27 12:46:42 +0100387 if ((cmd->tf_flags & IDE_TFLAG_FS) == 0)
388 ide_finish_cmd(drive, cmd, stat);
389 else
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200390 ide_complete_rq(drive, BLK_STS_OK, blk_rq_sectors(cmd->rq) << 9);
Bartlomiej Zolnierkiewicz0a1248c2009-03-27 12:46:41 +0100391 return ide_stopped;
392out_err:
Bartlomiej Zolnierkiewicz041cea12009-03-27 12:46:41 +0100393 ide_error_cmd(drive, cmd);
394 return ide_error(drive, __func__, stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395}
396
Bartlomiej Zolnierkiewiczadb1af92009-03-27 12:46:38 +0100397static ide_startstop_t pre_task_out_intr(ide_drive_t *drive,
398 struct ide_cmd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399{
400 ide_startstop_t startstop;
401
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +0200402 if (ide_wait_stat(&startstop, drive, ATA_DRQ,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 drive->bad_wstat, WAIT_DRQ)) {
Jaswinder Singh Rajput2d5abce2009-08-02 20:17:34 -0700404 pr_err("%s: no DRQ after issuing %sWRITE%s\n", drive->name,
Bartlomiej Zolnierkiewicz0dfb9912009-03-27 12:46:39 +0100405 (cmd->tf_flags & IDE_TFLAG_MULTI_PIO) ? "MULT" : "",
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200406 (drive->dev_flags & IDE_DFLAG_LBA48) ? "_EXT" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 return startstop;
408 }
409
Sebastian Andrzej Siewior47b82e82018-05-04 16:24:46 +0200410 if (!force_irqthreads && (drive->dev_flags & IDE_DFLAG_UNMASK) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 local_irq_disable();
412
Bartlomiej Zolnierkiewicz60c0cd02009-03-27 12:46:46 +0100413 ide_set_handler(drive, &task_pio_intr, WAIT_WORSTCASE);
Bartlomiej Zolnierkiewicz901bd08a2009-03-27 12:46:39 +0100414
Bartlomiej Zolnierkiewiczadb1af92009-03-27 12:46:38 +0100415 ide_pio_datablock(drive, cmd, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416
417 return ide_started;
418}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100420int ide_raw_taskfile(ide_drive_t *drive, struct ide_cmd *cmd, u8 *buf,
421 u16 nsect)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422{
FUJITA Tomonori154ed282008-07-15 21:21:43 +0200423 struct request *rq;
424 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100426 rq = blk_get_request(drive->queue,
427 (cmd->tf_flags & IDE_TFLAG_WRITE) ?
Christoph Hellwigff005a02018-05-09 09:54:05 +0200428 REQ_OP_DRV_OUT : REQ_OP_DRV_IN, 0);
Christoph Hellwig2f5a8e802017-01-31 16:57:30 +0100429 ide_req(rq)->type = ATA_PRIV_TASKFILE;
Tejun Heod868ca22009-04-19 07:00:42 +0900430
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 /*
432 * (ks) We transfer currently only whole sectors.
433 * This is suffient for now. But, it would be great,
434 * if we would find a solution to transfer any size.
435 * To support special commands like READ LONG.
436 */
Tejun Heod868ca22009-04-19 07:00:42 +0900437 if (nsect) {
438 error = blk_rq_map_kern(drive->queue, rq, buf,
Christoph Hellwig0eb0b632018-05-09 09:54:08 +0200439 nsect * SECTOR_SIZE, GFP_NOIO);
Tejun Heod868ca22009-04-19 07:00:42 +0900440 if (error)
441 goto put_req;
442 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443
Christoph Hellwig22ce0a72018-11-10 09:30:49 +0100444 ide_req(rq)->special = cmd;
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100445 cmd->rq = rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446
Christoph Hellwigb7819b92017-04-20 16:02:55 +0200447 blk_execute_rq(drive->queue, NULL, rq, 0);
Christoph Hellwig17d53632017-04-20 16:03:01 +0200448 error = scsi_req(rq)->result ? -EIO : 0;
Tejun Heod868ca22009-04-19 07:00:42 +0900449put_req:
450 blk_put_request(rq);
FUJITA Tomonori154ed282008-07-15 21:21:43 +0200451 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453EXPORT_SYMBOL(ide_raw_taskfile);
454
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100455int ide_no_data_taskfile(ide_drive_t *drive, struct ide_cmd *cmd)
Bartlomiej Zolnierkiewicz9a3c49b2008-01-25 22:17:07 +0100456{
Bartlomiej Zolnierkiewicz0dfb9912009-03-27 12:46:39 +0100457 cmd->protocol = ATA_PROT_NODATA;
Bartlomiej Zolnierkiewicz9a3c49b2008-01-25 22:17:07 +0100458
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100459 return ide_raw_taskfile(drive, cmd, NULL, 0);
Bartlomiej Zolnierkiewicz9a3c49b2008-01-25 22:17:07 +0100460}
461EXPORT_SYMBOL_GPL(ide_no_data_taskfile);
462
Bartlomiej Zolnierkiewicz26a5b042007-11-05 21:42:27 +0100463#ifdef CONFIG_IDE_TASK_IOCTL
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100464int ide_taskfile_ioctl(ide_drive_t *drive, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465{
466 ide_task_request_t *req_task;
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100467 struct ide_cmd cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 u8 *outbuf = NULL;
469 u8 *inbuf = NULL;
Bartlomiej Zolnierkiewiczac026ff2008-01-25 22:17:14 +0100470 u8 *data_buf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 int err = 0;
472 int tasksize = sizeof(struct ide_task_request_s);
Alan Cox3a42bb22006-10-16 16:31:02 +0100473 unsigned int taskin = 0;
474 unsigned int taskout = 0;
Bartlomiej Zolnierkiewiczac026ff2008-01-25 22:17:14 +0100475 u16 nsect = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 char __user *buf = (char __user *)arg;
477
Julia Lawall7d543d82010-06-04 16:11:17 -0700478 req_task = memdup_user(buf, tasksize);
479 if (IS_ERR(req_task))
480 return PTR_ERR(req_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481
Alan Cox3a42bb22006-10-16 16:31:02 +0100482 taskout = req_task->out_size;
483 taskin = req_task->in_size;
Jaswinder Singh Rajput2d5abce2009-08-02 20:17:34 -0700484
Alan Cox3a42bb22006-10-16 16:31:02 +0100485 if (taskin > 65536 || taskout > 65536) {
486 err = -EINVAL;
487 goto abort;
488 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489
490 if (taskout) {
491 int outtotal = tasksize;
Deepak Saxenaf5e3c2f2005-11-07 01:01:25 -0800492 outbuf = kzalloc(taskout, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 if (outbuf == NULL) {
494 err = -ENOMEM;
495 goto abort;
496 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 if (copy_from_user(outbuf, buf + outtotal, taskout)) {
498 err = -EFAULT;
499 goto abort;
500 }
501 }
502
503 if (taskin) {
504 int intotal = tasksize + taskout;
Deepak Saxenaf5e3c2f2005-11-07 01:01:25 -0800505 inbuf = kzalloc(taskin, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 if (inbuf == NULL) {
507 err = -ENOMEM;
508 goto abort;
509 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 if (copy_from_user(inbuf, buf + intotal, taskin)) {
511 err = -EFAULT;
512 goto abort;
513 }
514 }
515
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100516 memset(&cmd, 0, sizeof(cmd));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517
Sergei Shtylyov745483f2009-04-08 14:13:02 +0200518 memcpy(&cmd.hob, req_task->hob_ports, HDIO_DRIVE_HOB_HDR_SIZE - 2);
519 memcpy(&cmd.tf, req_task->io_ports, HDIO_DRIVE_TASK_HDR_SIZE);
Bartlomiej Zolnierkiewicz866e2ec2008-01-25 22:17:14 +0100520
Sergei Shtylyov60f85012009-04-08 14:13:01 +0200521 cmd.valid.out.tf = IDE_VALID_DEVICE;
522 cmd.valid.in.tf = IDE_VALID_DEVICE | IDE_VALID_IN_TF;
523 cmd.tf_flags = IDE_TFLAG_IO_16BIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524
Sergei Shtylyov60f85012009-04-08 14:13:01 +0200525 if (drive->dev_flags & IDE_DFLAG_LBA48) {
526 cmd.tf_flags |= IDE_TFLAG_LBA48;
527 cmd.valid.in.hob = IDE_VALID_IN_HOB;
528 }
Bartlomiej Zolnierkiewicza3bbb9d2008-01-25 22:17:10 +0100529
Bartlomiej Zolnierkiewicz74095a92008-01-25 22:17:07 +0100530 if (req_task->out_flags.all) {
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100531 cmd.ftf_flags |= IDE_FTFLAG_FLAGGED;
Bartlomiej Zolnierkiewicz74095a92008-01-25 22:17:07 +0100532
533 if (req_task->out_flags.b.data)
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100534 cmd.ftf_flags |= IDE_FTFLAG_OUT_DATA;
Bartlomiej Zolnierkiewicz74095a92008-01-25 22:17:07 +0100535
536 if (req_task->out_flags.b.nsector_hob)
Sergei Shtylyov60f85012009-04-08 14:13:01 +0200537 cmd.valid.out.hob |= IDE_VALID_NSECT;
Bartlomiej Zolnierkiewicz74095a92008-01-25 22:17:07 +0100538 if (req_task->out_flags.b.sector_hob)
Sergei Shtylyov60f85012009-04-08 14:13:01 +0200539 cmd.valid.out.hob |= IDE_VALID_LBAL;
Bartlomiej Zolnierkiewicz74095a92008-01-25 22:17:07 +0100540 if (req_task->out_flags.b.lcyl_hob)
Sergei Shtylyov60f85012009-04-08 14:13:01 +0200541 cmd.valid.out.hob |= IDE_VALID_LBAM;
Bartlomiej Zolnierkiewicz74095a92008-01-25 22:17:07 +0100542 if (req_task->out_flags.b.hcyl_hob)
Sergei Shtylyov60f85012009-04-08 14:13:01 +0200543 cmd.valid.out.hob |= IDE_VALID_LBAH;
Bartlomiej Zolnierkiewicz74095a92008-01-25 22:17:07 +0100544
545 if (req_task->out_flags.b.error_feature)
Sergei Shtylyov60f85012009-04-08 14:13:01 +0200546 cmd.valid.out.tf |= IDE_VALID_FEATURE;
Bartlomiej Zolnierkiewicz74095a92008-01-25 22:17:07 +0100547 if (req_task->out_flags.b.nsector)
Sergei Shtylyov60f85012009-04-08 14:13:01 +0200548 cmd.valid.out.tf |= IDE_VALID_NSECT;
Bartlomiej Zolnierkiewicz74095a92008-01-25 22:17:07 +0100549 if (req_task->out_flags.b.sector)
Sergei Shtylyov60f85012009-04-08 14:13:01 +0200550 cmd.valid.out.tf |= IDE_VALID_LBAL;
Bartlomiej Zolnierkiewicz74095a92008-01-25 22:17:07 +0100551 if (req_task->out_flags.b.lcyl)
Sergei Shtylyov60f85012009-04-08 14:13:01 +0200552 cmd.valid.out.tf |= IDE_VALID_LBAM;
Bartlomiej Zolnierkiewicz74095a92008-01-25 22:17:07 +0100553 if (req_task->out_flags.b.hcyl)
Sergei Shtylyov60f85012009-04-08 14:13:01 +0200554 cmd.valid.out.tf |= IDE_VALID_LBAH;
Bartlomiej Zolnierkiewicza3bbb9d2008-01-25 22:17:10 +0100555 } else {
Sergei Shtylyov60f85012009-04-08 14:13:01 +0200556 cmd.valid.out.tf |= IDE_VALID_OUT_TF;
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100557 if (cmd.tf_flags & IDE_TFLAG_LBA48)
Sergei Shtylyov60f85012009-04-08 14:13:01 +0200558 cmd.valid.out.hob |= IDE_VALID_OUT_HOB;
Bartlomiej Zolnierkiewicz74095a92008-01-25 22:17:07 +0100559 }
560
Bartlomiej Zolnierkiewicz866e2ec2008-01-25 22:17:14 +0100561 if (req_task->in_flags.b.data)
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100562 cmd.ftf_flags |= IDE_FTFLAG_IN_DATA;
Bartlomiej Zolnierkiewicz866e2ec2008-01-25 22:17:14 +0100563
Bartlomiej Zolnierkiewicz04d09b02009-03-27 12:46:38 +0100564 if (req_task->req_cmd == IDE_DRIVE_TASK_RAW_WRITE) {
565 /* fixup data phase if needed */
566 if (req_task->data_phase == TASKFILE_IN_DMAQ ||
567 req_task->data_phase == TASKFILE_IN_DMA)
Bartlomiej Zolnierkiewicz0dfb9912009-03-27 12:46:39 +0100568 cmd.tf_flags |= IDE_TFLAG_WRITE;
Bartlomiej Zolnierkiewicz04d09b02009-03-27 12:46:38 +0100569 }
570
Bartlomiej Zolnierkiewicz0dfb9912009-03-27 12:46:39 +0100571 cmd.protocol = ATA_PROT_DMA;
572
573 switch (req_task->data_phase) {
Jaswinder Singh Rajput2d5abce2009-08-02 20:17:34 -0700574 case TASKFILE_MULTI_OUT:
575 if (!drive->mult_count) {
576 /* (hs): give up if multcount is not set */
577 pr_err("%s: %s Multimode Write multcount is not set\n",
578 drive->name, __func__);
579 err = -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 goto abort;
Jaswinder Singh Rajput2d5abce2009-08-02 20:17:34 -0700581 }
582 cmd.tf_flags |= IDE_TFLAG_MULTI_PIO;
583 /* fall through */
584 case TASKFILE_OUT:
585 cmd.protocol = ATA_PROT_PIO;
586 /* fall through */
587 case TASKFILE_OUT_DMAQ:
588 case TASKFILE_OUT_DMA:
589 cmd.tf_flags |= IDE_TFLAG_WRITE;
590 nsect = taskout / SECTOR_SIZE;
591 data_buf = outbuf;
592 break;
593 case TASKFILE_MULTI_IN:
594 if (!drive->mult_count) {
595 /* (hs): give up if multcount is not set */
596 pr_err("%s: %s Multimode Read multcount is not set\n",
597 drive->name, __func__);
598 err = -EPERM;
599 goto abort;
600 }
601 cmd.tf_flags |= IDE_TFLAG_MULTI_PIO;
602 /* fall through */
603 case TASKFILE_IN:
604 cmd.protocol = ATA_PROT_PIO;
605 /* fall through */
606 case TASKFILE_IN_DMAQ:
607 case TASKFILE_IN_DMA:
608 nsect = taskin / SECTOR_SIZE;
609 data_buf = inbuf;
610 break;
611 case TASKFILE_NO_DATA:
612 cmd.protocol = ATA_PROT_NODATA;
613 break;
614 default:
615 err = -EFAULT;
616 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 }
618
Bartlomiej Zolnierkiewiczac026ff2008-01-25 22:17:14 +0100619 if (req_task->req_cmd == IDE_DRIVE_TASK_NO_DATA)
620 nsect = 0;
621 else if (!nsect) {
Sergei Shtylyov745483f2009-04-08 14:13:02 +0200622 nsect = (cmd.hob.nsect << 8) | cmd.tf.nsect;
Bartlomiej Zolnierkiewiczac026ff2008-01-25 22:17:14 +0100623
624 if (!nsect) {
Jaswinder Singh Rajput2d5abce2009-08-02 20:17:34 -0700625 pr_err("%s: in/out command without data\n",
Bartlomiej Zolnierkiewiczac026ff2008-01-25 22:17:14 +0100626 drive->name);
627 err = -EFAULT;
628 goto abort;
629 }
630 }
631
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100632 err = ide_raw_taskfile(drive, &cmd, data_buf, nsect);
Bartlomiej Zolnierkiewiczac026ff2008-01-25 22:17:14 +0100633
Sergei Shtylyov745483f2009-04-08 14:13:02 +0200634 memcpy(req_task->hob_ports, &cmd.hob, HDIO_DRIVE_HOB_HDR_SIZE - 2);
635 memcpy(req_task->io_ports, &cmd.tf, HDIO_DRIVE_TASK_HDR_SIZE);
Bartlomiej Zolnierkiewicz866e2ec2008-01-25 22:17:14 +0100636
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100637 if ((cmd.ftf_flags & IDE_FTFLAG_SET_IN_FLAGS) &&
Bartlomiej Zolnierkiewicz866e2ec2008-01-25 22:17:14 +0100638 req_task->in_flags.all == 0) {
639 req_task->in_flags.all = IDE_TASKFILE_STD_IN_FLAGS;
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200640 if (drive->dev_flags & IDE_DFLAG_LBA48)
Bartlomiej Zolnierkiewicz866e2ec2008-01-25 22:17:14 +0100641 req_task->in_flags.all |= (IDE_HOB_STD_IN_FLAGS << 8);
642 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643
644 if (copy_to_user(buf, req_task, tasksize)) {
645 err = -EFAULT;
646 goto abort;
647 }
648 if (taskout) {
649 int outtotal = tasksize;
650 if (copy_to_user(buf + outtotal, outbuf, taskout)) {
651 err = -EFAULT;
652 goto abort;
653 }
654 }
655 if (taskin) {
656 int intotal = tasksize + taskout;
657 if (copy_to_user(buf + intotal, inbuf, taskin)) {
658 err = -EFAULT;
659 goto abort;
660 }
661 }
662abort:
663 kfree(req_task);
Jesper Juhl6044ec82005-11-07 01:01:32 -0800664 kfree(outbuf);
665 kfree(inbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 return err;
668}
Bartlomiej Zolnierkiewicz26a5b042007-11-05 21:42:27 +0100669#endif