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); |
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) { | |
87 | u8 data[2] = { tf->data, tf->hob_data }; | |
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 | |
22aa4b32 BZ |
492 | memcpy(&cmd.tf_array[0], req_task->hob_ports, |
493 | HDIO_DRIVE_HOB_HDR_SIZE - 2); | |
494 | memcpy(&cmd.tf_array[6], req_task->io_ports, | |
495 | HDIO_DRIVE_TASK_HDR_SIZE); | |
866e2ec9 | 496 | |
22aa4b32 BZ |
497 | cmd.tf_flags = IDE_TFLAG_IO_16BIT | IDE_TFLAG_DEVICE | |
498 | IDE_TFLAG_IN_TF; | |
1da177e4 | 499 | |
97100fc8 | 500 | if (drive->dev_flags & IDE_DFLAG_LBA48) |
22aa4b32 | 501 | cmd.tf_flags |= (IDE_TFLAG_LBA48 | IDE_TFLAG_IN_HOB); |
a3bbb9d8 | 502 | |
74095a91 | 503 | if (req_task->out_flags.all) { |
22aa4b32 | 504 | cmd.ftf_flags |= IDE_FTFLAG_FLAGGED; |
74095a91 BZ |
505 | |
506 | if (req_task->out_flags.b.data) | |
22aa4b32 | 507 | cmd.ftf_flags |= IDE_FTFLAG_OUT_DATA; |
74095a91 BZ |
508 | |
509 | if (req_task->out_flags.b.nsector_hob) | |
22aa4b32 | 510 | cmd.tf_flags |= IDE_TFLAG_OUT_HOB_NSECT; |
74095a91 | 511 | if (req_task->out_flags.b.sector_hob) |
22aa4b32 | 512 | cmd.tf_flags |= IDE_TFLAG_OUT_HOB_LBAL; |
74095a91 | 513 | if (req_task->out_flags.b.lcyl_hob) |
22aa4b32 | 514 | cmd.tf_flags |= IDE_TFLAG_OUT_HOB_LBAM; |
74095a91 | 515 | if (req_task->out_flags.b.hcyl_hob) |
22aa4b32 | 516 | cmd.tf_flags |= IDE_TFLAG_OUT_HOB_LBAH; |
74095a91 BZ |
517 | |
518 | if (req_task->out_flags.b.error_feature) | |
22aa4b32 | 519 | cmd.tf_flags |= IDE_TFLAG_OUT_FEATURE; |
74095a91 | 520 | if (req_task->out_flags.b.nsector) |
22aa4b32 | 521 | cmd.tf_flags |= IDE_TFLAG_OUT_NSECT; |
74095a91 | 522 | if (req_task->out_flags.b.sector) |
22aa4b32 | 523 | cmd.tf_flags |= IDE_TFLAG_OUT_LBAL; |
74095a91 | 524 | if (req_task->out_flags.b.lcyl) |
22aa4b32 | 525 | cmd.tf_flags |= IDE_TFLAG_OUT_LBAM; |
74095a91 | 526 | if (req_task->out_flags.b.hcyl) |
22aa4b32 | 527 | cmd.tf_flags |= IDE_TFLAG_OUT_LBAH; |
a3bbb9d8 | 528 | } else { |
22aa4b32 BZ |
529 | cmd.tf_flags |= IDE_TFLAG_OUT_TF; |
530 | if (cmd.tf_flags & IDE_TFLAG_LBA48) | |
531 | cmd.tf_flags |= IDE_TFLAG_OUT_HOB; | |
74095a91 BZ |
532 | } |
533 | ||
866e2ec9 | 534 | if (req_task->in_flags.b.data) |
22aa4b32 | 535 | cmd.ftf_flags |= IDE_FTFLAG_IN_DATA; |
866e2ec9 | 536 | |
04d09b0e BZ |
537 | if (req_task->req_cmd == IDE_DRIVE_TASK_RAW_WRITE) { |
538 | /* fixup data phase if needed */ | |
539 | if (req_task->data_phase == TASKFILE_IN_DMAQ || | |
540 | req_task->data_phase == TASKFILE_IN_DMA) | |
0dfb991c | 541 | cmd.tf_flags |= IDE_TFLAG_WRITE; |
04d09b0e BZ |
542 | } |
543 | ||
0dfb991c BZ |
544 | cmd.protocol = ATA_PROT_DMA; |
545 | ||
546 | switch (req_task->data_phase) { | |
1da177e4 LT |
547 | case TASKFILE_MULTI_OUT: |
548 | if (!drive->mult_count) { | |
549 | /* (hs): give up if multcount is not set */ | |
550 | printk(KERN_ERR "%s: %s Multimode Write " \ | |
551 | "multcount is not set\n", | |
eb63963a | 552 | drive->name, __func__); |
1da177e4 LT |
553 | err = -EPERM; |
554 | goto abort; | |
555 | } | |
0dfb991c | 556 | cmd.tf_flags |= IDE_TFLAG_MULTI_PIO; |
1da177e4 LT |
557 | /* fall through */ |
558 | case TASKFILE_OUT: | |
0dfb991c | 559 | cmd.protocol = ATA_PROT_PIO; |
ac026ff2 BZ |
560 | /* fall through */ |
561 | case TASKFILE_OUT_DMAQ: | |
562 | case TASKFILE_OUT_DMA: | |
04d09b0e | 563 | cmd.tf_flags |= IDE_TFLAG_WRITE; |
ac026ff2 BZ |
564 | nsect = taskout / SECTOR_SIZE; |
565 | data_buf = outbuf; | |
1da177e4 LT |
566 | break; |
567 | case TASKFILE_MULTI_IN: | |
568 | if (!drive->mult_count) { | |
569 | /* (hs): give up if multcount is not set */ | |
570 | printk(KERN_ERR "%s: %s Multimode Read failure " \ | |
571 | "multcount is not set\n", | |
eb63963a | 572 | drive->name, __func__); |
1da177e4 LT |
573 | err = -EPERM; |
574 | goto abort; | |
575 | } | |
0dfb991c | 576 | cmd.tf_flags |= IDE_TFLAG_MULTI_PIO; |
1da177e4 LT |
577 | /* fall through */ |
578 | case TASKFILE_IN: | |
0dfb991c | 579 | cmd.protocol = ATA_PROT_PIO; |
ac026ff2 BZ |
580 | /* fall through */ |
581 | case TASKFILE_IN_DMAQ: | |
582 | case TASKFILE_IN_DMA: | |
583 | nsect = taskin / SECTOR_SIZE; | |
584 | data_buf = inbuf; | |
1da177e4 LT |
585 | break; |
586 | case TASKFILE_NO_DATA: | |
0dfb991c | 587 | cmd.protocol = ATA_PROT_NODATA; |
1da177e4 LT |
588 | break; |
589 | default: | |
590 | err = -EFAULT; | |
591 | goto abort; | |
592 | } | |
593 | ||
ac026ff2 BZ |
594 | if (req_task->req_cmd == IDE_DRIVE_TASK_NO_DATA) |
595 | nsect = 0; | |
596 | else if (!nsect) { | |
22aa4b32 | 597 | nsect = (cmd.tf.hob_nsect << 8) | cmd.tf.nsect; |
ac026ff2 BZ |
598 | |
599 | if (!nsect) { | |
600 | printk(KERN_ERR "%s: in/out command without data\n", | |
601 | drive->name); | |
602 | err = -EFAULT; | |
603 | goto abort; | |
604 | } | |
605 | } | |
606 | ||
22aa4b32 | 607 | err = ide_raw_taskfile(drive, &cmd, data_buf, nsect); |
ac026ff2 | 608 | |
22aa4b32 BZ |
609 | memcpy(req_task->hob_ports, &cmd.tf_array[0], |
610 | HDIO_DRIVE_HOB_HDR_SIZE - 2); | |
611 | memcpy(req_task->io_ports, &cmd.tf_array[6], | |
612 | HDIO_DRIVE_TASK_HDR_SIZE); | |
866e2ec9 | 613 | |
22aa4b32 | 614 | if ((cmd.ftf_flags & IDE_FTFLAG_SET_IN_FLAGS) && |
866e2ec9 BZ |
615 | req_task->in_flags.all == 0) { |
616 | req_task->in_flags.all = IDE_TASKFILE_STD_IN_FLAGS; | |
97100fc8 | 617 | if (drive->dev_flags & IDE_DFLAG_LBA48) |
866e2ec9 BZ |
618 | req_task->in_flags.all |= (IDE_HOB_STD_IN_FLAGS << 8); |
619 | } | |
1da177e4 LT |
620 | |
621 | if (copy_to_user(buf, req_task, tasksize)) { | |
622 | err = -EFAULT; | |
623 | goto abort; | |
624 | } | |
625 | if (taskout) { | |
626 | int outtotal = tasksize; | |
627 | if (copy_to_user(buf + outtotal, outbuf, taskout)) { | |
628 | err = -EFAULT; | |
629 | goto abort; | |
630 | } | |
631 | } | |
632 | if (taskin) { | |
633 | int intotal = tasksize + taskout; | |
634 | if (copy_to_user(buf + intotal, inbuf, taskin)) { | |
635 | err = -EFAULT; | |
636 | goto abort; | |
637 | } | |
638 | } | |
639 | abort: | |
640 | kfree(req_task); | |
6044ec88 JJ |
641 | kfree(outbuf); |
642 | kfree(inbuf); | |
1da177e4 LT |
643 | |
644 | // printk("IDE Taskfile ioctl ended. rc = %i\n", err); | |
645 | ||
1da177e4 LT |
646 | return err; |
647 | } | |
26a5b040 | 648 | #endif |