blob: a79150e6be07fc6ed545cc90ff0023bf1d922154 [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
72 if (IDE_CONTROL_REG)
73 hwif->OUTB(drive->ctl, IDE_CONTROL_REG); /* clear nIEN */
74
75 if ((task->tf_flags & IDE_TFLAG_NO_SELECT_MASK) == 0)
76 SELECT_MASK(drive, 0);
77
78 if (task->tf_flags & IDE_TFLAG_LBA48) {
79 hwif->OUTB(tf->hob_feature, IDE_FEATURE_REG);
80 hwif->OUTB(tf->hob_nsect, IDE_NSECTOR_REG);
81 hwif->OUTB(tf->hob_lbal, IDE_SECTOR_REG);
82 hwif->OUTB(tf->hob_lbam, IDE_LCYL_REG);
83 hwif->OUTB(tf->hob_lbah, IDE_HCYL_REG);
84 }
85
86 hwif->OUTB(tf->feature, IDE_FEATURE_REG);
87 hwif->OUTB(tf->nsect, IDE_NSECTOR_REG);
88 hwif->OUTB(tf->lbal, IDE_SECTOR_REG);
89 hwif->OUTB(tf->lbam, IDE_LCYL_REG);
90 hwif->OUTB(tf->lbah, IDE_HCYL_REG);
91
92 hwif->OUTB((tf->device & HIHI) | drive->select.all, IDE_SELECT_REG);
93}
94
95EXPORT_SYMBOL_GPL(ide_tf_load);
96
Linus Torvalds1da177e2005-04-16 15:20:36 -070097int taskfile_lib_get_identify (ide_drive_t *drive, u8 *buf)
98{
99 ide_task_t args;
Bartlomiej Zolnierkiewicz650d8412008-01-25 22:17:06 +0100100
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 memset(&args, 0, sizeof(ide_task_t));
Bartlomiej Zolnierkiewicz650d8412008-01-25 22:17:06 +0100102 args.tf.nsect = 0x01;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 if (drive->media == ide_disk)
Bartlomiej Zolnierkiewicz650d8412008-01-25 22:17:06 +0100104 args.tf.command = WIN_IDENTIFY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 else
Bartlomiej Zolnierkiewicz650d8412008-01-25 22:17:06 +0100106 args.tf.command = WIN_PIDENTIFY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 args.command_type = IDE_DRIVE_TASK_IN;
108 args.data_phase = TASKFILE_IN;
109 args.handler = &task_in_intr;
110 return ide_raw_taskfile(drive, &args, buf);
111}
112
113ide_startstop_t do_rw_taskfile (ide_drive_t *drive, ide_task_t *task)
114{
115 ide_hwif_t *hwif = HWIF(drive);
Bartlomiej Zolnierkiewicz650d8412008-01-25 22:17:06 +0100116 struct ide_taskfile *tf = &task->tf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
Bartlomiej Zolnierkiewicz9e422372008-01-25 22:17:07 +0100118 if (drive->addressing == 1)
119 task->tf_flags |= IDE_TFLAG_LBA48;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
Bartlomiej Zolnierkiewicz9e422372008-01-25 22:17:07 +0100121 ide_tf_load(drive, task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
123 if (task->handler != NULL) {
124 if (task->prehandler != NULL) {
Bartlomiej Zolnierkiewicz650d8412008-01-25 22:17:06 +0100125 hwif->OUTBSYNC(drive, tf->command, IDE_COMMAND_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 ndelay(400); /* FIXME */
127 return task->prehandler(drive, task->rq);
128 }
Bartlomiej Zolnierkiewicz650d8412008-01-25 22:17:06 +0100129 ide_execute_command(drive, tf->command, task->handler, WAIT_WORSTCASE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 return ide_started;
131 }
132
133 if (!drive->using_dma)
134 return ide_stopped;
135
Bartlomiej Zolnierkiewicz650d8412008-01-25 22:17:06 +0100136 switch (tf->command) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 case WIN_WRITEDMA_ONCE:
138 case WIN_WRITEDMA:
139 case WIN_WRITEDMA_EXT:
140 case WIN_READDMA_ONCE:
141 case WIN_READDMA:
142 case WIN_READDMA_EXT:
143 case WIN_IDENTIFY_DMA:
144 if (!hwif->dma_setup(drive)) {
Bartlomiej Zolnierkiewicz650d8412008-01-25 22:17:06 +0100145 hwif->dma_exec_cmd(drive, tf->command);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 hwif->dma_start(drive);
147 return ide_started;
148 }
149 break;
150 default:
151 if (task->handler == NULL)
152 return ide_stopped;
153 }
154
155 return ide_stopped;
156}
157
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158/*
159 * set_multmode_intr() is invoked on completion of a WIN_SETMULT cmd.
160 */
161ide_startstop_t set_multmode_intr (ide_drive_t *drive)
162{
163 ide_hwif_t *hwif = HWIF(drive);
164 u8 stat;
165
166 if (OK_STAT(stat = hwif->INB(IDE_STATUS_REG),READY_STAT,BAD_STAT)) {
167 drive->mult_count = drive->mult_req;
168 } else {
169 drive->mult_req = drive->mult_count = 0;
170 drive->special.b.recalibrate = 1;
171 (void) ide_dump_status(drive, "set_multmode", stat);
172 }
173 return ide_stopped;
174}
175
176/*
177 * set_geometry_intr() is invoked on completion of a WIN_SPECIFY cmd.
178 */
179ide_startstop_t set_geometry_intr (ide_drive_t *drive)
180{
181 ide_hwif_t *hwif = HWIF(drive);
182 int retries = 5;
183 u8 stat;
184
185 while (((stat = hwif->INB(IDE_STATUS_REG)) & BUSY_STAT) && retries--)
186 udelay(10);
187
188 if (OK_STAT(stat, READY_STAT, BAD_STAT))
189 return ide_stopped;
190
191 if (stat & (ERR_STAT|DRQ_STAT))
192 return ide_error(drive, "set_geometry_intr", stat);
193
Eric Sesterhenn125e1872006-06-23 02:06:06 -0700194 BUG_ON(HWGROUP(drive)->handler != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 ide_set_handler(drive, &set_geometry_intr, WAIT_WORSTCASE, NULL);
196 return ide_started;
197}
198
199/*
200 * recal_intr() is invoked on completion of a WIN_RESTORE (recalibrate) cmd.
201 */
202ide_startstop_t recal_intr (ide_drive_t *drive)
203{
204 ide_hwif_t *hwif = HWIF(drive);
205 u8 stat;
206
207 if (!OK_STAT(stat = hwif->INB(IDE_STATUS_REG), READY_STAT, BAD_STAT))
208 return ide_error(drive, "recal_intr", stat);
209 return ide_stopped;
210}
211
212/*
213 * Handler for commands without a data phase
214 */
215ide_startstop_t task_no_data_intr (ide_drive_t *drive)
216{
217 ide_task_t *args = HWGROUP(drive)->rq->special;
218 ide_hwif_t *hwif = HWIF(drive);
219 u8 stat;
220
Ingo Molnar366c7f52006-07-03 00:25:25 -0700221 local_irq_enable_in_hardirq();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 if (!OK_STAT(stat = hwif->INB(IDE_STATUS_REG),READY_STAT,BAD_STAT)) {
223 return ide_error(drive, "task_no_data_intr", stat);
224 /* calls ide_end_drive_cmd */
225 }
226 if (args)
227 ide_end_drive_cmd(drive, stat, hwif->INB(IDE_ERROR_REG));
228
229 return ide_stopped;
230}
231
232EXPORT_SYMBOL(task_no_data_intr);
233
234static u8 wait_drive_not_busy(ide_drive_t *drive)
235{
236 ide_hwif_t *hwif = HWIF(drive);
Masatake YAMATOb42fa132007-07-03 22:28:34 +0200237 int retries;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 u8 stat;
239
240 /*
241 * Last sector was transfered, wait until drive is ready.
242 * This can take up to 10 usec, but we will wait max 1 ms
243 * (drive_cmd_intr() waits that long).
244 */
Masatake YAMATOb42fa132007-07-03 22:28:34 +0200245 for (retries = 0; retries < 100; retries++) {
246 if ((stat = hwif->INB(IDE_STATUS_REG)) & BUSY_STAT)
247 udelay(10);
248 else
249 break;
250 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
Masatake YAMATOb42fa132007-07-03 22:28:34 +0200252 if (stat & BUSY_STAT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 printk(KERN_ERR "%s: drive still BUSY!\n", drive->name);
254
255 return stat;
256}
257
258static void ide_pio_sector(ide_drive_t *drive, unsigned int write)
259{
260 ide_hwif_t *hwif = drive->hwif;
261 struct scatterlist *sg = hwif->sg_table;
Jens Axboe55c16a72007-07-25 08:13:56 +0200262 struct scatterlist *cursg = hwif->cursg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 struct page *page;
264#ifdef CONFIG_HIGHMEM
265 unsigned long flags;
266#endif
267 unsigned int offset;
268 u8 *buf;
269
Jens Axboe55c16a72007-07-25 08:13:56 +0200270 cursg = hwif->cursg;
271 if (!cursg) {
272 cursg = sg;
273 hwif->cursg = sg;
274 }
275
Jens Axboe45711f12007-10-22 21:19:53 +0200276 page = sg_page(cursg);
Jens Axboe55c16a72007-07-25 08:13:56 +0200277 offset = cursg->offset + hwif->cursg_ofs * SECTOR_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
279 /* get the current page and offset */
280 page = nth_page(page, (offset >> PAGE_SHIFT));
281 offset %= PAGE_SIZE;
282
283#ifdef CONFIG_HIGHMEM
284 local_irq_save(flags);
285#endif
286 buf = kmap_atomic(page, KM_BIO_SRC_IRQ) + offset;
287
288 hwif->nleft--;
289 hwif->cursg_ofs++;
290
Jens Axboe55c16a72007-07-25 08:13:56 +0200291 if ((hwif->cursg_ofs * SECTOR_SIZE) == cursg->length) {
292 hwif->cursg = sg_next(hwif->cursg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 hwif->cursg_ofs = 0;
294 }
295
296 /* do the actual data transfer */
297 if (write)
298 taskfile_output_data(drive, buf, SECTOR_WORDS);
299 else
300 taskfile_input_data(drive, buf, SECTOR_WORDS);
301
302 kunmap_atomic(buf, KM_BIO_SRC_IRQ);
303#ifdef CONFIG_HIGHMEM
304 local_irq_restore(flags);
305#endif
306}
307
308static void ide_pio_multi(ide_drive_t *drive, unsigned int write)
309{
310 unsigned int nsect;
311
312 nsect = min_t(unsigned int, drive->hwif->nleft, drive->mult_count);
313 while (nsect--)
314 ide_pio_sector(drive, write);
315}
316
Arjan van de Ven858119e2006-01-14 13:20:43 -0800317static void ide_pio_datablock(ide_drive_t *drive, struct request *rq,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 unsigned int write)
319{
320 if (rq->bio) /* fs request */
321 rq->errors = 0;
322
Andrew Morton651c29a2006-02-15 15:17:37 -0800323 touch_softlockup_watchdog();
324
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 switch (drive->hwif->data_phase) {
326 case TASKFILE_MULTI_IN:
327 case TASKFILE_MULTI_OUT:
328 ide_pio_multi(drive, write);
329 break;
330 default:
331 ide_pio_sector(drive, write);
332 break;
333 }
334}
335
336static ide_startstop_t task_error(ide_drive_t *drive, struct request *rq,
337 const char *s, u8 stat)
338{
339 if (rq->bio) {
340 ide_hwif_t *hwif = drive->hwif;
341 int sectors = hwif->nsect - hwif->nleft;
342
343 switch (hwif->data_phase) {
344 case TASKFILE_IN:
345 if (hwif->nleft)
346 break;
347 /* fall through */
348 case TASKFILE_OUT:
349 sectors--;
350 break;
351 case TASKFILE_MULTI_IN:
352 if (hwif->nleft)
353 break;
354 /* fall through */
355 case TASKFILE_MULTI_OUT:
356 sectors -= drive->mult_count;
357 default:
358 break;
359 }
360
361 if (sectors > 0) {
362 ide_driver_t *drv;
363
364 drv = *(ide_driver_t **)rq->rq_disk->private_data;
365 drv->end_request(drive, 1, sectors);
366 }
367 }
368 return ide_error(drive, s, stat);
369}
370
371static void task_end_request(ide_drive_t *drive, struct request *rq, u8 stat)
372{
Jens Axboe55c16a72007-07-25 08:13:56 +0200373 HWIF(drive)->cursg = NULL;
374
Jens Axboe4aff5e22006-08-10 08:44:47 +0200375 if (rq->cmd_type == REQ_TYPE_ATA_TASKFILE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 ide_task_t *task = rq->special;
377
378 if (task->tf_out_flags.all) {
379 u8 err = drive->hwif->INB(IDE_ERROR_REG);
380 ide_end_drive_cmd(drive, stat, err);
381 return;
382 }
383 }
384
Richard Purdie03731fb2006-03-31 02:31:15 -0800385 if (rq->rq_disk) {
386 ide_driver_t *drv;
387
388 drv = *(ide_driver_t **)rq->rq_disk->private_data;;
389 drv->end_request(drive, 1, rq->hard_nr_sectors);
390 } else
391 ide_end_request(drive, 1, rq->hard_nr_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392}
393
394/*
395 * Handler for command with PIO data-in phase (Read/Read Multiple).
396 */
397ide_startstop_t task_in_intr (ide_drive_t *drive)
398{
399 ide_hwif_t *hwif = drive->hwif;
400 struct request *rq = HWGROUP(drive)->rq;
401 u8 stat = hwif->INB(IDE_STATUS_REG);
402
403 /* new way for dealing with premature shared PCI interrupts */
404 if (!OK_STAT(stat, DATA_READY, BAD_R_STAT)) {
405 if (stat & (ERR_STAT | DRQ_STAT))
406 return task_error(drive, rq, __FUNCTION__, stat);
407 /* No data yet, so wait for another IRQ. */
408 ide_set_handler(drive, &task_in_intr, WAIT_WORSTCASE, NULL);
409 return ide_started;
410 }
411
412 ide_pio_datablock(drive, rq, 0);
413
414 /* If it was the last datablock check status and finish transfer. */
415 if (!hwif->nleft) {
416 stat = wait_drive_not_busy(drive);
417 if (!OK_STAT(stat, 0, BAD_R_STAT))
418 return task_error(drive, rq, __FUNCTION__, stat);
419 task_end_request(drive, rq, stat);
420 return ide_stopped;
421 }
422
423 /* Still data left to transfer. */
424 ide_set_handler(drive, &task_in_intr, WAIT_WORSTCASE, NULL);
425
426 return ide_started;
427}
428EXPORT_SYMBOL(task_in_intr);
429
430/*
431 * Handler for command with PIO data-out phase (Write/Write Multiple).
432 */
433static ide_startstop_t task_out_intr (ide_drive_t *drive)
434{
435 ide_hwif_t *hwif = drive->hwif;
436 struct request *rq = HWGROUP(drive)->rq;
437 u8 stat = hwif->INB(IDE_STATUS_REG);
438
439 if (!OK_STAT(stat, DRIVE_READY, drive->bad_wstat))
440 return task_error(drive, rq, __FUNCTION__, stat);
441
442 /* Deal with unexpected ATA data phase. */
443 if (((stat & DRQ_STAT) == 0) ^ !hwif->nleft)
444 return task_error(drive, rq, __FUNCTION__, stat);
445
446 if (!hwif->nleft) {
447 task_end_request(drive, rq, stat);
448 return ide_stopped;
449 }
450
451 /* Still data left to transfer. */
452 ide_pio_datablock(drive, rq, 1);
453 ide_set_handler(drive, &task_out_intr, WAIT_WORSTCASE, NULL);
454
455 return ide_started;
456}
457
458ide_startstop_t pre_task_out_intr (ide_drive_t *drive, struct request *rq)
459{
460 ide_startstop_t startstop;
461
462 if (ide_wait_stat(&startstop, drive, DATA_READY,
463 drive->bad_wstat, WAIT_DRQ)) {
464 printk(KERN_ERR "%s: no DRQ after issuing %sWRITE%s\n",
465 drive->name,
466 drive->hwif->data_phase ? "MULT" : "",
467 drive->addressing ? "_EXT" : "");
468 return startstop;
469 }
470
471 if (!drive->unmask)
472 local_irq_disable();
473
474 ide_set_handler(drive, &task_out_intr, WAIT_WORSTCASE, NULL);
475 ide_pio_datablock(drive, rq, 1);
476
477 return ide_started;
478}
479EXPORT_SYMBOL(pre_task_out_intr);
480
481static int ide_diag_taskfile(ide_drive_t *drive, ide_task_t *args, unsigned long data_size, u8 *buf)
482{
483 struct request rq;
484
485 memset(&rq, 0, sizeof(rq));
Bartlomiej Zolnierkiewicz02ac2462007-11-05 21:42:27 +0100486 rq.ref_count = 1;
Jens Axboe4aff5e22006-08-10 08:44:47 +0200487 rq.cmd_type = REQ_TYPE_ATA_TASKFILE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 rq.buffer = buf;
489
490 /*
491 * (ks) We transfer currently only whole sectors.
492 * This is suffient for now. But, it would be great,
493 * if we would find a solution to transfer any size.
494 * To support special commands like READ LONG.
495 */
496 if (args->command_type != IDE_DRIVE_TASK_NO_DATA) {
497 if (data_size == 0)
Bartlomiej Zolnierkiewicz650d8412008-01-25 22:17:06 +0100498 rq.nr_sectors = (args->tf.hob_nsect << 8) | args->tf.nsect;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 else
500 rq.nr_sectors = data_size / SECTOR_SIZE;
501
502 if (!rq.nr_sectors) {
503 printk(KERN_ERR "%s: in/out command without data\n",
504 drive->name);
505 return -EFAULT;
506 }
507
508 rq.hard_nr_sectors = rq.nr_sectors;
509 rq.hard_cur_sectors = rq.current_nr_sectors = rq.nr_sectors;
510
511 if (args->command_type == IDE_DRIVE_TASK_RAW_WRITE)
Jens Axboe4aff5e22006-08-10 08:44:47 +0200512 rq.cmd_flags |= REQ_RW;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 }
514
515 rq.special = args;
Timothy Thelinef0f6a432005-09-16 19:28:16 -0700516 args->rq = &rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 return ide_do_drive_cmd(drive, &rq, ide_wait);
518}
519
520int ide_raw_taskfile (ide_drive_t *drive, ide_task_t *args, u8 *buf)
521{
522 return ide_diag_taskfile(drive, args, 0, buf);
523}
524
525EXPORT_SYMBOL(ide_raw_taskfile);
526
Bartlomiej Zolnierkiewicz26a5b042007-11-05 21:42:27 +0100527#ifdef CONFIG_IDE_TASK_IOCTL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528int ide_taskfile_ioctl (ide_drive_t *drive, unsigned int cmd, unsigned long arg)
529{
530 ide_task_request_t *req_task;
531 ide_task_t args;
532 u8 *outbuf = NULL;
533 u8 *inbuf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 int err = 0;
535 int tasksize = sizeof(struct ide_task_request_s);
Alan Cox3a42bb22006-10-16 16:31:02 +0100536 unsigned int taskin = 0;
537 unsigned int taskout = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 u8 io_32bit = drive->io_32bit;
539 char __user *buf = (char __user *)arg;
540
541// printk("IDE Taskfile ...\n");
542
Deepak Saxenaf5e3c2f2005-11-07 01:01:25 -0800543 req_task = kzalloc(tasksize, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 if (req_task == NULL) return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 if (copy_from_user(req_task, buf, tasksize)) {
546 kfree(req_task);
547 return -EFAULT;
548 }
549
Alan Cox3a42bb22006-10-16 16:31:02 +0100550 taskout = req_task->out_size;
551 taskin = req_task->in_size;
552
553 if (taskin > 65536 || taskout > 65536) {
554 err = -EINVAL;
555 goto abort;
556 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557
558 if (taskout) {
559 int outtotal = tasksize;
Deepak Saxenaf5e3c2f2005-11-07 01:01:25 -0800560 outbuf = kzalloc(taskout, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 if (outbuf == NULL) {
562 err = -ENOMEM;
563 goto abort;
564 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 if (copy_from_user(outbuf, buf + outtotal, taskout)) {
566 err = -EFAULT;
567 goto abort;
568 }
569 }
570
571 if (taskin) {
572 int intotal = tasksize + taskout;
Deepak Saxenaf5e3c2f2005-11-07 01:01:25 -0800573 inbuf = kzalloc(taskin, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 if (inbuf == NULL) {
575 err = -ENOMEM;
576 goto abort;
577 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 if (copy_from_user(inbuf, buf + intotal, taskin)) {
579 err = -EFAULT;
580 goto abort;
581 }
582 }
583
584 memset(&args, 0, sizeof(ide_task_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585
Bartlomiej Zolnierkiewicz650d8412008-01-25 22:17:06 +0100586 memcpy(&args.tf_array[0], req_task->hob_ports, HDIO_DRIVE_HOB_HDR_SIZE - 2);
587 memcpy(&args.tf_array[6], req_task->io_ports, HDIO_DRIVE_TASK_HDR_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 args.tf_in_flags = req_task->in_flags;
589 args.tf_out_flags = req_task->out_flags;
590 args.data_phase = req_task->data_phase;
591 args.command_type = req_task->req_cmd;
592
593 drive->io_32bit = 0;
594 switch(req_task->data_phase) {
595 case TASKFILE_OUT_DMAQ:
596 case TASKFILE_OUT_DMA:
597 err = ide_diag_taskfile(drive, &args, taskout, outbuf);
598 break;
599 case TASKFILE_IN_DMAQ:
600 case TASKFILE_IN_DMA:
601 err = ide_diag_taskfile(drive, &args, taskin, inbuf);
602 break;
603 case TASKFILE_MULTI_OUT:
604 if (!drive->mult_count) {
605 /* (hs): give up if multcount is not set */
606 printk(KERN_ERR "%s: %s Multimode Write " \
607 "multcount is not set\n",
608 drive->name, __FUNCTION__);
609 err = -EPERM;
610 goto abort;
611 }
612 /* fall through */
613 case TASKFILE_OUT:
614 args.prehandler = &pre_task_out_intr;
615 args.handler = &task_out_intr;
616 err = ide_diag_taskfile(drive, &args, taskout, outbuf);
617 break;
618 case TASKFILE_MULTI_IN:
619 if (!drive->mult_count) {
620 /* (hs): give up if multcount is not set */
621 printk(KERN_ERR "%s: %s Multimode Read failure " \
622 "multcount is not set\n",
623 drive->name, __FUNCTION__);
624 err = -EPERM;
625 goto abort;
626 }
627 /* fall through */
628 case TASKFILE_IN:
629 args.handler = &task_in_intr;
630 err = ide_diag_taskfile(drive, &args, taskin, inbuf);
631 break;
632 case TASKFILE_NO_DATA:
633 args.handler = &task_no_data_intr;
634 err = ide_diag_taskfile(drive, &args, 0, NULL);
635 break;
636 default:
637 err = -EFAULT;
638 goto abort;
639 }
640
Bartlomiej Zolnierkiewicz650d8412008-01-25 22:17:06 +0100641 memcpy(req_task->hob_ports, &args.tf_array[0], HDIO_DRIVE_HOB_HDR_SIZE - 2);
642 memcpy(req_task->io_ports, &args.tf_array[6], HDIO_DRIVE_TASK_HDR_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 req_task->in_flags = args.tf_in_flags;
644 req_task->out_flags = args.tf_out_flags;
645
646 if (copy_to_user(buf, req_task, tasksize)) {
647 err = -EFAULT;
648 goto abort;
649 }
650 if (taskout) {
651 int outtotal = tasksize;
652 if (copy_to_user(buf + outtotal, outbuf, taskout)) {
653 err = -EFAULT;
654 goto abort;
655 }
656 }
657 if (taskin) {
658 int intotal = tasksize + taskout;
659 if (copy_to_user(buf + intotal, inbuf, taskin)) {
660 err = -EFAULT;
661 goto abort;
662 }
663 }
664abort:
665 kfree(req_task);
Jesper Juhl6044ec82005-11-07 01:01:32 -0800666 kfree(outbuf);
667 kfree(inbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668
669// printk("IDE Taskfile ioctl ended. rc = %i\n", err);
670
671 drive->io_32bit = io_32bit;
672
673 return err;
674}
Bartlomiej Zolnierkiewicz26a5b042007-11-05 21:42:27 +0100675#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676
677int ide_wait_cmd (ide_drive_t *drive, u8 cmd, u8 nsect, u8 feature, u8 sectors, u8 *buf)
678{
679 struct request rq;
680 u8 buffer[4];
681
682 if (!buf)
683 buf = buffer;
684 memset(buf, 0, 4 + SECTOR_WORDS * 4 * sectors);
685 ide_init_drive_cmd(&rq);
686 rq.buffer = buf;
687 *buf++ = cmd;
688 *buf++ = nsect;
689 *buf++ = feature;
690 *buf++ = sectors;
691 return ide_do_drive_cmd(drive, &rq, ide_wait);
692}
693
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694int ide_cmd_ioctl (ide_drive_t *drive, unsigned int cmd, unsigned long arg)
695{
696 int err = 0;
697 u8 args[4], *argbuf = args;
698 u8 xfer_rate = 0;
699 int argsize = 4;
700 ide_task_t tfargs;
Bartlomiej Zolnierkiewicz650d8412008-01-25 22:17:06 +0100701 struct ide_taskfile *tf = &tfargs.tf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
703 if (NULL == (void *) arg) {
704 struct request rq;
705 ide_init_drive_cmd(&rq);
706 return ide_do_drive_cmd(drive, &rq, ide_wait);
707 }
708
709 if (copy_from_user(args, (void __user *)arg, 4))
710 return -EFAULT;
711
712 memset(&tfargs, 0, sizeof(ide_task_t));
Bartlomiej Zolnierkiewicz650d8412008-01-25 22:17:06 +0100713 tf->feature = args[2];
714 tf->nsect = args[3];
715 tf->lbal = args[1];
716 tf->command = args[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717
718 if (args[3]) {
719 argsize = 4 + (SECTOR_WORDS * 4 * args[3]);
Deepak Saxenaf5e3c2f2005-11-07 01:01:25 -0800720 argbuf = kzalloc(argsize, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 if (argbuf == NULL)
722 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 }
724 if (set_transfer(drive, &tfargs)) {
725 xfer_rate = args[1];
726 if (ide_ata66_check(drive, &tfargs))
727 goto abort;
728 }
729
730 err = ide_wait_cmd(drive, args[0], args[1], args[2], args[3], argbuf);
731
732 if (!err && xfer_rate) {
733 /* active-retuning-calls future */
734 ide_set_xfer_rate(drive, xfer_rate);
735 ide_driveid_update(drive);
736 }
737abort:
738 if (copy_to_user((void __user *)arg, argbuf, argsize))
739 err = -EFAULT;
740 if (argsize > 4)
741 kfree(argbuf);
742 return err;
743}
744
745static int ide_wait_cmd_task(ide_drive_t *drive, u8 *buf)
746{
747 struct request rq;
748
749 ide_init_drive_cmd(&rq);
Jens Axboe4aff5e22006-08-10 08:44:47 +0200750 rq.cmd_type = REQ_TYPE_ATA_TASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 rq.buffer = buf;
752 return ide_do_drive_cmd(drive, &rq, ide_wait);
753}
754
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755int ide_task_ioctl (ide_drive_t *drive, unsigned int cmd, unsigned long arg)
756{
757 void __user *p = (void __user *)arg;
758 int err = 0;
759 u8 args[7], *argbuf = args;
760 int argsize = 7;
761
762 if (copy_from_user(args, p, 7))
763 return -EFAULT;
764 err = ide_wait_cmd_task(drive, argbuf);
765 if (copy_to_user(p, argbuf, argsize))
766 err = -EFAULT;
767 return err;
768}
769
770/*
771 * NOTICE: This is additions from IBM to provide a discrete interface,
772 * for selective taskregister access operations. Nice JOB Klaus!!!
773 * Glad to be able to work and co-develop this with you and IBM.
774 */
775ide_startstop_t flagged_taskfile (ide_drive_t *drive, ide_task_t *task)
776{
777 ide_hwif_t *hwif = HWIF(drive);
Bartlomiej Zolnierkiewicz650d8412008-01-25 22:17:06 +0100778 struct ide_taskfile *tf = &task->tf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779
780 if (task->data_phase == TASKFILE_MULTI_IN ||
781 task->data_phase == TASKFILE_MULTI_OUT) {
782 if (!drive->mult_count) {
783 printk(KERN_ERR "%s: multimode not set!\n", drive->name);
784 return ide_stopped;
785 }
786 }
787
788 /*
Bartlomiej Zolnierkiewicze07bc702005-11-19 22:17:55 +0100789 * (ks) Check taskfile in flags.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 * If set, then execute as it is defined.
791 * If not set, then define default settings.
792 * The default values are:
Bartlomiej Zolnierkiewicze07bc702005-11-19 22:17:55 +0100793 * read all taskfile registers (except data)
794 * read the hob registers (sector, nsector, lcyl, hcyl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 if (task->tf_in_flags.all == 0) {
797 task->tf_in_flags.all = IDE_TASKFILE_STD_IN_FLAGS;
798 if (drive->addressing == 1)
799 task->tf_in_flags.all |= (IDE_HOB_STD_IN_FLAGS << 8);
800 }
801
802 /* ALL Command Block Executions SHALL clear nIEN, unless otherwise */
803 if (IDE_CONTROL_REG)
804 /* clear nIEN */
805 hwif->OUTB(drive->ctl, IDE_CONTROL_REG);
806 SELECT_MASK(drive, 0);
807
Bartlomiej Zolnierkiewicz650d8412008-01-25 22:17:06 +0100808 if (task->tf_out_flags.b.data)
809 hwif->OUTW((tf->hob_data << 8) | tf->data, IDE_DATA_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810
811 /* (ks) send hob registers first */
812 if (task->tf_out_flags.b.nsector_hob)
Bartlomiej Zolnierkiewicz650d8412008-01-25 22:17:06 +0100813 hwif->OUTB(tf->hob_nsect, IDE_NSECTOR_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 if (task->tf_out_flags.b.sector_hob)
Bartlomiej Zolnierkiewicz650d8412008-01-25 22:17:06 +0100815 hwif->OUTB(tf->hob_lbal, IDE_SECTOR_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 if (task->tf_out_flags.b.lcyl_hob)
Bartlomiej Zolnierkiewicz650d8412008-01-25 22:17:06 +0100817 hwif->OUTB(tf->hob_lbam, IDE_LCYL_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 if (task->tf_out_flags.b.hcyl_hob)
Bartlomiej Zolnierkiewicz650d8412008-01-25 22:17:06 +0100819 hwif->OUTB(tf->hob_lbah, IDE_HCYL_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820
821 /* (ks) Send now the standard registers */
822 if (task->tf_out_flags.b.error_feature)
Bartlomiej Zolnierkiewicz650d8412008-01-25 22:17:06 +0100823 hwif->OUTB(tf->feature, IDE_FEATURE_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 /* refers to number of sectors to transfer */
825 if (task->tf_out_flags.b.nsector)
Bartlomiej Zolnierkiewicz650d8412008-01-25 22:17:06 +0100826 hwif->OUTB(tf->nsect, IDE_NSECTOR_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 /* refers to sector offset or start sector */
828 if (task->tf_out_flags.b.sector)
Bartlomiej Zolnierkiewicz650d8412008-01-25 22:17:06 +0100829 hwif->OUTB(tf->lbal, IDE_SECTOR_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 if (task->tf_out_flags.b.lcyl)
Bartlomiej Zolnierkiewicz650d8412008-01-25 22:17:06 +0100831 hwif->OUTB(tf->lbam, IDE_LCYL_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 if (task->tf_out_flags.b.hcyl)
Bartlomiej Zolnierkiewicz650d8412008-01-25 22:17:06 +0100833 hwif->OUTB(tf->lbah, IDE_HCYL_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834
835 /*
836 * (ks) In the flagged taskfile approch, we will use all specified
837 * registers and the register value will not be changed, except the
838 * select bit (master/slave) in the drive_head register. We must make
839 * sure that the desired drive is selected.
840 */
Bartlomiej Zolnierkiewicz650d8412008-01-25 22:17:06 +0100841 hwif->OUTB(tf->device | drive->select.all, IDE_SELECT_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 switch(task->data_phase) {
843
844 case TASKFILE_OUT_DMAQ:
845 case TASKFILE_OUT_DMA:
846 case TASKFILE_IN_DMAQ:
847 case TASKFILE_IN_DMA:
Bartlomiej Zolnierkiewiczeda5b352007-10-20 00:32:37 +0200848 if (!drive->using_dma)
849 break;
850
Bartlomiej Zolnierkiewiczdd35b7b2007-10-20 00:32:37 +0200851 if (!hwif->dma_setup(drive)) {
Bartlomiej Zolnierkiewicz650d8412008-01-25 22:17:06 +0100852 hwif->dma_exec_cmd(drive, tf->command);
Bartlomiej Zolnierkiewiczdd35b7b2007-10-20 00:32:37 +0200853 hwif->dma_start(drive);
854 return ide_started;
855 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 break;
857
858 default:
859 if (task->handler == NULL)
860 return ide_stopped;
861
862 /* Issue the command */
863 if (task->prehandler) {
Bartlomiej Zolnierkiewicz650d8412008-01-25 22:17:06 +0100864 hwif->OUTBSYNC(drive, tf->command, IDE_COMMAND_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 ndelay(400); /* FIXME */
866 return task->prehandler(drive, task->rq);
867 }
Bartlomiej Zolnierkiewicz650d8412008-01-25 22:17:06 +0100868 ide_execute_command(drive, tf->command, task->handler, WAIT_WORSTCASE, NULL);
Bartlomiej Zolnierkiewiczdd35b7b2007-10-20 00:32:37 +0200869 return ide_started;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 }
871
Bartlomiej Zolnierkiewiczdd35b7b2007-10-20 00:32:37 +0200872 return ide_stopped;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873}