blob: 03c4a0c55bf41338cdfef26b6155d634e6e5be1d [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 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
Bartlomiej Zolnierkiewiczac026ff2008-01-25 22:17:14 +0100501int ide_raw_taskfile(ide_drive_t *drive, ide_task_t *task, u8 *buf, u16 nsect)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502{
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 */
Bartlomiej Zolnierkiewiczac026ff2008-01-25 22:17:14 +0100516 rq.hard_nr_sectors = rq.nr_sectors = nsect;
517 rq.hard_cur_sectors = rq.current_nr_sectors = nsect;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518
Bartlomiej Zolnierkiewiczac026ff2008-01-25 22:17:14 +0100519 if (task->tf_flags & IDE_TFLAG_WRITE)
520 rq.cmd_flags |= REQ_RW;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521
Bartlomiej Zolnierkiewiczac026ff2008-01-25 22:17:14 +0100522 rq.special = task;
523 task->rq = &rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 return ide_do_drive_cmd(drive, &rq, ide_wait);
526}
527
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528EXPORT_SYMBOL(ide_raw_taskfile);
529
Bartlomiej Zolnierkiewicz9a3c49b2008-01-25 22:17:07 +0100530int ide_no_data_taskfile(ide_drive_t *drive, ide_task_t *task)
531{
Bartlomiej Zolnierkiewiczac026ff2008-01-25 22:17:14 +0100532 task->data_phase = TASKFILE_NO_DATA;
533 task->handler = task_no_data_intr;
Bartlomiej Zolnierkiewicz9a3c49b2008-01-25 22:17:07 +0100534
Bartlomiej Zolnierkiewiczac026ff2008-01-25 22:17:14 +0100535 return ide_raw_taskfile(drive, task, NULL, 0);
Bartlomiej Zolnierkiewicz9a3c49b2008-01-25 22:17:07 +0100536}
537EXPORT_SYMBOL_GPL(ide_no_data_taskfile);
538
Bartlomiej Zolnierkiewicz26a5b042007-11-05 21:42:27 +0100539#ifdef CONFIG_IDE_TASK_IOCTL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540int ide_taskfile_ioctl (ide_drive_t *drive, unsigned int cmd, unsigned long arg)
541{
542 ide_task_request_t *req_task;
543 ide_task_t args;
544 u8 *outbuf = NULL;
545 u8 *inbuf = NULL;
Bartlomiej Zolnierkiewiczac026ff2008-01-25 22:17:14 +0100546 u8 *data_buf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 int err = 0;
548 int tasksize = sizeof(struct ide_task_request_s);
Alan Cox3a42bb22006-10-16 16:31:02 +0100549 unsigned int taskin = 0;
550 unsigned int taskout = 0;
Bartlomiej Zolnierkiewiczac026ff2008-01-25 22:17:14 +0100551 u16 nsect = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 u8 io_32bit = drive->io_32bit;
553 char __user *buf = (char __user *)arg;
554
555// printk("IDE Taskfile ...\n");
556
Deepak Saxenaf5e3c2f2005-11-07 01:01:25 -0800557 req_task = kzalloc(tasksize, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 if (req_task == NULL) return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 if (copy_from_user(req_task, buf, tasksize)) {
560 kfree(req_task);
561 return -EFAULT;
562 }
563
Alan Cox3a42bb22006-10-16 16:31:02 +0100564 taskout = req_task->out_size;
565 taskin = req_task->in_size;
566
567 if (taskin > 65536 || taskout > 65536) {
568 err = -EINVAL;
569 goto abort;
570 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571
572 if (taskout) {
573 int outtotal = tasksize;
Deepak Saxenaf5e3c2f2005-11-07 01:01:25 -0800574 outbuf = kzalloc(taskout, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 if (outbuf == NULL) {
576 err = -ENOMEM;
577 goto abort;
578 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 if (copy_from_user(outbuf, buf + outtotal, taskout)) {
580 err = -EFAULT;
581 goto abort;
582 }
583 }
584
585 if (taskin) {
586 int intotal = tasksize + taskout;
Deepak Saxenaf5e3c2f2005-11-07 01:01:25 -0800587 inbuf = kzalloc(taskin, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 if (inbuf == NULL) {
589 err = -ENOMEM;
590 goto abort;
591 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 if (copy_from_user(inbuf, buf + intotal, taskin)) {
593 err = -EFAULT;
594 goto abort;
595 }
596 }
597
598 memset(&args, 0, sizeof(ide_task_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599
Bartlomiej Zolnierkiewicz650d8412008-01-25 22:17:06 +0100600 memcpy(&args.tf_array[0], req_task->hob_ports, HDIO_DRIVE_HOB_HDR_SIZE - 2);
601 memcpy(&args.tf_array[6], req_task->io_ports, HDIO_DRIVE_TASK_HDR_SIZE);
Bartlomiej Zolnierkiewicz866e2ec2008-01-25 22:17:14 +0100602
603 args.data_phase = req_task->data_phase;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604
Bartlomiej Zolnierkiewicza3bbb9d2008-01-25 22:17:10 +0100605 args.tf_flags = IDE_TFLAG_OUT_DEVICE;
606 if (drive->addressing == 1)
607 args.tf_flags |= IDE_TFLAG_LBA48;
608
Bartlomiej Zolnierkiewicz74095a92008-01-25 22:17:07 +0100609 if (req_task->out_flags.all) {
610 args.tf_flags |= IDE_TFLAG_FLAGGED;
611
612 if (req_task->out_flags.b.data)
613 args.tf_flags |= IDE_TFLAG_OUT_DATA;
614
615 if (req_task->out_flags.b.nsector_hob)
616 args.tf_flags |= IDE_TFLAG_OUT_HOB_NSECT;
617 if (req_task->out_flags.b.sector_hob)
618 args.tf_flags |= IDE_TFLAG_OUT_HOB_LBAL;
619 if (req_task->out_flags.b.lcyl_hob)
620 args.tf_flags |= IDE_TFLAG_OUT_HOB_LBAM;
621 if (req_task->out_flags.b.hcyl_hob)
622 args.tf_flags |= IDE_TFLAG_OUT_HOB_LBAH;
623
624 if (req_task->out_flags.b.error_feature)
625 args.tf_flags |= IDE_TFLAG_OUT_FEATURE;
626 if (req_task->out_flags.b.nsector)
627 args.tf_flags |= IDE_TFLAG_OUT_NSECT;
628 if (req_task->out_flags.b.sector)
629 args.tf_flags |= IDE_TFLAG_OUT_LBAL;
630 if (req_task->out_flags.b.lcyl)
631 args.tf_flags |= IDE_TFLAG_OUT_LBAM;
632 if (req_task->out_flags.b.hcyl)
633 args.tf_flags |= IDE_TFLAG_OUT_LBAH;
Bartlomiej Zolnierkiewicza3bbb9d2008-01-25 22:17:10 +0100634 } else {
635 args.tf_flags |= IDE_TFLAG_OUT_TF;
636 if (args.tf_flags & IDE_TFLAG_LBA48)
637 args.tf_flags |= IDE_TFLAG_OUT_HOB;
Bartlomiej Zolnierkiewicz74095a92008-01-25 22:17:07 +0100638 }
639
Bartlomiej Zolnierkiewicz866e2ec2008-01-25 22:17:14 +0100640 if (req_task->in_flags.b.data)
641 args.tf_flags |= IDE_TFLAG_IN_DATA;
642
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 drive->io_32bit = 0;
644 switch(req_task->data_phase) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 case TASKFILE_MULTI_OUT:
646 if (!drive->mult_count) {
647 /* (hs): give up if multcount is not set */
648 printk(KERN_ERR "%s: %s Multimode Write " \
649 "multcount is not set\n",
650 drive->name, __FUNCTION__);
651 err = -EPERM;
652 goto abort;
653 }
654 /* fall through */
655 case TASKFILE_OUT:
656 args.prehandler = &pre_task_out_intr;
657 args.handler = &task_out_intr;
Bartlomiej Zolnierkiewiczac026ff2008-01-25 22:17:14 +0100658 /* fall through */
659 case TASKFILE_OUT_DMAQ:
660 case TASKFILE_OUT_DMA:
661 nsect = taskout / SECTOR_SIZE;
662 data_buf = outbuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 break;
664 case TASKFILE_MULTI_IN:
665 if (!drive->mult_count) {
666 /* (hs): give up if multcount is not set */
667 printk(KERN_ERR "%s: %s Multimode Read failure " \
668 "multcount is not set\n",
669 drive->name, __FUNCTION__);
670 err = -EPERM;
671 goto abort;
672 }
673 /* fall through */
674 case TASKFILE_IN:
675 args.handler = &task_in_intr;
Bartlomiej Zolnierkiewiczac026ff2008-01-25 22:17:14 +0100676 /* fall through */
677 case TASKFILE_IN_DMAQ:
678 case TASKFILE_IN_DMA:
679 nsect = taskin / SECTOR_SIZE;
680 data_buf = inbuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 break;
682 case TASKFILE_NO_DATA:
683 args.handler = &task_no_data_intr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 break;
685 default:
686 err = -EFAULT;
687 goto abort;
688 }
689
Bartlomiej Zolnierkiewiczac026ff2008-01-25 22:17:14 +0100690 if (req_task->req_cmd == IDE_DRIVE_TASK_NO_DATA)
691 nsect = 0;
692 else if (!nsect) {
693 nsect = (args.tf.hob_nsect << 8) | args.tf.nsect;
694
695 if (!nsect) {
696 printk(KERN_ERR "%s: in/out command without data\n",
697 drive->name);
698 err = -EFAULT;
699 goto abort;
700 }
701 }
702
703 if (req_task->req_cmd == IDE_DRIVE_TASK_RAW_WRITE)
704 args.tf_flags |= IDE_TFLAG_WRITE;
705
706 err = ide_raw_taskfile(drive, &args, data_buf, nsect);
707
Bartlomiej Zolnierkiewicz650d8412008-01-25 22:17:06 +0100708 memcpy(req_task->hob_ports, &args.tf_array[0], HDIO_DRIVE_HOB_HDR_SIZE - 2);
709 memcpy(req_task->io_ports, &args.tf_array[6], HDIO_DRIVE_TASK_HDR_SIZE);
Bartlomiej Zolnierkiewicz866e2ec2008-01-25 22:17:14 +0100710
711 if ((args.tf_flags & IDE_TFLAG_FLAGGED_SET_IN_FLAGS) &&
712 req_task->in_flags.all == 0) {
713 req_task->in_flags.all = IDE_TASKFILE_STD_IN_FLAGS;
714 if (drive->addressing == 1)
715 req_task->in_flags.all |= (IDE_HOB_STD_IN_FLAGS << 8);
716 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717
718 if (copy_to_user(buf, req_task, tasksize)) {
719 err = -EFAULT;
720 goto abort;
721 }
722 if (taskout) {
723 int outtotal = tasksize;
724 if (copy_to_user(buf + outtotal, outbuf, taskout)) {
725 err = -EFAULT;
726 goto abort;
727 }
728 }
729 if (taskin) {
730 int intotal = tasksize + taskout;
731 if (copy_to_user(buf + intotal, inbuf, taskin)) {
732 err = -EFAULT;
733 goto abort;
734 }
735 }
736abort:
737 kfree(req_task);
Jesper Juhl6044ec82005-11-07 01:01:32 -0800738 kfree(outbuf);
739 kfree(inbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740
741// printk("IDE Taskfile ioctl ended. rc = %i\n", err);
742
743 drive->io_32bit = io_32bit;
744
745 return err;
746}
Bartlomiej Zolnierkiewicz26a5b042007-11-05 21:42:27 +0100747#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748
749int ide_wait_cmd (ide_drive_t *drive, u8 cmd, u8 nsect, u8 feature, u8 sectors, u8 *buf)
750{
751 struct request rq;
752 u8 buffer[4];
753
754 if (!buf)
755 buf = buffer;
756 memset(buf, 0, 4 + SECTOR_WORDS * 4 * sectors);
757 ide_init_drive_cmd(&rq);
758 rq.buffer = buf;
759 *buf++ = cmd;
760 *buf++ = nsect;
761 *buf++ = feature;
762 *buf++ = sectors;
763 return ide_do_drive_cmd(drive, &rq, ide_wait);
764}
765
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766int ide_cmd_ioctl (ide_drive_t *drive, unsigned int cmd, unsigned long arg)
767{
768 int err = 0;
769 u8 args[4], *argbuf = args;
770 u8 xfer_rate = 0;
771 int argsize = 4;
772 ide_task_t tfargs;
Bartlomiej Zolnierkiewicz650d8412008-01-25 22:17:06 +0100773 struct ide_taskfile *tf = &tfargs.tf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774
775 if (NULL == (void *) arg) {
776 struct request rq;
777 ide_init_drive_cmd(&rq);
778 return ide_do_drive_cmd(drive, &rq, ide_wait);
779 }
780
781 if (copy_from_user(args, (void __user *)arg, 4))
782 return -EFAULT;
783
784 memset(&tfargs, 0, sizeof(ide_task_t));
Bartlomiej Zolnierkiewicz650d8412008-01-25 22:17:06 +0100785 tf->feature = args[2];
786 tf->nsect = args[3];
787 tf->lbal = args[1];
788 tf->command = args[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789
790 if (args[3]) {
791 argsize = 4 + (SECTOR_WORDS * 4 * args[3]);
Deepak Saxenaf5e3c2f2005-11-07 01:01:25 -0800792 argbuf = kzalloc(argsize, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 if (argbuf == NULL)
794 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 }
796 if (set_transfer(drive, &tfargs)) {
797 xfer_rate = args[1];
798 if (ide_ata66_check(drive, &tfargs))
799 goto abort;
800 }
801
802 err = ide_wait_cmd(drive, args[0], args[1], args[2], args[3], argbuf);
803
804 if (!err && xfer_rate) {
805 /* active-retuning-calls future */
806 ide_set_xfer_rate(drive, xfer_rate);
807 ide_driveid_update(drive);
808 }
809abort:
810 if (copy_to_user((void __user *)arg, argbuf, argsize))
811 err = -EFAULT;
812 if (argsize > 4)
813 kfree(argbuf);
814 return err;
815}
816
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817int ide_task_ioctl (ide_drive_t *drive, unsigned int cmd, unsigned long arg)
818{
819 void __user *p = (void __user *)arg;
820 int err = 0;
Bartlomiej Zolnierkiewicz14b89ef2008-01-25 22:17:11 +0100821 u8 args[7];
822 ide_task_t task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823
824 if (copy_from_user(args, p, 7))
825 return -EFAULT;
Bartlomiej Zolnierkiewicz14b89ef2008-01-25 22:17:11 +0100826
827 memset(&task, 0, sizeof(task));
828 memcpy(&task.tf_array[7], &args[1], 6);
829 task.tf.command = args[0];
830 task.tf_flags = IDE_TFLAG_OUT_TF | IDE_TFLAG_OUT_DEVICE;
831
832 err = ide_no_data_taskfile(drive, &task);
833
834 args[0] = task.tf.command;
835 memcpy(&args[1], &task.tf_array[7], 6);
836
837 if (copy_to_user(p, args, 7))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 err = -EFAULT;
Bartlomiej Zolnierkiewicz14b89ef2008-01-25 22:17:11 +0100839
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 return err;
841}
842
843/*
844 * NOTICE: This is additions from IBM to provide a discrete interface,
845 * for selective taskregister access operations. Nice JOB Klaus!!!
846 * Glad to be able to work and co-develop this with you and IBM.
847 */
848ide_startstop_t flagged_taskfile (ide_drive_t *drive, ide_task_t *task)
849{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 if (task->data_phase == TASKFILE_MULTI_IN ||
851 task->data_phase == TASKFILE_MULTI_OUT) {
852 if (!drive->mult_count) {
853 printk(KERN_ERR "%s: multimode not set!\n", drive->name);
854 return ide_stopped;
855 }
856 }
857
Bartlomiej Zolnierkiewicz866e2ec2008-01-25 22:17:14 +0100858 task->tf_flags |= IDE_TFLAG_FLAGGED_SET_IN_FLAGS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859
Bartlomiej Zolnierkiewicz74095a92008-01-25 22:17:07 +0100860 return do_rw_taskfile(drive, task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861}