blob: 1f664ea57e5846966a409e2e68badcb1ff291138 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/drivers/ide/ide-taskfile.c Version 0.38 March 05, 2003
3 *
4 * Copyright (C) 2000-2002 Michael Cornwell <cornwell@acm.org>
5 * Copyright (C) 2000-2002 Andre Hedrick <andre@linux-ide.org>
6 * Copyright (C) 2001-2002 Klaus Smolin
7 * IBM Storage Technology Division
8 * Copyright (C) 2003-2004 Bartlomiej Zolnierkiewicz
9 *
10 * The big the bad and the ugly.
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 */
12
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/module.h>
14#include <linux/types.h>
15#include <linux/string.h>
16#include <linux/kernel.h>
17#include <linux/timer.h>
18#include <linux/mm.h>
Andrew Morton651c29a2006-02-15 15:17:37 -080019#include <linux/sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/interrupt.h>
21#include <linux/major.h>
22#include <linux/errno.h>
23#include <linux/genhd.h>
24#include <linux/blkpg.h>
25#include <linux/slab.h>
26#include <linux/pci.h>
27#include <linux/delay.h>
28#include <linux/hdreg.h>
29#include <linux/ide.h>
30#include <linux/bitops.h>
Jens Axboe55c16a72007-07-25 08:13:56 +020031#include <linux/scatterlist.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
33#include <asm/byteorder.h>
34#include <asm/irq.h>
35#include <asm/uaccess.h>
36#include <asm/io.h>
37
Bartlomiej Zolnierkiewicz9e422372008-01-25 22:17:07 +010038void ide_tf_load(ide_drive_t *drive, ide_task_t *task)
39{
40 ide_hwif_t *hwif = drive->hwif;
41 struct ide_taskfile *tf = &task->tf;
42 u8 HIHI = (task->tf_flags & IDE_TFLAG_LBA48) ? 0xE0 : 0xEF;
43
Bartlomiej Zolnierkiewicz74095a92008-01-25 22:17:07 +010044 if (task->tf_flags & IDE_TFLAG_FLAGGED)
45 HIHI = 0xFF;
46
Bartlomiej Zolnierkiewicz807e35d2008-01-25 22:17:10 +010047#ifdef DEBUG
48 printk("%s: tf: feat 0x%02x nsect 0x%02x lbal 0x%02x "
49 "lbam 0x%02x lbah 0x%02x dev 0x%02x cmd 0x%02x\n",
50 drive->name, tf->feature, tf->nsect, tf->lbal,
51 tf->lbam, tf->lbah, tf->device, tf->command);
Bartlomiej Zolnierkiewicz6dd9b832008-01-26 20:13:03 +010052 printk("%s: hob: nsect 0x%02x lbal 0x%02x "
53 "lbam 0x%02x lbah 0x%02x\n",
54 drive->name, tf->hob_nsect, tf->hob_lbal,
55 tf->hob_lbam, tf->hob_lbah);
Bartlomiej Zolnierkiewicz807e35d2008-01-25 22:17:10 +010056#endif
57
Bartlomiej Zolnierkiewicz81ca6912008-01-26 20:13:08 +010058 ide_set_irq(drive, 1);
Bartlomiej Zolnierkiewicz9e422372008-01-25 22:17:07 +010059
60 if ((task->tf_flags & IDE_TFLAG_NO_SELECT_MASK) == 0)
61 SELECT_MASK(drive, 0);
62
Bartlomiej Zolnierkiewicz74095a92008-01-25 22:17:07 +010063 if (task->tf_flags & IDE_TFLAG_OUT_DATA)
64 hwif->OUTW((tf->hob_data << 8) | tf->data, IDE_DATA_REG);
Bartlomiej Zolnierkiewicz9e422372008-01-25 22:17:07 +010065
Bartlomiej Zolnierkiewicz74095a92008-01-25 22:17:07 +010066 if (task->tf_flags & IDE_TFLAG_OUT_HOB_FEATURE)
67 hwif->OUTB(tf->hob_feature, IDE_FEATURE_REG);
68 if (task->tf_flags & IDE_TFLAG_OUT_HOB_NSECT)
69 hwif->OUTB(tf->hob_nsect, IDE_NSECTOR_REG);
70 if (task->tf_flags & IDE_TFLAG_OUT_HOB_LBAL)
71 hwif->OUTB(tf->hob_lbal, IDE_SECTOR_REG);
72 if (task->tf_flags & IDE_TFLAG_OUT_HOB_LBAM)
73 hwif->OUTB(tf->hob_lbam, IDE_LCYL_REG);
74 if (task->tf_flags & IDE_TFLAG_OUT_HOB_LBAH)
75 hwif->OUTB(tf->hob_lbah, IDE_HCYL_REG);
76
77 if (task->tf_flags & IDE_TFLAG_OUT_FEATURE)
78 hwif->OUTB(tf->feature, IDE_FEATURE_REG);
79 if (task->tf_flags & IDE_TFLAG_OUT_NSECT)
80 hwif->OUTB(tf->nsect, IDE_NSECTOR_REG);
81 if (task->tf_flags & IDE_TFLAG_OUT_LBAL)
82 hwif->OUTB(tf->lbal, IDE_SECTOR_REG);
83 if (task->tf_flags & IDE_TFLAG_OUT_LBAM)
84 hwif->OUTB(tf->lbam, IDE_LCYL_REG);
85 if (task->tf_flags & IDE_TFLAG_OUT_LBAH)
86 hwif->OUTB(tf->lbah, IDE_HCYL_REG);
Bartlomiej Zolnierkiewicz9e422372008-01-25 22:17:07 +010087
Bartlomiej Zolnierkiewicz807e35d2008-01-25 22:17:10 +010088 if (task->tf_flags & IDE_TFLAG_OUT_DEVICE)
89 hwif->OUTB((tf->device & HIHI) | drive->select.all, IDE_SELECT_REG);
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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
Bartlomiej Zolnierkiewicz1edee602008-01-25 22:17:15 +0100139 if (task->data_phase == TASKFILE_MULTI_IN ||
140 task->data_phase == TASKFILE_MULTI_OUT) {
141 if (!drive->mult_count) {
142 printk(KERN_ERR "%s: multimode not set!\n",
143 drive->name);
144 return ide_stopped;
145 }
146 }
147
148 if (task->tf_flags & IDE_TFLAG_FLAGGED)
149 task->tf_flags |= IDE_TFLAG_FLAGGED_SET_IN_FLAGS;
150
Bartlomiej Zolnierkiewiczf6e29e32008-01-25 22:17:16 +0100151 if ((task->tf_flags & IDE_TFLAG_DMA_PIO_FALLBACK) == 0)
152 ide_tf_load(drive, task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
Bartlomiej Zolnierkiewicz10d90152008-01-25 22:17:16 +0100154 switch (task->data_phase) {
155 case TASKFILE_MULTI_OUT:
156 case TASKFILE_OUT:
157 hwif->OUTBSYNC(drive, tf->command, IDE_COMMAND_REG);
158 ndelay(400); /* FIXME */
159 return pre_task_out_intr(drive, task->rq);
160 case TASKFILE_MULTI_IN:
161 case TASKFILE_IN:
Bartlomiej Zolnierkiewicz57d73662008-01-25 22:17:16 +0100162 handler = task_in_intr;
Bartlomiej Zolnierkiewicz1192e522008-01-25 22:17:16 +0100163 /* fall-through */
Bartlomiej Zolnierkiewicz10d90152008-01-25 22:17:16 +0100164 case TASKFILE_NO_DATA:
Bartlomiej Zolnierkiewicz57d73662008-01-25 22:17:16 +0100165 if (handler == NULL)
166 handler = task_no_data_intr;
Bartlomiej Zolnierkiewicz1192e522008-01-25 22:17:16 +0100167 /* WIN_{SPECIFY,RESTORE,SETMULT} use custom handlers */
Bartlomiej Zolnierkiewicz57d73662008-01-25 22:17:16 +0100168 if (task->tf_flags & IDE_TFLAG_CUSTOM_HANDLER) {
169 switch (tf->command) {
170 case WIN_SPECIFY: handler = set_geometry_intr; break;
171 case WIN_RESTORE: handler = recal_intr; break;
172 case WIN_SETMULT: handler = set_multmode_intr; break;
173 }
174 }
175 ide_execute_command(drive, tf->command, handler,
176 WAIT_WORSTCASE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 return ide_started;
Bartlomiej Zolnierkiewicz10d90152008-01-25 22:17:16 +0100178 default:
179 if (task_dma_ok(task) == 0 || drive->using_dma == 0 ||
180 hwif->dma_setup(drive))
181 return ide_stopped;
Bartlomiej Zolnierkiewicz74095a92008-01-25 22:17:07 +0100182 hwif->dma_exec_cmd(drive, tf->command);
183 hwif->dma_start(drive);
184 return ide_started;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186}
Bartlomiej Zolnierkiewiczf6e29e32008-01-25 22:17:16 +0100187EXPORT_SYMBOL_GPL(do_rw_taskfile);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189/*
190 * set_multmode_intr() is invoked on completion of a WIN_SETMULT cmd.
191 */
Bartlomiej Zolnierkiewicz57d73662008-01-25 22:17:16 +0100192static ide_startstop_t set_multmode_intr(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193{
194 ide_hwif_t *hwif = HWIF(drive);
195 u8 stat;
196
197 if (OK_STAT(stat = hwif->INB(IDE_STATUS_REG),READY_STAT,BAD_STAT)) {
198 drive->mult_count = drive->mult_req;
199 } else {
200 drive->mult_req = drive->mult_count = 0;
201 drive->special.b.recalibrate = 1;
202 (void) ide_dump_status(drive, "set_multmode", stat);
203 }
204 return ide_stopped;
205}
206
207/*
208 * set_geometry_intr() is invoked on completion of a WIN_SPECIFY cmd.
209 */
Bartlomiej Zolnierkiewicz57d73662008-01-25 22:17:16 +0100210static ide_startstop_t set_geometry_intr(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211{
212 ide_hwif_t *hwif = HWIF(drive);
213 int retries = 5;
214 u8 stat;
215
216 while (((stat = hwif->INB(IDE_STATUS_REG)) & BUSY_STAT) && retries--)
217 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{
235 ide_hwif_t *hwif = HWIF(drive);
236 u8 stat;
237
238 if (!OK_STAT(stat = hwif->INB(IDE_STATUS_REG), READY_STAT, BAD_STAT))
239 return ide_error(drive, "recal_intr", stat);
240 return ide_stopped;
241}
242
243/*
244 * Handler for commands without a data phase
245 */
Bartlomiej Zolnierkiewicz1192e522008-01-25 22:17:16 +0100246static ide_startstop_t task_no_data_intr(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247{
248 ide_task_t *args = HWGROUP(drive)->rq->special;
249 ide_hwif_t *hwif = HWIF(drive);
250 u8 stat;
251
Ingo Molnar366c7f52006-07-03 00:25:25 -0700252 local_irq_enable_in_hardirq();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 if (!OK_STAT(stat = hwif->INB(IDE_STATUS_REG),READY_STAT,BAD_STAT)) {
254 return ide_error(drive, "task_no_data_intr", stat);
255 /* calls ide_end_drive_cmd */
256 }
257 if (args)
258 ide_end_drive_cmd(drive, stat, hwif->INB(IDE_ERROR_REG));
259
260 return ide_stopped;
261}
262
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263static u8 wait_drive_not_busy(ide_drive_t *drive)
264{
265 ide_hwif_t *hwif = HWIF(drive);
Masatake YAMATOb42fa132007-07-03 22:28:34 +0200266 int retries;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 u8 stat;
268
269 /*
270 * Last sector was transfered, wait until drive is ready.
271 * This can take up to 10 usec, but we will wait max 1 ms
272 * (drive_cmd_intr() waits that long).
273 */
Masatake YAMATOb42fa132007-07-03 22:28:34 +0200274 for (retries = 0; retries < 100; retries++) {
275 if ((stat = hwif->INB(IDE_STATUS_REG)) & BUSY_STAT)
276 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
411static void task_end_request(ide_drive_t *drive, struct request *rq, u8 stat)
412{
Jens Axboe55c16a72007-07-25 08:13:56 +0200413 HWIF(drive)->cursg = NULL;
414
Jens Axboe4aff5e22006-08-10 08:44:47 +0200415 if (rq->cmd_type == REQ_TYPE_ATA_TASKFILE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 ide_task_t *task = rq->special;
417
Bartlomiej Zolnierkiewicz74095a92008-01-25 22:17:07 +0100418 if (task->tf_flags & IDE_TFLAG_FLAGGED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 u8 err = drive->hwif->INB(IDE_ERROR_REG);
420 ide_end_drive_cmd(drive, stat, err);
421 return;
422 }
423 }
424
Richard Purdie03731fb2006-03-31 02:31:15 -0800425 if (rq->rq_disk) {
426 ide_driver_t *drv;
427
428 drv = *(ide_driver_t **)rq->rq_disk->private_data;;
429 drv->end_request(drive, 1, rq->hard_nr_sectors);
430 } else
431 ide_end_request(drive, 1, rq->hard_nr_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432}
433
434/*
435 * Handler for command with PIO data-in phase (Read/Read Multiple).
436 */
Bartlomiej Zolnierkiewiczf6e29e32008-01-25 22:17:16 +0100437static ide_startstop_t task_in_intr(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438{
439 ide_hwif_t *hwif = drive->hwif;
440 struct request *rq = HWGROUP(drive)->rq;
441 u8 stat = hwif->INB(IDE_STATUS_REG);
442
443 /* new way for dealing with premature shared PCI interrupts */
444 if (!OK_STAT(stat, DATA_READY, BAD_R_STAT)) {
445 if (stat & (ERR_STAT | DRQ_STAT))
446 return task_error(drive, rq, __FUNCTION__, stat);
447 /* No data yet, so wait for another IRQ. */
448 ide_set_handler(drive, &task_in_intr, WAIT_WORSTCASE, NULL);
449 return ide_started;
450 }
451
452 ide_pio_datablock(drive, rq, 0);
453
454 /* If it was the last datablock check status and finish transfer. */
455 if (!hwif->nleft) {
456 stat = wait_drive_not_busy(drive);
Bartlomiej Zolnierkiewicz73d7de02008-01-26 20:13:10 +0100457 if (!OK_STAT(stat, 0, BAD_STAT))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 return task_error(drive, rq, __FUNCTION__, stat);
459 task_end_request(drive, rq, stat);
460 return ide_stopped;
461 }
462
463 /* Still data left to transfer. */
464 ide_set_handler(drive, &task_in_intr, WAIT_WORSTCASE, NULL);
465
466 return ide_started;
467}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468
469/*
470 * Handler for command with PIO data-out phase (Write/Write Multiple).
471 */
472static ide_startstop_t task_out_intr (ide_drive_t *drive)
473{
474 ide_hwif_t *hwif = drive->hwif;
475 struct request *rq = HWGROUP(drive)->rq;
476 u8 stat = hwif->INB(IDE_STATUS_REG);
477
478 if (!OK_STAT(stat, DRIVE_READY, drive->bad_wstat))
479 return task_error(drive, rq, __FUNCTION__, stat);
480
481 /* Deal with unexpected ATA data phase. */
482 if (((stat & DRQ_STAT) == 0) ^ !hwif->nleft)
483 return task_error(drive, rq, __FUNCTION__, stat);
484
485 if (!hwif->nleft) {
486 task_end_request(drive, rq, stat);
487 return ide_stopped;
488 }
489
490 /* Still data left to transfer. */
491 ide_pio_datablock(drive, rq, 1);
492 ide_set_handler(drive, &task_out_intr, WAIT_WORSTCASE, NULL);
493
494 return ide_started;
495}
496
Bartlomiej Zolnierkiewiczf6e29e32008-01-25 22:17:16 +0100497static ide_startstop_t pre_task_out_intr(ide_drive_t *drive, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498{
499 ide_startstop_t startstop;
500
501 if (ide_wait_stat(&startstop, drive, DATA_READY,
502 drive->bad_wstat, WAIT_DRQ)) {
503 printk(KERN_ERR "%s: no DRQ after issuing %sWRITE%s\n",
504 drive->name,
505 drive->hwif->data_phase ? "MULT" : "",
506 drive->addressing ? "_EXT" : "");
507 return startstop;
508 }
509
510 if (!drive->unmask)
511 local_irq_disable();
512
513 ide_set_handler(drive, &task_out_intr, WAIT_WORSTCASE, NULL);
514 ide_pio_datablock(drive, rq, 1);
515
516 return ide_started;
517}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518
Bartlomiej Zolnierkiewiczac026ff2008-01-25 22:17:14 +0100519int ide_raw_taskfile(ide_drive_t *drive, ide_task_t *task, u8 *buf, u16 nsect)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520{
521 struct request rq;
522
523 memset(&rq, 0, sizeof(rq));
Bartlomiej Zolnierkiewicz02ac2462007-11-05 21:42:27 +0100524 rq.ref_count = 1;
Jens Axboe4aff5e22006-08-10 08:44:47 +0200525 rq.cmd_type = REQ_TYPE_ATA_TASKFILE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 rq.buffer = buf;
527
528 /*
529 * (ks) We transfer currently only whole sectors.
530 * This is suffient for now. But, it would be great,
531 * if we would find a solution to transfer any size.
532 * To support special commands like READ LONG.
533 */
Bartlomiej Zolnierkiewiczac026ff2008-01-25 22:17:14 +0100534 rq.hard_nr_sectors = rq.nr_sectors = nsect;
535 rq.hard_cur_sectors = rq.current_nr_sectors = nsect;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536
Bartlomiej Zolnierkiewiczac026ff2008-01-25 22:17:14 +0100537 if (task->tf_flags & IDE_TFLAG_WRITE)
538 rq.cmd_flags |= REQ_RW;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539
Bartlomiej Zolnierkiewiczac026ff2008-01-25 22:17:14 +0100540 rq.special = task;
541 task->rq = &rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 return ide_do_drive_cmd(drive, &rq, ide_wait);
544}
545
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546EXPORT_SYMBOL(ide_raw_taskfile);
547
Bartlomiej Zolnierkiewicz9a3c49b2008-01-25 22:17:07 +0100548int ide_no_data_taskfile(ide_drive_t *drive, ide_task_t *task)
549{
Bartlomiej Zolnierkiewiczac026ff2008-01-25 22:17:14 +0100550 task->data_phase = TASKFILE_NO_DATA;
Bartlomiej Zolnierkiewicz9a3c49b2008-01-25 22:17:07 +0100551
Bartlomiej Zolnierkiewiczac026ff2008-01-25 22:17:14 +0100552 return ide_raw_taskfile(drive, task, NULL, 0);
Bartlomiej Zolnierkiewicz9a3c49b2008-01-25 22:17:07 +0100553}
554EXPORT_SYMBOL_GPL(ide_no_data_taskfile);
555
Bartlomiej Zolnierkiewicz26a5b042007-11-05 21:42:27 +0100556#ifdef CONFIG_IDE_TASK_IOCTL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557int ide_taskfile_ioctl (ide_drive_t *drive, unsigned int cmd, unsigned long arg)
558{
559 ide_task_request_t *req_task;
560 ide_task_t args;
561 u8 *outbuf = NULL;
562 u8 *inbuf = NULL;
Bartlomiej Zolnierkiewiczac026ff2008-01-25 22:17:14 +0100563 u8 *data_buf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 int err = 0;
565 int tasksize = sizeof(struct ide_task_request_s);
Alan Cox3a42bb22006-10-16 16:31:02 +0100566 unsigned int taskin = 0;
567 unsigned int taskout = 0;
Bartlomiej Zolnierkiewiczac026ff2008-01-25 22:17:14 +0100568 u16 nsect = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 char __user *buf = (char __user *)arg;
570
571// printk("IDE Taskfile ...\n");
572
Deepak Saxenaf5e3c2f2005-11-07 01:01:25 -0800573 req_task = kzalloc(tasksize, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 if (req_task == NULL) return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 if (copy_from_user(req_task, buf, tasksize)) {
576 kfree(req_task);
577 return -EFAULT;
578 }
579
Alan Cox3a42bb22006-10-16 16:31:02 +0100580 taskout = req_task->out_size;
581 taskin = req_task->in_size;
582
583 if (taskin > 65536 || taskout > 65536) {
584 err = -EINVAL;
585 goto abort;
586 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587
588 if (taskout) {
589 int outtotal = tasksize;
Deepak Saxenaf5e3c2f2005-11-07 01:01:25 -0800590 outbuf = kzalloc(taskout, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 if (outbuf == NULL) {
592 err = -ENOMEM;
593 goto abort;
594 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 if (copy_from_user(outbuf, buf + outtotal, taskout)) {
596 err = -EFAULT;
597 goto abort;
598 }
599 }
600
601 if (taskin) {
602 int intotal = tasksize + taskout;
Deepak Saxenaf5e3c2f2005-11-07 01:01:25 -0800603 inbuf = kzalloc(taskin, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 if (inbuf == NULL) {
605 err = -ENOMEM;
606 goto abort;
607 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 if (copy_from_user(inbuf, buf + intotal, taskin)) {
609 err = -EFAULT;
610 goto abort;
611 }
612 }
613
614 memset(&args, 0, sizeof(ide_task_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615
Bartlomiej Zolnierkiewicz650d8412008-01-25 22:17:06 +0100616 memcpy(&args.tf_array[0], req_task->hob_ports, HDIO_DRIVE_HOB_HDR_SIZE - 2);
617 memcpy(&args.tf_array[6], req_task->io_ports, HDIO_DRIVE_TASK_HDR_SIZE);
Bartlomiej Zolnierkiewicz866e2ec2008-01-25 22:17:14 +0100618
619 args.data_phase = req_task->data_phase;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620
Bartlomiej Zolnierkiewicz657cc1a2008-01-26 20:13:10 +0100621 args.tf_flags = IDE_TFLAG_IO_16BIT | IDE_TFLAG_DEVICE |
622 IDE_TFLAG_IN_TF;
Bartlomiej Zolnierkiewicza3bbb9d2008-01-25 22:17:10 +0100623 if (drive->addressing == 1)
Bartlomiej Zolnierkiewicz657cc1a2008-01-26 20:13:10 +0100624 args.tf_flags |= (IDE_TFLAG_LBA48 | IDE_TFLAG_IN_HOB);
Bartlomiej Zolnierkiewicza3bbb9d2008-01-25 22:17:10 +0100625
Bartlomiej Zolnierkiewicz74095a92008-01-25 22:17:07 +0100626 if (req_task->out_flags.all) {
627 args.tf_flags |= IDE_TFLAG_FLAGGED;
628
629 if (req_task->out_flags.b.data)
630 args.tf_flags |= IDE_TFLAG_OUT_DATA;
631
632 if (req_task->out_flags.b.nsector_hob)
633 args.tf_flags |= IDE_TFLAG_OUT_HOB_NSECT;
634 if (req_task->out_flags.b.sector_hob)
635 args.tf_flags |= IDE_TFLAG_OUT_HOB_LBAL;
636 if (req_task->out_flags.b.lcyl_hob)
637 args.tf_flags |= IDE_TFLAG_OUT_HOB_LBAM;
638 if (req_task->out_flags.b.hcyl_hob)
639 args.tf_flags |= IDE_TFLAG_OUT_HOB_LBAH;
640
641 if (req_task->out_flags.b.error_feature)
642 args.tf_flags |= IDE_TFLAG_OUT_FEATURE;
643 if (req_task->out_flags.b.nsector)
644 args.tf_flags |= IDE_TFLAG_OUT_NSECT;
645 if (req_task->out_flags.b.sector)
646 args.tf_flags |= IDE_TFLAG_OUT_LBAL;
647 if (req_task->out_flags.b.lcyl)
648 args.tf_flags |= IDE_TFLAG_OUT_LBAM;
649 if (req_task->out_flags.b.hcyl)
650 args.tf_flags |= IDE_TFLAG_OUT_LBAH;
Bartlomiej Zolnierkiewicza3bbb9d2008-01-25 22:17:10 +0100651 } else {
652 args.tf_flags |= IDE_TFLAG_OUT_TF;
653 if (args.tf_flags & IDE_TFLAG_LBA48)
654 args.tf_flags |= IDE_TFLAG_OUT_HOB;
Bartlomiej Zolnierkiewicz74095a92008-01-25 22:17:07 +0100655 }
656
Bartlomiej Zolnierkiewicz866e2ec2008-01-25 22:17:14 +0100657 if (req_task->in_flags.b.data)
658 args.tf_flags |= IDE_TFLAG_IN_DATA;
659
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 switch(req_task->data_phase) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 case TASKFILE_MULTI_OUT:
662 if (!drive->mult_count) {
663 /* (hs): give up if multcount is not set */
664 printk(KERN_ERR "%s: %s Multimode Write " \
665 "multcount is not set\n",
666 drive->name, __FUNCTION__);
667 err = -EPERM;
668 goto abort;
669 }
670 /* fall through */
671 case TASKFILE_OUT:
Bartlomiej Zolnierkiewiczac026ff2008-01-25 22:17:14 +0100672 /* fall through */
673 case TASKFILE_OUT_DMAQ:
674 case TASKFILE_OUT_DMA:
675 nsect = taskout / SECTOR_SIZE;
676 data_buf = outbuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 break;
678 case TASKFILE_MULTI_IN:
679 if (!drive->mult_count) {
680 /* (hs): give up if multcount is not set */
681 printk(KERN_ERR "%s: %s Multimode Read failure " \
682 "multcount is not set\n",
683 drive->name, __FUNCTION__);
684 err = -EPERM;
685 goto abort;
686 }
687 /* fall through */
688 case TASKFILE_IN:
Bartlomiej Zolnierkiewiczac026ff2008-01-25 22:17:14 +0100689 /* fall through */
690 case TASKFILE_IN_DMAQ:
691 case TASKFILE_IN_DMA:
692 nsect = taskin / SECTOR_SIZE;
693 data_buf = inbuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 break;
695 case TASKFILE_NO_DATA:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 break;
697 default:
698 err = -EFAULT;
699 goto abort;
700 }
701
Bartlomiej Zolnierkiewiczac026ff2008-01-25 22:17:14 +0100702 if (req_task->req_cmd == IDE_DRIVE_TASK_NO_DATA)
703 nsect = 0;
704 else if (!nsect) {
705 nsect = (args.tf.hob_nsect << 8) | args.tf.nsect;
706
707 if (!nsect) {
708 printk(KERN_ERR "%s: in/out command without data\n",
709 drive->name);
710 err = -EFAULT;
711 goto abort;
712 }
713 }
714
715 if (req_task->req_cmd == IDE_DRIVE_TASK_RAW_WRITE)
716 args.tf_flags |= IDE_TFLAG_WRITE;
717
718 err = ide_raw_taskfile(drive, &args, data_buf, nsect);
719
Bartlomiej Zolnierkiewicz650d8412008-01-25 22:17:06 +0100720 memcpy(req_task->hob_ports, &args.tf_array[0], HDIO_DRIVE_HOB_HDR_SIZE - 2);
721 memcpy(req_task->io_ports, &args.tf_array[6], HDIO_DRIVE_TASK_HDR_SIZE);
Bartlomiej Zolnierkiewicz866e2ec2008-01-25 22:17:14 +0100722
723 if ((args.tf_flags & IDE_TFLAG_FLAGGED_SET_IN_FLAGS) &&
724 req_task->in_flags.all == 0) {
725 req_task->in_flags.all = IDE_TASKFILE_STD_IN_FLAGS;
726 if (drive->addressing == 1)
727 req_task->in_flags.all |= (IDE_HOB_STD_IN_FLAGS << 8);
728 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729
730 if (copy_to_user(buf, req_task, tasksize)) {
731 err = -EFAULT;
732 goto abort;
733 }
734 if (taskout) {
735 int outtotal = tasksize;
736 if (copy_to_user(buf + outtotal, outbuf, taskout)) {
737 err = -EFAULT;
738 goto abort;
739 }
740 }
741 if (taskin) {
742 int intotal = tasksize + taskout;
743 if (copy_to_user(buf + intotal, inbuf, taskin)) {
744 err = -EFAULT;
745 goto abort;
746 }
747 }
748abort:
749 kfree(req_task);
Jesper Juhl6044ec82005-11-07 01:01:32 -0800750 kfree(outbuf);
751 kfree(inbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752
753// printk("IDE Taskfile ioctl ended. rc = %i\n", err);
754
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 return err;
756}
Bartlomiej Zolnierkiewicz26a5b042007-11-05 21:42:27 +0100757#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758
759int ide_wait_cmd (ide_drive_t *drive, u8 cmd, u8 nsect, u8 feature, u8 sectors, u8 *buf)
760{
761 struct request rq;
762 u8 buffer[4];
763
764 if (!buf)
765 buf = buffer;
766 memset(buf, 0, 4 + SECTOR_WORDS * 4 * sectors);
767 ide_init_drive_cmd(&rq);
768 rq.buffer = buf;
769 *buf++ = cmd;
770 *buf++ = nsect;
771 *buf++ = feature;
772 *buf++ = sectors;
773 return ide_do_drive_cmd(drive, &rq, ide_wait);
774}
775
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776int ide_cmd_ioctl (ide_drive_t *drive, unsigned int cmd, unsigned long arg)
777{
778 int err = 0;
779 u8 args[4], *argbuf = args;
780 u8 xfer_rate = 0;
781 int argsize = 4;
782 ide_task_t tfargs;
Bartlomiej Zolnierkiewicz650d8412008-01-25 22:17:06 +0100783 struct ide_taskfile *tf = &tfargs.tf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784
785 if (NULL == (void *) arg) {
786 struct request rq;
787 ide_init_drive_cmd(&rq);
788 return ide_do_drive_cmd(drive, &rq, ide_wait);
789 }
790
791 if (copy_from_user(args, (void __user *)arg, 4))
792 return -EFAULT;
793
794 memset(&tfargs, 0, sizeof(ide_task_t));
Bartlomiej Zolnierkiewicz650d8412008-01-25 22:17:06 +0100795 tf->feature = args[2];
796 tf->nsect = args[3];
797 tf->lbal = args[1];
798 tf->command = args[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799
800 if (args[3]) {
801 argsize = 4 + (SECTOR_WORDS * 4 * args[3]);
Deepak Saxenaf5e3c2f2005-11-07 01:01:25 -0800802 argbuf = kzalloc(argsize, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 if (argbuf == NULL)
804 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 }
806 if (set_transfer(drive, &tfargs)) {
807 xfer_rate = args[1];
808 if (ide_ata66_check(drive, &tfargs))
809 goto abort;
810 }
811
812 err = ide_wait_cmd(drive, args[0], args[1], args[2], args[3], argbuf);
813
814 if (!err && xfer_rate) {
815 /* active-retuning-calls future */
816 ide_set_xfer_rate(drive, xfer_rate);
817 ide_driveid_update(drive);
818 }
819abort:
820 if (copy_to_user((void __user *)arg, argbuf, argsize))
821 err = -EFAULT;
822 if (argsize > 4)
823 kfree(argbuf);
824 return err;
825}
826
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827int ide_task_ioctl (ide_drive_t *drive, unsigned int cmd, unsigned long arg)
828{
829 void __user *p = (void __user *)arg;
830 int err = 0;
Bartlomiej Zolnierkiewicz14b89ef2008-01-25 22:17:11 +0100831 u8 args[7];
832 ide_task_t task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833
834 if (copy_from_user(args, p, 7))
835 return -EFAULT;
Bartlomiej Zolnierkiewicz14b89ef2008-01-25 22:17:11 +0100836
837 memset(&task, 0, sizeof(task));
838 memcpy(&task.tf_array[7], &args[1], 6);
839 task.tf.command = args[0];
Bartlomiej Zolnierkiewicz657cc1a2008-01-26 20:13:10 +0100840 task.tf_flags = IDE_TFLAG_TF | IDE_TFLAG_DEVICE;
Bartlomiej Zolnierkiewicz14b89ef2008-01-25 22:17:11 +0100841
842 err = ide_no_data_taskfile(drive, &task);
843
844 args[0] = task.tf.command;
845 memcpy(&args[1], &task.tf_array[7], 6);
846
847 if (copy_to_user(p, args, 7))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 err = -EFAULT;
Bartlomiej Zolnierkiewicz14b89ef2008-01-25 22:17:11 +0100849
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 return err;
851}