blob: 925fb92418932b6cb8f8ab12ee6282ae50cf3a2f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Bartlomiej Zolnierkiewicz59bca8c2008-02-01 23:09:33 +01002 * Copyright (C) 2000-2002 Michael Cornwell <cornwell@acm.org>
3 * Copyright (C) 2000-2002 Andre Hedrick <andre@linux-ide.org>
4 * Copyright (C) 2001-2002 Klaus Smolin
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * IBM Storage Technology Division
Bartlomiej Zolnierkiewicz59bca8c2008-02-01 23:09:33 +01006 * Copyright (C) 2003-2004, 2007 Bartlomiej Zolnierkiewicz
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
8 * The big the bad and the ugly.
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 */
10
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/types.h>
12#include <linux/string.h>
13#include <linux/kernel.h>
Andrew Morton651c29a2006-02-15 15:17:37 -080014#include <linux/sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/interrupt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/errno.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/delay.h>
19#include <linux/hdreg.h>
20#include <linux/ide.h>
Jens Axboe55c16a72007-07-25 08:13:56 +020021#include <linux/scatterlist.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <asm/uaccess.h>
24#include <asm/io.h>
25
Bartlomiej Zolnierkiewicz089c5c72008-04-28 23:44:39 +020026void ide_tf_dump(const char *s, struct ide_taskfile *tf)
27{
28#ifdef DEBUG
29 printk("%s: tf: feat 0x%02x nsect 0x%02x lbal 0x%02x "
30 "lbam 0x%02x lbah 0x%02x dev 0x%02x cmd 0x%02x\n",
31 s, tf->feature, tf->nsect, tf->lbal,
32 tf->lbam, tf->lbah, tf->device, tf->command);
33 printk("%s: hob: nsect 0x%02x lbal 0x%02x "
34 "lbam 0x%02x lbah 0x%02x\n",
35 s, tf->hob_nsect, tf->hob_lbal,
36 tf->hob_lbam, tf->hob_lbah);
37#endif
38}
39
Linus Torvalds1da177e2005-04-16 15:20:36 -070040int taskfile_lib_get_identify (ide_drive_t *drive, u8 *buf)
41{
42 ide_task_t args;
Bartlomiej Zolnierkiewicz650d8412008-01-25 22:17:06 +010043
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 memset(&args, 0, sizeof(ide_task_t));
Bartlomiej Zolnierkiewicz650d8412008-01-25 22:17:06 +010045 args.tf.nsect = 0x01;
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 if (drive->media == ide_disk)
Bartlomiej Zolnierkiewiczaaaade32008-10-10 22:39:21 +020047 args.tf.command = ATA_CMD_ID_ATA;
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 else
Bartlomiej Zolnierkiewiczaaaade32008-10-10 22:39:21 +020049 args.tf.command = ATA_CMD_ID_ATAPI;
Bartlomiej Zolnierkiewicz657cc1a2008-01-26 20:13:10 +010050 args.tf_flags = IDE_TFLAG_TF | IDE_TFLAG_DEVICE;
Bartlomiej Zolnierkiewiczac026ff2008-01-25 22:17:14 +010051 args.data_phase = TASKFILE_IN;
Bartlomiej Zolnierkiewiczac026ff2008-01-25 22:17:14 +010052 return ide_raw_taskfile(drive, &args, buf, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070053}
54
Bartlomiej Zolnierkiewicz1192e522008-01-25 22:17:16 +010055static ide_startstop_t task_no_data_intr(ide_drive_t *);
Bartlomiej Zolnierkiewiczf6e29e32008-01-25 22:17:16 +010056static ide_startstop_t pre_task_out_intr(ide_drive_t *, struct request *);
57static ide_startstop_t task_in_intr(ide_drive_t *);
Bartlomiej Zolnierkiewicz1192e522008-01-25 22:17:16 +010058
Linus Torvalds1da177e2005-04-16 15:20:36 -070059ide_startstop_t do_rw_taskfile (ide_drive_t *drive, ide_task_t *task)
60{
Bartlomiej Zolnierkiewicz898ec222009-01-06 17:20:52 +010061 ide_hwif_t *hwif = drive->hwif;
Bartlomiej Zolnierkiewicz650d8412008-01-25 22:17:06 +010062 struct ide_taskfile *tf = &task->tf;
Bartlomiej Zolnierkiewicz57d73662008-01-25 22:17:16 +010063 ide_handler_t *handler = NULL;
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +020064 const struct ide_tp_ops *tp_ops = hwif->tp_ops;
Bartlomiej Zolnierkiewiczf37afda2008-04-26 22:25:24 +020065 const struct ide_dma_ops *dma_ops = hwif->dma_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
Bartlomiej Zolnierkiewicz1edee602008-01-25 22:17:15 +010067 if (task->data_phase == TASKFILE_MULTI_IN ||
68 task->data_phase == TASKFILE_MULTI_OUT) {
69 if (!drive->mult_count) {
70 printk(KERN_ERR "%s: multimode not set!\n",
71 drive->name);
72 return ide_stopped;
73 }
74 }
75
Bartlomiej Zolnierkiewicz19710d22009-03-27 12:46:28 +010076 if (task->ftf_flags & IDE_FTFLAG_FLAGGED)
77 task->ftf_flags |= IDE_FTFLAG_SET_IN_FLAGS;
Bartlomiej Zolnierkiewicz1edee602008-01-25 22:17:15 +010078
Bartlomiej Zolnierkiewiczd6ff9f62008-10-13 21:39:41 +020079 memcpy(&hwif->task, task, sizeof(*task));
80
Bartlomiej Zolnierkiewicz089c5c72008-04-28 23:44:39 +020081 if ((task->tf_flags & IDE_TFLAG_DMA_PIO_FALLBACK) == 0) {
82 ide_tf_dump(drive->name, tf);
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +020083 tp_ops->set_irq(hwif, 1);
Bartlomiej Zolnierkiewiczed4af482008-07-15 21:21:48 +020084 SELECT_MASK(drive, 0);
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +020085 tp_ops->tf_load(drive, task);
Bartlomiej Zolnierkiewicz089c5c72008-04-28 23:44:39 +020086 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
Bartlomiej Zolnierkiewicz10d90152008-01-25 22:17:16 +010088 switch (task->data_phase) {
89 case TASKFILE_MULTI_OUT:
90 case TASKFILE_OUT:
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +020091 tp_ops->exec_command(hwif, tf->command);
Bartlomiej Zolnierkiewicz10d90152008-01-25 22:17:16 +010092 ndelay(400); /* FIXME */
93 return pre_task_out_intr(drive, task->rq);
94 case TASKFILE_MULTI_IN:
95 case TASKFILE_IN:
Bartlomiej Zolnierkiewicz57d73662008-01-25 22:17:16 +010096 handler = task_in_intr;
Bartlomiej Zolnierkiewicz1192e522008-01-25 22:17:16 +010097 /* fall-through */
Bartlomiej Zolnierkiewicz10d90152008-01-25 22:17:16 +010098 case TASKFILE_NO_DATA:
Bartlomiej Zolnierkiewicz57d73662008-01-25 22:17:16 +010099 if (handler == NULL)
100 handler = task_no_data_intr;
Bartlomiej Zolnierkiewicz57d73662008-01-25 22:17:16 +0100101 ide_execute_command(drive, tf->command, handler,
102 WAIT_WORSTCASE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 return ide_started;
Bartlomiej Zolnierkiewicz10d90152008-01-25 22:17:16 +0100104 default:
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200105 if ((drive->dev_flags & IDE_DFLAG_USING_DMA) == 0 ||
Bartlomiej Zolnierkiewicze6830a82009-03-27 12:46:37 +0100106 ide_build_sglist(drive, hwif->rq) == 0 ||
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200107 dma_ops->dma_setup(drive))
Bartlomiej Zolnierkiewicz10d90152008-01-25 22:17:16 +0100108 return ide_stopped;
Bartlomiej Zolnierkiewicz5e37bdc2008-04-26 22:25:24 +0200109 dma_ops->dma_exec_cmd(drive, tf->command);
110 dma_ops->dma_start(drive);
Bartlomiej Zolnierkiewicz74095a92008-01-25 22:17:07 +0100111 return ide_started;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113}
Bartlomiej Zolnierkiewiczf6e29e32008-01-25 22:17:16 +0100114EXPORT_SYMBOL_GPL(do_rw_taskfile);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116/*
Bartlomiej Zolnierkiewiczd6ff9f62008-10-13 21:39:41 +0200117 * Handler for commands without a data phase
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 */
Bartlomiej Zolnierkiewiczd6ff9f62008-10-13 21:39:41 +0200119static ide_startstop_t task_no_data_intr(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120{
Bartlomiej Zolnierkiewiczb73c7ee2008-07-23 19:55:52 +0200121 ide_hwif_t *hwif = drive->hwif;
Bartlomiej Zolnierkiewiczd6ff9f62008-10-13 21:39:41 +0200122 ide_task_t *task = &hwif->task;
123 struct ide_taskfile *tf = &task->tf;
124 int custom = (task->tf_flags & IDE_TFLAG_CUSTOM_HANDLER) ? 1 : 0;
125 int retries = (custom && tf->command == ATA_CMD_INIT_DEV_PARAMS) ? 5 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 u8 stat;
127
Bartlomiej Zolnierkiewicz90d2c6b2008-07-24 22:53:36 +0200128 local_irq_enable_in_hardirq();
129
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200130 while (1) {
131 stat = hwif->tp_ops->read_status(hwif);
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +0200132 if ((stat & ATA_BUSY) == 0 || retries-- == 0)
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200133 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 udelay(10);
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200135 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
Bartlomiej Zolnierkiewiczd6ff9f62008-10-13 21:39:41 +0200137 if (!OK_STAT(stat, ATA_DRDY, BAD_STAT)) {
138 if (custom && tf->command == ATA_CMD_SET_MULTI) {
139 drive->mult_req = drive->mult_count = 0;
140 drive->special.b.recalibrate = 1;
141 (void)ide_dump_status(drive, __func__, stat);
142 return ide_stopped;
143 } else if (custom && tf->command == ATA_CMD_INIT_DEV_PARAMS) {
144 if ((stat & (ATA_ERR | ATA_DRQ)) == 0) {
145 ide_set_handler(drive, &task_no_data_intr,
146 WAIT_WORSTCASE, NULL);
147 return ide_started;
148 }
149 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 return ide_error(drive, "task_no_data_intr", stat);
Bartlomiej Zolnierkiewiczd6ff9f62008-10-13 21:39:41 +0200151 }
Bartlomiej Zolnierkiewiczc47137a2008-02-06 02:57:51 +0100152
Bartlomiej Zolnierkiewicza09485d2009-03-27 12:46:31 +0100153 if (custom && tf->command == ATA_CMD_IDLEIMMEDIATE) {
Elias Oltmanns4abdc6e2008-10-13 21:39:50 +0200154 hwif->tp_ops->tf_read(drive, task);
155 if (tf->lbal != 0xc4) {
156 printk(KERN_ERR "%s: head unload failed!\n",
157 drive->name);
158 ide_tf_dump(drive->name, tf);
159 } else
160 drive->dev_flags |= IDE_DFLAG_PARKED;
Bartlomiej Zolnierkiewicza09485d2009-03-27 12:46:31 +0100161 } else if (custom && tf->command == ATA_CMD_SET_MULTI)
Bartlomiej Zolnierkiewiczd6ff9f62008-10-13 21:39:41 +0200162 drive->mult_count = drive->mult_req;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
Bartlomiej Zolnierkiewicza09485d2009-03-27 12:46:31 +0100164 if (custom == 0 || tf->command == ATA_CMD_IDLEIMMEDIATE) {
165 struct request *rq = hwif->rq;
166 u8 err = ide_read_error(drive);
167
168 if (blk_pm_request(rq))
169 ide_complete_pm_rq(drive, rq);
170 else {
171 if (rq->cmd_type == REQ_TYPE_ATA_TASKFILE)
172 ide_complete_task(drive, task, stat, err);
173 ide_complete_rq(drive, err);
174 }
175 }
176
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 return ide_stopped;
178}
179
Adrian Bunkda6f4c72008-02-01 23:09:16 +0100180static u8 wait_drive_not_busy(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181{
Bartlomiej Zolnierkiewiczb73c7ee2008-07-23 19:55:52 +0200182 ide_hwif_t *hwif = drive->hwif;
Masatake YAMATOb42fa132007-07-03 22:28:34 +0200183 int retries;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 u8 stat;
185
186 /*
Bartlomiej Zolnierkiewiczf54feaf2008-06-20 20:53:33 +0200187 * Last sector was transfered, wait until device is ready. This can
188 * take up to 6 ms on some ATAPI devices, so we will wait max 10 ms.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 */
Bartlomiej Zolnierkiewiczf54feaf2008-06-20 20:53:33 +0200190 for (retries = 0; retries < 1000; retries++) {
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200191 stat = hwif->tp_ops->read_status(hwif);
Bartlomiej Zolnierkiewiczc47137a2008-02-06 02:57:51 +0100192
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +0200193 if (stat & ATA_BUSY)
Masatake YAMATOb42fa132007-07-03 22:28:34 +0200194 udelay(10);
195 else
196 break;
197 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +0200199 if (stat & ATA_BUSY)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 printk(KERN_ERR "%s: drive still BUSY!\n", drive->name);
201
202 return stat;
203}
204
Bartlomiej Zolnierkiewicz92d3ab22008-04-28 23:44:36 +0200205static void ide_pio_sector(ide_drive_t *drive, struct request *rq,
206 unsigned int write)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207{
208 ide_hwif_t *hwif = drive->hwif;
209 struct scatterlist *sg = hwif->sg_table;
Jens Axboe55c16a72007-07-25 08:13:56 +0200210 struct scatterlist *cursg = hwif->cursg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 struct page *page;
212#ifdef CONFIG_HIGHMEM
213 unsigned long flags;
214#endif
215 unsigned int offset;
216 u8 *buf;
217
Jens Axboe55c16a72007-07-25 08:13:56 +0200218 cursg = hwif->cursg;
219 if (!cursg) {
220 cursg = sg;
221 hwif->cursg = sg;
222 }
223
Jens Axboe45711f12007-10-22 21:19:53 +0200224 page = sg_page(cursg);
Jens Axboe55c16a72007-07-25 08:13:56 +0200225 offset = cursg->offset + hwif->cursg_ofs * SECTOR_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226
227 /* get the current page and offset */
228 page = nth_page(page, (offset >> PAGE_SHIFT));
229 offset %= PAGE_SIZE;
230
231#ifdef CONFIG_HIGHMEM
232 local_irq_save(flags);
233#endif
234 buf = kmap_atomic(page, KM_BIO_SRC_IRQ) + offset;
235
236 hwif->nleft--;
237 hwif->cursg_ofs++;
238
Jens Axboe55c16a72007-07-25 08:13:56 +0200239 if ((hwif->cursg_ofs * SECTOR_SIZE) == cursg->length) {
240 hwif->cursg = sg_next(hwif->cursg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 hwif->cursg_ofs = 0;
242 }
243
244 /* do the actual data transfer */
245 if (write)
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200246 hwif->tp_ops->output_data(drive, rq, buf, SECTOR_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 else
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200248 hwif->tp_ops->input_data(drive, rq, buf, SECTOR_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249
250 kunmap_atomic(buf, KM_BIO_SRC_IRQ);
251#ifdef CONFIG_HIGHMEM
252 local_irq_restore(flags);
253#endif
254}
255
Bartlomiej Zolnierkiewicz92d3ab22008-04-28 23:44:36 +0200256static void ide_pio_multi(ide_drive_t *drive, struct request *rq,
257 unsigned int write)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258{
259 unsigned int nsect;
260
261 nsect = min_t(unsigned int, drive->hwif->nleft, drive->mult_count);
262 while (nsect--)
Bartlomiej Zolnierkiewicz92d3ab22008-04-28 23:44:36 +0200263 ide_pio_sector(drive, rq, write);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264}
265
Arjan van de Ven858119e2006-01-14 13:20:43 -0800266static void ide_pio_datablock(ide_drive_t *drive, struct request *rq,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 unsigned int write)
268{
Bartlomiej Zolnierkiewicze3d9a732009-03-27 12:46:32 +0100269 ide_task_t *task = &drive->hwif->task;
Tejun Heo35cf2b92008-01-26 20:13:10 +0100270 u8 saved_io_32bit = drive->io_32bit;
271
Bartlomiej Zolnierkiewiczb109f522009-03-27 12:46:37 +0100272 if (blk_fs_request(rq))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 rq->errors = 0;
274
Bartlomiej Zolnierkiewicze3d9a732009-03-27 12:46:32 +0100275 if (task->tf_flags & IDE_TFLAG_IO_16BIT)
276 drive->io_32bit = 0;
Tejun Heo35cf2b92008-01-26 20:13:10 +0100277
Andrew Morton651c29a2006-02-15 15:17:37 -0800278 touch_softlockup_watchdog();
279
Bartlomiej Zolnierkiewicze3d9a732009-03-27 12:46:32 +0100280 switch (task->data_phase) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 case TASKFILE_MULTI_IN:
282 case TASKFILE_MULTI_OUT:
Bartlomiej Zolnierkiewicz92d3ab22008-04-28 23:44:36 +0200283 ide_pio_multi(drive, rq, write);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 break;
285 default:
Bartlomiej Zolnierkiewicz92d3ab22008-04-28 23:44:36 +0200286 ide_pio_sector(drive, rq, write);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 break;
288 }
Tejun Heo35cf2b92008-01-26 20:13:10 +0100289
290 drive->io_32bit = saved_io_32bit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291}
292
293static ide_startstop_t task_error(ide_drive_t *drive, struct request *rq,
294 const char *s, u8 stat)
295{
Bartlomiej Zolnierkiewiczb109f522009-03-27 12:46:37 +0100296 if (blk_fs_request(rq)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 ide_hwif_t *hwif = drive->hwif;
Bartlomiej Zolnierkiewicze3d9a732009-03-27 12:46:32 +0100298 ide_task_t *task = &hwif->task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 int sectors = hwif->nsect - hwif->nleft;
300
Bartlomiej Zolnierkiewicze3d9a732009-03-27 12:46:32 +0100301 switch (task->data_phase) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 case TASKFILE_IN:
303 if (hwif->nleft)
304 break;
305 /* fall through */
306 case TASKFILE_OUT:
307 sectors--;
308 break;
309 case TASKFILE_MULTI_IN:
310 if (hwif->nleft)
311 break;
312 /* fall through */
313 case TASKFILE_MULTI_OUT:
314 sectors -= drive->mult_count;
315 default:
316 break;
317 }
318
Bartlomiej Zolnierkiewiczc152cc12009-03-27 12:46:34 +0100319 if (sectors > 0)
320 ide_end_request(drive, 1, sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 }
322 return ide_error(drive, s, stat);
323}
324
Tejun Heo4d7a9842008-01-26 20:13:11 +0100325void task_end_request(ide_drive_t *drive, struct request *rq, u8 stat)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326{
Bartlomiej Zolnierkiewiczb109f522009-03-27 12:46:37 +0100327 if (blk_fs_request(rq) == 0) {
Bartlomiej Zolnierkiewicza09485d2009-03-27 12:46:31 +0100328 ide_task_t *task = rq->special;
Bartlomiej Zolnierkiewicz64a57fe2008-02-06 02:57:51 +0100329 u8 err = ide_read_error(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
Bartlomiej Zolnierkiewicza09485d2009-03-27 12:46:31 +0100331 if (task)
332 ide_complete_task(drive, task, stat, err);
333 ide_complete_rq(drive, err);
Tejun Heo4d7a9842008-01-26 20:13:11 +0100334 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 }
336
Bartlomiej Zolnierkiewiczc152cc12009-03-27 12:46:34 +0100337 ide_end_request(drive, 1, rq->nr_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338}
339
340/*
Linus Torvalds6c3c3152008-03-18 21:26:24 -0700341 * We got an interrupt on a task_in case, but no errors and no DRQ.
342 *
343 * It might be a spurious irq (shared irq), but it might be a
344 * command that had no output.
345 */
346static ide_startstop_t task_in_unexpected(ide_drive_t *drive, struct request *rq, u8 stat)
347{
348 /* Command all done? */
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +0200349 if (OK_STAT(stat, ATA_DRDY, ATA_BUSY)) {
Linus Torvalds6c3c3152008-03-18 21:26:24 -0700350 task_end_request(drive, rq, stat);
351 return ide_stopped;
352 }
353
354 /* Assume it was a spurious irq */
355 ide_set_handler(drive, &task_in_intr, WAIT_WORSTCASE, NULL);
356 return ide_started;
357}
358
359/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 * Handler for command with PIO data-in phase (Read/Read Multiple).
361 */
Bartlomiej Zolnierkiewiczf6e29e32008-01-25 22:17:16 +0100362static ide_startstop_t task_in_intr(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363{
364 ide_hwif_t *hwif = drive->hwif;
Bartlomiej Zolnierkiewiczb65fac32009-01-06 17:20:50 +0100365 struct request *rq = hwif->rq;
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200366 u8 stat = hwif->tp_ops->read_status(hwif);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367
Linus Torvalds6c3c3152008-03-18 21:26:24 -0700368 /* Error? */
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +0200369 if (stat & ATA_ERR)
Harvey Harrisoneb639632008-04-26 22:25:20 +0200370 return task_error(drive, rq, __func__, stat);
Linus Torvalds6c3c3152008-03-18 21:26:24 -0700371
372 /* Didn't want any data? Odd. */
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +0200373 if ((stat & ATA_DRQ) == 0)
Linus Torvalds6c3c3152008-03-18 21:26:24 -0700374 return task_in_unexpected(drive, rq, stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
376 ide_pio_datablock(drive, rq, 0);
377
Linus Torvalds6c3c3152008-03-18 21:26:24 -0700378 /* Are we done? Check status and finish transfer. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 if (!hwif->nleft) {
380 stat = wait_drive_not_busy(drive);
Bartlomiej Zolnierkiewicz73d7de02008-01-26 20:13:10 +0100381 if (!OK_STAT(stat, 0, BAD_STAT))
Harvey Harrisoneb639632008-04-26 22:25:20 +0200382 return task_error(drive, rq, __func__, stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 task_end_request(drive, rq, stat);
384 return ide_stopped;
385 }
386
387 /* Still data left to transfer. */
388 ide_set_handler(drive, &task_in_intr, WAIT_WORSTCASE, NULL);
389
390 return ide_started;
391}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392
393/*
394 * Handler for command with PIO data-out phase (Write/Write Multiple).
395 */
396static ide_startstop_t task_out_intr (ide_drive_t *drive)
397{
398 ide_hwif_t *hwif = drive->hwif;
Bartlomiej Zolnierkiewiczb65fac32009-01-06 17:20:50 +0100399 struct request *rq = hwif->rq;
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200400 u8 stat = hwif->tp_ops->read_status(hwif);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401
402 if (!OK_STAT(stat, DRIVE_READY, drive->bad_wstat))
Harvey Harrisoneb639632008-04-26 22:25:20 +0200403 return task_error(drive, rq, __func__, stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404
405 /* Deal with unexpected ATA data phase. */
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +0200406 if (((stat & ATA_DRQ) == 0) ^ !hwif->nleft)
Harvey Harrisoneb639632008-04-26 22:25:20 +0200407 return task_error(drive, rq, __func__, stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
409 if (!hwif->nleft) {
410 task_end_request(drive, rq, stat);
411 return ide_stopped;
412 }
413
414 /* Still data left to transfer. */
415 ide_pio_datablock(drive, rq, 1);
416 ide_set_handler(drive, &task_out_intr, WAIT_WORSTCASE, NULL);
417
418 return ide_started;
419}
420
Bartlomiej Zolnierkiewiczf6e29e32008-01-25 22:17:16 +0100421static ide_startstop_t pre_task_out_intr(ide_drive_t *drive, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422{
Bartlomiej Zolnierkiewicze3d9a732009-03-27 12:46:32 +0100423 ide_task_t *task = &drive->hwif->task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 ide_startstop_t startstop;
425
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +0200426 if (ide_wait_stat(&startstop, drive, ATA_DRQ,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 drive->bad_wstat, WAIT_DRQ)) {
428 printk(KERN_ERR "%s: no DRQ after issuing %sWRITE%s\n",
Bartlomiej Zolnierkiewiczc7db9662009-03-27 12:46:27 +0100429 drive->name,
Bartlomiej Zolnierkiewicze3d9a732009-03-27 12:46:32 +0100430 task->data_phase == TASKFILE_MULTI_OUT ? "MULT" : "",
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200431 (drive->dev_flags & IDE_DFLAG_LBA48) ? "_EXT" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 return startstop;
433 }
434
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200435 if ((drive->dev_flags & IDE_DFLAG_UNMASK) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 local_irq_disable();
437
438 ide_set_handler(drive, &task_out_intr, WAIT_WORSTCASE, NULL);
439 ide_pio_datablock(drive, rq, 1);
440
441 return ide_started;
442}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443
Bartlomiej Zolnierkiewiczac026ff2008-01-25 22:17:14 +0100444int ide_raw_taskfile(ide_drive_t *drive, ide_task_t *task, u8 *buf, u16 nsect)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445{
FUJITA Tomonori154ed282008-07-15 21:21:43 +0200446 struct request *rq;
447 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448
FUJITA Tomonori154ed282008-07-15 21:21:43 +0200449 rq = blk_get_request(drive->queue, READ, __GFP_WAIT);
450 rq->cmd_type = REQ_TYPE_ATA_TASKFILE;
451 rq->buffer = buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452
453 /*
454 * (ks) We transfer currently only whole sectors.
455 * This is suffient for now. But, it would be great,
456 * if we would find a solution to transfer any size.
457 * To support special commands like READ LONG.
458 */
FUJITA Tomonori154ed282008-07-15 21:21:43 +0200459 rq->hard_nr_sectors = rq->nr_sectors = nsect;
460 rq->hard_cur_sectors = rq->current_nr_sectors = nsect;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461
Bartlomiej Zolnierkiewiczac026ff2008-01-25 22:17:14 +0100462 if (task->tf_flags & IDE_TFLAG_WRITE)
FUJITA Tomonori154ed282008-07-15 21:21:43 +0200463 rq->cmd_flags |= REQ_RW;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464
FUJITA Tomonori154ed282008-07-15 21:21:43 +0200465 rq->special = task;
466 task->rq = rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467
FUJITA Tomonori154ed282008-07-15 21:21:43 +0200468 error = blk_execute_rq(drive->queue, NULL, rq, 0);
469 blk_put_request(rq);
470
471 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472}
473
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474EXPORT_SYMBOL(ide_raw_taskfile);
475
Bartlomiej Zolnierkiewicz9a3c49b2008-01-25 22:17:07 +0100476int ide_no_data_taskfile(ide_drive_t *drive, ide_task_t *task)
477{
Bartlomiej Zolnierkiewiczac026ff2008-01-25 22:17:14 +0100478 task->data_phase = TASKFILE_NO_DATA;
Bartlomiej Zolnierkiewicz9a3c49b2008-01-25 22:17:07 +0100479
Bartlomiej Zolnierkiewiczac026ff2008-01-25 22:17:14 +0100480 return ide_raw_taskfile(drive, task, NULL, 0);
Bartlomiej Zolnierkiewicz9a3c49b2008-01-25 22:17:07 +0100481}
482EXPORT_SYMBOL_GPL(ide_no_data_taskfile);
483
Bartlomiej Zolnierkiewicz26a5b042007-11-05 21:42:27 +0100484#ifdef CONFIG_IDE_TASK_IOCTL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485int ide_taskfile_ioctl (ide_drive_t *drive, unsigned int cmd, unsigned long arg)
486{
487 ide_task_request_t *req_task;
488 ide_task_t args;
489 u8 *outbuf = NULL;
490 u8 *inbuf = NULL;
Bartlomiej Zolnierkiewiczac026ff2008-01-25 22:17:14 +0100491 u8 *data_buf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 int err = 0;
493 int tasksize = sizeof(struct ide_task_request_s);
Alan Cox3a42bb22006-10-16 16:31:02 +0100494 unsigned int taskin = 0;
495 unsigned int taskout = 0;
Bartlomiej Zolnierkiewiczac026ff2008-01-25 22:17:14 +0100496 u16 nsect = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 char __user *buf = (char __user *)arg;
498
499// printk("IDE Taskfile ...\n");
500
Deepak Saxenaf5e3c2f2005-11-07 01:01:25 -0800501 req_task = kzalloc(tasksize, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 if (req_task == NULL) return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 if (copy_from_user(req_task, buf, tasksize)) {
504 kfree(req_task);
505 return -EFAULT;
506 }
507
Alan Cox3a42bb22006-10-16 16:31:02 +0100508 taskout = req_task->out_size;
509 taskin = req_task->in_size;
510
511 if (taskin > 65536 || taskout > 65536) {
512 err = -EINVAL;
513 goto abort;
514 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515
516 if (taskout) {
517 int outtotal = tasksize;
Deepak Saxenaf5e3c2f2005-11-07 01:01:25 -0800518 outbuf = kzalloc(taskout, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 if (outbuf == NULL) {
520 err = -ENOMEM;
521 goto abort;
522 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 if (copy_from_user(outbuf, buf + outtotal, taskout)) {
524 err = -EFAULT;
525 goto abort;
526 }
527 }
528
529 if (taskin) {
530 int intotal = tasksize + taskout;
Deepak Saxenaf5e3c2f2005-11-07 01:01:25 -0800531 inbuf = kzalloc(taskin, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 if (inbuf == NULL) {
533 err = -ENOMEM;
534 goto abort;
535 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 if (copy_from_user(inbuf, buf + intotal, taskin)) {
537 err = -EFAULT;
538 goto abort;
539 }
540 }
541
542 memset(&args, 0, sizeof(ide_task_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543
Bartlomiej Zolnierkiewicz650d8412008-01-25 22:17:06 +0100544 memcpy(&args.tf_array[0], req_task->hob_ports, HDIO_DRIVE_HOB_HDR_SIZE - 2);
545 memcpy(&args.tf_array[6], req_task->io_ports, HDIO_DRIVE_TASK_HDR_SIZE);
Bartlomiej Zolnierkiewicz866e2ec2008-01-25 22:17:14 +0100546
547 args.data_phase = req_task->data_phase;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548
Bartlomiej Zolnierkiewicz657cc1a2008-01-26 20:13:10 +0100549 args.tf_flags = IDE_TFLAG_IO_16BIT | IDE_TFLAG_DEVICE |
550 IDE_TFLAG_IN_TF;
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200551 if (drive->dev_flags & IDE_DFLAG_LBA48)
Bartlomiej Zolnierkiewicz657cc1a2008-01-26 20:13:10 +0100552 args.tf_flags |= (IDE_TFLAG_LBA48 | IDE_TFLAG_IN_HOB);
Bartlomiej Zolnierkiewicza3bbb9d2008-01-25 22:17:10 +0100553
Bartlomiej Zolnierkiewicz74095a92008-01-25 22:17:07 +0100554 if (req_task->out_flags.all) {
Bartlomiej Zolnierkiewicz19710d22009-03-27 12:46:28 +0100555 args.ftf_flags |= IDE_FTFLAG_FLAGGED;
Bartlomiej Zolnierkiewicz74095a92008-01-25 22:17:07 +0100556
557 if (req_task->out_flags.b.data)
Bartlomiej Zolnierkiewicz19710d22009-03-27 12:46:28 +0100558 args.ftf_flags |= IDE_FTFLAG_OUT_DATA;
Bartlomiej Zolnierkiewicz74095a92008-01-25 22:17:07 +0100559
560 if (req_task->out_flags.b.nsector_hob)
561 args.tf_flags |= IDE_TFLAG_OUT_HOB_NSECT;
562 if (req_task->out_flags.b.sector_hob)
563 args.tf_flags |= IDE_TFLAG_OUT_HOB_LBAL;
564 if (req_task->out_flags.b.lcyl_hob)
565 args.tf_flags |= IDE_TFLAG_OUT_HOB_LBAM;
566 if (req_task->out_flags.b.hcyl_hob)
567 args.tf_flags |= IDE_TFLAG_OUT_HOB_LBAH;
568
569 if (req_task->out_flags.b.error_feature)
570 args.tf_flags |= IDE_TFLAG_OUT_FEATURE;
571 if (req_task->out_flags.b.nsector)
572 args.tf_flags |= IDE_TFLAG_OUT_NSECT;
573 if (req_task->out_flags.b.sector)
574 args.tf_flags |= IDE_TFLAG_OUT_LBAL;
575 if (req_task->out_flags.b.lcyl)
576 args.tf_flags |= IDE_TFLAG_OUT_LBAM;
577 if (req_task->out_flags.b.hcyl)
578 args.tf_flags |= IDE_TFLAG_OUT_LBAH;
Bartlomiej Zolnierkiewicza3bbb9d2008-01-25 22:17:10 +0100579 } else {
580 args.tf_flags |= IDE_TFLAG_OUT_TF;
581 if (args.tf_flags & IDE_TFLAG_LBA48)
582 args.tf_flags |= IDE_TFLAG_OUT_HOB;
Bartlomiej Zolnierkiewicz74095a92008-01-25 22:17:07 +0100583 }
584
Bartlomiej Zolnierkiewicz866e2ec2008-01-25 22:17:14 +0100585 if (req_task->in_flags.b.data)
Bartlomiej Zolnierkiewicz19710d22009-03-27 12:46:28 +0100586 args.ftf_flags |= IDE_FTFLAG_IN_DATA;
Bartlomiej Zolnierkiewicz866e2ec2008-01-25 22:17:14 +0100587
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 switch(req_task->data_phase) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 case TASKFILE_MULTI_OUT:
590 if (!drive->mult_count) {
591 /* (hs): give up if multcount is not set */
592 printk(KERN_ERR "%s: %s Multimode Write " \
593 "multcount is not set\n",
Harvey Harrisoneb639632008-04-26 22:25:20 +0200594 drive->name, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 err = -EPERM;
596 goto abort;
597 }
598 /* fall through */
599 case TASKFILE_OUT:
Bartlomiej Zolnierkiewiczac026ff2008-01-25 22:17:14 +0100600 /* fall through */
601 case TASKFILE_OUT_DMAQ:
602 case TASKFILE_OUT_DMA:
603 nsect = taskout / SECTOR_SIZE;
604 data_buf = outbuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 break;
606 case TASKFILE_MULTI_IN:
607 if (!drive->mult_count) {
608 /* (hs): give up if multcount is not set */
609 printk(KERN_ERR "%s: %s Multimode Read failure " \
610 "multcount is not set\n",
Harvey Harrisoneb639632008-04-26 22:25:20 +0200611 drive->name, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 err = -EPERM;
613 goto abort;
614 }
615 /* fall through */
616 case TASKFILE_IN:
Bartlomiej Zolnierkiewiczac026ff2008-01-25 22:17:14 +0100617 /* fall through */
618 case TASKFILE_IN_DMAQ:
619 case TASKFILE_IN_DMA:
620 nsect = taskin / SECTOR_SIZE;
621 data_buf = inbuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 break;
623 case TASKFILE_NO_DATA:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 break;
625 default:
626 err = -EFAULT;
627 goto abort;
628 }
629
Bartlomiej Zolnierkiewiczac026ff2008-01-25 22:17:14 +0100630 if (req_task->req_cmd == IDE_DRIVE_TASK_NO_DATA)
631 nsect = 0;
632 else if (!nsect) {
633 nsect = (args.tf.hob_nsect << 8) | args.tf.nsect;
634
635 if (!nsect) {
636 printk(KERN_ERR "%s: in/out command without data\n",
637 drive->name);
638 err = -EFAULT;
639 goto abort;
640 }
641 }
642
643 if (req_task->req_cmd == IDE_DRIVE_TASK_RAW_WRITE)
644 args.tf_flags |= IDE_TFLAG_WRITE;
645
646 err = ide_raw_taskfile(drive, &args, data_buf, nsect);
647
Bartlomiej Zolnierkiewicz650d8412008-01-25 22:17:06 +0100648 memcpy(req_task->hob_ports, &args.tf_array[0], HDIO_DRIVE_HOB_HDR_SIZE - 2);
649 memcpy(req_task->io_ports, &args.tf_array[6], HDIO_DRIVE_TASK_HDR_SIZE);
Bartlomiej Zolnierkiewicz866e2ec2008-01-25 22:17:14 +0100650
Bartlomiej Zolnierkiewicz19710d22009-03-27 12:46:28 +0100651 if ((args.ftf_flags & IDE_FTFLAG_SET_IN_FLAGS) &&
Bartlomiej Zolnierkiewicz866e2ec2008-01-25 22:17:14 +0100652 req_task->in_flags.all == 0) {
653 req_task->in_flags.all = IDE_TASKFILE_STD_IN_FLAGS;
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200654 if (drive->dev_flags & IDE_DFLAG_LBA48)
Bartlomiej Zolnierkiewicz866e2ec2008-01-25 22:17:14 +0100655 req_task->in_flags.all |= (IDE_HOB_STD_IN_FLAGS << 8);
656 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657
658 if (copy_to_user(buf, req_task, tasksize)) {
659 err = -EFAULT;
660 goto abort;
661 }
662 if (taskout) {
663 int outtotal = tasksize;
664 if (copy_to_user(buf + outtotal, outbuf, taskout)) {
665 err = -EFAULT;
666 goto abort;
667 }
668 }
669 if (taskin) {
670 int intotal = tasksize + taskout;
671 if (copy_to_user(buf + intotal, inbuf, taskin)) {
672 err = -EFAULT;
673 goto abort;
674 }
675 }
676abort:
677 kfree(req_task);
Jesper Juhl6044ec82005-11-07 01:01:32 -0800678 kfree(outbuf);
679 kfree(inbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680
681// printk("IDE Taskfile ioctl ended. rc = %i\n", err);
682
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 return err;
684}
Bartlomiej Zolnierkiewicz26a5b042007-11-05 21:42:27 +0100685#endif