blob: dc84f8bde52a81b159a29d43f3af82a34a3cf992 [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;
Sergei Shtylyov60f85012009-04-08 14:13:01 +020050 cmd.valid.out.tf = IDE_VALID_OUT_TF | IDE_VALID_DEVICE;
51 cmd.valid.in.tf = IDE_VALID_IN_TF | IDE_VALID_DEVICE;
Bartlomiej Zolnierkiewicz0dfb9912009-03-27 12:46:39 +010052 cmd.protocol = ATA_PROT_PIO;
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +010053
54 return ide_raw_taskfile(drive, &cmd, buf, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070055}
56
Bartlomiej Zolnierkiewicz1192e522008-01-25 22:17:16 +010057static ide_startstop_t task_no_data_intr(ide_drive_t *);
Bartlomiej Zolnierkiewiczadb1af92009-03-27 12:46:38 +010058static ide_startstop_t pre_task_out_intr(ide_drive_t *, struct ide_cmd *);
Bartlomiej Zolnierkiewicz901bd08a2009-03-27 12:46:39 +010059static ide_startstop_t task_pio_intr(ide_drive_t *);
Bartlomiej Zolnierkiewicz1192e522008-01-25 22:17:16 +010060
Bartlomiej Zolnierkiewiczadb1af92009-03-27 12:46:38 +010061ide_startstop_t do_rw_taskfile(ide_drive_t *drive, struct ide_cmd *orig_cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -070062{
Bartlomiej Zolnierkiewicz898ec222009-01-06 17:20:52 +010063 ide_hwif_t *hwif = drive->hwif;
Bartlomiej Zolnierkiewiczadb1af92009-03-27 12:46:38 +010064 struct ide_cmd *cmd = &hwif->cmd;
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +010065 struct ide_taskfile *tf = &cmd->tf;
Bartlomiej Zolnierkiewicz57d73662008-01-25 22:17:16 +010066 ide_handler_t *handler = NULL;
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +020067 const struct ide_tp_ops *tp_ops = hwif->tp_ops;
Bartlomiej Zolnierkiewiczf37afda2008-04-26 22:25:24 +020068 const struct ide_dma_ops *dma_ops = hwif->dma_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
Bartlomiej Zolnierkiewicz0dfb9912009-03-27 12:46:39 +010070 if (orig_cmd->protocol == ATA_PROT_PIO &&
71 (orig_cmd->tf_flags & IDE_TFLAG_MULTI_PIO) &&
72 drive->mult_count == 0) {
73 printk(KERN_ERR "%s: multimode not set!\n", drive->name);
74 return ide_stopped;
Bartlomiej Zolnierkiewicz1edee602008-01-25 22:17:15 +010075 }
76
Bartlomiej Zolnierkiewiczadb1af92009-03-27 12:46:38 +010077 if (orig_cmd->ftf_flags & IDE_FTFLAG_FLAGGED)
78 orig_cmd->ftf_flags |= IDE_FTFLAG_SET_IN_FLAGS;
Bartlomiej Zolnierkiewicz1edee602008-01-25 22:17:15 +010079
Bartlomiej Zolnierkiewiczadb1af92009-03-27 12:46:38 +010080 memcpy(cmd, orig_cmd, sizeof(*cmd));
Bartlomiej Zolnierkiewiczd6ff9f62008-10-13 21:39:41 +020081
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +010082 if ((cmd->tf_flags & IDE_TFLAG_DMA_PIO_FALLBACK) == 0) {
Bartlomiej Zolnierkiewicz089c5c72008-04-28 23:44:39 +020083 ide_tf_dump(drive->name, tf);
Sergei Shtylyovecf3a312009-03-31 20:15:30 +020084 tp_ops->write_devctl(hwif, ATA_DEVCTL_OBS);
Bartlomiej Zolnierkiewiczed4af482008-07-15 21:21:48 +020085 SELECT_MASK(drive, 0);
Sergei Shtylyov35218d12009-03-31 20:15:31 +020086
87 if (cmd->ftf_flags & IDE_FTFLAG_OUT_DATA) {
88 u8 data[2] = { tf->data, tf->hob_data };
89
90 tp_ops->output_data(drive, cmd, data, 2);
91 }
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +010092 tp_ops->tf_load(drive, cmd);
Bartlomiej Zolnierkiewicz089c5c72008-04-28 23:44:39 +020093 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
Bartlomiej Zolnierkiewicz0dfb9912009-03-27 12:46:39 +010095 switch (cmd->protocol) {
96 case ATA_PROT_PIO:
97 if (cmd->tf_flags & IDE_TFLAG_WRITE) {
98 tp_ops->exec_command(hwif, tf->command);
99 ndelay(400); /* FIXME */
100 return pre_task_out_intr(drive, cmd);
101 }
Bartlomiej Zolnierkiewicz901bd08a2009-03-27 12:46:39 +0100102 handler = task_pio_intr;
Bartlomiej Zolnierkiewicz1192e522008-01-25 22:17:16 +0100103 /* fall-through */
Bartlomiej Zolnierkiewicz0dfb9912009-03-27 12:46:39 +0100104 case ATA_PROT_NODATA:
Bartlomiej Zolnierkiewicz57d73662008-01-25 22:17:16 +0100105 if (handler == NULL)
106 handler = task_no_data_intr;
Bartlomiej Zolnierkiewicz35b5d0b2009-03-27 12:46:47 +0100107 ide_execute_command(drive, cmd, handler, WAIT_WORSTCASE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 return ide_started;
Bartlomiej Zolnierkiewiczb788ee92009-03-27 12:46:46 +0100109 case ATA_PROT_DMA:
Bartlomiej Zolnierkiewicz5ae54122009-03-31 20:15:20 +0200110 if (ide_dma_prepare(drive, cmd))
Bartlomiej Zolnierkiewicz10d90152008-01-25 22:17:16 +0100111 return ide_stopped;
Bartlomiej Zolnierkiewicz22117d62009-03-27 12:46:47 +0100112 hwif->expiry = dma_ops->dma_timer_expiry;
Bartlomiej Zolnierkiewicz35b5d0b2009-03-27 12:46:47 +0100113 ide_execute_command(drive, cmd, ide_dma_intr, 2 * WAIT_CMD);
Bartlomiej Zolnierkiewicz5e37bdc2008-04-26 22:25:24 +0200114 dma_ops->dma_start(drive);
Bartlomiej Zolnierkiewiczb788ee92009-03-27 12:46:46 +0100115 default:
Bartlomiej Zolnierkiewicz74095a92008-01-25 22:17:07 +0100116 return ide_started;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118}
Bartlomiej Zolnierkiewiczf6e29e32008-01-25 22:17:16 +0100119EXPORT_SYMBOL_GPL(do_rw_taskfile);
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,
Bartlomiej Zolnierkiewicz60c0cd02009-03-27 12:46:46 +0100148 WAIT_WORSTCASE);
Bartlomiej Zolnierkiewiczd6ff9f62008-10-13 21:39:41 +0200149 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 Zolnierkiewicze7fedc32009-03-27 12:46:41 +0100155 if (custom && tf->command == ATA_CMD_SET_MULTI)
Bartlomiej Zolnierkiewiczd6ff9f62008-10-13 21:39:41 +0200156 drive->mult_count = drive->mult_req;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
Bartlomiej Zolnierkiewiczd364c7f2009-03-27 12:46:42 +0100158 if (custom == 0 || tf->command == ATA_CMD_IDLEIMMEDIATE ||
159 tf->command == ATA_CMD_CHK_POWER) {
Bartlomiej Zolnierkiewicza09485d2009-03-27 12:46:31 +0100160 struct request *rq = hwif->rq;
Bartlomiej Zolnierkiewicza09485d2009-03-27 12:46:31 +0100161
162 if (blk_pm_request(rq))
163 ide_complete_pm_rq(drive, rq);
Bartlomiej Zolnierkiewicz2230d90d2009-03-27 12:46:42 +0100164 else
165 ide_finish_cmd(drive, cmd, stat);
Bartlomiej Zolnierkiewicza09485d2009-03-27 12:46:31 +0100166 }
167
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 return ide_stopped;
169}
170
Adrian Bunkda6f4c72008-02-01 23:09:16 +0100171static u8 wait_drive_not_busy(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172{
Bartlomiej Zolnierkiewiczb73c7ee2008-07-23 19:55:52 +0200173 ide_hwif_t *hwif = drive->hwif;
Masatake YAMATOb42fa132007-07-03 22:28:34 +0200174 int retries;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 u8 stat;
176
177 /*
Bartlomiej Zolnierkiewiczf54feaf2008-06-20 20:53:33 +0200178 * Last sector was transfered, wait until device is ready. This can
179 * take up to 6 ms on some ATAPI devices, so we will wait max 10 ms.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 */
Bartlomiej Zolnierkiewiczf54feaf2008-06-20 20:53:33 +0200181 for (retries = 0; retries < 1000; retries++) {
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200182 stat = hwif->tp_ops->read_status(hwif);
Bartlomiej Zolnierkiewiczc47137a2008-02-06 02:57:51 +0100183
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +0200184 if (stat & ATA_BUSY)
Masatake YAMATOb42fa132007-07-03 22:28:34 +0200185 udelay(10);
186 else
187 break;
188 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +0200190 if (stat & ATA_BUSY)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 printk(KERN_ERR "%s: drive still BUSY!\n", drive->name);
192
193 return stat;
194}
195
Bartlomiej Zolnierkiewicza08915b2009-03-31 20:15:13 +0200196void ide_pio_bytes(ide_drive_t *drive, struct ide_cmd *cmd,
197 unsigned int write, unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198{
199 ide_hwif_t *hwif = drive->hwif;
200 struct scatterlist *sg = hwif->sg_table;
Bartlomiej Zolnierkiewiczb6308ee2009-03-27 12:46:38 +0100201 struct scatterlist *cursg = cmd->cursg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 unsigned int offset;
205 u8 *buf;
206
Bartlomiej Zolnierkiewiczb6308ee2009-03-27 12:46:38 +0100207 cursg = cmd->cursg;
Bartlomiej Zolnierkiewicz7a007982009-03-31 20:14:59 +0200208 if (cursg == NULL)
209 cursg = cmd->cursg = sg;
Jens Axboe55c16a72007-07-25 08:13:56 +0200210
Bartlomiej Zolnierkiewicz7a007982009-03-31 20:14:59 +0200211 while (len) {
212 unsigned nr_bytes = min(len, cursg->length - cmd->cursg_ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213
Bartlomiej Zolnierkiewicz7a007982009-03-31 20:14:59 +0200214 if (nr_bytes > PAGE_SIZE)
215 nr_bytes = PAGE_SIZE;
216
217 page = sg_page(cursg);
218 offset = cursg->offset + cmd->cursg_ofs;
219
220 /* get the current page and offset */
221 page = nth_page(page, (offset >> PAGE_SHIFT));
222 offset %= PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
Bartlomiej Zolnierkiewiczf2bc3162009-03-31 20:14:59 +0200224 if (PageHighMem(page))
225 local_irq_save(flags);
226
Bartlomiej Zolnierkiewicz7a007982009-03-31 20:14:59 +0200227 buf = kmap_atomic(page, KM_BIO_SRC_IRQ) + offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228
Bartlomiej Zolnierkiewicz7a007982009-03-31 20:14:59 +0200229 cmd->nleft -= nr_bytes;
230 cmd->cursg_ofs += nr_bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
Bartlomiej Zolnierkiewicz7a007982009-03-31 20:14:59 +0200232 if (cmd->cursg_ofs == cursg->length) {
233 cursg = cmd->cursg = sg_next(cmd->cursg);
234 cmd->cursg_ofs = 0;
235 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236
Bartlomiej Zolnierkiewicz7a007982009-03-31 20:14:59 +0200237 /* do the actual data transfer */
238 if (write)
239 hwif->tp_ops->output_data(drive, cmd, buf, nr_bytes);
240 else
241 hwif->tp_ops->input_data(drive, cmd, buf, nr_bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242
Bartlomiej Zolnierkiewicz7a007982009-03-31 20:14:59 +0200243 kunmap_atomic(buf, KM_BIO_SRC_IRQ);
Bartlomiej Zolnierkiewiczf2bc3162009-03-31 20:14:59 +0200244
245 if (PageHighMem(page))
246 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
Bartlomiej Zolnierkiewicz7a007982009-03-31 20:14:59 +0200248 len -= nr_bytes;
249 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250}
Bartlomiej Zolnierkiewicza08915b2009-03-31 20:15:13 +0200251EXPORT_SYMBOL_GPL(ide_pio_bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
Bartlomiej Zolnierkiewiczadb1af92009-03-27 12:46:38 +0100253static void ide_pio_datablock(ide_drive_t *drive, struct ide_cmd *cmd,
254 unsigned int write)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255{
Bartlomiej Zolnierkiewicz7a007982009-03-31 20:14:59 +0200256 unsigned int nr_bytes;
257
Tejun Heo35cf2b92008-01-26 20:13:10 +0100258 u8 saved_io_32bit = drive->io_32bit;
259
Bartlomiej Zolnierkiewiczadb1af92009-03-27 12:46:38 +0100260 if (cmd->tf_flags & IDE_TFLAG_FS)
261 cmd->rq->errors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100263 if (cmd->tf_flags & IDE_TFLAG_IO_16BIT)
Bartlomiej Zolnierkiewicze3d9a732009-03-27 12:46:32 +0100264 drive->io_32bit = 0;
Tejun Heo35cf2b92008-01-26 20:13:10 +0100265
Andrew Morton651c29a2006-02-15 15:17:37 -0800266 touch_softlockup_watchdog();
267
Bartlomiej Zolnierkiewicz0dfb9912009-03-27 12:46:39 +0100268 if (cmd->tf_flags & IDE_TFLAG_MULTI_PIO)
Bartlomiej Zolnierkiewicz7a007982009-03-31 20:14:59 +0200269 nr_bytes = min_t(unsigned, cmd->nleft, drive->mult_count << 9);
Bartlomiej Zolnierkiewicz0dfb9912009-03-27 12:46:39 +0100270 else
Bartlomiej Zolnierkiewicz7a007982009-03-31 20:14:59 +0200271 nr_bytes = SECTOR_SIZE;
272
273 ide_pio_bytes(drive, cmd, write, nr_bytes);
Tejun Heo35cf2b92008-01-26 20:13:10 +0100274
275 drive->io_32bit = saved_io_32bit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276}
277
Bartlomiej Zolnierkiewicz041cea12009-03-27 12:46:41 +0100278static void ide_error_cmd(ide_drive_t *drive, struct ide_cmd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279{
Bartlomiej Zolnierkiewiczadb1af92009-03-27 12:46:38 +0100280 if (cmd->tf_flags & IDE_TFLAG_FS) {
Bartlomiej Zolnierkiewiczbf717c02009-03-27 12:46:47 +0100281 int nr_bytes = cmd->nbytes - cmd->nleft;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
Bartlomiej Zolnierkiewicz0dfb9912009-03-27 12:46:39 +0100283 if (cmd->protocol == ATA_PROT_PIO &&
284 ((cmd->tf_flags & IDE_TFLAG_WRITE) || cmd->nleft == 0)) {
285 if (cmd->tf_flags & IDE_TFLAG_MULTI_PIO)
Bartlomiej Zolnierkiewiczbf717c02009-03-27 12:46:47 +0100286 nr_bytes -= drive->mult_count << 9;
Bartlomiej Zolnierkiewicz0dfb9912009-03-27 12:46:39 +0100287 else
Bartlomiej Zolnierkiewiczbf717c02009-03-27 12:46:47 +0100288 nr_bytes -= SECTOR_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 }
290
Bartlomiej Zolnierkiewiczbf717c02009-03-27 12:46:47 +0100291 if (nr_bytes > 0)
292 ide_complete_rq(drive, 0, nr_bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294}
295
Bartlomiej Zolnierkiewiczadb1af92009-03-27 12:46:38 +0100296void ide_finish_cmd(ide_drive_t *drive, struct ide_cmd *cmd, u8 stat)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297{
Bartlomiej Zolnierkiewicz6902a532009-03-27 12:46:43 +0100298 struct request *rq = drive->hwif->rq;
Bartlomiej Zolnierkiewicz2230d90d2009-03-27 12:46:42 +0100299 u8 err = ide_read_error(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300
Bartlomiej Zolnierkiewicz2230d90d2009-03-27 12:46:42 +0100301 ide_complete_cmd(drive, cmd, stat, err);
Bartlomiej Zolnierkiewicz6902a532009-03-27 12:46:43 +0100302 rq->errors = err;
Bartlomiej Zolnierkiewiczf974b192009-03-27 12:46:44 +0100303 ide_complete_rq(drive, err ? -EIO : 0, blk_rq_bytes(rq));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304}
305
306/*
Bartlomiej Zolnierkiewicz901bd08a2009-03-27 12:46:39 +0100307 * Handler for command with PIO data phase.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 */
Bartlomiej Zolnierkiewicz901bd08a2009-03-27 12:46:39 +0100309static ide_startstop_t task_pio_intr(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310{
311 ide_hwif_t *hwif = drive->hwif;
Bartlomiej Zolnierkiewiczadb1af92009-03-27 12:46:38 +0100312 struct ide_cmd *cmd = &drive->hwif->cmd;
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200313 u8 stat = hwif->tp_ops->read_status(hwif);
Bartlomiej Zolnierkiewicz901bd08a2009-03-27 12:46:39 +0100314 u8 write = !!(cmd->tf_flags & IDE_TFLAG_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315
Bartlomiej Zolnierkiewicz901bd08a2009-03-27 12:46:39 +0100316 if (write == 0) {
317 /* Error? */
318 if (stat & ATA_ERR)
Bartlomiej Zolnierkiewicz0a1248c2009-03-27 12:46:41 +0100319 goto out_err;
Linus Torvalds6c3c3152008-03-18 21:26:24 -0700320
Bartlomiej Zolnierkiewicz901bd08a2009-03-27 12:46:39 +0100321 /* Didn't want any data? Odd. */
Bartlomiej Zolnierkiewicz151055e2009-03-27 12:46:39 +0100322 if ((stat & ATA_DRQ) == 0) {
323 /* Command all done? */
Bartlomiej Zolnierkiewicz0a1248c2009-03-27 12:46:41 +0100324 if (OK_STAT(stat, ATA_DRDY, ATA_BUSY))
325 goto out_end;
Bartlomiej Zolnierkiewicz151055e2009-03-27 12:46:39 +0100326
327 /* Assume it was a spurious irq */
Bartlomiej Zolnierkiewicz0a1248c2009-03-27 12:46:41 +0100328 goto out_wait;
Bartlomiej Zolnierkiewicz151055e2009-03-27 12:46:39 +0100329 }
Bartlomiej Zolnierkiewicz901bd08a2009-03-27 12:46:39 +0100330 } else {
331 if (!OK_STAT(stat, DRIVE_READY, drive->bad_wstat))
Bartlomiej Zolnierkiewicz0a1248c2009-03-27 12:46:41 +0100332 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333
Bartlomiej Zolnierkiewicz901bd08a2009-03-27 12:46:39 +0100334 /* Deal with unexpected ATA data phase. */
335 if (((stat & ATA_DRQ) == 0) ^ (cmd->nleft == 0))
Bartlomiej Zolnierkiewicz0a1248c2009-03-27 12:46:41 +0100336 goto out_err;
Bartlomiej Zolnierkiewicz901bd08a2009-03-27 12:46:39 +0100337 }
338
Bartlomiej Zolnierkiewicz0a1248c2009-03-27 12:46:41 +0100339 if (write && cmd->nleft == 0)
340 goto out_end;
Bartlomiej Zolnierkiewicz901bd08a2009-03-27 12:46:39 +0100341
342 /* Still data left to transfer. */
343 ide_pio_datablock(drive, cmd, write);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344
Linus Torvalds6c3c3152008-03-18 21:26:24 -0700345 /* Are we done? Check status and finish transfer. */
Bartlomiej Zolnierkiewicz901bd08a2009-03-27 12:46:39 +0100346 if (write == 0 && cmd->nleft == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 stat = wait_drive_not_busy(drive);
Bartlomiej Zolnierkiewicz73d7de02008-01-26 20:13:10 +0100348 if (!OK_STAT(stat, 0, BAD_STAT))
Bartlomiej Zolnierkiewicz0a1248c2009-03-27 12:46:41 +0100349 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350
Bartlomiej Zolnierkiewicz0a1248c2009-03-27 12:46:41 +0100351 goto out_end;
352 }
353out_wait:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 /* Still data left to transfer. */
Bartlomiej Zolnierkiewicz60c0cd02009-03-27 12:46:46 +0100355 ide_set_handler(drive, &task_pio_intr, WAIT_WORSTCASE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 return ide_started;
Bartlomiej Zolnierkiewicz0a1248c2009-03-27 12:46:41 +0100357out_end:
Bartlomiej Zolnierkiewicz2230d90d2009-03-27 12:46:42 +0100358 if ((cmd->tf_flags & IDE_TFLAG_FS) == 0)
359 ide_finish_cmd(drive, cmd, stat);
360 else
Bartlomiej Zolnierkiewicz130e8862009-03-27 12:46:45 +0100361 ide_complete_rq(drive, 0, cmd->rq->nr_sectors << 9);
Bartlomiej Zolnierkiewicz0a1248c2009-03-27 12:46:41 +0100362 return ide_stopped;
363out_err:
Bartlomiej Zolnierkiewicz041cea12009-03-27 12:46:41 +0100364 ide_error_cmd(drive, cmd);
365 return ide_error(drive, __func__, stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366}
367
Bartlomiej Zolnierkiewiczadb1af92009-03-27 12:46:38 +0100368static ide_startstop_t pre_task_out_intr(ide_drive_t *drive,
369 struct ide_cmd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370{
371 ide_startstop_t startstop;
372
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +0200373 if (ide_wait_stat(&startstop, drive, ATA_DRQ,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 drive->bad_wstat, WAIT_DRQ)) {
375 printk(KERN_ERR "%s: no DRQ after issuing %sWRITE%s\n",
Bartlomiej Zolnierkiewiczc7db9662009-03-27 12:46:27 +0100376 drive->name,
Bartlomiej Zolnierkiewicz0dfb9912009-03-27 12:46:39 +0100377 (cmd->tf_flags & IDE_TFLAG_MULTI_PIO) ? "MULT" : "",
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200378 (drive->dev_flags & IDE_DFLAG_LBA48) ? "_EXT" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 return startstop;
380 }
381
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200382 if ((drive->dev_flags & IDE_DFLAG_UNMASK) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 local_irq_disable();
384
Bartlomiej Zolnierkiewicz60c0cd02009-03-27 12:46:46 +0100385 ide_set_handler(drive, &task_pio_intr, WAIT_WORSTCASE);
Bartlomiej Zolnierkiewicz901bd08a2009-03-27 12:46:39 +0100386
Bartlomiej Zolnierkiewiczadb1af92009-03-27 12:46:38 +0100387 ide_pio_datablock(drive, cmd, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388
389 return ide_started;
390}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100392int ide_raw_taskfile(ide_drive_t *drive, struct ide_cmd *cmd, u8 *buf,
393 u16 nsect)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394{
FUJITA Tomonori154ed282008-07-15 21:21:43 +0200395 struct request *rq;
396 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397
FUJITA Tomonori154ed282008-07-15 21:21:43 +0200398 rq = blk_get_request(drive->queue, READ, __GFP_WAIT);
399 rq->cmd_type = REQ_TYPE_ATA_TASKFILE;
400 rq->buffer = buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401
402 /*
403 * (ks) We transfer currently only whole sectors.
404 * This is suffient for now. But, it would be great,
405 * if we would find a solution to transfer any size.
406 * To support special commands like READ LONG.
407 */
FUJITA Tomonori154ed282008-07-15 21:21:43 +0200408 rq->hard_nr_sectors = rq->nr_sectors = nsect;
409 rq->hard_cur_sectors = rq->current_nr_sectors = nsect;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100411 if (cmd->tf_flags & IDE_TFLAG_WRITE)
FUJITA Tomonori154ed282008-07-15 21:21:43 +0200412 rq->cmd_flags |= REQ_RW;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100414 rq->special = cmd;
415 cmd->rq = rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416
FUJITA Tomonori154ed282008-07-15 21:21:43 +0200417 error = blk_execute_rq(drive->queue, NULL, rq, 0);
418 blk_put_request(rq);
419
420 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421}
422
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423EXPORT_SYMBOL(ide_raw_taskfile);
424
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100425int ide_no_data_taskfile(ide_drive_t *drive, struct ide_cmd *cmd)
Bartlomiej Zolnierkiewicz9a3c49b2008-01-25 22:17:07 +0100426{
Bartlomiej Zolnierkiewicz0dfb9912009-03-27 12:46:39 +0100427 cmd->protocol = ATA_PROT_NODATA;
Bartlomiej Zolnierkiewicz9a3c49b2008-01-25 22:17:07 +0100428
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100429 return ide_raw_taskfile(drive, cmd, NULL, 0);
Bartlomiej Zolnierkiewicz9a3c49b2008-01-25 22:17:07 +0100430}
431EXPORT_SYMBOL_GPL(ide_no_data_taskfile);
432
Bartlomiej Zolnierkiewicz26a5b042007-11-05 21:42:27 +0100433#ifdef CONFIG_IDE_TASK_IOCTL
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100434int ide_taskfile_ioctl(ide_drive_t *drive, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435{
436 ide_task_request_t *req_task;
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100437 struct ide_cmd cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 u8 *outbuf = NULL;
439 u8 *inbuf = NULL;
Bartlomiej Zolnierkiewiczac026ff2008-01-25 22:17:14 +0100440 u8 *data_buf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 int err = 0;
442 int tasksize = sizeof(struct ide_task_request_s);
Alan Cox3a42bb22006-10-16 16:31:02 +0100443 unsigned int taskin = 0;
444 unsigned int taskout = 0;
Bartlomiej Zolnierkiewiczac026ff2008-01-25 22:17:14 +0100445 u16 nsect = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 char __user *buf = (char __user *)arg;
447
448// printk("IDE Taskfile ...\n");
449
Deepak Saxenaf5e3c2f2005-11-07 01:01:25 -0800450 req_task = kzalloc(tasksize, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 if (req_task == NULL) return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 if (copy_from_user(req_task, buf, tasksize)) {
453 kfree(req_task);
454 return -EFAULT;
455 }
456
Alan Cox3a42bb22006-10-16 16:31:02 +0100457 taskout = req_task->out_size;
458 taskin = req_task->in_size;
459
460 if (taskin > 65536 || taskout > 65536) {
461 err = -EINVAL;
462 goto abort;
463 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464
465 if (taskout) {
466 int outtotal = tasksize;
Deepak Saxenaf5e3c2f2005-11-07 01:01:25 -0800467 outbuf = kzalloc(taskout, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 if (outbuf == NULL) {
469 err = -ENOMEM;
470 goto abort;
471 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 if (copy_from_user(outbuf, buf + outtotal, taskout)) {
473 err = -EFAULT;
474 goto abort;
475 }
476 }
477
478 if (taskin) {
479 int intotal = tasksize + taskout;
Deepak Saxenaf5e3c2f2005-11-07 01:01:25 -0800480 inbuf = kzalloc(taskin, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 if (inbuf == NULL) {
482 err = -ENOMEM;
483 goto abort;
484 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 if (copy_from_user(inbuf, buf + intotal, taskin)) {
486 err = -EFAULT;
487 goto abort;
488 }
489 }
490
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100491 memset(&cmd, 0, sizeof(cmd));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100493 memcpy(&cmd.tf_array[0], req_task->hob_ports,
494 HDIO_DRIVE_HOB_HDR_SIZE - 2);
495 memcpy(&cmd.tf_array[6], req_task->io_ports,
496 HDIO_DRIVE_TASK_HDR_SIZE);
Bartlomiej Zolnierkiewicz866e2ec2008-01-25 22:17:14 +0100497
Sergei Shtylyov60f85012009-04-08 14:13:01 +0200498 cmd.valid.out.tf = IDE_VALID_DEVICE;
499 cmd.valid.in.tf = IDE_VALID_DEVICE | IDE_VALID_IN_TF;
500 cmd.tf_flags = IDE_TFLAG_IO_16BIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501
Sergei Shtylyov60f85012009-04-08 14:13:01 +0200502 if (drive->dev_flags & IDE_DFLAG_LBA48) {
503 cmd.tf_flags |= IDE_TFLAG_LBA48;
504 cmd.valid.in.hob = IDE_VALID_IN_HOB;
505 }
Bartlomiej Zolnierkiewicza3bbb9d2008-01-25 22:17:10 +0100506
Bartlomiej Zolnierkiewicz74095a92008-01-25 22:17:07 +0100507 if (req_task->out_flags.all) {
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100508 cmd.ftf_flags |= IDE_FTFLAG_FLAGGED;
Bartlomiej Zolnierkiewicz74095a92008-01-25 22:17:07 +0100509
510 if (req_task->out_flags.b.data)
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100511 cmd.ftf_flags |= IDE_FTFLAG_OUT_DATA;
Bartlomiej Zolnierkiewicz74095a92008-01-25 22:17:07 +0100512
513 if (req_task->out_flags.b.nsector_hob)
Sergei Shtylyov60f85012009-04-08 14:13:01 +0200514 cmd.valid.out.hob |= IDE_VALID_NSECT;
Bartlomiej Zolnierkiewicz74095a92008-01-25 22:17:07 +0100515 if (req_task->out_flags.b.sector_hob)
Sergei Shtylyov60f85012009-04-08 14:13:01 +0200516 cmd.valid.out.hob |= IDE_VALID_LBAL;
Bartlomiej Zolnierkiewicz74095a92008-01-25 22:17:07 +0100517 if (req_task->out_flags.b.lcyl_hob)
Sergei Shtylyov60f85012009-04-08 14:13:01 +0200518 cmd.valid.out.hob |= IDE_VALID_LBAM;
Bartlomiej Zolnierkiewicz74095a92008-01-25 22:17:07 +0100519 if (req_task->out_flags.b.hcyl_hob)
Sergei Shtylyov60f85012009-04-08 14:13:01 +0200520 cmd.valid.out.hob |= IDE_VALID_LBAH;
Bartlomiej Zolnierkiewicz74095a92008-01-25 22:17:07 +0100521
522 if (req_task->out_flags.b.error_feature)
Sergei Shtylyov60f85012009-04-08 14:13:01 +0200523 cmd.valid.out.tf |= IDE_VALID_FEATURE;
Bartlomiej Zolnierkiewicz74095a92008-01-25 22:17:07 +0100524 if (req_task->out_flags.b.nsector)
Sergei Shtylyov60f85012009-04-08 14:13:01 +0200525 cmd.valid.out.tf |= IDE_VALID_NSECT;
Bartlomiej Zolnierkiewicz74095a92008-01-25 22:17:07 +0100526 if (req_task->out_flags.b.sector)
Sergei Shtylyov60f85012009-04-08 14:13:01 +0200527 cmd.valid.out.tf |= IDE_VALID_LBAL;
Bartlomiej Zolnierkiewicz74095a92008-01-25 22:17:07 +0100528 if (req_task->out_flags.b.lcyl)
Sergei Shtylyov60f85012009-04-08 14:13:01 +0200529 cmd.valid.out.tf |= IDE_VALID_LBAM;
Bartlomiej Zolnierkiewicz74095a92008-01-25 22:17:07 +0100530 if (req_task->out_flags.b.hcyl)
Sergei Shtylyov60f85012009-04-08 14:13:01 +0200531 cmd.valid.out.tf |= IDE_VALID_LBAH;
Bartlomiej Zolnierkiewicza3bbb9d2008-01-25 22:17:10 +0100532 } else {
Sergei Shtylyov60f85012009-04-08 14:13:01 +0200533 cmd.valid.out.tf |= IDE_VALID_OUT_TF;
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100534 if (cmd.tf_flags & IDE_TFLAG_LBA48)
Sergei Shtylyov60f85012009-04-08 14:13:01 +0200535 cmd.valid.out.hob |= IDE_VALID_OUT_HOB;
Bartlomiej Zolnierkiewicz74095a92008-01-25 22:17:07 +0100536 }
537
Bartlomiej Zolnierkiewicz866e2ec2008-01-25 22:17:14 +0100538 if (req_task->in_flags.b.data)
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100539 cmd.ftf_flags |= IDE_FTFLAG_IN_DATA;
Bartlomiej Zolnierkiewicz866e2ec2008-01-25 22:17:14 +0100540
Bartlomiej Zolnierkiewicz04d09b02009-03-27 12:46:38 +0100541 if (req_task->req_cmd == IDE_DRIVE_TASK_RAW_WRITE) {
542 /* fixup data phase if needed */
543 if (req_task->data_phase == TASKFILE_IN_DMAQ ||
544 req_task->data_phase == TASKFILE_IN_DMA)
Bartlomiej Zolnierkiewicz0dfb9912009-03-27 12:46:39 +0100545 cmd.tf_flags |= IDE_TFLAG_WRITE;
Bartlomiej Zolnierkiewicz04d09b02009-03-27 12:46:38 +0100546 }
547
Bartlomiej Zolnierkiewicz0dfb9912009-03-27 12:46:39 +0100548 cmd.protocol = ATA_PROT_DMA;
549
550 switch (req_task->data_phase) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 case TASKFILE_MULTI_OUT:
552 if (!drive->mult_count) {
553 /* (hs): give up if multcount is not set */
554 printk(KERN_ERR "%s: %s Multimode Write " \
555 "multcount is not set\n",
Harvey Harrisoneb639632008-04-26 22:25:20 +0200556 drive->name, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 err = -EPERM;
558 goto abort;
559 }
Bartlomiej Zolnierkiewicz0dfb9912009-03-27 12:46:39 +0100560 cmd.tf_flags |= IDE_TFLAG_MULTI_PIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 /* fall through */
562 case TASKFILE_OUT:
Bartlomiej Zolnierkiewicz0dfb9912009-03-27 12:46:39 +0100563 cmd.protocol = ATA_PROT_PIO;
Bartlomiej Zolnierkiewiczac026ff2008-01-25 22:17:14 +0100564 /* fall through */
565 case TASKFILE_OUT_DMAQ:
566 case TASKFILE_OUT_DMA:
Bartlomiej Zolnierkiewicz04d09b02009-03-27 12:46:38 +0100567 cmd.tf_flags |= IDE_TFLAG_WRITE;
Bartlomiej Zolnierkiewiczac026ff2008-01-25 22:17:14 +0100568 nsect = taskout / SECTOR_SIZE;
569 data_buf = outbuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 break;
571 case TASKFILE_MULTI_IN:
572 if (!drive->mult_count) {
573 /* (hs): give up if multcount is not set */
574 printk(KERN_ERR "%s: %s Multimode Read failure " \
575 "multcount is not set\n",
Harvey Harrisoneb639632008-04-26 22:25:20 +0200576 drive->name, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 err = -EPERM;
578 goto abort;
579 }
Bartlomiej Zolnierkiewicz0dfb9912009-03-27 12:46:39 +0100580 cmd.tf_flags |= IDE_TFLAG_MULTI_PIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 /* fall through */
582 case TASKFILE_IN:
Bartlomiej Zolnierkiewicz0dfb9912009-03-27 12:46:39 +0100583 cmd.protocol = ATA_PROT_PIO;
Bartlomiej Zolnierkiewiczac026ff2008-01-25 22:17:14 +0100584 /* fall through */
585 case TASKFILE_IN_DMAQ:
586 case TASKFILE_IN_DMA:
587 nsect = taskin / SECTOR_SIZE;
588 data_buf = inbuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 break;
590 case TASKFILE_NO_DATA:
Bartlomiej Zolnierkiewicz0dfb9912009-03-27 12:46:39 +0100591 cmd.protocol = ATA_PROT_NODATA;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 break;
593 default:
594 err = -EFAULT;
595 goto abort;
596 }
597
Bartlomiej Zolnierkiewiczac026ff2008-01-25 22:17:14 +0100598 if (req_task->req_cmd == IDE_DRIVE_TASK_NO_DATA)
599 nsect = 0;
600 else if (!nsect) {
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100601 nsect = (cmd.tf.hob_nsect << 8) | cmd.tf.nsect;
Bartlomiej Zolnierkiewiczac026ff2008-01-25 22:17:14 +0100602
603 if (!nsect) {
604 printk(KERN_ERR "%s: in/out command without data\n",
605 drive->name);
606 err = -EFAULT;
607 goto abort;
608 }
609 }
610
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100611 err = ide_raw_taskfile(drive, &cmd, data_buf, nsect);
Bartlomiej Zolnierkiewiczac026ff2008-01-25 22:17:14 +0100612
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100613 memcpy(req_task->hob_ports, &cmd.tf_array[0],
614 HDIO_DRIVE_HOB_HDR_SIZE - 2);
615 memcpy(req_task->io_ports, &cmd.tf_array[6],
616 HDIO_DRIVE_TASK_HDR_SIZE);
Bartlomiej Zolnierkiewicz866e2ec2008-01-25 22:17:14 +0100617
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100618 if ((cmd.ftf_flags & IDE_FTFLAG_SET_IN_FLAGS) &&
Bartlomiej Zolnierkiewicz866e2ec2008-01-25 22:17:14 +0100619 req_task->in_flags.all == 0) {
620 req_task->in_flags.all = IDE_TASKFILE_STD_IN_FLAGS;
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200621 if (drive->dev_flags & IDE_DFLAG_LBA48)
Bartlomiej Zolnierkiewicz866e2ec2008-01-25 22:17:14 +0100622 req_task->in_flags.all |= (IDE_HOB_STD_IN_FLAGS << 8);
623 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624
625 if (copy_to_user(buf, req_task, tasksize)) {
626 err = -EFAULT;
627 goto abort;
628 }
629 if (taskout) {
630 int outtotal = tasksize;
631 if (copy_to_user(buf + outtotal, outbuf, taskout)) {
632 err = -EFAULT;
633 goto abort;
634 }
635 }
636 if (taskin) {
637 int intotal = tasksize + taskout;
638 if (copy_to_user(buf + intotal, inbuf, taskin)) {
639 err = -EFAULT;
640 goto abort;
641 }
642 }
643abort:
644 kfree(req_task);
Jesper Juhl6044ec82005-11-07 01:01:32 -0800645 kfree(outbuf);
646 kfree(inbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647
648// printk("IDE Taskfile ioctl ended. rc = %i\n", err);
649
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 return err;
651}
Bartlomiej Zolnierkiewicz26a5b042007-11-05 21:42:27 +0100652#endif