blob: 5a1a9f7846b6fc630d3654338d46b67d33a810a4 [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
66int taskfile_lib_get_identify (ide_drive_t *drive, u8 *buf)
67{
68 ide_task_t args;
Bartlomiej Zolnierkiewicz650d8412008-01-25 22:17:06 +010069
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 memset(&args, 0, sizeof(ide_task_t));
Bartlomiej Zolnierkiewicz650d8412008-01-25 22:17:06 +010071 args.tf.nsect = 0x01;
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 if (drive->media == ide_disk)
Bartlomiej Zolnierkiewicz650d8412008-01-25 22:17:06 +010073 args.tf.command = WIN_IDENTIFY;
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 else
Bartlomiej Zolnierkiewicz650d8412008-01-25 22:17:06 +010075 args.tf.command = WIN_PIDENTIFY;
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 args.command_type = IDE_DRIVE_TASK_IN;
77 args.data_phase = TASKFILE_IN;
78 args.handler = &task_in_intr;
79 return ide_raw_taskfile(drive, &args, buf);
80}
81
82ide_startstop_t do_rw_taskfile (ide_drive_t *drive, ide_task_t *task)
83{
84 ide_hwif_t *hwif = HWIF(drive);
Bartlomiej Zolnierkiewicz650d8412008-01-25 22:17:06 +010085 struct ide_taskfile *tf = &task->tf;
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 u8 HIHI = (drive->addressing == 1) ? 0xE0 : 0xEF;
87
88 /* ALL Command Block Executions SHALL clear nIEN, unless otherwise */
89 if (IDE_CONTROL_REG) {
90 /* clear nIEN */
91 hwif->OUTB(drive->ctl, IDE_CONTROL_REG);
92 }
93 SELECT_MASK(drive, 0);
94
95 if (drive->addressing == 1) {
Bartlomiej Zolnierkiewicz650d8412008-01-25 22:17:06 +010096 hwif->OUTB(tf->hob_feature, IDE_FEATURE_REG);
97 hwif->OUTB(tf->hob_nsect, IDE_NSECTOR_REG);
98 hwif->OUTB(tf->hob_lbal, IDE_SECTOR_REG);
99 hwif->OUTB(tf->hob_lbam, IDE_LCYL_REG);
100 hwif->OUTB(tf->hob_lbah, IDE_HCYL_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 }
102
Bartlomiej Zolnierkiewicz650d8412008-01-25 22:17:06 +0100103 hwif->OUTB(tf->feature, IDE_FEATURE_REG);
104 hwif->OUTB(tf->nsect, IDE_NSECTOR_REG);
105 hwif->OUTB(tf->lbal, IDE_SECTOR_REG);
106 hwif->OUTB(tf->lbam, IDE_LCYL_REG);
107 hwif->OUTB(tf->lbah, IDE_HCYL_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
Bartlomiej Zolnierkiewicz650d8412008-01-25 22:17:06 +0100109 hwif->OUTB((tf->device & HIHI) | drive->select.all, IDE_SELECT_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
111 if (task->handler != NULL) {
112 if (task->prehandler != NULL) {
Bartlomiej Zolnierkiewicz650d8412008-01-25 22:17:06 +0100113 hwif->OUTBSYNC(drive, tf->command, IDE_COMMAND_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 ndelay(400); /* FIXME */
115 return task->prehandler(drive, task->rq);
116 }
Bartlomiej Zolnierkiewicz650d8412008-01-25 22:17:06 +0100117 ide_execute_command(drive, tf->command, task->handler, WAIT_WORSTCASE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 return ide_started;
119 }
120
121 if (!drive->using_dma)
122 return ide_stopped;
123
Bartlomiej Zolnierkiewicz650d8412008-01-25 22:17:06 +0100124 switch (tf->command) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 case WIN_WRITEDMA_ONCE:
126 case WIN_WRITEDMA:
127 case WIN_WRITEDMA_EXT:
128 case WIN_READDMA_ONCE:
129 case WIN_READDMA:
130 case WIN_READDMA_EXT:
131 case WIN_IDENTIFY_DMA:
132 if (!hwif->dma_setup(drive)) {
Bartlomiej Zolnierkiewicz650d8412008-01-25 22:17:06 +0100133 hwif->dma_exec_cmd(drive, tf->command);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 hwif->dma_start(drive);
135 return ide_started;
136 }
137 break;
138 default:
139 if (task->handler == NULL)
140 return ide_stopped;
141 }
142
143 return ide_stopped;
144}
145
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146/*
147 * set_multmode_intr() is invoked on completion of a WIN_SETMULT cmd.
148 */
149ide_startstop_t set_multmode_intr (ide_drive_t *drive)
150{
151 ide_hwif_t *hwif = HWIF(drive);
152 u8 stat;
153
154 if (OK_STAT(stat = hwif->INB(IDE_STATUS_REG),READY_STAT,BAD_STAT)) {
155 drive->mult_count = drive->mult_req;
156 } else {
157 drive->mult_req = drive->mult_count = 0;
158 drive->special.b.recalibrate = 1;
159 (void) ide_dump_status(drive, "set_multmode", stat);
160 }
161 return ide_stopped;
162}
163
164/*
165 * set_geometry_intr() is invoked on completion of a WIN_SPECIFY cmd.
166 */
167ide_startstop_t set_geometry_intr (ide_drive_t *drive)
168{
169 ide_hwif_t *hwif = HWIF(drive);
170 int retries = 5;
171 u8 stat;
172
173 while (((stat = hwif->INB(IDE_STATUS_REG)) & BUSY_STAT) && retries--)
174 udelay(10);
175
176 if (OK_STAT(stat, READY_STAT, BAD_STAT))
177 return ide_stopped;
178
179 if (stat & (ERR_STAT|DRQ_STAT))
180 return ide_error(drive, "set_geometry_intr", stat);
181
Eric Sesterhenn125e1872006-06-23 02:06:06 -0700182 BUG_ON(HWGROUP(drive)->handler != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 ide_set_handler(drive, &set_geometry_intr, WAIT_WORSTCASE, NULL);
184 return ide_started;
185}
186
187/*
188 * recal_intr() is invoked on completion of a WIN_RESTORE (recalibrate) cmd.
189 */
190ide_startstop_t recal_intr (ide_drive_t *drive)
191{
192 ide_hwif_t *hwif = HWIF(drive);
193 u8 stat;
194
195 if (!OK_STAT(stat = hwif->INB(IDE_STATUS_REG), READY_STAT, BAD_STAT))
196 return ide_error(drive, "recal_intr", stat);
197 return ide_stopped;
198}
199
200/*
201 * Handler for commands without a data phase
202 */
203ide_startstop_t task_no_data_intr (ide_drive_t *drive)
204{
205 ide_task_t *args = HWGROUP(drive)->rq->special;
206 ide_hwif_t *hwif = HWIF(drive);
207 u8 stat;
208
Ingo Molnar366c7f52006-07-03 00:25:25 -0700209 local_irq_enable_in_hardirq();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 if (!OK_STAT(stat = hwif->INB(IDE_STATUS_REG),READY_STAT,BAD_STAT)) {
211 return ide_error(drive, "task_no_data_intr", stat);
212 /* calls ide_end_drive_cmd */
213 }
214 if (args)
215 ide_end_drive_cmd(drive, stat, hwif->INB(IDE_ERROR_REG));
216
217 return ide_stopped;
218}
219
220EXPORT_SYMBOL(task_no_data_intr);
221
222static u8 wait_drive_not_busy(ide_drive_t *drive)
223{
224 ide_hwif_t *hwif = HWIF(drive);
Masatake YAMATOb42fa132007-07-03 22:28:34 +0200225 int retries;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 u8 stat;
227
228 /*
229 * Last sector was transfered, wait until drive is ready.
230 * This can take up to 10 usec, but we will wait max 1 ms
231 * (drive_cmd_intr() waits that long).
232 */
Masatake YAMATOb42fa132007-07-03 22:28:34 +0200233 for (retries = 0; retries < 100; retries++) {
234 if ((stat = hwif->INB(IDE_STATUS_REG)) & BUSY_STAT)
235 udelay(10);
236 else
237 break;
238 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239
Masatake YAMATOb42fa132007-07-03 22:28:34 +0200240 if (stat & BUSY_STAT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 printk(KERN_ERR "%s: drive still BUSY!\n", drive->name);
242
243 return stat;
244}
245
246static void ide_pio_sector(ide_drive_t *drive, unsigned int write)
247{
248 ide_hwif_t *hwif = drive->hwif;
249 struct scatterlist *sg = hwif->sg_table;
Jens Axboe55c16a72007-07-25 08:13:56 +0200250 struct scatterlist *cursg = hwif->cursg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 struct page *page;
252#ifdef CONFIG_HIGHMEM
253 unsigned long flags;
254#endif
255 unsigned int offset;
256 u8 *buf;
257
Jens Axboe55c16a72007-07-25 08:13:56 +0200258 cursg = hwif->cursg;
259 if (!cursg) {
260 cursg = sg;
261 hwif->cursg = sg;
262 }
263
Jens Axboe45711f12007-10-22 21:19:53 +0200264 page = sg_page(cursg);
Jens Axboe55c16a72007-07-25 08:13:56 +0200265 offset = cursg->offset + hwif->cursg_ofs * SECTOR_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266
267 /* get the current page and offset */
268 page = nth_page(page, (offset >> PAGE_SHIFT));
269 offset %= PAGE_SIZE;
270
271#ifdef CONFIG_HIGHMEM
272 local_irq_save(flags);
273#endif
274 buf = kmap_atomic(page, KM_BIO_SRC_IRQ) + offset;
275
276 hwif->nleft--;
277 hwif->cursg_ofs++;
278
Jens Axboe55c16a72007-07-25 08:13:56 +0200279 if ((hwif->cursg_ofs * SECTOR_SIZE) == cursg->length) {
280 hwif->cursg = sg_next(hwif->cursg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 hwif->cursg_ofs = 0;
282 }
283
284 /* do the actual data transfer */
285 if (write)
286 taskfile_output_data(drive, buf, SECTOR_WORDS);
287 else
288 taskfile_input_data(drive, buf, SECTOR_WORDS);
289
290 kunmap_atomic(buf, KM_BIO_SRC_IRQ);
291#ifdef CONFIG_HIGHMEM
292 local_irq_restore(flags);
293#endif
294}
295
296static void ide_pio_multi(ide_drive_t *drive, unsigned int write)
297{
298 unsigned int nsect;
299
300 nsect = min_t(unsigned int, drive->hwif->nleft, drive->mult_count);
301 while (nsect--)
302 ide_pio_sector(drive, write);
303}
304
Arjan van de Ven858119e2006-01-14 13:20:43 -0800305static void ide_pio_datablock(ide_drive_t *drive, struct request *rq,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 unsigned int write)
307{
308 if (rq->bio) /* fs request */
309 rq->errors = 0;
310
Andrew Morton651c29a2006-02-15 15:17:37 -0800311 touch_softlockup_watchdog();
312
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 switch (drive->hwif->data_phase) {
314 case TASKFILE_MULTI_IN:
315 case TASKFILE_MULTI_OUT:
316 ide_pio_multi(drive, write);
317 break;
318 default:
319 ide_pio_sector(drive, write);
320 break;
321 }
322}
323
324static ide_startstop_t task_error(ide_drive_t *drive, struct request *rq,
325 const char *s, u8 stat)
326{
327 if (rq->bio) {
328 ide_hwif_t *hwif = drive->hwif;
329 int sectors = hwif->nsect - hwif->nleft;
330
331 switch (hwif->data_phase) {
332 case TASKFILE_IN:
333 if (hwif->nleft)
334 break;
335 /* fall through */
336 case TASKFILE_OUT:
337 sectors--;
338 break;
339 case TASKFILE_MULTI_IN:
340 if (hwif->nleft)
341 break;
342 /* fall through */
343 case TASKFILE_MULTI_OUT:
344 sectors -= drive->mult_count;
345 default:
346 break;
347 }
348
349 if (sectors > 0) {
350 ide_driver_t *drv;
351
352 drv = *(ide_driver_t **)rq->rq_disk->private_data;
353 drv->end_request(drive, 1, sectors);
354 }
355 }
356 return ide_error(drive, s, stat);
357}
358
359static void task_end_request(ide_drive_t *drive, struct request *rq, u8 stat)
360{
Jens Axboe55c16a72007-07-25 08:13:56 +0200361 HWIF(drive)->cursg = NULL;
362
Jens Axboe4aff5e22006-08-10 08:44:47 +0200363 if (rq->cmd_type == REQ_TYPE_ATA_TASKFILE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 ide_task_t *task = rq->special;
365
366 if (task->tf_out_flags.all) {
367 u8 err = drive->hwif->INB(IDE_ERROR_REG);
368 ide_end_drive_cmd(drive, stat, err);
369 return;
370 }
371 }
372
Richard Purdie03731fb2006-03-31 02:31:15 -0800373 if (rq->rq_disk) {
374 ide_driver_t *drv;
375
376 drv = *(ide_driver_t **)rq->rq_disk->private_data;;
377 drv->end_request(drive, 1, rq->hard_nr_sectors);
378 } else
379 ide_end_request(drive, 1, rq->hard_nr_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380}
381
382/*
383 * Handler for command with PIO data-in phase (Read/Read Multiple).
384 */
385ide_startstop_t task_in_intr (ide_drive_t *drive)
386{
387 ide_hwif_t *hwif = drive->hwif;
388 struct request *rq = HWGROUP(drive)->rq;
389 u8 stat = hwif->INB(IDE_STATUS_REG);
390
391 /* new way for dealing with premature shared PCI interrupts */
392 if (!OK_STAT(stat, DATA_READY, BAD_R_STAT)) {
393 if (stat & (ERR_STAT | DRQ_STAT))
394 return task_error(drive, rq, __FUNCTION__, stat);
395 /* No data yet, so wait for another IRQ. */
396 ide_set_handler(drive, &task_in_intr, WAIT_WORSTCASE, NULL);
397 return ide_started;
398 }
399
400 ide_pio_datablock(drive, rq, 0);
401
402 /* If it was the last datablock check status and finish transfer. */
403 if (!hwif->nleft) {
404 stat = wait_drive_not_busy(drive);
405 if (!OK_STAT(stat, 0, BAD_R_STAT))
406 return task_error(drive, rq, __FUNCTION__, stat);
407 task_end_request(drive, rq, stat);
408 return ide_stopped;
409 }
410
411 /* Still data left to transfer. */
412 ide_set_handler(drive, &task_in_intr, WAIT_WORSTCASE, NULL);
413
414 return ide_started;
415}
416EXPORT_SYMBOL(task_in_intr);
417
418/*
419 * Handler for command with PIO data-out phase (Write/Write Multiple).
420 */
421static ide_startstop_t task_out_intr (ide_drive_t *drive)
422{
423 ide_hwif_t *hwif = drive->hwif;
424 struct request *rq = HWGROUP(drive)->rq;
425 u8 stat = hwif->INB(IDE_STATUS_REG);
426
427 if (!OK_STAT(stat, DRIVE_READY, drive->bad_wstat))
428 return task_error(drive, rq, __FUNCTION__, stat);
429
430 /* Deal with unexpected ATA data phase. */
431 if (((stat & DRQ_STAT) == 0) ^ !hwif->nleft)
432 return task_error(drive, rq, __FUNCTION__, stat);
433
434 if (!hwif->nleft) {
435 task_end_request(drive, rq, stat);
436 return ide_stopped;
437 }
438
439 /* Still data left to transfer. */
440 ide_pio_datablock(drive, rq, 1);
441 ide_set_handler(drive, &task_out_intr, WAIT_WORSTCASE, NULL);
442
443 return ide_started;
444}
445
446ide_startstop_t pre_task_out_intr (ide_drive_t *drive, struct request *rq)
447{
448 ide_startstop_t startstop;
449
450 if (ide_wait_stat(&startstop, drive, DATA_READY,
451 drive->bad_wstat, WAIT_DRQ)) {
452 printk(KERN_ERR "%s: no DRQ after issuing %sWRITE%s\n",
453 drive->name,
454 drive->hwif->data_phase ? "MULT" : "",
455 drive->addressing ? "_EXT" : "");
456 return startstop;
457 }
458
459 if (!drive->unmask)
460 local_irq_disable();
461
462 ide_set_handler(drive, &task_out_intr, WAIT_WORSTCASE, NULL);
463 ide_pio_datablock(drive, rq, 1);
464
465 return ide_started;
466}
467EXPORT_SYMBOL(pre_task_out_intr);
468
469static int ide_diag_taskfile(ide_drive_t *drive, ide_task_t *args, unsigned long data_size, u8 *buf)
470{
471 struct request rq;
472
473 memset(&rq, 0, sizeof(rq));
Bartlomiej Zolnierkiewicz02ac2462007-11-05 21:42:27 +0100474 rq.ref_count = 1;
Jens Axboe4aff5e22006-08-10 08:44:47 +0200475 rq.cmd_type = REQ_TYPE_ATA_TASKFILE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 rq.buffer = buf;
477
478 /*
479 * (ks) We transfer currently only whole sectors.
480 * This is suffient for now. But, it would be great,
481 * if we would find a solution to transfer any size.
482 * To support special commands like READ LONG.
483 */
484 if (args->command_type != IDE_DRIVE_TASK_NO_DATA) {
485 if (data_size == 0)
Bartlomiej Zolnierkiewicz650d8412008-01-25 22:17:06 +0100486 rq.nr_sectors = (args->tf.hob_nsect << 8) | args->tf.nsect;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 else
488 rq.nr_sectors = data_size / SECTOR_SIZE;
489
490 if (!rq.nr_sectors) {
491 printk(KERN_ERR "%s: in/out command without data\n",
492 drive->name);
493 return -EFAULT;
494 }
495
496 rq.hard_nr_sectors = rq.nr_sectors;
497 rq.hard_cur_sectors = rq.current_nr_sectors = rq.nr_sectors;
498
499 if (args->command_type == IDE_DRIVE_TASK_RAW_WRITE)
Jens Axboe4aff5e22006-08-10 08:44:47 +0200500 rq.cmd_flags |= REQ_RW;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 }
502
503 rq.special = args;
Timothy Thelinef0f6a432005-09-16 19:28:16 -0700504 args->rq = &rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 return ide_do_drive_cmd(drive, &rq, ide_wait);
506}
507
508int ide_raw_taskfile (ide_drive_t *drive, ide_task_t *args, u8 *buf)
509{
510 return ide_diag_taskfile(drive, args, 0, buf);
511}
512
513EXPORT_SYMBOL(ide_raw_taskfile);
514
Bartlomiej Zolnierkiewicz26a5b042007-11-05 21:42:27 +0100515#ifdef CONFIG_IDE_TASK_IOCTL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516int ide_taskfile_ioctl (ide_drive_t *drive, unsigned int cmd, unsigned long arg)
517{
518 ide_task_request_t *req_task;
519 ide_task_t args;
520 u8 *outbuf = NULL;
521 u8 *inbuf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 int err = 0;
523 int tasksize = sizeof(struct ide_task_request_s);
Alan Cox3a42bb22006-10-16 16:31:02 +0100524 unsigned int taskin = 0;
525 unsigned int taskout = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 u8 io_32bit = drive->io_32bit;
527 char __user *buf = (char __user *)arg;
528
529// printk("IDE Taskfile ...\n");
530
Deepak Saxenaf5e3c2f2005-11-07 01:01:25 -0800531 req_task = kzalloc(tasksize, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 if (req_task == NULL) return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 if (copy_from_user(req_task, buf, tasksize)) {
534 kfree(req_task);
535 return -EFAULT;
536 }
537
Alan Cox3a42bb22006-10-16 16:31:02 +0100538 taskout = req_task->out_size;
539 taskin = req_task->in_size;
540
541 if (taskin > 65536 || taskout > 65536) {
542 err = -EINVAL;
543 goto abort;
544 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545
546 if (taskout) {
547 int outtotal = tasksize;
Deepak Saxenaf5e3c2f2005-11-07 01:01:25 -0800548 outbuf = kzalloc(taskout, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 if (outbuf == NULL) {
550 err = -ENOMEM;
551 goto abort;
552 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 if (copy_from_user(outbuf, buf + outtotal, taskout)) {
554 err = -EFAULT;
555 goto abort;
556 }
557 }
558
559 if (taskin) {
560 int intotal = tasksize + taskout;
Deepak Saxenaf5e3c2f2005-11-07 01:01:25 -0800561 inbuf = kzalloc(taskin, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 if (inbuf == NULL) {
563 err = -ENOMEM;
564 goto abort;
565 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 if (copy_from_user(inbuf, buf + intotal, taskin)) {
567 err = -EFAULT;
568 goto abort;
569 }
570 }
571
572 memset(&args, 0, sizeof(ide_task_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573
Bartlomiej Zolnierkiewicz650d8412008-01-25 22:17:06 +0100574 memcpy(&args.tf_array[0], req_task->hob_ports, HDIO_DRIVE_HOB_HDR_SIZE - 2);
575 memcpy(&args.tf_array[6], req_task->io_ports, HDIO_DRIVE_TASK_HDR_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 args.tf_in_flags = req_task->in_flags;
577 args.tf_out_flags = req_task->out_flags;
578 args.data_phase = req_task->data_phase;
579 args.command_type = req_task->req_cmd;
580
581 drive->io_32bit = 0;
582 switch(req_task->data_phase) {
583 case TASKFILE_OUT_DMAQ:
584 case TASKFILE_OUT_DMA:
585 err = ide_diag_taskfile(drive, &args, taskout, outbuf);
586 break;
587 case TASKFILE_IN_DMAQ:
588 case TASKFILE_IN_DMA:
589 err = ide_diag_taskfile(drive, &args, taskin, inbuf);
590 break;
591 case TASKFILE_MULTI_OUT:
592 if (!drive->mult_count) {
593 /* (hs): give up if multcount is not set */
594 printk(KERN_ERR "%s: %s Multimode Write " \
595 "multcount is not set\n",
596 drive->name, __FUNCTION__);
597 err = -EPERM;
598 goto abort;
599 }
600 /* fall through */
601 case TASKFILE_OUT:
602 args.prehandler = &pre_task_out_intr;
603 args.handler = &task_out_intr;
604 err = ide_diag_taskfile(drive, &args, taskout, outbuf);
605 break;
606 case TASKFILE_MULTI_IN:
607 if (!drive->mult_count) {
608 /* (hs): give up if multcount is not set */
609 printk(KERN_ERR "%s: %s Multimode Read failure " \
610 "multcount is not set\n",
611 drive->name, __FUNCTION__);
612 err = -EPERM;
613 goto abort;
614 }
615 /* fall through */
616 case TASKFILE_IN:
617 args.handler = &task_in_intr;
618 err = ide_diag_taskfile(drive, &args, taskin, inbuf);
619 break;
620 case TASKFILE_NO_DATA:
621 args.handler = &task_no_data_intr;
622 err = ide_diag_taskfile(drive, &args, 0, NULL);
623 break;
624 default:
625 err = -EFAULT;
626 goto abort;
627 }
628
Bartlomiej Zolnierkiewicz650d8412008-01-25 22:17:06 +0100629 memcpy(req_task->hob_ports, &args.tf_array[0], HDIO_DRIVE_HOB_HDR_SIZE - 2);
630 memcpy(req_task->io_ports, &args.tf_array[6], HDIO_DRIVE_TASK_HDR_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 req_task->in_flags = args.tf_in_flags;
632 req_task->out_flags = args.tf_out_flags;
633
634 if (copy_to_user(buf, req_task, tasksize)) {
635 err = -EFAULT;
636 goto abort;
637 }
638 if (taskout) {
639 int outtotal = tasksize;
640 if (copy_to_user(buf + outtotal, outbuf, taskout)) {
641 err = -EFAULT;
642 goto abort;
643 }
644 }
645 if (taskin) {
646 int intotal = tasksize + taskout;
647 if (copy_to_user(buf + intotal, inbuf, taskin)) {
648 err = -EFAULT;
649 goto abort;
650 }
651 }
652abort:
653 kfree(req_task);
Jesper Juhl6044ec82005-11-07 01:01:32 -0800654 kfree(outbuf);
655 kfree(inbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656
657// printk("IDE Taskfile ioctl ended. rc = %i\n", err);
658
659 drive->io_32bit = io_32bit;
660
661 return err;
662}
Bartlomiej Zolnierkiewicz26a5b042007-11-05 21:42:27 +0100663#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664
665int ide_wait_cmd (ide_drive_t *drive, u8 cmd, u8 nsect, u8 feature, u8 sectors, u8 *buf)
666{
667 struct request rq;
668 u8 buffer[4];
669
670 if (!buf)
671 buf = buffer;
672 memset(buf, 0, 4 + SECTOR_WORDS * 4 * sectors);
673 ide_init_drive_cmd(&rq);
674 rq.buffer = buf;
675 *buf++ = cmd;
676 *buf++ = nsect;
677 *buf++ = feature;
678 *buf++ = sectors;
679 return ide_do_drive_cmd(drive, &rq, ide_wait);
680}
681
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682int ide_cmd_ioctl (ide_drive_t *drive, unsigned int cmd, unsigned long arg)
683{
684 int err = 0;
685 u8 args[4], *argbuf = args;
686 u8 xfer_rate = 0;
687 int argsize = 4;
688 ide_task_t tfargs;
Bartlomiej Zolnierkiewicz650d8412008-01-25 22:17:06 +0100689 struct ide_taskfile *tf = &tfargs.tf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690
691 if (NULL == (void *) arg) {
692 struct request rq;
693 ide_init_drive_cmd(&rq);
694 return ide_do_drive_cmd(drive, &rq, ide_wait);
695 }
696
697 if (copy_from_user(args, (void __user *)arg, 4))
698 return -EFAULT;
699
700 memset(&tfargs, 0, sizeof(ide_task_t));
Bartlomiej Zolnierkiewicz650d8412008-01-25 22:17:06 +0100701 tf->feature = args[2];
702 tf->nsect = args[3];
703 tf->lbal = args[1];
704 tf->command = args[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705
706 if (args[3]) {
707 argsize = 4 + (SECTOR_WORDS * 4 * args[3]);
Deepak Saxenaf5e3c2f2005-11-07 01:01:25 -0800708 argbuf = kzalloc(argsize, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 if (argbuf == NULL)
710 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 }
712 if (set_transfer(drive, &tfargs)) {
713 xfer_rate = args[1];
714 if (ide_ata66_check(drive, &tfargs))
715 goto abort;
716 }
717
718 err = ide_wait_cmd(drive, args[0], args[1], args[2], args[3], argbuf);
719
720 if (!err && xfer_rate) {
721 /* active-retuning-calls future */
722 ide_set_xfer_rate(drive, xfer_rate);
723 ide_driveid_update(drive);
724 }
725abort:
726 if (copy_to_user((void __user *)arg, argbuf, argsize))
727 err = -EFAULT;
728 if (argsize > 4)
729 kfree(argbuf);
730 return err;
731}
732
733static int ide_wait_cmd_task(ide_drive_t *drive, u8 *buf)
734{
735 struct request rq;
736
737 ide_init_drive_cmd(&rq);
Jens Axboe4aff5e22006-08-10 08:44:47 +0200738 rq.cmd_type = REQ_TYPE_ATA_TASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 rq.buffer = buf;
740 return ide_do_drive_cmd(drive, &rq, ide_wait);
741}
742
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743int ide_task_ioctl (ide_drive_t *drive, unsigned int cmd, unsigned long arg)
744{
745 void __user *p = (void __user *)arg;
746 int err = 0;
747 u8 args[7], *argbuf = args;
748 int argsize = 7;
749
750 if (copy_from_user(args, p, 7))
751 return -EFAULT;
752 err = ide_wait_cmd_task(drive, argbuf);
753 if (copy_to_user(p, argbuf, argsize))
754 err = -EFAULT;
755 return err;
756}
757
758/*
759 * NOTICE: This is additions from IBM to provide a discrete interface,
760 * for selective taskregister access operations. Nice JOB Klaus!!!
761 * Glad to be able to work and co-develop this with you and IBM.
762 */
763ide_startstop_t flagged_taskfile (ide_drive_t *drive, ide_task_t *task)
764{
765 ide_hwif_t *hwif = HWIF(drive);
Bartlomiej Zolnierkiewicz650d8412008-01-25 22:17:06 +0100766 struct ide_taskfile *tf = &task->tf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767
768 if (task->data_phase == TASKFILE_MULTI_IN ||
769 task->data_phase == TASKFILE_MULTI_OUT) {
770 if (!drive->mult_count) {
771 printk(KERN_ERR "%s: multimode not set!\n", drive->name);
772 return ide_stopped;
773 }
774 }
775
776 /*
Bartlomiej Zolnierkiewicze07bc702005-11-19 22:17:55 +0100777 * (ks) Check taskfile in flags.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 * If set, then execute as it is defined.
779 * If not set, then define default settings.
780 * The default values are:
Bartlomiej Zolnierkiewicze07bc702005-11-19 22:17:55 +0100781 * read all taskfile registers (except data)
782 * read the hob registers (sector, nsector, lcyl, hcyl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 if (task->tf_in_flags.all == 0) {
785 task->tf_in_flags.all = IDE_TASKFILE_STD_IN_FLAGS;
786 if (drive->addressing == 1)
787 task->tf_in_flags.all |= (IDE_HOB_STD_IN_FLAGS << 8);
788 }
789
790 /* ALL Command Block Executions SHALL clear nIEN, unless otherwise */
791 if (IDE_CONTROL_REG)
792 /* clear nIEN */
793 hwif->OUTB(drive->ctl, IDE_CONTROL_REG);
794 SELECT_MASK(drive, 0);
795
Bartlomiej Zolnierkiewicz650d8412008-01-25 22:17:06 +0100796 if (task->tf_out_flags.b.data)
797 hwif->OUTW((tf->hob_data << 8) | tf->data, IDE_DATA_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798
799 /* (ks) send hob registers first */
800 if (task->tf_out_flags.b.nsector_hob)
Bartlomiej Zolnierkiewicz650d8412008-01-25 22:17:06 +0100801 hwif->OUTB(tf->hob_nsect, IDE_NSECTOR_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 if (task->tf_out_flags.b.sector_hob)
Bartlomiej Zolnierkiewicz650d8412008-01-25 22:17:06 +0100803 hwif->OUTB(tf->hob_lbal, IDE_SECTOR_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 if (task->tf_out_flags.b.lcyl_hob)
Bartlomiej Zolnierkiewicz650d8412008-01-25 22:17:06 +0100805 hwif->OUTB(tf->hob_lbam, IDE_LCYL_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 if (task->tf_out_flags.b.hcyl_hob)
Bartlomiej Zolnierkiewicz650d8412008-01-25 22:17:06 +0100807 hwif->OUTB(tf->hob_lbah, IDE_HCYL_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808
809 /* (ks) Send now the standard registers */
810 if (task->tf_out_flags.b.error_feature)
Bartlomiej Zolnierkiewicz650d8412008-01-25 22:17:06 +0100811 hwif->OUTB(tf->feature, IDE_FEATURE_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 /* refers to number of sectors to transfer */
813 if (task->tf_out_flags.b.nsector)
Bartlomiej Zolnierkiewicz650d8412008-01-25 22:17:06 +0100814 hwif->OUTB(tf->nsect, IDE_NSECTOR_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 /* refers to sector offset or start sector */
816 if (task->tf_out_flags.b.sector)
Bartlomiej Zolnierkiewicz650d8412008-01-25 22:17:06 +0100817 hwif->OUTB(tf->lbal, IDE_SECTOR_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 if (task->tf_out_flags.b.lcyl)
Bartlomiej Zolnierkiewicz650d8412008-01-25 22:17:06 +0100819 hwif->OUTB(tf->lbam, IDE_LCYL_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 if (task->tf_out_flags.b.hcyl)
Bartlomiej Zolnierkiewicz650d8412008-01-25 22:17:06 +0100821 hwif->OUTB(tf->lbah, IDE_HCYL_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822
823 /*
824 * (ks) In the flagged taskfile approch, we will use all specified
825 * registers and the register value will not be changed, except the
826 * select bit (master/slave) in the drive_head register. We must make
827 * sure that the desired drive is selected.
828 */
Bartlomiej Zolnierkiewicz650d8412008-01-25 22:17:06 +0100829 hwif->OUTB(tf->device | drive->select.all, IDE_SELECT_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 switch(task->data_phase) {
831
832 case TASKFILE_OUT_DMAQ:
833 case TASKFILE_OUT_DMA:
834 case TASKFILE_IN_DMAQ:
835 case TASKFILE_IN_DMA:
Bartlomiej Zolnierkiewiczeda5b352007-10-20 00:32:37 +0200836 if (!drive->using_dma)
837 break;
838
Bartlomiej Zolnierkiewiczdd35b7b2007-10-20 00:32:37 +0200839 if (!hwif->dma_setup(drive)) {
Bartlomiej Zolnierkiewicz650d8412008-01-25 22:17:06 +0100840 hwif->dma_exec_cmd(drive, tf->command);
Bartlomiej Zolnierkiewiczdd35b7b2007-10-20 00:32:37 +0200841 hwif->dma_start(drive);
842 return ide_started;
843 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 break;
845
846 default:
847 if (task->handler == NULL)
848 return ide_stopped;
849
850 /* Issue the command */
851 if (task->prehandler) {
Bartlomiej Zolnierkiewicz650d8412008-01-25 22:17:06 +0100852 hwif->OUTBSYNC(drive, tf->command, IDE_COMMAND_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 ndelay(400); /* FIXME */
854 return task->prehandler(drive, task->rq);
855 }
Bartlomiej Zolnierkiewicz650d8412008-01-25 22:17:06 +0100856 ide_execute_command(drive, tf->command, task->handler, WAIT_WORSTCASE, NULL);
Bartlomiej Zolnierkiewiczdd35b7b2007-10-20 00:32:37 +0200857 return ide_started;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 }
859
Bartlomiej Zolnierkiewiczdd35b7b2007-10-20 00:32:37 +0200860 return ide_stopped;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861}