blob: 657537f96c3afc7bd6504a415751ee674d485984 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Jeff Garzikaf36d7f2005-08-28 20:18:39 -04002 * libata-core.c - helper library for ATA
3 *
4 * Maintained by: Jeff Garzik <jgarzik@pobox.com>
5 * Please ALWAYS copy linux-ide@vger.kernel.org
6 * on emails.
7 *
8 * Copyright 2003-2004 Red Hat, Inc. All rights reserved.
9 * Copyright 2003-2004 Jeff Garzik
10 *
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2, or (at your option)
15 * any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; see the file COPYING. If not, write to
24 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
25 *
26 *
27 * libata documentation is available via 'make {ps|pdf}docs',
28 * as Documentation/DocBook/libata.*
29 *
30 * Hardware documentation available from http://www.t13.org/ and
31 * http://www.sata-io.org/
32 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070033 */
34
35#include <linux/config.h>
36#include <linux/kernel.h>
37#include <linux/module.h>
38#include <linux/pci.h>
39#include <linux/init.h>
40#include <linux/list.h>
41#include <linux/mm.h>
42#include <linux/highmem.h>
43#include <linux/spinlock.h>
44#include <linux/blkdev.h>
45#include <linux/delay.h>
46#include <linux/timer.h>
47#include <linux/interrupt.h>
48#include <linux/completion.h>
49#include <linux/suspend.h>
50#include <linux/workqueue.h>
Jeff Garzik67846b32005-10-05 02:58:32 -040051#include <linux/jiffies.h>
David Hardeman378f0582005-09-17 17:55:31 +100052#include <linux/scatterlist.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070053#include <scsi/scsi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070054#include "scsi_priv.h"
Jeff Garzik193515d2005-11-07 00:59:37 -050055#include <scsi/scsi_cmnd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070056#include <scsi/scsi_host.h>
57#include <linux/libata.h>
58#include <asm/io.h>
59#include <asm/semaphore.h>
60#include <asm/byteorder.h>
61
62#include "libata.h"
63
64static unsigned int ata_busy_sleep (struct ata_port *ap,
65 unsigned long tmout_pat,
66 unsigned long tmout);
Albert Lee59a10b12005-10-12 15:09:42 +080067static void ata_dev_reread_id(struct ata_port *ap, struct ata_device *dev);
Albert Lee8bf62ec2005-05-12 15:29:42 -040068static void ata_dev_init_params(struct ata_port *ap, struct ata_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070069static void ata_set_mode(struct ata_port *ap);
70static void ata_dev_set_xfermode(struct ata_port *ap, struct ata_device *dev);
Jeff Garzik057ace52005-10-22 14:27:05 -040071static unsigned int ata_get_mode_mask(const struct ata_port *ap, int shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -070072static int fgb(u32 bitmap);
Jeff Garzik057ace52005-10-22 14:27:05 -040073static int ata_choose_xfer_mode(const struct ata_port *ap,
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 u8 *xfer_mode_out,
75 unsigned int *xfer_shift_out);
Linus Torvalds1da177e2005-04-16 15:20:36 -070076static void __ata_qc_complete(struct ata_queued_cmd *qc);
Jeff Garzike33b9df2005-10-09 09:51:46 -040077static void ata_pio_error(struct ata_port *ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
79static unsigned int ata_unique_id = 1;
80static struct workqueue_struct *ata_wq;
81
Jeff Garzik1623c812005-08-30 03:37:42 -040082int atapi_enabled = 0;
83module_param(atapi_enabled, int, 0444);
84MODULE_PARM_DESC(atapi_enabled, "Enable discovery of ATAPI devices (0=off, 1=on)");
85
Linus Torvalds1da177e2005-04-16 15:20:36 -070086MODULE_AUTHOR("Jeff Garzik");
87MODULE_DESCRIPTION("Library module for ATA devices");
88MODULE_LICENSE("GPL");
89MODULE_VERSION(DRV_VERSION);
90
91/**
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -040092 * ata_tf_load_pio - send taskfile registers to host controller
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 * @ap: Port to which output is sent
94 * @tf: ATA taskfile register set
95 *
96 * Outputs ATA taskfile to standard ATA host controller.
97 *
98 * LOCKING:
99 * Inherited from caller.
100 */
101
Jeff Garzik057ace52005-10-22 14:27:05 -0400102static void ata_tf_load_pio(struct ata_port *ap, const struct ata_taskfile *tf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103{
104 struct ata_ioports *ioaddr = &ap->ioaddr;
105 unsigned int is_addr = tf->flags & ATA_TFLAG_ISADDR;
106
107 if (tf->ctl != ap->last_ctl) {
108 outb(tf->ctl, ioaddr->ctl_addr);
109 ap->last_ctl = tf->ctl;
110 ata_wait_idle(ap);
111 }
112
113 if (is_addr && (tf->flags & ATA_TFLAG_LBA48)) {
114 outb(tf->hob_feature, ioaddr->feature_addr);
115 outb(tf->hob_nsect, ioaddr->nsect_addr);
116 outb(tf->hob_lbal, ioaddr->lbal_addr);
117 outb(tf->hob_lbam, ioaddr->lbam_addr);
118 outb(tf->hob_lbah, ioaddr->lbah_addr);
119 VPRINTK("hob: feat 0x%X nsect 0x%X, lba 0x%X 0x%X 0x%X\n",
120 tf->hob_feature,
121 tf->hob_nsect,
122 tf->hob_lbal,
123 tf->hob_lbam,
124 tf->hob_lbah);
125 }
126
127 if (is_addr) {
128 outb(tf->feature, ioaddr->feature_addr);
129 outb(tf->nsect, ioaddr->nsect_addr);
130 outb(tf->lbal, ioaddr->lbal_addr);
131 outb(tf->lbam, ioaddr->lbam_addr);
132 outb(tf->lbah, ioaddr->lbah_addr);
133 VPRINTK("feat 0x%X nsect 0x%X lba 0x%X 0x%X 0x%X\n",
134 tf->feature,
135 tf->nsect,
136 tf->lbal,
137 tf->lbam,
138 tf->lbah);
139 }
140
141 if (tf->flags & ATA_TFLAG_DEVICE) {
142 outb(tf->device, ioaddr->device_addr);
143 VPRINTK("device 0x%X\n", tf->device);
144 }
145
146 ata_wait_idle(ap);
147}
148
149/**
150 * ata_tf_load_mmio - send taskfile registers to host controller
151 * @ap: Port to which output is sent
152 * @tf: ATA taskfile register set
153 *
154 * Outputs ATA taskfile to standard ATA host controller using MMIO.
155 *
156 * LOCKING:
157 * Inherited from caller.
158 */
159
Jeff Garzik057ace52005-10-22 14:27:05 -0400160static void ata_tf_load_mmio(struct ata_port *ap, const struct ata_taskfile *tf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161{
162 struct ata_ioports *ioaddr = &ap->ioaddr;
163 unsigned int is_addr = tf->flags & ATA_TFLAG_ISADDR;
164
165 if (tf->ctl != ap->last_ctl) {
166 writeb(tf->ctl, (void __iomem *) ap->ioaddr.ctl_addr);
167 ap->last_ctl = tf->ctl;
168 ata_wait_idle(ap);
169 }
170
171 if (is_addr && (tf->flags & ATA_TFLAG_LBA48)) {
172 writeb(tf->hob_feature, (void __iomem *) ioaddr->feature_addr);
173 writeb(tf->hob_nsect, (void __iomem *) ioaddr->nsect_addr);
174 writeb(tf->hob_lbal, (void __iomem *) ioaddr->lbal_addr);
175 writeb(tf->hob_lbam, (void __iomem *) ioaddr->lbam_addr);
176 writeb(tf->hob_lbah, (void __iomem *) ioaddr->lbah_addr);
177 VPRINTK("hob: feat 0x%X nsect 0x%X, lba 0x%X 0x%X 0x%X\n",
178 tf->hob_feature,
179 tf->hob_nsect,
180 tf->hob_lbal,
181 tf->hob_lbam,
182 tf->hob_lbah);
183 }
184
185 if (is_addr) {
186 writeb(tf->feature, (void __iomem *) ioaddr->feature_addr);
187 writeb(tf->nsect, (void __iomem *) ioaddr->nsect_addr);
188 writeb(tf->lbal, (void __iomem *) ioaddr->lbal_addr);
189 writeb(tf->lbam, (void __iomem *) ioaddr->lbam_addr);
190 writeb(tf->lbah, (void __iomem *) ioaddr->lbah_addr);
191 VPRINTK("feat 0x%X nsect 0x%X lba 0x%X 0x%X 0x%X\n",
192 tf->feature,
193 tf->nsect,
194 tf->lbal,
195 tf->lbam,
196 tf->lbah);
197 }
198
199 if (tf->flags & ATA_TFLAG_DEVICE) {
200 writeb(tf->device, (void __iomem *) ioaddr->device_addr);
201 VPRINTK("device 0x%X\n", tf->device);
202 }
203
204 ata_wait_idle(ap);
205}
206
Edward Falk0baab862005-06-02 18:17:13 -0400207
208/**
209 * ata_tf_load - send taskfile registers to host controller
210 * @ap: Port to which output is sent
211 * @tf: ATA taskfile register set
212 *
213 * Outputs ATA taskfile to standard ATA host controller using MMIO
214 * or PIO as indicated by the ATA_FLAG_MMIO flag.
215 * Writes the control, feature, nsect, lbal, lbam, and lbah registers.
216 * Optionally (ATA_TFLAG_LBA48) writes hob_feature, hob_nsect,
217 * hob_lbal, hob_lbam, and hob_lbah.
218 *
219 * This function waits for idle (!BUSY and !DRQ) after writing
220 * registers. If the control register has a new value, this
221 * function also waits for idle after writing control and before
222 * writing the remaining registers.
223 *
224 * May be used as the tf_load() entry in ata_port_operations.
225 *
226 * LOCKING:
227 * Inherited from caller.
228 */
Jeff Garzik057ace52005-10-22 14:27:05 -0400229void ata_tf_load(struct ata_port *ap, const struct ata_taskfile *tf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230{
231 if (ap->flags & ATA_FLAG_MMIO)
232 ata_tf_load_mmio(ap, tf);
233 else
234 ata_tf_load_pio(ap, tf);
235}
236
237/**
Edward Falk0baab862005-06-02 18:17:13 -0400238 * ata_exec_command_pio - issue ATA command to host controller
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 * @ap: port to which command is being issued
240 * @tf: ATA taskfile register set
241 *
Edward Falk0baab862005-06-02 18:17:13 -0400242 * Issues PIO write to ATA command register, with proper
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 * synchronization with interrupt handler / other threads.
244 *
245 * LOCKING:
246 * spin_lock_irqsave(host_set lock)
247 */
248
Jeff Garzik057ace52005-10-22 14:27:05 -0400249static void ata_exec_command_pio(struct ata_port *ap, const struct ata_taskfile *tf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250{
251 DPRINTK("ata%u: cmd 0x%X\n", ap->id, tf->command);
252
253 outb(tf->command, ap->ioaddr.command_addr);
254 ata_pause(ap);
255}
256
257
258/**
259 * ata_exec_command_mmio - issue ATA command to host controller
260 * @ap: port to which command is being issued
261 * @tf: ATA taskfile register set
262 *
263 * Issues MMIO write to ATA command register, with proper
264 * synchronization with interrupt handler / other threads.
265 *
266 * LOCKING:
267 * spin_lock_irqsave(host_set lock)
268 */
269
Jeff Garzik057ace52005-10-22 14:27:05 -0400270static void ata_exec_command_mmio(struct ata_port *ap, const struct ata_taskfile *tf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271{
272 DPRINTK("ata%u: cmd 0x%X\n", ap->id, tf->command);
273
274 writeb(tf->command, (void __iomem *) ap->ioaddr.command_addr);
275 ata_pause(ap);
276}
277
Edward Falk0baab862005-06-02 18:17:13 -0400278
279/**
280 * ata_exec_command - issue ATA command to host controller
281 * @ap: port to which command is being issued
282 * @tf: ATA taskfile register set
283 *
284 * Issues PIO/MMIO write to ATA command register, with proper
285 * synchronization with interrupt handler / other threads.
286 *
287 * LOCKING:
288 * spin_lock_irqsave(host_set lock)
289 */
Jeff Garzik057ace52005-10-22 14:27:05 -0400290void ata_exec_command(struct ata_port *ap, const struct ata_taskfile *tf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291{
292 if (ap->flags & ATA_FLAG_MMIO)
293 ata_exec_command_mmio(ap, tf);
294 else
295 ata_exec_command_pio(ap, tf);
296}
297
298/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 * ata_tf_to_host - issue ATA taskfile to host controller
300 * @ap: port to which command is being issued
301 * @tf: ATA taskfile register set
302 *
303 * Issues ATA taskfile register set to ATA host controller,
304 * with proper synchronization with interrupt handler and
305 * other threads.
306 *
307 * LOCKING:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 * spin_lock_irqsave(host_set lock)
309 */
310
Jeff Garzike5338252005-10-30 21:37:17 -0500311static inline void ata_tf_to_host(struct ata_port *ap,
312 const struct ata_taskfile *tf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313{
314 ap->ops->tf_load(ap, tf);
315 ap->ops->exec_command(ap, tf);
316}
317
318/**
Edward Falk0baab862005-06-02 18:17:13 -0400319 * ata_tf_read_pio - input device's ATA taskfile shadow registers
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 * @ap: Port from which input is read
321 * @tf: ATA taskfile register set for storing input
322 *
323 * Reads ATA taskfile registers for currently-selected device
324 * into @tf.
325 *
326 * LOCKING:
327 * Inherited from caller.
328 */
329
330static void ata_tf_read_pio(struct ata_port *ap, struct ata_taskfile *tf)
331{
332 struct ata_ioports *ioaddr = &ap->ioaddr;
333
Jeff Garzikac19bff2005-10-29 13:58:21 -0400334 tf->command = ata_check_status(ap);
Jeff Garzik0169e282005-10-29 21:25:10 -0400335 tf->feature = inb(ioaddr->error_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 tf->nsect = inb(ioaddr->nsect_addr);
337 tf->lbal = inb(ioaddr->lbal_addr);
338 tf->lbam = inb(ioaddr->lbam_addr);
339 tf->lbah = inb(ioaddr->lbah_addr);
340 tf->device = inb(ioaddr->device_addr);
341
342 if (tf->flags & ATA_TFLAG_LBA48) {
343 outb(tf->ctl | ATA_HOB, ioaddr->ctl_addr);
344 tf->hob_feature = inb(ioaddr->error_addr);
345 tf->hob_nsect = inb(ioaddr->nsect_addr);
346 tf->hob_lbal = inb(ioaddr->lbal_addr);
347 tf->hob_lbam = inb(ioaddr->lbam_addr);
348 tf->hob_lbah = inb(ioaddr->lbah_addr);
349 }
350}
351
352/**
353 * ata_tf_read_mmio - input device's ATA taskfile shadow registers
354 * @ap: Port from which input is read
355 * @tf: ATA taskfile register set for storing input
356 *
357 * Reads ATA taskfile registers for currently-selected device
358 * into @tf via MMIO.
359 *
360 * LOCKING:
361 * Inherited from caller.
362 */
363
364static void ata_tf_read_mmio(struct ata_port *ap, struct ata_taskfile *tf)
365{
366 struct ata_ioports *ioaddr = &ap->ioaddr;
367
Jeff Garzikac19bff2005-10-29 13:58:21 -0400368 tf->command = ata_check_status(ap);
Jeff Garzik0169e282005-10-29 21:25:10 -0400369 tf->feature = readb((void __iomem *)ioaddr->error_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 tf->nsect = readb((void __iomem *)ioaddr->nsect_addr);
371 tf->lbal = readb((void __iomem *)ioaddr->lbal_addr);
372 tf->lbam = readb((void __iomem *)ioaddr->lbam_addr);
373 tf->lbah = readb((void __iomem *)ioaddr->lbah_addr);
374 tf->device = readb((void __iomem *)ioaddr->device_addr);
375
376 if (tf->flags & ATA_TFLAG_LBA48) {
377 writeb(tf->ctl | ATA_HOB, (void __iomem *) ap->ioaddr.ctl_addr);
378 tf->hob_feature = readb((void __iomem *)ioaddr->error_addr);
379 tf->hob_nsect = readb((void __iomem *)ioaddr->nsect_addr);
380 tf->hob_lbal = readb((void __iomem *)ioaddr->lbal_addr);
381 tf->hob_lbam = readb((void __iomem *)ioaddr->lbam_addr);
382 tf->hob_lbah = readb((void __iomem *)ioaddr->lbah_addr);
383 }
384}
385
Edward Falk0baab862005-06-02 18:17:13 -0400386
387/**
388 * ata_tf_read - input device's ATA taskfile shadow registers
389 * @ap: Port from which input is read
390 * @tf: ATA taskfile register set for storing input
391 *
392 * Reads ATA taskfile registers for currently-selected device
393 * into @tf.
394 *
395 * Reads nsect, lbal, lbam, lbah, and device. If ATA_TFLAG_LBA48
396 * is set, also reads the hob registers.
397 *
398 * May be used as the tf_read() entry in ata_port_operations.
399 *
400 * LOCKING:
401 * Inherited from caller.
402 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403void ata_tf_read(struct ata_port *ap, struct ata_taskfile *tf)
404{
405 if (ap->flags & ATA_FLAG_MMIO)
406 ata_tf_read_mmio(ap, tf);
407 else
408 ata_tf_read_pio(ap, tf);
409}
410
411/**
412 * ata_check_status_pio - Read device status reg & clear interrupt
413 * @ap: port where the device is
414 *
415 * Reads ATA taskfile status register for currently-selected device
Edward Falk0baab862005-06-02 18:17:13 -0400416 * and return its value. This also clears pending interrupts
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 * from this device
418 *
419 * LOCKING:
420 * Inherited from caller.
421 */
422static u8 ata_check_status_pio(struct ata_port *ap)
423{
424 return inb(ap->ioaddr.status_addr);
425}
426
427/**
428 * ata_check_status_mmio - Read device status reg & clear interrupt
429 * @ap: port where the device is
430 *
431 * Reads ATA taskfile status register for currently-selected device
Edward Falk0baab862005-06-02 18:17:13 -0400432 * via MMIO and return its value. This also clears pending interrupts
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 * from this device
434 *
435 * LOCKING:
436 * Inherited from caller.
437 */
438static u8 ata_check_status_mmio(struct ata_port *ap)
439{
440 return readb((void __iomem *) ap->ioaddr.status_addr);
441}
442
Edward Falk0baab862005-06-02 18:17:13 -0400443
444/**
445 * ata_check_status - Read device status reg & clear interrupt
446 * @ap: port where the device is
447 *
448 * Reads ATA taskfile status register for currently-selected device
449 * and return its value. This also clears pending interrupts
450 * from this device
451 *
452 * May be used as the check_status() entry in ata_port_operations.
453 *
454 * LOCKING:
455 * Inherited from caller.
456 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457u8 ata_check_status(struct ata_port *ap)
458{
459 if (ap->flags & ATA_FLAG_MMIO)
460 return ata_check_status_mmio(ap);
461 return ata_check_status_pio(ap);
462}
463
Edward Falk0baab862005-06-02 18:17:13 -0400464
465/**
466 * ata_altstatus - Read device alternate status reg
467 * @ap: port where the device is
468 *
469 * Reads ATA taskfile alternate status register for
470 * currently-selected device and return its value.
471 *
472 * Note: may NOT be used as the check_altstatus() entry in
473 * ata_port_operations.
474 *
475 * LOCKING:
476 * Inherited from caller.
477 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478u8 ata_altstatus(struct ata_port *ap)
479{
480 if (ap->ops->check_altstatus)
481 return ap->ops->check_altstatus(ap);
482
483 if (ap->flags & ATA_FLAG_MMIO)
484 return readb((void __iomem *)ap->ioaddr.altstatus_addr);
485 return inb(ap->ioaddr.altstatus_addr);
486}
487
Edward Falk0baab862005-06-02 18:17:13 -0400488
489/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 * ata_tf_to_fis - Convert ATA taskfile to SATA FIS structure
491 * @tf: Taskfile to convert
492 * @fis: Buffer into which data will output
493 * @pmp: Port multiplier port
494 *
495 * Converts a standard ATA taskfile to a Serial ATA
496 * FIS structure (Register - Host to Device).
497 *
498 * LOCKING:
499 * Inherited from caller.
500 */
501
Jeff Garzik057ace52005-10-22 14:27:05 -0400502void ata_tf_to_fis(const struct ata_taskfile *tf, u8 *fis, u8 pmp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503{
504 fis[0] = 0x27; /* Register - Host to Device FIS */
505 fis[1] = (pmp & 0xf) | (1 << 7); /* Port multiplier number,
506 bit 7 indicates Command FIS */
507 fis[2] = tf->command;
508 fis[3] = tf->feature;
509
510 fis[4] = tf->lbal;
511 fis[5] = tf->lbam;
512 fis[6] = tf->lbah;
513 fis[7] = tf->device;
514
515 fis[8] = tf->hob_lbal;
516 fis[9] = tf->hob_lbam;
517 fis[10] = tf->hob_lbah;
518 fis[11] = tf->hob_feature;
519
520 fis[12] = tf->nsect;
521 fis[13] = tf->hob_nsect;
522 fis[14] = 0;
523 fis[15] = tf->ctl;
524
525 fis[16] = 0;
526 fis[17] = 0;
527 fis[18] = 0;
528 fis[19] = 0;
529}
530
531/**
532 * ata_tf_from_fis - Convert SATA FIS to ATA taskfile
533 * @fis: Buffer from which data will be input
534 * @tf: Taskfile to output
535 *
Mark Lorde12a1be2005-11-12 18:55:45 -0500536 * Converts a serial ATA FIS structure to a standard ATA taskfile.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 *
538 * LOCKING:
539 * Inherited from caller.
540 */
541
Jeff Garzik057ace52005-10-22 14:27:05 -0400542void ata_tf_from_fis(const u8 *fis, struct ata_taskfile *tf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543{
544 tf->command = fis[2]; /* status */
545 tf->feature = fis[3]; /* error */
546
547 tf->lbal = fis[4];
548 tf->lbam = fis[5];
549 tf->lbah = fis[6];
550 tf->device = fis[7];
551
552 tf->hob_lbal = fis[8];
553 tf->hob_lbam = fis[9];
554 tf->hob_lbah = fis[10];
555
556 tf->nsect = fis[12];
557 tf->hob_nsect = fis[13];
558}
559
Albert Lee8cbd6df2005-10-12 15:06:27 +0800560static const u8 ata_rw_cmds[] = {
561 /* pio multi */
562 ATA_CMD_READ_MULTI,
563 ATA_CMD_WRITE_MULTI,
564 ATA_CMD_READ_MULTI_EXT,
565 ATA_CMD_WRITE_MULTI_EXT,
566 /* pio */
567 ATA_CMD_PIO_READ,
568 ATA_CMD_PIO_WRITE,
569 ATA_CMD_PIO_READ_EXT,
570 ATA_CMD_PIO_WRITE_EXT,
571 /* dma */
572 ATA_CMD_READ,
573 ATA_CMD_WRITE,
574 ATA_CMD_READ_EXT,
575 ATA_CMD_WRITE_EXT
576};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577
578/**
Albert Lee8cbd6df2005-10-12 15:06:27 +0800579 * ata_rwcmd_protocol - set taskfile r/w commands and protocol
580 * @qc: command to examine and configure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 *
Albert Lee8cbd6df2005-10-12 15:06:27 +0800582 * Examine the device configuration and tf->flags to calculate
583 * the proper read/write commands and protocol to use.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 *
585 * LOCKING:
586 * caller.
587 */
Albert Lee8cbd6df2005-10-12 15:06:27 +0800588void ata_rwcmd_protocol(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589{
Albert Lee8cbd6df2005-10-12 15:06:27 +0800590 struct ata_taskfile *tf = &qc->tf;
591 struct ata_device *dev = qc->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592
Albert Lee8cbd6df2005-10-12 15:06:27 +0800593 int index, lba48, write;
594
595 lba48 = (tf->flags & ATA_TFLAG_LBA48) ? 2 : 0;
596 write = (tf->flags & ATA_TFLAG_WRITE) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
Albert Lee8cbd6df2005-10-12 15:06:27 +0800598 if (dev->flags & ATA_DFLAG_PIO) {
599 tf->protocol = ATA_PROT_PIO;
600 index = dev->multi_count ? 0 : 4;
601 } else {
602 tf->protocol = ATA_PROT_DMA;
603 index = 8;
604 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605
Albert Lee8cbd6df2005-10-12 15:06:27 +0800606 tf->command = ata_rw_cmds[index + lba48 + write];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607}
608
Arjan van de Ven98ac62d2005-11-28 10:06:23 +0100609static const char * const xfer_mode_str[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 "UDMA/16",
611 "UDMA/25",
612 "UDMA/33",
613 "UDMA/44",
614 "UDMA/66",
615 "UDMA/100",
616 "UDMA/133",
617 "UDMA7",
618 "MWDMA0",
619 "MWDMA1",
620 "MWDMA2",
621 "PIO0",
622 "PIO1",
623 "PIO2",
624 "PIO3",
625 "PIO4",
626};
627
628/**
629 * ata_udma_string - convert UDMA bit offset to string
630 * @mask: mask of bits supported; only highest bit counts.
631 *
632 * Determine string which represents the highest speed
633 * (highest bit in @udma_mask).
634 *
635 * LOCKING:
636 * None.
637 *
638 * RETURNS:
639 * Constant C string representing highest speed listed in
640 * @udma_mask, or the constant C string "<n/a>".
641 */
642
643static const char *ata_mode_string(unsigned int mask)
644{
645 int i;
646
647 for (i = 7; i >= 0; i--)
648 if (mask & (1 << i))
649 goto out;
650 for (i = ATA_SHIFT_MWDMA + 2; i >= ATA_SHIFT_MWDMA; i--)
651 if (mask & (1 << i))
652 goto out;
653 for (i = ATA_SHIFT_PIO + 4; i >= ATA_SHIFT_PIO; i--)
654 if (mask & (1 << i))
655 goto out;
656
657 return "<n/a>";
658
659out:
660 return xfer_mode_str[i];
661}
662
663/**
664 * ata_pio_devchk - PATA device presence detection
665 * @ap: ATA channel to examine
666 * @device: Device to examine (starting at zero)
667 *
668 * This technique was originally described in
669 * Hale Landis's ATADRVR (www.ata-atapi.com), and
670 * later found its way into the ATA/ATAPI spec.
671 *
672 * Write a pattern to the ATA shadow registers,
673 * and if a device is present, it will respond by
674 * correctly storing and echoing back the
675 * ATA shadow register contents.
676 *
677 * LOCKING:
678 * caller.
679 */
680
681static unsigned int ata_pio_devchk(struct ata_port *ap,
682 unsigned int device)
683{
684 struct ata_ioports *ioaddr = &ap->ioaddr;
685 u8 nsect, lbal;
686
687 ap->ops->dev_select(ap, device);
688
689 outb(0x55, ioaddr->nsect_addr);
690 outb(0xaa, ioaddr->lbal_addr);
691
692 outb(0xaa, ioaddr->nsect_addr);
693 outb(0x55, ioaddr->lbal_addr);
694
695 outb(0x55, ioaddr->nsect_addr);
696 outb(0xaa, ioaddr->lbal_addr);
697
698 nsect = inb(ioaddr->nsect_addr);
699 lbal = inb(ioaddr->lbal_addr);
700
701 if ((nsect == 0x55) && (lbal == 0xaa))
702 return 1; /* we found a device */
703
704 return 0; /* nothing found */
705}
706
707/**
708 * ata_mmio_devchk - PATA device presence detection
709 * @ap: ATA channel to examine
710 * @device: Device to examine (starting at zero)
711 *
712 * This technique was originally described in
713 * Hale Landis's ATADRVR (www.ata-atapi.com), and
714 * later found its way into the ATA/ATAPI spec.
715 *
716 * Write a pattern to the ATA shadow registers,
717 * and if a device is present, it will respond by
718 * correctly storing and echoing back the
719 * ATA shadow register contents.
720 *
721 * LOCKING:
722 * caller.
723 */
724
725static unsigned int ata_mmio_devchk(struct ata_port *ap,
726 unsigned int device)
727{
728 struct ata_ioports *ioaddr = &ap->ioaddr;
729 u8 nsect, lbal;
730
731 ap->ops->dev_select(ap, device);
732
733 writeb(0x55, (void __iomem *) ioaddr->nsect_addr);
734 writeb(0xaa, (void __iomem *) ioaddr->lbal_addr);
735
736 writeb(0xaa, (void __iomem *) ioaddr->nsect_addr);
737 writeb(0x55, (void __iomem *) ioaddr->lbal_addr);
738
739 writeb(0x55, (void __iomem *) ioaddr->nsect_addr);
740 writeb(0xaa, (void __iomem *) ioaddr->lbal_addr);
741
742 nsect = readb((void __iomem *) ioaddr->nsect_addr);
743 lbal = readb((void __iomem *) ioaddr->lbal_addr);
744
745 if ((nsect == 0x55) && (lbal == 0xaa))
746 return 1; /* we found a device */
747
748 return 0; /* nothing found */
749}
750
751/**
752 * ata_devchk - PATA device presence detection
753 * @ap: ATA channel to examine
754 * @device: Device to examine (starting at zero)
755 *
756 * Dispatch ATA device presence detection, depending
757 * on whether we are using PIO or MMIO to talk to the
758 * ATA shadow registers.
759 *
760 * LOCKING:
761 * caller.
762 */
763
764static unsigned int ata_devchk(struct ata_port *ap,
765 unsigned int device)
766{
767 if (ap->flags & ATA_FLAG_MMIO)
768 return ata_mmio_devchk(ap, device);
769 return ata_pio_devchk(ap, device);
770}
771
772/**
773 * ata_dev_classify - determine device type based on ATA-spec signature
774 * @tf: ATA taskfile register set for device to be identified
775 *
776 * Determine from taskfile register contents whether a device is
777 * ATA or ATAPI, as per "Signature and persistence" section
778 * of ATA/PI spec (volume 1, sect 5.14).
779 *
780 * LOCKING:
781 * None.
782 *
783 * RETURNS:
784 * Device type, %ATA_DEV_ATA, %ATA_DEV_ATAPI, or %ATA_DEV_UNKNOWN
785 * the event of failure.
786 */
787
Jeff Garzik057ace52005-10-22 14:27:05 -0400788unsigned int ata_dev_classify(const struct ata_taskfile *tf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789{
790 /* Apple's open source Darwin code hints that some devices only
791 * put a proper signature into the LBA mid/high registers,
792 * So, we only check those. It's sufficient for uniqueness.
793 */
794
795 if (((tf->lbam == 0) && (tf->lbah == 0)) ||
796 ((tf->lbam == 0x3c) && (tf->lbah == 0xc3))) {
797 DPRINTK("found ATA device by sig\n");
798 return ATA_DEV_ATA;
799 }
800
801 if (((tf->lbam == 0x14) && (tf->lbah == 0xeb)) ||
802 ((tf->lbam == 0x69) && (tf->lbah == 0x96))) {
803 DPRINTK("found ATAPI device by sig\n");
804 return ATA_DEV_ATAPI;
805 }
806
807 DPRINTK("unknown device\n");
808 return ATA_DEV_UNKNOWN;
809}
810
811/**
812 * ata_dev_try_classify - Parse returned ATA device signature
813 * @ap: ATA channel to examine
814 * @device: Device to examine (starting at zero)
815 *
816 * After an event -- SRST, E.D.D., or SATA COMRESET -- occurs,
817 * an ATA/ATAPI-defined set of values is placed in the ATA
818 * shadow registers, indicating the results of device detection
819 * and diagnostics.
820 *
821 * Select the ATA device, and read the values from the ATA shadow
822 * registers. Then parse according to the Error register value,
823 * and the spec-defined values examined by ata_dev_classify().
824 *
825 * LOCKING:
826 * caller.
827 */
828
829static u8 ata_dev_try_classify(struct ata_port *ap, unsigned int device)
830{
831 struct ata_device *dev = &ap->device[device];
832 struct ata_taskfile tf;
833 unsigned int class;
834 u8 err;
835
836 ap->ops->dev_select(ap, device);
837
838 memset(&tf, 0, sizeof(tf));
839
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 ap->ops->tf_read(ap, &tf);
Jeff Garzik0169e282005-10-29 21:25:10 -0400841 err = tf.feature;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842
843 dev->class = ATA_DEV_NONE;
844
845 /* see if device passed diags */
846 if (err == 1)
847 /* do nothing */ ;
848 else if ((device == 0) && (err == 0x81))
849 /* do nothing */ ;
850 else
851 return err;
852
853 /* determine if device if ATA or ATAPI */
854 class = ata_dev_classify(&tf);
855 if (class == ATA_DEV_UNKNOWN)
856 return err;
857 if ((class == ATA_DEV_ATA) && (ata_chk_status(ap) == 0))
858 return err;
859
860 dev->class = class;
861
862 return err;
863}
864
865/**
866 * ata_dev_id_string - Convert IDENTIFY DEVICE page into string
867 * @id: IDENTIFY DEVICE results we will examine
868 * @s: string into which data is output
869 * @ofs: offset into identify device page
870 * @len: length of string to return. must be an even number.
871 *
872 * The strings in the IDENTIFY DEVICE page are broken up into
873 * 16-bit chunks. Run through the string, and output each
874 * 8-bit chunk linearly, regardless of platform.
875 *
876 * LOCKING:
877 * caller.
878 */
879
Jeff Garzik057ace52005-10-22 14:27:05 -0400880void ata_dev_id_string(const u16 *id, unsigned char *s,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 unsigned int ofs, unsigned int len)
882{
883 unsigned int c;
884
885 while (len > 0) {
886 c = id[ofs] >> 8;
887 *s = c;
888 s++;
889
890 c = id[ofs] & 0xff;
891 *s = c;
892 s++;
893
894 ofs++;
895 len -= 2;
896 }
897}
898
Edward Falk0baab862005-06-02 18:17:13 -0400899
900/**
901 * ata_noop_dev_select - Select device 0/1 on ATA bus
902 * @ap: ATA channel to manipulate
903 * @device: ATA device (numbered from zero) to select
904 *
905 * This function performs no actual function.
906 *
907 * May be used as the dev_select() entry in ata_port_operations.
908 *
909 * LOCKING:
910 * caller.
911 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912void ata_noop_dev_select (struct ata_port *ap, unsigned int device)
913{
914}
915
Edward Falk0baab862005-06-02 18:17:13 -0400916
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917/**
918 * ata_std_dev_select - Select device 0/1 on ATA bus
919 * @ap: ATA channel to manipulate
920 * @device: ATA device (numbered from zero) to select
921 *
922 * Use the method defined in the ATA specification to
923 * make either device 0, or device 1, active on the
Edward Falk0baab862005-06-02 18:17:13 -0400924 * ATA channel. Works with both PIO and MMIO.
925 *
926 * May be used as the dev_select() entry in ata_port_operations.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 *
928 * LOCKING:
929 * caller.
930 */
931
932void ata_std_dev_select (struct ata_port *ap, unsigned int device)
933{
934 u8 tmp;
935
936 if (device == 0)
937 tmp = ATA_DEVICE_OBS;
938 else
939 tmp = ATA_DEVICE_OBS | ATA_DEV1;
940
941 if (ap->flags & ATA_FLAG_MMIO) {
942 writeb(tmp, (void __iomem *) ap->ioaddr.device_addr);
943 } else {
944 outb(tmp, ap->ioaddr.device_addr);
945 }
946 ata_pause(ap); /* needed; also flushes, for mmio */
947}
948
949/**
950 * ata_dev_select - Select device 0/1 on ATA bus
951 * @ap: ATA channel to manipulate
952 * @device: ATA device (numbered from zero) to select
953 * @wait: non-zero to wait for Status register BSY bit to clear
954 * @can_sleep: non-zero if context allows sleeping
955 *
956 * Use the method defined in the ATA specification to
957 * make either device 0, or device 1, active on the
958 * ATA channel.
959 *
960 * This is a high-level version of ata_std_dev_select(),
961 * which additionally provides the services of inserting
962 * the proper pauses and status polling, where needed.
963 *
964 * LOCKING:
965 * caller.
966 */
967
968void ata_dev_select(struct ata_port *ap, unsigned int device,
969 unsigned int wait, unsigned int can_sleep)
970{
971 VPRINTK("ENTER, ata%u: device %u, wait %u\n",
972 ap->id, device, wait);
973
974 if (wait)
975 ata_wait_idle(ap);
976
977 ap->ops->dev_select(ap, device);
978
979 if (wait) {
980 if (can_sleep && ap->device[device].class == ATA_DEV_ATAPI)
981 msleep(150);
982 ata_wait_idle(ap);
983 }
984}
985
986/**
987 * ata_dump_id - IDENTIFY DEVICE info debugging output
988 * @dev: Device whose IDENTIFY DEVICE page we will dump
989 *
990 * Dump selected 16-bit words from a detected device's
991 * IDENTIFY PAGE page.
992 *
993 * LOCKING:
994 * caller.
995 */
996
Jeff Garzik057ace52005-10-22 14:27:05 -0400997static inline void ata_dump_id(const struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998{
999 DPRINTK("49==0x%04x "
1000 "53==0x%04x "
1001 "63==0x%04x "
1002 "64==0x%04x "
1003 "75==0x%04x \n",
1004 dev->id[49],
1005 dev->id[53],
1006 dev->id[63],
1007 dev->id[64],
1008 dev->id[75]);
1009 DPRINTK("80==0x%04x "
1010 "81==0x%04x "
1011 "82==0x%04x "
1012 "83==0x%04x "
1013 "84==0x%04x \n",
1014 dev->id[80],
1015 dev->id[81],
1016 dev->id[82],
1017 dev->id[83],
1018 dev->id[84]);
1019 DPRINTK("88==0x%04x "
1020 "93==0x%04x\n",
1021 dev->id[88],
1022 dev->id[93]);
1023}
1024
Alan Cox11e29e22005-10-21 18:46:32 -04001025/*
1026 * Compute the PIO modes available for this device. This is not as
1027 * trivial as it seems if we must consider early devices correctly.
1028 *
1029 * FIXME: pre IDE drive timing (do we care ?).
1030 */
1031
Jeff Garzik057ace52005-10-22 14:27:05 -04001032static unsigned int ata_pio_modes(const struct ata_device *adev)
Alan Cox11e29e22005-10-21 18:46:32 -04001033{
1034 u16 modes;
1035
1036 /* Usual case. Word 53 indicates word 88 is valid */
1037 if (adev->id[ATA_ID_FIELD_VALID] & (1 << 2)) {
1038 modes = adev->id[ATA_ID_PIO_MODES] & 0x03;
1039 modes <<= 3;
1040 modes |= 0x7;
1041 return modes;
1042 }
1043
1044 /* If word 88 isn't valid then Word 51 holds the PIO timing number
1045 for the maximum. Turn it into a mask and return it */
1046 modes = (2 << (adev->id[ATA_ID_OLD_PIO_MODES] & 0xFF)) - 1 ;
1047 return modes;
1048}
1049
Jeff Garzik64f043d2005-11-17 10:50:01 -05001050static int ata_qc_wait_err(struct ata_queued_cmd *qc,
1051 struct completion *wait)
1052{
1053 int rc = 0;
1054
1055 if (wait_for_completion_timeout(wait, 30 * HZ) < 1) {
1056 /* timeout handling */
Albert Leea22e2eb2005-12-05 15:38:02 +08001057 qc->err_mask |= ac_err_mask(ata_chk_status(qc->ap));
Jeff Garzik64f043d2005-11-17 10:50:01 -05001058
Albert Leea22e2eb2005-12-05 15:38:02 +08001059 if (!qc->err_mask) {
Jeff Garzik64f043d2005-11-17 10:50:01 -05001060 printk(KERN_WARNING "ata%u: slow completion (cmd %x)\n",
1061 qc->ap->id, qc->tf.command);
1062 } else {
1063 printk(KERN_WARNING "ata%u: qc timeout (cmd %x)\n",
1064 qc->ap->id, qc->tf.command);
1065 rc = -EIO;
1066 }
1067
Albert Leea22e2eb2005-12-05 15:38:02 +08001068 ata_qc_complete(qc);
Jeff Garzik64f043d2005-11-17 10:50:01 -05001069 }
1070
1071 return rc;
1072}
1073
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074/**
1075 * ata_dev_identify - obtain IDENTIFY x DEVICE page
1076 * @ap: port on which device we wish to probe resides
1077 * @device: device bus address, starting at zero
1078 *
1079 * Following bus reset, we issue the IDENTIFY [PACKET] DEVICE
1080 * command, and read back the 512-byte device information page.
1081 * The device information page is fed to us via the standard
1082 * PIO-IN protocol, but we hand-code it here. (TODO: investigate
1083 * using standard PIO-IN paths)
1084 *
1085 * After reading the device information page, we use several
1086 * bits of information from it to initialize data structures
1087 * that will be used during the lifetime of the ata_device.
1088 * Other data from the info page is used to disqualify certain
1089 * older ATA devices we do not wish to support.
1090 *
1091 * LOCKING:
1092 * Inherited from caller. Some functions called by this function
1093 * obtain the host_set lock.
1094 */
1095
1096static void ata_dev_identify(struct ata_port *ap, unsigned int device)
1097{
1098 struct ata_device *dev = &ap->device[device];
Albert Lee8bf62ec2005-05-12 15:29:42 -04001099 unsigned int major_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 u16 tmp;
1101 unsigned long xfer_modes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 unsigned int using_edd;
1103 DECLARE_COMPLETION(wait);
1104 struct ata_queued_cmd *qc;
1105 unsigned long flags;
1106 int rc;
1107
1108 if (!ata_dev_present(dev)) {
1109 DPRINTK("ENTER/EXIT (host %u, dev %u) -- nodev\n",
1110 ap->id, device);
1111 return;
1112 }
1113
1114 if (ap->flags & (ATA_FLAG_SRST | ATA_FLAG_SATA_RESET))
1115 using_edd = 0;
1116 else
1117 using_edd = 1;
1118
1119 DPRINTK("ENTER, host %u, dev %u\n", ap->id, device);
1120
1121 assert (dev->class == ATA_DEV_ATA || dev->class == ATA_DEV_ATAPI ||
1122 dev->class == ATA_DEV_NONE);
1123
1124 ata_dev_select(ap, device, 1, 1); /* select device 0/1 */
1125
1126 qc = ata_qc_new_init(ap, dev);
1127 BUG_ON(qc == NULL);
1128
1129 ata_sg_init_one(qc, dev->id, sizeof(dev->id));
1130 qc->dma_dir = DMA_FROM_DEVICE;
1131 qc->tf.protocol = ATA_PROT_PIO;
1132 qc->nsect = 1;
1133
1134retry:
1135 if (dev->class == ATA_DEV_ATA) {
1136 qc->tf.command = ATA_CMD_ID_ATA;
1137 DPRINTK("do ATA identify\n");
1138 } else {
1139 qc->tf.command = ATA_CMD_ID_ATAPI;
1140 DPRINTK("do ATAPI identify\n");
1141 }
1142
1143 qc->waiting = &wait;
1144 qc->complete_fn = ata_qc_complete_noop;
1145
1146 spin_lock_irqsave(&ap->host_set->lock, flags);
1147 rc = ata_qc_issue(qc);
1148 spin_unlock_irqrestore(&ap->host_set->lock, flags);
1149
1150 if (rc)
1151 goto err_out;
1152 else
Jeff Garzik64f043d2005-11-17 10:50:01 -05001153 ata_qc_wait_err(qc, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154
Jeff Garzik0169e282005-10-29 21:25:10 -04001155 spin_lock_irqsave(&ap->host_set->lock, flags);
1156 ap->ops->tf_read(ap, &qc->tf);
1157 spin_unlock_irqrestore(&ap->host_set->lock, flags);
1158
1159 if (qc->tf.command & ATA_ERR) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 /*
1161 * arg! EDD works for all test cases, but seems to return
1162 * the ATA signature for some ATAPI devices. Until the
1163 * reason for this is found and fixed, we fix up the mess
1164 * here. If IDENTIFY DEVICE returns command aborted
1165 * (as ATAPI devices do), then we issue an
1166 * IDENTIFY PACKET DEVICE.
1167 *
1168 * ATA software reset (SRST, the default) does not appear
1169 * to have this problem.
1170 */
Albert Lee7c398332005-11-09 13:03:30 +08001171 if ((using_edd) && (dev->class == ATA_DEV_ATA)) {
Jeff Garzik0169e282005-10-29 21:25:10 -04001172 u8 err = qc->tf.feature;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 if (err & ATA_ABORTED) {
1174 dev->class = ATA_DEV_ATAPI;
1175 qc->cursg = 0;
1176 qc->cursg_ofs = 0;
1177 qc->cursect = 0;
1178 qc->nsect = 1;
Albert Leea22e2eb2005-12-05 15:38:02 +08001179 qc->err_mask = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 goto retry;
1181 }
1182 }
1183 goto err_out;
1184 }
1185
1186 swap_buf_le16(dev->id, ATA_ID_WORDS);
1187
1188 /* print device capabilities */
1189 printk(KERN_DEBUG "ata%u: dev %u cfg "
1190 "49:%04x 82:%04x 83:%04x 84:%04x 85:%04x 86:%04x 87:%04x 88:%04x\n",
1191 ap->id, device, dev->id[49],
1192 dev->id[82], dev->id[83], dev->id[84],
1193 dev->id[85], dev->id[86], dev->id[87],
1194 dev->id[88]);
1195
1196 /*
1197 * common ATA, ATAPI feature tests
1198 */
1199
Albert Lee8bf62ec2005-05-12 15:29:42 -04001200 /* we require DMA support (bits 8 of word 49) */
1201 if (!ata_id_has_dma(dev->id)) {
1202 printk(KERN_DEBUG "ata%u: no dma\n", ap->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 goto err_out_nosup;
1204 }
1205
1206 /* quick-n-dirty find max transfer mode; for printk only */
1207 xfer_modes = dev->id[ATA_ID_UDMA_MODES];
1208 if (!xfer_modes)
1209 xfer_modes = (dev->id[ATA_ID_MWDMA_MODES]) << ATA_SHIFT_MWDMA;
Alan Cox11e29e22005-10-21 18:46:32 -04001210 if (!xfer_modes)
1211 xfer_modes = ata_pio_modes(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212
1213 ata_dump_id(dev);
1214
1215 /* ATA-specific feature tests */
1216 if (dev->class == ATA_DEV_ATA) {
1217 if (!ata_id_is_ata(dev->id)) /* sanity check */
1218 goto err_out_nosup;
1219
Albert Lee8bf62ec2005-05-12 15:29:42 -04001220 /* get major version */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 tmp = dev->id[ATA_ID_MAJOR_VER];
Albert Lee8bf62ec2005-05-12 15:29:42 -04001222 for (major_version = 14; major_version >= 1; major_version--)
1223 if (tmp & (1 << major_version))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 break;
1225
Albert Lee8bf62ec2005-05-12 15:29:42 -04001226 /*
1227 * The exact sequence expected by certain pre-ATA4 drives is:
1228 * SRST RESET
1229 * IDENTIFY
1230 * INITIALIZE DEVICE PARAMETERS
1231 * anything else..
1232 * Some drives were very specific about that exact sequence.
1233 */
Albert Lee59a10b12005-10-12 15:09:42 +08001234 if (major_version < 4 || (!ata_id_has_lba(dev->id))) {
Albert Lee8bf62ec2005-05-12 15:29:42 -04001235 ata_dev_init_params(ap, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236
Albert Lee59a10b12005-10-12 15:09:42 +08001237 /* current CHS translation info (id[53-58]) might be
1238 * changed. reread the identify device info.
1239 */
1240 ata_dev_reread_id(ap, dev);
1241 }
1242
Albert Lee8bf62ec2005-05-12 15:29:42 -04001243 if (ata_id_has_lba(dev->id)) {
1244 dev->flags |= ATA_DFLAG_LBA;
1245
1246 if (ata_id_has_lba48(dev->id)) {
1247 dev->flags |= ATA_DFLAG_LBA48;
1248 dev->n_sectors = ata_id_u64(dev->id, 100);
1249 } else {
1250 dev->n_sectors = ata_id_u32(dev->id, 60);
1251 }
1252
1253 /* print device info to dmesg */
1254 printk(KERN_INFO "ata%u: dev %u ATA-%d, max %s, %Lu sectors:%s\n",
1255 ap->id, device,
1256 major_version,
1257 ata_mode_string(xfer_modes),
1258 (unsigned long long)dev->n_sectors,
1259 dev->flags & ATA_DFLAG_LBA48 ? " LBA48" : " LBA");
1260 } else {
1261 /* CHS */
1262
1263 /* Default translation */
1264 dev->cylinders = dev->id[1];
1265 dev->heads = dev->id[3];
1266 dev->sectors = dev->id[6];
1267 dev->n_sectors = dev->cylinders * dev->heads * dev->sectors;
1268
1269 if (ata_id_current_chs_valid(dev->id)) {
1270 /* Current CHS translation is valid. */
1271 dev->cylinders = dev->id[54];
1272 dev->heads = dev->id[55];
1273 dev->sectors = dev->id[56];
1274
1275 dev->n_sectors = ata_id_u32(dev->id, 57);
1276 }
1277
1278 /* print device info to dmesg */
1279 printk(KERN_INFO "ata%u: dev %u ATA-%d, max %s, %Lu sectors: CHS %d/%d/%d\n",
1280 ap->id, device,
1281 major_version,
1282 ata_mode_string(xfer_modes),
1283 (unsigned long long)dev->n_sectors,
1284 (int)dev->cylinders, (int)dev->heads, (int)dev->sectors);
1285
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 }
1287
Albert Lee07f6f7d2005-11-01 19:33:20 +08001288 if (dev->id[59] & 0x100) {
1289 dev->multi_count = dev->id[59] & 0xff;
1290 DPRINTK("ata%u: dev %u multi count %u\n",
1291 ap->id, device, dev->multi_count);
1292 }
1293
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 ap->host->max_cmd_len = 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 }
1296
1297 /* ATAPI-specific feature tests */
Jeff Garzik2c13b7c2005-11-14 14:14:16 -05001298 else if (dev->class == ATA_DEV_ATAPI) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 if (ata_id_is_ata(dev->id)) /* sanity check */
1300 goto err_out_nosup;
1301
1302 rc = atapi_cdb_len(dev->id);
1303 if ((rc < 12) || (rc > ATAPI_CDB_LEN)) {
1304 printk(KERN_WARNING "ata%u: unsupported CDB len\n", ap->id);
1305 goto err_out_nosup;
1306 }
1307 ap->cdb_len = (unsigned int) rc;
1308 ap->host->max_cmd_len = (unsigned char) ap->cdb_len;
1309
Albert Lee312f7da2005-09-27 17:38:03 +08001310 if (ata_id_cdb_intr(dev->id))
1311 dev->flags |= ATA_DFLAG_CDB_INTR;
1312
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 /* print device info to dmesg */
1314 printk(KERN_INFO "ata%u: dev %u ATAPI, max %s\n",
1315 ap->id, device,
1316 ata_mode_string(xfer_modes));
1317 }
1318
1319 DPRINTK("EXIT, drv_stat = 0x%x\n", ata_chk_status(ap));
1320 return;
1321
1322err_out_nosup:
1323 printk(KERN_WARNING "ata%u: dev %u not supported, ignoring\n",
1324 ap->id, device);
1325err_out:
1326 dev->class++; /* converts ATA_DEV_xxx into ATA_DEV_xxx_UNSUP */
1327 DPRINTK("EXIT, err\n");
1328}
1329
Brad Campbell6f2f3812005-05-12 15:07:47 -04001330
Jeff Garzik057ace52005-10-22 14:27:05 -04001331static inline u8 ata_dev_knobble(const struct ata_port *ap)
Brad Campbell6f2f3812005-05-12 15:07:47 -04001332{
1333 return ((ap->cbl == ATA_CBL_SATA) && (!ata_id_is_sata(ap->device->id)));
1334}
1335
1336/**
1337 * ata_dev_config - Run device specific handlers and check for
1338 * SATA->PATA bridges
Jeff Garzik8a60a072005-07-31 13:13:24 -04001339 * @ap: Bus
Brad Campbell6f2f3812005-05-12 15:07:47 -04001340 * @i: Device
1341 *
1342 * LOCKING:
1343 */
Jeff Garzik8a60a072005-07-31 13:13:24 -04001344
Brad Campbell6f2f3812005-05-12 15:07:47 -04001345void ata_dev_config(struct ata_port *ap, unsigned int i)
1346{
1347 /* limit bridge transfers to udma5, 200 sectors */
1348 if (ata_dev_knobble(ap)) {
1349 printk(KERN_INFO "ata%u(%u): applying bridge limits\n",
1350 ap->id, ap->device->devno);
1351 ap->udma_mask &= ATA_UDMA5;
1352 ap->host->max_sectors = ATA_MAX_SECTORS;
1353 ap->host->hostt->max_sectors = ATA_MAX_SECTORS;
1354 ap->device->flags |= ATA_DFLAG_LOCK_SECTORS;
1355 }
1356
1357 if (ap->ops->dev_config)
1358 ap->ops->dev_config(ap, &ap->device[i]);
1359}
1360
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361/**
1362 * ata_bus_probe - Reset and probe ATA bus
1363 * @ap: Bus to probe
1364 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04001365 * Master ATA bus probing function. Initiates a hardware-dependent
1366 * bus reset, then attempts to identify any devices found on
1367 * the bus.
1368 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001370 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 *
1372 * RETURNS:
1373 * Zero on success, non-zero on error.
1374 */
1375
1376static int ata_bus_probe(struct ata_port *ap)
1377{
1378 unsigned int i, found = 0;
1379
1380 ap->ops->phy_reset(ap);
1381 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1382 goto err_out;
1383
1384 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1385 ata_dev_identify(ap, i);
1386 if (ata_dev_present(&ap->device[i])) {
1387 found = 1;
Brad Campbell6f2f3812005-05-12 15:07:47 -04001388 ata_dev_config(ap,i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 }
1390 }
1391
1392 if ((!found) || (ap->flags & ATA_FLAG_PORT_DISABLED))
1393 goto err_out_disable;
1394
1395 ata_set_mode(ap);
1396 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1397 goto err_out_disable;
1398
1399 return 0;
1400
1401err_out_disable:
1402 ap->ops->port_disable(ap);
1403err_out:
1404 return -1;
1405}
1406
1407/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04001408 * ata_port_probe - Mark port as enabled
1409 * @ap: Port for which we indicate enablement
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04001411 * Modify @ap data structure such that the system
1412 * thinks that the entire port is enabled.
1413 *
1414 * LOCKING: host_set lock, or some other form of
1415 * serialization.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 */
1417
1418void ata_port_probe(struct ata_port *ap)
1419{
1420 ap->flags &= ~ATA_FLAG_PORT_DISABLED;
1421}
1422
1423/**
Jeff Garzik780a87f2005-05-30 15:41:05 -04001424 * __sata_phy_reset - Wake/reset a low-level SATA PHY
1425 * @ap: SATA port associated with target SATA PHY.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001427 * This function issues commands to standard SATA Sxxx
1428 * PHY registers, to wake up the phy (and device), and
1429 * clear any reset condition.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 *
1431 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001432 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 *
1434 */
1435void __sata_phy_reset(struct ata_port *ap)
1436{
1437 u32 sstatus;
1438 unsigned long timeout = jiffies + (HZ * 5);
1439
1440 if (ap->flags & ATA_FLAG_SATA_RESET) {
Brett Russcdcca892005-03-28 15:10:27 -05001441 /* issue phy wake/reset */
1442 scr_write_flush(ap, SCR_CONTROL, 0x301);
Tejun Heo62ba2842005-06-26 23:27:19 +09001443 /* Couldn't find anything in SATA I/II specs, but
1444 * AHCI-1.1 10.4.2 says at least 1 ms. */
1445 mdelay(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 }
Brett Russcdcca892005-03-28 15:10:27 -05001447 scr_write_flush(ap, SCR_CONTROL, 0x300); /* phy wake/clear reset */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448
1449 /* wait for phy to become ready, if necessary */
1450 do {
1451 msleep(200);
1452 sstatus = scr_read(ap, SCR_STATUS);
1453 if ((sstatus & 0xf) != 1)
1454 break;
1455 } while (time_before(jiffies, timeout));
1456
1457 /* TODO: phy layer with polling, timeouts, etc. */
Jeff Garzik656563e2005-11-20 03:36:45 -05001458 sstatus = scr_read(ap, SCR_STATUS);
1459 if (sata_dev_present(ap)) {
1460 const char *speed;
1461 u32 tmp;
1462
1463 tmp = (sstatus >> 4) & 0xf;
1464 if (tmp & (1 << 0))
1465 speed = "1.5";
1466 else if (tmp & (1 << 1))
1467 speed = "3.0";
1468 else
1469 speed = "<unknown>";
1470 printk(KERN_INFO "ata%u: SATA link up %s Gbps (SStatus %X)\n",
1471 ap->id, speed, sstatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 ata_port_probe(ap);
Jeff Garzik656563e2005-11-20 03:36:45 -05001473 } else {
1474 printk(KERN_INFO "ata%u: SATA link down (SStatus %X)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 ap->id, sstatus);
1476 ata_port_disable(ap);
1477 }
1478
1479 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1480 return;
1481
1482 if (ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT)) {
1483 ata_port_disable(ap);
1484 return;
1485 }
1486
1487 ap->cbl = ATA_CBL_SATA;
1488}
1489
1490/**
Jeff Garzik780a87f2005-05-30 15:41:05 -04001491 * sata_phy_reset - Reset SATA bus.
1492 * @ap: SATA port associated with target SATA PHY.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001494 * This function resets the SATA bus, and then probes
1495 * the bus for devices.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 *
1497 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001498 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 *
1500 */
1501void sata_phy_reset(struct ata_port *ap)
1502{
1503 __sata_phy_reset(ap);
1504 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1505 return;
1506 ata_bus_reset(ap);
1507}
1508
1509/**
Jeff Garzik780a87f2005-05-30 15:41:05 -04001510 * ata_port_disable - Disable port.
1511 * @ap: Port to be disabled.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001513 * Modify @ap data structure such that the system
1514 * thinks that the entire port is disabled, and should
1515 * never attempt to probe or communicate with devices
1516 * on this port.
1517 *
1518 * LOCKING: host_set lock, or some other form of
1519 * serialization.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 */
1521
1522void ata_port_disable(struct ata_port *ap)
1523{
1524 ap->device[0].class = ATA_DEV_NONE;
1525 ap->device[1].class = ATA_DEV_NONE;
1526 ap->flags |= ATA_FLAG_PORT_DISABLED;
1527}
1528
Alan Cox452503f2005-10-21 19:01:32 -04001529/*
1530 * This mode timing computation functionality is ported over from
1531 * drivers/ide/ide-timing.h and was originally written by Vojtech Pavlik
1532 */
1533/*
1534 * PIO 0-5, MWDMA 0-2 and UDMA 0-6 timings (in nanoseconds).
1535 * These were taken from ATA/ATAPI-6 standard, rev 0a, except
1536 * for PIO 5, which is a nonstandard extension and UDMA6, which
1537 * is currently supported only by Maxtor drives.
1538 */
1539
1540static const struct ata_timing ata_timing[] = {
1541
1542 { XFER_UDMA_6, 0, 0, 0, 0, 0, 0, 0, 15 },
1543 { XFER_UDMA_5, 0, 0, 0, 0, 0, 0, 0, 20 },
1544 { XFER_UDMA_4, 0, 0, 0, 0, 0, 0, 0, 30 },
1545 { XFER_UDMA_3, 0, 0, 0, 0, 0, 0, 0, 45 },
1546
1547 { XFER_UDMA_2, 0, 0, 0, 0, 0, 0, 0, 60 },
1548 { XFER_UDMA_1, 0, 0, 0, 0, 0, 0, 0, 80 },
1549 { XFER_UDMA_0, 0, 0, 0, 0, 0, 0, 0, 120 },
1550
1551/* { XFER_UDMA_SLOW, 0, 0, 0, 0, 0, 0, 0, 150 }, */
1552
1553 { XFER_MW_DMA_2, 25, 0, 0, 0, 70, 25, 120, 0 },
1554 { XFER_MW_DMA_1, 45, 0, 0, 0, 80, 50, 150, 0 },
1555 { XFER_MW_DMA_0, 60, 0, 0, 0, 215, 215, 480, 0 },
1556
1557 { XFER_SW_DMA_2, 60, 0, 0, 0, 120, 120, 240, 0 },
1558 { XFER_SW_DMA_1, 90, 0, 0, 0, 240, 240, 480, 0 },
1559 { XFER_SW_DMA_0, 120, 0, 0, 0, 480, 480, 960, 0 },
1560
1561/* { XFER_PIO_5, 20, 50, 30, 100, 50, 30, 100, 0 }, */
1562 { XFER_PIO_4, 25, 70, 25, 120, 70, 25, 120, 0 },
1563 { XFER_PIO_3, 30, 80, 70, 180, 80, 70, 180, 0 },
1564
1565 { XFER_PIO_2, 30, 290, 40, 330, 100, 90, 240, 0 },
1566 { XFER_PIO_1, 50, 290, 93, 383, 125, 100, 383, 0 },
1567 { XFER_PIO_0, 70, 290, 240, 600, 165, 150, 600, 0 },
1568
1569/* { XFER_PIO_SLOW, 120, 290, 240, 960, 290, 240, 960, 0 }, */
1570
1571 { 0xFF }
1572};
1573
1574#define ENOUGH(v,unit) (((v)-1)/(unit)+1)
1575#define EZ(v,unit) ((v)?ENOUGH(v,unit):0)
1576
1577static void ata_timing_quantize(const struct ata_timing *t, struct ata_timing *q, int T, int UT)
1578{
1579 q->setup = EZ(t->setup * 1000, T);
1580 q->act8b = EZ(t->act8b * 1000, T);
1581 q->rec8b = EZ(t->rec8b * 1000, T);
1582 q->cyc8b = EZ(t->cyc8b * 1000, T);
1583 q->active = EZ(t->active * 1000, T);
1584 q->recover = EZ(t->recover * 1000, T);
1585 q->cycle = EZ(t->cycle * 1000, T);
1586 q->udma = EZ(t->udma * 1000, UT);
1587}
1588
1589void ata_timing_merge(const struct ata_timing *a, const struct ata_timing *b,
1590 struct ata_timing *m, unsigned int what)
1591{
1592 if (what & ATA_TIMING_SETUP ) m->setup = max(a->setup, b->setup);
1593 if (what & ATA_TIMING_ACT8B ) m->act8b = max(a->act8b, b->act8b);
1594 if (what & ATA_TIMING_REC8B ) m->rec8b = max(a->rec8b, b->rec8b);
1595 if (what & ATA_TIMING_CYC8B ) m->cyc8b = max(a->cyc8b, b->cyc8b);
1596 if (what & ATA_TIMING_ACTIVE ) m->active = max(a->active, b->active);
1597 if (what & ATA_TIMING_RECOVER) m->recover = max(a->recover, b->recover);
1598 if (what & ATA_TIMING_CYCLE ) m->cycle = max(a->cycle, b->cycle);
1599 if (what & ATA_TIMING_UDMA ) m->udma = max(a->udma, b->udma);
1600}
1601
1602static const struct ata_timing* ata_timing_find_mode(unsigned short speed)
1603{
1604 const struct ata_timing *t;
1605
1606 for (t = ata_timing; t->mode != speed; t++)
Alan Cox91190752005-10-26 12:17:46 -04001607 if (t->mode == 0xFF)
Alan Cox452503f2005-10-21 19:01:32 -04001608 return NULL;
1609 return t;
1610}
1611
1612int ata_timing_compute(struct ata_device *adev, unsigned short speed,
1613 struct ata_timing *t, int T, int UT)
1614{
1615 const struct ata_timing *s;
1616 struct ata_timing p;
1617
1618 /*
1619 * Find the mode.
Albert Lee75b1f2f2005-11-16 17:06:18 +08001620 */
Alan Cox452503f2005-10-21 19:01:32 -04001621
1622 if (!(s = ata_timing_find_mode(speed)))
1623 return -EINVAL;
1624
Albert Lee75b1f2f2005-11-16 17:06:18 +08001625 memcpy(t, s, sizeof(*s));
1626
Alan Cox452503f2005-10-21 19:01:32 -04001627 /*
1628 * If the drive is an EIDE drive, it can tell us it needs extended
1629 * PIO/MW_DMA cycle timing.
1630 */
1631
1632 if (adev->id[ATA_ID_FIELD_VALID] & 2) { /* EIDE drive */
1633 memset(&p, 0, sizeof(p));
1634 if(speed >= XFER_PIO_0 && speed <= XFER_SW_DMA_0) {
1635 if (speed <= XFER_PIO_2) p.cycle = p.cyc8b = adev->id[ATA_ID_EIDE_PIO];
1636 else p.cycle = p.cyc8b = adev->id[ATA_ID_EIDE_PIO_IORDY];
1637 } else if(speed >= XFER_MW_DMA_0 && speed <= XFER_MW_DMA_2) {
1638 p.cycle = adev->id[ATA_ID_EIDE_DMA_MIN];
1639 }
1640 ata_timing_merge(&p, t, t, ATA_TIMING_CYCLE | ATA_TIMING_CYC8B);
1641 }
1642
1643 /*
1644 * Convert the timing to bus clock counts.
1645 */
1646
Albert Lee75b1f2f2005-11-16 17:06:18 +08001647 ata_timing_quantize(t, t, T, UT);
Alan Cox452503f2005-10-21 19:01:32 -04001648
1649 /*
1650 * Even in DMA/UDMA modes we still use PIO access for IDENTIFY, S.M.A.R.T
1651 * and some other commands. We have to ensure that the DMA cycle timing is
1652 * slower/equal than the fastest PIO timing.
1653 */
1654
1655 if (speed > XFER_PIO_4) {
1656 ata_timing_compute(adev, adev->pio_mode, &p, T, UT);
1657 ata_timing_merge(&p, t, t, ATA_TIMING_ALL);
1658 }
1659
1660 /*
1661 * Lenghten active & recovery time so that cycle time is correct.
1662 */
1663
1664 if (t->act8b + t->rec8b < t->cyc8b) {
1665 t->act8b += (t->cyc8b - (t->act8b + t->rec8b)) / 2;
1666 t->rec8b = t->cyc8b - t->act8b;
1667 }
1668
1669 if (t->active + t->recover < t->cycle) {
1670 t->active += (t->cycle - (t->active + t->recover)) / 2;
1671 t->recover = t->cycle - t->active;
1672 }
1673
1674 return 0;
1675}
1676
Jeff Garzik057ace52005-10-22 14:27:05 -04001677static const struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 unsigned int shift;
1679 u8 base;
1680} xfer_mode_classes[] = {
1681 { ATA_SHIFT_UDMA, XFER_UDMA_0 },
1682 { ATA_SHIFT_MWDMA, XFER_MW_DMA_0 },
1683 { ATA_SHIFT_PIO, XFER_PIO_0 },
1684};
1685
1686static inline u8 base_from_shift(unsigned int shift)
1687{
1688 int i;
1689
1690 for (i = 0; i < ARRAY_SIZE(xfer_mode_classes); i++)
1691 if (xfer_mode_classes[i].shift == shift)
1692 return xfer_mode_classes[i].base;
1693
1694 return 0xff;
1695}
1696
1697static void ata_dev_set_mode(struct ata_port *ap, struct ata_device *dev)
1698{
1699 int ofs, idx;
1700 u8 base;
1701
1702 if (!ata_dev_present(dev) || (ap->flags & ATA_FLAG_PORT_DISABLED))
1703 return;
1704
1705 if (dev->xfer_shift == ATA_SHIFT_PIO)
1706 dev->flags |= ATA_DFLAG_PIO;
1707
1708 ata_dev_set_xfermode(ap, dev);
1709
1710 base = base_from_shift(dev->xfer_shift);
1711 ofs = dev->xfer_mode - base;
1712 idx = ofs + dev->xfer_shift;
1713 WARN_ON(idx >= ARRAY_SIZE(xfer_mode_str));
1714
1715 DPRINTK("idx=%d xfer_shift=%u, xfer_mode=0x%x, base=0x%x, offset=%d\n",
1716 idx, dev->xfer_shift, (int)dev->xfer_mode, (int)base, ofs);
1717
1718 printk(KERN_INFO "ata%u: dev %u configured for %s\n",
1719 ap->id, dev->devno, xfer_mode_str[idx]);
1720}
1721
1722static int ata_host_set_pio(struct ata_port *ap)
1723{
1724 unsigned int mask;
1725 int x, i;
1726 u8 base, xfer_mode;
1727
1728 mask = ata_get_mode_mask(ap, ATA_SHIFT_PIO);
1729 x = fgb(mask);
1730 if (x < 0) {
1731 printk(KERN_WARNING "ata%u: no PIO support\n", ap->id);
1732 return -1;
1733 }
1734
1735 base = base_from_shift(ATA_SHIFT_PIO);
1736 xfer_mode = base + x;
1737
1738 DPRINTK("base 0x%x xfer_mode 0x%x mask 0x%x x %d\n",
1739 (int)base, (int)xfer_mode, mask, x);
1740
1741 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1742 struct ata_device *dev = &ap->device[i];
1743 if (ata_dev_present(dev)) {
1744 dev->pio_mode = xfer_mode;
1745 dev->xfer_mode = xfer_mode;
1746 dev->xfer_shift = ATA_SHIFT_PIO;
1747 if (ap->ops->set_piomode)
1748 ap->ops->set_piomode(ap, dev);
1749 }
1750 }
1751
1752 return 0;
1753}
1754
1755static void ata_host_set_dma(struct ata_port *ap, u8 xfer_mode,
1756 unsigned int xfer_shift)
1757{
1758 int i;
1759
1760 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1761 struct ata_device *dev = &ap->device[i];
1762 if (ata_dev_present(dev)) {
1763 dev->dma_mode = xfer_mode;
1764 dev->xfer_mode = xfer_mode;
1765 dev->xfer_shift = xfer_shift;
1766 if (ap->ops->set_dmamode)
1767 ap->ops->set_dmamode(ap, dev);
1768 }
1769 }
1770}
1771
1772/**
1773 * ata_set_mode - Program timings and issue SET FEATURES - XFER
1774 * @ap: port on which timings will be programmed
1775 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001776 * Set ATA device disk transfer mode (PIO3, UDMA6, etc.).
1777 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001779 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 *
1781 */
1782static void ata_set_mode(struct ata_port *ap)
1783{
Albert Lee8cbd6df2005-10-12 15:06:27 +08001784 unsigned int xfer_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 u8 xfer_mode;
1786 int rc;
1787
1788 /* step 1: always set host PIO timings */
1789 rc = ata_host_set_pio(ap);
1790 if (rc)
1791 goto err_out;
1792
1793 /* step 2: choose the best data xfer mode */
1794 xfer_mode = xfer_shift = 0;
1795 rc = ata_choose_xfer_mode(ap, &xfer_mode, &xfer_shift);
1796 if (rc)
1797 goto err_out;
1798
1799 /* step 3: if that xfer mode isn't PIO, set host DMA timings */
1800 if (xfer_shift != ATA_SHIFT_PIO)
1801 ata_host_set_dma(ap, xfer_mode, xfer_shift);
1802
1803 /* step 4: update devices' xfer mode */
1804 ata_dev_set_mode(ap, &ap->device[0]);
1805 ata_dev_set_mode(ap, &ap->device[1]);
1806
1807 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1808 return;
1809
1810 if (ap->ops->post_set_mode)
1811 ap->ops->post_set_mode(ap);
1812
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 return;
1814
1815err_out:
1816 ata_port_disable(ap);
1817}
1818
1819/**
1820 * ata_busy_sleep - sleep until BSY clears, or timeout
1821 * @ap: port containing status register to be polled
1822 * @tmout_pat: impatience timeout
1823 * @tmout: overall timeout
1824 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001825 * Sleep until ATA Status register bit BSY clears,
1826 * or a timeout occurs.
1827 *
1828 * LOCKING: None.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 *
1830 */
1831
1832static unsigned int ata_busy_sleep (struct ata_port *ap,
1833 unsigned long tmout_pat,
1834 unsigned long tmout)
1835{
1836 unsigned long timer_start, timeout;
1837 u8 status;
1838
1839 status = ata_busy_wait(ap, ATA_BUSY, 300);
1840 timer_start = jiffies;
1841 timeout = timer_start + tmout_pat;
1842 while ((status & ATA_BUSY) && (time_before(jiffies, timeout))) {
1843 msleep(50);
1844 status = ata_busy_wait(ap, ATA_BUSY, 3);
1845 }
1846
1847 if (status & ATA_BUSY)
1848 printk(KERN_WARNING "ata%u is slow to respond, "
1849 "please be patient\n", ap->id);
1850
1851 timeout = timer_start + tmout;
1852 while ((status & ATA_BUSY) && (time_before(jiffies, timeout))) {
1853 msleep(50);
1854 status = ata_chk_status(ap);
1855 }
1856
1857 if (status & ATA_BUSY) {
1858 printk(KERN_ERR "ata%u failed to respond (%lu secs)\n",
1859 ap->id, tmout / HZ);
1860 return 1;
1861 }
1862
1863 return 0;
1864}
1865
1866static void ata_bus_post_reset(struct ata_port *ap, unsigned int devmask)
1867{
1868 struct ata_ioports *ioaddr = &ap->ioaddr;
1869 unsigned int dev0 = devmask & (1 << 0);
1870 unsigned int dev1 = devmask & (1 << 1);
1871 unsigned long timeout;
1872
1873 /* if device 0 was found in ata_devchk, wait for its
1874 * BSY bit to clear
1875 */
1876 if (dev0)
1877 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
1878
1879 /* if device 1 was found in ata_devchk, wait for
1880 * register access, then wait for BSY to clear
1881 */
1882 timeout = jiffies + ATA_TMOUT_BOOT;
1883 while (dev1) {
1884 u8 nsect, lbal;
1885
1886 ap->ops->dev_select(ap, 1);
1887 if (ap->flags & ATA_FLAG_MMIO) {
1888 nsect = readb((void __iomem *) ioaddr->nsect_addr);
1889 lbal = readb((void __iomem *) ioaddr->lbal_addr);
1890 } else {
1891 nsect = inb(ioaddr->nsect_addr);
1892 lbal = inb(ioaddr->lbal_addr);
1893 }
1894 if ((nsect == 1) && (lbal == 1))
1895 break;
1896 if (time_after(jiffies, timeout)) {
1897 dev1 = 0;
1898 break;
1899 }
1900 msleep(50); /* give drive a breather */
1901 }
1902 if (dev1)
1903 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
1904
1905 /* is all this really necessary? */
1906 ap->ops->dev_select(ap, 0);
1907 if (dev1)
1908 ap->ops->dev_select(ap, 1);
1909 if (dev0)
1910 ap->ops->dev_select(ap, 0);
1911}
1912
1913/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04001914 * ata_bus_edd - Issue EXECUTE DEVICE DIAGNOSTIC command.
1915 * @ap: Port to reset and probe
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04001917 * Use the EXECUTE DEVICE DIAGNOSTIC command to reset and
1918 * probe the bus. Not often used these days.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 *
1920 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001921 * PCI/etc. bus probe sem.
Jeff Garzike5338252005-10-30 21:37:17 -05001922 * Obtains host_set lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923 *
1924 */
1925
1926static unsigned int ata_bus_edd(struct ata_port *ap)
1927{
1928 struct ata_taskfile tf;
Jeff Garzike5338252005-10-30 21:37:17 -05001929 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930
1931 /* set up execute-device-diag (bus reset) taskfile */
1932 /* also, take interrupts to a known state (disabled) */
1933 DPRINTK("execute-device-diag\n");
1934 ata_tf_init(ap, &tf, 0);
1935 tf.ctl |= ATA_NIEN;
1936 tf.command = ATA_CMD_EDD;
1937 tf.protocol = ATA_PROT_NODATA;
1938
1939 /* do bus reset */
Jeff Garzike5338252005-10-30 21:37:17 -05001940 spin_lock_irqsave(&ap->host_set->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 ata_tf_to_host(ap, &tf);
Jeff Garzike5338252005-10-30 21:37:17 -05001942 spin_unlock_irqrestore(&ap->host_set->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943
1944 /* spec says at least 2ms. but who knows with those
1945 * crazy ATAPI devices...
1946 */
1947 msleep(150);
1948
1949 return ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
1950}
1951
1952static unsigned int ata_bus_softreset(struct ata_port *ap,
1953 unsigned int devmask)
1954{
1955 struct ata_ioports *ioaddr = &ap->ioaddr;
1956
1957 DPRINTK("ata%u: bus reset via SRST\n", ap->id);
1958
1959 /* software reset. causes dev0 to be selected */
1960 if (ap->flags & ATA_FLAG_MMIO) {
1961 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
1962 udelay(20); /* FIXME: flush */
1963 writeb(ap->ctl | ATA_SRST, (void __iomem *) ioaddr->ctl_addr);
1964 udelay(20); /* FIXME: flush */
1965 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
1966 } else {
1967 outb(ap->ctl, ioaddr->ctl_addr);
1968 udelay(10);
1969 outb(ap->ctl | ATA_SRST, ioaddr->ctl_addr);
1970 udelay(10);
1971 outb(ap->ctl, ioaddr->ctl_addr);
1972 }
1973
1974 /* spec mandates ">= 2ms" before checking status.
1975 * We wait 150ms, because that was the magic delay used for
1976 * ATAPI devices in Hale Landis's ATADRVR, for the period of time
1977 * between when the ATA command register is written, and then
1978 * status is checked. Because waiting for "a while" before
1979 * checking status is fine, post SRST, we perform this magic
1980 * delay here as well.
1981 */
1982 msleep(150);
1983
1984 ata_bus_post_reset(ap, devmask);
1985
1986 return 0;
1987}
1988
1989/**
1990 * ata_bus_reset - reset host port and associated ATA channel
1991 * @ap: port to reset
1992 *
1993 * This is typically the first time we actually start issuing
1994 * commands to the ATA channel. We wait for BSY to clear, then
1995 * issue EXECUTE DEVICE DIAGNOSTIC command, polling for its
1996 * result. Determine what devices, if any, are on the channel
1997 * by looking at the device 0/1 error register. Look at the signature
1998 * stored in each device's taskfile registers, to determine if
1999 * the device is ATA or ATAPI.
2000 *
2001 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002002 * PCI/etc. bus probe sem.
2003 * Obtains host_set lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004 *
2005 * SIDE EFFECTS:
2006 * Sets ATA_FLAG_PORT_DISABLED if bus reset fails.
2007 */
2008
2009void ata_bus_reset(struct ata_port *ap)
2010{
2011 struct ata_ioports *ioaddr = &ap->ioaddr;
2012 unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS;
2013 u8 err;
2014 unsigned int dev0, dev1 = 0, rc = 0, devmask = 0;
2015
2016 DPRINTK("ENTER, host %u, port %u\n", ap->id, ap->port_no);
2017
2018 /* determine if device 0/1 are present */
2019 if (ap->flags & ATA_FLAG_SATA_RESET)
2020 dev0 = 1;
2021 else {
2022 dev0 = ata_devchk(ap, 0);
2023 if (slave_possible)
2024 dev1 = ata_devchk(ap, 1);
2025 }
2026
2027 if (dev0)
2028 devmask |= (1 << 0);
2029 if (dev1)
2030 devmask |= (1 << 1);
2031
2032 /* select device 0 again */
2033 ap->ops->dev_select(ap, 0);
2034
2035 /* issue bus reset */
2036 if (ap->flags & ATA_FLAG_SRST)
2037 rc = ata_bus_softreset(ap, devmask);
2038 else if ((ap->flags & ATA_FLAG_SATA_RESET) == 0) {
2039 /* set up device control */
2040 if (ap->flags & ATA_FLAG_MMIO)
2041 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
2042 else
2043 outb(ap->ctl, ioaddr->ctl_addr);
2044 rc = ata_bus_edd(ap);
2045 }
2046
2047 if (rc)
2048 goto err_out;
2049
2050 /*
2051 * determine by signature whether we have ATA or ATAPI devices
2052 */
2053 err = ata_dev_try_classify(ap, 0);
2054 if ((slave_possible) && (err != 0x81))
2055 ata_dev_try_classify(ap, 1);
2056
2057 /* re-enable interrupts */
2058 if (ap->ioaddr.ctl_addr) /* FIXME: hack. create a hook instead */
2059 ata_irq_on(ap);
2060
2061 /* is double-select really necessary? */
2062 if (ap->device[1].class != ATA_DEV_NONE)
2063 ap->ops->dev_select(ap, 1);
2064 if (ap->device[0].class != ATA_DEV_NONE)
2065 ap->ops->dev_select(ap, 0);
2066
2067 /* if no devices were detected, disable this port */
2068 if ((ap->device[0].class == ATA_DEV_NONE) &&
2069 (ap->device[1].class == ATA_DEV_NONE))
2070 goto err_out;
2071
2072 if (ap->flags & (ATA_FLAG_SATA_RESET | ATA_FLAG_SRST)) {
2073 /* set up device control for ATA_FLAG_SATA_RESET */
2074 if (ap->flags & ATA_FLAG_MMIO)
2075 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
2076 else
2077 outb(ap->ctl, ioaddr->ctl_addr);
2078 }
2079
2080 DPRINTK("EXIT\n");
2081 return;
2082
2083err_out:
2084 printk(KERN_ERR "ata%u: disabling port\n", ap->id);
2085 ap->ops->port_disable(ap);
2086
2087 DPRINTK("EXIT\n");
2088}
2089
Jeff Garzik057ace52005-10-22 14:27:05 -04002090static void ata_pr_blacklisted(const struct ata_port *ap,
2091 const struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092{
2093 printk(KERN_WARNING "ata%u: dev %u is on DMA blacklist, disabling DMA\n",
2094 ap->id, dev->devno);
2095}
2096
Arjan van de Ven98ac62d2005-11-28 10:06:23 +01002097static const char * const ata_dma_blacklist [] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 "WDC AC11000H",
2099 "WDC AC22100H",
2100 "WDC AC32500H",
2101 "WDC AC33100H",
2102 "WDC AC31600H",
2103 "WDC AC32100H",
2104 "WDC AC23200L",
2105 "Compaq CRD-8241B",
2106 "CRD-8400B",
2107 "CRD-8480B",
2108 "CRD-8482B",
2109 "CRD-84",
2110 "SanDisk SDP3B",
2111 "SanDisk SDP3B-64",
2112 "SANYO CD-ROM CRD",
2113 "HITACHI CDR-8",
2114 "HITACHI CDR-8335",
2115 "HITACHI CDR-8435",
2116 "Toshiba CD-ROM XM-6202B",
Jeff Garzike9222562005-06-28 00:03:37 -04002117 "TOSHIBA CD-ROM XM-1702BC",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118 "CD-532E-A",
2119 "E-IDE CD-ROM CR-840",
2120 "CD-ROM Drive/F5A",
2121 "WPI CDD-820",
2122 "SAMSUNG CD-ROM SC-148C",
2123 "SAMSUNG CD-ROM SC",
2124 "SanDisk SDP3B-64",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 "ATAPI CD-ROM DRIVE 40X MAXIMUM",
2126 "_NEC DV5800A",
2127};
2128
Jeff Garzik057ace52005-10-22 14:27:05 -04002129static int ata_dma_blacklisted(const struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130{
2131 unsigned char model_num[40];
2132 char *s;
2133 unsigned int len;
2134 int i;
2135
2136 ata_dev_id_string(dev->id, model_num, ATA_ID_PROD_OFS,
2137 sizeof(model_num));
2138 s = &model_num[0];
2139 len = strnlen(s, sizeof(model_num));
2140
2141 /* ATAPI specifies that empty space is blank-filled; remove blanks */
2142 while ((len > 0) && (s[len - 1] == ' ')) {
2143 len--;
2144 s[len] = 0;
2145 }
2146
2147 for (i = 0; i < ARRAY_SIZE(ata_dma_blacklist); i++)
2148 if (!strncmp(ata_dma_blacklist[i], s, len))
2149 return 1;
2150
2151 return 0;
2152}
2153
Jeff Garzik057ace52005-10-22 14:27:05 -04002154static unsigned int ata_get_mode_mask(const struct ata_port *ap, int shift)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155{
Jeff Garzik057ace52005-10-22 14:27:05 -04002156 const struct ata_device *master, *slave;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157 unsigned int mask;
2158
2159 master = &ap->device[0];
2160 slave = &ap->device[1];
2161
2162 assert (ata_dev_present(master) || ata_dev_present(slave));
2163
2164 if (shift == ATA_SHIFT_UDMA) {
2165 mask = ap->udma_mask;
2166 if (ata_dev_present(master)) {
2167 mask &= (master->id[ATA_ID_UDMA_MODES] & 0xff);
Jeff Garzik057ace52005-10-22 14:27:05 -04002168 if (ata_dma_blacklisted(master)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169 mask = 0;
2170 ata_pr_blacklisted(ap, master);
2171 }
2172 }
2173 if (ata_dev_present(slave)) {
2174 mask &= (slave->id[ATA_ID_UDMA_MODES] & 0xff);
Jeff Garzik057ace52005-10-22 14:27:05 -04002175 if (ata_dma_blacklisted(slave)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 mask = 0;
2177 ata_pr_blacklisted(ap, slave);
2178 }
2179 }
2180 }
2181 else if (shift == ATA_SHIFT_MWDMA) {
2182 mask = ap->mwdma_mask;
2183 if (ata_dev_present(master)) {
2184 mask &= (master->id[ATA_ID_MWDMA_MODES] & 0x07);
Jeff Garzik057ace52005-10-22 14:27:05 -04002185 if (ata_dma_blacklisted(master)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186 mask = 0;
2187 ata_pr_blacklisted(ap, master);
2188 }
2189 }
2190 if (ata_dev_present(slave)) {
2191 mask &= (slave->id[ATA_ID_MWDMA_MODES] & 0x07);
Jeff Garzik057ace52005-10-22 14:27:05 -04002192 if (ata_dma_blacklisted(slave)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193 mask = 0;
2194 ata_pr_blacklisted(ap, slave);
2195 }
2196 }
2197 }
2198 else if (shift == ATA_SHIFT_PIO) {
2199 mask = ap->pio_mask;
2200 if (ata_dev_present(master)) {
2201 /* spec doesn't return explicit support for
2202 * PIO0-2, so we fake it
2203 */
2204 u16 tmp_mode = master->id[ATA_ID_PIO_MODES] & 0x03;
2205 tmp_mode <<= 3;
2206 tmp_mode |= 0x7;
2207 mask &= tmp_mode;
2208 }
2209 if (ata_dev_present(slave)) {
2210 /* spec doesn't return explicit support for
2211 * PIO0-2, so we fake it
2212 */
2213 u16 tmp_mode = slave->id[ATA_ID_PIO_MODES] & 0x03;
2214 tmp_mode <<= 3;
2215 tmp_mode |= 0x7;
2216 mask &= tmp_mode;
2217 }
2218 }
2219 else {
2220 mask = 0xffffffff; /* shut up compiler warning */
2221 BUG();
2222 }
2223
2224 return mask;
2225}
2226
2227/* find greatest bit */
2228static int fgb(u32 bitmap)
2229{
2230 unsigned int i;
2231 int x = -1;
2232
2233 for (i = 0; i < 32; i++)
2234 if (bitmap & (1 << i))
2235 x = i;
2236
2237 return x;
2238}
2239
2240/**
2241 * ata_choose_xfer_mode - attempt to find best transfer mode
2242 * @ap: Port for which an xfer mode will be selected
2243 * @xfer_mode_out: (output) SET FEATURES - XFER MODE code
2244 * @xfer_shift_out: (output) bit shift that selects this mode
2245 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04002246 * Based on host and device capabilities, determine the
2247 * maximum transfer mode that is amenable to all.
2248 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002250 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251 *
2252 * RETURNS:
2253 * Zero on success, negative on error.
2254 */
2255
Jeff Garzik057ace52005-10-22 14:27:05 -04002256static int ata_choose_xfer_mode(const struct ata_port *ap,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257 u8 *xfer_mode_out,
2258 unsigned int *xfer_shift_out)
2259{
2260 unsigned int mask, shift;
2261 int x, i;
2262
2263 for (i = 0; i < ARRAY_SIZE(xfer_mode_classes); i++) {
2264 shift = xfer_mode_classes[i].shift;
2265 mask = ata_get_mode_mask(ap, shift);
2266
2267 x = fgb(mask);
2268 if (x >= 0) {
2269 *xfer_mode_out = xfer_mode_classes[i].base + x;
2270 *xfer_shift_out = shift;
2271 return 0;
2272 }
2273 }
2274
2275 return -1;
2276}
2277
2278/**
2279 * ata_dev_set_xfermode - Issue SET FEATURES - XFER MODE command
2280 * @ap: Port associated with device @dev
2281 * @dev: Device to which command will be sent
2282 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04002283 * Issue SET FEATURES - XFER MODE command to device @dev
2284 * on port @ap.
2285 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002287 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288 */
2289
2290static void ata_dev_set_xfermode(struct ata_port *ap, struct ata_device *dev)
2291{
2292 DECLARE_COMPLETION(wait);
2293 struct ata_queued_cmd *qc;
2294 int rc;
2295 unsigned long flags;
2296
2297 /* set up set-features taskfile */
2298 DPRINTK("set features - xfer mode\n");
2299
2300 qc = ata_qc_new_init(ap, dev);
2301 BUG_ON(qc == NULL);
2302
2303 qc->tf.command = ATA_CMD_SET_FEATURES;
2304 qc->tf.feature = SETFEATURES_XFER;
2305 qc->tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
2306 qc->tf.protocol = ATA_PROT_NODATA;
2307 qc->tf.nsect = dev->xfer_mode;
2308
2309 qc->waiting = &wait;
2310 qc->complete_fn = ata_qc_complete_noop;
2311
2312 spin_lock_irqsave(&ap->host_set->lock, flags);
2313 rc = ata_qc_issue(qc);
2314 spin_unlock_irqrestore(&ap->host_set->lock, flags);
2315
2316 if (rc)
2317 ata_port_disable(ap);
2318 else
Jeff Garzik64f043d2005-11-17 10:50:01 -05002319 ata_qc_wait_err(qc, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320
2321 DPRINTK("EXIT\n");
2322}
2323
2324/**
Albert Lee59a10b12005-10-12 15:09:42 +08002325 * ata_dev_reread_id - Reread the device identify device info
2326 * @ap: port where the device is
2327 * @dev: device to reread the identify device info
2328 *
2329 * LOCKING:
2330 */
2331
2332static void ata_dev_reread_id(struct ata_port *ap, struct ata_device *dev)
2333{
2334 DECLARE_COMPLETION(wait);
2335 struct ata_queued_cmd *qc;
2336 unsigned long flags;
2337 int rc;
2338
2339 qc = ata_qc_new_init(ap, dev);
2340 BUG_ON(qc == NULL);
2341
2342 ata_sg_init_one(qc, dev->id, sizeof(dev->id));
2343 qc->dma_dir = DMA_FROM_DEVICE;
2344
2345 if (dev->class == ATA_DEV_ATA) {
2346 qc->tf.command = ATA_CMD_ID_ATA;
2347 DPRINTK("do ATA identify\n");
2348 } else {
2349 qc->tf.command = ATA_CMD_ID_ATAPI;
2350 DPRINTK("do ATAPI identify\n");
2351 }
2352
2353 qc->tf.flags |= ATA_TFLAG_DEVICE;
2354 qc->tf.protocol = ATA_PROT_PIO;
2355 qc->nsect = 1;
2356
2357 qc->waiting = &wait;
2358 qc->complete_fn = ata_qc_complete_noop;
2359
2360 spin_lock_irqsave(&ap->host_set->lock, flags);
2361 rc = ata_qc_issue(qc);
2362 spin_unlock_irqrestore(&ap->host_set->lock, flags);
2363
2364 if (rc)
2365 goto err_out;
2366
Jeff Garzik64f043d2005-11-17 10:50:01 -05002367 ata_qc_wait_err(qc, &wait);
Albert Lee59a10b12005-10-12 15:09:42 +08002368
2369 swap_buf_le16(dev->id, ATA_ID_WORDS);
2370
2371 ata_dump_id(dev);
2372
2373 DPRINTK("EXIT\n");
2374
2375 return;
2376err_out:
2377 ata_port_disable(ap);
2378}
2379
2380/**
Albert Lee8bf62ec2005-05-12 15:29:42 -04002381 * ata_dev_init_params - Issue INIT DEV PARAMS command
2382 * @ap: Port associated with device @dev
2383 * @dev: Device to which command will be sent
2384 *
2385 * LOCKING:
2386 */
2387
2388static void ata_dev_init_params(struct ata_port *ap, struct ata_device *dev)
2389{
2390 DECLARE_COMPLETION(wait);
2391 struct ata_queued_cmd *qc;
2392 int rc;
2393 unsigned long flags;
2394 u16 sectors = dev->id[6];
2395 u16 heads = dev->id[3];
2396
2397 /* Number of sectors per track 1-255. Number of heads 1-16 */
2398 if (sectors < 1 || sectors > 255 || heads < 1 || heads > 16)
2399 return;
2400
2401 /* set up init dev params taskfile */
2402 DPRINTK("init dev params \n");
2403
2404 qc = ata_qc_new_init(ap, dev);
2405 BUG_ON(qc == NULL);
2406
2407 qc->tf.command = ATA_CMD_INIT_DEV_PARAMS;
2408 qc->tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
2409 qc->tf.protocol = ATA_PROT_NODATA;
2410 qc->tf.nsect = sectors;
2411 qc->tf.device |= (heads - 1) & 0x0f; /* max head = num. of heads - 1 */
2412
2413 qc->waiting = &wait;
2414 qc->complete_fn = ata_qc_complete_noop;
2415
2416 spin_lock_irqsave(&ap->host_set->lock, flags);
2417 rc = ata_qc_issue(qc);
2418 spin_unlock_irqrestore(&ap->host_set->lock, flags);
2419
2420 if (rc)
2421 ata_port_disable(ap);
2422 else
Jeff Garzik64f043d2005-11-17 10:50:01 -05002423 ata_qc_wait_err(qc, &wait);
Albert Lee8bf62ec2005-05-12 15:29:42 -04002424
2425 DPRINTK("EXIT\n");
2426}
2427
2428/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04002429 * ata_sg_clean - Unmap DMA memory associated with command
2430 * @qc: Command containing DMA memory to be released
2431 *
2432 * Unmap all mapped DMA memory associated with this command.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433 *
2434 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002435 * spin_lock_irqsave(host_set lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436 */
2437
2438static void ata_sg_clean(struct ata_queued_cmd *qc)
2439{
2440 struct ata_port *ap = qc->ap;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002441 struct scatterlist *sg = qc->__sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442 int dir = qc->dma_dir;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002443 void *pad_buf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444
2445 assert(qc->flags & ATA_QCFLAG_DMAMAP);
2446 assert(sg != NULL);
2447
2448 if (qc->flags & ATA_QCFLAG_SINGLE)
2449 assert(qc->n_elem == 1);
2450
Jeff Garzik2c13b7c2005-11-14 14:14:16 -05002451 VPRINTK("unmapping %u sg elements\n", qc->n_elem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002453 /* if we padded the buffer out to 32-bit bound, and data
2454 * xfer direction is from-device, we must copy from the
2455 * pad buffer back into the supplied buffer
2456 */
2457 if (qc->pad_len && !(qc->tf.flags & ATA_TFLAG_WRITE))
2458 pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
2459
2460 if (qc->flags & ATA_QCFLAG_SG) {
Jeff Garzike1410f22005-11-14 14:06:26 -05002461 if (qc->n_elem)
2462 dma_unmap_sg(ap->host_set->dev, sg, qc->n_elem, dir);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002463 /* restore last sg */
2464 sg[qc->orig_n_elem - 1].length += qc->pad_len;
2465 if (pad_buf) {
2466 struct scatterlist *psg = &qc->pad_sgent;
2467 void *addr = kmap_atomic(psg->page, KM_IRQ0);
2468 memcpy(addr + psg->offset, pad_buf, qc->pad_len);
2469 kunmap_atomic(psg->page, KM_IRQ0);
2470 }
2471 } else {
Jeff Garzike1410f22005-11-14 14:06:26 -05002472 if (sg_dma_len(&sg[0]) > 0)
2473 dma_unmap_single(ap->host_set->dev,
2474 sg_dma_address(&sg[0]), sg_dma_len(&sg[0]),
2475 dir);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002476 /* restore sg */
2477 sg->length += qc->pad_len;
2478 if (pad_buf)
2479 memcpy(qc->buf_virt + sg->length - qc->pad_len,
2480 pad_buf, qc->pad_len);
2481 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482
2483 qc->flags &= ~ATA_QCFLAG_DMAMAP;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002484 qc->__sg = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485}
2486
2487/**
2488 * ata_fill_sg - Fill PCI IDE PRD table
2489 * @qc: Metadata associated with taskfile to be transferred
2490 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04002491 * Fill PCI IDE PRD (scatter-gather) table with segments
2492 * associated with the current disk command.
2493 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494 * LOCKING:
Jeff Garzik780a87f2005-05-30 15:41:05 -04002495 * spin_lock_irqsave(host_set lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496 *
2497 */
2498static void ata_fill_sg(struct ata_queued_cmd *qc)
2499{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500 struct ata_port *ap = qc->ap;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002501 struct scatterlist *sg;
2502 unsigned int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002504 assert(qc->__sg != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505 assert(qc->n_elem > 0);
2506
2507 idx = 0;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002508 ata_for_each_sg(sg, qc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509 u32 addr, offset;
2510 u32 sg_len, len;
2511
2512 /* determine if physical DMA addr spans 64K boundary.
2513 * Note h/w doesn't support 64-bit, so we unconditionally
2514 * truncate dma_addr_t to u32.
2515 */
2516 addr = (u32) sg_dma_address(sg);
2517 sg_len = sg_dma_len(sg);
2518
2519 while (sg_len) {
2520 offset = addr & 0xffff;
2521 len = sg_len;
2522 if ((offset + sg_len) > 0x10000)
2523 len = 0x10000 - offset;
2524
2525 ap->prd[idx].addr = cpu_to_le32(addr);
2526 ap->prd[idx].flags_len = cpu_to_le32(len & 0xffff);
2527 VPRINTK("PRD[%u] = (0x%X, 0x%X)\n", idx, addr, len);
2528
2529 idx++;
2530 sg_len -= len;
2531 addr += len;
2532 }
2533 }
2534
2535 if (idx)
2536 ap->prd[idx - 1].flags_len |= cpu_to_le32(ATA_PRD_EOT);
2537}
2538/**
2539 * ata_check_atapi_dma - Check whether ATAPI DMA can be supported
2540 * @qc: Metadata associated with taskfile to check
2541 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04002542 * Allow low-level driver to filter ATA PACKET commands, returning
2543 * a status indicating whether or not it is OK to use DMA for the
2544 * supplied PACKET command.
2545 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002547 * spin_lock_irqsave(host_set lock)
2548 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549 * RETURNS: 0 when ATAPI DMA can be used
2550 * nonzero otherwise
2551 */
2552int ata_check_atapi_dma(struct ata_queued_cmd *qc)
2553{
2554 struct ata_port *ap = qc->ap;
2555 int rc = 0; /* Assume ATAPI DMA is OK by default */
2556
2557 if (ap->ops->check_atapi_dma)
2558 rc = ap->ops->check_atapi_dma(qc);
2559
2560 return rc;
2561}
2562/**
2563 * ata_qc_prep - Prepare taskfile for submission
2564 * @qc: Metadata associated with taskfile to be prepared
2565 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04002566 * Prepare ATA taskfile for submission.
2567 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568 * LOCKING:
2569 * spin_lock_irqsave(host_set lock)
2570 */
2571void ata_qc_prep(struct ata_queued_cmd *qc)
2572{
2573 if (!(qc->flags & ATA_QCFLAG_DMAMAP))
2574 return;
2575
2576 ata_fill_sg(qc);
2577}
2578
Jeff Garzik0cba6322005-05-30 19:49:12 -04002579/**
2580 * ata_sg_init_one - Associate command with memory buffer
2581 * @qc: Command to be associated
2582 * @buf: Memory buffer
2583 * @buflen: Length of memory buffer, in bytes.
2584 *
2585 * Initialize the data-related elements of queued_cmd @qc
2586 * to point to a single memory buffer, @buf of byte length @buflen.
2587 *
2588 * LOCKING:
2589 * spin_lock_irqsave(host_set lock)
2590 */
2591
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592void ata_sg_init_one(struct ata_queued_cmd *qc, void *buf, unsigned int buflen)
2593{
2594 struct scatterlist *sg;
2595
2596 qc->flags |= ATA_QCFLAG_SINGLE;
2597
2598 memset(&qc->sgent, 0, sizeof(qc->sgent));
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002599 qc->__sg = &qc->sgent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600 qc->n_elem = 1;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002601 qc->orig_n_elem = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602 qc->buf_virt = buf;
2603
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002604 sg = qc->__sg;
Jeff Garzikf0612bb2005-10-30 01:58:18 -05002605 sg_init_one(sg, buf, buflen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606}
2607
Jeff Garzik0cba6322005-05-30 19:49:12 -04002608/**
2609 * ata_sg_init - Associate command with scatter-gather table.
2610 * @qc: Command to be associated
2611 * @sg: Scatter-gather table.
2612 * @n_elem: Number of elements in s/g table.
2613 *
2614 * Initialize the data-related elements of queued_cmd @qc
2615 * to point to a scatter-gather table @sg, containing @n_elem
2616 * elements.
2617 *
2618 * LOCKING:
2619 * spin_lock_irqsave(host_set lock)
2620 */
2621
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622void ata_sg_init(struct ata_queued_cmd *qc, struct scatterlist *sg,
2623 unsigned int n_elem)
2624{
2625 qc->flags |= ATA_QCFLAG_SG;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002626 qc->__sg = sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627 qc->n_elem = n_elem;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002628 qc->orig_n_elem = n_elem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629}
2630
2631/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04002632 * ata_sg_setup_one - DMA-map the memory buffer associated with a command.
2633 * @qc: Command with memory buffer to be mapped.
2634 *
2635 * DMA-map the memory buffer associated with queued_cmd @qc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636 *
2637 * LOCKING:
2638 * spin_lock_irqsave(host_set lock)
2639 *
2640 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002641 * Zero on success, negative on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642 */
2643
2644static int ata_sg_setup_one(struct ata_queued_cmd *qc)
2645{
2646 struct ata_port *ap = qc->ap;
2647 int dir = qc->dma_dir;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002648 struct scatterlist *sg = qc->__sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649 dma_addr_t dma_address;
2650
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002651 /* we must lengthen transfers to end on a 32-bit boundary */
2652 qc->pad_len = sg->length & 3;
2653 if (qc->pad_len) {
2654 void *pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
2655 struct scatterlist *psg = &qc->pad_sgent;
2656
2657 assert(qc->dev->class == ATA_DEV_ATAPI);
2658
2659 memset(pad_buf, 0, ATA_DMA_PAD_SZ);
2660
2661 if (qc->tf.flags & ATA_TFLAG_WRITE)
2662 memcpy(pad_buf, qc->buf_virt + sg->length - qc->pad_len,
2663 qc->pad_len);
2664
2665 sg_dma_address(psg) = ap->pad_dma + (qc->tag * ATA_DMA_PAD_SZ);
2666 sg_dma_len(psg) = ATA_DMA_PAD_SZ;
2667 /* trim sg */
2668 sg->length -= qc->pad_len;
2669
2670 DPRINTK("padding done, sg->length=%u pad_len=%u\n",
2671 sg->length, qc->pad_len);
2672 }
2673
Jeff Garzike1410f22005-11-14 14:06:26 -05002674 if (!sg->length) {
2675 sg_dma_address(sg) = 0;
2676 goto skip_map;
2677 }
2678
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679 dma_address = dma_map_single(ap->host_set->dev, qc->buf_virt,
Albert Lee32529e02005-05-26 03:49:42 -04002680 sg->length, dir);
Tejun Heo537a95d2005-11-05 14:29:01 -05002681 if (dma_mapping_error(dma_address)) {
2682 /* restore sg */
2683 sg->length += qc->pad_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684 return -1;
Tejun Heo537a95d2005-11-05 14:29:01 -05002685 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002686
2687 sg_dma_address(sg) = dma_address;
Jeff Garzike1410f22005-11-14 14:06:26 -05002688skip_map:
Albert Lee32529e02005-05-26 03:49:42 -04002689 sg_dma_len(sg) = sg->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690
2691 DPRINTK("mapped buffer of %d bytes for %s\n", sg_dma_len(sg),
2692 qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
2693
2694 return 0;
2695}
2696
2697/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04002698 * ata_sg_setup - DMA-map the scatter-gather table associated with a command.
2699 * @qc: Command with scatter-gather table to be mapped.
2700 *
2701 * DMA-map the scatter-gather table associated with queued_cmd @qc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702 *
2703 * LOCKING:
2704 * spin_lock_irqsave(host_set lock)
2705 *
2706 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002707 * Zero on success, negative on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002708 *
2709 */
2710
2711static int ata_sg_setup(struct ata_queued_cmd *qc)
2712{
2713 struct ata_port *ap = qc->ap;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002714 struct scatterlist *sg = qc->__sg;
2715 struct scatterlist *lsg = &sg[qc->n_elem - 1];
Jeff Garzike1410f22005-11-14 14:06:26 -05002716 int n_elem, pre_n_elem, dir, trim_sg = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717
2718 VPRINTK("ENTER, ata%u\n", ap->id);
2719 assert(qc->flags & ATA_QCFLAG_SG);
2720
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002721 /* we must lengthen transfers to end on a 32-bit boundary */
2722 qc->pad_len = lsg->length & 3;
2723 if (qc->pad_len) {
2724 void *pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
2725 struct scatterlist *psg = &qc->pad_sgent;
2726 unsigned int offset;
2727
2728 assert(qc->dev->class == ATA_DEV_ATAPI);
2729
2730 memset(pad_buf, 0, ATA_DMA_PAD_SZ);
2731
2732 /*
2733 * psg->page/offset are used to copy to-be-written
2734 * data in this function or read data in ata_sg_clean.
2735 */
2736 offset = lsg->offset + lsg->length - qc->pad_len;
2737 psg->page = nth_page(lsg->page, offset >> PAGE_SHIFT);
2738 psg->offset = offset_in_page(offset);
2739
2740 if (qc->tf.flags & ATA_TFLAG_WRITE) {
2741 void *addr = kmap_atomic(psg->page, KM_IRQ0);
2742 memcpy(pad_buf, addr + psg->offset, qc->pad_len);
2743 kunmap_atomic(psg->page, KM_IRQ0);
2744 }
2745
2746 sg_dma_address(psg) = ap->pad_dma + (qc->tag * ATA_DMA_PAD_SZ);
2747 sg_dma_len(psg) = ATA_DMA_PAD_SZ;
2748 /* trim last sg */
2749 lsg->length -= qc->pad_len;
Jeff Garzike1410f22005-11-14 14:06:26 -05002750 if (lsg->length == 0)
2751 trim_sg = 1;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002752
2753 DPRINTK("padding done, sg[%d].length=%u pad_len=%u\n",
2754 qc->n_elem - 1, lsg->length, qc->pad_len);
2755 }
2756
Jeff Garzike1410f22005-11-14 14:06:26 -05002757 pre_n_elem = qc->n_elem;
2758 if (trim_sg && pre_n_elem)
2759 pre_n_elem--;
2760
2761 if (!pre_n_elem) {
2762 n_elem = 0;
2763 goto skip_map;
2764 }
2765
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766 dir = qc->dma_dir;
Jeff Garzike1410f22005-11-14 14:06:26 -05002767 n_elem = dma_map_sg(ap->host_set->dev, sg, pre_n_elem, dir);
Tejun Heo537a95d2005-11-05 14:29:01 -05002768 if (n_elem < 1) {
2769 /* restore last sg */
2770 lsg->length += qc->pad_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771 return -1;
Tejun Heo537a95d2005-11-05 14:29:01 -05002772 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773
2774 DPRINTK("%d sg elements mapped\n", n_elem);
2775
Jeff Garzike1410f22005-11-14 14:06:26 -05002776skip_map:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002777 qc->n_elem = n_elem;
2778
2779 return 0;
2780}
2781
2782/**
Tejun Heo40e8c822005-08-22 17:12:45 +09002783 * ata_poll_qc_complete - turn irq back on and finish qc
2784 * @qc: Command to complete
Randy Dunlap8e8b77d2005-11-01 21:29:27 -08002785 * @err_mask: ATA status register content
Tejun Heo40e8c822005-08-22 17:12:45 +09002786 *
2787 * LOCKING:
2788 * None. (grabs host lock)
2789 */
2790
Albert Leea22e2eb2005-12-05 15:38:02 +08002791void ata_poll_qc_complete(struct ata_queued_cmd *qc)
Tejun Heo40e8c822005-08-22 17:12:45 +09002792{
2793 struct ata_port *ap = qc->ap;
Jeff Garzikb8f61532005-08-25 22:01:20 -04002794 unsigned long flags;
Tejun Heo40e8c822005-08-22 17:12:45 +09002795
Jeff Garzikb8f61532005-08-25 22:01:20 -04002796 spin_lock_irqsave(&ap->host_set->lock, flags);
Tejun Heo40e8c822005-08-22 17:12:45 +09002797 ata_irq_on(ap);
Albert Leea22e2eb2005-12-05 15:38:02 +08002798 ata_qc_complete(qc);
Jeff Garzikb8f61532005-08-25 22:01:20 -04002799 spin_unlock_irqrestore(&ap->host_set->lock, flags);
Tejun Heo40e8c822005-08-22 17:12:45 +09002800}
2801
2802/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803 * ata_pio_poll -
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04002804 * @ap: the target ata_port
Linus Torvalds1da177e2005-04-16 15:20:36 -07002805 *
2806 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002807 * None. (executing in kernel thread context)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002808 *
2809 * RETURNS:
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04002810 * timeout value to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811 */
2812
2813static unsigned long ata_pio_poll(struct ata_port *ap)
2814{
Albert Leec14b8332005-12-05 15:36:08 +08002815 struct ata_queued_cmd *qc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002816 u8 status;
Albert Lee14be71f2005-09-27 17:36:35 +08002817 unsigned int poll_state = HSM_ST_UNKNOWN;
2818 unsigned int reg_state = HSM_ST_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002819
Albert Leec14b8332005-12-05 15:36:08 +08002820 qc = ata_qc_from_tag(ap, ap->active_tag);
2821 assert(qc != NULL);
2822
Albert Lee14be71f2005-09-27 17:36:35 +08002823 switch (ap->hsm_task_state) {
2824 case HSM_ST:
2825 case HSM_ST_POLL:
2826 poll_state = HSM_ST_POLL;
2827 reg_state = HSM_ST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828 break;
Albert Lee14be71f2005-09-27 17:36:35 +08002829 case HSM_ST_LAST:
2830 case HSM_ST_LAST_POLL:
2831 poll_state = HSM_ST_LAST_POLL;
2832 reg_state = HSM_ST_LAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833 break;
2834 default:
2835 BUG();
2836 break;
2837 }
2838
2839 status = ata_chk_status(ap);
2840 if (status & ATA_BUSY) {
2841 if (time_after(jiffies, ap->pio_task_timeout)) {
Albert Lee1c848982005-12-05 15:40:15 +08002842 qc->err_mask |= AC_ERR_ATA_BUS;
Albert Lee7c398332005-11-09 13:03:30 +08002843 ap->hsm_task_state = HSM_ST_TMOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844 return 0;
2845 }
Albert Lee14be71f2005-09-27 17:36:35 +08002846 ap->hsm_task_state = poll_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002847 return ATA_SHORT_PAUSE;
2848 }
2849
Albert Lee14be71f2005-09-27 17:36:35 +08002850 ap->hsm_task_state = reg_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851 return 0;
2852}
2853
2854/**
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04002855 * ata_pio_complete - check if drive is busy or idle
2856 * @ap: the target ata_port
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857 *
2858 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002859 * None. (executing in kernel thread context)
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04002860 *
2861 * RETURNS:
Albert Leefbcdd802005-11-01 19:30:05 +08002862 * Zero if qc completed.
2863 * Non-zero if has next.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864 */
2865
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04002866static int ata_pio_complete (struct ata_port *ap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002867{
2868 struct ata_queued_cmd *qc;
2869 u8 drv_stat;
2870
2871 /*
Alan Cox31433ea2005-08-26 15:56:47 +01002872 * This is purely heuristic. This is a fast path. Sometimes when
2873 * we enter, BSY will be cleared in a chk-status or two. If not,
2874 * the drive is probably seeking or something. Snooze for a couple
2875 * msecs, then chk-status again. If still busy, fall back to
Albert Lee07f6f7d2005-11-01 19:33:20 +08002876 * HSM_ST_LAST_POLL state.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877 */
Albert Leefe79e682005-12-06 11:34:59 +08002878 drv_stat = ata_busy_wait(ap, ATA_BUSY, 10);
2879 if (drv_stat & ATA_BUSY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880 msleep(2);
Albert Leefe79e682005-12-06 11:34:59 +08002881 drv_stat = ata_busy_wait(ap, ATA_BUSY, 10);
2882 if (drv_stat & ATA_BUSY) {
Albert Lee14be71f2005-09-27 17:36:35 +08002883 ap->hsm_task_state = HSM_ST_LAST_POLL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884 ap->pio_task_timeout = jiffies + ATA_TMOUT_PIO;
Albert Leefbcdd802005-11-01 19:30:05 +08002885 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002886 }
2887 }
2888
Albert Leec14b8332005-12-05 15:36:08 +08002889 qc = ata_qc_from_tag(ap, ap->active_tag);
2890 assert(qc != NULL);
2891
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892 drv_stat = ata_wait_idle(ap);
2893 if (!ata_ok(drv_stat)) {
Albert Lee1c848982005-12-05 15:40:15 +08002894 qc->err_mask |= __ac_err_mask(drv_stat);
Albert Lee14be71f2005-09-27 17:36:35 +08002895 ap->hsm_task_state = HSM_ST_ERR;
Albert Leefbcdd802005-11-01 19:30:05 +08002896 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002897 }
2898
Albert Lee14be71f2005-09-27 17:36:35 +08002899 ap->hsm_task_state = HSM_ST_IDLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900
Albert Leea22e2eb2005-12-05 15:38:02 +08002901 assert(qc->err_mask == 0);
2902 ata_poll_qc_complete(qc);
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04002903
2904 /* another command may start at this point */
2905
Albert Leefbcdd802005-11-01 19:30:05 +08002906 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002907}
2908
Edward Falk0baab862005-06-02 18:17:13 -04002909
2910/**
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04002911 * swap_buf_le16 - swap halves of 16-words in place
Edward Falk0baab862005-06-02 18:17:13 -04002912 * @buf: Buffer to swap
2913 * @buf_words: Number of 16-bit words in buffer.
2914 *
2915 * Swap halves of 16-bit words if needed to convert from
2916 * little-endian byte order to native cpu byte order, or
2917 * vice-versa.
2918 *
2919 * LOCKING:
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04002920 * Inherited from caller.
Edward Falk0baab862005-06-02 18:17:13 -04002921 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922void swap_buf_le16(u16 *buf, unsigned int buf_words)
2923{
2924#ifdef __BIG_ENDIAN
2925 unsigned int i;
2926
2927 for (i = 0; i < buf_words; i++)
2928 buf[i] = le16_to_cpu(buf[i]);
2929#endif /* __BIG_ENDIAN */
2930}
2931
Albert Lee6ae4cfb2005-08-12 14:15:34 +08002932/**
2933 * ata_mmio_data_xfer - Transfer data by MMIO
2934 * @ap: port to read/write
2935 * @buf: data buffer
2936 * @buflen: buffer length
Jeff Garzik344baba2005-09-07 01:15:17 -04002937 * @write_data: read/write
Albert Lee6ae4cfb2005-08-12 14:15:34 +08002938 *
2939 * Transfer data from/to the device data register by MMIO.
2940 *
2941 * LOCKING:
2942 * Inherited from caller.
Albert Lee6ae4cfb2005-08-12 14:15:34 +08002943 */
2944
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945static void ata_mmio_data_xfer(struct ata_port *ap, unsigned char *buf,
2946 unsigned int buflen, int write_data)
2947{
2948 unsigned int i;
2949 unsigned int words = buflen >> 1;
2950 u16 *buf16 = (u16 *) buf;
2951 void __iomem *mmio = (void __iomem *)ap->ioaddr.data_addr;
2952
Albert Lee6ae4cfb2005-08-12 14:15:34 +08002953 /* Transfer multiple of 2 bytes */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002954 if (write_data) {
2955 for (i = 0; i < words; i++)
2956 writew(le16_to_cpu(buf16[i]), mmio);
2957 } else {
2958 for (i = 0; i < words; i++)
2959 buf16[i] = cpu_to_le16(readw(mmio));
2960 }
Albert Lee6ae4cfb2005-08-12 14:15:34 +08002961
2962 /* Transfer trailing 1 byte, if any. */
2963 if (unlikely(buflen & 0x01)) {
2964 u16 align_buf[1] = { 0 };
2965 unsigned char *trailing_buf = buf + buflen - 1;
2966
2967 if (write_data) {
2968 memcpy(align_buf, trailing_buf, 1);
2969 writew(le16_to_cpu(align_buf[0]), mmio);
2970 } else {
2971 align_buf[0] = cpu_to_le16(readw(mmio));
2972 memcpy(trailing_buf, align_buf, 1);
2973 }
2974 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002975}
2976
Albert Lee6ae4cfb2005-08-12 14:15:34 +08002977/**
2978 * ata_pio_data_xfer - Transfer data by PIO
2979 * @ap: port to read/write
2980 * @buf: data buffer
2981 * @buflen: buffer length
Jeff Garzik344baba2005-09-07 01:15:17 -04002982 * @write_data: read/write
Albert Lee6ae4cfb2005-08-12 14:15:34 +08002983 *
2984 * Transfer data from/to the device data register by PIO.
2985 *
2986 * LOCKING:
2987 * Inherited from caller.
Albert Lee6ae4cfb2005-08-12 14:15:34 +08002988 */
2989
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990static void ata_pio_data_xfer(struct ata_port *ap, unsigned char *buf,
2991 unsigned int buflen, int write_data)
2992{
Albert Lee6ae4cfb2005-08-12 14:15:34 +08002993 unsigned int words = buflen >> 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994
Albert Lee6ae4cfb2005-08-12 14:15:34 +08002995 /* Transfer multiple of 2 bytes */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996 if (write_data)
Albert Lee6ae4cfb2005-08-12 14:15:34 +08002997 outsw(ap->ioaddr.data_addr, buf, words);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998 else
Albert Lee6ae4cfb2005-08-12 14:15:34 +08002999 insw(ap->ioaddr.data_addr, buf, words);
3000
3001 /* Transfer trailing 1 byte, if any. */
3002 if (unlikely(buflen & 0x01)) {
3003 u16 align_buf[1] = { 0 };
3004 unsigned char *trailing_buf = buf + buflen - 1;
3005
3006 if (write_data) {
3007 memcpy(align_buf, trailing_buf, 1);
3008 outw(le16_to_cpu(align_buf[0]), ap->ioaddr.data_addr);
3009 } else {
3010 align_buf[0] = cpu_to_le16(inw(ap->ioaddr.data_addr));
3011 memcpy(trailing_buf, align_buf, 1);
3012 }
3013 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003014}
3015
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003016/**
3017 * ata_data_xfer - Transfer data from/to the data register.
3018 * @ap: port to read/write
3019 * @buf: data buffer
3020 * @buflen: buffer length
3021 * @do_write: read/write
3022 *
3023 * Transfer data from/to the device data register.
3024 *
3025 * LOCKING:
3026 * Inherited from caller.
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003027 */
3028
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029static void ata_data_xfer(struct ata_port *ap, unsigned char *buf,
3030 unsigned int buflen, int do_write)
3031{
3032 if (ap->flags & ATA_FLAG_MMIO)
3033 ata_mmio_data_xfer(ap, buf, buflen, do_write);
3034 else
3035 ata_pio_data_xfer(ap, buf, buflen, do_write);
3036}
3037
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003038/**
3039 * ata_pio_sector - Transfer ATA_SECT_SIZE (512 bytes) of data.
3040 * @qc: Command on going
3041 *
3042 * Transfer ATA_SECT_SIZE of data from/to the ATA device.
3043 *
3044 * LOCKING:
3045 * Inherited from caller.
3046 */
3047
Linus Torvalds1da177e2005-04-16 15:20:36 -07003048static void ata_pio_sector(struct ata_queued_cmd *qc)
3049{
3050 int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003051 struct scatterlist *sg = qc->__sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003052 struct ata_port *ap = qc->ap;
3053 struct page *page;
3054 unsigned int offset;
3055 unsigned char *buf;
3056
3057 if (qc->cursect == (qc->nsect - 1))
Albert Lee14be71f2005-09-27 17:36:35 +08003058 ap->hsm_task_state = HSM_ST_LAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003059
3060 page = sg[qc->cursg].page;
3061 offset = sg[qc->cursg].offset + qc->cursg_ofs * ATA_SECT_SIZE;
3062
3063 /* get the current page and offset */
3064 page = nth_page(page, (offset >> PAGE_SHIFT));
3065 offset %= PAGE_SIZE;
3066
Albert Lee7282aa42005-10-09 09:46:07 -04003067 DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
3068
Albert Lee91b8b312005-10-09 09:48:44 -04003069 if (PageHighMem(page)) {
3070 unsigned long flags;
Albert Lee7282aa42005-10-09 09:46:07 -04003071
Albert Lee91b8b312005-10-09 09:48:44 -04003072 local_irq_save(flags);
3073 buf = kmap_atomic(page, KM_IRQ0);
Albert Lee083958d2005-10-09 09:47:31 -04003074
Albert Lee91b8b312005-10-09 09:48:44 -04003075 /* do the actual data transfer */
3076 ata_data_xfer(ap, buf + offset, ATA_SECT_SIZE, do_write);
3077
3078 kunmap_atomic(buf, KM_IRQ0);
3079 local_irq_restore(flags);
3080 } else {
3081 buf = page_address(page);
3082 ata_data_xfer(ap, buf + offset, ATA_SECT_SIZE, do_write);
3083 }
Albert Lee7282aa42005-10-09 09:46:07 -04003084
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085 qc->cursect++;
3086 qc->cursg_ofs++;
3087
Albert Lee32529e02005-05-26 03:49:42 -04003088 if ((qc->cursg_ofs * ATA_SECT_SIZE) == (&sg[qc->cursg])->length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003089 qc->cursg++;
3090 qc->cursg_ofs = 0;
3091 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003092}
3093
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003094/**
Albert Lee07f6f7d2005-11-01 19:33:20 +08003095 * ata_pio_sectors - Transfer one or many 512-byte sectors.
3096 * @qc: Command on going
3097 *
3098 * Transfer one or many ATA_SECT_SIZE of data from/to the
3099 * ATA device for the DRQ request.
3100 *
3101 * LOCKING:
3102 * Inherited from caller.
3103 */
3104
3105static void ata_pio_sectors(struct ata_queued_cmd *qc)
3106{
3107 if (is_multi_taskfile(&qc->tf)) {
3108 /* READ/WRITE MULTIPLE */
3109 unsigned int nsect;
3110
3111 assert(qc->dev->multi_count);
3112
3113 nsect = min(qc->nsect - qc->cursect, qc->dev->multi_count);
3114 while (nsect--)
3115 ata_pio_sector(qc);
3116 } else
3117 ata_pio_sector(qc);
3118}
3119
3120/**
Albert Leec71c1852005-10-04 06:03:45 -04003121 * atapi_send_cdb - Write CDB bytes to hardware
3122 * @ap: Port to which ATAPI device is attached.
3123 * @qc: Taskfile currently active
3124 *
3125 * When device has indicated its readiness to accept
3126 * a CDB, this function is called. Send the CDB.
3127 *
3128 * LOCKING:
3129 * caller.
3130 */
3131
3132static void atapi_send_cdb(struct ata_port *ap, struct ata_queued_cmd *qc)
3133{
3134 /* send SCSI cdb */
3135 DPRINTK("send cdb\n");
3136 assert(ap->cdb_len >= 12);
3137
3138 ata_data_xfer(ap, qc->cdb, ap->cdb_len, 1);
3139 ata_altstatus(ap); /* flush */
3140
3141 switch (qc->tf.protocol) {
3142 case ATA_PROT_ATAPI:
3143 ap->hsm_task_state = HSM_ST;
3144 break;
3145 case ATA_PROT_ATAPI_NODATA:
3146 ap->hsm_task_state = HSM_ST_LAST;
3147 break;
3148 case ATA_PROT_ATAPI_DMA:
3149 ap->hsm_task_state = HSM_ST_LAST;
3150 /* initiate bmdma */
3151 ap->ops->bmdma_start(qc);
3152 break;
3153 }
3154}
3155
3156/**
Albert Leee27486d2005-11-01 19:24:49 +08003157 * ata_pio_first_block - Write first data block to hardware
3158 * @ap: Port to which ATA/ATAPI device is attached.
Albert Leec71c1852005-10-04 06:03:45 -04003159 *
3160 * When device has indicated its readiness to accept
3161 * the data, this function sends out the CDB or
3162 * the first data block by PIO.
3163 * After this,
3164 * - If polling, ata_pio_task() handles the rest.
3165 * - Otherwise, interrupt handler takes over.
3166 *
3167 * LOCKING:
3168 * Kernel thread context (may sleep)
Albert Leefbcdd802005-11-01 19:30:05 +08003169 *
3170 * RETURNS:
3171 * Zero if irq handler takes over
3172 * Non-zero if has next (polling).
Albert Leec71c1852005-10-04 06:03:45 -04003173 */
3174
Albert Leefbcdd802005-11-01 19:30:05 +08003175static int ata_pio_first_block(struct ata_port *ap)
Albert Leec71c1852005-10-04 06:03:45 -04003176{
Albert Leec71c1852005-10-04 06:03:45 -04003177 struct ata_queued_cmd *qc;
3178 u8 status;
3179 unsigned long flags;
Albert Leefbcdd802005-11-01 19:30:05 +08003180 int has_next;
Albert Leec71c1852005-10-04 06:03:45 -04003181
3182 qc = ata_qc_from_tag(ap, ap->active_tag);
3183 assert(qc != NULL);
3184 assert(qc->flags & ATA_QCFLAG_ACTIVE);
3185
Albert Leefbcdd802005-11-01 19:30:05 +08003186 /* if polling, we will stay in the work queue after sending the data.
3187 * otherwise, interrupt handler takes over after sending the data.
3188 */
3189 has_next = (qc->tf.flags & ATA_TFLAG_POLLING);
3190
Albert Leec71c1852005-10-04 06:03:45 -04003191 /* sleep-wait for BSY to clear */
3192 DPRINTK("busy wait\n");
Albert Leefbcdd802005-11-01 19:30:05 +08003193 if (ata_busy_sleep(ap, ATA_TMOUT_DATAOUT_QUICK, ATA_TMOUT_DATAOUT)) {
3194 ap->hsm_task_state = HSM_ST_TMOUT;
Albert Leec71c1852005-10-04 06:03:45 -04003195 goto err_out;
Albert Leefbcdd802005-11-01 19:30:05 +08003196 }
Albert Leec71c1852005-10-04 06:03:45 -04003197
3198 /* make sure DRQ is set */
3199 status = ata_chk_status(ap);
Albert Leefbcdd802005-11-01 19:30:05 +08003200 if ((status & (ATA_BUSY | ATA_DRQ)) != ATA_DRQ) {
3201 /* device status error */
3202 ap->hsm_task_state = HSM_ST_ERR;
Albert Leec71c1852005-10-04 06:03:45 -04003203 goto err_out;
Albert Leefbcdd802005-11-01 19:30:05 +08003204 }
Albert Leec71c1852005-10-04 06:03:45 -04003205
3206 /* Send the CDB (atapi) or the first data block (ata pio out).
3207 * During the state transition, interrupt handler shouldn't
3208 * be invoked before the data transfer is complete and
3209 * hsm_task_state is changed. Hence, the following locking.
3210 */
3211 spin_lock_irqsave(&ap->host_set->lock, flags);
3212
3213 if (qc->tf.protocol == ATA_PROT_PIO) {
3214 /* PIO data out protocol.
3215 * send first data block.
3216 */
3217
Albert Lee07f6f7d2005-11-01 19:33:20 +08003218 /* ata_pio_sectors() might change the state to HSM_ST_LAST.
3219 * so, the state is changed here before ata_pio_sectors().
Albert Leec71c1852005-10-04 06:03:45 -04003220 */
3221 ap->hsm_task_state = HSM_ST;
Albert Lee07f6f7d2005-11-01 19:33:20 +08003222 ata_pio_sectors(qc);
Albert Leec71c1852005-10-04 06:03:45 -04003223 ata_altstatus(ap); /* flush */
3224 } else
3225 /* send CDB */
3226 atapi_send_cdb(ap, qc);
3227
Albert Leefbcdd802005-11-01 19:30:05 +08003228 spin_unlock_irqrestore(&ap->host_set->lock, flags);
3229
Albert Leec71c1852005-10-04 06:03:45 -04003230 /* if polling, ata_pio_task() handles the rest.
3231 * otherwise, interrupt handler takes over from here.
3232 */
Albert Leefbcdd802005-11-01 19:30:05 +08003233 return has_next;
Albert Leec71c1852005-10-04 06:03:45 -04003234
3235err_out:
Albert Leefbcdd802005-11-01 19:30:05 +08003236 return 1; /* has next */
Albert Leec71c1852005-10-04 06:03:45 -04003237}
3238
3239/**
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003240 * __atapi_pio_bytes - Transfer data from/to the ATAPI device.
3241 * @qc: Command on going
3242 * @bytes: number of bytes
3243 *
3244 * Transfer Transfer data from/to the ATAPI device.
3245 *
3246 * LOCKING:
3247 * Inherited from caller.
3248 *
3249 */
3250
Linus Torvalds1da177e2005-04-16 15:20:36 -07003251static void __atapi_pio_bytes(struct ata_queued_cmd *qc, unsigned int bytes)
3252{
3253 int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003254 struct scatterlist *sg = qc->__sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003255 struct ata_port *ap = qc->ap;
3256 struct page *page;
3257 unsigned char *buf;
3258 unsigned int offset, count;
3259
Albert Lee563a6e12005-08-12 14:17:50 +08003260 if (qc->curbytes + bytes >= qc->nbytes)
Albert Lee14be71f2005-09-27 17:36:35 +08003261 ap->hsm_task_state = HSM_ST_LAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003262
3263next_sg:
Albert Lee563a6e12005-08-12 14:17:50 +08003264 if (unlikely(qc->cursg >= qc->n_elem)) {
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003265 /*
Albert Lee563a6e12005-08-12 14:17:50 +08003266 * The end of qc->sg is reached and the device expects
3267 * more data to transfer. In order not to overrun qc->sg
3268 * and fulfill length specified in the byte count register,
3269 * - for read case, discard trailing data from the device
3270 * - for write case, padding zero data to the device
3271 */
3272 u16 pad_buf[1] = { 0 };
3273 unsigned int words = bytes >> 1;
3274 unsigned int i;
3275
3276 if (words) /* warning if bytes > 1 */
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003277 printk(KERN_WARNING "ata%u: %u bytes trailing data\n",
Albert Lee563a6e12005-08-12 14:17:50 +08003278 ap->id, bytes);
3279
3280 for (i = 0; i < words; i++)
3281 ata_data_xfer(ap, (unsigned char*)pad_buf, 2, do_write);
3282
Albert Lee14be71f2005-09-27 17:36:35 +08003283 ap->hsm_task_state = HSM_ST_LAST;
Albert Lee563a6e12005-08-12 14:17:50 +08003284 return;
3285 }
3286
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003287 sg = &qc->__sg[qc->cursg];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003288
Linus Torvalds1da177e2005-04-16 15:20:36 -07003289 page = sg->page;
3290 offset = sg->offset + qc->cursg_ofs;
3291
3292 /* get the current page and offset */
3293 page = nth_page(page, (offset >> PAGE_SHIFT));
3294 offset %= PAGE_SIZE;
3295
Albert Lee6952df02005-06-06 15:56:03 +08003296 /* don't overrun current sg */
Albert Lee32529e02005-05-26 03:49:42 -04003297 count = min(sg->length - qc->cursg_ofs, bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003298
3299 /* don't cross page boundaries */
3300 count = min(count, (unsigned int)PAGE_SIZE - offset);
3301
Albert Lee7282aa42005-10-09 09:46:07 -04003302 DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
3303
Albert Lee91b8b312005-10-09 09:48:44 -04003304 if (PageHighMem(page)) {
3305 unsigned long flags;
Albert Lee7282aa42005-10-09 09:46:07 -04003306
Albert Lee91b8b312005-10-09 09:48:44 -04003307 local_irq_save(flags);
3308 buf = kmap_atomic(page, KM_IRQ0);
Albert Lee083958d2005-10-09 09:47:31 -04003309
Albert Lee91b8b312005-10-09 09:48:44 -04003310 /* do the actual data transfer */
3311 ata_data_xfer(ap, buf + offset, count, do_write);
3312
3313 kunmap_atomic(buf, KM_IRQ0);
3314 local_irq_restore(flags);
3315 } else {
3316 buf = page_address(page);
3317 ata_data_xfer(ap, buf + offset, count, do_write);
3318 }
Albert Lee7282aa42005-10-09 09:46:07 -04003319
Linus Torvalds1da177e2005-04-16 15:20:36 -07003320 bytes -= count;
3321 qc->curbytes += count;
3322 qc->cursg_ofs += count;
3323
Albert Lee32529e02005-05-26 03:49:42 -04003324 if (qc->cursg_ofs == sg->length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003325 qc->cursg++;
3326 qc->cursg_ofs = 0;
3327 }
3328
Albert Lee563a6e12005-08-12 14:17:50 +08003329 if (bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003330 goto next_sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003331}
3332
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003333/**
3334 * atapi_pio_bytes - Transfer data from/to the ATAPI device.
3335 * @qc: Command on going
3336 *
3337 * Transfer Transfer data from/to the ATAPI device.
3338 *
3339 * LOCKING:
3340 * Inherited from caller.
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003341 */
3342
Linus Torvalds1da177e2005-04-16 15:20:36 -07003343static void atapi_pio_bytes(struct ata_queued_cmd *qc)
3344{
3345 struct ata_port *ap = qc->ap;
3346 struct ata_device *dev = qc->dev;
3347 unsigned int ireason, bc_lo, bc_hi, bytes;
3348 int i_write, do_write = (qc->tf.flags & ATA_TFLAG_WRITE) ? 1 : 0;
3349
3350 ap->ops->tf_read(ap, &qc->tf);
3351 ireason = qc->tf.nsect;
3352 bc_lo = qc->tf.lbam;
3353 bc_hi = qc->tf.lbah;
3354 bytes = (bc_hi << 8) | bc_lo;
3355
3356 /* shall be cleared to zero, indicating xfer of data */
3357 if (ireason & (1 << 0))
3358 goto err_out;
3359
3360 /* make sure transfer direction matches expected */
3361 i_write = ((ireason & (1 << 1)) == 0) ? 1 : 0;
3362 if (do_write != i_write)
3363 goto err_out;
3364
Albert Lee312f7da2005-09-27 17:38:03 +08003365 VPRINTK("ata%u: xfering %d bytes\n", ap->id, bytes);
3366
Linus Torvalds1da177e2005-04-16 15:20:36 -07003367 __atapi_pio_bytes(qc, bytes);
3368
3369 return;
3370
3371err_out:
3372 printk(KERN_INFO "ata%u: dev %u: ATAPI check failed\n",
3373 ap->id, dev->devno);
Albert Lee1c848982005-12-05 15:40:15 +08003374 qc->err_mask |= AC_ERR_ATA_BUS;
Albert Lee14be71f2005-09-27 17:36:35 +08003375 ap->hsm_task_state = HSM_ST_ERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003376}
3377
3378/**
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04003379 * ata_pio_block - start PIO on a block
3380 * @ap: the target ata_port
Linus Torvalds1da177e2005-04-16 15:20:36 -07003381 *
3382 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003383 * None. (executing in kernel thread context)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003384 */
3385
3386static void ata_pio_block(struct ata_port *ap)
3387{
3388 struct ata_queued_cmd *qc;
3389 u8 status;
3390
3391 /*
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04003392 * This is purely heuristic. This is a fast path.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003393 * Sometimes when we enter, BSY will be cleared in
3394 * a chk-status or two. If not, the drive is probably seeking
3395 * or something. Snooze for a couple msecs, then
3396 * chk-status again. If still busy, fall back to
Albert Lee14be71f2005-09-27 17:36:35 +08003397 * HSM_ST_POLL state.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003398 */
3399 status = ata_busy_wait(ap, ATA_BUSY, 5);
3400 if (status & ATA_BUSY) {
3401 msleep(2);
3402 status = ata_busy_wait(ap, ATA_BUSY, 10);
3403 if (status & ATA_BUSY) {
Albert Lee14be71f2005-09-27 17:36:35 +08003404 ap->hsm_task_state = HSM_ST_POLL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003405 ap->pio_task_timeout = jiffies + ATA_TMOUT_PIO;
3406 return;
3407 }
3408 }
3409
3410 qc = ata_qc_from_tag(ap, ap->active_tag);
3411 assert(qc != NULL);
3412
Albert Leefe79e682005-12-06 11:34:59 +08003413 /* check error */
3414 if (status & (ATA_ERR | ATA_DF)) {
3415 qc->err_mask |= AC_ERR_DEV;
3416 ap->hsm_task_state = HSM_ST_ERR;
3417 return;
3418 }
3419
3420 /* transfer data if any */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003421 if (is_atapi_taskfile(&qc->tf)) {
Albert Leefe79e682005-12-06 11:34:59 +08003422 /* DRQ=0 means no more data to transfer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003423 if ((status & ATA_DRQ) == 0) {
Albert Lee14be71f2005-09-27 17:36:35 +08003424 ap->hsm_task_state = HSM_ST_LAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003425 return;
3426 }
3427
3428 atapi_pio_bytes(qc);
3429 } else {
3430 /* handle BSY=0, DRQ=0 as error */
3431 if ((status & ATA_DRQ) == 0) {
Albert Lee1c848982005-12-05 15:40:15 +08003432 qc->err_mask |= AC_ERR_ATA_BUS;
Albert Lee14be71f2005-09-27 17:36:35 +08003433 ap->hsm_task_state = HSM_ST_ERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003434 return;
3435 }
3436
Albert Lee07f6f7d2005-11-01 19:33:20 +08003437 ata_pio_sectors(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003438 }
Albert Lee467b16d2005-11-01 19:19:01 +08003439
3440 ata_altstatus(ap); /* flush */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003441}
3442
3443static void ata_pio_error(struct ata_port *ap)
3444{
3445 struct ata_queued_cmd *qc;
Jeff Garzika7dac442005-10-30 04:44:42 -05003446
3447 printk(KERN_WARNING "ata%u: PIO error\n", ap->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003448
3449 qc = ata_qc_from_tag(ap, ap->active_tag);
3450 assert(qc != NULL);
3451
Albert Lee1c848982005-12-05 15:40:15 +08003452 /* make sure qc->err_mask is available to
3453 * know what's wrong and recover
3454 */
3455 assert(qc->err_mask);
3456
Albert Lee14be71f2005-09-27 17:36:35 +08003457 ap->hsm_task_state = HSM_ST_IDLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003458
Albert Leea22e2eb2005-12-05 15:38:02 +08003459 ata_poll_qc_complete(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003460}
3461
3462static void ata_pio_task(void *_data)
3463{
3464 struct ata_port *ap = _data;
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003465 unsigned long timeout;
Albert Leefbcdd802005-11-01 19:30:05 +08003466 int has_next;
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003467
3468fsm_start:
3469 timeout = 0;
Albert Leefbcdd802005-11-01 19:30:05 +08003470 has_next = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003471
Albert Lee14be71f2005-09-27 17:36:35 +08003472 switch (ap->hsm_task_state) {
Albert Leee27486d2005-11-01 19:24:49 +08003473 case HSM_ST_FIRST:
Albert Leefbcdd802005-11-01 19:30:05 +08003474 has_next = ata_pio_first_block(ap);
3475 break;
Albert Leee27486d2005-11-01 19:24:49 +08003476
Albert Lee14be71f2005-09-27 17:36:35 +08003477 case HSM_ST:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003478 ata_pio_block(ap);
3479 break;
3480
Albert Lee14be71f2005-09-27 17:36:35 +08003481 case HSM_ST_LAST:
Albert Leefbcdd802005-11-01 19:30:05 +08003482 has_next = ata_pio_complete(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003483 break;
3484
Albert Lee14be71f2005-09-27 17:36:35 +08003485 case HSM_ST_POLL:
3486 case HSM_ST_LAST_POLL:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003487 timeout = ata_pio_poll(ap);
3488 break;
3489
Albert Lee14be71f2005-09-27 17:36:35 +08003490 case HSM_ST_TMOUT:
3491 case HSM_ST_ERR:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003492 ata_pio_error(ap);
3493 return;
Albert Lee467b16d2005-11-01 19:19:01 +08003494
3495 default:
3496 BUG();
3497 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003498 }
3499
3500 if (timeout)
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003501 queue_delayed_work(ata_wq, &ap->pio_task, timeout);
Albert Leefbcdd802005-11-01 19:30:05 +08003502 else if (has_next)
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003503 goto fsm_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003504}
3505
Linus Torvalds1da177e2005-04-16 15:20:36 -07003506/**
3507 * ata_qc_timeout - Handle timeout of queued command
3508 * @qc: Command that timed out
3509 *
3510 * Some part of the kernel (currently, only the SCSI layer)
3511 * has noticed that the active command on port @ap has not
3512 * completed after a specified length of time. Handle this
3513 * condition by disabling DMA (if necessary) and completing
3514 * transactions, with error if necessary.
3515 *
3516 * This also handles the case of the "lost interrupt", where
3517 * for some reason (possibly hardware bug, possibly driver bug)
3518 * an interrupt was not delivered to the driver, even though the
3519 * transaction completed successfully.
3520 *
3521 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003522 * Inherited from SCSI layer (none, can sleep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003523 */
3524
3525static void ata_qc_timeout(struct ata_queued_cmd *qc)
3526{
3527 struct ata_port *ap = qc->ap;
Jeff Garzikb8f61532005-08-25 22:01:20 -04003528 struct ata_host_set *host_set = ap->host_set;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003529 u8 host_stat = 0, drv_stat;
Jeff Garzikb8f61532005-08-25 22:01:20 -04003530 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003531
3532 DPRINTK("ENTER\n");
3533
Jeff Garzikb8f61532005-08-25 22:01:20 -04003534 spin_lock_irqsave(&host_set->lock, flags);
3535
Linus Torvalds1da177e2005-04-16 15:20:36 -07003536 /* hack alert! We cannot use the supplied completion
3537 * function from inside the ->eh_strategy_handler() thread.
3538 * libata is the only user of ->eh_strategy_handler() in
3539 * any kernel, so the default scsi_done() assumes it is
3540 * not being called from the SCSI EH.
3541 */
3542 qc->scsidone = scsi_finish_command;
3543
3544 switch (qc->tf.protocol) {
3545
3546 case ATA_PROT_DMA:
3547 case ATA_PROT_ATAPI_DMA:
3548 host_stat = ap->ops->bmdma_status(ap);
3549
3550 /* before we do anything else, clear DMA-Start bit */
Alan Coxb73fc892005-08-26 16:03:19 +01003551 ap->ops->bmdma_stop(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003552
3553 /* fall through */
3554
3555 default:
3556 ata_altstatus(ap);
3557 drv_stat = ata_chk_status(ap);
3558
3559 /* ack bmdma irq events */
3560 ap->ops->irq_clear(ap);
3561
3562 printk(KERN_ERR "ata%u: command 0x%x timeout, stat 0x%x host_stat 0x%x\n",
3563 ap->id, qc->tf.command, drv_stat, host_stat);
3564
Albert Lee312f7da2005-09-27 17:38:03 +08003565 ap->hsm_task_state = HSM_ST_IDLE;
3566
Linus Torvalds1da177e2005-04-16 15:20:36 -07003567 /* complete taskfile transaction */
Albert Leea22e2eb2005-12-05 15:38:02 +08003568 qc->err_mask |= ac_err_mask(drv_stat);
3569 ata_qc_complete(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003570 break;
3571 }
Jeff Garzikb8f61532005-08-25 22:01:20 -04003572
3573 spin_unlock_irqrestore(&host_set->lock, flags);
3574
Linus Torvalds1da177e2005-04-16 15:20:36 -07003575 DPRINTK("EXIT\n");
3576}
3577
3578/**
3579 * ata_eng_timeout - Handle timeout of queued command
3580 * @ap: Port on which timed-out command is active
3581 *
3582 * Some part of the kernel (currently, only the SCSI layer)
3583 * has noticed that the active command on port @ap has not
3584 * completed after a specified length of time. Handle this
3585 * condition by disabling DMA (if necessary) and completing
3586 * transactions, with error if necessary.
3587 *
3588 * This also handles the case of the "lost interrupt", where
3589 * for some reason (possibly hardware bug, possibly driver bug)
3590 * an interrupt was not delivered to the driver, even though the
3591 * transaction completed successfully.
3592 *
3593 * LOCKING:
3594 * Inherited from SCSI layer (none, can sleep)
3595 */
3596
3597void ata_eng_timeout(struct ata_port *ap)
3598{
3599 struct ata_queued_cmd *qc;
3600
3601 DPRINTK("ENTER\n");
3602
3603 qc = ata_qc_from_tag(ap, ap->active_tag);
Jeff Garzike12669e2005-10-05 18:39:23 -04003604 if (qc)
3605 ata_qc_timeout(qc);
3606 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003607 printk(KERN_ERR "ata%u: BUG: timeout without command\n",
3608 ap->id);
3609 goto out;
3610 }
3611
Linus Torvalds1da177e2005-04-16 15:20:36 -07003612out:
3613 DPRINTK("EXIT\n");
3614}
3615
3616/**
3617 * ata_qc_new - Request an available ATA command, for queueing
3618 * @ap: Port associated with device @dev
3619 * @dev: Device from whom we request an available command structure
3620 *
3621 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003622 * None.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003623 */
3624
3625static struct ata_queued_cmd *ata_qc_new(struct ata_port *ap)
3626{
3627 struct ata_queued_cmd *qc = NULL;
3628 unsigned int i;
3629
3630 for (i = 0; i < ATA_MAX_QUEUE; i++)
3631 if (!test_and_set_bit(i, &ap->qactive)) {
3632 qc = ata_qc_from_tag(ap, i);
3633 break;
3634 }
3635
3636 if (qc)
3637 qc->tag = i;
3638
3639 return qc;
3640}
3641
3642/**
3643 * ata_qc_new_init - Request an available ATA command, and initialize it
3644 * @ap: Port associated with device @dev
3645 * @dev: Device from whom we request an available command structure
3646 *
3647 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003648 * None.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003649 */
3650
3651struct ata_queued_cmd *ata_qc_new_init(struct ata_port *ap,
3652 struct ata_device *dev)
3653{
3654 struct ata_queued_cmd *qc;
3655
3656 qc = ata_qc_new(ap);
3657 if (qc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003658 qc->scsicmd = NULL;
3659 qc->ap = ap;
3660 qc->dev = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003661
Jeff Garzik2c13b7c2005-11-14 14:14:16 -05003662 ata_qc_reinit(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003663 }
3664
3665 return qc;
3666}
3667
Albert Leea22e2eb2005-12-05 15:38:02 +08003668int ata_qc_complete_noop(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003669{
3670 return 0;
3671}
3672
3673static void __ata_qc_complete(struct ata_queued_cmd *qc)
3674{
3675 struct ata_port *ap = qc->ap;
3676 unsigned int tag, do_clear = 0;
3677
3678 qc->flags = 0;
3679 tag = qc->tag;
3680 if (likely(ata_tag_valid(tag))) {
3681 if (tag == ap->active_tag)
3682 ap->active_tag = ATA_TAG_POISON;
3683 qc->tag = ATA_TAG_POISON;
3684 do_clear = 1;
3685 }
3686
3687 if (qc->waiting) {
3688 struct completion *waiting = qc->waiting;
3689 qc->waiting = NULL;
3690 complete(waiting);
3691 }
3692
3693 if (likely(do_clear))
3694 clear_bit(tag, &ap->qactive);
3695}
3696
3697/**
3698 * ata_qc_free - free unused ata_queued_cmd
3699 * @qc: Command to complete
3700 *
3701 * Designed to free unused ata_queued_cmd object
3702 * in case something prevents using it.
3703 *
3704 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003705 * spin_lock_irqsave(host_set lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003706 */
3707void ata_qc_free(struct ata_queued_cmd *qc)
3708{
3709 assert(qc != NULL); /* ata_qc_from_tag _might_ return NULL */
3710 assert(qc->waiting == NULL); /* nothing should be waiting */
3711
3712 __ata_qc_complete(qc);
3713}
3714
3715/**
3716 * ata_qc_complete - Complete an active ATA command
3717 * @qc: Command to complete
Randy Dunlap8e8b77d2005-11-01 21:29:27 -08003718 * @err_mask: ATA Status register contents
Jeff Garzik0cba6322005-05-30 19:49:12 -04003719 *
3720 * Indicate to the mid and upper layers that an ATA
3721 * command has completed, with either an ok or not-ok status.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003722 *
3723 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003724 * spin_lock_irqsave(host_set lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003725 */
3726
Albert Leea22e2eb2005-12-05 15:38:02 +08003727void ata_qc_complete(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003728{
3729 int rc;
3730
3731 assert(qc != NULL); /* ata_qc_from_tag _might_ return NULL */
3732 assert(qc->flags & ATA_QCFLAG_ACTIVE);
3733
3734 if (likely(qc->flags & ATA_QCFLAG_DMAMAP))
3735 ata_sg_clean(qc);
3736
Albert Lee3f3791d2005-08-16 14:25:38 +08003737 /* atapi: mark qc as inactive to prevent the interrupt handler
3738 * from completing the command twice later, before the error handler
3739 * is called. (when rc != 0 and atapi request sense is needed)
3740 */
3741 qc->flags &= ~ATA_QCFLAG_ACTIVE;
3742
Linus Torvalds1da177e2005-04-16 15:20:36 -07003743 /* call completion callback */
Albert Leea22e2eb2005-12-05 15:38:02 +08003744 rc = qc->complete_fn(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003745
3746 /* if callback indicates not to complete command (non-zero),
3747 * return immediately
3748 */
3749 if (rc != 0)
3750 return;
3751
3752 __ata_qc_complete(qc);
3753
3754 VPRINTK("EXIT\n");
3755}
3756
3757static inline int ata_should_dma_map(struct ata_queued_cmd *qc)
3758{
3759 struct ata_port *ap = qc->ap;
3760
3761 switch (qc->tf.protocol) {
3762 case ATA_PROT_DMA:
3763 case ATA_PROT_ATAPI_DMA:
3764 return 1;
3765
3766 case ATA_PROT_ATAPI:
3767 case ATA_PROT_PIO:
3768 case ATA_PROT_PIO_MULT:
3769 if (ap->flags & ATA_FLAG_PIO_DMA)
3770 return 1;
3771
3772 /* fall through */
3773
3774 default:
3775 return 0;
3776 }
3777
3778 /* never reached */
3779}
3780
3781/**
3782 * ata_qc_issue - issue taskfile to device
3783 * @qc: command to issue to device
3784 *
3785 * Prepare an ATA command to submission to device.
3786 * This includes mapping the data into a DMA-able
3787 * area, filling in the S/G table, and finally
3788 * writing the taskfile to hardware, starting the command.
3789 *
3790 * LOCKING:
3791 * spin_lock_irqsave(host_set lock)
3792 *
3793 * RETURNS:
3794 * Zero on success, negative on error.
3795 */
3796
3797int ata_qc_issue(struct ata_queued_cmd *qc)
3798{
3799 struct ata_port *ap = qc->ap;
3800
3801 if (ata_should_dma_map(qc)) {
3802 if (qc->flags & ATA_QCFLAG_SG) {
3803 if (ata_sg_setup(qc))
3804 goto err_out;
3805 } else if (qc->flags & ATA_QCFLAG_SINGLE) {
3806 if (ata_sg_setup_one(qc))
3807 goto err_out;
3808 }
3809 } else {
3810 qc->flags &= ~ATA_QCFLAG_DMAMAP;
3811 }
3812
3813 ap->ops->qc_prep(qc);
3814
3815 qc->ap->active_tag = qc->tag;
3816 qc->flags |= ATA_QCFLAG_ACTIVE;
3817
3818 return ap->ops->qc_issue(qc);
3819
3820err_out:
3821 return -1;
3822}
3823
Edward Falk0baab862005-06-02 18:17:13 -04003824
Linus Torvalds1da177e2005-04-16 15:20:36 -07003825/**
3826 * ata_qc_issue_prot - issue taskfile to device in proto-dependent manner
3827 * @qc: command to issue to device
3828 *
3829 * Using various libata functions and hooks, this function
3830 * starts an ATA command. ATA commands are grouped into
3831 * classes called "protocols", and issuing each type of protocol
3832 * is slightly different.
3833 *
Edward Falk0baab862005-06-02 18:17:13 -04003834 * May be used as the qc_issue() entry in ata_port_operations.
3835 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003836 * LOCKING:
3837 * spin_lock_irqsave(host_set lock)
3838 *
3839 * RETURNS:
3840 * Zero on success, negative on error.
3841 */
3842
3843int ata_qc_issue_prot(struct ata_queued_cmd *qc)
3844{
3845 struct ata_port *ap = qc->ap;
3846
Albert Leee50362e2005-09-27 17:39:50 +08003847 /* Use polling pio if the LLD doesn't handle
3848 * interrupt driven pio and atapi CDB interrupt.
3849 */
3850 if (ap->flags & ATA_FLAG_PIO_POLLING) {
3851 switch (qc->tf.protocol) {
3852 case ATA_PROT_PIO:
3853 case ATA_PROT_ATAPI:
3854 case ATA_PROT_ATAPI_NODATA:
3855 qc->tf.flags |= ATA_TFLAG_POLLING;
3856 break;
3857 case ATA_PROT_ATAPI_DMA:
3858 if (qc->dev->flags & ATA_DFLAG_CDB_INTR)
3859 BUG();
3860 break;
3861 default:
3862 break;
3863 }
3864 }
3865
Albert Lee312f7da2005-09-27 17:38:03 +08003866 /* select the device */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003867 ata_dev_select(ap, qc->dev->devno, 1, 0);
3868
Albert Lee312f7da2005-09-27 17:38:03 +08003869 /* start the command */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003870 switch (qc->tf.protocol) {
3871 case ATA_PROT_NODATA:
Albert Lee312f7da2005-09-27 17:38:03 +08003872 if (qc->tf.flags & ATA_TFLAG_POLLING)
3873 ata_qc_set_polling(qc);
3874
Jeff Garzike5338252005-10-30 21:37:17 -05003875 ata_tf_to_host(ap, &qc->tf);
Albert Lee312f7da2005-09-27 17:38:03 +08003876 ap->hsm_task_state = HSM_ST_LAST;
3877
3878 if (qc->tf.flags & ATA_TFLAG_POLLING)
3879 queue_work(ata_wq, &ap->pio_task);
3880
Linus Torvalds1da177e2005-04-16 15:20:36 -07003881 break;
3882
3883 case ATA_PROT_DMA:
Albert Lee312f7da2005-09-27 17:38:03 +08003884 assert(!(qc->tf.flags & ATA_TFLAG_POLLING));
3885
Linus Torvalds1da177e2005-04-16 15:20:36 -07003886 ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
3887 ap->ops->bmdma_setup(qc); /* set up bmdma */
3888 ap->ops->bmdma_start(qc); /* initiate bmdma */
Albert Lee312f7da2005-09-27 17:38:03 +08003889 ap->hsm_task_state = HSM_ST_LAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003890 break;
3891
Albert Lee312f7da2005-09-27 17:38:03 +08003892 case ATA_PROT_PIO:
3893 if (qc->tf.flags & ATA_TFLAG_POLLING)
3894 ata_qc_set_polling(qc);
3895
Jeff Garzike5338252005-10-30 21:37:17 -05003896 ata_tf_to_host(ap, &qc->tf);
Albert Lee312f7da2005-09-27 17:38:03 +08003897
Albert Lee54f00382005-09-30 19:14:19 +08003898 if (qc->tf.flags & ATA_TFLAG_WRITE) {
3899 /* PIO data out protocol */
3900 ap->hsm_task_state = HSM_ST_FIRST;
Albert Leee27486d2005-11-01 19:24:49 +08003901 queue_work(ata_wq, &ap->pio_task);
Albert Lee54f00382005-09-30 19:14:19 +08003902
3903 /* always send first data block using
Albert Leee27486d2005-11-01 19:24:49 +08003904 * the ata_pio_task() codepath.
Albert Lee54f00382005-09-30 19:14:19 +08003905 */
Albert Lee312f7da2005-09-27 17:38:03 +08003906 } else {
Albert Lee54f00382005-09-30 19:14:19 +08003907 /* PIO data in protocol */
3908 ap->hsm_task_state = HSM_ST;
Albert Lee312f7da2005-09-27 17:38:03 +08003909
Albert Lee54f00382005-09-30 19:14:19 +08003910 if (qc->tf.flags & ATA_TFLAG_POLLING)
3911 queue_work(ata_wq, &ap->pio_task);
Albert Lee312f7da2005-09-27 17:38:03 +08003912
Albert Lee54f00382005-09-30 19:14:19 +08003913 /* if polling, ata_pio_task() handles the rest.
3914 * otherwise, interrupt handler takes over from here.
3915 */
Albert Lee312f7da2005-09-27 17:38:03 +08003916 }
3917
Linus Torvalds1da177e2005-04-16 15:20:36 -07003918 break;
3919
3920 case ATA_PROT_ATAPI:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003921 case ATA_PROT_ATAPI_NODATA:
Albert Lee312f7da2005-09-27 17:38:03 +08003922 if (qc->tf.flags & ATA_TFLAG_POLLING)
3923 ata_qc_set_polling(qc);
3924
Jeff Garzike5338252005-10-30 21:37:17 -05003925 ata_tf_to_host(ap, &qc->tf);
Albert Lee312f7da2005-09-27 17:38:03 +08003926 ap->hsm_task_state = HSM_ST_FIRST;
3927
3928 /* send cdb by polling if no cdb interrupt */
3929 if ((!(qc->dev->flags & ATA_DFLAG_CDB_INTR)) ||
3930 (qc->tf.flags & ATA_TFLAG_POLLING))
Albert Leee27486d2005-11-01 19:24:49 +08003931 queue_work(ata_wq, &ap->pio_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003932 break;
3933
3934 case ATA_PROT_ATAPI_DMA:
Albert Lee312f7da2005-09-27 17:38:03 +08003935 assert(!(qc->tf.flags & ATA_TFLAG_POLLING));
3936
Linus Torvalds1da177e2005-04-16 15:20:36 -07003937 ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
3938 ap->ops->bmdma_setup(qc); /* set up bmdma */
Albert Lee312f7da2005-09-27 17:38:03 +08003939 ap->hsm_task_state = HSM_ST_FIRST;
3940
3941 /* send cdb by polling if no cdb interrupt */
3942 if (!(qc->dev->flags & ATA_DFLAG_CDB_INTR))
Albert Leee27486d2005-11-01 19:24:49 +08003943 queue_work(ata_wq, &ap->pio_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003944 break;
3945
3946 default:
3947 WARN_ON(1);
3948 return -1;
3949 }
3950
3951 return 0;
3952}
3953
3954/**
Edward Falk0baab862005-06-02 18:17:13 -04003955 * ata_bmdma_setup_mmio - Set up PCI IDE BMDMA transaction
Linus Torvalds1da177e2005-04-16 15:20:36 -07003956 * @qc: Info associated with this ATA transaction.
3957 *
3958 * LOCKING:
3959 * spin_lock_irqsave(host_set lock)
3960 */
3961
3962static void ata_bmdma_setup_mmio (struct ata_queued_cmd *qc)
3963{
3964 struct ata_port *ap = qc->ap;
3965 unsigned int rw = (qc->tf.flags & ATA_TFLAG_WRITE);
3966 u8 dmactl;
3967 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
3968
3969 /* load PRD table addr. */
3970 mb(); /* make sure PRD table writes are visible to controller */
3971 writel(ap->prd_dma, mmio + ATA_DMA_TABLE_OFS);
3972
3973 /* specify data direction, triple-check start bit is clear */
3974 dmactl = readb(mmio + ATA_DMA_CMD);
3975 dmactl &= ~(ATA_DMA_WR | ATA_DMA_START);
3976 if (!rw)
3977 dmactl |= ATA_DMA_WR;
3978 writeb(dmactl, mmio + ATA_DMA_CMD);
3979
3980 /* issue r/w command */
3981 ap->ops->exec_command(ap, &qc->tf);
3982}
3983
3984/**
Alan Coxb73fc892005-08-26 16:03:19 +01003985 * ata_bmdma_start_mmio - Start a PCI IDE BMDMA transaction
Linus Torvalds1da177e2005-04-16 15:20:36 -07003986 * @qc: Info associated with this ATA transaction.
3987 *
3988 * LOCKING:
3989 * spin_lock_irqsave(host_set lock)
3990 */
3991
3992static void ata_bmdma_start_mmio (struct ata_queued_cmd *qc)
3993{
3994 struct ata_port *ap = qc->ap;
3995 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
3996 u8 dmactl;
3997
3998 /* start host DMA transaction */
3999 dmactl = readb(mmio + ATA_DMA_CMD);
4000 writeb(dmactl | ATA_DMA_START, mmio + ATA_DMA_CMD);
4001
4002 /* Strictly, one may wish to issue a readb() here, to
4003 * flush the mmio write. However, control also passes
4004 * to the hardware at this point, and it will interrupt
4005 * us when we are to resume control. So, in effect,
4006 * we don't care when the mmio write flushes.
4007 * Further, a read of the DMA status register _immediately_
4008 * following the write may not be what certain flaky hardware
4009 * is expected, so I think it is best to not add a readb()
4010 * without first all the MMIO ATA cards/mobos.
4011 * Or maybe I'm just being paranoid.
4012 */
4013}
4014
4015/**
4016 * ata_bmdma_setup_pio - Set up PCI IDE BMDMA transaction (PIO)
4017 * @qc: Info associated with this ATA transaction.
4018 *
4019 * LOCKING:
4020 * spin_lock_irqsave(host_set lock)
4021 */
4022
4023static void ata_bmdma_setup_pio (struct ata_queued_cmd *qc)
4024{
4025 struct ata_port *ap = qc->ap;
4026 unsigned int rw = (qc->tf.flags & ATA_TFLAG_WRITE);
4027 u8 dmactl;
4028
4029 /* load PRD table addr. */
4030 outl(ap->prd_dma, ap->ioaddr.bmdma_addr + ATA_DMA_TABLE_OFS);
4031
4032 /* specify data direction, triple-check start bit is clear */
4033 dmactl = inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
4034 dmactl &= ~(ATA_DMA_WR | ATA_DMA_START);
4035 if (!rw)
4036 dmactl |= ATA_DMA_WR;
4037 outb(dmactl, ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
4038
4039 /* issue r/w command */
4040 ap->ops->exec_command(ap, &qc->tf);
4041}
4042
4043/**
4044 * ata_bmdma_start_pio - Start a PCI IDE BMDMA transaction (PIO)
4045 * @qc: Info associated with this ATA transaction.
4046 *
4047 * LOCKING:
4048 * spin_lock_irqsave(host_set lock)
4049 */
4050
4051static void ata_bmdma_start_pio (struct ata_queued_cmd *qc)
4052{
4053 struct ata_port *ap = qc->ap;
4054 u8 dmactl;
4055
4056 /* start host DMA transaction */
4057 dmactl = inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
4058 outb(dmactl | ATA_DMA_START,
4059 ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
4060}
4061
Edward Falk0baab862005-06-02 18:17:13 -04004062
4063/**
4064 * ata_bmdma_start - Start a PCI IDE BMDMA transaction
4065 * @qc: Info associated with this ATA transaction.
4066 *
4067 * Writes the ATA_DMA_START flag to the DMA command register.
4068 *
4069 * May be used as the bmdma_start() entry in ata_port_operations.
4070 *
4071 * LOCKING:
4072 * spin_lock_irqsave(host_set lock)
4073 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004074void ata_bmdma_start(struct ata_queued_cmd *qc)
4075{
4076 if (qc->ap->flags & ATA_FLAG_MMIO)
4077 ata_bmdma_start_mmio(qc);
4078 else
4079 ata_bmdma_start_pio(qc);
4080}
4081
Edward Falk0baab862005-06-02 18:17:13 -04004082
4083/**
4084 * ata_bmdma_setup - Set up PCI IDE BMDMA transaction
4085 * @qc: Info associated with this ATA transaction.
4086 *
4087 * Writes address of PRD table to device's PRD Table Address
4088 * register, sets the DMA control register, and calls
4089 * ops->exec_command() to start the transfer.
4090 *
4091 * May be used as the bmdma_setup() entry in ata_port_operations.
4092 *
4093 * LOCKING:
4094 * spin_lock_irqsave(host_set lock)
4095 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004096void ata_bmdma_setup(struct ata_queued_cmd *qc)
4097{
4098 if (qc->ap->flags & ATA_FLAG_MMIO)
4099 ata_bmdma_setup_mmio(qc);
4100 else
4101 ata_bmdma_setup_pio(qc);
4102}
4103
Edward Falk0baab862005-06-02 18:17:13 -04004104
4105/**
4106 * ata_bmdma_irq_clear - Clear PCI IDE BMDMA interrupt.
Jeff Garzikdecc6d02005-06-02 18:42:33 -04004107 * @ap: Port associated with this ATA transaction.
Edward Falk0baab862005-06-02 18:17:13 -04004108 *
4109 * Clear interrupt and error flags in DMA status register.
4110 *
4111 * May be used as the irq_clear() entry in ata_port_operations.
4112 *
4113 * LOCKING:
4114 * spin_lock_irqsave(host_set lock)
4115 */
4116
Linus Torvalds1da177e2005-04-16 15:20:36 -07004117void ata_bmdma_irq_clear(struct ata_port *ap)
4118{
4119 if (ap->flags & ATA_FLAG_MMIO) {
4120 void __iomem *mmio = ((void __iomem *) ap->ioaddr.bmdma_addr) + ATA_DMA_STATUS;
4121 writeb(readb(mmio), mmio);
4122 } else {
4123 unsigned long addr = ap->ioaddr.bmdma_addr + ATA_DMA_STATUS;
4124 outb(inb(addr), addr);
4125 }
4126
4127}
4128
Edward Falk0baab862005-06-02 18:17:13 -04004129
4130/**
4131 * ata_bmdma_status - Read PCI IDE BMDMA status
Jeff Garzikdecc6d02005-06-02 18:42:33 -04004132 * @ap: Port associated with this ATA transaction.
Edward Falk0baab862005-06-02 18:17:13 -04004133 *
4134 * Read and return BMDMA status register.
4135 *
4136 * May be used as the bmdma_status() entry in ata_port_operations.
4137 *
4138 * LOCKING:
4139 * spin_lock_irqsave(host_set lock)
4140 */
4141
Linus Torvalds1da177e2005-04-16 15:20:36 -07004142u8 ata_bmdma_status(struct ata_port *ap)
4143{
4144 u8 host_stat;
4145 if (ap->flags & ATA_FLAG_MMIO) {
4146 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
4147 host_stat = readb(mmio + ATA_DMA_STATUS);
4148 } else
Albert Leeee500aa2005-09-27 17:34:38 +08004149 host_stat = inb(ap->ioaddr.bmdma_addr + ATA_DMA_STATUS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004150 return host_stat;
4151}
4152
Edward Falk0baab862005-06-02 18:17:13 -04004153
4154/**
4155 * ata_bmdma_stop - Stop PCI IDE BMDMA transfer
Alan Coxb73fc892005-08-26 16:03:19 +01004156 * @qc: Command we are ending DMA for
Edward Falk0baab862005-06-02 18:17:13 -04004157 *
4158 * Clears the ATA_DMA_START flag in the dma control register
4159 *
4160 * May be used as the bmdma_stop() entry in ata_port_operations.
4161 *
4162 * LOCKING:
4163 * spin_lock_irqsave(host_set lock)
4164 */
4165
Alan Coxb73fc892005-08-26 16:03:19 +01004166void ata_bmdma_stop(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004167{
Alan Coxb73fc892005-08-26 16:03:19 +01004168 struct ata_port *ap = qc->ap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004169 if (ap->flags & ATA_FLAG_MMIO) {
4170 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
4171
4172 /* clear start/stop bit */
4173 writeb(readb(mmio + ATA_DMA_CMD) & ~ATA_DMA_START,
4174 mmio + ATA_DMA_CMD);
4175 } else {
4176 /* clear start/stop bit */
4177 outb(inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD) & ~ATA_DMA_START,
4178 ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
4179 }
4180
4181 /* one-PIO-cycle guaranteed wait, per spec, for HDMA1:0 transition */
4182 ata_altstatus(ap); /* dummy read */
4183}
4184
4185/**
4186 * ata_host_intr - Handle host interrupt for given (port, task)
4187 * @ap: Port on which interrupt arrived (possibly...)
4188 * @qc: Taskfile currently active in engine
4189 *
4190 * Handle host interrupt for given queued command. Currently,
4191 * only DMA interrupts are handled. All other commands are
4192 * handled via polling with interrupts disabled (nIEN bit).
4193 *
4194 * LOCKING:
4195 * spin_lock_irqsave(host_set lock)
4196 *
4197 * RETURNS:
4198 * One if interrupt was handled, zero if not (shared irq).
4199 */
4200
4201inline unsigned int ata_host_intr (struct ata_port *ap,
4202 struct ata_queued_cmd *qc)
4203{
Albert Lee312f7da2005-09-27 17:38:03 +08004204 u8 status, host_stat = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004205
Albert Lee312f7da2005-09-27 17:38:03 +08004206 VPRINTK("ata%u: protocol %d task_state %d\n",
4207 ap->id, qc->tf.protocol, ap->hsm_task_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004208
Albert Lee312f7da2005-09-27 17:38:03 +08004209 /* Check whether we are expecting interrupt in this state */
4210 switch (ap->hsm_task_state) {
4211 case HSM_ST_FIRST:
4212 /* Check the ATA_DFLAG_CDB_INTR flag is enough here.
4213 * The flag was turned on only for atapi devices.
4214 * No need to check is_atapi_taskfile(&qc->tf) again.
4215 */
4216 if (!(qc->dev->flags & ATA_DFLAG_CDB_INTR))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004217 goto idle_irq;
Albert Lee312f7da2005-09-27 17:38:03 +08004218 break;
4219 case HSM_ST_LAST:
4220 if (qc->tf.protocol == ATA_PROT_DMA ||
4221 qc->tf.protocol == ATA_PROT_ATAPI_DMA) {
4222 /* check status of DMA engine */
4223 host_stat = ap->ops->bmdma_status(ap);
4224 VPRINTK("ata%u: host_stat 0x%X\n", ap->id, host_stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004225
Albert Lee312f7da2005-09-27 17:38:03 +08004226 /* if it's not our irq... */
4227 if (!(host_stat & ATA_DMA_INTR))
4228 goto idle_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004229
Albert Lee312f7da2005-09-27 17:38:03 +08004230 /* before we do anything else, clear DMA-Start bit */
4231 ap->ops->bmdma_stop(qc);
4232 }
4233 break;
4234 case HSM_ST:
4235 break;
4236 default:
4237 goto idle_irq;
4238 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004239
Albert Lee312f7da2005-09-27 17:38:03 +08004240 /* check altstatus */
4241 status = ata_altstatus(ap);
4242 if (status & ATA_BUSY)
4243 goto idle_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004244
Albert Lee312f7da2005-09-27 17:38:03 +08004245 /* check main status, clearing INTRQ */
4246 status = ata_chk_status(ap);
4247 if (unlikely(status & ATA_BUSY))
4248 goto idle_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004249
Albert Lee312f7da2005-09-27 17:38:03 +08004250 DPRINTK("ata%u: protocol %d task_state %d (dev_stat 0x%X)\n",
4251 ap->id, qc->tf.protocol, ap->hsm_task_state, status);
4252
4253 /* ack bmdma irq events */
4254 ap->ops->irq_clear(ap);
4255
4256 /* check error */
4257 if (unlikely((status & ATA_ERR) || (host_stat & ATA_DMA_ERR)))
4258 ap->hsm_task_state = HSM_ST_ERR;
4259
4260fsm_start:
4261 switch (ap->hsm_task_state) {
4262 case HSM_ST_FIRST:
4263 /* Some pre-ATAPI-4 devices assert INTRQ
4264 * at this state when ready to receive CDB.
4265 */
4266
4267 /* check device status */
4268 if (unlikely((status & (ATA_BUSY | ATA_DRQ)) != ATA_DRQ)) {
4269 /* Wrong status. Let EH handle this */
4270 ap->hsm_task_state = HSM_ST_ERR;
4271 goto fsm_start;
4272 }
4273
4274 atapi_send_cdb(ap, qc);
4275
4276 break;
4277
4278 case HSM_ST:
4279 /* complete command or read/write the data register */
4280 if (qc->tf.protocol == ATA_PROT_ATAPI) {
4281 /* ATAPI PIO protocol */
4282 if ((status & ATA_DRQ) == 0) {
4283 /* no more data to transfer */
4284 ap->hsm_task_state = HSM_ST_LAST;
4285 goto fsm_start;
4286 }
4287
4288 atapi_pio_bytes(qc);
4289
4290 if (unlikely(ap->hsm_task_state == HSM_ST_ERR))
4291 /* bad ireason reported by device */
4292 goto fsm_start;
4293
4294 } else {
4295 /* ATA PIO protocol */
4296 if (unlikely((status & ATA_DRQ) == 0)) {
4297 /* handle BSY=0, DRQ=0 as error */
4298 ap->hsm_task_state = HSM_ST_ERR;
4299 goto fsm_start;
4300 }
4301
Albert Lee07f6f7d2005-11-01 19:33:20 +08004302 ata_pio_sectors(qc);
Albert Lee312f7da2005-09-27 17:38:03 +08004303
4304 if (ap->hsm_task_state == HSM_ST_LAST &&
4305 (!(qc->tf.flags & ATA_TFLAG_WRITE))) {
4306 /* all data read */
4307 ata_altstatus(ap);
4308 status = ata_chk_status(ap);
4309 goto fsm_start;
4310 }
4311 }
4312
4313 ata_altstatus(ap); /* flush */
4314 break;
4315
4316 case HSM_ST_LAST:
4317 if (unlikely(status & ATA_DRQ)) {
4318 /* handle DRQ=1 as error */
4319 ap->hsm_task_state = HSM_ST_ERR;
4320 goto fsm_start;
4321 }
4322
4323 /* no more data to transfer */
4324 DPRINTK("ata%u: command complete, drv_stat 0x%x\n",
4325 ap->id, status);
4326
4327 ap->hsm_task_state = HSM_ST_IDLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004328
4329 /* complete taskfile transaction */
Albert Leea22e2eb2005-12-05 15:38:02 +08004330 qc->err_mask |= ac_err_mask(status);
4331 ata_qc_complete(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004332 break;
4333
Albert Lee312f7da2005-09-27 17:38:03 +08004334 case HSM_ST_ERR:
4335 printk(KERN_ERR "ata%u: command error, drv_stat 0x%x host_stat 0x%x\n",
4336 ap->id, status, host_stat);
4337
4338 ap->hsm_task_state = HSM_ST_IDLE;
Jeff Garzik278efe92005-12-06 05:01:27 -05004339 qc->err_mask |= __ac_err_mask(status);
4340 ata_qc_complete(qc);
Albert Lee312f7da2005-09-27 17:38:03 +08004341 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004342 default:
4343 goto idle_irq;
4344 }
4345
4346 return 1; /* irq handled */
4347
4348idle_irq:
4349 ap->stats.idle_irq++;
4350
4351#ifdef ATA_IRQ_TRAP
4352 if ((ap->stats.idle_irq % 1000) == 0) {
4353 handled = 1;
4354 ata_irq_ack(ap, 0); /* debug trap */
4355 printk(KERN_WARNING "ata%d: irq trap\n", ap->id);
4356 }
4357#endif
4358 return 0; /* irq not handled */
4359}
4360
4361/**
4362 * ata_interrupt - Default ATA host interrupt handler
Jeff Garzik0cba6322005-05-30 19:49:12 -04004363 * @irq: irq line (unused)
4364 * @dev_instance: pointer to our ata_host_set information structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07004365 * @regs: unused
4366 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04004367 * Default interrupt handler for PCI IDE devices. Calls
4368 * ata_host_intr() for each port that is not disabled.
4369 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004370 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004371 * Obtains host_set lock during operation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004372 *
4373 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004374 * IRQ_NONE or IRQ_HANDLED.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004375 */
4376
4377irqreturn_t ata_interrupt (int irq, void *dev_instance, struct pt_regs *regs)
4378{
4379 struct ata_host_set *host_set = dev_instance;
4380 unsigned int i;
4381 unsigned int handled = 0;
4382 unsigned long flags;
4383
4384 /* TODO: make _irqsave conditional on x86 PCI IDE legacy mode */
4385 spin_lock_irqsave(&host_set->lock, flags);
4386
4387 for (i = 0; i < host_set->n_ports; i++) {
4388 struct ata_port *ap;
4389
4390 ap = host_set->ports[i];
Tejun Heoc1389502005-08-22 14:59:24 +09004391 if (ap &&
Albert Lee312f7da2005-09-27 17:38:03 +08004392 !(ap->flags & ATA_FLAG_PORT_DISABLED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004393 struct ata_queued_cmd *qc;
4394
4395 qc = ata_qc_from_tag(ap, ap->active_tag);
Albert Lee312f7da2005-09-27 17:38:03 +08004396 if (qc && (!(qc->tf.flags & ATA_TFLAG_POLLING)) &&
Albert Lee21b1ed72005-04-29 17:34:59 +08004397 (qc->flags & ATA_QCFLAG_ACTIVE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004398 handled |= ata_host_intr(ap, qc);
4399 }
4400 }
4401
4402 spin_unlock_irqrestore(&host_set->lock, flags);
4403
4404 return IRQ_RETVAL(handled);
4405}
4406
4407/**
Edward Falk0baab862005-06-02 18:17:13 -04004408 * ata_port_start - Set port up for dma.
4409 * @ap: Port to initialize
4410 *
4411 * Called just after data structures for each port are
4412 * initialized. Allocates space for PRD table.
4413 *
4414 * May be used as the port_start() entry in ata_port_operations.
4415 *
4416 * LOCKING:
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04004417 * Inherited from caller.
Edward Falk0baab862005-06-02 18:17:13 -04004418 */
4419
Linus Torvalds1da177e2005-04-16 15:20:36 -07004420int ata_port_start (struct ata_port *ap)
4421{
4422 struct device *dev = ap->host_set->dev;
Jeff Garzik6037d6b2005-11-04 22:08:00 -05004423 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004424
4425 ap->prd = dma_alloc_coherent(dev, ATA_PRD_TBL_SZ, &ap->prd_dma, GFP_KERNEL);
4426 if (!ap->prd)
4427 return -ENOMEM;
4428
Jeff Garzik6037d6b2005-11-04 22:08:00 -05004429 rc = ata_pad_alloc(ap, dev);
4430 if (rc) {
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004431 dma_free_coherent(dev, ATA_PRD_TBL_SZ, ap->prd, ap->prd_dma);
Jeff Garzik6037d6b2005-11-04 22:08:00 -05004432 return rc;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004433 }
4434
Linus Torvalds1da177e2005-04-16 15:20:36 -07004435 DPRINTK("prd alloc, virt %p, dma %llx\n", ap->prd, (unsigned long long) ap->prd_dma);
4436
4437 return 0;
4438}
4439
Edward Falk0baab862005-06-02 18:17:13 -04004440
4441/**
4442 * ata_port_stop - Undo ata_port_start()
4443 * @ap: Port to shut down
4444 *
4445 * Frees the PRD table.
4446 *
4447 * May be used as the port_stop() entry in ata_port_operations.
4448 *
4449 * LOCKING:
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04004450 * Inherited from caller.
Edward Falk0baab862005-06-02 18:17:13 -04004451 */
4452
Linus Torvalds1da177e2005-04-16 15:20:36 -07004453void ata_port_stop (struct ata_port *ap)
4454{
4455 struct device *dev = ap->host_set->dev;
4456
4457 dma_free_coherent(dev, ATA_PRD_TBL_SZ, ap->prd, ap->prd_dma);
Jeff Garzik6037d6b2005-11-04 22:08:00 -05004458 ata_pad_free(ap, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004459}
4460
Jeff Garzikaa8f0dc2005-05-26 21:54:27 -04004461void ata_host_stop (struct ata_host_set *host_set)
4462{
4463 if (host_set->mmio_base)
4464 iounmap(host_set->mmio_base);
4465}
4466
4467
Linus Torvalds1da177e2005-04-16 15:20:36 -07004468/**
4469 * ata_host_remove - Unregister SCSI host structure with upper layers
4470 * @ap: Port to unregister
4471 * @do_unregister: 1 if we fully unregister, 0 to just stop the port
4472 *
4473 * LOCKING:
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04004474 * Inherited from caller.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004475 */
4476
4477static void ata_host_remove(struct ata_port *ap, unsigned int do_unregister)
4478{
4479 struct Scsi_Host *sh = ap->host;
4480
4481 DPRINTK("ENTER\n");
4482
4483 if (do_unregister)
4484 scsi_remove_host(sh);
4485
4486 ap->ops->port_stop(ap);
4487}
4488
4489/**
4490 * ata_host_init - Initialize an ata_port structure
4491 * @ap: Structure to initialize
4492 * @host: associated SCSI mid-layer structure
4493 * @host_set: Collection of hosts to which @ap belongs
4494 * @ent: Probe information provided by low-level driver
4495 * @port_no: Port number associated with this ata_port
4496 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04004497 * Initialize a new ata_port structure, and its associated
4498 * scsi_host.
4499 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004500 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004501 * Inherited from caller.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004502 */
4503
4504static void ata_host_init(struct ata_port *ap, struct Scsi_Host *host,
4505 struct ata_host_set *host_set,
Jeff Garzik057ace52005-10-22 14:27:05 -04004506 const struct ata_probe_ent *ent, unsigned int port_no)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004507{
4508 unsigned int i;
4509
4510 host->max_id = 16;
4511 host->max_lun = 1;
4512 host->max_channel = 1;
4513 host->unique_id = ata_unique_id++;
4514 host->max_cmd_len = 12;
Christoph Hellwig12413192005-06-11 01:05:01 +02004515
Linus Torvalds1da177e2005-04-16 15:20:36 -07004516 ap->flags = ATA_FLAG_PORT_DISABLED;
4517 ap->id = host->unique_id;
4518 ap->host = host;
4519 ap->ctl = ATA_DEVCTL_OBS;
4520 ap->host_set = host_set;
4521 ap->port_no = port_no;
4522 ap->hard_port_no =
4523 ent->legacy_mode ? ent->hard_port_no : port_no;
4524 ap->pio_mask = ent->pio_mask;
4525 ap->mwdma_mask = ent->mwdma_mask;
4526 ap->udma_mask = ent->udma_mask;
4527 ap->flags |= ent->host_flags;
4528 ap->ops = ent->port_ops;
4529 ap->cbl = ATA_CBL_NONE;
4530 ap->active_tag = ATA_TAG_POISON;
4531 ap->last_ctl = 0xFF;
4532
Linus Torvalds1da177e2005-04-16 15:20:36 -07004533 INIT_WORK(&ap->pio_task, ata_pio_task, ap);
4534
4535 for (i = 0; i < ATA_MAX_DEVICES; i++)
4536 ap->device[i].devno = i;
4537
4538#ifdef ATA_IRQ_TRAP
4539 ap->stats.unhandled_irq = 1;
4540 ap->stats.idle_irq = 1;
4541#endif
4542
4543 memcpy(&ap->ioaddr, &ent->port[port_no], sizeof(struct ata_ioports));
4544}
4545
4546/**
4547 * ata_host_add - Attach low-level ATA driver to system
4548 * @ent: Information provided by low-level driver
4549 * @host_set: Collections of ports to which we add
4550 * @port_no: Port number associated with this host
4551 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04004552 * Attach low-level ATA driver to system.
4553 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004554 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004555 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004556 *
4557 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004558 * New ata_port on success, for NULL on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004559 */
4560
Jeff Garzik057ace52005-10-22 14:27:05 -04004561static struct ata_port * ata_host_add(const struct ata_probe_ent *ent,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004562 struct ata_host_set *host_set,
4563 unsigned int port_no)
4564{
4565 struct Scsi_Host *host;
4566 struct ata_port *ap;
4567 int rc;
4568
4569 DPRINTK("ENTER\n");
4570 host = scsi_host_alloc(ent->sht, sizeof(struct ata_port));
4571 if (!host)
4572 return NULL;
4573
4574 ap = (struct ata_port *) &host->hostdata[0];
4575
4576 ata_host_init(ap, host, host_set, ent, port_no);
4577
4578 rc = ap->ops->port_start(ap);
4579 if (rc)
4580 goto err_out;
4581
4582 return ap;
4583
4584err_out:
4585 scsi_host_put(host);
4586 return NULL;
4587}
4588
4589/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04004590 * ata_device_add - Register hardware device with ATA and SCSI layers
4591 * @ent: Probe information describing hardware device to be registered
4592 *
4593 * This function processes the information provided in the probe
4594 * information struct @ent, allocates the necessary ATA and SCSI
4595 * host information structures, initializes them, and registers
4596 * everything with requisite kernel subsystems.
4597 *
4598 * This function requests irqs, probes the ATA bus, and probes
4599 * the SCSI bus.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004600 *
4601 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004602 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004603 *
4604 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004605 * Number of ports registered. Zero on error (no ports registered).
Linus Torvalds1da177e2005-04-16 15:20:36 -07004606 */
4607
Jeff Garzik057ace52005-10-22 14:27:05 -04004608int ata_device_add(const struct ata_probe_ent *ent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004609{
4610 unsigned int count = 0, i;
4611 struct device *dev = ent->dev;
4612 struct ata_host_set *host_set;
4613
4614 DPRINTK("ENTER\n");
4615 /* alloc a container for our list of ATA ports (buses) */
Randy Dunlap57f3bda2005-10-28 20:37:23 -07004616 host_set = kzalloc(sizeof(struct ata_host_set) +
Linus Torvalds1da177e2005-04-16 15:20:36 -07004617 (ent->n_ports * sizeof(void *)), GFP_KERNEL);
4618 if (!host_set)
4619 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004620 spin_lock_init(&host_set->lock);
4621
4622 host_set->dev = dev;
4623 host_set->n_ports = ent->n_ports;
4624 host_set->irq = ent->irq;
4625 host_set->mmio_base = ent->mmio_base;
4626 host_set->private_data = ent->private_data;
4627 host_set->ops = ent->port_ops;
4628
4629 /* register each port bound to this device */
4630 for (i = 0; i < ent->n_ports; i++) {
4631 struct ata_port *ap;
4632 unsigned long xfer_mode_mask;
4633
4634 ap = ata_host_add(ent, host_set, i);
4635 if (!ap)
4636 goto err_out;
4637
4638 host_set->ports[i] = ap;
4639 xfer_mode_mask =(ap->udma_mask << ATA_SHIFT_UDMA) |
4640 (ap->mwdma_mask << ATA_SHIFT_MWDMA) |
4641 (ap->pio_mask << ATA_SHIFT_PIO);
4642
4643 /* print per-port info to dmesg */
4644 printk(KERN_INFO "ata%u: %cATA max %s cmd 0x%lX ctl 0x%lX "
4645 "bmdma 0x%lX irq %lu\n",
4646 ap->id,
4647 ap->flags & ATA_FLAG_SATA ? 'S' : 'P',
4648 ata_mode_string(xfer_mode_mask),
4649 ap->ioaddr.cmd_addr,
4650 ap->ioaddr.ctl_addr,
4651 ap->ioaddr.bmdma_addr,
4652 ent->irq);
4653
4654 ata_chk_status(ap);
4655 host_set->ops->irq_clear(ap);
4656 count++;
4657 }
4658
Randy Dunlap57f3bda2005-10-28 20:37:23 -07004659 if (!count)
4660 goto err_free_ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004661
4662 /* obtain irq, that is shared between channels */
4663 if (request_irq(ent->irq, ent->port_ops->irq_handler, ent->irq_flags,
4664 DRV_NAME, host_set))
4665 goto err_out;
4666
4667 /* perform each probe synchronously */
4668 DPRINTK("probe begin\n");
4669 for (i = 0; i < count; i++) {
4670 struct ata_port *ap;
4671 int rc;
4672
4673 ap = host_set->ports[i];
4674
4675 DPRINTK("ata%u: probe begin\n", ap->id);
4676 rc = ata_bus_probe(ap);
4677 DPRINTK("ata%u: probe end\n", ap->id);
4678
4679 if (rc) {
4680 /* FIXME: do something useful here?
4681 * Current libata behavior will
4682 * tear down everything when
4683 * the module is removed
4684 * or the h/w is unplugged.
4685 */
4686 }
4687
4688 rc = scsi_add_host(ap->host, dev);
4689 if (rc) {
4690 printk(KERN_ERR "ata%u: scsi_add_host failed\n",
4691 ap->id);
4692 /* FIXME: do something useful here */
4693 /* FIXME: handle unconditional calls to
4694 * scsi_scan_host and ata_host_remove, below,
4695 * at the very least
4696 */
4697 }
4698 }
4699
4700 /* probes are done, now scan each port's disk(s) */
4701 DPRINTK("probe begin\n");
4702 for (i = 0; i < count; i++) {
4703 struct ata_port *ap = host_set->ports[i];
4704
Jeff Garzik644dd0c2005-10-03 15:55:19 -04004705 ata_scsi_scan_host(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004706 }
4707
4708 dev_set_drvdata(dev, host_set);
4709
4710 VPRINTK("EXIT, returning %u\n", ent->n_ports);
4711 return ent->n_ports; /* success */
4712
4713err_out:
4714 for (i = 0; i < count; i++) {
4715 ata_host_remove(host_set->ports[i], 1);
4716 scsi_host_put(host_set->ports[i]->host);
4717 }
Randy Dunlap57f3bda2005-10-28 20:37:23 -07004718err_free_ret:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004719 kfree(host_set);
4720 VPRINTK("EXIT, returning 0\n");
4721 return 0;
4722}
4723
4724/**
Alan Cox17b14452005-09-15 15:44:00 +01004725 * ata_host_set_remove - PCI layer callback for device removal
4726 * @host_set: ATA host set that was removed
4727 *
4728 * Unregister all objects associated with this host set. Free those
4729 * objects.
4730 *
4731 * LOCKING:
4732 * Inherited from calling layer (may sleep).
4733 */
4734
Alan Cox17b14452005-09-15 15:44:00 +01004735void ata_host_set_remove(struct ata_host_set *host_set)
4736{
4737 struct ata_port *ap;
4738 unsigned int i;
4739
4740 for (i = 0; i < host_set->n_ports; i++) {
4741 ap = host_set->ports[i];
4742 scsi_remove_host(ap->host);
4743 }
4744
4745 free_irq(host_set->irq, host_set);
4746
4747 for (i = 0; i < host_set->n_ports; i++) {
4748 ap = host_set->ports[i];
4749
4750 ata_scsi_release(ap->host);
4751
4752 if ((ap->flags & ATA_FLAG_NO_LEGACY) == 0) {
4753 struct ata_ioports *ioaddr = &ap->ioaddr;
4754
4755 if (ioaddr->cmd_addr == 0x1f0)
4756 release_region(0x1f0, 8);
4757 else if (ioaddr->cmd_addr == 0x170)
4758 release_region(0x170, 8);
4759 }
4760
4761 scsi_host_put(ap->host);
4762 }
4763
4764 if (host_set->ops->host_stop)
4765 host_set->ops->host_stop(host_set);
4766
4767 kfree(host_set);
4768}
4769
4770/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004771 * ata_scsi_release - SCSI layer callback hook for host unload
4772 * @host: libata host to be unloaded
4773 *
4774 * Performs all duties necessary to shut down a libata port...
4775 * Kill port kthread, disable port, and release resources.
4776 *
4777 * LOCKING:
4778 * Inherited from SCSI layer.
4779 *
4780 * RETURNS:
4781 * One.
4782 */
4783
4784int ata_scsi_release(struct Scsi_Host *host)
4785{
4786 struct ata_port *ap = (struct ata_port *) &host->hostdata[0];
4787
4788 DPRINTK("ENTER\n");
4789
4790 ap->ops->port_disable(ap);
4791 ata_host_remove(ap, 0);
4792
4793 DPRINTK("EXIT\n");
4794 return 1;
4795}
4796
4797/**
4798 * ata_std_ports - initialize ioaddr with standard port offsets.
4799 * @ioaddr: IO address structure to be initialized
Edward Falk0baab862005-06-02 18:17:13 -04004800 *
4801 * Utility function which initializes data_addr, error_addr,
4802 * feature_addr, nsect_addr, lbal_addr, lbam_addr, lbah_addr,
4803 * device_addr, status_addr, and command_addr to standard offsets
4804 * relative to cmd_addr.
4805 *
4806 * Does not set ctl_addr, altstatus_addr, bmdma_addr, or scr_addr.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004807 */
Edward Falk0baab862005-06-02 18:17:13 -04004808
Linus Torvalds1da177e2005-04-16 15:20:36 -07004809void ata_std_ports(struct ata_ioports *ioaddr)
4810{
4811 ioaddr->data_addr = ioaddr->cmd_addr + ATA_REG_DATA;
4812 ioaddr->error_addr = ioaddr->cmd_addr + ATA_REG_ERR;
4813 ioaddr->feature_addr = ioaddr->cmd_addr + ATA_REG_FEATURE;
4814 ioaddr->nsect_addr = ioaddr->cmd_addr + ATA_REG_NSECT;
4815 ioaddr->lbal_addr = ioaddr->cmd_addr + ATA_REG_LBAL;
4816 ioaddr->lbam_addr = ioaddr->cmd_addr + ATA_REG_LBAM;
4817 ioaddr->lbah_addr = ioaddr->cmd_addr + ATA_REG_LBAH;
4818 ioaddr->device_addr = ioaddr->cmd_addr + ATA_REG_DEVICE;
4819 ioaddr->status_addr = ioaddr->cmd_addr + ATA_REG_STATUS;
4820 ioaddr->command_addr = ioaddr->cmd_addr + ATA_REG_CMD;
4821}
4822
4823static struct ata_probe_ent *
Jeff Garzik057ace52005-10-22 14:27:05 -04004824ata_probe_ent_alloc(struct device *dev, const struct ata_port_info *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004825{
4826 struct ata_probe_ent *probe_ent;
4827
Randy Dunlap57f3bda2005-10-28 20:37:23 -07004828 probe_ent = kzalloc(sizeof(*probe_ent), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004829 if (!probe_ent) {
4830 printk(KERN_ERR DRV_NAME "(%s): out of memory\n",
4831 kobject_name(&(dev->kobj)));
4832 return NULL;
4833 }
4834
Linus Torvalds1da177e2005-04-16 15:20:36 -07004835 INIT_LIST_HEAD(&probe_ent->node);
4836 probe_ent->dev = dev;
4837
4838 probe_ent->sht = port->sht;
4839 probe_ent->host_flags = port->host_flags;
4840 probe_ent->pio_mask = port->pio_mask;
4841 probe_ent->mwdma_mask = port->mwdma_mask;
4842 probe_ent->udma_mask = port->udma_mask;
4843 probe_ent->port_ops = port->port_ops;
4844
4845 return probe_ent;
4846}
4847
Edward Falk0baab862005-06-02 18:17:13 -04004848
4849
Jeff Garzik374b1872005-08-30 05:42:52 -04004850#ifdef CONFIG_PCI
4851
4852void ata_pci_host_stop (struct ata_host_set *host_set)
4853{
4854 struct pci_dev *pdev = to_pci_dev(host_set->dev);
4855
4856 pci_iounmap(pdev, host_set->mmio_base);
4857}
4858
Edward Falk0baab862005-06-02 18:17:13 -04004859/**
4860 * ata_pci_init_native_mode - Initialize native-mode driver
4861 * @pdev: pci device to be initialized
4862 * @port: array[2] of pointers to port info structures.
Alan Cox47a86592005-10-04 08:09:19 -04004863 * @ports: bitmap of ports present
Edward Falk0baab862005-06-02 18:17:13 -04004864 *
4865 * Utility function which allocates and initializes an
4866 * ata_probe_ent structure for a standard dual-port
4867 * PIO-based IDE controller. The returned ata_probe_ent
4868 * structure can be passed to ata_device_add(). The returned
4869 * ata_probe_ent structure should then be freed with kfree().
Alan Cox47a86592005-10-04 08:09:19 -04004870 *
4871 * The caller need only pass the address of the primary port, the
4872 * secondary will be deduced automatically. If the device has non
4873 * standard secondary port mappings this function can be called twice,
4874 * once for each interface.
Edward Falk0baab862005-06-02 18:17:13 -04004875 */
4876
Linus Torvalds1da177e2005-04-16 15:20:36 -07004877struct ata_probe_ent *
Alan Cox47a86592005-10-04 08:09:19 -04004878ata_pci_init_native_mode(struct pci_dev *pdev, struct ata_port_info **port, int ports)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004879{
4880 struct ata_probe_ent *probe_ent =
4881 ata_probe_ent_alloc(pci_dev_to_dev(pdev), port[0]);
Alan Cox47a86592005-10-04 08:09:19 -04004882 int p = 0;
4883
Linus Torvalds1da177e2005-04-16 15:20:36 -07004884 if (!probe_ent)
4885 return NULL;
4886
Linus Torvalds1da177e2005-04-16 15:20:36 -07004887 probe_ent->irq = pdev->irq;
4888 probe_ent->irq_flags = SA_SHIRQ;
Alan Coxe99f8b52005-11-08 14:09:44 +00004889 probe_ent->private_data = port[0]->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004890
Alan Cox47a86592005-10-04 08:09:19 -04004891 if (ports & ATA_PORT_PRIMARY) {
4892 probe_ent->port[p].cmd_addr = pci_resource_start(pdev, 0);
4893 probe_ent->port[p].altstatus_addr =
4894 probe_ent->port[p].ctl_addr =
4895 pci_resource_start(pdev, 1) | ATA_PCI_CTL_OFS;
4896 probe_ent->port[p].bmdma_addr = pci_resource_start(pdev, 4);
4897 ata_std_ports(&probe_ent->port[p]);
4898 p++;
4899 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004900
Alan Cox47a86592005-10-04 08:09:19 -04004901 if (ports & ATA_PORT_SECONDARY) {
4902 probe_ent->port[p].cmd_addr = pci_resource_start(pdev, 2);
4903 probe_ent->port[p].altstatus_addr =
4904 probe_ent->port[p].ctl_addr =
4905 pci_resource_start(pdev, 3) | ATA_PCI_CTL_OFS;
4906 probe_ent->port[p].bmdma_addr = pci_resource_start(pdev, 4) + 8;
4907 ata_std_ports(&probe_ent->port[p]);
4908 p++;
4909 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004910
Alan Cox47a86592005-10-04 08:09:19 -04004911 probe_ent->n_ports = p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004912 return probe_ent;
4913}
4914
Jeff Garzik0f0d5192005-10-30 06:41:29 -05004915static struct ata_probe_ent *ata_pci_init_legacy_port(struct pci_dev *pdev, struct ata_port_info *port, int port_num)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004916{
Alan Cox47a86592005-10-04 08:09:19 -04004917 struct ata_probe_ent *probe_ent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004918
Jeff Garzik0f0d5192005-10-30 06:41:29 -05004919 probe_ent = ata_probe_ent_alloc(pci_dev_to_dev(pdev), port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004920 if (!probe_ent)
4921 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004922
Linus Torvalds1da177e2005-04-16 15:20:36 -07004923 probe_ent->legacy_mode = 1;
Alan Cox47a86592005-10-04 08:09:19 -04004924 probe_ent->n_ports = 1;
4925 probe_ent->hard_port_no = port_num;
Alan Coxe99f8b52005-11-08 14:09:44 +00004926 probe_ent->private_data = port->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004927
Alan Cox47a86592005-10-04 08:09:19 -04004928 switch(port_num)
4929 {
4930 case 0:
4931 probe_ent->irq = 14;
4932 probe_ent->port[0].cmd_addr = 0x1f0;
4933 probe_ent->port[0].altstatus_addr =
4934 probe_ent->port[0].ctl_addr = 0x3f6;
4935 break;
4936 case 1:
4937 probe_ent->irq = 15;
4938 probe_ent->port[0].cmd_addr = 0x170;
4939 probe_ent->port[0].altstatus_addr =
4940 probe_ent->port[0].ctl_addr = 0x376;
4941 break;
4942 }
4943 probe_ent->port[0].bmdma_addr = pci_resource_start(pdev, 4) + 8 * port_num;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004944 ata_std_ports(&probe_ent->port[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004945 return probe_ent;
4946}
4947
4948/**
4949 * ata_pci_init_one - Initialize/register PCI IDE host controller
4950 * @pdev: Controller to be initialized
4951 * @port_info: Information from low-level host driver
4952 * @n_ports: Number of ports attached to host controller
4953 *
Edward Falk0baab862005-06-02 18:17:13 -04004954 * This is a helper function which can be called from a driver's
4955 * xxx_init_one() probe function if the hardware uses traditional
4956 * IDE taskfile registers.
4957 *
4958 * This function calls pci_enable_device(), reserves its register
4959 * regions, sets the dma mask, enables bus master mode, and calls
4960 * ata_device_add()
4961 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004962 * LOCKING:
4963 * Inherited from PCI layer (may sleep).
4964 *
4965 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004966 * Zero on success, negative on errno-based value on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004967 */
4968
4969int ata_pci_init_one (struct pci_dev *pdev, struct ata_port_info **port_info,
4970 unsigned int n_ports)
4971{
Alan Cox47a86592005-10-04 08:09:19 -04004972 struct ata_probe_ent *probe_ent = NULL, *probe_ent2 = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004973 struct ata_port_info *port[2];
4974 u8 tmp8, mask;
4975 unsigned int legacy_mode = 0;
4976 int disable_dev_on_err = 1;
4977 int rc;
4978
4979 DPRINTK("ENTER\n");
4980
4981 port[0] = port_info[0];
4982 if (n_ports > 1)
4983 port[1] = port_info[1];
4984 else
4985 port[1] = port[0];
4986
4987 if ((port[0]->host_flags & ATA_FLAG_NO_LEGACY) == 0
4988 && (pdev->class >> 8) == PCI_CLASS_STORAGE_IDE) {
Alan Cox47a86592005-10-04 08:09:19 -04004989 /* TODO: What if one channel is in native mode ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004990 pci_read_config_byte(pdev, PCI_CLASS_PROG, &tmp8);
4991 mask = (1 << 2) | (1 << 0);
4992 if ((tmp8 & mask) != mask)
4993 legacy_mode = (1 << 3);
4994 }
4995
4996 /* FIXME... */
Alan Cox47a86592005-10-04 08:09:19 -04004997 if ((!legacy_mode) && (n_ports > 2)) {
4998 printk(KERN_ERR "ata: BUG: native mode, n_ports > 2\n");
4999 n_ports = 2;
5000 /* For now */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005001 }
5002
Alan Cox47a86592005-10-04 08:09:19 -04005003 /* FIXME: Really for ATA it isn't safe because the device may be
5004 multi-purpose and we want to leave it alone if it was already
5005 enabled. Secondly for shared use as Arjan says we want refcounting
5006
5007 Checking dev->is_enabled is insufficient as this is not set at
5008 boot for the primary video which is BIOS enabled
5009 */
5010
Linus Torvalds1da177e2005-04-16 15:20:36 -07005011 rc = pci_enable_device(pdev);
5012 if (rc)
5013 return rc;
5014
5015 rc = pci_request_regions(pdev, DRV_NAME);
5016 if (rc) {
5017 disable_dev_on_err = 0;
5018 goto err_out;
5019 }
5020
Alan Cox47a86592005-10-04 08:09:19 -04005021 /* FIXME: Should use platform specific mappers for legacy port ranges */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005022 if (legacy_mode) {
5023 if (!request_region(0x1f0, 8, "libata")) {
5024 struct resource *conflict, res;
5025 res.start = 0x1f0;
5026 res.end = 0x1f0 + 8 - 1;
5027 conflict = ____request_resource(&ioport_resource, &res);
5028 if (!strcmp(conflict->name, "libata"))
5029 legacy_mode |= (1 << 0);
5030 else {
5031 disable_dev_on_err = 0;
5032 printk(KERN_WARNING "ata: 0x1f0 IDE port busy\n");
5033 }
5034 } else
5035 legacy_mode |= (1 << 0);
5036
5037 if (!request_region(0x170, 8, "libata")) {
5038 struct resource *conflict, res;
5039 res.start = 0x170;
5040 res.end = 0x170 + 8 - 1;
5041 conflict = ____request_resource(&ioport_resource, &res);
5042 if (!strcmp(conflict->name, "libata"))
5043 legacy_mode |= (1 << 1);
5044 else {
5045 disable_dev_on_err = 0;
5046 printk(KERN_WARNING "ata: 0x170 IDE port busy\n");
5047 }
5048 } else
5049 legacy_mode |= (1 << 1);
5050 }
5051
5052 /* we have legacy mode, but all ports are unavailable */
5053 if (legacy_mode == (1 << 3)) {
5054 rc = -EBUSY;
5055 goto err_out_regions;
5056 }
5057
5058 rc = pci_set_dma_mask(pdev, ATA_DMA_MASK);
5059 if (rc)
5060 goto err_out_regions;
5061 rc = pci_set_consistent_dma_mask(pdev, ATA_DMA_MASK);
5062 if (rc)
5063 goto err_out_regions;
5064
5065 if (legacy_mode) {
Alan Cox47a86592005-10-04 08:09:19 -04005066 if (legacy_mode & (1 << 0))
Jeff Garzik0f0d5192005-10-30 06:41:29 -05005067 probe_ent = ata_pci_init_legacy_port(pdev, port[0], 0);
Alan Cox47a86592005-10-04 08:09:19 -04005068 if (legacy_mode & (1 << 1))
Jeff Garzik0f0d5192005-10-30 06:41:29 -05005069 probe_ent2 = ata_pci_init_legacy_port(pdev, port[1], 1);
Alan Cox47a86592005-10-04 08:09:19 -04005070 } else {
5071 if (n_ports == 2)
5072 probe_ent = ata_pci_init_native_mode(pdev, port, ATA_PORT_PRIMARY | ATA_PORT_SECONDARY);
5073 else
5074 probe_ent = ata_pci_init_native_mode(pdev, port, ATA_PORT_PRIMARY);
5075 }
5076 if (!probe_ent && !probe_ent2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005077 rc = -ENOMEM;
5078 goto err_out_regions;
5079 }
5080
5081 pci_set_master(pdev);
5082
5083 /* FIXME: check ata_device_add return */
5084 if (legacy_mode) {
5085 if (legacy_mode & (1 << 0))
5086 ata_device_add(probe_ent);
5087 if (legacy_mode & (1 << 1))
5088 ata_device_add(probe_ent2);
5089 } else
5090 ata_device_add(probe_ent);
5091
5092 kfree(probe_ent);
5093 kfree(probe_ent2);
5094
5095 return 0;
5096
5097err_out_regions:
5098 if (legacy_mode & (1 << 0))
5099 release_region(0x1f0, 8);
5100 if (legacy_mode & (1 << 1))
5101 release_region(0x170, 8);
5102 pci_release_regions(pdev);
5103err_out:
5104 if (disable_dev_on_err)
5105 pci_disable_device(pdev);
5106 return rc;
5107}
5108
5109/**
5110 * ata_pci_remove_one - PCI layer callback for device removal
5111 * @pdev: PCI device that was removed
5112 *
5113 * PCI layer indicates to libata via this hook that
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04005114 * hot-unplug or module unload event has occurred.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005115 * Handle this by unregistering all objects associated
5116 * with this PCI device. Free those objects. Then finally
5117 * release PCI resources and disable device.
5118 *
5119 * LOCKING:
5120 * Inherited from PCI layer (may sleep).
5121 */
5122
5123void ata_pci_remove_one (struct pci_dev *pdev)
5124{
5125 struct device *dev = pci_dev_to_dev(pdev);
5126 struct ata_host_set *host_set = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005127
Alan Cox17b14452005-09-15 15:44:00 +01005128 ata_host_set_remove(host_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005129 pci_release_regions(pdev);
5130 pci_disable_device(pdev);
5131 dev_set_drvdata(dev, NULL);
5132}
5133
5134/* move to PCI subsystem */
Jeff Garzik057ace52005-10-22 14:27:05 -04005135int pci_test_config_bits(struct pci_dev *pdev, const struct pci_bits *bits)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005136{
5137 unsigned long tmp = 0;
5138
5139 switch (bits->width) {
5140 case 1: {
5141 u8 tmp8 = 0;
5142 pci_read_config_byte(pdev, bits->reg, &tmp8);
5143 tmp = tmp8;
5144 break;
5145 }
5146 case 2: {
5147 u16 tmp16 = 0;
5148 pci_read_config_word(pdev, bits->reg, &tmp16);
5149 tmp = tmp16;
5150 break;
5151 }
5152 case 4: {
5153 u32 tmp32 = 0;
5154 pci_read_config_dword(pdev, bits->reg, &tmp32);
5155 tmp = tmp32;
5156 break;
5157 }
5158
5159 default:
5160 return -EINVAL;
5161 }
5162
5163 tmp &= bits->mask;
5164
5165 return (tmp == bits->val) ? 1 : 0;
5166}
5167#endif /* CONFIG_PCI */
5168
5169
Linus Torvalds1da177e2005-04-16 15:20:36 -07005170static int __init ata_init(void)
5171{
5172 ata_wq = create_workqueue("ata");
5173 if (!ata_wq)
5174 return -ENOMEM;
5175
5176 printk(KERN_DEBUG "libata version " DRV_VERSION " loaded.\n");
5177 return 0;
5178}
5179
5180static void __exit ata_exit(void)
5181{
5182 destroy_workqueue(ata_wq);
5183}
5184
5185module_init(ata_init);
5186module_exit(ata_exit);
5187
Jeff Garzik67846b32005-10-05 02:58:32 -04005188static unsigned long ratelimit_time;
5189static spinlock_t ata_ratelimit_lock = SPIN_LOCK_UNLOCKED;
5190
5191int ata_ratelimit(void)
5192{
5193 int rc;
5194 unsigned long flags;
5195
5196 spin_lock_irqsave(&ata_ratelimit_lock, flags);
5197
5198 if (time_after(jiffies, ratelimit_time)) {
5199 rc = 1;
5200 ratelimit_time = jiffies + (HZ/5);
5201 } else
5202 rc = 0;
5203
5204 spin_unlock_irqrestore(&ata_ratelimit_lock, flags);
5205
5206 return rc;
5207}
5208
Linus Torvalds1da177e2005-04-16 15:20:36 -07005209/*
5210 * libata is essentially a library of internal helper functions for
5211 * low-level ATA host controller drivers. As such, the API/ABI is
5212 * likely to change as new drivers are added and updated.
5213 * Do not depend on ABI/API stability.
5214 */
5215
5216EXPORT_SYMBOL_GPL(ata_std_bios_param);
5217EXPORT_SYMBOL_GPL(ata_std_ports);
5218EXPORT_SYMBOL_GPL(ata_device_add);
Alan Cox17b14452005-09-15 15:44:00 +01005219EXPORT_SYMBOL_GPL(ata_host_set_remove);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005220EXPORT_SYMBOL_GPL(ata_sg_init);
5221EXPORT_SYMBOL_GPL(ata_sg_init_one);
5222EXPORT_SYMBOL_GPL(ata_qc_complete);
5223EXPORT_SYMBOL_GPL(ata_qc_issue_prot);
5224EXPORT_SYMBOL_GPL(ata_eng_timeout);
5225EXPORT_SYMBOL_GPL(ata_tf_load);
5226EXPORT_SYMBOL_GPL(ata_tf_read);
5227EXPORT_SYMBOL_GPL(ata_noop_dev_select);
5228EXPORT_SYMBOL_GPL(ata_std_dev_select);
5229EXPORT_SYMBOL_GPL(ata_tf_to_fis);
5230EXPORT_SYMBOL_GPL(ata_tf_from_fis);
5231EXPORT_SYMBOL_GPL(ata_check_status);
5232EXPORT_SYMBOL_GPL(ata_altstatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005233EXPORT_SYMBOL_GPL(ata_exec_command);
5234EXPORT_SYMBOL_GPL(ata_port_start);
5235EXPORT_SYMBOL_GPL(ata_port_stop);
Jeff Garzikaa8f0dc2005-05-26 21:54:27 -04005236EXPORT_SYMBOL_GPL(ata_host_stop);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005237EXPORT_SYMBOL_GPL(ata_interrupt);
5238EXPORT_SYMBOL_GPL(ata_qc_prep);
5239EXPORT_SYMBOL_GPL(ata_bmdma_setup);
5240EXPORT_SYMBOL_GPL(ata_bmdma_start);
5241EXPORT_SYMBOL_GPL(ata_bmdma_irq_clear);
5242EXPORT_SYMBOL_GPL(ata_bmdma_status);
5243EXPORT_SYMBOL_GPL(ata_bmdma_stop);
5244EXPORT_SYMBOL_GPL(ata_port_probe);
5245EXPORT_SYMBOL_GPL(sata_phy_reset);
5246EXPORT_SYMBOL_GPL(__sata_phy_reset);
5247EXPORT_SYMBOL_GPL(ata_bus_reset);
5248EXPORT_SYMBOL_GPL(ata_port_disable);
Jeff Garzik67846b32005-10-05 02:58:32 -04005249EXPORT_SYMBOL_GPL(ata_ratelimit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005250EXPORT_SYMBOL_GPL(ata_scsi_ioctl);
5251EXPORT_SYMBOL_GPL(ata_scsi_queuecmd);
5252EXPORT_SYMBOL_GPL(ata_scsi_error);
5253EXPORT_SYMBOL_GPL(ata_scsi_slave_config);
5254EXPORT_SYMBOL_GPL(ata_scsi_release);
5255EXPORT_SYMBOL_GPL(ata_host_intr);
5256EXPORT_SYMBOL_GPL(ata_dev_classify);
5257EXPORT_SYMBOL_GPL(ata_dev_id_string);
Brad Campbell6f2f3812005-05-12 15:07:47 -04005258EXPORT_SYMBOL_GPL(ata_dev_config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005259EXPORT_SYMBOL_GPL(ata_scsi_simulate);
5260
Alan Cox452503f2005-10-21 19:01:32 -04005261EXPORT_SYMBOL_GPL(ata_timing_compute);
5262EXPORT_SYMBOL_GPL(ata_timing_merge);
5263
Linus Torvalds1da177e2005-04-16 15:20:36 -07005264#ifdef CONFIG_PCI
5265EXPORT_SYMBOL_GPL(pci_test_config_bits);
Jeff Garzik374b1872005-08-30 05:42:52 -04005266EXPORT_SYMBOL_GPL(ata_pci_host_stop);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005267EXPORT_SYMBOL_GPL(ata_pci_init_native_mode);
5268EXPORT_SYMBOL_GPL(ata_pci_init_one);
5269EXPORT_SYMBOL_GPL(ata_pci_remove_one);
5270#endif /* CONFIG_PCI */