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