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.
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>
26 void ide_tf_dump(const char *s, struct ide_taskfile *tf)
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);
40 int taskfile_lib_get_identify (ide_drive_t *drive, u8 *buf)
44 memset(&cmd, 0, sizeof(cmd));
46 if (drive->media == ide_disk)
47 cmd.tf.command = ATA_CMD_ID_ATA;
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);
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);
85 tp_ops->tf_load(drive, cmd);
88 switch (cmd->protocol) {
90 if (cmd->tf_flags & IDE_TFLAG_WRITE) {
91 tp_ops->exec_command(hwif, tf->command);
92 ndelay(400); /* FIXME */
93 return pre_task_out_intr(drive, cmd);
95 handler = task_pio_intr;
99 handler = task_no_data_intr;
100 ide_execute_command(drive, cmd, handler, WAIT_WORSTCASE);
103 if (ide_dma_prepare(drive, cmd))
105 hwif->expiry = dma_ops->dma_timer_expiry;
106 ide_execute_command(drive, cmd, ide_dma_intr, 2 * WAIT_CMD);
107 dma_ops->dma_start(drive);
112 EXPORT_SYMBOL_GPL(do_rw_taskfile);
114 static ide_startstop_t task_no_data_intr(ide_drive_t *drive)
116 ide_hwif_t *hwif = drive->hwif;
117 struct ide_cmd *cmd = &hwif->cmd;
118 struct ide_taskfile *tf = &cmd->tf;
119 int custom = (cmd->tf_flags & IDE_TFLAG_CUSTOM_HANDLER) ? 1 : 0;
120 int retries = (custom && tf->command == ATA_CMD_INIT_DEV_PARAMS) ? 5 : 1;
123 local_irq_enable_in_hardirq();
126 stat = hwif->tp_ops->read_status(hwif);
127 if ((stat & ATA_BUSY) == 0 || retries-- == 0)
132 if (!OK_STAT(stat, ATA_DRDY, BAD_STAT)) {
133 if (custom && tf->command == ATA_CMD_SET_MULTI) {
134 drive->mult_req = drive->mult_count = 0;
135 drive->special.b.recalibrate = 1;
136 (void)ide_dump_status(drive, __func__, stat);
138 } else if (custom && tf->command == ATA_CMD_INIT_DEV_PARAMS) {
139 if ((stat & (ATA_ERR | ATA_DRQ)) == 0) {
140 ide_set_handler(drive, &task_no_data_intr,
145 return ide_error(drive, "task_no_data_intr", stat);
148 if (custom && tf->command == ATA_CMD_SET_MULTI)
149 drive->mult_count = drive->mult_req;
151 if (custom == 0 || tf->command == ATA_CMD_IDLEIMMEDIATE ||
152 tf->command == ATA_CMD_CHK_POWER) {
153 struct request *rq = hwif->rq;
155 if (blk_pm_request(rq))
156 ide_complete_pm_rq(drive, rq);
158 ide_finish_cmd(drive, cmd, stat);
164 static u8 wait_drive_not_busy(ide_drive_t *drive)
166 ide_hwif_t *hwif = drive->hwif;
171 * Last sector was transfered, wait until device is ready. This can
172 * take up to 6 ms on some ATAPI devices, so we will wait max 10 ms.
174 for (retries = 0; retries < 1000; retries++) {
175 stat = hwif->tp_ops->read_status(hwif);
184 printk(KERN_ERR "%s: drive still BUSY!\n", drive->name);
189 void ide_pio_bytes(ide_drive_t *drive, struct ide_cmd *cmd,
190 unsigned int write, unsigned int len)
192 ide_hwif_t *hwif = drive->hwif;
193 struct scatterlist *sg = hwif->sg_table;
194 struct scatterlist *cursg = cmd->cursg;
202 cursg = cmd->cursg = sg;
205 unsigned nr_bytes = min(len, cursg->length - cmd->cursg_ofs);
207 if (nr_bytes > PAGE_SIZE)
208 nr_bytes = PAGE_SIZE;
210 page = sg_page(cursg);
211 offset = cursg->offset + cmd->cursg_ofs;
213 /* get the current page and offset */
214 page = nth_page(page, (offset >> PAGE_SHIFT));
217 if (PageHighMem(page))
218 local_irq_save(flags);
220 buf = kmap_atomic(page, KM_BIO_SRC_IRQ) + offset;
222 cmd->nleft -= nr_bytes;
223 cmd->cursg_ofs += nr_bytes;
225 if (cmd->cursg_ofs == cursg->length) {
226 cursg = cmd->cursg = sg_next(cmd->cursg);
230 /* do the actual data transfer */
232 hwif->tp_ops->output_data(drive, cmd, buf, nr_bytes);
234 hwif->tp_ops->input_data(drive, cmd, buf, nr_bytes);
236 kunmap_atomic(buf, KM_BIO_SRC_IRQ);
238 if (PageHighMem(page))
239 local_irq_restore(flags);
244 EXPORT_SYMBOL_GPL(ide_pio_bytes);
246 static void ide_pio_datablock(ide_drive_t *drive, struct ide_cmd *cmd,
249 unsigned int nr_bytes;
251 u8 saved_io_32bit = drive->io_32bit;
253 if (cmd->tf_flags & IDE_TFLAG_FS)
256 if (cmd->tf_flags & IDE_TFLAG_IO_16BIT)
259 touch_softlockup_watchdog();
261 if (cmd->tf_flags & IDE_TFLAG_MULTI_PIO)
262 nr_bytes = min_t(unsigned, cmd->nleft, drive->mult_count << 9);
264 nr_bytes = SECTOR_SIZE;
266 ide_pio_bytes(drive, cmd, write, nr_bytes);
268 drive->io_32bit = saved_io_32bit;
271 static void ide_error_cmd(ide_drive_t *drive, struct ide_cmd *cmd)
273 if (cmd->tf_flags & IDE_TFLAG_FS) {
274 int nr_bytes = cmd->nbytes - cmd->nleft;
276 if (cmd->protocol == ATA_PROT_PIO &&
277 ((cmd->tf_flags & IDE_TFLAG_WRITE) || cmd->nleft == 0)) {
278 if (cmd->tf_flags & IDE_TFLAG_MULTI_PIO)
279 nr_bytes -= drive->mult_count << 9;
281 nr_bytes -= SECTOR_SIZE;
285 ide_complete_rq(drive, 0, nr_bytes);
289 void ide_finish_cmd(ide_drive_t *drive, struct ide_cmd *cmd, u8 stat)
291 struct request *rq = drive->hwif->rq;
292 u8 err = ide_read_error(drive);
294 ide_complete_cmd(drive, cmd, stat, err);
296 ide_complete_rq(drive, err ? -EIO : 0, blk_rq_bytes(rq));
300 * Handler for command with PIO data phase.
302 static ide_startstop_t task_pio_intr(ide_drive_t *drive)
304 ide_hwif_t *hwif = drive->hwif;
305 struct ide_cmd *cmd = &drive->hwif->cmd;
306 u8 stat = hwif->tp_ops->read_status(hwif);
307 u8 write = !!(cmd->tf_flags & IDE_TFLAG_WRITE);
314 /* Didn't want any data? Odd. */
315 if ((stat & ATA_DRQ) == 0) {
316 /* Command all done? */
317 if (OK_STAT(stat, ATA_DRDY, ATA_BUSY))
320 /* Assume it was a spurious irq */
324 if (!OK_STAT(stat, DRIVE_READY, drive->bad_wstat))
327 /* Deal with unexpected ATA data phase. */
328 if (((stat & ATA_DRQ) == 0) ^ (cmd->nleft == 0))
332 if (write && cmd->nleft == 0)
335 /* Still data left to transfer. */
336 ide_pio_datablock(drive, cmd, write);
338 /* Are we done? Check status and finish transfer. */
339 if (write == 0 && cmd->nleft == 0) {
340 stat = wait_drive_not_busy(drive);
341 if (!OK_STAT(stat, 0, BAD_STAT))
347 /* Still data left to transfer. */
348 ide_set_handler(drive, &task_pio_intr, WAIT_WORSTCASE);
351 if ((cmd->tf_flags & IDE_TFLAG_FS) == 0)
352 ide_finish_cmd(drive, cmd, stat);
354 ide_complete_rq(drive, 0, cmd->rq->nr_sectors << 9);
357 ide_error_cmd(drive, cmd);
358 return ide_error(drive, __func__, stat);
361 static ide_startstop_t pre_task_out_intr(ide_drive_t *drive,
364 ide_startstop_t startstop;
366 if (ide_wait_stat(&startstop, drive, ATA_DRQ,
367 drive->bad_wstat, WAIT_DRQ)) {
368 printk(KERN_ERR "%s: no DRQ after issuing %sWRITE%s\n",
370 (cmd->tf_flags & IDE_TFLAG_MULTI_PIO) ? "MULT" : "",
371 (drive->dev_flags & IDE_DFLAG_LBA48) ? "_EXT" : "");
375 if ((drive->dev_flags & IDE_DFLAG_UNMASK) == 0)
378 ide_set_handler(drive, &task_pio_intr, WAIT_WORSTCASE);
380 ide_pio_datablock(drive, cmd, 1);
385 int ide_raw_taskfile(ide_drive_t *drive, struct ide_cmd *cmd, u8 *buf,
391 rq = blk_get_request(drive->queue, READ, __GFP_WAIT);
392 rq->cmd_type = REQ_TYPE_ATA_TASKFILE;
396 * (ks) We transfer currently only whole sectors.
397 * This is suffient for now. But, it would be great,
398 * if we would find a solution to transfer any size.
399 * To support special commands like READ LONG.
401 rq->hard_nr_sectors = rq->nr_sectors = nsect;
402 rq->hard_cur_sectors = rq->current_nr_sectors = nsect;
404 if (cmd->tf_flags & IDE_TFLAG_WRITE)
405 rq->cmd_flags |= REQ_RW;
410 error = blk_execute_rq(drive->queue, NULL, rq, 0);
416 EXPORT_SYMBOL(ide_raw_taskfile);
418 int ide_no_data_taskfile(ide_drive_t *drive, struct ide_cmd *cmd)
420 cmd->protocol = ATA_PROT_NODATA;
422 return ide_raw_taskfile(drive, cmd, NULL, 0);
424 EXPORT_SYMBOL_GPL(ide_no_data_taskfile);
426 #ifdef CONFIG_IDE_TASK_IOCTL
427 int ide_taskfile_ioctl(ide_drive_t *drive, unsigned long arg)
429 ide_task_request_t *req_task;
435 int tasksize = sizeof(struct ide_task_request_s);
436 unsigned int taskin = 0;
437 unsigned int taskout = 0;
439 char __user *buf = (char __user *)arg;
441 // printk("IDE Taskfile ...\n");
443 req_task = kzalloc(tasksize, GFP_KERNEL);
444 if (req_task == NULL) return -ENOMEM;
445 if (copy_from_user(req_task, buf, tasksize)) {
450 taskout = req_task->out_size;
451 taskin = req_task->in_size;
453 if (taskin > 65536 || taskout > 65536) {
459 int outtotal = tasksize;
460 outbuf = kzalloc(taskout, GFP_KERNEL);
461 if (outbuf == NULL) {
465 if (copy_from_user(outbuf, buf + outtotal, taskout)) {
472 int intotal = tasksize + taskout;
473 inbuf = kzalloc(taskin, GFP_KERNEL);
478 if (copy_from_user(inbuf, buf + intotal, taskin)) {
484 memset(&cmd, 0, sizeof(cmd));
486 memcpy(&cmd.tf_array[0], req_task->hob_ports,
487 HDIO_DRIVE_HOB_HDR_SIZE - 2);
488 memcpy(&cmd.tf_array[6], req_task->io_ports,
489 HDIO_DRIVE_TASK_HDR_SIZE);
491 cmd.tf_flags = IDE_TFLAG_IO_16BIT | IDE_TFLAG_DEVICE |
494 if (drive->dev_flags & IDE_DFLAG_LBA48)
495 cmd.tf_flags |= (IDE_TFLAG_LBA48 | IDE_TFLAG_IN_HOB);
497 if (req_task->out_flags.all) {
498 cmd.ftf_flags |= IDE_FTFLAG_FLAGGED;
500 if (req_task->out_flags.b.data)
501 cmd.ftf_flags |= IDE_FTFLAG_OUT_DATA;
503 if (req_task->out_flags.b.nsector_hob)
504 cmd.tf_flags |= IDE_TFLAG_OUT_HOB_NSECT;
505 if (req_task->out_flags.b.sector_hob)
506 cmd.tf_flags |= IDE_TFLAG_OUT_HOB_LBAL;
507 if (req_task->out_flags.b.lcyl_hob)
508 cmd.tf_flags |= IDE_TFLAG_OUT_HOB_LBAM;
509 if (req_task->out_flags.b.hcyl_hob)
510 cmd.tf_flags |= IDE_TFLAG_OUT_HOB_LBAH;
512 if (req_task->out_flags.b.error_feature)
513 cmd.tf_flags |= IDE_TFLAG_OUT_FEATURE;
514 if (req_task->out_flags.b.nsector)
515 cmd.tf_flags |= IDE_TFLAG_OUT_NSECT;
516 if (req_task->out_flags.b.sector)
517 cmd.tf_flags |= IDE_TFLAG_OUT_LBAL;
518 if (req_task->out_flags.b.lcyl)
519 cmd.tf_flags |= IDE_TFLAG_OUT_LBAM;
520 if (req_task->out_flags.b.hcyl)
521 cmd.tf_flags |= IDE_TFLAG_OUT_LBAH;
523 cmd.tf_flags |= IDE_TFLAG_OUT_TF;
524 if (cmd.tf_flags & IDE_TFLAG_LBA48)
525 cmd.tf_flags |= IDE_TFLAG_OUT_HOB;
528 if (req_task->in_flags.b.data)
529 cmd.ftf_flags |= IDE_FTFLAG_IN_DATA;
531 if (req_task->req_cmd == IDE_DRIVE_TASK_RAW_WRITE) {
532 /* fixup data phase if needed */
533 if (req_task->data_phase == TASKFILE_IN_DMAQ ||
534 req_task->data_phase == TASKFILE_IN_DMA)
535 cmd.tf_flags |= IDE_TFLAG_WRITE;
538 cmd.protocol = ATA_PROT_DMA;
540 switch (req_task->data_phase) {
541 case TASKFILE_MULTI_OUT:
542 if (!drive->mult_count) {
543 /* (hs): give up if multcount is not set */
544 printk(KERN_ERR "%s: %s Multimode Write " \
545 "multcount is not set\n",
546 drive->name, __func__);
550 cmd.tf_flags |= IDE_TFLAG_MULTI_PIO;
553 cmd.protocol = ATA_PROT_PIO;
555 case TASKFILE_OUT_DMAQ:
556 case TASKFILE_OUT_DMA:
557 cmd.tf_flags |= IDE_TFLAG_WRITE;
558 nsect = taskout / SECTOR_SIZE;
561 case TASKFILE_MULTI_IN:
562 if (!drive->mult_count) {
563 /* (hs): give up if multcount is not set */
564 printk(KERN_ERR "%s: %s Multimode Read failure " \
565 "multcount is not set\n",
566 drive->name, __func__);
570 cmd.tf_flags |= IDE_TFLAG_MULTI_PIO;
573 cmd.protocol = ATA_PROT_PIO;
575 case TASKFILE_IN_DMAQ:
576 case TASKFILE_IN_DMA:
577 nsect = taskin / SECTOR_SIZE;
580 case TASKFILE_NO_DATA:
581 cmd.protocol = ATA_PROT_NODATA;
588 if (req_task->req_cmd == IDE_DRIVE_TASK_NO_DATA)
591 nsect = (cmd.tf.hob_nsect << 8) | cmd.tf.nsect;
594 printk(KERN_ERR "%s: in/out command without data\n",
601 err = ide_raw_taskfile(drive, &cmd, data_buf, nsect);
603 memcpy(req_task->hob_ports, &cmd.tf_array[0],
604 HDIO_DRIVE_HOB_HDR_SIZE - 2);
605 memcpy(req_task->io_ports, &cmd.tf_array[6],
606 HDIO_DRIVE_TASK_HDR_SIZE);
608 if ((cmd.ftf_flags & IDE_FTFLAG_SET_IN_FLAGS) &&
609 req_task->in_flags.all == 0) {
610 req_task->in_flags.all = IDE_TASKFILE_STD_IN_FLAGS;
611 if (drive->dev_flags & IDE_DFLAG_LBA48)
612 req_task->in_flags.all |= (IDE_HOB_STD_IN_FLAGS << 8);
615 if (copy_to_user(buf, req_task, tasksize)) {
620 int outtotal = tasksize;
621 if (copy_to_user(buf + outtotal, outbuf, taskout)) {
627 int intotal = tasksize + taskout;
628 if (copy_to_user(buf + intotal, inbuf, taskin)) {
638 // printk("IDE Taskfile ioctl ended. rc = %i\n", err);