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