blob: 6665fc4724b996a8e0e5c039c16b2e5c9d044333 [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. Silvadf561f662020-08-23 17:36:59 -0500132 fallthrough;
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. Silvadf561f662020-08-23 17:36:59 -0500144 fallthrough;
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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 unsigned int offset;
234 u8 *buf;
235
Bartlomiej Zolnierkiewicz7a007982009-03-31 20:14:59 +0200236 if (cursg == NULL)
237 cursg = cmd->cursg = sg;
Jens Axboe55c16a72007-07-25 08:13:56 +0200238
Bartlomiej Zolnierkiewicz7a007982009-03-31 20:14:59 +0200239 while (len) {
240 unsigned nr_bytes = min(len, cursg->length - cmd->cursg_ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241
Bartlomiej Zolnierkiewicz7a007982009-03-31 20:14:59 +0200242 page = sg_page(cursg);
243 offset = cursg->offset + cmd->cursg_ofs;
244
245 /* get the current page and offset */
246 page = nth_page(page, (offset >> PAGE_SHIFT));
247 offset %= PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
Steven J. Hill15812082013-06-20 10:30:37 -0500249 nr_bytes = min_t(unsigned, nr_bytes, (PAGE_SIZE - offset));
250
Cong Wang45b408d2011-11-25 23:14:20 +0800251 buf = kmap_atomic(page) + offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
Bartlomiej Zolnierkiewicz7a007982009-03-31 20:14:59 +0200253 cmd->nleft -= nr_bytes;
254 cmd->cursg_ofs += nr_bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
Bartlomiej Zolnierkiewicz7a007982009-03-31 20:14:59 +0200256 if (cmd->cursg_ofs == cursg->length) {
257 cursg = cmd->cursg = sg_next(cmd->cursg);
258 cmd->cursg_ofs = 0;
259 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260
Bartlomiej Zolnierkiewicz7a007982009-03-31 20:14:59 +0200261 /* do the actual data transfer */
262 if (write)
263 hwif->tp_ops->output_data(drive, cmd, buf, nr_bytes);
264 else
265 hwif->tp_ops->input_data(drive, cmd, buf, nr_bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266
Cong Wang45b408d2011-11-25 23:14:20 +0800267 kunmap_atomic(buf);
Bartlomiej Zolnierkiewiczf2bc3162009-03-31 20:14:59 +0200268
Bartlomiej Zolnierkiewicz7a007982009-03-31 20:14:59 +0200269 len -= nr_bytes;
270 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271}
Bartlomiej Zolnierkiewicza08915b2009-03-31 20:15:13 +0200272EXPORT_SYMBOL_GPL(ide_pio_bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273
Bartlomiej Zolnierkiewiczadb1af92009-03-27 12:46:38 +0100274static void ide_pio_datablock(ide_drive_t *drive, struct ide_cmd *cmd,
275 unsigned int write)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276{
Bartlomiej Zolnierkiewicz7a007982009-03-31 20:14:59 +0200277 unsigned int nr_bytes;
278
Tejun Heo35cf2b92008-01-26 20:13:10 +0100279 u8 saved_io_32bit = drive->io_32bit;
280
Bartlomiej Zolnierkiewiczadb1af92009-03-27 12:46:38 +0100281 if (cmd->tf_flags & IDE_TFLAG_FS)
Christoph Hellwig17d53632017-04-20 16:03:01 +0200282 scsi_req(cmd->rq)->result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100284 if (cmd->tf_flags & IDE_TFLAG_IO_16BIT)
Bartlomiej Zolnierkiewicze3d9a732009-03-27 12:46:32 +0100285 drive->io_32bit = 0;
Tejun Heo35cf2b92008-01-26 20:13:10 +0100286
Andrew Morton651c29a2006-02-15 15:17:37 -0800287 touch_softlockup_watchdog();
288
Bartlomiej Zolnierkiewicz0dfb9912009-03-27 12:46:39 +0100289 if (cmd->tf_flags & IDE_TFLAG_MULTI_PIO)
Bartlomiej Zolnierkiewicz7a007982009-03-31 20:14:59 +0200290 nr_bytes = min_t(unsigned, cmd->nleft, drive->mult_count << 9);
Bartlomiej Zolnierkiewicz0dfb9912009-03-27 12:46:39 +0100291 else
Bartlomiej Zolnierkiewicz7a007982009-03-31 20:14:59 +0200292 nr_bytes = SECTOR_SIZE;
293
294 ide_pio_bytes(drive, cmd, write, nr_bytes);
Tejun Heo35cf2b92008-01-26 20:13:10 +0100295
296 drive->io_32bit = saved_io_32bit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297}
298
Bartlomiej Zolnierkiewicz041cea12009-03-27 12:46:41 +0100299static void ide_error_cmd(ide_drive_t *drive, struct ide_cmd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300{
Bartlomiej Zolnierkiewiczadb1af92009-03-27 12:46:38 +0100301 if (cmd->tf_flags & IDE_TFLAG_FS) {
Bartlomiej Zolnierkiewiczbf717c02009-03-27 12:46:47 +0100302 int nr_bytes = cmd->nbytes - cmd->nleft;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303
Bartlomiej Zolnierkiewicz0dfb9912009-03-27 12:46:39 +0100304 if (cmd->protocol == ATA_PROT_PIO &&
305 ((cmd->tf_flags & IDE_TFLAG_WRITE) || cmd->nleft == 0)) {
306 if (cmd->tf_flags & IDE_TFLAG_MULTI_PIO)
Bartlomiej Zolnierkiewiczbf717c02009-03-27 12:46:47 +0100307 nr_bytes -= drive->mult_count << 9;
Bartlomiej Zolnierkiewicz0dfb9912009-03-27 12:46:39 +0100308 else
Bartlomiej Zolnierkiewiczbf717c02009-03-27 12:46:47 +0100309 nr_bytes -= SECTOR_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 }
311
Bartlomiej Zolnierkiewiczbf717c02009-03-27 12:46:47 +0100312 if (nr_bytes > 0)
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200313 ide_complete_rq(drive, BLK_STS_OK, nr_bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315}
316
Bartlomiej Zolnierkiewiczadb1af92009-03-27 12:46:38 +0100317void ide_finish_cmd(ide_drive_t *drive, struct ide_cmd *cmd, u8 stat)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318{
Bartlomiej Zolnierkiewicz6902a532009-03-27 12:46:43 +0100319 struct request *rq = drive->hwif->rq;
Bartlomiej Zolnierkiewicz665d66e2009-06-23 11:35:51 +0000320 u8 err = ide_read_error(drive), nsect = cmd->tf.nsect;
321 u8 set_xfer = !!(cmd->tf_flags & IDE_TFLAG_SET_XFER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322
Bartlomiej Zolnierkiewicz2230d90d2009-03-27 12:46:42 +0100323 ide_complete_cmd(drive, cmd, stat, err);
Christoph Hellwig17d53632017-04-20 16:03:01 +0200324 scsi_req(rq)->result = err;
Bartlomiej Zolnierkiewicz665d66e2009-06-23 11:35:51 +0000325
326 if (err == 0 && set_xfer) {
327 ide_set_xfer_rate(drive, nsect);
328 ide_driveid_update(drive);
329 }
330
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200331 ide_complete_rq(drive, err ? BLK_STS_IOERR : BLK_STS_OK, blk_rq_bytes(rq));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332}
333
334/*
Bartlomiej Zolnierkiewicz901bd08a2009-03-27 12:46:39 +0100335 * Handler for command with PIO data phase.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 */
Bartlomiej Zolnierkiewicz901bd08a2009-03-27 12:46:39 +0100337static ide_startstop_t task_pio_intr(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338{
339 ide_hwif_t *hwif = drive->hwif;
Bartlomiej Zolnierkiewiczadb1af92009-03-27 12:46:38 +0100340 struct ide_cmd *cmd = &drive->hwif->cmd;
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200341 u8 stat = hwif->tp_ops->read_status(hwif);
Bartlomiej Zolnierkiewicz901bd08a2009-03-27 12:46:39 +0100342 u8 write = !!(cmd->tf_flags & IDE_TFLAG_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343
Bartlomiej Zolnierkiewicz901bd08a2009-03-27 12:46:39 +0100344 if (write == 0) {
345 /* Error? */
346 if (stat & ATA_ERR)
Bartlomiej Zolnierkiewicz0a1248c2009-03-27 12:46:41 +0100347 goto out_err;
Linus Torvalds6c3c3152008-03-18 21:26:24 -0700348
Bartlomiej Zolnierkiewicz901bd08a2009-03-27 12:46:39 +0100349 /* Didn't want any data? Odd. */
Bartlomiej Zolnierkiewicz151055e2009-03-27 12:46:39 +0100350 if ((stat & ATA_DRQ) == 0) {
351 /* Command all done? */
Bartlomiej Zolnierkiewicz0a1248c2009-03-27 12:46:41 +0100352 if (OK_STAT(stat, ATA_DRDY, ATA_BUSY))
353 goto out_end;
Bartlomiej Zolnierkiewicz151055e2009-03-27 12:46:39 +0100354
355 /* Assume it was a spurious irq */
Bartlomiej Zolnierkiewicz0a1248c2009-03-27 12:46:41 +0100356 goto out_wait;
Bartlomiej Zolnierkiewicz151055e2009-03-27 12:46:39 +0100357 }
Bartlomiej Zolnierkiewicz901bd08a2009-03-27 12:46:39 +0100358 } else {
359 if (!OK_STAT(stat, DRIVE_READY, drive->bad_wstat))
Bartlomiej Zolnierkiewicz0a1248c2009-03-27 12:46:41 +0100360 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361
Bartlomiej Zolnierkiewicz901bd08a2009-03-27 12:46:39 +0100362 /* Deal with unexpected ATA data phase. */
363 if (((stat & ATA_DRQ) == 0) ^ (cmd->nleft == 0))
Bartlomiej Zolnierkiewicz0a1248c2009-03-27 12:46:41 +0100364 goto out_err;
Bartlomiej Zolnierkiewicz901bd08a2009-03-27 12:46:39 +0100365 }
366
Bartlomiej Zolnierkiewicz0a1248c2009-03-27 12:46:41 +0100367 if (write && cmd->nleft == 0)
368 goto out_end;
Bartlomiej Zolnierkiewicz901bd08a2009-03-27 12:46:39 +0100369
370 /* Still data left to transfer. */
371 ide_pio_datablock(drive, cmd, write);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372
Linus Torvalds6c3c3152008-03-18 21:26:24 -0700373 /* Are we done? Check status and finish transfer. */
Bartlomiej Zolnierkiewicz901bd08a2009-03-27 12:46:39 +0100374 if (write == 0 && cmd->nleft == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 stat = wait_drive_not_busy(drive);
Bartlomiej Zolnierkiewicz73d7de02008-01-26 20:13:10 +0100376 if (!OK_STAT(stat, 0, BAD_STAT))
Bartlomiej Zolnierkiewicz0a1248c2009-03-27 12:46:41 +0100377 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378
Bartlomiej Zolnierkiewicz0a1248c2009-03-27 12:46:41 +0100379 goto out_end;
380 }
381out_wait:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 /* Still data left to transfer. */
Bartlomiej Zolnierkiewicz60c0cd02009-03-27 12:46:46 +0100383 ide_set_handler(drive, &task_pio_intr, WAIT_WORSTCASE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 return ide_started;
Bartlomiej Zolnierkiewicz0a1248c2009-03-27 12:46:41 +0100385out_end:
Bartlomiej Zolnierkiewicz2230d90d2009-03-27 12:46:42 +0100386 if ((cmd->tf_flags & IDE_TFLAG_FS) == 0)
387 ide_finish_cmd(drive, cmd, stat);
388 else
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200389 ide_complete_rq(drive, BLK_STS_OK, blk_rq_sectors(cmd->rq) << 9);
Bartlomiej Zolnierkiewicz0a1248c2009-03-27 12:46:41 +0100390 return ide_stopped;
391out_err:
Bartlomiej Zolnierkiewicz041cea12009-03-27 12:46:41 +0100392 ide_error_cmd(drive, cmd);
393 return ide_error(drive, __func__, stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394}
395
Bartlomiej Zolnierkiewiczadb1af92009-03-27 12:46:38 +0100396static ide_startstop_t pre_task_out_intr(ide_drive_t *drive,
397 struct ide_cmd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398{
399 ide_startstop_t startstop;
400
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +0200401 if (ide_wait_stat(&startstop, drive, ATA_DRQ,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 drive->bad_wstat, WAIT_DRQ)) {
Jaswinder Singh Rajput2d5abce2009-08-02 20:17:34 -0700403 pr_err("%s: no DRQ after issuing %sWRITE%s\n", drive->name,
Bartlomiej Zolnierkiewicz0dfb9912009-03-27 12:46:39 +0100404 (cmd->tf_flags & IDE_TFLAG_MULTI_PIO) ? "MULT" : "",
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200405 (drive->dev_flags & IDE_DFLAG_LBA48) ? "_EXT" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 return startstop;
407 }
408
Sebastian Andrzej Siewior47b82e82018-05-04 16:24:46 +0200409 if (!force_irqthreads && (drive->dev_flags & IDE_DFLAG_UNMASK) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 local_irq_disable();
411
Bartlomiej Zolnierkiewicz60c0cd02009-03-27 12:46:46 +0100412 ide_set_handler(drive, &task_pio_intr, WAIT_WORSTCASE);
Bartlomiej Zolnierkiewicz901bd08a2009-03-27 12:46:39 +0100413
Bartlomiej Zolnierkiewiczadb1af92009-03-27 12:46:38 +0100414 ide_pio_datablock(drive, cmd, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
416 return ide_started;
417}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100419int ide_raw_taskfile(ide_drive_t *drive, struct ide_cmd *cmd, u8 *buf,
420 u16 nsect)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421{
FUJITA Tomonori154ed282008-07-15 21:21:43 +0200422 struct request *rq;
423 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100425 rq = blk_get_request(drive->queue,
426 (cmd->tf_flags & IDE_TFLAG_WRITE) ?
Christoph Hellwigff005a02018-05-09 09:54:05 +0200427 REQ_OP_DRV_OUT : REQ_OP_DRV_IN, 0);
Christoph Hellwig2f5a8e802017-01-31 16:57:30 +0100428 ide_req(rq)->type = ATA_PRIV_TASKFILE;
Tejun Heod868ca22009-04-19 07:00:42 +0900429
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 /*
431 * (ks) We transfer currently only whole sectors.
432 * This is suffient for now. But, it would be great,
433 * if we would find a solution to transfer any size.
434 * To support special commands like READ LONG.
435 */
Tejun Heod868ca22009-04-19 07:00:42 +0900436 if (nsect) {
437 error = blk_rq_map_kern(drive->queue, rq, buf,
Christoph Hellwig0eb0b632018-05-09 09:54:08 +0200438 nsect * SECTOR_SIZE, GFP_NOIO);
Tejun Heod868ca22009-04-19 07:00:42 +0900439 if (error)
440 goto put_req;
441 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442
Christoph Hellwig22ce0a72018-11-10 09:30:49 +0100443 ide_req(rq)->special = cmd;
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100444 cmd->rq = rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445
Guoqing Jiang684da762021-01-25 05:49:58 +0100446 blk_execute_rq(NULL, rq, 0);
Christoph Hellwig17d53632017-04-20 16:03:01 +0200447 error = scsi_req(rq)->result ? -EIO : 0;
Tejun Heod868ca22009-04-19 07:00:42 +0900448put_req:
449 blk_put_request(rq);
FUJITA Tomonori154ed282008-07-15 21:21:43 +0200450 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452EXPORT_SYMBOL(ide_raw_taskfile);
453
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100454int ide_no_data_taskfile(ide_drive_t *drive, struct ide_cmd *cmd)
Bartlomiej Zolnierkiewicz9a3c49b2008-01-25 22:17:07 +0100455{
Bartlomiej Zolnierkiewicz0dfb9912009-03-27 12:46:39 +0100456 cmd->protocol = ATA_PROT_NODATA;
Bartlomiej Zolnierkiewicz9a3c49b2008-01-25 22:17:07 +0100457
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100458 return ide_raw_taskfile(drive, cmd, NULL, 0);
Bartlomiej Zolnierkiewicz9a3c49b2008-01-25 22:17:07 +0100459}
460EXPORT_SYMBOL_GPL(ide_no_data_taskfile);
461
Bartlomiej Zolnierkiewicz26a5b042007-11-05 21:42:27 +0100462#ifdef CONFIG_IDE_TASK_IOCTL
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100463int ide_taskfile_ioctl(ide_drive_t *drive, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464{
465 ide_task_request_t *req_task;
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100466 struct ide_cmd cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 u8 *outbuf = NULL;
468 u8 *inbuf = NULL;
Bartlomiej Zolnierkiewiczac026ff2008-01-25 22:17:14 +0100469 u8 *data_buf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 int err = 0;
471 int tasksize = sizeof(struct ide_task_request_s);
Alan Cox3a42bb22006-10-16 16:31:02 +0100472 unsigned int taskin = 0;
473 unsigned int taskout = 0;
Bartlomiej Zolnierkiewiczac026ff2008-01-25 22:17:14 +0100474 u16 nsect = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 char __user *buf = (char __user *)arg;
476
Julia Lawall7d543d82010-06-04 16:11:17 -0700477 req_task = memdup_user(buf, tasksize);
478 if (IS_ERR(req_task))
479 return PTR_ERR(req_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480
Alan Cox3a42bb22006-10-16 16:31:02 +0100481 taskout = req_task->out_size;
482 taskin = req_task->in_size;
Jaswinder Singh Rajput2d5abce2009-08-02 20:17:34 -0700483
Alan Cox3a42bb22006-10-16 16:31:02 +0100484 if (taskin > 65536 || taskout > 65536) {
485 err = -EINVAL;
486 goto abort;
487 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488
489 if (taskout) {
490 int outtotal = tasksize;
Deepak Saxenaf5e3c2f2005-11-07 01:01:25 -0800491 outbuf = kzalloc(taskout, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 if (outbuf == NULL) {
493 err = -ENOMEM;
494 goto abort;
495 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 if (copy_from_user(outbuf, buf + outtotal, taskout)) {
497 err = -EFAULT;
498 goto abort;
499 }
500 }
501
502 if (taskin) {
503 int intotal = tasksize + taskout;
Deepak Saxenaf5e3c2f2005-11-07 01:01:25 -0800504 inbuf = kzalloc(taskin, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 if (inbuf == NULL) {
506 err = -ENOMEM;
507 goto abort;
508 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 if (copy_from_user(inbuf, buf + intotal, taskin)) {
510 err = -EFAULT;
511 goto abort;
512 }
513 }
514
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100515 memset(&cmd, 0, sizeof(cmd));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516
Sergei Shtylyov745483f2009-04-08 14:13:02 +0200517 memcpy(&cmd.hob, req_task->hob_ports, HDIO_DRIVE_HOB_HDR_SIZE - 2);
518 memcpy(&cmd.tf, req_task->io_ports, HDIO_DRIVE_TASK_HDR_SIZE);
Bartlomiej Zolnierkiewicz866e2ec2008-01-25 22:17:14 +0100519
Sergei Shtylyov60f85012009-04-08 14:13:01 +0200520 cmd.valid.out.tf = IDE_VALID_DEVICE;
521 cmd.valid.in.tf = IDE_VALID_DEVICE | IDE_VALID_IN_TF;
522 cmd.tf_flags = IDE_TFLAG_IO_16BIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523
Sergei Shtylyov60f85012009-04-08 14:13:01 +0200524 if (drive->dev_flags & IDE_DFLAG_LBA48) {
525 cmd.tf_flags |= IDE_TFLAG_LBA48;
526 cmd.valid.in.hob = IDE_VALID_IN_HOB;
527 }
Bartlomiej Zolnierkiewicza3bbb9d2008-01-25 22:17:10 +0100528
Bartlomiej Zolnierkiewicz74095a92008-01-25 22:17:07 +0100529 if (req_task->out_flags.all) {
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100530 cmd.ftf_flags |= IDE_FTFLAG_FLAGGED;
Bartlomiej Zolnierkiewicz74095a92008-01-25 22:17:07 +0100531
532 if (req_task->out_flags.b.data)
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100533 cmd.ftf_flags |= IDE_FTFLAG_OUT_DATA;
Bartlomiej Zolnierkiewicz74095a92008-01-25 22:17:07 +0100534
535 if (req_task->out_flags.b.nsector_hob)
Sergei Shtylyov60f85012009-04-08 14:13:01 +0200536 cmd.valid.out.hob |= IDE_VALID_NSECT;
Bartlomiej Zolnierkiewicz74095a92008-01-25 22:17:07 +0100537 if (req_task->out_flags.b.sector_hob)
Sergei Shtylyov60f85012009-04-08 14:13:01 +0200538 cmd.valid.out.hob |= IDE_VALID_LBAL;
Bartlomiej Zolnierkiewicz74095a92008-01-25 22:17:07 +0100539 if (req_task->out_flags.b.lcyl_hob)
Sergei Shtylyov60f85012009-04-08 14:13:01 +0200540 cmd.valid.out.hob |= IDE_VALID_LBAM;
Bartlomiej Zolnierkiewicz74095a92008-01-25 22:17:07 +0100541 if (req_task->out_flags.b.hcyl_hob)
Sergei Shtylyov60f85012009-04-08 14:13:01 +0200542 cmd.valid.out.hob |= IDE_VALID_LBAH;
Bartlomiej Zolnierkiewicz74095a92008-01-25 22:17:07 +0100543
544 if (req_task->out_flags.b.error_feature)
Sergei Shtylyov60f85012009-04-08 14:13:01 +0200545 cmd.valid.out.tf |= IDE_VALID_FEATURE;
Bartlomiej Zolnierkiewicz74095a92008-01-25 22:17:07 +0100546 if (req_task->out_flags.b.nsector)
Sergei Shtylyov60f85012009-04-08 14:13:01 +0200547 cmd.valid.out.tf |= IDE_VALID_NSECT;
Bartlomiej Zolnierkiewicz74095a92008-01-25 22:17:07 +0100548 if (req_task->out_flags.b.sector)
Sergei Shtylyov60f85012009-04-08 14:13:01 +0200549 cmd.valid.out.tf |= IDE_VALID_LBAL;
Bartlomiej Zolnierkiewicz74095a92008-01-25 22:17:07 +0100550 if (req_task->out_flags.b.lcyl)
Sergei Shtylyov60f85012009-04-08 14:13:01 +0200551 cmd.valid.out.tf |= IDE_VALID_LBAM;
Bartlomiej Zolnierkiewicz74095a92008-01-25 22:17:07 +0100552 if (req_task->out_flags.b.hcyl)
Sergei Shtylyov60f85012009-04-08 14:13:01 +0200553 cmd.valid.out.tf |= IDE_VALID_LBAH;
Bartlomiej Zolnierkiewicza3bbb9d2008-01-25 22:17:10 +0100554 } else {
Sergei Shtylyov60f85012009-04-08 14:13:01 +0200555 cmd.valid.out.tf |= IDE_VALID_OUT_TF;
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100556 if (cmd.tf_flags & IDE_TFLAG_LBA48)
Sergei Shtylyov60f85012009-04-08 14:13:01 +0200557 cmd.valid.out.hob |= IDE_VALID_OUT_HOB;
Bartlomiej Zolnierkiewicz74095a92008-01-25 22:17:07 +0100558 }
559
Bartlomiej Zolnierkiewicz866e2ec2008-01-25 22:17:14 +0100560 if (req_task->in_flags.b.data)
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100561 cmd.ftf_flags |= IDE_FTFLAG_IN_DATA;
Bartlomiej Zolnierkiewicz866e2ec2008-01-25 22:17:14 +0100562
Bartlomiej Zolnierkiewicz04d09b02009-03-27 12:46:38 +0100563 if (req_task->req_cmd == IDE_DRIVE_TASK_RAW_WRITE) {
564 /* fixup data phase if needed */
565 if (req_task->data_phase == TASKFILE_IN_DMAQ ||
566 req_task->data_phase == TASKFILE_IN_DMA)
Bartlomiej Zolnierkiewicz0dfb9912009-03-27 12:46:39 +0100567 cmd.tf_flags |= IDE_TFLAG_WRITE;
Bartlomiej Zolnierkiewicz04d09b02009-03-27 12:46:38 +0100568 }
569
Bartlomiej Zolnierkiewicz0dfb9912009-03-27 12:46:39 +0100570 cmd.protocol = ATA_PROT_DMA;
571
572 switch (req_task->data_phase) {
Jaswinder Singh Rajput2d5abce2009-08-02 20:17:34 -0700573 case TASKFILE_MULTI_OUT:
574 if (!drive->mult_count) {
575 /* (hs): give up if multcount is not set */
576 pr_err("%s: %s Multimode Write multcount is not set\n",
577 drive->name, __func__);
578 err = -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 goto abort;
Jaswinder Singh Rajput2d5abce2009-08-02 20:17:34 -0700580 }
581 cmd.tf_flags |= IDE_TFLAG_MULTI_PIO;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -0500582 fallthrough;
Jaswinder Singh Rajput2d5abce2009-08-02 20:17:34 -0700583 case TASKFILE_OUT:
584 cmd.protocol = ATA_PROT_PIO;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -0500585 fallthrough;
Jaswinder Singh Rajput2d5abce2009-08-02 20:17:34 -0700586 case TASKFILE_OUT_DMAQ:
587 case TASKFILE_OUT_DMA:
588 cmd.tf_flags |= IDE_TFLAG_WRITE;
589 nsect = taskout / SECTOR_SIZE;
590 data_buf = outbuf;
591 break;
592 case TASKFILE_MULTI_IN:
593 if (!drive->mult_count) {
594 /* (hs): give up if multcount is not set */
595 pr_err("%s: %s Multimode Read multcount is not set\n",
596 drive->name, __func__);
597 err = -EPERM;
598 goto abort;
599 }
600 cmd.tf_flags |= IDE_TFLAG_MULTI_PIO;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -0500601 fallthrough;
Jaswinder Singh Rajput2d5abce2009-08-02 20:17:34 -0700602 case TASKFILE_IN:
603 cmd.protocol = ATA_PROT_PIO;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -0500604 fallthrough;
Jaswinder Singh Rajput2d5abce2009-08-02 20:17:34 -0700605 case TASKFILE_IN_DMAQ:
606 case TASKFILE_IN_DMA:
607 nsect = taskin / SECTOR_SIZE;
608 data_buf = inbuf;
609 break;
610 case TASKFILE_NO_DATA:
611 cmd.protocol = ATA_PROT_NODATA;
612 break;
613 default:
614 err = -EFAULT;
615 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 }
617
Bartlomiej Zolnierkiewiczac026ff2008-01-25 22:17:14 +0100618 if (req_task->req_cmd == IDE_DRIVE_TASK_NO_DATA)
619 nsect = 0;
620 else if (!nsect) {
Sergei Shtylyov745483f2009-04-08 14:13:02 +0200621 nsect = (cmd.hob.nsect << 8) | cmd.tf.nsect;
Bartlomiej Zolnierkiewiczac026ff2008-01-25 22:17:14 +0100622
623 if (!nsect) {
Jaswinder Singh Rajput2d5abce2009-08-02 20:17:34 -0700624 pr_err("%s: in/out command without data\n",
Bartlomiej Zolnierkiewiczac026ff2008-01-25 22:17:14 +0100625 drive->name);
626 err = -EFAULT;
627 goto abort;
628 }
629 }
630
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100631 err = ide_raw_taskfile(drive, &cmd, data_buf, nsect);
Bartlomiej Zolnierkiewiczac026ff2008-01-25 22:17:14 +0100632
Sergei Shtylyov745483f2009-04-08 14:13:02 +0200633 memcpy(req_task->hob_ports, &cmd.hob, HDIO_DRIVE_HOB_HDR_SIZE - 2);
634 memcpy(req_task->io_ports, &cmd.tf, HDIO_DRIVE_TASK_HDR_SIZE);
Bartlomiej Zolnierkiewicz866e2ec2008-01-25 22:17:14 +0100635
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100636 if ((cmd.ftf_flags & IDE_FTFLAG_SET_IN_FLAGS) &&
Bartlomiej Zolnierkiewicz866e2ec2008-01-25 22:17:14 +0100637 req_task->in_flags.all == 0) {
638 req_task->in_flags.all = IDE_TASKFILE_STD_IN_FLAGS;
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200639 if (drive->dev_flags & IDE_DFLAG_LBA48)
Bartlomiej Zolnierkiewicz866e2ec2008-01-25 22:17:14 +0100640 req_task->in_flags.all |= (IDE_HOB_STD_IN_FLAGS << 8);
641 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642
643 if (copy_to_user(buf, req_task, tasksize)) {
644 err = -EFAULT;
645 goto abort;
646 }
647 if (taskout) {
648 int outtotal = tasksize;
649 if (copy_to_user(buf + outtotal, outbuf, taskout)) {
650 err = -EFAULT;
651 goto abort;
652 }
653 }
654 if (taskin) {
655 int intotal = tasksize + taskout;
656 if (copy_to_user(buf + intotal, inbuf, taskin)) {
657 err = -EFAULT;
658 goto abort;
659 }
660 }
661abort:
662 kfree(req_task);
Jesper Juhl6044ec82005-11-07 01:01:32 -0800663 kfree(outbuf);
664 kfree(inbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 return err;
667}
Bartlomiej Zolnierkiewicz26a5b042007-11-05 21:42:27 +0100668#endif