]>
Commit | Line | Data |
---|---|---|
1da177e4 | 1 | /* |
59bca8cc BZ |
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 | |
1da177e4 | 5 | * IBM Storage Technology Division |
59bca8cc | 6 | * Copyright (C) 2003-2004, 2007 Bartlomiej Zolnierkiewicz |
1da177e4 LT |
7 | * |
8 | * The big the bad and the ugly. | |
1da177e4 LT |
9 | */ |
10 | ||
1da177e4 LT |
11 | #include <linux/types.h> |
12 | #include <linux/string.h> | |
13 | #include <linux/kernel.h> | |
651c29a1 | 14 | #include <linux/sched.h> |
1da177e4 | 15 | #include <linux/interrupt.h> |
1da177e4 | 16 | #include <linux/errno.h> |
1da177e4 | 17 | #include <linux/slab.h> |
1da177e4 LT |
18 | #include <linux/delay.h> |
19 | #include <linux/hdreg.h> | |
20 | #include <linux/ide.h> | |
55c16a70 | 21 | #include <linux/scatterlist.h> |
1da177e4 | 22 | |
1da177e4 LT |
23 | #include <asm/uaccess.h> |
24 | #include <asm/io.h> | |
25 | ||
745483f1 | 26 | void ide_tf_dump(const char *s, struct ide_cmd *cmd) |
9e42237f | 27 | { |
807e35d6 BZ |
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", | |
745483f1 SS |
31 | s, cmd->tf.feature, cmd->tf.nsect, |
32 | cmd->tf.lbal, cmd->tf.lbam, cmd->tf.lbah, | |
33 | cmd->tf.device, cmd->tf.command); | |
34 | printk("%s: hob: nsect 0x%02x lbal 0x%02x lbam 0x%02x lbah 0x%02x\n", | |
35 | s, cmd->hob.nsect, cmd->hob.lbal, cmd->hob.lbam, cmd->hob.lbah); | |
807e35d6 | 36 | #endif |
089c5c7e BZ |
37 | } |
38 | ||
1da177e4 LT |
39 | int taskfile_lib_get_identify (ide_drive_t *drive, u8 *buf) |
40 | { | |
22aa4b32 | 41 | struct ide_cmd cmd; |
650d841d | 42 | |
22aa4b32 BZ |
43 | memset(&cmd, 0, sizeof(cmd)); |
44 | cmd.tf.nsect = 0x01; | |
1da177e4 | 45 | if (drive->media == ide_disk) |
22aa4b32 | 46 | cmd.tf.command = ATA_CMD_ID_ATA; |
1da177e4 | 47 | else |
22aa4b32 | 48 | cmd.tf.command = ATA_CMD_ID_ATAPI; |
60f85019 SS |
49 | cmd.valid.out.tf = IDE_VALID_OUT_TF | IDE_VALID_DEVICE; |
50 | cmd.valid.in.tf = IDE_VALID_IN_TF | IDE_VALID_DEVICE; | |
0dfb991c | 51 | cmd.protocol = ATA_PROT_PIO; |
22aa4b32 BZ |
52 | |
53 | return ide_raw_taskfile(drive, &cmd, buf, 1); | |
1da177e4 LT |
54 | } |
55 | ||
1192e528 | 56 | static ide_startstop_t task_no_data_intr(ide_drive_t *); |
adb1af98 | 57 | static ide_startstop_t pre_task_out_intr(ide_drive_t *, struct ide_cmd *); |
901bd08a | 58 | static ide_startstop_t task_pio_intr(ide_drive_t *); |
1192e528 | 59 | |
adb1af98 | 60 | ide_startstop_t do_rw_taskfile(ide_drive_t *drive, struct ide_cmd *orig_cmd) |
1da177e4 | 61 | { |
898ec223 | 62 | ide_hwif_t *hwif = drive->hwif; |
adb1af98 | 63 | struct ide_cmd *cmd = &hwif->cmd; |
22aa4b32 | 64 | struct ide_taskfile *tf = &cmd->tf; |
57d7366b | 65 | ide_handler_t *handler = NULL; |
374e042c | 66 | const struct ide_tp_ops *tp_ops = hwif->tp_ops; |
f37afdac | 67 | const struct ide_dma_ops *dma_ops = hwif->dma_ops; |
1da177e4 | 68 | |
0dfb991c BZ |
69 | if (orig_cmd->protocol == ATA_PROT_PIO && |
70 | (orig_cmd->tf_flags & IDE_TFLAG_MULTI_PIO) && | |
71 | drive->mult_count == 0) { | |
72 | printk(KERN_ERR "%s: multimode not set!\n", drive->name); | |
73 | return ide_stopped; | |
1edee60e BZ |
74 | } |
75 | ||
adb1af98 BZ |
76 | if (orig_cmd->ftf_flags & IDE_FTFLAG_FLAGGED) |
77 | orig_cmd->ftf_flags |= IDE_FTFLAG_SET_IN_FLAGS; | |
1edee60e | 78 | |
adb1af98 | 79 | memcpy(cmd, orig_cmd, sizeof(*cmd)); |
d6ff9f64 | 80 | |
22aa4b32 | 81 | if ((cmd->tf_flags & IDE_TFLAG_DMA_PIO_FALLBACK) == 0) { |
745483f1 | 82 | ide_tf_dump(drive->name, cmd); |
ecf3a31d | 83 | tp_ops->write_devctl(hwif, ATA_DEVCTL_OBS); |
ed4af48f | 84 | SELECT_MASK(drive, 0); |
35218d1c SS |
85 | |
86 | if (cmd->ftf_flags & IDE_FTFLAG_OUT_DATA) { | |
745483f1 | 87 | u8 data[2] = { cmd->tf.data, cmd->hob.data }; |
35218d1c SS |
88 | |
89 | tp_ops->output_data(drive, cmd, data, 2); | |
90 | } | |
22aa4b32 | 91 | tp_ops->tf_load(drive, cmd); |
089c5c7e | 92 | } |
1da177e4 | 93 | |
0dfb991c BZ |
94 | switch (cmd->protocol) { |
95 | case ATA_PROT_PIO: | |
96 | if (cmd->tf_flags & IDE_TFLAG_WRITE) { | |
97 | tp_ops->exec_command(hwif, tf->command); | |
98 | ndelay(400); /* FIXME */ | |
99 | return pre_task_out_intr(drive, cmd); | |
100 | } | |
901bd08a | 101 | handler = task_pio_intr; |
1192e528 | 102 | /* fall-through */ |
0dfb991c | 103 | case ATA_PROT_NODATA: |
57d7366b BZ |
104 | if (handler == NULL) |
105 | handler = task_no_data_intr; | |
35b5d0be | 106 | ide_execute_command(drive, cmd, handler, WAIT_WORSTCASE); |
1da177e4 | 107 | return ide_started; |
b788ee9c | 108 | case ATA_PROT_DMA: |
5ae5412d | 109 | if (ide_dma_prepare(drive, cmd)) |
10d90157 | 110 | return ide_stopped; |
22117d6e | 111 | hwif->expiry = dma_ops->dma_timer_expiry; |
35b5d0be | 112 | ide_execute_command(drive, cmd, ide_dma_intr, 2 * WAIT_CMD); |
5e37bdc0 | 113 | dma_ops->dma_start(drive); |
b788ee9c | 114 | default: |
74095a91 | 115 | return ide_started; |
1da177e4 | 116 | } |
1da177e4 | 117 | } |
f6e29e35 | 118 | EXPORT_SYMBOL_GPL(do_rw_taskfile); |
1da177e4 | 119 | |
d6ff9f64 | 120 | static ide_startstop_t task_no_data_intr(ide_drive_t *drive) |
1da177e4 | 121 | { |
b73c7ee2 | 122 | ide_hwif_t *hwif = drive->hwif; |
22aa4b32 BZ |
123 | struct ide_cmd *cmd = &hwif->cmd; |
124 | struct ide_taskfile *tf = &cmd->tf; | |
125 | int custom = (cmd->tf_flags & IDE_TFLAG_CUSTOM_HANDLER) ? 1 : 0; | |
d6ff9f64 | 126 | int retries = (custom && tf->command == ATA_CMD_INIT_DEV_PARAMS) ? 5 : 1; |
1da177e4 LT |
127 | u8 stat; |
128 | ||
90d2c6bc BZ |
129 | local_irq_enable_in_hardirq(); |
130 | ||
374e042c BZ |
131 | while (1) { |
132 | stat = hwif->tp_ops->read_status(hwif); | |
3a7d2484 | 133 | if ((stat & ATA_BUSY) == 0 || retries-- == 0) |
374e042c | 134 | break; |
1da177e4 | 135 | udelay(10); |
374e042c | 136 | }; |
1da177e4 | 137 | |
d6ff9f64 BZ |
138 | if (!OK_STAT(stat, ATA_DRDY, BAD_STAT)) { |
139 | if (custom && tf->command == ATA_CMD_SET_MULTI) { | |
140 | drive->mult_req = drive->mult_count = 0; | |
141 | drive->special.b.recalibrate = 1; | |
142 | (void)ide_dump_status(drive, __func__, stat); | |
143 | return ide_stopped; | |
144 | } else if (custom && tf->command == ATA_CMD_INIT_DEV_PARAMS) { | |
145 | if ((stat & (ATA_ERR | ATA_DRQ)) == 0) { | |
146 | ide_set_handler(drive, &task_no_data_intr, | |
60c0cd02 | 147 | WAIT_WORSTCASE); |
d6ff9f64 BZ |
148 | return ide_started; |
149 | } | |
150 | } | |
1da177e4 | 151 | return ide_error(drive, "task_no_data_intr", stat); |
d6ff9f64 | 152 | } |
c47137a9 | 153 | |
e7fedc3c | 154 | if (custom && tf->command == ATA_CMD_SET_MULTI) |
d6ff9f64 | 155 | drive->mult_count = drive->mult_req; |
1da177e4 | 156 | |
d364c7f5 BZ |
157 | if (custom == 0 || tf->command == ATA_CMD_IDLEIMMEDIATE || |
158 | tf->command == ATA_CMD_CHK_POWER) { | |
a09485df | 159 | struct request *rq = hwif->rq; |
a09485df BZ |
160 | |
161 | if (blk_pm_request(rq)) | |
162 | ide_complete_pm_rq(drive, rq); | |
2230d90d BZ |
163 | else |
164 | ide_finish_cmd(drive, cmd, stat); | |
a09485df BZ |
165 | } |
166 | ||
1da177e4 LT |
167 | return ide_stopped; |
168 | } | |
169 | ||
da6f4c7f | 170 | static u8 wait_drive_not_busy(ide_drive_t *drive) |
1da177e4 | 171 | { |
b73c7ee2 | 172 | ide_hwif_t *hwif = drive->hwif; |
b42fa133 | 173 | int retries; |
1da177e4 LT |
174 | u8 stat; |
175 | ||
176 | /* | |
f54feafa BZ |
177 | * Last sector was transfered, wait until device is ready. This can |
178 | * take up to 6 ms on some ATAPI devices, so we will wait max 10 ms. | |
1da177e4 | 179 | */ |
f54feafa | 180 | for (retries = 0; retries < 1000; retries++) { |
374e042c | 181 | stat = hwif->tp_ops->read_status(hwif); |
c47137a9 | 182 | |
3a7d2484 | 183 | if (stat & ATA_BUSY) |
b42fa133 MY |
184 | udelay(10); |
185 | else | |
186 | break; | |
187 | } | |
1da177e4 | 188 | |
3a7d2484 | 189 | if (stat & ATA_BUSY) |
1da177e4 LT |
190 | printk(KERN_ERR "%s: drive still BUSY!\n", drive->name); |
191 | ||
192 | return stat; | |
193 | } | |
194 | ||
a08915ba BZ |
195 | void ide_pio_bytes(ide_drive_t *drive, struct ide_cmd *cmd, |
196 | unsigned int write, unsigned int len) | |
1da177e4 LT |
197 | { |
198 | ide_hwif_t *hwif = drive->hwif; | |
199 | struct scatterlist *sg = hwif->sg_table; | |
b6308ee0 | 200 | struct scatterlist *cursg = cmd->cursg; |
1da177e4 | 201 | struct page *page; |
1da177e4 | 202 | unsigned long flags; |
1da177e4 LT |
203 | unsigned int offset; |
204 | u8 *buf; | |
205 | ||
b6308ee0 | 206 | cursg = cmd->cursg; |
7a00798b BZ |
207 | if (cursg == NULL) |
208 | cursg = cmd->cursg = sg; | |
209 | ||
210 | while (len) { | |
211 | unsigned nr_bytes = min(len, cursg->length - cmd->cursg_ofs); | |
55c16a70 | 212 | |
7a00798b BZ |
213 | if (nr_bytes > PAGE_SIZE) |
214 | nr_bytes = PAGE_SIZE; | |
1da177e4 | 215 | |
7a00798b BZ |
216 | page = sg_page(cursg); |
217 | offset = cursg->offset + cmd->cursg_ofs; | |
218 | ||
219 | /* get the current page and offset */ | |
220 | page = nth_page(page, (offset >> PAGE_SHIFT)); | |
221 | offset %= PAGE_SIZE; | |
1da177e4 | 222 | |
f2bc3167 BZ |
223 | if (PageHighMem(page)) |
224 | local_irq_save(flags); | |
225 | ||
7a00798b | 226 | buf = kmap_atomic(page, KM_BIO_SRC_IRQ) + offset; |
1da177e4 | 227 | |
7a00798b BZ |
228 | cmd->nleft -= nr_bytes; |
229 | cmd->cursg_ofs += nr_bytes; | |
1da177e4 | 230 | |
7a00798b BZ |
231 | if (cmd->cursg_ofs == cursg->length) { |
232 | cursg = cmd->cursg = sg_next(cmd->cursg); | |
233 | cmd->cursg_ofs = 0; | |
234 | } | |
1da177e4 | 235 | |
7a00798b BZ |
236 | /* do the actual data transfer */ |
237 | if (write) | |
238 | hwif->tp_ops->output_data(drive, cmd, buf, nr_bytes); | |
239 | else | |
240 | hwif->tp_ops->input_data(drive, cmd, buf, nr_bytes); | |
1da177e4 | 241 | |
7a00798b | 242 | kunmap_atomic(buf, KM_BIO_SRC_IRQ); |
f2bc3167 BZ |
243 | |
244 | if (PageHighMem(page)) | |
245 | local_irq_restore(flags); | |
1da177e4 | 246 | |
7a00798b BZ |
247 | len -= nr_bytes; |
248 | } | |
1da177e4 | 249 | } |
a08915ba | 250 | EXPORT_SYMBOL_GPL(ide_pio_bytes); |
1da177e4 | 251 | |
adb1af98 BZ |
252 | static void ide_pio_datablock(ide_drive_t *drive, struct ide_cmd *cmd, |
253 | unsigned int write) | |
1da177e4 | 254 | { |
7a00798b BZ |
255 | unsigned int nr_bytes; |
256 | ||
35cf2b94 TH |
257 | u8 saved_io_32bit = drive->io_32bit; |
258 | ||
adb1af98 BZ |
259 | if (cmd->tf_flags & IDE_TFLAG_FS) |
260 | cmd->rq->errors = 0; | |
1da177e4 | 261 | |
22aa4b32 | 262 | if (cmd->tf_flags & IDE_TFLAG_IO_16BIT) |
e3d9a73a | 263 | drive->io_32bit = 0; |
35cf2b94 | 264 | |
651c29a1 AM |
265 | touch_softlockup_watchdog(); |
266 | ||
0dfb991c | 267 | if (cmd->tf_flags & IDE_TFLAG_MULTI_PIO) |
7a00798b | 268 | nr_bytes = min_t(unsigned, cmd->nleft, drive->mult_count << 9); |
0dfb991c | 269 | else |
7a00798b BZ |
270 | nr_bytes = SECTOR_SIZE; |
271 | ||
272 | ide_pio_bytes(drive, cmd, write, nr_bytes); | |
35cf2b94 TH |
273 | |
274 | drive->io_32bit = saved_io_32bit; | |
1da177e4 LT |
275 | } |
276 | ||
041cea10 | 277 | static void ide_error_cmd(ide_drive_t *drive, struct ide_cmd *cmd) |
1da177e4 | 278 | { |
adb1af98 | 279 | if (cmd->tf_flags & IDE_TFLAG_FS) { |
bf717c0a | 280 | int nr_bytes = cmd->nbytes - cmd->nleft; |
1da177e4 | 281 | |
0dfb991c BZ |
282 | if (cmd->protocol == ATA_PROT_PIO && |
283 | ((cmd->tf_flags & IDE_TFLAG_WRITE) || cmd->nleft == 0)) { | |
284 | if (cmd->tf_flags & IDE_TFLAG_MULTI_PIO) | |
bf717c0a | 285 | nr_bytes -= drive->mult_count << 9; |
0dfb991c | 286 | else |
bf717c0a | 287 | nr_bytes -= SECTOR_SIZE; |
1da177e4 LT |
288 | } |
289 | ||
bf717c0a BZ |
290 | if (nr_bytes > 0) |
291 | ide_complete_rq(drive, 0, nr_bytes); | |
1da177e4 | 292 | } |
1da177e4 LT |
293 | } |
294 | ||
adb1af98 | 295 | void ide_finish_cmd(ide_drive_t *drive, struct ide_cmd *cmd, u8 stat) |
1da177e4 | 296 | { |
6902a533 | 297 | struct request *rq = drive->hwif->rq; |
2230d90d | 298 | u8 err = ide_read_error(drive); |
1da177e4 | 299 | |
2230d90d | 300 | ide_complete_cmd(drive, cmd, stat, err); |
6902a533 | 301 | rq->errors = err; |
f974b196 | 302 | ide_complete_rq(drive, err ? -EIO : 0, blk_rq_bytes(rq)); |
1da177e4 LT |
303 | } |
304 | ||
305 | /* | |
901bd08a | 306 | * Handler for command with PIO data phase. |
1da177e4 | 307 | */ |
901bd08a | 308 | static ide_startstop_t task_pio_intr(ide_drive_t *drive) |
1da177e4 LT |
309 | { |
310 | ide_hwif_t *hwif = drive->hwif; | |
adb1af98 | 311 | struct ide_cmd *cmd = &drive->hwif->cmd; |
374e042c | 312 | u8 stat = hwif->tp_ops->read_status(hwif); |
901bd08a | 313 | u8 write = !!(cmd->tf_flags & IDE_TFLAG_WRITE); |
1da177e4 | 314 | |
901bd08a BZ |
315 | if (write == 0) { |
316 | /* Error? */ | |
317 | if (stat & ATA_ERR) | |
0a1248c5 | 318 | goto out_err; |
1da177e4 | 319 | |
901bd08a | 320 | /* Didn't want any data? Odd. */ |
151055ed BZ |
321 | if ((stat & ATA_DRQ) == 0) { |
322 | /* Command all done? */ | |
0a1248c5 BZ |
323 | if (OK_STAT(stat, ATA_DRDY, ATA_BUSY)) |
324 | goto out_end; | |
151055ed BZ |
325 | |
326 | /* Assume it was a spurious irq */ | |
0a1248c5 | 327 | goto out_wait; |
151055ed | 328 | } |
901bd08a BZ |
329 | } else { |
330 | if (!OK_STAT(stat, DRIVE_READY, drive->bad_wstat)) | |
0a1248c5 | 331 | goto out_err; |
1da177e4 | 332 | |
901bd08a BZ |
333 | /* Deal with unexpected ATA data phase. */ |
334 | if (((stat & ATA_DRQ) == 0) ^ (cmd->nleft == 0)) | |
0a1248c5 | 335 | goto out_err; |
901bd08a BZ |
336 | } |
337 | ||
0a1248c5 BZ |
338 | if (write && cmd->nleft == 0) |
339 | goto out_end; | |
1da177e4 LT |
340 | |
341 | /* Still data left to transfer. */ | |
901bd08a | 342 | ide_pio_datablock(drive, cmd, write); |
1da177e4 | 343 | |
901bd08a BZ |
344 | /* Are we done? Check status and finish transfer. */ |
345 | if (write == 0 && cmd->nleft == 0) { | |
346 | stat = wait_drive_not_busy(drive); | |
347 | if (!OK_STAT(stat, 0, BAD_STAT)) | |
0a1248c5 | 348 | goto out_err; |
1da177e4 | 349 | |
0a1248c5 BZ |
350 | goto out_end; |
351 | } | |
352 | out_wait: | |
1da177e4 | 353 | /* Still data left to transfer. */ |
60c0cd02 | 354 | ide_set_handler(drive, &task_pio_intr, WAIT_WORSTCASE); |
1da177e4 | 355 | return ide_started; |
0a1248c5 | 356 | out_end: |
2230d90d BZ |
357 | if ((cmd->tf_flags & IDE_TFLAG_FS) == 0) |
358 | ide_finish_cmd(drive, cmd, stat); | |
359 | else | |
130e8867 | 360 | ide_complete_rq(drive, 0, cmd->rq->nr_sectors << 9); |
0a1248c5 BZ |
361 | return ide_stopped; |
362 | out_err: | |
041cea10 BZ |
363 | ide_error_cmd(drive, cmd); |
364 | return ide_error(drive, __func__, stat); | |
1da177e4 LT |
365 | } |
366 | ||
adb1af98 BZ |
367 | static ide_startstop_t pre_task_out_intr(ide_drive_t *drive, |
368 | struct ide_cmd *cmd) | |
1da177e4 LT |
369 | { |
370 | ide_startstop_t startstop; | |
371 | ||
3a7d2484 | 372 | if (ide_wait_stat(&startstop, drive, ATA_DRQ, |
1da177e4 LT |
373 | drive->bad_wstat, WAIT_DRQ)) { |
374 | printk(KERN_ERR "%s: no DRQ after issuing %sWRITE%s\n", | |
c7db966b | 375 | drive->name, |
0dfb991c | 376 | (cmd->tf_flags & IDE_TFLAG_MULTI_PIO) ? "MULT" : "", |
97100fc8 | 377 | (drive->dev_flags & IDE_DFLAG_LBA48) ? "_EXT" : ""); |
1da177e4 LT |
378 | return startstop; |
379 | } | |
380 | ||
97100fc8 | 381 | if ((drive->dev_flags & IDE_DFLAG_UNMASK) == 0) |
1da177e4 LT |
382 | local_irq_disable(); |
383 | ||
60c0cd02 | 384 | ide_set_handler(drive, &task_pio_intr, WAIT_WORSTCASE); |
901bd08a | 385 | |
adb1af98 | 386 | ide_pio_datablock(drive, cmd, 1); |
1da177e4 LT |
387 | |
388 | return ide_started; | |
389 | } | |
1da177e4 | 390 | |
22aa4b32 BZ |
391 | int ide_raw_taskfile(ide_drive_t *drive, struct ide_cmd *cmd, u8 *buf, |
392 | u16 nsect) | |
1da177e4 | 393 | { |
154ed280 FT |
394 | struct request *rq; |
395 | int error; | |
1da177e4 | 396 | |
154ed280 FT |
397 | rq = blk_get_request(drive->queue, READ, __GFP_WAIT); |
398 | rq->cmd_type = REQ_TYPE_ATA_TASKFILE; | |
399 | rq->buffer = buf; | |
1da177e4 LT |
400 | |
401 | /* | |
402 | * (ks) We transfer currently only whole sectors. | |
403 | * This is suffient for now. But, it would be great, | |
404 | * if we would find a solution to transfer any size. | |
405 | * To support special commands like READ LONG. | |
406 | */ | |
154ed280 FT |
407 | rq->hard_nr_sectors = rq->nr_sectors = nsect; |
408 | rq->hard_cur_sectors = rq->current_nr_sectors = nsect; | |
1da177e4 | 409 | |
22aa4b32 | 410 | if (cmd->tf_flags & IDE_TFLAG_WRITE) |
154ed280 | 411 | rq->cmd_flags |= REQ_RW; |
1da177e4 | 412 | |
22aa4b32 BZ |
413 | rq->special = cmd; |
414 | cmd->rq = rq; | |
1da177e4 | 415 | |
154ed280 FT |
416 | error = blk_execute_rq(drive->queue, NULL, rq, 0); |
417 | blk_put_request(rq); | |
418 | ||
419 | return error; | |
1da177e4 LT |
420 | } |
421 | ||
1da177e4 LT |
422 | EXPORT_SYMBOL(ide_raw_taskfile); |
423 | ||
22aa4b32 | 424 | int ide_no_data_taskfile(ide_drive_t *drive, struct ide_cmd *cmd) |
9a3c49be | 425 | { |
0dfb991c | 426 | cmd->protocol = ATA_PROT_NODATA; |
9a3c49be | 427 | |
22aa4b32 | 428 | return ide_raw_taskfile(drive, cmd, NULL, 0); |
9a3c49be BZ |
429 | } |
430 | EXPORT_SYMBOL_GPL(ide_no_data_taskfile); | |
431 | ||
26a5b040 | 432 | #ifdef CONFIG_IDE_TASK_IOCTL |
22aa4b32 | 433 | int ide_taskfile_ioctl(ide_drive_t *drive, unsigned long arg) |
1da177e4 LT |
434 | { |
435 | ide_task_request_t *req_task; | |
22aa4b32 | 436 | struct ide_cmd cmd; |
1da177e4 LT |
437 | u8 *outbuf = NULL; |
438 | u8 *inbuf = NULL; | |
ac026ff2 | 439 | u8 *data_buf = NULL; |
1da177e4 LT |
440 | int err = 0; |
441 | int tasksize = sizeof(struct ide_task_request_s); | |
3a42bb22 AC |
442 | unsigned int taskin = 0; |
443 | unsigned int taskout = 0; | |
ac026ff2 | 444 | u16 nsect = 0; |
1da177e4 LT |
445 | char __user *buf = (char __user *)arg; |
446 | ||
447 | // printk("IDE Taskfile ...\n"); | |
448 | ||
f5e3c2fa | 449 | req_task = kzalloc(tasksize, GFP_KERNEL); |
1da177e4 | 450 | if (req_task == NULL) return -ENOMEM; |
1da177e4 LT |
451 | if (copy_from_user(req_task, buf, tasksize)) { |
452 | kfree(req_task); | |
453 | return -EFAULT; | |
454 | } | |
455 | ||
3a42bb22 AC |
456 | taskout = req_task->out_size; |
457 | taskin = req_task->in_size; | |
458 | ||
459 | if (taskin > 65536 || taskout > 65536) { | |
460 | err = -EINVAL; | |
461 | goto abort; | |
462 | } | |
1da177e4 LT |
463 | |
464 | if (taskout) { | |
465 | int outtotal = tasksize; | |
f5e3c2fa | 466 | outbuf = kzalloc(taskout, GFP_KERNEL); |
1da177e4 LT |
467 | if (outbuf == NULL) { |
468 | err = -ENOMEM; | |
469 | goto abort; | |
470 | } | |
1da177e4 LT |
471 | if (copy_from_user(outbuf, buf + outtotal, taskout)) { |
472 | err = -EFAULT; | |
473 | goto abort; | |
474 | } | |
475 | } | |
476 | ||
477 | if (taskin) { | |
478 | int intotal = tasksize + taskout; | |
f5e3c2fa | 479 | inbuf = kzalloc(taskin, GFP_KERNEL); |
1da177e4 LT |
480 | if (inbuf == NULL) { |
481 | err = -ENOMEM; | |
482 | goto abort; | |
483 | } | |
1da177e4 LT |
484 | if (copy_from_user(inbuf, buf + intotal, taskin)) { |
485 | err = -EFAULT; | |
486 | goto abort; | |
487 | } | |
488 | } | |
489 | ||
22aa4b32 | 490 | memset(&cmd, 0, sizeof(cmd)); |
1da177e4 | 491 | |
745483f1 SS |
492 | memcpy(&cmd.hob, req_task->hob_ports, HDIO_DRIVE_HOB_HDR_SIZE - 2); |
493 | memcpy(&cmd.tf, req_task->io_ports, HDIO_DRIVE_TASK_HDR_SIZE); | |
866e2ec9 | 494 | |
60f85019 SS |
495 | cmd.valid.out.tf = IDE_VALID_DEVICE; |
496 | cmd.valid.in.tf = IDE_VALID_DEVICE | IDE_VALID_IN_TF; | |
497 | cmd.tf_flags = IDE_TFLAG_IO_16BIT; | |
1da177e4 | 498 | |
60f85019 SS |
499 | if (drive->dev_flags & IDE_DFLAG_LBA48) { |
500 | cmd.tf_flags |= IDE_TFLAG_LBA48; | |
501 | cmd.valid.in.hob = IDE_VALID_IN_HOB; | |
502 | } | |
a3bbb9d8 | 503 | |
74095a91 | 504 | if (req_task->out_flags.all) { |
22aa4b32 | 505 | cmd.ftf_flags |= IDE_FTFLAG_FLAGGED; |
74095a91 BZ |
506 | |
507 | if (req_task->out_flags.b.data) | |
22aa4b32 | 508 | cmd.ftf_flags |= IDE_FTFLAG_OUT_DATA; |
74095a91 BZ |
509 | |
510 | if (req_task->out_flags.b.nsector_hob) | |
60f85019 | 511 | cmd.valid.out.hob |= IDE_VALID_NSECT; |
74095a91 | 512 | if (req_task->out_flags.b.sector_hob) |
60f85019 | 513 | cmd.valid.out.hob |= IDE_VALID_LBAL; |
74095a91 | 514 | if (req_task->out_flags.b.lcyl_hob) |
60f85019 | 515 | cmd.valid.out.hob |= IDE_VALID_LBAM; |
74095a91 | 516 | if (req_task->out_flags.b.hcyl_hob) |
60f85019 | 517 | cmd.valid.out.hob |= IDE_VALID_LBAH; |
74095a91 BZ |
518 | |
519 | if (req_task->out_flags.b.error_feature) | |
60f85019 | 520 | cmd.valid.out.tf |= IDE_VALID_FEATURE; |
74095a91 | 521 | if (req_task->out_flags.b.nsector) |
60f85019 | 522 | cmd.valid.out.tf |= IDE_VALID_NSECT; |
74095a91 | 523 | if (req_task->out_flags.b.sector) |
60f85019 | 524 | cmd.valid.out.tf |= IDE_VALID_LBAL; |
74095a91 | 525 | if (req_task->out_flags.b.lcyl) |
60f85019 | 526 | cmd.valid.out.tf |= IDE_VALID_LBAM; |
74095a91 | 527 | if (req_task->out_flags.b.hcyl) |
60f85019 | 528 | cmd.valid.out.tf |= IDE_VALID_LBAH; |
a3bbb9d8 | 529 | } else { |
60f85019 | 530 | cmd.valid.out.tf |= IDE_VALID_OUT_TF; |
22aa4b32 | 531 | if (cmd.tf_flags & IDE_TFLAG_LBA48) |
60f85019 | 532 | cmd.valid.out.hob |= IDE_VALID_OUT_HOB; |
74095a91 BZ |
533 | } |
534 | ||
866e2ec9 | 535 | if (req_task->in_flags.b.data) |
22aa4b32 | 536 | cmd.ftf_flags |= IDE_FTFLAG_IN_DATA; |
866e2ec9 | 537 | |
04d09b0e BZ |
538 | if (req_task->req_cmd == IDE_DRIVE_TASK_RAW_WRITE) { |
539 | /* fixup data phase if needed */ | |
540 | if (req_task->data_phase == TASKFILE_IN_DMAQ || | |
541 | req_task->data_phase == TASKFILE_IN_DMA) | |
0dfb991c | 542 | cmd.tf_flags |= IDE_TFLAG_WRITE; |
04d09b0e BZ |
543 | } |
544 | ||
0dfb991c BZ |
545 | cmd.protocol = ATA_PROT_DMA; |
546 | ||
547 | switch (req_task->data_phase) { | |
1da177e4 LT |
548 | case TASKFILE_MULTI_OUT: |
549 | if (!drive->mult_count) { | |
550 | /* (hs): give up if multcount is not set */ | |
551 | printk(KERN_ERR "%s: %s Multimode Write " \ | |
552 | "multcount is not set\n", | |
eb63963a | 553 | drive->name, __func__); |
1da177e4 LT |
554 | err = -EPERM; |
555 | goto abort; | |
556 | } | |
0dfb991c | 557 | cmd.tf_flags |= IDE_TFLAG_MULTI_PIO; |
1da177e4 LT |
558 | /* fall through */ |
559 | case TASKFILE_OUT: | |
0dfb991c | 560 | cmd.protocol = ATA_PROT_PIO; |
ac026ff2 BZ |
561 | /* fall through */ |
562 | case TASKFILE_OUT_DMAQ: | |
563 | case TASKFILE_OUT_DMA: | |
04d09b0e | 564 | cmd.tf_flags |= IDE_TFLAG_WRITE; |
ac026ff2 BZ |
565 | nsect = taskout / SECTOR_SIZE; |
566 | data_buf = outbuf; | |
1da177e4 LT |
567 | break; |
568 | case TASKFILE_MULTI_IN: | |
569 | if (!drive->mult_count) { | |
570 | /* (hs): give up if multcount is not set */ | |
571 | printk(KERN_ERR "%s: %s Multimode Read failure " \ | |
572 | "multcount is not set\n", | |
eb63963a | 573 | drive->name, __func__); |
1da177e4 LT |
574 | err = -EPERM; |
575 | goto abort; | |
576 | } | |
0dfb991c | 577 | cmd.tf_flags |= IDE_TFLAG_MULTI_PIO; |
1da177e4 LT |
578 | /* fall through */ |
579 | case TASKFILE_IN: | |
0dfb991c | 580 | cmd.protocol = ATA_PROT_PIO; |
ac026ff2 BZ |
581 | /* fall through */ |
582 | case TASKFILE_IN_DMAQ: | |
583 | case TASKFILE_IN_DMA: | |
584 | nsect = taskin / SECTOR_SIZE; | |
585 | data_buf = inbuf; | |
1da177e4 LT |
586 | break; |
587 | case TASKFILE_NO_DATA: | |
0dfb991c | 588 | cmd.protocol = ATA_PROT_NODATA; |
1da177e4 LT |
589 | break; |
590 | default: | |
591 | err = -EFAULT; | |
592 | goto abort; | |
593 | } | |
594 | ||
ac026ff2 BZ |
595 | if (req_task->req_cmd == IDE_DRIVE_TASK_NO_DATA) |
596 | nsect = 0; | |
597 | else if (!nsect) { | |
745483f1 | 598 | nsect = (cmd.hob.nsect << 8) | cmd.tf.nsect; |
ac026ff2 BZ |
599 | |
600 | if (!nsect) { | |
601 | printk(KERN_ERR "%s: in/out command without data\n", | |
602 | drive->name); | |
603 | err = -EFAULT; | |
604 | goto abort; | |
605 | } | |
606 | } | |
607 | ||
22aa4b32 | 608 | err = ide_raw_taskfile(drive, &cmd, data_buf, nsect); |
ac026ff2 | 609 | |
745483f1 SS |
610 | memcpy(req_task->hob_ports, &cmd.hob, HDIO_DRIVE_HOB_HDR_SIZE - 2); |
611 | memcpy(req_task->io_ports, &cmd.tf, HDIO_DRIVE_TASK_HDR_SIZE); | |
866e2ec9 | 612 | |
22aa4b32 | 613 | if ((cmd.ftf_flags & IDE_FTFLAG_SET_IN_FLAGS) && |
866e2ec9 BZ |
614 | req_task->in_flags.all == 0) { |
615 | req_task->in_flags.all = IDE_TASKFILE_STD_IN_FLAGS; | |
97100fc8 | 616 | if (drive->dev_flags & IDE_DFLAG_LBA48) |
866e2ec9 BZ |
617 | req_task->in_flags.all |= (IDE_HOB_STD_IN_FLAGS << 8); |
618 | } | |
1da177e4 LT |
619 | |
620 | if (copy_to_user(buf, req_task, tasksize)) { | |
621 | err = -EFAULT; | |
622 | goto abort; | |
623 | } | |
624 | if (taskout) { | |
625 | int outtotal = tasksize; | |
626 | if (copy_to_user(buf + outtotal, outbuf, taskout)) { | |
627 | err = -EFAULT; | |
628 | goto abort; | |
629 | } | |
630 | } | |
631 | if (taskin) { | |
632 | int intotal = tasksize + taskout; | |
633 | if (copy_to_user(buf + intotal, inbuf, taskin)) { | |
634 | err = -EFAULT; | |
635 | goto abort; | |
636 | } | |
637 | } | |
638 | abort: | |
639 | kfree(req_task); | |
6044ec88 JJ |
640 | kfree(outbuf); |
641 | kfree(inbuf); | |
1da177e4 LT |
642 | |
643 | // printk("IDE Taskfile ioctl ended. rc = %i\n", err); | |
644 | ||
1da177e4 LT |
645 | return err; |
646 | } | |
26a5b040 | 647 | #endif |