blob: a7668b459fe12cc56d508aa0db0a7ff90b14d8b5 [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 Zolnierkiewicz9e422372008-01-25 22:17:07 +010075 if (IDE_CONTROL_REG)
76 hwif->OUTB(drive->ctl, IDE_CONTROL_REG); /* clear nIEN */
77
78 if ((task->tf_flags & IDE_TFLAG_NO_SELECT_MASK) == 0)
79 SELECT_MASK(drive, 0);
80
Bartlomiej Zolnierkiewicz74095a92008-01-25 22:17:07 +010081 if (task->tf_flags & IDE_TFLAG_OUT_DATA)
82 hwif->OUTW((tf->hob_data << 8) | tf->data, IDE_DATA_REG);
Bartlomiej Zolnierkiewicz9e422372008-01-25 22:17:07 +010083
Bartlomiej Zolnierkiewicz74095a92008-01-25 22:17:07 +010084 if (task->tf_flags & IDE_TFLAG_OUT_HOB_FEATURE)
85 hwif->OUTB(tf->hob_feature, IDE_FEATURE_REG);
86 if (task->tf_flags & IDE_TFLAG_OUT_HOB_NSECT)
87 hwif->OUTB(tf->hob_nsect, IDE_NSECTOR_REG);
88 if (task->tf_flags & IDE_TFLAG_OUT_HOB_LBAL)
89 hwif->OUTB(tf->hob_lbal, IDE_SECTOR_REG);
90 if (task->tf_flags & IDE_TFLAG_OUT_HOB_LBAM)
91 hwif->OUTB(tf->hob_lbam, IDE_LCYL_REG);
92 if (task->tf_flags & IDE_TFLAG_OUT_HOB_LBAH)
93 hwif->OUTB(tf->hob_lbah, IDE_HCYL_REG);
94
95 if (task->tf_flags & IDE_TFLAG_OUT_FEATURE)
96 hwif->OUTB(tf->feature, IDE_FEATURE_REG);
97 if (task->tf_flags & IDE_TFLAG_OUT_NSECT)
98 hwif->OUTB(tf->nsect, IDE_NSECTOR_REG);
99 if (task->tf_flags & IDE_TFLAG_OUT_LBAL)
100 hwif->OUTB(tf->lbal, IDE_SECTOR_REG);
101 if (task->tf_flags & IDE_TFLAG_OUT_LBAM)
102 hwif->OUTB(tf->lbam, IDE_LCYL_REG);
103 if (task->tf_flags & IDE_TFLAG_OUT_LBAH)
104 hwif->OUTB(tf->lbah, IDE_HCYL_REG);
Bartlomiej Zolnierkiewicz9e422372008-01-25 22:17:07 +0100105
106 hwif->OUTB((tf->device & HIHI) | drive->select.all, IDE_SELECT_REG);
107}
108
109EXPORT_SYMBOL_GPL(ide_tf_load);
110
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111int taskfile_lib_get_identify (ide_drive_t *drive, u8 *buf)
112{
113 ide_task_t args;
Bartlomiej Zolnierkiewicz650d8412008-01-25 22:17:06 +0100114
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 memset(&args, 0, sizeof(ide_task_t));
Bartlomiej Zolnierkiewicz650d8412008-01-25 22:17:06 +0100116 args.tf.nsect = 0x01;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 if (drive->media == ide_disk)
Bartlomiej Zolnierkiewicz650d8412008-01-25 22:17:06 +0100118 args.tf.command = WIN_IDENTIFY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 else
Bartlomiej Zolnierkiewicz650d8412008-01-25 22:17:06 +0100120 args.tf.command = WIN_PIDENTIFY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 args.command_type = IDE_DRIVE_TASK_IN;
122 args.data_phase = TASKFILE_IN;
123 args.handler = &task_in_intr;
124 return ide_raw_taskfile(drive, &args, buf);
125}
126
Bartlomiej Zolnierkiewicz74095a92008-01-25 22:17:07 +0100127static int inline task_dma_ok(ide_task_t *task)
128{
129 if (task->tf_flags & IDE_TFLAG_FLAGGED)
130 return 1;
131
132 switch (task->tf.command) {
133 case WIN_WRITEDMA_ONCE:
134 case WIN_WRITEDMA:
135 case WIN_WRITEDMA_EXT:
136 case WIN_READDMA_ONCE:
137 case WIN_READDMA:
138 case WIN_READDMA_EXT:
139 case WIN_IDENTIFY_DMA:
140 return 1;
141 }
142
143 return 0;
144}
145
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146ide_startstop_t do_rw_taskfile (ide_drive_t *drive, ide_task_t *task)
147{
148 ide_hwif_t *hwif = HWIF(drive);
Bartlomiej Zolnierkiewicz650d8412008-01-25 22:17:06 +0100149 struct ide_taskfile *tf = &task->tf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
Bartlomiej Zolnierkiewicz9e422372008-01-25 22:17:07 +0100151 ide_tf_load(drive, task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
153 if (task->handler != NULL) {
154 if (task->prehandler != NULL) {
Bartlomiej Zolnierkiewicz650d8412008-01-25 22:17:06 +0100155 hwif->OUTBSYNC(drive, tf->command, IDE_COMMAND_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 ndelay(400); /* FIXME */
157 return task->prehandler(drive, task->rq);
158 }
Bartlomiej Zolnierkiewicz650d8412008-01-25 22:17:06 +0100159 ide_execute_command(drive, tf->command, task->handler, WAIT_WORSTCASE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 return ide_started;
161 }
162
Bartlomiej Zolnierkiewicz74095a92008-01-25 22:17:07 +0100163 if (task_dma_ok(task) && drive->using_dma && !hwif->dma_setup(drive)) {
164 hwif->dma_exec_cmd(drive, tf->command);
165 hwif->dma_start(drive);
166 return ide_started;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 }
168
169 return ide_stopped;
170}
171
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172/*
173 * set_multmode_intr() is invoked on completion of a WIN_SETMULT cmd.
174 */
175ide_startstop_t set_multmode_intr (ide_drive_t *drive)
176{
177 ide_hwif_t *hwif = HWIF(drive);
178 u8 stat;
179
180 if (OK_STAT(stat = hwif->INB(IDE_STATUS_REG),READY_STAT,BAD_STAT)) {
181 drive->mult_count = drive->mult_req;
182 } else {
183 drive->mult_req = drive->mult_count = 0;
184 drive->special.b.recalibrate = 1;
185 (void) ide_dump_status(drive, "set_multmode", stat);
186 }
187 return ide_stopped;
188}
189
190/*
191 * set_geometry_intr() is invoked on completion of a WIN_SPECIFY cmd.
192 */
193ide_startstop_t set_geometry_intr (ide_drive_t *drive)
194{
195 ide_hwif_t *hwif = HWIF(drive);
196 int retries = 5;
197 u8 stat;
198
199 while (((stat = hwif->INB(IDE_STATUS_REG)) & BUSY_STAT) && retries--)
200 udelay(10);
201
202 if (OK_STAT(stat, READY_STAT, BAD_STAT))
203 return ide_stopped;
204
205 if (stat & (ERR_STAT|DRQ_STAT))
206 return ide_error(drive, "set_geometry_intr", stat);
207
Eric Sesterhenn125e1872006-06-23 02:06:06 -0700208 BUG_ON(HWGROUP(drive)->handler != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 ide_set_handler(drive, &set_geometry_intr, WAIT_WORSTCASE, NULL);
210 return ide_started;
211}
212
213/*
214 * recal_intr() is invoked on completion of a WIN_RESTORE (recalibrate) cmd.
215 */
216ide_startstop_t recal_intr (ide_drive_t *drive)
217{
218 ide_hwif_t *hwif = HWIF(drive);
219 u8 stat;
220
221 if (!OK_STAT(stat = hwif->INB(IDE_STATUS_REG), READY_STAT, BAD_STAT))
222 return ide_error(drive, "recal_intr", stat);
223 return ide_stopped;
224}
225
226/*
227 * Handler for commands without a data phase
228 */
229ide_startstop_t task_no_data_intr (ide_drive_t *drive)
230{
231 ide_task_t *args = HWGROUP(drive)->rq->special;
232 ide_hwif_t *hwif = HWIF(drive);
233 u8 stat;
234
Ingo Molnar366c7f52006-07-03 00:25:25 -0700235 local_irq_enable_in_hardirq();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 if (!OK_STAT(stat = hwif->INB(IDE_STATUS_REG),READY_STAT,BAD_STAT)) {
237 return ide_error(drive, "task_no_data_intr", stat);
238 /* calls ide_end_drive_cmd */
239 }
240 if (args)
241 ide_end_drive_cmd(drive, stat, hwif->INB(IDE_ERROR_REG));
242
243 return ide_stopped;
244}
245
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246static u8 wait_drive_not_busy(ide_drive_t *drive)
247{
248 ide_hwif_t *hwif = HWIF(drive);
Masatake YAMATOb42fa132007-07-03 22:28:34 +0200249 int retries;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 u8 stat;
251
252 /*
253 * Last sector was transfered, wait until drive is ready.
254 * This can take up to 10 usec, but we will wait max 1 ms
255 * (drive_cmd_intr() waits that long).
256 */
Masatake YAMATOb42fa132007-07-03 22:28:34 +0200257 for (retries = 0; retries < 100; retries++) {
258 if ((stat = hwif->INB(IDE_STATUS_REG)) & BUSY_STAT)
259 udelay(10);
260 else
261 break;
262 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
Masatake YAMATOb42fa132007-07-03 22:28:34 +0200264 if (stat & BUSY_STAT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 printk(KERN_ERR "%s: drive still BUSY!\n", drive->name);
266
267 return stat;
268}
269
270static void ide_pio_sector(ide_drive_t *drive, unsigned int write)
271{
272 ide_hwif_t *hwif = drive->hwif;
273 struct scatterlist *sg = hwif->sg_table;
Jens Axboe55c16a72007-07-25 08:13:56 +0200274 struct scatterlist *cursg = hwif->cursg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 struct page *page;
276#ifdef CONFIG_HIGHMEM
277 unsigned long flags;
278#endif
279 unsigned int offset;
280 u8 *buf;
281
Jens Axboe55c16a72007-07-25 08:13:56 +0200282 cursg = hwif->cursg;
283 if (!cursg) {
284 cursg = sg;
285 hwif->cursg = sg;
286 }
287
Jens Axboe45711f12007-10-22 21:19:53 +0200288 page = sg_page(cursg);
Jens Axboe55c16a72007-07-25 08:13:56 +0200289 offset = cursg->offset + hwif->cursg_ofs * SECTOR_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290
291 /* get the current page and offset */
292 page = nth_page(page, (offset >> PAGE_SHIFT));
293 offset %= PAGE_SIZE;
294
295#ifdef CONFIG_HIGHMEM
296 local_irq_save(flags);
297#endif
298 buf = kmap_atomic(page, KM_BIO_SRC_IRQ) + offset;
299
300 hwif->nleft--;
301 hwif->cursg_ofs++;
302
Jens Axboe55c16a72007-07-25 08:13:56 +0200303 if ((hwif->cursg_ofs * SECTOR_SIZE) == cursg->length) {
304 hwif->cursg = sg_next(hwif->cursg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 hwif->cursg_ofs = 0;
306 }
307
308 /* do the actual data transfer */
309 if (write)
310 taskfile_output_data(drive, buf, SECTOR_WORDS);
311 else
312 taskfile_input_data(drive, buf, SECTOR_WORDS);
313
314 kunmap_atomic(buf, KM_BIO_SRC_IRQ);
315#ifdef CONFIG_HIGHMEM
316 local_irq_restore(flags);
317#endif
318}
319
320static void ide_pio_multi(ide_drive_t *drive, unsigned int write)
321{
322 unsigned int nsect;
323
324 nsect = min_t(unsigned int, drive->hwif->nleft, drive->mult_count);
325 while (nsect--)
326 ide_pio_sector(drive, write);
327}
328
Arjan van de Ven858119e2006-01-14 13:20:43 -0800329static void ide_pio_datablock(ide_drive_t *drive, struct request *rq,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 unsigned int write)
331{
332 if (rq->bio) /* fs request */
333 rq->errors = 0;
334
Andrew Morton651c29a2006-02-15 15:17:37 -0800335 touch_softlockup_watchdog();
336
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 switch (drive->hwif->data_phase) {
338 case TASKFILE_MULTI_IN:
339 case TASKFILE_MULTI_OUT:
340 ide_pio_multi(drive, write);
341 break;
342 default:
343 ide_pio_sector(drive, write);
344 break;
345 }
346}
347
348static ide_startstop_t task_error(ide_drive_t *drive, struct request *rq,
349 const char *s, u8 stat)
350{
351 if (rq->bio) {
352 ide_hwif_t *hwif = drive->hwif;
353 int sectors = hwif->nsect - hwif->nleft;
354
355 switch (hwif->data_phase) {
356 case TASKFILE_IN:
357 if (hwif->nleft)
358 break;
359 /* fall through */
360 case TASKFILE_OUT:
361 sectors--;
362 break;
363 case TASKFILE_MULTI_IN:
364 if (hwif->nleft)
365 break;
366 /* fall through */
367 case TASKFILE_MULTI_OUT:
368 sectors -= drive->mult_count;
369 default:
370 break;
371 }
372
373 if (sectors > 0) {
374 ide_driver_t *drv;
375
376 drv = *(ide_driver_t **)rq->rq_disk->private_data;
377 drv->end_request(drive, 1, sectors);
378 }
379 }
380 return ide_error(drive, s, stat);
381}
382
383static void task_end_request(ide_drive_t *drive, struct request *rq, u8 stat)
384{
Jens Axboe55c16a72007-07-25 08:13:56 +0200385 HWIF(drive)->cursg = NULL;
386
Jens Axboe4aff5e22006-08-10 08:44:47 +0200387 if (rq->cmd_type == REQ_TYPE_ATA_TASKFILE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 ide_task_t *task = rq->special;
389
Bartlomiej Zolnierkiewicz74095a92008-01-25 22:17:07 +0100390 if (task->tf_flags & IDE_TFLAG_FLAGGED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 u8 err = drive->hwif->INB(IDE_ERROR_REG);
392 ide_end_drive_cmd(drive, stat, err);
393 return;
394 }
395 }
396
Richard Purdie03731fb2006-03-31 02:31:15 -0800397 if (rq->rq_disk) {
398 ide_driver_t *drv;
399
400 drv = *(ide_driver_t **)rq->rq_disk->private_data;;
401 drv->end_request(drive, 1, rq->hard_nr_sectors);
402 } else
403 ide_end_request(drive, 1, rq->hard_nr_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404}
405
406/*
407 * Handler for command with PIO data-in phase (Read/Read Multiple).
408 */
409ide_startstop_t task_in_intr (ide_drive_t *drive)
410{
411 ide_hwif_t *hwif = drive->hwif;
412 struct request *rq = HWGROUP(drive)->rq;
413 u8 stat = hwif->INB(IDE_STATUS_REG);
414
415 /* new way for dealing with premature shared PCI interrupts */
416 if (!OK_STAT(stat, DATA_READY, BAD_R_STAT)) {
417 if (stat & (ERR_STAT | DRQ_STAT))
418 return task_error(drive, rq, __FUNCTION__, stat);
419 /* No data yet, so wait for another IRQ. */
420 ide_set_handler(drive, &task_in_intr, WAIT_WORSTCASE, NULL);
421 return ide_started;
422 }
423
424 ide_pio_datablock(drive, rq, 0);
425
426 /* If it was the last datablock check status and finish transfer. */
427 if (!hwif->nleft) {
428 stat = wait_drive_not_busy(drive);
429 if (!OK_STAT(stat, 0, BAD_R_STAT))
430 return task_error(drive, rq, __FUNCTION__, stat);
431 task_end_request(drive, rq, stat);
432 return ide_stopped;
433 }
434
435 /* Still data left to transfer. */
436 ide_set_handler(drive, &task_in_intr, WAIT_WORSTCASE, NULL);
437
438 return ide_started;
439}
440EXPORT_SYMBOL(task_in_intr);
441
442/*
443 * Handler for command with PIO data-out phase (Write/Write Multiple).
444 */
445static ide_startstop_t task_out_intr (ide_drive_t *drive)
446{
447 ide_hwif_t *hwif = drive->hwif;
448 struct request *rq = HWGROUP(drive)->rq;
449 u8 stat = hwif->INB(IDE_STATUS_REG);
450
451 if (!OK_STAT(stat, DRIVE_READY, drive->bad_wstat))
452 return task_error(drive, rq, __FUNCTION__, stat);
453
454 /* Deal with unexpected ATA data phase. */
455 if (((stat & DRQ_STAT) == 0) ^ !hwif->nleft)
456 return task_error(drive, rq, __FUNCTION__, stat);
457
458 if (!hwif->nleft) {
459 task_end_request(drive, rq, stat);
460 return ide_stopped;
461 }
462
463 /* Still data left to transfer. */
464 ide_pio_datablock(drive, rq, 1);
465 ide_set_handler(drive, &task_out_intr, WAIT_WORSTCASE, NULL);
466
467 return ide_started;
468}
469
470ide_startstop_t pre_task_out_intr (ide_drive_t *drive, struct request *rq)
471{
472 ide_startstop_t startstop;
473
474 if (ide_wait_stat(&startstop, drive, DATA_READY,
475 drive->bad_wstat, WAIT_DRQ)) {
476 printk(KERN_ERR "%s: no DRQ after issuing %sWRITE%s\n",
477 drive->name,
478 drive->hwif->data_phase ? "MULT" : "",
479 drive->addressing ? "_EXT" : "");
480 return startstop;
481 }
482
483 if (!drive->unmask)
484 local_irq_disable();
485
486 ide_set_handler(drive, &task_out_intr, WAIT_WORSTCASE, NULL);
487 ide_pio_datablock(drive, rq, 1);
488
489 return ide_started;
490}
491EXPORT_SYMBOL(pre_task_out_intr);
492
493static int ide_diag_taskfile(ide_drive_t *drive, ide_task_t *args, unsigned long data_size, u8 *buf)
494{
495 struct request rq;
496
497 memset(&rq, 0, sizeof(rq));
Bartlomiej Zolnierkiewicz02ac2462007-11-05 21:42:27 +0100498 rq.ref_count = 1;
Jens Axboe4aff5e22006-08-10 08:44:47 +0200499 rq.cmd_type = REQ_TYPE_ATA_TASKFILE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 rq.buffer = buf;
501
502 /*
503 * (ks) We transfer currently only whole sectors.
504 * This is suffient for now. But, it would be great,
505 * if we would find a solution to transfer any size.
506 * To support special commands like READ LONG.
507 */
508 if (args->command_type != IDE_DRIVE_TASK_NO_DATA) {
509 if (data_size == 0)
Bartlomiej Zolnierkiewicz650d8412008-01-25 22:17:06 +0100510 rq.nr_sectors = (args->tf.hob_nsect << 8) | args->tf.nsect;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 else
512 rq.nr_sectors = data_size / SECTOR_SIZE;
513
514 if (!rq.nr_sectors) {
515 printk(KERN_ERR "%s: in/out command without data\n",
516 drive->name);
517 return -EFAULT;
518 }
519
520 rq.hard_nr_sectors = rq.nr_sectors;
521 rq.hard_cur_sectors = rq.current_nr_sectors = rq.nr_sectors;
522
523 if (args->command_type == IDE_DRIVE_TASK_RAW_WRITE)
Jens Axboe4aff5e22006-08-10 08:44:47 +0200524 rq.cmd_flags |= REQ_RW;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 }
526
527 rq.special = args;
Timothy Thelinef0f6a432005-09-16 19:28:16 -0700528 args->rq = &rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 return ide_do_drive_cmd(drive, &rq, ide_wait);
530}
531
532int ide_raw_taskfile (ide_drive_t *drive, ide_task_t *args, u8 *buf)
533{
534 return ide_diag_taskfile(drive, args, 0, buf);
535}
536
537EXPORT_SYMBOL(ide_raw_taskfile);
538
Bartlomiej Zolnierkiewicz9a3c49b2008-01-25 22:17:07 +0100539int ide_no_data_taskfile(ide_drive_t *drive, ide_task_t *task)
540{
541 task->command_type = IDE_DRIVE_TASK_NO_DATA;
542 task->data_phase = TASKFILE_NO_DATA;
543 task->handler = task_no_data_intr;
544
545 return ide_raw_taskfile(drive, task, NULL);
546}
547EXPORT_SYMBOL_GPL(ide_no_data_taskfile);
548
Bartlomiej Zolnierkiewicz26a5b042007-11-05 21:42:27 +0100549#ifdef CONFIG_IDE_TASK_IOCTL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550int ide_taskfile_ioctl (ide_drive_t *drive, unsigned int cmd, unsigned long arg)
551{
552 ide_task_request_t *req_task;
553 ide_task_t args;
554 u8 *outbuf = NULL;
555 u8 *inbuf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 int err = 0;
557 int tasksize = sizeof(struct ide_task_request_s);
Alan Cox3a42bb22006-10-16 16:31:02 +0100558 unsigned int taskin = 0;
559 unsigned int taskout = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 u8 io_32bit = drive->io_32bit;
561 char __user *buf = (char __user *)arg;
562
563// printk("IDE Taskfile ...\n");
564
Deepak Saxenaf5e3c2f2005-11-07 01:01:25 -0800565 req_task = kzalloc(tasksize, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 if (req_task == NULL) return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 if (copy_from_user(req_task, buf, tasksize)) {
568 kfree(req_task);
569 return -EFAULT;
570 }
571
Alan Cox3a42bb22006-10-16 16:31:02 +0100572 taskout = req_task->out_size;
573 taskin = req_task->in_size;
574
575 if (taskin > 65536 || taskout > 65536) {
576 err = -EINVAL;
577 goto abort;
578 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579
580 if (taskout) {
581 int outtotal = tasksize;
Deepak Saxenaf5e3c2f2005-11-07 01:01:25 -0800582 outbuf = kzalloc(taskout, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 if (outbuf == NULL) {
584 err = -ENOMEM;
585 goto abort;
586 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 if (copy_from_user(outbuf, buf + outtotal, taskout)) {
588 err = -EFAULT;
589 goto abort;
590 }
591 }
592
593 if (taskin) {
594 int intotal = tasksize + taskout;
Deepak Saxenaf5e3c2f2005-11-07 01:01:25 -0800595 inbuf = kzalloc(taskin, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 if (inbuf == NULL) {
597 err = -ENOMEM;
598 goto abort;
599 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 if (copy_from_user(inbuf, buf + intotal, taskin)) {
601 err = -EFAULT;
602 goto abort;
603 }
604 }
605
606 memset(&args, 0, sizeof(ide_task_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607
Bartlomiej Zolnierkiewicz650d8412008-01-25 22:17:06 +0100608 memcpy(&args.tf_array[0], req_task->hob_ports, HDIO_DRIVE_HOB_HDR_SIZE - 2);
609 memcpy(&args.tf_array[6], req_task->io_ports, HDIO_DRIVE_TASK_HDR_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 args.tf_in_flags = req_task->in_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 args.data_phase = req_task->data_phase;
612 args.command_type = req_task->req_cmd;
613
Bartlomiej Zolnierkiewicz74095a92008-01-25 22:17:07 +0100614 if (req_task->out_flags.all) {
615 args.tf_flags |= IDE_TFLAG_FLAGGED;
616
617 if (req_task->out_flags.b.data)
618 args.tf_flags |= IDE_TFLAG_OUT_DATA;
619
620 if (req_task->out_flags.b.nsector_hob)
621 args.tf_flags |= IDE_TFLAG_OUT_HOB_NSECT;
622 if (req_task->out_flags.b.sector_hob)
623 args.tf_flags |= IDE_TFLAG_OUT_HOB_LBAL;
624 if (req_task->out_flags.b.lcyl_hob)
625 args.tf_flags |= IDE_TFLAG_OUT_HOB_LBAM;
626 if (req_task->out_flags.b.hcyl_hob)
627 args.tf_flags |= IDE_TFLAG_OUT_HOB_LBAH;
628
629 if (req_task->out_flags.b.error_feature)
630 args.tf_flags |= IDE_TFLAG_OUT_FEATURE;
631 if (req_task->out_flags.b.nsector)
632 args.tf_flags |= IDE_TFLAG_OUT_NSECT;
633 if (req_task->out_flags.b.sector)
634 args.tf_flags |= IDE_TFLAG_OUT_LBAL;
635 if (req_task->out_flags.b.lcyl)
636 args.tf_flags |= IDE_TFLAG_OUT_LBAM;
637 if (req_task->out_flags.b.hcyl)
638 args.tf_flags |= IDE_TFLAG_OUT_LBAH;
639 }
640
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 drive->io_32bit = 0;
642 switch(req_task->data_phase) {
643 case TASKFILE_OUT_DMAQ:
644 case TASKFILE_OUT_DMA:
645 err = ide_diag_taskfile(drive, &args, taskout, outbuf);
646 break;
647 case TASKFILE_IN_DMAQ:
648 case TASKFILE_IN_DMA:
649 err = ide_diag_taskfile(drive, &args, taskin, inbuf);
650 break;
651 case TASKFILE_MULTI_OUT:
652 if (!drive->mult_count) {
653 /* (hs): give up if multcount is not set */
654 printk(KERN_ERR "%s: %s Multimode Write " \
655 "multcount is not set\n",
656 drive->name, __FUNCTION__);
657 err = -EPERM;
658 goto abort;
659 }
660 /* fall through */
661 case TASKFILE_OUT:
662 args.prehandler = &pre_task_out_intr;
663 args.handler = &task_out_intr;
664 err = ide_diag_taskfile(drive, &args, taskout, outbuf);
665 break;
666 case TASKFILE_MULTI_IN:
667 if (!drive->mult_count) {
668 /* (hs): give up if multcount is not set */
669 printk(KERN_ERR "%s: %s Multimode Read failure " \
670 "multcount is not set\n",
671 drive->name, __FUNCTION__);
672 err = -EPERM;
673 goto abort;
674 }
675 /* fall through */
676 case TASKFILE_IN:
677 args.handler = &task_in_intr;
678 err = ide_diag_taskfile(drive, &args, taskin, inbuf);
679 break;
680 case TASKFILE_NO_DATA:
681 args.handler = &task_no_data_intr;
682 err = ide_diag_taskfile(drive, &args, 0, NULL);
683 break;
684 default:
685 err = -EFAULT;
686 goto abort;
687 }
688
Bartlomiej Zolnierkiewicz650d8412008-01-25 22:17:06 +0100689 memcpy(req_task->hob_ports, &args.tf_array[0], HDIO_DRIVE_HOB_HDR_SIZE - 2);
690 memcpy(req_task->io_ports, &args.tf_array[6], HDIO_DRIVE_TASK_HDR_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 req_task->in_flags = args.tf_in_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692
693 if (copy_to_user(buf, req_task, tasksize)) {
694 err = -EFAULT;
695 goto abort;
696 }
697 if (taskout) {
698 int outtotal = tasksize;
699 if (copy_to_user(buf + outtotal, outbuf, taskout)) {
700 err = -EFAULT;
701 goto abort;
702 }
703 }
704 if (taskin) {
705 int intotal = tasksize + taskout;
706 if (copy_to_user(buf + intotal, inbuf, taskin)) {
707 err = -EFAULT;
708 goto abort;
709 }
710 }
711abort:
712 kfree(req_task);
Jesper Juhl6044ec82005-11-07 01:01:32 -0800713 kfree(outbuf);
714 kfree(inbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715
716// printk("IDE Taskfile ioctl ended. rc = %i\n", err);
717
718 drive->io_32bit = io_32bit;
719
720 return err;
721}
Bartlomiej Zolnierkiewicz26a5b042007-11-05 21:42:27 +0100722#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723
724int ide_wait_cmd (ide_drive_t *drive, u8 cmd, u8 nsect, u8 feature, u8 sectors, u8 *buf)
725{
726 struct request rq;
727 u8 buffer[4];
728
729 if (!buf)
730 buf = buffer;
731 memset(buf, 0, 4 + SECTOR_WORDS * 4 * sectors);
732 ide_init_drive_cmd(&rq);
733 rq.buffer = buf;
734 *buf++ = cmd;
735 *buf++ = nsect;
736 *buf++ = feature;
737 *buf++ = sectors;
738 return ide_do_drive_cmd(drive, &rq, ide_wait);
739}
740
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741int ide_cmd_ioctl (ide_drive_t *drive, unsigned int cmd, unsigned long arg)
742{
743 int err = 0;
744 u8 args[4], *argbuf = args;
745 u8 xfer_rate = 0;
746 int argsize = 4;
747 ide_task_t tfargs;
Bartlomiej Zolnierkiewicz650d8412008-01-25 22:17:06 +0100748 struct ide_taskfile *tf = &tfargs.tf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749
750 if (NULL == (void *) arg) {
751 struct request rq;
752 ide_init_drive_cmd(&rq);
753 return ide_do_drive_cmd(drive, &rq, ide_wait);
754 }
755
756 if (copy_from_user(args, (void __user *)arg, 4))
757 return -EFAULT;
758
759 memset(&tfargs, 0, sizeof(ide_task_t));
Bartlomiej Zolnierkiewicz650d8412008-01-25 22:17:06 +0100760 tf->feature = args[2];
761 tf->nsect = args[3];
762 tf->lbal = args[1];
763 tf->command = args[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764
765 if (args[3]) {
766 argsize = 4 + (SECTOR_WORDS * 4 * args[3]);
Deepak Saxenaf5e3c2f2005-11-07 01:01:25 -0800767 argbuf = kzalloc(argsize, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 if (argbuf == NULL)
769 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 }
771 if (set_transfer(drive, &tfargs)) {
772 xfer_rate = args[1];
773 if (ide_ata66_check(drive, &tfargs))
774 goto abort;
775 }
776
777 err = ide_wait_cmd(drive, args[0], args[1], args[2], args[3], argbuf);
778
779 if (!err && xfer_rate) {
780 /* active-retuning-calls future */
781 ide_set_xfer_rate(drive, xfer_rate);
782 ide_driveid_update(drive);
783 }
784abort:
785 if (copy_to_user((void __user *)arg, argbuf, argsize))
786 err = -EFAULT;
787 if (argsize > 4)
788 kfree(argbuf);
789 return err;
790}
791
792static int ide_wait_cmd_task(ide_drive_t *drive, u8 *buf)
793{
794 struct request rq;
795
796 ide_init_drive_cmd(&rq);
Jens Axboe4aff5e22006-08-10 08:44:47 +0200797 rq.cmd_type = REQ_TYPE_ATA_TASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 rq.buffer = buf;
799 return ide_do_drive_cmd(drive, &rq, ide_wait);
800}
801
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802int ide_task_ioctl (ide_drive_t *drive, unsigned int cmd, unsigned long arg)
803{
804 void __user *p = (void __user *)arg;
805 int err = 0;
806 u8 args[7], *argbuf = args;
807 int argsize = 7;
808
809 if (copy_from_user(args, p, 7))
810 return -EFAULT;
811 err = ide_wait_cmd_task(drive, argbuf);
812 if (copy_to_user(p, argbuf, argsize))
813 err = -EFAULT;
814 return err;
815}
816
817/*
818 * NOTICE: This is additions from IBM to provide a discrete interface,
819 * for selective taskregister access operations. Nice JOB Klaus!!!
820 * Glad to be able to work and co-develop this with you and IBM.
821 */
822ide_startstop_t flagged_taskfile (ide_drive_t *drive, ide_task_t *task)
823{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 if (task->data_phase == TASKFILE_MULTI_IN ||
825 task->data_phase == TASKFILE_MULTI_OUT) {
826 if (!drive->mult_count) {
827 printk(KERN_ERR "%s: multimode not set!\n", drive->name);
828 return ide_stopped;
829 }
830 }
831
832 /*
Bartlomiej Zolnierkiewicze07bc702005-11-19 22:17:55 +0100833 * (ks) Check taskfile in flags.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 * If set, then execute as it is defined.
835 * If not set, then define default settings.
836 * The default values are:
Bartlomiej Zolnierkiewicze07bc702005-11-19 22:17:55 +0100837 * read all taskfile registers (except data)
838 * read the hob registers (sector, nsector, lcyl, hcyl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 if (task->tf_in_flags.all == 0) {
841 task->tf_in_flags.all = IDE_TASKFILE_STD_IN_FLAGS;
842 if (drive->addressing == 1)
843 task->tf_in_flags.all |= (IDE_HOB_STD_IN_FLAGS << 8);
844 }
845
Bartlomiej Zolnierkiewicz74095a92008-01-25 22:17:07 +0100846 return do_rw_taskfile(drive, task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847}