ide: change order of register access in ide_config_drive_speed()
[linux-2.6-block.git] / drivers / ide / ide-iops.c
CommitLineData
1da177e4 1/*
1da177e4
LT
2 * Copyright (C) 2000-2002 Andre Hedrick <andre@linux-ide.org>
3 * Copyright (C) 2003 Red Hat <alan@redhat.com>
4 *
5 */
6
1da177e4
LT
7#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>
1e86240f 24#include <linux/nmi.h>
1da177e4
LT
25
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
1da177e4
LT
40static void ide_outb (u8 val, unsigned long port)
41{
42 outb(val, port);
43}
44
f8c4bd0a 45static void ide_outbsync(ide_hwif_t *hwif, u8 addr, unsigned long port)
1da177e4
LT
46{
47 outb(addr, port);
48}
49
1da177e4
LT
50void default_hwif_iops (ide_hwif_t *hwif)
51{
52 hwif->OUTB = ide_outb;
53 hwif->OUTBSYNC = ide_outbsync;
1da177e4 54 hwif->INB = ide_inb;
1da177e4
LT
55}
56
1da177e4
LT
57/*
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
1da177e4
LT
66static void ide_mm_outb (u8 value, unsigned long port)
67{
68 writeb(value, (void __iomem *) port);
69}
70
f8c4bd0a 71static void ide_mm_outbsync(ide_hwif_t *hwif, u8 value, unsigned long port)
1da177e4
LT
72{
73 writeb(value, (void __iomem *) port);
74}
75
1da177e4
LT
76void 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;
1da177e4 82 hwif->INB = ide_mm_inb;
1da177e4
LT
83}
84
85EXPORT_SYMBOL(default_hwif_mmiops);
86
1da177e4
LT
87void SELECT_DRIVE (ide_drive_t *drive)
88{
23579a2a 89 ide_hwif_t *hwif = drive->hwif;
ac95beed 90 const struct ide_port_ops *port_ops = hwif->port_ops;
23579a2a 91
ac95beed
BZ
92 if (port_ops && port_ops->selectproc)
93 port_ops->selectproc(drive);
23579a2a 94
4c3032d8 95 hwif->OUTB(drive->select.all, hwif->io_ports.device_addr);
1da177e4
LT
96}
97
ed4af48f 98void SELECT_MASK(ide_drive_t *drive, int mask)
1da177e4 99{
ac95beed
BZ
100 const struct ide_port_ops *port_ops = drive->hwif->port_ops;
101
102 if (port_ops && port_ops->maskproc)
103 port_ops->maskproc(drive, mask);
1da177e4
LT
104}
105
c6dfa867
BZ
106static void ide_exec_command(ide_hwif_t *hwif, u8 cmd)
107{
108 if (hwif->host_flags & IDE_HFLAG_MMIO)
109 writeb(cmd, (void __iomem *)hwif->io_ports.command_addr);
110 else
111 outb(cmd, hwif->io_ports.command_addr);
112}
113
b73c7ee2
BZ
114static u8 ide_read_status(ide_hwif_t *hwif)
115{
116 if (hwif->host_flags & IDE_HFLAG_MMIO)
117 return readb((void __iomem *)hwif->io_ports.status_addr);
118 else
119 return inb(hwif->io_ports.status_addr);
120}
121
1f6d8a0f
BZ
122static u8 ide_read_altstatus(ide_hwif_t *hwif)
123{
124 if (hwif->host_flags & IDE_HFLAG_MMIO)
125 return readb((void __iomem *)hwif->io_ports.ctl_addr);
126 else
127 return inb(hwif->io_ports.ctl_addr);
128}
129
b2f951aa
BZ
130static u8 ide_read_sff_dma_status(ide_hwif_t *hwif)
131{
132 if (hwif->host_flags & IDE_HFLAG_MMIO)
cab7f8ed 133 return readb((void __iomem *)(hwif->dma_base + ATA_DMA_STATUS));
b2f951aa 134 else
cab7f8ed 135 return inb(hwif->dma_base + ATA_DMA_STATUS);
b2f951aa
BZ
136}
137
6e6afb3b
BZ
138static void ide_set_irq(ide_hwif_t *hwif, int on)
139{
140 u8 ctl = ATA_DEVCTL_OBS;
141
142 if (on == 4) { /* hack for SRST */
143 ctl |= 4;
144 on &= ~4;
145 }
146
147 ctl |= on ? 0 : 2;
148
149 if (hwif->host_flags & IDE_HFLAG_MMIO)
150 writeb(ctl, (void __iomem *)hwif->io_ports.ctl_addr);
151 else
152 outb(ctl, hwif->io_ports.ctl_addr);
153}
154
94cd5b62 155static void ide_tf_load(ide_drive_t *drive, ide_task_t *task)
d309e0bb
BZ
156{
157 ide_hwif_t *hwif = drive->hwif;
158 struct ide_io_ports *io_ports = &hwif->io_ports;
159 struct ide_taskfile *tf = &task->tf;
ca545c1e
BZ
160 void (*tf_outb)(u8 addr, unsigned long port);
161 u8 mmio = (hwif->host_flags & IDE_HFLAG_MMIO) ? 1 : 0;
d309e0bb
BZ
162 u8 HIHI = (task->tf_flags & IDE_TFLAG_LBA48) ? 0xE0 : 0xEF;
163
ca545c1e
BZ
164 if (mmio)
165 tf_outb = ide_mm_outb;
166 else
167 tf_outb = ide_outb;
168
d309e0bb
BZ
169 if (task->tf_flags & IDE_TFLAG_FLAGGED)
170 HIHI = 0xFF;
171
ca545c1e
BZ
172 if (task->tf_flags & IDE_TFLAG_OUT_DATA) {
173 u16 data = (tf->hob_data << 8) | tf->data;
174
175 if (mmio)
176 writew(data, (void __iomem *)io_ports->data_addr);
177 else
178 outw(data, io_ports->data_addr);
179 }
d309e0bb
BZ
180
181 if (task->tf_flags & IDE_TFLAG_OUT_HOB_FEATURE)
ca545c1e 182 tf_outb(tf->hob_feature, io_ports->feature_addr);
d309e0bb 183 if (task->tf_flags & IDE_TFLAG_OUT_HOB_NSECT)
ca545c1e 184 tf_outb(tf->hob_nsect, io_ports->nsect_addr);
d309e0bb 185 if (task->tf_flags & IDE_TFLAG_OUT_HOB_LBAL)
ca545c1e 186 tf_outb(tf->hob_lbal, io_ports->lbal_addr);
d309e0bb 187 if (task->tf_flags & IDE_TFLAG_OUT_HOB_LBAM)
ca545c1e 188 tf_outb(tf->hob_lbam, io_ports->lbam_addr);
d309e0bb 189 if (task->tf_flags & IDE_TFLAG_OUT_HOB_LBAH)
ca545c1e 190 tf_outb(tf->hob_lbah, io_ports->lbah_addr);
d309e0bb
BZ
191
192 if (task->tf_flags & IDE_TFLAG_OUT_FEATURE)
ca545c1e 193 tf_outb(tf->feature, io_ports->feature_addr);
d309e0bb 194 if (task->tf_flags & IDE_TFLAG_OUT_NSECT)
ca545c1e 195 tf_outb(tf->nsect, io_ports->nsect_addr);
d309e0bb 196 if (task->tf_flags & IDE_TFLAG_OUT_LBAL)
ca545c1e 197 tf_outb(tf->lbal, io_ports->lbal_addr);
d309e0bb 198 if (task->tf_flags & IDE_TFLAG_OUT_LBAM)
ca545c1e 199 tf_outb(tf->lbam, io_ports->lbam_addr);
d309e0bb 200 if (task->tf_flags & IDE_TFLAG_OUT_LBAH)
ca545c1e 201 tf_outb(tf->lbah, io_ports->lbah_addr);
d309e0bb
BZ
202
203 if (task->tf_flags & IDE_TFLAG_OUT_DEVICE)
ca545c1e
BZ
204 tf_outb((tf->device & HIHI) | drive->select.all,
205 io_ports->device_addr);
d309e0bb
BZ
206}
207
94cd5b62 208static void ide_tf_read(ide_drive_t *drive, ide_task_t *task)
d309e0bb
BZ
209{
210 ide_hwif_t *hwif = drive->hwif;
211 struct ide_io_ports *io_ports = &hwif->io_ports;
212 struct ide_taskfile *tf = &task->tf;
ca545c1e
BZ
213 void (*tf_outb)(u8 addr, unsigned long port);
214 u8 (*tf_inb)(unsigned long port);
215 u8 mmio = (hwif->host_flags & IDE_HFLAG_MMIO) ? 1 : 0;
216
217 if (mmio) {
218 tf_outb = ide_mm_outb;
219 tf_inb = ide_mm_inb;
220 } else {
221 tf_outb = ide_outb;
222 tf_inb = ide_inb;
223 }
d309e0bb
BZ
224
225 if (task->tf_flags & IDE_TFLAG_IN_DATA) {
ca545c1e
BZ
226 u16 data;
227
228 if (mmio)
229 data = readw((void __iomem *)io_ports->data_addr);
230 else
231 data = inw(io_ports->data_addr);
d309e0bb
BZ
232
233 tf->data = data & 0xff;
234 tf->hob_data = (data >> 8) & 0xff;
235 }
236
237 /* be sure we're looking at the low order bits */
ff074883 238 tf_outb(ATA_DEVCTL_OBS & ~0x80, io_ports->ctl_addr);
d309e0bb
BZ
239
240 if (task->tf_flags & IDE_TFLAG_IN_NSECT)
ca545c1e 241 tf->nsect = tf_inb(io_ports->nsect_addr);
d309e0bb 242 if (task->tf_flags & IDE_TFLAG_IN_LBAL)
ca545c1e 243 tf->lbal = tf_inb(io_ports->lbal_addr);
d309e0bb 244 if (task->tf_flags & IDE_TFLAG_IN_LBAM)
ca545c1e 245 tf->lbam = tf_inb(io_ports->lbam_addr);
d309e0bb 246 if (task->tf_flags & IDE_TFLAG_IN_LBAH)
ca545c1e 247 tf->lbah = tf_inb(io_ports->lbah_addr);
d309e0bb 248 if (task->tf_flags & IDE_TFLAG_IN_DEVICE)
ca545c1e 249 tf->device = tf_inb(io_ports->device_addr);
d309e0bb
BZ
250
251 if (task->tf_flags & IDE_TFLAG_LBA48) {
ff074883 252 tf_outb(ATA_DEVCTL_OBS | 0x80, io_ports->ctl_addr);
d309e0bb
BZ
253
254 if (task->tf_flags & IDE_TFLAG_IN_HOB_FEATURE)
ca545c1e 255 tf->hob_feature = tf_inb(io_ports->feature_addr);
d309e0bb 256 if (task->tf_flags & IDE_TFLAG_IN_HOB_NSECT)
ca545c1e 257 tf->hob_nsect = tf_inb(io_ports->nsect_addr);
d309e0bb 258 if (task->tf_flags & IDE_TFLAG_IN_HOB_LBAL)
ca545c1e 259 tf->hob_lbal = tf_inb(io_ports->lbal_addr);
d309e0bb 260 if (task->tf_flags & IDE_TFLAG_IN_HOB_LBAM)
ca545c1e 261 tf->hob_lbam = tf_inb(io_ports->lbam_addr);
d309e0bb 262 if (task->tf_flags & IDE_TFLAG_IN_HOB_LBAH)
ca545c1e 263 tf->hob_lbah = tf_inb(io_ports->lbah_addr);
d309e0bb
BZ
264 }
265}
266
1da177e4
LT
267/*
268 * Some localbus EIDE interfaces require a special access sequence
269 * when using 32-bit I/O instructions to transfer data. We call this
270 * the "vlb_sync" sequence, which consists of three successive reads
271 * of the sector count register location, with interrupts disabled
272 * to ensure that the reads all happen together.
273 */
22cdd6ce 274static void ata_vlb_sync(unsigned long port)
1da177e4 275{
22cdd6ce
BZ
276 (void)inb(port);
277 (void)inb(port);
278 (void)inb(port);
1da177e4
LT
279}
280
281/*
282 * This is used for most PIO data transfers *from* the IDE interface
9567b349
BZ
283 *
284 * These routines will round up any request for an odd number of bytes,
285 * so if an odd len is specified, be sure that there's at least one
286 * extra byte allocated for the buffer.
1da177e4 287 */
92d3ab27 288static void ata_input_data(ide_drive_t *drive, struct request *rq,
9567b349 289 void *buf, unsigned int len)
1da177e4 290{
4c3032d8
BZ
291 ide_hwif_t *hwif = drive->hwif;
292 struct ide_io_ports *io_ports = &hwif->io_ports;
9567b349 293 unsigned long data_addr = io_ports->data_addr;
4c3032d8 294 u8 io_32bit = drive->io_32bit;
16bb69c1 295 u8 mmio = (hwif->host_flags & IDE_HFLAG_MMIO) ? 1 : 0;
1da177e4 296
9567b349
BZ
297 len++;
298
1da177e4 299 if (io_32bit) {
16bb69c1 300 unsigned long uninitialized_var(flags);
23579a2a 301
22cdd6ce 302 if ((io_32bit & 2) && !mmio) {
1da177e4 303 local_irq_save(flags);
22cdd6ce 304 ata_vlb_sync(io_ports->nsect_addr);
16bb69c1
BZ
305 }
306
307 if (mmio)
308 __ide_mm_insl((void __iomem *)data_addr, buf, len / 4);
309 else
310 insl(data_addr, buf, len / 4);
311
22cdd6ce 312 if ((io_32bit & 2) && !mmio)
1da177e4 313 local_irq_restore(flags);
9567b349 314
16bb69c1
BZ
315 if ((len & 3) >= 2) {
316 if (mmio)
317 __ide_mm_insw((void __iomem *)data_addr,
318 (u8 *)buf + (len & ~3), 1);
319 else
320 insw(data_addr, (u8 *)buf + (len & ~3), 1);
321 }
322 } else {
323 if (mmio)
324 __ide_mm_insw((void __iomem *)data_addr, buf, len / 2);
325 else
326 insw(data_addr, buf, len / 2);
327 }
1da177e4
LT
328}
329
330/*
331 * This is used for most PIO data transfers *to* the IDE interface
332 */
92d3ab27 333static void ata_output_data(ide_drive_t *drive, struct request *rq,
9567b349 334 void *buf, unsigned int len)
1da177e4 335{
4c3032d8
BZ
336 ide_hwif_t *hwif = drive->hwif;
337 struct ide_io_ports *io_ports = &hwif->io_ports;
9567b349 338 unsigned long data_addr = io_ports->data_addr;
4c3032d8 339 u8 io_32bit = drive->io_32bit;
16bb69c1 340 u8 mmio = (hwif->host_flags & IDE_HFLAG_MMIO) ? 1 : 0;
1da177e4
LT
341
342 if (io_32bit) {
16bb69c1 343 unsigned long uninitialized_var(flags);
23579a2a 344
22cdd6ce 345 if ((io_32bit & 2) && !mmio) {
1da177e4 346 local_irq_save(flags);
22cdd6ce 347 ata_vlb_sync(io_ports->nsect_addr);
16bb69c1
BZ
348 }
349
350 if (mmio)
351 __ide_mm_outsl((void __iomem *)data_addr, buf, len / 4);
352 else
353 outsl(data_addr, buf, len / 4);
354
22cdd6ce 355 if ((io_32bit & 2) && !mmio)
1da177e4 356 local_irq_restore(flags);
1da177e4 357
16bb69c1
BZ
358 if ((len & 3) >= 2) {
359 if (mmio)
360 __ide_mm_outsw((void __iomem *)data_addr,
361 (u8 *)buf + (len & ~3), 1);
362 else
363 outsw(data_addr, (u8 *)buf + (len & ~3), 1);
364 }
365 } else {
366 if (mmio)
367 __ide_mm_outsw((void __iomem *)data_addr, buf, len / 2);
368 else
369 outsw(data_addr, buf, len / 2);
370 }
1da177e4
LT
371}
372
373void default_hwif_transport(ide_hwif_t *hwif)
374{
c6dfa867 375 hwif->exec_command = ide_exec_command;
b73c7ee2 376 hwif->read_status = ide_read_status;
1f6d8a0f 377 hwif->read_altstatus = ide_read_altstatus;
b2f951aa
BZ
378 hwif->read_sff_dma_status = ide_read_sff_dma_status;
379
6e6afb3b
BZ
380 hwif->set_irq = ide_set_irq;
381
94cd5b62
BZ
382 hwif->tf_load = ide_tf_load;
383 hwif->tf_read = ide_tf_read;
384
9567b349
BZ
385 hwif->input_data = ata_input_data;
386 hwif->output_data = ata_output_data;
1da177e4
LT
387}
388
1da177e4
LT
389void ide_fix_driveid (struct hd_driveid *id)
390{
391#ifndef __LITTLE_ENDIAN
392# ifdef __BIG_ENDIAN
393 int i;
394 u16 *stringcast;
395
396 id->config = __le16_to_cpu(id->config);
397 id->cyls = __le16_to_cpu(id->cyls);
398 id->reserved2 = __le16_to_cpu(id->reserved2);
399 id->heads = __le16_to_cpu(id->heads);
400 id->track_bytes = __le16_to_cpu(id->track_bytes);
401 id->sector_bytes = __le16_to_cpu(id->sector_bytes);
402 id->sectors = __le16_to_cpu(id->sectors);
403 id->vendor0 = __le16_to_cpu(id->vendor0);
404 id->vendor1 = __le16_to_cpu(id->vendor1);
405 id->vendor2 = __le16_to_cpu(id->vendor2);
406 stringcast = (u16 *)&id->serial_no[0];
407 for (i = 0; i < (20/2); i++)
408 stringcast[i] = __le16_to_cpu(stringcast[i]);
409 id->buf_type = __le16_to_cpu(id->buf_type);
410 id->buf_size = __le16_to_cpu(id->buf_size);
411 id->ecc_bytes = __le16_to_cpu(id->ecc_bytes);
412 stringcast = (u16 *)&id->fw_rev[0];
413 for (i = 0; i < (8/2); i++)
414 stringcast[i] = __le16_to_cpu(stringcast[i]);
415 stringcast = (u16 *)&id->model[0];
416 for (i = 0; i < (40/2); i++)
417 stringcast[i] = __le16_to_cpu(stringcast[i]);
418 id->dword_io = __le16_to_cpu(id->dword_io);
419 id->reserved50 = __le16_to_cpu(id->reserved50);
420 id->field_valid = __le16_to_cpu(id->field_valid);
421 id->cur_cyls = __le16_to_cpu(id->cur_cyls);
422 id->cur_heads = __le16_to_cpu(id->cur_heads);
423 id->cur_sectors = __le16_to_cpu(id->cur_sectors);
424 id->cur_capacity0 = __le16_to_cpu(id->cur_capacity0);
425 id->cur_capacity1 = __le16_to_cpu(id->cur_capacity1);
426 id->lba_capacity = __le32_to_cpu(id->lba_capacity);
427 id->dma_1word = __le16_to_cpu(id->dma_1word);
428 id->dma_mword = __le16_to_cpu(id->dma_mword);
429 id->eide_pio_modes = __le16_to_cpu(id->eide_pio_modes);
430 id->eide_dma_min = __le16_to_cpu(id->eide_dma_min);
431 id->eide_dma_time = __le16_to_cpu(id->eide_dma_time);
432 id->eide_pio = __le16_to_cpu(id->eide_pio);
433 id->eide_pio_iordy = __le16_to_cpu(id->eide_pio_iordy);
434 for (i = 0; i < 2; ++i)
435 id->words69_70[i] = __le16_to_cpu(id->words69_70[i]);
436 for (i = 0; i < 4; ++i)
437 id->words71_74[i] = __le16_to_cpu(id->words71_74[i]);
438 id->queue_depth = __le16_to_cpu(id->queue_depth);
439 for (i = 0; i < 4; ++i)
440 id->words76_79[i] = __le16_to_cpu(id->words76_79[i]);
441 id->major_rev_num = __le16_to_cpu(id->major_rev_num);
442 id->minor_rev_num = __le16_to_cpu(id->minor_rev_num);
443 id->command_set_1 = __le16_to_cpu(id->command_set_1);
444 id->command_set_2 = __le16_to_cpu(id->command_set_2);
445 id->cfsse = __le16_to_cpu(id->cfsse);
446 id->cfs_enable_1 = __le16_to_cpu(id->cfs_enable_1);
447 id->cfs_enable_2 = __le16_to_cpu(id->cfs_enable_2);
448 id->csf_default = __le16_to_cpu(id->csf_default);
449 id->dma_ultra = __le16_to_cpu(id->dma_ultra);
450 id->trseuc = __le16_to_cpu(id->trseuc);
451 id->trsEuc = __le16_to_cpu(id->trsEuc);
452 id->CurAPMvalues = __le16_to_cpu(id->CurAPMvalues);
453 id->mprc = __le16_to_cpu(id->mprc);
454 id->hw_config = __le16_to_cpu(id->hw_config);
455 id->acoustic = __le16_to_cpu(id->acoustic);
456 id->msrqs = __le16_to_cpu(id->msrqs);
457 id->sxfert = __le16_to_cpu(id->sxfert);
458 id->sal = __le16_to_cpu(id->sal);
459 id->spg = __le32_to_cpu(id->spg);
460 id->lba_capacity_2 = __le64_to_cpu(id->lba_capacity_2);
461 for (i = 0; i < 22; i++)
462 id->words104_125[i] = __le16_to_cpu(id->words104_125[i]);
463 id->last_lun = __le16_to_cpu(id->last_lun);
464 id->word127 = __le16_to_cpu(id->word127);
465 id->dlf = __le16_to_cpu(id->dlf);
466 id->csfo = __le16_to_cpu(id->csfo);
467 for (i = 0; i < 26; i++)
468 id->words130_155[i] = __le16_to_cpu(id->words130_155[i]);
469 id->word156 = __le16_to_cpu(id->word156);
470 for (i = 0; i < 3; i++)
471 id->words157_159[i] = __le16_to_cpu(id->words157_159[i]);
472 id->cfa_power = __le16_to_cpu(id->cfa_power);
473 for (i = 0; i < 14; i++)
474 id->words161_175[i] = __le16_to_cpu(id->words161_175[i]);
475 for (i = 0; i < 31; i++)
476 id->words176_205[i] = __le16_to_cpu(id->words176_205[i]);
477 for (i = 0; i < 48; i++)
478 id->words206_254[i] = __le16_to_cpu(id->words206_254[i]);
479 id->integrity_word = __le16_to_cpu(id->integrity_word);
480# else
481# error "Please fix <asm/byteorder.h>"
482# endif
483#endif
484}
485
01745112
BZ
486/*
487 * ide_fixstring() cleans up and (optionally) byte-swaps a text string,
488 * removing leading/trailing blanks and compressing internal blanks.
489 * It is primarily used to tidy up the model name/number fields as
490 * returned by the WIN_[P]IDENTIFY commands.
491 */
492
1da177e4
LT
493void ide_fixstring (u8 *s, const int bytecount, const int byteswap)
494{
495 u8 *p = s, *end = &s[bytecount & ~1]; /* bytecount must be even */
496
497 if (byteswap) {
498 /* convert from big-endian to host byte order */
499 for (p = end ; p != s;) {
500 unsigned short *pp = (unsigned short *) (p -= 2);
501 *pp = ntohs(*pp);
502 }
503 }
504 /* strip leading blanks */
505 while (s != end && *s == ' ')
506 ++s;
507 /* compress internal blanks and strip trailing blanks */
508 while (s != end && *s) {
509 if (*s++ != ' ' || (s != end && *s && *s != ' '))
510 *p++ = *(s-1);
511 }
512 /* wipe out trailing garbage */
513 while (p != end)
514 *p++ = '\0';
515}
516
517EXPORT_SYMBOL(ide_fixstring);
518
519/*
520 * Needed for PCI irq sharing
521 */
522int drive_is_ready (ide_drive_t *drive)
523{
524 ide_hwif_t *hwif = HWIF(drive);
525 u8 stat = 0;
526
527 if (drive->waiting_for_dma)
5e37bdc0 528 return hwif->dma_ops->dma_test_irq(drive);
1da177e4
LT
529
530#if 0
531 /* need to guarantee 400ns since last command was issued */
532 udelay(1);
533#endif
534
1da177e4
LT
535 /*
536 * We do a passive status test under shared PCI interrupts on
537 * cards that truly share the ATA side interrupt, but may also share
538 * an interrupt with another pci card/device. We make no assumptions
539 * about possible isa-pnp and pci-pnp issues yet.
540 */
4c3032d8 541 if (hwif->io_ports.ctl_addr)
1f6d8a0f 542 stat = hwif->read_altstatus(hwif);
1da177e4 543 else
1da177e4 544 /* Note: this may clear a pending IRQ!! */
b73c7ee2 545 stat = hwif->read_status(hwif);
1da177e4
LT
546
547 if (stat & BUSY_STAT)
548 /* drive busy: definitely not interrupting */
549 return 0;
550
551 /* drive ready: *might* be interrupting */
552 return 1;
553}
554
555EXPORT_SYMBOL(drive_is_ready);
556
1da177e4
LT
557/*
558 * This routine busy-waits for the drive status to be not "busy".
559 * It then checks the status for all of the "good" bits and none
560 * of the "bad" bits, and if all is okay it returns 0. All other
74af21cf 561 * cases return error -- caller may then invoke ide_error().
1da177e4
LT
562 *
563 * This routine should get fixed to not hog the cpu during extra long waits..
564 * That could be done by busy-waiting for the first jiffy or two, and then
565 * setting a timer to wake up at half second intervals thereafter,
566 * until timeout is achieved, before timing out.
567 */
aedea591 568static int __ide_wait_stat(ide_drive_t *drive, u8 good, u8 bad, unsigned long timeout, u8 *rstat)
1da177e4 569{
b73c7ee2 570 ide_hwif_t *hwif = drive->hwif;
1da177e4 571 unsigned long flags;
74af21cf
BZ
572 int i;
573 u8 stat;
1da177e4
LT
574
575 udelay(1); /* spec allows drive 400ns to assert "BUSY" */
b73c7ee2 576 stat = hwif->read_status(hwif);
c47137a9
BZ
577
578 if (stat & BUSY_STAT) {
1da177e4
LT
579 local_irq_set(flags);
580 timeout += jiffies;
b73c7ee2 581 while ((stat = hwif->read_status(hwif)) & BUSY_STAT) {
1da177e4
LT
582 if (time_after(jiffies, timeout)) {
583 /*
584 * One last read after the timeout in case
585 * heavy interrupt load made us not make any
586 * progress during the timeout..
587 */
b73c7ee2 588 stat = hwif->read_status(hwif);
1da177e4
LT
589 if (!(stat & BUSY_STAT))
590 break;
591
592 local_irq_restore(flags);
74af21cf
BZ
593 *rstat = stat;
594 return -EBUSY;
1da177e4
LT
595 }
596 }
597 local_irq_restore(flags);
598 }
599 /*
600 * Allow status to settle, then read it again.
601 * A few rare drives vastly violate the 400ns spec here,
602 * so we'll wait up to 10usec for a "good" status
603 * rather than expensively fail things immediately.
604 * This fix courtesy of Matthew Faupel & Niccolo Rigacci.
605 */
606 for (i = 0; i < 10; i++) {
607 udelay(1);
b73c7ee2 608 stat = hwif->read_status(hwif);
c47137a9
BZ
609
610 if (OK_STAT(stat, good, bad)) {
74af21cf 611 *rstat = stat;
1da177e4 612 return 0;
74af21cf 613 }
1da177e4 614 }
74af21cf
BZ
615 *rstat = stat;
616 return -EFAULT;
617}
618
619/*
620 * In case of error returns error value after doing "*startstop = ide_error()".
621 * The caller should return the updated value of "startstop" in this case,
622 * "startstop" is unchanged when the function returns 0.
623 */
624int ide_wait_stat(ide_startstop_t *startstop, ide_drive_t *drive, u8 good, u8 bad, unsigned long timeout)
625{
626 int err;
627 u8 stat;
628
629 /* bail early if we've exceeded max_failures */
630 if (drive->max_failures && (drive->failures > drive->max_failures)) {
631 *startstop = ide_stopped;
632 return 1;
633 }
634
635 err = __ide_wait_stat(drive, good, bad, timeout, &stat);
636
637 if (err) {
638 char *s = (err == -EBUSY) ? "status timeout" : "status error";
639 *startstop = ide_error(drive, s, stat);
640 }
641
642 return err;
1da177e4
LT
643}
644
645EXPORT_SYMBOL(ide_wait_stat);
646
a5b7e70d
BZ
647/**
648 * ide_in_drive_list - look for drive in black/white list
649 * @id: drive identifier
650 * @drive_table: list to inspect
651 *
652 * Look for a drive in the blacklist and the whitelist tables
653 * Returns 1 if the drive is found in the table.
654 */
655
656int ide_in_drive_list(struct hd_driveid *id, const struct drive_list_entry *drive_table)
657{
658 for ( ; drive_table->id_model; drive_table++)
659 if ((!strcmp(drive_table->id_model, id->model)) &&
660 (!drive_table->id_firmware ||
661 strstr(id->fw_rev, drive_table->id_firmware)))
662 return 1;
663 return 0;
664}
665
b0244a00
BZ
666EXPORT_SYMBOL_GPL(ide_in_drive_list);
667
a5b7e70d
BZ
668/*
669 * Early UDMA66 devices don't set bit14 to 1, only bit13 is valid.
670 * We list them here and depend on the device side cable detection for them.
8588a2b7
BZ
671 *
672 * Some optical devices with the buggy firmwares have the same problem.
a5b7e70d
BZ
673 */
674static const struct drive_list_entry ivb_list[] = {
675 { "QUANTUM FIREBALLlct10 05" , "A03.0900" },
8588a2b7 676 { "TSSTcorp CDDVDW SH-S202J" , "SB00" },
e97564f3
PM
677 { "TSSTcorp CDDVDW SH-S202J" , "SB01" },
678 { "TSSTcorp CDDVDW SH-S202N" , "SB00" },
679 { "TSSTcorp CDDVDW SH-S202N" , "SB01" },
3ced5c49
AS
680 { "TSSTcorp CDDVDW SH-S202H" , "SB00" },
681 { "TSSTcorp CDDVDW SH-S202H" , "SB01" },
a5b7e70d
BZ
682 { NULL , NULL }
683};
684
1da177e4
LT
685/*
686 * All hosts that use the 80c ribbon must use!
687 * The name is derived from upper byte of word 93 and the 80c ribbon.
688 */
689u8 eighty_ninty_three (ide_drive_t *drive)
690{
7f8f48af
BZ
691 ide_hwif_t *hwif = drive->hwif;
692 struct hd_driveid *id = drive->id;
a5b7e70d 693 int ivb = ide_in_drive_list(id, ivb_list);
7f8f48af 694
49521f97
BZ
695 if (hwif->cbl == ATA_CBL_PATA40_SHORT)
696 return 1;
697
a5b7e70d
BZ
698 if (ivb)
699 printk(KERN_DEBUG "%s: skipping word 93 validity check\n",
700 drive->name);
701
b98f8803
GK
702 if (ide_dev_is_sata(id) && !ivb)
703 return 1;
704
a5b7e70d 705 if (hwif->cbl != ATA_CBL_PATA80 && !ivb)
7f8f48af 706 goto no_80w;
1a1276e7 707
f68d9320
BZ
708 /*
709 * FIXME:
f367bed0 710 * - change master/slave IDENTIFY order
a5b7e70d 711 * - force bit13 (80c cable present) check also for !ivb devices
f68d9320
BZ
712 * (unless the slave device is pre-ATA3)
713 */
a5b7e70d 714 if ((id->hw_config & 0x4000) || (ivb && (id->hw_config & 0x2000)))
7f8f48af
BZ
715 return 1;
716
717no_80w:
718 if (drive->udma33_warned == 1)
719 return 0;
720
721 printk(KERN_WARNING "%s: %s side 80-wire cable detection failed, "
722 "limiting max speed to UDMA33\n",
49521f97
BZ
723 drive->name,
724 hwif->cbl == ATA_CBL_PATA80 ? "drive" : "host");
7f8f48af
BZ
725
726 drive->udma33_warned = 1;
727
728 return 0;
1da177e4
LT
729}
730
8a455134 731int ide_driveid_update(ide_drive_t *drive)
1da177e4 732{
8a455134 733 ide_hwif_t *hwif = drive->hwif;
1da177e4 734 struct hd_driveid *id;
8a455134 735 unsigned long timeout, flags;
c47137a9 736 u8 stat;
1da177e4 737
1da177e4
LT
738 /*
739 * Re-read drive->id for possible DMA mode
740 * change (copied from ide-probe.c)
741 */
1da177e4
LT
742
743 SELECT_MASK(drive, 1);
6e6afb3b 744 hwif->set_irq(hwif, 0);
1da177e4 745 msleep(50);
c6dfa867 746 hwif->exec_command(hwif, WIN_IDENTIFY);
1da177e4
LT
747 timeout = jiffies + WAIT_WORSTCASE;
748 do {
749 if (time_after(jiffies, timeout)) {
750 SELECT_MASK(drive, 0);
751 return 0; /* drive timed-out */
752 }
c47137a9 753
1da177e4 754 msleep(50); /* give drive a breather */
1f6d8a0f 755 stat = hwif->read_altstatus(hwif);
c47137a9
BZ
756 } while (stat & BUSY_STAT);
757
1da177e4 758 msleep(50); /* wait for IRQ and DRQ_STAT */
b73c7ee2 759 stat = hwif->read_status(hwif);
c47137a9
BZ
760
761 if (!OK_STAT(stat, DRQ_STAT, BAD_R_STAT)) {
1da177e4
LT
762 SELECT_MASK(drive, 0);
763 printk("%s: CHECK for good STATUS\n", drive->name);
764 return 0;
765 }
766 local_irq_save(flags);
767 SELECT_MASK(drive, 0);
768 id = kmalloc(SECTOR_WORDS*4, GFP_ATOMIC);
769 if (!id) {
770 local_irq_restore(flags);
771 return 0;
772 }
9567b349 773 hwif->input_data(drive, NULL, id, SECTOR_SIZE);
b73c7ee2 774 (void)hwif->read_status(hwif); /* clear drive IRQ */
1da177e4
LT
775 local_irq_enable();
776 local_irq_restore(flags);
777 ide_fix_driveid(id);
778 if (id) {
779 drive->id->dma_ultra = id->dma_ultra;
780 drive->id->dma_mword = id->dma_mword;
781 drive->id->dma_1word = id->dma_1word;
782 /* anything more ? */
783 kfree(id);
3ab7efe8
BZ
784
785 if (drive->using_dma && ide_id_dma_bug(drive))
786 ide_dma_off(drive);
1da177e4
LT
787 }
788
789 return 1;
1da177e4
LT
790}
791
74af21cf 792int ide_config_drive_speed(ide_drive_t *drive, u8 speed)
1da177e4 793{
74af21cf 794 ide_hwif_t *hwif = drive->hwif;
4c3032d8 795 struct ide_io_ports *io_ports = &hwif->io_ports;
89613e66 796 int error = 0;
1da177e4
LT
797 u8 stat;
798
1da177e4 799#ifdef CONFIG_BLK_DEV_IDEDMA
5e37bdc0
BZ
800 if (hwif->dma_ops) /* check if host supports DMA */
801 hwif->dma_ops->dma_host_set(drive, 0);
1da177e4
LT
802#endif
803
89613e66
SS
804 /* Skip setting PIO flow-control modes on pre-EIDE drives */
805 if ((speed & 0xf8) == XFER_PIO_0 && !(drive->id->capability & 0x08))
806 goto skip;
807
1da177e4
LT
808 /*
809 * Don't use ide_wait_cmd here - it will
810 * attempt to set_geometry and recalibrate,
811 * but for some reason these don't work at
812 * this point (lost interrupt).
813 */
814 /*
815 * Select the drive, and issue the SETFEATURES command
816 */
817 disable_irq_nosync(hwif->irq);
818
819 /*
820 * FIXME: we race against the running IRQ here if
821 * this is called from non IRQ context. If we use
822 * disable_irq() we hang on the error path. Work
823 * is needed.
824 */
825
826 udelay(1);
827 SELECT_DRIVE(drive);
828 SELECT_MASK(drive, 0);
829 udelay(1);
6e6afb3b 830 hwif->set_irq(hwif, 0);
4c3032d8 831 hwif->OUTB(SETFEATURES_XFER, io_ports->feature_addr);
3c09384e 832 hwif->OUTB(speed, io_ports->nsect_addr);
c6dfa867 833 hwif->exec_command(hwif, WIN_SETFEATURES);
81ca6919 834 if (drive->quirk_list == 2)
6e6afb3b 835 hwif->set_irq(hwif, 1);
1da177e4 836
74af21cf
BZ
837 error = __ide_wait_stat(drive, drive->ready_stat,
838 BUSY_STAT|DRQ_STAT|ERR_STAT,
839 WAIT_CMD, &stat);
1da177e4
LT
840
841 SELECT_MASK(drive, 0);
842
843 enable_irq(hwif->irq);
844
845 if (error) {
846 (void) ide_dump_status(drive, "set_drive_speed_status", stat);
847 return error;
848 }
849
850 drive->id->dma_ultra &= ~0xFF00;
851 drive->id->dma_mword &= ~0x0F00;
852 drive->id->dma_1word &= ~0x0F00;
853
89613e66 854 skip:
1da177e4 855#ifdef CONFIG_BLK_DEV_IDEDMA
f37aaf9e
BZ
856 if ((speed >= XFER_SW_DMA_0 || (hwif->host_flags & IDE_HFLAG_VDMA)) &&
857 drive->using_dma)
5e37bdc0
BZ
858 hwif->dma_ops->dma_host_set(drive, 1);
859 else if (hwif->dma_ops) /* check if host supports DMA */
4a546e04 860 ide_dma_off_quietly(drive);
1da177e4
LT
861#endif
862
863 switch(speed) {
864 case XFER_UDMA_7: drive->id->dma_ultra |= 0x8080; break;
865 case XFER_UDMA_6: drive->id->dma_ultra |= 0x4040; break;
866 case XFER_UDMA_5: drive->id->dma_ultra |= 0x2020; break;
867 case XFER_UDMA_4: drive->id->dma_ultra |= 0x1010; break;
868 case XFER_UDMA_3: drive->id->dma_ultra |= 0x0808; break;
869 case XFER_UDMA_2: drive->id->dma_ultra |= 0x0404; break;
870 case XFER_UDMA_1: drive->id->dma_ultra |= 0x0202; break;
871 case XFER_UDMA_0: drive->id->dma_ultra |= 0x0101; break;
872 case XFER_MW_DMA_2: drive->id->dma_mword |= 0x0404; break;
873 case XFER_MW_DMA_1: drive->id->dma_mword |= 0x0202; break;
874 case XFER_MW_DMA_0: drive->id->dma_mword |= 0x0101; break;
875 case XFER_SW_DMA_2: drive->id->dma_1word |= 0x0404; break;
876 case XFER_SW_DMA_1: drive->id->dma_1word |= 0x0202; break;
877 case XFER_SW_DMA_0: drive->id->dma_1word |= 0x0101; break;
878 default: break;
879 }
880 if (!drive->init_speed)
881 drive->init_speed = speed;
882 drive->current_speed = speed;
883 return error;
884}
885
1da177e4
LT
886/*
887 * This should get invoked any time we exit the driver to
888 * wait for an interrupt response from a drive. handler() points
889 * at the appropriate code to handle the next interrupt, and a
890 * timer is started to prevent us from waiting forever in case
891 * something goes wrong (see the ide_timer_expiry() handler later on).
892 *
893 * See also ide_execute_command
894 */
895static void __ide_set_handler (ide_drive_t *drive, ide_handler_t *handler,
896 unsigned int timeout, ide_expiry_t *expiry)
897{
898 ide_hwgroup_t *hwgroup = HWGROUP(drive);
899
d30a426d 900 BUG_ON(hwgroup->handler);
1da177e4
LT
901 hwgroup->handler = handler;
902 hwgroup->expiry = expiry;
903 hwgroup->timer.expires = jiffies + timeout;
d30a426d 904 hwgroup->req_gen_timer = hwgroup->req_gen;
1da177e4
LT
905 add_timer(&hwgroup->timer);
906}
907
908void ide_set_handler (ide_drive_t *drive, ide_handler_t *handler,
909 unsigned int timeout, ide_expiry_t *expiry)
910{
911 unsigned long flags;
912 spin_lock_irqsave(&ide_lock, flags);
913 __ide_set_handler(drive, handler, timeout, expiry);
914 spin_unlock_irqrestore(&ide_lock, flags);
915}
916
917EXPORT_SYMBOL(ide_set_handler);
918
919/**
920 * ide_execute_command - execute an IDE command
921 * @drive: IDE drive to issue the command against
922 * @command: command byte to write
923 * @handler: handler for next phase
924 * @timeout: timeout for command
925 * @expiry: handler to run on timeout
926 *
927 * Helper function to issue an IDE command. This handles the
928 * atomicity requirements, command timing and ensures that the
929 * handler and IRQ setup do not race. All IDE command kick off
930 * should go via this function or do equivalent locking.
931 */
cd2a2d96
BZ
932
933void ide_execute_command(ide_drive_t *drive, u8 cmd, ide_handler_t *handler,
934 unsigned timeout, ide_expiry_t *expiry)
1da177e4
LT
935{
936 unsigned long flags;
1da177e4 937 ide_hwif_t *hwif = HWIF(drive);
629f944b 938
1da177e4 939 spin_lock_irqsave(&ide_lock, flags);
629f944b 940 __ide_set_handler(drive, handler, timeout, expiry);
c6dfa867 941 hwif->exec_command(hwif, cmd);
629f944b
BZ
942 /*
943 * Drive takes 400nS to respond, we must avoid the IRQ being
944 * serviced before that.
945 *
946 * FIXME: we could skip this delay with care on non shared devices
947 */
1da177e4
LT
948 ndelay(400);
949 spin_unlock_irqrestore(&ide_lock, flags);
950}
1da177e4
LT
951EXPORT_SYMBOL(ide_execute_command);
952
1fc14258
BZ
953void ide_execute_pkt_cmd(ide_drive_t *drive)
954{
955 ide_hwif_t *hwif = drive->hwif;
956 unsigned long flags;
957
958 spin_lock_irqsave(&ide_lock, flags);
c6dfa867 959 hwif->exec_command(hwif, WIN_PACKETCMD);
1fc14258
BZ
960 ndelay(400);
961 spin_unlock_irqrestore(&ide_lock, flags);
962}
963EXPORT_SYMBOL_GPL(ide_execute_pkt_cmd);
1da177e4 964
64a8f00f 965static inline void ide_complete_drive_reset(ide_drive_t *drive, int err)
79e36a9f
EO
966{
967 struct request *rq = drive->hwif->hwgroup->rq;
968
969 if (rq && blk_special_request(rq) && rq->cmd[0] == REQ_DRIVE_RESET)
64a8f00f 970 ide_end_request(drive, err ? err : 1, 0);
79e36a9f
EO
971}
972
1da177e4
LT
973/* needed below */
974static ide_startstop_t do_reset1 (ide_drive_t *, int);
975
976/*
977 * atapi_reset_pollfunc() gets invoked to poll the interface for completion every 50ms
978 * during an atapi drive reset operation. If the drive has not yet responded,
979 * and we have not yet hit our maximum waiting time, then the timer is restarted
980 * for another 50ms.
981 */
982static ide_startstop_t atapi_reset_pollfunc (ide_drive_t *drive)
983{
b73c7ee2
BZ
984 ide_hwif_t *hwif = drive->hwif;
985 ide_hwgroup_t *hwgroup = hwif->hwgroup;
1da177e4
LT
986 u8 stat;
987
988 SELECT_DRIVE(drive);
989 udelay (10);
b73c7ee2 990 stat = hwif->read_status(hwif);
1da177e4 991
c47137a9 992 if (OK_STAT(stat, 0, BUSY_STAT))
1da177e4 993 printk("%s: ATAPI reset complete\n", drive->name);
c47137a9 994 else {
1da177e4 995 if (time_before(jiffies, hwgroup->poll_timeout)) {
1da177e4
LT
996 ide_set_handler(drive, &atapi_reset_pollfunc, HZ/20, NULL);
997 /* continue polling */
998 return ide_started;
999 }
1000 /* end of polling */
1001 hwgroup->polling = 0;
1002 printk("%s: ATAPI reset timed-out, status=0x%02x\n",
1003 drive->name, stat);
1004 /* do it the old fashioned way */
1005 return do_reset1(drive, 1);
1006 }
1007 /* done polling */
1008 hwgroup->polling = 0;
64a8f00f 1009 ide_complete_drive_reset(drive, 0);
1da177e4
LT
1010 return ide_stopped;
1011}
1012
1013/*
1014 * reset_pollfunc() gets invoked to poll the interface for completion every 50ms
1015 * during an ide reset operation. If the drives have not yet responded,
1016 * and we have not yet hit our maximum waiting time, then the timer is restarted
1017 * for another 50ms.
1018 */
1019static ide_startstop_t reset_pollfunc (ide_drive_t *drive)
1020{
1021 ide_hwgroup_t *hwgroup = HWGROUP(drive);
1022 ide_hwif_t *hwif = HWIF(drive);
ac95beed 1023 const struct ide_port_ops *port_ops = hwif->port_ops;
1da177e4 1024 u8 tmp;
64a8f00f 1025 int err = 0;
1da177e4 1026
ac95beed 1027 if (port_ops && port_ops->reset_poll) {
64a8f00f
EO
1028 err = port_ops->reset_poll(drive);
1029 if (err) {
1da177e4
LT
1030 printk(KERN_ERR "%s: host reset_poll failure for %s.\n",
1031 hwif->name, drive->name);
79e36a9f 1032 goto out;
1da177e4
LT
1033 }
1034 }
1035
b73c7ee2 1036 tmp = hwif->read_status(hwif);
c47137a9
BZ
1037
1038 if (!OK_STAT(tmp, 0, BUSY_STAT)) {
1da177e4 1039 if (time_before(jiffies, hwgroup->poll_timeout)) {
1da177e4
LT
1040 ide_set_handler(drive, &reset_pollfunc, HZ/20, NULL);
1041 /* continue polling */
1042 return ide_started;
1043 }
1044 printk("%s: reset timed-out, status=0x%02x\n", hwif->name, tmp);
1045 drive->failures++;
64a8f00f 1046 err = -EIO;
1da177e4
LT
1047 } else {
1048 printk("%s: reset: ", hwif->name);
64a57fe4
BZ
1049 tmp = ide_read_error(drive);
1050
1051 if (tmp == 1) {
1da177e4
LT
1052 printk("success\n");
1053 drive->failures = 0;
1054 } else {
1055 drive->failures++;
1056 printk("master: ");
1057 switch (tmp & 0x7f) {
1058 case 1: printk("passed");
1059 break;
1060 case 2: printk("formatter device error");
1061 break;
1062 case 3: printk("sector buffer error");
1063 break;
1064 case 4: printk("ECC circuitry error");
1065 break;
1066 case 5: printk("controlling MPU error");
1067 break;
1068 default:printk("error (0x%02x?)", tmp);
1069 }
1070 if (tmp & 0x80)
1071 printk("; slave: failed");
1072 printk("\n");
64a8f00f 1073 err = -EIO;
1da177e4
LT
1074 }
1075 }
79e36a9f 1076out:
64a8f00f
EO
1077 hwgroup->polling = 0; /* done polling */
1078 ide_complete_drive_reset(drive, err);
1da177e4
LT
1079 return ide_stopped;
1080}
1081
1da177e4
LT
1082static void ide_disk_pre_reset(ide_drive_t *drive)
1083{
1084 int legacy = (drive->id->cfs_enable_2 & 0x0400) ? 0 : 1;
1085
1086 drive->special.all = 0;
1087 drive->special.b.set_geometry = legacy;
1088 drive->special.b.recalibrate = legacy;
4ee06b7e 1089 drive->mult_count = 0;
1da177e4
LT
1090 if (!drive->keep_settings && !drive->using_dma)
1091 drive->mult_req = 0;
1092 if (drive->mult_req != drive->mult_count)
1093 drive->special.b.set_multmode = 1;
1094}
1095
1096static void pre_reset(ide_drive_t *drive)
1097{
ac95beed
BZ
1098 const struct ide_port_ops *port_ops = drive->hwif->port_ops;
1099
1da177e4
LT
1100 if (drive->media == ide_disk)
1101 ide_disk_pre_reset(drive);
1102 else
1103 drive->post_reset = 1;
1104
99ffbe0e
BZ
1105 if (drive->using_dma) {
1106 if (drive->crc_count)
578cfa0d 1107 ide_check_dma_crc(drive);
99ffbe0e
BZ
1108 else
1109 ide_dma_off(drive);
1110 }
1111
1112 if (!drive->keep_settings) {
1113 if (!drive->using_dma) {
1da177e4
LT
1114 drive->unmask = 0;
1115 drive->io_32bit = 0;
1116 }
1117 return;
1118 }
1da177e4 1119
ac95beed
BZ
1120 if (port_ops && port_ops->pre_reset)
1121 port_ops->pre_reset(drive);
1da177e4 1122
513daadd
SS
1123 if (drive->current_speed != 0xff)
1124 drive->desired_speed = drive->current_speed;
1125 drive->current_speed = 0xff;
1da177e4
LT
1126}
1127
1128/*
1129 * do_reset1() attempts to recover a confused drive by resetting it.
1130 * Unfortunately, resetting a disk drive actually resets all devices on
1131 * the same interface, so it can really be thought of as resetting the
1132 * interface rather than resetting the drive.
1133 *
1134 * ATAPI devices have their own reset mechanism which allows them to be
1135 * individually reset without clobbering other devices on the same interface.
1136 *
1137 * Unfortunately, the IDE interface does not generate an interrupt to let
1138 * us know when the reset operation has finished, so we must poll for this.
1139 * Equally poor, though, is the fact that this may a very long time to complete,
1140 * (up to 30 seconds worstcase). So, instead of busy-waiting here for it,
1141 * we set a timer to poll at 50ms intervals.
1142 */
1143static ide_startstop_t do_reset1 (ide_drive_t *drive, int do_not_try_atapi)
1144{
1145 unsigned int unit;
1146 unsigned long flags;
1147 ide_hwif_t *hwif;
1148 ide_hwgroup_t *hwgroup;
4c3032d8 1149 struct ide_io_ports *io_ports;
ac95beed 1150 const struct ide_port_ops *port_ops;
23579a2a 1151
1da177e4
LT
1152 spin_lock_irqsave(&ide_lock, flags);
1153 hwif = HWIF(drive);
1154 hwgroup = HWGROUP(drive);
1155
4c3032d8
BZ
1156 io_ports = &hwif->io_ports;
1157
1da177e4 1158 /* We must not reset with running handlers */
125e1874 1159 BUG_ON(hwgroup->handler != NULL);
1da177e4
LT
1160
1161 /* For an ATAPI device, first try an ATAPI SRST. */
1162 if (drive->media != ide_disk && !do_not_try_atapi) {
1163 pre_reset(drive);
1164 SELECT_DRIVE(drive);
1165 udelay (20);
c6dfa867 1166 hwif->exec_command(hwif, WIN_SRST);
68ad9910 1167 ndelay(400);
1da177e4
LT
1168 hwgroup->poll_timeout = jiffies + WAIT_WORSTCASE;
1169 hwgroup->polling = 1;
1170 __ide_set_handler(drive, &atapi_reset_pollfunc, HZ/20, NULL);
1171 spin_unlock_irqrestore(&ide_lock, flags);
1172 return ide_started;
1173 }
1174
1175 /*
1176 * First, reset any device state data we were maintaining
1177 * for any of the drives on this interface.
1178 */
1179 for (unit = 0; unit < MAX_DRIVES; ++unit)
1180 pre_reset(&hwif->drives[unit]);
1181
4c3032d8 1182 if (io_ports->ctl_addr == 0) {
1da177e4 1183 spin_unlock_irqrestore(&ide_lock, flags);
64a8f00f 1184 ide_complete_drive_reset(drive, -ENXIO);
1da177e4
LT
1185 return ide_stopped;
1186 }
1187
1188 /*
1189 * Note that we also set nIEN while resetting the device,
1190 * to mask unwanted interrupts from the interface during the reset.
1191 * However, due to the design of PC hardware, this will cause an
1192 * immediate interrupt due to the edge transition it produces.
1193 * This single interrupt gives us a "fast poll" for drives that
1194 * recover from reset very quickly, saving us the first 50ms wait time.
6e6afb3b
BZ
1195 *
1196 * TODO: add ->softreset method and stop abusing ->set_irq
1da177e4
LT
1197 */
1198 /* set SRST and nIEN */
6e6afb3b 1199 hwif->set_irq(hwif, 4);
1da177e4
LT
1200 /* more than enough time */
1201 udelay(10);
6e6afb3b
BZ
1202 /* clear SRST, leave nIEN (unless device is on the quirk list) */
1203 hwif->set_irq(hwif, drive->quirk_list == 2);
1da177e4
LT
1204 /* more than enough time */
1205 udelay(10);
1206 hwgroup->poll_timeout = jiffies + WAIT_WORSTCASE;
1207 hwgroup->polling = 1;
1208 __ide_set_handler(drive, &reset_pollfunc, HZ/20, NULL);
1209
1210 /*
1211 * Some weird controller like resetting themselves to a strange
1212 * state when the disks are reset this way. At least, the Winbond
1213 * 553 documentation says that
1214 */
ac95beed
BZ
1215 port_ops = hwif->port_ops;
1216 if (port_ops && port_ops->resetproc)
1217 port_ops->resetproc(drive);
1da177e4
LT
1218
1219 spin_unlock_irqrestore(&ide_lock, flags);
1220 return ide_started;
1221}
1222
1223/*
1224 * ide_do_reset() is the entry point to the drive/interface reset code.
1225 */
1226
1227ide_startstop_t ide_do_reset (ide_drive_t *drive)
1228{
1229 return do_reset1(drive, 0);
1230}
1231
1232EXPORT_SYMBOL(ide_do_reset);
1233
1234/*
1235 * ide_wait_not_busy() waits for the currently selected device on the hwif
9d501529 1236 * to report a non-busy status, see comments in ide_probe_port().
1da177e4
LT
1237 */
1238int ide_wait_not_busy(ide_hwif_t *hwif, unsigned long timeout)
1239{
1240 u8 stat = 0;
1241
1242 while(timeout--) {
1243 /*
1244 * Turn this into a schedule() sleep once I'm sure
1245 * about locking issues (2.5 work ?).
1246 */
1247 mdelay(1);
b73c7ee2 1248 stat = hwif->read_status(hwif);
1da177e4
LT
1249 if ((stat & BUSY_STAT) == 0)
1250 return 0;
1251 /*
1252 * Assume a value of 0xff means nothing is connected to
1253 * the interface and it doesn't implement the pull-down
1254 * resistor on D7.
1255 */
1256 if (stat == 0xff)
1257 return -ENODEV;
6842f8c8 1258 touch_softlockup_watchdog();
1e86240f 1259 touch_nmi_watchdog();
1da177e4
LT
1260 }
1261 return -EBUSY;
1262}
1263
1264EXPORT_SYMBOL_GPL(ide_wait_not_busy);
1265