blob: 063e0eb6c9e41ba3c130cf3d9af3e3ccdac092ee [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 Zolnierkiewicz9e422372008-01-25 22:17:07 +010086 if (IDE_CONTROL_REG)
87 hwif->OUTB(drive->ctl, IDE_CONTROL_REG); /* clear nIEN */
88
89 if ((task->tf_flags & IDE_TFLAG_NO_SELECT_MASK) == 0)
90 SELECT_MASK(drive, 0);
91
Bartlomiej Zolnierkiewicz74095a92008-01-25 22:17:07 +010092 if (task->tf_flags & IDE_TFLAG_OUT_DATA)
93 hwif->OUTW((tf->hob_data << 8) | tf->data, IDE_DATA_REG);
Bartlomiej Zolnierkiewicz9e422372008-01-25 22:17:07 +010094
Bartlomiej Zolnierkiewicz74095a92008-01-25 22:17:07 +010095 if (task->tf_flags & IDE_TFLAG_OUT_HOB_FEATURE)
96 hwif->OUTB(tf->hob_feature, IDE_FEATURE_REG);
97 if (task->tf_flags & IDE_TFLAG_OUT_HOB_NSECT)
98 hwif->OUTB(tf->hob_nsect, IDE_NSECTOR_REG);
99 if (task->tf_flags & IDE_TFLAG_OUT_HOB_LBAL)
100 hwif->OUTB(tf->hob_lbal, IDE_SECTOR_REG);
101 if (task->tf_flags & IDE_TFLAG_OUT_HOB_LBAM)
102 hwif->OUTB(tf->hob_lbam, IDE_LCYL_REG);
103 if (task->tf_flags & IDE_TFLAG_OUT_HOB_LBAH)
104 hwif->OUTB(tf->hob_lbah, IDE_HCYL_REG);
105
106 if (task->tf_flags & IDE_TFLAG_OUT_FEATURE)
107 hwif->OUTB(tf->feature, IDE_FEATURE_REG);
108 if (task->tf_flags & IDE_TFLAG_OUT_NSECT)
109 hwif->OUTB(tf->nsect, IDE_NSECTOR_REG);
110 if (task->tf_flags & IDE_TFLAG_OUT_LBAL)
111 hwif->OUTB(tf->lbal, IDE_SECTOR_REG);
112 if (task->tf_flags & IDE_TFLAG_OUT_LBAM)
113 hwif->OUTB(tf->lbam, IDE_LCYL_REG);
114 if (task->tf_flags & IDE_TFLAG_OUT_LBAH)
115 hwif->OUTB(tf->lbah, IDE_HCYL_REG);
Bartlomiej Zolnierkiewicz9e422372008-01-25 22:17:07 +0100116
Bartlomiej Zolnierkiewicz807e35d2008-01-25 22:17:10 +0100117 if (task->tf_flags & IDE_TFLAG_OUT_DEVICE)
118 hwif->OUTB((tf->device & HIHI) | drive->select.all, IDE_SELECT_REG);
Bartlomiej Zolnierkiewicz9e422372008-01-25 22:17:07 +0100119}
120
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121int taskfile_lib_get_identify (ide_drive_t *drive, u8 *buf)
122{
123 ide_task_t args;
Bartlomiej Zolnierkiewicz650d8412008-01-25 22:17:06 +0100124
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 memset(&args, 0, sizeof(ide_task_t));
Bartlomiej Zolnierkiewicz650d8412008-01-25 22:17:06 +0100126 args.tf.nsect = 0x01;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 if (drive->media == ide_disk)
Bartlomiej Zolnierkiewicz650d8412008-01-25 22:17:06 +0100128 args.tf.command = WIN_IDENTIFY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 else
Bartlomiej Zolnierkiewicz650d8412008-01-25 22:17:06 +0100130 args.tf.command = WIN_PIDENTIFY;
Bartlomiej Zolnierkiewiczac026ff2008-01-25 22:17:14 +0100131 args.tf_flags = IDE_TFLAG_OUT_TF | IDE_TFLAG_OUT_DEVICE;
132 args.data_phase = TASKFILE_IN;
Bartlomiej Zolnierkiewiczac026ff2008-01-25 22:17:14 +0100133 return ide_raw_taskfile(drive, &args, buf, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134}
135
Bartlomiej Zolnierkiewicz74095a92008-01-25 22:17:07 +0100136static int inline task_dma_ok(ide_task_t *task)
137{
Bartlomiej Zolnierkiewiczf6e29e32008-01-25 22:17:16 +0100138 if (blk_fs_request(task->rq) || (task->tf_flags & IDE_TFLAG_FLAGGED))
Bartlomiej Zolnierkiewicz74095a92008-01-25 22:17:07 +0100139 return 1;
140
141 switch (task->tf.command) {
142 case WIN_WRITEDMA_ONCE:
143 case WIN_WRITEDMA:
144 case WIN_WRITEDMA_EXT:
145 case WIN_READDMA_ONCE:
146 case WIN_READDMA:
147 case WIN_READDMA_EXT:
148 case WIN_IDENTIFY_DMA:
149 return 1;
150 }
151
152 return 0;
153}
154
Bartlomiej Zolnierkiewicz1192e522008-01-25 22:17:16 +0100155static ide_startstop_t task_no_data_intr(ide_drive_t *);
Bartlomiej Zolnierkiewicz57d73662008-01-25 22:17:16 +0100156static ide_startstop_t set_geometry_intr(ide_drive_t *);
157static ide_startstop_t recal_intr(ide_drive_t *);
158static ide_startstop_t set_multmode_intr(ide_drive_t *);
Bartlomiej Zolnierkiewiczf6e29e32008-01-25 22:17:16 +0100159static ide_startstop_t pre_task_out_intr(ide_drive_t *, struct request *);
160static ide_startstop_t task_in_intr(ide_drive_t *);
Bartlomiej Zolnierkiewicz1192e522008-01-25 22:17:16 +0100161
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162ide_startstop_t do_rw_taskfile (ide_drive_t *drive, ide_task_t *task)
163{
164 ide_hwif_t *hwif = HWIF(drive);
Bartlomiej Zolnierkiewicz650d8412008-01-25 22:17:06 +0100165 struct ide_taskfile *tf = &task->tf;
Bartlomiej Zolnierkiewicz57d73662008-01-25 22:17:16 +0100166 ide_handler_t *handler = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
Bartlomiej Zolnierkiewicz1edee602008-01-25 22:17:15 +0100168 if (task->data_phase == TASKFILE_MULTI_IN ||
169 task->data_phase == TASKFILE_MULTI_OUT) {
170 if (!drive->mult_count) {
171 printk(KERN_ERR "%s: multimode not set!\n",
172 drive->name);
173 return ide_stopped;
174 }
175 }
176
177 if (task->tf_flags & IDE_TFLAG_FLAGGED)
178 task->tf_flags |= IDE_TFLAG_FLAGGED_SET_IN_FLAGS;
179
Bartlomiej Zolnierkiewiczf6e29e32008-01-25 22:17:16 +0100180 if ((task->tf_flags & IDE_TFLAG_DMA_PIO_FALLBACK) == 0)
181 ide_tf_load(drive, task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182
Bartlomiej Zolnierkiewicz10d90152008-01-25 22:17:16 +0100183 switch (task->data_phase) {
184 case TASKFILE_MULTI_OUT:
185 case TASKFILE_OUT:
186 hwif->OUTBSYNC(drive, tf->command, IDE_COMMAND_REG);
187 ndelay(400); /* FIXME */
188 return pre_task_out_intr(drive, task->rq);
189 case TASKFILE_MULTI_IN:
190 case TASKFILE_IN:
Bartlomiej Zolnierkiewicz57d73662008-01-25 22:17:16 +0100191 handler = task_in_intr;
Bartlomiej Zolnierkiewicz1192e522008-01-25 22:17:16 +0100192 /* fall-through */
Bartlomiej Zolnierkiewicz10d90152008-01-25 22:17:16 +0100193 case TASKFILE_NO_DATA:
Bartlomiej Zolnierkiewicz57d73662008-01-25 22:17:16 +0100194 if (handler == NULL)
195 handler = task_no_data_intr;
Bartlomiej Zolnierkiewicz1192e522008-01-25 22:17:16 +0100196 /* WIN_{SPECIFY,RESTORE,SETMULT} use custom handlers */
Bartlomiej Zolnierkiewicz57d73662008-01-25 22:17:16 +0100197 if (task->tf_flags & IDE_TFLAG_CUSTOM_HANDLER) {
198 switch (tf->command) {
199 case WIN_SPECIFY: handler = set_geometry_intr; break;
200 case WIN_RESTORE: handler = recal_intr; break;
201 case WIN_SETMULT: handler = set_multmode_intr; break;
202 }
203 }
204 ide_execute_command(drive, tf->command, handler,
205 WAIT_WORSTCASE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 return ide_started;
Bartlomiej Zolnierkiewicz10d90152008-01-25 22:17:16 +0100207 default:
208 if (task_dma_ok(task) == 0 || drive->using_dma == 0 ||
209 hwif->dma_setup(drive))
210 return ide_stopped;
Bartlomiej Zolnierkiewicz74095a92008-01-25 22:17:07 +0100211 hwif->dma_exec_cmd(drive, tf->command);
212 hwif->dma_start(drive);
213 return ide_started;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215}
Bartlomiej Zolnierkiewiczf6e29e32008-01-25 22:17:16 +0100216EXPORT_SYMBOL_GPL(do_rw_taskfile);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218/*
219 * set_multmode_intr() is invoked on completion of a WIN_SETMULT cmd.
220 */
Bartlomiej Zolnierkiewicz57d73662008-01-25 22:17:16 +0100221static ide_startstop_t set_multmode_intr(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222{
223 ide_hwif_t *hwif = HWIF(drive);
224 u8 stat;
225
226 if (OK_STAT(stat = hwif->INB(IDE_STATUS_REG),READY_STAT,BAD_STAT)) {
227 drive->mult_count = drive->mult_req;
228 } else {
229 drive->mult_req = drive->mult_count = 0;
230 drive->special.b.recalibrate = 1;
231 (void) ide_dump_status(drive, "set_multmode", stat);
232 }
233 return ide_stopped;
234}
235
236/*
237 * set_geometry_intr() is invoked on completion of a WIN_SPECIFY cmd.
238 */
Bartlomiej Zolnierkiewicz57d73662008-01-25 22:17:16 +0100239static ide_startstop_t set_geometry_intr(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240{
241 ide_hwif_t *hwif = HWIF(drive);
242 int retries = 5;
243 u8 stat;
244
245 while (((stat = hwif->INB(IDE_STATUS_REG)) & BUSY_STAT) && retries--)
246 udelay(10);
247
248 if (OK_STAT(stat, READY_STAT, BAD_STAT))
249 return ide_stopped;
250
251 if (stat & (ERR_STAT|DRQ_STAT))
252 return ide_error(drive, "set_geometry_intr", stat);
253
Eric Sesterhenn125e1872006-06-23 02:06:06 -0700254 BUG_ON(HWGROUP(drive)->handler != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 ide_set_handler(drive, &set_geometry_intr, WAIT_WORSTCASE, NULL);
256 return ide_started;
257}
258
259/*
260 * recal_intr() is invoked on completion of a WIN_RESTORE (recalibrate) cmd.
261 */
Bartlomiej Zolnierkiewicz57d73662008-01-25 22:17:16 +0100262static ide_startstop_t recal_intr(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263{
264 ide_hwif_t *hwif = HWIF(drive);
265 u8 stat;
266
267 if (!OK_STAT(stat = hwif->INB(IDE_STATUS_REG), READY_STAT, BAD_STAT))
268 return ide_error(drive, "recal_intr", stat);
269 return ide_stopped;
270}
271
272/*
273 * Handler for commands without a data phase
274 */
Bartlomiej Zolnierkiewicz1192e522008-01-25 22:17:16 +0100275static ide_startstop_t task_no_data_intr(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276{
277 ide_task_t *args = HWGROUP(drive)->rq->special;
278 ide_hwif_t *hwif = HWIF(drive);
279 u8 stat;
280
Ingo Molnar366c7f52006-07-03 00:25:25 -0700281 local_irq_enable_in_hardirq();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 if (!OK_STAT(stat = hwif->INB(IDE_STATUS_REG),READY_STAT,BAD_STAT)) {
283 return ide_error(drive, "task_no_data_intr", stat);
284 /* calls ide_end_drive_cmd */
285 }
286 if (args)
287 ide_end_drive_cmd(drive, stat, hwif->INB(IDE_ERROR_REG));
288
289 return ide_stopped;
290}
291
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292static u8 wait_drive_not_busy(ide_drive_t *drive)
293{
294 ide_hwif_t *hwif = HWIF(drive);
Masatake YAMATOb42fa132007-07-03 22:28:34 +0200295 int retries;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 u8 stat;
297
298 /*
299 * Last sector was transfered, wait until drive is ready.
300 * This can take up to 10 usec, but we will wait max 1 ms
301 * (drive_cmd_intr() waits that long).
302 */
Masatake YAMATOb42fa132007-07-03 22:28:34 +0200303 for (retries = 0; retries < 100; retries++) {
304 if ((stat = hwif->INB(IDE_STATUS_REG)) & BUSY_STAT)
305 udelay(10);
306 else
307 break;
308 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309
Masatake YAMATOb42fa132007-07-03 22:28:34 +0200310 if (stat & BUSY_STAT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 printk(KERN_ERR "%s: drive still BUSY!\n", drive->name);
312
313 return stat;
314}
315
316static void ide_pio_sector(ide_drive_t *drive, unsigned int write)
317{
318 ide_hwif_t *hwif = drive->hwif;
319 struct scatterlist *sg = hwif->sg_table;
Jens Axboe55c16a72007-07-25 08:13:56 +0200320 struct scatterlist *cursg = hwif->cursg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 struct page *page;
322#ifdef CONFIG_HIGHMEM
323 unsigned long flags;
324#endif
325 unsigned int offset;
326 u8 *buf;
327
Jens Axboe55c16a72007-07-25 08:13:56 +0200328 cursg = hwif->cursg;
329 if (!cursg) {
330 cursg = sg;
331 hwif->cursg = sg;
332 }
333
Jens Axboe45711f12007-10-22 21:19:53 +0200334 page = sg_page(cursg);
Jens Axboe55c16a72007-07-25 08:13:56 +0200335 offset = cursg->offset + hwif->cursg_ofs * SECTOR_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336
337 /* get the current page and offset */
338 page = nth_page(page, (offset >> PAGE_SHIFT));
339 offset %= PAGE_SIZE;
340
341#ifdef CONFIG_HIGHMEM
342 local_irq_save(flags);
343#endif
344 buf = kmap_atomic(page, KM_BIO_SRC_IRQ) + offset;
345
346 hwif->nleft--;
347 hwif->cursg_ofs++;
348
Jens Axboe55c16a72007-07-25 08:13:56 +0200349 if ((hwif->cursg_ofs * SECTOR_SIZE) == cursg->length) {
350 hwif->cursg = sg_next(hwif->cursg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 hwif->cursg_ofs = 0;
352 }
353
354 /* do the actual data transfer */
355 if (write)
356 taskfile_output_data(drive, buf, SECTOR_WORDS);
357 else
358 taskfile_input_data(drive, buf, SECTOR_WORDS);
359
360 kunmap_atomic(buf, KM_BIO_SRC_IRQ);
361#ifdef CONFIG_HIGHMEM
362 local_irq_restore(flags);
363#endif
364}
365
366static void ide_pio_multi(ide_drive_t *drive, unsigned int write)
367{
368 unsigned int nsect;
369
370 nsect = min_t(unsigned int, drive->hwif->nleft, drive->mult_count);
371 while (nsect--)
372 ide_pio_sector(drive, write);
373}
374
Arjan van de Ven858119e2006-01-14 13:20:43 -0800375static void ide_pio_datablock(ide_drive_t *drive, struct request *rq,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 unsigned int write)
377{
378 if (rq->bio) /* fs request */
379 rq->errors = 0;
380
Andrew Morton651c29a2006-02-15 15:17:37 -0800381 touch_softlockup_watchdog();
382
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 switch (drive->hwif->data_phase) {
384 case TASKFILE_MULTI_IN:
385 case TASKFILE_MULTI_OUT:
386 ide_pio_multi(drive, write);
387 break;
388 default:
389 ide_pio_sector(drive, write);
390 break;
391 }
392}
393
394static ide_startstop_t task_error(ide_drive_t *drive, struct request *rq,
395 const char *s, u8 stat)
396{
397 if (rq->bio) {
398 ide_hwif_t *hwif = drive->hwif;
399 int sectors = hwif->nsect - hwif->nleft;
400
401 switch (hwif->data_phase) {
402 case TASKFILE_IN:
403 if (hwif->nleft)
404 break;
405 /* fall through */
406 case TASKFILE_OUT:
407 sectors--;
408 break;
409 case TASKFILE_MULTI_IN:
410 if (hwif->nleft)
411 break;
412 /* fall through */
413 case TASKFILE_MULTI_OUT:
414 sectors -= drive->mult_count;
415 default:
416 break;
417 }
418
419 if (sectors > 0) {
420 ide_driver_t *drv;
421
422 drv = *(ide_driver_t **)rq->rq_disk->private_data;
423 drv->end_request(drive, 1, sectors);
424 }
425 }
426 return ide_error(drive, s, stat);
427}
428
429static void task_end_request(ide_drive_t *drive, struct request *rq, u8 stat)
430{
Jens Axboe55c16a72007-07-25 08:13:56 +0200431 HWIF(drive)->cursg = NULL;
432
Jens Axboe4aff5e22006-08-10 08:44:47 +0200433 if (rq->cmd_type == REQ_TYPE_ATA_TASKFILE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 ide_task_t *task = rq->special;
435
Bartlomiej Zolnierkiewicz74095a92008-01-25 22:17:07 +0100436 if (task->tf_flags & IDE_TFLAG_FLAGGED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 u8 err = drive->hwif->INB(IDE_ERROR_REG);
438 ide_end_drive_cmd(drive, stat, err);
439 return;
440 }
441 }
442
Richard Purdie03731fb2006-03-31 02:31:15 -0800443 if (rq->rq_disk) {
444 ide_driver_t *drv;
445
446 drv = *(ide_driver_t **)rq->rq_disk->private_data;;
447 drv->end_request(drive, 1, rq->hard_nr_sectors);
448 } else
449 ide_end_request(drive, 1, rq->hard_nr_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450}
451
452/*
453 * Handler for command with PIO data-in phase (Read/Read Multiple).
454 */
Bartlomiej Zolnierkiewiczf6e29e32008-01-25 22:17:16 +0100455static ide_startstop_t task_in_intr(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456{
457 ide_hwif_t *hwif = drive->hwif;
458 struct request *rq = HWGROUP(drive)->rq;
459 u8 stat = hwif->INB(IDE_STATUS_REG);
460
461 /* new way for dealing with premature shared PCI interrupts */
462 if (!OK_STAT(stat, DATA_READY, BAD_R_STAT)) {
463 if (stat & (ERR_STAT | DRQ_STAT))
464 return task_error(drive, rq, __FUNCTION__, stat);
465 /* No data yet, so wait for another IRQ. */
466 ide_set_handler(drive, &task_in_intr, WAIT_WORSTCASE, NULL);
467 return ide_started;
468 }
469
470 ide_pio_datablock(drive, rq, 0);
471
472 /* If it was the last datablock check status and finish transfer. */
473 if (!hwif->nleft) {
474 stat = wait_drive_not_busy(drive);
475 if (!OK_STAT(stat, 0, BAD_R_STAT))
476 return task_error(drive, rq, __FUNCTION__, stat);
477 task_end_request(drive, rq, stat);
478 return ide_stopped;
479 }
480
481 /* Still data left to transfer. */
482 ide_set_handler(drive, &task_in_intr, WAIT_WORSTCASE, NULL);
483
484 return ide_started;
485}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486
487/*
488 * Handler for command with PIO data-out phase (Write/Write Multiple).
489 */
490static ide_startstop_t task_out_intr (ide_drive_t *drive)
491{
492 ide_hwif_t *hwif = drive->hwif;
493 struct request *rq = HWGROUP(drive)->rq;
494 u8 stat = hwif->INB(IDE_STATUS_REG);
495
496 if (!OK_STAT(stat, DRIVE_READY, drive->bad_wstat))
497 return task_error(drive, rq, __FUNCTION__, stat);
498
499 /* Deal with unexpected ATA data phase. */
500 if (((stat & DRQ_STAT) == 0) ^ !hwif->nleft)
501 return task_error(drive, rq, __FUNCTION__, stat);
502
503 if (!hwif->nleft) {
504 task_end_request(drive, rq, stat);
505 return ide_stopped;
506 }
507
508 /* Still data left to transfer. */
509 ide_pio_datablock(drive, rq, 1);
510 ide_set_handler(drive, &task_out_intr, WAIT_WORSTCASE, NULL);
511
512 return ide_started;
513}
514
Bartlomiej Zolnierkiewiczf6e29e32008-01-25 22:17:16 +0100515static ide_startstop_t pre_task_out_intr(ide_drive_t *drive, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516{
517 ide_startstop_t startstop;
518
519 if (ide_wait_stat(&startstop, drive, DATA_READY,
520 drive->bad_wstat, WAIT_DRQ)) {
521 printk(KERN_ERR "%s: no DRQ after issuing %sWRITE%s\n",
522 drive->name,
523 drive->hwif->data_phase ? "MULT" : "",
524 drive->addressing ? "_EXT" : "");
525 return startstop;
526 }
527
528 if (!drive->unmask)
529 local_irq_disable();
530
531 ide_set_handler(drive, &task_out_intr, WAIT_WORSTCASE, NULL);
532 ide_pio_datablock(drive, rq, 1);
533
534 return ide_started;
535}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536
Bartlomiej Zolnierkiewiczac026ff2008-01-25 22:17:14 +0100537int ide_raw_taskfile(ide_drive_t *drive, ide_task_t *task, u8 *buf, u16 nsect)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538{
539 struct request rq;
540
541 memset(&rq, 0, sizeof(rq));
Bartlomiej Zolnierkiewicz02ac2462007-11-05 21:42:27 +0100542 rq.ref_count = 1;
Jens Axboe4aff5e22006-08-10 08:44:47 +0200543 rq.cmd_type = REQ_TYPE_ATA_TASKFILE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 rq.buffer = buf;
545
546 /*
547 * (ks) We transfer currently only whole sectors.
548 * This is suffient for now. But, it would be great,
549 * if we would find a solution to transfer any size.
550 * To support special commands like READ LONG.
551 */
Bartlomiej Zolnierkiewiczac026ff2008-01-25 22:17:14 +0100552 rq.hard_nr_sectors = rq.nr_sectors = nsect;
553 rq.hard_cur_sectors = rq.current_nr_sectors = nsect;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554
Bartlomiej Zolnierkiewiczac026ff2008-01-25 22:17:14 +0100555 if (task->tf_flags & IDE_TFLAG_WRITE)
556 rq.cmd_flags |= REQ_RW;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557
Bartlomiej Zolnierkiewiczac026ff2008-01-25 22:17:14 +0100558 rq.special = task;
559 task->rq = &rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 return ide_do_drive_cmd(drive, &rq, ide_wait);
562}
563
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564EXPORT_SYMBOL(ide_raw_taskfile);
565
Bartlomiej Zolnierkiewicz9a3c49b2008-01-25 22:17:07 +0100566int ide_no_data_taskfile(ide_drive_t *drive, ide_task_t *task)
567{
Bartlomiej Zolnierkiewiczac026ff2008-01-25 22:17:14 +0100568 task->data_phase = TASKFILE_NO_DATA;
Bartlomiej Zolnierkiewicz9a3c49b2008-01-25 22:17:07 +0100569
Bartlomiej Zolnierkiewiczac026ff2008-01-25 22:17:14 +0100570 return ide_raw_taskfile(drive, task, NULL, 0);
Bartlomiej Zolnierkiewicz9a3c49b2008-01-25 22:17:07 +0100571}
572EXPORT_SYMBOL_GPL(ide_no_data_taskfile);
573
Bartlomiej Zolnierkiewicz26a5b042007-11-05 21:42:27 +0100574#ifdef CONFIG_IDE_TASK_IOCTL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575int ide_taskfile_ioctl (ide_drive_t *drive, unsigned int cmd, unsigned long arg)
576{
577 ide_task_request_t *req_task;
578 ide_task_t args;
579 u8 *outbuf = NULL;
580 u8 *inbuf = NULL;
Bartlomiej Zolnierkiewiczac026ff2008-01-25 22:17:14 +0100581 u8 *data_buf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 int err = 0;
583 int tasksize = sizeof(struct ide_task_request_s);
Alan Cox3a42bb22006-10-16 16:31:02 +0100584 unsigned int taskin = 0;
585 unsigned int taskout = 0;
Bartlomiej Zolnierkiewiczac026ff2008-01-25 22:17:14 +0100586 u16 nsect = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 u8 io_32bit = drive->io_32bit;
588 char __user *buf = (char __user *)arg;
589
590// printk("IDE Taskfile ...\n");
591
Deepak Saxenaf5e3c2f2005-11-07 01:01:25 -0800592 req_task = kzalloc(tasksize, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 if (req_task == NULL) return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 if (copy_from_user(req_task, buf, tasksize)) {
595 kfree(req_task);
596 return -EFAULT;
597 }
598
Alan Cox3a42bb22006-10-16 16:31:02 +0100599 taskout = req_task->out_size;
600 taskin = req_task->in_size;
601
602 if (taskin > 65536 || taskout > 65536) {
603 err = -EINVAL;
604 goto abort;
605 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606
607 if (taskout) {
608 int outtotal = tasksize;
Deepak Saxenaf5e3c2f2005-11-07 01:01:25 -0800609 outbuf = kzalloc(taskout, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 if (outbuf == NULL) {
611 err = -ENOMEM;
612 goto abort;
613 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 if (copy_from_user(outbuf, buf + outtotal, taskout)) {
615 err = -EFAULT;
616 goto abort;
617 }
618 }
619
620 if (taskin) {
621 int intotal = tasksize + taskout;
Deepak Saxenaf5e3c2f2005-11-07 01:01:25 -0800622 inbuf = kzalloc(taskin, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 if (inbuf == NULL) {
624 err = -ENOMEM;
625 goto abort;
626 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 if (copy_from_user(inbuf, buf + intotal, taskin)) {
628 err = -EFAULT;
629 goto abort;
630 }
631 }
632
633 memset(&args, 0, sizeof(ide_task_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634
Bartlomiej Zolnierkiewicz650d8412008-01-25 22:17:06 +0100635 memcpy(&args.tf_array[0], req_task->hob_ports, HDIO_DRIVE_HOB_HDR_SIZE - 2);
636 memcpy(&args.tf_array[6], req_task->io_ports, HDIO_DRIVE_TASK_HDR_SIZE);
Bartlomiej Zolnierkiewicz866e2ec2008-01-25 22:17:14 +0100637
638 args.data_phase = req_task->data_phase;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639
Bartlomiej Zolnierkiewicza3bbb9d2008-01-25 22:17:10 +0100640 args.tf_flags = IDE_TFLAG_OUT_DEVICE;
641 if (drive->addressing == 1)
642 args.tf_flags |= IDE_TFLAG_LBA48;
643
Bartlomiej Zolnierkiewicz74095a92008-01-25 22:17:07 +0100644 if (req_task->out_flags.all) {
645 args.tf_flags |= IDE_TFLAG_FLAGGED;
646
647 if (req_task->out_flags.b.data)
648 args.tf_flags |= IDE_TFLAG_OUT_DATA;
649
650 if (req_task->out_flags.b.nsector_hob)
651 args.tf_flags |= IDE_TFLAG_OUT_HOB_NSECT;
652 if (req_task->out_flags.b.sector_hob)
653 args.tf_flags |= IDE_TFLAG_OUT_HOB_LBAL;
654 if (req_task->out_flags.b.lcyl_hob)
655 args.tf_flags |= IDE_TFLAG_OUT_HOB_LBAM;
656 if (req_task->out_flags.b.hcyl_hob)
657 args.tf_flags |= IDE_TFLAG_OUT_HOB_LBAH;
658
659 if (req_task->out_flags.b.error_feature)
660 args.tf_flags |= IDE_TFLAG_OUT_FEATURE;
661 if (req_task->out_flags.b.nsector)
662 args.tf_flags |= IDE_TFLAG_OUT_NSECT;
663 if (req_task->out_flags.b.sector)
664 args.tf_flags |= IDE_TFLAG_OUT_LBAL;
665 if (req_task->out_flags.b.lcyl)
666 args.tf_flags |= IDE_TFLAG_OUT_LBAM;
667 if (req_task->out_flags.b.hcyl)
668 args.tf_flags |= IDE_TFLAG_OUT_LBAH;
Bartlomiej Zolnierkiewicza3bbb9d2008-01-25 22:17:10 +0100669 } else {
670 args.tf_flags |= IDE_TFLAG_OUT_TF;
671 if (args.tf_flags & IDE_TFLAG_LBA48)
672 args.tf_flags |= IDE_TFLAG_OUT_HOB;
Bartlomiej Zolnierkiewicz74095a92008-01-25 22:17:07 +0100673 }
674
Bartlomiej Zolnierkiewicz866e2ec2008-01-25 22:17:14 +0100675 if (req_task->in_flags.b.data)
676 args.tf_flags |= IDE_TFLAG_IN_DATA;
677
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 drive->io_32bit = 0;
679 switch(req_task->data_phase) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 case TASKFILE_MULTI_OUT:
681 if (!drive->mult_count) {
682 /* (hs): give up if multcount is not set */
683 printk(KERN_ERR "%s: %s Multimode Write " \
684 "multcount is not set\n",
685 drive->name, __FUNCTION__);
686 err = -EPERM;
687 goto abort;
688 }
689 /* fall through */
690 case TASKFILE_OUT:
Bartlomiej Zolnierkiewiczac026ff2008-01-25 22:17:14 +0100691 /* fall through */
692 case TASKFILE_OUT_DMAQ:
693 case TASKFILE_OUT_DMA:
694 nsect = taskout / SECTOR_SIZE;
695 data_buf = outbuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 break;
697 case TASKFILE_MULTI_IN:
698 if (!drive->mult_count) {
699 /* (hs): give up if multcount is not set */
700 printk(KERN_ERR "%s: %s Multimode Read failure " \
701 "multcount is not set\n",
702 drive->name, __FUNCTION__);
703 err = -EPERM;
704 goto abort;
705 }
706 /* fall through */
707 case TASKFILE_IN:
Bartlomiej Zolnierkiewiczac026ff2008-01-25 22:17:14 +0100708 /* fall through */
709 case TASKFILE_IN_DMAQ:
710 case TASKFILE_IN_DMA:
711 nsect = taskin / SECTOR_SIZE;
712 data_buf = inbuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 break;
714 case TASKFILE_NO_DATA:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 break;
716 default:
717 err = -EFAULT;
718 goto abort;
719 }
720
Bartlomiej Zolnierkiewiczac026ff2008-01-25 22:17:14 +0100721 if (req_task->req_cmd == IDE_DRIVE_TASK_NO_DATA)
722 nsect = 0;
723 else if (!nsect) {
724 nsect = (args.tf.hob_nsect << 8) | args.tf.nsect;
725
726 if (!nsect) {
727 printk(KERN_ERR "%s: in/out command without data\n",
728 drive->name);
729 err = -EFAULT;
730 goto abort;
731 }
732 }
733
734 if (req_task->req_cmd == IDE_DRIVE_TASK_RAW_WRITE)
735 args.tf_flags |= IDE_TFLAG_WRITE;
736
737 err = ide_raw_taskfile(drive, &args, data_buf, nsect);
738
Bartlomiej Zolnierkiewicz650d8412008-01-25 22:17:06 +0100739 memcpy(req_task->hob_ports, &args.tf_array[0], HDIO_DRIVE_HOB_HDR_SIZE - 2);
740 memcpy(req_task->io_ports, &args.tf_array[6], HDIO_DRIVE_TASK_HDR_SIZE);
Bartlomiej Zolnierkiewicz866e2ec2008-01-25 22:17:14 +0100741
742 if ((args.tf_flags & IDE_TFLAG_FLAGGED_SET_IN_FLAGS) &&
743 req_task->in_flags.all == 0) {
744 req_task->in_flags.all = IDE_TASKFILE_STD_IN_FLAGS;
745 if (drive->addressing == 1)
746 req_task->in_flags.all |= (IDE_HOB_STD_IN_FLAGS << 8);
747 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748
749 if (copy_to_user(buf, req_task, tasksize)) {
750 err = -EFAULT;
751 goto abort;
752 }
753 if (taskout) {
754 int outtotal = tasksize;
755 if (copy_to_user(buf + outtotal, outbuf, taskout)) {
756 err = -EFAULT;
757 goto abort;
758 }
759 }
760 if (taskin) {
761 int intotal = tasksize + taskout;
762 if (copy_to_user(buf + intotal, inbuf, taskin)) {
763 err = -EFAULT;
764 goto abort;
765 }
766 }
767abort:
768 kfree(req_task);
Jesper Juhl6044ec82005-11-07 01:01:32 -0800769 kfree(outbuf);
770 kfree(inbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771
772// printk("IDE Taskfile ioctl ended. rc = %i\n", err);
773
774 drive->io_32bit = io_32bit;
775
776 return err;
777}
Bartlomiej Zolnierkiewicz26a5b042007-11-05 21:42:27 +0100778#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779
780int ide_wait_cmd (ide_drive_t *drive, u8 cmd, u8 nsect, u8 feature, u8 sectors, u8 *buf)
781{
782 struct request rq;
783 u8 buffer[4];
784
785 if (!buf)
786 buf = buffer;
787 memset(buf, 0, 4 + SECTOR_WORDS * 4 * sectors);
788 ide_init_drive_cmd(&rq);
789 rq.buffer = buf;
790 *buf++ = cmd;
791 *buf++ = nsect;
792 *buf++ = feature;
793 *buf++ = sectors;
794 return ide_do_drive_cmd(drive, &rq, ide_wait);
795}
796
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797int ide_cmd_ioctl (ide_drive_t *drive, unsigned int cmd, unsigned long arg)
798{
799 int err = 0;
800 u8 args[4], *argbuf = args;
801 u8 xfer_rate = 0;
802 int argsize = 4;
803 ide_task_t tfargs;
Bartlomiej Zolnierkiewicz650d8412008-01-25 22:17:06 +0100804 struct ide_taskfile *tf = &tfargs.tf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805
806 if (NULL == (void *) arg) {
807 struct request rq;
808 ide_init_drive_cmd(&rq);
809 return ide_do_drive_cmd(drive, &rq, ide_wait);
810 }
811
812 if (copy_from_user(args, (void __user *)arg, 4))
813 return -EFAULT;
814
815 memset(&tfargs, 0, sizeof(ide_task_t));
Bartlomiej Zolnierkiewicz650d8412008-01-25 22:17:06 +0100816 tf->feature = args[2];
817 tf->nsect = args[3];
818 tf->lbal = args[1];
819 tf->command = args[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820
821 if (args[3]) {
822 argsize = 4 + (SECTOR_WORDS * 4 * args[3]);
Deepak Saxenaf5e3c2f2005-11-07 01:01:25 -0800823 argbuf = kzalloc(argsize, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 if (argbuf == NULL)
825 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 }
827 if (set_transfer(drive, &tfargs)) {
828 xfer_rate = args[1];
829 if (ide_ata66_check(drive, &tfargs))
830 goto abort;
831 }
832
833 err = ide_wait_cmd(drive, args[0], args[1], args[2], args[3], argbuf);
834
835 if (!err && xfer_rate) {
836 /* active-retuning-calls future */
837 ide_set_xfer_rate(drive, xfer_rate);
838 ide_driveid_update(drive);
839 }
840abort:
841 if (copy_to_user((void __user *)arg, argbuf, argsize))
842 err = -EFAULT;
843 if (argsize > 4)
844 kfree(argbuf);
845 return err;
846}
847
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848int ide_task_ioctl (ide_drive_t *drive, unsigned int cmd, unsigned long arg)
849{
850 void __user *p = (void __user *)arg;
851 int err = 0;
Bartlomiej Zolnierkiewicz14b89ef2008-01-25 22:17:11 +0100852 u8 args[7];
853 ide_task_t task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854
855 if (copy_from_user(args, p, 7))
856 return -EFAULT;
Bartlomiej Zolnierkiewicz14b89ef2008-01-25 22:17:11 +0100857
858 memset(&task, 0, sizeof(task));
859 memcpy(&task.tf_array[7], &args[1], 6);
860 task.tf.command = args[0];
861 task.tf_flags = IDE_TFLAG_OUT_TF | IDE_TFLAG_OUT_DEVICE;
862
863 err = ide_no_data_taskfile(drive, &task);
864
865 args[0] = task.tf.command;
866 memcpy(&args[1], &task.tf_array[7], 6);
867
868 if (copy_to_user(p, args, 7))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 err = -EFAULT;
Bartlomiej Zolnierkiewicz14b89ef2008-01-25 22:17:11 +0100870
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 return err;
872}