blob: 1a34edb183ecf5eef469f01d1824853be68a08ed [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070038static void ata_bswap_data (void *buffer, int wcount)
39{
40 u16 *p = buffer;
41
42 while (wcount--) {
43 *p = *p << 8 | *p >> 8; p++;
44 *p = *p << 8 | *p >> 8; p++;
45 }
46}
47
48static void taskfile_input_data(ide_drive_t *drive, void *buffer, u32 wcount)
49{
50 HWIF(drive)->ata_input_data(drive, buffer, wcount);
51 if (drive->bswap)
52 ata_bswap_data(buffer, wcount);
53}
54
55static void taskfile_output_data(ide_drive_t *drive, void *buffer, u32 wcount)
56{
57 if (drive->bswap) {
58 ata_bswap_data(buffer, wcount);
59 HWIF(drive)->ata_output_data(drive, buffer, wcount);
60 ata_bswap_data(buffer, wcount);
61 } else {
62 HWIF(drive)->ata_output_data(drive, buffer, wcount);
63 }
64}
65
Bartlomiej Zolnierkiewicz9e422372008-01-25 22:17:07 +010066void ide_tf_load(ide_drive_t *drive, ide_task_t *task)
67{
68 ide_hwif_t *hwif = drive->hwif;
69 struct ide_taskfile *tf = &task->tf;
70 u8 HIHI = (task->tf_flags & IDE_TFLAG_LBA48) ? 0xE0 : 0xEF;
71
Bartlomiej Zolnierkiewicz74095a92008-01-25 22:17:07 +010072 if (task->tf_flags & IDE_TFLAG_FLAGGED)
73 HIHI = 0xFF;
74
Bartlomiej Zolnierkiewicz807e35d2008-01-25 22:17:10 +010075#ifdef DEBUG
76 printk("%s: tf: feat 0x%02x nsect 0x%02x lbal 0x%02x "
77 "lbam 0x%02x lbah 0x%02x dev 0x%02x cmd 0x%02x\n",
78 drive->name, tf->feature, tf->nsect, tf->lbal,
79 tf->lbam, tf->lbah, tf->device, tf->command);
80#endif
81
Bartlomiej Zolnierkiewicz9e422372008-01-25 22:17:07 +010082 if (IDE_CONTROL_REG)
83 hwif->OUTB(drive->ctl, IDE_CONTROL_REG); /* clear nIEN */
84
85 if ((task->tf_flags & IDE_TFLAG_NO_SELECT_MASK) == 0)
86 SELECT_MASK(drive, 0);
87
Bartlomiej Zolnierkiewicz74095a92008-01-25 22:17:07 +010088 if (task->tf_flags & IDE_TFLAG_OUT_DATA)
89 hwif->OUTW((tf->hob_data << 8) | tf->data, IDE_DATA_REG);
Bartlomiej Zolnierkiewicz9e422372008-01-25 22:17:07 +010090
Bartlomiej Zolnierkiewicz74095a92008-01-25 22:17:07 +010091 if (task->tf_flags & IDE_TFLAG_OUT_HOB_FEATURE)
92 hwif->OUTB(tf->hob_feature, IDE_FEATURE_REG);
93 if (task->tf_flags & IDE_TFLAG_OUT_HOB_NSECT)
94 hwif->OUTB(tf->hob_nsect, IDE_NSECTOR_REG);
95 if (task->tf_flags & IDE_TFLAG_OUT_HOB_LBAL)
96 hwif->OUTB(tf->hob_lbal, IDE_SECTOR_REG);
97 if (task->tf_flags & IDE_TFLAG_OUT_HOB_LBAM)
98 hwif->OUTB(tf->hob_lbam, IDE_LCYL_REG);
99 if (task->tf_flags & IDE_TFLAG_OUT_HOB_LBAH)
100 hwif->OUTB(tf->hob_lbah, IDE_HCYL_REG);
101
102 if (task->tf_flags & IDE_TFLAG_OUT_FEATURE)
103 hwif->OUTB(tf->feature, IDE_FEATURE_REG);
104 if (task->tf_flags & IDE_TFLAG_OUT_NSECT)
105 hwif->OUTB(tf->nsect, IDE_NSECTOR_REG);
106 if (task->tf_flags & IDE_TFLAG_OUT_LBAL)
107 hwif->OUTB(tf->lbal, IDE_SECTOR_REG);
108 if (task->tf_flags & IDE_TFLAG_OUT_LBAM)
109 hwif->OUTB(tf->lbam, IDE_LCYL_REG);
110 if (task->tf_flags & IDE_TFLAG_OUT_LBAH)
111 hwif->OUTB(tf->lbah, IDE_HCYL_REG);
Bartlomiej Zolnierkiewicz9e422372008-01-25 22:17:07 +0100112
Bartlomiej Zolnierkiewicz807e35d2008-01-25 22:17:10 +0100113 if (task->tf_flags & IDE_TFLAG_OUT_DEVICE)
114 hwif->OUTB((tf->device & HIHI) | drive->select.all, IDE_SELECT_REG);
Bartlomiej Zolnierkiewicz9e422372008-01-25 22:17:07 +0100115}
116
117EXPORT_SYMBOL_GPL(ide_tf_load);
118
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119int taskfile_lib_get_identify (ide_drive_t *drive, u8 *buf)
120{
121 ide_task_t args;
Bartlomiej Zolnierkiewicz650d8412008-01-25 22:17:06 +0100122
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 memset(&args, 0, sizeof(ide_task_t));
Bartlomiej Zolnierkiewicz650d8412008-01-25 22:17:06 +0100124 args.tf.nsect = 0x01;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 if (drive->media == ide_disk)
Bartlomiej Zolnierkiewicz650d8412008-01-25 22:17:06 +0100126 args.tf.command = WIN_IDENTIFY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 else
Bartlomiej Zolnierkiewicz650d8412008-01-25 22:17:06 +0100128 args.tf.command = WIN_PIDENTIFY;
Bartlomiej Zolnierkiewiczac026ff2008-01-25 22:17:14 +0100129 args.tf_flags = IDE_TFLAG_OUT_TF | IDE_TFLAG_OUT_DEVICE;
130 args.data_phase = TASKFILE_IN;
131 args.handler = task_in_intr;
132 return ide_raw_taskfile(drive, &args, buf, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133}
134
Bartlomiej Zolnierkiewicz74095a92008-01-25 22:17:07 +0100135static int inline task_dma_ok(ide_task_t *task)
136{
137 if (task->tf_flags & IDE_TFLAG_FLAGGED)
138 return 1;
139
140 switch (task->tf.command) {
141 case WIN_WRITEDMA_ONCE:
142 case WIN_WRITEDMA:
143 case WIN_WRITEDMA_EXT:
144 case WIN_READDMA_ONCE:
145 case WIN_READDMA:
146 case WIN_READDMA_EXT:
147 case WIN_IDENTIFY_DMA:
148 return 1;
149 }
150
151 return 0;
152}
153
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154ide_startstop_t do_rw_taskfile (ide_drive_t *drive, ide_task_t *task)
155{
156 ide_hwif_t *hwif = HWIF(drive);
Bartlomiej Zolnierkiewicz650d8412008-01-25 22:17:06 +0100157 struct ide_taskfile *tf = &task->tf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
Bartlomiej Zolnierkiewicz1edee602008-01-25 22:17:15 +0100159 if (task->data_phase == TASKFILE_MULTI_IN ||
160 task->data_phase == TASKFILE_MULTI_OUT) {
161 if (!drive->mult_count) {
162 printk(KERN_ERR "%s: multimode not set!\n",
163 drive->name);
164 return ide_stopped;
165 }
166 }
167
168 if (task->tf_flags & IDE_TFLAG_FLAGGED)
169 task->tf_flags |= IDE_TFLAG_FLAGGED_SET_IN_FLAGS;
170
Bartlomiej Zolnierkiewicz9e422372008-01-25 22:17:07 +0100171 ide_tf_load(drive, task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
173 if (task->handler != NULL) {
174 if (task->prehandler != NULL) {
Bartlomiej Zolnierkiewicz650d8412008-01-25 22:17:06 +0100175 hwif->OUTBSYNC(drive, tf->command, IDE_COMMAND_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 ndelay(400); /* FIXME */
177 return task->prehandler(drive, task->rq);
178 }
Bartlomiej Zolnierkiewicz650d8412008-01-25 22:17:06 +0100179 ide_execute_command(drive, tf->command, task->handler, WAIT_WORSTCASE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 return ide_started;
181 }
182
Bartlomiej Zolnierkiewicz74095a92008-01-25 22:17:07 +0100183 if (task_dma_ok(task) && drive->using_dma && !hwif->dma_setup(drive)) {
184 hwif->dma_exec_cmd(drive, tf->command);
185 hwif->dma_start(drive);
186 return ide_started;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 }
188
189 return ide_stopped;
190}
191
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192/*
193 * set_multmode_intr() is invoked on completion of a WIN_SETMULT cmd.
194 */
195ide_startstop_t set_multmode_intr (ide_drive_t *drive)
196{
197 ide_hwif_t *hwif = HWIF(drive);
198 u8 stat;
199
200 if (OK_STAT(stat = hwif->INB(IDE_STATUS_REG),READY_STAT,BAD_STAT)) {
201 drive->mult_count = drive->mult_req;
202 } else {
203 drive->mult_req = drive->mult_count = 0;
204 drive->special.b.recalibrate = 1;
205 (void) ide_dump_status(drive, "set_multmode", stat);
206 }
207 return ide_stopped;
208}
209
210/*
211 * set_geometry_intr() is invoked on completion of a WIN_SPECIFY cmd.
212 */
213ide_startstop_t set_geometry_intr (ide_drive_t *drive)
214{
215 ide_hwif_t *hwif = HWIF(drive);
216 int retries = 5;
217 u8 stat;
218
219 while (((stat = hwif->INB(IDE_STATUS_REG)) & BUSY_STAT) && retries--)
220 udelay(10);
221
222 if (OK_STAT(stat, READY_STAT, BAD_STAT))
223 return ide_stopped;
224
225 if (stat & (ERR_STAT|DRQ_STAT))
226 return ide_error(drive, "set_geometry_intr", stat);
227
Eric Sesterhenn125e1872006-06-23 02:06:06 -0700228 BUG_ON(HWGROUP(drive)->handler != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 ide_set_handler(drive, &set_geometry_intr, WAIT_WORSTCASE, NULL);
230 return ide_started;
231}
232
233/*
234 * recal_intr() is invoked on completion of a WIN_RESTORE (recalibrate) cmd.
235 */
236ide_startstop_t recal_intr (ide_drive_t *drive)
237{
238 ide_hwif_t *hwif = HWIF(drive);
239 u8 stat;
240
241 if (!OK_STAT(stat = hwif->INB(IDE_STATUS_REG), READY_STAT, BAD_STAT))
242 return ide_error(drive, "recal_intr", stat);
243 return ide_stopped;
244}
245
246/*
247 * Handler for commands without a data phase
248 */
249ide_startstop_t task_no_data_intr (ide_drive_t *drive)
250{
251 ide_task_t *args = HWGROUP(drive)->rq->special;
252 ide_hwif_t *hwif = HWIF(drive);
253 u8 stat;
254
Ingo Molnar366c7f52006-07-03 00:25:25 -0700255 local_irq_enable_in_hardirq();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 if (!OK_STAT(stat = hwif->INB(IDE_STATUS_REG),READY_STAT,BAD_STAT)) {
257 return ide_error(drive, "task_no_data_intr", stat);
258 /* calls ide_end_drive_cmd */
259 }
260 if (args)
261 ide_end_drive_cmd(drive, stat, hwif->INB(IDE_ERROR_REG));
262
263 return ide_stopped;
264}
265
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266static u8 wait_drive_not_busy(ide_drive_t *drive)
267{
268 ide_hwif_t *hwif = HWIF(drive);
Masatake YAMATOb42fa132007-07-03 22:28:34 +0200269 int retries;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 u8 stat;
271
272 /*
273 * Last sector was transfered, wait until drive is ready.
274 * This can take up to 10 usec, but we will wait max 1 ms
275 * (drive_cmd_intr() waits that long).
276 */
Masatake YAMATOb42fa132007-07-03 22:28:34 +0200277 for (retries = 0; retries < 100; retries++) {
278 if ((stat = hwif->INB(IDE_STATUS_REG)) & BUSY_STAT)
279 udelay(10);
280 else
281 break;
282 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
Masatake YAMATOb42fa132007-07-03 22:28:34 +0200284 if (stat & BUSY_STAT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 printk(KERN_ERR "%s: drive still BUSY!\n", drive->name);
286
287 return stat;
288}
289
290static void ide_pio_sector(ide_drive_t *drive, unsigned int write)
291{
292 ide_hwif_t *hwif = drive->hwif;
293 struct scatterlist *sg = hwif->sg_table;
Jens Axboe55c16a72007-07-25 08:13:56 +0200294 struct scatterlist *cursg = hwif->cursg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 struct page *page;
296#ifdef CONFIG_HIGHMEM
297 unsigned long flags;
298#endif
299 unsigned int offset;
300 u8 *buf;
301
Jens Axboe55c16a72007-07-25 08:13:56 +0200302 cursg = hwif->cursg;
303 if (!cursg) {
304 cursg = sg;
305 hwif->cursg = sg;
306 }
307
Jens Axboe45711f12007-10-22 21:19:53 +0200308 page = sg_page(cursg);
Jens Axboe55c16a72007-07-25 08:13:56 +0200309 offset = cursg->offset + hwif->cursg_ofs * SECTOR_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310
311 /* get the current page and offset */
312 page = nth_page(page, (offset >> PAGE_SHIFT));
313 offset %= PAGE_SIZE;
314
315#ifdef CONFIG_HIGHMEM
316 local_irq_save(flags);
317#endif
318 buf = kmap_atomic(page, KM_BIO_SRC_IRQ) + offset;
319
320 hwif->nleft--;
321 hwif->cursg_ofs++;
322
Jens Axboe55c16a72007-07-25 08:13:56 +0200323 if ((hwif->cursg_ofs * SECTOR_SIZE) == cursg->length) {
324 hwif->cursg = sg_next(hwif->cursg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 hwif->cursg_ofs = 0;
326 }
327
328 /* do the actual data transfer */
329 if (write)
330 taskfile_output_data(drive, buf, SECTOR_WORDS);
331 else
332 taskfile_input_data(drive, buf, SECTOR_WORDS);
333
334 kunmap_atomic(buf, KM_BIO_SRC_IRQ);
335#ifdef CONFIG_HIGHMEM
336 local_irq_restore(flags);
337#endif
338}
339
340static void ide_pio_multi(ide_drive_t *drive, unsigned int write)
341{
342 unsigned int nsect;
343
344 nsect = min_t(unsigned int, drive->hwif->nleft, drive->mult_count);
345 while (nsect--)
346 ide_pio_sector(drive, write);
347}
348
Arjan van de Ven858119e2006-01-14 13:20:43 -0800349static void ide_pio_datablock(ide_drive_t *drive, struct request *rq,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 unsigned int write)
351{
352 if (rq->bio) /* fs request */
353 rq->errors = 0;
354
Andrew Morton651c29a2006-02-15 15:17:37 -0800355 touch_softlockup_watchdog();
356
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 switch (drive->hwif->data_phase) {
358 case TASKFILE_MULTI_IN:
359 case TASKFILE_MULTI_OUT:
360 ide_pio_multi(drive, write);
361 break;
362 default:
363 ide_pio_sector(drive, write);
364 break;
365 }
366}
367
368static ide_startstop_t task_error(ide_drive_t *drive, struct request *rq,
369 const char *s, u8 stat)
370{
371 if (rq->bio) {
372 ide_hwif_t *hwif = drive->hwif;
373 int sectors = hwif->nsect - hwif->nleft;
374
375 switch (hwif->data_phase) {
376 case TASKFILE_IN:
377 if (hwif->nleft)
378 break;
379 /* fall through */
380 case TASKFILE_OUT:
381 sectors--;
382 break;
383 case TASKFILE_MULTI_IN:
384 if (hwif->nleft)
385 break;
386 /* fall through */
387 case TASKFILE_MULTI_OUT:
388 sectors -= drive->mult_count;
389 default:
390 break;
391 }
392
393 if (sectors > 0) {
394 ide_driver_t *drv;
395
396 drv = *(ide_driver_t **)rq->rq_disk->private_data;
397 drv->end_request(drive, 1, sectors);
398 }
399 }
400 return ide_error(drive, s, stat);
401}
402
403static void task_end_request(ide_drive_t *drive, struct request *rq, u8 stat)
404{
Jens Axboe55c16a72007-07-25 08:13:56 +0200405 HWIF(drive)->cursg = NULL;
406
Jens Axboe4aff5e22006-08-10 08:44:47 +0200407 if (rq->cmd_type == REQ_TYPE_ATA_TASKFILE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 ide_task_t *task = rq->special;
409
Bartlomiej Zolnierkiewicz74095a92008-01-25 22:17:07 +0100410 if (task->tf_flags & IDE_TFLAG_FLAGGED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 u8 err = drive->hwif->INB(IDE_ERROR_REG);
412 ide_end_drive_cmd(drive, stat, err);
413 return;
414 }
415 }
416
Richard Purdie03731fb2006-03-31 02:31:15 -0800417 if (rq->rq_disk) {
418 ide_driver_t *drv;
419
420 drv = *(ide_driver_t **)rq->rq_disk->private_data;;
421 drv->end_request(drive, 1, rq->hard_nr_sectors);
422 } else
423 ide_end_request(drive, 1, rq->hard_nr_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424}
425
426/*
427 * Handler for command with PIO data-in phase (Read/Read Multiple).
428 */
429ide_startstop_t task_in_intr (ide_drive_t *drive)
430{
431 ide_hwif_t *hwif = drive->hwif;
432 struct request *rq = HWGROUP(drive)->rq;
433 u8 stat = hwif->INB(IDE_STATUS_REG);
434
435 /* new way for dealing with premature shared PCI interrupts */
436 if (!OK_STAT(stat, DATA_READY, BAD_R_STAT)) {
437 if (stat & (ERR_STAT | DRQ_STAT))
438 return task_error(drive, rq, __FUNCTION__, stat);
439 /* No data yet, so wait for another IRQ. */
440 ide_set_handler(drive, &task_in_intr, WAIT_WORSTCASE, NULL);
441 return ide_started;
442 }
443
444 ide_pio_datablock(drive, rq, 0);
445
446 /* If it was the last datablock check status and finish transfer. */
447 if (!hwif->nleft) {
448 stat = wait_drive_not_busy(drive);
449 if (!OK_STAT(stat, 0, BAD_R_STAT))
450 return task_error(drive, rq, __FUNCTION__, stat);
451 task_end_request(drive, rq, stat);
452 return ide_stopped;
453 }
454
455 /* Still data left to transfer. */
456 ide_set_handler(drive, &task_in_intr, WAIT_WORSTCASE, NULL);
457
458 return ide_started;
459}
460EXPORT_SYMBOL(task_in_intr);
461
462/*
463 * Handler for command with PIO data-out phase (Write/Write Multiple).
464 */
465static ide_startstop_t task_out_intr (ide_drive_t *drive)
466{
467 ide_hwif_t *hwif = drive->hwif;
468 struct request *rq = HWGROUP(drive)->rq;
469 u8 stat = hwif->INB(IDE_STATUS_REG);
470
471 if (!OK_STAT(stat, DRIVE_READY, drive->bad_wstat))
472 return task_error(drive, rq, __FUNCTION__, stat);
473
474 /* Deal with unexpected ATA data phase. */
475 if (((stat & DRQ_STAT) == 0) ^ !hwif->nleft)
476 return task_error(drive, rq, __FUNCTION__, stat);
477
478 if (!hwif->nleft) {
479 task_end_request(drive, rq, stat);
480 return ide_stopped;
481 }
482
483 /* Still data left to transfer. */
484 ide_pio_datablock(drive, rq, 1);
485 ide_set_handler(drive, &task_out_intr, WAIT_WORSTCASE, NULL);
486
487 return ide_started;
488}
489
490ide_startstop_t pre_task_out_intr (ide_drive_t *drive, struct request *rq)
491{
492 ide_startstop_t startstop;
493
494 if (ide_wait_stat(&startstop, drive, DATA_READY,
495 drive->bad_wstat, WAIT_DRQ)) {
496 printk(KERN_ERR "%s: no DRQ after issuing %sWRITE%s\n",
497 drive->name,
498 drive->hwif->data_phase ? "MULT" : "",
499 drive->addressing ? "_EXT" : "");
500 return startstop;
501 }
502
503 if (!drive->unmask)
504 local_irq_disable();
505
506 ide_set_handler(drive, &task_out_intr, WAIT_WORSTCASE, NULL);
507 ide_pio_datablock(drive, rq, 1);
508
509 return ide_started;
510}
511EXPORT_SYMBOL(pre_task_out_intr);
512
Bartlomiej Zolnierkiewiczac026ff2008-01-25 22:17:14 +0100513int ide_raw_taskfile(ide_drive_t *drive, ide_task_t *task, u8 *buf, u16 nsect)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514{
515 struct request rq;
516
517 memset(&rq, 0, sizeof(rq));
Bartlomiej Zolnierkiewicz02ac2462007-11-05 21:42:27 +0100518 rq.ref_count = 1;
Jens Axboe4aff5e22006-08-10 08:44:47 +0200519 rq.cmd_type = REQ_TYPE_ATA_TASKFILE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 rq.buffer = buf;
521
522 /*
523 * (ks) We transfer currently only whole sectors.
524 * This is suffient for now. But, it would be great,
525 * if we would find a solution to transfer any size.
526 * To support special commands like READ LONG.
527 */
Bartlomiej Zolnierkiewiczac026ff2008-01-25 22:17:14 +0100528 rq.hard_nr_sectors = rq.nr_sectors = nsect;
529 rq.hard_cur_sectors = rq.current_nr_sectors = nsect;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530
Bartlomiej Zolnierkiewiczac026ff2008-01-25 22:17:14 +0100531 if (task->tf_flags & IDE_TFLAG_WRITE)
532 rq.cmd_flags |= REQ_RW;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533
Bartlomiej Zolnierkiewiczac026ff2008-01-25 22:17:14 +0100534 rq.special = task;
535 task->rq = &rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 return ide_do_drive_cmd(drive, &rq, ide_wait);
538}
539
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540EXPORT_SYMBOL(ide_raw_taskfile);
541
Bartlomiej Zolnierkiewicz9a3c49b2008-01-25 22:17:07 +0100542int ide_no_data_taskfile(ide_drive_t *drive, ide_task_t *task)
543{
Bartlomiej Zolnierkiewiczac026ff2008-01-25 22:17:14 +0100544 task->data_phase = TASKFILE_NO_DATA;
545 task->handler = task_no_data_intr;
Bartlomiej Zolnierkiewicz9a3c49b2008-01-25 22:17:07 +0100546
Bartlomiej Zolnierkiewiczac026ff2008-01-25 22:17:14 +0100547 return ide_raw_taskfile(drive, task, NULL, 0);
Bartlomiej Zolnierkiewicz9a3c49b2008-01-25 22:17:07 +0100548}
549EXPORT_SYMBOL_GPL(ide_no_data_taskfile);
550
Bartlomiej Zolnierkiewicz26a5b042007-11-05 21:42:27 +0100551#ifdef CONFIG_IDE_TASK_IOCTL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552int ide_taskfile_ioctl (ide_drive_t *drive, unsigned int cmd, unsigned long arg)
553{
554 ide_task_request_t *req_task;
555 ide_task_t args;
556 u8 *outbuf = NULL;
557 u8 *inbuf = NULL;
Bartlomiej Zolnierkiewiczac026ff2008-01-25 22:17:14 +0100558 u8 *data_buf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 int err = 0;
560 int tasksize = sizeof(struct ide_task_request_s);
Alan Cox3a42bb22006-10-16 16:31:02 +0100561 unsigned int taskin = 0;
562 unsigned int taskout = 0;
Bartlomiej Zolnierkiewiczac026ff2008-01-25 22:17:14 +0100563 u16 nsect = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 u8 io_32bit = drive->io_32bit;
565 char __user *buf = (char __user *)arg;
566
567// printk("IDE Taskfile ...\n");
568
Deepak Saxenaf5e3c2f2005-11-07 01:01:25 -0800569 req_task = kzalloc(tasksize, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 if (req_task == NULL) return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 if (copy_from_user(req_task, buf, tasksize)) {
572 kfree(req_task);
573 return -EFAULT;
574 }
575
Alan Cox3a42bb22006-10-16 16:31:02 +0100576 taskout = req_task->out_size;
577 taskin = req_task->in_size;
578
579 if (taskin > 65536 || taskout > 65536) {
580 err = -EINVAL;
581 goto abort;
582 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583
584 if (taskout) {
585 int outtotal = tasksize;
Deepak Saxenaf5e3c2f2005-11-07 01:01:25 -0800586 outbuf = kzalloc(taskout, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 if (outbuf == NULL) {
588 err = -ENOMEM;
589 goto abort;
590 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 if (copy_from_user(outbuf, buf + outtotal, taskout)) {
592 err = -EFAULT;
593 goto abort;
594 }
595 }
596
597 if (taskin) {
598 int intotal = tasksize + taskout;
Deepak Saxenaf5e3c2f2005-11-07 01:01:25 -0800599 inbuf = kzalloc(taskin, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 if (inbuf == NULL) {
601 err = -ENOMEM;
602 goto abort;
603 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 if (copy_from_user(inbuf, buf + intotal, taskin)) {
605 err = -EFAULT;
606 goto abort;
607 }
608 }
609
610 memset(&args, 0, sizeof(ide_task_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611
Bartlomiej Zolnierkiewicz650d8412008-01-25 22:17:06 +0100612 memcpy(&args.tf_array[0], req_task->hob_ports, HDIO_DRIVE_HOB_HDR_SIZE - 2);
613 memcpy(&args.tf_array[6], req_task->io_ports, HDIO_DRIVE_TASK_HDR_SIZE);
Bartlomiej Zolnierkiewicz866e2ec2008-01-25 22:17:14 +0100614
615 args.data_phase = req_task->data_phase;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616
Bartlomiej Zolnierkiewicza3bbb9d2008-01-25 22:17:10 +0100617 args.tf_flags = IDE_TFLAG_OUT_DEVICE;
618 if (drive->addressing == 1)
619 args.tf_flags |= IDE_TFLAG_LBA48;
620
Bartlomiej Zolnierkiewicz74095a92008-01-25 22:17:07 +0100621 if (req_task->out_flags.all) {
622 args.tf_flags |= IDE_TFLAG_FLAGGED;
623
624 if (req_task->out_flags.b.data)
625 args.tf_flags |= IDE_TFLAG_OUT_DATA;
626
627 if (req_task->out_flags.b.nsector_hob)
628 args.tf_flags |= IDE_TFLAG_OUT_HOB_NSECT;
629 if (req_task->out_flags.b.sector_hob)
630 args.tf_flags |= IDE_TFLAG_OUT_HOB_LBAL;
631 if (req_task->out_flags.b.lcyl_hob)
632 args.tf_flags |= IDE_TFLAG_OUT_HOB_LBAM;
633 if (req_task->out_flags.b.hcyl_hob)
634 args.tf_flags |= IDE_TFLAG_OUT_HOB_LBAH;
635
636 if (req_task->out_flags.b.error_feature)
637 args.tf_flags |= IDE_TFLAG_OUT_FEATURE;
638 if (req_task->out_flags.b.nsector)
639 args.tf_flags |= IDE_TFLAG_OUT_NSECT;
640 if (req_task->out_flags.b.sector)
641 args.tf_flags |= IDE_TFLAG_OUT_LBAL;
642 if (req_task->out_flags.b.lcyl)
643 args.tf_flags |= IDE_TFLAG_OUT_LBAM;
644 if (req_task->out_flags.b.hcyl)
645 args.tf_flags |= IDE_TFLAG_OUT_LBAH;
Bartlomiej Zolnierkiewicza3bbb9d2008-01-25 22:17:10 +0100646 } else {
647 args.tf_flags |= IDE_TFLAG_OUT_TF;
648 if (args.tf_flags & IDE_TFLAG_LBA48)
649 args.tf_flags |= IDE_TFLAG_OUT_HOB;
Bartlomiej Zolnierkiewicz74095a92008-01-25 22:17:07 +0100650 }
651
Bartlomiej Zolnierkiewicz866e2ec2008-01-25 22:17:14 +0100652 if (req_task->in_flags.b.data)
653 args.tf_flags |= IDE_TFLAG_IN_DATA;
654
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 drive->io_32bit = 0;
656 switch(req_task->data_phase) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 case TASKFILE_MULTI_OUT:
658 if (!drive->mult_count) {
659 /* (hs): give up if multcount is not set */
660 printk(KERN_ERR "%s: %s Multimode Write " \
661 "multcount is not set\n",
662 drive->name, __FUNCTION__);
663 err = -EPERM;
664 goto abort;
665 }
666 /* fall through */
667 case TASKFILE_OUT:
668 args.prehandler = &pre_task_out_intr;
669 args.handler = &task_out_intr;
Bartlomiej Zolnierkiewiczac026ff2008-01-25 22:17:14 +0100670 /* fall through */
671 case TASKFILE_OUT_DMAQ:
672 case TASKFILE_OUT_DMA:
673 nsect = taskout / SECTOR_SIZE;
674 data_buf = outbuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 break;
676 case TASKFILE_MULTI_IN:
677 if (!drive->mult_count) {
678 /* (hs): give up if multcount is not set */
679 printk(KERN_ERR "%s: %s Multimode Read failure " \
680 "multcount is not set\n",
681 drive->name, __FUNCTION__);
682 err = -EPERM;
683 goto abort;
684 }
685 /* fall through */
686 case TASKFILE_IN:
687 args.handler = &task_in_intr;
Bartlomiej Zolnierkiewiczac026ff2008-01-25 22:17:14 +0100688 /* fall through */
689 case TASKFILE_IN_DMAQ:
690 case TASKFILE_IN_DMA:
691 nsect = taskin / SECTOR_SIZE;
692 data_buf = inbuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 break;
694 case TASKFILE_NO_DATA:
695 args.handler = &task_no_data_intr;
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
755 drive->io_32bit = io_32bit;
756
757 return err;
758}
Bartlomiej Zolnierkiewicz26a5b042007-11-05 21:42:27 +0100759#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760
761int ide_wait_cmd (ide_drive_t *drive, u8 cmd, u8 nsect, u8 feature, u8 sectors, u8 *buf)
762{
763 struct request rq;
764 u8 buffer[4];
765
766 if (!buf)
767 buf = buffer;
768 memset(buf, 0, 4 + SECTOR_WORDS * 4 * sectors);
769 ide_init_drive_cmd(&rq);
770 rq.buffer = buf;
771 *buf++ = cmd;
772 *buf++ = nsect;
773 *buf++ = feature;
774 *buf++ = sectors;
775 return ide_do_drive_cmd(drive, &rq, ide_wait);
776}
777
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778int ide_cmd_ioctl (ide_drive_t *drive, unsigned int cmd, unsigned long arg)
779{
780 int err = 0;
781 u8 args[4], *argbuf = args;
782 u8 xfer_rate = 0;
783 int argsize = 4;
784 ide_task_t tfargs;
Bartlomiej Zolnierkiewicz650d8412008-01-25 22:17:06 +0100785 struct ide_taskfile *tf = &tfargs.tf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786
787 if (NULL == (void *) arg) {
788 struct request rq;
789 ide_init_drive_cmd(&rq);
790 return ide_do_drive_cmd(drive, &rq, ide_wait);
791 }
792
793 if (copy_from_user(args, (void __user *)arg, 4))
794 return -EFAULT;
795
796 memset(&tfargs, 0, sizeof(ide_task_t));
Bartlomiej Zolnierkiewicz650d8412008-01-25 22:17:06 +0100797 tf->feature = args[2];
798 tf->nsect = args[3];
799 tf->lbal = args[1];
800 tf->command = args[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801
802 if (args[3]) {
803 argsize = 4 + (SECTOR_WORDS * 4 * args[3]);
Deepak Saxenaf5e3c2f2005-11-07 01:01:25 -0800804 argbuf = kzalloc(argsize, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 if (argbuf == NULL)
806 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 }
808 if (set_transfer(drive, &tfargs)) {
809 xfer_rate = args[1];
810 if (ide_ata66_check(drive, &tfargs))
811 goto abort;
812 }
813
814 err = ide_wait_cmd(drive, args[0], args[1], args[2], args[3], argbuf);
815
816 if (!err && xfer_rate) {
817 /* active-retuning-calls future */
818 ide_set_xfer_rate(drive, xfer_rate);
819 ide_driveid_update(drive);
820 }
821abort:
822 if (copy_to_user((void __user *)arg, argbuf, argsize))
823 err = -EFAULT;
824 if (argsize > 4)
825 kfree(argbuf);
826 return err;
827}
828
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829int ide_task_ioctl (ide_drive_t *drive, unsigned int cmd, unsigned long arg)
830{
831 void __user *p = (void __user *)arg;
832 int err = 0;
Bartlomiej Zolnierkiewicz14b89ef2008-01-25 22:17:11 +0100833 u8 args[7];
834 ide_task_t task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835
836 if (copy_from_user(args, p, 7))
837 return -EFAULT;
Bartlomiej Zolnierkiewicz14b89ef2008-01-25 22:17:11 +0100838
839 memset(&task, 0, sizeof(task));
840 memcpy(&task.tf_array[7], &args[1], 6);
841 task.tf.command = args[0];
842 task.tf_flags = IDE_TFLAG_OUT_TF | IDE_TFLAG_OUT_DEVICE;
843
844 err = ide_no_data_taskfile(drive, &task);
845
846 args[0] = task.tf.command;
847 memcpy(&args[1], &task.tf_array[7], 6);
848
849 if (copy_to_user(p, args, 7))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 err = -EFAULT;
Bartlomiej Zolnierkiewicz14b89ef2008-01-25 22:17:11 +0100851
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 return err;
853}