blob: 2d63ea9ee61b3c56bb7e02fd2e43170094fa9422 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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.
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 */
12
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#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>
Andrew Morton651c29a2006-02-15 15:17:37 -080019#include <linux/sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#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>
Jens Axboe55c16a72007-07-25 08:13:56 +020031#include <linux/scatterlist.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
33#include <asm/byteorder.h>
34#include <asm/irq.h>
35#include <asm/uaccess.h>
36#include <asm/io.h>
37
Linus Torvalds1da177e2005-04-16 15:20:36 -070038static 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
48static 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
55static 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
Bartlomiej Zolnierkiewicz9e422372008-01-25 22:17:07 +010066void 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
Bartlomiej Zolnierkiewicz74095a92008-01-25 22:17:07 +010072 if (task->tf_flags & IDE_TFLAG_FLAGGED)
73 HIHI = 0xFF;
74
Bartlomiej Zolnierkiewicz807e35d2008-01-25 22:17:10 +010075#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
Bartlomiej Zolnierkiewicz9e422372008-01-25 22:17:07 +010082 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
Bartlomiej Zolnierkiewicz74095a92008-01-25 22:17:07 +010088 if (task->tf_flags & IDE_TFLAG_OUT_DATA)
89 hwif->OUTW((tf->hob_data << 8) | tf->data, IDE_DATA_REG);
Bartlomiej Zolnierkiewicz9e422372008-01-25 22:17:07 +010090
Bartlomiej Zolnierkiewicz74095a92008-01-25 22:17:07 +010091 if (task->tf_flags & IDE_TFLAG_OUT_HOB_FEATURE)
92 hwif->OUTB(tf->hob_feature, IDE_FEATURE_REG);
93 if (task->tf_flags & IDE_TFLAG_OUT_HOB_NSECT)
94 hwif->OUTB(tf->hob_nsect, IDE_NSECTOR_REG);
95 if (task->tf_flags & IDE_TFLAG_OUT_HOB_LBAL)
96 hwif->OUTB(tf->hob_lbal, IDE_SECTOR_REG);
97 if (task->tf_flags & IDE_TFLAG_OUT_HOB_LBAM)
98 hwif->OUTB(tf->hob_lbam, IDE_LCYL_REG);
99 if (task->tf_flags & IDE_TFLAG_OUT_HOB_LBAH)
100 hwif->OUTB(tf->hob_lbah, IDE_HCYL_REG);
101
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);
Bartlomiej Zolnierkiewicz9e422372008-01-25 22:17:07 +0100112
Bartlomiej Zolnierkiewicz807e35d2008-01-25 22:17:10 +0100113 if (task->tf_flags & IDE_TFLAG_OUT_DEVICE)
114 hwif->OUTB((tf->device & HIHI) | drive->select.all, IDE_SELECT_REG);
Bartlomiej Zolnierkiewicz9e422372008-01-25 22:17:07 +0100115}
116
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117int taskfile_lib_get_identify (ide_drive_t *drive, u8 *buf)
118{
119 ide_task_t args;
Bartlomiej Zolnierkiewicz650d8412008-01-25 22:17:06 +0100120
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 memset(&args, 0, sizeof(ide_task_t));
Bartlomiej Zolnierkiewicz650d8412008-01-25 22:17:06 +0100122 args.tf.nsect = 0x01;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 if (drive->media == ide_disk)
Bartlomiej Zolnierkiewicz650d8412008-01-25 22:17:06 +0100124 args.tf.command = WIN_IDENTIFY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 else
Bartlomiej Zolnierkiewicz650d8412008-01-25 22:17:06 +0100126 args.tf.command = WIN_PIDENTIFY;
Bartlomiej Zolnierkiewiczac026ff2008-01-25 22:17:14 +0100127 args.tf_flags = IDE_TFLAG_OUT_TF | IDE_TFLAG_OUT_DEVICE;
128 args.data_phase = TASKFILE_IN;
Bartlomiej Zolnierkiewiczac026ff2008-01-25 22:17:14 +0100129 return ide_raw_taskfile(drive, &args, buf, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130}
131
Bartlomiej Zolnierkiewicz74095a92008-01-25 22:17:07 +0100132static int inline task_dma_ok(ide_task_t *task)
133{
Bartlomiej Zolnierkiewiczf6e29e32008-01-25 22:17:16 +0100134 if (blk_fs_request(task->rq) || (task->tf_flags & IDE_TFLAG_FLAGGED))
Bartlomiej Zolnierkiewicz74095a92008-01-25 22:17:07 +0100135 return 1;
136
137 switch (task->tf.command) {
138 case WIN_WRITEDMA_ONCE:
139 case WIN_WRITEDMA:
140 case WIN_WRITEDMA_EXT:
141 case WIN_READDMA_ONCE:
142 case WIN_READDMA:
143 case WIN_READDMA_EXT:
144 case WIN_IDENTIFY_DMA:
145 return 1;
146 }
147
148 return 0;
149}
150
Bartlomiej Zolnierkiewicz1192e522008-01-25 22:17:16 +0100151static ide_startstop_t task_no_data_intr(ide_drive_t *);
Bartlomiej Zolnierkiewicz57d73662008-01-25 22:17:16 +0100152static ide_startstop_t set_geometry_intr(ide_drive_t *);
153static ide_startstop_t recal_intr(ide_drive_t *);
154static ide_startstop_t set_multmode_intr(ide_drive_t *);
Bartlomiej Zolnierkiewiczf6e29e32008-01-25 22:17:16 +0100155static ide_startstop_t pre_task_out_intr(ide_drive_t *, struct request *);
156static ide_startstop_t task_in_intr(ide_drive_t *);
Bartlomiej Zolnierkiewicz1192e522008-01-25 22:17:16 +0100157
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158ide_startstop_t do_rw_taskfile (ide_drive_t *drive, ide_task_t *task)
159{
160 ide_hwif_t *hwif = HWIF(drive);
Bartlomiej Zolnierkiewicz650d8412008-01-25 22:17:06 +0100161 struct ide_taskfile *tf = &task->tf;
Bartlomiej Zolnierkiewicz57d73662008-01-25 22:17:16 +0100162 ide_handler_t *handler = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
Bartlomiej Zolnierkiewicz1edee602008-01-25 22:17:15 +0100164 if (task->data_phase == TASKFILE_MULTI_IN ||
165 task->data_phase == TASKFILE_MULTI_OUT) {
166 if (!drive->mult_count) {
167 printk(KERN_ERR "%s: multimode not set!\n",
168 drive->name);
169 return ide_stopped;
170 }
171 }
172
173 if (task->tf_flags & IDE_TFLAG_FLAGGED)
174 task->tf_flags |= IDE_TFLAG_FLAGGED_SET_IN_FLAGS;
175
Bartlomiej Zolnierkiewiczf6e29e32008-01-25 22:17:16 +0100176 if ((task->tf_flags & IDE_TFLAG_DMA_PIO_FALLBACK) == 0)
177 ide_tf_load(drive, task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178
Bartlomiej Zolnierkiewicz10d90152008-01-25 22:17:16 +0100179 switch (task->data_phase) {
180 case TASKFILE_MULTI_OUT:
181 case TASKFILE_OUT:
182 hwif->OUTBSYNC(drive, tf->command, IDE_COMMAND_REG);
183 ndelay(400); /* FIXME */
184 return pre_task_out_intr(drive, task->rq);
185 case TASKFILE_MULTI_IN:
186 case TASKFILE_IN:
Bartlomiej Zolnierkiewicz57d73662008-01-25 22:17:16 +0100187 handler = task_in_intr;
Bartlomiej Zolnierkiewicz1192e522008-01-25 22:17:16 +0100188 /* fall-through */
Bartlomiej Zolnierkiewicz10d90152008-01-25 22:17:16 +0100189 case TASKFILE_NO_DATA:
Bartlomiej Zolnierkiewicz57d73662008-01-25 22:17:16 +0100190 if (handler == NULL)
191 handler = task_no_data_intr;
Bartlomiej Zolnierkiewicz1192e522008-01-25 22:17:16 +0100192 /* WIN_{SPECIFY,RESTORE,SETMULT} use custom handlers */
Bartlomiej Zolnierkiewicz57d73662008-01-25 22:17:16 +0100193 if (task->tf_flags & IDE_TFLAG_CUSTOM_HANDLER) {
194 switch (tf->command) {
195 case WIN_SPECIFY: handler = set_geometry_intr; break;
196 case WIN_RESTORE: handler = recal_intr; break;
197 case WIN_SETMULT: handler = set_multmode_intr; break;
198 }
199 }
200 ide_execute_command(drive, tf->command, handler,
201 WAIT_WORSTCASE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 return ide_started;
Bartlomiej Zolnierkiewicz10d90152008-01-25 22:17:16 +0100203 default:
204 if (task_dma_ok(task) == 0 || drive->using_dma == 0 ||
205 hwif->dma_setup(drive))
206 return ide_stopped;
Bartlomiej Zolnierkiewicz74095a92008-01-25 22:17:07 +0100207 hwif->dma_exec_cmd(drive, tf->command);
208 hwif->dma_start(drive);
209 return ide_started;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211}
Bartlomiej Zolnierkiewiczf6e29e32008-01-25 22:17:16 +0100212EXPORT_SYMBOL_GPL(do_rw_taskfile);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214/*
215 * set_multmode_intr() is invoked on completion of a WIN_SETMULT cmd.
216 */
Bartlomiej Zolnierkiewicz57d73662008-01-25 22:17:16 +0100217static ide_startstop_t set_multmode_intr(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218{
219 ide_hwif_t *hwif = HWIF(drive);
220 u8 stat;
221
222 if (OK_STAT(stat = hwif->INB(IDE_STATUS_REG),READY_STAT,BAD_STAT)) {
223 drive->mult_count = drive->mult_req;
224 } else {
225 drive->mult_req = drive->mult_count = 0;
226 drive->special.b.recalibrate = 1;
227 (void) ide_dump_status(drive, "set_multmode", stat);
228 }
229 return ide_stopped;
230}
231
232/*
233 * set_geometry_intr() is invoked on completion of a WIN_SPECIFY cmd.
234 */
Bartlomiej Zolnierkiewicz57d73662008-01-25 22:17:16 +0100235static ide_startstop_t set_geometry_intr(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236{
237 ide_hwif_t *hwif = HWIF(drive);
238 int retries = 5;
239 u8 stat;
240
241 while (((stat = hwif->INB(IDE_STATUS_REG)) & BUSY_STAT) && retries--)
242 udelay(10);
243
244 if (OK_STAT(stat, READY_STAT, BAD_STAT))
245 return ide_stopped;
246
247 if (stat & (ERR_STAT|DRQ_STAT))
248 return ide_error(drive, "set_geometry_intr", stat);
249
Eric Sesterhenn125e1872006-06-23 02:06:06 -0700250 BUG_ON(HWGROUP(drive)->handler != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 ide_set_handler(drive, &set_geometry_intr, WAIT_WORSTCASE, NULL);
252 return ide_started;
253}
254
255/*
256 * recal_intr() is invoked on completion of a WIN_RESTORE (recalibrate) cmd.
257 */
Bartlomiej Zolnierkiewicz57d73662008-01-25 22:17:16 +0100258static ide_startstop_t recal_intr(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259{
260 ide_hwif_t *hwif = HWIF(drive);
261 u8 stat;
262
263 if (!OK_STAT(stat = hwif->INB(IDE_STATUS_REG), READY_STAT, BAD_STAT))
264 return ide_error(drive, "recal_intr", stat);
265 return ide_stopped;
266}
267
268/*
269 * Handler for commands without a data phase
270 */
Bartlomiej Zolnierkiewicz1192e522008-01-25 22:17:16 +0100271static ide_startstop_t task_no_data_intr(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272{
273 ide_task_t *args = HWGROUP(drive)->rq->special;
274 ide_hwif_t *hwif = HWIF(drive);
275 u8 stat;
276
Ingo Molnar366c7f52006-07-03 00:25:25 -0700277 local_irq_enable_in_hardirq();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 if (!OK_STAT(stat = hwif->INB(IDE_STATUS_REG),READY_STAT,BAD_STAT)) {
279 return ide_error(drive, "task_no_data_intr", stat);
280 /* calls ide_end_drive_cmd */
281 }
282 if (args)
283 ide_end_drive_cmd(drive, stat, hwif->INB(IDE_ERROR_REG));
284
285 return ide_stopped;
286}
287
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288static u8 wait_drive_not_busy(ide_drive_t *drive)
289{
290 ide_hwif_t *hwif = HWIF(drive);
Masatake YAMATOb42fa132007-07-03 22:28:34 +0200291 int retries;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 u8 stat;
293
294 /*
295 * Last sector was transfered, wait until drive is ready.
296 * This can take up to 10 usec, but we will wait max 1 ms
297 * (drive_cmd_intr() waits that long).
298 */
Masatake YAMATOb42fa132007-07-03 22:28:34 +0200299 for (retries = 0; retries < 100; retries++) {
300 if ((stat = hwif->INB(IDE_STATUS_REG)) & BUSY_STAT)
301 udelay(10);
302 else
303 break;
304 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305
Masatake YAMATOb42fa132007-07-03 22:28:34 +0200306 if (stat & BUSY_STAT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 printk(KERN_ERR "%s: drive still BUSY!\n", drive->name);
308
309 return stat;
310}
311
312static void ide_pio_sector(ide_drive_t *drive, unsigned int write)
313{
314 ide_hwif_t *hwif = drive->hwif;
315 struct scatterlist *sg = hwif->sg_table;
Jens Axboe55c16a72007-07-25 08:13:56 +0200316 struct scatterlist *cursg = hwif->cursg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 struct page *page;
318#ifdef CONFIG_HIGHMEM
319 unsigned long flags;
320#endif
321 unsigned int offset;
322 u8 *buf;
323
Jens Axboe55c16a72007-07-25 08:13:56 +0200324 cursg = hwif->cursg;
325 if (!cursg) {
326 cursg = sg;
327 hwif->cursg = sg;
328 }
329
Jens Axboe45711f12007-10-22 21:19:53 +0200330 page = sg_page(cursg);
Jens Axboe55c16a72007-07-25 08:13:56 +0200331 offset = cursg->offset + hwif->cursg_ofs * SECTOR_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332
333 /* get the current page and offset */
334 page = nth_page(page, (offset >> PAGE_SHIFT));
335 offset %= PAGE_SIZE;
336
337#ifdef CONFIG_HIGHMEM
338 local_irq_save(flags);
339#endif
340 buf = kmap_atomic(page, KM_BIO_SRC_IRQ) + offset;
341
342 hwif->nleft--;
343 hwif->cursg_ofs++;
344
Jens Axboe55c16a72007-07-25 08:13:56 +0200345 if ((hwif->cursg_ofs * SECTOR_SIZE) == cursg->length) {
346 hwif->cursg = sg_next(hwif->cursg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 hwif->cursg_ofs = 0;
348 }
349
350 /* do the actual data transfer */
351 if (write)
352 taskfile_output_data(drive, buf, SECTOR_WORDS);
353 else
354 taskfile_input_data(drive, buf, SECTOR_WORDS);
355
356 kunmap_atomic(buf, KM_BIO_SRC_IRQ);
357#ifdef CONFIG_HIGHMEM
358 local_irq_restore(flags);
359#endif
360}
361
362static void ide_pio_multi(ide_drive_t *drive, unsigned int write)
363{
364 unsigned int nsect;
365
366 nsect = min_t(unsigned int, drive->hwif->nleft, drive->mult_count);
367 while (nsect--)
368 ide_pio_sector(drive, write);
369}
370
Arjan van de Ven858119e2006-01-14 13:20:43 -0800371static void ide_pio_datablock(ide_drive_t *drive, struct request *rq,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 unsigned int write)
373{
374 if (rq->bio) /* fs request */
375 rq->errors = 0;
376
Andrew Morton651c29a2006-02-15 15:17:37 -0800377 touch_softlockup_watchdog();
378
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 switch (drive->hwif->data_phase) {
380 case TASKFILE_MULTI_IN:
381 case TASKFILE_MULTI_OUT:
382 ide_pio_multi(drive, write);
383 break;
384 default:
385 ide_pio_sector(drive, write);
386 break;
387 }
388}
389
390static ide_startstop_t task_error(ide_drive_t *drive, struct request *rq,
391 const char *s, u8 stat)
392{
393 if (rq->bio) {
394 ide_hwif_t *hwif = drive->hwif;
395 int sectors = hwif->nsect - hwif->nleft;
396
397 switch (hwif->data_phase) {
398 case TASKFILE_IN:
399 if (hwif->nleft)
400 break;
401 /* fall through */
402 case TASKFILE_OUT:
403 sectors--;
404 break;
405 case TASKFILE_MULTI_IN:
406 if (hwif->nleft)
407 break;
408 /* fall through */
409 case TASKFILE_MULTI_OUT:
410 sectors -= drive->mult_count;
411 default:
412 break;
413 }
414
415 if (sectors > 0) {
416 ide_driver_t *drv;
417
418 drv = *(ide_driver_t **)rq->rq_disk->private_data;
419 drv->end_request(drive, 1, sectors);
420 }
421 }
422 return ide_error(drive, s, stat);
423}
424
425static void task_end_request(ide_drive_t *drive, struct request *rq, u8 stat)
426{
Jens Axboe55c16a72007-07-25 08:13:56 +0200427 HWIF(drive)->cursg = NULL;
428
Jens Axboe4aff5e22006-08-10 08:44:47 +0200429 if (rq->cmd_type == REQ_TYPE_ATA_TASKFILE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 ide_task_t *task = rq->special;
431
Bartlomiej Zolnierkiewicz74095a92008-01-25 22:17:07 +0100432 if (task->tf_flags & IDE_TFLAG_FLAGGED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 u8 err = drive->hwif->INB(IDE_ERROR_REG);
434 ide_end_drive_cmd(drive, stat, err);
435 return;
436 }
437 }
438
Richard Purdie03731fb2006-03-31 02:31:15 -0800439 if (rq->rq_disk) {
440 ide_driver_t *drv;
441
442 drv = *(ide_driver_t **)rq->rq_disk->private_data;;
443 drv->end_request(drive, 1, rq->hard_nr_sectors);
444 } else
445 ide_end_request(drive, 1, rq->hard_nr_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446}
447
448/*
449 * Handler for command with PIO data-in phase (Read/Read Multiple).
450 */
Bartlomiej Zolnierkiewiczf6e29e32008-01-25 22:17:16 +0100451static ide_startstop_t task_in_intr(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452{
453 ide_hwif_t *hwif = drive->hwif;
454 struct request *rq = HWGROUP(drive)->rq;
455 u8 stat = hwif->INB(IDE_STATUS_REG);
456
457 /* new way for dealing with premature shared PCI interrupts */
458 if (!OK_STAT(stat, DATA_READY, BAD_R_STAT)) {
459 if (stat & (ERR_STAT | DRQ_STAT))
460 return task_error(drive, rq, __FUNCTION__, stat);
461 /* No data yet, so wait for another IRQ. */
462 ide_set_handler(drive, &task_in_intr, WAIT_WORSTCASE, NULL);
463 return ide_started;
464 }
465
466 ide_pio_datablock(drive, rq, 0);
467
468 /* If it was the last datablock check status and finish transfer. */
469 if (!hwif->nleft) {
470 stat = wait_drive_not_busy(drive);
471 if (!OK_STAT(stat, 0, BAD_R_STAT))
472 return task_error(drive, rq, __FUNCTION__, stat);
473 task_end_request(drive, rq, stat);
474 return ide_stopped;
475 }
476
477 /* Still data left to transfer. */
478 ide_set_handler(drive, &task_in_intr, WAIT_WORSTCASE, NULL);
479
480 return ide_started;
481}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482
483/*
484 * Handler for command with PIO data-out phase (Write/Write Multiple).
485 */
486static ide_startstop_t task_out_intr (ide_drive_t *drive)
487{
488 ide_hwif_t *hwif = drive->hwif;
489 struct request *rq = HWGROUP(drive)->rq;
490 u8 stat = hwif->INB(IDE_STATUS_REG);
491
492 if (!OK_STAT(stat, DRIVE_READY, drive->bad_wstat))
493 return task_error(drive, rq, __FUNCTION__, stat);
494
495 /* Deal with unexpected ATA data phase. */
496 if (((stat & DRQ_STAT) == 0) ^ !hwif->nleft)
497 return task_error(drive, rq, __FUNCTION__, stat);
498
499 if (!hwif->nleft) {
500 task_end_request(drive, rq, stat);
501 return ide_stopped;
502 }
503
504 /* Still data left to transfer. */
505 ide_pio_datablock(drive, rq, 1);
506 ide_set_handler(drive, &task_out_intr, WAIT_WORSTCASE, NULL);
507
508 return ide_started;
509}
510
Bartlomiej Zolnierkiewiczf6e29e32008-01-25 22:17:16 +0100511static ide_startstop_t pre_task_out_intr(ide_drive_t *drive, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512{
513 ide_startstop_t startstop;
514
515 if (ide_wait_stat(&startstop, drive, DATA_READY,
516 drive->bad_wstat, WAIT_DRQ)) {
517 printk(KERN_ERR "%s: no DRQ after issuing %sWRITE%s\n",
518 drive->name,
519 drive->hwif->data_phase ? "MULT" : "",
520 drive->addressing ? "_EXT" : "");
521 return startstop;
522 }
523
524 if (!drive->unmask)
525 local_irq_disable();
526
527 ide_set_handler(drive, &task_out_intr, WAIT_WORSTCASE, NULL);
528 ide_pio_datablock(drive, rq, 1);
529
530 return ide_started;
531}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532
Bartlomiej Zolnierkiewiczac026ff2008-01-25 22:17:14 +0100533int ide_raw_taskfile(ide_drive_t *drive, ide_task_t *task, u8 *buf, u16 nsect)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534{
535 struct request rq;
536
537 memset(&rq, 0, sizeof(rq));
Bartlomiej Zolnierkiewicz02ac2462007-11-05 21:42:27 +0100538 rq.ref_count = 1;
Jens Axboe4aff5e22006-08-10 08:44:47 +0200539 rq.cmd_type = REQ_TYPE_ATA_TASKFILE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 rq.buffer = buf;
541
542 /*
543 * (ks) We transfer currently only whole sectors.
544 * This is suffient for now. But, it would be great,
545 * if we would find a solution to transfer any size.
546 * To support special commands like READ LONG.
547 */
Bartlomiej Zolnierkiewiczac026ff2008-01-25 22:17:14 +0100548 rq.hard_nr_sectors = rq.nr_sectors = nsect;
549 rq.hard_cur_sectors = rq.current_nr_sectors = nsect;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550
Bartlomiej Zolnierkiewiczac026ff2008-01-25 22:17:14 +0100551 if (task->tf_flags & IDE_TFLAG_WRITE)
552 rq.cmd_flags |= REQ_RW;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553
Bartlomiej Zolnierkiewiczac026ff2008-01-25 22:17:14 +0100554 rq.special = task;
555 task->rq = &rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 return ide_do_drive_cmd(drive, &rq, ide_wait);
558}
559
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560EXPORT_SYMBOL(ide_raw_taskfile);
561
Bartlomiej Zolnierkiewicz9a3c49b2008-01-25 22:17:07 +0100562int ide_no_data_taskfile(ide_drive_t *drive, ide_task_t *task)
563{
Bartlomiej Zolnierkiewiczac026ff2008-01-25 22:17:14 +0100564 task->data_phase = TASKFILE_NO_DATA;
Bartlomiej Zolnierkiewicz9a3c49b2008-01-25 22:17:07 +0100565
Bartlomiej Zolnierkiewiczac026ff2008-01-25 22:17:14 +0100566 return ide_raw_taskfile(drive, task, NULL, 0);
Bartlomiej Zolnierkiewicz9a3c49b2008-01-25 22:17:07 +0100567}
568EXPORT_SYMBOL_GPL(ide_no_data_taskfile);
569
Bartlomiej Zolnierkiewicz26a5b042007-11-05 21:42:27 +0100570#ifdef CONFIG_IDE_TASK_IOCTL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571int ide_taskfile_ioctl (ide_drive_t *drive, unsigned int cmd, unsigned long arg)
572{
573 ide_task_request_t *req_task;
574 ide_task_t args;
575 u8 *outbuf = NULL;
576 u8 *inbuf = NULL;
Bartlomiej Zolnierkiewiczac026ff2008-01-25 22:17:14 +0100577 u8 *data_buf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 int err = 0;
579 int tasksize = sizeof(struct ide_task_request_s);
Alan Cox3a42bb22006-10-16 16:31:02 +0100580 unsigned int taskin = 0;
581 unsigned int taskout = 0;
Bartlomiej Zolnierkiewiczac026ff2008-01-25 22:17:14 +0100582 u16 nsect = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 u8 io_32bit = drive->io_32bit;
584 char __user *buf = (char __user *)arg;
585
586// printk("IDE Taskfile ...\n");
587
Deepak Saxenaf5e3c2f2005-11-07 01:01:25 -0800588 req_task = kzalloc(tasksize, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 if (req_task == NULL) return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 if (copy_from_user(req_task, buf, tasksize)) {
591 kfree(req_task);
592 return -EFAULT;
593 }
594
Alan Cox3a42bb22006-10-16 16:31:02 +0100595 taskout = req_task->out_size;
596 taskin = req_task->in_size;
597
598 if (taskin > 65536 || taskout > 65536) {
599 err = -EINVAL;
600 goto abort;
601 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602
603 if (taskout) {
604 int outtotal = tasksize;
Deepak Saxenaf5e3c2f2005-11-07 01:01:25 -0800605 outbuf = kzalloc(taskout, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 if (outbuf == NULL) {
607 err = -ENOMEM;
608 goto abort;
609 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 if (copy_from_user(outbuf, buf + outtotal, taskout)) {
611 err = -EFAULT;
612 goto abort;
613 }
614 }
615
616 if (taskin) {
617 int intotal = tasksize + taskout;
Deepak Saxenaf5e3c2f2005-11-07 01:01:25 -0800618 inbuf = kzalloc(taskin, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 if (inbuf == NULL) {
620 err = -ENOMEM;
621 goto abort;
622 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 if (copy_from_user(inbuf, buf + intotal, taskin)) {
624 err = -EFAULT;
625 goto abort;
626 }
627 }
628
629 memset(&args, 0, sizeof(ide_task_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630
Bartlomiej Zolnierkiewicz650d8412008-01-25 22:17:06 +0100631 memcpy(&args.tf_array[0], req_task->hob_ports, HDIO_DRIVE_HOB_HDR_SIZE - 2);
632 memcpy(&args.tf_array[6], req_task->io_ports, HDIO_DRIVE_TASK_HDR_SIZE);
Bartlomiej Zolnierkiewicz866e2ec2008-01-25 22:17:14 +0100633
634 args.data_phase = req_task->data_phase;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635
Bartlomiej Zolnierkiewicza3bbb9d2008-01-25 22:17:10 +0100636 args.tf_flags = IDE_TFLAG_OUT_DEVICE;
637 if (drive->addressing == 1)
638 args.tf_flags |= IDE_TFLAG_LBA48;
639
Bartlomiej Zolnierkiewicz74095a92008-01-25 22:17:07 +0100640 if (req_task->out_flags.all) {
641 args.tf_flags |= IDE_TFLAG_FLAGGED;
642
643 if (req_task->out_flags.b.data)
644 args.tf_flags |= IDE_TFLAG_OUT_DATA;
645
646 if (req_task->out_flags.b.nsector_hob)
647 args.tf_flags |= IDE_TFLAG_OUT_HOB_NSECT;
648 if (req_task->out_flags.b.sector_hob)
649 args.tf_flags |= IDE_TFLAG_OUT_HOB_LBAL;
650 if (req_task->out_flags.b.lcyl_hob)
651 args.tf_flags |= IDE_TFLAG_OUT_HOB_LBAM;
652 if (req_task->out_flags.b.hcyl_hob)
653 args.tf_flags |= IDE_TFLAG_OUT_HOB_LBAH;
654
655 if (req_task->out_flags.b.error_feature)
656 args.tf_flags |= IDE_TFLAG_OUT_FEATURE;
657 if (req_task->out_flags.b.nsector)
658 args.tf_flags |= IDE_TFLAG_OUT_NSECT;
659 if (req_task->out_flags.b.sector)
660 args.tf_flags |= IDE_TFLAG_OUT_LBAL;
661 if (req_task->out_flags.b.lcyl)
662 args.tf_flags |= IDE_TFLAG_OUT_LBAM;
663 if (req_task->out_flags.b.hcyl)
664 args.tf_flags |= IDE_TFLAG_OUT_LBAH;
Bartlomiej Zolnierkiewicza3bbb9d2008-01-25 22:17:10 +0100665 } else {
666 args.tf_flags |= IDE_TFLAG_OUT_TF;
667 if (args.tf_flags & IDE_TFLAG_LBA48)
668 args.tf_flags |= IDE_TFLAG_OUT_HOB;
Bartlomiej Zolnierkiewicz74095a92008-01-25 22:17:07 +0100669 }
670
Bartlomiej Zolnierkiewicz866e2ec2008-01-25 22:17:14 +0100671 if (req_task->in_flags.b.data)
672 args.tf_flags |= IDE_TFLAG_IN_DATA;
673
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 drive->io_32bit = 0;
675 switch(req_task->data_phase) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 case TASKFILE_MULTI_OUT:
677 if (!drive->mult_count) {
678 /* (hs): give up if multcount is not set */
679 printk(KERN_ERR "%s: %s Multimode Write " \
680 "multcount is not set\n",
681 drive->name, __FUNCTION__);
682 err = -EPERM;
683 goto abort;
684 }
685 /* fall through */
686 case TASKFILE_OUT:
Bartlomiej Zolnierkiewiczac026ff2008-01-25 22:17:14 +0100687 /* fall through */
688 case TASKFILE_OUT_DMAQ:
689 case TASKFILE_OUT_DMA:
690 nsect = taskout / SECTOR_SIZE;
691 data_buf = outbuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 break;
693 case TASKFILE_MULTI_IN:
694 if (!drive->mult_count) {
695 /* (hs): give up if multcount is not set */
696 printk(KERN_ERR "%s: %s Multimode Read failure " \
697 "multcount is not set\n",
698 drive->name, __FUNCTION__);
699 err = -EPERM;
700 goto abort;
701 }
702 /* fall through */
703 case TASKFILE_IN:
Bartlomiej Zolnierkiewiczac026ff2008-01-25 22:17:14 +0100704 /* fall through */
705 case TASKFILE_IN_DMAQ:
706 case TASKFILE_IN_DMA:
707 nsect = taskin / SECTOR_SIZE;
708 data_buf = inbuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 break;
710 case TASKFILE_NO_DATA:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 break;
712 default:
713 err = -EFAULT;
714 goto abort;
715 }
716
Bartlomiej Zolnierkiewiczac026ff2008-01-25 22:17:14 +0100717 if (req_task->req_cmd == IDE_DRIVE_TASK_NO_DATA)
718 nsect = 0;
719 else if (!nsect) {
720 nsect = (args.tf.hob_nsect << 8) | args.tf.nsect;
721
722 if (!nsect) {
723 printk(KERN_ERR "%s: in/out command without data\n",
724 drive->name);
725 err = -EFAULT;
726 goto abort;
727 }
728 }
729
730 if (req_task->req_cmd == IDE_DRIVE_TASK_RAW_WRITE)
731 args.tf_flags |= IDE_TFLAG_WRITE;
732
733 err = ide_raw_taskfile(drive, &args, data_buf, nsect);
734
Bartlomiej Zolnierkiewicz650d8412008-01-25 22:17:06 +0100735 memcpy(req_task->hob_ports, &args.tf_array[0], HDIO_DRIVE_HOB_HDR_SIZE - 2);
736 memcpy(req_task->io_ports, &args.tf_array[6], HDIO_DRIVE_TASK_HDR_SIZE);
Bartlomiej Zolnierkiewicz866e2ec2008-01-25 22:17:14 +0100737
738 if ((args.tf_flags & IDE_TFLAG_FLAGGED_SET_IN_FLAGS) &&
739 req_task->in_flags.all == 0) {
740 req_task->in_flags.all = IDE_TASKFILE_STD_IN_FLAGS;
741 if (drive->addressing == 1)
742 req_task->in_flags.all |= (IDE_HOB_STD_IN_FLAGS << 8);
743 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744
745 if (copy_to_user(buf, req_task, tasksize)) {
746 err = -EFAULT;
747 goto abort;
748 }
749 if (taskout) {
750 int outtotal = tasksize;
751 if (copy_to_user(buf + outtotal, outbuf, taskout)) {
752 err = -EFAULT;
753 goto abort;
754 }
755 }
756 if (taskin) {
757 int intotal = tasksize + taskout;
758 if (copy_to_user(buf + intotal, inbuf, taskin)) {
759 err = -EFAULT;
760 goto abort;
761 }
762 }
763abort:
764 kfree(req_task);
Jesper Juhl6044ec82005-11-07 01:01:32 -0800765 kfree(outbuf);
766 kfree(inbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767
768// printk("IDE Taskfile ioctl ended. rc = %i\n", err);
769
770 drive->io_32bit = io_32bit;
771
772 return err;
773}
Bartlomiej Zolnierkiewicz26a5b042007-11-05 21:42:27 +0100774#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775
776int ide_wait_cmd (ide_drive_t *drive, u8 cmd, u8 nsect, u8 feature, u8 sectors, u8 *buf)
777{
778 struct request rq;
779 u8 buffer[4];
780
781 if (!buf)
782 buf = buffer;
783 memset(buf, 0, 4 + SECTOR_WORDS * 4 * sectors);
784 ide_init_drive_cmd(&rq);
785 rq.buffer = buf;
786 *buf++ = cmd;
787 *buf++ = nsect;
788 *buf++ = feature;
789 *buf++ = sectors;
790 return ide_do_drive_cmd(drive, &rq, ide_wait);
791}
792
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793int ide_cmd_ioctl (ide_drive_t *drive, unsigned int cmd, unsigned long arg)
794{
795 int err = 0;
796 u8 args[4], *argbuf = args;
797 u8 xfer_rate = 0;
798 int argsize = 4;
799 ide_task_t tfargs;
Bartlomiej Zolnierkiewicz650d8412008-01-25 22:17:06 +0100800 struct ide_taskfile *tf = &tfargs.tf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801
802 if (NULL == (void *) arg) {
803 struct request rq;
804 ide_init_drive_cmd(&rq);
805 return ide_do_drive_cmd(drive, &rq, ide_wait);
806 }
807
808 if (copy_from_user(args, (void __user *)arg, 4))
809 return -EFAULT;
810
811 memset(&tfargs, 0, sizeof(ide_task_t));
Bartlomiej Zolnierkiewicz650d8412008-01-25 22:17:06 +0100812 tf->feature = args[2];
813 tf->nsect = args[3];
814 tf->lbal = args[1];
815 tf->command = args[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816
817 if (args[3]) {
818 argsize = 4 + (SECTOR_WORDS * 4 * args[3]);
Deepak Saxenaf5e3c2f2005-11-07 01:01:25 -0800819 argbuf = kzalloc(argsize, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 if (argbuf == NULL)
821 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 }
823 if (set_transfer(drive, &tfargs)) {
824 xfer_rate = args[1];
825 if (ide_ata66_check(drive, &tfargs))
826 goto abort;
827 }
828
829 err = ide_wait_cmd(drive, args[0], args[1], args[2], args[3], argbuf);
830
831 if (!err && xfer_rate) {
832 /* active-retuning-calls future */
833 ide_set_xfer_rate(drive, xfer_rate);
834 ide_driveid_update(drive);
835 }
836abort:
837 if (copy_to_user((void __user *)arg, argbuf, argsize))
838 err = -EFAULT;
839 if (argsize > 4)
840 kfree(argbuf);
841 return err;
842}
843
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844int ide_task_ioctl (ide_drive_t *drive, unsigned int cmd, unsigned long arg)
845{
846 void __user *p = (void __user *)arg;
847 int err = 0;
Bartlomiej Zolnierkiewicz14b89ef2008-01-25 22:17:11 +0100848 u8 args[7];
849 ide_task_t task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850
851 if (copy_from_user(args, p, 7))
852 return -EFAULT;
Bartlomiej Zolnierkiewicz14b89ef2008-01-25 22:17:11 +0100853
854 memset(&task, 0, sizeof(task));
855 memcpy(&task.tf_array[7], &args[1], 6);
856 task.tf.command = args[0];
857 task.tf_flags = IDE_TFLAG_OUT_TF | IDE_TFLAG_OUT_DEVICE;
858
859 err = ide_no_data_taskfile(drive, &task);
860
861 args[0] = task.tf.command;
862 memcpy(&args[1], &task.tf_array[7], 6);
863
864 if (copy_to_user(p, args, 7))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 err = -EFAULT;
Bartlomiej Zolnierkiewicz14b89ef2008-01-25 22:17:11 +0100866
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 return err;
868}