Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Bartlomiej Zolnierkiewicz | 59bca8c | 2008-02-01 23:09:33 +0100 | [diff] [blame] | 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 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5 | * IBM Storage Technology Division |
Bartlomiej Zolnierkiewicz | 59bca8c | 2008-02-01 23:09:33 +0100 | [diff] [blame] | 6 | * Copyright (C) 2003-2004, 2007 Bartlomiej Zolnierkiewicz |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | * |
| 8 | * The big the bad and the ugly. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | */ |
| 10 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | #include <linux/types.h> |
| 12 | #include <linux/string.h> |
| 13 | #include <linux/kernel.h> |
Andrew Morton | 651c29a | 2006-02-15 15:17:37 -0800 | [diff] [blame] | 14 | #include <linux/sched.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | #include <linux/interrupt.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | #include <linux/errno.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | #include <linux/delay.h> |
| 19 | #include <linux/hdreg.h> |
| 20 | #include <linux/ide.h> |
Jens Axboe | 55c16a7 | 2007-07-25 08:13:56 +0200 | [diff] [blame] | 21 | #include <linux/scatterlist.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | #include <asm/uaccess.h> |
| 24 | #include <asm/io.h> |
| 25 | |
Bartlomiej Zolnierkiewicz | 089c5c7 | 2008-04-28 23:44:39 +0200 | [diff] [blame] | 26 | void ide_tf_dump(const char *s, struct ide_taskfile *tf) |
| 27 | { |
| 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 |
| 38 | } |
| 39 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | int taskfile_lib_get_identify (ide_drive_t *drive, u8 *buf) |
| 41 | { |
| 42 | ide_task_t args; |
Bartlomiej Zolnierkiewicz | 650d841 | 2008-01-25 22:17:06 +0100 | [diff] [blame] | 43 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | memset(&args, 0, sizeof(ide_task_t)); |
Bartlomiej Zolnierkiewicz | 650d841 | 2008-01-25 22:17:06 +0100 | [diff] [blame] | 45 | args.tf.nsect = 0x01; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | if (drive->media == ide_disk) |
Bartlomiej Zolnierkiewicz | 650d841 | 2008-01-25 22:17:06 +0100 | [diff] [blame] | 47 | args.tf.command = WIN_IDENTIFY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | else |
Bartlomiej Zolnierkiewicz | 650d841 | 2008-01-25 22:17:06 +0100 | [diff] [blame] | 49 | args.tf.command = WIN_PIDENTIFY; |
Bartlomiej Zolnierkiewicz | 657cc1a | 2008-01-26 20:13:10 +0100 | [diff] [blame] | 50 | args.tf_flags = IDE_TFLAG_TF | IDE_TFLAG_DEVICE; |
Bartlomiej Zolnierkiewicz | ac026ff | 2008-01-25 22:17:14 +0100 | [diff] [blame] | 51 | args.data_phase = TASKFILE_IN; |
Bartlomiej Zolnierkiewicz | ac026ff | 2008-01-25 22:17:14 +0100 | [diff] [blame] | 52 | return ide_raw_taskfile(drive, &args, buf, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | } |
| 54 | |
Bartlomiej Zolnierkiewicz | 1192e52 | 2008-01-25 22:17:16 +0100 | [diff] [blame] | 55 | static ide_startstop_t task_no_data_intr(ide_drive_t *); |
Bartlomiej Zolnierkiewicz | 57d7366 | 2008-01-25 22:17:16 +0100 | [diff] [blame] | 56 | static ide_startstop_t set_geometry_intr(ide_drive_t *); |
| 57 | static ide_startstop_t recal_intr(ide_drive_t *); |
| 58 | static ide_startstop_t set_multmode_intr(ide_drive_t *); |
Bartlomiej Zolnierkiewicz | f6e29e3 | 2008-01-25 22:17:16 +0100 | [diff] [blame] | 59 | static ide_startstop_t pre_task_out_intr(ide_drive_t *, struct request *); |
| 60 | static ide_startstop_t task_in_intr(ide_drive_t *); |
Bartlomiej Zolnierkiewicz | 1192e52 | 2008-01-25 22:17:16 +0100 | [diff] [blame] | 61 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | ide_startstop_t do_rw_taskfile (ide_drive_t *drive, ide_task_t *task) |
| 63 | { |
| 64 | ide_hwif_t *hwif = HWIF(drive); |
Bartlomiej Zolnierkiewicz | 650d841 | 2008-01-25 22:17:06 +0100 | [diff] [blame] | 65 | struct ide_taskfile *tf = &task->tf; |
Bartlomiej Zolnierkiewicz | 57d7366 | 2008-01-25 22:17:16 +0100 | [diff] [blame] | 66 | ide_handler_t *handler = NULL; |
Bartlomiej Zolnierkiewicz | 374e042 | 2008-07-23 19:55:56 +0200 | [diff] [blame^] | 67 | const struct ide_tp_ops *tp_ops = hwif->tp_ops; |
Bartlomiej Zolnierkiewicz | f37afda | 2008-04-26 22:25:24 +0200 | [diff] [blame] | 68 | const struct ide_dma_ops *dma_ops = hwif->dma_ops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | |
Bartlomiej Zolnierkiewicz | 1edee60 | 2008-01-25 22:17:15 +0100 | [diff] [blame] | 70 | if (task->data_phase == TASKFILE_MULTI_IN || |
| 71 | task->data_phase == TASKFILE_MULTI_OUT) { |
| 72 | if (!drive->mult_count) { |
| 73 | printk(KERN_ERR "%s: multimode not set!\n", |
| 74 | drive->name); |
| 75 | return ide_stopped; |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | if (task->tf_flags & IDE_TFLAG_FLAGGED) |
| 80 | task->tf_flags |= IDE_TFLAG_FLAGGED_SET_IN_FLAGS; |
| 81 | |
Bartlomiej Zolnierkiewicz | 089c5c7 | 2008-04-28 23:44:39 +0200 | [diff] [blame] | 82 | if ((task->tf_flags & IDE_TFLAG_DMA_PIO_FALLBACK) == 0) { |
| 83 | ide_tf_dump(drive->name, tf); |
Bartlomiej Zolnierkiewicz | 374e042 | 2008-07-23 19:55:56 +0200 | [diff] [blame^] | 84 | tp_ops->set_irq(hwif, 1); |
Bartlomiej Zolnierkiewicz | ed4af48 | 2008-07-15 21:21:48 +0200 | [diff] [blame] | 85 | SELECT_MASK(drive, 0); |
Bartlomiej Zolnierkiewicz | 374e042 | 2008-07-23 19:55:56 +0200 | [diff] [blame^] | 86 | tp_ops->tf_load(drive, task); |
Bartlomiej Zolnierkiewicz | 089c5c7 | 2008-04-28 23:44:39 +0200 | [diff] [blame] | 87 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | |
Bartlomiej Zolnierkiewicz | 10d9015 | 2008-01-25 22:17:16 +0100 | [diff] [blame] | 89 | switch (task->data_phase) { |
| 90 | case TASKFILE_MULTI_OUT: |
| 91 | case TASKFILE_OUT: |
Bartlomiej Zolnierkiewicz | 374e042 | 2008-07-23 19:55:56 +0200 | [diff] [blame^] | 92 | tp_ops->exec_command(hwif, tf->command); |
Bartlomiej Zolnierkiewicz | 10d9015 | 2008-01-25 22:17:16 +0100 | [diff] [blame] | 93 | ndelay(400); /* FIXME */ |
| 94 | return pre_task_out_intr(drive, task->rq); |
| 95 | case TASKFILE_MULTI_IN: |
| 96 | case TASKFILE_IN: |
Bartlomiej Zolnierkiewicz | 57d7366 | 2008-01-25 22:17:16 +0100 | [diff] [blame] | 97 | handler = task_in_intr; |
Bartlomiej Zolnierkiewicz | 1192e52 | 2008-01-25 22:17:16 +0100 | [diff] [blame] | 98 | /* fall-through */ |
Bartlomiej Zolnierkiewicz | 10d9015 | 2008-01-25 22:17:16 +0100 | [diff] [blame] | 99 | case TASKFILE_NO_DATA: |
Bartlomiej Zolnierkiewicz | 57d7366 | 2008-01-25 22:17:16 +0100 | [diff] [blame] | 100 | if (handler == NULL) |
| 101 | handler = task_no_data_intr; |
Bartlomiej Zolnierkiewicz | 1192e52 | 2008-01-25 22:17:16 +0100 | [diff] [blame] | 102 | /* WIN_{SPECIFY,RESTORE,SETMULT} use custom handlers */ |
Bartlomiej Zolnierkiewicz | 57d7366 | 2008-01-25 22:17:16 +0100 | [diff] [blame] | 103 | if (task->tf_flags & IDE_TFLAG_CUSTOM_HANDLER) { |
| 104 | switch (tf->command) { |
| 105 | case WIN_SPECIFY: handler = set_geometry_intr; break; |
| 106 | case WIN_RESTORE: handler = recal_intr; break; |
| 107 | case WIN_SETMULT: handler = set_multmode_intr; break; |
| 108 | } |
| 109 | } |
| 110 | ide_execute_command(drive, tf->command, handler, |
| 111 | WAIT_WORSTCASE, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | return ide_started; |
Bartlomiej Zolnierkiewicz | 10d9015 | 2008-01-25 22:17:16 +0100 | [diff] [blame] | 113 | default: |
Bartlomiej Zolnierkiewicz | 07fe69d | 2008-07-16 20:33:43 +0200 | [diff] [blame] | 114 | if (drive->using_dma == 0 || dma_ops->dma_setup(drive)) |
Bartlomiej Zolnierkiewicz | 10d9015 | 2008-01-25 22:17:16 +0100 | [diff] [blame] | 115 | return ide_stopped; |
Bartlomiej Zolnierkiewicz | 5e37bdc | 2008-04-26 22:25:24 +0200 | [diff] [blame] | 116 | dma_ops->dma_exec_cmd(drive, tf->command); |
| 117 | dma_ops->dma_start(drive); |
Bartlomiej Zolnierkiewicz | 74095a9 | 2008-01-25 22:17:07 +0100 | [diff] [blame] | 118 | return ide_started; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | } |
Bartlomiej Zolnierkiewicz | f6e29e3 | 2008-01-25 22:17:16 +0100 | [diff] [blame] | 121 | EXPORT_SYMBOL_GPL(do_rw_taskfile); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | /* |
| 124 | * set_multmode_intr() is invoked on completion of a WIN_SETMULT cmd. |
| 125 | */ |
Bartlomiej Zolnierkiewicz | 57d7366 | 2008-01-25 22:17:16 +0100 | [diff] [blame] | 126 | static ide_startstop_t set_multmode_intr(ide_drive_t *drive) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | { |
Bartlomiej Zolnierkiewicz | b73c7ee | 2008-07-23 19:55:52 +0200 | [diff] [blame] | 128 | ide_hwif_t *hwif = drive->hwif; |
Bartlomiej Zolnierkiewicz | 374e042 | 2008-07-23 19:55:56 +0200 | [diff] [blame^] | 129 | u8 stat = hwif->tp_ops->read_status(hwif); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | |
Bartlomiej Zolnierkiewicz | c47137a | 2008-02-06 02:57:51 +0100 | [diff] [blame] | 131 | if (OK_STAT(stat, READY_STAT, BAD_STAT)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | drive->mult_count = drive->mult_req; |
Bartlomiej Zolnierkiewicz | c47137a | 2008-02-06 02:57:51 +0100 | [diff] [blame] | 133 | else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | drive->mult_req = drive->mult_count = 0; |
| 135 | drive->special.b.recalibrate = 1; |
| 136 | (void) ide_dump_status(drive, "set_multmode", stat); |
| 137 | } |
| 138 | return ide_stopped; |
| 139 | } |
| 140 | |
| 141 | /* |
| 142 | * set_geometry_intr() is invoked on completion of a WIN_SPECIFY cmd. |
| 143 | */ |
Bartlomiej Zolnierkiewicz | 57d7366 | 2008-01-25 22:17:16 +0100 | [diff] [blame] | 144 | static ide_startstop_t set_geometry_intr(ide_drive_t *drive) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | { |
Bartlomiej Zolnierkiewicz | b73c7ee | 2008-07-23 19:55:52 +0200 | [diff] [blame] | 146 | ide_hwif_t *hwif = drive->hwif; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | int retries = 5; |
| 148 | u8 stat; |
| 149 | |
Bartlomiej Zolnierkiewicz | 374e042 | 2008-07-23 19:55:56 +0200 | [diff] [blame^] | 150 | while (1) { |
| 151 | stat = hwif->tp_ops->read_status(hwif); |
| 152 | if ((stat & BUSY_STAT) == 0 || retries-- == 0) |
| 153 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | udelay(10); |
Bartlomiej Zolnierkiewicz | 374e042 | 2008-07-23 19:55:56 +0200 | [diff] [blame^] | 155 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | |
| 157 | if (OK_STAT(stat, READY_STAT, BAD_STAT)) |
| 158 | return ide_stopped; |
| 159 | |
| 160 | if (stat & (ERR_STAT|DRQ_STAT)) |
| 161 | return ide_error(drive, "set_geometry_intr", stat); |
| 162 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | ide_set_handler(drive, &set_geometry_intr, WAIT_WORSTCASE, NULL); |
| 164 | return ide_started; |
| 165 | } |
| 166 | |
| 167 | /* |
| 168 | * recal_intr() is invoked on completion of a WIN_RESTORE (recalibrate) cmd. |
| 169 | */ |
Bartlomiej Zolnierkiewicz | 57d7366 | 2008-01-25 22:17:16 +0100 | [diff] [blame] | 170 | static ide_startstop_t recal_intr(ide_drive_t *drive) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | { |
Bartlomiej Zolnierkiewicz | b73c7ee | 2008-07-23 19:55:52 +0200 | [diff] [blame] | 172 | ide_hwif_t *hwif = drive->hwif; |
Bartlomiej Zolnierkiewicz | 374e042 | 2008-07-23 19:55:56 +0200 | [diff] [blame^] | 173 | u8 stat = hwif->tp_ops->read_status(hwif); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | |
Bartlomiej Zolnierkiewicz | c47137a | 2008-02-06 02:57:51 +0100 | [diff] [blame] | 175 | if (!OK_STAT(stat, READY_STAT, BAD_STAT)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | return ide_error(drive, "recal_intr", stat); |
| 177 | return ide_stopped; |
| 178 | } |
| 179 | |
| 180 | /* |
| 181 | * Handler for commands without a data phase |
| 182 | */ |
Bartlomiej Zolnierkiewicz | 1192e52 | 2008-01-25 22:17:16 +0100 | [diff] [blame] | 183 | static ide_startstop_t task_no_data_intr(ide_drive_t *drive) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 184 | { |
Bartlomiej Zolnierkiewicz | b73c7ee | 2008-07-23 19:55:52 +0200 | [diff] [blame] | 185 | ide_hwif_t *hwif = drive->hwif; |
| 186 | ide_task_t *args = hwif->hwgroup->rq->special; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | u8 stat; |
| 188 | |
Ingo Molnar | 366c7f5 | 2006-07-03 00:25:25 -0700 | [diff] [blame] | 189 | local_irq_enable_in_hardirq(); |
Bartlomiej Zolnierkiewicz | 374e042 | 2008-07-23 19:55:56 +0200 | [diff] [blame^] | 190 | stat = hwif->tp_ops->read_status(hwif); |
Bartlomiej Zolnierkiewicz | c47137a | 2008-02-06 02:57:51 +0100 | [diff] [blame] | 191 | |
| 192 | if (!OK_STAT(stat, READY_STAT, BAD_STAT)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 193 | return ide_error(drive, "task_no_data_intr", stat); |
| 194 | /* calls ide_end_drive_cmd */ |
Bartlomiej Zolnierkiewicz | c47137a | 2008-02-06 02:57:51 +0100 | [diff] [blame] | 195 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | if (args) |
Bartlomiej Zolnierkiewicz | 64a57fe | 2008-02-06 02:57:51 +0100 | [diff] [blame] | 197 | ide_end_drive_cmd(drive, stat, ide_read_error(drive)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | |
| 199 | return ide_stopped; |
| 200 | } |
| 201 | |
Adrian Bunk | da6f4c7 | 2008-02-01 23:09:16 +0100 | [diff] [blame] | 202 | static u8 wait_drive_not_busy(ide_drive_t *drive) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | { |
Bartlomiej Zolnierkiewicz | b73c7ee | 2008-07-23 19:55:52 +0200 | [diff] [blame] | 204 | ide_hwif_t *hwif = drive->hwif; |
Masatake YAMATO | b42fa13 | 2007-07-03 22:28:34 +0200 | [diff] [blame] | 205 | int retries; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 206 | u8 stat; |
| 207 | |
| 208 | /* |
Bartlomiej Zolnierkiewicz | f54feaf | 2008-06-20 20:53:33 +0200 | [diff] [blame] | 209 | * Last sector was transfered, wait until device is ready. This can |
| 210 | * take up to 6 ms on some ATAPI devices, so we will wait max 10 ms. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | */ |
Bartlomiej Zolnierkiewicz | f54feaf | 2008-06-20 20:53:33 +0200 | [diff] [blame] | 212 | for (retries = 0; retries < 1000; retries++) { |
Bartlomiej Zolnierkiewicz | 374e042 | 2008-07-23 19:55:56 +0200 | [diff] [blame^] | 213 | stat = hwif->tp_ops->read_status(hwif); |
Bartlomiej Zolnierkiewicz | c47137a | 2008-02-06 02:57:51 +0100 | [diff] [blame] | 214 | |
| 215 | if (stat & BUSY_STAT) |
Masatake YAMATO | b42fa13 | 2007-07-03 22:28:34 +0200 | [diff] [blame] | 216 | udelay(10); |
| 217 | else |
| 218 | break; |
| 219 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 220 | |
Masatake YAMATO | b42fa13 | 2007-07-03 22:28:34 +0200 | [diff] [blame] | 221 | if (stat & BUSY_STAT) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 222 | printk(KERN_ERR "%s: drive still BUSY!\n", drive->name); |
| 223 | |
| 224 | return stat; |
| 225 | } |
| 226 | |
Bartlomiej Zolnierkiewicz | 92d3ab2 | 2008-04-28 23:44:36 +0200 | [diff] [blame] | 227 | static void ide_pio_sector(ide_drive_t *drive, struct request *rq, |
| 228 | unsigned int write) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | { |
| 230 | ide_hwif_t *hwif = drive->hwif; |
| 231 | struct scatterlist *sg = hwif->sg_table; |
Jens Axboe | 55c16a7 | 2007-07-25 08:13:56 +0200 | [diff] [blame] | 232 | struct scatterlist *cursg = hwif->cursg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 233 | struct page *page; |
| 234 | #ifdef CONFIG_HIGHMEM |
| 235 | unsigned long flags; |
| 236 | #endif |
| 237 | unsigned int offset; |
| 238 | u8 *buf; |
| 239 | |
Jens Axboe | 55c16a7 | 2007-07-25 08:13:56 +0200 | [diff] [blame] | 240 | cursg = hwif->cursg; |
| 241 | if (!cursg) { |
| 242 | cursg = sg; |
| 243 | hwif->cursg = sg; |
| 244 | } |
| 245 | |
Jens Axboe | 45711f1 | 2007-10-22 21:19:53 +0200 | [diff] [blame] | 246 | page = sg_page(cursg); |
Jens Axboe | 55c16a7 | 2007-07-25 08:13:56 +0200 | [diff] [blame] | 247 | offset = cursg->offset + hwif->cursg_ofs * SECTOR_SIZE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 248 | |
| 249 | /* get the current page and offset */ |
| 250 | page = nth_page(page, (offset >> PAGE_SHIFT)); |
| 251 | offset %= PAGE_SIZE; |
| 252 | |
| 253 | #ifdef CONFIG_HIGHMEM |
| 254 | local_irq_save(flags); |
| 255 | #endif |
| 256 | buf = kmap_atomic(page, KM_BIO_SRC_IRQ) + offset; |
| 257 | |
| 258 | hwif->nleft--; |
| 259 | hwif->cursg_ofs++; |
| 260 | |
Jens Axboe | 55c16a7 | 2007-07-25 08:13:56 +0200 | [diff] [blame] | 261 | if ((hwif->cursg_ofs * SECTOR_SIZE) == cursg->length) { |
| 262 | hwif->cursg = sg_next(hwif->cursg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 263 | hwif->cursg_ofs = 0; |
| 264 | } |
| 265 | |
| 266 | /* do the actual data transfer */ |
| 267 | if (write) |
Bartlomiej Zolnierkiewicz | 374e042 | 2008-07-23 19:55:56 +0200 | [diff] [blame^] | 268 | hwif->tp_ops->output_data(drive, rq, buf, SECTOR_SIZE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 269 | else |
Bartlomiej Zolnierkiewicz | 374e042 | 2008-07-23 19:55:56 +0200 | [diff] [blame^] | 270 | hwif->tp_ops->input_data(drive, rq, buf, SECTOR_SIZE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 271 | |
| 272 | kunmap_atomic(buf, KM_BIO_SRC_IRQ); |
| 273 | #ifdef CONFIG_HIGHMEM |
| 274 | local_irq_restore(flags); |
| 275 | #endif |
| 276 | } |
| 277 | |
Bartlomiej Zolnierkiewicz | 92d3ab2 | 2008-04-28 23:44:36 +0200 | [diff] [blame] | 278 | static void ide_pio_multi(ide_drive_t *drive, struct request *rq, |
| 279 | unsigned int write) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 280 | { |
| 281 | unsigned int nsect; |
| 282 | |
| 283 | nsect = min_t(unsigned int, drive->hwif->nleft, drive->mult_count); |
| 284 | while (nsect--) |
Bartlomiej Zolnierkiewicz | 92d3ab2 | 2008-04-28 23:44:36 +0200 | [diff] [blame] | 285 | ide_pio_sector(drive, rq, write); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 286 | } |
| 287 | |
Arjan van de Ven | 858119e | 2006-01-14 13:20:43 -0800 | [diff] [blame] | 288 | static void ide_pio_datablock(ide_drive_t *drive, struct request *rq, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 289 | unsigned int write) |
| 290 | { |
Tejun Heo | 35cf2b9 | 2008-01-26 20:13:10 +0100 | [diff] [blame] | 291 | u8 saved_io_32bit = drive->io_32bit; |
| 292 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 293 | if (rq->bio) /* fs request */ |
| 294 | rq->errors = 0; |
| 295 | |
Tejun Heo | 35cf2b9 | 2008-01-26 20:13:10 +0100 | [diff] [blame] | 296 | if (rq->cmd_type == REQ_TYPE_ATA_TASKFILE) { |
| 297 | ide_task_t *task = rq->special; |
| 298 | |
| 299 | if (task->tf_flags & IDE_TFLAG_IO_16BIT) |
| 300 | drive->io_32bit = 0; |
| 301 | } |
| 302 | |
Andrew Morton | 651c29a | 2006-02-15 15:17:37 -0800 | [diff] [blame] | 303 | touch_softlockup_watchdog(); |
| 304 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 305 | switch (drive->hwif->data_phase) { |
| 306 | case TASKFILE_MULTI_IN: |
| 307 | case TASKFILE_MULTI_OUT: |
Bartlomiej Zolnierkiewicz | 92d3ab2 | 2008-04-28 23:44:36 +0200 | [diff] [blame] | 308 | ide_pio_multi(drive, rq, write); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 309 | break; |
| 310 | default: |
Bartlomiej Zolnierkiewicz | 92d3ab2 | 2008-04-28 23:44:36 +0200 | [diff] [blame] | 311 | ide_pio_sector(drive, rq, write); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 312 | break; |
| 313 | } |
Tejun Heo | 35cf2b9 | 2008-01-26 20:13:10 +0100 | [diff] [blame] | 314 | |
| 315 | drive->io_32bit = saved_io_32bit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | static ide_startstop_t task_error(ide_drive_t *drive, struct request *rq, |
| 319 | const char *s, u8 stat) |
| 320 | { |
| 321 | if (rq->bio) { |
| 322 | ide_hwif_t *hwif = drive->hwif; |
| 323 | int sectors = hwif->nsect - hwif->nleft; |
| 324 | |
| 325 | switch (hwif->data_phase) { |
| 326 | case TASKFILE_IN: |
| 327 | if (hwif->nleft) |
| 328 | break; |
| 329 | /* fall through */ |
| 330 | case TASKFILE_OUT: |
| 331 | sectors--; |
| 332 | break; |
| 333 | case TASKFILE_MULTI_IN: |
| 334 | if (hwif->nleft) |
| 335 | break; |
| 336 | /* fall through */ |
| 337 | case TASKFILE_MULTI_OUT: |
| 338 | sectors -= drive->mult_count; |
| 339 | default: |
| 340 | break; |
| 341 | } |
| 342 | |
| 343 | if (sectors > 0) { |
| 344 | ide_driver_t *drv; |
| 345 | |
| 346 | drv = *(ide_driver_t **)rq->rq_disk->private_data; |
| 347 | drv->end_request(drive, 1, sectors); |
| 348 | } |
| 349 | } |
| 350 | return ide_error(drive, s, stat); |
| 351 | } |
| 352 | |
Tejun Heo | 4d7a984 | 2008-01-26 20:13:11 +0100 | [diff] [blame] | 353 | void task_end_request(ide_drive_t *drive, struct request *rq, u8 stat) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 354 | { |
Jens Axboe | 4aff5e2 | 2006-08-10 08:44:47 +0200 | [diff] [blame] | 355 | if (rq->cmd_type == REQ_TYPE_ATA_TASKFILE) { |
Bartlomiej Zolnierkiewicz | 64a57fe | 2008-02-06 02:57:51 +0100 | [diff] [blame] | 356 | u8 err = ide_read_error(drive); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 357 | |
Tejun Heo | 4d7a984 | 2008-01-26 20:13:11 +0100 | [diff] [blame] | 358 | ide_end_drive_cmd(drive, stat, err); |
| 359 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 360 | } |
| 361 | |
Richard Purdie | 03731fb | 2006-03-31 02:31:15 -0800 | [diff] [blame] | 362 | if (rq->rq_disk) { |
| 363 | ide_driver_t *drv; |
| 364 | |
| 365 | drv = *(ide_driver_t **)rq->rq_disk->private_data;; |
Bartlomiej Zolnierkiewicz | 79f21b8 | 2008-01-26 20:13:11 +0100 | [diff] [blame] | 366 | drv->end_request(drive, 1, rq->nr_sectors); |
Richard Purdie | 03731fb | 2006-03-31 02:31:15 -0800 | [diff] [blame] | 367 | } else |
Bartlomiej Zolnierkiewicz | 79f21b8 | 2008-01-26 20:13:11 +0100 | [diff] [blame] | 368 | ide_end_request(drive, 1, rq->nr_sectors); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 369 | } |
| 370 | |
| 371 | /* |
Linus Torvalds | 6c3c315 | 2008-03-18 21:26:24 -0700 | [diff] [blame] | 372 | * We got an interrupt on a task_in case, but no errors and no DRQ. |
| 373 | * |
| 374 | * It might be a spurious irq (shared irq), but it might be a |
| 375 | * command that had no output. |
| 376 | */ |
| 377 | static ide_startstop_t task_in_unexpected(ide_drive_t *drive, struct request *rq, u8 stat) |
| 378 | { |
| 379 | /* Command all done? */ |
| 380 | if (OK_STAT(stat, READY_STAT, BUSY_STAT)) { |
| 381 | task_end_request(drive, rq, stat); |
| 382 | return ide_stopped; |
| 383 | } |
| 384 | |
| 385 | /* Assume it was a spurious irq */ |
| 386 | ide_set_handler(drive, &task_in_intr, WAIT_WORSTCASE, NULL); |
| 387 | return ide_started; |
| 388 | } |
| 389 | |
| 390 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 391 | * Handler for command with PIO data-in phase (Read/Read Multiple). |
| 392 | */ |
Bartlomiej Zolnierkiewicz | f6e29e3 | 2008-01-25 22:17:16 +0100 | [diff] [blame] | 393 | static ide_startstop_t task_in_intr(ide_drive_t *drive) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 394 | { |
| 395 | ide_hwif_t *hwif = drive->hwif; |
Bartlomiej Zolnierkiewicz | b73c7ee | 2008-07-23 19:55:52 +0200 | [diff] [blame] | 396 | struct request *rq = hwif->hwgroup->rq; |
Bartlomiej Zolnierkiewicz | 374e042 | 2008-07-23 19:55:56 +0200 | [diff] [blame^] | 397 | u8 stat = hwif->tp_ops->read_status(hwif); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 398 | |
Linus Torvalds | 6c3c315 | 2008-03-18 21:26:24 -0700 | [diff] [blame] | 399 | /* Error? */ |
| 400 | if (stat & ERR_STAT) |
Harvey Harrison | eb63963 | 2008-04-26 22:25:20 +0200 | [diff] [blame] | 401 | return task_error(drive, rq, __func__, stat); |
Linus Torvalds | 6c3c315 | 2008-03-18 21:26:24 -0700 | [diff] [blame] | 402 | |
| 403 | /* Didn't want any data? Odd. */ |
| 404 | if (!(stat & DRQ_STAT)) |
| 405 | return task_in_unexpected(drive, rq, stat); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 406 | |
| 407 | ide_pio_datablock(drive, rq, 0); |
| 408 | |
Linus Torvalds | 6c3c315 | 2008-03-18 21:26:24 -0700 | [diff] [blame] | 409 | /* Are we done? Check status and finish transfer. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 410 | if (!hwif->nleft) { |
| 411 | stat = wait_drive_not_busy(drive); |
Bartlomiej Zolnierkiewicz | 73d7de0 | 2008-01-26 20:13:10 +0100 | [diff] [blame] | 412 | if (!OK_STAT(stat, 0, BAD_STAT)) |
Harvey Harrison | eb63963 | 2008-04-26 22:25:20 +0200 | [diff] [blame] | 413 | return task_error(drive, rq, __func__, stat); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 414 | task_end_request(drive, rq, stat); |
| 415 | return ide_stopped; |
| 416 | } |
| 417 | |
| 418 | /* Still data left to transfer. */ |
| 419 | ide_set_handler(drive, &task_in_intr, WAIT_WORSTCASE, NULL); |
| 420 | |
| 421 | return ide_started; |
| 422 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 423 | |
| 424 | /* |
| 425 | * Handler for command with PIO data-out phase (Write/Write Multiple). |
| 426 | */ |
| 427 | static ide_startstop_t task_out_intr (ide_drive_t *drive) |
| 428 | { |
| 429 | ide_hwif_t *hwif = drive->hwif; |
| 430 | struct request *rq = HWGROUP(drive)->rq; |
Bartlomiej Zolnierkiewicz | 374e042 | 2008-07-23 19:55:56 +0200 | [diff] [blame^] | 431 | u8 stat = hwif->tp_ops->read_status(hwif); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 432 | |
| 433 | if (!OK_STAT(stat, DRIVE_READY, drive->bad_wstat)) |
Harvey Harrison | eb63963 | 2008-04-26 22:25:20 +0200 | [diff] [blame] | 434 | return task_error(drive, rq, __func__, stat); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 435 | |
| 436 | /* Deal with unexpected ATA data phase. */ |
| 437 | if (((stat & DRQ_STAT) == 0) ^ !hwif->nleft) |
Harvey Harrison | eb63963 | 2008-04-26 22:25:20 +0200 | [diff] [blame] | 438 | return task_error(drive, rq, __func__, stat); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 439 | |
| 440 | if (!hwif->nleft) { |
| 441 | task_end_request(drive, rq, stat); |
| 442 | return ide_stopped; |
| 443 | } |
| 444 | |
| 445 | /* Still data left to transfer. */ |
| 446 | ide_pio_datablock(drive, rq, 1); |
| 447 | ide_set_handler(drive, &task_out_intr, WAIT_WORSTCASE, NULL); |
| 448 | |
| 449 | return ide_started; |
| 450 | } |
| 451 | |
Bartlomiej Zolnierkiewicz | f6e29e3 | 2008-01-25 22:17:16 +0100 | [diff] [blame] | 452 | static ide_startstop_t pre_task_out_intr(ide_drive_t *drive, struct request *rq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 453 | { |
| 454 | ide_startstop_t startstop; |
| 455 | |
Bartlomiej Zolnierkiewicz | 4906f3b | 2008-01-26 20:13:11 +0100 | [diff] [blame] | 456 | if (ide_wait_stat(&startstop, drive, DRQ_STAT, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 457 | drive->bad_wstat, WAIT_DRQ)) { |
| 458 | printk(KERN_ERR "%s: no DRQ after issuing %sWRITE%s\n", |
| 459 | drive->name, |
| 460 | drive->hwif->data_phase ? "MULT" : "", |
| 461 | drive->addressing ? "_EXT" : ""); |
| 462 | return startstop; |
| 463 | } |
| 464 | |
| 465 | if (!drive->unmask) |
| 466 | local_irq_disable(); |
| 467 | |
| 468 | ide_set_handler(drive, &task_out_intr, WAIT_WORSTCASE, NULL); |
| 469 | ide_pio_datablock(drive, rq, 1); |
| 470 | |
| 471 | return ide_started; |
| 472 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 473 | |
Bartlomiej Zolnierkiewicz | ac026ff | 2008-01-25 22:17:14 +0100 | [diff] [blame] | 474 | int ide_raw_taskfile(ide_drive_t *drive, ide_task_t *task, u8 *buf, u16 nsect) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 475 | { |
FUJITA Tomonori | 154ed28 | 2008-07-15 21:21:43 +0200 | [diff] [blame] | 476 | struct request *rq; |
| 477 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 478 | |
FUJITA Tomonori | 154ed28 | 2008-07-15 21:21:43 +0200 | [diff] [blame] | 479 | rq = blk_get_request(drive->queue, READ, __GFP_WAIT); |
| 480 | rq->cmd_type = REQ_TYPE_ATA_TASKFILE; |
| 481 | rq->buffer = buf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 482 | |
| 483 | /* |
| 484 | * (ks) We transfer currently only whole sectors. |
| 485 | * This is suffient for now. But, it would be great, |
| 486 | * if we would find a solution to transfer any size. |
| 487 | * To support special commands like READ LONG. |
| 488 | */ |
FUJITA Tomonori | 154ed28 | 2008-07-15 21:21:43 +0200 | [diff] [blame] | 489 | rq->hard_nr_sectors = rq->nr_sectors = nsect; |
| 490 | rq->hard_cur_sectors = rq->current_nr_sectors = nsect; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 491 | |
Bartlomiej Zolnierkiewicz | ac026ff | 2008-01-25 22:17:14 +0100 | [diff] [blame] | 492 | if (task->tf_flags & IDE_TFLAG_WRITE) |
FUJITA Tomonori | 154ed28 | 2008-07-15 21:21:43 +0200 | [diff] [blame] | 493 | rq->cmd_flags |= REQ_RW; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 494 | |
FUJITA Tomonori | 154ed28 | 2008-07-15 21:21:43 +0200 | [diff] [blame] | 495 | rq->special = task; |
| 496 | task->rq = rq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 497 | |
FUJITA Tomonori | 154ed28 | 2008-07-15 21:21:43 +0200 | [diff] [blame] | 498 | error = blk_execute_rq(drive->queue, NULL, rq, 0); |
| 499 | blk_put_request(rq); |
| 500 | |
| 501 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 502 | } |
| 503 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 504 | EXPORT_SYMBOL(ide_raw_taskfile); |
| 505 | |
Bartlomiej Zolnierkiewicz | 9a3c49b | 2008-01-25 22:17:07 +0100 | [diff] [blame] | 506 | int ide_no_data_taskfile(ide_drive_t *drive, ide_task_t *task) |
| 507 | { |
Bartlomiej Zolnierkiewicz | ac026ff | 2008-01-25 22:17:14 +0100 | [diff] [blame] | 508 | task->data_phase = TASKFILE_NO_DATA; |
Bartlomiej Zolnierkiewicz | 9a3c49b | 2008-01-25 22:17:07 +0100 | [diff] [blame] | 509 | |
Bartlomiej Zolnierkiewicz | ac026ff | 2008-01-25 22:17:14 +0100 | [diff] [blame] | 510 | return ide_raw_taskfile(drive, task, NULL, 0); |
Bartlomiej Zolnierkiewicz | 9a3c49b | 2008-01-25 22:17:07 +0100 | [diff] [blame] | 511 | } |
| 512 | EXPORT_SYMBOL_GPL(ide_no_data_taskfile); |
| 513 | |
Bartlomiej Zolnierkiewicz | 26a5b04 | 2007-11-05 21:42:27 +0100 | [diff] [blame] | 514 | #ifdef CONFIG_IDE_TASK_IOCTL |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 515 | int ide_taskfile_ioctl (ide_drive_t *drive, unsigned int cmd, unsigned long arg) |
| 516 | { |
| 517 | ide_task_request_t *req_task; |
| 518 | ide_task_t args; |
| 519 | u8 *outbuf = NULL; |
| 520 | u8 *inbuf = NULL; |
Bartlomiej Zolnierkiewicz | ac026ff | 2008-01-25 22:17:14 +0100 | [diff] [blame] | 521 | u8 *data_buf = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 522 | int err = 0; |
| 523 | int tasksize = sizeof(struct ide_task_request_s); |
Alan Cox | 3a42bb2 | 2006-10-16 16:31:02 +0100 | [diff] [blame] | 524 | unsigned int taskin = 0; |
| 525 | unsigned int taskout = 0; |
Bartlomiej Zolnierkiewicz | ac026ff | 2008-01-25 22:17:14 +0100 | [diff] [blame] | 526 | u16 nsect = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 527 | char __user *buf = (char __user *)arg; |
| 528 | |
| 529 | // printk("IDE Taskfile ...\n"); |
| 530 | |
Deepak Saxena | f5e3c2f | 2005-11-07 01:01:25 -0800 | [diff] [blame] | 531 | req_task = kzalloc(tasksize, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 532 | if (req_task == NULL) return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 533 | if (copy_from_user(req_task, buf, tasksize)) { |
| 534 | kfree(req_task); |
| 535 | return -EFAULT; |
| 536 | } |
| 537 | |
Alan Cox | 3a42bb2 | 2006-10-16 16:31:02 +0100 | [diff] [blame] | 538 | taskout = req_task->out_size; |
| 539 | taskin = req_task->in_size; |
| 540 | |
| 541 | if (taskin > 65536 || taskout > 65536) { |
| 542 | err = -EINVAL; |
| 543 | goto abort; |
| 544 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 545 | |
| 546 | if (taskout) { |
| 547 | int outtotal = tasksize; |
Deepak Saxena | f5e3c2f | 2005-11-07 01:01:25 -0800 | [diff] [blame] | 548 | outbuf = kzalloc(taskout, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 549 | if (outbuf == NULL) { |
| 550 | err = -ENOMEM; |
| 551 | goto abort; |
| 552 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 553 | if (copy_from_user(outbuf, buf + outtotal, taskout)) { |
| 554 | err = -EFAULT; |
| 555 | goto abort; |
| 556 | } |
| 557 | } |
| 558 | |
| 559 | if (taskin) { |
| 560 | int intotal = tasksize + taskout; |
Deepak Saxena | f5e3c2f | 2005-11-07 01:01:25 -0800 | [diff] [blame] | 561 | inbuf = kzalloc(taskin, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 562 | if (inbuf == NULL) { |
| 563 | err = -ENOMEM; |
| 564 | goto abort; |
| 565 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 566 | if (copy_from_user(inbuf, buf + intotal, taskin)) { |
| 567 | err = -EFAULT; |
| 568 | goto abort; |
| 569 | } |
| 570 | } |
| 571 | |
| 572 | memset(&args, 0, sizeof(ide_task_t)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 573 | |
Bartlomiej Zolnierkiewicz | 650d841 | 2008-01-25 22:17:06 +0100 | [diff] [blame] | 574 | memcpy(&args.tf_array[0], req_task->hob_ports, HDIO_DRIVE_HOB_HDR_SIZE - 2); |
| 575 | memcpy(&args.tf_array[6], req_task->io_ports, HDIO_DRIVE_TASK_HDR_SIZE); |
Bartlomiej Zolnierkiewicz | 866e2ec | 2008-01-25 22:17:14 +0100 | [diff] [blame] | 576 | |
| 577 | args.data_phase = req_task->data_phase; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 578 | |
Bartlomiej Zolnierkiewicz | 657cc1a | 2008-01-26 20:13:10 +0100 | [diff] [blame] | 579 | args.tf_flags = IDE_TFLAG_IO_16BIT | IDE_TFLAG_DEVICE | |
| 580 | IDE_TFLAG_IN_TF; |
Bartlomiej Zolnierkiewicz | a3bbb9d | 2008-01-25 22:17:10 +0100 | [diff] [blame] | 581 | if (drive->addressing == 1) |
Bartlomiej Zolnierkiewicz | 657cc1a | 2008-01-26 20:13:10 +0100 | [diff] [blame] | 582 | args.tf_flags |= (IDE_TFLAG_LBA48 | IDE_TFLAG_IN_HOB); |
Bartlomiej Zolnierkiewicz | a3bbb9d | 2008-01-25 22:17:10 +0100 | [diff] [blame] | 583 | |
Bartlomiej Zolnierkiewicz | 74095a9 | 2008-01-25 22:17:07 +0100 | [diff] [blame] | 584 | if (req_task->out_flags.all) { |
| 585 | args.tf_flags |= IDE_TFLAG_FLAGGED; |
| 586 | |
| 587 | if (req_task->out_flags.b.data) |
| 588 | args.tf_flags |= IDE_TFLAG_OUT_DATA; |
| 589 | |
| 590 | if (req_task->out_flags.b.nsector_hob) |
| 591 | args.tf_flags |= IDE_TFLAG_OUT_HOB_NSECT; |
| 592 | if (req_task->out_flags.b.sector_hob) |
| 593 | args.tf_flags |= IDE_TFLAG_OUT_HOB_LBAL; |
| 594 | if (req_task->out_flags.b.lcyl_hob) |
| 595 | args.tf_flags |= IDE_TFLAG_OUT_HOB_LBAM; |
| 596 | if (req_task->out_flags.b.hcyl_hob) |
| 597 | args.tf_flags |= IDE_TFLAG_OUT_HOB_LBAH; |
| 598 | |
| 599 | if (req_task->out_flags.b.error_feature) |
| 600 | args.tf_flags |= IDE_TFLAG_OUT_FEATURE; |
| 601 | if (req_task->out_flags.b.nsector) |
| 602 | args.tf_flags |= IDE_TFLAG_OUT_NSECT; |
| 603 | if (req_task->out_flags.b.sector) |
| 604 | args.tf_flags |= IDE_TFLAG_OUT_LBAL; |
| 605 | if (req_task->out_flags.b.lcyl) |
| 606 | args.tf_flags |= IDE_TFLAG_OUT_LBAM; |
| 607 | if (req_task->out_flags.b.hcyl) |
| 608 | args.tf_flags |= IDE_TFLAG_OUT_LBAH; |
Bartlomiej Zolnierkiewicz | a3bbb9d | 2008-01-25 22:17:10 +0100 | [diff] [blame] | 609 | } else { |
| 610 | args.tf_flags |= IDE_TFLAG_OUT_TF; |
| 611 | if (args.tf_flags & IDE_TFLAG_LBA48) |
| 612 | args.tf_flags |= IDE_TFLAG_OUT_HOB; |
Bartlomiej Zolnierkiewicz | 74095a9 | 2008-01-25 22:17:07 +0100 | [diff] [blame] | 613 | } |
| 614 | |
Bartlomiej Zolnierkiewicz | 866e2ec | 2008-01-25 22:17:14 +0100 | [diff] [blame] | 615 | if (req_task->in_flags.b.data) |
| 616 | args.tf_flags |= IDE_TFLAG_IN_DATA; |
| 617 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 618 | switch(req_task->data_phase) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 619 | case TASKFILE_MULTI_OUT: |
| 620 | if (!drive->mult_count) { |
| 621 | /* (hs): give up if multcount is not set */ |
| 622 | printk(KERN_ERR "%s: %s Multimode Write " \ |
| 623 | "multcount is not set\n", |
Harvey Harrison | eb63963 | 2008-04-26 22:25:20 +0200 | [diff] [blame] | 624 | drive->name, __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 625 | err = -EPERM; |
| 626 | goto abort; |
| 627 | } |
| 628 | /* fall through */ |
| 629 | case TASKFILE_OUT: |
Bartlomiej Zolnierkiewicz | ac026ff | 2008-01-25 22:17:14 +0100 | [diff] [blame] | 630 | /* fall through */ |
| 631 | case TASKFILE_OUT_DMAQ: |
| 632 | case TASKFILE_OUT_DMA: |
| 633 | nsect = taskout / SECTOR_SIZE; |
| 634 | data_buf = outbuf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 635 | break; |
| 636 | case TASKFILE_MULTI_IN: |
| 637 | if (!drive->mult_count) { |
| 638 | /* (hs): give up if multcount is not set */ |
| 639 | printk(KERN_ERR "%s: %s Multimode Read failure " \ |
| 640 | "multcount is not set\n", |
Harvey Harrison | eb63963 | 2008-04-26 22:25:20 +0200 | [diff] [blame] | 641 | drive->name, __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 642 | err = -EPERM; |
| 643 | goto abort; |
| 644 | } |
| 645 | /* fall through */ |
| 646 | case TASKFILE_IN: |
Bartlomiej Zolnierkiewicz | ac026ff | 2008-01-25 22:17:14 +0100 | [diff] [blame] | 647 | /* fall through */ |
| 648 | case TASKFILE_IN_DMAQ: |
| 649 | case TASKFILE_IN_DMA: |
| 650 | nsect = taskin / SECTOR_SIZE; |
| 651 | data_buf = inbuf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 652 | break; |
| 653 | case TASKFILE_NO_DATA: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 654 | break; |
| 655 | default: |
| 656 | err = -EFAULT; |
| 657 | goto abort; |
| 658 | } |
| 659 | |
Bartlomiej Zolnierkiewicz | ac026ff | 2008-01-25 22:17:14 +0100 | [diff] [blame] | 660 | if (req_task->req_cmd == IDE_DRIVE_TASK_NO_DATA) |
| 661 | nsect = 0; |
| 662 | else if (!nsect) { |
| 663 | nsect = (args.tf.hob_nsect << 8) | args.tf.nsect; |
| 664 | |
| 665 | if (!nsect) { |
| 666 | printk(KERN_ERR "%s: in/out command without data\n", |
| 667 | drive->name); |
| 668 | err = -EFAULT; |
| 669 | goto abort; |
| 670 | } |
| 671 | } |
| 672 | |
| 673 | if (req_task->req_cmd == IDE_DRIVE_TASK_RAW_WRITE) |
| 674 | args.tf_flags |= IDE_TFLAG_WRITE; |
| 675 | |
| 676 | err = ide_raw_taskfile(drive, &args, data_buf, nsect); |
| 677 | |
Bartlomiej Zolnierkiewicz | 650d841 | 2008-01-25 22:17:06 +0100 | [diff] [blame] | 678 | memcpy(req_task->hob_ports, &args.tf_array[0], HDIO_DRIVE_HOB_HDR_SIZE - 2); |
| 679 | memcpy(req_task->io_ports, &args.tf_array[6], HDIO_DRIVE_TASK_HDR_SIZE); |
Bartlomiej Zolnierkiewicz | 866e2ec | 2008-01-25 22:17:14 +0100 | [diff] [blame] | 680 | |
| 681 | if ((args.tf_flags & IDE_TFLAG_FLAGGED_SET_IN_FLAGS) && |
| 682 | req_task->in_flags.all == 0) { |
| 683 | req_task->in_flags.all = IDE_TASKFILE_STD_IN_FLAGS; |
| 684 | if (drive->addressing == 1) |
| 685 | req_task->in_flags.all |= (IDE_HOB_STD_IN_FLAGS << 8); |
| 686 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 687 | |
| 688 | if (copy_to_user(buf, req_task, tasksize)) { |
| 689 | err = -EFAULT; |
| 690 | goto abort; |
| 691 | } |
| 692 | if (taskout) { |
| 693 | int outtotal = tasksize; |
| 694 | if (copy_to_user(buf + outtotal, outbuf, taskout)) { |
| 695 | err = -EFAULT; |
| 696 | goto abort; |
| 697 | } |
| 698 | } |
| 699 | if (taskin) { |
| 700 | int intotal = tasksize + taskout; |
| 701 | if (copy_to_user(buf + intotal, inbuf, taskin)) { |
| 702 | err = -EFAULT; |
| 703 | goto abort; |
| 704 | } |
| 705 | } |
| 706 | abort: |
| 707 | kfree(req_task); |
Jesper Juhl | 6044ec8 | 2005-11-07 01:01:32 -0800 | [diff] [blame] | 708 | kfree(outbuf); |
| 709 | kfree(inbuf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 710 | |
| 711 | // printk("IDE Taskfile ioctl ended. rc = %i\n", err); |
| 712 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 713 | return err; |
| 714 | } |
Bartlomiej Zolnierkiewicz | 26a5b04 | 2007-11-05 21:42:27 +0100 | [diff] [blame] | 715 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 716 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 717 | int ide_cmd_ioctl (ide_drive_t *drive, unsigned int cmd, unsigned long arg) |
| 718 | { |
Bartlomiej Zolnierkiewicz | 5a9e77a | 2008-01-26 20:13:13 +0100 | [diff] [blame] | 719 | u8 *buf = NULL; |
| 720 | int bufsize = 0, err = 0; |
| 721 | u8 args[4], xfer_rate = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 722 | ide_task_t tfargs; |
Bartlomiej Zolnierkiewicz | 650d841 | 2008-01-25 22:17:06 +0100 | [diff] [blame] | 723 | struct ide_taskfile *tf = &tfargs.tf; |
Bartlomiej Zolnierkiewicz | 5efe7c5 | 2008-02-02 19:56:46 +0100 | [diff] [blame] | 724 | struct hd_driveid *id = drive->id; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 725 | |
| 726 | if (NULL == (void *) arg) { |
FUJITA Tomonori | 154ed28 | 2008-07-15 21:21:43 +0200 | [diff] [blame] | 727 | struct request *rq; |
Bartlomiej Zolnierkiewicz | 145b75e | 2008-01-26 20:13:11 +0100 | [diff] [blame] | 728 | |
FUJITA Tomonori | 154ed28 | 2008-07-15 21:21:43 +0200 | [diff] [blame] | 729 | rq = blk_get_request(drive->queue, READ, __GFP_WAIT); |
| 730 | rq->cmd_type = REQ_TYPE_ATA_TASKFILE; |
| 731 | err = blk_execute_rq(drive->queue, NULL, rq, 0); |
| 732 | blk_put_request(rq); |
Bartlomiej Zolnierkiewicz | 145b75e | 2008-01-26 20:13:11 +0100 | [diff] [blame] | 733 | |
FUJITA Tomonori | 154ed28 | 2008-07-15 21:21:43 +0200 | [diff] [blame] | 734 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 735 | } |
| 736 | |
| 737 | if (copy_from_user(args, (void __user *)arg, 4)) |
| 738 | return -EFAULT; |
| 739 | |
| 740 | memset(&tfargs, 0, sizeof(ide_task_t)); |
Bartlomiej Zolnierkiewicz | 650d841 | 2008-01-25 22:17:06 +0100 | [diff] [blame] | 741 | tf->feature = args[2]; |
Bartlomiej Zolnierkiewicz | 5a9e77a | 2008-01-26 20:13:13 +0100 | [diff] [blame] | 742 | if (args[0] == WIN_SMART) { |
| 743 | tf->nsect = args[3]; |
| 744 | tf->lbal = args[1]; |
| 745 | tf->lbam = 0x4f; |
| 746 | tf->lbah = 0xc2; |
| 747 | tfargs.tf_flags = IDE_TFLAG_OUT_TF | IDE_TFLAG_IN_NSECT; |
| 748 | } else { |
| 749 | tf->nsect = args[1]; |
| 750 | tfargs.tf_flags = IDE_TFLAG_OUT_FEATURE | |
| 751 | IDE_TFLAG_OUT_NSECT | IDE_TFLAG_IN_NSECT; |
| 752 | } |
Bartlomiej Zolnierkiewicz | 650d841 | 2008-01-25 22:17:06 +0100 | [diff] [blame] | 753 | tf->command = args[0]; |
Bartlomiej Zolnierkiewicz | 5a9e77a | 2008-01-26 20:13:13 +0100 | [diff] [blame] | 754 | tfargs.data_phase = args[3] ? TASKFILE_IN : TASKFILE_NO_DATA; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 755 | |
| 756 | if (args[3]) { |
Bartlomiej Zolnierkiewicz | 5a9e77a | 2008-01-26 20:13:13 +0100 | [diff] [blame] | 757 | tfargs.tf_flags |= IDE_TFLAG_IO_16BIT; |
| 758 | bufsize = SECTOR_WORDS * 4 * args[3]; |
| 759 | buf = kzalloc(bufsize, GFP_KERNEL); |
| 760 | if (buf == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 761 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 762 | } |
Bartlomiej Zolnierkiewicz | 5a9e77a | 2008-01-26 20:13:13 +0100 | [diff] [blame] | 763 | |
Bartlomiej Zolnierkiewicz | 5efe7c5 | 2008-02-02 19:56:46 +0100 | [diff] [blame] | 764 | if (tf->command == WIN_SETFEATURES && |
| 765 | tf->feature == SETFEATURES_XFER && |
| 766 | tf->nsect >= XFER_SW_DMA_0 && |
| 767 | (id->dma_ultra || id->dma_mword || id->dma_1word)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 768 | xfer_rate = args[1]; |
Bartlomiej Zolnierkiewicz | af10f77 | 2008-02-02 19:56:46 +0100 | [diff] [blame] | 769 | if (tf->nsect > XFER_UDMA_2 && !eighty_ninty_three(drive)) { |
| 770 | printk(KERN_WARNING "%s: UDMA speeds >UDMA33 cannot " |
| 771 | "be set\n", drive->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 772 | goto abort; |
Bartlomiej Zolnierkiewicz | af10f77 | 2008-02-02 19:56:46 +0100 | [diff] [blame] | 773 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 774 | } |
| 775 | |
Bartlomiej Zolnierkiewicz | 5a9e77a | 2008-01-26 20:13:13 +0100 | [diff] [blame] | 776 | err = ide_raw_taskfile(drive, &tfargs, buf, args[3]); |
| 777 | |
| 778 | args[0] = tf->status; |
| 779 | args[1] = tf->error; |
| 780 | args[2] = tf->nsect; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 781 | |
| 782 | if (!err && xfer_rate) { |
| 783 | /* active-retuning-calls future */ |
| 784 | ide_set_xfer_rate(drive, xfer_rate); |
| 785 | ide_driveid_update(drive); |
| 786 | } |
| 787 | abort: |
Bartlomiej Zolnierkiewicz | 5a9e77a | 2008-01-26 20:13:13 +0100 | [diff] [blame] | 788 | if (copy_to_user((void __user *)arg, &args, 4)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 789 | err = -EFAULT; |
Bartlomiej Zolnierkiewicz | 5a9e77a | 2008-01-26 20:13:13 +0100 | [diff] [blame] | 790 | if (buf) { |
| 791 | if (copy_to_user((void __user *)(arg + 4), buf, bufsize)) |
| 792 | err = -EFAULT; |
| 793 | kfree(buf); |
| 794 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 795 | return err; |
| 796 | } |
| 797 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 798 | int ide_task_ioctl (ide_drive_t *drive, unsigned int cmd, unsigned long arg) |
| 799 | { |
| 800 | void __user *p = (void __user *)arg; |
| 801 | int err = 0; |
Bartlomiej Zolnierkiewicz | 14b89ef | 2008-01-25 22:17:11 +0100 | [diff] [blame] | 802 | u8 args[7]; |
| 803 | ide_task_t task; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 804 | |
| 805 | if (copy_from_user(args, p, 7)) |
| 806 | return -EFAULT; |
Bartlomiej Zolnierkiewicz | 14b89ef | 2008-01-25 22:17:11 +0100 | [diff] [blame] | 807 | |
| 808 | memset(&task, 0, sizeof(task)); |
| 809 | memcpy(&task.tf_array[7], &args[1], 6); |
| 810 | task.tf.command = args[0]; |
Bartlomiej Zolnierkiewicz | 657cc1a | 2008-01-26 20:13:10 +0100 | [diff] [blame] | 811 | task.tf_flags = IDE_TFLAG_TF | IDE_TFLAG_DEVICE; |
Bartlomiej Zolnierkiewicz | 14b89ef | 2008-01-25 22:17:11 +0100 | [diff] [blame] | 812 | |
| 813 | err = ide_no_data_taskfile(drive, &task); |
| 814 | |
| 815 | args[0] = task.tf.command; |
| 816 | memcpy(&args[1], &task.tf_array[7], 6); |
| 817 | |
| 818 | if (copy_to_user(p, args, 7)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 819 | err = -EFAULT; |
Bartlomiej Zolnierkiewicz | 14b89ef | 2008-01-25 22:17:11 +0100 | [diff] [blame] | 820 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 821 | return err; |
| 822 | } |