blob: 8a5a10fdcfcb2d5abfbcff7175e1e990e711f3d0 [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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 args.command_type = IDE_DRIVE_TASK_IN;
130 args.data_phase = TASKFILE_IN;
131 args.handler = &task_in_intr;
132 return ide_raw_taskfile(drive, &args, buf);
133}
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 Zolnierkiewicz9e422372008-01-25 22:17:07 +0100159 ide_tf_load(drive, task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
161 if (task->handler != NULL) {
162 if (task->prehandler != NULL) {
Bartlomiej Zolnierkiewicz650d8412008-01-25 22:17:06 +0100163 hwif->OUTBSYNC(drive, tf->command, IDE_COMMAND_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 ndelay(400); /* FIXME */
165 return task->prehandler(drive, task->rq);
166 }
Bartlomiej Zolnierkiewicz650d8412008-01-25 22:17:06 +0100167 ide_execute_command(drive, tf->command, task->handler, WAIT_WORSTCASE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 return ide_started;
169 }
170
Bartlomiej Zolnierkiewicz74095a92008-01-25 22:17:07 +0100171 if (task_dma_ok(task) && drive->using_dma && !hwif->dma_setup(drive)) {
172 hwif->dma_exec_cmd(drive, tf->command);
173 hwif->dma_start(drive);
174 return ide_started;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 }
176
177 return ide_stopped;
178}
179
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180/*
181 * set_multmode_intr() is invoked on completion of a WIN_SETMULT cmd.
182 */
183ide_startstop_t set_multmode_intr (ide_drive_t *drive)
184{
185 ide_hwif_t *hwif = HWIF(drive);
186 u8 stat;
187
188 if (OK_STAT(stat = hwif->INB(IDE_STATUS_REG),READY_STAT,BAD_STAT)) {
189 drive->mult_count = drive->mult_req;
190 } else {
191 drive->mult_req = drive->mult_count = 0;
192 drive->special.b.recalibrate = 1;
193 (void) ide_dump_status(drive, "set_multmode", stat);
194 }
195 return ide_stopped;
196}
197
198/*
199 * set_geometry_intr() is invoked on completion of a WIN_SPECIFY cmd.
200 */
201ide_startstop_t set_geometry_intr (ide_drive_t *drive)
202{
203 ide_hwif_t *hwif = HWIF(drive);
204 int retries = 5;
205 u8 stat;
206
207 while (((stat = hwif->INB(IDE_STATUS_REG)) & BUSY_STAT) && retries--)
208 udelay(10);
209
210 if (OK_STAT(stat, READY_STAT, BAD_STAT))
211 return ide_stopped;
212
213 if (stat & (ERR_STAT|DRQ_STAT))
214 return ide_error(drive, "set_geometry_intr", stat);
215
Eric Sesterhenn125e1872006-06-23 02:06:06 -0700216 BUG_ON(HWGROUP(drive)->handler != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 ide_set_handler(drive, &set_geometry_intr, WAIT_WORSTCASE, NULL);
218 return ide_started;
219}
220
221/*
222 * recal_intr() is invoked on completion of a WIN_RESTORE (recalibrate) cmd.
223 */
224ide_startstop_t recal_intr (ide_drive_t *drive)
225{
226 ide_hwif_t *hwif = HWIF(drive);
227 u8 stat;
228
229 if (!OK_STAT(stat = hwif->INB(IDE_STATUS_REG), READY_STAT, BAD_STAT))
230 return ide_error(drive, "recal_intr", stat);
231 return ide_stopped;
232}
233
234/*
235 * Handler for commands without a data phase
236 */
237ide_startstop_t task_no_data_intr (ide_drive_t *drive)
238{
239 ide_task_t *args = HWGROUP(drive)->rq->special;
240 ide_hwif_t *hwif = HWIF(drive);
241 u8 stat;
242
Ingo Molnar366c7f52006-07-03 00:25:25 -0700243 local_irq_enable_in_hardirq();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 if (!OK_STAT(stat = hwif->INB(IDE_STATUS_REG),READY_STAT,BAD_STAT)) {
245 return ide_error(drive, "task_no_data_intr", stat);
246 /* calls ide_end_drive_cmd */
247 }
248 if (args)
249 ide_end_drive_cmd(drive, stat, hwif->INB(IDE_ERROR_REG));
250
251 return ide_stopped;
252}
253
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254static u8 wait_drive_not_busy(ide_drive_t *drive)
255{
256 ide_hwif_t *hwif = HWIF(drive);
Masatake YAMATOb42fa132007-07-03 22:28:34 +0200257 int retries;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 u8 stat;
259
260 /*
261 * Last sector was transfered, wait until drive is ready.
262 * This can take up to 10 usec, but we will wait max 1 ms
263 * (drive_cmd_intr() waits that long).
264 */
Masatake YAMATOb42fa132007-07-03 22:28:34 +0200265 for (retries = 0; retries < 100; retries++) {
266 if ((stat = hwif->INB(IDE_STATUS_REG)) & BUSY_STAT)
267 udelay(10);
268 else
269 break;
270 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
Masatake YAMATOb42fa132007-07-03 22:28:34 +0200272 if (stat & BUSY_STAT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 printk(KERN_ERR "%s: drive still BUSY!\n", drive->name);
274
275 return stat;
276}
277
278static void ide_pio_sector(ide_drive_t *drive, unsigned int write)
279{
280 ide_hwif_t *hwif = drive->hwif;
281 struct scatterlist *sg = hwif->sg_table;
Jens Axboe55c16a72007-07-25 08:13:56 +0200282 struct scatterlist *cursg = hwif->cursg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 struct page *page;
284#ifdef CONFIG_HIGHMEM
285 unsigned long flags;
286#endif
287 unsigned int offset;
288 u8 *buf;
289
Jens Axboe55c16a72007-07-25 08:13:56 +0200290 cursg = hwif->cursg;
291 if (!cursg) {
292 cursg = sg;
293 hwif->cursg = sg;
294 }
295
Jens Axboe45711f12007-10-22 21:19:53 +0200296 page = sg_page(cursg);
Jens Axboe55c16a72007-07-25 08:13:56 +0200297 offset = cursg->offset + hwif->cursg_ofs * SECTOR_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298
299 /* get the current page and offset */
300 page = nth_page(page, (offset >> PAGE_SHIFT));
301 offset %= PAGE_SIZE;
302
303#ifdef CONFIG_HIGHMEM
304 local_irq_save(flags);
305#endif
306 buf = kmap_atomic(page, KM_BIO_SRC_IRQ) + offset;
307
308 hwif->nleft--;
309 hwif->cursg_ofs++;
310
Jens Axboe55c16a72007-07-25 08:13:56 +0200311 if ((hwif->cursg_ofs * SECTOR_SIZE) == cursg->length) {
312 hwif->cursg = sg_next(hwif->cursg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 hwif->cursg_ofs = 0;
314 }
315
316 /* do the actual data transfer */
317 if (write)
318 taskfile_output_data(drive, buf, SECTOR_WORDS);
319 else
320 taskfile_input_data(drive, buf, SECTOR_WORDS);
321
322 kunmap_atomic(buf, KM_BIO_SRC_IRQ);
323#ifdef CONFIG_HIGHMEM
324 local_irq_restore(flags);
325#endif
326}
327
328static void ide_pio_multi(ide_drive_t *drive, unsigned int write)
329{
330 unsigned int nsect;
331
332 nsect = min_t(unsigned int, drive->hwif->nleft, drive->mult_count);
333 while (nsect--)
334 ide_pio_sector(drive, write);
335}
336
Arjan van de Ven858119e2006-01-14 13:20:43 -0800337static void ide_pio_datablock(ide_drive_t *drive, struct request *rq,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 unsigned int write)
339{
340 if (rq->bio) /* fs request */
341 rq->errors = 0;
342
Andrew Morton651c29a2006-02-15 15:17:37 -0800343 touch_softlockup_watchdog();
344
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 switch (drive->hwif->data_phase) {
346 case TASKFILE_MULTI_IN:
347 case TASKFILE_MULTI_OUT:
348 ide_pio_multi(drive, write);
349 break;
350 default:
351 ide_pio_sector(drive, write);
352 break;
353 }
354}
355
356static ide_startstop_t task_error(ide_drive_t *drive, struct request *rq,
357 const char *s, u8 stat)
358{
359 if (rq->bio) {
360 ide_hwif_t *hwif = drive->hwif;
361 int sectors = hwif->nsect - hwif->nleft;
362
363 switch (hwif->data_phase) {
364 case TASKFILE_IN:
365 if (hwif->nleft)
366 break;
367 /* fall through */
368 case TASKFILE_OUT:
369 sectors--;
370 break;
371 case TASKFILE_MULTI_IN:
372 if (hwif->nleft)
373 break;
374 /* fall through */
375 case TASKFILE_MULTI_OUT:
376 sectors -= drive->mult_count;
377 default:
378 break;
379 }
380
381 if (sectors > 0) {
382 ide_driver_t *drv;
383
384 drv = *(ide_driver_t **)rq->rq_disk->private_data;
385 drv->end_request(drive, 1, sectors);
386 }
387 }
388 return ide_error(drive, s, stat);
389}
390
391static void task_end_request(ide_drive_t *drive, struct request *rq, u8 stat)
392{
Jens Axboe55c16a72007-07-25 08:13:56 +0200393 HWIF(drive)->cursg = NULL;
394
Jens Axboe4aff5e22006-08-10 08:44:47 +0200395 if (rq->cmd_type == REQ_TYPE_ATA_TASKFILE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 ide_task_t *task = rq->special;
397
Bartlomiej Zolnierkiewicz74095a92008-01-25 22:17:07 +0100398 if (task->tf_flags & IDE_TFLAG_FLAGGED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 u8 err = drive->hwif->INB(IDE_ERROR_REG);
400 ide_end_drive_cmd(drive, stat, err);
401 return;
402 }
403 }
404
Richard Purdie03731fb2006-03-31 02:31:15 -0800405 if (rq->rq_disk) {
406 ide_driver_t *drv;
407
408 drv = *(ide_driver_t **)rq->rq_disk->private_data;;
409 drv->end_request(drive, 1, rq->hard_nr_sectors);
410 } else
411 ide_end_request(drive, 1, rq->hard_nr_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412}
413
414/*
415 * Handler for command with PIO data-in phase (Read/Read Multiple).
416 */
417ide_startstop_t task_in_intr (ide_drive_t *drive)
418{
419 ide_hwif_t *hwif = drive->hwif;
420 struct request *rq = HWGROUP(drive)->rq;
421 u8 stat = hwif->INB(IDE_STATUS_REG);
422
423 /* new way for dealing with premature shared PCI interrupts */
424 if (!OK_STAT(stat, DATA_READY, BAD_R_STAT)) {
425 if (stat & (ERR_STAT | DRQ_STAT))
426 return task_error(drive, rq, __FUNCTION__, stat);
427 /* No data yet, so wait for another IRQ. */
428 ide_set_handler(drive, &task_in_intr, WAIT_WORSTCASE, NULL);
429 return ide_started;
430 }
431
432 ide_pio_datablock(drive, rq, 0);
433
434 /* If it was the last datablock check status and finish transfer. */
435 if (!hwif->nleft) {
436 stat = wait_drive_not_busy(drive);
437 if (!OK_STAT(stat, 0, BAD_R_STAT))
438 return task_error(drive, rq, __FUNCTION__, stat);
439 task_end_request(drive, rq, stat);
440 return ide_stopped;
441 }
442
443 /* Still data left to transfer. */
444 ide_set_handler(drive, &task_in_intr, WAIT_WORSTCASE, NULL);
445
446 return ide_started;
447}
448EXPORT_SYMBOL(task_in_intr);
449
450/*
451 * Handler for command with PIO data-out phase (Write/Write Multiple).
452 */
453static ide_startstop_t task_out_intr (ide_drive_t *drive)
454{
455 ide_hwif_t *hwif = drive->hwif;
456 struct request *rq = HWGROUP(drive)->rq;
457 u8 stat = hwif->INB(IDE_STATUS_REG);
458
459 if (!OK_STAT(stat, DRIVE_READY, drive->bad_wstat))
460 return task_error(drive, rq, __FUNCTION__, stat);
461
462 /* Deal with unexpected ATA data phase. */
463 if (((stat & DRQ_STAT) == 0) ^ !hwif->nleft)
464 return task_error(drive, rq, __FUNCTION__, stat);
465
466 if (!hwif->nleft) {
467 task_end_request(drive, rq, stat);
468 return ide_stopped;
469 }
470
471 /* Still data left to transfer. */
472 ide_pio_datablock(drive, rq, 1);
473 ide_set_handler(drive, &task_out_intr, WAIT_WORSTCASE, NULL);
474
475 return ide_started;
476}
477
478ide_startstop_t pre_task_out_intr (ide_drive_t *drive, struct request *rq)
479{
480 ide_startstop_t startstop;
481
482 if (ide_wait_stat(&startstop, drive, DATA_READY,
483 drive->bad_wstat, WAIT_DRQ)) {
484 printk(KERN_ERR "%s: no DRQ after issuing %sWRITE%s\n",
485 drive->name,
486 drive->hwif->data_phase ? "MULT" : "",
487 drive->addressing ? "_EXT" : "");
488 return startstop;
489 }
490
491 if (!drive->unmask)
492 local_irq_disable();
493
494 ide_set_handler(drive, &task_out_intr, WAIT_WORSTCASE, NULL);
495 ide_pio_datablock(drive, rq, 1);
496
497 return ide_started;
498}
499EXPORT_SYMBOL(pre_task_out_intr);
500
501static int ide_diag_taskfile(ide_drive_t *drive, ide_task_t *args, unsigned long data_size, u8 *buf)
502{
503 struct request rq;
504
505 memset(&rq, 0, sizeof(rq));
Bartlomiej Zolnierkiewicz02ac2462007-11-05 21:42:27 +0100506 rq.ref_count = 1;
Jens Axboe4aff5e22006-08-10 08:44:47 +0200507 rq.cmd_type = REQ_TYPE_ATA_TASKFILE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 rq.buffer = buf;
509
510 /*
511 * (ks) We transfer currently only whole sectors.
512 * This is suffient for now. But, it would be great,
513 * if we would find a solution to transfer any size.
514 * To support special commands like READ LONG.
515 */
516 if (args->command_type != IDE_DRIVE_TASK_NO_DATA) {
517 if (data_size == 0)
Bartlomiej Zolnierkiewicz650d8412008-01-25 22:17:06 +0100518 rq.nr_sectors = (args->tf.hob_nsect << 8) | args->tf.nsect;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 else
520 rq.nr_sectors = data_size / SECTOR_SIZE;
521
522 if (!rq.nr_sectors) {
523 printk(KERN_ERR "%s: in/out command without data\n",
524 drive->name);
525 return -EFAULT;
526 }
527
528 rq.hard_nr_sectors = rq.nr_sectors;
529 rq.hard_cur_sectors = rq.current_nr_sectors = rq.nr_sectors;
530
531 if (args->command_type == IDE_DRIVE_TASK_RAW_WRITE)
Jens Axboe4aff5e22006-08-10 08:44:47 +0200532 rq.cmd_flags |= REQ_RW;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 }
534
535 rq.special = args;
Timothy Thelinef0f6a432005-09-16 19:28:16 -0700536 args->rq = &rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 return ide_do_drive_cmd(drive, &rq, ide_wait);
538}
539
540int ide_raw_taskfile (ide_drive_t *drive, ide_task_t *args, u8 *buf)
541{
542 return ide_diag_taskfile(drive, args, 0, buf);
543}
544
545EXPORT_SYMBOL(ide_raw_taskfile);
546
Bartlomiej Zolnierkiewicz9a3c49b2008-01-25 22:17:07 +0100547int ide_no_data_taskfile(ide_drive_t *drive, ide_task_t *task)
548{
549 task->command_type = IDE_DRIVE_TASK_NO_DATA;
550 task->data_phase = TASKFILE_NO_DATA;
551 task->handler = task_no_data_intr;
552
553 return ide_raw_taskfile(drive, task, NULL);
554}
555EXPORT_SYMBOL_GPL(ide_no_data_taskfile);
556
Bartlomiej Zolnierkiewicz26a5b042007-11-05 21:42:27 +0100557#ifdef CONFIG_IDE_TASK_IOCTL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558int ide_taskfile_ioctl (ide_drive_t *drive, unsigned int cmd, unsigned long arg)
559{
560 ide_task_request_t *req_task;
561 ide_task_t args;
562 u8 *outbuf = NULL;
563 u8 *inbuf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 int err = 0;
565 int tasksize = sizeof(struct ide_task_request_s);
Alan Cox3a42bb22006-10-16 16:31:02 +0100566 unsigned int taskin = 0;
567 unsigned int taskout = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 u8 io_32bit = drive->io_32bit;
569 char __user *buf = (char __user *)arg;
570
571// printk("IDE Taskfile ...\n");
572
Deepak Saxenaf5e3c2f2005-11-07 01:01:25 -0800573 req_task = kzalloc(tasksize, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 if (req_task == NULL) return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 if (copy_from_user(req_task, buf, tasksize)) {
576 kfree(req_task);
577 return -EFAULT;
578 }
579
Alan Cox3a42bb22006-10-16 16:31:02 +0100580 taskout = req_task->out_size;
581 taskin = req_task->in_size;
582
583 if (taskin > 65536 || taskout > 65536) {
584 err = -EINVAL;
585 goto abort;
586 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587
588 if (taskout) {
589 int outtotal = tasksize;
Deepak Saxenaf5e3c2f2005-11-07 01:01:25 -0800590 outbuf = kzalloc(taskout, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 if (outbuf == NULL) {
592 err = -ENOMEM;
593 goto abort;
594 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 if (copy_from_user(outbuf, buf + outtotal, taskout)) {
596 err = -EFAULT;
597 goto abort;
598 }
599 }
600
601 if (taskin) {
602 int intotal = tasksize + taskout;
Deepak Saxenaf5e3c2f2005-11-07 01:01:25 -0800603 inbuf = kzalloc(taskin, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 if (inbuf == NULL) {
605 err = -ENOMEM;
606 goto abort;
607 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 if (copy_from_user(inbuf, buf + intotal, taskin)) {
609 err = -EFAULT;
610 goto abort;
611 }
612 }
613
614 memset(&args, 0, sizeof(ide_task_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615
Bartlomiej Zolnierkiewicz650d8412008-01-25 22:17:06 +0100616 memcpy(&args.tf_array[0], req_task->hob_ports, HDIO_DRIVE_HOB_HDR_SIZE - 2);
617 memcpy(&args.tf_array[6], req_task->io_ports, HDIO_DRIVE_TASK_HDR_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 args.tf_in_flags = req_task->in_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 args.data_phase = req_task->data_phase;
620 args.command_type = req_task->req_cmd;
621
Bartlomiej Zolnierkiewicz74095a92008-01-25 22:17:07 +0100622 if (req_task->out_flags.all) {
623 args.tf_flags |= IDE_TFLAG_FLAGGED;
624
625 if (req_task->out_flags.b.data)
626 args.tf_flags |= IDE_TFLAG_OUT_DATA;
627
628 if (req_task->out_flags.b.nsector_hob)
629 args.tf_flags |= IDE_TFLAG_OUT_HOB_NSECT;
630 if (req_task->out_flags.b.sector_hob)
631 args.tf_flags |= IDE_TFLAG_OUT_HOB_LBAL;
632 if (req_task->out_flags.b.lcyl_hob)
633 args.tf_flags |= IDE_TFLAG_OUT_HOB_LBAM;
634 if (req_task->out_flags.b.hcyl_hob)
635 args.tf_flags |= IDE_TFLAG_OUT_HOB_LBAH;
636
637 if (req_task->out_flags.b.error_feature)
638 args.tf_flags |= IDE_TFLAG_OUT_FEATURE;
639 if (req_task->out_flags.b.nsector)
640 args.tf_flags |= IDE_TFLAG_OUT_NSECT;
641 if (req_task->out_flags.b.sector)
642 args.tf_flags |= IDE_TFLAG_OUT_LBAL;
643 if (req_task->out_flags.b.lcyl)
644 args.tf_flags |= IDE_TFLAG_OUT_LBAM;
645 if (req_task->out_flags.b.hcyl)
646 args.tf_flags |= IDE_TFLAG_OUT_LBAH;
647 }
648
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 drive->io_32bit = 0;
650 switch(req_task->data_phase) {
651 case TASKFILE_OUT_DMAQ:
652 case TASKFILE_OUT_DMA:
653 err = ide_diag_taskfile(drive, &args, taskout, outbuf);
654 break;
655 case TASKFILE_IN_DMAQ:
656 case TASKFILE_IN_DMA:
657 err = ide_diag_taskfile(drive, &args, taskin, inbuf);
658 break;
659 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:
670 args.prehandler = &pre_task_out_intr;
671 args.handler = &task_out_intr;
672 err = ide_diag_taskfile(drive, &args, taskout, outbuf);
673 break;
674 case TASKFILE_MULTI_IN:
675 if (!drive->mult_count) {
676 /* (hs): give up if multcount is not set */
677 printk(KERN_ERR "%s: %s Multimode Read failure " \
678 "multcount is not set\n",
679 drive->name, __FUNCTION__);
680 err = -EPERM;
681 goto abort;
682 }
683 /* fall through */
684 case TASKFILE_IN:
685 args.handler = &task_in_intr;
686 err = ide_diag_taskfile(drive, &args, taskin, inbuf);
687 break;
688 case TASKFILE_NO_DATA:
689 args.handler = &task_no_data_intr;
690 err = ide_diag_taskfile(drive, &args, 0, NULL);
691 break;
692 default:
693 err = -EFAULT;
694 goto abort;
695 }
696
Bartlomiej Zolnierkiewicz650d8412008-01-25 22:17:06 +0100697 memcpy(req_task->hob_ports, &args.tf_array[0], HDIO_DRIVE_HOB_HDR_SIZE - 2);
698 memcpy(req_task->io_ports, &args.tf_array[6], HDIO_DRIVE_TASK_HDR_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 req_task->in_flags = args.tf_in_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700
701 if (copy_to_user(buf, req_task, tasksize)) {
702 err = -EFAULT;
703 goto abort;
704 }
705 if (taskout) {
706 int outtotal = tasksize;
707 if (copy_to_user(buf + outtotal, outbuf, taskout)) {
708 err = -EFAULT;
709 goto abort;
710 }
711 }
712 if (taskin) {
713 int intotal = tasksize + taskout;
714 if (copy_to_user(buf + intotal, inbuf, taskin)) {
715 err = -EFAULT;
716 goto abort;
717 }
718 }
719abort:
720 kfree(req_task);
Jesper Juhl6044ec82005-11-07 01:01:32 -0800721 kfree(outbuf);
722 kfree(inbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723
724// printk("IDE Taskfile ioctl ended. rc = %i\n", err);
725
726 drive->io_32bit = io_32bit;
727
728 return err;
729}
Bartlomiej Zolnierkiewicz26a5b042007-11-05 21:42:27 +0100730#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731
732int ide_wait_cmd (ide_drive_t *drive, u8 cmd, u8 nsect, u8 feature, u8 sectors, u8 *buf)
733{
734 struct request rq;
735 u8 buffer[4];
736
737 if (!buf)
738 buf = buffer;
739 memset(buf, 0, 4 + SECTOR_WORDS * 4 * sectors);
740 ide_init_drive_cmd(&rq);
741 rq.buffer = buf;
742 *buf++ = cmd;
743 *buf++ = nsect;
744 *buf++ = feature;
745 *buf++ = sectors;
746 return ide_do_drive_cmd(drive, &rq, ide_wait);
747}
748
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749int ide_cmd_ioctl (ide_drive_t *drive, unsigned int cmd, unsigned long arg)
750{
751 int err = 0;
752 u8 args[4], *argbuf = args;
753 u8 xfer_rate = 0;
754 int argsize = 4;
755 ide_task_t tfargs;
Bartlomiej Zolnierkiewicz650d8412008-01-25 22:17:06 +0100756 struct ide_taskfile *tf = &tfargs.tf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757
758 if (NULL == (void *) arg) {
759 struct request rq;
760 ide_init_drive_cmd(&rq);
761 return ide_do_drive_cmd(drive, &rq, ide_wait);
762 }
763
764 if (copy_from_user(args, (void __user *)arg, 4))
765 return -EFAULT;
766
767 memset(&tfargs, 0, sizeof(ide_task_t));
Bartlomiej Zolnierkiewicz650d8412008-01-25 22:17:06 +0100768 tf->feature = args[2];
769 tf->nsect = args[3];
770 tf->lbal = args[1];
771 tf->command = args[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772
773 if (args[3]) {
774 argsize = 4 + (SECTOR_WORDS * 4 * args[3]);
Deepak Saxenaf5e3c2f2005-11-07 01:01:25 -0800775 argbuf = kzalloc(argsize, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 if (argbuf == NULL)
777 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 }
779 if (set_transfer(drive, &tfargs)) {
780 xfer_rate = args[1];
781 if (ide_ata66_check(drive, &tfargs))
782 goto abort;
783 }
784
785 err = ide_wait_cmd(drive, args[0], args[1], args[2], args[3], argbuf);
786
787 if (!err && xfer_rate) {
788 /* active-retuning-calls future */
789 ide_set_xfer_rate(drive, xfer_rate);
790 ide_driveid_update(drive);
791 }
792abort:
793 if (copy_to_user((void __user *)arg, argbuf, argsize))
794 err = -EFAULT;
795 if (argsize > 4)
796 kfree(argbuf);
797 return err;
798}
799
800static int ide_wait_cmd_task(ide_drive_t *drive, u8 *buf)
801{
802 struct request rq;
803
804 ide_init_drive_cmd(&rq);
Jens Axboe4aff5e22006-08-10 08:44:47 +0200805 rq.cmd_type = REQ_TYPE_ATA_TASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 rq.buffer = buf;
807 return ide_do_drive_cmd(drive, &rq, ide_wait);
808}
809
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810int ide_task_ioctl (ide_drive_t *drive, unsigned int cmd, unsigned long arg)
811{
812 void __user *p = (void __user *)arg;
813 int err = 0;
814 u8 args[7], *argbuf = args;
815 int argsize = 7;
816
817 if (copy_from_user(args, p, 7))
818 return -EFAULT;
819 err = ide_wait_cmd_task(drive, argbuf);
820 if (copy_to_user(p, argbuf, argsize))
821 err = -EFAULT;
822 return err;
823}
824
825/*
826 * NOTICE: This is additions from IBM to provide a discrete interface,
827 * for selective taskregister access operations. Nice JOB Klaus!!!
828 * Glad to be able to work and co-develop this with you and IBM.
829 */
830ide_startstop_t flagged_taskfile (ide_drive_t *drive, ide_task_t *task)
831{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 if (task->data_phase == TASKFILE_MULTI_IN ||
833 task->data_phase == TASKFILE_MULTI_OUT) {
834 if (!drive->mult_count) {
835 printk(KERN_ERR "%s: multimode not set!\n", drive->name);
836 return ide_stopped;
837 }
838 }
839
840 /*
Bartlomiej Zolnierkiewicze07bc702005-11-19 22:17:55 +0100841 * (ks) Check taskfile in flags.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 * If set, then execute as it is defined.
843 * If not set, then define default settings.
844 * The default values are:
Bartlomiej Zolnierkiewicze07bc702005-11-19 22:17:55 +0100845 * read all taskfile registers (except data)
846 * read the hob registers (sector, nsector, lcyl, hcyl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 if (task->tf_in_flags.all == 0) {
849 task->tf_in_flags.all = IDE_TASKFILE_STD_IN_FLAGS;
850 if (drive->addressing == 1)
851 task->tf_in_flags.all |= (IDE_HOB_STD_IN_FLAGS << 8);
852 }
853
Bartlomiej Zolnierkiewicz74095a92008-01-25 22:17:07 +0100854 return do_rw_taskfile(drive, task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855}