cmd64x: remove /proc/ide/cmd64x
[deliverable/linux.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
9e42237f
BZ
38void ide_tf_load(ide_drive_t *drive, ide_task_t *task)
39{
40 ide_hwif_t *hwif = drive->hwif;
41 struct ide_taskfile *tf = &task->tf;
42 u8 HIHI = (task->tf_flags & IDE_TFLAG_LBA48) ? 0xE0 : 0xEF;
43
74095a91
BZ
44 if (task->tf_flags & IDE_TFLAG_FLAGGED)
45 HIHI = 0xFF;
46
807e35d6
BZ
47#ifdef DEBUG
48 printk("%s: tf: feat 0x%02x nsect 0x%02x lbal 0x%02x "
49 "lbam 0x%02x lbah 0x%02x dev 0x%02x cmd 0x%02x\n",
50 drive->name, tf->feature, tf->nsect, tf->lbal,
51 tf->lbam, tf->lbah, tf->device, tf->command);
6dd9b837
BZ
52 printk("%s: hob: nsect 0x%02x lbal 0x%02x "
53 "lbam 0x%02x lbah 0x%02x\n",
54 drive->name, tf->hob_nsect, tf->hob_lbal,
55 tf->hob_lbam, tf->hob_lbah);
807e35d6
BZ
56#endif
57
81ca6919 58 ide_set_irq(drive, 1);
9e42237f
BZ
59
60 if ((task->tf_flags & IDE_TFLAG_NO_SELECT_MASK) == 0)
61 SELECT_MASK(drive, 0);
62
74095a91
BZ
63 if (task->tf_flags & IDE_TFLAG_OUT_DATA)
64 hwif->OUTW((tf->hob_data << 8) | tf->data, IDE_DATA_REG);
65
66 if (task->tf_flags & IDE_TFLAG_OUT_HOB_FEATURE)
9e42237f 67 hwif->OUTB(tf->hob_feature, IDE_FEATURE_REG);
74095a91 68 if (task->tf_flags & IDE_TFLAG_OUT_HOB_NSECT)
9e42237f 69 hwif->OUTB(tf->hob_nsect, IDE_NSECTOR_REG);
74095a91 70 if (task->tf_flags & IDE_TFLAG_OUT_HOB_LBAL)
9e42237f 71 hwif->OUTB(tf->hob_lbal, IDE_SECTOR_REG);
74095a91 72 if (task->tf_flags & IDE_TFLAG_OUT_HOB_LBAM)
9e42237f 73 hwif->OUTB(tf->hob_lbam, IDE_LCYL_REG);
74095a91 74 if (task->tf_flags & IDE_TFLAG_OUT_HOB_LBAH)
9e42237f 75 hwif->OUTB(tf->hob_lbah, IDE_HCYL_REG);
9e42237f 76
74095a91
BZ
77 if (task->tf_flags & IDE_TFLAG_OUT_FEATURE)
78 hwif->OUTB(tf->feature, IDE_FEATURE_REG);
79 if (task->tf_flags & IDE_TFLAG_OUT_NSECT)
80 hwif->OUTB(tf->nsect, IDE_NSECTOR_REG);
81 if (task->tf_flags & IDE_TFLAG_OUT_LBAL)
82 hwif->OUTB(tf->lbal, IDE_SECTOR_REG);
83 if (task->tf_flags & IDE_TFLAG_OUT_LBAM)
84 hwif->OUTB(tf->lbam, IDE_LCYL_REG);
85 if (task->tf_flags & IDE_TFLAG_OUT_LBAH)
86 hwif->OUTB(tf->lbah, IDE_HCYL_REG);
9e42237f 87
807e35d6
BZ
88 if (task->tf_flags & IDE_TFLAG_OUT_DEVICE)
89 hwif->OUTB((tf->device & HIHI) | drive->select.all, IDE_SELECT_REG);
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;
ac026ff2
BZ
102 args.tf_flags = IDE_TFLAG_OUT_TF | IDE_TFLAG_OUT_DEVICE;
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;
1da177e4 138
1edee60e
BZ
139 if (task->data_phase == TASKFILE_MULTI_IN ||
140 task->data_phase == TASKFILE_MULTI_OUT) {
141 if (!drive->mult_count) {
142 printk(KERN_ERR "%s: multimode not set!\n",
143 drive->name);
144 return ide_stopped;
145 }
146 }
147
148 if (task->tf_flags & IDE_TFLAG_FLAGGED)
149 task->tf_flags |= IDE_TFLAG_FLAGGED_SET_IN_FLAGS;
150
f6e29e35
BZ
151 if ((task->tf_flags & IDE_TFLAG_DMA_PIO_FALLBACK) == 0)
152 ide_tf_load(drive, task);
1da177e4 153
10d90157
BZ
154 switch (task->data_phase) {
155 case TASKFILE_MULTI_OUT:
156 case TASKFILE_OUT:
157 hwif->OUTBSYNC(drive, tf->command, IDE_COMMAND_REG);
158 ndelay(400); /* FIXME */
159 return pre_task_out_intr(drive, task->rq);
160 case TASKFILE_MULTI_IN:
161 case TASKFILE_IN:
57d7366b 162 handler = task_in_intr;
1192e528 163 /* fall-through */
10d90157 164 case TASKFILE_NO_DATA:
57d7366b
BZ
165 if (handler == NULL)
166 handler = task_no_data_intr;
1192e528 167 /* WIN_{SPECIFY,RESTORE,SETMULT} use custom handlers */
57d7366b
BZ
168 if (task->tf_flags & IDE_TFLAG_CUSTOM_HANDLER) {
169 switch (tf->command) {
170 case WIN_SPECIFY: handler = set_geometry_intr; break;
171 case WIN_RESTORE: handler = recal_intr; break;
172 case WIN_SETMULT: handler = set_multmode_intr; break;
173 }
174 }
175 ide_execute_command(drive, tf->command, handler,
176 WAIT_WORSTCASE, NULL);
1da177e4 177 return ide_started;
10d90157
BZ
178 default:
179 if (task_dma_ok(task) == 0 || drive->using_dma == 0 ||
180 hwif->dma_setup(drive))
181 return ide_stopped;
74095a91
BZ
182 hwif->dma_exec_cmd(drive, tf->command);
183 hwif->dma_start(drive);
184 return ide_started;
1da177e4 185 }
1da177e4 186}
f6e29e35 187EXPORT_SYMBOL_GPL(do_rw_taskfile);
1da177e4 188
1da177e4
LT
189/*
190 * set_multmode_intr() is invoked on completion of a WIN_SETMULT cmd.
191 */
57d7366b 192static ide_startstop_t set_multmode_intr(ide_drive_t *drive)
1da177e4
LT
193{
194 ide_hwif_t *hwif = HWIF(drive);
195 u8 stat;
196
197 if (OK_STAT(stat = hwif->INB(IDE_STATUS_REG),READY_STAT,BAD_STAT)) {
198 drive->mult_count = drive->mult_req;
199 } else {
200 drive->mult_req = drive->mult_count = 0;
201 drive->special.b.recalibrate = 1;
202 (void) ide_dump_status(drive, "set_multmode", stat);
203 }
204 return ide_stopped;
205}
206
207/*
208 * set_geometry_intr() is invoked on completion of a WIN_SPECIFY cmd.
209 */
57d7366b 210static ide_startstop_t set_geometry_intr(ide_drive_t *drive)
1da177e4
LT
211{
212 ide_hwif_t *hwif = HWIF(drive);
213 int retries = 5;
214 u8 stat;
215
216 while (((stat = hwif->INB(IDE_STATUS_REG)) & BUSY_STAT) && retries--)
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
LT
234{
235 ide_hwif_t *hwif = HWIF(drive);
236 u8 stat;
237
238 if (!OK_STAT(stat = hwif->INB(IDE_STATUS_REG), READY_STAT, BAD_STAT))
239 return ide_error(drive, "recal_intr", stat);
240 return ide_stopped;
241}
242
243/*
244 * Handler for commands without a data phase
245 */
1192e528 246static ide_startstop_t task_no_data_intr(ide_drive_t *drive)
1da177e4
LT
247{
248 ide_task_t *args = HWGROUP(drive)->rq->special;
249 ide_hwif_t *hwif = HWIF(drive);
250 u8 stat;
251
366c7f55 252 local_irq_enable_in_hardirq();
1da177e4
LT
253 if (!OK_STAT(stat = hwif->INB(IDE_STATUS_REG),READY_STAT,BAD_STAT)) {
254 return ide_error(drive, "task_no_data_intr", stat);
255 /* calls ide_end_drive_cmd */
256 }
257 if (args)
258 ide_end_drive_cmd(drive, stat, hwif->INB(IDE_ERROR_REG));
259
260 return ide_stopped;
261}
262
1da177e4
LT
263static u8 wait_drive_not_busy(ide_drive_t *drive)
264{
265 ide_hwif_t *hwif = HWIF(drive);
b42fa133 266 int retries;
1da177e4
LT
267 u8 stat;
268
269 /*
270 * Last sector was transfered, wait until drive is ready.
271 * This can take up to 10 usec, but we will wait max 1 ms
272 * (drive_cmd_intr() waits that long).
273 */
b42fa133
MY
274 for (retries = 0; retries < 100; retries++) {
275 if ((stat = hwif->INB(IDE_STATUS_REG)) & BUSY_STAT)
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{
349 if (rq->bio) /* fs request */
350 rq->errors = 0;
351
651c29a1
AM
352 touch_softlockup_watchdog();
353
1da177e4
LT
354 switch (drive->hwif->data_phase) {
355 case TASKFILE_MULTI_IN:
356 case TASKFILE_MULTI_OUT:
357 ide_pio_multi(drive, write);
358 break;
359 default:
360 ide_pio_sector(drive, write);
361 break;
362 }
363}
364
365static ide_startstop_t task_error(ide_drive_t *drive, struct request *rq,
366 const char *s, u8 stat)
367{
368 if (rq->bio) {
369 ide_hwif_t *hwif = drive->hwif;
370 int sectors = hwif->nsect - hwif->nleft;
371
372 switch (hwif->data_phase) {
373 case TASKFILE_IN:
374 if (hwif->nleft)
375 break;
376 /* fall through */
377 case TASKFILE_OUT:
378 sectors--;
379 break;
380 case TASKFILE_MULTI_IN:
381 if (hwif->nleft)
382 break;
383 /* fall through */
384 case TASKFILE_MULTI_OUT:
385 sectors -= drive->mult_count;
386 default:
387 break;
388 }
389
390 if (sectors > 0) {
391 ide_driver_t *drv;
392
393 drv = *(ide_driver_t **)rq->rq_disk->private_data;
394 drv->end_request(drive, 1, sectors);
395 }
396 }
397 return ide_error(drive, s, stat);
398}
399
400static void task_end_request(ide_drive_t *drive, struct request *rq, u8 stat)
401{
55c16a70
JA
402 HWIF(drive)->cursg = NULL;
403
4aff5e23 404 if (rq->cmd_type == REQ_TYPE_ATA_TASKFILE) {
1da177e4
LT
405 ide_task_t *task = rq->special;
406
74095a91 407 if (task->tf_flags & IDE_TFLAG_FLAGGED) {
1da177e4
LT
408 u8 err = drive->hwif->INB(IDE_ERROR_REG);
409 ide_end_drive_cmd(drive, stat, err);
410 return;
411 }
412 }
413
03731fbd
RP
414 if (rq->rq_disk) {
415 ide_driver_t *drv;
416
417 drv = *(ide_driver_t **)rq->rq_disk->private_data;;
418 drv->end_request(drive, 1, rq->hard_nr_sectors);
419 } else
420 ide_end_request(drive, 1, rq->hard_nr_sectors);
1da177e4
LT
421}
422
423/*
424 * Handler for command with PIO data-in phase (Read/Read Multiple).
425 */
f6e29e35 426static ide_startstop_t task_in_intr(ide_drive_t *drive)
1da177e4
LT
427{
428 ide_hwif_t *hwif = drive->hwif;
429 struct request *rq = HWGROUP(drive)->rq;
430 u8 stat = hwif->INB(IDE_STATUS_REG);
431
432 /* new way for dealing with premature shared PCI interrupts */
433 if (!OK_STAT(stat, DATA_READY, BAD_R_STAT)) {
434 if (stat & (ERR_STAT | DRQ_STAT))
435 return task_error(drive, rq, __FUNCTION__, stat);
436 /* No data yet, so wait for another IRQ. */
437 ide_set_handler(drive, &task_in_intr, WAIT_WORSTCASE, NULL);
438 return ide_started;
439 }
440
441 ide_pio_datablock(drive, rq, 0);
442
443 /* If it was the last datablock check status and finish transfer. */
444 if (!hwif->nleft) {
445 stat = wait_drive_not_busy(drive);
446 if (!OK_STAT(stat, 0, BAD_R_STAT))
447 return task_error(drive, rq, __FUNCTION__, stat);
448 task_end_request(drive, rq, stat);
449 return ide_stopped;
450 }
451
452 /* Still data left to transfer. */
453 ide_set_handler(drive, &task_in_intr, WAIT_WORSTCASE, NULL);
454
455 return ide_started;
456}
1da177e4
LT
457
458/*
459 * Handler for command with PIO data-out phase (Write/Write Multiple).
460 */
461static ide_startstop_t task_out_intr (ide_drive_t *drive)
462{
463 ide_hwif_t *hwif = drive->hwif;
464 struct request *rq = HWGROUP(drive)->rq;
465 u8 stat = hwif->INB(IDE_STATUS_REG);
466
467 if (!OK_STAT(stat, DRIVE_READY, drive->bad_wstat))
468 return task_error(drive, rq, __FUNCTION__, stat);
469
470 /* Deal with unexpected ATA data phase. */
471 if (((stat & DRQ_STAT) == 0) ^ !hwif->nleft)
472 return task_error(drive, rq, __FUNCTION__, stat);
473
474 if (!hwif->nleft) {
475 task_end_request(drive, rq, stat);
476 return ide_stopped;
477 }
478
479 /* Still data left to transfer. */
480 ide_pio_datablock(drive, rq, 1);
481 ide_set_handler(drive, &task_out_intr, WAIT_WORSTCASE, NULL);
482
483 return ide_started;
484}
485
f6e29e35 486static ide_startstop_t pre_task_out_intr(ide_drive_t *drive, struct request *rq)
1da177e4
LT
487{
488 ide_startstop_t startstop;
489
490 if (ide_wait_stat(&startstop, drive, DATA_READY,
491 drive->bad_wstat, WAIT_DRQ)) {
492 printk(KERN_ERR "%s: no DRQ after issuing %sWRITE%s\n",
493 drive->name,
494 drive->hwif->data_phase ? "MULT" : "",
495 drive->addressing ? "_EXT" : "");
496 return startstop;
497 }
498
499 if (!drive->unmask)
500 local_irq_disable();
501
502 ide_set_handler(drive, &task_out_intr, WAIT_WORSTCASE, NULL);
503 ide_pio_datablock(drive, rq, 1);
504
505 return ide_started;
506}
1da177e4 507
ac026ff2 508int ide_raw_taskfile(ide_drive_t *drive, ide_task_t *task, u8 *buf, u16 nsect)
1da177e4
LT
509{
510 struct request rq;
511
512 memset(&rq, 0, sizeof(rq));
02ac2460 513 rq.ref_count = 1;
4aff5e23 514 rq.cmd_type = REQ_TYPE_ATA_TASKFILE;
1da177e4
LT
515 rq.buffer = buf;
516
517 /*
518 * (ks) We transfer currently only whole sectors.
519 * This is suffient for now. But, it would be great,
520 * if we would find a solution to transfer any size.
521 * To support special commands like READ LONG.
522 */
ac026ff2
BZ
523 rq.hard_nr_sectors = rq.nr_sectors = nsect;
524 rq.hard_cur_sectors = rq.current_nr_sectors = nsect;
1da177e4 525
ac026ff2
BZ
526 if (task->tf_flags & IDE_TFLAG_WRITE)
527 rq.cmd_flags |= REQ_RW;
1da177e4 528
ac026ff2
BZ
529 rq.special = task;
530 task->rq = &rq;
1da177e4 531
1da177e4
LT
532 return ide_do_drive_cmd(drive, &rq, ide_wait);
533}
534
1da177e4
LT
535EXPORT_SYMBOL(ide_raw_taskfile);
536
9a3c49be
BZ
537int ide_no_data_taskfile(ide_drive_t *drive, ide_task_t *task)
538{
ac026ff2 539 task->data_phase = TASKFILE_NO_DATA;
9a3c49be 540
ac026ff2 541 return ide_raw_taskfile(drive, task, NULL, 0);
9a3c49be
BZ
542}
543EXPORT_SYMBOL_GPL(ide_no_data_taskfile);
544
26a5b040 545#ifdef CONFIG_IDE_TASK_IOCTL
1da177e4
LT
546int ide_taskfile_ioctl (ide_drive_t *drive, unsigned int cmd, unsigned long arg)
547{
548 ide_task_request_t *req_task;
549 ide_task_t args;
550 u8 *outbuf = NULL;
551 u8 *inbuf = NULL;
ac026ff2 552 u8 *data_buf = NULL;
1da177e4
LT
553 int err = 0;
554 int tasksize = sizeof(struct ide_task_request_s);
3a42bb22
AC
555 unsigned int taskin = 0;
556 unsigned int taskout = 0;
ac026ff2 557 u16 nsect = 0;
1da177e4
LT
558 u8 io_32bit = drive->io_32bit;
559 char __user *buf = (char __user *)arg;
560
561// printk("IDE Taskfile ...\n");
562
f5e3c2fa 563 req_task = kzalloc(tasksize, GFP_KERNEL);
1da177e4 564 if (req_task == NULL) return -ENOMEM;
1da177e4
LT
565 if (copy_from_user(req_task, buf, tasksize)) {
566 kfree(req_task);
567 return -EFAULT;
568 }
569
3a42bb22
AC
570 taskout = req_task->out_size;
571 taskin = req_task->in_size;
572
573 if (taskin > 65536 || taskout > 65536) {
574 err = -EINVAL;
575 goto abort;
576 }
1da177e4
LT
577
578 if (taskout) {
579 int outtotal = tasksize;
f5e3c2fa 580 outbuf = kzalloc(taskout, GFP_KERNEL);
1da177e4
LT
581 if (outbuf == NULL) {
582 err = -ENOMEM;
583 goto abort;
584 }
1da177e4
LT
585 if (copy_from_user(outbuf, buf + outtotal, taskout)) {
586 err = -EFAULT;
587 goto abort;
588 }
589 }
590
591 if (taskin) {
592 int intotal = tasksize + taskout;
f5e3c2fa 593 inbuf = kzalloc(taskin, GFP_KERNEL);
1da177e4
LT
594 if (inbuf == NULL) {
595 err = -ENOMEM;
596 goto abort;
597 }
1da177e4
LT
598 if (copy_from_user(inbuf, buf + intotal, taskin)) {
599 err = -EFAULT;
600 goto abort;
601 }
602 }
603
604 memset(&args, 0, sizeof(ide_task_t));
1da177e4 605
650d841d
BZ
606 memcpy(&args.tf_array[0], req_task->hob_ports, HDIO_DRIVE_HOB_HDR_SIZE - 2);
607 memcpy(&args.tf_array[6], req_task->io_ports, HDIO_DRIVE_TASK_HDR_SIZE);
866e2ec9
BZ
608
609 args.data_phase = req_task->data_phase;
1da177e4 610
a3bbb9d8
BZ
611 args.tf_flags = IDE_TFLAG_OUT_DEVICE;
612 if (drive->addressing == 1)
613 args.tf_flags |= IDE_TFLAG_LBA48;
614
74095a91
BZ
615 if (req_task->out_flags.all) {
616 args.tf_flags |= IDE_TFLAG_FLAGGED;
617
618 if (req_task->out_flags.b.data)
619 args.tf_flags |= IDE_TFLAG_OUT_DATA;
620
621 if (req_task->out_flags.b.nsector_hob)
622 args.tf_flags |= IDE_TFLAG_OUT_HOB_NSECT;
623 if (req_task->out_flags.b.sector_hob)
624 args.tf_flags |= IDE_TFLAG_OUT_HOB_LBAL;
625 if (req_task->out_flags.b.lcyl_hob)
626 args.tf_flags |= IDE_TFLAG_OUT_HOB_LBAM;
627 if (req_task->out_flags.b.hcyl_hob)
628 args.tf_flags |= IDE_TFLAG_OUT_HOB_LBAH;
629
630 if (req_task->out_flags.b.error_feature)
631 args.tf_flags |= IDE_TFLAG_OUT_FEATURE;
632 if (req_task->out_flags.b.nsector)
633 args.tf_flags |= IDE_TFLAG_OUT_NSECT;
634 if (req_task->out_flags.b.sector)
635 args.tf_flags |= IDE_TFLAG_OUT_LBAL;
636 if (req_task->out_flags.b.lcyl)
637 args.tf_flags |= IDE_TFLAG_OUT_LBAM;
638 if (req_task->out_flags.b.hcyl)
639 args.tf_flags |= IDE_TFLAG_OUT_LBAH;
a3bbb9d8
BZ
640 } else {
641 args.tf_flags |= IDE_TFLAG_OUT_TF;
642 if (args.tf_flags & IDE_TFLAG_LBA48)
643 args.tf_flags |= IDE_TFLAG_OUT_HOB;
74095a91
BZ
644 }
645
866e2ec9
BZ
646 if (req_task->in_flags.b.data)
647 args.tf_flags |= IDE_TFLAG_IN_DATA;
648
1da177e4
LT
649 drive->io_32bit = 0;
650 switch(req_task->data_phase) {
1da177e4
LT
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:
ac026ff2
BZ
662 /* fall through */
663 case TASKFILE_OUT_DMAQ:
664 case TASKFILE_OUT_DMA:
665 nsect = taskout / SECTOR_SIZE;
666 data_buf = outbuf;
1da177e4
LT
667 break;
668 case TASKFILE_MULTI_IN:
669 if (!drive->mult_count) {
670 /* (hs): give up if multcount is not set */
671 printk(KERN_ERR "%s: %s Multimode Read failure " \
672 "multcount is not set\n",
673 drive->name, __FUNCTION__);
674 err = -EPERM;
675 goto abort;
676 }
677 /* fall through */
678 case TASKFILE_IN:
ac026ff2
BZ
679 /* fall through */
680 case TASKFILE_IN_DMAQ:
681 case TASKFILE_IN_DMA:
682 nsect = taskin / SECTOR_SIZE;
683 data_buf = inbuf;
1da177e4
LT
684 break;
685 case TASKFILE_NO_DATA:
1da177e4
LT
686 break;
687 default:
688 err = -EFAULT;
689 goto abort;
690 }
691
ac026ff2
BZ
692 if (req_task->req_cmd == IDE_DRIVE_TASK_NO_DATA)
693 nsect = 0;
694 else if (!nsect) {
695 nsect = (args.tf.hob_nsect << 8) | args.tf.nsect;
696
697 if (!nsect) {
698 printk(KERN_ERR "%s: in/out command without data\n",
699 drive->name);
700 err = -EFAULT;
701 goto abort;
702 }
703 }
704
705 if (req_task->req_cmd == IDE_DRIVE_TASK_RAW_WRITE)
706 args.tf_flags |= IDE_TFLAG_WRITE;
707
708 err = ide_raw_taskfile(drive, &args, data_buf, nsect);
709
650d841d
BZ
710 memcpy(req_task->hob_ports, &args.tf_array[0], HDIO_DRIVE_HOB_HDR_SIZE - 2);
711 memcpy(req_task->io_ports, &args.tf_array[6], HDIO_DRIVE_TASK_HDR_SIZE);
866e2ec9
BZ
712
713 if ((args.tf_flags & IDE_TFLAG_FLAGGED_SET_IN_FLAGS) &&
714 req_task->in_flags.all == 0) {
715 req_task->in_flags.all = IDE_TASKFILE_STD_IN_FLAGS;
716 if (drive->addressing == 1)
717 req_task->in_flags.all |= (IDE_HOB_STD_IN_FLAGS << 8);
718 }
1da177e4
LT
719
720 if (copy_to_user(buf, req_task, tasksize)) {
721 err = -EFAULT;
722 goto abort;
723 }
724 if (taskout) {
725 int outtotal = tasksize;
726 if (copy_to_user(buf + outtotal, outbuf, taskout)) {
727 err = -EFAULT;
728 goto abort;
729 }
730 }
731 if (taskin) {
732 int intotal = tasksize + taskout;
733 if (copy_to_user(buf + intotal, inbuf, taskin)) {
734 err = -EFAULT;
735 goto abort;
736 }
737 }
738abort:
739 kfree(req_task);
6044ec88
JJ
740 kfree(outbuf);
741 kfree(inbuf);
1da177e4
LT
742
743// printk("IDE Taskfile ioctl ended. rc = %i\n", err);
744
745 drive->io_32bit = io_32bit;
746
747 return err;
748}
26a5b040 749#endif
1da177e4
LT
750
751int ide_wait_cmd (ide_drive_t *drive, u8 cmd, u8 nsect, u8 feature, u8 sectors, u8 *buf)
752{
753 struct request rq;
754 u8 buffer[4];
755
756 if (!buf)
757 buf = buffer;
758 memset(buf, 0, 4 + SECTOR_WORDS * 4 * sectors);
759 ide_init_drive_cmd(&rq);
760 rq.buffer = buf;
761 *buf++ = cmd;
762 *buf++ = nsect;
763 *buf++ = feature;
764 *buf++ = sectors;
765 return ide_do_drive_cmd(drive, &rq, ide_wait);
766}
767
1da177e4
LT
768int ide_cmd_ioctl (ide_drive_t *drive, unsigned int cmd, unsigned long arg)
769{
770 int err = 0;
771 u8 args[4], *argbuf = args;
772 u8 xfer_rate = 0;
773 int argsize = 4;
774 ide_task_t tfargs;
650d841d 775 struct ide_taskfile *tf = &tfargs.tf;
1da177e4
LT
776
777 if (NULL == (void *) arg) {
778 struct request rq;
779 ide_init_drive_cmd(&rq);
780 return ide_do_drive_cmd(drive, &rq, ide_wait);
781 }
782
783 if (copy_from_user(args, (void __user *)arg, 4))
784 return -EFAULT;
785
786 memset(&tfargs, 0, sizeof(ide_task_t));
650d841d
BZ
787 tf->feature = args[2];
788 tf->nsect = args[3];
789 tf->lbal = args[1];
790 tf->command = args[0];
1da177e4
LT
791
792 if (args[3]) {
793 argsize = 4 + (SECTOR_WORDS * 4 * args[3]);
f5e3c2fa 794 argbuf = kzalloc(argsize, GFP_KERNEL);
1da177e4
LT
795 if (argbuf == NULL)
796 return -ENOMEM;
1da177e4
LT
797 }
798 if (set_transfer(drive, &tfargs)) {
799 xfer_rate = args[1];
800 if (ide_ata66_check(drive, &tfargs))
801 goto abort;
802 }
803
804 err = ide_wait_cmd(drive, args[0], args[1], args[2], args[3], argbuf);
805
806 if (!err && xfer_rate) {
807 /* active-retuning-calls future */
808 ide_set_xfer_rate(drive, xfer_rate);
809 ide_driveid_update(drive);
810 }
811abort:
812 if (copy_to_user((void __user *)arg, argbuf, argsize))
813 err = -EFAULT;
814 if (argsize > 4)
815 kfree(argbuf);
816 return err;
817}
818
1da177e4
LT
819int ide_task_ioctl (ide_drive_t *drive, unsigned int cmd, unsigned long arg)
820{
821 void __user *p = (void __user *)arg;
822 int err = 0;
14b89ef9
BZ
823 u8 args[7];
824 ide_task_t task;
1da177e4
LT
825
826 if (copy_from_user(args, p, 7))
827 return -EFAULT;
14b89ef9
BZ
828
829 memset(&task, 0, sizeof(task));
830 memcpy(&task.tf_array[7], &args[1], 6);
831 task.tf.command = args[0];
832 task.tf_flags = IDE_TFLAG_OUT_TF | IDE_TFLAG_OUT_DEVICE;
833
834 err = ide_no_data_taskfile(drive, &task);
835
836 args[0] = task.tf.command;
837 memcpy(&args[1], &task.tf_array[7], 6);
838
839 if (copy_to_user(p, args, 7))
1da177e4 840 err = -EFAULT;
14b89ef9 841
1da177e4
LT
842 return err;
843}
This page took 0.573746 seconds and 5 git commands to generate.