blob: 4a6576f6146bee3bcd5fc1fa0ff54c6d157b3624 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * IDE I/O functions
3 *
4 * Basic PIO and command management functionality.
5 *
6 * This code was split off from ide.c. See ide.c for history and original
7 * copyrights.
8 *
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the
11 * Free Software Foundation; either version 2, or (at your option) any
12 * later version.
13 *
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
18 *
19 * For the avoidance of doubt the "preferred form" of this code is one which
20 * is in an open non patent encumbered format. Where cryptographic key signing
21 * forms part of the process of creating an executable the information
22 * including keys needed to generate an equivalently functional executable
23 * are deemed to be part of the source code.
24 */
25
26
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/module.h>
28#include <linux/types.h>
29#include <linux/string.h>
30#include <linux/kernel.h>
31#include <linux/timer.h>
32#include <linux/mm.h>
33#include <linux/interrupt.h>
34#include <linux/major.h>
35#include <linux/errno.h>
36#include <linux/genhd.h>
37#include <linux/blkpg.h>
38#include <linux/slab.h>
39#include <linux/init.h>
40#include <linux/pci.h>
41#include <linux/delay.h>
42#include <linux/ide.h>
Bartlomiej Zolnierkiewicz3ceca722008-10-10 22:39:27 +020043#include <linux/hdreg.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include <linux/completion.h>
45#include <linux/reboot.h>
46#include <linux/cdrom.h>
47#include <linux/seq_file.h>
48#include <linux/device.h>
49#include <linux/kmod.h>
50#include <linux/scatterlist.h>
Jiri Slaby1977f032007-10-18 23:40:25 -070051#include <linux/bitops.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
53#include <asm/byteorder.h>
54#include <asm/irq.h>
55#include <asm/uaccess.h>
56#include <asm/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
Adrian Bunka7ff7d42006-02-03 03:04:56 -080058static int __ide_end_request(ide_drive_t *drive, struct request *rq,
Bartlomiej Zolnierkiewiczbbc615b2007-10-20 00:32:36 +020059 int uptodate, unsigned int nr_bytes, int dequeue)
Linus Torvalds1da177e2005-04-16 15:20:36 -070060{
61 int ret = 1;
Kiyoshi Ueda5e36bb62008-01-28 10:34:20 +010062 int error = 0;
63
64 if (uptodate <= 0)
65 error = uptodate ? uptodate : -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 /*
68 * if failfast is set on a request, override number of sectors and
69 * complete the whole request right now
70 */
Kiyoshi Ueda5e36bb62008-01-28 10:34:20 +010071 if (blk_noretry_request(rq) && error)
Jens Axboe41e9d342007-07-19 08:13:01 +020072 nr_bytes = rq->hard_nr_sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
Kiyoshi Ueda5e36bb62008-01-28 10:34:20 +010074 if (!blk_fs_request(rq) && error && !rq->errors)
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 rq->errors = -EIO;
76
77 /*
78 * decide whether to reenable DMA -- 3 is a random magic for now,
79 * if we DMA timeout more than 3 times, just stay in PIO
80 */
Bartlomiej Zolnierkiewiczc3922042008-10-13 21:39:37 +020081 if ((drive->dev_flags & IDE_DFLAG_DMA_PIO_RETRY) &&
82 drive->retry_pio <= 3) {
83 drive->dev_flags &= ~IDE_DFLAG_DMA_PIO_RETRY;
Bartlomiej Zolnierkiewicz4a546e02008-01-26 20:13:01 +010084 ide_dma_on(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 }
86
Bartlomiej Zolnierkiewicz3c8a2cc2008-12-29 20:27:31 +010087 if (!blk_end_request(rq, error, nr_bytes))
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 ret = 0;
Bartlomiej Zolnierkiewicza72b2142008-12-29 20:27:30 +010089
90 if (ret == 0 && dequeue)
91 drive->hwif->hwgroup->rq = NULL;
Jens Axboe8672d572006-01-09 16:03:35 +010092
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 return ret;
94}
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
96/**
97 * ide_end_request - complete an IDE I/O
98 * @drive: IDE device for the I/O
99 * @uptodate:
100 * @nr_sectors: number of sectors completed
101 *
102 * This is our end_request wrapper function. We complete the I/O
103 * update random number input and dequeue the request, which if
104 * it was tagged may be out of order.
105 */
106
107int ide_end_request (ide_drive_t *drive, int uptodate, int nr_sectors)
108{
Jens Axboe41e9d342007-07-19 08:13:01 +0200109 unsigned int nr_bytes = nr_sectors << 9;
Bartlomiej Zolnierkiewicz1d0bf582008-12-29 20:27:30 +0100110 struct request *rq = drive->hwif->hwgroup->rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
Jens Axboe41e9d342007-07-19 08:13:01 +0200112 if (!nr_bytes) {
113 if (blk_pc_request(rq))
114 nr_bytes = rq->data_len;
115 else
116 nr_bytes = rq->hard_cur_sectors << 9;
117 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
Bartlomiej Zolnierkiewicza72b2142008-12-29 20:27:30 +0100119 return __ide_end_request(drive, rq, uptodate, nr_bytes, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120}
121EXPORT_SYMBOL(ide_end_request);
122
Bartlomiej Zolnierkiewicz6b7d8fc2008-12-02 20:40:03 +0100123static void ide_complete_power_step(ide_drive_t *drive, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124{
Jens Axboec00895a2006-09-30 20:29:12 +0200125 struct request_pm_state *pm = rq->data;
Jens Axboead3cadd2006-06-13 08:46:57 +0200126
Bartlomiej Zolnierkiewicz6b7d8fc2008-12-02 20:40:03 +0100127#ifdef DEBUG_PM
128 printk(KERN_INFO "%s: complete_power_step(step: %d)\n",
129 drive->name, pm->pm_step);
130#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 if (drive->media != ide_disk)
132 return;
133
Jens Axboead3cadd2006-06-13 08:46:57 +0200134 switch (pm->pm_step) {
Bartlomiej Zolnierkiewicz0d346ba2008-10-13 21:39:38 +0200135 case IDE_PM_FLUSH_CACHE: /* Suspend step 1 (flush cache) */
Jens Axboead3cadd2006-06-13 08:46:57 +0200136 if (pm->pm_state == PM_EVENT_FREEZE)
Bartlomiej Zolnierkiewicz0d346ba2008-10-13 21:39:38 +0200137 pm->pm_step = IDE_PM_COMPLETED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 else
Bartlomiej Zolnierkiewicz0d346ba2008-10-13 21:39:38 +0200139 pm->pm_step = IDE_PM_STANDBY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 break;
Bartlomiej Zolnierkiewicz0d346ba2008-10-13 21:39:38 +0200141 case IDE_PM_STANDBY: /* Suspend step 2 (standby) */
142 pm->pm_step = IDE_PM_COMPLETED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 break;
Bartlomiej Zolnierkiewicz0d346ba2008-10-13 21:39:38 +0200144 case IDE_PM_RESTORE_PIO: /* Resume step 1 (restore PIO) */
145 pm->pm_step = IDE_PM_IDLE;
Jason Lunz8c2c0112006-10-03 01:14:26 -0700146 break;
Bartlomiej Zolnierkiewicz0d346ba2008-10-13 21:39:38 +0200147 case IDE_PM_IDLE: /* Resume step 2 (idle)*/
148 pm->pm_step = IDE_PM_RESTORE_DMA;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 break;
150 }
151}
152
153static ide_startstop_t ide_start_power_step(ide_drive_t *drive, struct request *rq)
154{
Jens Axboec00895a2006-09-30 20:29:12 +0200155 struct request_pm_state *pm = rq->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 ide_task_t *args = rq->special;
157
158 memset(args, 0, sizeof(*args));
159
Jens Axboead3cadd2006-06-13 08:46:57 +0200160 switch (pm->pm_step) {
Bartlomiej Zolnierkiewicz0d346ba2008-10-13 21:39:38 +0200161 case IDE_PM_FLUSH_CACHE: /* Suspend step 1 (flush cache) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 if (drive->media != ide_disk)
163 break;
164 /* Not supported? Switch to next step now. */
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200165 if (ata_id_flush_enabled(drive->id) == 0 ||
166 (drive->dev_flags & IDE_DFLAG_WCACHE) == 0) {
Bartlomiej Zolnierkiewicz6b7d8fc2008-12-02 20:40:03 +0100167 ide_complete_power_step(drive, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 return ide_stopped;
169 }
Bartlomiej Zolnierkiewiczff2779b2008-10-10 22:39:31 +0200170 if (ata_id_flush_ext_enabled(drive->id))
Bartlomiej Zolnierkiewiczaaaade32008-10-10 22:39:21 +0200171 args->tf.command = ATA_CMD_FLUSH_EXT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 else
Bartlomiej Zolnierkiewiczaaaade32008-10-10 22:39:21 +0200173 args->tf.command = ATA_CMD_FLUSH;
Bartlomiej Zolnierkiewicz74095a92008-01-25 22:17:07 +0100174 goto out_do_tf;
Bartlomiej Zolnierkiewicz0d346ba2008-10-13 21:39:38 +0200175 case IDE_PM_STANDBY: /* Suspend step 2 (standby) */
Bartlomiej Zolnierkiewiczaaaade32008-10-10 22:39:21 +0200176 args->tf.command = ATA_CMD_STANDBYNOW1;
Bartlomiej Zolnierkiewicz74095a92008-01-25 22:17:07 +0100177 goto out_do_tf;
Bartlomiej Zolnierkiewicz0d346ba2008-10-13 21:39:38 +0200178 case IDE_PM_RESTORE_PIO: /* Resume step 1 (restore PIO) */
Bartlomiej Zolnierkiewicz26bcb872007-10-11 23:54:00 +0200179 ide_set_max_pio(drive);
Bartlomiej Zolnierkiewicz317a46a2007-05-10 00:01:11 +0200180 /*
Bartlomiej Zolnierkiewicz0d346ba2008-10-13 21:39:38 +0200181 * skip IDE_PM_IDLE for ATAPI devices
Bartlomiej Zolnierkiewicz317a46a2007-05-10 00:01:11 +0200182 */
183 if (drive->media != ide_disk)
Bartlomiej Zolnierkiewicz0d346ba2008-10-13 21:39:38 +0200184 pm->pm_step = IDE_PM_RESTORE_DMA;
Bartlomiej Zolnierkiewicz317a46a2007-05-10 00:01:11 +0200185 else
Bartlomiej Zolnierkiewicz6b7d8fc2008-12-02 20:40:03 +0100186 ide_complete_power_step(drive, rq);
Jason Lunz8c2c0112006-10-03 01:14:26 -0700187 return ide_stopped;
Bartlomiej Zolnierkiewicz0d346ba2008-10-13 21:39:38 +0200188 case IDE_PM_IDLE: /* Resume step 2 (idle) */
Bartlomiej Zolnierkiewiczaaaade32008-10-10 22:39:21 +0200189 args->tf.command = ATA_CMD_IDLEIMMEDIATE;
Bartlomiej Zolnierkiewicz74095a92008-01-25 22:17:07 +0100190 goto out_do_tf;
Bartlomiej Zolnierkiewicz0d346ba2008-10-13 21:39:38 +0200191 case IDE_PM_RESTORE_DMA: /* Resume step 3 (restore DMA) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 /*
Bartlomiej Zolnierkiewicz0ae2e172007-10-16 22:29:55 +0200193 * Right now, all we do is call ide_set_dma(drive),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 * we could be smarter and check for current xfer_speed
195 * in struct drive etc...
196 */
Bartlomiej Zolnierkiewicz5e37bdc2008-04-26 22:25:24 +0200197 if (drive->hwif->dma_ops == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 break;
Bartlomiej Zolnierkiewicz1a659882008-12-08 17:42:35 +0100199 /*
200 * TODO: respect IDE_DFLAG_USING_DMA
201 */
202 ide_set_dma(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 break;
204 }
Bartlomiej Zolnierkiewicz0d346ba2008-10-13 21:39:38 +0200205
206 pm->pm_step = IDE_PM_COMPLETED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 return ide_stopped;
Bartlomiej Zolnierkiewicz74095a92008-01-25 22:17:07 +0100208
209out_do_tf:
Bartlomiej Zolnierkiewicz657cc1a2008-01-26 20:13:10 +0100210 args->tf_flags = IDE_TFLAG_TF | IDE_TFLAG_DEVICE;
Bartlomiej Zolnierkiewiczac026ff2008-01-25 22:17:14 +0100211 args->data_phase = TASKFILE_NO_DATA;
Bartlomiej Zolnierkiewicz74095a92008-01-25 22:17:07 +0100212 return do_rw_taskfile(drive, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213}
214
215/**
Alan Coxdbe217a2006-06-25 05:47:44 -0700216 * ide_end_dequeued_request - complete an IDE I/O
217 * @drive: IDE device for the I/O
218 * @uptodate:
219 * @nr_sectors: number of sectors completed
220 *
221 * Complete an I/O that is no longer on the request queue. This
222 * typically occurs when we pull the request and issue a REQUEST_SENSE.
223 * We must still finish the old request but we must not tamper with the
224 * queue in the meantime.
225 *
226 * NOTE: This path does not handle barrier, but barrier is not supported
227 * on ide-cd anyway.
228 */
229
230int ide_end_dequeued_request(ide_drive_t *drive, struct request *rq,
231 int uptodate, int nr_sectors)
232{
Jens Axboe4aff5e22006-08-10 08:44:47 +0200233 BUG_ON(!blk_rq_started(rq));
Bartlomiej Zolnierkiewicz1d0bf582008-12-29 20:27:30 +0100234
Bartlomiej Zolnierkiewicza72b2142008-12-29 20:27:30 +0100235 return __ide_end_request(drive, rq, uptodate, nr_sectors << 9, 0);
Alan Coxdbe217a2006-06-25 05:47:44 -0700236}
237EXPORT_SYMBOL_GPL(ide_end_dequeued_request);
238
239
240/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 * ide_complete_pm_request - end the current Power Management request
242 * @drive: target drive
243 * @rq: request
244 *
245 * This function cleans up the current PM request and stops the queue
246 * if necessary.
247 */
248static void ide_complete_pm_request (ide_drive_t *drive, struct request *rq)
249{
250 unsigned long flags;
251
252#ifdef DEBUG_PM
253 printk("%s: completing PM request, %s\n", drive->name,
254 blk_pm_suspend_request(rq) ? "suspend" : "resume");
255#endif
256 spin_lock_irqsave(&ide_lock, flags);
257 if (blk_pm_suspend_request(rq)) {
258 blk_stop_queue(drive->queue);
259 } else {
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200260 drive->dev_flags &= ~IDE_DFLAG_BLOCKED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 blk_start_queue(drive->queue);
262 }
Bartlomiej Zolnierkiewicz1d0bf582008-12-29 20:27:30 +0100263 spin_unlock_irqrestore(&ide_lock, flags);
264
265 drive->hwif->hwgroup->rq = NULL;
266
Bartlomiej Zolnierkiewicz3c8a2cc2008-12-29 20:27:31 +0100267 if (blk_end_request(rq, 0, 0))
Kiyoshi Ueda5e36bb62008-01-28 10:34:20 +0100268 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269}
270
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271/**
272 * ide_end_drive_cmd - end an explicit drive command
273 * @drive: command
274 * @stat: status bits
275 * @err: error bits
276 *
277 * Clean up after success/failure of an explicit drive command.
278 * These get thrown onto the queue so they are synchronized with
279 * real I/O operations on the drive.
280 *
281 * In LBA48 mode we have to read the register set twice to get
282 * all the extra information out.
283 */
284
285void ide_end_drive_cmd (ide_drive_t *drive, u8 stat, u8 err)
286{
Bartlomiej Zolnierkiewicz1d0bf582008-12-29 20:27:30 +0100287 ide_hwgroup_t *hwgroup = drive->hwif->hwgroup;
288 struct request *rq = hwgroup->rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289
Bartlomiej Zolnierkiewicz7267c332008-01-26 20:13:13 +0100290 if (rq->cmd_type == REQ_TYPE_ATA_TASKFILE) {
Bartlomiej Zolnierkiewicz395d8ef2008-02-11 00:32:14 +0100291 ide_task_t *task = (ide_task_t *)rq->special;
292
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 if (rq->errors == 0)
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +0200294 rq->errors = !OK_STAT(stat, ATA_DRDY, BAD_STAT);
Bartlomiej Zolnierkiewicz395d8ef2008-02-11 00:32:14 +0100295
296 if (task) {
297 struct ide_taskfile *tf = &task->tf;
Bartlomiej Zolnierkiewicz650d8412008-01-25 22:17:06 +0100298
Bartlomiej Zolnierkiewicz650d8412008-01-25 22:17:06 +0100299 tf->error = err;
Bartlomiej Zolnierkiewicz650d8412008-01-25 22:17:06 +0100300 tf->status = stat;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200302 drive->hwif->tp_ops->tf_read(drive, task);
Bartlomiej Zolnierkiewicz395d8ef2008-02-11 00:32:14 +0100303
304 if (task->tf_flags & IDE_TFLAG_DYN)
305 kfree(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 }
307 } else if (blk_pm_request(rq)) {
Jens Axboec00895a2006-09-30 20:29:12 +0200308 struct request_pm_state *pm = rq->data;
Bartlomiej Zolnierkiewicz6b7d8fc2008-12-02 20:40:03 +0100309
310 ide_complete_power_step(drive, rq);
Bartlomiej Zolnierkiewicz0d346ba2008-10-13 21:39:38 +0200311 if (pm->pm_step == IDE_PM_COMPLETED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 ide_complete_pm_request(drive, rq);
313 return;
314 }
315
Bartlomiej Zolnierkiewicz1d0bf582008-12-29 20:27:30 +0100316 hwgroup->rq = NULL;
317
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 rq->errors = err;
Bartlomiej Zolnierkiewicz1d0bf582008-12-29 20:27:30 +0100319
Bartlomiej Zolnierkiewicz3c8a2cc2008-12-29 20:27:31 +0100320 if (unlikely(blk_end_request(rq, (rq->errors ? -EIO : 0),
321 blk_rq_bytes(rq))))
Kiyoshi Ueda5e36bb62008-01-28 10:34:20 +0100322 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324EXPORT_SYMBOL(ide_end_drive_cmd);
325
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326static void ide_kill_rq(ide_drive_t *drive, struct request *rq)
327{
328 if (rq->rq_disk) {
329 ide_driver_t *drv;
330
331 drv = *(ide_driver_t **)rq->rq_disk->private_data;
332 drv->end_request(drive, 0, 0);
333 } else
334 ide_end_request(drive, 0, 0);
335}
336
337static ide_startstop_t ide_ata_error(ide_drive_t *drive, struct request *rq, u8 stat, u8 err)
338{
339 ide_hwif_t *hwif = drive->hwif;
340
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200341 if ((stat & ATA_BUSY) ||
342 ((stat & ATA_DF) && (drive->dev_flags & IDE_DFLAG_NOWERR) == 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 /* other bits are useless when BUSY */
344 rq->errors |= ERROR_RESET;
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +0200345 } else if (stat & ATA_ERR) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 /* err has different meaning on cdrom and tape */
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +0200347 if (err == ATA_ABORTED) {
Bartlomiej Zolnierkiewiczd1d76712008-10-13 21:39:38 +0200348 if ((drive->dev_flags & IDE_DFLAG_LBA) &&
Bartlomiej Zolnierkiewiczaaaade32008-10-10 22:39:21 +0200349 /* some newer drives don't support ATA_CMD_INIT_DEV_PARAMS */
350 hwif->tp_ops->read_status(hwif) == ATA_CMD_INIT_DEV_PARAMS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 return ide_stopped;
352 } else if ((err & BAD_CRC) == BAD_CRC) {
353 /* UDMA crc error, just retry the operation */
354 drive->crc_count++;
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +0200355 } else if (err & (ATA_BBK | ATA_UNC)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 /* retries won't help these */
357 rq->errors = ERROR_MAX;
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +0200358 } else if (err & ATA_TRK0NF) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 /* help it find track zero */
360 rq->errors |= ERROR_RECAL;
361 }
362 }
363
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +0200364 if ((stat & ATA_DRQ) && rq_data_dir(rq) == READ &&
Bartlomiej Zolnierkiewicz57279a72008-07-15 21:21:47 +0200365 (hwif->host_flags & IDE_HFLAG_ERROR_STOPS_FIFO) == 0) {
366 int nsect = drive->mult_count ? drive->mult_count : 1;
367
368 ide_pad_transfer(drive, READ, nsect * SECTOR_SIZE);
369 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
Suleiman Souhlal513daad2007-03-26 23:03:20 +0200371 if (rq->errors >= ERROR_MAX || blk_noretry_request(rq)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 ide_kill_rq(drive, rq);
Suleiman Souhlal513daad2007-03-26 23:03:20 +0200373 return ide_stopped;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 }
Suleiman Souhlal513daad2007-03-26 23:03:20 +0200375
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +0200376 if (hwif->tp_ops->read_status(hwif) & (ATA_BUSY | ATA_DRQ))
Suleiman Souhlal513daad2007-03-26 23:03:20 +0200377 rq->errors |= ERROR_RESET;
378
379 if ((rq->errors & ERROR_RESET) == ERROR_RESET) {
380 ++rq->errors;
381 return ide_do_reset(drive);
382 }
383
384 if ((rq->errors & ERROR_RECAL) == ERROR_RECAL)
385 drive->special.b.recalibrate = 1;
386
387 ++rq->errors;
388
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 return ide_stopped;
390}
391
392static ide_startstop_t ide_atapi_error(ide_drive_t *drive, struct request *rq, u8 stat, u8 err)
393{
394 ide_hwif_t *hwif = drive->hwif;
395
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200396 if ((stat & ATA_BUSY) ||
397 ((stat & ATA_DF) && (drive->dev_flags & IDE_DFLAG_NOWERR) == 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 /* other bits are useless when BUSY */
399 rq->errors |= ERROR_RESET;
400 } else {
401 /* add decoding error stuff */
402 }
403
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +0200404 if (hwif->tp_ops->read_status(hwif) & (ATA_BUSY | ATA_DRQ))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 /* force an abort */
Bartlomiej Zolnierkiewiczaaaade32008-10-10 22:39:21 +0200406 hwif->tp_ops->exec_command(hwif, ATA_CMD_IDLEIMMEDIATE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407
408 if (rq->errors >= ERROR_MAX) {
409 ide_kill_rq(drive, rq);
410 } else {
411 if ((rq->errors & ERROR_RESET) == ERROR_RESET) {
412 ++rq->errors;
413 return ide_do_reset(drive);
414 }
415 ++rq->errors;
416 }
417
418 return ide_stopped;
419}
420
421ide_startstop_t
422__ide_error(ide_drive_t *drive, struct request *rq, u8 stat, u8 err)
423{
424 if (drive->media == ide_disk)
425 return ide_ata_error(drive, rq, stat, err);
426 return ide_atapi_error(drive, rq, stat, err);
427}
428
429EXPORT_SYMBOL_GPL(__ide_error);
430
431/**
432 * ide_error - handle an error on the IDE
433 * @drive: drive the error occurred on
434 * @msg: message to report
435 * @stat: status bits
436 *
437 * ide_error() takes action based on the error returned by the drive.
438 * For normal I/O that may well include retries. We deal with
439 * both new-style (taskfile) and old style command handling here.
440 * In the case of taskfile command handling there is work left to
441 * do
442 */
443
444ide_startstop_t ide_error (ide_drive_t *drive, const char *msg, u8 stat)
445{
446 struct request *rq;
447 u8 err;
448
449 err = ide_dump_status(drive, msg, stat);
450
451 if ((rq = HWGROUP(drive)->rq) == NULL)
452 return ide_stopped;
453
454 /* retry only "normal" I/O: */
Jens Axboe4aff5e22006-08-10 08:44:47 +0200455 if (!blk_fs_request(rq)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 rq->errors = 1;
457 ide_end_drive_cmd(drive, stat, err);
458 return ide_stopped;
459 }
460
461 if (rq->rq_disk) {
462 ide_driver_t *drv;
463
464 drv = *(ide_driver_t **)rq->rq_disk->private_data;
465 return drv->error(drive, rq, stat, err);
466 } else
467 return __ide_error(drive, rq, stat, err);
468}
469
470EXPORT_SYMBOL_GPL(ide_error);
471
Bartlomiej Zolnierkiewicz57d73662008-01-25 22:17:16 +0100472static void ide_tf_set_specify_cmd(ide_drive_t *drive, struct ide_taskfile *tf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473{
Bartlomiej Zolnierkiewicz57d73662008-01-25 22:17:16 +0100474 tf->nsect = drive->sect;
475 tf->lbal = drive->sect;
476 tf->lbam = drive->cyl;
477 tf->lbah = drive->cyl >> 8;
Bartlomiej Zolnierkiewicz7f612f22008-10-13 21:39:40 +0200478 tf->device = (drive->head - 1) | drive->select;
Bartlomiej Zolnierkiewiczaaaade32008-10-10 22:39:21 +0200479 tf->command = ATA_CMD_INIT_DEV_PARAMS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480}
481
Bartlomiej Zolnierkiewicz57d73662008-01-25 22:17:16 +0100482static void ide_tf_set_restore_cmd(ide_drive_t *drive, struct ide_taskfile *tf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483{
Bartlomiej Zolnierkiewicz57d73662008-01-25 22:17:16 +0100484 tf->nsect = drive->sect;
Bartlomiej Zolnierkiewiczaaaade32008-10-10 22:39:21 +0200485 tf->command = ATA_CMD_RESTORE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486}
487
Bartlomiej Zolnierkiewicz57d73662008-01-25 22:17:16 +0100488static void ide_tf_set_setmult_cmd(ide_drive_t *drive, struct ide_taskfile *tf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489{
Bartlomiej Zolnierkiewicz57d73662008-01-25 22:17:16 +0100490 tf->nsect = drive->mult_req;
Bartlomiej Zolnierkiewiczaaaade32008-10-10 22:39:21 +0200491 tf->command = ATA_CMD_SET_MULTI;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492}
493
494static ide_startstop_t ide_disk_special(ide_drive_t *drive)
495{
496 special_t *s = &drive->special;
497 ide_task_t args;
498
499 memset(&args, 0, sizeof(ide_task_t));
Bartlomiej Zolnierkiewiczac026ff2008-01-25 22:17:14 +0100500 args.data_phase = TASKFILE_NO_DATA;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501
502 if (s->b.set_geometry) {
503 s->b.set_geometry = 0;
Bartlomiej Zolnierkiewicz57d73662008-01-25 22:17:16 +0100504 ide_tf_set_specify_cmd(drive, &args.tf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 } else if (s->b.recalibrate) {
506 s->b.recalibrate = 0;
Bartlomiej Zolnierkiewicz57d73662008-01-25 22:17:16 +0100507 ide_tf_set_restore_cmd(drive, &args.tf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 } else if (s->b.set_multmode) {
509 s->b.set_multmode = 0;
Bartlomiej Zolnierkiewicz57d73662008-01-25 22:17:16 +0100510 ide_tf_set_setmult_cmd(drive, &args.tf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 } else if (s->all) {
512 int special = s->all;
513 s->all = 0;
514 printk(KERN_ERR "%s: bad special flag: 0x%02x\n", drive->name, special);
515 return ide_stopped;
516 }
517
Bartlomiej Zolnierkiewicz657cc1a2008-01-26 20:13:10 +0100518 args.tf_flags = IDE_TFLAG_TF | IDE_TFLAG_DEVICE |
Bartlomiej Zolnierkiewicz57d73662008-01-25 22:17:16 +0100519 IDE_TFLAG_CUSTOM_HANDLER;
Bartlomiej Zolnierkiewicz74095a92008-01-25 22:17:07 +0100520
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 do_rw_taskfile(drive, &args);
522
523 return ide_started;
524}
525
526/**
527 * do_special - issue some special commands
528 * @drive: drive the command is for
529 *
Bartlomiej Zolnierkiewiczaaaade32008-10-10 22:39:21 +0200530 * do_special() is used to issue ATA_CMD_INIT_DEV_PARAMS,
531 * ATA_CMD_RESTORE and ATA_CMD_SET_MULTI commands to a drive.
532 *
533 * It used to do much more, but has been scaled back.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 */
535
536static ide_startstop_t do_special (ide_drive_t *drive)
537{
538 special_t *s = &drive->special;
539
540#ifdef DEBUG
541 printk("%s: do_special: 0x%02x\n", drive->name, s->all);
542#endif
Bartlomiej Zolnierkiewicz6982daf2008-10-13 21:39:40 +0200543 if (drive->media == ide_disk)
544 return ide_disk_special(drive);
Bartlomiej Zolnierkiewicz26bcb872007-10-11 23:54:00 +0200545
Bartlomiej Zolnierkiewicz6982daf2008-10-13 21:39:40 +0200546 s->all = 0;
547 drive->mult_req = 0;
548 return ide_stopped;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549}
550
551void ide_map_sg(ide_drive_t *drive, struct request *rq)
552{
553 ide_hwif_t *hwif = drive->hwif;
554 struct scatterlist *sg = hwif->sg_table;
555
556 if (hwif->sg_mapped) /* needed by ide-scsi */
557 return;
558
Jens Axboe4aff5e22006-08-10 08:44:47 +0200559 if (rq->cmd_type != REQ_TYPE_ATA_TASKFILE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 hwif->sg_nents = blk_rq_map_sg(drive->queue, rq, sg);
561 } else {
562 sg_init_one(sg, rq->buffer, rq->nr_sectors * SECTOR_SIZE);
563 hwif->sg_nents = 1;
564 }
565}
566
567EXPORT_SYMBOL_GPL(ide_map_sg);
568
569void ide_init_sg_cmd(ide_drive_t *drive, struct request *rq)
570{
571 ide_hwif_t *hwif = drive->hwif;
572
573 hwif->nsect = hwif->nleft = rq->nr_sectors;
Jens Axboe55c16a72007-07-25 08:13:56 +0200574 hwif->cursg_ofs = 0;
575 hwif->cursg = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576}
577
578EXPORT_SYMBOL_GPL(ide_init_sg_cmd);
579
580/**
581 * execute_drive_command - issue special drive command
Adrian Bunk338cec32005-09-10 00:26:54 -0700582 * @drive: the drive to issue the command on
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 * @rq: the request structure holding the command
584 *
585 * execute_drive_cmd() issues a special drive command, usually
586 * initiated by ioctl() from the external hdparm program. The
587 * command can be a drive command, drive task or taskfile
588 * operation. Weirdly you can call it with NULL to wait for
589 * all commands to finish. Don't do this as that is due to change
590 */
591
592static ide_startstop_t execute_drive_cmd (ide_drive_t *drive,
593 struct request *rq)
594{
595 ide_hwif_t *hwif = HWIF(drive);
Bartlomiej Zolnierkiewicz7267c332008-01-26 20:13:13 +0100596 ide_task_t *task = rq->special;
Bartlomiej Zolnierkiewicz21d535c2008-01-25 22:17:09 +0100597
Bartlomiej Zolnierkiewicz7267c332008-01-26 20:13:13 +0100598 if (task) {
Bartlomiej Zolnierkiewicz21d535c2008-01-25 22:17:09 +0100599 hwif->data_phase = task->data_phase;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600
601 switch (hwif->data_phase) {
602 case TASKFILE_MULTI_OUT:
603 case TASKFILE_OUT:
604 case TASKFILE_MULTI_IN:
605 case TASKFILE_IN:
606 ide_init_sg_cmd(drive, rq);
607 ide_map_sg(drive, rq);
608 default:
609 break;
610 }
611
Bartlomiej Zolnierkiewicz21d535c2008-01-25 22:17:09 +0100612 return do_rw_taskfile(drive, task);
613 }
614
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 /*
616 * NULL is actually a valid way of waiting for
617 * all current requests to be flushed from the queue.
618 */
619#ifdef DEBUG
620 printk("%s: DRIVE_CMD (null)\n", drive->name);
621#endif
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200622 ide_end_drive_cmd(drive, hwif->tp_ops->read_status(hwif),
Bartlomiej Zolnierkiewiczb73c7ee2008-07-23 19:55:52 +0200623 ide_read_error(drive));
Bartlomiej Zolnierkiewicz64a57fe2008-02-06 02:57:51 +0100624
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 return ide_stopped;
626}
627
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +0200628int ide_devset_execute(ide_drive_t *drive, const struct ide_devset *setting,
629 int arg)
630{
631 struct request_queue *q = drive->queue;
632 struct request *rq;
633 int ret = 0;
634
635 if (!(setting->flags & DS_SYNC))
636 return setting->set(drive, arg);
637
Elias Oltmannse415e492008-10-13 21:39:45 +0200638 rq = blk_get_request(q, READ, __GFP_WAIT);
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +0200639 rq->cmd_type = REQ_TYPE_SPECIAL;
640 rq->cmd_len = 5;
641 rq->cmd[0] = REQ_DEVSET_EXEC;
642 *(int *)&rq->cmd[1] = arg;
643 rq->special = setting->set;
644
645 if (blk_execute_rq(q, NULL, rq, 0))
646 ret = rq->errors;
647 blk_put_request(rq);
648
649 return ret;
650}
651EXPORT_SYMBOL_GPL(ide_devset_execute);
652
Elias Oltmanns79e36a92008-07-16 20:33:48 +0200653static ide_startstop_t ide_special_rq(ide_drive_t *drive, struct request *rq)
654{
Elias Oltmanns4abdc6e2008-10-13 21:39:50 +0200655 u8 cmd = rq->cmd[0];
656
657 if (cmd == REQ_PARK_HEADS || cmd == REQ_UNPARK_HEADS) {
658 ide_task_t task;
659 struct ide_taskfile *tf = &task.tf;
660
661 memset(&task, 0, sizeof(task));
662 if (cmd == REQ_PARK_HEADS) {
663 drive->sleep = *(unsigned long *)rq->special;
664 drive->dev_flags |= IDE_DFLAG_SLEEPING;
665 tf->command = ATA_CMD_IDLEIMMEDIATE;
666 tf->feature = 0x44;
667 tf->lbal = 0x4c;
668 tf->lbam = 0x4e;
669 tf->lbah = 0x55;
670 task.tf_flags |= IDE_TFLAG_CUSTOM_HANDLER;
671 } else /* cmd == REQ_UNPARK_HEADS */
672 tf->command = ATA_CMD_CHK_POWER;
673
674 task.tf_flags |= IDE_TFLAG_TF | IDE_TFLAG_DEVICE;
675 task.rq = rq;
676 drive->hwif->data_phase = task.data_phase = TASKFILE_NO_DATA;
677 return do_rw_taskfile(drive, &task);
678 }
679
680 switch (cmd) {
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +0200681 case REQ_DEVSET_EXEC:
682 {
683 int err, (*setfunc)(ide_drive_t *, int) = rq->special;
684
685 err = setfunc(drive, *(int *)&rq->cmd[1]);
686 if (err)
687 rq->errors = err;
688 else
689 err = 1;
690 ide_end_request(drive, err, 0);
691 return ide_stopped;
692 }
Elias Oltmanns79e36a92008-07-16 20:33:48 +0200693 case REQ_DRIVE_RESET:
694 return ide_do_reset(drive);
695 default:
696 blk_dump_rq_flags(rq, "ide_special_rq - bad request");
697 ide_end_request(drive, 0, 0);
698 return ide_stopped;
699 }
700}
701
Jens Axboead3cadd2006-06-13 08:46:57 +0200702static void ide_check_pm_state(ide_drive_t *drive, struct request *rq)
703{
Jens Axboec00895a2006-09-30 20:29:12 +0200704 struct request_pm_state *pm = rq->data;
Jens Axboead3cadd2006-06-13 08:46:57 +0200705
706 if (blk_pm_suspend_request(rq) &&
Bartlomiej Zolnierkiewicz0d346ba2008-10-13 21:39:38 +0200707 pm->pm_step == IDE_PM_START_SUSPEND)
Jens Axboead3cadd2006-06-13 08:46:57 +0200708 /* Mark drive blocked when starting the suspend sequence. */
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200709 drive->dev_flags |= IDE_DFLAG_BLOCKED;
Jens Axboead3cadd2006-06-13 08:46:57 +0200710 else if (blk_pm_resume_request(rq) &&
Bartlomiej Zolnierkiewicz0d346ba2008-10-13 21:39:38 +0200711 pm->pm_step == IDE_PM_START_RESUME) {
Jens Axboead3cadd2006-06-13 08:46:57 +0200712 /*
713 * The first thing we do on wakeup is to wait for BSY bit to
714 * go away (with a looong timeout) as a drive on this hwif may
715 * just be POSTing itself.
716 * We do that before even selecting as the "other" device on
717 * the bus may be broken enough to walk on our toes at this
718 * point.
719 */
Bartlomiej Zolnierkiewicz6e6afb32008-07-23 19:55:52 +0200720 ide_hwif_t *hwif = drive->hwif;
Jens Axboead3cadd2006-06-13 08:46:57 +0200721 int rc;
722#ifdef DEBUG_PM
723 printk("%s: Wakeup request inited, waiting for !BSY...\n", drive->name);
724#endif
Bartlomiej Zolnierkiewicz6e6afb32008-07-23 19:55:52 +0200725 rc = ide_wait_not_busy(hwif, 35000);
Jens Axboead3cadd2006-06-13 08:46:57 +0200726 if (rc)
727 printk(KERN_WARNING "%s: bus not ready on wakeup\n", drive->name);
728 SELECT_DRIVE(drive);
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200729 hwif->tp_ops->set_irq(hwif, 1);
Bartlomiej Zolnierkiewicz6e6afb32008-07-23 19:55:52 +0200730 rc = ide_wait_not_busy(hwif, 100000);
Jens Axboead3cadd2006-06-13 08:46:57 +0200731 if (rc)
732 printk(KERN_WARNING "%s: drive not ready on wakeup\n", drive->name);
733 }
734}
735
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736/**
737 * start_request - start of I/O and command issuing for IDE
738 *
739 * start_request() initiates handling of a new I/O request. It
Bartlomiej Zolnierkiewicz3c619ff2008-10-10 22:39:22 +0200740 * accepts commands and I/O (read/write) requests.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 *
742 * FIXME: this function needs a rename
743 */
744
745static ide_startstop_t start_request (ide_drive_t *drive, struct request *rq)
746{
747 ide_startstop_t startstop;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748
Jens Axboe4aff5e22006-08-10 08:44:47 +0200749 BUG_ON(!blk_rq_started(rq));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750
751#ifdef DEBUG
752 printk("%s: start_request: current=0x%08lx\n",
753 HWIF(drive)->name, (unsigned long) rq);
754#endif
755
756 /* bail early if we've exceeded max_failures */
757 if (drive->max_failures && (drive->failures > drive->max_failures)) {
Aristeu Rozanskib5e1a4e2008-01-25 22:17:04 +0100758 rq->cmd_flags |= REQ_FAILED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 goto kill_rq;
760 }
761
Jens Axboead3cadd2006-06-13 08:46:57 +0200762 if (blk_pm_request(rq))
763 ide_check_pm_state(drive, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764
765 SELECT_DRIVE(drive);
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +0200766 if (ide_wait_stat(&startstop, drive, drive->ready_stat,
767 ATA_BUSY | ATA_DRQ, WAIT_READY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 printk(KERN_ERR "%s: drive not ready for command\n", drive->name);
769 return startstop;
770 }
771 if (!drive->special.all) {
772 ide_driver_t *drv;
773
Suleiman Souhlal513daad2007-03-26 23:03:20 +0200774 /*
775 * We reset the drive so we need to issue a SETFEATURES.
776 * Do it _after_ do_special() restored device parameters.
777 */
778 if (drive->current_speed == 0xff)
779 ide_config_drive_speed(drive, drive->desired_speed);
780
Bartlomiej Zolnierkiewicz7267c332008-01-26 20:13:13 +0100781 if (rq->cmd_type == REQ_TYPE_ATA_TASKFILE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 return execute_drive_cmd(drive, rq);
783 else if (blk_pm_request(rq)) {
Jens Axboec00895a2006-09-30 20:29:12 +0200784 struct request_pm_state *pm = rq->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785#ifdef DEBUG_PM
786 printk("%s: start_power_step(step: %d)\n",
Bartlomiej Zolnierkiewicz6b7d8fc2008-12-02 20:40:03 +0100787 drive->name, pm->pm_step);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788#endif
789 startstop = ide_start_power_step(drive, rq);
790 if (startstop == ide_stopped &&
Bartlomiej Zolnierkiewicz0d346ba2008-10-13 21:39:38 +0200791 pm->pm_step == IDE_PM_COMPLETED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 ide_complete_pm_request(drive, rq);
793 return startstop;
Elias Oltmanns79e36a92008-07-16 20:33:48 +0200794 } else if (!rq->rq_disk && blk_special_request(rq))
795 /*
796 * TODO: Once all ULDs have been modified to
797 * check for specific op codes rather than
798 * blindly accepting any special request, the
799 * check for ->rq_disk above may be replaced
800 * by a more suitable mechanism or even
801 * dropped entirely.
802 */
803 return ide_special_rq(drive, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804
805 drv = *(ide_driver_t **)rq->rq_disk->private_data;
Bartlomiej Zolnierkiewicz3c619ff2008-10-10 22:39:22 +0200806
807 return drv->do_request(drive, rq, rq->sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 }
809 return do_special(drive);
810kill_rq:
811 ide_kill_rq(drive, rq);
812 return ide_stopped;
813}
814
815/**
816 * ide_stall_queue - pause an IDE device
817 * @drive: drive to stall
818 * @timeout: time to stall for (jiffies)
819 *
820 * ide_stall_queue() can be used by a drive to give excess bandwidth back
821 * to the hwgroup by sleeping for timeout jiffies.
822 */
823
824void ide_stall_queue (ide_drive_t *drive, unsigned long timeout)
825{
826 if (timeout > WAIT_WORSTCASE)
827 timeout = WAIT_WORSTCASE;
828 drive->sleep = timeout + jiffies;
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200829 drive->dev_flags |= IDE_DFLAG_SLEEPING;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830}
831
832EXPORT_SYMBOL(ide_stall_queue);
833
834#define WAKEUP(drive) ((drive)->service_start + 2 * (drive)->service_time)
835
836/**
837 * choose_drive - select a drive to service
838 * @hwgroup: hardware group to select on
839 *
840 * choose_drive() selects the next drive which will be serviced.
841 * This is necessary because the IDE layer can't issue commands
842 * to both drives on the same cable, unlike SCSI.
843 */
844
845static inline ide_drive_t *choose_drive (ide_hwgroup_t *hwgroup)
846{
847 ide_drive_t *drive, *best;
848
849repeat:
850 best = NULL;
851 drive = hwgroup->drive;
852
853 /*
854 * drive is doing pre-flush, ordered write, post-flush sequence. even
855 * though that is 3 requests, it must be seen as a single transaction.
856 * we must not preempt this drive until that is complete
857 */
858 if (blk_queue_flushing(drive->queue)) {
859 /*
860 * small race where queue could get replugged during
861 * the 3-request flush cycle, just yank the plug since
862 * we want it to finish asap
863 */
864 blk_remove_plug(drive->queue);
865 return drive;
866 }
867
868 do {
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200869 u8 dev_s = !!(drive->dev_flags & IDE_DFLAG_SLEEPING);
870 u8 best_s = (best && !!(best->dev_flags & IDE_DFLAG_SLEEPING));
871
872 if ((dev_s == 0 || time_after_eq(jiffies, drive->sleep)) &&
873 !elv_queue_empty(drive->queue)) {
874 if (best == NULL ||
875 (dev_s && (best_s == 0 || time_before(drive->sleep, best->sleep))) ||
876 (best_s == 0 && time_before(WAKEUP(drive), WAKEUP(best)))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 if (!blk_queue_plugged(drive->queue))
878 best = drive;
879 }
880 }
881 } while ((drive = drive->next) != hwgroup->drive);
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200882
883 if (best && (best->dev_flags & IDE_DFLAG_NICE1) &&
884 (best->dev_flags & IDE_DFLAG_SLEEPING) == 0 &&
885 best != hwgroup->drive && best->service_time > WAIT_MIN_SLEEP) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 long t = (signed long)(WAKEUP(best) - jiffies);
887 if (t >= WAIT_MIN_SLEEP) {
888 /*
889 * We *may* have some time to spare, but first let's see if
890 * someone can potentially benefit from our nice mood today..
891 */
892 drive = best->next;
893 do {
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200894 if ((drive->dev_flags & IDE_DFLAG_SLEEPING) == 0
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 && time_before(jiffies - best->service_time, WAKEUP(drive))
896 && time_before(WAKEUP(drive), jiffies + t))
897 {
898 ide_stall_queue(best, min_t(long, t, 10 * WAIT_MIN_SLEEP));
899 goto repeat;
900 }
901 } while ((drive = drive->next) != best);
902 }
903 }
904 return best;
905}
906
907/*
908 * Issue a new request to a drive from hwgroup
909 * Caller must have already done spin_lock_irqsave(&ide_lock, ..);
910 *
911 * A hwgroup is a serialized group of IDE interfaces. Usually there is
912 * exactly one hwif (interface) per hwgroup, but buggy controllers (eg. CMD640)
913 * may have both interfaces in a single hwgroup to "serialize" access.
914 * Or possibly multiple ISA interfaces can share a common IRQ by being grouped
915 * together into one hwgroup for serialized access.
916 *
917 * Note also that several hwgroups can end up sharing a single IRQ,
918 * possibly along with many other devices. This is especially common in
919 * PCI-based systems with off-board IDE controller cards.
920 *
921 * The IDE driver uses the single global ide_lock spinlock to protect
922 * access to the request queues, and to protect the hwgroup->busy flag.
923 *
924 * The first thread into the driver for a particular hwgroup sets the
925 * hwgroup->busy flag to indicate that this hwgroup is now active,
926 * and then initiates processing of the top request from the request queue.
927 *
928 * Other threads attempting entry notice the busy setting, and will simply
929 * queue their new requests and exit immediately. Note that hwgroup->busy
930 * remains set even when the driver is merely awaiting the next interrupt.
931 * Thus, the meaning is "this hwgroup is busy processing a request".
932 *
933 * When processing of a request completes, the completing thread or IRQ-handler
934 * will start the next request from the queue. If no more work remains,
935 * the driver will clear the hwgroup->busy flag and exit.
936 *
937 * The ide_lock (spinlock) is used to protect all access to the
938 * hwgroup->busy flag, but is otherwise not needed for most processing in
939 * the driver. This makes the driver much more friendlier to shared IRQs
940 * than previous designs, while remaining 100% (?) SMP safe and capable.
941 */
942static void ide_do_request (ide_hwgroup_t *hwgroup, int masked_irq)
943{
944 ide_drive_t *drive;
945 ide_hwif_t *hwif;
946 struct request *rq;
947 ide_startstop_t startstop;
Benjamin Herrenschmidt867f8b42005-10-09 10:37:47 +1000948 int loops = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 /* caller must own ide_lock */
951 BUG_ON(!irqs_disabled());
952
953 while (!hwgroup->busy) {
954 hwgroup->busy = 1;
Michael Schmitzf9e33262008-12-02 20:40:02 +0100955 /* for atari only */
956 ide_get_lock(ide_intr, hwgroup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 drive = choose_drive(hwgroup);
958 if (drive == NULL) {
959 int sleeping = 0;
960 unsigned long sleep = 0; /* shut up, gcc */
961 hwgroup->rq = NULL;
962 drive = hwgroup->drive;
963 do {
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200964 if ((drive->dev_flags & IDE_DFLAG_SLEEPING) &&
965 (sleeping == 0 ||
966 time_before(drive->sleep, sleep))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 sleeping = 1;
968 sleep = drive->sleep;
969 }
970 } while ((drive = drive->next) != hwgroup->drive);
971 if (sleeping) {
972 /*
973 * Take a short snooze, and then wake up this hwgroup again.
974 * This gives other hwgroups on the same a chance to
975 * play fairly with us, just in case there are big differences
976 * in relative throughputs.. don't want to hog the cpu too much.
977 */
978 if (time_before(sleep, jiffies + WAIT_MIN_SLEEP))
979 sleep = jiffies + WAIT_MIN_SLEEP;
980#if 1
981 if (timer_pending(&hwgroup->timer))
982 printk(KERN_CRIT "ide_set_handler: timer already active\n");
983#endif
984 /* so that ide_timer_expiry knows what to do */
985 hwgroup->sleeping = 1;
Suleiman Souhlal23450312007-04-10 22:38:37 +0200986 hwgroup->req_gen_timer = hwgroup->req_gen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 mod_timer(&hwgroup->timer, sleep);
988 /* we purposely leave hwgroup->busy==1
989 * while sleeping */
990 } else {
991 /* Ugly, but how can we sleep for the lock
992 * otherwise? perhaps from tq_disk?
993 */
994
995 /* for atari only */
996 ide_release_lock();
997 hwgroup->busy = 0;
998 }
999
1000 /* no more work for this hwgroup (for now) */
1001 return;
1002 }
Benjamin Herrenschmidt867f8b42005-10-09 10:37:47 +10001003 again:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 hwif = HWIF(drive);
Bartlomiej Zolnierkiewicz81ca6912008-01-26 20:13:08 +01001005 if (hwgroup->hwif->sharing_irq && hwif != hwgroup->hwif) {
Bartlomiej Zolnierkiewicz7299a392008-01-25 22:17:14 +01001006 /*
1007 * set nIEN for previous hwif, drives in the
1008 * quirk_list may not like intr setups/cleanups
1009 */
1010 if (drive->quirk_list != 1)
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +02001011 hwif->tp_ops->set_irq(hwif, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 }
1013 hwgroup->hwif = hwif;
1014 hwgroup->drive = drive;
Elias Oltmanns4abdc6e2008-10-13 21:39:50 +02001015 drive->dev_flags &= ~(IDE_DFLAG_SLEEPING | IDE_DFLAG_PARKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 drive->service_start = jiffies;
1017
1018 if (blk_queue_plugged(drive->queue)) {
1019 printk(KERN_ERR "ide: huh? queue was plugged!\n");
1020 break;
1021 }
1022
1023 /*
1024 * we know that the queue isn't empty, but this can happen
1025 * if the q->prep_rq_fn() decides to kill a request
1026 */
1027 rq = elv_next_request(drive->queue);
1028 if (!rq) {
1029 hwgroup->busy = 0;
1030 break;
1031 }
1032
1033 /*
1034 * Sanity: don't accept a request that isn't a PM request
1035 * if we are currently power managed. This is very important as
1036 * blk_stop_queue() doesn't prevent the elv_next_request()
1037 * above to return us whatever is in the queue. Since we call
1038 * ide_do_request() ourselves, we end up taking requests while
1039 * the queue is blocked...
1040 *
1041 * We let requests forced at head of queue with ide-preempt
1042 * though. I hope that doesn't happen too much, hopefully not
1043 * unless the subdriver triggers such a thing in its own PM
1044 * state machine.
Benjamin Herrenschmidt867f8b42005-10-09 10:37:47 +10001045 *
1046 * We count how many times we loop here to make sure we service
1047 * all drives in the hwgroup without looping for ever
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 */
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +02001049 if ((drive->dev_flags & IDE_DFLAG_BLOCKED) &&
1050 blk_pm_request(rq) == 0 &&
1051 (rq->cmd_flags & REQ_PREEMPT) == 0) {
Benjamin Herrenschmidt867f8b42005-10-09 10:37:47 +10001052 drive = drive->next ? drive->next : hwgroup->drive;
1053 if (loops++ < 4 && !blk_queue_plugged(drive->queue))
1054 goto again;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 /* We clear busy, there should be no pending ATA command at this point. */
1056 hwgroup->busy = 0;
1057 break;
1058 }
1059
1060 hwgroup->rq = rq;
1061
1062 /*
1063 * Some systems have trouble with IDE IRQs arriving while
1064 * the driver is still setting things up. So, here we disable
1065 * the IRQ used by this interface while the request is being started.
1066 * This may look bad at first, but pretty much the same thing
1067 * happens anyway when any interrupt comes in, IDE or otherwise
1068 * -- the kernel masks the IRQ while it is being handled.
1069 */
1070 if (masked_irq != IDE_NO_IRQ && hwif->irq != masked_irq)
1071 disable_irq_nosync(hwif->irq);
1072 spin_unlock(&ide_lock);
Ingo Molnar366c7f52006-07-03 00:25:25 -07001073 local_irq_enable_in_hardirq();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 /* allow other IRQs while we start this request */
1075 startstop = start_request(drive, rq);
1076 spin_lock_irq(&ide_lock);
1077 if (masked_irq != IDE_NO_IRQ && hwif->irq != masked_irq)
1078 enable_irq(hwif->irq);
1079 if (startstop == ide_stopped)
1080 hwgroup->busy = 0;
1081 }
1082}
1083
1084/*
1085 * Passes the stuff to ide_do_request
1086 */
Jens Axboe165125e2007-07-24 09:28:11 +02001087void do_ide_request(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088{
1089 ide_drive_t *drive = q->queuedata;
1090
1091 ide_do_request(HWGROUP(drive), IDE_NO_IRQ);
1092}
1093
1094/*
1095 * un-busy the hwgroup etc, and clear any pending DMA status. we want to
1096 * retry the current request in pio mode instead of risking tossing it
1097 * all away
1098 */
1099static ide_startstop_t ide_dma_timeout_retry(ide_drive_t *drive, int error)
1100{
1101 ide_hwif_t *hwif = HWIF(drive);
1102 struct request *rq;
1103 ide_startstop_t ret = ide_stopped;
1104
1105 /*
1106 * end current dma transaction
1107 */
1108
1109 if (error < 0) {
1110 printk(KERN_WARNING "%s: DMA timeout error\n", drive->name);
Bartlomiej Zolnierkiewicz5e37bdc2008-04-26 22:25:24 +02001111 (void)hwif->dma_ops->dma_end(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 ret = ide_error(drive, "dma timeout error",
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +02001113 hwif->tp_ops->read_status(hwif));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 } else {
1115 printk(KERN_WARNING "%s: DMA timeout retry\n", drive->name);
Bartlomiej Zolnierkiewicz5e37bdc2008-04-26 22:25:24 +02001116 hwif->dma_ops->dma_timeout(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 }
1118
1119 /*
1120 * disable dma for now, but remember that we did so because of
1121 * a timeout -- we'll reenable after we finish this next request
1122 * (or rather the first chunk of it) in pio.
1123 */
Bartlomiej Zolnierkiewiczc3922042008-10-13 21:39:37 +02001124 drive->dev_flags |= IDE_DFLAG_DMA_PIO_RETRY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 drive->retry_pio++;
Bartlomiej Zolnierkiewicz4a546e02008-01-26 20:13:01 +01001126 ide_dma_off_quietly(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127
1128 /*
1129 * un-busy drive etc (hwgroup->busy is cleared on return) and
1130 * make sure request is sane
1131 */
1132 rq = HWGROUP(drive)->rq;
Hua Zhongce42f192006-10-03 01:14:15 -07001133
1134 if (!rq)
1135 goto out;
1136
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 HWGROUP(drive)->rq = NULL;
1138
1139 rq->errors = 0;
1140
1141 if (!rq->bio)
1142 goto out;
1143
1144 rq->sector = rq->bio->bi_sector;
1145 rq->current_nr_sectors = bio_iovec(rq->bio)->bv_len >> 9;
1146 rq->hard_cur_sectors = rq->current_nr_sectors;
1147 rq->buffer = bio_data(rq->bio);
1148out:
1149 return ret;
1150}
1151
1152/**
1153 * ide_timer_expiry - handle lack of an IDE interrupt
1154 * @data: timer callback magic (hwgroup)
1155 *
1156 * An IDE command has timed out before the expected drive return
1157 * occurred. At this point we attempt to clean up the current
1158 * mess. If the current handler includes an expiry handler then
1159 * we invoke the expiry handler, and providing it is happy the
1160 * work is done. If that fails we apply generic recovery rules
1161 * invoking the handler and checking the drive DMA status. We
1162 * have an excessively incestuous relationship with the DMA
1163 * logic that wants cleaning up.
1164 */
1165
1166void ide_timer_expiry (unsigned long data)
1167{
1168 ide_hwgroup_t *hwgroup = (ide_hwgroup_t *) data;
1169 ide_handler_t *handler;
1170 ide_expiry_t *expiry;
1171 unsigned long flags;
1172 unsigned long wait = -1;
1173
1174 spin_lock_irqsave(&ide_lock, flags);
1175
Suleiman Souhlal23450312007-04-10 22:38:37 +02001176 if (((handler = hwgroup->handler) == NULL) ||
1177 (hwgroup->req_gen != hwgroup->req_gen_timer)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 /*
1179 * Either a marginal timeout occurred
1180 * (got the interrupt just as timer expired),
1181 * or we were "sleeping" to give other devices a chance.
1182 * Either way, we don't really want to complain about anything.
1183 */
1184 if (hwgroup->sleeping) {
1185 hwgroup->sleeping = 0;
1186 hwgroup->busy = 0;
1187 }
1188 } else {
1189 ide_drive_t *drive = hwgroup->drive;
1190 if (!drive) {
1191 printk(KERN_ERR "ide_timer_expiry: hwgroup->drive was NULL\n");
1192 hwgroup->handler = NULL;
1193 } else {
1194 ide_hwif_t *hwif;
1195 ide_startstop_t startstop = ide_stopped;
1196 if (!hwgroup->busy) {
1197 hwgroup->busy = 1; /* paranoia */
1198 printk(KERN_ERR "%s: ide_timer_expiry: hwgroup->busy was 0 ??\n", drive->name);
1199 }
1200 if ((expiry = hwgroup->expiry) != NULL) {
1201 /* continue */
1202 if ((wait = expiry(drive)) > 0) {
1203 /* reset timer */
1204 hwgroup->timer.expires = jiffies + wait;
Suleiman Souhlal23450312007-04-10 22:38:37 +02001205 hwgroup->req_gen_timer = hwgroup->req_gen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 add_timer(&hwgroup->timer);
1207 spin_unlock_irqrestore(&ide_lock, flags);
1208 return;
1209 }
1210 }
1211 hwgroup->handler = NULL;
1212 /*
1213 * We need to simulate a real interrupt when invoking
1214 * the handler() function, which means we need to
1215 * globally mask the specific IRQ:
1216 */
1217 spin_unlock(&ide_lock);
1218 hwif = HWIF(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 /* disable_irq_nosync ?? */
1220 disable_irq(hwif->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 /* local CPU only,
1222 * as if we were handling an interrupt */
1223 local_irq_disable();
1224 if (hwgroup->polling) {
1225 startstop = handler(drive);
1226 } else if (drive_is_ready(drive)) {
1227 if (drive->waiting_for_dma)
Bartlomiej Zolnierkiewicz5e37bdc2008-04-26 22:25:24 +02001228 hwif->dma_ops->dma_lost_irq(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 (void)ide_ack_intr(hwif);
1230 printk(KERN_WARNING "%s: lost interrupt\n", drive->name);
1231 startstop = handler(drive);
1232 } else {
1233 if (drive->waiting_for_dma) {
1234 startstop = ide_dma_timeout_retry(drive, wait);
1235 } else
1236 startstop =
Bartlomiej Zolnierkiewiczc47137a2008-02-06 02:57:51 +01001237 ide_error(drive, "irq timeout",
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +02001238 hwif->tp_ops->read_status(hwif));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 }
1240 drive->service_time = jiffies - drive->service_start;
1241 spin_lock_irq(&ide_lock);
1242 enable_irq(hwif->irq);
1243 if (startstop == ide_stopped)
1244 hwgroup->busy = 0;
1245 }
1246 }
1247 ide_do_request(hwgroup, IDE_NO_IRQ);
1248 spin_unlock_irqrestore(&ide_lock, flags);
1249}
1250
1251/**
1252 * unexpected_intr - handle an unexpected IDE interrupt
1253 * @irq: interrupt line
1254 * @hwgroup: hwgroup being processed
1255 *
1256 * There's nothing really useful we can do with an unexpected interrupt,
1257 * other than reading the status register (to clear it), and logging it.
1258 * There should be no way that an irq can happen before we're ready for it,
1259 * so we needn't worry much about losing an "important" interrupt here.
1260 *
1261 * On laptops (and "green" PCs), an unexpected interrupt occurs whenever
1262 * the drive enters "idle", "standby", or "sleep" mode, so if the status
1263 * looks "good", we just ignore the interrupt completely.
1264 *
1265 * This routine assumes __cli() is in effect when called.
1266 *
1267 * If an unexpected interrupt happens on irq15 while we are handling irq14
1268 * and if the two interfaces are "serialized" (CMD640), then it looks like
1269 * we could screw up by interfering with a new request being set up for
1270 * irq15.
1271 *
1272 * In reality, this is a non-issue. The new command is not sent unless
1273 * the drive is ready to accept one, in which case we know the drive is
1274 * not trying to interrupt us. And ide_set_handler() is always invoked
1275 * before completing the issuance of any new drive command, so we will not
1276 * be accidentally invoked as a result of any valid command completion
1277 * interrupt.
1278 *
1279 * Note that we must walk the entire hwgroup here. We know which hwif
1280 * is doing the current command, but we don't know which hwif burped
1281 * mysteriously.
1282 */
1283
1284static void unexpected_intr (int irq, ide_hwgroup_t *hwgroup)
1285{
1286 u8 stat;
1287 ide_hwif_t *hwif = hwgroup->hwif;
1288
1289 /*
1290 * handle the unexpected interrupt
1291 */
1292 do {
1293 if (hwif->irq == irq) {
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +02001294 stat = hwif->tp_ops->read_status(hwif);
Bartlomiej Zolnierkiewiczb73c7ee2008-07-23 19:55:52 +02001295
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +02001296 if (!OK_STAT(stat, ATA_DRDY, BAD_STAT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 /* Try to not flood the console with msgs */
1298 static unsigned long last_msgtime, count;
1299 ++count;
1300 if (time_after(jiffies, last_msgtime + HZ)) {
1301 last_msgtime = jiffies;
1302 printk(KERN_ERR "%s%s: unexpected interrupt, "
1303 "status=0x%02x, count=%ld\n",
1304 hwif->name,
1305 (hwif->next==hwgroup->hwif) ? "" : "(?)", stat, count);
1306 }
1307 }
1308 }
1309 } while ((hwif = hwif->next) != hwgroup->hwif);
1310}
1311
1312/**
1313 * ide_intr - default IDE interrupt handler
1314 * @irq: interrupt number
1315 * @dev_id: hwif group
1316 * @regs: unused weirdness from the kernel irq layer
1317 *
1318 * This is the default IRQ handler for the IDE layer. You should
1319 * not need to override it. If you do be aware it is subtle in
1320 * places
1321 *
1322 * hwgroup->hwif is the interface in the group currently performing
1323 * a command. hwgroup->drive is the drive and hwgroup->handler is
1324 * the IRQ handler to call. As we issue a command the handlers
1325 * step through multiple states, reassigning the handler to the
1326 * next step in the process. Unlike a smart SCSI controller IDE
1327 * expects the main processor to sequence the various transfer
1328 * stages. We also manage a poll timer to catch up with most
1329 * timeout situations. There are still a few where the handlers
1330 * don't ever decide to give up.
1331 *
1332 * The handler eventually returns ide_stopped to indicate the
1333 * request completed. At this point we issue the next request
1334 * on the hwgroup and the process begins again.
1335 */
1336
David Howells7d12e782006-10-05 14:55:46 +01001337irqreturn_t ide_intr (int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338{
1339 unsigned long flags;
1340 ide_hwgroup_t *hwgroup = (ide_hwgroup_t *)dev_id;
1341 ide_hwif_t *hwif;
1342 ide_drive_t *drive;
1343 ide_handler_t *handler;
1344 ide_startstop_t startstop;
Bartlomiej Zolnierkiewicz3e0e29f2008-12-29 20:27:29 +01001345 irqreturn_t irq_ret = IRQ_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346
1347 spin_lock_irqsave(&ide_lock, flags);
1348 hwif = hwgroup->hwif;
1349
Bartlomiej Zolnierkiewicz3e0e29f2008-12-29 20:27:29 +01001350 if (!ide_ack_intr(hwif))
1351 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352
1353 if ((handler = hwgroup->handler) == NULL || hwgroup->polling) {
1354 /*
1355 * Not expecting an interrupt from this drive.
1356 * That means this could be:
1357 * (1) an interrupt from another PCI device
1358 * sharing the same PCI INT# as us.
1359 * or (2) a drive just entered sleep or standby mode,
1360 * and is interrupting to let us know.
1361 * or (3) a spurious interrupt of unknown origin.
1362 *
1363 * For PCI, we cannot tell the difference,
1364 * so in that case we just ignore it and hope it goes away.
1365 *
1366 * FIXME: unexpected_intr should be hwif-> then we can
1367 * remove all the ifdef PCI crap
1368 */
1369#ifdef CONFIG_BLK_DEV_IDEPCI
Bartlomiej Zolnierkiewicz425afb62008-02-01 23:09:31 +01001370 if (hwif->chipset != ide_pci)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371#endif /* CONFIG_BLK_DEV_IDEPCI */
1372 {
1373 /*
1374 * Probably not a shared PCI interrupt,
1375 * so we can safely try to do something about it:
1376 */
1377 unexpected_intr(irq, hwgroup);
1378#ifdef CONFIG_BLK_DEV_IDEPCI
1379 } else {
1380 /*
1381 * Whack the status register, just in case
1382 * we have a leftover pending IRQ.
1383 */
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +02001384 (void)hwif->tp_ops->read_status(hwif);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385#endif /* CONFIG_BLK_DEV_IDEPCI */
1386 }
Bartlomiej Zolnierkiewicz3e0e29f2008-12-29 20:27:29 +01001387 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 }
Bartlomiej Zolnierkiewicz3e0e29f2008-12-29 20:27:29 +01001389
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 drive = hwgroup->drive;
1391 if (!drive) {
1392 /*
1393 * This should NEVER happen, and there isn't much
1394 * we could do about it here.
1395 *
1396 * [Note - this can occur if the drive is hot unplugged]
1397 */
Bartlomiej Zolnierkiewicz3e0e29f2008-12-29 20:27:29 +01001398 goto out_handled;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 }
Bartlomiej Zolnierkiewicz3e0e29f2008-12-29 20:27:29 +01001400
1401 if (!drive_is_ready(drive))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 /*
1403 * This happens regularly when we share a PCI IRQ with
1404 * another device. Unfortunately, it can also happen
1405 * with some buggy drives that trigger the IRQ before
1406 * their status register is up to date. Hopefully we have
1407 * enough advance overhead that the latter isn't a problem.
1408 */
Bartlomiej Zolnierkiewicz3e0e29f2008-12-29 20:27:29 +01001409 goto out;
1410
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 if (!hwgroup->busy) {
1412 hwgroup->busy = 1; /* paranoia */
1413 printk(KERN_ERR "%s: ide_intr: hwgroup->busy was 0 ??\n", drive->name);
1414 }
1415 hwgroup->handler = NULL;
Suleiman Souhlal23450312007-04-10 22:38:37 +02001416 hwgroup->req_gen++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 del_timer(&hwgroup->timer);
1418 spin_unlock(&ide_lock);
1419
Bartlomiej Zolnierkiewiczbfa7d8e2008-10-13 21:39:42 +02001420 if (hwif->port_ops && hwif->port_ops->clear_irq)
1421 hwif->port_ops->clear_irq(drive);
Albert Leef0dd8712007-02-17 02:40:21 +01001422
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +02001423 if (drive->dev_flags & IDE_DFLAG_UNMASK)
Ingo Molnar366c7f52006-07-03 00:25:25 -07001424 local_irq_enable_in_hardirq();
Bartlomiej Zolnierkiewiczbfa7d8e2008-10-13 21:39:42 +02001425
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 /* service this interrupt, may set handler for next interrupt */
1427 startstop = handler(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428
Bartlomiej Zolnierkiewiczbfa7d8e2008-10-13 21:39:42 +02001429 spin_lock_irq(&ide_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 /*
1431 * Note that handler() may have set things up for another
1432 * interrupt to occur soon, but it cannot happen until
1433 * we exit from this routine, because it will be the
1434 * same irq as is currently being serviced here, and Linux
1435 * won't allow another of the same (on any CPU) until we return.
1436 */
1437 drive->service_time = jiffies - drive->service_start;
1438 if (startstop == ide_stopped) {
1439 if (hwgroup->handler == NULL) { /* paranoia */
1440 hwgroup->busy = 0;
1441 ide_do_request(hwgroup, hwif->irq);
1442 } else {
1443 printk(KERN_ERR "%s: ide_intr: huh? expected NULL handler "
1444 "on exit\n", drive->name);
1445 }
1446 }
Bartlomiej Zolnierkiewicz3e0e29f2008-12-29 20:27:29 +01001447out_handled:
1448 irq_ret = IRQ_HANDLED;
1449out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 spin_unlock_irqrestore(&ide_lock, flags);
Bartlomiej Zolnierkiewicz3e0e29f2008-12-29 20:27:29 +01001451 return irq_ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452}
1453
1454/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 * ide_do_drive_cmd - issue IDE special command
1456 * @drive: device to issue command
1457 * @rq: request to issue
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 *
1459 * This function issues a special IDE device request
1460 * onto the request queue.
1461 *
FUJITA Tomonori63f5abb2008-07-15 21:21:51 +02001462 * the rq is queued at the head of the request queue, displacing
1463 * the currently-being-processed request and this function
1464 * returns immediately without waiting for the new rq to be
1465 * completed. This is VERY DANGEROUS, and is intended for
1466 * careful use by the ATAPI tape/cdrom driver code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 */
FUJITA Tomonori63f5abb2008-07-15 21:21:51 +02001468
1469void ide_do_drive_cmd(ide_drive_t *drive, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470{
Bartlomiej Zolnierkiewicz1d0bf582008-12-29 20:27:30 +01001471 ide_hwgroup_t *hwgroup = drive->hwif->hwgroup;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 unsigned long flags;
Bartlomiej Zolnierkiewicz1d0bf582008-12-29 20:27:30 +01001473
1474 hwgroup->rq = NULL;
Bartlomiej Zolnierkiewicze8a96aa2008-07-15 21:21:41 +02001475
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 spin_lock_irqsave(&ide_lock, flags);
Jens Axboef73e2d12008-10-17 14:03:08 +02001477 __elv_add_request(drive->queue, rq, ELEVATOR_INSERT_FRONT, 0);
1478 blk_start_queueing(drive->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 spin_unlock_irqrestore(&ide_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480}
1481
1482EXPORT_SYMBOL(ide_do_drive_cmd);
Bartlomiej Zolnierkiewicz2fc57382008-01-25 22:17:13 +01001483
1484void ide_pktcmd_tf_load(ide_drive_t *drive, u32 tf_flags, u16 bcount, u8 dma)
1485{
Bartlomiej Zolnierkiewicz6e6afb32008-07-23 19:55:52 +02001486 ide_hwif_t *hwif = drive->hwif;
Bartlomiej Zolnierkiewicz2fc57382008-01-25 22:17:13 +01001487 ide_task_t task;
1488
1489 memset(&task, 0, sizeof(task));
1490 task.tf_flags = IDE_TFLAG_OUT_LBAH | IDE_TFLAG_OUT_LBAM |
1491 IDE_TFLAG_OUT_FEATURE | tf_flags;
1492 task.tf.feature = dma; /* Use PIO/DMA */
1493 task.tf.lbam = bcount & 0xff;
1494 task.tf.lbah = (bcount >> 8) & 0xff;
1495
Bartlomiej Zolnierkiewicz089c5c72008-04-28 23:44:39 +02001496 ide_tf_dump(drive->name, &task.tf);
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +02001497 hwif->tp_ops->set_irq(hwif, 1);
Bartlomiej Zolnierkiewiczed4af482008-07-15 21:21:48 +02001498 SELECT_MASK(drive, 0);
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +02001499 hwif->tp_ops->tf_load(drive, &task);
Bartlomiej Zolnierkiewicz2fc57382008-01-25 22:17:13 +01001500}
1501
1502EXPORT_SYMBOL_GPL(ide_pktcmd_tf_load);
Bartlomiej Zolnierkiewicz9f87abe2008-04-28 23:44:41 +02001503
1504void ide_pad_transfer(ide_drive_t *drive, int write, int len)
1505{
1506 ide_hwif_t *hwif = drive->hwif;
1507 u8 buf[4] = { 0 };
1508
1509 while (len > 0) {
1510 if (write)
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +02001511 hwif->tp_ops->output_data(drive, NULL, buf, min(4, len));
Bartlomiej Zolnierkiewicz9f87abe2008-04-28 23:44:41 +02001512 else
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +02001513 hwif->tp_ops->input_data(drive, NULL, buf, min(4, len));
Bartlomiej Zolnierkiewicz9f87abe2008-04-28 23:44:41 +02001514 len -= 4;
1515 }
1516}
1517EXPORT_SYMBOL_GPL(ide_pad_transfer);