Staging: epl: clean up demo_main.c
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / ide / ide-taskfile.c
blob243421ce40d02ccee63b211f202017782e4d8262
1 /*
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
5 * IBM Storage Technology Division
6 * Copyright (C) 2003-2004, 2007 Bartlomiej Zolnierkiewicz
8 * The big the bad and the ugly.
9 */
11 #include <linux/types.h>
12 #include <linux/string.h>
13 #include <linux/kernel.h>
14 #include <linux/sched.h>
15 #include <linux/interrupt.h>
16 #include <linux/errno.h>
17 #include <linux/slab.h>
18 #include <linux/delay.h>
19 #include <linux/hdreg.h>
20 #include <linux/ide.h>
21 #include <linux/scatterlist.h>
23 #include <asm/uaccess.h>
24 #include <asm/io.h>
26 void ide_tf_dump(const char *s, struct ide_taskfile *tf)
28 #ifdef DEBUG
29 printk("%s: tf: feat 0x%02x nsect 0x%02x lbal 0x%02x "
30 "lbam 0x%02x lbah 0x%02x dev 0x%02x cmd 0x%02x\n",
31 s, tf->feature, tf->nsect, tf->lbal,
32 tf->lbam, tf->lbah, tf->device, tf->command);
33 printk("%s: hob: nsect 0x%02x lbal 0x%02x "
34 "lbam 0x%02x lbah 0x%02x\n",
35 s, tf->hob_nsect, tf->hob_lbal,
36 tf->hob_lbam, tf->hob_lbah);
37 #endif
40 int taskfile_lib_get_identify (ide_drive_t *drive, u8 *buf)
42 struct ide_cmd cmd;
44 memset(&cmd, 0, sizeof(cmd));
45 cmd.tf.nsect = 0x01;
46 if (drive->media == ide_disk)
47 cmd.tf.command = ATA_CMD_ID_ATA;
48 else
49 cmd.tf.command = ATA_CMD_ID_ATAPI;
50 cmd.tf_flags = IDE_TFLAG_TF | IDE_TFLAG_DEVICE;
51 cmd.protocol = ATA_PROT_PIO;
53 return ide_raw_taskfile(drive, &cmd, buf, 1);
56 static ide_startstop_t task_no_data_intr(ide_drive_t *);
57 static ide_startstop_t pre_task_out_intr(ide_drive_t *, struct ide_cmd *);
58 static ide_startstop_t task_pio_intr(ide_drive_t *);
60 ide_startstop_t do_rw_taskfile(ide_drive_t *drive, struct ide_cmd *orig_cmd)
62 ide_hwif_t *hwif = drive->hwif;
63 struct ide_cmd *cmd = &hwif->cmd;
64 struct ide_taskfile *tf = &cmd->tf;
65 ide_handler_t *handler = NULL;
66 const struct ide_tp_ops *tp_ops = hwif->tp_ops;
67 const struct ide_dma_ops *dma_ops = hwif->dma_ops;
69 if (orig_cmd->protocol == ATA_PROT_PIO &&
70 (orig_cmd->tf_flags & IDE_TFLAG_MULTI_PIO) &&
71 drive->mult_count == 0) {
72 printk(KERN_ERR "%s: multimode not set!\n", drive->name);
73 return ide_stopped;
76 if (orig_cmd->ftf_flags & IDE_FTFLAG_FLAGGED)
77 orig_cmd->ftf_flags |= IDE_FTFLAG_SET_IN_FLAGS;
79 memcpy(cmd, orig_cmd, sizeof(*cmd));
81 if ((cmd->tf_flags & IDE_TFLAG_DMA_PIO_FALLBACK) == 0) {
82 ide_tf_dump(drive->name, tf);
83 tp_ops->write_devctl(hwif, ATA_DEVCTL_OBS);
84 SELECT_MASK(drive, 0);
86 if (cmd->ftf_flags & IDE_FTFLAG_OUT_DATA) {
87 u8 data[2] = { tf->data, tf->hob_data };
89 tp_ops->output_data(drive, cmd, data, 2);
91 tp_ops->tf_load(drive, cmd);
94 switch (cmd->protocol) {
95 case ATA_PROT_PIO:
96 if (cmd->tf_flags & IDE_TFLAG_WRITE) {
97 tp_ops->exec_command(hwif, tf->command);
98 ndelay(400); /* FIXME */
99 return pre_task_out_intr(drive, cmd);
101 handler = task_pio_intr;
102 /* fall-through */
103 case ATA_PROT_NODATA:
104 if (handler == NULL)
105 handler = task_no_data_intr;
106 ide_execute_command(drive, cmd, handler, WAIT_WORSTCASE);
107 return ide_started;
108 case ATA_PROT_DMA:
109 if (ide_dma_prepare(drive, cmd))
110 return ide_stopped;
111 hwif->expiry = dma_ops->dma_timer_expiry;
112 ide_execute_command(drive, cmd, ide_dma_intr, 2 * WAIT_CMD);
113 dma_ops->dma_start(drive);
114 default:
115 return ide_started;
118 EXPORT_SYMBOL_GPL(do_rw_taskfile);
120 static ide_startstop_t task_no_data_intr(ide_drive_t *drive)
122 ide_hwif_t *hwif = drive->hwif;
123 struct ide_cmd *cmd = &hwif->cmd;
124 struct ide_taskfile *tf = &cmd->tf;
125 int custom = (cmd->tf_flags & IDE_TFLAG_CUSTOM_HANDLER) ? 1 : 0;
126 int retries = (custom && tf->command == ATA_CMD_INIT_DEV_PARAMS) ? 5 : 1;
127 u8 stat;
129 local_irq_enable_in_hardirq();
131 while (1) {
132 stat = hwif->tp_ops->read_status(hwif);
133 if ((stat & ATA_BUSY) == 0 || retries-- == 0)
134 break;
135 udelay(10);
138 if (!OK_STAT(stat, ATA_DRDY, BAD_STAT)) {
139 if (custom && tf->command == ATA_CMD_SET_MULTI) {
140 drive->mult_req = drive->mult_count = 0;
141 drive->special.b.recalibrate = 1;
142 (void)ide_dump_status(drive, __func__, stat);
143 return ide_stopped;
144 } else if (custom && tf->command == ATA_CMD_INIT_DEV_PARAMS) {
145 if ((stat & (ATA_ERR | ATA_DRQ)) == 0) {
146 ide_set_handler(drive, &task_no_data_intr,
147 WAIT_WORSTCASE);
148 return ide_started;
151 return ide_error(drive, "task_no_data_intr", stat);
154 if (custom && tf->command == ATA_CMD_SET_MULTI)
155 drive->mult_count = drive->mult_req;
157 if (custom == 0 || tf->command == ATA_CMD_IDLEIMMEDIATE ||
158 tf->command == ATA_CMD_CHK_POWER) {
159 struct request *rq = hwif->rq;
161 if (blk_pm_request(rq))
162 ide_complete_pm_rq(drive, rq);
163 else
164 ide_finish_cmd(drive, cmd, stat);
167 return ide_stopped;
170 static u8 wait_drive_not_busy(ide_drive_t *drive)
172 ide_hwif_t *hwif = drive->hwif;
173 int retries;
174 u8 stat;
177 * Last sector was transfered, wait until device is ready. This can
178 * take up to 6 ms on some ATAPI devices, so we will wait max 10 ms.
180 for (retries = 0; retries < 1000; retries++) {
181 stat = hwif->tp_ops->read_status(hwif);
183 if (stat & ATA_BUSY)
184 udelay(10);
185 else
186 break;
189 if (stat & ATA_BUSY)
190 printk(KERN_ERR "%s: drive still BUSY!\n", drive->name);
192 return stat;
195 void ide_pio_bytes(ide_drive_t *drive, struct ide_cmd *cmd,
196 unsigned int write, unsigned int len)
198 ide_hwif_t *hwif = drive->hwif;
199 struct scatterlist *sg = hwif->sg_table;
200 struct scatterlist *cursg = cmd->cursg;
201 struct page *page;
202 unsigned long flags;
203 unsigned int offset;
204 u8 *buf;
206 cursg = cmd->cursg;
207 if (cursg == NULL)
208 cursg = cmd->cursg = sg;
210 while (len) {
211 unsigned nr_bytes = min(len, cursg->length - cmd->cursg_ofs);
213 if (nr_bytes > PAGE_SIZE)
214 nr_bytes = PAGE_SIZE;
216 page = sg_page(cursg);
217 offset = cursg->offset + cmd->cursg_ofs;
219 /* get the current page and offset */
220 page = nth_page(page, (offset >> PAGE_SHIFT));
221 offset %= PAGE_SIZE;
223 if (PageHighMem(page))
224 local_irq_save(flags);
226 buf = kmap_atomic(page, KM_BIO_SRC_IRQ) + offset;
228 cmd->nleft -= nr_bytes;
229 cmd->cursg_ofs += nr_bytes;
231 if (cmd->cursg_ofs == cursg->length) {
232 cursg = cmd->cursg = sg_next(cmd->cursg);
233 cmd->cursg_ofs = 0;
236 /* do the actual data transfer */
237 if (write)
238 hwif->tp_ops->output_data(drive, cmd, buf, nr_bytes);
239 else
240 hwif->tp_ops->input_data(drive, cmd, buf, nr_bytes);
242 kunmap_atomic(buf, KM_BIO_SRC_IRQ);
244 if (PageHighMem(page))
245 local_irq_restore(flags);
247 len -= nr_bytes;
250 EXPORT_SYMBOL_GPL(ide_pio_bytes);
252 static void ide_pio_datablock(ide_drive_t *drive, struct ide_cmd *cmd,
253 unsigned int write)
255 unsigned int nr_bytes;
257 u8 saved_io_32bit = drive->io_32bit;
259 if (cmd->tf_flags & IDE_TFLAG_FS)
260 cmd->rq->errors = 0;
262 if (cmd->tf_flags & IDE_TFLAG_IO_16BIT)
263 drive->io_32bit = 0;
265 touch_softlockup_watchdog();
267 if (cmd->tf_flags & IDE_TFLAG_MULTI_PIO)
268 nr_bytes = min_t(unsigned, cmd->nleft, drive->mult_count << 9);
269 else
270 nr_bytes = SECTOR_SIZE;
272 ide_pio_bytes(drive, cmd, write, nr_bytes);
274 drive->io_32bit = saved_io_32bit;
277 static void ide_error_cmd(ide_drive_t *drive, struct ide_cmd *cmd)
279 if (cmd->tf_flags & IDE_TFLAG_FS) {
280 int nr_bytes = cmd->nbytes - cmd->nleft;
282 if (cmd->protocol == ATA_PROT_PIO &&
283 ((cmd->tf_flags & IDE_TFLAG_WRITE) || cmd->nleft == 0)) {
284 if (cmd->tf_flags & IDE_TFLAG_MULTI_PIO)
285 nr_bytes -= drive->mult_count << 9;
286 else
287 nr_bytes -= SECTOR_SIZE;
290 if (nr_bytes > 0)
291 ide_complete_rq(drive, 0, nr_bytes);
295 void ide_finish_cmd(ide_drive_t *drive, struct ide_cmd *cmd, u8 stat)
297 struct request *rq = drive->hwif->rq;
298 u8 err = ide_read_error(drive);
300 ide_complete_cmd(drive, cmd, stat, err);
301 rq->errors = err;
302 ide_complete_rq(drive, err ? -EIO : 0, blk_rq_bytes(rq));
306 * Handler for command with PIO data phase.
308 static ide_startstop_t task_pio_intr(ide_drive_t *drive)
310 ide_hwif_t *hwif = drive->hwif;
311 struct ide_cmd *cmd = &drive->hwif->cmd;
312 u8 stat = hwif->tp_ops->read_status(hwif);
313 u8 write = !!(cmd->tf_flags & IDE_TFLAG_WRITE);
315 if (write == 0) {
316 /* Error? */
317 if (stat & ATA_ERR)
318 goto out_err;
320 /* Didn't want any data? Odd. */
321 if ((stat & ATA_DRQ) == 0) {
322 /* Command all done? */
323 if (OK_STAT(stat, ATA_DRDY, ATA_BUSY))
324 goto out_end;
326 /* Assume it was a spurious irq */
327 goto out_wait;
329 } else {
330 if (!OK_STAT(stat, DRIVE_READY, drive->bad_wstat))
331 goto out_err;
333 /* Deal with unexpected ATA data phase. */
334 if (((stat & ATA_DRQ) == 0) ^ (cmd->nleft == 0))
335 goto out_err;
338 if (write && cmd->nleft == 0)
339 goto out_end;
341 /* Still data left to transfer. */
342 ide_pio_datablock(drive, cmd, write);
344 /* Are we done? Check status and finish transfer. */
345 if (write == 0 && cmd->nleft == 0) {
346 stat = wait_drive_not_busy(drive);
347 if (!OK_STAT(stat, 0, BAD_STAT))
348 goto out_err;
350 goto out_end;
352 out_wait:
353 /* Still data left to transfer. */
354 ide_set_handler(drive, &task_pio_intr, WAIT_WORSTCASE);
355 return ide_started;
356 out_end:
357 if ((cmd->tf_flags & IDE_TFLAG_FS) == 0)
358 ide_finish_cmd(drive, cmd, stat);
359 else
360 ide_complete_rq(drive, 0, cmd->rq->nr_sectors << 9);
361 return ide_stopped;
362 out_err:
363 ide_error_cmd(drive, cmd);
364 return ide_error(drive, __func__, stat);
367 static ide_startstop_t pre_task_out_intr(ide_drive_t *drive,
368 struct ide_cmd *cmd)
370 ide_startstop_t startstop;
372 if (ide_wait_stat(&startstop, drive, ATA_DRQ,
373 drive->bad_wstat, WAIT_DRQ)) {
374 printk(KERN_ERR "%s: no DRQ after issuing %sWRITE%s\n",
375 drive->name,
376 (cmd->tf_flags & IDE_TFLAG_MULTI_PIO) ? "MULT" : "",
377 (drive->dev_flags & IDE_DFLAG_LBA48) ? "_EXT" : "");
378 return startstop;
381 if ((drive->dev_flags & IDE_DFLAG_UNMASK) == 0)
382 local_irq_disable();
384 ide_set_handler(drive, &task_pio_intr, WAIT_WORSTCASE);
386 ide_pio_datablock(drive, cmd, 1);
388 return ide_started;
391 int ide_raw_taskfile(ide_drive_t *drive, struct ide_cmd *cmd, u8 *buf,
392 u16 nsect)
394 struct request *rq;
395 int error;
397 rq = blk_get_request(drive->queue, READ, __GFP_WAIT);
398 rq->cmd_type = REQ_TYPE_ATA_TASKFILE;
399 rq->buffer = buf;
402 * (ks) We transfer currently only whole sectors.
403 * This is suffient for now. But, it would be great,
404 * if we would find a solution to transfer any size.
405 * To support special commands like READ LONG.
407 rq->hard_nr_sectors = rq->nr_sectors = nsect;
408 rq->hard_cur_sectors = rq->current_nr_sectors = nsect;
410 if (cmd->tf_flags & IDE_TFLAG_WRITE)
411 rq->cmd_flags |= REQ_RW;
413 rq->special = cmd;
414 cmd->rq = rq;
416 error = blk_execute_rq(drive->queue, NULL, rq, 0);
417 blk_put_request(rq);
419 return error;
422 EXPORT_SYMBOL(ide_raw_taskfile);
424 int ide_no_data_taskfile(ide_drive_t *drive, struct ide_cmd *cmd)
426 cmd->protocol = ATA_PROT_NODATA;
428 return ide_raw_taskfile(drive, cmd, NULL, 0);
430 EXPORT_SYMBOL_GPL(ide_no_data_taskfile);
432 #ifdef CONFIG_IDE_TASK_IOCTL
433 int ide_taskfile_ioctl(ide_drive_t *drive, unsigned long arg)
435 ide_task_request_t *req_task;
436 struct ide_cmd cmd;
437 u8 *outbuf = NULL;
438 u8 *inbuf = NULL;
439 u8 *data_buf = NULL;
440 int err = 0;
441 int tasksize = sizeof(struct ide_task_request_s);
442 unsigned int taskin = 0;
443 unsigned int taskout = 0;
444 u16 nsect = 0;
445 char __user *buf = (char __user *)arg;
447 // printk("IDE Taskfile ...\n");
449 req_task = kzalloc(tasksize, GFP_KERNEL);
450 if (req_task == NULL) return -ENOMEM;
451 if (copy_from_user(req_task, buf, tasksize)) {
452 kfree(req_task);
453 return -EFAULT;
456 taskout = req_task->out_size;
457 taskin = req_task->in_size;
459 if (taskin > 65536 || taskout > 65536) {
460 err = -EINVAL;
461 goto abort;
464 if (taskout) {
465 int outtotal = tasksize;
466 outbuf = kzalloc(taskout, GFP_KERNEL);
467 if (outbuf == NULL) {
468 err = -ENOMEM;
469 goto abort;
471 if (copy_from_user(outbuf, buf + outtotal, taskout)) {
472 err = -EFAULT;
473 goto abort;
477 if (taskin) {
478 int intotal = tasksize + taskout;
479 inbuf = kzalloc(taskin, GFP_KERNEL);
480 if (inbuf == NULL) {
481 err = -ENOMEM;
482 goto abort;
484 if (copy_from_user(inbuf, buf + intotal, taskin)) {
485 err = -EFAULT;
486 goto abort;
490 memset(&cmd, 0, sizeof(cmd));
492 memcpy(&cmd.tf_array[0], req_task->hob_ports,
493 HDIO_DRIVE_HOB_HDR_SIZE - 2);
494 memcpy(&cmd.tf_array[6], req_task->io_ports,
495 HDIO_DRIVE_TASK_HDR_SIZE);
497 cmd.tf_flags = IDE_TFLAG_IO_16BIT | IDE_TFLAG_DEVICE |
498 IDE_TFLAG_IN_TF;
500 if (drive->dev_flags & IDE_DFLAG_LBA48)
501 cmd.tf_flags |= (IDE_TFLAG_LBA48 | IDE_TFLAG_IN_HOB);
503 if (req_task->out_flags.all) {
504 cmd.ftf_flags |= IDE_FTFLAG_FLAGGED;
506 if (req_task->out_flags.b.data)
507 cmd.ftf_flags |= IDE_FTFLAG_OUT_DATA;
509 if (req_task->out_flags.b.nsector_hob)
510 cmd.tf_flags |= IDE_TFLAG_OUT_HOB_NSECT;
511 if (req_task->out_flags.b.sector_hob)
512 cmd.tf_flags |= IDE_TFLAG_OUT_HOB_LBAL;
513 if (req_task->out_flags.b.lcyl_hob)
514 cmd.tf_flags |= IDE_TFLAG_OUT_HOB_LBAM;
515 if (req_task->out_flags.b.hcyl_hob)
516 cmd.tf_flags |= IDE_TFLAG_OUT_HOB_LBAH;
518 if (req_task->out_flags.b.error_feature)
519 cmd.tf_flags |= IDE_TFLAG_OUT_FEATURE;
520 if (req_task->out_flags.b.nsector)
521 cmd.tf_flags |= IDE_TFLAG_OUT_NSECT;
522 if (req_task->out_flags.b.sector)
523 cmd.tf_flags |= IDE_TFLAG_OUT_LBAL;
524 if (req_task->out_flags.b.lcyl)
525 cmd.tf_flags |= IDE_TFLAG_OUT_LBAM;
526 if (req_task->out_flags.b.hcyl)
527 cmd.tf_flags |= IDE_TFLAG_OUT_LBAH;
528 } else {
529 cmd.tf_flags |= IDE_TFLAG_OUT_TF;
530 if (cmd.tf_flags & IDE_TFLAG_LBA48)
531 cmd.tf_flags |= IDE_TFLAG_OUT_HOB;
534 if (req_task->in_flags.b.data)
535 cmd.ftf_flags |= IDE_FTFLAG_IN_DATA;
537 if (req_task->req_cmd == IDE_DRIVE_TASK_RAW_WRITE) {
538 /* fixup data phase if needed */
539 if (req_task->data_phase == TASKFILE_IN_DMAQ ||
540 req_task->data_phase == TASKFILE_IN_DMA)
541 cmd.tf_flags |= IDE_TFLAG_WRITE;
544 cmd.protocol = ATA_PROT_DMA;
546 switch (req_task->data_phase) {
547 case TASKFILE_MULTI_OUT:
548 if (!drive->mult_count) {
549 /* (hs): give up if multcount is not set */
550 printk(KERN_ERR "%s: %s Multimode Write " \
551 "multcount is not set\n",
552 drive->name, __func__);
553 err = -EPERM;
554 goto abort;
556 cmd.tf_flags |= IDE_TFLAG_MULTI_PIO;
557 /* fall through */
558 case TASKFILE_OUT:
559 cmd.protocol = ATA_PROT_PIO;
560 /* fall through */
561 case TASKFILE_OUT_DMAQ:
562 case TASKFILE_OUT_DMA:
563 cmd.tf_flags |= IDE_TFLAG_WRITE;
564 nsect = taskout / SECTOR_SIZE;
565 data_buf = outbuf;
566 break;
567 case TASKFILE_MULTI_IN:
568 if (!drive->mult_count) {
569 /* (hs): give up if multcount is not set */
570 printk(KERN_ERR "%s: %s Multimode Read failure " \
571 "multcount is not set\n",
572 drive->name, __func__);
573 err = -EPERM;
574 goto abort;
576 cmd.tf_flags |= IDE_TFLAG_MULTI_PIO;
577 /* fall through */
578 case TASKFILE_IN:
579 cmd.protocol = ATA_PROT_PIO;
580 /* fall through */
581 case TASKFILE_IN_DMAQ:
582 case TASKFILE_IN_DMA:
583 nsect = taskin / SECTOR_SIZE;
584 data_buf = inbuf;
585 break;
586 case TASKFILE_NO_DATA:
587 cmd.protocol = ATA_PROT_NODATA;
588 break;
589 default:
590 err = -EFAULT;
591 goto abort;
594 if (req_task->req_cmd == IDE_DRIVE_TASK_NO_DATA)
595 nsect = 0;
596 else if (!nsect) {
597 nsect = (cmd.tf.hob_nsect << 8) | cmd.tf.nsect;
599 if (!nsect) {
600 printk(KERN_ERR "%s: in/out command without data\n",
601 drive->name);
602 err = -EFAULT;
603 goto abort;
607 err = ide_raw_taskfile(drive, &cmd, data_buf, nsect);
609 memcpy(req_task->hob_ports, &cmd.tf_array[0],
610 HDIO_DRIVE_HOB_HDR_SIZE - 2);
611 memcpy(req_task->io_ports, &cmd.tf_array[6],
612 HDIO_DRIVE_TASK_HDR_SIZE);
614 if ((cmd.ftf_flags & IDE_FTFLAG_SET_IN_FLAGS) &&
615 req_task->in_flags.all == 0) {
616 req_task->in_flags.all = IDE_TASKFILE_STD_IN_FLAGS;
617 if (drive->dev_flags & IDE_DFLAG_LBA48)
618 req_task->in_flags.all |= (IDE_HOB_STD_IN_FLAGS << 8);
621 if (copy_to_user(buf, req_task, tasksize)) {
622 err = -EFAULT;
623 goto abort;
625 if (taskout) {
626 int outtotal = tasksize;
627 if (copy_to_user(buf + outtotal, outbuf, taskout)) {
628 err = -EFAULT;
629 goto abort;
632 if (taskin) {
633 int intotal = tasksize + taskout;
634 if (copy_to_user(buf + intotal, inbuf, taskin)) {
635 err = -EFAULT;
636 goto abort;
639 abort:
640 kfree(req_task);
641 kfree(outbuf);
642 kfree(inbuf);
644 // printk("IDE Taskfile ioctl ended. rc = %i\n", err);
646 return err;
648 #endif