blob: c58edc86ed3eb8d1722e3b19abc565803409d35c [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);
Bartlomiej Zolnierkiewicz6dd9b832008-01-26 20:13:03 +010080 printk("%s: hob: nsect 0x%02x lbal 0x%02x "
81 "lbam 0x%02x lbah 0x%02x\n",
82 drive->name, tf->hob_nsect, tf->hob_lbal,
83 tf->hob_lbam, tf->hob_lbah);
Bartlomiej Zolnierkiewicz807e35d2008-01-25 22:17:10 +010084#endif
85
Bartlomiej Zolnierkiewicz81ca6912008-01-26 20:13:08 +010086 ide_set_irq(drive, 1);
Bartlomiej Zolnierkiewicz9e422372008-01-25 22:17:07 +010087
88 if ((task->tf_flags & IDE_TFLAG_NO_SELECT_MASK) == 0)
89 SELECT_MASK(drive, 0);
90
Bartlomiej Zolnierkiewicz74095a92008-01-25 22:17:07 +010091 if (task->tf_flags & IDE_TFLAG_OUT_DATA)
92 hwif->OUTW((tf->hob_data << 8) | tf->data, IDE_DATA_REG);
Bartlomiej Zolnierkiewicz9e422372008-01-25 22:17:07 +010093
Bartlomiej Zolnierkiewicz74095a92008-01-25 22:17:07 +010094 if (task->tf_flags & IDE_TFLAG_OUT_HOB_FEATURE)
95 hwif->OUTB(tf->hob_feature, IDE_FEATURE_REG);
96 if (task->tf_flags & IDE_TFLAG_OUT_HOB_NSECT)
97 hwif->OUTB(tf->hob_nsect, IDE_NSECTOR_REG);
98 if (task->tf_flags & IDE_TFLAG_OUT_HOB_LBAL)
99 hwif->OUTB(tf->hob_lbal, IDE_SECTOR_REG);
100 if (task->tf_flags & IDE_TFLAG_OUT_HOB_LBAM)
101 hwif->OUTB(tf->hob_lbam, IDE_LCYL_REG);
102 if (task->tf_flags & IDE_TFLAG_OUT_HOB_LBAH)
103 hwif->OUTB(tf->hob_lbah, IDE_HCYL_REG);
104
105 if (task->tf_flags & IDE_TFLAG_OUT_FEATURE)
106 hwif->OUTB(tf->feature, IDE_FEATURE_REG);
107 if (task->tf_flags & IDE_TFLAG_OUT_NSECT)
108 hwif->OUTB(tf->nsect, IDE_NSECTOR_REG);
109 if (task->tf_flags & IDE_TFLAG_OUT_LBAL)
110 hwif->OUTB(tf->lbal, IDE_SECTOR_REG);
111 if (task->tf_flags & IDE_TFLAG_OUT_LBAM)
112 hwif->OUTB(tf->lbam, IDE_LCYL_REG);
113 if (task->tf_flags & IDE_TFLAG_OUT_LBAH)
114 hwif->OUTB(tf->lbah, IDE_HCYL_REG);
Bartlomiej Zolnierkiewicz9e422372008-01-25 22:17:07 +0100115
Bartlomiej Zolnierkiewicz807e35d2008-01-25 22:17:10 +0100116 if (task->tf_flags & IDE_TFLAG_OUT_DEVICE)
117 hwif->OUTB((tf->device & HIHI) | drive->select.all, IDE_SELECT_REG);
Bartlomiej Zolnierkiewicz9e422372008-01-25 22:17:07 +0100118}
119
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120int taskfile_lib_get_identify (ide_drive_t *drive, u8 *buf)
121{
122 ide_task_t args;
Bartlomiej Zolnierkiewicz650d8412008-01-25 22:17:06 +0100123
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 memset(&args, 0, sizeof(ide_task_t));
Bartlomiej Zolnierkiewicz650d8412008-01-25 22:17:06 +0100125 args.tf.nsect = 0x01;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 if (drive->media == ide_disk)
Bartlomiej Zolnierkiewicz650d8412008-01-25 22:17:06 +0100127 args.tf.command = WIN_IDENTIFY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 else
Bartlomiej Zolnierkiewicz650d8412008-01-25 22:17:06 +0100129 args.tf.command = WIN_PIDENTIFY;
Bartlomiej Zolnierkiewiczac026ff2008-01-25 22:17:14 +0100130 args.tf_flags = IDE_TFLAG_OUT_TF | IDE_TFLAG_OUT_DEVICE;
131 args.data_phase = TASKFILE_IN;
Bartlomiej Zolnierkiewiczac026ff2008-01-25 22:17:14 +0100132 return ide_raw_taskfile(drive, &args, buf, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133}
134
Bartlomiej Zolnierkiewicz74095a92008-01-25 22:17:07 +0100135static int inline task_dma_ok(ide_task_t *task)
136{
Bartlomiej Zolnierkiewiczf6e29e32008-01-25 22:17:16 +0100137 if (blk_fs_request(task->rq) || (task->tf_flags & IDE_TFLAG_FLAGGED))
Bartlomiej Zolnierkiewicz74095a92008-01-25 22:17:07 +0100138 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
Bartlomiej Zolnierkiewicz1192e522008-01-25 22:17:16 +0100154static ide_startstop_t task_no_data_intr(ide_drive_t *);
Bartlomiej Zolnierkiewicz57d73662008-01-25 22:17:16 +0100155static ide_startstop_t set_geometry_intr(ide_drive_t *);
156static ide_startstop_t recal_intr(ide_drive_t *);
157static ide_startstop_t set_multmode_intr(ide_drive_t *);
Bartlomiej Zolnierkiewiczf6e29e32008-01-25 22:17:16 +0100158static ide_startstop_t pre_task_out_intr(ide_drive_t *, struct request *);
159static ide_startstop_t task_in_intr(ide_drive_t *);
Bartlomiej Zolnierkiewicz1192e522008-01-25 22:17:16 +0100160
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161ide_startstop_t do_rw_taskfile (ide_drive_t *drive, ide_task_t *task)
162{
163 ide_hwif_t *hwif = HWIF(drive);
Bartlomiej Zolnierkiewicz650d8412008-01-25 22:17:06 +0100164 struct ide_taskfile *tf = &task->tf;
Bartlomiej Zolnierkiewicz57d73662008-01-25 22:17:16 +0100165 ide_handler_t *handler = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
Bartlomiej Zolnierkiewicz1edee602008-01-25 22:17:15 +0100167 if (task->data_phase == TASKFILE_MULTI_IN ||
168 task->data_phase == TASKFILE_MULTI_OUT) {
169 if (!drive->mult_count) {
170 printk(KERN_ERR "%s: multimode not set!\n",
171 drive->name);
172 return ide_stopped;
173 }
174 }
175
176 if (task->tf_flags & IDE_TFLAG_FLAGGED)
177 task->tf_flags |= IDE_TFLAG_FLAGGED_SET_IN_FLAGS;
178
Bartlomiej Zolnierkiewiczf6e29e32008-01-25 22:17:16 +0100179 if ((task->tf_flags & IDE_TFLAG_DMA_PIO_FALLBACK) == 0)
180 ide_tf_load(drive, task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
Bartlomiej Zolnierkiewicz10d90152008-01-25 22:17:16 +0100182 switch (task->data_phase) {
183 case TASKFILE_MULTI_OUT:
184 case TASKFILE_OUT:
185 hwif->OUTBSYNC(drive, tf->command, IDE_COMMAND_REG);
186 ndelay(400); /* FIXME */
187 return pre_task_out_intr(drive, task->rq);
188 case TASKFILE_MULTI_IN:
189 case TASKFILE_IN:
Bartlomiej Zolnierkiewicz57d73662008-01-25 22:17:16 +0100190 handler = task_in_intr;
Bartlomiej Zolnierkiewicz1192e522008-01-25 22:17:16 +0100191 /* fall-through */
Bartlomiej Zolnierkiewicz10d90152008-01-25 22:17:16 +0100192 case TASKFILE_NO_DATA:
Bartlomiej Zolnierkiewicz57d73662008-01-25 22:17:16 +0100193 if (handler == NULL)
194 handler = task_no_data_intr;
Bartlomiej Zolnierkiewicz1192e522008-01-25 22:17:16 +0100195 /* WIN_{SPECIFY,RESTORE,SETMULT} use custom handlers */
Bartlomiej Zolnierkiewicz57d73662008-01-25 22:17:16 +0100196 if (task->tf_flags & IDE_TFLAG_CUSTOM_HANDLER) {
197 switch (tf->command) {
198 case WIN_SPECIFY: handler = set_geometry_intr; break;
199 case WIN_RESTORE: handler = recal_intr; break;
200 case WIN_SETMULT: handler = set_multmode_intr; break;
201 }
202 }
203 ide_execute_command(drive, tf->command, handler,
204 WAIT_WORSTCASE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 return ide_started;
Bartlomiej Zolnierkiewicz10d90152008-01-25 22:17:16 +0100206 default:
207 if (task_dma_ok(task) == 0 || drive->using_dma == 0 ||
208 hwif->dma_setup(drive))
209 return ide_stopped;
Bartlomiej Zolnierkiewicz74095a92008-01-25 22:17:07 +0100210 hwif->dma_exec_cmd(drive, tf->command);
211 hwif->dma_start(drive);
212 return ide_started;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214}
Bartlomiej Zolnierkiewiczf6e29e32008-01-25 22:17:16 +0100215EXPORT_SYMBOL_GPL(do_rw_taskfile);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217/*
218 * set_multmode_intr() is invoked on completion of a WIN_SETMULT cmd.
219 */
Bartlomiej Zolnierkiewicz57d73662008-01-25 22:17:16 +0100220static ide_startstop_t set_multmode_intr(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221{
222 ide_hwif_t *hwif = HWIF(drive);
223 u8 stat;
224
225 if (OK_STAT(stat = hwif->INB(IDE_STATUS_REG),READY_STAT,BAD_STAT)) {
226 drive->mult_count = drive->mult_req;
227 } else {
228 drive->mult_req = drive->mult_count = 0;
229 drive->special.b.recalibrate = 1;
230 (void) ide_dump_status(drive, "set_multmode", stat);
231 }
232 return ide_stopped;
233}
234
235/*
236 * set_geometry_intr() is invoked on completion of a WIN_SPECIFY cmd.
237 */
Bartlomiej Zolnierkiewicz57d73662008-01-25 22:17:16 +0100238static ide_startstop_t set_geometry_intr(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239{
240 ide_hwif_t *hwif = HWIF(drive);
241 int retries = 5;
242 u8 stat;
243
244 while (((stat = hwif->INB(IDE_STATUS_REG)) & BUSY_STAT) && retries--)
245 udelay(10);
246
247 if (OK_STAT(stat, READY_STAT, BAD_STAT))
248 return ide_stopped;
249
250 if (stat & (ERR_STAT|DRQ_STAT))
251 return ide_error(drive, "set_geometry_intr", stat);
252
Eric Sesterhenn125e1872006-06-23 02:06:06 -0700253 BUG_ON(HWGROUP(drive)->handler != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 ide_set_handler(drive, &set_geometry_intr, WAIT_WORSTCASE, NULL);
255 return ide_started;
256}
257
258/*
259 * recal_intr() is invoked on completion of a WIN_RESTORE (recalibrate) cmd.
260 */
Bartlomiej Zolnierkiewicz57d73662008-01-25 22:17:16 +0100261static ide_startstop_t recal_intr(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262{
263 ide_hwif_t *hwif = HWIF(drive);
264 u8 stat;
265
266 if (!OK_STAT(stat = hwif->INB(IDE_STATUS_REG), READY_STAT, BAD_STAT))
267 return ide_error(drive, "recal_intr", stat);
268 return ide_stopped;
269}
270
271/*
272 * Handler for commands without a data phase
273 */
Bartlomiej Zolnierkiewicz1192e522008-01-25 22:17:16 +0100274static ide_startstop_t task_no_data_intr(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275{
276 ide_task_t *args = HWGROUP(drive)->rq->special;
277 ide_hwif_t *hwif = HWIF(drive);
278 u8 stat;
279
Ingo Molnar366c7f52006-07-03 00:25:25 -0700280 local_irq_enable_in_hardirq();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 if (!OK_STAT(stat = hwif->INB(IDE_STATUS_REG),READY_STAT,BAD_STAT)) {
282 return ide_error(drive, "task_no_data_intr", stat);
283 /* calls ide_end_drive_cmd */
284 }
285 if (args)
286 ide_end_drive_cmd(drive, stat, hwif->INB(IDE_ERROR_REG));
287
288 return ide_stopped;
289}
290
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291static u8 wait_drive_not_busy(ide_drive_t *drive)
292{
293 ide_hwif_t *hwif = HWIF(drive);
Masatake YAMATOb42fa132007-07-03 22:28:34 +0200294 int retries;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 u8 stat;
296
297 /*
298 * Last sector was transfered, wait until drive is ready.
299 * This can take up to 10 usec, but we will wait max 1 ms
300 * (drive_cmd_intr() waits that long).
301 */
Masatake YAMATOb42fa132007-07-03 22:28:34 +0200302 for (retries = 0; retries < 100; retries++) {
303 if ((stat = hwif->INB(IDE_STATUS_REG)) & BUSY_STAT)
304 udelay(10);
305 else
306 break;
307 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
Masatake YAMATOb42fa132007-07-03 22:28:34 +0200309 if (stat & BUSY_STAT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 printk(KERN_ERR "%s: drive still BUSY!\n", drive->name);
311
312 return stat;
313}
314
315static void ide_pio_sector(ide_drive_t *drive, unsigned int write)
316{
317 ide_hwif_t *hwif = drive->hwif;
318 struct scatterlist *sg = hwif->sg_table;
Jens Axboe55c16a72007-07-25 08:13:56 +0200319 struct scatterlist *cursg = hwif->cursg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 struct page *page;
321#ifdef CONFIG_HIGHMEM
322 unsigned long flags;
323#endif
324 unsigned int offset;
325 u8 *buf;
326
Jens Axboe55c16a72007-07-25 08:13:56 +0200327 cursg = hwif->cursg;
328 if (!cursg) {
329 cursg = sg;
330 hwif->cursg = sg;
331 }
332
Jens Axboe45711f12007-10-22 21:19:53 +0200333 page = sg_page(cursg);
Jens Axboe55c16a72007-07-25 08:13:56 +0200334 offset = cursg->offset + hwif->cursg_ofs * SECTOR_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335
336 /* get the current page and offset */
337 page = nth_page(page, (offset >> PAGE_SHIFT));
338 offset %= PAGE_SIZE;
339
340#ifdef CONFIG_HIGHMEM
341 local_irq_save(flags);
342#endif
343 buf = kmap_atomic(page, KM_BIO_SRC_IRQ) + offset;
344
345 hwif->nleft--;
346 hwif->cursg_ofs++;
347
Jens Axboe55c16a72007-07-25 08:13:56 +0200348 if ((hwif->cursg_ofs * SECTOR_SIZE) == cursg->length) {
349 hwif->cursg = sg_next(hwif->cursg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 hwif->cursg_ofs = 0;
351 }
352
353 /* do the actual data transfer */
354 if (write)
355 taskfile_output_data(drive, buf, SECTOR_WORDS);
356 else
357 taskfile_input_data(drive, buf, SECTOR_WORDS);
358
359 kunmap_atomic(buf, KM_BIO_SRC_IRQ);
360#ifdef CONFIG_HIGHMEM
361 local_irq_restore(flags);
362#endif
363}
364
365static void ide_pio_multi(ide_drive_t *drive, unsigned int write)
366{
367 unsigned int nsect;
368
369 nsect = min_t(unsigned int, drive->hwif->nleft, drive->mult_count);
370 while (nsect--)
371 ide_pio_sector(drive, write);
372}
373
Arjan van de Ven858119e2006-01-14 13:20:43 -0800374static void ide_pio_datablock(ide_drive_t *drive, struct request *rq,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 unsigned int write)
376{
377 if (rq->bio) /* fs request */
378 rq->errors = 0;
379
Andrew Morton651c29a2006-02-15 15:17:37 -0800380 touch_softlockup_watchdog();
381
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 switch (drive->hwif->data_phase) {
383 case TASKFILE_MULTI_IN:
384 case TASKFILE_MULTI_OUT:
385 ide_pio_multi(drive, write);
386 break;
387 default:
388 ide_pio_sector(drive, write);
389 break;
390 }
391}
392
393static ide_startstop_t task_error(ide_drive_t *drive, struct request *rq,
394 const char *s, u8 stat)
395{
396 if (rq->bio) {
397 ide_hwif_t *hwif = drive->hwif;
398 int sectors = hwif->nsect - hwif->nleft;
399
400 switch (hwif->data_phase) {
401 case TASKFILE_IN:
402 if (hwif->nleft)
403 break;
404 /* fall through */
405 case TASKFILE_OUT:
406 sectors--;
407 break;
408 case TASKFILE_MULTI_IN:
409 if (hwif->nleft)
410 break;
411 /* fall through */
412 case TASKFILE_MULTI_OUT:
413 sectors -= drive->mult_count;
414 default:
415 break;
416 }
417
418 if (sectors > 0) {
419 ide_driver_t *drv;
420
421 drv = *(ide_driver_t **)rq->rq_disk->private_data;
422 drv->end_request(drive, 1, sectors);
423 }
424 }
425 return ide_error(drive, s, stat);
426}
427
428static void task_end_request(ide_drive_t *drive, struct request *rq, u8 stat)
429{
Jens Axboe55c16a72007-07-25 08:13:56 +0200430 HWIF(drive)->cursg = NULL;
431
Jens Axboe4aff5e22006-08-10 08:44:47 +0200432 if (rq->cmd_type == REQ_TYPE_ATA_TASKFILE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 ide_task_t *task = rq->special;
434
Bartlomiej Zolnierkiewicz74095a92008-01-25 22:17:07 +0100435 if (task->tf_flags & IDE_TFLAG_FLAGGED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 u8 err = drive->hwif->INB(IDE_ERROR_REG);
437 ide_end_drive_cmd(drive, stat, err);
438 return;
439 }
440 }
441
Richard Purdie03731fb2006-03-31 02:31:15 -0800442 if (rq->rq_disk) {
443 ide_driver_t *drv;
444
445 drv = *(ide_driver_t **)rq->rq_disk->private_data;;
446 drv->end_request(drive, 1, rq->hard_nr_sectors);
447 } else
448 ide_end_request(drive, 1, rq->hard_nr_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449}
450
451/*
452 * Handler for command with PIO data-in phase (Read/Read Multiple).
453 */
Bartlomiej Zolnierkiewiczf6e29e32008-01-25 22:17:16 +0100454static ide_startstop_t task_in_intr(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455{
456 ide_hwif_t *hwif = drive->hwif;
457 struct request *rq = HWGROUP(drive)->rq;
458 u8 stat = hwif->INB(IDE_STATUS_REG);
459
460 /* new way for dealing with premature shared PCI interrupts */
461 if (!OK_STAT(stat, DATA_READY, BAD_R_STAT)) {
462 if (stat & (ERR_STAT | DRQ_STAT))
463 return task_error(drive, rq, __FUNCTION__, stat);
464 /* No data yet, so wait for another IRQ. */
465 ide_set_handler(drive, &task_in_intr, WAIT_WORSTCASE, NULL);
466 return ide_started;
467 }
468
469 ide_pio_datablock(drive, rq, 0);
470
471 /* If it was the last datablock check status and finish transfer. */
472 if (!hwif->nleft) {
473 stat = wait_drive_not_busy(drive);
474 if (!OK_STAT(stat, 0, BAD_R_STAT))
475 return task_error(drive, rq, __FUNCTION__, stat);
476 task_end_request(drive, rq, stat);
477 return ide_stopped;
478 }
479
480 /* Still data left to transfer. */
481 ide_set_handler(drive, &task_in_intr, WAIT_WORSTCASE, NULL);
482
483 return ide_started;
484}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485
486/*
487 * Handler for command with PIO data-out phase (Write/Write Multiple).
488 */
489static ide_startstop_t task_out_intr (ide_drive_t *drive)
490{
491 ide_hwif_t *hwif = drive->hwif;
492 struct request *rq = HWGROUP(drive)->rq;
493 u8 stat = hwif->INB(IDE_STATUS_REG);
494
495 if (!OK_STAT(stat, DRIVE_READY, drive->bad_wstat))
496 return task_error(drive, rq, __FUNCTION__, stat);
497
498 /* Deal with unexpected ATA data phase. */
499 if (((stat & DRQ_STAT) == 0) ^ !hwif->nleft)
500 return task_error(drive, rq, __FUNCTION__, stat);
501
502 if (!hwif->nleft) {
503 task_end_request(drive, rq, stat);
504 return ide_stopped;
505 }
506
507 /* Still data left to transfer. */
508 ide_pio_datablock(drive, rq, 1);
509 ide_set_handler(drive, &task_out_intr, WAIT_WORSTCASE, NULL);
510
511 return ide_started;
512}
513
Bartlomiej Zolnierkiewiczf6e29e32008-01-25 22:17:16 +0100514static ide_startstop_t pre_task_out_intr(ide_drive_t *drive, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515{
516 ide_startstop_t startstop;
517
518 if (ide_wait_stat(&startstop, drive, DATA_READY,
519 drive->bad_wstat, WAIT_DRQ)) {
520 printk(KERN_ERR "%s: no DRQ after issuing %sWRITE%s\n",
521 drive->name,
522 drive->hwif->data_phase ? "MULT" : "",
523 drive->addressing ? "_EXT" : "");
524 return startstop;
525 }
526
527 if (!drive->unmask)
528 local_irq_disable();
529
530 ide_set_handler(drive, &task_out_intr, WAIT_WORSTCASE, NULL);
531 ide_pio_datablock(drive, rq, 1);
532
533 return ide_started;
534}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535
Bartlomiej Zolnierkiewiczac026ff2008-01-25 22:17:14 +0100536int ide_raw_taskfile(ide_drive_t *drive, ide_task_t *task, u8 *buf, u16 nsect)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537{
538 struct request rq;
539
540 memset(&rq, 0, sizeof(rq));
Bartlomiej Zolnierkiewicz02ac2462007-11-05 21:42:27 +0100541 rq.ref_count = 1;
Jens Axboe4aff5e22006-08-10 08:44:47 +0200542 rq.cmd_type = REQ_TYPE_ATA_TASKFILE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 rq.buffer = buf;
544
545 /*
546 * (ks) We transfer currently only whole sectors.
547 * This is suffient for now. But, it would be great,
548 * if we would find a solution to transfer any size.
549 * To support special commands like READ LONG.
550 */
Bartlomiej Zolnierkiewiczac026ff2008-01-25 22:17:14 +0100551 rq.hard_nr_sectors = rq.nr_sectors = nsect;
552 rq.hard_cur_sectors = rq.current_nr_sectors = nsect;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553
Bartlomiej Zolnierkiewiczac026ff2008-01-25 22:17:14 +0100554 if (task->tf_flags & IDE_TFLAG_WRITE)
555 rq.cmd_flags |= REQ_RW;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556
Bartlomiej Zolnierkiewiczac026ff2008-01-25 22:17:14 +0100557 rq.special = task;
558 task->rq = &rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 return ide_do_drive_cmd(drive, &rq, ide_wait);
561}
562
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563EXPORT_SYMBOL(ide_raw_taskfile);
564
Bartlomiej Zolnierkiewicz9a3c49b2008-01-25 22:17:07 +0100565int ide_no_data_taskfile(ide_drive_t *drive, ide_task_t *task)
566{
Bartlomiej Zolnierkiewiczac026ff2008-01-25 22:17:14 +0100567 task->data_phase = TASKFILE_NO_DATA;
Bartlomiej Zolnierkiewicz9a3c49b2008-01-25 22:17:07 +0100568
Bartlomiej Zolnierkiewiczac026ff2008-01-25 22:17:14 +0100569 return ide_raw_taskfile(drive, task, NULL, 0);
Bartlomiej Zolnierkiewicz9a3c49b2008-01-25 22:17:07 +0100570}
571EXPORT_SYMBOL_GPL(ide_no_data_taskfile);
572
Bartlomiej Zolnierkiewicz26a5b042007-11-05 21:42:27 +0100573#ifdef CONFIG_IDE_TASK_IOCTL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574int ide_taskfile_ioctl (ide_drive_t *drive, unsigned int cmd, unsigned long arg)
575{
576 ide_task_request_t *req_task;
577 ide_task_t args;
578 u8 *outbuf = NULL;
579 u8 *inbuf = NULL;
Bartlomiej Zolnierkiewiczac026ff2008-01-25 22:17:14 +0100580 u8 *data_buf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 int err = 0;
582 int tasksize = sizeof(struct ide_task_request_s);
Alan Cox3a42bb22006-10-16 16:31:02 +0100583 unsigned int taskin = 0;
584 unsigned int taskout = 0;
Bartlomiej Zolnierkiewiczac026ff2008-01-25 22:17:14 +0100585 u16 nsect = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 u8 io_32bit = drive->io_32bit;
587 char __user *buf = (char __user *)arg;
588
589// printk("IDE Taskfile ...\n");
590
Deepak Saxenaf5e3c2f2005-11-07 01:01:25 -0800591 req_task = kzalloc(tasksize, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 if (req_task == NULL) return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 if (copy_from_user(req_task, buf, tasksize)) {
594 kfree(req_task);
595 return -EFAULT;
596 }
597
Alan Cox3a42bb22006-10-16 16:31:02 +0100598 taskout = req_task->out_size;
599 taskin = req_task->in_size;
600
601 if (taskin > 65536 || taskout > 65536) {
602 err = -EINVAL;
603 goto abort;
604 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605
606 if (taskout) {
607 int outtotal = tasksize;
Deepak Saxenaf5e3c2f2005-11-07 01:01:25 -0800608 outbuf = kzalloc(taskout, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 if (outbuf == NULL) {
610 err = -ENOMEM;
611 goto abort;
612 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 if (copy_from_user(outbuf, buf + outtotal, taskout)) {
614 err = -EFAULT;
615 goto abort;
616 }
617 }
618
619 if (taskin) {
620 int intotal = tasksize + taskout;
Deepak Saxenaf5e3c2f2005-11-07 01:01:25 -0800621 inbuf = kzalloc(taskin, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 if (inbuf == NULL) {
623 err = -ENOMEM;
624 goto abort;
625 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 if (copy_from_user(inbuf, buf + intotal, taskin)) {
627 err = -EFAULT;
628 goto abort;
629 }
630 }
631
632 memset(&args, 0, sizeof(ide_task_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633
Bartlomiej Zolnierkiewicz650d8412008-01-25 22:17:06 +0100634 memcpy(&args.tf_array[0], req_task->hob_ports, HDIO_DRIVE_HOB_HDR_SIZE - 2);
635 memcpy(&args.tf_array[6], req_task->io_ports, HDIO_DRIVE_TASK_HDR_SIZE);
Bartlomiej Zolnierkiewicz866e2ec2008-01-25 22:17:14 +0100636
637 args.data_phase = req_task->data_phase;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638
Bartlomiej Zolnierkiewicza3bbb9d2008-01-25 22:17:10 +0100639 args.tf_flags = IDE_TFLAG_OUT_DEVICE;
640 if (drive->addressing == 1)
641 args.tf_flags |= IDE_TFLAG_LBA48;
642
Bartlomiej Zolnierkiewicz74095a92008-01-25 22:17:07 +0100643 if (req_task->out_flags.all) {
644 args.tf_flags |= IDE_TFLAG_FLAGGED;
645
646 if (req_task->out_flags.b.data)
647 args.tf_flags |= IDE_TFLAG_OUT_DATA;
648
649 if (req_task->out_flags.b.nsector_hob)
650 args.tf_flags |= IDE_TFLAG_OUT_HOB_NSECT;
651 if (req_task->out_flags.b.sector_hob)
652 args.tf_flags |= IDE_TFLAG_OUT_HOB_LBAL;
653 if (req_task->out_flags.b.lcyl_hob)
654 args.tf_flags |= IDE_TFLAG_OUT_HOB_LBAM;
655 if (req_task->out_flags.b.hcyl_hob)
656 args.tf_flags |= IDE_TFLAG_OUT_HOB_LBAH;
657
658 if (req_task->out_flags.b.error_feature)
659 args.tf_flags |= IDE_TFLAG_OUT_FEATURE;
660 if (req_task->out_flags.b.nsector)
661 args.tf_flags |= IDE_TFLAG_OUT_NSECT;
662 if (req_task->out_flags.b.sector)
663 args.tf_flags |= IDE_TFLAG_OUT_LBAL;
664 if (req_task->out_flags.b.lcyl)
665 args.tf_flags |= IDE_TFLAG_OUT_LBAM;
666 if (req_task->out_flags.b.hcyl)
667 args.tf_flags |= IDE_TFLAG_OUT_LBAH;
Bartlomiej Zolnierkiewicza3bbb9d2008-01-25 22:17:10 +0100668 } else {
669 args.tf_flags |= IDE_TFLAG_OUT_TF;
670 if (args.tf_flags & IDE_TFLAG_LBA48)
671 args.tf_flags |= IDE_TFLAG_OUT_HOB;
Bartlomiej Zolnierkiewicz74095a92008-01-25 22:17:07 +0100672 }
673
Bartlomiej Zolnierkiewicz866e2ec2008-01-25 22:17:14 +0100674 if (req_task->in_flags.b.data)
675 args.tf_flags |= IDE_TFLAG_IN_DATA;
676
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 drive->io_32bit = 0;
678 switch(req_task->data_phase) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 case TASKFILE_MULTI_OUT:
680 if (!drive->mult_count) {
681 /* (hs): give up if multcount is not set */
682 printk(KERN_ERR "%s: %s Multimode Write " \
683 "multcount is not set\n",
684 drive->name, __FUNCTION__);
685 err = -EPERM;
686 goto abort;
687 }
688 /* fall through */
689 case TASKFILE_OUT:
Bartlomiej Zolnierkiewiczac026ff2008-01-25 22:17:14 +0100690 /* fall through */
691 case TASKFILE_OUT_DMAQ:
692 case TASKFILE_OUT_DMA:
693 nsect = taskout / SECTOR_SIZE;
694 data_buf = outbuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 break;
696 case TASKFILE_MULTI_IN:
697 if (!drive->mult_count) {
698 /* (hs): give up if multcount is not set */
699 printk(KERN_ERR "%s: %s Multimode Read failure " \
700 "multcount is not set\n",
701 drive->name, __FUNCTION__);
702 err = -EPERM;
703 goto abort;
704 }
705 /* fall through */
706 case TASKFILE_IN:
Bartlomiej Zolnierkiewiczac026ff2008-01-25 22:17:14 +0100707 /* fall through */
708 case TASKFILE_IN_DMAQ:
709 case TASKFILE_IN_DMA:
710 nsect = taskin / SECTOR_SIZE;
711 data_buf = inbuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 break;
713 case TASKFILE_NO_DATA:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 break;
715 default:
716 err = -EFAULT;
717 goto abort;
718 }
719
Bartlomiej Zolnierkiewiczac026ff2008-01-25 22:17:14 +0100720 if (req_task->req_cmd == IDE_DRIVE_TASK_NO_DATA)
721 nsect = 0;
722 else if (!nsect) {
723 nsect = (args.tf.hob_nsect << 8) | args.tf.nsect;
724
725 if (!nsect) {
726 printk(KERN_ERR "%s: in/out command without data\n",
727 drive->name);
728 err = -EFAULT;
729 goto abort;
730 }
731 }
732
733 if (req_task->req_cmd == IDE_DRIVE_TASK_RAW_WRITE)
734 args.tf_flags |= IDE_TFLAG_WRITE;
735
736 err = ide_raw_taskfile(drive, &args, data_buf, nsect);
737
Bartlomiej Zolnierkiewicz650d8412008-01-25 22:17:06 +0100738 memcpy(req_task->hob_ports, &args.tf_array[0], HDIO_DRIVE_HOB_HDR_SIZE - 2);
739 memcpy(req_task->io_ports, &args.tf_array[6], HDIO_DRIVE_TASK_HDR_SIZE);
Bartlomiej Zolnierkiewicz866e2ec2008-01-25 22:17:14 +0100740
741 if ((args.tf_flags & IDE_TFLAG_FLAGGED_SET_IN_FLAGS) &&
742 req_task->in_flags.all == 0) {
743 req_task->in_flags.all = IDE_TASKFILE_STD_IN_FLAGS;
744 if (drive->addressing == 1)
745 req_task->in_flags.all |= (IDE_HOB_STD_IN_FLAGS << 8);
746 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747
748 if (copy_to_user(buf, req_task, tasksize)) {
749 err = -EFAULT;
750 goto abort;
751 }
752 if (taskout) {
753 int outtotal = tasksize;
754 if (copy_to_user(buf + outtotal, outbuf, taskout)) {
755 err = -EFAULT;
756 goto abort;
757 }
758 }
759 if (taskin) {
760 int intotal = tasksize + taskout;
761 if (copy_to_user(buf + intotal, inbuf, taskin)) {
762 err = -EFAULT;
763 goto abort;
764 }
765 }
766abort:
767 kfree(req_task);
Jesper Juhl6044ec82005-11-07 01:01:32 -0800768 kfree(outbuf);
769 kfree(inbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770
771// printk("IDE Taskfile ioctl ended. rc = %i\n", err);
772
773 drive->io_32bit = io_32bit;
774
775 return err;
776}
Bartlomiej Zolnierkiewicz26a5b042007-11-05 21:42:27 +0100777#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778
779int ide_wait_cmd (ide_drive_t *drive, u8 cmd, u8 nsect, u8 feature, u8 sectors, u8 *buf)
780{
781 struct request rq;
782 u8 buffer[4];
783
784 if (!buf)
785 buf = buffer;
786 memset(buf, 0, 4 + SECTOR_WORDS * 4 * sectors);
787 ide_init_drive_cmd(&rq);
788 rq.buffer = buf;
789 *buf++ = cmd;
790 *buf++ = nsect;
791 *buf++ = feature;
792 *buf++ = sectors;
793 return ide_do_drive_cmd(drive, &rq, ide_wait);
794}
795
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796int ide_cmd_ioctl (ide_drive_t *drive, unsigned int cmd, unsigned long arg)
797{
798 int err = 0;
799 u8 args[4], *argbuf = args;
800 u8 xfer_rate = 0;
801 int argsize = 4;
802 ide_task_t tfargs;
Bartlomiej Zolnierkiewicz650d8412008-01-25 22:17:06 +0100803 struct ide_taskfile *tf = &tfargs.tf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804
805 if (NULL == (void *) arg) {
806 struct request rq;
807 ide_init_drive_cmd(&rq);
808 return ide_do_drive_cmd(drive, &rq, ide_wait);
809 }
810
811 if (copy_from_user(args, (void __user *)arg, 4))
812 return -EFAULT;
813
814 memset(&tfargs, 0, sizeof(ide_task_t));
Bartlomiej Zolnierkiewicz650d8412008-01-25 22:17:06 +0100815 tf->feature = args[2];
816 tf->nsect = args[3];
817 tf->lbal = args[1];
818 tf->command = args[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819
820 if (args[3]) {
821 argsize = 4 + (SECTOR_WORDS * 4 * args[3]);
Deepak Saxenaf5e3c2f2005-11-07 01:01:25 -0800822 argbuf = kzalloc(argsize, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 if (argbuf == NULL)
824 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 }
826 if (set_transfer(drive, &tfargs)) {
827 xfer_rate = args[1];
828 if (ide_ata66_check(drive, &tfargs))
829 goto abort;
830 }
831
832 err = ide_wait_cmd(drive, args[0], args[1], args[2], args[3], argbuf);
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 }
839abort:
840 if (copy_to_user((void __user *)arg, argbuf, argsize))
841 err = -EFAULT;
842 if (argsize > 4)
843 kfree(argbuf);
844 return err;
845}
846
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847int ide_task_ioctl (ide_drive_t *drive, unsigned int cmd, unsigned long arg)
848{
849 void __user *p = (void __user *)arg;
850 int err = 0;
Bartlomiej Zolnierkiewicz14b89ef2008-01-25 22:17:11 +0100851 u8 args[7];
852 ide_task_t task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853
854 if (copy_from_user(args, p, 7))
855 return -EFAULT;
Bartlomiej Zolnierkiewicz14b89ef2008-01-25 22:17:11 +0100856
857 memset(&task, 0, sizeof(task));
858 memcpy(&task.tf_array[7], &args[1], 6);
859 task.tf.command = args[0];
860 task.tf_flags = IDE_TFLAG_OUT_TF | IDE_TFLAG_OUT_DEVICE;
861
862 err = ide_no_data_taskfile(drive, &task);
863
864 args[0] = task.tf.command;
865 memcpy(&args[1], &task.tf_array[7], 6);
866
867 if (copy_to_user(p, args, 7))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 err = -EFAULT;
Bartlomiej Zolnierkiewicz14b89ef2008-01-25 22:17:11 +0100869
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 return err;
871}