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/module.h>
12 #include <linux/types.h>
13 #include <linux/string.h>
14 #include <linux/kernel.h>
15 #include <linux/timer.h>
17 #include <linux/sched.h>
18 #include <linux/interrupt.h>
19 #include <linux/major.h>
20 #include <linux/errno.h>
21 #include <linux/genhd.h>
22 #include <linux/blkpg.h>
23 #include <linux/slab.h>
24 #include <linux/pci.h>
25 #include <linux/delay.h>
26 #include <linux/hdreg.h>
27 #include <linux/ide.h>
28 #include <linux/bitops.h>
29 #include <linux/scatterlist.h>
31 #include <asm/byteorder.h>
33 #include <asm/uaccess.h>
36 void ide_tf_dump(const char *s, struct ide_taskfile *tf)
39 printk("%s: tf: feat 0x%02x nsect 0x%02x lbal 0x%02x "
40 "lbam 0x%02x lbah 0x%02x dev 0x%02x cmd 0x%02x\n",
41 s, tf->feature, tf->nsect, tf->lbal,
42 tf->lbam, tf->lbah, tf->device, tf->command);
43 printk("%s: hob: nsect 0x%02x lbal 0x%02x "
44 "lbam 0x%02x lbah 0x%02x\n",
45 s, tf->hob_nsect, tf->hob_lbal,
46 tf->hob_lbam, tf->hob_lbah);
50 int taskfile_lib_get_identify (ide_drive_t *drive, u8 *buf)
54 memset(&args, 0, sizeof(ide_task_t));
56 if (drive->media == ide_disk)
57 args.tf.command = WIN_IDENTIFY;
59 args.tf.command = WIN_PIDENTIFY;
60 args.tf_flags = IDE_TFLAG_TF | IDE_TFLAG_DEVICE;
61 args.data_phase = TASKFILE_IN;
62 return ide_raw_taskfile(drive, &args, buf, 1);
65 static int inline task_dma_ok(ide_task_t *task)
67 if (blk_fs_request(task->rq) || (task->tf_flags & IDE_TFLAG_FLAGGED))
70 switch (task->tf.command) {
71 case WIN_WRITEDMA_ONCE:
73 case WIN_WRITEDMA_EXT:
74 case WIN_READDMA_ONCE:
77 case WIN_IDENTIFY_DMA:
84 static ide_startstop_t task_no_data_intr(ide_drive_t *);
85 static ide_startstop_t set_geometry_intr(ide_drive_t *);
86 static ide_startstop_t recal_intr(ide_drive_t *);
87 static ide_startstop_t set_multmode_intr(ide_drive_t *);
88 static ide_startstop_t pre_task_out_intr(ide_drive_t *, struct request *);
89 static ide_startstop_t task_in_intr(ide_drive_t *);
91 ide_startstop_t do_rw_taskfile (ide_drive_t *drive, ide_task_t *task)
93 ide_hwif_t *hwif = HWIF(drive);
94 struct ide_taskfile *tf = &task->tf;
95 ide_handler_t *handler = NULL;
96 const struct ide_dma_ops *dma_ops = hwif->dma_ops;
98 if (task->data_phase == TASKFILE_MULTI_IN ||
99 task->data_phase == TASKFILE_MULTI_OUT) {
100 if (!drive->mult_count) {
101 printk(KERN_ERR "%s: multimode not set!\n",
107 if (task->tf_flags & IDE_TFLAG_FLAGGED)
108 task->tf_flags |= IDE_TFLAG_FLAGGED_SET_IN_FLAGS;
110 if ((task->tf_flags & IDE_TFLAG_DMA_PIO_FALLBACK) == 0) {
111 ide_tf_dump(drive->name, tf);
112 hwif->tf_load(drive, task);
115 switch (task->data_phase) {
116 case TASKFILE_MULTI_OUT:
118 hwif->OUTBSYNC(drive, tf->command, hwif->io_ports.command_addr);
119 ndelay(400); /* FIXME */
120 return pre_task_out_intr(drive, task->rq);
121 case TASKFILE_MULTI_IN:
123 handler = task_in_intr;
125 case TASKFILE_NO_DATA:
127 handler = task_no_data_intr;
128 /* WIN_{SPECIFY,RESTORE,SETMULT} use custom handlers */
129 if (task->tf_flags & IDE_TFLAG_CUSTOM_HANDLER) {
130 switch (tf->command) {
131 case WIN_SPECIFY: handler = set_geometry_intr; break;
132 case WIN_RESTORE: handler = recal_intr; break;
133 case WIN_SETMULT: handler = set_multmode_intr; break;
136 ide_execute_command(drive, tf->command, handler,
137 WAIT_WORSTCASE, NULL);
140 if (task_dma_ok(task) == 0 || drive->using_dma == 0 ||
141 dma_ops->dma_setup(drive))
143 dma_ops->dma_exec_cmd(drive, tf->command);
144 dma_ops->dma_start(drive);
148 EXPORT_SYMBOL_GPL(do_rw_taskfile);
151 * set_multmode_intr() is invoked on completion of a WIN_SETMULT cmd.
153 static ide_startstop_t set_multmode_intr(ide_drive_t *drive)
155 u8 stat = ide_read_status(drive);
157 if (OK_STAT(stat, READY_STAT, BAD_STAT))
158 drive->mult_count = drive->mult_req;
160 drive->mult_req = drive->mult_count = 0;
161 drive->special.b.recalibrate = 1;
162 (void) ide_dump_status(drive, "set_multmode", stat);
168 * set_geometry_intr() is invoked on completion of a WIN_SPECIFY cmd.
170 static ide_startstop_t set_geometry_intr(ide_drive_t *drive)
175 while (((stat = ide_read_status(drive)) & BUSY_STAT) && retries--)
178 if (OK_STAT(stat, READY_STAT, BAD_STAT))
181 if (stat & (ERR_STAT|DRQ_STAT))
182 return ide_error(drive, "set_geometry_intr", stat);
184 BUG_ON(HWGROUP(drive)->handler != NULL);
185 ide_set_handler(drive, &set_geometry_intr, WAIT_WORSTCASE, NULL);
190 * recal_intr() is invoked on completion of a WIN_RESTORE (recalibrate) cmd.
192 static ide_startstop_t recal_intr(ide_drive_t *drive)
194 u8 stat = ide_read_status(drive);
196 if (!OK_STAT(stat, READY_STAT, BAD_STAT))
197 return ide_error(drive, "recal_intr", stat);
202 * Handler for commands without a data phase
204 static ide_startstop_t task_no_data_intr(ide_drive_t *drive)
206 ide_task_t *args = HWGROUP(drive)->rq->special;
209 local_irq_enable_in_hardirq();
210 stat = ide_read_status(drive);
212 if (!OK_STAT(stat, READY_STAT, BAD_STAT))
213 return ide_error(drive, "task_no_data_intr", stat);
214 /* calls ide_end_drive_cmd */
217 ide_end_drive_cmd(drive, stat, ide_read_error(drive));
222 static u8 wait_drive_not_busy(ide_drive_t *drive)
228 * Last sector was transfered, wait until device is ready. This can
229 * take up to 6 ms on some ATAPI devices, so we will wait max 10 ms.
231 for (retries = 0; retries < 1000; retries++) {
232 stat = ide_read_status(drive);
234 if (stat & BUSY_STAT)
240 if (stat & BUSY_STAT)
241 printk(KERN_ERR "%s: drive still BUSY!\n", drive->name);
246 static void ide_pio_sector(ide_drive_t *drive, struct request *rq,
249 ide_hwif_t *hwif = drive->hwif;
250 struct scatterlist *sg = hwif->sg_table;
251 struct scatterlist *cursg = hwif->cursg;
253 #ifdef CONFIG_HIGHMEM
265 page = sg_page(cursg);
266 offset = cursg->offset + hwif->cursg_ofs * SECTOR_SIZE;
268 /* get the current page and offset */
269 page = nth_page(page, (offset >> PAGE_SHIFT));
272 #ifdef CONFIG_HIGHMEM
273 local_irq_save(flags);
275 buf = kmap_atomic(page, KM_BIO_SRC_IRQ) + offset;
280 if ((hwif->cursg_ofs * SECTOR_SIZE) == cursg->length) {
281 hwif->cursg = sg_next(hwif->cursg);
285 /* do the actual data transfer */
287 hwif->output_data(drive, rq, buf, SECTOR_SIZE);
289 hwif->input_data(drive, rq, buf, SECTOR_SIZE);
291 kunmap_atomic(buf, KM_BIO_SRC_IRQ);
292 #ifdef CONFIG_HIGHMEM
293 local_irq_restore(flags);
297 static void ide_pio_multi(ide_drive_t *drive, struct request *rq,
302 nsect = min_t(unsigned int, drive->hwif->nleft, drive->mult_count);
304 ide_pio_sector(drive, rq, write);
307 static void ide_pio_datablock(ide_drive_t *drive, struct request *rq,
310 u8 saved_io_32bit = drive->io_32bit;
312 if (rq->bio) /* fs request */
315 if (rq->cmd_type == REQ_TYPE_ATA_TASKFILE) {
316 ide_task_t *task = rq->special;
318 if (task->tf_flags & IDE_TFLAG_IO_16BIT)
322 touch_softlockup_watchdog();
324 switch (drive->hwif->data_phase) {
325 case TASKFILE_MULTI_IN:
326 case TASKFILE_MULTI_OUT:
327 ide_pio_multi(drive, rq, write);
330 ide_pio_sector(drive, rq, write);
334 drive->io_32bit = saved_io_32bit;
337 static ide_startstop_t task_error(ide_drive_t *drive, struct request *rq,
338 const char *s, u8 stat)
341 ide_hwif_t *hwif = drive->hwif;
342 int sectors = hwif->nsect - hwif->nleft;
344 switch (hwif->data_phase) {
352 case TASKFILE_MULTI_IN:
356 case TASKFILE_MULTI_OUT:
357 sectors -= drive->mult_count;
365 drv = *(ide_driver_t **)rq->rq_disk->private_data;
366 drv->end_request(drive, 1, sectors);
369 return ide_error(drive, s, stat);
372 void task_end_request(ide_drive_t *drive, struct request *rq, u8 stat)
374 if (rq->cmd_type == REQ_TYPE_ATA_TASKFILE) {
375 u8 err = ide_read_error(drive);
377 ide_end_drive_cmd(drive, stat, err);
384 drv = *(ide_driver_t **)rq->rq_disk->private_data;;
385 drv->end_request(drive, 1, rq->nr_sectors);
387 ide_end_request(drive, 1, rq->nr_sectors);
391 * We got an interrupt on a task_in case, but no errors and no DRQ.
393 * It might be a spurious irq (shared irq), but it might be a
394 * command that had no output.
396 static ide_startstop_t task_in_unexpected(ide_drive_t *drive, struct request *rq, u8 stat)
398 /* Command all done? */
399 if (OK_STAT(stat, READY_STAT, BUSY_STAT)) {
400 task_end_request(drive, rq, stat);
404 /* Assume it was a spurious irq */
405 ide_set_handler(drive, &task_in_intr, WAIT_WORSTCASE, NULL);
410 * Handler for command with PIO data-in phase (Read/Read Multiple).
412 static ide_startstop_t task_in_intr(ide_drive_t *drive)
414 ide_hwif_t *hwif = drive->hwif;
415 struct request *rq = HWGROUP(drive)->rq;
416 u8 stat = ide_read_status(drive);
420 return task_error(drive, rq, __func__, stat);
422 /* Didn't want any data? Odd. */
423 if (!(stat & DRQ_STAT))
424 return task_in_unexpected(drive, rq, stat);
426 ide_pio_datablock(drive, rq, 0);
428 /* Are we done? Check status and finish transfer. */
430 stat = wait_drive_not_busy(drive);
431 if (!OK_STAT(stat, 0, BAD_STAT))
432 return task_error(drive, rq, __func__, stat);
433 task_end_request(drive, rq, stat);
437 /* Still data left to transfer. */
438 ide_set_handler(drive, &task_in_intr, WAIT_WORSTCASE, NULL);
444 * Handler for command with PIO data-out phase (Write/Write Multiple).
446 static ide_startstop_t task_out_intr (ide_drive_t *drive)
448 ide_hwif_t *hwif = drive->hwif;
449 struct request *rq = HWGROUP(drive)->rq;
450 u8 stat = ide_read_status(drive);
452 if (!OK_STAT(stat, DRIVE_READY, drive->bad_wstat))
453 return task_error(drive, rq, __func__, stat);
455 /* Deal with unexpected ATA data phase. */
456 if (((stat & DRQ_STAT) == 0) ^ !hwif->nleft)
457 return task_error(drive, rq, __func__, stat);
460 task_end_request(drive, rq, stat);
464 /* Still data left to transfer. */
465 ide_pio_datablock(drive, rq, 1);
466 ide_set_handler(drive, &task_out_intr, WAIT_WORSTCASE, NULL);
471 static ide_startstop_t pre_task_out_intr(ide_drive_t *drive, struct request *rq)
473 ide_startstop_t startstop;
475 if (ide_wait_stat(&startstop, drive, DRQ_STAT,
476 drive->bad_wstat, WAIT_DRQ)) {
477 printk(KERN_ERR "%s: no DRQ after issuing %sWRITE%s\n",
479 drive->hwif->data_phase ? "MULT" : "",
480 drive->addressing ? "_EXT" : "");
487 ide_set_handler(drive, &task_out_intr, WAIT_WORSTCASE, NULL);
488 ide_pio_datablock(drive, rq, 1);
493 int ide_raw_taskfile(ide_drive_t *drive, ide_task_t *task, u8 *buf, u16 nsect)
497 blk_rq_init(NULL, &rq);
498 rq.cmd_type = REQ_TYPE_ATA_TASKFILE;
502 * (ks) We transfer currently only whole sectors.
503 * This is suffient for now. But, it would be great,
504 * if we would find a solution to transfer any size.
505 * To support special commands like READ LONG.
507 rq.hard_nr_sectors = rq.nr_sectors = nsect;
508 rq.hard_cur_sectors = rq.current_nr_sectors = nsect;
510 if (task->tf_flags & IDE_TFLAG_WRITE)
511 rq.cmd_flags |= REQ_RW;
516 return ide_do_drive_cmd(drive, &rq, ide_wait);
519 EXPORT_SYMBOL(ide_raw_taskfile);
521 int ide_no_data_taskfile(ide_drive_t *drive, ide_task_t *task)
523 task->data_phase = TASKFILE_NO_DATA;
525 return ide_raw_taskfile(drive, task, NULL, 0);
527 EXPORT_SYMBOL_GPL(ide_no_data_taskfile);
529 #ifdef CONFIG_IDE_TASK_IOCTL
530 int ide_taskfile_ioctl (ide_drive_t *drive, unsigned int cmd, unsigned long arg)
532 ide_task_request_t *req_task;
538 int tasksize = sizeof(struct ide_task_request_s);
539 unsigned int taskin = 0;
540 unsigned int taskout = 0;
542 char __user *buf = (char __user *)arg;
544 // printk("IDE Taskfile ...\n");
546 req_task = kzalloc(tasksize, GFP_KERNEL);
547 if (req_task == NULL) return -ENOMEM;
548 if (copy_from_user(req_task, buf, tasksize)) {
553 taskout = req_task->out_size;
554 taskin = req_task->in_size;
556 if (taskin > 65536 || taskout > 65536) {
562 int outtotal = tasksize;
563 outbuf = kzalloc(taskout, GFP_KERNEL);
564 if (outbuf == NULL) {
568 if (copy_from_user(outbuf, buf + outtotal, taskout)) {
575 int intotal = tasksize + taskout;
576 inbuf = kzalloc(taskin, GFP_KERNEL);
581 if (copy_from_user(inbuf, buf + intotal, taskin)) {
587 memset(&args, 0, sizeof(ide_task_t));
589 memcpy(&args.tf_array[0], req_task->hob_ports, HDIO_DRIVE_HOB_HDR_SIZE - 2);
590 memcpy(&args.tf_array[6], req_task->io_ports, HDIO_DRIVE_TASK_HDR_SIZE);
592 args.data_phase = req_task->data_phase;
594 args.tf_flags = IDE_TFLAG_IO_16BIT | IDE_TFLAG_DEVICE |
596 if (drive->addressing == 1)
597 args.tf_flags |= (IDE_TFLAG_LBA48 | IDE_TFLAG_IN_HOB);
599 if (req_task->out_flags.all) {
600 args.tf_flags |= IDE_TFLAG_FLAGGED;
602 if (req_task->out_flags.b.data)
603 args.tf_flags |= IDE_TFLAG_OUT_DATA;
605 if (req_task->out_flags.b.nsector_hob)
606 args.tf_flags |= IDE_TFLAG_OUT_HOB_NSECT;
607 if (req_task->out_flags.b.sector_hob)
608 args.tf_flags |= IDE_TFLAG_OUT_HOB_LBAL;
609 if (req_task->out_flags.b.lcyl_hob)
610 args.tf_flags |= IDE_TFLAG_OUT_HOB_LBAM;
611 if (req_task->out_flags.b.hcyl_hob)
612 args.tf_flags |= IDE_TFLAG_OUT_HOB_LBAH;
614 if (req_task->out_flags.b.error_feature)
615 args.tf_flags |= IDE_TFLAG_OUT_FEATURE;
616 if (req_task->out_flags.b.nsector)
617 args.tf_flags |= IDE_TFLAG_OUT_NSECT;
618 if (req_task->out_flags.b.sector)
619 args.tf_flags |= IDE_TFLAG_OUT_LBAL;
620 if (req_task->out_flags.b.lcyl)
621 args.tf_flags |= IDE_TFLAG_OUT_LBAM;
622 if (req_task->out_flags.b.hcyl)
623 args.tf_flags |= IDE_TFLAG_OUT_LBAH;
625 args.tf_flags |= IDE_TFLAG_OUT_TF;
626 if (args.tf_flags & IDE_TFLAG_LBA48)
627 args.tf_flags |= IDE_TFLAG_OUT_HOB;
630 if (req_task->in_flags.b.data)
631 args.tf_flags |= IDE_TFLAG_IN_DATA;
633 switch(req_task->data_phase) {
634 case TASKFILE_MULTI_OUT:
635 if (!drive->mult_count) {
636 /* (hs): give up if multcount is not set */
637 printk(KERN_ERR "%s: %s Multimode Write " \
638 "multcount is not set\n",
639 drive->name, __func__);
646 case TASKFILE_OUT_DMAQ:
647 case TASKFILE_OUT_DMA:
648 nsect = taskout / SECTOR_SIZE;
651 case TASKFILE_MULTI_IN:
652 if (!drive->mult_count) {
653 /* (hs): give up if multcount is not set */
654 printk(KERN_ERR "%s: %s Multimode Read failure " \
655 "multcount is not set\n",
656 drive->name, __func__);
663 case TASKFILE_IN_DMAQ:
664 case TASKFILE_IN_DMA:
665 nsect = taskin / SECTOR_SIZE;
668 case TASKFILE_NO_DATA:
675 if (req_task->req_cmd == IDE_DRIVE_TASK_NO_DATA)
678 nsect = (args.tf.hob_nsect << 8) | args.tf.nsect;
681 printk(KERN_ERR "%s: in/out command without data\n",
688 if (req_task->req_cmd == IDE_DRIVE_TASK_RAW_WRITE)
689 args.tf_flags |= IDE_TFLAG_WRITE;
691 err = ide_raw_taskfile(drive, &args, data_buf, nsect);
693 memcpy(req_task->hob_ports, &args.tf_array[0], HDIO_DRIVE_HOB_HDR_SIZE - 2);
694 memcpy(req_task->io_ports, &args.tf_array[6], HDIO_DRIVE_TASK_HDR_SIZE);
696 if ((args.tf_flags & IDE_TFLAG_FLAGGED_SET_IN_FLAGS) &&
697 req_task->in_flags.all == 0) {
698 req_task->in_flags.all = IDE_TASKFILE_STD_IN_FLAGS;
699 if (drive->addressing == 1)
700 req_task->in_flags.all |= (IDE_HOB_STD_IN_FLAGS << 8);
703 if (copy_to_user(buf, req_task, tasksize)) {
708 int outtotal = tasksize;
709 if (copy_to_user(buf + outtotal, outbuf, taskout)) {
715 int intotal = tasksize + taskout;
716 if (copy_to_user(buf + intotal, inbuf, taskin)) {
726 // printk("IDE Taskfile ioctl ended. rc = %i\n", err);
732 int ide_cmd_ioctl (ide_drive_t *drive, unsigned int cmd, unsigned long arg)
735 int bufsize = 0, err = 0;
736 u8 args[4], xfer_rate = 0;
738 struct ide_taskfile *tf = &tfargs.tf;
739 struct hd_driveid *id = drive->id;
741 if (NULL == (void *) arg) {
744 ide_init_drive_cmd(&rq);
745 rq.cmd_type = REQ_TYPE_ATA_TASKFILE;
747 return ide_do_drive_cmd(drive, &rq, ide_wait);
750 if (copy_from_user(args, (void __user *)arg, 4))
753 memset(&tfargs, 0, sizeof(ide_task_t));
754 tf->feature = args[2];
755 if (args[0] == WIN_SMART) {
760 tfargs.tf_flags = IDE_TFLAG_OUT_TF | IDE_TFLAG_IN_NSECT;
763 tfargs.tf_flags = IDE_TFLAG_OUT_FEATURE |
764 IDE_TFLAG_OUT_NSECT | IDE_TFLAG_IN_NSECT;
766 tf->command = args[0];
767 tfargs.data_phase = args[3] ? TASKFILE_IN : TASKFILE_NO_DATA;
770 tfargs.tf_flags |= IDE_TFLAG_IO_16BIT;
771 bufsize = SECTOR_WORDS * 4 * args[3];
772 buf = kzalloc(bufsize, GFP_KERNEL);
777 if (tf->command == WIN_SETFEATURES &&
778 tf->feature == SETFEATURES_XFER &&
779 tf->nsect >= XFER_SW_DMA_0 &&
780 (id->dma_ultra || id->dma_mword || id->dma_1word)) {
782 if (tf->nsect > XFER_UDMA_2 && !eighty_ninty_three(drive)) {
783 printk(KERN_WARNING "%s: UDMA speeds >UDMA33 cannot "
784 "be set\n", drive->name);
789 err = ide_raw_taskfile(drive, &tfargs, buf, args[3]);
791 args[0] = tf->status;
795 if (!err && xfer_rate) {
796 /* active-retuning-calls future */
797 ide_set_xfer_rate(drive, xfer_rate);
798 ide_driveid_update(drive);
801 if (copy_to_user((void __user *)arg, &args, 4))
804 if (copy_to_user((void __user *)(arg + 4), buf, bufsize))
811 int ide_task_ioctl (ide_drive_t *drive, unsigned int cmd, unsigned long arg)
813 void __user *p = (void __user *)arg;
818 if (copy_from_user(args, p, 7))
821 memset(&task, 0, sizeof(task));
822 memcpy(&task.tf_array[7], &args[1], 6);
823 task.tf.command = args[0];
824 task.tf_flags = IDE_TFLAG_TF | IDE_TFLAG_DEVICE;
826 err = ide_no_data_taskfile(drive, &task);
828 args[0] = task.tf.command;
829 memcpy(&args[1], &task.tf_array[7], 6);
831 if (copy_to_user(p, args, 7))