blob: 9504aa54c33b19bf67495c7680130acded6d4ae0 [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/module.h>
12#include <linux/types.h>
13#include <linux/string.h>
14#include <linux/kernel.h>
15#include <linux/timer.h>
16#include <linux/mm.h>
Andrew Morton651c29a2006-02-15 15:17:37 -080017#include <linux/sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/interrupt.h>
19#include <linux/major.h>
20#include <linux/errno.h>
21#include <linux/genhd.h>
22#include <linux/blkpg.h>
23#include <linux/slab.h>
24#include <linux/pci.h>
25#include <linux/delay.h>
26#include <linux/hdreg.h>
27#include <linux/ide.h>
28#include <linux/bitops.h>
Jens Axboe55c16a72007-07-25 08:13:56 +020029#include <linux/scatterlist.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
31#include <asm/byteorder.h>
32#include <asm/irq.h>
33#include <asm/uaccess.h>
34#include <asm/io.h>
35
Bartlomiej Zolnierkiewicz9e422372008-01-25 22:17:07 +010036void ide_tf_load(ide_drive_t *drive, ide_task_t *task)
37{
38 ide_hwif_t *hwif = drive->hwif;
39 struct ide_taskfile *tf = &task->tf;
40 u8 HIHI = (task->tf_flags & IDE_TFLAG_LBA48) ? 0xE0 : 0xEF;
41
Bartlomiej Zolnierkiewicz74095a92008-01-25 22:17:07 +010042 if (task->tf_flags & IDE_TFLAG_FLAGGED)
43 HIHI = 0xFF;
44
Bartlomiej Zolnierkiewicz807e35d2008-01-25 22:17:10 +010045#ifdef DEBUG
46 printk("%s: tf: feat 0x%02x nsect 0x%02x lbal 0x%02x "
47 "lbam 0x%02x lbah 0x%02x dev 0x%02x cmd 0x%02x\n",
48 drive->name, tf->feature, tf->nsect, tf->lbal,
49 tf->lbam, tf->lbah, tf->device, tf->command);
Bartlomiej Zolnierkiewicz6dd9b832008-01-26 20:13:03 +010050 printk("%s: hob: nsect 0x%02x lbal 0x%02x "
51 "lbam 0x%02x lbah 0x%02x\n",
52 drive->name, tf->hob_nsect, tf->hob_lbal,
53 tf->hob_lbam, tf->hob_lbah);
Bartlomiej Zolnierkiewicz807e35d2008-01-25 22:17:10 +010054#endif
55
Bartlomiej Zolnierkiewicz81ca6912008-01-26 20:13:08 +010056 ide_set_irq(drive, 1);
Bartlomiej Zolnierkiewicz9e422372008-01-25 22:17:07 +010057
58 if ((task->tf_flags & IDE_TFLAG_NO_SELECT_MASK) == 0)
59 SELECT_MASK(drive, 0);
60
Bartlomiej Zolnierkiewicz74095a92008-01-25 22:17:07 +010061 if (task->tf_flags & IDE_TFLAG_OUT_DATA)
Bartlomiej Zolnierkiewicz23579a22008-04-18 00:46:26 +020062 hwif->OUTW((tf->hob_data << 8) | tf->data,
63 hwif->io_ports[IDE_DATA_OFFSET]);
Bartlomiej Zolnierkiewicz9e422372008-01-25 22:17:07 +010064
Bartlomiej Zolnierkiewicz74095a92008-01-25 22:17:07 +010065 if (task->tf_flags & IDE_TFLAG_OUT_HOB_FEATURE)
Bartlomiej Zolnierkiewicz23579a22008-04-18 00:46:26 +020066 hwif->OUTB(tf->hob_feature, hwif->io_ports[IDE_FEATURE_OFFSET]);
Bartlomiej Zolnierkiewicz74095a92008-01-25 22:17:07 +010067 if (task->tf_flags & IDE_TFLAG_OUT_HOB_NSECT)
Bartlomiej Zolnierkiewicz23579a22008-04-18 00:46:26 +020068 hwif->OUTB(tf->hob_nsect, hwif->io_ports[IDE_NSECTOR_OFFSET]);
Bartlomiej Zolnierkiewicz74095a92008-01-25 22:17:07 +010069 if (task->tf_flags & IDE_TFLAG_OUT_HOB_LBAL)
Bartlomiej Zolnierkiewicz23579a22008-04-18 00:46:26 +020070 hwif->OUTB(tf->hob_lbal, hwif->io_ports[IDE_SECTOR_OFFSET]);
Bartlomiej Zolnierkiewicz74095a92008-01-25 22:17:07 +010071 if (task->tf_flags & IDE_TFLAG_OUT_HOB_LBAM)
Bartlomiej Zolnierkiewicz23579a22008-04-18 00:46:26 +020072 hwif->OUTB(tf->hob_lbam, hwif->io_ports[IDE_LCYL_OFFSET]);
Bartlomiej Zolnierkiewicz74095a92008-01-25 22:17:07 +010073 if (task->tf_flags & IDE_TFLAG_OUT_HOB_LBAH)
Bartlomiej Zolnierkiewicz23579a22008-04-18 00:46:26 +020074 hwif->OUTB(tf->hob_lbah, hwif->io_ports[IDE_HCYL_OFFSET]);
Bartlomiej Zolnierkiewicz74095a92008-01-25 22:17:07 +010075
76 if (task->tf_flags & IDE_TFLAG_OUT_FEATURE)
Bartlomiej Zolnierkiewicz23579a22008-04-18 00:46:26 +020077 hwif->OUTB(tf->feature, hwif->io_ports[IDE_FEATURE_OFFSET]);
Bartlomiej Zolnierkiewicz74095a92008-01-25 22:17:07 +010078 if (task->tf_flags & IDE_TFLAG_OUT_NSECT)
Bartlomiej Zolnierkiewicz23579a22008-04-18 00:46:26 +020079 hwif->OUTB(tf->nsect, hwif->io_ports[IDE_NSECTOR_OFFSET]);
Bartlomiej Zolnierkiewicz74095a92008-01-25 22:17:07 +010080 if (task->tf_flags & IDE_TFLAG_OUT_LBAL)
Bartlomiej Zolnierkiewicz23579a22008-04-18 00:46:26 +020081 hwif->OUTB(tf->lbal, hwif->io_ports[IDE_SECTOR_OFFSET]);
Bartlomiej Zolnierkiewicz74095a92008-01-25 22:17:07 +010082 if (task->tf_flags & IDE_TFLAG_OUT_LBAM)
Bartlomiej Zolnierkiewicz23579a22008-04-18 00:46:26 +020083 hwif->OUTB(tf->lbam, hwif->io_ports[IDE_LCYL_OFFSET]);
Bartlomiej Zolnierkiewicz74095a92008-01-25 22:17:07 +010084 if (task->tf_flags & IDE_TFLAG_OUT_LBAH)
Bartlomiej Zolnierkiewicz23579a22008-04-18 00:46:26 +020085 hwif->OUTB(tf->lbah, hwif->io_ports[IDE_HCYL_OFFSET]);
Bartlomiej Zolnierkiewicz9e422372008-01-25 22:17:07 +010086
Bartlomiej Zolnierkiewicz807e35d2008-01-25 22:17:10 +010087 if (task->tf_flags & IDE_TFLAG_OUT_DEVICE)
Bartlomiej Zolnierkiewicz23579a22008-04-18 00:46:26 +020088 hwif->OUTB((tf->device & HIHI) | drive->select.all,
89 hwif->io_ports[IDE_SELECT_OFFSET]);
Bartlomiej Zolnierkiewicz9e422372008-01-25 22:17:07 +010090}
91
Linus Torvalds1da177e2005-04-16 15:20:36 -070092int taskfile_lib_get_identify (ide_drive_t *drive, u8 *buf)
93{
94 ide_task_t args;
Bartlomiej Zolnierkiewicz650d8412008-01-25 22:17:06 +010095
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 memset(&args, 0, sizeof(ide_task_t));
Bartlomiej Zolnierkiewicz650d8412008-01-25 22:17:06 +010097 args.tf.nsect = 0x01;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 if (drive->media == ide_disk)
Bartlomiej Zolnierkiewicz650d8412008-01-25 22:17:06 +010099 args.tf.command = WIN_IDENTIFY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 else
Bartlomiej Zolnierkiewicz650d8412008-01-25 22:17:06 +0100101 args.tf.command = WIN_PIDENTIFY;
Bartlomiej Zolnierkiewicz657cc1a2008-01-26 20:13:10 +0100102 args.tf_flags = IDE_TFLAG_TF | IDE_TFLAG_DEVICE;
Bartlomiej Zolnierkiewiczac026ff2008-01-25 22:17:14 +0100103 args.data_phase = TASKFILE_IN;
Bartlomiej Zolnierkiewiczac026ff2008-01-25 22:17:14 +0100104 return ide_raw_taskfile(drive, &args, buf, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105}
106
Bartlomiej Zolnierkiewicz74095a92008-01-25 22:17:07 +0100107static int inline task_dma_ok(ide_task_t *task)
108{
Bartlomiej Zolnierkiewiczf6e29e32008-01-25 22:17:16 +0100109 if (blk_fs_request(task->rq) || (task->tf_flags & IDE_TFLAG_FLAGGED))
Bartlomiej Zolnierkiewicz74095a92008-01-25 22:17:07 +0100110 return 1;
111
112 switch (task->tf.command) {
113 case WIN_WRITEDMA_ONCE:
114 case WIN_WRITEDMA:
115 case WIN_WRITEDMA_EXT:
116 case WIN_READDMA_ONCE:
117 case WIN_READDMA:
118 case WIN_READDMA_EXT:
119 case WIN_IDENTIFY_DMA:
120 return 1;
121 }
122
123 return 0;
124}
125
Bartlomiej Zolnierkiewicz1192e522008-01-25 22:17:16 +0100126static ide_startstop_t task_no_data_intr(ide_drive_t *);
Bartlomiej Zolnierkiewicz57d73662008-01-25 22:17:16 +0100127static ide_startstop_t set_geometry_intr(ide_drive_t *);
128static ide_startstop_t recal_intr(ide_drive_t *);
129static ide_startstop_t set_multmode_intr(ide_drive_t *);
Bartlomiej Zolnierkiewiczf6e29e32008-01-25 22:17:16 +0100130static ide_startstop_t pre_task_out_intr(ide_drive_t *, struct request *);
131static ide_startstop_t task_in_intr(ide_drive_t *);
Bartlomiej Zolnierkiewicz1192e522008-01-25 22:17:16 +0100132
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133ide_startstop_t do_rw_taskfile (ide_drive_t *drive, ide_task_t *task)
134{
135 ide_hwif_t *hwif = HWIF(drive);
Bartlomiej Zolnierkiewicz650d8412008-01-25 22:17:06 +0100136 struct ide_taskfile *tf = &task->tf;
Bartlomiej Zolnierkiewicz57d73662008-01-25 22:17:16 +0100137 ide_handler_t *handler = NULL;
Bartlomiej Zolnierkiewicz5e37bdc2008-04-26 22:25:24 +0200138 struct ide_dma_ops *dma_ops = hwif->dma_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
Bartlomiej Zolnierkiewicz1edee602008-01-25 22:17:15 +0100140 if (task->data_phase == TASKFILE_MULTI_IN ||
141 task->data_phase == TASKFILE_MULTI_OUT) {
142 if (!drive->mult_count) {
143 printk(KERN_ERR "%s: multimode not set!\n",
144 drive->name);
145 return ide_stopped;
146 }
147 }
148
149 if (task->tf_flags & IDE_TFLAG_FLAGGED)
150 task->tf_flags |= IDE_TFLAG_FLAGGED_SET_IN_FLAGS;
151
Bartlomiej Zolnierkiewiczf6e29e32008-01-25 22:17:16 +0100152 if ((task->tf_flags & IDE_TFLAG_DMA_PIO_FALLBACK) == 0)
153 ide_tf_load(drive, task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
Bartlomiej Zolnierkiewicz10d90152008-01-25 22:17:16 +0100155 switch (task->data_phase) {
156 case TASKFILE_MULTI_OUT:
157 case TASKFILE_OUT:
Bartlomiej Zolnierkiewicz23579a22008-04-18 00:46:26 +0200158 hwif->OUTBSYNC(drive, tf->command,
159 hwif->io_ports[IDE_COMMAND_OFFSET]);
Bartlomiej Zolnierkiewicz10d90152008-01-25 22:17:16 +0100160 ndelay(400); /* FIXME */
161 return pre_task_out_intr(drive, task->rq);
162 case TASKFILE_MULTI_IN:
163 case TASKFILE_IN:
Bartlomiej Zolnierkiewicz57d73662008-01-25 22:17:16 +0100164 handler = task_in_intr;
Bartlomiej Zolnierkiewicz1192e522008-01-25 22:17:16 +0100165 /* fall-through */
Bartlomiej Zolnierkiewicz10d90152008-01-25 22:17:16 +0100166 case TASKFILE_NO_DATA:
Bartlomiej Zolnierkiewicz57d73662008-01-25 22:17:16 +0100167 if (handler == NULL)
168 handler = task_no_data_intr;
Bartlomiej Zolnierkiewicz1192e522008-01-25 22:17:16 +0100169 /* WIN_{SPECIFY,RESTORE,SETMULT} use custom handlers */
Bartlomiej Zolnierkiewicz57d73662008-01-25 22:17:16 +0100170 if (task->tf_flags & IDE_TFLAG_CUSTOM_HANDLER) {
171 switch (tf->command) {
172 case WIN_SPECIFY: handler = set_geometry_intr; break;
173 case WIN_RESTORE: handler = recal_intr; break;
174 case WIN_SETMULT: handler = set_multmode_intr; break;
175 }
176 }
177 ide_execute_command(drive, tf->command, handler,
178 WAIT_WORSTCASE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 return ide_started;
Bartlomiej Zolnierkiewicz10d90152008-01-25 22:17:16 +0100180 default:
181 if (task_dma_ok(task) == 0 || drive->using_dma == 0 ||
Bartlomiej Zolnierkiewicz5e37bdc2008-04-26 22:25:24 +0200182 dma_ops->dma_setup(drive))
Bartlomiej Zolnierkiewicz10d90152008-01-25 22:17:16 +0100183 return ide_stopped;
Bartlomiej Zolnierkiewicz5e37bdc2008-04-26 22:25:24 +0200184 dma_ops->dma_exec_cmd(drive, tf->command);
185 dma_ops->dma_start(drive);
Bartlomiej Zolnierkiewicz74095a92008-01-25 22:17:07 +0100186 return ide_started;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188}
Bartlomiej Zolnierkiewiczf6e29e32008-01-25 22:17:16 +0100189EXPORT_SYMBOL_GPL(do_rw_taskfile);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191/*
192 * set_multmode_intr() is invoked on completion of a WIN_SETMULT cmd.
193 */
Bartlomiej Zolnierkiewicz57d73662008-01-25 22:17:16 +0100194static ide_startstop_t set_multmode_intr(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195{
Bartlomiej Zolnierkiewiczc47137a2008-02-06 02:57:51 +0100196 u8 stat = ide_read_status(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197
Bartlomiej Zolnierkiewiczc47137a2008-02-06 02:57:51 +0100198 if (OK_STAT(stat, READY_STAT, BAD_STAT))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 drive->mult_count = drive->mult_req;
Bartlomiej Zolnierkiewiczc47137a2008-02-06 02:57:51 +0100200 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 drive->mult_req = drive->mult_count = 0;
202 drive->special.b.recalibrate = 1;
203 (void) ide_dump_status(drive, "set_multmode", stat);
204 }
205 return ide_stopped;
206}
207
208/*
209 * set_geometry_intr() is invoked on completion of a WIN_SPECIFY cmd.
210 */
Bartlomiej Zolnierkiewicz57d73662008-01-25 22:17:16 +0100211static ide_startstop_t set_geometry_intr(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 int retries = 5;
214 u8 stat;
215
Bartlomiej Zolnierkiewiczc47137a2008-02-06 02:57:51 +0100216 while (((stat = ide_read_status(drive)) & BUSY_STAT) && retries--)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 udelay(10);
218
219 if (OK_STAT(stat, READY_STAT, BAD_STAT))
220 return ide_stopped;
221
222 if (stat & (ERR_STAT|DRQ_STAT))
223 return ide_error(drive, "set_geometry_intr", stat);
224
Eric Sesterhenn125e1872006-06-23 02:06:06 -0700225 BUG_ON(HWGROUP(drive)->handler != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 ide_set_handler(drive, &set_geometry_intr, WAIT_WORSTCASE, NULL);
227 return ide_started;
228}
229
230/*
231 * recal_intr() is invoked on completion of a WIN_RESTORE (recalibrate) cmd.
232 */
Bartlomiej Zolnierkiewicz57d73662008-01-25 22:17:16 +0100233static ide_startstop_t recal_intr(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234{
Bartlomiej Zolnierkiewiczc47137a2008-02-06 02:57:51 +0100235 u8 stat = ide_read_status(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236
Bartlomiej Zolnierkiewiczc47137a2008-02-06 02:57:51 +0100237 if (!OK_STAT(stat, READY_STAT, BAD_STAT))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 return ide_error(drive, "recal_intr", stat);
239 return ide_stopped;
240}
241
242/*
243 * Handler for commands without a data phase
244 */
Bartlomiej Zolnierkiewicz1192e522008-01-25 22:17:16 +0100245static ide_startstop_t task_no_data_intr(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246{
247 ide_task_t *args = HWGROUP(drive)->rq->special;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 u8 stat;
249
Ingo Molnar366c7f52006-07-03 00:25:25 -0700250 local_irq_enable_in_hardirq();
Bartlomiej Zolnierkiewiczc47137a2008-02-06 02:57:51 +0100251 stat = ide_read_status(drive);
252
253 if (!OK_STAT(stat, READY_STAT, BAD_STAT))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 return ide_error(drive, "task_no_data_intr", stat);
255 /* calls ide_end_drive_cmd */
Bartlomiej Zolnierkiewiczc47137a2008-02-06 02:57:51 +0100256
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 if (args)
Bartlomiej Zolnierkiewicz64a57fe2008-02-06 02:57:51 +0100258 ide_end_drive_cmd(drive, stat, ide_read_error(drive));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259
260 return ide_stopped;
261}
262
Adrian Bunkda6f4c72008-02-01 23:09:16 +0100263static u8 wait_drive_not_busy(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264{
Masatake YAMATOb42fa132007-07-03 22:28:34 +0200265 int retries;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 u8 stat;
267
268 /*
269 * Last sector was transfered, wait until drive is ready.
Bartlomiej Zolnierkiewicz26245652008-01-26 20:13:11 +0100270 * This can take up to 10 usec, but we will wait max 1 ms.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 */
Masatake YAMATOb42fa132007-07-03 22:28:34 +0200272 for (retries = 0; retries < 100; retries++) {
Bartlomiej Zolnierkiewiczc47137a2008-02-06 02:57:51 +0100273 stat = ide_read_status(drive);
274
275 if (stat & BUSY_STAT)
Masatake YAMATOb42fa132007-07-03 22:28:34 +0200276 udelay(10);
277 else
278 break;
279 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280
Masatake YAMATOb42fa132007-07-03 22:28:34 +0200281 if (stat & BUSY_STAT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 printk(KERN_ERR "%s: drive still BUSY!\n", drive->name);
283
284 return stat;
285}
286
287static void ide_pio_sector(ide_drive_t *drive, unsigned int write)
288{
289 ide_hwif_t *hwif = drive->hwif;
290 struct scatterlist *sg = hwif->sg_table;
Jens Axboe55c16a72007-07-25 08:13:56 +0200291 struct scatterlist *cursg = hwif->cursg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 struct page *page;
293#ifdef CONFIG_HIGHMEM
294 unsigned long flags;
295#endif
296 unsigned int offset;
297 u8 *buf;
298
Jens Axboe55c16a72007-07-25 08:13:56 +0200299 cursg = hwif->cursg;
300 if (!cursg) {
301 cursg = sg;
302 hwif->cursg = sg;
303 }
304
Jens Axboe45711f12007-10-22 21:19:53 +0200305 page = sg_page(cursg);
Jens Axboe55c16a72007-07-25 08:13:56 +0200306 offset = cursg->offset + hwif->cursg_ofs * SECTOR_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307
308 /* get the current page and offset */
309 page = nth_page(page, (offset >> PAGE_SHIFT));
310 offset %= PAGE_SIZE;
311
312#ifdef CONFIG_HIGHMEM
313 local_irq_save(flags);
314#endif
315 buf = kmap_atomic(page, KM_BIO_SRC_IRQ) + offset;
316
317 hwif->nleft--;
318 hwif->cursg_ofs++;
319
Jens Axboe55c16a72007-07-25 08:13:56 +0200320 if ((hwif->cursg_ofs * SECTOR_SIZE) == cursg->length) {
321 hwif->cursg = sg_next(hwif->cursg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 hwif->cursg_ofs = 0;
323 }
324
325 /* do the actual data transfer */
326 if (write)
Bartlomiej Zolnierkiewicz9e47be02008-01-26 20:13:09 +0100327 hwif->ata_output_data(drive, buf, SECTOR_WORDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 else
Bartlomiej Zolnierkiewicz9e47be02008-01-26 20:13:09 +0100329 hwif->ata_input_data(drive, buf, SECTOR_WORDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
331 kunmap_atomic(buf, KM_BIO_SRC_IRQ);
332#ifdef CONFIG_HIGHMEM
333 local_irq_restore(flags);
334#endif
335}
336
337static void ide_pio_multi(ide_drive_t *drive, unsigned int write)
338{
339 unsigned int nsect;
340
341 nsect = min_t(unsigned int, drive->hwif->nleft, drive->mult_count);
342 while (nsect--)
343 ide_pio_sector(drive, write);
344}
345
Arjan van de Ven858119e2006-01-14 13:20:43 -0800346static void ide_pio_datablock(ide_drive_t *drive, struct request *rq,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 unsigned int write)
348{
Tejun Heo35cf2b92008-01-26 20:13:10 +0100349 u8 saved_io_32bit = drive->io_32bit;
350
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 if (rq->bio) /* fs request */
352 rq->errors = 0;
353
Tejun Heo35cf2b92008-01-26 20:13:10 +0100354 if (rq->cmd_type == REQ_TYPE_ATA_TASKFILE) {
355 ide_task_t *task = rq->special;
356
357 if (task->tf_flags & IDE_TFLAG_IO_16BIT)
358 drive->io_32bit = 0;
359 }
360
Andrew Morton651c29a2006-02-15 15:17:37 -0800361 touch_softlockup_watchdog();
362
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 switch (drive->hwif->data_phase) {
364 case TASKFILE_MULTI_IN:
365 case TASKFILE_MULTI_OUT:
366 ide_pio_multi(drive, write);
367 break;
368 default:
369 ide_pio_sector(drive, write);
370 break;
371 }
Tejun Heo35cf2b92008-01-26 20:13:10 +0100372
373 drive->io_32bit = saved_io_32bit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374}
375
376static ide_startstop_t task_error(ide_drive_t *drive, struct request *rq,
377 const char *s, u8 stat)
378{
379 if (rq->bio) {
380 ide_hwif_t *hwif = drive->hwif;
381 int sectors = hwif->nsect - hwif->nleft;
382
383 switch (hwif->data_phase) {
384 case TASKFILE_IN:
385 if (hwif->nleft)
386 break;
387 /* fall through */
388 case TASKFILE_OUT:
389 sectors--;
390 break;
391 case TASKFILE_MULTI_IN:
392 if (hwif->nleft)
393 break;
394 /* fall through */
395 case TASKFILE_MULTI_OUT:
396 sectors -= drive->mult_count;
397 default:
398 break;
399 }
400
401 if (sectors > 0) {
402 ide_driver_t *drv;
403
404 drv = *(ide_driver_t **)rq->rq_disk->private_data;
405 drv->end_request(drive, 1, sectors);
406 }
407 }
408 return ide_error(drive, s, stat);
409}
410
Tejun Heo4d7a9842008-01-26 20:13:11 +0100411void task_end_request(ide_drive_t *drive, struct request *rq, u8 stat)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412{
Jens Axboe4aff5e22006-08-10 08:44:47 +0200413 if (rq->cmd_type == REQ_TYPE_ATA_TASKFILE) {
Bartlomiej Zolnierkiewicz64a57fe2008-02-06 02:57:51 +0100414 u8 err = ide_read_error(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
Tejun Heo4d7a9842008-01-26 20:13:11 +0100416 ide_end_drive_cmd(drive, stat, err);
417 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 }
419
Richard Purdie03731fb2006-03-31 02:31:15 -0800420 if (rq->rq_disk) {
421 ide_driver_t *drv;
422
423 drv = *(ide_driver_t **)rq->rq_disk->private_data;;
Bartlomiej Zolnierkiewicz79f21b82008-01-26 20:13:11 +0100424 drv->end_request(drive, 1, rq->nr_sectors);
Richard Purdie03731fb2006-03-31 02:31:15 -0800425 } else
Bartlomiej Zolnierkiewicz79f21b82008-01-26 20:13:11 +0100426 ide_end_request(drive, 1, rq->nr_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427}
428
429/*
Linus Torvalds6c3c3152008-03-18 21:26:24 -0700430 * We got an interrupt on a task_in case, but no errors and no DRQ.
431 *
432 * It might be a spurious irq (shared irq), but it might be a
433 * command that had no output.
434 */
435static ide_startstop_t task_in_unexpected(ide_drive_t *drive, struct request *rq, u8 stat)
436{
437 /* Command all done? */
438 if (OK_STAT(stat, READY_STAT, BUSY_STAT)) {
439 task_end_request(drive, rq, stat);
440 return ide_stopped;
441 }
442
443 /* Assume it was a spurious irq */
444 ide_set_handler(drive, &task_in_intr, WAIT_WORSTCASE, NULL);
445 return ide_started;
446}
447
448/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 * Handler for command with PIO data-in phase (Read/Read Multiple).
450 */
Bartlomiej Zolnierkiewiczf6e29e32008-01-25 22:17:16 +0100451static ide_startstop_t task_in_intr(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452{
453 ide_hwif_t *hwif = drive->hwif;
454 struct request *rq = HWGROUP(drive)->rq;
Bartlomiej Zolnierkiewiczc47137a2008-02-06 02:57:51 +0100455 u8 stat = ide_read_status(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456
Linus Torvalds6c3c3152008-03-18 21:26:24 -0700457 /* Error? */
458 if (stat & ERR_STAT)
Harvey Harrisoneb639632008-04-26 22:25:20 +0200459 return task_error(drive, rq, __func__, stat);
Linus Torvalds6c3c3152008-03-18 21:26:24 -0700460
461 /* Didn't want any data? Odd. */
462 if (!(stat & DRQ_STAT))
463 return task_in_unexpected(drive, rq, stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464
465 ide_pio_datablock(drive, rq, 0);
466
Linus Torvalds6c3c3152008-03-18 21:26:24 -0700467 /* Are we done? Check status and finish transfer. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 if (!hwif->nleft) {
469 stat = wait_drive_not_busy(drive);
Bartlomiej Zolnierkiewicz73d7de02008-01-26 20:13:10 +0100470 if (!OK_STAT(stat, 0, BAD_STAT))
Harvey Harrisoneb639632008-04-26 22:25:20 +0200471 return task_error(drive, rq, __func__, stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 task_end_request(drive, rq, stat);
473 return ide_stopped;
474 }
475
476 /* Still data left to transfer. */
477 ide_set_handler(drive, &task_in_intr, WAIT_WORSTCASE, NULL);
478
479 return ide_started;
480}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481
482/*
483 * Handler for command with PIO data-out phase (Write/Write Multiple).
484 */
485static ide_startstop_t task_out_intr (ide_drive_t *drive)
486{
487 ide_hwif_t *hwif = drive->hwif;
488 struct request *rq = HWGROUP(drive)->rq;
Bartlomiej Zolnierkiewiczc47137a2008-02-06 02:57:51 +0100489 u8 stat = ide_read_status(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490
491 if (!OK_STAT(stat, DRIVE_READY, drive->bad_wstat))
Harvey Harrisoneb639632008-04-26 22:25:20 +0200492 return task_error(drive, rq, __func__, stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493
494 /* Deal with unexpected ATA data phase. */
495 if (((stat & DRQ_STAT) == 0) ^ !hwif->nleft)
Harvey Harrisoneb639632008-04-26 22:25:20 +0200496 return task_error(drive, rq, __func__, stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497
498 if (!hwif->nleft) {
499 task_end_request(drive, rq, stat);
500 return ide_stopped;
501 }
502
503 /* Still data left to transfer. */
504 ide_pio_datablock(drive, rq, 1);
505 ide_set_handler(drive, &task_out_intr, WAIT_WORSTCASE, NULL);
506
507 return ide_started;
508}
509
Bartlomiej Zolnierkiewiczf6e29e32008-01-25 22:17:16 +0100510static ide_startstop_t pre_task_out_intr(ide_drive_t *drive, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511{
512 ide_startstop_t startstop;
513
Bartlomiej Zolnierkiewicz4906f3b2008-01-26 20:13:11 +0100514 if (ide_wait_stat(&startstop, drive, DRQ_STAT,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 drive->bad_wstat, WAIT_DRQ)) {
516 printk(KERN_ERR "%s: no DRQ after issuing %sWRITE%s\n",
517 drive->name,
518 drive->hwif->data_phase ? "MULT" : "",
519 drive->addressing ? "_EXT" : "");
520 return startstop;
521 }
522
523 if (!drive->unmask)
524 local_irq_disable();
525
526 ide_set_handler(drive, &task_out_intr, WAIT_WORSTCASE, NULL);
527 ide_pio_datablock(drive, rq, 1);
528
529 return ide_started;
530}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531
Bartlomiej Zolnierkiewiczac026ff2008-01-25 22:17:14 +0100532int ide_raw_taskfile(ide_drive_t *drive, ide_task_t *task, u8 *buf, u16 nsect)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533{
534 struct request rq;
535
536 memset(&rq, 0, sizeof(rq));
Bartlomiej Zolnierkiewicz02ac2462007-11-05 21:42:27 +0100537 rq.ref_count = 1;
Jens Axboe4aff5e22006-08-10 08:44:47 +0200538 rq.cmd_type = REQ_TYPE_ATA_TASKFILE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 rq.buffer = buf;
540
541 /*
542 * (ks) We transfer currently only whole sectors.
543 * This is suffient for now. But, it would be great,
544 * if we would find a solution to transfer any size.
545 * To support special commands like READ LONG.
546 */
Bartlomiej Zolnierkiewiczac026ff2008-01-25 22:17:14 +0100547 rq.hard_nr_sectors = rq.nr_sectors = nsect;
548 rq.hard_cur_sectors = rq.current_nr_sectors = nsect;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549
Bartlomiej Zolnierkiewiczac026ff2008-01-25 22:17:14 +0100550 if (task->tf_flags & IDE_TFLAG_WRITE)
551 rq.cmd_flags |= REQ_RW;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552
Bartlomiej Zolnierkiewiczac026ff2008-01-25 22:17:14 +0100553 rq.special = task;
554 task->rq = &rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 return ide_do_drive_cmd(drive, &rq, ide_wait);
557}
558
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559EXPORT_SYMBOL(ide_raw_taskfile);
560
Bartlomiej Zolnierkiewicz9a3c49b2008-01-25 22:17:07 +0100561int ide_no_data_taskfile(ide_drive_t *drive, ide_task_t *task)
562{
Bartlomiej Zolnierkiewiczac026ff2008-01-25 22:17:14 +0100563 task->data_phase = TASKFILE_NO_DATA;
Bartlomiej Zolnierkiewicz9a3c49b2008-01-25 22:17:07 +0100564
Bartlomiej Zolnierkiewiczac026ff2008-01-25 22:17:14 +0100565 return ide_raw_taskfile(drive, task, NULL, 0);
Bartlomiej Zolnierkiewicz9a3c49b2008-01-25 22:17:07 +0100566}
567EXPORT_SYMBOL_GPL(ide_no_data_taskfile);
568
Bartlomiej Zolnierkiewicz26a5b042007-11-05 21:42:27 +0100569#ifdef CONFIG_IDE_TASK_IOCTL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570int ide_taskfile_ioctl (ide_drive_t *drive, unsigned int cmd, unsigned long arg)
571{
572 ide_task_request_t *req_task;
573 ide_task_t args;
574 u8 *outbuf = NULL;
575 u8 *inbuf = NULL;
Bartlomiej Zolnierkiewiczac026ff2008-01-25 22:17:14 +0100576 u8 *data_buf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 int err = 0;
578 int tasksize = sizeof(struct ide_task_request_s);
Alan Cox3a42bb22006-10-16 16:31:02 +0100579 unsigned int taskin = 0;
580 unsigned int taskout = 0;
Bartlomiej Zolnierkiewiczac026ff2008-01-25 22:17:14 +0100581 u16 nsect = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 char __user *buf = (char __user *)arg;
583
584// printk("IDE Taskfile ...\n");
585
Deepak Saxenaf5e3c2f2005-11-07 01:01:25 -0800586 req_task = kzalloc(tasksize, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 if (req_task == NULL) return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 if (copy_from_user(req_task, buf, tasksize)) {
589 kfree(req_task);
590 return -EFAULT;
591 }
592
Alan Cox3a42bb22006-10-16 16:31:02 +0100593 taskout = req_task->out_size;
594 taskin = req_task->in_size;
595
596 if (taskin > 65536 || taskout > 65536) {
597 err = -EINVAL;
598 goto abort;
599 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600
601 if (taskout) {
602 int outtotal = tasksize;
Deepak Saxenaf5e3c2f2005-11-07 01:01:25 -0800603 outbuf = kzalloc(taskout, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 if (outbuf == NULL) {
605 err = -ENOMEM;
606 goto abort;
607 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 if (copy_from_user(outbuf, buf + outtotal, taskout)) {
609 err = -EFAULT;
610 goto abort;
611 }
612 }
613
614 if (taskin) {
615 int intotal = tasksize + taskout;
Deepak Saxenaf5e3c2f2005-11-07 01:01:25 -0800616 inbuf = kzalloc(taskin, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 if (inbuf == NULL) {
618 err = -ENOMEM;
619 goto abort;
620 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 if (copy_from_user(inbuf, buf + intotal, taskin)) {
622 err = -EFAULT;
623 goto abort;
624 }
625 }
626
627 memset(&args, 0, sizeof(ide_task_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628
Bartlomiej Zolnierkiewicz650d8412008-01-25 22:17:06 +0100629 memcpy(&args.tf_array[0], req_task->hob_ports, HDIO_DRIVE_HOB_HDR_SIZE - 2);
630 memcpy(&args.tf_array[6], req_task->io_ports, HDIO_DRIVE_TASK_HDR_SIZE);
Bartlomiej Zolnierkiewicz866e2ec2008-01-25 22:17:14 +0100631
632 args.data_phase = req_task->data_phase;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633
Bartlomiej Zolnierkiewicz657cc1a2008-01-26 20:13:10 +0100634 args.tf_flags = IDE_TFLAG_IO_16BIT | IDE_TFLAG_DEVICE |
635 IDE_TFLAG_IN_TF;
Bartlomiej Zolnierkiewicza3bbb9d2008-01-25 22:17:10 +0100636 if (drive->addressing == 1)
Bartlomiej Zolnierkiewicz657cc1a2008-01-26 20:13:10 +0100637 args.tf_flags |= (IDE_TFLAG_LBA48 | IDE_TFLAG_IN_HOB);
Bartlomiej Zolnierkiewicza3bbb9d2008-01-25 22:17:10 +0100638
Bartlomiej Zolnierkiewicz74095a92008-01-25 22:17:07 +0100639 if (req_task->out_flags.all) {
640 args.tf_flags |= IDE_TFLAG_FLAGGED;
641
642 if (req_task->out_flags.b.data)
643 args.tf_flags |= IDE_TFLAG_OUT_DATA;
644
645 if (req_task->out_flags.b.nsector_hob)
646 args.tf_flags |= IDE_TFLAG_OUT_HOB_NSECT;
647 if (req_task->out_flags.b.sector_hob)
648 args.tf_flags |= IDE_TFLAG_OUT_HOB_LBAL;
649 if (req_task->out_flags.b.lcyl_hob)
650 args.tf_flags |= IDE_TFLAG_OUT_HOB_LBAM;
651 if (req_task->out_flags.b.hcyl_hob)
652 args.tf_flags |= IDE_TFLAG_OUT_HOB_LBAH;
653
654 if (req_task->out_flags.b.error_feature)
655 args.tf_flags |= IDE_TFLAG_OUT_FEATURE;
656 if (req_task->out_flags.b.nsector)
657 args.tf_flags |= IDE_TFLAG_OUT_NSECT;
658 if (req_task->out_flags.b.sector)
659 args.tf_flags |= IDE_TFLAG_OUT_LBAL;
660 if (req_task->out_flags.b.lcyl)
661 args.tf_flags |= IDE_TFLAG_OUT_LBAM;
662 if (req_task->out_flags.b.hcyl)
663 args.tf_flags |= IDE_TFLAG_OUT_LBAH;
Bartlomiej Zolnierkiewicza3bbb9d2008-01-25 22:17:10 +0100664 } else {
665 args.tf_flags |= IDE_TFLAG_OUT_TF;
666 if (args.tf_flags & IDE_TFLAG_LBA48)
667 args.tf_flags |= IDE_TFLAG_OUT_HOB;
Bartlomiej Zolnierkiewicz74095a92008-01-25 22:17:07 +0100668 }
669
Bartlomiej Zolnierkiewicz866e2ec2008-01-25 22:17:14 +0100670 if (req_task->in_flags.b.data)
671 args.tf_flags |= IDE_TFLAG_IN_DATA;
672
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 switch(req_task->data_phase) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 case TASKFILE_MULTI_OUT:
675 if (!drive->mult_count) {
676 /* (hs): give up if multcount is not set */
677 printk(KERN_ERR "%s: %s Multimode Write " \
678 "multcount is not set\n",
Harvey Harrisoneb639632008-04-26 22:25:20 +0200679 drive->name, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 err = -EPERM;
681 goto abort;
682 }
683 /* fall through */
684 case TASKFILE_OUT:
Bartlomiej Zolnierkiewiczac026ff2008-01-25 22:17:14 +0100685 /* fall through */
686 case TASKFILE_OUT_DMAQ:
687 case TASKFILE_OUT_DMA:
688 nsect = taskout / SECTOR_SIZE;
689 data_buf = outbuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 break;
691 case TASKFILE_MULTI_IN:
692 if (!drive->mult_count) {
693 /* (hs): give up if multcount is not set */
694 printk(KERN_ERR "%s: %s Multimode Read failure " \
695 "multcount is not set\n",
Harvey Harrisoneb639632008-04-26 22:25:20 +0200696 drive->name, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 err = -EPERM;
698 goto abort;
699 }
700 /* fall through */
701 case TASKFILE_IN:
Bartlomiej Zolnierkiewiczac026ff2008-01-25 22:17:14 +0100702 /* fall through */
703 case TASKFILE_IN_DMAQ:
704 case TASKFILE_IN_DMA:
705 nsect = taskin / SECTOR_SIZE;
706 data_buf = inbuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 break;
708 case TASKFILE_NO_DATA:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 break;
710 default:
711 err = -EFAULT;
712 goto abort;
713 }
714
Bartlomiej Zolnierkiewiczac026ff2008-01-25 22:17:14 +0100715 if (req_task->req_cmd == IDE_DRIVE_TASK_NO_DATA)
716 nsect = 0;
717 else if (!nsect) {
718 nsect = (args.tf.hob_nsect << 8) | args.tf.nsect;
719
720 if (!nsect) {
721 printk(KERN_ERR "%s: in/out command without data\n",
722 drive->name);
723 err = -EFAULT;
724 goto abort;
725 }
726 }
727
728 if (req_task->req_cmd == IDE_DRIVE_TASK_RAW_WRITE)
729 args.tf_flags |= IDE_TFLAG_WRITE;
730
731 err = ide_raw_taskfile(drive, &args, data_buf, nsect);
732
Bartlomiej Zolnierkiewicz650d8412008-01-25 22:17:06 +0100733 memcpy(req_task->hob_ports, &args.tf_array[0], HDIO_DRIVE_HOB_HDR_SIZE - 2);
734 memcpy(req_task->io_ports, &args.tf_array[6], HDIO_DRIVE_TASK_HDR_SIZE);
Bartlomiej Zolnierkiewicz866e2ec2008-01-25 22:17:14 +0100735
736 if ((args.tf_flags & IDE_TFLAG_FLAGGED_SET_IN_FLAGS) &&
737 req_task->in_flags.all == 0) {
738 req_task->in_flags.all = IDE_TASKFILE_STD_IN_FLAGS;
739 if (drive->addressing == 1)
740 req_task->in_flags.all |= (IDE_HOB_STD_IN_FLAGS << 8);
741 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742
743 if (copy_to_user(buf, req_task, tasksize)) {
744 err = -EFAULT;
745 goto abort;
746 }
747 if (taskout) {
748 int outtotal = tasksize;
749 if (copy_to_user(buf + outtotal, outbuf, taskout)) {
750 err = -EFAULT;
751 goto abort;
752 }
753 }
754 if (taskin) {
755 int intotal = tasksize + taskout;
756 if (copy_to_user(buf + intotal, inbuf, taskin)) {
757 err = -EFAULT;
758 goto abort;
759 }
760 }
761abort:
762 kfree(req_task);
Jesper Juhl6044ec82005-11-07 01:01:32 -0800763 kfree(outbuf);
764 kfree(inbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765
766// printk("IDE Taskfile ioctl ended. rc = %i\n", err);
767
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 return err;
769}
Bartlomiej Zolnierkiewicz26a5b042007-11-05 21:42:27 +0100770#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772int ide_cmd_ioctl (ide_drive_t *drive, unsigned int cmd, unsigned long arg)
773{
Bartlomiej Zolnierkiewicz5a9e77a2008-01-26 20:13:13 +0100774 u8 *buf = NULL;
775 int bufsize = 0, err = 0;
776 u8 args[4], xfer_rate = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 ide_task_t tfargs;
Bartlomiej Zolnierkiewicz650d8412008-01-25 22:17:06 +0100778 struct ide_taskfile *tf = &tfargs.tf;
Bartlomiej Zolnierkiewicz5efe7c52008-02-02 19:56:46 +0100779 struct hd_driveid *id = drive->id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780
781 if (NULL == (void *) arg) {
782 struct request rq;
Bartlomiej Zolnierkiewicz145b75e2008-01-26 20:13:11 +0100783
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 ide_init_drive_cmd(&rq);
Bartlomiej Zolnierkiewicz852738f2008-01-26 20:13:12 +0100785 rq.cmd_type = REQ_TYPE_ATA_TASKFILE;
Bartlomiej Zolnierkiewicz145b75e2008-01-26 20:13:11 +0100786
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 return ide_do_drive_cmd(drive, &rq, ide_wait);
788 }
789
790 if (copy_from_user(args, (void __user *)arg, 4))
791 return -EFAULT;
792
793 memset(&tfargs, 0, sizeof(ide_task_t));
Bartlomiej Zolnierkiewicz650d8412008-01-25 22:17:06 +0100794 tf->feature = args[2];
Bartlomiej Zolnierkiewicz5a9e77a2008-01-26 20:13:13 +0100795 if (args[0] == WIN_SMART) {
796 tf->nsect = args[3];
797 tf->lbal = args[1];
798 tf->lbam = 0x4f;
799 tf->lbah = 0xc2;
800 tfargs.tf_flags = IDE_TFLAG_OUT_TF | IDE_TFLAG_IN_NSECT;
801 } else {
802 tf->nsect = args[1];
803 tfargs.tf_flags = IDE_TFLAG_OUT_FEATURE |
804 IDE_TFLAG_OUT_NSECT | IDE_TFLAG_IN_NSECT;
805 }
Bartlomiej Zolnierkiewicz650d8412008-01-25 22:17:06 +0100806 tf->command = args[0];
Bartlomiej Zolnierkiewicz5a9e77a2008-01-26 20:13:13 +0100807 tfargs.data_phase = args[3] ? TASKFILE_IN : TASKFILE_NO_DATA;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808
809 if (args[3]) {
Bartlomiej Zolnierkiewicz5a9e77a2008-01-26 20:13:13 +0100810 tfargs.tf_flags |= IDE_TFLAG_IO_16BIT;
811 bufsize = SECTOR_WORDS * 4 * args[3];
812 buf = kzalloc(bufsize, GFP_KERNEL);
813 if (buf == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 }
Bartlomiej Zolnierkiewicz5a9e77a2008-01-26 20:13:13 +0100816
Bartlomiej Zolnierkiewicz5efe7c52008-02-02 19:56:46 +0100817 if (tf->command == WIN_SETFEATURES &&
818 tf->feature == SETFEATURES_XFER &&
819 tf->nsect >= XFER_SW_DMA_0 &&
820 (id->dma_ultra || id->dma_mword || id->dma_1word)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 xfer_rate = args[1];
Bartlomiej Zolnierkiewiczaf10f772008-02-02 19:56:46 +0100822 if (tf->nsect > XFER_UDMA_2 && !eighty_ninty_three(drive)) {
823 printk(KERN_WARNING "%s: UDMA speeds >UDMA33 cannot "
824 "be set\n", drive->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 goto abort;
Bartlomiej Zolnierkiewiczaf10f772008-02-02 19:56:46 +0100826 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 }
828
Bartlomiej Zolnierkiewicz5a9e77a2008-01-26 20:13:13 +0100829 err = ide_raw_taskfile(drive, &tfargs, buf, args[3]);
830
831 args[0] = tf->status;
832 args[1] = tf->error;
833 args[2] = tf->nsect;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834
835 if (!err && xfer_rate) {
836 /* active-retuning-calls future */
837 ide_set_xfer_rate(drive, xfer_rate);
838 ide_driveid_update(drive);
839 }
840abort:
Bartlomiej Zolnierkiewicz5a9e77a2008-01-26 20:13:13 +0100841 if (copy_to_user((void __user *)arg, &args, 4))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 err = -EFAULT;
Bartlomiej Zolnierkiewicz5a9e77a2008-01-26 20:13:13 +0100843 if (buf) {
844 if (copy_to_user((void __user *)(arg + 4), buf, bufsize))
845 err = -EFAULT;
846 kfree(buf);
847 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 return err;
849}
850
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851int ide_task_ioctl (ide_drive_t *drive, unsigned int cmd, unsigned long arg)
852{
853 void __user *p = (void __user *)arg;
854 int err = 0;
Bartlomiej Zolnierkiewicz14b89ef2008-01-25 22:17:11 +0100855 u8 args[7];
856 ide_task_t task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857
858 if (copy_from_user(args, p, 7))
859 return -EFAULT;
Bartlomiej Zolnierkiewicz14b89ef2008-01-25 22:17:11 +0100860
861 memset(&task, 0, sizeof(task));
862 memcpy(&task.tf_array[7], &args[1], 6);
863 task.tf.command = args[0];
Bartlomiej Zolnierkiewicz657cc1a2008-01-26 20:13:10 +0100864 task.tf_flags = IDE_TFLAG_TF | IDE_TFLAG_DEVICE;
Bartlomiej Zolnierkiewicz14b89ef2008-01-25 22:17:11 +0100865
866 err = ide_no_data_taskfile(drive, &task);
867
868 args[0] = task.tf.command;
869 memcpy(&args[1], &task.tf_array[7], 6);
870
871 if (copy_to_user(p, args, 7))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 err = -EFAULT;
Bartlomiej Zolnierkiewicz14b89ef2008-01-25 22:17:11 +0100873
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 return err;
875}