ide-io.c revision 3c8a2cce47c6813383c9e38134e31f7e5f72e9d8
1/*
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
27#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>
43#include <linux/hdreg.h>
44#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>
51#include <linux/bitops.h>
52
53#include <asm/byteorder.h>
54#include <asm/irq.h>
55#include <asm/uaccess.h>
56#include <asm/io.h>
57
58static int __ide_end_request(ide_drive_t *drive, struct request *rq,
59			     int uptodate, unsigned int nr_bytes, int dequeue)
60{
61	int ret = 1;
62	int error = 0;
63
64	if (uptodate <= 0)
65		error = uptodate ? uptodate : -EIO;
66
67	/*
68	 * if failfast is set on a request, override number of sectors and
69	 * complete the whole request right now
70	 */
71	if (blk_noretry_request(rq) && error)
72		nr_bytes = rq->hard_nr_sectors << 9;
73
74	if (!blk_fs_request(rq) && error && !rq->errors)
75		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	 */
81	if ((drive->dev_flags & IDE_DFLAG_DMA_PIO_RETRY) &&
82	    drive->retry_pio <= 3) {
83		drive->dev_flags &= ~IDE_DFLAG_DMA_PIO_RETRY;
84		ide_dma_on(drive);
85	}
86
87	if (!blk_end_request(rq, error, nr_bytes))
88		ret = 0;
89
90	if (ret == 0 && dequeue)
91		drive->hwif->hwgroup->rq = NULL;
92
93	return ret;
94}
95
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{
109	unsigned int nr_bytes = nr_sectors << 9;
110	struct request *rq = drive->hwif->hwgroup->rq;
111
112	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	}
118
119	return __ide_end_request(drive, rq, uptodate, nr_bytes, 1);
120}
121EXPORT_SYMBOL(ide_end_request);
122
123static void ide_complete_power_step(ide_drive_t *drive, struct request *rq)
124{
125	struct request_pm_state *pm = rq->data;
126
127#ifdef DEBUG_PM
128	printk(KERN_INFO "%s: complete_power_step(step: %d)\n",
129		drive->name, pm->pm_step);
130#endif
131	if (drive->media != ide_disk)
132		return;
133
134	switch (pm->pm_step) {
135	case IDE_PM_FLUSH_CACHE:	/* Suspend step 1 (flush cache) */
136		if (pm->pm_state == PM_EVENT_FREEZE)
137			pm->pm_step = IDE_PM_COMPLETED;
138		else
139			pm->pm_step = IDE_PM_STANDBY;
140		break;
141	case IDE_PM_STANDBY:		/* Suspend step 2 (standby) */
142		pm->pm_step = IDE_PM_COMPLETED;
143		break;
144	case IDE_PM_RESTORE_PIO:	/* Resume step 1 (restore PIO) */
145		pm->pm_step = IDE_PM_IDLE;
146		break;
147	case IDE_PM_IDLE:		/* Resume step 2 (idle)*/
148		pm->pm_step = IDE_PM_RESTORE_DMA;
149		break;
150	}
151}
152
153static ide_startstop_t ide_start_power_step(ide_drive_t *drive, struct request *rq)
154{
155	struct request_pm_state *pm = rq->data;
156	ide_task_t *args = rq->special;
157
158	memset(args, 0, sizeof(*args));
159
160	switch (pm->pm_step) {
161	case IDE_PM_FLUSH_CACHE:	/* Suspend step 1 (flush cache) */
162		if (drive->media != ide_disk)
163			break;
164		/* Not supported? Switch to next step now. */
165		if (ata_id_flush_enabled(drive->id) == 0 ||
166		    (drive->dev_flags & IDE_DFLAG_WCACHE) == 0) {
167			ide_complete_power_step(drive, rq);
168			return ide_stopped;
169		}
170		if (ata_id_flush_ext_enabled(drive->id))
171			args->tf.command = ATA_CMD_FLUSH_EXT;
172		else
173			args->tf.command = ATA_CMD_FLUSH;
174		goto out_do_tf;
175	case IDE_PM_STANDBY:		/* Suspend step 2 (standby) */
176		args->tf.command = ATA_CMD_STANDBYNOW1;
177		goto out_do_tf;
178	case IDE_PM_RESTORE_PIO:	/* Resume step 1 (restore PIO) */
179		ide_set_max_pio(drive);
180		/*
181		 * skip IDE_PM_IDLE for ATAPI devices
182		 */
183		if (drive->media != ide_disk)
184			pm->pm_step = IDE_PM_RESTORE_DMA;
185		else
186			ide_complete_power_step(drive, rq);
187		return ide_stopped;
188	case IDE_PM_IDLE:		/* Resume step 2 (idle) */
189		args->tf.command = ATA_CMD_IDLEIMMEDIATE;
190		goto out_do_tf;
191	case IDE_PM_RESTORE_DMA:	/* Resume step 3 (restore DMA) */
192		/*
193		 * Right now, all we do is call ide_set_dma(drive),
194		 * we could be smarter and check for current xfer_speed
195		 * in struct drive etc...
196		 */
197		if (drive->hwif->dma_ops == NULL)
198			break;
199		/*
200		 * TODO: respect IDE_DFLAG_USING_DMA
201		 */
202		ide_set_dma(drive);
203		break;
204	}
205
206	pm->pm_step = IDE_PM_COMPLETED;
207	return ide_stopped;
208
209out_do_tf:
210	args->tf_flags	 = IDE_TFLAG_TF | IDE_TFLAG_DEVICE;
211	args->data_phase = TASKFILE_NO_DATA;
212	return do_rw_taskfile(drive, args);
213}
214
215/**
216 *	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{
233	BUG_ON(!blk_rq_started(rq));
234
235	return __ide_end_request(drive, rq, uptodate, nr_sectors << 9, 0);
236}
237EXPORT_SYMBOL_GPL(ide_end_dequeued_request);
238
239
240/**
241 *	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 {
260		drive->dev_flags &= ~IDE_DFLAG_BLOCKED;
261		blk_start_queue(drive->queue);
262	}
263	spin_unlock_irqrestore(&ide_lock, flags);
264
265	drive->hwif->hwgroup->rq = NULL;
266
267	if (blk_end_request(rq, 0, 0))
268		BUG();
269}
270
271/**
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{
287	ide_hwgroup_t *hwgroup = drive->hwif->hwgroup;
288	struct request *rq = hwgroup->rq;
289
290	if (rq->cmd_type == REQ_TYPE_ATA_TASKFILE) {
291		ide_task_t *task = (ide_task_t *)rq->special;
292
293		if (rq->errors == 0)
294			rq->errors = !OK_STAT(stat, ATA_DRDY, BAD_STAT);
295
296		if (task) {
297			struct ide_taskfile *tf = &task->tf;
298
299			tf->error = err;
300			tf->status = stat;
301
302			drive->hwif->tp_ops->tf_read(drive, task);
303
304			if (task->tf_flags & IDE_TFLAG_DYN)
305				kfree(task);
306		}
307	} else if (blk_pm_request(rq)) {
308		struct request_pm_state *pm = rq->data;
309
310		ide_complete_power_step(drive, rq);
311		if (pm->pm_step == IDE_PM_COMPLETED)
312			ide_complete_pm_request(drive, rq);
313		return;
314	}
315
316	hwgroup->rq = NULL;
317
318	rq->errors = err;
319
320	if (unlikely(blk_end_request(rq, (rq->errors ? -EIO : 0),
321				     blk_rq_bytes(rq))))
322		BUG();
323}
324EXPORT_SYMBOL(ide_end_drive_cmd);
325
326static 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
341	if ((stat & ATA_BUSY) ||
342	    ((stat & ATA_DF) && (drive->dev_flags & IDE_DFLAG_NOWERR) == 0)) {
343		/* other bits are useless when BUSY */
344		rq->errors |= ERROR_RESET;
345	} else if (stat & ATA_ERR) {
346		/* err has different meaning on cdrom and tape */
347		if (err == ATA_ABORTED) {
348			if ((drive->dev_flags & IDE_DFLAG_LBA) &&
349			    /* some newer drives don't support ATA_CMD_INIT_DEV_PARAMS */
350			    hwif->tp_ops->read_status(hwif) == ATA_CMD_INIT_DEV_PARAMS)
351				return ide_stopped;
352		} else if ((err & BAD_CRC) == BAD_CRC) {
353			/* UDMA crc error, just retry the operation */
354			drive->crc_count++;
355		} else if (err & (ATA_BBK | ATA_UNC)) {
356			/* retries won't help these */
357			rq->errors = ERROR_MAX;
358		} else if (err & ATA_TRK0NF) {
359			/* help it find track zero */
360			rq->errors |= ERROR_RECAL;
361		}
362	}
363
364	if ((stat & ATA_DRQ) && rq_data_dir(rq) == READ &&
365	    (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	}
370
371	if (rq->errors >= ERROR_MAX || blk_noretry_request(rq)) {
372		ide_kill_rq(drive, rq);
373		return ide_stopped;
374	}
375
376	if (hwif->tp_ops->read_status(hwif) & (ATA_BUSY | ATA_DRQ))
377		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
389	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
396	if ((stat & ATA_BUSY) ||
397	    ((stat & ATA_DF) && (drive->dev_flags & IDE_DFLAG_NOWERR) == 0)) {
398		/* other bits are useless when BUSY */
399		rq->errors |= ERROR_RESET;
400	} else {
401		/* add decoding error stuff */
402	}
403
404	if (hwif->tp_ops->read_status(hwif) & (ATA_BUSY | ATA_DRQ))
405		/* force an abort */
406		hwif->tp_ops->exec_command(hwif, ATA_CMD_IDLEIMMEDIATE);
407
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: */
455	if (!blk_fs_request(rq)) {
456		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
472static void ide_tf_set_specify_cmd(ide_drive_t *drive, struct ide_taskfile *tf)
473{
474	tf->nsect   = drive->sect;
475	tf->lbal    = drive->sect;
476	tf->lbam    = drive->cyl;
477	tf->lbah    = drive->cyl >> 8;
478	tf->device  = (drive->head - 1) | drive->select;
479	tf->command = ATA_CMD_INIT_DEV_PARAMS;
480}
481
482static void ide_tf_set_restore_cmd(ide_drive_t *drive, struct ide_taskfile *tf)
483{
484	tf->nsect   = drive->sect;
485	tf->command = ATA_CMD_RESTORE;
486}
487
488static void ide_tf_set_setmult_cmd(ide_drive_t *drive, struct ide_taskfile *tf)
489{
490	tf->nsect   = drive->mult_req;
491	tf->command = ATA_CMD_SET_MULTI;
492}
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));
500	args.data_phase = TASKFILE_NO_DATA;
501
502	if (s->b.set_geometry) {
503		s->b.set_geometry = 0;
504		ide_tf_set_specify_cmd(drive, &args.tf);
505	} else if (s->b.recalibrate) {
506		s->b.recalibrate = 0;
507		ide_tf_set_restore_cmd(drive, &args.tf);
508	} else if (s->b.set_multmode) {
509		s->b.set_multmode = 0;
510		ide_tf_set_setmult_cmd(drive, &args.tf);
511	} 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
518	args.tf_flags = IDE_TFLAG_TF | IDE_TFLAG_DEVICE |
519			IDE_TFLAG_CUSTOM_HANDLER;
520
521	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 *
530 *	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.
534 */
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
543	if (drive->media == ide_disk)
544		return ide_disk_special(drive);
545
546	s->all = 0;
547	drive->mult_req = 0;
548	return ide_stopped;
549}
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
559	if (rq->cmd_type != REQ_TYPE_ATA_TASKFILE) {
560		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;
574	hwif->cursg_ofs = 0;
575	hwif->cursg = NULL;
576}
577
578EXPORT_SYMBOL_GPL(ide_init_sg_cmd);
579
580/**
581 *	execute_drive_command	-	issue special drive command
582 *	@drive: the drive to issue the command on
583 *	@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);
596	ide_task_t *task = rq->special;
597
598	if (task) {
599		hwif->data_phase = task->data_phase;
600
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
612		return do_rw_taskfile(drive, task);
613	}
614
615 	/*
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
622	ide_end_drive_cmd(drive, hwif->tp_ops->read_status(hwif),
623			  ide_read_error(drive));
624
625 	return ide_stopped;
626}
627
628int 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
638	rq = blk_get_request(q, READ, __GFP_WAIT);
639	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
653static ide_startstop_t ide_special_rq(ide_drive_t *drive, struct request *rq)
654{
655	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) {
681	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	}
693	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
702static void ide_check_pm_state(ide_drive_t *drive, struct request *rq)
703{
704	struct request_pm_state *pm = rq->data;
705
706	if (blk_pm_suspend_request(rq) &&
707	    pm->pm_step == IDE_PM_START_SUSPEND)
708		/* Mark drive blocked when starting the suspend sequence. */
709		drive->dev_flags |= IDE_DFLAG_BLOCKED;
710	else if (blk_pm_resume_request(rq) &&
711		 pm->pm_step == IDE_PM_START_RESUME) {
712		/*
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		 */
720		ide_hwif_t *hwif = drive->hwif;
721		int rc;
722#ifdef DEBUG_PM
723		printk("%s: Wakeup request inited, waiting for !BSY...\n", drive->name);
724#endif
725		rc = ide_wait_not_busy(hwif, 35000);
726		if (rc)
727			printk(KERN_WARNING "%s: bus not ready on wakeup\n", drive->name);
728		SELECT_DRIVE(drive);
729		hwif->tp_ops->set_irq(hwif, 1);
730		rc = ide_wait_not_busy(hwif, 100000);
731		if (rc)
732			printk(KERN_WARNING "%s: drive not ready on wakeup\n", drive->name);
733	}
734}
735
736/**
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
740 *	accepts commands and I/O (read/write) requests.
741 *
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;
748
749	BUG_ON(!blk_rq_started(rq));
750
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)) {
758		rq->cmd_flags |= REQ_FAILED;
759		goto kill_rq;
760	}
761
762	if (blk_pm_request(rq))
763		ide_check_pm_state(drive, rq);
764
765	SELECT_DRIVE(drive);
766	if (ide_wait_stat(&startstop, drive, drive->ready_stat,
767			  ATA_BUSY | ATA_DRQ, WAIT_READY)) {
768		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
774		/*
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
781		if (rq->cmd_type == REQ_TYPE_ATA_TASKFILE)
782			return execute_drive_cmd(drive, rq);
783		else if (blk_pm_request(rq)) {
784			struct request_pm_state *pm = rq->data;
785#ifdef DEBUG_PM
786			printk("%s: start_power_step(step: %d)\n",
787				drive->name, pm->pm_step);
788#endif
789			startstop = ide_start_power_step(drive, rq);
790			if (startstop == ide_stopped &&
791			    pm->pm_step == IDE_PM_COMPLETED)
792				ide_complete_pm_request(drive, rq);
793			return startstop;
794		} 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);
804
805		drv = *(ide_driver_t **)rq->rq_disk->private_data;
806
807		return drv->do_request(drive, rq, rq->sector);
808	}
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;
829	drive->dev_flags |= IDE_DFLAG_SLEEPING;
830}
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 {
869		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)))) {
877				if (!blk_queue_plugged(drive->queue))
878					best = drive;
879			}
880		}
881	} while ((drive = drive->next) != hwgroup->drive);
882
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) {
886		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 {
894				if ((drive->dev_flags & IDE_DFLAG_SLEEPING) == 0
895				 && 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;
948	int             loops = 0;
949
950	/* caller must own ide_lock */
951	BUG_ON(!irqs_disabled());
952
953	while (!hwgroup->busy) {
954		hwgroup->busy = 1;
955		/* for atari only */
956		ide_get_lock(ide_intr, hwgroup);
957		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 {
964				if ((drive->dev_flags & IDE_DFLAG_SLEEPING) &&
965				    (sleeping == 0 ||
966				     time_before(drive->sleep, sleep))) {
967					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;
986				hwgroup->req_gen_timer = hwgroup->req_gen;
987				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		}
1003	again:
1004		hwif = HWIF(drive);
1005		if (hwgroup->hwif->sharing_irq && hwif != hwgroup->hwif) {
1006			/*
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)
1011				hwif->tp_ops->set_irq(hwif, 0);
1012		}
1013		hwgroup->hwif = hwif;
1014		hwgroup->drive = drive;
1015		drive->dev_flags &= ~(IDE_DFLAG_SLEEPING | IDE_DFLAG_PARKED);
1016		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.
1045		 *
1046		 * We count how many times we loop here to make sure we service
1047		 * all drives in the hwgroup without looping for ever
1048		 */
1049		if ((drive->dev_flags & IDE_DFLAG_BLOCKED) &&
1050		    blk_pm_request(rq) == 0 &&
1051		    (rq->cmd_flags & REQ_PREEMPT) == 0) {
1052			drive = drive->next ? drive->next : hwgroup->drive;
1053			if (loops++ < 4 && !blk_queue_plugged(drive->queue))
1054				goto again;
1055			/* 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);
1073		local_irq_enable_in_hardirq();
1074			/* 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 */
1087void do_ide_request(struct request_queue *q)
1088{
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);
1111		(void)hwif->dma_ops->dma_end(drive);
1112		ret = ide_error(drive, "dma timeout error",
1113				hwif->tp_ops->read_status(hwif));
1114	} else {
1115		printk(KERN_WARNING "%s: DMA timeout retry\n", drive->name);
1116		hwif->dma_ops->dma_timeout(drive);
1117	}
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	 */
1124	drive->dev_flags |= IDE_DFLAG_DMA_PIO_RETRY;
1125	drive->retry_pio++;
1126	ide_dma_off_quietly(drive);
1127
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;
1133
1134	if (!rq)
1135		goto out;
1136
1137	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
1176	if (((handler = hwgroup->handler) == NULL) ||
1177	    (hwgroup->req_gen != hwgroup->req_gen_timer)) {
1178		/*
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;
1205					hwgroup->req_gen_timer = hwgroup->req_gen;
1206					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);
1219			/* disable_irq_nosync ?? */
1220			disable_irq(hwif->irq);
1221			/* 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)
1228					hwif->dma_ops->dma_lost_irq(drive);
1229				(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 =
1237					ide_error(drive, "irq timeout",
1238						  hwif->tp_ops->read_status(hwif));
1239			}
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) {
1294			stat = hwif->tp_ops->read_status(hwif);
1295
1296			if (!OK_STAT(stat, ATA_DRDY, BAD_STAT)) {
1297				/* 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
1337irqreturn_t ide_intr (int irq, void *dev_id)
1338{
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;
1345	irqreturn_t irq_ret = IRQ_NONE;
1346
1347	spin_lock_irqsave(&ide_lock, flags);
1348	hwif = hwgroup->hwif;
1349
1350	if (!ide_ack_intr(hwif))
1351		goto out;
1352
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
1370		if (hwif->chipset != ide_pci)
1371#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			 */
1384			(void)hwif->tp_ops->read_status(hwif);
1385#endif /* CONFIG_BLK_DEV_IDEPCI */
1386		}
1387		goto out;
1388	}
1389
1390	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		 */
1398		goto out_handled;
1399	}
1400
1401	if (!drive_is_ready(drive))
1402		/*
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		 */
1409		goto out;
1410
1411	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;
1416	hwgroup->req_gen++;
1417	del_timer(&hwgroup->timer);
1418	spin_unlock(&ide_lock);
1419
1420	if (hwif->port_ops && hwif->port_ops->clear_irq)
1421		hwif->port_ops->clear_irq(drive);
1422
1423	if (drive->dev_flags & IDE_DFLAG_UNMASK)
1424		local_irq_enable_in_hardirq();
1425
1426	/* service this interrupt, may set handler for next interrupt */
1427	startstop = handler(drive);
1428
1429	spin_lock_irq(&ide_lock);
1430	/*
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	}
1447out_handled:
1448	irq_ret = IRQ_HANDLED;
1449out:
1450	spin_unlock_irqrestore(&ide_lock, flags);
1451	return irq_ret;
1452}
1453
1454/**
1455 *	ide_do_drive_cmd	-	issue IDE special command
1456 *	@drive: device to issue command
1457 *	@rq: request to issue
1458 *
1459 *	This function issues a special IDE device request
1460 *	onto the request queue.
1461 *
1462 *	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.
1467 */
1468
1469void ide_do_drive_cmd(ide_drive_t *drive, struct request *rq)
1470{
1471	ide_hwgroup_t *hwgroup = drive->hwif->hwgroup;
1472	unsigned long flags;
1473
1474	hwgroup->rq = NULL;
1475
1476	spin_lock_irqsave(&ide_lock, flags);
1477	__elv_add_request(drive->queue, rq, ELEVATOR_INSERT_FRONT, 0);
1478	blk_start_queueing(drive->queue);
1479	spin_unlock_irqrestore(&ide_lock, flags);
1480}
1481
1482EXPORT_SYMBOL(ide_do_drive_cmd);
1483
1484void ide_pktcmd_tf_load(ide_drive_t *drive, u32 tf_flags, u16 bcount, u8 dma)
1485{
1486	ide_hwif_t *hwif = drive->hwif;
1487	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
1496	ide_tf_dump(drive->name, &task.tf);
1497	hwif->tp_ops->set_irq(hwif, 1);
1498	SELECT_MASK(drive, 0);
1499	hwif->tp_ops->tf_load(drive, &task);
1500}
1501
1502EXPORT_SYMBOL_GPL(ide_pktcmd_tf_load);
1503
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)
1511			hwif->tp_ops->output_data(drive, NULL, buf, min(4, len));
1512		else
1513			hwif->tp_ops->input_data(drive, NULL, buf, min(4, len));
1514		len -= 4;
1515	}
1516}
1517EXPORT_SYMBOL_GPL(ide_pad_transfer);
1518