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