blob: 5f6d01a4222c3122397e480526065be314ab536e [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
Bartlomiej Zolnierkiewicz10d90152008-01-25 22:17:16 +0100173 switch (task->data_phase) {
174 case TASKFILE_MULTI_OUT:
175 case TASKFILE_OUT:
176 hwif->OUTBSYNC(drive, tf->command, IDE_COMMAND_REG);
177 ndelay(400); /* FIXME */
178 return pre_task_out_intr(drive, task->rq);
179 case TASKFILE_MULTI_IN:
180 case TASKFILE_IN:
181 case TASKFILE_NO_DATA:
Bartlomiej Zolnierkiewicz650d8412008-01-25 22:17:06 +0100182 ide_execute_command(drive, tf->command, task->handler, WAIT_WORSTCASE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 return ide_started;
Bartlomiej Zolnierkiewicz10d90152008-01-25 22:17:16 +0100184 default:
185 if (task_dma_ok(task) == 0 || drive->using_dma == 0 ||
186 hwif->dma_setup(drive))
187 return ide_stopped;
Bartlomiej Zolnierkiewicz74095a92008-01-25 22:17:07 +0100188 hwif->dma_exec_cmd(drive, tf->command);
189 hwif->dma_start(drive);
190 return ide_started;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192}
193
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194/*
195 * set_multmode_intr() is invoked on completion of a WIN_SETMULT cmd.
196 */
197ide_startstop_t set_multmode_intr (ide_drive_t *drive)
198{
199 ide_hwif_t *hwif = HWIF(drive);
200 u8 stat;
201
202 if (OK_STAT(stat = hwif->INB(IDE_STATUS_REG),READY_STAT,BAD_STAT)) {
203 drive->mult_count = drive->mult_req;
204 } else {
205 drive->mult_req = drive->mult_count = 0;
206 drive->special.b.recalibrate = 1;
207 (void) ide_dump_status(drive, "set_multmode", stat);
208 }
209 return ide_stopped;
210}
211
212/*
213 * set_geometry_intr() is invoked on completion of a WIN_SPECIFY cmd.
214 */
215ide_startstop_t set_geometry_intr (ide_drive_t *drive)
216{
217 ide_hwif_t *hwif = HWIF(drive);
218 int retries = 5;
219 u8 stat;
220
221 while (((stat = hwif->INB(IDE_STATUS_REG)) & BUSY_STAT) && retries--)
222 udelay(10);
223
224 if (OK_STAT(stat, READY_STAT, BAD_STAT))
225 return ide_stopped;
226
227 if (stat & (ERR_STAT|DRQ_STAT))
228 return ide_error(drive, "set_geometry_intr", stat);
229
Eric Sesterhenn125e1872006-06-23 02:06:06 -0700230 BUG_ON(HWGROUP(drive)->handler != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 ide_set_handler(drive, &set_geometry_intr, WAIT_WORSTCASE, NULL);
232 return ide_started;
233}
234
235/*
236 * recal_intr() is invoked on completion of a WIN_RESTORE (recalibrate) cmd.
237 */
238ide_startstop_t recal_intr (ide_drive_t *drive)
239{
240 ide_hwif_t *hwif = HWIF(drive);
241 u8 stat;
242
243 if (!OK_STAT(stat = hwif->INB(IDE_STATUS_REG), READY_STAT, BAD_STAT))
244 return ide_error(drive, "recal_intr", stat);
245 return ide_stopped;
246}
247
248/*
249 * Handler for commands without a data phase
250 */
251ide_startstop_t task_no_data_intr (ide_drive_t *drive)
252{
253 ide_task_t *args = HWGROUP(drive)->rq->special;
254 ide_hwif_t *hwif = HWIF(drive);
255 u8 stat;
256
Ingo Molnar366c7f52006-07-03 00:25:25 -0700257 local_irq_enable_in_hardirq();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 if (!OK_STAT(stat = hwif->INB(IDE_STATUS_REG),READY_STAT,BAD_STAT)) {
259 return ide_error(drive, "task_no_data_intr", stat);
260 /* calls ide_end_drive_cmd */
261 }
262 if (args)
263 ide_end_drive_cmd(drive, stat, hwif->INB(IDE_ERROR_REG));
264
265 return ide_stopped;
266}
267
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268static u8 wait_drive_not_busy(ide_drive_t *drive)
269{
270 ide_hwif_t *hwif = HWIF(drive);
Masatake YAMATOb42fa132007-07-03 22:28:34 +0200271 int retries;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 u8 stat;
273
274 /*
275 * Last sector was transfered, wait until drive is ready.
276 * This can take up to 10 usec, but we will wait max 1 ms
277 * (drive_cmd_intr() waits that long).
278 */
Masatake YAMATOb42fa132007-07-03 22:28:34 +0200279 for (retries = 0; retries < 100; retries++) {
280 if ((stat = hwif->INB(IDE_STATUS_REG)) & BUSY_STAT)
281 udelay(10);
282 else
283 break;
284 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285
Masatake YAMATOb42fa132007-07-03 22:28:34 +0200286 if (stat & BUSY_STAT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 printk(KERN_ERR "%s: drive still BUSY!\n", drive->name);
288
289 return stat;
290}
291
292static void ide_pio_sector(ide_drive_t *drive, unsigned int write)
293{
294 ide_hwif_t *hwif = drive->hwif;
295 struct scatterlist *sg = hwif->sg_table;
Jens Axboe55c16a72007-07-25 08:13:56 +0200296 struct scatterlist *cursg = hwif->cursg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 struct page *page;
298#ifdef CONFIG_HIGHMEM
299 unsigned long flags;
300#endif
301 unsigned int offset;
302 u8 *buf;
303
Jens Axboe55c16a72007-07-25 08:13:56 +0200304 cursg = hwif->cursg;
305 if (!cursg) {
306 cursg = sg;
307 hwif->cursg = sg;
308 }
309
Jens Axboe45711f12007-10-22 21:19:53 +0200310 page = sg_page(cursg);
Jens Axboe55c16a72007-07-25 08:13:56 +0200311 offset = cursg->offset + hwif->cursg_ofs * SECTOR_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
313 /* get the current page and offset */
314 page = nth_page(page, (offset >> PAGE_SHIFT));
315 offset %= PAGE_SIZE;
316
317#ifdef CONFIG_HIGHMEM
318 local_irq_save(flags);
319#endif
320 buf = kmap_atomic(page, KM_BIO_SRC_IRQ) + offset;
321
322 hwif->nleft--;
323 hwif->cursg_ofs++;
324
Jens Axboe55c16a72007-07-25 08:13:56 +0200325 if ((hwif->cursg_ofs * SECTOR_SIZE) == cursg->length) {
326 hwif->cursg = sg_next(hwif->cursg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 hwif->cursg_ofs = 0;
328 }
329
330 /* do the actual data transfer */
331 if (write)
332 taskfile_output_data(drive, buf, SECTOR_WORDS);
333 else
334 taskfile_input_data(drive, buf, SECTOR_WORDS);
335
336 kunmap_atomic(buf, KM_BIO_SRC_IRQ);
337#ifdef CONFIG_HIGHMEM
338 local_irq_restore(flags);
339#endif
340}
341
342static void ide_pio_multi(ide_drive_t *drive, unsigned int write)
343{
344 unsigned int nsect;
345
346 nsect = min_t(unsigned int, drive->hwif->nleft, drive->mult_count);
347 while (nsect--)
348 ide_pio_sector(drive, write);
349}
350
Arjan van de Ven858119e2006-01-14 13:20:43 -0800351static void ide_pio_datablock(ide_drive_t *drive, struct request *rq,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 unsigned int write)
353{
354 if (rq->bio) /* fs request */
355 rq->errors = 0;
356
Andrew Morton651c29a2006-02-15 15:17:37 -0800357 touch_softlockup_watchdog();
358
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 switch (drive->hwif->data_phase) {
360 case TASKFILE_MULTI_IN:
361 case TASKFILE_MULTI_OUT:
362 ide_pio_multi(drive, write);
363 break;
364 default:
365 ide_pio_sector(drive, write);
366 break;
367 }
368}
369
370static ide_startstop_t task_error(ide_drive_t *drive, struct request *rq,
371 const char *s, u8 stat)
372{
373 if (rq->bio) {
374 ide_hwif_t *hwif = drive->hwif;
375 int sectors = hwif->nsect - hwif->nleft;
376
377 switch (hwif->data_phase) {
378 case TASKFILE_IN:
379 if (hwif->nleft)
380 break;
381 /* fall through */
382 case TASKFILE_OUT:
383 sectors--;
384 break;
385 case TASKFILE_MULTI_IN:
386 if (hwif->nleft)
387 break;
388 /* fall through */
389 case TASKFILE_MULTI_OUT:
390 sectors -= drive->mult_count;
391 default:
392 break;
393 }
394
395 if (sectors > 0) {
396 ide_driver_t *drv;
397
398 drv = *(ide_driver_t **)rq->rq_disk->private_data;
399 drv->end_request(drive, 1, sectors);
400 }
401 }
402 return ide_error(drive, s, stat);
403}
404
405static void task_end_request(ide_drive_t *drive, struct request *rq, u8 stat)
406{
Jens Axboe55c16a72007-07-25 08:13:56 +0200407 HWIF(drive)->cursg = NULL;
408
Jens Axboe4aff5e22006-08-10 08:44:47 +0200409 if (rq->cmd_type == REQ_TYPE_ATA_TASKFILE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 ide_task_t *task = rq->special;
411
Bartlomiej Zolnierkiewicz74095a92008-01-25 22:17:07 +0100412 if (task->tf_flags & IDE_TFLAG_FLAGGED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 u8 err = drive->hwif->INB(IDE_ERROR_REG);
414 ide_end_drive_cmd(drive, stat, err);
415 return;
416 }
417 }
418
Richard Purdie03731fb2006-03-31 02:31:15 -0800419 if (rq->rq_disk) {
420 ide_driver_t *drv;
421
422 drv = *(ide_driver_t **)rq->rq_disk->private_data;;
423 drv->end_request(drive, 1, rq->hard_nr_sectors);
424 } else
425 ide_end_request(drive, 1, rq->hard_nr_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426}
427
428/*
429 * Handler for command with PIO data-in phase (Read/Read Multiple).
430 */
431ide_startstop_t task_in_intr (ide_drive_t *drive)
432{
433 ide_hwif_t *hwif = drive->hwif;
434 struct request *rq = HWGROUP(drive)->rq;
435 u8 stat = hwif->INB(IDE_STATUS_REG);
436
437 /* new way for dealing with premature shared PCI interrupts */
438 if (!OK_STAT(stat, DATA_READY, BAD_R_STAT)) {
439 if (stat & (ERR_STAT | DRQ_STAT))
440 return task_error(drive, rq, __FUNCTION__, stat);
441 /* No data yet, so wait for another IRQ. */
442 ide_set_handler(drive, &task_in_intr, WAIT_WORSTCASE, NULL);
443 return ide_started;
444 }
445
446 ide_pio_datablock(drive, rq, 0);
447
448 /* If it was the last datablock check status and finish transfer. */
449 if (!hwif->nleft) {
450 stat = wait_drive_not_busy(drive);
451 if (!OK_STAT(stat, 0, BAD_R_STAT))
452 return task_error(drive, rq, __FUNCTION__, stat);
453 task_end_request(drive, rq, stat);
454 return ide_stopped;
455 }
456
457 /* Still data left to transfer. */
458 ide_set_handler(drive, &task_in_intr, WAIT_WORSTCASE, NULL);
459
460 return ide_started;
461}
462EXPORT_SYMBOL(task_in_intr);
463
464/*
465 * Handler for command with PIO data-out phase (Write/Write Multiple).
466 */
467static ide_startstop_t task_out_intr (ide_drive_t *drive)
468{
469 ide_hwif_t *hwif = drive->hwif;
470 struct request *rq = HWGROUP(drive)->rq;
471 u8 stat = hwif->INB(IDE_STATUS_REG);
472
473 if (!OK_STAT(stat, DRIVE_READY, drive->bad_wstat))
474 return task_error(drive, rq, __FUNCTION__, stat);
475
476 /* Deal with unexpected ATA data phase. */
477 if (((stat & DRQ_STAT) == 0) ^ !hwif->nleft)
478 return task_error(drive, rq, __FUNCTION__, stat);
479
480 if (!hwif->nleft) {
481 task_end_request(drive, rq, stat);
482 return ide_stopped;
483 }
484
485 /* Still data left to transfer. */
486 ide_pio_datablock(drive, rq, 1);
487 ide_set_handler(drive, &task_out_intr, WAIT_WORSTCASE, NULL);
488
489 return ide_started;
490}
491
492ide_startstop_t pre_task_out_intr (ide_drive_t *drive, struct request *rq)
493{
494 ide_startstop_t startstop;
495
496 if (ide_wait_stat(&startstop, drive, DATA_READY,
497 drive->bad_wstat, WAIT_DRQ)) {
498 printk(KERN_ERR "%s: no DRQ after issuing %sWRITE%s\n",
499 drive->name,
500 drive->hwif->data_phase ? "MULT" : "",
501 drive->addressing ? "_EXT" : "");
502 return startstop;
503 }
504
505 if (!drive->unmask)
506 local_irq_disable();
507
508 ide_set_handler(drive, &task_out_intr, WAIT_WORSTCASE, NULL);
509 ide_pio_datablock(drive, rq, 1);
510
511 return ide_started;
512}
513EXPORT_SYMBOL(pre_task_out_intr);
514
Bartlomiej Zolnierkiewiczac026ff2008-01-25 22:17:14 +0100515int ide_raw_taskfile(ide_drive_t *drive, ide_task_t *task, u8 *buf, u16 nsect)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516{
517 struct request rq;
518
519 memset(&rq, 0, sizeof(rq));
Bartlomiej Zolnierkiewicz02ac2462007-11-05 21:42:27 +0100520 rq.ref_count = 1;
Jens Axboe4aff5e22006-08-10 08:44:47 +0200521 rq.cmd_type = REQ_TYPE_ATA_TASKFILE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 rq.buffer = buf;
523
524 /*
525 * (ks) We transfer currently only whole sectors.
526 * This is suffient for now. But, it would be great,
527 * if we would find a solution to transfer any size.
528 * To support special commands like READ LONG.
529 */
Bartlomiej Zolnierkiewiczac026ff2008-01-25 22:17:14 +0100530 rq.hard_nr_sectors = rq.nr_sectors = nsect;
531 rq.hard_cur_sectors = rq.current_nr_sectors = nsect;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532
Bartlomiej Zolnierkiewiczac026ff2008-01-25 22:17:14 +0100533 if (task->tf_flags & IDE_TFLAG_WRITE)
534 rq.cmd_flags |= REQ_RW;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535
Bartlomiej Zolnierkiewiczac026ff2008-01-25 22:17:14 +0100536 rq.special = task;
537 task->rq = &rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 return ide_do_drive_cmd(drive, &rq, ide_wait);
540}
541
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542EXPORT_SYMBOL(ide_raw_taskfile);
543
Bartlomiej Zolnierkiewicz9a3c49b2008-01-25 22:17:07 +0100544int ide_no_data_taskfile(ide_drive_t *drive, ide_task_t *task)
545{
Bartlomiej Zolnierkiewiczac026ff2008-01-25 22:17:14 +0100546 task->data_phase = TASKFILE_NO_DATA;
547 task->handler = task_no_data_intr;
Bartlomiej Zolnierkiewicz9a3c49b2008-01-25 22:17:07 +0100548
Bartlomiej Zolnierkiewiczac026ff2008-01-25 22:17:14 +0100549 return ide_raw_taskfile(drive, task, NULL, 0);
Bartlomiej Zolnierkiewicz9a3c49b2008-01-25 22:17:07 +0100550}
551EXPORT_SYMBOL_GPL(ide_no_data_taskfile);
552
Bartlomiej Zolnierkiewicz26a5b042007-11-05 21:42:27 +0100553#ifdef CONFIG_IDE_TASK_IOCTL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554int ide_taskfile_ioctl (ide_drive_t *drive, unsigned int cmd, unsigned long arg)
555{
556 ide_task_request_t *req_task;
557 ide_task_t args;
558 u8 *outbuf = NULL;
559 u8 *inbuf = NULL;
Bartlomiej Zolnierkiewiczac026ff2008-01-25 22:17:14 +0100560 u8 *data_buf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 int err = 0;
562 int tasksize = sizeof(struct ide_task_request_s);
Alan Cox3a42bb22006-10-16 16:31:02 +0100563 unsigned int taskin = 0;
564 unsigned int taskout = 0;
Bartlomiej Zolnierkiewiczac026ff2008-01-25 22:17:14 +0100565 u16 nsect = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 u8 io_32bit = drive->io_32bit;
567 char __user *buf = (char __user *)arg;
568
569// printk("IDE Taskfile ...\n");
570
Deepak Saxenaf5e3c2f2005-11-07 01:01:25 -0800571 req_task = kzalloc(tasksize, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 if (req_task == NULL) return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 if (copy_from_user(req_task, buf, tasksize)) {
574 kfree(req_task);
575 return -EFAULT;
576 }
577
Alan Cox3a42bb22006-10-16 16:31:02 +0100578 taskout = req_task->out_size;
579 taskin = req_task->in_size;
580
581 if (taskin > 65536 || taskout > 65536) {
582 err = -EINVAL;
583 goto abort;
584 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585
586 if (taskout) {
587 int outtotal = tasksize;
Deepak Saxenaf5e3c2f2005-11-07 01:01:25 -0800588 outbuf = kzalloc(taskout, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 if (outbuf == NULL) {
590 err = -ENOMEM;
591 goto abort;
592 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 if (copy_from_user(outbuf, buf + outtotal, taskout)) {
594 err = -EFAULT;
595 goto abort;
596 }
597 }
598
599 if (taskin) {
600 int intotal = tasksize + taskout;
Deepak Saxenaf5e3c2f2005-11-07 01:01:25 -0800601 inbuf = kzalloc(taskin, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 if (inbuf == NULL) {
603 err = -ENOMEM;
604 goto abort;
605 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 if (copy_from_user(inbuf, buf + intotal, taskin)) {
607 err = -EFAULT;
608 goto abort;
609 }
610 }
611
612 memset(&args, 0, sizeof(ide_task_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613
Bartlomiej Zolnierkiewicz650d8412008-01-25 22:17:06 +0100614 memcpy(&args.tf_array[0], req_task->hob_ports, HDIO_DRIVE_HOB_HDR_SIZE - 2);
615 memcpy(&args.tf_array[6], req_task->io_ports, HDIO_DRIVE_TASK_HDR_SIZE);
Bartlomiej Zolnierkiewicz866e2ec2008-01-25 22:17:14 +0100616
617 args.data_phase = req_task->data_phase;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618
Bartlomiej Zolnierkiewicza3bbb9d2008-01-25 22:17:10 +0100619 args.tf_flags = IDE_TFLAG_OUT_DEVICE;
620 if (drive->addressing == 1)
621 args.tf_flags |= IDE_TFLAG_LBA48;
622
Bartlomiej Zolnierkiewicz74095a92008-01-25 22:17:07 +0100623 if (req_task->out_flags.all) {
624 args.tf_flags |= IDE_TFLAG_FLAGGED;
625
626 if (req_task->out_flags.b.data)
627 args.tf_flags |= IDE_TFLAG_OUT_DATA;
628
629 if (req_task->out_flags.b.nsector_hob)
630 args.tf_flags |= IDE_TFLAG_OUT_HOB_NSECT;
631 if (req_task->out_flags.b.sector_hob)
632 args.tf_flags |= IDE_TFLAG_OUT_HOB_LBAL;
633 if (req_task->out_flags.b.lcyl_hob)
634 args.tf_flags |= IDE_TFLAG_OUT_HOB_LBAM;
635 if (req_task->out_flags.b.hcyl_hob)
636 args.tf_flags |= IDE_TFLAG_OUT_HOB_LBAH;
637
638 if (req_task->out_flags.b.error_feature)
639 args.tf_flags |= IDE_TFLAG_OUT_FEATURE;
640 if (req_task->out_flags.b.nsector)
641 args.tf_flags |= IDE_TFLAG_OUT_NSECT;
642 if (req_task->out_flags.b.sector)
643 args.tf_flags |= IDE_TFLAG_OUT_LBAL;
644 if (req_task->out_flags.b.lcyl)
645 args.tf_flags |= IDE_TFLAG_OUT_LBAM;
646 if (req_task->out_flags.b.hcyl)
647 args.tf_flags |= IDE_TFLAG_OUT_LBAH;
Bartlomiej Zolnierkiewicza3bbb9d2008-01-25 22:17:10 +0100648 } else {
649 args.tf_flags |= IDE_TFLAG_OUT_TF;
650 if (args.tf_flags & IDE_TFLAG_LBA48)
651 args.tf_flags |= IDE_TFLAG_OUT_HOB;
Bartlomiej Zolnierkiewicz74095a92008-01-25 22:17:07 +0100652 }
653
Bartlomiej Zolnierkiewicz866e2ec2008-01-25 22:17:14 +0100654 if (req_task->in_flags.b.data)
655 args.tf_flags |= IDE_TFLAG_IN_DATA;
656
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 drive->io_32bit = 0;
658 switch(req_task->data_phase) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 case TASKFILE_MULTI_OUT:
660 if (!drive->mult_count) {
661 /* (hs): give up if multcount is not set */
662 printk(KERN_ERR "%s: %s Multimode Write " \
663 "multcount is not set\n",
664 drive->name, __FUNCTION__);
665 err = -EPERM;
666 goto abort;
667 }
668 /* fall through */
669 case TASKFILE_OUT:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 args.handler = &task_out_intr;
Bartlomiej Zolnierkiewiczac026ff2008-01-25 22:17:14 +0100671 /* fall through */
672 case TASKFILE_OUT_DMAQ:
673 case TASKFILE_OUT_DMA:
674 nsect = taskout / SECTOR_SIZE;
675 data_buf = outbuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 break;
677 case TASKFILE_MULTI_IN:
678 if (!drive->mult_count) {
679 /* (hs): give up if multcount is not set */
680 printk(KERN_ERR "%s: %s Multimode Read failure " \
681 "multcount is not set\n",
682 drive->name, __FUNCTION__);
683 err = -EPERM;
684 goto abort;
685 }
686 /* fall through */
687 case TASKFILE_IN:
688 args.handler = &task_in_intr;
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:
696 args.handler = &task_no_data_intr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 break;
698 default:
699 err = -EFAULT;
700 goto abort;
701 }
702
Bartlomiej Zolnierkiewiczac026ff2008-01-25 22:17:14 +0100703 if (req_task->req_cmd == IDE_DRIVE_TASK_NO_DATA)
704 nsect = 0;
705 else if (!nsect) {
706 nsect = (args.tf.hob_nsect << 8) | args.tf.nsect;
707
708 if (!nsect) {
709 printk(KERN_ERR "%s: in/out command without data\n",
710 drive->name);
711 err = -EFAULT;
712 goto abort;
713 }
714 }
715
716 if (req_task->req_cmd == IDE_DRIVE_TASK_RAW_WRITE)
717 args.tf_flags |= IDE_TFLAG_WRITE;
718
719 err = ide_raw_taskfile(drive, &args, data_buf, nsect);
720
Bartlomiej Zolnierkiewicz650d8412008-01-25 22:17:06 +0100721 memcpy(req_task->hob_ports, &args.tf_array[0], HDIO_DRIVE_HOB_HDR_SIZE - 2);
722 memcpy(req_task->io_ports, &args.tf_array[6], HDIO_DRIVE_TASK_HDR_SIZE);
Bartlomiej Zolnierkiewicz866e2ec2008-01-25 22:17:14 +0100723
724 if ((args.tf_flags & IDE_TFLAG_FLAGGED_SET_IN_FLAGS) &&
725 req_task->in_flags.all == 0) {
726 req_task->in_flags.all = IDE_TASKFILE_STD_IN_FLAGS;
727 if (drive->addressing == 1)
728 req_task->in_flags.all |= (IDE_HOB_STD_IN_FLAGS << 8);
729 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730
731 if (copy_to_user(buf, req_task, tasksize)) {
732 err = -EFAULT;
733 goto abort;
734 }
735 if (taskout) {
736 int outtotal = tasksize;
737 if (copy_to_user(buf + outtotal, outbuf, taskout)) {
738 err = -EFAULT;
739 goto abort;
740 }
741 }
742 if (taskin) {
743 int intotal = tasksize + taskout;
744 if (copy_to_user(buf + intotal, inbuf, taskin)) {
745 err = -EFAULT;
746 goto abort;
747 }
748 }
749abort:
750 kfree(req_task);
Jesper Juhl6044ec82005-11-07 01:01:32 -0800751 kfree(outbuf);
752 kfree(inbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753
754// printk("IDE Taskfile ioctl ended. rc = %i\n", err);
755
756 drive->io_32bit = io_32bit;
757
758 return err;
759}
Bartlomiej Zolnierkiewicz26a5b042007-11-05 21:42:27 +0100760#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761
762int ide_wait_cmd (ide_drive_t *drive, u8 cmd, u8 nsect, u8 feature, u8 sectors, u8 *buf)
763{
764 struct request rq;
765 u8 buffer[4];
766
767 if (!buf)
768 buf = buffer;
769 memset(buf, 0, 4 + SECTOR_WORDS * 4 * sectors);
770 ide_init_drive_cmd(&rq);
771 rq.buffer = buf;
772 *buf++ = cmd;
773 *buf++ = nsect;
774 *buf++ = feature;
775 *buf++ = sectors;
776 return ide_do_drive_cmd(drive, &rq, ide_wait);
777}
778
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779int ide_cmd_ioctl (ide_drive_t *drive, unsigned int cmd, unsigned long arg)
780{
781 int err = 0;
782 u8 args[4], *argbuf = args;
783 u8 xfer_rate = 0;
784 int argsize = 4;
785 ide_task_t tfargs;
Bartlomiej Zolnierkiewicz650d8412008-01-25 22:17:06 +0100786 struct ide_taskfile *tf = &tfargs.tf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787
788 if (NULL == (void *) arg) {
789 struct request rq;
790 ide_init_drive_cmd(&rq);
791 return ide_do_drive_cmd(drive, &rq, ide_wait);
792 }
793
794 if (copy_from_user(args, (void __user *)arg, 4))
795 return -EFAULT;
796
797 memset(&tfargs, 0, sizeof(ide_task_t));
Bartlomiej Zolnierkiewicz650d8412008-01-25 22:17:06 +0100798 tf->feature = args[2];
799 tf->nsect = args[3];
800 tf->lbal = args[1];
801 tf->command = args[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802
803 if (args[3]) {
804 argsize = 4 + (SECTOR_WORDS * 4 * args[3]);
Deepak Saxenaf5e3c2f2005-11-07 01:01:25 -0800805 argbuf = kzalloc(argsize, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 if (argbuf == NULL)
807 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 }
809 if (set_transfer(drive, &tfargs)) {
810 xfer_rate = args[1];
811 if (ide_ata66_check(drive, &tfargs))
812 goto abort;
813 }
814
815 err = ide_wait_cmd(drive, args[0], args[1], args[2], args[3], argbuf);
816
817 if (!err && xfer_rate) {
818 /* active-retuning-calls future */
819 ide_set_xfer_rate(drive, xfer_rate);
820 ide_driveid_update(drive);
821 }
822abort:
823 if (copy_to_user((void __user *)arg, argbuf, argsize))
824 err = -EFAULT;
825 if (argsize > 4)
826 kfree(argbuf);
827 return err;
828}
829
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830int ide_task_ioctl (ide_drive_t *drive, unsigned int cmd, unsigned long arg)
831{
832 void __user *p = (void __user *)arg;
833 int err = 0;
Bartlomiej Zolnierkiewicz14b89ef2008-01-25 22:17:11 +0100834 u8 args[7];
835 ide_task_t task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836
837 if (copy_from_user(args, p, 7))
838 return -EFAULT;
Bartlomiej Zolnierkiewicz14b89ef2008-01-25 22:17:11 +0100839
840 memset(&task, 0, sizeof(task));
841 memcpy(&task.tf_array[7], &args[1], 6);
842 task.tf.command = args[0];
843 task.tf_flags = IDE_TFLAG_OUT_TF | IDE_TFLAG_OUT_DEVICE;
844
845 err = ide_no_data_taskfile(drive, &task);
846
847 args[0] = task.tf.command;
848 memcpy(&args[1], &task.tf_array[7], 6);
849
850 if (copy_to_user(p, args, 7))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 err = -EFAULT;
Bartlomiej Zolnierkiewicz14b89ef2008-01-25 22:17:11 +0100852
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 return err;
854}