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.data_phase = TASKFILE_IN;
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_in_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->data_phase == TASKFILE_MULTI_IN ||
70 orig_cmd->data_phase == TASKFILE_MULTI_OUT) {
71 if (!drive->mult_count) {
72 printk(KERN_ERR "%s: multimode not set!\n",
78 if (orig_cmd->ftf_flags & IDE_FTFLAG_FLAGGED)
79 orig_cmd->ftf_flags |= IDE_FTFLAG_SET_IN_FLAGS;
81 memcpy(cmd, orig_cmd, sizeof(*cmd));
83 if ((cmd->tf_flags & IDE_TFLAG_DMA_PIO_FALLBACK) == 0) {
84 ide_tf_dump(drive->name, tf);
85 tp_ops->set_irq(hwif, 1);
86 SELECT_MASK(drive, 0);
87 tp_ops->tf_load(drive, cmd);
90 switch (cmd->data_phase) {
91 case TASKFILE_MULTI_OUT:
93 tp_ops->exec_command(hwif, tf->command);
94 ndelay(400); /* FIXME */
95 return pre_task_out_intr(drive, cmd);
96 case TASKFILE_MULTI_IN:
98 handler = task_in_intr;
100 case TASKFILE_NO_DATA:
102 handler = task_no_data_intr;
103 ide_execute_command(drive, tf->command, handler,
104 WAIT_WORSTCASE, NULL);
107 if ((drive->dev_flags & IDE_DFLAG_USING_DMA) == 0 ||
108 ide_build_sglist(drive, hwif->rq) == 0 ||
109 dma_ops->dma_setup(drive))
111 dma_ops->dma_exec_cmd(drive, tf->command);
112 dma_ops->dma_start(drive);
116 EXPORT_SYMBOL_GPL(do_rw_taskfile);
119 * Handler for commands without a data phase
121 static ide_startstop_t task_no_data_intr(ide_drive_t *drive)
123 ide_hwif_t *hwif = drive->hwif;
124 struct ide_cmd *cmd = &hwif->cmd;
125 struct ide_taskfile *tf = &cmd->tf;
126 int custom = (cmd->tf_flags & IDE_TFLAG_CUSTOM_HANDLER) ? 1 : 0;
127 int retries = (custom && tf->command == ATA_CMD_INIT_DEV_PARAMS) ? 5 : 1;
130 local_irq_enable_in_hardirq();
133 stat = hwif->tp_ops->read_status(hwif);
134 if ((stat & ATA_BUSY) == 0 || retries-- == 0)
139 if (!OK_STAT(stat, ATA_DRDY, BAD_STAT)) {
140 if (custom && tf->command == ATA_CMD_SET_MULTI) {
141 drive->mult_req = drive->mult_count = 0;
142 drive->special.b.recalibrate = 1;
143 (void)ide_dump_status(drive, __func__, stat);
145 } else if (custom && tf->command == ATA_CMD_INIT_DEV_PARAMS) {
146 if ((stat & (ATA_ERR | ATA_DRQ)) == 0) {
147 ide_set_handler(drive, &task_no_data_intr,
148 WAIT_WORSTCASE, NULL);
152 return ide_error(drive, "task_no_data_intr", stat);
155 if (custom && tf->command == ATA_CMD_IDLEIMMEDIATE) {
156 hwif->tp_ops->tf_read(drive, cmd);
157 if (tf->lbal != 0xc4) {
158 printk(KERN_ERR "%s: head unload failed!\n",
160 ide_tf_dump(drive->name, tf);
162 drive->dev_flags |= IDE_DFLAG_PARKED;
163 } else if (custom && tf->command == ATA_CMD_SET_MULTI)
164 drive->mult_count = drive->mult_req;
166 if (custom == 0 || tf->command == ATA_CMD_IDLEIMMEDIATE) {
167 struct request *rq = hwif->rq;
168 u8 err = ide_read_error(drive);
170 if (blk_pm_request(rq))
171 ide_complete_pm_rq(drive, rq);
173 if (rq->cmd_type == REQ_TYPE_ATA_TASKFILE)
174 ide_complete_cmd(drive, cmd, stat, err);
175 ide_complete_rq(drive, err);
182 static u8 wait_drive_not_busy(ide_drive_t *drive)
184 ide_hwif_t *hwif = drive->hwif;
189 * Last sector was transfered, wait until device is ready. This can
190 * take up to 6 ms on some ATAPI devices, so we will wait max 10 ms.
192 for (retries = 0; retries < 1000; retries++) {
193 stat = hwif->tp_ops->read_status(hwif);
202 printk(KERN_ERR "%s: drive still BUSY!\n", drive->name);
207 static void ide_pio_sector(ide_drive_t *drive, struct ide_cmd *cmd,
210 ide_hwif_t *hwif = drive->hwif;
211 struct scatterlist *sg = hwif->sg_table;
212 struct scatterlist *cursg = cmd->cursg;
214 #ifdef CONFIG_HIGHMEM
226 page = sg_page(cursg);
227 offset = cursg->offset + cmd->cursg_ofs * SECTOR_SIZE;
229 /* get the current page and offset */
230 page = nth_page(page, (offset >> PAGE_SHIFT));
233 #ifdef CONFIG_HIGHMEM
234 local_irq_save(flags);
236 buf = kmap_atomic(page, KM_BIO_SRC_IRQ) + offset;
241 if ((cmd->cursg_ofs * SECTOR_SIZE) == cursg->length) {
242 cmd->cursg = sg_next(cmd->cursg);
246 /* do the actual data transfer */
248 hwif->tp_ops->output_data(drive, cmd, buf, SECTOR_SIZE);
250 hwif->tp_ops->input_data(drive, cmd, buf, SECTOR_SIZE);
252 kunmap_atomic(buf, KM_BIO_SRC_IRQ);
253 #ifdef CONFIG_HIGHMEM
254 local_irq_restore(flags);
258 static void ide_pio_multi(ide_drive_t *drive, struct ide_cmd *cmd,
263 nsect = min_t(unsigned int, cmd->nleft, drive->mult_count);
265 ide_pio_sector(drive, cmd, write);
268 static void ide_pio_datablock(ide_drive_t *drive, struct ide_cmd *cmd,
271 u8 saved_io_32bit = drive->io_32bit;
273 if (cmd->tf_flags & IDE_TFLAG_FS)
276 if (cmd->tf_flags & IDE_TFLAG_IO_16BIT)
279 touch_softlockup_watchdog();
281 switch (cmd->data_phase) {
282 case TASKFILE_MULTI_IN:
283 case TASKFILE_MULTI_OUT:
284 ide_pio_multi(drive, cmd, write);
287 ide_pio_sector(drive, cmd, write);
291 drive->io_32bit = saved_io_32bit;
294 static ide_startstop_t task_error(ide_drive_t *drive, struct ide_cmd *cmd,
295 const char *s, u8 stat)
297 if (cmd->tf_flags & IDE_TFLAG_FS) {
298 int sectors = cmd->nsect - cmd->nleft;
300 switch (cmd->data_phase) {
308 case TASKFILE_MULTI_IN:
312 case TASKFILE_MULTI_OUT:
313 sectors -= drive->mult_count;
319 ide_end_request(drive, 1, sectors);
321 return ide_error(drive, s, stat);
324 void ide_finish_cmd(ide_drive_t *drive, struct ide_cmd *cmd, u8 stat)
326 if ((cmd->tf_flags & IDE_TFLAG_FS) == 0) {
327 u8 err = ide_read_error(drive);
329 ide_complete_cmd(drive, cmd, stat, err);
330 ide_complete_rq(drive, err);
334 ide_end_request(drive, 1, cmd->rq->nr_sectors);
338 * We got an interrupt on a task_in case, but no errors and no DRQ.
340 * It might be a spurious irq (shared irq), but it might be a
341 * command that had no output.
343 static ide_startstop_t task_in_unexpected(ide_drive_t *drive,
344 struct ide_cmd *cmd, u8 stat)
346 /* Command all done? */
347 if (OK_STAT(stat, ATA_DRDY, ATA_BUSY)) {
348 ide_finish_cmd(drive, cmd, stat);
352 /* Assume it was a spurious irq */
353 ide_set_handler(drive, &task_in_intr, WAIT_WORSTCASE, NULL);
358 * Handler for command with PIO data-in phase (Read/Read Multiple).
360 static ide_startstop_t task_in_intr(ide_drive_t *drive)
362 ide_hwif_t *hwif = drive->hwif;
363 struct ide_cmd *cmd = &drive->hwif->cmd;
364 u8 stat = hwif->tp_ops->read_status(hwif);
368 return task_error(drive, cmd, __func__, stat);
370 /* Didn't want any data? Odd. */
371 if ((stat & ATA_DRQ) == 0)
372 return task_in_unexpected(drive, cmd, stat);
374 ide_pio_datablock(drive, cmd, 0);
376 /* Are we done? Check status and finish transfer. */
377 if (cmd->nleft == 0) {
378 stat = wait_drive_not_busy(drive);
379 if (!OK_STAT(stat, 0, BAD_STAT))
380 return task_error(drive, cmd, __func__, stat);
381 ide_finish_cmd(drive, cmd, stat);
385 /* Still data left to transfer. */
386 ide_set_handler(drive, &task_in_intr, WAIT_WORSTCASE, NULL);
392 * Handler for command with PIO data-out phase (Write/Write Multiple).
394 static ide_startstop_t task_out_intr (ide_drive_t *drive)
396 ide_hwif_t *hwif = drive->hwif;
397 struct ide_cmd *cmd = &drive->hwif->cmd;
398 u8 stat = hwif->tp_ops->read_status(hwif);
400 if (!OK_STAT(stat, DRIVE_READY, drive->bad_wstat))
401 return task_error(drive, cmd, __func__, stat);
403 /* Deal with unexpected ATA data phase. */
404 if (((stat & ATA_DRQ) == 0) ^ (cmd->nleft == 0))
405 return task_error(drive, cmd, __func__, stat);
407 if (cmd->nleft == 0) {
408 ide_finish_cmd(drive, cmd, stat);
412 /* Still data left to transfer. */
413 ide_pio_datablock(drive, cmd, 1);
414 ide_set_handler(drive, &task_out_intr, WAIT_WORSTCASE, NULL);
419 static ide_startstop_t pre_task_out_intr(ide_drive_t *drive,
422 ide_startstop_t startstop;
424 if (ide_wait_stat(&startstop, drive, ATA_DRQ,
425 drive->bad_wstat, WAIT_DRQ)) {
426 printk(KERN_ERR "%s: no DRQ after issuing %sWRITE%s\n",
428 cmd->data_phase == TASKFILE_MULTI_OUT ? "MULT" : "",
429 (drive->dev_flags & IDE_DFLAG_LBA48) ? "_EXT" : "");
433 if ((drive->dev_flags & IDE_DFLAG_UNMASK) == 0)
436 ide_set_handler(drive, &task_out_intr, WAIT_WORSTCASE, NULL);
437 ide_pio_datablock(drive, cmd, 1);
442 int ide_raw_taskfile(ide_drive_t *drive, struct ide_cmd *cmd, u8 *buf,
448 rq = blk_get_request(drive->queue, READ, __GFP_WAIT);
449 rq->cmd_type = REQ_TYPE_ATA_TASKFILE;
453 * (ks) We transfer currently only whole sectors.
454 * This is suffient for now. But, it would be great,
455 * if we would find a solution to transfer any size.
456 * To support special commands like READ LONG.
458 rq->hard_nr_sectors = rq->nr_sectors = nsect;
459 rq->hard_cur_sectors = rq->current_nr_sectors = nsect;
461 if (cmd->tf_flags & IDE_TFLAG_WRITE)
462 rq->cmd_flags |= REQ_RW;
467 error = blk_execute_rq(drive->queue, NULL, rq, 0);
473 EXPORT_SYMBOL(ide_raw_taskfile);
475 int ide_no_data_taskfile(ide_drive_t *drive, struct ide_cmd *cmd)
477 cmd->data_phase = TASKFILE_NO_DATA;
479 return ide_raw_taskfile(drive, cmd, NULL, 0);
481 EXPORT_SYMBOL_GPL(ide_no_data_taskfile);
483 #ifdef CONFIG_IDE_TASK_IOCTL
484 int ide_taskfile_ioctl(ide_drive_t *drive, unsigned long arg)
486 ide_task_request_t *req_task;
492 int tasksize = sizeof(struct ide_task_request_s);
493 unsigned int taskin = 0;
494 unsigned int taskout = 0;
496 char __user *buf = (char __user *)arg;
498 // printk("IDE Taskfile ...\n");
500 req_task = kzalloc(tasksize, GFP_KERNEL);
501 if (req_task == NULL) return -ENOMEM;
502 if (copy_from_user(req_task, buf, tasksize)) {
507 taskout = req_task->out_size;
508 taskin = req_task->in_size;
510 if (taskin > 65536 || taskout > 65536) {
516 int outtotal = tasksize;
517 outbuf = kzalloc(taskout, GFP_KERNEL);
518 if (outbuf == NULL) {
522 if (copy_from_user(outbuf, buf + outtotal, taskout)) {
529 int intotal = tasksize + taskout;
530 inbuf = kzalloc(taskin, GFP_KERNEL);
535 if (copy_from_user(inbuf, buf + intotal, taskin)) {
541 memset(&cmd, 0, sizeof(cmd));
543 memcpy(&cmd.tf_array[0], req_task->hob_ports,
544 HDIO_DRIVE_HOB_HDR_SIZE - 2);
545 memcpy(&cmd.tf_array[6], req_task->io_ports,
546 HDIO_DRIVE_TASK_HDR_SIZE);
548 cmd.data_phase = req_task->data_phase;
549 cmd.tf_flags = IDE_TFLAG_IO_16BIT | IDE_TFLAG_DEVICE |
552 if (drive->dev_flags & IDE_DFLAG_LBA48)
553 cmd.tf_flags |= (IDE_TFLAG_LBA48 | IDE_TFLAG_IN_HOB);
555 if (req_task->out_flags.all) {
556 cmd.ftf_flags |= IDE_FTFLAG_FLAGGED;
558 if (req_task->out_flags.b.data)
559 cmd.ftf_flags |= IDE_FTFLAG_OUT_DATA;
561 if (req_task->out_flags.b.nsector_hob)
562 cmd.tf_flags |= IDE_TFLAG_OUT_HOB_NSECT;
563 if (req_task->out_flags.b.sector_hob)
564 cmd.tf_flags |= IDE_TFLAG_OUT_HOB_LBAL;
565 if (req_task->out_flags.b.lcyl_hob)
566 cmd.tf_flags |= IDE_TFLAG_OUT_HOB_LBAM;
567 if (req_task->out_flags.b.hcyl_hob)
568 cmd.tf_flags |= IDE_TFLAG_OUT_HOB_LBAH;
570 if (req_task->out_flags.b.error_feature)
571 cmd.tf_flags |= IDE_TFLAG_OUT_FEATURE;
572 if (req_task->out_flags.b.nsector)
573 cmd.tf_flags |= IDE_TFLAG_OUT_NSECT;
574 if (req_task->out_flags.b.sector)
575 cmd.tf_flags |= IDE_TFLAG_OUT_LBAL;
576 if (req_task->out_flags.b.lcyl)
577 cmd.tf_flags |= IDE_TFLAG_OUT_LBAM;
578 if (req_task->out_flags.b.hcyl)
579 cmd.tf_flags |= IDE_TFLAG_OUT_LBAH;
581 cmd.tf_flags |= IDE_TFLAG_OUT_TF;
582 if (cmd.tf_flags & IDE_TFLAG_LBA48)
583 cmd.tf_flags |= IDE_TFLAG_OUT_HOB;
586 if (req_task->in_flags.b.data)
587 cmd.ftf_flags |= IDE_FTFLAG_IN_DATA;
589 switch(req_task->data_phase) {
590 case TASKFILE_MULTI_OUT:
591 if (!drive->mult_count) {
592 /* (hs): give up if multcount is not set */
593 printk(KERN_ERR "%s: %s Multimode Write " \
594 "multcount is not set\n",
595 drive->name, __func__);
602 case TASKFILE_OUT_DMAQ:
603 case TASKFILE_OUT_DMA:
604 nsect = taskout / SECTOR_SIZE;
607 case TASKFILE_MULTI_IN:
608 if (!drive->mult_count) {
609 /* (hs): give up if multcount is not set */
610 printk(KERN_ERR "%s: %s Multimode Read failure " \
611 "multcount is not set\n",
612 drive->name, __func__);
619 case TASKFILE_IN_DMAQ:
620 case TASKFILE_IN_DMA:
621 nsect = taskin / SECTOR_SIZE;
624 case TASKFILE_NO_DATA:
631 if (req_task->req_cmd == IDE_DRIVE_TASK_NO_DATA)
634 nsect = (cmd.tf.hob_nsect << 8) | cmd.tf.nsect;
637 printk(KERN_ERR "%s: in/out command without data\n",
644 if (req_task->req_cmd == IDE_DRIVE_TASK_RAW_WRITE)
645 cmd.tf_flags |= IDE_TFLAG_WRITE;
647 err = ide_raw_taskfile(drive, &cmd, data_buf, nsect);
649 memcpy(req_task->hob_ports, &cmd.tf_array[0],
650 HDIO_DRIVE_HOB_HDR_SIZE - 2);
651 memcpy(req_task->io_ports, &cmd.tf_array[6],
652 HDIO_DRIVE_TASK_HDR_SIZE);
654 if ((cmd.ftf_flags & IDE_FTFLAG_SET_IN_FLAGS) &&
655 req_task->in_flags.all == 0) {
656 req_task->in_flags.all = IDE_TASKFILE_STD_IN_FLAGS;
657 if (drive->dev_flags & IDE_DFLAG_LBA48)
658 req_task->in_flags.all |= (IDE_HOB_STD_IN_FLAGS << 8);
661 if (copy_to_user(buf, req_task, tasksize)) {
666 int outtotal = tasksize;
667 if (copy_to_user(buf + outtotal, outbuf, taskout)) {
673 int intotal = tasksize + taskout;
674 if (copy_to_user(buf + intotal, inbuf, taskin)) {
684 // printk("IDE Taskfile ioctl ended. rc = %i\n", err);