blob: cb11c7861a509ef050e2dddda8755616c38587e7 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * Copyright (C) 2000-2002 Andre Hedrick <andre@linux-ide.org>
3 * Copyright (C) 2003 Red Hat <alan@redhat.com>
4 *
5 */
6
Linus Torvalds1da177e2005-04-16 15:20:36 -07007#include <linux/module.h>
8#include <linux/types.h>
9#include <linux/string.h>
10#include <linux/kernel.h>
11#include <linux/timer.h>
12#include <linux/mm.h>
13#include <linux/interrupt.h>
14#include <linux/major.h>
15#include <linux/errno.h>
16#include <linux/genhd.h>
17#include <linux/blkpg.h>
18#include <linux/slab.h>
19#include <linux/pci.h>
20#include <linux/delay.h>
21#include <linux/hdreg.h>
22#include <linux/ide.h>
23#include <linux/bitops.h>
Michal Schmidt1e862402006-07-30 03:03:29 -070024#include <linux/nmi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
26#include <asm/byteorder.h>
27#include <asm/irq.h>
28#include <asm/uaccess.h>
29#include <asm/io.h>
30
31/*
32 * Conventional PIO operations for ATA devices
33 */
34
35static u8 ide_inb (unsigned long port)
36{
37 return (u8) inb(port);
38}
39
Linus Torvalds1da177e2005-04-16 15:20:36 -070040static void ide_outb (u8 val, unsigned long port)
41{
42 outb(val, port);
43}
44
Bartlomiej Zolnierkiewiczf8c4bd0a2008-07-15 21:21:49 +020045static void ide_outbsync(ide_hwif_t *hwif, u8 addr, unsigned long port)
Linus Torvalds1da177e2005-04-16 15:20:36 -070046{
47 outb(addr, port);
48}
49
Linus Torvalds1da177e2005-04-16 15:20:36 -070050void default_hwif_iops (ide_hwif_t *hwif)
51{
52 hwif->OUTB = ide_outb;
53 hwif->OUTBSYNC = ide_outbsync;
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 hwif->INB = ide_inb;
Linus Torvalds1da177e2005-04-16 15:20:36 -070055}
56
Linus Torvalds1da177e2005-04-16 15:20:36 -070057/*
58 * MMIO operations, typically used for SATA controllers
59 */
60
61static u8 ide_mm_inb (unsigned long port)
62{
63 return (u8) readb((void __iomem *) port);
64}
65
Linus Torvalds1da177e2005-04-16 15:20:36 -070066static void ide_mm_outb (u8 value, unsigned long port)
67{
68 writeb(value, (void __iomem *) port);
69}
70
Bartlomiej Zolnierkiewiczf8c4bd0a2008-07-15 21:21:49 +020071static void ide_mm_outbsync(ide_hwif_t *hwif, u8 value, unsigned long port)
Linus Torvalds1da177e2005-04-16 15:20:36 -070072{
73 writeb(value, (void __iomem *) port);
74}
75
Linus Torvalds1da177e2005-04-16 15:20:36 -070076void default_hwif_mmiops (ide_hwif_t *hwif)
77{
78 hwif->OUTB = ide_mm_outb;
79 /* Most systems will need to override OUTBSYNC, alas however
80 this one is controller specific! */
81 hwif->OUTBSYNC = ide_mm_outbsync;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 hwif->INB = ide_mm_inb;
Linus Torvalds1da177e2005-04-16 15:20:36 -070083}
84
85EXPORT_SYMBOL(default_hwif_mmiops);
86
Linus Torvalds1da177e2005-04-16 15:20:36 -070087void SELECT_DRIVE (ide_drive_t *drive)
88{
Bartlomiej Zolnierkiewicz23579a22008-04-18 00:46:26 +020089 ide_hwif_t *hwif = drive->hwif;
Bartlomiej Zolnierkiewiczac95bee2008-04-26 22:25:14 +020090 const struct ide_port_ops *port_ops = hwif->port_ops;
Bartlomiej Zolnierkiewicz40f095f2008-07-23 19:55:53 +020091 ide_task_t task;
Bartlomiej Zolnierkiewicz23579a22008-04-18 00:46:26 +020092
Bartlomiej Zolnierkiewiczac95bee2008-04-26 22:25:14 +020093 if (port_ops && port_ops->selectproc)
94 port_ops->selectproc(drive);
Bartlomiej Zolnierkiewicz23579a22008-04-18 00:46:26 +020095
Bartlomiej Zolnierkiewicz40f095f2008-07-23 19:55:53 +020096 memset(&task, 0, sizeof(task));
97 task.tf_flags = IDE_TFLAG_OUT_DEVICE;
98
99 drive->hwif->tf_load(drive, &task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100}
101
Bartlomiej Zolnierkiewiczed4af482008-07-15 21:21:48 +0200102void SELECT_MASK(ide_drive_t *drive, int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103{
Bartlomiej Zolnierkiewiczac95bee2008-04-26 22:25:14 +0200104 const struct ide_port_ops *port_ops = drive->hwif->port_ops;
105
106 if (port_ops && port_ops->maskproc)
107 port_ops->maskproc(drive, mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108}
109
Bartlomiej Zolnierkiewiczc6dfa862008-07-23 19:55:51 +0200110static void ide_exec_command(ide_hwif_t *hwif, u8 cmd)
111{
112 if (hwif->host_flags & IDE_HFLAG_MMIO)
113 writeb(cmd, (void __iomem *)hwif->io_ports.command_addr);
114 else
115 outb(cmd, hwif->io_ports.command_addr);
116}
117
Bartlomiej Zolnierkiewiczb73c7ee2008-07-23 19:55:52 +0200118static u8 ide_read_status(ide_hwif_t *hwif)
119{
120 if (hwif->host_flags & IDE_HFLAG_MMIO)
121 return readb((void __iomem *)hwif->io_ports.status_addr);
122 else
123 return inb(hwif->io_ports.status_addr);
124}
125
Bartlomiej Zolnierkiewicz1f6d8a02008-07-23 19:55:52 +0200126static u8 ide_read_altstatus(ide_hwif_t *hwif)
127{
128 if (hwif->host_flags & IDE_HFLAG_MMIO)
129 return readb((void __iomem *)hwif->io_ports.ctl_addr);
130 else
131 return inb(hwif->io_ports.ctl_addr);
132}
133
Bartlomiej Zolnierkiewiczb2f951a2008-07-23 19:55:50 +0200134static u8 ide_read_sff_dma_status(ide_hwif_t *hwif)
135{
136 if (hwif->host_flags & IDE_HFLAG_MMIO)
Bartlomiej Zolnierkiewiczcab7f8e2008-07-23 19:55:51 +0200137 return readb((void __iomem *)(hwif->dma_base + ATA_DMA_STATUS));
Bartlomiej Zolnierkiewiczb2f951a2008-07-23 19:55:50 +0200138 else
Bartlomiej Zolnierkiewiczcab7f8e2008-07-23 19:55:51 +0200139 return inb(hwif->dma_base + ATA_DMA_STATUS);
Bartlomiej Zolnierkiewiczb2f951a2008-07-23 19:55:50 +0200140}
141
Bartlomiej Zolnierkiewicz6e6afb32008-07-23 19:55:52 +0200142static void ide_set_irq(ide_hwif_t *hwif, int on)
143{
144 u8 ctl = ATA_DEVCTL_OBS;
145
146 if (on == 4) { /* hack for SRST */
147 ctl |= 4;
148 on &= ~4;
149 }
150
151 ctl |= on ? 0 : 2;
152
153 if (hwif->host_flags & IDE_HFLAG_MMIO)
154 writeb(ctl, (void __iomem *)hwif->io_ports.ctl_addr);
155 else
156 outb(ctl, hwif->io_ports.ctl_addr);
157}
158
Bartlomiej Zolnierkiewicz94cd5b62008-04-28 23:44:40 +0200159static void ide_tf_load(ide_drive_t *drive, ide_task_t *task)
Bartlomiej Zolnierkiewiczd309e0b2008-04-28 23:44:39 +0200160{
161 ide_hwif_t *hwif = drive->hwif;
162 struct ide_io_ports *io_ports = &hwif->io_ports;
163 struct ide_taskfile *tf = &task->tf;
Bartlomiej Zolnierkiewiczca545c12008-04-28 23:44:41 +0200164 void (*tf_outb)(u8 addr, unsigned long port);
165 u8 mmio = (hwif->host_flags & IDE_HFLAG_MMIO) ? 1 : 0;
Bartlomiej Zolnierkiewiczd309e0b2008-04-28 23:44:39 +0200166 u8 HIHI = (task->tf_flags & IDE_TFLAG_LBA48) ? 0xE0 : 0xEF;
167
Bartlomiej Zolnierkiewiczca545c12008-04-28 23:44:41 +0200168 if (mmio)
169 tf_outb = ide_mm_outb;
170 else
171 tf_outb = ide_outb;
172
Bartlomiej Zolnierkiewiczd309e0b2008-04-28 23:44:39 +0200173 if (task->tf_flags & IDE_TFLAG_FLAGGED)
174 HIHI = 0xFF;
175
Bartlomiej Zolnierkiewiczca545c12008-04-28 23:44:41 +0200176 if (task->tf_flags & IDE_TFLAG_OUT_DATA) {
177 u16 data = (tf->hob_data << 8) | tf->data;
178
179 if (mmio)
180 writew(data, (void __iomem *)io_ports->data_addr);
181 else
182 outw(data, io_ports->data_addr);
183 }
Bartlomiej Zolnierkiewiczd309e0b2008-04-28 23:44:39 +0200184
185 if (task->tf_flags & IDE_TFLAG_OUT_HOB_FEATURE)
Bartlomiej Zolnierkiewiczca545c12008-04-28 23:44:41 +0200186 tf_outb(tf->hob_feature, io_ports->feature_addr);
Bartlomiej Zolnierkiewiczd309e0b2008-04-28 23:44:39 +0200187 if (task->tf_flags & IDE_TFLAG_OUT_HOB_NSECT)
Bartlomiej Zolnierkiewiczca545c12008-04-28 23:44:41 +0200188 tf_outb(tf->hob_nsect, io_ports->nsect_addr);
Bartlomiej Zolnierkiewiczd309e0b2008-04-28 23:44:39 +0200189 if (task->tf_flags & IDE_TFLAG_OUT_HOB_LBAL)
Bartlomiej Zolnierkiewiczca545c12008-04-28 23:44:41 +0200190 tf_outb(tf->hob_lbal, io_ports->lbal_addr);
Bartlomiej Zolnierkiewiczd309e0b2008-04-28 23:44:39 +0200191 if (task->tf_flags & IDE_TFLAG_OUT_HOB_LBAM)
Bartlomiej Zolnierkiewiczca545c12008-04-28 23:44:41 +0200192 tf_outb(tf->hob_lbam, io_ports->lbam_addr);
Bartlomiej Zolnierkiewiczd309e0b2008-04-28 23:44:39 +0200193 if (task->tf_flags & IDE_TFLAG_OUT_HOB_LBAH)
Bartlomiej Zolnierkiewiczca545c12008-04-28 23:44:41 +0200194 tf_outb(tf->hob_lbah, io_ports->lbah_addr);
Bartlomiej Zolnierkiewiczd309e0b2008-04-28 23:44:39 +0200195
196 if (task->tf_flags & IDE_TFLAG_OUT_FEATURE)
Bartlomiej Zolnierkiewiczca545c12008-04-28 23:44:41 +0200197 tf_outb(tf->feature, io_ports->feature_addr);
Bartlomiej Zolnierkiewiczd309e0b2008-04-28 23:44:39 +0200198 if (task->tf_flags & IDE_TFLAG_OUT_NSECT)
Bartlomiej Zolnierkiewiczca545c12008-04-28 23:44:41 +0200199 tf_outb(tf->nsect, io_ports->nsect_addr);
Bartlomiej Zolnierkiewiczd309e0b2008-04-28 23:44:39 +0200200 if (task->tf_flags & IDE_TFLAG_OUT_LBAL)
Bartlomiej Zolnierkiewiczca545c12008-04-28 23:44:41 +0200201 tf_outb(tf->lbal, io_ports->lbal_addr);
Bartlomiej Zolnierkiewiczd309e0b2008-04-28 23:44:39 +0200202 if (task->tf_flags & IDE_TFLAG_OUT_LBAM)
Bartlomiej Zolnierkiewiczca545c12008-04-28 23:44:41 +0200203 tf_outb(tf->lbam, io_ports->lbam_addr);
Bartlomiej Zolnierkiewiczd309e0b2008-04-28 23:44:39 +0200204 if (task->tf_flags & IDE_TFLAG_OUT_LBAH)
Bartlomiej Zolnierkiewiczca545c12008-04-28 23:44:41 +0200205 tf_outb(tf->lbah, io_ports->lbah_addr);
Bartlomiej Zolnierkiewiczd309e0b2008-04-28 23:44:39 +0200206
207 if (task->tf_flags & IDE_TFLAG_OUT_DEVICE)
Bartlomiej Zolnierkiewiczca545c12008-04-28 23:44:41 +0200208 tf_outb((tf->device & HIHI) | drive->select.all,
209 io_ports->device_addr);
Bartlomiej Zolnierkiewiczd309e0b2008-04-28 23:44:39 +0200210}
211
Bartlomiej Zolnierkiewicz94cd5b62008-04-28 23:44:40 +0200212static void ide_tf_read(ide_drive_t *drive, ide_task_t *task)
Bartlomiej Zolnierkiewiczd309e0b2008-04-28 23:44:39 +0200213{
214 ide_hwif_t *hwif = drive->hwif;
215 struct ide_io_ports *io_ports = &hwif->io_ports;
216 struct ide_taskfile *tf = &task->tf;
Bartlomiej Zolnierkiewiczca545c12008-04-28 23:44:41 +0200217 void (*tf_outb)(u8 addr, unsigned long port);
218 u8 (*tf_inb)(unsigned long port);
219 u8 mmio = (hwif->host_flags & IDE_HFLAG_MMIO) ? 1 : 0;
220
221 if (mmio) {
222 tf_outb = ide_mm_outb;
223 tf_inb = ide_mm_inb;
224 } else {
225 tf_outb = ide_outb;
226 tf_inb = ide_inb;
227 }
Bartlomiej Zolnierkiewiczd309e0b2008-04-28 23:44:39 +0200228
229 if (task->tf_flags & IDE_TFLAG_IN_DATA) {
Bartlomiej Zolnierkiewiczca545c12008-04-28 23:44:41 +0200230 u16 data;
231
232 if (mmio)
233 data = readw((void __iomem *)io_ports->data_addr);
234 else
235 data = inw(io_ports->data_addr);
Bartlomiej Zolnierkiewiczd309e0b2008-04-28 23:44:39 +0200236
237 tf->data = data & 0xff;
238 tf->hob_data = (data >> 8) & 0xff;
239 }
240
241 /* be sure we're looking at the low order bits */
Bartlomiej Zolnierkiewiczff074882008-07-15 21:21:50 +0200242 tf_outb(ATA_DEVCTL_OBS & ~0x80, io_ports->ctl_addr);
Bartlomiej Zolnierkiewiczd309e0b2008-04-28 23:44:39 +0200243
Bartlomiej Zolnierkiewicz92eb4382008-07-23 19:55:53 +0200244 if (task->tf_flags & IDE_TFLAG_IN_FEATURE)
245 tf->feature = tf_inb(io_ports->feature_addr);
Bartlomiej Zolnierkiewiczd309e0b2008-04-28 23:44:39 +0200246 if (task->tf_flags & IDE_TFLAG_IN_NSECT)
Bartlomiej Zolnierkiewiczca545c12008-04-28 23:44:41 +0200247 tf->nsect = tf_inb(io_ports->nsect_addr);
Bartlomiej Zolnierkiewiczd309e0b2008-04-28 23:44:39 +0200248 if (task->tf_flags & IDE_TFLAG_IN_LBAL)
Bartlomiej Zolnierkiewiczca545c12008-04-28 23:44:41 +0200249 tf->lbal = tf_inb(io_ports->lbal_addr);
Bartlomiej Zolnierkiewiczd309e0b2008-04-28 23:44:39 +0200250 if (task->tf_flags & IDE_TFLAG_IN_LBAM)
Bartlomiej Zolnierkiewiczca545c12008-04-28 23:44:41 +0200251 tf->lbam = tf_inb(io_ports->lbam_addr);
Bartlomiej Zolnierkiewiczd309e0b2008-04-28 23:44:39 +0200252 if (task->tf_flags & IDE_TFLAG_IN_LBAH)
Bartlomiej Zolnierkiewiczca545c12008-04-28 23:44:41 +0200253 tf->lbah = tf_inb(io_ports->lbah_addr);
Bartlomiej Zolnierkiewiczd309e0b2008-04-28 23:44:39 +0200254 if (task->tf_flags & IDE_TFLAG_IN_DEVICE)
Bartlomiej Zolnierkiewiczca545c12008-04-28 23:44:41 +0200255 tf->device = tf_inb(io_ports->device_addr);
Bartlomiej Zolnierkiewiczd309e0b2008-04-28 23:44:39 +0200256
257 if (task->tf_flags & IDE_TFLAG_LBA48) {
Bartlomiej Zolnierkiewiczff074882008-07-15 21:21:50 +0200258 tf_outb(ATA_DEVCTL_OBS | 0x80, io_ports->ctl_addr);
Bartlomiej Zolnierkiewiczd309e0b2008-04-28 23:44:39 +0200259
260 if (task->tf_flags & IDE_TFLAG_IN_HOB_FEATURE)
Bartlomiej Zolnierkiewiczca545c12008-04-28 23:44:41 +0200261 tf->hob_feature = tf_inb(io_ports->feature_addr);
Bartlomiej Zolnierkiewiczd309e0b2008-04-28 23:44:39 +0200262 if (task->tf_flags & IDE_TFLAG_IN_HOB_NSECT)
Bartlomiej Zolnierkiewiczca545c12008-04-28 23:44:41 +0200263 tf->hob_nsect = tf_inb(io_ports->nsect_addr);
Bartlomiej Zolnierkiewiczd309e0b2008-04-28 23:44:39 +0200264 if (task->tf_flags & IDE_TFLAG_IN_HOB_LBAL)
Bartlomiej Zolnierkiewiczca545c12008-04-28 23:44:41 +0200265 tf->hob_lbal = tf_inb(io_ports->lbal_addr);
Bartlomiej Zolnierkiewiczd309e0b2008-04-28 23:44:39 +0200266 if (task->tf_flags & IDE_TFLAG_IN_HOB_LBAM)
Bartlomiej Zolnierkiewiczca545c12008-04-28 23:44:41 +0200267 tf->hob_lbam = tf_inb(io_ports->lbam_addr);
Bartlomiej Zolnierkiewiczd309e0b2008-04-28 23:44:39 +0200268 if (task->tf_flags & IDE_TFLAG_IN_HOB_LBAH)
Bartlomiej Zolnierkiewiczca545c12008-04-28 23:44:41 +0200269 tf->hob_lbah = tf_inb(io_ports->lbah_addr);
Bartlomiej Zolnierkiewiczd309e0b2008-04-28 23:44:39 +0200270 }
271}
272
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273/*
274 * Some localbus EIDE interfaces require a special access sequence
275 * when using 32-bit I/O instructions to transfer data. We call this
276 * the "vlb_sync" sequence, which consists of three successive reads
277 * of the sector count register location, with interrupts disabled
278 * to ensure that the reads all happen together.
279 */
Bartlomiej Zolnierkiewicz22cdd6c2008-04-28 23:44:41 +0200280static void ata_vlb_sync(unsigned long port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281{
Bartlomiej Zolnierkiewicz22cdd6c2008-04-28 23:44:41 +0200282 (void)inb(port);
283 (void)inb(port);
284 (void)inb(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285}
286
287/*
288 * This is used for most PIO data transfers *from* the IDE interface
Bartlomiej Zolnierkiewicz9567b342008-04-28 23:44:36 +0200289 *
290 * These routines will round up any request for an odd number of bytes,
291 * so if an odd len is specified, be sure that there's at least one
292 * extra byte allocated for the buffer.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 */
Bartlomiej Zolnierkiewicz92d3ab22008-04-28 23:44:36 +0200294static void ata_input_data(ide_drive_t *drive, struct request *rq,
Bartlomiej Zolnierkiewicz9567b342008-04-28 23:44:36 +0200295 void *buf, unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296{
Bartlomiej Zolnierkiewicz4c3032d2008-04-27 15:38:32 +0200297 ide_hwif_t *hwif = drive->hwif;
298 struct ide_io_ports *io_ports = &hwif->io_ports;
Bartlomiej Zolnierkiewicz9567b342008-04-28 23:44:36 +0200299 unsigned long data_addr = io_ports->data_addr;
Bartlomiej Zolnierkiewicz4c3032d2008-04-27 15:38:32 +0200300 u8 io_32bit = drive->io_32bit;
Bartlomiej Zolnierkiewicz16bb69c2008-04-28 23:44:37 +0200301 u8 mmio = (hwif->host_flags & IDE_HFLAG_MMIO) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302
Bartlomiej Zolnierkiewicz9567b342008-04-28 23:44:36 +0200303 len++;
304
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 if (io_32bit) {
Bartlomiej Zolnierkiewicz16bb69c2008-04-28 23:44:37 +0200306 unsigned long uninitialized_var(flags);
Bartlomiej Zolnierkiewicz23579a22008-04-18 00:46:26 +0200307
Bartlomiej Zolnierkiewicz22cdd6c2008-04-28 23:44:41 +0200308 if ((io_32bit & 2) && !mmio) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 local_irq_save(flags);
Bartlomiej Zolnierkiewicz22cdd6c2008-04-28 23:44:41 +0200310 ata_vlb_sync(io_ports->nsect_addr);
Bartlomiej Zolnierkiewicz16bb69c2008-04-28 23:44:37 +0200311 }
Bartlomiej Zolnierkiewicz9567b342008-04-28 23:44:36 +0200312
Bartlomiej Zolnierkiewicz16bb69c2008-04-28 23:44:37 +0200313 if (mmio)
314 __ide_mm_insl((void __iomem *)data_addr, buf, len / 4);
315 else
316 insl(data_addr, buf, len / 4);
317
Bartlomiej Zolnierkiewicz22cdd6c2008-04-28 23:44:41 +0200318 if ((io_32bit & 2) && !mmio)
Bartlomiej Zolnierkiewicz16bb69c2008-04-28 23:44:37 +0200319 local_irq_restore(flags);
320
321 if ((len & 3) >= 2) {
322 if (mmio)
323 __ide_mm_insw((void __iomem *)data_addr,
324 (u8 *)buf + (len & ~3), 1);
325 else
326 insw(data_addr, (u8 *)buf + (len & ~3), 1);
327 }
328 } else {
329 if (mmio)
330 __ide_mm_insw((void __iomem *)data_addr, buf, len / 2);
331 else
332 insw(data_addr, buf, len / 2);
333 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334}
335
336/*
337 * This is used for most PIO data transfers *to* the IDE interface
338 */
Bartlomiej Zolnierkiewicz92d3ab22008-04-28 23:44:36 +0200339static void ata_output_data(ide_drive_t *drive, struct request *rq,
Bartlomiej Zolnierkiewicz9567b342008-04-28 23:44:36 +0200340 void *buf, unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341{
Bartlomiej Zolnierkiewicz4c3032d2008-04-27 15:38:32 +0200342 ide_hwif_t *hwif = drive->hwif;
343 struct ide_io_ports *io_ports = &hwif->io_ports;
Bartlomiej Zolnierkiewicz9567b342008-04-28 23:44:36 +0200344 unsigned long data_addr = io_ports->data_addr;
Bartlomiej Zolnierkiewicz4c3032d2008-04-27 15:38:32 +0200345 u8 io_32bit = drive->io_32bit;
Bartlomiej Zolnierkiewicz16bb69c2008-04-28 23:44:37 +0200346 u8 mmio = (hwif->host_flags & IDE_HFLAG_MMIO) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347
348 if (io_32bit) {
Bartlomiej Zolnierkiewicz16bb69c2008-04-28 23:44:37 +0200349 unsigned long uninitialized_var(flags);
Bartlomiej Zolnierkiewicz23579a22008-04-18 00:46:26 +0200350
Bartlomiej Zolnierkiewicz22cdd6c2008-04-28 23:44:41 +0200351 if ((io_32bit & 2) && !mmio) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 local_irq_save(flags);
Bartlomiej Zolnierkiewicz22cdd6c2008-04-28 23:44:41 +0200353 ata_vlb_sync(io_ports->nsect_addr);
Bartlomiej Zolnierkiewicz16bb69c2008-04-28 23:44:37 +0200354 }
Bartlomiej Zolnierkiewicz9567b342008-04-28 23:44:36 +0200355
Bartlomiej Zolnierkiewicz16bb69c2008-04-28 23:44:37 +0200356 if (mmio)
357 __ide_mm_outsl((void __iomem *)data_addr, buf, len / 4);
358 else
359 outsl(data_addr, buf, len / 4);
360
Bartlomiej Zolnierkiewicz22cdd6c2008-04-28 23:44:41 +0200361 if ((io_32bit & 2) && !mmio)
Bartlomiej Zolnierkiewicz16bb69c2008-04-28 23:44:37 +0200362 local_irq_restore(flags);
363
364 if ((len & 3) >= 2) {
365 if (mmio)
366 __ide_mm_outsw((void __iomem *)data_addr,
367 (u8 *)buf + (len & ~3), 1);
368 else
369 outsw(data_addr, (u8 *)buf + (len & ~3), 1);
370 }
371 } else {
372 if (mmio)
373 __ide_mm_outsw((void __iomem *)data_addr, buf, len / 2);
374 else
375 outsw(data_addr, buf, len / 2);
376 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377}
378
379void default_hwif_transport(ide_hwif_t *hwif)
380{
Bartlomiej Zolnierkiewiczc6dfa862008-07-23 19:55:51 +0200381 hwif->exec_command = ide_exec_command;
Bartlomiej Zolnierkiewiczb73c7ee2008-07-23 19:55:52 +0200382 hwif->read_status = ide_read_status;
Bartlomiej Zolnierkiewicz1f6d8a02008-07-23 19:55:52 +0200383 hwif->read_altstatus = ide_read_altstatus;
Bartlomiej Zolnierkiewiczb2f951a2008-07-23 19:55:50 +0200384 hwif->read_sff_dma_status = ide_read_sff_dma_status;
385
Bartlomiej Zolnierkiewicz6e6afb32008-07-23 19:55:52 +0200386 hwif->set_irq = ide_set_irq;
387
Bartlomiej Zolnierkiewicz94cd5b62008-04-28 23:44:40 +0200388 hwif->tf_load = ide_tf_load;
389 hwif->tf_read = ide_tf_read;
390
Bartlomiej Zolnierkiewicz9567b342008-04-28 23:44:36 +0200391 hwif->input_data = ata_input_data;
392 hwif->output_data = ata_output_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393}
394
Bartlomiej Zolnierkiewicz92eb4382008-07-23 19:55:53 +0200395u8 ide_read_error(ide_drive_t *drive)
396{
397 ide_task_t task;
398
399 memset(&task, 0, sizeof(task));
400 task.tf_flags = IDE_TFLAG_IN_FEATURE;
401
402 drive->hwif->tf_read(drive, &task);
403
404 return task.tf.error;
405}
406EXPORT_SYMBOL_GPL(ide_read_error);
407
Bartlomiej Zolnierkiewicz18236492008-07-23 19:55:54 +0200408void ide_read_bcount_and_ireason(ide_drive_t *drive, u16 *bcount, u8 *ireason)
409{
410 ide_task_t task;
411
412 memset(&task, 0, sizeof(task));
413 task.tf_flags = IDE_TFLAG_IN_LBAH | IDE_TFLAG_IN_LBAM |
414 IDE_TFLAG_IN_NSECT;
415
416 drive->hwif->tf_read(drive, &task);
417
418 *bcount = (task.tf.lbah << 8) | task.tf.lbam;
419 *ireason = task.tf.nsect & 3;
420}
421EXPORT_SYMBOL_GPL(ide_read_bcount_and_ireason);
422
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423void ide_fix_driveid (struct hd_driveid *id)
424{
425#ifndef __LITTLE_ENDIAN
426# ifdef __BIG_ENDIAN
427 int i;
428 u16 *stringcast;
429
430 id->config = __le16_to_cpu(id->config);
431 id->cyls = __le16_to_cpu(id->cyls);
432 id->reserved2 = __le16_to_cpu(id->reserved2);
433 id->heads = __le16_to_cpu(id->heads);
434 id->track_bytes = __le16_to_cpu(id->track_bytes);
435 id->sector_bytes = __le16_to_cpu(id->sector_bytes);
436 id->sectors = __le16_to_cpu(id->sectors);
437 id->vendor0 = __le16_to_cpu(id->vendor0);
438 id->vendor1 = __le16_to_cpu(id->vendor1);
439 id->vendor2 = __le16_to_cpu(id->vendor2);
440 stringcast = (u16 *)&id->serial_no[0];
441 for (i = 0; i < (20/2); i++)
442 stringcast[i] = __le16_to_cpu(stringcast[i]);
443 id->buf_type = __le16_to_cpu(id->buf_type);
444 id->buf_size = __le16_to_cpu(id->buf_size);
445 id->ecc_bytes = __le16_to_cpu(id->ecc_bytes);
446 stringcast = (u16 *)&id->fw_rev[0];
447 for (i = 0; i < (8/2); i++)
448 stringcast[i] = __le16_to_cpu(stringcast[i]);
449 stringcast = (u16 *)&id->model[0];
450 for (i = 0; i < (40/2); i++)
451 stringcast[i] = __le16_to_cpu(stringcast[i]);
452 id->dword_io = __le16_to_cpu(id->dword_io);
453 id->reserved50 = __le16_to_cpu(id->reserved50);
454 id->field_valid = __le16_to_cpu(id->field_valid);
455 id->cur_cyls = __le16_to_cpu(id->cur_cyls);
456 id->cur_heads = __le16_to_cpu(id->cur_heads);
457 id->cur_sectors = __le16_to_cpu(id->cur_sectors);
458 id->cur_capacity0 = __le16_to_cpu(id->cur_capacity0);
459 id->cur_capacity1 = __le16_to_cpu(id->cur_capacity1);
460 id->lba_capacity = __le32_to_cpu(id->lba_capacity);
461 id->dma_1word = __le16_to_cpu(id->dma_1word);
462 id->dma_mword = __le16_to_cpu(id->dma_mword);
463 id->eide_pio_modes = __le16_to_cpu(id->eide_pio_modes);
464 id->eide_dma_min = __le16_to_cpu(id->eide_dma_min);
465 id->eide_dma_time = __le16_to_cpu(id->eide_dma_time);
466 id->eide_pio = __le16_to_cpu(id->eide_pio);
467 id->eide_pio_iordy = __le16_to_cpu(id->eide_pio_iordy);
468 for (i = 0; i < 2; ++i)
469 id->words69_70[i] = __le16_to_cpu(id->words69_70[i]);
470 for (i = 0; i < 4; ++i)
471 id->words71_74[i] = __le16_to_cpu(id->words71_74[i]);
472 id->queue_depth = __le16_to_cpu(id->queue_depth);
473 for (i = 0; i < 4; ++i)
474 id->words76_79[i] = __le16_to_cpu(id->words76_79[i]);
475 id->major_rev_num = __le16_to_cpu(id->major_rev_num);
476 id->minor_rev_num = __le16_to_cpu(id->minor_rev_num);
477 id->command_set_1 = __le16_to_cpu(id->command_set_1);
478 id->command_set_2 = __le16_to_cpu(id->command_set_2);
479 id->cfsse = __le16_to_cpu(id->cfsse);
480 id->cfs_enable_1 = __le16_to_cpu(id->cfs_enable_1);
481 id->cfs_enable_2 = __le16_to_cpu(id->cfs_enable_2);
482 id->csf_default = __le16_to_cpu(id->csf_default);
483 id->dma_ultra = __le16_to_cpu(id->dma_ultra);
484 id->trseuc = __le16_to_cpu(id->trseuc);
485 id->trsEuc = __le16_to_cpu(id->trsEuc);
486 id->CurAPMvalues = __le16_to_cpu(id->CurAPMvalues);
487 id->mprc = __le16_to_cpu(id->mprc);
488 id->hw_config = __le16_to_cpu(id->hw_config);
489 id->acoustic = __le16_to_cpu(id->acoustic);
490 id->msrqs = __le16_to_cpu(id->msrqs);
491 id->sxfert = __le16_to_cpu(id->sxfert);
492 id->sal = __le16_to_cpu(id->sal);
493 id->spg = __le32_to_cpu(id->spg);
494 id->lba_capacity_2 = __le64_to_cpu(id->lba_capacity_2);
495 for (i = 0; i < 22; i++)
496 id->words104_125[i] = __le16_to_cpu(id->words104_125[i]);
497 id->last_lun = __le16_to_cpu(id->last_lun);
498 id->word127 = __le16_to_cpu(id->word127);
499 id->dlf = __le16_to_cpu(id->dlf);
500 id->csfo = __le16_to_cpu(id->csfo);
501 for (i = 0; i < 26; i++)
502 id->words130_155[i] = __le16_to_cpu(id->words130_155[i]);
503 id->word156 = __le16_to_cpu(id->word156);
504 for (i = 0; i < 3; i++)
505 id->words157_159[i] = __le16_to_cpu(id->words157_159[i]);
506 id->cfa_power = __le16_to_cpu(id->cfa_power);
507 for (i = 0; i < 14; i++)
508 id->words161_175[i] = __le16_to_cpu(id->words161_175[i]);
509 for (i = 0; i < 31; i++)
510 id->words176_205[i] = __le16_to_cpu(id->words176_205[i]);
511 for (i = 0; i < 48; i++)
512 id->words206_254[i] = __le16_to_cpu(id->words206_254[i]);
513 id->integrity_word = __le16_to_cpu(id->integrity_word);
514# else
515# error "Please fix <asm/byteorder.h>"
516# endif
517#endif
518}
519
Bartlomiej Zolnierkiewicz01745112007-11-05 21:42:29 +0100520/*
521 * ide_fixstring() cleans up and (optionally) byte-swaps a text string,
522 * removing leading/trailing blanks and compressing internal blanks.
523 * It is primarily used to tidy up the model name/number fields as
524 * returned by the WIN_[P]IDENTIFY commands.
525 */
526
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527void ide_fixstring (u8 *s, const int bytecount, const int byteswap)
528{
529 u8 *p = s, *end = &s[bytecount & ~1]; /* bytecount must be even */
530
531 if (byteswap) {
532 /* convert from big-endian to host byte order */
533 for (p = end ; p != s;) {
534 unsigned short *pp = (unsigned short *) (p -= 2);
535 *pp = ntohs(*pp);
536 }
537 }
538 /* strip leading blanks */
539 while (s != end && *s == ' ')
540 ++s;
541 /* compress internal blanks and strip trailing blanks */
542 while (s != end && *s) {
543 if (*s++ != ' ' || (s != end && *s && *s != ' '))
544 *p++ = *(s-1);
545 }
546 /* wipe out trailing garbage */
547 while (p != end)
548 *p++ = '\0';
549}
550
551EXPORT_SYMBOL(ide_fixstring);
552
553/*
554 * Needed for PCI irq sharing
555 */
556int drive_is_ready (ide_drive_t *drive)
557{
558 ide_hwif_t *hwif = HWIF(drive);
559 u8 stat = 0;
560
561 if (drive->waiting_for_dma)
Bartlomiej Zolnierkiewicz5e37bdc2008-04-26 22:25:24 +0200562 return hwif->dma_ops->dma_test_irq(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563
564#if 0
565 /* need to guarantee 400ns since last command was issued */
566 udelay(1);
567#endif
568
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 /*
570 * We do a passive status test under shared PCI interrupts on
571 * cards that truly share the ATA side interrupt, but may also share
572 * an interrupt with another pci card/device. We make no assumptions
573 * about possible isa-pnp and pci-pnp issues yet.
574 */
Bartlomiej Zolnierkiewicz4c3032d2008-04-27 15:38:32 +0200575 if (hwif->io_ports.ctl_addr)
Bartlomiej Zolnierkiewicz1f6d8a02008-07-23 19:55:52 +0200576 stat = hwif->read_altstatus(hwif);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 /* Note: this may clear a pending IRQ!! */
Bartlomiej Zolnierkiewiczb73c7ee2008-07-23 19:55:52 +0200579 stat = hwif->read_status(hwif);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580
581 if (stat & BUSY_STAT)
582 /* drive busy: definitely not interrupting */
583 return 0;
584
585 /* drive ready: *might* be interrupting */
586 return 1;
587}
588
589EXPORT_SYMBOL(drive_is_ready);
590
591/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 * This routine busy-waits for the drive status to be not "busy".
593 * It then checks the status for all of the "good" bits and none
594 * of the "bad" bits, and if all is okay it returns 0. All other
Bartlomiej Zolnierkiewicz74af21c2007-10-13 17:47:49 +0200595 * cases return error -- caller may then invoke ide_error().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 *
597 * This routine should get fixed to not hog the cpu during extra long waits..
598 * That could be done by busy-waiting for the first jiffy or two, and then
599 * setting a timer to wake up at half second intervals thereafter,
600 * until timeout is achieved, before timing out.
601 */
Bartlomiej Zolnierkiewiczaedea592007-10-13 17:47:50 +0200602static int __ide_wait_stat(ide_drive_t *drive, u8 good, u8 bad, unsigned long timeout, u8 *rstat)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603{
Bartlomiej Zolnierkiewiczb73c7ee2008-07-23 19:55:52 +0200604 ide_hwif_t *hwif = drive->hwif;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 unsigned long flags;
Bartlomiej Zolnierkiewicz74af21c2007-10-13 17:47:49 +0200606 int i;
607 u8 stat;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608
609 udelay(1); /* spec allows drive 400ns to assert "BUSY" */
Bartlomiej Zolnierkiewiczb73c7ee2008-07-23 19:55:52 +0200610 stat = hwif->read_status(hwif);
Bartlomiej Zolnierkiewiczc47137a2008-02-06 02:57:51 +0100611
612 if (stat & BUSY_STAT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 local_irq_set(flags);
614 timeout += jiffies;
Bartlomiej Zolnierkiewiczb73c7ee2008-07-23 19:55:52 +0200615 while ((stat = hwif->read_status(hwif)) & BUSY_STAT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 if (time_after(jiffies, timeout)) {
617 /*
618 * One last read after the timeout in case
619 * heavy interrupt load made us not make any
620 * progress during the timeout..
621 */
Bartlomiej Zolnierkiewiczb73c7ee2008-07-23 19:55:52 +0200622 stat = hwif->read_status(hwif);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 if (!(stat & BUSY_STAT))
624 break;
625
626 local_irq_restore(flags);
Bartlomiej Zolnierkiewicz74af21c2007-10-13 17:47:49 +0200627 *rstat = stat;
628 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 }
630 }
631 local_irq_restore(flags);
632 }
633 /*
634 * Allow status to settle, then read it again.
635 * A few rare drives vastly violate the 400ns spec here,
636 * so we'll wait up to 10usec for a "good" status
637 * rather than expensively fail things immediately.
638 * This fix courtesy of Matthew Faupel & Niccolo Rigacci.
639 */
640 for (i = 0; i < 10; i++) {
641 udelay(1);
Bartlomiej Zolnierkiewiczb73c7ee2008-07-23 19:55:52 +0200642 stat = hwif->read_status(hwif);
Bartlomiej Zolnierkiewiczc47137a2008-02-06 02:57:51 +0100643
644 if (OK_STAT(stat, good, bad)) {
Bartlomiej Zolnierkiewicz74af21c2007-10-13 17:47:49 +0200645 *rstat = stat;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 return 0;
Bartlomiej Zolnierkiewicz74af21c2007-10-13 17:47:49 +0200647 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 }
Bartlomiej Zolnierkiewicz74af21c2007-10-13 17:47:49 +0200649 *rstat = stat;
650 return -EFAULT;
651}
652
653/*
654 * In case of error returns error value after doing "*startstop = ide_error()".
655 * The caller should return the updated value of "startstop" in this case,
656 * "startstop" is unchanged when the function returns 0.
657 */
658int ide_wait_stat(ide_startstop_t *startstop, ide_drive_t *drive, u8 good, u8 bad, unsigned long timeout)
659{
660 int err;
661 u8 stat;
662
663 /* bail early if we've exceeded max_failures */
664 if (drive->max_failures && (drive->failures > drive->max_failures)) {
665 *startstop = ide_stopped;
666 return 1;
667 }
668
669 err = __ide_wait_stat(drive, good, bad, timeout, &stat);
670
671 if (err) {
672 char *s = (err == -EBUSY) ? "status timeout" : "status error";
673 *startstop = ide_error(drive, s, stat);
674 }
675
676 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677}
678
679EXPORT_SYMBOL(ide_wait_stat);
680
Bartlomiej Zolnierkiewicza5b7e702007-08-20 22:42:56 +0200681/**
682 * ide_in_drive_list - look for drive in black/white list
683 * @id: drive identifier
684 * @drive_table: list to inspect
685 *
686 * Look for a drive in the blacklist and the whitelist tables
687 * Returns 1 if the drive is found in the table.
688 */
689
690int ide_in_drive_list(struct hd_driveid *id, const struct drive_list_entry *drive_table)
691{
692 for ( ; drive_table->id_model; drive_table++)
693 if ((!strcmp(drive_table->id_model, id->model)) &&
694 (!drive_table->id_firmware ||
695 strstr(id->fw_rev, drive_table->id_firmware)))
696 return 1;
697 return 0;
698}
699
Bartlomiej Zolnierkiewiczb0244a02007-08-20 22:42:57 +0200700EXPORT_SYMBOL_GPL(ide_in_drive_list);
701
Bartlomiej Zolnierkiewicza5b7e702007-08-20 22:42:56 +0200702/*
703 * Early UDMA66 devices don't set bit14 to 1, only bit13 is valid.
704 * We list them here and depend on the device side cable detection for them.
Bartlomiej Zolnierkiewicz8588a2b72007-10-26 20:31:16 +0200705 *
706 * Some optical devices with the buggy firmwares have the same problem.
Bartlomiej Zolnierkiewicza5b7e702007-08-20 22:42:56 +0200707 */
708static const struct drive_list_entry ivb_list[] = {
709 { "QUANTUM FIREBALLlct10 05" , "A03.0900" },
Bartlomiej Zolnierkiewicz8588a2b72007-10-26 20:31:16 +0200710 { "TSSTcorp CDDVDW SH-S202J" , "SB00" },
Peter Missele97564f2007-11-27 21:35:57 +0100711 { "TSSTcorp CDDVDW SH-S202J" , "SB01" },
712 { "TSSTcorp CDDVDW SH-S202N" , "SB00" },
713 { "TSSTcorp CDDVDW SH-S202N" , "SB01" },
Alexander Smal3ced5c42008-04-28 23:44:43 +0200714 { "TSSTcorp CDDVDW SH-S202H" , "SB00" },
715 { "TSSTcorp CDDVDW SH-S202H" , "SB01" },
Bartlomiej Zolnierkiewicza5b7e702007-08-20 22:42:56 +0200716 { NULL , NULL }
717};
718
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719/*
720 * All hosts that use the 80c ribbon must use!
721 * The name is derived from upper byte of word 93 and the 80c ribbon.
722 */
723u8 eighty_ninty_three (ide_drive_t *drive)
724{
Bartlomiej Zolnierkiewicz7f8f48a2007-05-10 00:01:10 +0200725 ide_hwif_t *hwif = drive->hwif;
726 struct hd_driveid *id = drive->id;
Bartlomiej Zolnierkiewicza5b7e702007-08-20 22:42:56 +0200727 int ivb = ide_in_drive_list(id, ivb_list);
Bartlomiej Zolnierkiewicz7f8f48a2007-05-10 00:01:10 +0200728
Bartlomiej Zolnierkiewicz49521f92007-07-09 23:17:58 +0200729 if (hwif->cbl == ATA_CBL_PATA40_SHORT)
730 return 1;
731
Bartlomiej Zolnierkiewicza5b7e702007-08-20 22:42:56 +0200732 if (ivb)
733 printk(KERN_DEBUG "%s: skipping word 93 validity check\n",
734 drive->name);
735
George Kibardinb98f8802008-01-10 23:03:42 +0100736 if (ide_dev_is_sata(id) && !ivb)
737 return 1;
738
Bartlomiej Zolnierkiewicza5b7e702007-08-20 22:42:56 +0200739 if (hwif->cbl != ATA_CBL_PATA80 && !ivb)
Bartlomiej Zolnierkiewicz7f8f48a2007-05-10 00:01:10 +0200740 goto no_80w;
Alan Cox1a1276e2006-06-28 04:26:58 -0700741
Bartlomiej Zolnierkiewiczf68d9322007-03-26 23:03:18 +0200742 /*
743 * FIXME:
Bartlomiej Zolnierkiewiczf367bed2008-03-29 19:48:21 +0100744 * - change master/slave IDENTIFY order
Bartlomiej Zolnierkiewicza5b7e702007-08-20 22:42:56 +0200745 * - force bit13 (80c cable present) check also for !ivb devices
Bartlomiej Zolnierkiewiczf68d9322007-03-26 23:03:18 +0200746 * (unless the slave device is pre-ATA3)
747 */
Bartlomiej Zolnierkiewicza5b7e702007-08-20 22:42:56 +0200748 if ((id->hw_config & 0x4000) || (ivb && (id->hw_config & 0x2000)))
Bartlomiej Zolnierkiewicz7f8f48a2007-05-10 00:01:10 +0200749 return 1;
750
751no_80w:
752 if (drive->udma33_warned == 1)
753 return 0;
754
755 printk(KERN_WARNING "%s: %s side 80-wire cable detection failed, "
756 "limiting max speed to UDMA33\n",
Bartlomiej Zolnierkiewicz49521f92007-07-09 23:17:58 +0200757 drive->name,
758 hwif->cbl == ATA_CBL_PATA80 ? "drive" : "host");
Bartlomiej Zolnierkiewicz7f8f48a2007-05-10 00:01:10 +0200759
760 drive->udma33_warned = 1;
761
762 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763}
764
Bartlomiej Zolnierkiewicz8a455132007-10-20 00:32:36 +0200765int ide_driveid_update(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766{
Bartlomiej Zolnierkiewicz8a455132007-10-20 00:32:36 +0200767 ide_hwif_t *hwif = drive->hwif;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 struct hd_driveid *id;
Bartlomiej Zolnierkiewicz8a455132007-10-20 00:32:36 +0200769 unsigned long timeout, flags;
Bartlomiej Zolnierkiewiczc47137a2008-02-06 02:57:51 +0100770 u8 stat;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 /*
773 * Re-read drive->id for possible DMA mode
774 * change (copied from ide-probe.c)
775 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776
777 SELECT_MASK(drive, 1);
Bartlomiej Zolnierkiewicz6e6afb32008-07-23 19:55:52 +0200778 hwif->set_irq(hwif, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 msleep(50);
Bartlomiej Zolnierkiewiczc6dfa862008-07-23 19:55:51 +0200780 hwif->exec_command(hwif, WIN_IDENTIFY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 timeout = jiffies + WAIT_WORSTCASE;
782 do {
783 if (time_after(jiffies, timeout)) {
784 SELECT_MASK(drive, 0);
785 return 0; /* drive timed-out */
786 }
Bartlomiej Zolnierkiewiczc47137a2008-02-06 02:57:51 +0100787
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 msleep(50); /* give drive a breather */
Bartlomiej Zolnierkiewicz1f6d8a02008-07-23 19:55:52 +0200789 stat = hwif->read_altstatus(hwif);
Bartlomiej Zolnierkiewiczc47137a2008-02-06 02:57:51 +0100790 } while (stat & BUSY_STAT);
791
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 msleep(50); /* wait for IRQ and DRQ_STAT */
Bartlomiej Zolnierkiewiczb73c7ee2008-07-23 19:55:52 +0200793 stat = hwif->read_status(hwif);
Bartlomiej Zolnierkiewiczc47137a2008-02-06 02:57:51 +0100794
795 if (!OK_STAT(stat, DRQ_STAT, BAD_R_STAT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 SELECT_MASK(drive, 0);
797 printk("%s: CHECK for good STATUS\n", drive->name);
798 return 0;
799 }
800 local_irq_save(flags);
801 SELECT_MASK(drive, 0);
802 id = kmalloc(SECTOR_WORDS*4, GFP_ATOMIC);
803 if (!id) {
804 local_irq_restore(flags);
805 return 0;
806 }
Bartlomiej Zolnierkiewicz9567b342008-04-28 23:44:36 +0200807 hwif->input_data(drive, NULL, id, SECTOR_SIZE);
Bartlomiej Zolnierkiewiczb73c7ee2008-07-23 19:55:52 +0200808 (void)hwif->read_status(hwif); /* clear drive IRQ */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 local_irq_enable();
810 local_irq_restore(flags);
811 ide_fix_driveid(id);
812 if (id) {
813 drive->id->dma_ultra = id->dma_ultra;
814 drive->id->dma_mword = id->dma_mword;
815 drive->id->dma_1word = id->dma_1word;
816 /* anything more ? */
817 kfree(id);
Bartlomiej Zolnierkiewicz3ab7efe2007-12-12 23:31:58 +0100818
819 if (drive->using_dma && ide_id_dma_bug(drive))
820 ide_dma_off(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 }
822
823 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824}
825
Bartlomiej Zolnierkiewicz74af21c2007-10-13 17:47:49 +0200826int ide_config_drive_speed(ide_drive_t *drive, u8 speed)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827{
Bartlomiej Zolnierkiewicz74af21c2007-10-13 17:47:49 +0200828 ide_hwif_t *hwif = drive->hwif;
Sergei Shtylyov89613e62007-11-27 21:35:52 +0100829 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 u8 stat;
Bartlomiej Zolnierkiewicz59be2c82008-07-23 19:55:52 +0200831 ide_task_t task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833#ifdef CONFIG_BLK_DEV_IDEDMA
Bartlomiej Zolnierkiewicz5e37bdc2008-04-26 22:25:24 +0200834 if (hwif->dma_ops) /* check if host supports DMA */
835 hwif->dma_ops->dma_host_set(drive, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836#endif
837
Sergei Shtylyov89613e62007-11-27 21:35:52 +0100838 /* Skip setting PIO flow-control modes on pre-EIDE drives */
839 if ((speed & 0xf8) == XFER_PIO_0 && !(drive->id->capability & 0x08))
840 goto skip;
841
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 /*
843 * Don't use ide_wait_cmd here - it will
844 * attempt to set_geometry and recalibrate,
845 * but for some reason these don't work at
846 * this point (lost interrupt).
847 */
848 /*
849 * Select the drive, and issue the SETFEATURES command
850 */
851 disable_irq_nosync(hwif->irq);
852
853 /*
854 * FIXME: we race against the running IRQ here if
855 * this is called from non IRQ context. If we use
856 * disable_irq() we hang on the error path. Work
857 * is needed.
858 */
859
860 udelay(1);
861 SELECT_DRIVE(drive);
862 SELECT_MASK(drive, 0);
863 udelay(1);
Bartlomiej Zolnierkiewicz6e6afb32008-07-23 19:55:52 +0200864 hwif->set_irq(hwif, 0);
Bartlomiej Zolnierkiewicz59be2c82008-07-23 19:55:52 +0200865
866 memset(&task, 0, sizeof(task));
867 task.tf_flags = IDE_TFLAG_OUT_FEATURE | IDE_TFLAG_OUT_NSECT;
868 task.tf.feature = SETFEATURES_XFER;
869 task.tf.nsect = speed;
870
871 hwif->tf_load(drive, &task);
872
Bartlomiej Zolnierkiewiczc6dfa862008-07-23 19:55:51 +0200873 hwif->exec_command(hwif, WIN_SETFEATURES);
Bartlomiej Zolnierkiewicz59be2c82008-07-23 19:55:52 +0200874
Bartlomiej Zolnierkiewicz81ca6912008-01-26 20:13:08 +0100875 if (drive->quirk_list == 2)
Bartlomiej Zolnierkiewicz6e6afb32008-07-23 19:55:52 +0200876 hwif->set_irq(hwif, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877
Bartlomiej Zolnierkiewicz74af21c2007-10-13 17:47:49 +0200878 error = __ide_wait_stat(drive, drive->ready_stat,
879 BUSY_STAT|DRQ_STAT|ERR_STAT,
880 WAIT_CMD, &stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881
882 SELECT_MASK(drive, 0);
883
884 enable_irq(hwif->irq);
885
886 if (error) {
887 (void) ide_dump_status(drive, "set_drive_speed_status", stat);
888 return error;
889 }
890
891 drive->id->dma_ultra &= ~0xFF00;
892 drive->id->dma_mword &= ~0x0F00;
893 drive->id->dma_1word &= ~0x0F00;
894
Sergei Shtylyov89613e62007-11-27 21:35:52 +0100895 skip:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896#ifdef CONFIG_BLK_DEV_IDEDMA
Bartlomiej Zolnierkiewiczf37aaf92008-01-26 20:13:02 +0100897 if ((speed >= XFER_SW_DMA_0 || (hwif->host_flags & IDE_HFLAG_VDMA)) &&
898 drive->using_dma)
Bartlomiej Zolnierkiewicz5e37bdc2008-04-26 22:25:24 +0200899 hwif->dma_ops->dma_host_set(drive, 1);
900 else if (hwif->dma_ops) /* check if host supports DMA */
Bartlomiej Zolnierkiewicz4a546e02008-01-26 20:13:01 +0100901 ide_dma_off_quietly(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902#endif
903
904 switch(speed) {
905 case XFER_UDMA_7: drive->id->dma_ultra |= 0x8080; break;
906 case XFER_UDMA_6: drive->id->dma_ultra |= 0x4040; break;
907 case XFER_UDMA_5: drive->id->dma_ultra |= 0x2020; break;
908 case XFER_UDMA_4: drive->id->dma_ultra |= 0x1010; break;
909 case XFER_UDMA_3: drive->id->dma_ultra |= 0x0808; break;
910 case XFER_UDMA_2: drive->id->dma_ultra |= 0x0404; break;
911 case XFER_UDMA_1: drive->id->dma_ultra |= 0x0202; break;
912 case XFER_UDMA_0: drive->id->dma_ultra |= 0x0101; break;
913 case XFER_MW_DMA_2: drive->id->dma_mword |= 0x0404; break;
914 case XFER_MW_DMA_1: drive->id->dma_mword |= 0x0202; break;
915 case XFER_MW_DMA_0: drive->id->dma_mword |= 0x0101; break;
916 case XFER_SW_DMA_2: drive->id->dma_1word |= 0x0404; break;
917 case XFER_SW_DMA_1: drive->id->dma_1word |= 0x0202; break;
918 case XFER_SW_DMA_0: drive->id->dma_1word |= 0x0101; break;
919 default: break;
920 }
921 if (!drive->init_speed)
922 drive->init_speed = speed;
923 drive->current_speed = speed;
924 return error;
925}
926
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927/*
928 * This should get invoked any time we exit the driver to
929 * wait for an interrupt response from a drive. handler() points
930 * at the appropriate code to handle the next interrupt, and a
931 * timer is started to prevent us from waiting forever in case
932 * something goes wrong (see the ide_timer_expiry() handler later on).
933 *
934 * See also ide_execute_command
935 */
936static void __ide_set_handler (ide_drive_t *drive, ide_handler_t *handler,
937 unsigned int timeout, ide_expiry_t *expiry)
938{
939 ide_hwgroup_t *hwgroup = HWGROUP(drive);
940
Sergei Shtylyovd30a4262008-02-11 00:32:12 +0100941 BUG_ON(hwgroup->handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 hwgroup->handler = handler;
943 hwgroup->expiry = expiry;
944 hwgroup->timer.expires = jiffies + timeout;
Sergei Shtylyovd30a4262008-02-11 00:32:12 +0100945 hwgroup->req_gen_timer = hwgroup->req_gen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 add_timer(&hwgroup->timer);
947}
948
949void ide_set_handler (ide_drive_t *drive, ide_handler_t *handler,
950 unsigned int timeout, ide_expiry_t *expiry)
951{
952 unsigned long flags;
953 spin_lock_irqsave(&ide_lock, flags);
954 __ide_set_handler(drive, handler, timeout, expiry);
955 spin_unlock_irqrestore(&ide_lock, flags);
956}
957
958EXPORT_SYMBOL(ide_set_handler);
959
960/**
961 * ide_execute_command - execute an IDE command
962 * @drive: IDE drive to issue the command against
963 * @command: command byte to write
964 * @handler: handler for next phase
965 * @timeout: timeout for command
966 * @expiry: handler to run on timeout
967 *
968 * Helper function to issue an IDE command. This handles the
969 * atomicity requirements, command timing and ensures that the
970 * handler and IRQ setup do not race. All IDE command kick off
971 * should go via this function or do equivalent locking.
972 */
Bartlomiej Zolnierkiewiczcd2a2d92008-01-25 22:17:06 +0100973
974void ide_execute_command(ide_drive_t *drive, u8 cmd, ide_handler_t *handler,
975 unsigned timeout, ide_expiry_t *expiry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976{
977 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 ide_hwif_t *hwif = HWIF(drive);
Bartlomiej Zolnierkiewicz629f9442008-02-02 19:56:46 +0100979
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 spin_lock_irqsave(&ide_lock, flags);
Bartlomiej Zolnierkiewicz629f9442008-02-02 19:56:46 +0100981 __ide_set_handler(drive, handler, timeout, expiry);
Bartlomiej Zolnierkiewiczc6dfa862008-07-23 19:55:51 +0200982 hwif->exec_command(hwif, cmd);
Bartlomiej Zolnierkiewicz629f9442008-02-02 19:56:46 +0100983 /*
984 * Drive takes 400nS to respond, we must avoid the IRQ being
985 * serviced before that.
986 *
987 * FIXME: we could skip this delay with care on non shared devices
988 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 ndelay(400);
990 spin_unlock_irqrestore(&ide_lock, flags);
991}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992EXPORT_SYMBOL(ide_execute_command);
993
Bartlomiej Zolnierkiewicz1fc14252008-04-28 23:44:39 +0200994void ide_execute_pkt_cmd(ide_drive_t *drive)
995{
996 ide_hwif_t *hwif = drive->hwif;
997 unsigned long flags;
998
999 spin_lock_irqsave(&ide_lock, flags);
Bartlomiej Zolnierkiewiczc6dfa862008-07-23 19:55:51 +02001000 hwif->exec_command(hwif, WIN_PACKETCMD);
Bartlomiej Zolnierkiewicz1fc14252008-04-28 23:44:39 +02001001 ndelay(400);
1002 spin_unlock_irqrestore(&ide_lock, flags);
1003}
1004EXPORT_SYMBOL_GPL(ide_execute_pkt_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005
Elias Oltmanns64a8f002008-07-16 20:33:48 +02001006static inline void ide_complete_drive_reset(ide_drive_t *drive, int err)
Elias Oltmanns79e36a92008-07-16 20:33:48 +02001007{
1008 struct request *rq = drive->hwif->hwgroup->rq;
1009
1010 if (rq && blk_special_request(rq) && rq->cmd[0] == REQ_DRIVE_RESET)
Elias Oltmanns64a8f002008-07-16 20:33:48 +02001011 ide_end_request(drive, err ? err : 1, 0);
Elias Oltmanns79e36a92008-07-16 20:33:48 +02001012}
1013
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014/* needed below */
1015static ide_startstop_t do_reset1 (ide_drive_t *, int);
1016
1017/*
1018 * atapi_reset_pollfunc() gets invoked to poll the interface for completion every 50ms
1019 * during an atapi drive reset operation. If the drive has not yet responded,
1020 * and we have not yet hit our maximum waiting time, then the timer is restarted
1021 * for another 50ms.
1022 */
1023static ide_startstop_t atapi_reset_pollfunc (ide_drive_t *drive)
1024{
Bartlomiej Zolnierkiewiczb73c7ee2008-07-23 19:55:52 +02001025 ide_hwif_t *hwif = drive->hwif;
1026 ide_hwgroup_t *hwgroup = hwif->hwgroup;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 u8 stat;
1028
1029 SELECT_DRIVE(drive);
1030 udelay (10);
Bartlomiej Zolnierkiewiczb73c7ee2008-07-23 19:55:52 +02001031 stat = hwif->read_status(hwif);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032
Bartlomiej Zolnierkiewiczc47137a2008-02-06 02:57:51 +01001033 if (OK_STAT(stat, 0, BUSY_STAT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 printk("%s: ATAPI reset complete\n", drive->name);
Bartlomiej Zolnierkiewiczc47137a2008-02-06 02:57:51 +01001035 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 if (time_before(jiffies, hwgroup->poll_timeout)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 ide_set_handler(drive, &atapi_reset_pollfunc, HZ/20, NULL);
1038 /* continue polling */
1039 return ide_started;
1040 }
1041 /* end of polling */
1042 hwgroup->polling = 0;
1043 printk("%s: ATAPI reset timed-out, status=0x%02x\n",
1044 drive->name, stat);
1045 /* do it the old fashioned way */
1046 return do_reset1(drive, 1);
1047 }
1048 /* done polling */
1049 hwgroup->polling = 0;
Elias Oltmanns64a8f002008-07-16 20:33:48 +02001050 ide_complete_drive_reset(drive, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 return ide_stopped;
1052}
1053
1054/*
1055 * reset_pollfunc() gets invoked to poll the interface for completion every 50ms
1056 * during an ide reset operation. If the drives have not yet responded,
1057 * and we have not yet hit our maximum waiting time, then the timer is restarted
1058 * for another 50ms.
1059 */
1060static ide_startstop_t reset_pollfunc (ide_drive_t *drive)
1061{
1062 ide_hwgroup_t *hwgroup = HWGROUP(drive);
1063 ide_hwif_t *hwif = HWIF(drive);
Bartlomiej Zolnierkiewiczac95bee2008-04-26 22:25:14 +02001064 const struct ide_port_ops *port_ops = hwif->port_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 u8 tmp;
Elias Oltmanns64a8f002008-07-16 20:33:48 +02001066 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067
Bartlomiej Zolnierkiewiczac95bee2008-04-26 22:25:14 +02001068 if (port_ops && port_ops->reset_poll) {
Elias Oltmanns64a8f002008-07-16 20:33:48 +02001069 err = port_ops->reset_poll(drive);
1070 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 printk(KERN_ERR "%s: host reset_poll failure for %s.\n",
1072 hwif->name, drive->name);
Elias Oltmanns79e36a92008-07-16 20:33:48 +02001073 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 }
1075 }
1076
Bartlomiej Zolnierkiewiczb73c7ee2008-07-23 19:55:52 +02001077 tmp = hwif->read_status(hwif);
Bartlomiej Zolnierkiewiczc47137a2008-02-06 02:57:51 +01001078
1079 if (!OK_STAT(tmp, 0, BUSY_STAT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 if (time_before(jiffies, hwgroup->poll_timeout)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 ide_set_handler(drive, &reset_pollfunc, HZ/20, NULL);
1082 /* continue polling */
1083 return ide_started;
1084 }
1085 printk("%s: reset timed-out, status=0x%02x\n", hwif->name, tmp);
1086 drive->failures++;
Elias Oltmanns64a8f002008-07-16 20:33:48 +02001087 err = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 } else {
1089 printk("%s: reset: ", hwif->name);
Bartlomiej Zolnierkiewicz64a57fe2008-02-06 02:57:51 +01001090 tmp = ide_read_error(drive);
1091
1092 if (tmp == 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 printk("success\n");
1094 drive->failures = 0;
1095 } else {
1096 drive->failures++;
1097 printk("master: ");
1098 switch (tmp & 0x7f) {
1099 case 1: printk("passed");
1100 break;
1101 case 2: printk("formatter device error");
1102 break;
1103 case 3: printk("sector buffer error");
1104 break;
1105 case 4: printk("ECC circuitry error");
1106 break;
1107 case 5: printk("controlling MPU error");
1108 break;
1109 default:printk("error (0x%02x?)", tmp);
1110 }
1111 if (tmp & 0x80)
1112 printk("; slave: failed");
1113 printk("\n");
Elias Oltmanns64a8f002008-07-16 20:33:48 +02001114 err = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 }
1116 }
Elias Oltmanns79e36a92008-07-16 20:33:48 +02001117out:
Elias Oltmanns64a8f002008-07-16 20:33:48 +02001118 hwgroup->polling = 0; /* done polling */
1119 ide_complete_drive_reset(drive, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 return ide_stopped;
1121}
1122
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123static void ide_disk_pre_reset(ide_drive_t *drive)
1124{
1125 int legacy = (drive->id->cfs_enable_2 & 0x0400) ? 0 : 1;
1126
1127 drive->special.all = 0;
1128 drive->special.b.set_geometry = legacy;
1129 drive->special.b.recalibrate = legacy;
Bartlomiej Zolnierkiewicz4ee06b72008-01-25 22:17:08 +01001130 drive->mult_count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 if (!drive->keep_settings && !drive->using_dma)
1132 drive->mult_req = 0;
1133 if (drive->mult_req != drive->mult_count)
1134 drive->special.b.set_multmode = 1;
1135}
1136
1137static void pre_reset(ide_drive_t *drive)
1138{
Bartlomiej Zolnierkiewiczac95bee2008-04-26 22:25:14 +02001139 const struct ide_port_ops *port_ops = drive->hwif->port_ops;
1140
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 if (drive->media == ide_disk)
1142 ide_disk_pre_reset(drive);
1143 else
1144 drive->post_reset = 1;
1145
Bartlomiej Zolnierkiewicz99ffbe02008-02-02 19:56:47 +01001146 if (drive->using_dma) {
1147 if (drive->crc_count)
Bartlomiej Zolnierkiewicz578cfa02008-02-02 19:56:47 +01001148 ide_check_dma_crc(drive);
Bartlomiej Zolnierkiewicz99ffbe02008-02-02 19:56:47 +01001149 else
1150 ide_dma_off(drive);
1151 }
1152
1153 if (!drive->keep_settings) {
1154 if (!drive->using_dma) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 drive->unmask = 0;
1156 drive->io_32bit = 0;
1157 }
1158 return;
1159 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160
Bartlomiej Zolnierkiewiczac95bee2008-04-26 22:25:14 +02001161 if (port_ops && port_ops->pre_reset)
1162 port_ops->pre_reset(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163
Suleiman Souhlal513daad2007-03-26 23:03:20 +02001164 if (drive->current_speed != 0xff)
1165 drive->desired_speed = drive->current_speed;
1166 drive->current_speed = 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167}
1168
1169/*
1170 * do_reset1() attempts to recover a confused drive by resetting it.
1171 * Unfortunately, resetting a disk drive actually resets all devices on
1172 * the same interface, so it can really be thought of as resetting the
1173 * interface rather than resetting the drive.
1174 *
1175 * ATAPI devices have their own reset mechanism which allows them to be
1176 * individually reset without clobbering other devices on the same interface.
1177 *
1178 * Unfortunately, the IDE interface does not generate an interrupt to let
1179 * us know when the reset operation has finished, so we must poll for this.
1180 * Equally poor, though, is the fact that this may a very long time to complete,
1181 * (up to 30 seconds worstcase). So, instead of busy-waiting here for it,
1182 * we set a timer to poll at 50ms intervals.
1183 */
1184static ide_startstop_t do_reset1 (ide_drive_t *drive, int do_not_try_atapi)
1185{
1186 unsigned int unit;
1187 unsigned long flags;
1188 ide_hwif_t *hwif;
1189 ide_hwgroup_t *hwgroup;
Bartlomiej Zolnierkiewicz4c3032d2008-04-27 15:38:32 +02001190 struct ide_io_ports *io_ports;
Bartlomiej Zolnierkiewiczac95bee2008-04-26 22:25:14 +02001191 const struct ide_port_ops *port_ops;
Bartlomiej Zolnierkiewicz23579a22008-04-18 00:46:26 +02001192
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 spin_lock_irqsave(&ide_lock, flags);
1194 hwif = HWIF(drive);
1195 hwgroup = HWGROUP(drive);
1196
Bartlomiej Zolnierkiewicz4c3032d2008-04-27 15:38:32 +02001197 io_ports = &hwif->io_ports;
1198
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 /* We must not reset with running handlers */
Eric Sesterhenn125e1872006-06-23 02:06:06 -07001200 BUG_ON(hwgroup->handler != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201
1202 /* For an ATAPI device, first try an ATAPI SRST. */
1203 if (drive->media != ide_disk && !do_not_try_atapi) {
1204 pre_reset(drive);
1205 SELECT_DRIVE(drive);
1206 udelay (20);
Bartlomiej Zolnierkiewiczc6dfa862008-07-23 19:55:51 +02001207 hwif->exec_command(hwif, WIN_SRST);
Alan Cox68ad9912005-06-27 15:24:25 -07001208 ndelay(400);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 hwgroup->poll_timeout = jiffies + WAIT_WORSTCASE;
1210 hwgroup->polling = 1;
1211 __ide_set_handler(drive, &atapi_reset_pollfunc, HZ/20, NULL);
1212 spin_unlock_irqrestore(&ide_lock, flags);
1213 return ide_started;
1214 }
1215
1216 /*
1217 * First, reset any device state data we were maintaining
1218 * for any of the drives on this interface.
1219 */
1220 for (unit = 0; unit < MAX_DRIVES; ++unit)
1221 pre_reset(&hwif->drives[unit]);
1222
Bartlomiej Zolnierkiewicz4c3032d2008-04-27 15:38:32 +02001223 if (io_ports->ctl_addr == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 spin_unlock_irqrestore(&ide_lock, flags);
Elias Oltmanns64a8f002008-07-16 20:33:48 +02001225 ide_complete_drive_reset(drive, -ENXIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 return ide_stopped;
1227 }
1228
1229 /*
1230 * Note that we also set nIEN while resetting the device,
1231 * to mask unwanted interrupts from the interface during the reset.
1232 * However, due to the design of PC hardware, this will cause an
1233 * immediate interrupt due to the edge transition it produces.
1234 * This single interrupt gives us a "fast poll" for drives that
1235 * recover from reset very quickly, saving us the first 50ms wait time.
Bartlomiej Zolnierkiewicz6e6afb32008-07-23 19:55:52 +02001236 *
1237 * TODO: add ->softreset method and stop abusing ->set_irq
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 */
1239 /* set SRST and nIEN */
Bartlomiej Zolnierkiewicz6e6afb32008-07-23 19:55:52 +02001240 hwif->set_irq(hwif, 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 /* more than enough time */
1242 udelay(10);
Bartlomiej Zolnierkiewicz6e6afb32008-07-23 19:55:52 +02001243 /* clear SRST, leave nIEN (unless device is on the quirk list) */
1244 hwif->set_irq(hwif, drive->quirk_list == 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 /* more than enough time */
1246 udelay(10);
1247 hwgroup->poll_timeout = jiffies + WAIT_WORSTCASE;
1248 hwgroup->polling = 1;
1249 __ide_set_handler(drive, &reset_pollfunc, HZ/20, NULL);
1250
1251 /*
1252 * Some weird controller like resetting themselves to a strange
1253 * state when the disks are reset this way. At least, the Winbond
1254 * 553 documentation says that
1255 */
Bartlomiej Zolnierkiewiczac95bee2008-04-26 22:25:14 +02001256 port_ops = hwif->port_ops;
1257 if (port_ops && port_ops->resetproc)
1258 port_ops->resetproc(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259
1260 spin_unlock_irqrestore(&ide_lock, flags);
1261 return ide_started;
1262}
1263
1264/*
1265 * ide_do_reset() is the entry point to the drive/interface reset code.
1266 */
1267
1268ide_startstop_t ide_do_reset (ide_drive_t *drive)
1269{
1270 return do_reset1(drive, 0);
1271}
1272
1273EXPORT_SYMBOL(ide_do_reset);
1274
1275/*
1276 * ide_wait_not_busy() waits for the currently selected device on the hwif
Bartlomiej Zolnierkiewicz9d501522008-02-01 23:09:36 +01001277 * to report a non-busy status, see comments in ide_probe_port().
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 */
1279int ide_wait_not_busy(ide_hwif_t *hwif, unsigned long timeout)
1280{
1281 u8 stat = 0;
1282
1283 while(timeout--) {
1284 /*
1285 * Turn this into a schedule() sleep once I'm sure
1286 * about locking issues (2.5 work ?).
1287 */
1288 mdelay(1);
Bartlomiej Zolnierkiewiczb73c7ee2008-07-23 19:55:52 +02001289 stat = hwif->read_status(hwif);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 if ((stat & BUSY_STAT) == 0)
1291 return 0;
1292 /*
1293 * Assume a value of 0xff means nothing is connected to
1294 * the interface and it doesn't implement the pull-down
1295 * resistor on D7.
1296 */
1297 if (stat == 0xff)
1298 return -ENODEV;
Ingo Molnar6842f8c2006-02-03 03:04:55 -08001299 touch_softlockup_watchdog();
Michal Schmidt1e862402006-07-30 03:03:29 -07001300 touch_nmi_watchdog();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 }
1302 return -EBUSY;
1303}
1304
1305EXPORT_SYMBOL_GPL(ide_wait_not_busy);
1306