ide: remove ide_cmd() helper
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / ide / ide-taskfile.c
CommitLineData
1da177e4
LT
1/*
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.
1da177e4
LT
11 */
12
1da177e4
LT
13#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>
651c29a1 19#include <linux/sched.h>
1da177e4
LT
20#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>
55c16a70 31#include <linux/scatterlist.h>
1da177e4
LT
32
33#include <asm/byteorder.h>
34#include <asm/irq.h>
35#include <asm/uaccess.h>
36#include <asm/io.h>
37
1da177e4
LT
38static 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
9e42237f
BZ
66void 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
74095a91
BZ
72 if (task->tf_flags & IDE_TFLAG_FLAGGED)
73 HIHI = 0xFF;
74
9e42237f
BZ
75 if (IDE_CONTROL_REG)
76 hwif->OUTB(drive->ctl, IDE_CONTROL_REG); /* clear nIEN */
77
78 if ((task->tf_flags & IDE_TFLAG_NO_SELECT_MASK) == 0)
79 SELECT_MASK(drive, 0);
80
74095a91
BZ
81 if (task->tf_flags & IDE_TFLAG_OUT_DATA)
82 hwif->OUTW((tf->hob_data << 8) | tf->data, IDE_DATA_REG);
83
84 if (task->tf_flags & IDE_TFLAG_OUT_HOB_FEATURE)
9e42237f 85 hwif->OUTB(tf->hob_feature, IDE_FEATURE_REG);
74095a91 86 if (task->tf_flags & IDE_TFLAG_OUT_HOB_NSECT)
9e42237f 87 hwif->OUTB(tf->hob_nsect, IDE_NSECTOR_REG);
74095a91 88 if (task->tf_flags & IDE_TFLAG_OUT_HOB_LBAL)
9e42237f 89 hwif->OUTB(tf->hob_lbal, IDE_SECTOR_REG);
74095a91 90 if (task->tf_flags & IDE_TFLAG_OUT_HOB_LBAM)
9e42237f 91 hwif->OUTB(tf->hob_lbam, IDE_LCYL_REG);
74095a91 92 if (task->tf_flags & IDE_TFLAG_OUT_HOB_LBAH)
9e42237f 93 hwif->OUTB(tf->hob_lbah, IDE_HCYL_REG);
9e42237f 94
74095a91
BZ
95 if (task->tf_flags & IDE_TFLAG_OUT_FEATURE)
96 hwif->OUTB(tf->feature, IDE_FEATURE_REG);
97 if (task->tf_flags & IDE_TFLAG_OUT_NSECT)
98 hwif->OUTB(tf->nsect, IDE_NSECTOR_REG);
99 if (task->tf_flags & IDE_TFLAG_OUT_LBAL)
100 hwif->OUTB(tf->lbal, IDE_SECTOR_REG);
101 if (task->tf_flags & IDE_TFLAG_OUT_LBAM)
102 hwif->OUTB(tf->lbam, IDE_LCYL_REG);
103 if (task->tf_flags & IDE_TFLAG_OUT_LBAH)
104 hwif->OUTB(tf->lbah, IDE_HCYL_REG);
9e42237f
BZ
105
106 hwif->OUTB((tf->device & HIHI) | drive->select.all, IDE_SELECT_REG);
107}
108
109EXPORT_SYMBOL_GPL(ide_tf_load);
110
1da177e4
LT
111int taskfile_lib_get_identify (ide_drive_t *drive, u8 *buf)
112{
113 ide_task_t args;
650d841d 114
1da177e4 115 memset(&args, 0, sizeof(ide_task_t));
650d841d 116 args.tf.nsect = 0x01;
1da177e4 117 if (drive->media == ide_disk)
650d841d 118 args.tf.command = WIN_IDENTIFY;
1da177e4 119 else
650d841d 120 args.tf.command = WIN_PIDENTIFY;
1da177e4
LT
121 args.command_type = IDE_DRIVE_TASK_IN;
122 args.data_phase = TASKFILE_IN;
123 args.handler = &task_in_intr;
124 return ide_raw_taskfile(drive, &args, buf);
125}
126
74095a91
BZ
127static int inline task_dma_ok(ide_task_t *task)
128{
129 if (task->tf_flags & IDE_TFLAG_FLAGGED)
130 return 1;
131
132 switch (task->tf.command) {
133 case WIN_WRITEDMA_ONCE:
134 case WIN_WRITEDMA:
135 case WIN_WRITEDMA_EXT:
136 case WIN_READDMA_ONCE:
137 case WIN_READDMA:
138 case WIN_READDMA_EXT:
139 case WIN_IDENTIFY_DMA:
140 return 1;
141 }
142
143 return 0;
144}
145
1da177e4
LT
146ide_startstop_t do_rw_taskfile (ide_drive_t *drive, ide_task_t *task)
147{
148 ide_hwif_t *hwif = HWIF(drive);
650d841d 149 struct ide_taskfile *tf = &task->tf;
1da177e4 150
9e42237f 151 ide_tf_load(drive, task);
1da177e4
LT
152
153 if (task->handler != NULL) {
154 if (task->prehandler != NULL) {
650d841d 155 hwif->OUTBSYNC(drive, tf->command, IDE_COMMAND_REG);
1da177e4
LT
156 ndelay(400); /* FIXME */
157 return task->prehandler(drive, task->rq);
158 }
650d841d 159 ide_execute_command(drive, tf->command, task->handler, WAIT_WORSTCASE, NULL);
1da177e4
LT
160 return ide_started;
161 }
162
74095a91
BZ
163 if (task_dma_ok(task) && drive->using_dma && !hwif->dma_setup(drive)) {
164 hwif->dma_exec_cmd(drive, tf->command);
165 hwif->dma_start(drive);
166 return ide_started;
1da177e4
LT
167 }
168
169 return ide_stopped;
170}
171
1da177e4
LT
172/*
173 * set_multmode_intr() is invoked on completion of a WIN_SETMULT cmd.
174 */
175ide_startstop_t set_multmode_intr (ide_drive_t *drive)
176{
177 ide_hwif_t *hwif = HWIF(drive);
178 u8 stat;
179
180 if (OK_STAT(stat = hwif->INB(IDE_STATUS_REG),READY_STAT,BAD_STAT)) {
181 drive->mult_count = drive->mult_req;
182 } else {
183 drive->mult_req = drive->mult_count = 0;
184 drive->special.b.recalibrate = 1;
185 (void) ide_dump_status(drive, "set_multmode", stat);
186 }
187 return ide_stopped;
188}
189
190/*
191 * set_geometry_intr() is invoked on completion of a WIN_SPECIFY cmd.
192 */
193ide_startstop_t set_geometry_intr (ide_drive_t *drive)
194{
195 ide_hwif_t *hwif = HWIF(drive);
196 int retries = 5;
197 u8 stat;
198
199 while (((stat = hwif->INB(IDE_STATUS_REG)) & BUSY_STAT) && retries--)
200 udelay(10);
201
202 if (OK_STAT(stat, READY_STAT, BAD_STAT))
203 return ide_stopped;
204
205 if (stat & (ERR_STAT|DRQ_STAT))
206 return ide_error(drive, "set_geometry_intr", stat);
207
125e1874 208 BUG_ON(HWGROUP(drive)->handler != NULL);
1da177e4
LT
209 ide_set_handler(drive, &set_geometry_intr, WAIT_WORSTCASE, NULL);
210 return ide_started;
211}
212
213/*
214 * recal_intr() is invoked on completion of a WIN_RESTORE (recalibrate) cmd.
215 */
216ide_startstop_t recal_intr (ide_drive_t *drive)
217{
218 ide_hwif_t *hwif = HWIF(drive);
219 u8 stat;
220
221 if (!OK_STAT(stat = hwif->INB(IDE_STATUS_REG), READY_STAT, BAD_STAT))
222 return ide_error(drive, "recal_intr", stat);
223 return ide_stopped;
224}
225
226/*
227 * Handler for commands without a data phase
228 */
229ide_startstop_t task_no_data_intr (ide_drive_t *drive)
230{
231 ide_task_t *args = HWGROUP(drive)->rq->special;
232 ide_hwif_t *hwif = HWIF(drive);
233 u8 stat;
234
366c7f55 235 local_irq_enable_in_hardirq();
1da177e4
LT
236 if (!OK_STAT(stat = hwif->INB(IDE_STATUS_REG),READY_STAT,BAD_STAT)) {
237 return ide_error(drive, "task_no_data_intr", stat);
238 /* calls ide_end_drive_cmd */
239 }
240 if (args)
241 ide_end_drive_cmd(drive, stat, hwif->INB(IDE_ERROR_REG));
242
243 return ide_stopped;
244}
245
1da177e4
LT
246static u8 wait_drive_not_busy(ide_drive_t *drive)
247{
248 ide_hwif_t *hwif = HWIF(drive);
b42fa133 249 int retries;
1da177e4
LT
250 u8 stat;
251
252 /*
253 * Last sector was transfered, wait until drive is ready.
254 * This can take up to 10 usec, but we will wait max 1 ms
255 * (drive_cmd_intr() waits that long).
256 */
b42fa133
MY
257 for (retries = 0; retries < 100; retries++) {
258 if ((stat = hwif->INB(IDE_STATUS_REG)) & BUSY_STAT)
259 udelay(10);
260 else
261 break;
262 }
1da177e4 263
b42fa133 264 if (stat & BUSY_STAT)
1da177e4
LT
265 printk(KERN_ERR "%s: drive still BUSY!\n", drive->name);
266
267 return stat;
268}
269
270static void ide_pio_sector(ide_drive_t *drive, unsigned int write)
271{
272 ide_hwif_t *hwif = drive->hwif;
273 struct scatterlist *sg = hwif->sg_table;
55c16a70 274 struct scatterlist *cursg = hwif->cursg;
1da177e4
LT
275 struct page *page;
276#ifdef CONFIG_HIGHMEM
277 unsigned long flags;
278#endif
279 unsigned int offset;
280 u8 *buf;
281
55c16a70
JA
282 cursg = hwif->cursg;
283 if (!cursg) {
284 cursg = sg;
285 hwif->cursg = sg;
286 }
287
45711f1a 288 page = sg_page(cursg);
55c16a70 289 offset = cursg->offset + hwif->cursg_ofs * SECTOR_SIZE;
1da177e4
LT
290
291 /* get the current page and offset */
292 page = nth_page(page, (offset >> PAGE_SHIFT));
293 offset %= PAGE_SIZE;
294
295#ifdef CONFIG_HIGHMEM
296 local_irq_save(flags);
297#endif
298 buf = kmap_atomic(page, KM_BIO_SRC_IRQ) + offset;
299
300 hwif->nleft--;
301 hwif->cursg_ofs++;
302
55c16a70
JA
303 if ((hwif->cursg_ofs * SECTOR_SIZE) == cursg->length) {
304 hwif->cursg = sg_next(hwif->cursg);
1da177e4
LT
305 hwif->cursg_ofs = 0;
306 }
307
308 /* do the actual data transfer */
309 if (write)
310 taskfile_output_data(drive, buf, SECTOR_WORDS);
311 else
312 taskfile_input_data(drive, buf, SECTOR_WORDS);
313
314 kunmap_atomic(buf, KM_BIO_SRC_IRQ);
315#ifdef CONFIG_HIGHMEM
316 local_irq_restore(flags);
317#endif
318}
319
320static void ide_pio_multi(ide_drive_t *drive, unsigned int write)
321{
322 unsigned int nsect;
323
324 nsect = min_t(unsigned int, drive->hwif->nleft, drive->mult_count);
325 while (nsect--)
326 ide_pio_sector(drive, write);
327}
328
858119e1 329static void ide_pio_datablock(ide_drive_t *drive, struct request *rq,
1da177e4
LT
330 unsigned int write)
331{
332 if (rq->bio) /* fs request */
333 rq->errors = 0;
334
651c29a1
AM
335 touch_softlockup_watchdog();
336
1da177e4
LT
337 switch (drive->hwif->data_phase) {
338 case TASKFILE_MULTI_IN:
339 case TASKFILE_MULTI_OUT:
340 ide_pio_multi(drive, write);
341 break;
342 default:
343 ide_pio_sector(drive, write);
344 break;
345 }
346}
347
348static ide_startstop_t task_error(ide_drive_t *drive, struct request *rq,
349 const char *s, u8 stat)
350{
351 if (rq->bio) {
352 ide_hwif_t *hwif = drive->hwif;
353 int sectors = hwif->nsect - hwif->nleft;
354
355 switch (hwif->data_phase) {
356 case TASKFILE_IN:
357 if (hwif->nleft)
358 break;
359 /* fall through */
360 case TASKFILE_OUT:
361 sectors--;
362 break;
363 case TASKFILE_MULTI_IN:
364 if (hwif->nleft)
365 break;
366 /* fall through */
367 case TASKFILE_MULTI_OUT:
368 sectors -= drive->mult_count;
369 default:
370 break;
371 }
372
373 if (sectors > 0) {
374 ide_driver_t *drv;
375
376 drv = *(ide_driver_t **)rq->rq_disk->private_data;
377 drv->end_request(drive, 1, sectors);
378 }
379 }
380 return ide_error(drive, s, stat);
381}
382
383static void task_end_request(ide_drive_t *drive, struct request *rq, u8 stat)
384{
55c16a70
JA
385 HWIF(drive)->cursg = NULL;
386
4aff5e23 387 if (rq->cmd_type == REQ_TYPE_ATA_TASKFILE) {
1da177e4
LT
388 ide_task_t *task = rq->special;
389
74095a91 390 if (task->tf_flags & IDE_TFLAG_FLAGGED) {
1da177e4
LT
391 u8 err = drive->hwif->INB(IDE_ERROR_REG);
392 ide_end_drive_cmd(drive, stat, err);
393 return;
394 }
395 }
396
03731fbd
RP
397 if (rq->rq_disk) {
398 ide_driver_t *drv;
399
400 drv = *(ide_driver_t **)rq->rq_disk->private_data;;
401 drv->end_request(drive, 1, rq->hard_nr_sectors);
402 } else
403 ide_end_request(drive, 1, rq->hard_nr_sectors);
1da177e4
LT
404}
405
406/*
407 * Handler for command with PIO data-in phase (Read/Read Multiple).
408 */
409ide_startstop_t task_in_intr (ide_drive_t *drive)
410{
411 ide_hwif_t *hwif = drive->hwif;
412 struct request *rq = HWGROUP(drive)->rq;
413 u8 stat = hwif->INB(IDE_STATUS_REG);
414
415 /* new way for dealing with premature shared PCI interrupts */
416 if (!OK_STAT(stat, DATA_READY, BAD_R_STAT)) {
417 if (stat & (ERR_STAT | DRQ_STAT))
418 return task_error(drive, rq, __FUNCTION__, stat);
419 /* No data yet, so wait for another IRQ. */
420 ide_set_handler(drive, &task_in_intr, WAIT_WORSTCASE, NULL);
421 return ide_started;
422 }
423
424 ide_pio_datablock(drive, rq, 0);
425
426 /* If it was the last datablock check status and finish transfer. */
427 if (!hwif->nleft) {
428 stat = wait_drive_not_busy(drive);
429 if (!OK_STAT(stat, 0, BAD_R_STAT))
430 return task_error(drive, rq, __FUNCTION__, stat);
431 task_end_request(drive, rq, stat);
432 return ide_stopped;
433 }
434
435 /* Still data left to transfer. */
436 ide_set_handler(drive, &task_in_intr, WAIT_WORSTCASE, NULL);
437
438 return ide_started;
439}
440EXPORT_SYMBOL(task_in_intr);
441
442/*
443 * Handler for command with PIO data-out phase (Write/Write Multiple).
444 */
445static ide_startstop_t task_out_intr (ide_drive_t *drive)
446{
447 ide_hwif_t *hwif = drive->hwif;
448 struct request *rq = HWGROUP(drive)->rq;
449 u8 stat = hwif->INB(IDE_STATUS_REG);
450
451 if (!OK_STAT(stat, DRIVE_READY, drive->bad_wstat))
452 return task_error(drive, rq, __FUNCTION__, stat);
453
454 /* Deal with unexpected ATA data phase. */
455 if (((stat & DRQ_STAT) == 0) ^ !hwif->nleft)
456 return task_error(drive, rq, __FUNCTION__, stat);
457
458 if (!hwif->nleft) {
459 task_end_request(drive, rq, stat);
460 return ide_stopped;
461 }
462
463 /* Still data left to transfer. */
464 ide_pio_datablock(drive, rq, 1);
465 ide_set_handler(drive, &task_out_intr, WAIT_WORSTCASE, NULL);
466
467 return ide_started;
468}
469
470ide_startstop_t pre_task_out_intr (ide_drive_t *drive, struct request *rq)
471{
472 ide_startstop_t startstop;
473
474 if (ide_wait_stat(&startstop, drive, DATA_READY,
475 drive->bad_wstat, WAIT_DRQ)) {
476 printk(KERN_ERR "%s: no DRQ after issuing %sWRITE%s\n",
477 drive->name,
478 drive->hwif->data_phase ? "MULT" : "",
479 drive->addressing ? "_EXT" : "");
480 return startstop;
481 }
482
483 if (!drive->unmask)
484 local_irq_disable();
485
486 ide_set_handler(drive, &task_out_intr, WAIT_WORSTCASE, NULL);
487 ide_pio_datablock(drive, rq, 1);
488
489 return ide_started;
490}
491EXPORT_SYMBOL(pre_task_out_intr);
492
493static int ide_diag_taskfile(ide_drive_t *drive, ide_task_t *args, unsigned long data_size, u8 *buf)
494{
495 struct request rq;
496
497 memset(&rq, 0, sizeof(rq));
02ac2460 498 rq.ref_count = 1;
4aff5e23 499 rq.cmd_type = REQ_TYPE_ATA_TASKFILE;
1da177e4
LT
500 rq.buffer = buf;
501
502 /*
503 * (ks) We transfer currently only whole sectors.
504 * This is suffient for now. But, it would be great,
505 * if we would find a solution to transfer any size.
506 * To support special commands like READ LONG.
507 */
508 if (args->command_type != IDE_DRIVE_TASK_NO_DATA) {
509 if (data_size == 0)
650d841d 510 rq.nr_sectors = (args->tf.hob_nsect << 8) | args->tf.nsect;
1da177e4
LT
511 else
512 rq.nr_sectors = data_size / SECTOR_SIZE;
513
514 if (!rq.nr_sectors) {
515 printk(KERN_ERR "%s: in/out command without data\n",
516 drive->name);
517 return -EFAULT;
518 }
519
520 rq.hard_nr_sectors = rq.nr_sectors;
521 rq.hard_cur_sectors = rq.current_nr_sectors = rq.nr_sectors;
522
523 if (args->command_type == IDE_DRIVE_TASK_RAW_WRITE)
4aff5e23 524 rq.cmd_flags |= REQ_RW;
1da177e4
LT
525 }
526
527 rq.special = args;
ef0f6a43 528 args->rq = &rq;
1da177e4
LT
529 return ide_do_drive_cmd(drive, &rq, ide_wait);
530}
531
532int ide_raw_taskfile (ide_drive_t *drive, ide_task_t *args, u8 *buf)
533{
534 return ide_diag_taskfile(drive, args, 0, buf);
535}
536
537EXPORT_SYMBOL(ide_raw_taskfile);
538
9a3c49be
BZ
539int ide_no_data_taskfile(ide_drive_t *drive, ide_task_t *task)
540{
541 task->command_type = IDE_DRIVE_TASK_NO_DATA;
542 task->data_phase = TASKFILE_NO_DATA;
543 task->handler = task_no_data_intr;
544
545 return ide_raw_taskfile(drive, task, NULL);
546}
547EXPORT_SYMBOL_GPL(ide_no_data_taskfile);
548
26a5b040 549#ifdef CONFIG_IDE_TASK_IOCTL
1da177e4
LT
550int ide_taskfile_ioctl (ide_drive_t *drive, unsigned int cmd, unsigned long arg)
551{
552 ide_task_request_t *req_task;
553 ide_task_t args;
554 u8 *outbuf = NULL;
555 u8 *inbuf = NULL;
1da177e4
LT
556 int err = 0;
557 int tasksize = sizeof(struct ide_task_request_s);
3a42bb22
AC
558 unsigned int taskin = 0;
559 unsigned int taskout = 0;
1da177e4
LT
560 u8 io_32bit = drive->io_32bit;
561 char __user *buf = (char __user *)arg;
562
563// printk("IDE Taskfile ...\n");
564
f5e3c2fa 565 req_task = kzalloc(tasksize, GFP_KERNEL);
1da177e4 566 if (req_task == NULL) return -ENOMEM;
1da177e4
LT
567 if (copy_from_user(req_task, buf, tasksize)) {
568 kfree(req_task);
569 return -EFAULT;
570 }
571
3a42bb22
AC
572 taskout = req_task->out_size;
573 taskin = req_task->in_size;
574
575 if (taskin > 65536 || taskout > 65536) {
576 err = -EINVAL;
577 goto abort;
578 }
1da177e4
LT
579
580 if (taskout) {
581 int outtotal = tasksize;
f5e3c2fa 582 outbuf = kzalloc(taskout, GFP_KERNEL);
1da177e4
LT
583 if (outbuf == NULL) {
584 err = -ENOMEM;
585 goto abort;
586 }
1da177e4
LT
587 if (copy_from_user(outbuf, buf + outtotal, taskout)) {
588 err = -EFAULT;
589 goto abort;
590 }
591 }
592
593 if (taskin) {
594 int intotal = tasksize + taskout;
f5e3c2fa 595 inbuf = kzalloc(taskin, GFP_KERNEL);
1da177e4
LT
596 if (inbuf == NULL) {
597 err = -ENOMEM;
598 goto abort;
599 }
1da177e4
LT
600 if (copy_from_user(inbuf, buf + intotal, taskin)) {
601 err = -EFAULT;
602 goto abort;
603 }
604 }
605
606 memset(&args, 0, sizeof(ide_task_t));
1da177e4 607
650d841d
BZ
608 memcpy(&args.tf_array[0], req_task->hob_ports, HDIO_DRIVE_HOB_HDR_SIZE - 2);
609 memcpy(&args.tf_array[6], req_task->io_ports, HDIO_DRIVE_TASK_HDR_SIZE);
1da177e4 610 args.tf_in_flags = req_task->in_flags;
1da177e4
LT
611 args.data_phase = req_task->data_phase;
612 args.command_type = req_task->req_cmd;
613
74095a91
BZ
614 if (req_task->out_flags.all) {
615 args.tf_flags |= IDE_TFLAG_FLAGGED;
616
617 if (req_task->out_flags.b.data)
618 args.tf_flags |= IDE_TFLAG_OUT_DATA;
619
620 if (req_task->out_flags.b.nsector_hob)
621 args.tf_flags |= IDE_TFLAG_OUT_HOB_NSECT;
622 if (req_task->out_flags.b.sector_hob)
623 args.tf_flags |= IDE_TFLAG_OUT_HOB_LBAL;
624 if (req_task->out_flags.b.lcyl_hob)
625 args.tf_flags |= IDE_TFLAG_OUT_HOB_LBAM;
626 if (req_task->out_flags.b.hcyl_hob)
627 args.tf_flags |= IDE_TFLAG_OUT_HOB_LBAH;
628
629 if (req_task->out_flags.b.error_feature)
630 args.tf_flags |= IDE_TFLAG_OUT_FEATURE;
631 if (req_task->out_flags.b.nsector)
632 args.tf_flags |= IDE_TFLAG_OUT_NSECT;
633 if (req_task->out_flags.b.sector)
634 args.tf_flags |= IDE_TFLAG_OUT_LBAL;
635 if (req_task->out_flags.b.lcyl)
636 args.tf_flags |= IDE_TFLAG_OUT_LBAM;
637 if (req_task->out_flags.b.hcyl)
638 args.tf_flags |= IDE_TFLAG_OUT_LBAH;
639 }
640
1da177e4
LT
641 drive->io_32bit = 0;
642 switch(req_task->data_phase) {
643 case TASKFILE_OUT_DMAQ:
644 case TASKFILE_OUT_DMA:
645 err = ide_diag_taskfile(drive, &args, taskout, outbuf);
646 break;
647 case TASKFILE_IN_DMAQ:
648 case TASKFILE_IN_DMA:
649 err = ide_diag_taskfile(drive, &args, taskin, inbuf);
650 break;
651 case TASKFILE_MULTI_OUT:
652 if (!drive->mult_count) {
653 /* (hs): give up if multcount is not set */
654 printk(KERN_ERR "%s: %s Multimode Write " \
655 "multcount is not set\n",
656 drive->name, __FUNCTION__);
657 err = -EPERM;
658 goto abort;
659 }
660 /* fall through */
661 case TASKFILE_OUT:
662 args.prehandler = &pre_task_out_intr;
663 args.handler = &task_out_intr;
664 err = ide_diag_taskfile(drive, &args, taskout, outbuf);
665 break;
666 case TASKFILE_MULTI_IN:
667 if (!drive->mult_count) {
668 /* (hs): give up if multcount is not set */
669 printk(KERN_ERR "%s: %s Multimode Read failure " \
670 "multcount is not set\n",
671 drive->name, __FUNCTION__);
672 err = -EPERM;
673 goto abort;
674 }
675 /* fall through */
676 case TASKFILE_IN:
677 args.handler = &task_in_intr;
678 err = ide_diag_taskfile(drive, &args, taskin, inbuf);
679 break;
680 case TASKFILE_NO_DATA:
681 args.handler = &task_no_data_intr;
682 err = ide_diag_taskfile(drive, &args, 0, NULL);
683 break;
684 default:
685 err = -EFAULT;
686 goto abort;
687 }
688
650d841d
BZ
689 memcpy(req_task->hob_ports, &args.tf_array[0], HDIO_DRIVE_HOB_HDR_SIZE - 2);
690 memcpy(req_task->io_ports, &args.tf_array[6], HDIO_DRIVE_TASK_HDR_SIZE);
1da177e4 691 req_task->in_flags = args.tf_in_flags;
1da177e4
LT
692
693 if (copy_to_user(buf, req_task, tasksize)) {
694 err = -EFAULT;
695 goto abort;
696 }
697 if (taskout) {
698 int outtotal = tasksize;
699 if (copy_to_user(buf + outtotal, outbuf, taskout)) {
700 err = -EFAULT;
701 goto abort;
702 }
703 }
704 if (taskin) {
705 int intotal = tasksize + taskout;
706 if (copy_to_user(buf + intotal, inbuf, taskin)) {
707 err = -EFAULT;
708 goto abort;
709 }
710 }
711abort:
712 kfree(req_task);
6044ec88
JJ
713 kfree(outbuf);
714 kfree(inbuf);
1da177e4
LT
715
716// printk("IDE Taskfile ioctl ended. rc = %i\n", err);
717
718 drive->io_32bit = io_32bit;
719
720 return err;
721}
26a5b040 722#endif
1da177e4
LT
723
724int ide_wait_cmd (ide_drive_t *drive, u8 cmd, u8 nsect, u8 feature, u8 sectors, u8 *buf)
725{
726 struct request rq;
727 u8 buffer[4];
728
729 if (!buf)
730 buf = buffer;
731 memset(buf, 0, 4 + SECTOR_WORDS * 4 * sectors);
732 ide_init_drive_cmd(&rq);
733 rq.buffer = buf;
734 *buf++ = cmd;
735 *buf++ = nsect;
736 *buf++ = feature;
737 *buf++ = sectors;
738 return ide_do_drive_cmd(drive, &rq, ide_wait);
739}
740
1da177e4
LT
741int ide_cmd_ioctl (ide_drive_t *drive, unsigned int cmd, unsigned long arg)
742{
743 int err = 0;
744 u8 args[4], *argbuf = args;
745 u8 xfer_rate = 0;
746 int argsize = 4;
747 ide_task_t tfargs;
650d841d 748 struct ide_taskfile *tf = &tfargs.tf;
1da177e4
LT
749
750 if (NULL == (void *) arg) {
751 struct request rq;
752 ide_init_drive_cmd(&rq);
753 return ide_do_drive_cmd(drive, &rq, ide_wait);
754 }
755
756 if (copy_from_user(args, (void __user *)arg, 4))
757 return -EFAULT;
758
759 memset(&tfargs, 0, sizeof(ide_task_t));
650d841d
BZ
760 tf->feature = args[2];
761 tf->nsect = args[3];
762 tf->lbal = args[1];
763 tf->command = args[0];
1da177e4
LT
764
765 if (args[3]) {
766 argsize = 4 + (SECTOR_WORDS * 4 * args[3]);
f5e3c2fa 767 argbuf = kzalloc(argsize, GFP_KERNEL);
1da177e4
LT
768 if (argbuf == NULL)
769 return -ENOMEM;
1da177e4
LT
770 }
771 if (set_transfer(drive, &tfargs)) {
772 xfer_rate = args[1];
773 if (ide_ata66_check(drive, &tfargs))
774 goto abort;
775 }
776
777 err = ide_wait_cmd(drive, args[0], args[1], args[2], args[3], argbuf);
778
779 if (!err && xfer_rate) {
780 /* active-retuning-calls future */
781 ide_set_xfer_rate(drive, xfer_rate);
782 ide_driveid_update(drive);
783 }
784abort:
785 if (copy_to_user((void __user *)arg, argbuf, argsize))
786 err = -EFAULT;
787 if (argsize > 4)
788 kfree(argbuf);
789 return err;
790}
791
792static int ide_wait_cmd_task(ide_drive_t *drive, u8 *buf)
793{
794 struct request rq;
795
796 ide_init_drive_cmd(&rq);
4aff5e23 797 rq.cmd_type = REQ_TYPE_ATA_TASK;
1da177e4
LT
798 rq.buffer = buf;
799 return ide_do_drive_cmd(drive, &rq, ide_wait);
800}
801
1da177e4
LT
802int ide_task_ioctl (ide_drive_t *drive, unsigned int cmd, unsigned long arg)
803{
804 void __user *p = (void __user *)arg;
805 int err = 0;
806 u8 args[7], *argbuf = args;
807 int argsize = 7;
808
809 if (copy_from_user(args, p, 7))
810 return -EFAULT;
811 err = ide_wait_cmd_task(drive, argbuf);
812 if (copy_to_user(p, argbuf, argsize))
813 err = -EFAULT;
814 return err;
815}
816
817/*
818 * NOTICE: This is additions from IBM to provide a discrete interface,
819 * for selective taskregister access operations. Nice JOB Klaus!!!
820 * Glad to be able to work and co-develop this with you and IBM.
821 */
822ide_startstop_t flagged_taskfile (ide_drive_t *drive, ide_task_t *task)
823{
1da177e4
LT
824 if (task->data_phase == TASKFILE_MULTI_IN ||
825 task->data_phase == TASKFILE_MULTI_OUT) {
826 if (!drive->mult_count) {
827 printk(KERN_ERR "%s: multimode not set!\n", drive->name);
828 return ide_stopped;
829 }
830 }
831
832 /*
e07bc709 833 * (ks) Check taskfile in flags.
1da177e4
LT
834 * If set, then execute as it is defined.
835 * If not set, then define default settings.
836 * The default values are:
e07bc709
BZ
837 * read all taskfile registers (except data)
838 * read the hob registers (sector, nsector, lcyl, hcyl)
1da177e4 839 */
1da177e4
LT
840 if (task->tf_in_flags.all == 0) {
841 task->tf_in_flags.all = IDE_TASKFILE_STD_IN_FLAGS;
842 if (drive->addressing == 1)
843 task->tf_in_flags.all |= (IDE_HOB_STD_IN_FLAGS << 8);
844 }
845
74095a91 846 return do_rw_taskfile(drive, task);
1da177e4 847}