ide: add struct ide_dma_ops (take 3)
[deliverable/linux.git] / drivers / ide / ide-taskfile.c
CommitLineData
1da177e4 1/*
59bca8cc
BZ
2 * Copyright (C) 2000-2002 Michael Cornwell <cornwell@acm.org>
3 * Copyright (C) 2000-2002 Andre Hedrick <andre@linux-ide.org>
4 * Copyright (C) 2001-2002 Klaus Smolin
1da177e4 5 * IBM Storage Technology Division
59bca8cc 6 * Copyright (C) 2003-2004, 2007 Bartlomiej Zolnierkiewicz
1da177e4
LT
7 *
8 * The big the bad and the ugly.
1da177e4
LT
9 */
10
1da177e4
LT
11#include <linux/module.h>
12#include <linux/types.h>
13#include <linux/string.h>
14#include <linux/kernel.h>
15#include <linux/timer.h>
16#include <linux/mm.h>
651c29a1 17#include <linux/sched.h>
1da177e4
LT
18#include <linux/interrupt.h>
19#include <linux/major.h>
20#include <linux/errno.h>
21#include <linux/genhd.h>
22#include <linux/blkpg.h>
23#include <linux/slab.h>
24#include <linux/pci.h>
25#include <linux/delay.h>
26#include <linux/hdreg.h>
27#include <linux/ide.h>
28#include <linux/bitops.h>
55c16a70 29#include <linux/scatterlist.h>
1da177e4
LT
30
31#include <asm/byteorder.h>
32#include <asm/irq.h>
33#include <asm/uaccess.h>
34#include <asm/io.h>
35
9e42237f
BZ
36void ide_tf_load(ide_drive_t *drive, ide_task_t *task)
37{
38 ide_hwif_t *hwif = drive->hwif;
39 struct ide_taskfile *tf = &task->tf;
40 u8 HIHI = (task->tf_flags & IDE_TFLAG_LBA48) ? 0xE0 : 0xEF;
41
74095a91
BZ
42 if (task->tf_flags & IDE_TFLAG_FLAGGED)
43 HIHI = 0xFF;
44
807e35d6
BZ
45#ifdef DEBUG
46 printk("%s: tf: feat 0x%02x nsect 0x%02x lbal 0x%02x "
47 "lbam 0x%02x lbah 0x%02x dev 0x%02x cmd 0x%02x\n",
48 drive->name, tf->feature, tf->nsect, tf->lbal,
49 tf->lbam, tf->lbah, tf->device, tf->command);
6dd9b837
BZ
50 printk("%s: hob: nsect 0x%02x lbal 0x%02x "
51 "lbam 0x%02x lbah 0x%02x\n",
52 drive->name, tf->hob_nsect, tf->hob_lbal,
53 tf->hob_lbam, tf->hob_lbah);
807e35d6
BZ
54#endif
55
81ca6919 56 ide_set_irq(drive, 1);
9e42237f
BZ
57
58 if ((task->tf_flags & IDE_TFLAG_NO_SELECT_MASK) == 0)
59 SELECT_MASK(drive, 0);
60
74095a91 61 if (task->tf_flags & IDE_TFLAG_OUT_DATA)
23579a2a
BZ
62 hwif->OUTW((tf->hob_data << 8) | tf->data,
63 hwif->io_ports[IDE_DATA_OFFSET]);
74095a91
BZ
64
65 if (task->tf_flags & IDE_TFLAG_OUT_HOB_FEATURE)
23579a2a 66 hwif->OUTB(tf->hob_feature, hwif->io_ports[IDE_FEATURE_OFFSET]);
74095a91 67 if (task->tf_flags & IDE_TFLAG_OUT_HOB_NSECT)
23579a2a 68 hwif->OUTB(tf->hob_nsect, hwif->io_ports[IDE_NSECTOR_OFFSET]);
74095a91 69 if (task->tf_flags & IDE_TFLAG_OUT_HOB_LBAL)
23579a2a 70 hwif->OUTB(tf->hob_lbal, hwif->io_ports[IDE_SECTOR_OFFSET]);
74095a91 71 if (task->tf_flags & IDE_TFLAG_OUT_HOB_LBAM)
23579a2a 72 hwif->OUTB(tf->hob_lbam, hwif->io_ports[IDE_LCYL_OFFSET]);
74095a91 73 if (task->tf_flags & IDE_TFLAG_OUT_HOB_LBAH)
23579a2a 74 hwif->OUTB(tf->hob_lbah, hwif->io_ports[IDE_HCYL_OFFSET]);
9e42237f 75
74095a91 76 if (task->tf_flags & IDE_TFLAG_OUT_FEATURE)
23579a2a 77 hwif->OUTB(tf->feature, hwif->io_ports[IDE_FEATURE_OFFSET]);
74095a91 78 if (task->tf_flags & IDE_TFLAG_OUT_NSECT)
23579a2a 79 hwif->OUTB(tf->nsect, hwif->io_ports[IDE_NSECTOR_OFFSET]);
74095a91 80 if (task->tf_flags & IDE_TFLAG_OUT_LBAL)
23579a2a 81 hwif->OUTB(tf->lbal, hwif->io_ports[IDE_SECTOR_OFFSET]);
74095a91 82 if (task->tf_flags & IDE_TFLAG_OUT_LBAM)
23579a2a 83 hwif->OUTB(tf->lbam, hwif->io_ports[IDE_LCYL_OFFSET]);
74095a91 84 if (task->tf_flags & IDE_TFLAG_OUT_LBAH)
23579a2a 85 hwif->OUTB(tf->lbah, hwif->io_ports[IDE_HCYL_OFFSET]);
9e42237f 86
807e35d6 87 if (task->tf_flags & IDE_TFLAG_OUT_DEVICE)
23579a2a
BZ
88 hwif->OUTB((tf->device & HIHI) | drive->select.all,
89 hwif->io_ports[IDE_SELECT_OFFSET]);
9e42237f
BZ
90}
91
1da177e4
LT
92int taskfile_lib_get_identify (ide_drive_t *drive, u8 *buf)
93{
94 ide_task_t args;
650d841d 95
1da177e4 96 memset(&args, 0, sizeof(ide_task_t));
650d841d 97 args.tf.nsect = 0x01;
1da177e4 98 if (drive->media == ide_disk)
650d841d 99 args.tf.command = WIN_IDENTIFY;
1da177e4 100 else
650d841d 101 args.tf.command = WIN_PIDENTIFY;
657cc1a8 102 args.tf_flags = IDE_TFLAG_TF | IDE_TFLAG_DEVICE;
ac026ff2 103 args.data_phase = TASKFILE_IN;
ac026ff2 104 return ide_raw_taskfile(drive, &args, buf, 1);
1da177e4
LT
105}
106
74095a91
BZ
107static int inline task_dma_ok(ide_task_t *task)
108{
f6e29e35 109 if (blk_fs_request(task->rq) || (task->tf_flags & IDE_TFLAG_FLAGGED))
74095a91
BZ
110 return 1;
111
112 switch (task->tf.command) {
113 case WIN_WRITEDMA_ONCE:
114 case WIN_WRITEDMA:
115 case WIN_WRITEDMA_EXT:
116 case WIN_READDMA_ONCE:
117 case WIN_READDMA:
118 case WIN_READDMA_EXT:
119 case WIN_IDENTIFY_DMA:
120 return 1;
121 }
122
123 return 0;
124}
125
1192e528 126static ide_startstop_t task_no_data_intr(ide_drive_t *);
57d7366b
BZ
127static ide_startstop_t set_geometry_intr(ide_drive_t *);
128static ide_startstop_t recal_intr(ide_drive_t *);
129static ide_startstop_t set_multmode_intr(ide_drive_t *);
f6e29e35
BZ
130static ide_startstop_t pre_task_out_intr(ide_drive_t *, struct request *);
131static ide_startstop_t task_in_intr(ide_drive_t *);
1192e528 132
1da177e4
LT
133ide_startstop_t do_rw_taskfile (ide_drive_t *drive, ide_task_t *task)
134{
135 ide_hwif_t *hwif = HWIF(drive);
650d841d 136 struct ide_taskfile *tf = &task->tf;
57d7366b 137 ide_handler_t *handler = NULL;
5e37bdc0 138 struct ide_dma_ops *dma_ops = hwif->dma_ops;
1da177e4 139
1edee60e
BZ
140 if (task->data_phase == TASKFILE_MULTI_IN ||
141 task->data_phase == TASKFILE_MULTI_OUT) {
142 if (!drive->mult_count) {
143 printk(KERN_ERR "%s: multimode not set!\n",
144 drive->name);
145 return ide_stopped;
146 }
147 }
148
149 if (task->tf_flags & IDE_TFLAG_FLAGGED)
150 task->tf_flags |= IDE_TFLAG_FLAGGED_SET_IN_FLAGS;
151
f6e29e35
BZ
152 if ((task->tf_flags & IDE_TFLAG_DMA_PIO_FALLBACK) == 0)
153 ide_tf_load(drive, task);
1da177e4 154
10d90157
BZ
155 switch (task->data_phase) {
156 case TASKFILE_MULTI_OUT:
157 case TASKFILE_OUT:
23579a2a
BZ
158 hwif->OUTBSYNC(drive, tf->command,
159 hwif->io_ports[IDE_COMMAND_OFFSET]);
10d90157
BZ
160 ndelay(400); /* FIXME */
161 return pre_task_out_intr(drive, task->rq);
162 case TASKFILE_MULTI_IN:
163 case TASKFILE_IN:
57d7366b 164 handler = task_in_intr;
1192e528 165 /* fall-through */
10d90157 166 case TASKFILE_NO_DATA:
57d7366b
BZ
167 if (handler == NULL)
168 handler = task_no_data_intr;
1192e528 169 /* WIN_{SPECIFY,RESTORE,SETMULT} use custom handlers */
57d7366b
BZ
170 if (task->tf_flags & IDE_TFLAG_CUSTOM_HANDLER) {
171 switch (tf->command) {
172 case WIN_SPECIFY: handler = set_geometry_intr; break;
173 case WIN_RESTORE: handler = recal_intr; break;
174 case WIN_SETMULT: handler = set_multmode_intr; break;
175 }
176 }
177 ide_execute_command(drive, tf->command, handler,
178 WAIT_WORSTCASE, NULL);
1da177e4 179 return ide_started;
10d90157
BZ
180 default:
181 if (task_dma_ok(task) == 0 || drive->using_dma == 0 ||
5e37bdc0 182 dma_ops->dma_setup(drive))
10d90157 183 return ide_stopped;
5e37bdc0
BZ
184 dma_ops->dma_exec_cmd(drive, tf->command);
185 dma_ops->dma_start(drive);
74095a91 186 return ide_started;
1da177e4 187 }
1da177e4 188}
f6e29e35 189EXPORT_SYMBOL_GPL(do_rw_taskfile);
1da177e4 190
1da177e4
LT
191/*
192 * set_multmode_intr() is invoked on completion of a WIN_SETMULT cmd.
193 */
57d7366b 194static ide_startstop_t set_multmode_intr(ide_drive_t *drive)
1da177e4 195{
c47137a9 196 u8 stat = ide_read_status(drive);
1da177e4 197
c47137a9 198 if (OK_STAT(stat, READY_STAT, BAD_STAT))
1da177e4 199 drive->mult_count = drive->mult_req;
c47137a9 200 else {
1da177e4
LT
201 drive->mult_req = drive->mult_count = 0;
202 drive->special.b.recalibrate = 1;
203 (void) ide_dump_status(drive, "set_multmode", stat);
204 }
205 return ide_stopped;
206}
207
208/*
209 * set_geometry_intr() is invoked on completion of a WIN_SPECIFY cmd.
210 */
57d7366b 211static ide_startstop_t set_geometry_intr(ide_drive_t *drive)
1da177e4 212{
1da177e4
LT
213 int retries = 5;
214 u8 stat;
215
c47137a9 216 while (((stat = ide_read_status(drive)) & BUSY_STAT) && retries--)
1da177e4
LT
217 udelay(10);
218
219 if (OK_STAT(stat, READY_STAT, BAD_STAT))
220 return ide_stopped;
221
222 if (stat & (ERR_STAT|DRQ_STAT))
223 return ide_error(drive, "set_geometry_intr", stat);
224
125e1874 225 BUG_ON(HWGROUP(drive)->handler != NULL);
1da177e4
LT
226 ide_set_handler(drive, &set_geometry_intr, WAIT_WORSTCASE, NULL);
227 return ide_started;
228}
229
230/*
231 * recal_intr() is invoked on completion of a WIN_RESTORE (recalibrate) cmd.
232 */
57d7366b 233static ide_startstop_t recal_intr(ide_drive_t *drive)
1da177e4 234{
c47137a9 235 u8 stat = ide_read_status(drive);
1da177e4 236
c47137a9 237 if (!OK_STAT(stat, READY_STAT, BAD_STAT))
1da177e4
LT
238 return ide_error(drive, "recal_intr", stat);
239 return ide_stopped;
240}
241
242/*
243 * Handler for commands without a data phase
244 */
1192e528 245static ide_startstop_t task_no_data_intr(ide_drive_t *drive)
1da177e4
LT
246{
247 ide_task_t *args = HWGROUP(drive)->rq->special;
1da177e4
LT
248 u8 stat;
249
366c7f55 250 local_irq_enable_in_hardirq();
c47137a9
BZ
251 stat = ide_read_status(drive);
252
253 if (!OK_STAT(stat, READY_STAT, BAD_STAT))
1da177e4
LT
254 return ide_error(drive, "task_no_data_intr", stat);
255 /* calls ide_end_drive_cmd */
c47137a9 256
1da177e4 257 if (args)
64a57fe4 258 ide_end_drive_cmd(drive, stat, ide_read_error(drive));
1da177e4
LT
259
260 return ide_stopped;
261}
262
da6f4c7f 263static u8 wait_drive_not_busy(ide_drive_t *drive)
1da177e4 264{
b42fa133 265 int retries;
1da177e4
LT
266 u8 stat;
267
268 /*
269 * Last sector was transfered, wait until drive is ready.
2624565c 270 * This can take up to 10 usec, but we will wait max 1 ms.
1da177e4 271 */
b42fa133 272 for (retries = 0; retries < 100; retries++) {
c47137a9
BZ
273 stat = ide_read_status(drive);
274
275 if (stat & BUSY_STAT)
b42fa133
MY
276 udelay(10);
277 else
278 break;
279 }
1da177e4 280
b42fa133 281 if (stat & BUSY_STAT)
1da177e4
LT
282 printk(KERN_ERR "%s: drive still BUSY!\n", drive->name);
283
284 return stat;
285}
286
287static void ide_pio_sector(ide_drive_t *drive, unsigned int write)
288{
289 ide_hwif_t *hwif = drive->hwif;
290 struct scatterlist *sg = hwif->sg_table;
55c16a70 291 struct scatterlist *cursg = hwif->cursg;
1da177e4
LT
292 struct page *page;
293#ifdef CONFIG_HIGHMEM
294 unsigned long flags;
295#endif
296 unsigned int offset;
297 u8 *buf;
298
55c16a70
JA
299 cursg = hwif->cursg;
300 if (!cursg) {
301 cursg = sg;
302 hwif->cursg = sg;
303 }
304
45711f1a 305 page = sg_page(cursg);
55c16a70 306 offset = cursg->offset + hwif->cursg_ofs * SECTOR_SIZE;
1da177e4
LT
307
308 /* get the current page and offset */
309 page = nth_page(page, (offset >> PAGE_SHIFT));
310 offset %= PAGE_SIZE;
311
312#ifdef CONFIG_HIGHMEM
313 local_irq_save(flags);
314#endif
315 buf = kmap_atomic(page, KM_BIO_SRC_IRQ) + offset;
316
317 hwif->nleft--;
318 hwif->cursg_ofs++;
319
55c16a70
JA
320 if ((hwif->cursg_ofs * SECTOR_SIZE) == cursg->length) {
321 hwif->cursg = sg_next(hwif->cursg);
1da177e4
LT
322 hwif->cursg_ofs = 0;
323 }
324
325 /* do the actual data transfer */
326 if (write)
9e47be0c 327 hwif->ata_output_data(drive, buf, SECTOR_WORDS);
1da177e4 328 else
9e47be0c 329 hwif->ata_input_data(drive, buf, SECTOR_WORDS);
1da177e4
LT
330
331 kunmap_atomic(buf, KM_BIO_SRC_IRQ);
332#ifdef CONFIG_HIGHMEM
333 local_irq_restore(flags);
334#endif
335}
336
337static void ide_pio_multi(ide_drive_t *drive, unsigned int write)
338{
339 unsigned int nsect;
340
341 nsect = min_t(unsigned int, drive->hwif->nleft, drive->mult_count);
342 while (nsect--)
343 ide_pio_sector(drive, write);
344}
345
858119e1 346static void ide_pio_datablock(ide_drive_t *drive, struct request *rq,
1da177e4
LT
347 unsigned int write)
348{
35cf2b94
TH
349 u8 saved_io_32bit = drive->io_32bit;
350
1da177e4
LT
351 if (rq->bio) /* fs request */
352 rq->errors = 0;
353
35cf2b94
TH
354 if (rq->cmd_type == REQ_TYPE_ATA_TASKFILE) {
355 ide_task_t *task = rq->special;
356
357 if (task->tf_flags & IDE_TFLAG_IO_16BIT)
358 drive->io_32bit = 0;
359 }
360
651c29a1
AM
361 touch_softlockup_watchdog();
362
1da177e4
LT
363 switch (drive->hwif->data_phase) {
364 case TASKFILE_MULTI_IN:
365 case TASKFILE_MULTI_OUT:
366 ide_pio_multi(drive, write);
367 break;
368 default:
369 ide_pio_sector(drive, write);
370 break;
371 }
35cf2b94
TH
372
373 drive->io_32bit = saved_io_32bit;
1da177e4
LT
374}
375
376static ide_startstop_t task_error(ide_drive_t *drive, struct request *rq,
377 const char *s, u8 stat)
378{
379 if (rq->bio) {
380 ide_hwif_t *hwif = drive->hwif;
381 int sectors = hwif->nsect - hwif->nleft;
382
383 switch (hwif->data_phase) {
384 case TASKFILE_IN:
385 if (hwif->nleft)
386 break;
387 /* fall through */
388 case TASKFILE_OUT:
389 sectors--;
390 break;
391 case TASKFILE_MULTI_IN:
392 if (hwif->nleft)
393 break;
394 /* fall through */
395 case TASKFILE_MULTI_OUT:
396 sectors -= drive->mult_count;
397 default:
398 break;
399 }
400
401 if (sectors > 0) {
402 ide_driver_t *drv;
403
404 drv = *(ide_driver_t **)rq->rq_disk->private_data;
405 drv->end_request(drive, 1, sectors);
406 }
407 }
408 return ide_error(drive, s, stat);
409}
410
4d7a984b 411void task_end_request(ide_drive_t *drive, struct request *rq, u8 stat)
1da177e4 412{
4aff5e23 413 if (rq->cmd_type == REQ_TYPE_ATA_TASKFILE) {
64a57fe4 414 u8 err = ide_read_error(drive);
1da177e4 415
4d7a984b
TH
416 ide_end_drive_cmd(drive, stat, err);
417 return;
1da177e4
LT
418 }
419
03731fbd
RP
420 if (rq->rq_disk) {
421 ide_driver_t *drv;
422
423 drv = *(ide_driver_t **)rq->rq_disk->private_data;;
79f21b84 424 drv->end_request(drive, 1, rq->nr_sectors);
03731fbd 425 } else
79f21b84 426 ide_end_request(drive, 1, rq->nr_sectors);
1da177e4
LT
427}
428
6c3c3158
LT
429/*
430 * We got an interrupt on a task_in case, but no errors and no DRQ.
431 *
432 * It might be a spurious irq (shared irq), but it might be a
433 * command that had no output.
434 */
435static ide_startstop_t task_in_unexpected(ide_drive_t *drive, struct request *rq, u8 stat)
436{
437 /* Command all done? */
438 if (OK_STAT(stat, READY_STAT, BUSY_STAT)) {
439 task_end_request(drive, rq, stat);
440 return ide_stopped;
441 }
442
443 /* Assume it was a spurious irq */
444 ide_set_handler(drive, &task_in_intr, WAIT_WORSTCASE, NULL);
445 return ide_started;
446}
447
1da177e4
LT
448/*
449 * Handler for command with PIO data-in phase (Read/Read Multiple).
450 */
f6e29e35 451static ide_startstop_t task_in_intr(ide_drive_t *drive)
1da177e4
LT
452{
453 ide_hwif_t *hwif = drive->hwif;
454 struct request *rq = HWGROUP(drive)->rq;
c47137a9 455 u8 stat = ide_read_status(drive);
1da177e4 456
6c3c3158
LT
457 /* Error? */
458 if (stat & ERR_STAT)
eb63963a 459 return task_error(drive, rq, __func__, stat);
6c3c3158
LT
460
461 /* Didn't want any data? Odd. */
462 if (!(stat & DRQ_STAT))
463 return task_in_unexpected(drive, rq, stat);
1da177e4
LT
464
465 ide_pio_datablock(drive, rq, 0);
466
6c3c3158 467 /* Are we done? Check status and finish transfer. */
1da177e4
LT
468 if (!hwif->nleft) {
469 stat = wait_drive_not_busy(drive);
73d7de0c 470 if (!OK_STAT(stat, 0, BAD_STAT))
eb63963a 471 return task_error(drive, rq, __func__, stat);
1da177e4
LT
472 task_end_request(drive, rq, stat);
473 return ide_stopped;
474 }
475
476 /* Still data left to transfer. */
477 ide_set_handler(drive, &task_in_intr, WAIT_WORSTCASE, NULL);
478
479 return ide_started;
480}
1da177e4
LT
481
482/*
483 * Handler for command with PIO data-out phase (Write/Write Multiple).
484 */
485static ide_startstop_t task_out_intr (ide_drive_t *drive)
486{
487 ide_hwif_t *hwif = drive->hwif;
488 struct request *rq = HWGROUP(drive)->rq;
c47137a9 489 u8 stat = ide_read_status(drive);
1da177e4
LT
490
491 if (!OK_STAT(stat, DRIVE_READY, drive->bad_wstat))
eb63963a 492 return task_error(drive, rq, __func__, stat);
1da177e4
LT
493
494 /* Deal with unexpected ATA data phase. */
495 if (((stat & DRQ_STAT) == 0) ^ !hwif->nleft)
eb63963a 496 return task_error(drive, rq, __func__, stat);
1da177e4
LT
497
498 if (!hwif->nleft) {
499 task_end_request(drive, rq, stat);
500 return ide_stopped;
501 }
502
503 /* Still data left to transfer. */
504 ide_pio_datablock(drive, rq, 1);
505 ide_set_handler(drive, &task_out_intr, WAIT_WORSTCASE, NULL);
506
507 return ide_started;
508}
509
f6e29e35 510static ide_startstop_t pre_task_out_intr(ide_drive_t *drive, struct request *rq)
1da177e4
LT
511{
512 ide_startstop_t startstop;
513
4906f3b4 514 if (ide_wait_stat(&startstop, drive, DRQ_STAT,
1da177e4
LT
515 drive->bad_wstat, WAIT_DRQ)) {
516 printk(KERN_ERR "%s: no DRQ after issuing %sWRITE%s\n",
517 drive->name,
518 drive->hwif->data_phase ? "MULT" : "",
519 drive->addressing ? "_EXT" : "");
520 return startstop;
521 }
522
523 if (!drive->unmask)
524 local_irq_disable();
525
526 ide_set_handler(drive, &task_out_intr, WAIT_WORSTCASE, NULL);
527 ide_pio_datablock(drive, rq, 1);
528
529 return ide_started;
530}
1da177e4 531
ac026ff2 532int ide_raw_taskfile(ide_drive_t *drive, ide_task_t *task, u8 *buf, u16 nsect)
1da177e4
LT
533{
534 struct request rq;
535
536 memset(&rq, 0, sizeof(rq));
02ac2460 537 rq.ref_count = 1;
4aff5e23 538 rq.cmd_type = REQ_TYPE_ATA_TASKFILE;
1da177e4
LT
539 rq.buffer = buf;
540
541 /*
542 * (ks) We transfer currently only whole sectors.
543 * This is suffient for now. But, it would be great,
544 * if we would find a solution to transfer any size.
545 * To support special commands like READ LONG.
546 */
ac026ff2
BZ
547 rq.hard_nr_sectors = rq.nr_sectors = nsect;
548 rq.hard_cur_sectors = rq.current_nr_sectors = nsect;
1da177e4 549
ac026ff2
BZ
550 if (task->tf_flags & IDE_TFLAG_WRITE)
551 rq.cmd_flags |= REQ_RW;
1da177e4 552
ac026ff2
BZ
553 rq.special = task;
554 task->rq = &rq;
1da177e4 555
1da177e4
LT
556 return ide_do_drive_cmd(drive, &rq, ide_wait);
557}
558
1da177e4
LT
559EXPORT_SYMBOL(ide_raw_taskfile);
560
9a3c49be
BZ
561int ide_no_data_taskfile(ide_drive_t *drive, ide_task_t *task)
562{
ac026ff2 563 task->data_phase = TASKFILE_NO_DATA;
9a3c49be 564
ac026ff2 565 return ide_raw_taskfile(drive, task, NULL, 0);
9a3c49be
BZ
566}
567EXPORT_SYMBOL_GPL(ide_no_data_taskfile);
568
26a5b040 569#ifdef CONFIG_IDE_TASK_IOCTL
1da177e4
LT
570int ide_taskfile_ioctl (ide_drive_t *drive, unsigned int cmd, unsigned long arg)
571{
572 ide_task_request_t *req_task;
573 ide_task_t args;
574 u8 *outbuf = NULL;
575 u8 *inbuf = NULL;
ac026ff2 576 u8 *data_buf = NULL;
1da177e4
LT
577 int err = 0;
578 int tasksize = sizeof(struct ide_task_request_s);
3a42bb22
AC
579 unsigned int taskin = 0;
580 unsigned int taskout = 0;
ac026ff2 581 u16 nsect = 0;
1da177e4
LT
582 char __user *buf = (char __user *)arg;
583
584// printk("IDE Taskfile ...\n");
585
f5e3c2fa 586 req_task = kzalloc(tasksize, GFP_KERNEL);
1da177e4 587 if (req_task == NULL) return -ENOMEM;
1da177e4
LT
588 if (copy_from_user(req_task, buf, tasksize)) {
589 kfree(req_task);
590 return -EFAULT;
591 }
592
3a42bb22
AC
593 taskout = req_task->out_size;
594 taskin = req_task->in_size;
595
596 if (taskin > 65536 || taskout > 65536) {
597 err = -EINVAL;
598 goto abort;
599 }
1da177e4
LT
600
601 if (taskout) {
602 int outtotal = tasksize;
f5e3c2fa 603 outbuf = kzalloc(taskout, GFP_KERNEL);
1da177e4
LT
604 if (outbuf == NULL) {
605 err = -ENOMEM;
606 goto abort;
607 }
1da177e4
LT
608 if (copy_from_user(outbuf, buf + outtotal, taskout)) {
609 err = -EFAULT;
610 goto abort;
611 }
612 }
613
614 if (taskin) {
615 int intotal = tasksize + taskout;
f5e3c2fa 616 inbuf = kzalloc(taskin, GFP_KERNEL);
1da177e4
LT
617 if (inbuf == NULL) {
618 err = -ENOMEM;
619 goto abort;
620 }
1da177e4
LT
621 if (copy_from_user(inbuf, buf + intotal, taskin)) {
622 err = -EFAULT;
623 goto abort;
624 }
625 }
626
627 memset(&args, 0, sizeof(ide_task_t));
1da177e4 628
650d841d
BZ
629 memcpy(&args.tf_array[0], req_task->hob_ports, HDIO_DRIVE_HOB_HDR_SIZE - 2);
630 memcpy(&args.tf_array[6], req_task->io_ports, HDIO_DRIVE_TASK_HDR_SIZE);
866e2ec9
BZ
631
632 args.data_phase = req_task->data_phase;
1da177e4 633
657cc1a8
BZ
634 args.tf_flags = IDE_TFLAG_IO_16BIT | IDE_TFLAG_DEVICE |
635 IDE_TFLAG_IN_TF;
a3bbb9d8 636 if (drive->addressing == 1)
657cc1a8 637 args.tf_flags |= (IDE_TFLAG_LBA48 | IDE_TFLAG_IN_HOB);
a3bbb9d8 638
74095a91
BZ
639 if (req_task->out_flags.all) {
640 args.tf_flags |= IDE_TFLAG_FLAGGED;
641
642 if (req_task->out_flags.b.data)
643 args.tf_flags |= IDE_TFLAG_OUT_DATA;
644
645 if (req_task->out_flags.b.nsector_hob)
646 args.tf_flags |= IDE_TFLAG_OUT_HOB_NSECT;
647 if (req_task->out_flags.b.sector_hob)
648 args.tf_flags |= IDE_TFLAG_OUT_HOB_LBAL;
649 if (req_task->out_flags.b.lcyl_hob)
650 args.tf_flags |= IDE_TFLAG_OUT_HOB_LBAM;
651 if (req_task->out_flags.b.hcyl_hob)
652 args.tf_flags |= IDE_TFLAG_OUT_HOB_LBAH;
653
654 if (req_task->out_flags.b.error_feature)
655 args.tf_flags |= IDE_TFLAG_OUT_FEATURE;
656 if (req_task->out_flags.b.nsector)
657 args.tf_flags |= IDE_TFLAG_OUT_NSECT;
658 if (req_task->out_flags.b.sector)
659 args.tf_flags |= IDE_TFLAG_OUT_LBAL;
660 if (req_task->out_flags.b.lcyl)
661 args.tf_flags |= IDE_TFLAG_OUT_LBAM;
662 if (req_task->out_flags.b.hcyl)
663 args.tf_flags |= IDE_TFLAG_OUT_LBAH;
a3bbb9d8
BZ
664 } else {
665 args.tf_flags |= IDE_TFLAG_OUT_TF;
666 if (args.tf_flags & IDE_TFLAG_LBA48)
667 args.tf_flags |= IDE_TFLAG_OUT_HOB;
74095a91
BZ
668 }
669
866e2ec9
BZ
670 if (req_task->in_flags.b.data)
671 args.tf_flags |= IDE_TFLAG_IN_DATA;
672
1da177e4 673 switch(req_task->data_phase) {
1da177e4
LT
674 case TASKFILE_MULTI_OUT:
675 if (!drive->mult_count) {
676 /* (hs): give up if multcount is not set */
677 printk(KERN_ERR "%s: %s Multimode Write " \
678 "multcount is not set\n",
eb63963a 679 drive->name, __func__);
1da177e4
LT
680 err = -EPERM;
681 goto abort;
682 }
683 /* fall through */
684 case TASKFILE_OUT:
ac026ff2
BZ
685 /* fall through */
686 case TASKFILE_OUT_DMAQ:
687 case TASKFILE_OUT_DMA:
688 nsect = taskout / SECTOR_SIZE;
689 data_buf = outbuf;
1da177e4
LT
690 break;
691 case TASKFILE_MULTI_IN:
692 if (!drive->mult_count) {
693 /* (hs): give up if multcount is not set */
694 printk(KERN_ERR "%s: %s Multimode Read failure " \
695 "multcount is not set\n",
eb63963a 696 drive->name, __func__);
1da177e4
LT
697 err = -EPERM;
698 goto abort;
699 }
700 /* fall through */
701 case TASKFILE_IN:
ac026ff2
BZ
702 /* fall through */
703 case TASKFILE_IN_DMAQ:
704 case TASKFILE_IN_DMA:
705 nsect = taskin / SECTOR_SIZE;
706 data_buf = inbuf;
1da177e4
LT
707 break;
708 case TASKFILE_NO_DATA:
1da177e4
LT
709 break;
710 default:
711 err = -EFAULT;
712 goto abort;
713 }
714
ac026ff2
BZ
715 if (req_task->req_cmd == IDE_DRIVE_TASK_NO_DATA)
716 nsect = 0;
717 else if (!nsect) {
718 nsect = (args.tf.hob_nsect << 8) | args.tf.nsect;
719
720 if (!nsect) {
721 printk(KERN_ERR "%s: in/out command without data\n",
722 drive->name);
723 err = -EFAULT;
724 goto abort;
725 }
726 }
727
728 if (req_task->req_cmd == IDE_DRIVE_TASK_RAW_WRITE)
729 args.tf_flags |= IDE_TFLAG_WRITE;
730
731 err = ide_raw_taskfile(drive, &args, data_buf, nsect);
732
650d841d
BZ
733 memcpy(req_task->hob_ports, &args.tf_array[0], HDIO_DRIVE_HOB_HDR_SIZE - 2);
734 memcpy(req_task->io_ports, &args.tf_array[6], HDIO_DRIVE_TASK_HDR_SIZE);
866e2ec9
BZ
735
736 if ((args.tf_flags & IDE_TFLAG_FLAGGED_SET_IN_FLAGS) &&
737 req_task->in_flags.all == 0) {
738 req_task->in_flags.all = IDE_TASKFILE_STD_IN_FLAGS;
739 if (drive->addressing == 1)
740 req_task->in_flags.all |= (IDE_HOB_STD_IN_FLAGS << 8);
741 }
1da177e4
LT
742
743 if (copy_to_user(buf, req_task, tasksize)) {
744 err = -EFAULT;
745 goto abort;
746 }
747 if (taskout) {
748 int outtotal = tasksize;
749 if (copy_to_user(buf + outtotal, outbuf, taskout)) {
750 err = -EFAULT;
751 goto abort;
752 }
753 }
754 if (taskin) {
755 int intotal = tasksize + taskout;
756 if (copy_to_user(buf + intotal, inbuf, taskin)) {
757 err = -EFAULT;
758 goto abort;
759 }
760 }
761abort:
762 kfree(req_task);
6044ec88
JJ
763 kfree(outbuf);
764 kfree(inbuf);
1da177e4
LT
765
766// printk("IDE Taskfile ioctl ended. rc = %i\n", err);
767
1da177e4
LT
768 return err;
769}
26a5b040 770#endif
1da177e4 771
1da177e4
LT
772int ide_cmd_ioctl (ide_drive_t *drive, unsigned int cmd, unsigned long arg)
773{
5a9e77af
BZ
774 u8 *buf = NULL;
775 int bufsize = 0, err = 0;
776 u8 args[4], xfer_rate = 0;
1da177e4 777 ide_task_t tfargs;
650d841d 778 struct ide_taskfile *tf = &tfargs.tf;
5efe7c54 779 struct hd_driveid *id = drive->id;
1da177e4
LT
780
781 if (NULL == (void *) arg) {
782 struct request rq;
145b75e9 783
1da177e4 784 ide_init_drive_cmd(&rq);
852738f3 785 rq.cmd_type = REQ_TYPE_ATA_TASKFILE;
145b75e9 786
1da177e4
LT
787 return ide_do_drive_cmd(drive, &rq, ide_wait);
788 }
789
790 if (copy_from_user(args, (void __user *)arg, 4))
791 return -EFAULT;
792
793 memset(&tfargs, 0, sizeof(ide_task_t));
650d841d 794 tf->feature = args[2];
5a9e77af
BZ
795 if (args[0] == WIN_SMART) {
796 tf->nsect = args[3];
797 tf->lbal = args[1];
798 tf->lbam = 0x4f;
799 tf->lbah = 0xc2;
800 tfargs.tf_flags = IDE_TFLAG_OUT_TF | IDE_TFLAG_IN_NSECT;
801 } else {
802 tf->nsect = args[1];
803 tfargs.tf_flags = IDE_TFLAG_OUT_FEATURE |
804 IDE_TFLAG_OUT_NSECT | IDE_TFLAG_IN_NSECT;
805 }
650d841d 806 tf->command = args[0];
5a9e77af 807 tfargs.data_phase = args[3] ? TASKFILE_IN : TASKFILE_NO_DATA;
1da177e4
LT
808
809 if (args[3]) {
5a9e77af
BZ
810 tfargs.tf_flags |= IDE_TFLAG_IO_16BIT;
811 bufsize = SECTOR_WORDS * 4 * args[3];
812 buf = kzalloc(bufsize, GFP_KERNEL);
813 if (buf == NULL)
1da177e4 814 return -ENOMEM;
1da177e4 815 }
5a9e77af 816
5efe7c54
BZ
817 if (tf->command == WIN_SETFEATURES &&
818 tf->feature == SETFEATURES_XFER &&
819 tf->nsect >= XFER_SW_DMA_0 &&
820 (id->dma_ultra || id->dma_mword || id->dma_1word)) {
1da177e4 821 xfer_rate = args[1];
af10f773
BZ
822 if (tf->nsect > XFER_UDMA_2 && !eighty_ninty_three(drive)) {
823 printk(KERN_WARNING "%s: UDMA speeds >UDMA33 cannot "
824 "be set\n", drive->name);
1da177e4 825 goto abort;
af10f773 826 }
1da177e4
LT
827 }
828
5a9e77af
BZ
829 err = ide_raw_taskfile(drive, &tfargs, buf, args[3]);
830
831 args[0] = tf->status;
832 args[1] = tf->error;
833 args[2] = tf->nsect;
1da177e4
LT
834
835 if (!err && xfer_rate) {
836 /* active-retuning-calls future */
837 ide_set_xfer_rate(drive, xfer_rate);
838 ide_driveid_update(drive);
839 }
840abort:
5a9e77af 841 if (copy_to_user((void __user *)arg, &args, 4))
1da177e4 842 err = -EFAULT;
5a9e77af
BZ
843 if (buf) {
844 if (copy_to_user((void __user *)(arg + 4), buf, bufsize))
845 err = -EFAULT;
846 kfree(buf);
847 }
1da177e4
LT
848 return err;
849}
850
1da177e4
LT
851int ide_task_ioctl (ide_drive_t *drive, unsigned int cmd, unsigned long arg)
852{
853 void __user *p = (void __user *)arg;
854 int err = 0;
14b89ef9
BZ
855 u8 args[7];
856 ide_task_t task;
1da177e4
LT
857
858 if (copy_from_user(args, p, 7))
859 return -EFAULT;
14b89ef9
BZ
860
861 memset(&task, 0, sizeof(task));
862 memcpy(&task.tf_array[7], &args[1], 6);
863 task.tf.command = args[0];
657cc1a8 864 task.tf_flags = IDE_TFLAG_TF | IDE_TFLAG_DEVICE;
14b89ef9
BZ
865
866 err = ide_no_data_taskfile(drive, &task);
867
868 args[0] = task.tf.command;
869 memcpy(&args[1], &task.tf_array[7], 6);
870
871 if (copy_to_user(p, args, 7))
1da177e4 872 err = -EFAULT;
14b89ef9 873
1da177e4
LT
874 return err;
875}
This page took 0.586902 seconds and 5 git commands to generate.