blob: 93ae2dcc837c183c41f367ad3c1c9d8688a169d6 [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 Lee8bf62ece2005-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 -070076
77static unsigned int ata_unique_id = 1;
78static struct workqueue_struct *ata_wq;
79
Jeff Garzik1623c812005-08-30 03:37:42 -040080int atapi_enabled = 0;
81module_param(atapi_enabled, int, 0444);
82MODULE_PARM_DESC(atapi_enabled, "Enable discovery of ATAPI devices (0=off, 1=on)");
83
Linus Torvalds1da177e2005-04-16 15:20:36 -070084MODULE_AUTHOR("Jeff Garzik");
85MODULE_DESCRIPTION("Library module for ATA devices");
86MODULE_LICENSE("GPL");
87MODULE_VERSION(DRV_VERSION);
88
89/**
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -040090 * ata_tf_load_pio - send taskfile registers to host controller
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 * @ap: Port to which output is sent
92 * @tf: ATA taskfile register set
93 *
94 * Outputs ATA taskfile to standard ATA host controller.
95 *
96 * LOCKING:
97 * Inherited from caller.
98 */
99
Jeff Garzik057ace52005-10-22 14:27:05 -0400100static void ata_tf_load_pio(struct ata_port *ap, const struct ata_taskfile *tf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101{
102 struct ata_ioports *ioaddr = &ap->ioaddr;
103 unsigned int is_addr = tf->flags & ATA_TFLAG_ISADDR;
104
105 if (tf->ctl != ap->last_ctl) {
106 outb(tf->ctl, ioaddr->ctl_addr);
107 ap->last_ctl = tf->ctl;
108 ata_wait_idle(ap);
109 }
110
111 if (is_addr && (tf->flags & ATA_TFLAG_LBA48)) {
112 outb(tf->hob_feature, ioaddr->feature_addr);
113 outb(tf->hob_nsect, ioaddr->nsect_addr);
114 outb(tf->hob_lbal, ioaddr->lbal_addr);
115 outb(tf->hob_lbam, ioaddr->lbam_addr);
116 outb(tf->hob_lbah, ioaddr->lbah_addr);
117 VPRINTK("hob: feat 0x%X nsect 0x%X, lba 0x%X 0x%X 0x%X\n",
118 tf->hob_feature,
119 tf->hob_nsect,
120 tf->hob_lbal,
121 tf->hob_lbam,
122 tf->hob_lbah);
123 }
124
125 if (is_addr) {
126 outb(tf->feature, ioaddr->feature_addr);
127 outb(tf->nsect, ioaddr->nsect_addr);
128 outb(tf->lbal, ioaddr->lbal_addr);
129 outb(tf->lbam, ioaddr->lbam_addr);
130 outb(tf->lbah, ioaddr->lbah_addr);
131 VPRINTK("feat 0x%X nsect 0x%X lba 0x%X 0x%X 0x%X\n",
132 tf->feature,
133 tf->nsect,
134 tf->lbal,
135 tf->lbam,
136 tf->lbah);
137 }
138
139 if (tf->flags & ATA_TFLAG_DEVICE) {
140 outb(tf->device, ioaddr->device_addr);
141 VPRINTK("device 0x%X\n", tf->device);
142 }
143
144 ata_wait_idle(ap);
145}
146
147/**
148 * ata_tf_load_mmio - send taskfile registers to host controller
149 * @ap: Port to which output is sent
150 * @tf: ATA taskfile register set
151 *
152 * Outputs ATA taskfile to standard ATA host controller using MMIO.
153 *
154 * LOCKING:
155 * Inherited from caller.
156 */
157
Jeff Garzik057ace52005-10-22 14:27:05 -0400158static void ata_tf_load_mmio(struct ata_port *ap, const struct ata_taskfile *tf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159{
160 struct ata_ioports *ioaddr = &ap->ioaddr;
161 unsigned int is_addr = tf->flags & ATA_TFLAG_ISADDR;
162
163 if (tf->ctl != ap->last_ctl) {
164 writeb(tf->ctl, (void __iomem *) ap->ioaddr.ctl_addr);
165 ap->last_ctl = tf->ctl;
166 ata_wait_idle(ap);
167 }
168
169 if (is_addr && (tf->flags & ATA_TFLAG_LBA48)) {
170 writeb(tf->hob_feature, (void __iomem *) ioaddr->feature_addr);
171 writeb(tf->hob_nsect, (void __iomem *) ioaddr->nsect_addr);
172 writeb(tf->hob_lbal, (void __iomem *) ioaddr->lbal_addr);
173 writeb(tf->hob_lbam, (void __iomem *) ioaddr->lbam_addr);
174 writeb(tf->hob_lbah, (void __iomem *) ioaddr->lbah_addr);
175 VPRINTK("hob: feat 0x%X nsect 0x%X, lba 0x%X 0x%X 0x%X\n",
176 tf->hob_feature,
177 tf->hob_nsect,
178 tf->hob_lbal,
179 tf->hob_lbam,
180 tf->hob_lbah);
181 }
182
183 if (is_addr) {
184 writeb(tf->feature, (void __iomem *) ioaddr->feature_addr);
185 writeb(tf->nsect, (void __iomem *) ioaddr->nsect_addr);
186 writeb(tf->lbal, (void __iomem *) ioaddr->lbal_addr);
187 writeb(tf->lbam, (void __iomem *) ioaddr->lbam_addr);
188 writeb(tf->lbah, (void __iomem *) ioaddr->lbah_addr);
189 VPRINTK("feat 0x%X nsect 0x%X lba 0x%X 0x%X 0x%X\n",
190 tf->feature,
191 tf->nsect,
192 tf->lbal,
193 tf->lbam,
194 tf->lbah);
195 }
196
197 if (tf->flags & ATA_TFLAG_DEVICE) {
198 writeb(tf->device, (void __iomem *) ioaddr->device_addr);
199 VPRINTK("device 0x%X\n", tf->device);
200 }
201
202 ata_wait_idle(ap);
203}
204
Edward Falk0baab862005-06-02 18:17:13 -0400205
206/**
207 * ata_tf_load - send taskfile registers to host controller
208 * @ap: Port to which output is sent
209 * @tf: ATA taskfile register set
210 *
211 * Outputs ATA taskfile to standard ATA host controller using MMIO
212 * or PIO as indicated by the ATA_FLAG_MMIO flag.
213 * Writes the control, feature, nsect, lbal, lbam, and lbah registers.
214 * Optionally (ATA_TFLAG_LBA48) writes hob_feature, hob_nsect,
215 * hob_lbal, hob_lbam, and hob_lbah.
216 *
217 * This function waits for idle (!BUSY and !DRQ) after writing
218 * registers. If the control register has a new value, this
219 * function also waits for idle after writing control and before
220 * writing the remaining registers.
221 *
222 * May be used as the tf_load() entry in ata_port_operations.
223 *
224 * LOCKING:
225 * Inherited from caller.
226 */
Jeff Garzik057ace52005-10-22 14:27:05 -0400227void ata_tf_load(struct ata_port *ap, const struct ata_taskfile *tf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228{
229 if (ap->flags & ATA_FLAG_MMIO)
230 ata_tf_load_mmio(ap, tf);
231 else
232 ata_tf_load_pio(ap, tf);
233}
234
235/**
Edward Falk0baab862005-06-02 18:17:13 -0400236 * ata_exec_command_pio - issue ATA command to host controller
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 * @ap: port to which command is being issued
238 * @tf: ATA taskfile register set
239 *
Edward Falk0baab862005-06-02 18:17:13 -0400240 * Issues PIO write to ATA command register, with proper
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 * synchronization with interrupt handler / other threads.
242 *
243 * LOCKING:
244 * spin_lock_irqsave(host_set lock)
245 */
246
Jeff Garzik057ace52005-10-22 14:27:05 -0400247static void ata_exec_command_pio(struct ata_port *ap, const struct ata_taskfile *tf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248{
249 DPRINTK("ata%u: cmd 0x%X\n", ap->id, tf->command);
250
251 outb(tf->command, ap->ioaddr.command_addr);
252 ata_pause(ap);
253}
254
255
256/**
257 * ata_exec_command_mmio - issue ATA command to host controller
258 * @ap: port to which command is being issued
259 * @tf: ATA taskfile register set
260 *
261 * Issues MMIO write to ATA command register, with proper
262 * synchronization with interrupt handler / other threads.
263 *
264 * LOCKING:
265 * spin_lock_irqsave(host_set lock)
266 */
267
Jeff Garzik057ace52005-10-22 14:27:05 -0400268static void ata_exec_command_mmio(struct ata_port *ap, const struct ata_taskfile *tf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269{
270 DPRINTK("ata%u: cmd 0x%X\n", ap->id, tf->command);
271
272 writeb(tf->command, (void __iomem *) ap->ioaddr.command_addr);
273 ata_pause(ap);
274}
275
Edward Falk0baab862005-06-02 18:17:13 -0400276
277/**
278 * ata_exec_command - issue ATA command to host controller
279 * @ap: port to which command is being issued
280 * @tf: ATA taskfile register set
281 *
282 * Issues PIO/MMIO write to ATA command register, with proper
283 * synchronization with interrupt handler / other threads.
284 *
285 * LOCKING:
286 * spin_lock_irqsave(host_set lock)
287 */
Jeff Garzik057ace52005-10-22 14:27:05 -0400288void ata_exec_command(struct ata_port *ap, const struct ata_taskfile *tf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289{
290 if (ap->flags & ATA_FLAG_MMIO)
291 ata_exec_command_mmio(ap, tf);
292 else
293 ata_exec_command_pio(ap, tf);
294}
295
296/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 * ata_tf_to_host - issue ATA taskfile to host controller
298 * @ap: port to which command is being issued
299 * @tf: ATA taskfile register set
300 *
301 * Issues ATA taskfile register set to ATA host controller,
302 * with proper synchronization with interrupt handler and
303 * other threads.
304 *
305 * LOCKING:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 * spin_lock_irqsave(host_set lock)
307 */
308
Jeff Garzike5338252005-10-30 21:37:17 -0500309static inline void ata_tf_to_host(struct ata_port *ap,
310 const struct ata_taskfile *tf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311{
312 ap->ops->tf_load(ap, tf);
313 ap->ops->exec_command(ap, tf);
314}
315
316/**
Edward Falk0baab862005-06-02 18:17:13 -0400317 * ata_tf_read_pio - input device's ATA taskfile shadow registers
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 * @ap: Port from which input is read
319 * @tf: ATA taskfile register set for storing input
320 *
321 * Reads ATA taskfile registers for currently-selected device
322 * into @tf.
323 *
324 * LOCKING:
325 * Inherited from caller.
326 */
327
328static void ata_tf_read_pio(struct ata_port *ap, struct ata_taskfile *tf)
329{
330 struct ata_ioports *ioaddr = &ap->ioaddr;
331
Jeff Garzikac19bff2005-10-29 13:58:21 -0400332 tf->command = ata_check_status(ap);
Jeff Garzik0169e282005-10-29 21:25:10 -0400333 tf->feature = inb(ioaddr->error_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 tf->nsect = inb(ioaddr->nsect_addr);
335 tf->lbal = inb(ioaddr->lbal_addr);
336 tf->lbam = inb(ioaddr->lbam_addr);
337 tf->lbah = inb(ioaddr->lbah_addr);
338 tf->device = inb(ioaddr->device_addr);
339
340 if (tf->flags & ATA_TFLAG_LBA48) {
341 outb(tf->ctl | ATA_HOB, ioaddr->ctl_addr);
342 tf->hob_feature = inb(ioaddr->error_addr);
343 tf->hob_nsect = inb(ioaddr->nsect_addr);
344 tf->hob_lbal = inb(ioaddr->lbal_addr);
345 tf->hob_lbam = inb(ioaddr->lbam_addr);
346 tf->hob_lbah = inb(ioaddr->lbah_addr);
347 }
348}
349
350/**
351 * ata_tf_read_mmio - input device's ATA taskfile shadow registers
352 * @ap: Port from which input is read
353 * @tf: ATA taskfile register set for storing input
354 *
355 * Reads ATA taskfile registers for currently-selected device
356 * into @tf via MMIO.
357 *
358 * LOCKING:
359 * Inherited from caller.
360 */
361
362static void ata_tf_read_mmio(struct ata_port *ap, struct ata_taskfile *tf)
363{
364 struct ata_ioports *ioaddr = &ap->ioaddr;
365
Jeff Garzikac19bff2005-10-29 13:58:21 -0400366 tf->command = ata_check_status(ap);
Jeff Garzik0169e282005-10-29 21:25:10 -0400367 tf->feature = readb((void __iomem *)ioaddr->error_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 tf->nsect = readb((void __iomem *)ioaddr->nsect_addr);
369 tf->lbal = readb((void __iomem *)ioaddr->lbal_addr);
370 tf->lbam = readb((void __iomem *)ioaddr->lbam_addr);
371 tf->lbah = readb((void __iomem *)ioaddr->lbah_addr);
372 tf->device = readb((void __iomem *)ioaddr->device_addr);
373
374 if (tf->flags & ATA_TFLAG_LBA48) {
375 writeb(tf->ctl | ATA_HOB, (void __iomem *) ap->ioaddr.ctl_addr);
376 tf->hob_feature = readb((void __iomem *)ioaddr->error_addr);
377 tf->hob_nsect = readb((void __iomem *)ioaddr->nsect_addr);
378 tf->hob_lbal = readb((void __iomem *)ioaddr->lbal_addr);
379 tf->hob_lbam = readb((void __iomem *)ioaddr->lbam_addr);
380 tf->hob_lbah = readb((void __iomem *)ioaddr->lbah_addr);
381 }
382}
383
Edward Falk0baab862005-06-02 18:17:13 -0400384
385/**
386 * ata_tf_read - input device's ATA taskfile shadow registers
387 * @ap: Port from which input is read
388 * @tf: ATA taskfile register set for storing input
389 *
390 * Reads ATA taskfile registers for currently-selected device
391 * into @tf.
392 *
393 * Reads nsect, lbal, lbam, lbah, and device. If ATA_TFLAG_LBA48
394 * is set, also reads the hob registers.
395 *
396 * May be used as the tf_read() entry in ata_port_operations.
397 *
398 * LOCKING:
399 * Inherited from caller.
400 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401void ata_tf_read(struct ata_port *ap, struct ata_taskfile *tf)
402{
403 if (ap->flags & ATA_FLAG_MMIO)
404 ata_tf_read_mmio(ap, tf);
405 else
406 ata_tf_read_pio(ap, tf);
407}
408
409/**
410 * ata_check_status_pio - Read device status reg & clear interrupt
411 * @ap: port where the device is
412 *
413 * Reads ATA taskfile status register for currently-selected device
Edward Falk0baab862005-06-02 18:17:13 -0400414 * and return its value. This also clears pending interrupts
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 * from this device
416 *
417 * LOCKING:
418 * Inherited from caller.
419 */
420static u8 ata_check_status_pio(struct ata_port *ap)
421{
422 return inb(ap->ioaddr.status_addr);
423}
424
425/**
426 * ata_check_status_mmio - Read device status reg & clear interrupt
427 * @ap: port where the device is
428 *
429 * Reads ATA taskfile status register for currently-selected device
Edward Falk0baab862005-06-02 18:17:13 -0400430 * via MMIO and return its value. This also clears pending interrupts
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 * from this device
432 *
433 * LOCKING:
434 * Inherited from caller.
435 */
436static u8 ata_check_status_mmio(struct ata_port *ap)
437{
438 return readb((void __iomem *) ap->ioaddr.status_addr);
439}
440
Edward Falk0baab862005-06-02 18:17:13 -0400441
442/**
443 * ata_check_status - Read device status reg & clear interrupt
444 * @ap: port where the device is
445 *
446 * Reads ATA taskfile status register for currently-selected device
447 * and return its value. This also clears pending interrupts
448 * from this device
449 *
450 * May be used as the check_status() entry in ata_port_operations.
451 *
452 * LOCKING:
453 * Inherited from caller.
454 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455u8 ata_check_status(struct ata_port *ap)
456{
457 if (ap->flags & ATA_FLAG_MMIO)
458 return ata_check_status_mmio(ap);
459 return ata_check_status_pio(ap);
460}
461
Edward Falk0baab862005-06-02 18:17:13 -0400462
463/**
464 * ata_altstatus - Read device alternate status reg
465 * @ap: port where the device is
466 *
467 * Reads ATA taskfile alternate status register for
468 * currently-selected device and return its value.
469 *
470 * Note: may NOT be used as the check_altstatus() entry in
471 * ata_port_operations.
472 *
473 * LOCKING:
474 * Inherited from caller.
475 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476u8 ata_altstatus(struct ata_port *ap)
477{
478 if (ap->ops->check_altstatus)
479 return ap->ops->check_altstatus(ap);
480
481 if (ap->flags & ATA_FLAG_MMIO)
482 return readb((void __iomem *)ap->ioaddr.altstatus_addr);
483 return inb(ap->ioaddr.altstatus_addr);
484}
485
Edward Falk0baab862005-06-02 18:17:13 -0400486
487/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 * ata_tf_to_fis - Convert ATA taskfile to SATA FIS structure
489 * @tf: Taskfile to convert
490 * @fis: Buffer into which data will output
491 * @pmp: Port multiplier port
492 *
493 * Converts a standard ATA taskfile to a Serial ATA
494 * FIS structure (Register - Host to Device).
495 *
496 * LOCKING:
497 * Inherited from caller.
498 */
499
Jeff Garzik057ace52005-10-22 14:27:05 -0400500void ata_tf_to_fis(const struct ata_taskfile *tf, u8 *fis, u8 pmp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501{
502 fis[0] = 0x27; /* Register - Host to Device FIS */
503 fis[1] = (pmp & 0xf) | (1 << 7); /* Port multiplier number,
504 bit 7 indicates Command FIS */
505 fis[2] = tf->command;
506 fis[3] = tf->feature;
507
508 fis[4] = tf->lbal;
509 fis[5] = tf->lbam;
510 fis[6] = tf->lbah;
511 fis[7] = tf->device;
512
513 fis[8] = tf->hob_lbal;
514 fis[9] = tf->hob_lbam;
515 fis[10] = tf->hob_lbah;
516 fis[11] = tf->hob_feature;
517
518 fis[12] = tf->nsect;
519 fis[13] = tf->hob_nsect;
520 fis[14] = 0;
521 fis[15] = tf->ctl;
522
523 fis[16] = 0;
524 fis[17] = 0;
525 fis[18] = 0;
526 fis[19] = 0;
527}
528
529/**
530 * ata_tf_from_fis - Convert SATA FIS to ATA taskfile
531 * @fis: Buffer from which data will be input
532 * @tf: Taskfile to output
533 *
Mark Lorde12a1be2005-11-12 18:55:45 -0500534 * Converts a serial ATA FIS structure to a standard ATA taskfile.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 *
536 * LOCKING:
537 * Inherited from caller.
538 */
539
Jeff Garzik057ace52005-10-22 14:27:05 -0400540void ata_tf_from_fis(const u8 *fis, struct ata_taskfile *tf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541{
542 tf->command = fis[2]; /* status */
543 tf->feature = fis[3]; /* error */
544
545 tf->lbal = fis[4];
546 tf->lbam = fis[5];
547 tf->lbah = fis[6];
548 tf->device = fis[7];
549
550 tf->hob_lbal = fis[8];
551 tf->hob_lbam = fis[9];
552 tf->hob_lbah = fis[10];
553
554 tf->nsect = fis[12];
555 tf->hob_nsect = fis[13];
556}
557
Albert Lee8cbd6df2005-10-12 15:06:27 +0800558static const u8 ata_rw_cmds[] = {
559 /* pio multi */
560 ATA_CMD_READ_MULTI,
561 ATA_CMD_WRITE_MULTI,
562 ATA_CMD_READ_MULTI_EXT,
563 ATA_CMD_WRITE_MULTI_EXT,
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100564 0,
565 0,
566 0,
567 ATA_CMD_WRITE_MULTI_FUA_EXT,
Albert Lee8cbd6df2005-10-12 15:06:27 +0800568 /* pio */
569 ATA_CMD_PIO_READ,
570 ATA_CMD_PIO_WRITE,
571 ATA_CMD_PIO_READ_EXT,
572 ATA_CMD_PIO_WRITE_EXT,
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100573 0,
574 0,
575 0,
576 0,
Albert Lee8cbd6df2005-10-12 15:06:27 +0800577 /* dma */
578 ATA_CMD_READ,
579 ATA_CMD_WRITE,
580 ATA_CMD_READ_EXT,
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100581 ATA_CMD_WRITE_EXT,
582 0,
583 0,
584 0,
585 ATA_CMD_WRITE_FUA_EXT
Albert Lee8cbd6df2005-10-12 15:06:27 +0800586};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587
588/**
Albert Lee8cbd6df2005-10-12 15:06:27 +0800589 * ata_rwcmd_protocol - set taskfile r/w commands and protocol
590 * @qc: command to examine and configure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 *
Albert Lee8cbd6df2005-10-12 15:06:27 +0800592 * Examine the device configuration and tf->flags to calculate
593 * the proper read/write commands and protocol to use.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 *
595 * LOCKING:
596 * caller.
597 */
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100598int ata_rwcmd_protocol(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599{
Albert Lee8cbd6df2005-10-12 15:06:27 +0800600 struct ata_taskfile *tf = &qc->tf;
601 struct ata_device *dev = qc->dev;
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100602 u8 cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100604 int index, fua, lba48, write;
Albert Lee8cbd6df2005-10-12 15:06:27 +0800605
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100606 fua = (tf->flags & ATA_TFLAG_FUA) ? 4 : 0;
Albert Lee8cbd6df2005-10-12 15:06:27 +0800607 lba48 = (tf->flags & ATA_TFLAG_LBA48) ? 2 : 0;
608 write = (tf->flags & ATA_TFLAG_WRITE) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609
Albert Lee8cbd6df2005-10-12 15:06:27 +0800610 if (dev->flags & ATA_DFLAG_PIO) {
611 tf->protocol = ATA_PROT_PIO;
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100612 index = dev->multi_count ? 0 : 8;
Alan Cox8d238e02006-01-17 20:50:31 +0000613 } else if (lba48 && (qc->ap->flags & ATA_FLAG_PIO_LBA48)) {
614 /* Unable to use DMA due to host limitation */
615 tf->protocol = ATA_PROT_PIO;
616 index = dev->multi_count ? 0 : 4;
Albert Lee8cbd6df2005-10-12 15:06:27 +0800617 } else {
618 tf->protocol = ATA_PROT_DMA;
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100619 index = 16;
Albert Lee8cbd6df2005-10-12 15:06:27 +0800620 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100622 cmd = ata_rw_cmds[index + fua + lba48 + write];
623 if (cmd) {
624 tf->command = cmd;
625 return 0;
626 }
627 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628}
629
Arjan van de Ven98ac62d2005-11-28 10:06:23 +0100630static const char * const xfer_mode_str[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 "UDMA/16",
632 "UDMA/25",
633 "UDMA/33",
634 "UDMA/44",
635 "UDMA/66",
636 "UDMA/100",
637 "UDMA/133",
638 "UDMA7",
639 "MWDMA0",
640 "MWDMA1",
641 "MWDMA2",
642 "PIO0",
643 "PIO1",
644 "PIO2",
645 "PIO3",
646 "PIO4",
647};
648
649/**
650 * ata_udma_string - convert UDMA bit offset to string
651 * @mask: mask of bits supported; only highest bit counts.
652 *
653 * Determine string which represents the highest speed
654 * (highest bit in @udma_mask).
655 *
656 * LOCKING:
657 * None.
658 *
659 * RETURNS:
660 * Constant C string representing highest speed listed in
661 * @udma_mask, or the constant C string "<n/a>".
662 */
663
664static const char *ata_mode_string(unsigned int mask)
665{
666 int i;
667
668 for (i = 7; i >= 0; i--)
669 if (mask & (1 << i))
670 goto out;
671 for (i = ATA_SHIFT_MWDMA + 2; i >= ATA_SHIFT_MWDMA; i--)
672 if (mask & (1 << i))
673 goto out;
674 for (i = ATA_SHIFT_PIO + 4; i >= ATA_SHIFT_PIO; i--)
675 if (mask & (1 << i))
676 goto out;
677
678 return "<n/a>";
679
680out:
681 return xfer_mode_str[i];
682}
683
684/**
685 * ata_pio_devchk - PATA device presence detection
686 * @ap: ATA channel to examine
687 * @device: Device to examine (starting at zero)
688 *
689 * This technique was originally described in
690 * Hale Landis's ATADRVR (www.ata-atapi.com), and
691 * later found its way into the ATA/ATAPI spec.
692 *
693 * Write a pattern to the ATA shadow registers,
694 * and if a device is present, it will respond by
695 * correctly storing and echoing back the
696 * ATA shadow register contents.
697 *
698 * LOCKING:
699 * caller.
700 */
701
702static unsigned int ata_pio_devchk(struct ata_port *ap,
703 unsigned int device)
704{
705 struct ata_ioports *ioaddr = &ap->ioaddr;
706 u8 nsect, lbal;
707
708 ap->ops->dev_select(ap, device);
709
710 outb(0x55, ioaddr->nsect_addr);
711 outb(0xaa, ioaddr->lbal_addr);
712
713 outb(0xaa, ioaddr->nsect_addr);
714 outb(0x55, ioaddr->lbal_addr);
715
716 outb(0x55, ioaddr->nsect_addr);
717 outb(0xaa, ioaddr->lbal_addr);
718
719 nsect = inb(ioaddr->nsect_addr);
720 lbal = inb(ioaddr->lbal_addr);
721
722 if ((nsect == 0x55) && (lbal == 0xaa))
723 return 1; /* we found a device */
724
725 return 0; /* nothing found */
726}
727
728/**
729 * ata_mmio_devchk - PATA device presence detection
730 * @ap: ATA channel to examine
731 * @device: Device to examine (starting at zero)
732 *
733 * This technique was originally described in
734 * Hale Landis's ATADRVR (www.ata-atapi.com), and
735 * later found its way into the ATA/ATAPI spec.
736 *
737 * Write a pattern to the ATA shadow registers,
738 * and if a device is present, it will respond by
739 * correctly storing and echoing back the
740 * ATA shadow register contents.
741 *
742 * LOCKING:
743 * caller.
744 */
745
746static unsigned int ata_mmio_devchk(struct ata_port *ap,
747 unsigned int device)
748{
749 struct ata_ioports *ioaddr = &ap->ioaddr;
750 u8 nsect, lbal;
751
752 ap->ops->dev_select(ap, device);
753
754 writeb(0x55, (void __iomem *) ioaddr->nsect_addr);
755 writeb(0xaa, (void __iomem *) ioaddr->lbal_addr);
756
757 writeb(0xaa, (void __iomem *) ioaddr->nsect_addr);
758 writeb(0x55, (void __iomem *) ioaddr->lbal_addr);
759
760 writeb(0x55, (void __iomem *) ioaddr->nsect_addr);
761 writeb(0xaa, (void __iomem *) ioaddr->lbal_addr);
762
763 nsect = readb((void __iomem *) ioaddr->nsect_addr);
764 lbal = readb((void __iomem *) ioaddr->lbal_addr);
765
766 if ((nsect == 0x55) && (lbal == 0xaa))
767 return 1; /* we found a device */
768
769 return 0; /* nothing found */
770}
771
772/**
773 * ata_devchk - PATA device presence detection
774 * @ap: ATA channel to examine
775 * @device: Device to examine (starting at zero)
776 *
777 * Dispatch ATA device presence detection, depending
778 * on whether we are using PIO or MMIO to talk to the
779 * ATA shadow registers.
780 *
781 * LOCKING:
782 * caller.
783 */
784
785static unsigned int ata_devchk(struct ata_port *ap,
786 unsigned int device)
787{
788 if (ap->flags & ATA_FLAG_MMIO)
789 return ata_mmio_devchk(ap, device);
790 return ata_pio_devchk(ap, device);
791}
792
793/**
794 * ata_dev_classify - determine device type based on ATA-spec signature
795 * @tf: ATA taskfile register set for device to be identified
796 *
797 * Determine from taskfile register contents whether a device is
798 * ATA or ATAPI, as per "Signature and persistence" section
799 * of ATA/PI spec (volume 1, sect 5.14).
800 *
801 * LOCKING:
802 * None.
803 *
804 * RETURNS:
805 * Device type, %ATA_DEV_ATA, %ATA_DEV_ATAPI, or %ATA_DEV_UNKNOWN
806 * the event of failure.
807 */
808
Jeff Garzik057ace52005-10-22 14:27:05 -0400809unsigned int ata_dev_classify(const struct ata_taskfile *tf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810{
811 /* Apple's open source Darwin code hints that some devices only
812 * put a proper signature into the LBA mid/high registers,
813 * So, we only check those. It's sufficient for uniqueness.
814 */
815
816 if (((tf->lbam == 0) && (tf->lbah == 0)) ||
817 ((tf->lbam == 0x3c) && (tf->lbah == 0xc3))) {
818 DPRINTK("found ATA device by sig\n");
819 return ATA_DEV_ATA;
820 }
821
822 if (((tf->lbam == 0x14) && (tf->lbah == 0xeb)) ||
823 ((tf->lbam == 0x69) && (tf->lbah == 0x96))) {
824 DPRINTK("found ATAPI device by sig\n");
825 return ATA_DEV_ATAPI;
826 }
827
828 DPRINTK("unknown device\n");
829 return ATA_DEV_UNKNOWN;
830}
831
832/**
833 * ata_dev_try_classify - Parse returned ATA device signature
834 * @ap: ATA channel to examine
835 * @device: Device to examine (starting at zero)
836 *
837 * After an event -- SRST, E.D.D., or SATA COMRESET -- occurs,
838 * an ATA/ATAPI-defined set of values is placed in the ATA
839 * shadow registers, indicating the results of device detection
840 * and diagnostics.
841 *
842 * Select the ATA device, and read the values from the ATA shadow
843 * registers. Then parse according to the Error register value,
844 * and the spec-defined values examined by ata_dev_classify().
845 *
846 * LOCKING:
847 * caller.
848 */
849
850static u8 ata_dev_try_classify(struct ata_port *ap, unsigned int device)
851{
852 struct ata_device *dev = &ap->device[device];
853 struct ata_taskfile tf;
854 unsigned int class;
855 u8 err;
856
857 ap->ops->dev_select(ap, device);
858
859 memset(&tf, 0, sizeof(tf));
860
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 ap->ops->tf_read(ap, &tf);
Jeff Garzik0169e282005-10-29 21:25:10 -0400862 err = tf.feature;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863
864 dev->class = ATA_DEV_NONE;
865
866 /* see if device passed diags */
867 if (err == 1)
868 /* do nothing */ ;
869 else if ((device == 0) && (err == 0x81))
870 /* do nothing */ ;
871 else
872 return err;
873
874 /* determine if device if ATA or ATAPI */
875 class = ata_dev_classify(&tf);
876 if (class == ATA_DEV_UNKNOWN)
877 return err;
878 if ((class == ATA_DEV_ATA) && (ata_chk_status(ap) == 0))
879 return err;
880
881 dev->class = class;
882
883 return err;
884}
885
886/**
887 * ata_dev_id_string - Convert IDENTIFY DEVICE page into string
888 * @id: IDENTIFY DEVICE results we will examine
889 * @s: string into which data is output
890 * @ofs: offset into identify device page
891 * @len: length of string to return. must be an even number.
892 *
893 * The strings in the IDENTIFY DEVICE page are broken up into
894 * 16-bit chunks. Run through the string, and output each
895 * 8-bit chunk linearly, regardless of platform.
896 *
897 * LOCKING:
898 * caller.
899 */
900
Jeff Garzik057ace52005-10-22 14:27:05 -0400901void ata_dev_id_string(const u16 *id, unsigned char *s,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 unsigned int ofs, unsigned int len)
903{
904 unsigned int c;
905
906 while (len > 0) {
907 c = id[ofs] >> 8;
908 *s = c;
909 s++;
910
911 c = id[ofs] & 0xff;
912 *s = c;
913 s++;
914
915 ofs++;
916 len -= 2;
917 }
918}
919
Edward Falk0baab862005-06-02 18:17:13 -0400920
921/**
922 * ata_noop_dev_select - Select device 0/1 on ATA bus
923 * @ap: ATA channel to manipulate
924 * @device: ATA device (numbered from zero) to select
925 *
926 * This function performs no actual function.
927 *
928 * May be used as the dev_select() entry in ata_port_operations.
929 *
930 * LOCKING:
931 * caller.
932 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933void ata_noop_dev_select (struct ata_port *ap, unsigned int device)
934{
935}
936
Edward Falk0baab862005-06-02 18:17:13 -0400937
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938/**
939 * ata_std_dev_select - Select device 0/1 on ATA bus
940 * @ap: ATA channel to manipulate
941 * @device: ATA device (numbered from zero) to select
942 *
943 * Use the method defined in the ATA specification to
944 * make either device 0, or device 1, active on the
Edward Falk0baab862005-06-02 18:17:13 -0400945 * ATA channel. Works with both PIO and MMIO.
946 *
947 * May be used as the dev_select() entry in ata_port_operations.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 *
949 * LOCKING:
950 * caller.
951 */
952
953void ata_std_dev_select (struct ata_port *ap, unsigned int device)
954{
955 u8 tmp;
956
957 if (device == 0)
958 tmp = ATA_DEVICE_OBS;
959 else
960 tmp = ATA_DEVICE_OBS | ATA_DEV1;
961
962 if (ap->flags & ATA_FLAG_MMIO) {
963 writeb(tmp, (void __iomem *) ap->ioaddr.device_addr);
964 } else {
965 outb(tmp, ap->ioaddr.device_addr);
966 }
967 ata_pause(ap); /* needed; also flushes, for mmio */
968}
969
970/**
971 * ata_dev_select - Select device 0/1 on ATA bus
972 * @ap: ATA channel to manipulate
973 * @device: ATA device (numbered from zero) to select
974 * @wait: non-zero to wait for Status register BSY bit to clear
975 * @can_sleep: non-zero if context allows sleeping
976 *
977 * Use the method defined in the ATA specification to
978 * make either device 0, or device 1, active on the
979 * ATA channel.
980 *
981 * This is a high-level version of ata_std_dev_select(),
982 * which additionally provides the services of inserting
983 * the proper pauses and status polling, where needed.
984 *
985 * LOCKING:
986 * caller.
987 */
988
989void ata_dev_select(struct ata_port *ap, unsigned int device,
990 unsigned int wait, unsigned int can_sleep)
991{
992 VPRINTK("ENTER, ata%u: device %u, wait %u\n",
993 ap->id, device, wait);
994
995 if (wait)
996 ata_wait_idle(ap);
997
998 ap->ops->dev_select(ap, device);
999
1000 if (wait) {
1001 if (can_sleep && ap->device[device].class == ATA_DEV_ATAPI)
1002 msleep(150);
1003 ata_wait_idle(ap);
1004 }
1005}
1006
1007/**
1008 * ata_dump_id - IDENTIFY DEVICE info debugging output
1009 * @dev: Device whose IDENTIFY DEVICE page we will dump
1010 *
1011 * Dump selected 16-bit words from a detected device's
1012 * IDENTIFY PAGE page.
1013 *
1014 * LOCKING:
1015 * caller.
1016 */
1017
Jeff Garzik057ace52005-10-22 14:27:05 -04001018static inline void ata_dump_id(const struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019{
1020 DPRINTK("49==0x%04x "
1021 "53==0x%04x "
1022 "63==0x%04x "
1023 "64==0x%04x "
1024 "75==0x%04x \n",
1025 dev->id[49],
1026 dev->id[53],
1027 dev->id[63],
1028 dev->id[64],
1029 dev->id[75]);
1030 DPRINTK("80==0x%04x "
1031 "81==0x%04x "
1032 "82==0x%04x "
1033 "83==0x%04x "
1034 "84==0x%04x \n",
1035 dev->id[80],
1036 dev->id[81],
1037 dev->id[82],
1038 dev->id[83],
1039 dev->id[84]);
1040 DPRINTK("88==0x%04x "
1041 "93==0x%04x\n",
1042 dev->id[88],
1043 dev->id[93]);
1044}
1045
Alan Cox11e29e22005-10-21 18:46:32 -04001046/*
1047 * Compute the PIO modes available for this device. This is not as
1048 * trivial as it seems if we must consider early devices correctly.
1049 *
1050 * FIXME: pre IDE drive timing (do we care ?).
1051 */
1052
Jeff Garzik057ace52005-10-22 14:27:05 -04001053static unsigned int ata_pio_modes(const struct ata_device *adev)
Alan Cox11e29e22005-10-21 18:46:32 -04001054{
1055 u16 modes;
1056
Alan Coxffa29452006-01-09 17:14:40 +00001057 /* Usual case. Word 53 indicates word 64 is valid */
1058 if (adev->id[ATA_ID_FIELD_VALID] & (1 << 1)) {
Alan Cox11e29e22005-10-21 18:46:32 -04001059 modes = adev->id[ATA_ID_PIO_MODES] & 0x03;
1060 modes <<= 3;
1061 modes |= 0x7;
1062 return modes;
1063 }
1064
Alan Coxffa29452006-01-09 17:14:40 +00001065 /* If word 64 isn't valid then Word 51 high byte holds the PIO timing
1066 number for the maximum. Turn it into a mask and return it */
1067 modes = (2 << ((adev->id[ATA_ID_OLD_PIO_MODES] >> 8) & 0xFF)) - 1 ;
Alan Cox11e29e22005-10-21 18:46:32 -04001068 return modes;
Alan Coxffa29452006-01-09 17:14:40 +00001069 /* But wait.. there's more. Design your standards by committee and
1070 you too can get a free iordy field to process. However its the
1071 speeds not the modes that are supported... Note drivers using the
1072 timing API will get this right anyway */
Alan Cox11e29e22005-10-21 18:46:32 -04001073}
1074
Tejun Heo77853bf2006-01-23 13:09:36 +09001075void ata_qc_complete_internal(struct ata_queued_cmd *qc)
Tejun Heoa2a7a662005-12-13 14:48:31 +09001076{
Tejun Heo77853bf2006-01-23 13:09:36 +09001077 struct completion *waiting = qc->private_data;
Tejun Heoa2a7a662005-12-13 14:48:31 +09001078
Tejun Heo77853bf2006-01-23 13:09:36 +09001079 qc->ap->ops->tf_read(qc->ap, &qc->tf);
Tejun Heoa2a7a662005-12-13 14:48:31 +09001080 complete(waiting);
Tejun Heoa2a7a662005-12-13 14:48:31 +09001081}
1082
1083/**
1084 * ata_exec_internal - execute libata internal command
1085 * @ap: Port to which the command is sent
1086 * @dev: Device to which the command is sent
1087 * @tf: Taskfile registers for the command and the result
1088 * @dma_dir: Data tranfer direction of the command
1089 * @buf: Data buffer of the command
1090 * @buflen: Length of data buffer
1091 *
1092 * Executes libata internal command with timeout. @tf contains
1093 * command on entry and result on return. Timeout and error
1094 * conditions are reported via return value. No recovery action
1095 * is taken after a command times out. It's caller's duty to
1096 * clean up after timeout.
1097 *
1098 * LOCKING:
1099 * None. Should be called with kernel context, might sleep.
1100 */
1101
1102static unsigned
1103ata_exec_internal(struct ata_port *ap, struct ata_device *dev,
1104 struct ata_taskfile *tf,
1105 int dma_dir, void *buf, unsigned int buflen)
1106{
1107 u8 command = tf->command;
1108 struct ata_queued_cmd *qc;
1109 DECLARE_COMPLETION(wait);
1110 unsigned long flags;
Tejun Heo77853bf2006-01-23 13:09:36 +09001111 unsigned int err_mask;
Tejun Heoa2a7a662005-12-13 14:48:31 +09001112
1113 spin_lock_irqsave(&ap->host_set->lock, flags);
1114
1115 qc = ata_qc_new_init(ap, dev);
1116 BUG_ON(qc == NULL);
1117
1118 qc->tf = *tf;
1119 qc->dma_dir = dma_dir;
1120 if (dma_dir != DMA_NONE) {
1121 ata_sg_init_one(qc, buf, buflen);
1122 qc->nsect = buflen / ATA_SECT_SIZE;
1123 }
1124
Tejun Heo77853bf2006-01-23 13:09:36 +09001125 qc->private_data = &wait;
Tejun Heoa2a7a662005-12-13 14:48:31 +09001126 qc->complete_fn = ata_qc_complete_internal;
1127
Tejun Heo9a3d9eb2006-01-23 13:09:36 +09001128 qc->err_mask = ata_qc_issue(qc);
1129 if (qc->err_mask)
Tejun Heo8e436af2006-01-23 13:09:36 +09001130 ata_qc_complete(qc);
Tejun Heoa2a7a662005-12-13 14:48:31 +09001131
1132 spin_unlock_irqrestore(&ap->host_set->lock, flags);
1133
1134 if (!wait_for_completion_timeout(&wait, ATA_TMOUT_INTERNAL)) {
1135 spin_lock_irqsave(&ap->host_set->lock, flags);
1136
1137 /* We're racing with irq here. If we lose, the
1138 * following test prevents us from completing the qc
1139 * again. If completion irq occurs after here but
1140 * before the caller cleans up, it will result in a
1141 * spurious interrupt. We can live with that.
1142 */
Tejun Heo77853bf2006-01-23 13:09:36 +09001143 if (qc->flags & ATA_QCFLAG_ACTIVE) {
Tejun Heo11a56d22006-01-23 13:09:36 +09001144 qc->err_mask = AC_ERR_TIMEOUT;
Tejun Heoa2a7a662005-12-13 14:48:31 +09001145 ata_qc_complete(qc);
1146 printk(KERN_WARNING "ata%u: qc timeout (cmd 0x%x)\n",
1147 ap->id, command);
1148 }
1149
1150 spin_unlock_irqrestore(&ap->host_set->lock, flags);
1151 }
1152
Tejun Heo77853bf2006-01-23 13:09:36 +09001153 *tf = qc->tf;
1154 err_mask = qc->err_mask;
1155
1156 ata_qc_free(qc);
1157
1158 return err_mask;
Tejun Heoa2a7a662005-12-13 14:48:31 +09001159}
1160
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161/**
Alan Cox1bc4ccf2006-01-09 17:18:14 +00001162 * ata_pio_need_iordy - check if iordy needed
1163 * @adev: ATA device
1164 *
1165 * Check if the current speed of the device requires IORDY. Used
1166 * by various controllers for chip configuration.
1167 */
1168
1169unsigned int ata_pio_need_iordy(const struct ata_device *adev)
1170{
1171 int pio;
1172 int speed = adev->pio_mode - XFER_PIO_0;
1173
1174 if (speed < 2)
1175 return 0;
1176 if (speed > 2)
1177 return 1;
1178
1179 /* If we have no drive specific rule, then PIO 2 is non IORDY */
1180
1181 if (adev->id[ATA_ID_FIELD_VALID] & 2) { /* EIDE */
1182 pio = adev->id[ATA_ID_EIDE_PIO];
1183 /* Is the speed faster than the drive allows non IORDY ? */
1184 if (pio) {
1185 /* This is cycle times not frequency - watch the logic! */
1186 if (pio > 240) /* PIO2 is 240nS per cycle */
1187 return 1;
1188 return 0;
1189 }
1190 }
1191 return 0;
1192}
1193
1194/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 * ata_dev_identify - obtain IDENTIFY x DEVICE page
1196 * @ap: port on which device we wish to probe resides
1197 * @device: device bus address, starting at zero
1198 *
1199 * Following bus reset, we issue the IDENTIFY [PACKET] DEVICE
1200 * command, and read back the 512-byte device information page.
1201 * The device information page is fed to us via the standard
1202 * PIO-IN protocol, but we hand-code it here. (TODO: investigate
1203 * using standard PIO-IN paths)
1204 *
1205 * After reading the device information page, we use several
1206 * bits of information from it to initialize data structures
1207 * that will be used during the lifetime of the ata_device.
1208 * Other data from the info page is used to disqualify certain
1209 * older ATA devices we do not wish to support.
1210 *
1211 * LOCKING:
1212 * Inherited from caller. Some functions called by this function
1213 * obtain the host_set lock.
1214 */
1215
1216static void ata_dev_identify(struct ata_port *ap, unsigned int device)
1217{
1218 struct ata_device *dev = &ap->device[device];
Albert Lee8bf62ece2005-05-12 15:29:42 -04001219 unsigned int major_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 u16 tmp;
1221 unsigned long xfer_modes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 unsigned int using_edd;
Tejun Heoa0123702005-12-13 14:49:31 +09001223 struct ata_taskfile tf;
1224 unsigned int err_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 int rc;
1226
1227 if (!ata_dev_present(dev)) {
1228 DPRINTK("ENTER/EXIT (host %u, dev %u) -- nodev\n",
1229 ap->id, device);
1230 return;
1231 }
1232
1233 if (ap->flags & (ATA_FLAG_SRST | ATA_FLAG_SATA_RESET))
1234 using_edd = 0;
1235 else
1236 using_edd = 1;
1237
1238 DPRINTK("ENTER, host %u, dev %u\n", ap->id, device);
1239
1240 assert (dev->class == ATA_DEV_ATA || dev->class == ATA_DEV_ATAPI ||
1241 dev->class == ATA_DEV_NONE);
1242
1243 ata_dev_select(ap, device, 1, 1); /* select device 0/1 */
1244
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245retry:
Tejun Heoa0123702005-12-13 14:49:31 +09001246 ata_tf_init(ap, &tf, device);
1247
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 if (dev->class == ATA_DEV_ATA) {
Tejun Heoa0123702005-12-13 14:49:31 +09001249 tf.command = ATA_CMD_ID_ATA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 DPRINTK("do ATA identify\n");
1251 } else {
Tejun Heoa0123702005-12-13 14:49:31 +09001252 tf.command = ATA_CMD_ID_ATAPI;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 DPRINTK("do ATAPI identify\n");
1254 }
1255
Tejun Heoa0123702005-12-13 14:49:31 +09001256 tf.protocol = ATA_PROT_PIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257
Tejun Heoa0123702005-12-13 14:49:31 +09001258 err_mask = ata_exec_internal(ap, dev, &tf, DMA_FROM_DEVICE,
1259 dev->id, sizeof(dev->id));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260
Tejun Heoa0123702005-12-13 14:49:31 +09001261 if (err_mask) {
1262 if (err_mask & ~AC_ERR_DEV)
1263 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 /*
1266 * arg! EDD works for all test cases, but seems to return
1267 * the ATA signature for some ATAPI devices. Until the
1268 * reason for this is found and fixed, we fix up the mess
1269 * here. If IDENTIFY DEVICE returns command aborted
1270 * (as ATAPI devices do), then we issue an
1271 * IDENTIFY PACKET DEVICE.
1272 *
1273 * ATA software reset (SRST, the default) does not appear
1274 * to have this problem.
1275 */
Albert Lee7c398332005-11-09 13:03:30 +08001276 if ((using_edd) && (dev->class == ATA_DEV_ATA)) {
Tejun Heoa0123702005-12-13 14:49:31 +09001277 u8 err = tf.feature;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 if (err & ATA_ABORTED) {
1279 dev->class = ATA_DEV_ATAPI;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 goto retry;
1281 }
1282 }
1283 goto err_out;
1284 }
1285
1286 swap_buf_le16(dev->id, ATA_ID_WORDS);
1287
1288 /* print device capabilities */
1289 printk(KERN_DEBUG "ata%u: dev %u cfg "
1290 "49:%04x 82:%04x 83:%04x 84:%04x 85:%04x 86:%04x 87:%04x 88:%04x\n",
1291 ap->id, device, dev->id[49],
1292 dev->id[82], dev->id[83], dev->id[84],
1293 dev->id[85], dev->id[86], dev->id[87],
1294 dev->id[88]);
1295
1296 /*
1297 * common ATA, ATAPI feature tests
1298 */
1299
Albert Lee8bf62ece2005-05-12 15:29:42 -04001300 /* we require DMA support (bits 8 of word 49) */
1301 if (!ata_id_has_dma(dev->id)) {
1302 printk(KERN_DEBUG "ata%u: no dma\n", ap->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 goto err_out_nosup;
1304 }
1305
1306 /* quick-n-dirty find max transfer mode; for printk only */
1307 xfer_modes = dev->id[ATA_ID_UDMA_MODES];
1308 if (!xfer_modes)
1309 xfer_modes = (dev->id[ATA_ID_MWDMA_MODES]) << ATA_SHIFT_MWDMA;
Alan Cox11e29e22005-10-21 18:46:32 -04001310 if (!xfer_modes)
1311 xfer_modes = ata_pio_modes(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312
1313 ata_dump_id(dev);
1314
1315 /* ATA-specific feature tests */
1316 if (dev->class == ATA_DEV_ATA) {
1317 if (!ata_id_is_ata(dev->id)) /* sanity check */
1318 goto err_out_nosup;
1319
Albert Lee8bf62ece2005-05-12 15:29:42 -04001320 /* get major version */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 tmp = dev->id[ATA_ID_MAJOR_VER];
Albert Lee8bf62ece2005-05-12 15:29:42 -04001322 for (major_version = 14; major_version >= 1; major_version--)
1323 if (tmp & (1 << major_version))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 break;
1325
Albert Lee8bf62ece2005-05-12 15:29:42 -04001326 /*
1327 * The exact sequence expected by certain pre-ATA4 drives is:
1328 * SRST RESET
1329 * IDENTIFY
1330 * INITIALIZE DEVICE PARAMETERS
1331 * anything else..
1332 * Some drives were very specific about that exact sequence.
1333 */
Albert Lee59a10b12005-10-12 15:09:42 +08001334 if (major_version < 4 || (!ata_id_has_lba(dev->id))) {
Albert Lee8bf62ece2005-05-12 15:29:42 -04001335 ata_dev_init_params(ap, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336
Albert Lee59a10b12005-10-12 15:09:42 +08001337 /* current CHS translation info (id[53-58]) might be
1338 * changed. reread the identify device info.
1339 */
1340 ata_dev_reread_id(ap, dev);
1341 }
1342
Albert Lee8bf62ece2005-05-12 15:29:42 -04001343 if (ata_id_has_lba(dev->id)) {
1344 dev->flags |= ATA_DFLAG_LBA;
1345
1346 if (ata_id_has_lba48(dev->id)) {
1347 dev->flags |= ATA_DFLAG_LBA48;
1348 dev->n_sectors = ata_id_u64(dev->id, 100);
1349 } else {
1350 dev->n_sectors = ata_id_u32(dev->id, 60);
1351 }
1352
1353 /* print device info to dmesg */
1354 printk(KERN_INFO "ata%u: dev %u ATA-%d, max %s, %Lu sectors:%s\n",
1355 ap->id, device,
1356 major_version,
1357 ata_mode_string(xfer_modes),
1358 (unsigned long long)dev->n_sectors,
1359 dev->flags & ATA_DFLAG_LBA48 ? " LBA48" : " LBA");
1360 } else {
1361 /* CHS */
1362
1363 /* Default translation */
1364 dev->cylinders = dev->id[1];
1365 dev->heads = dev->id[3];
1366 dev->sectors = dev->id[6];
1367 dev->n_sectors = dev->cylinders * dev->heads * dev->sectors;
1368
1369 if (ata_id_current_chs_valid(dev->id)) {
1370 /* Current CHS translation is valid. */
1371 dev->cylinders = dev->id[54];
1372 dev->heads = dev->id[55];
1373 dev->sectors = dev->id[56];
1374
1375 dev->n_sectors = ata_id_u32(dev->id, 57);
1376 }
1377
1378 /* print device info to dmesg */
1379 printk(KERN_INFO "ata%u: dev %u ATA-%d, max %s, %Lu sectors: CHS %d/%d/%d\n",
1380 ap->id, device,
1381 major_version,
1382 ata_mode_string(xfer_modes),
1383 (unsigned long long)dev->n_sectors,
1384 (int)dev->cylinders, (int)dev->heads, (int)dev->sectors);
1385
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 }
1387
1388 ap->host->max_cmd_len = 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 }
1390
1391 /* ATAPI-specific feature tests */
Jeff Garzik2c13b7c2005-11-14 14:14:16 -05001392 else if (dev->class == ATA_DEV_ATAPI) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 if (ata_id_is_ata(dev->id)) /* sanity check */
1394 goto err_out_nosup;
1395
1396 rc = atapi_cdb_len(dev->id);
1397 if ((rc < 12) || (rc > ATAPI_CDB_LEN)) {
1398 printk(KERN_WARNING "ata%u: unsupported CDB len\n", ap->id);
1399 goto err_out_nosup;
1400 }
1401 ap->cdb_len = (unsigned int) rc;
1402 ap->host->max_cmd_len = (unsigned char) ap->cdb_len;
1403
1404 /* print device info to dmesg */
1405 printk(KERN_INFO "ata%u: dev %u ATAPI, max %s\n",
1406 ap->id, device,
1407 ata_mode_string(xfer_modes));
1408 }
1409
1410 DPRINTK("EXIT, drv_stat = 0x%x\n", ata_chk_status(ap));
1411 return;
1412
1413err_out_nosup:
1414 printk(KERN_WARNING "ata%u: dev %u not supported, ignoring\n",
1415 ap->id, device);
1416err_out:
1417 dev->class++; /* converts ATA_DEV_xxx into ATA_DEV_xxx_UNSUP */
1418 DPRINTK("EXIT, err\n");
1419}
1420
Brad Campbell6f2f3812005-05-12 15:07:47 -04001421
Jeff Garzik057ace52005-10-22 14:27:05 -04001422static inline u8 ata_dev_knobble(const struct ata_port *ap)
Brad Campbell6f2f3812005-05-12 15:07:47 -04001423{
1424 return ((ap->cbl == ATA_CBL_SATA) && (!ata_id_is_sata(ap->device->id)));
1425}
1426
1427/**
1428 * ata_dev_config - Run device specific handlers and check for
1429 * SATA->PATA bridges
Jeff Garzik8a60a072005-07-31 13:13:24 -04001430 * @ap: Bus
Brad Campbell6f2f3812005-05-12 15:07:47 -04001431 * @i: Device
1432 *
1433 * LOCKING:
1434 */
Jeff Garzik8a60a072005-07-31 13:13:24 -04001435
Brad Campbell6f2f3812005-05-12 15:07:47 -04001436void ata_dev_config(struct ata_port *ap, unsigned int i)
1437{
1438 /* limit bridge transfers to udma5, 200 sectors */
1439 if (ata_dev_knobble(ap)) {
1440 printk(KERN_INFO "ata%u(%u): applying bridge limits\n",
1441 ap->id, ap->device->devno);
1442 ap->udma_mask &= ATA_UDMA5;
1443 ap->host->max_sectors = ATA_MAX_SECTORS;
1444 ap->host->hostt->max_sectors = ATA_MAX_SECTORS;
Alan Cox9d824d02006-01-17 20:51:55 +00001445 ap->device[i].flags |= ATA_DFLAG_LOCK_SECTORS;
Brad Campbell6f2f3812005-05-12 15:07:47 -04001446 }
1447
1448 if (ap->ops->dev_config)
1449 ap->ops->dev_config(ap, &ap->device[i]);
1450}
1451
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452/**
1453 * ata_bus_probe - Reset and probe ATA bus
1454 * @ap: Bus to probe
1455 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04001456 * Master ATA bus probing function. Initiates a hardware-dependent
1457 * bus reset, then attempts to identify any devices found on
1458 * the bus.
1459 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001461 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 *
1463 * RETURNS:
1464 * Zero on success, non-zero on error.
1465 */
1466
1467static int ata_bus_probe(struct ata_port *ap)
1468{
1469 unsigned int i, found = 0;
1470
1471 ap->ops->phy_reset(ap);
1472 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1473 goto err_out;
1474
1475 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1476 ata_dev_identify(ap, i);
1477 if (ata_dev_present(&ap->device[i])) {
1478 found = 1;
Brad Campbell6f2f3812005-05-12 15:07:47 -04001479 ata_dev_config(ap,i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 }
1481 }
1482
1483 if ((!found) || (ap->flags & ATA_FLAG_PORT_DISABLED))
1484 goto err_out_disable;
1485
1486 ata_set_mode(ap);
1487 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1488 goto err_out_disable;
1489
1490 return 0;
1491
1492err_out_disable:
1493 ap->ops->port_disable(ap);
1494err_out:
1495 return -1;
1496}
1497
1498/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04001499 * ata_port_probe - Mark port as enabled
1500 * @ap: Port for which we indicate enablement
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04001502 * Modify @ap data structure such that the system
1503 * thinks that the entire port is enabled.
1504 *
1505 * LOCKING: host_set lock, or some other form of
1506 * serialization.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 */
1508
1509void ata_port_probe(struct ata_port *ap)
1510{
1511 ap->flags &= ~ATA_FLAG_PORT_DISABLED;
1512}
1513
1514/**
Tejun Heo3be680b2005-12-19 22:35:02 +09001515 * sata_print_link_status - Print SATA link status
1516 * @ap: SATA port to printk link status about
1517 *
1518 * This function prints link speed and status of a SATA link.
1519 *
1520 * LOCKING:
1521 * None.
1522 */
1523static void sata_print_link_status(struct ata_port *ap)
1524{
1525 u32 sstatus, tmp;
1526 const char *speed;
1527
1528 if (!ap->ops->scr_read)
1529 return;
1530
1531 sstatus = scr_read(ap, SCR_STATUS);
1532
1533 if (sata_dev_present(ap)) {
1534 tmp = (sstatus >> 4) & 0xf;
1535 if (tmp & (1 << 0))
1536 speed = "1.5";
1537 else if (tmp & (1 << 1))
1538 speed = "3.0";
1539 else
1540 speed = "<unknown>";
1541 printk(KERN_INFO "ata%u: SATA link up %s Gbps (SStatus %X)\n",
1542 ap->id, speed, sstatus);
1543 } else {
1544 printk(KERN_INFO "ata%u: SATA link down (SStatus %X)\n",
1545 ap->id, sstatus);
1546 }
1547}
1548
1549/**
Jeff Garzik780a87f2005-05-30 15:41:05 -04001550 * __sata_phy_reset - Wake/reset a low-level SATA PHY
1551 * @ap: SATA port associated with target SATA PHY.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001553 * This function issues commands to standard SATA Sxxx
1554 * PHY registers, to wake up the phy (and device), and
1555 * clear any reset condition.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 *
1557 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001558 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 *
1560 */
1561void __sata_phy_reset(struct ata_port *ap)
1562{
1563 u32 sstatus;
1564 unsigned long timeout = jiffies + (HZ * 5);
1565
1566 if (ap->flags & ATA_FLAG_SATA_RESET) {
Brett Russcdcca89e2005-03-28 15:10:27 -05001567 /* issue phy wake/reset */
1568 scr_write_flush(ap, SCR_CONTROL, 0x301);
Tejun Heo62ba2842005-06-26 23:27:19 +09001569 /* Couldn't find anything in SATA I/II specs, but
1570 * AHCI-1.1 10.4.2 says at least 1 ms. */
1571 mdelay(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 }
Brett Russcdcca89e2005-03-28 15:10:27 -05001573 scr_write_flush(ap, SCR_CONTROL, 0x300); /* phy wake/clear reset */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574
1575 /* wait for phy to become ready, if necessary */
1576 do {
1577 msleep(200);
1578 sstatus = scr_read(ap, SCR_STATUS);
1579 if ((sstatus & 0xf) != 1)
1580 break;
1581 } while (time_before(jiffies, timeout));
1582
Tejun Heo3be680b2005-12-19 22:35:02 +09001583 /* print link status */
1584 sata_print_link_status(ap);
Jeff Garzik656563e2005-11-20 03:36:45 -05001585
Tejun Heo3be680b2005-12-19 22:35:02 +09001586 /* TODO: phy layer with polling, timeouts, etc. */
1587 if (sata_dev_present(ap))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 ata_port_probe(ap);
Tejun Heo3be680b2005-12-19 22:35:02 +09001589 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 ata_port_disable(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591
1592 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1593 return;
1594
1595 if (ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT)) {
1596 ata_port_disable(ap);
1597 return;
1598 }
1599
1600 ap->cbl = ATA_CBL_SATA;
1601}
1602
1603/**
Jeff Garzik780a87f2005-05-30 15:41:05 -04001604 * sata_phy_reset - Reset SATA bus.
1605 * @ap: SATA port associated with target SATA PHY.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001607 * This function resets the SATA bus, and then probes
1608 * the bus for devices.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 *
1610 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001611 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 *
1613 */
1614void sata_phy_reset(struct ata_port *ap)
1615{
1616 __sata_phy_reset(ap);
1617 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1618 return;
1619 ata_bus_reset(ap);
1620}
1621
1622/**
Jeff Garzik780a87f2005-05-30 15:41:05 -04001623 * ata_port_disable - Disable port.
1624 * @ap: Port to be disabled.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001626 * Modify @ap data structure such that the system
1627 * thinks that the entire port is disabled, and should
1628 * never attempt to probe or communicate with devices
1629 * on this port.
1630 *
1631 * LOCKING: host_set lock, or some other form of
1632 * serialization.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 */
1634
1635void ata_port_disable(struct ata_port *ap)
1636{
1637 ap->device[0].class = ATA_DEV_NONE;
1638 ap->device[1].class = ATA_DEV_NONE;
1639 ap->flags |= ATA_FLAG_PORT_DISABLED;
1640}
1641
Alan Cox452503f2005-10-21 19:01:32 -04001642/*
1643 * This mode timing computation functionality is ported over from
1644 * drivers/ide/ide-timing.h and was originally written by Vojtech Pavlik
1645 */
1646/*
1647 * PIO 0-5, MWDMA 0-2 and UDMA 0-6 timings (in nanoseconds).
1648 * These were taken from ATA/ATAPI-6 standard, rev 0a, except
1649 * for PIO 5, which is a nonstandard extension and UDMA6, which
1650 * is currently supported only by Maxtor drives.
1651 */
1652
1653static const struct ata_timing ata_timing[] = {
1654
1655 { XFER_UDMA_6, 0, 0, 0, 0, 0, 0, 0, 15 },
1656 { XFER_UDMA_5, 0, 0, 0, 0, 0, 0, 0, 20 },
1657 { XFER_UDMA_4, 0, 0, 0, 0, 0, 0, 0, 30 },
1658 { XFER_UDMA_3, 0, 0, 0, 0, 0, 0, 0, 45 },
1659
1660 { XFER_UDMA_2, 0, 0, 0, 0, 0, 0, 0, 60 },
1661 { XFER_UDMA_1, 0, 0, 0, 0, 0, 0, 0, 80 },
1662 { XFER_UDMA_0, 0, 0, 0, 0, 0, 0, 0, 120 },
1663
1664/* { XFER_UDMA_SLOW, 0, 0, 0, 0, 0, 0, 0, 150 }, */
1665
1666 { XFER_MW_DMA_2, 25, 0, 0, 0, 70, 25, 120, 0 },
1667 { XFER_MW_DMA_1, 45, 0, 0, 0, 80, 50, 150, 0 },
1668 { XFER_MW_DMA_0, 60, 0, 0, 0, 215, 215, 480, 0 },
1669
1670 { XFER_SW_DMA_2, 60, 0, 0, 0, 120, 120, 240, 0 },
1671 { XFER_SW_DMA_1, 90, 0, 0, 0, 240, 240, 480, 0 },
1672 { XFER_SW_DMA_0, 120, 0, 0, 0, 480, 480, 960, 0 },
1673
1674/* { XFER_PIO_5, 20, 50, 30, 100, 50, 30, 100, 0 }, */
1675 { XFER_PIO_4, 25, 70, 25, 120, 70, 25, 120, 0 },
1676 { XFER_PIO_3, 30, 80, 70, 180, 80, 70, 180, 0 },
1677
1678 { XFER_PIO_2, 30, 290, 40, 330, 100, 90, 240, 0 },
1679 { XFER_PIO_1, 50, 290, 93, 383, 125, 100, 383, 0 },
1680 { XFER_PIO_0, 70, 290, 240, 600, 165, 150, 600, 0 },
1681
1682/* { XFER_PIO_SLOW, 120, 290, 240, 960, 290, 240, 960, 0 }, */
1683
1684 { 0xFF }
1685};
1686
1687#define ENOUGH(v,unit) (((v)-1)/(unit)+1)
1688#define EZ(v,unit) ((v)?ENOUGH(v,unit):0)
1689
1690static void ata_timing_quantize(const struct ata_timing *t, struct ata_timing *q, int T, int UT)
1691{
1692 q->setup = EZ(t->setup * 1000, T);
1693 q->act8b = EZ(t->act8b * 1000, T);
1694 q->rec8b = EZ(t->rec8b * 1000, T);
1695 q->cyc8b = EZ(t->cyc8b * 1000, T);
1696 q->active = EZ(t->active * 1000, T);
1697 q->recover = EZ(t->recover * 1000, T);
1698 q->cycle = EZ(t->cycle * 1000, T);
1699 q->udma = EZ(t->udma * 1000, UT);
1700}
1701
1702void ata_timing_merge(const struct ata_timing *a, const struct ata_timing *b,
1703 struct ata_timing *m, unsigned int what)
1704{
1705 if (what & ATA_TIMING_SETUP ) m->setup = max(a->setup, b->setup);
1706 if (what & ATA_TIMING_ACT8B ) m->act8b = max(a->act8b, b->act8b);
1707 if (what & ATA_TIMING_REC8B ) m->rec8b = max(a->rec8b, b->rec8b);
1708 if (what & ATA_TIMING_CYC8B ) m->cyc8b = max(a->cyc8b, b->cyc8b);
1709 if (what & ATA_TIMING_ACTIVE ) m->active = max(a->active, b->active);
1710 if (what & ATA_TIMING_RECOVER) m->recover = max(a->recover, b->recover);
1711 if (what & ATA_TIMING_CYCLE ) m->cycle = max(a->cycle, b->cycle);
1712 if (what & ATA_TIMING_UDMA ) m->udma = max(a->udma, b->udma);
1713}
1714
1715static const struct ata_timing* ata_timing_find_mode(unsigned short speed)
1716{
1717 const struct ata_timing *t;
1718
1719 for (t = ata_timing; t->mode != speed; t++)
Alan Cox91190752005-10-26 12:17:46 -04001720 if (t->mode == 0xFF)
Alan Cox452503f2005-10-21 19:01:32 -04001721 return NULL;
1722 return t;
1723}
1724
1725int ata_timing_compute(struct ata_device *adev, unsigned short speed,
1726 struct ata_timing *t, int T, int UT)
1727{
1728 const struct ata_timing *s;
1729 struct ata_timing p;
1730
1731 /*
1732 * Find the mode.
Albert Lee75b1f2f2005-11-16 17:06:18 +08001733 */
Alan Cox452503f2005-10-21 19:01:32 -04001734
1735 if (!(s = ata_timing_find_mode(speed)))
1736 return -EINVAL;
1737
Albert Lee75b1f2f2005-11-16 17:06:18 +08001738 memcpy(t, s, sizeof(*s));
1739
Alan Cox452503f2005-10-21 19:01:32 -04001740 /*
1741 * If the drive is an EIDE drive, it can tell us it needs extended
1742 * PIO/MW_DMA cycle timing.
1743 */
1744
1745 if (adev->id[ATA_ID_FIELD_VALID] & 2) { /* EIDE drive */
1746 memset(&p, 0, sizeof(p));
1747 if(speed >= XFER_PIO_0 && speed <= XFER_SW_DMA_0) {
1748 if (speed <= XFER_PIO_2) p.cycle = p.cyc8b = adev->id[ATA_ID_EIDE_PIO];
1749 else p.cycle = p.cyc8b = adev->id[ATA_ID_EIDE_PIO_IORDY];
1750 } else if(speed >= XFER_MW_DMA_0 && speed <= XFER_MW_DMA_2) {
1751 p.cycle = adev->id[ATA_ID_EIDE_DMA_MIN];
1752 }
1753 ata_timing_merge(&p, t, t, ATA_TIMING_CYCLE | ATA_TIMING_CYC8B);
1754 }
1755
1756 /*
1757 * Convert the timing to bus clock counts.
1758 */
1759
Albert Lee75b1f2f2005-11-16 17:06:18 +08001760 ata_timing_quantize(t, t, T, UT);
Alan Cox452503f2005-10-21 19:01:32 -04001761
1762 /*
1763 * Even in DMA/UDMA modes we still use PIO access for IDENTIFY, S.M.A.R.T
1764 * and some other commands. We have to ensure that the DMA cycle timing is
1765 * slower/equal than the fastest PIO timing.
1766 */
1767
1768 if (speed > XFER_PIO_4) {
1769 ata_timing_compute(adev, adev->pio_mode, &p, T, UT);
1770 ata_timing_merge(&p, t, t, ATA_TIMING_ALL);
1771 }
1772
1773 /*
1774 * Lenghten active & recovery time so that cycle time is correct.
1775 */
1776
1777 if (t->act8b + t->rec8b < t->cyc8b) {
1778 t->act8b += (t->cyc8b - (t->act8b + t->rec8b)) / 2;
1779 t->rec8b = t->cyc8b - t->act8b;
1780 }
1781
1782 if (t->active + t->recover < t->cycle) {
1783 t->active += (t->cycle - (t->active + t->recover)) / 2;
1784 t->recover = t->cycle - t->active;
1785 }
1786
1787 return 0;
1788}
1789
Jeff Garzik057ace52005-10-22 14:27:05 -04001790static const struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 unsigned int shift;
1792 u8 base;
1793} xfer_mode_classes[] = {
1794 { ATA_SHIFT_UDMA, XFER_UDMA_0 },
1795 { ATA_SHIFT_MWDMA, XFER_MW_DMA_0 },
1796 { ATA_SHIFT_PIO, XFER_PIO_0 },
1797};
1798
Arjan van de Ven858119e2006-01-14 13:20:43 -08001799static u8 base_from_shift(unsigned int shift)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800{
1801 int i;
1802
1803 for (i = 0; i < ARRAY_SIZE(xfer_mode_classes); i++)
1804 if (xfer_mode_classes[i].shift == shift)
1805 return xfer_mode_classes[i].base;
1806
1807 return 0xff;
1808}
1809
1810static void ata_dev_set_mode(struct ata_port *ap, struct ata_device *dev)
1811{
1812 int ofs, idx;
1813 u8 base;
1814
1815 if (!ata_dev_present(dev) || (ap->flags & ATA_FLAG_PORT_DISABLED))
1816 return;
1817
1818 if (dev->xfer_shift == ATA_SHIFT_PIO)
1819 dev->flags |= ATA_DFLAG_PIO;
1820
1821 ata_dev_set_xfermode(ap, dev);
1822
1823 base = base_from_shift(dev->xfer_shift);
1824 ofs = dev->xfer_mode - base;
1825 idx = ofs + dev->xfer_shift;
1826 WARN_ON(idx >= ARRAY_SIZE(xfer_mode_str));
1827
1828 DPRINTK("idx=%d xfer_shift=%u, xfer_mode=0x%x, base=0x%x, offset=%d\n",
1829 idx, dev->xfer_shift, (int)dev->xfer_mode, (int)base, ofs);
1830
1831 printk(KERN_INFO "ata%u: dev %u configured for %s\n",
1832 ap->id, dev->devno, xfer_mode_str[idx]);
1833}
1834
1835static int ata_host_set_pio(struct ata_port *ap)
1836{
1837 unsigned int mask;
1838 int x, i;
1839 u8 base, xfer_mode;
1840
1841 mask = ata_get_mode_mask(ap, ATA_SHIFT_PIO);
1842 x = fgb(mask);
1843 if (x < 0) {
1844 printk(KERN_WARNING "ata%u: no PIO support\n", ap->id);
1845 return -1;
1846 }
1847
1848 base = base_from_shift(ATA_SHIFT_PIO);
1849 xfer_mode = base + x;
1850
1851 DPRINTK("base 0x%x xfer_mode 0x%x mask 0x%x x %d\n",
1852 (int)base, (int)xfer_mode, mask, x);
1853
1854 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1855 struct ata_device *dev = &ap->device[i];
1856 if (ata_dev_present(dev)) {
1857 dev->pio_mode = xfer_mode;
1858 dev->xfer_mode = xfer_mode;
1859 dev->xfer_shift = ATA_SHIFT_PIO;
1860 if (ap->ops->set_piomode)
1861 ap->ops->set_piomode(ap, dev);
1862 }
1863 }
1864
1865 return 0;
1866}
1867
1868static void ata_host_set_dma(struct ata_port *ap, u8 xfer_mode,
1869 unsigned int xfer_shift)
1870{
1871 int i;
1872
1873 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1874 struct ata_device *dev = &ap->device[i];
1875 if (ata_dev_present(dev)) {
1876 dev->dma_mode = xfer_mode;
1877 dev->xfer_mode = xfer_mode;
1878 dev->xfer_shift = xfer_shift;
1879 if (ap->ops->set_dmamode)
1880 ap->ops->set_dmamode(ap, dev);
1881 }
1882 }
1883}
1884
1885/**
1886 * ata_set_mode - Program timings and issue SET FEATURES - XFER
1887 * @ap: port on which timings will be programmed
1888 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001889 * Set ATA device disk transfer mode (PIO3, UDMA6, etc.).
1890 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001892 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 *
1894 */
1895static void ata_set_mode(struct ata_port *ap)
1896{
Albert Lee8cbd6df2005-10-12 15:06:27 +08001897 unsigned int xfer_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898 u8 xfer_mode;
1899 int rc;
1900
1901 /* step 1: always set host PIO timings */
1902 rc = ata_host_set_pio(ap);
1903 if (rc)
1904 goto err_out;
1905
1906 /* step 2: choose the best data xfer mode */
1907 xfer_mode = xfer_shift = 0;
1908 rc = ata_choose_xfer_mode(ap, &xfer_mode, &xfer_shift);
1909 if (rc)
1910 goto err_out;
1911
1912 /* step 3: if that xfer mode isn't PIO, set host DMA timings */
1913 if (xfer_shift != ATA_SHIFT_PIO)
1914 ata_host_set_dma(ap, xfer_mode, xfer_shift);
1915
1916 /* step 4: update devices' xfer mode */
1917 ata_dev_set_mode(ap, &ap->device[0]);
1918 ata_dev_set_mode(ap, &ap->device[1]);
1919
1920 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1921 return;
1922
1923 if (ap->ops->post_set_mode)
1924 ap->ops->post_set_mode(ap);
1925
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 return;
1927
1928err_out:
1929 ata_port_disable(ap);
1930}
1931
1932/**
1933 * ata_busy_sleep - sleep until BSY clears, or timeout
1934 * @ap: port containing status register to be polled
1935 * @tmout_pat: impatience timeout
1936 * @tmout: overall timeout
1937 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001938 * Sleep until ATA Status register bit BSY clears,
1939 * or a timeout occurs.
1940 *
1941 * LOCKING: None.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 *
1943 */
1944
1945static unsigned int ata_busy_sleep (struct ata_port *ap,
1946 unsigned long tmout_pat,
1947 unsigned long tmout)
1948{
1949 unsigned long timer_start, timeout;
1950 u8 status;
1951
1952 status = ata_busy_wait(ap, ATA_BUSY, 300);
1953 timer_start = jiffies;
1954 timeout = timer_start + tmout_pat;
1955 while ((status & ATA_BUSY) && (time_before(jiffies, timeout))) {
1956 msleep(50);
1957 status = ata_busy_wait(ap, ATA_BUSY, 3);
1958 }
1959
1960 if (status & ATA_BUSY)
1961 printk(KERN_WARNING "ata%u is slow to respond, "
1962 "please be patient\n", ap->id);
1963
1964 timeout = timer_start + tmout;
1965 while ((status & ATA_BUSY) && (time_before(jiffies, timeout))) {
1966 msleep(50);
1967 status = ata_chk_status(ap);
1968 }
1969
1970 if (status & ATA_BUSY) {
1971 printk(KERN_ERR "ata%u failed to respond (%lu secs)\n",
1972 ap->id, tmout / HZ);
1973 return 1;
1974 }
1975
1976 return 0;
1977}
1978
1979static void ata_bus_post_reset(struct ata_port *ap, unsigned int devmask)
1980{
1981 struct ata_ioports *ioaddr = &ap->ioaddr;
1982 unsigned int dev0 = devmask & (1 << 0);
1983 unsigned int dev1 = devmask & (1 << 1);
1984 unsigned long timeout;
1985
1986 /* if device 0 was found in ata_devchk, wait for its
1987 * BSY bit to clear
1988 */
1989 if (dev0)
1990 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
1991
1992 /* if device 1 was found in ata_devchk, wait for
1993 * register access, then wait for BSY to clear
1994 */
1995 timeout = jiffies + ATA_TMOUT_BOOT;
1996 while (dev1) {
1997 u8 nsect, lbal;
1998
1999 ap->ops->dev_select(ap, 1);
2000 if (ap->flags & ATA_FLAG_MMIO) {
2001 nsect = readb((void __iomem *) ioaddr->nsect_addr);
2002 lbal = readb((void __iomem *) ioaddr->lbal_addr);
2003 } else {
2004 nsect = inb(ioaddr->nsect_addr);
2005 lbal = inb(ioaddr->lbal_addr);
2006 }
2007 if ((nsect == 1) && (lbal == 1))
2008 break;
2009 if (time_after(jiffies, timeout)) {
2010 dev1 = 0;
2011 break;
2012 }
2013 msleep(50); /* give drive a breather */
2014 }
2015 if (dev1)
2016 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
2017
2018 /* is all this really necessary? */
2019 ap->ops->dev_select(ap, 0);
2020 if (dev1)
2021 ap->ops->dev_select(ap, 1);
2022 if (dev0)
2023 ap->ops->dev_select(ap, 0);
2024}
2025
2026/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04002027 * ata_bus_edd - Issue EXECUTE DEVICE DIAGNOSTIC command.
2028 * @ap: Port to reset and probe
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04002030 * Use the EXECUTE DEVICE DIAGNOSTIC command to reset and
2031 * probe the bus. Not often used these days.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 *
2033 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002034 * PCI/etc. bus probe sem.
Jeff Garzike5338252005-10-30 21:37:17 -05002035 * Obtains host_set lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 *
2037 */
2038
2039static unsigned int ata_bus_edd(struct ata_port *ap)
2040{
2041 struct ata_taskfile tf;
Jeff Garzike5338252005-10-30 21:37:17 -05002042 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043
2044 /* set up execute-device-diag (bus reset) taskfile */
2045 /* also, take interrupts to a known state (disabled) */
2046 DPRINTK("execute-device-diag\n");
2047 ata_tf_init(ap, &tf, 0);
2048 tf.ctl |= ATA_NIEN;
2049 tf.command = ATA_CMD_EDD;
2050 tf.protocol = ATA_PROT_NODATA;
2051
2052 /* do bus reset */
Jeff Garzike5338252005-10-30 21:37:17 -05002053 spin_lock_irqsave(&ap->host_set->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 ata_tf_to_host(ap, &tf);
Jeff Garzike5338252005-10-30 21:37:17 -05002055 spin_unlock_irqrestore(&ap->host_set->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056
2057 /* spec says at least 2ms. but who knows with those
2058 * crazy ATAPI devices...
2059 */
2060 msleep(150);
2061
2062 return ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
2063}
2064
2065static unsigned int ata_bus_softreset(struct ata_port *ap,
2066 unsigned int devmask)
2067{
2068 struct ata_ioports *ioaddr = &ap->ioaddr;
2069
2070 DPRINTK("ata%u: bus reset via SRST\n", ap->id);
2071
2072 /* software reset. causes dev0 to be selected */
2073 if (ap->flags & ATA_FLAG_MMIO) {
2074 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
2075 udelay(20); /* FIXME: flush */
2076 writeb(ap->ctl | ATA_SRST, (void __iomem *) ioaddr->ctl_addr);
2077 udelay(20); /* FIXME: flush */
2078 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
2079 } else {
2080 outb(ap->ctl, ioaddr->ctl_addr);
2081 udelay(10);
2082 outb(ap->ctl | ATA_SRST, ioaddr->ctl_addr);
2083 udelay(10);
2084 outb(ap->ctl, ioaddr->ctl_addr);
2085 }
2086
2087 /* spec mandates ">= 2ms" before checking status.
2088 * We wait 150ms, because that was the magic delay used for
2089 * ATAPI devices in Hale Landis's ATADRVR, for the period of time
2090 * between when the ATA command register is written, and then
2091 * status is checked. Because waiting for "a while" before
2092 * checking status is fine, post SRST, we perform this magic
2093 * delay here as well.
2094 */
2095 msleep(150);
2096
2097 ata_bus_post_reset(ap, devmask);
2098
2099 return 0;
2100}
2101
2102/**
2103 * ata_bus_reset - reset host port and associated ATA channel
2104 * @ap: port to reset
2105 *
2106 * This is typically the first time we actually start issuing
2107 * commands to the ATA channel. We wait for BSY to clear, then
2108 * issue EXECUTE DEVICE DIAGNOSTIC command, polling for its
2109 * result. Determine what devices, if any, are on the channel
2110 * by looking at the device 0/1 error register. Look at the signature
2111 * stored in each device's taskfile registers, to determine if
2112 * the device is ATA or ATAPI.
2113 *
2114 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002115 * PCI/etc. bus probe sem.
2116 * Obtains host_set lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117 *
2118 * SIDE EFFECTS:
2119 * Sets ATA_FLAG_PORT_DISABLED if bus reset fails.
2120 */
2121
2122void ata_bus_reset(struct ata_port *ap)
2123{
2124 struct ata_ioports *ioaddr = &ap->ioaddr;
2125 unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS;
2126 u8 err;
2127 unsigned int dev0, dev1 = 0, rc = 0, devmask = 0;
2128
2129 DPRINTK("ENTER, host %u, port %u\n", ap->id, ap->port_no);
2130
2131 /* determine if device 0/1 are present */
2132 if (ap->flags & ATA_FLAG_SATA_RESET)
2133 dev0 = 1;
2134 else {
2135 dev0 = ata_devchk(ap, 0);
2136 if (slave_possible)
2137 dev1 = ata_devchk(ap, 1);
2138 }
2139
2140 if (dev0)
2141 devmask |= (1 << 0);
2142 if (dev1)
2143 devmask |= (1 << 1);
2144
2145 /* select device 0 again */
2146 ap->ops->dev_select(ap, 0);
2147
2148 /* issue bus reset */
2149 if (ap->flags & ATA_FLAG_SRST)
2150 rc = ata_bus_softreset(ap, devmask);
2151 else if ((ap->flags & ATA_FLAG_SATA_RESET) == 0) {
2152 /* set up device control */
2153 if (ap->flags & ATA_FLAG_MMIO)
2154 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
2155 else
2156 outb(ap->ctl, ioaddr->ctl_addr);
2157 rc = ata_bus_edd(ap);
2158 }
2159
2160 if (rc)
2161 goto err_out;
2162
2163 /*
2164 * determine by signature whether we have ATA or ATAPI devices
2165 */
2166 err = ata_dev_try_classify(ap, 0);
2167 if ((slave_possible) && (err != 0x81))
2168 ata_dev_try_classify(ap, 1);
2169
2170 /* re-enable interrupts */
2171 if (ap->ioaddr.ctl_addr) /* FIXME: hack. create a hook instead */
2172 ata_irq_on(ap);
2173
2174 /* is double-select really necessary? */
2175 if (ap->device[1].class != ATA_DEV_NONE)
2176 ap->ops->dev_select(ap, 1);
2177 if (ap->device[0].class != ATA_DEV_NONE)
2178 ap->ops->dev_select(ap, 0);
2179
2180 /* if no devices were detected, disable this port */
2181 if ((ap->device[0].class == ATA_DEV_NONE) &&
2182 (ap->device[1].class == ATA_DEV_NONE))
2183 goto err_out;
2184
2185 if (ap->flags & (ATA_FLAG_SATA_RESET | ATA_FLAG_SRST)) {
2186 /* set up device control for ATA_FLAG_SATA_RESET */
2187 if (ap->flags & ATA_FLAG_MMIO)
2188 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
2189 else
2190 outb(ap->ctl, ioaddr->ctl_addr);
2191 }
2192
2193 DPRINTK("EXIT\n");
2194 return;
2195
2196err_out:
2197 printk(KERN_ERR "ata%u: disabling port\n", ap->id);
2198 ap->ops->port_disable(ap);
2199
2200 DPRINTK("EXIT\n");
2201}
2202
Jeff Garzik057ace52005-10-22 14:27:05 -04002203static void ata_pr_blacklisted(const struct ata_port *ap,
2204 const struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205{
2206 printk(KERN_WARNING "ata%u: dev %u is on DMA blacklist, disabling DMA\n",
2207 ap->id, dev->devno);
2208}
2209
Arjan van de Ven98ac62d2005-11-28 10:06:23 +01002210static const char * const ata_dma_blacklist [] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211 "WDC AC11000H",
2212 "WDC AC22100H",
2213 "WDC AC32500H",
2214 "WDC AC33100H",
2215 "WDC AC31600H",
2216 "WDC AC32100H",
2217 "WDC AC23200L",
2218 "Compaq CRD-8241B",
2219 "CRD-8400B",
2220 "CRD-8480B",
2221 "CRD-8482B",
2222 "CRD-84",
2223 "SanDisk SDP3B",
2224 "SanDisk SDP3B-64",
2225 "SANYO CD-ROM CRD",
2226 "HITACHI CDR-8",
2227 "HITACHI CDR-8335",
2228 "HITACHI CDR-8435",
2229 "Toshiba CD-ROM XM-6202B",
Jeff Garzike9222562005-06-28 00:03:37 -04002230 "TOSHIBA CD-ROM XM-1702BC",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231 "CD-532E-A",
2232 "E-IDE CD-ROM CR-840",
2233 "CD-ROM Drive/F5A",
2234 "WPI CDD-820",
2235 "SAMSUNG CD-ROM SC-148C",
2236 "SAMSUNG CD-ROM SC",
2237 "SanDisk SDP3B-64",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238 "ATAPI CD-ROM DRIVE 40X MAXIMUM",
2239 "_NEC DV5800A",
2240};
2241
Jeff Garzik057ace52005-10-22 14:27:05 -04002242static int ata_dma_blacklisted(const struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243{
2244 unsigned char model_num[40];
2245 char *s;
2246 unsigned int len;
2247 int i;
2248
2249 ata_dev_id_string(dev->id, model_num, ATA_ID_PROD_OFS,
2250 sizeof(model_num));
2251 s = &model_num[0];
2252 len = strnlen(s, sizeof(model_num));
2253
2254 /* ATAPI specifies that empty space is blank-filled; remove blanks */
2255 while ((len > 0) && (s[len - 1] == ' ')) {
2256 len--;
2257 s[len] = 0;
2258 }
2259
2260 for (i = 0; i < ARRAY_SIZE(ata_dma_blacklist); i++)
2261 if (!strncmp(ata_dma_blacklist[i], s, len))
2262 return 1;
2263
2264 return 0;
2265}
2266
Jeff Garzik057ace52005-10-22 14:27:05 -04002267static unsigned int ata_get_mode_mask(const struct ata_port *ap, int shift)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268{
Jeff Garzik057ace52005-10-22 14:27:05 -04002269 const struct ata_device *master, *slave;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270 unsigned int mask;
2271
2272 master = &ap->device[0];
2273 slave = &ap->device[1];
2274
2275 assert (ata_dev_present(master) || ata_dev_present(slave));
2276
2277 if (shift == ATA_SHIFT_UDMA) {
2278 mask = ap->udma_mask;
2279 if (ata_dev_present(master)) {
2280 mask &= (master->id[ATA_ID_UDMA_MODES] & 0xff);
Jeff Garzik057ace52005-10-22 14:27:05 -04002281 if (ata_dma_blacklisted(master)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282 mask = 0;
2283 ata_pr_blacklisted(ap, master);
2284 }
2285 }
2286 if (ata_dev_present(slave)) {
2287 mask &= (slave->id[ATA_ID_UDMA_MODES] & 0xff);
Jeff Garzik057ace52005-10-22 14:27:05 -04002288 if (ata_dma_blacklisted(slave)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289 mask = 0;
2290 ata_pr_blacklisted(ap, slave);
2291 }
2292 }
2293 }
2294 else if (shift == ATA_SHIFT_MWDMA) {
2295 mask = ap->mwdma_mask;
2296 if (ata_dev_present(master)) {
2297 mask &= (master->id[ATA_ID_MWDMA_MODES] & 0x07);
Jeff Garzik057ace52005-10-22 14:27:05 -04002298 if (ata_dma_blacklisted(master)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299 mask = 0;
2300 ata_pr_blacklisted(ap, master);
2301 }
2302 }
2303 if (ata_dev_present(slave)) {
2304 mask &= (slave->id[ATA_ID_MWDMA_MODES] & 0x07);
Jeff Garzik057ace52005-10-22 14:27:05 -04002305 if (ata_dma_blacklisted(slave)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306 mask = 0;
2307 ata_pr_blacklisted(ap, slave);
2308 }
2309 }
2310 }
2311 else if (shift == ATA_SHIFT_PIO) {
2312 mask = ap->pio_mask;
2313 if (ata_dev_present(master)) {
2314 /* spec doesn't return explicit support for
2315 * PIO0-2, so we fake it
2316 */
2317 u16 tmp_mode = master->id[ATA_ID_PIO_MODES] & 0x03;
2318 tmp_mode <<= 3;
2319 tmp_mode |= 0x7;
2320 mask &= tmp_mode;
2321 }
2322 if (ata_dev_present(slave)) {
2323 /* spec doesn't return explicit support for
2324 * PIO0-2, so we fake it
2325 */
2326 u16 tmp_mode = slave->id[ATA_ID_PIO_MODES] & 0x03;
2327 tmp_mode <<= 3;
2328 tmp_mode |= 0x7;
2329 mask &= tmp_mode;
2330 }
2331 }
2332 else {
2333 mask = 0xffffffff; /* shut up compiler warning */
2334 BUG();
2335 }
2336
2337 return mask;
2338}
2339
2340/* find greatest bit */
2341static int fgb(u32 bitmap)
2342{
2343 unsigned int i;
2344 int x = -1;
2345
2346 for (i = 0; i < 32; i++)
2347 if (bitmap & (1 << i))
2348 x = i;
2349
2350 return x;
2351}
2352
2353/**
2354 * ata_choose_xfer_mode - attempt to find best transfer mode
2355 * @ap: Port for which an xfer mode will be selected
2356 * @xfer_mode_out: (output) SET FEATURES - XFER MODE code
2357 * @xfer_shift_out: (output) bit shift that selects this mode
2358 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04002359 * Based on host and device capabilities, determine the
2360 * maximum transfer mode that is amenable to all.
2361 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002363 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364 *
2365 * RETURNS:
2366 * Zero on success, negative on error.
2367 */
2368
Jeff Garzik057ace52005-10-22 14:27:05 -04002369static int ata_choose_xfer_mode(const struct ata_port *ap,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370 u8 *xfer_mode_out,
2371 unsigned int *xfer_shift_out)
2372{
2373 unsigned int mask, shift;
2374 int x, i;
2375
2376 for (i = 0; i < ARRAY_SIZE(xfer_mode_classes); i++) {
2377 shift = xfer_mode_classes[i].shift;
2378 mask = ata_get_mode_mask(ap, shift);
2379
2380 x = fgb(mask);
2381 if (x >= 0) {
2382 *xfer_mode_out = xfer_mode_classes[i].base + x;
2383 *xfer_shift_out = shift;
2384 return 0;
2385 }
2386 }
2387
2388 return -1;
2389}
2390
2391/**
2392 * ata_dev_set_xfermode - Issue SET FEATURES - XFER MODE command
2393 * @ap: Port associated with device @dev
2394 * @dev: Device to which command will be sent
2395 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04002396 * Issue SET FEATURES - XFER MODE command to device @dev
2397 * on port @ap.
2398 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002400 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401 */
2402
2403static void ata_dev_set_xfermode(struct ata_port *ap, struct ata_device *dev)
2404{
Tejun Heoa0123702005-12-13 14:49:31 +09002405 struct ata_taskfile tf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406
2407 /* set up set-features taskfile */
2408 DPRINTK("set features - xfer mode\n");
2409
Tejun Heoa0123702005-12-13 14:49:31 +09002410 ata_tf_init(ap, &tf, dev->devno);
2411 tf.command = ATA_CMD_SET_FEATURES;
2412 tf.feature = SETFEATURES_XFER;
2413 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
2414 tf.protocol = ATA_PROT_NODATA;
2415 tf.nsect = dev->xfer_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416
Tejun Heoa0123702005-12-13 14:49:31 +09002417 if (ata_exec_internal(ap, dev, &tf, DMA_NONE, NULL, 0)) {
2418 printk(KERN_ERR "ata%u: failed to set xfermode, disabled\n",
2419 ap->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420 ata_port_disable(ap);
Tejun Heoa0123702005-12-13 14:49:31 +09002421 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422
2423 DPRINTK("EXIT\n");
2424}
2425
2426/**
Albert Lee59a10b12005-10-12 15:09:42 +08002427 * ata_dev_reread_id - Reread the device identify device info
2428 * @ap: port where the device is
2429 * @dev: device to reread the identify device info
2430 *
2431 * LOCKING:
2432 */
2433
2434static void ata_dev_reread_id(struct ata_port *ap, struct ata_device *dev)
2435{
Tejun Heoa0123702005-12-13 14:49:31 +09002436 struct ata_taskfile tf;
Albert Lee59a10b12005-10-12 15:09:42 +08002437
Tejun Heoa0123702005-12-13 14:49:31 +09002438 ata_tf_init(ap, &tf, dev->devno);
Albert Lee59a10b12005-10-12 15:09:42 +08002439
2440 if (dev->class == ATA_DEV_ATA) {
Tejun Heoa0123702005-12-13 14:49:31 +09002441 tf.command = ATA_CMD_ID_ATA;
Albert Lee59a10b12005-10-12 15:09:42 +08002442 DPRINTK("do ATA identify\n");
2443 } else {
Tejun Heoa0123702005-12-13 14:49:31 +09002444 tf.command = ATA_CMD_ID_ATAPI;
Albert Lee59a10b12005-10-12 15:09:42 +08002445 DPRINTK("do ATAPI identify\n");
2446 }
2447
Tejun Heoa0123702005-12-13 14:49:31 +09002448 tf.flags |= ATA_TFLAG_DEVICE;
2449 tf.protocol = ATA_PROT_PIO;
Albert Lee59a10b12005-10-12 15:09:42 +08002450
Tejun Heoa0123702005-12-13 14:49:31 +09002451 if (ata_exec_internal(ap, dev, &tf, DMA_FROM_DEVICE,
2452 dev->id, sizeof(dev->id)))
Albert Lee59a10b12005-10-12 15:09:42 +08002453 goto err_out;
2454
Albert Lee59a10b12005-10-12 15:09:42 +08002455 swap_buf_le16(dev->id, ATA_ID_WORDS);
2456
2457 ata_dump_id(dev);
2458
2459 DPRINTK("EXIT\n");
2460
2461 return;
2462err_out:
Tejun Heoa0123702005-12-13 14:49:31 +09002463 printk(KERN_ERR "ata%u: failed to reread ID, disabled\n", ap->id);
Albert Lee59a10b12005-10-12 15:09:42 +08002464 ata_port_disable(ap);
2465}
2466
2467/**
Albert Lee8bf62ece2005-05-12 15:29:42 -04002468 * ata_dev_init_params - Issue INIT DEV PARAMS command
2469 * @ap: Port associated with device @dev
2470 * @dev: Device to which command will be sent
2471 *
2472 * LOCKING:
2473 */
2474
2475static void ata_dev_init_params(struct ata_port *ap, struct ata_device *dev)
2476{
Tejun Heoa0123702005-12-13 14:49:31 +09002477 struct ata_taskfile tf;
Albert Lee8bf62ece2005-05-12 15:29:42 -04002478 u16 sectors = dev->id[6];
2479 u16 heads = dev->id[3];
2480
2481 /* Number of sectors per track 1-255. Number of heads 1-16 */
2482 if (sectors < 1 || sectors > 255 || heads < 1 || heads > 16)
2483 return;
2484
2485 /* set up init dev params taskfile */
2486 DPRINTK("init dev params \n");
2487
Tejun Heoa0123702005-12-13 14:49:31 +09002488 ata_tf_init(ap, &tf, dev->devno);
2489 tf.command = ATA_CMD_INIT_DEV_PARAMS;
2490 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
2491 tf.protocol = ATA_PROT_NODATA;
2492 tf.nsect = sectors;
2493 tf.device |= (heads - 1) & 0x0f; /* max head = num. of heads - 1 */
Albert Lee8bf62ece2005-05-12 15:29:42 -04002494
Tejun Heoa0123702005-12-13 14:49:31 +09002495 if (ata_exec_internal(ap, dev, &tf, DMA_NONE, NULL, 0)) {
2496 printk(KERN_ERR "ata%u: failed to init parameters, disabled\n",
2497 ap->id);
Albert Lee8bf62ece2005-05-12 15:29:42 -04002498 ata_port_disable(ap);
Tejun Heoa0123702005-12-13 14:49:31 +09002499 }
Albert Lee8bf62ece2005-05-12 15:29:42 -04002500
2501 DPRINTK("EXIT\n");
2502}
2503
2504/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04002505 * ata_sg_clean - Unmap DMA memory associated with command
2506 * @qc: Command containing DMA memory to be released
2507 *
2508 * Unmap all mapped DMA memory associated with this command.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509 *
2510 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002511 * spin_lock_irqsave(host_set lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512 */
2513
2514static void ata_sg_clean(struct ata_queued_cmd *qc)
2515{
2516 struct ata_port *ap = qc->ap;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002517 struct scatterlist *sg = qc->__sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518 int dir = qc->dma_dir;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002519 void *pad_buf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520
2521 assert(qc->flags & ATA_QCFLAG_DMAMAP);
2522 assert(sg != NULL);
2523
2524 if (qc->flags & ATA_QCFLAG_SINGLE)
2525 assert(qc->n_elem == 1);
2526
Jeff Garzik2c13b7c2005-11-14 14:14:16 -05002527 VPRINTK("unmapping %u sg elements\n", qc->n_elem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002529 /* if we padded the buffer out to 32-bit bound, and data
2530 * xfer direction is from-device, we must copy from the
2531 * pad buffer back into the supplied buffer
2532 */
2533 if (qc->pad_len && !(qc->tf.flags & ATA_TFLAG_WRITE))
2534 pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
2535
2536 if (qc->flags & ATA_QCFLAG_SG) {
Jeff Garzike1410f22005-11-14 14:06:26 -05002537 if (qc->n_elem)
2538 dma_unmap_sg(ap->host_set->dev, sg, qc->n_elem, dir);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002539 /* restore last sg */
2540 sg[qc->orig_n_elem - 1].length += qc->pad_len;
2541 if (pad_buf) {
2542 struct scatterlist *psg = &qc->pad_sgent;
2543 void *addr = kmap_atomic(psg->page, KM_IRQ0);
2544 memcpy(addr + psg->offset, pad_buf, qc->pad_len);
Mark Lorddfa15982005-12-12 23:19:28 -05002545 kunmap_atomic(addr, KM_IRQ0);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002546 }
2547 } else {
Jeff Garzike1410f22005-11-14 14:06:26 -05002548 if (sg_dma_len(&sg[0]) > 0)
2549 dma_unmap_single(ap->host_set->dev,
2550 sg_dma_address(&sg[0]), sg_dma_len(&sg[0]),
2551 dir);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002552 /* restore sg */
2553 sg->length += qc->pad_len;
2554 if (pad_buf)
2555 memcpy(qc->buf_virt + sg->length - qc->pad_len,
2556 pad_buf, qc->pad_len);
2557 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558
2559 qc->flags &= ~ATA_QCFLAG_DMAMAP;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002560 qc->__sg = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561}
2562
2563/**
2564 * ata_fill_sg - Fill PCI IDE PRD table
2565 * @qc: Metadata associated with taskfile to be transferred
2566 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04002567 * Fill PCI IDE PRD (scatter-gather) table with segments
2568 * associated with the current disk command.
2569 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570 * LOCKING:
Jeff Garzik780a87f2005-05-30 15:41:05 -04002571 * spin_lock_irqsave(host_set lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572 *
2573 */
2574static void ata_fill_sg(struct ata_queued_cmd *qc)
2575{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576 struct ata_port *ap = qc->ap;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002577 struct scatterlist *sg;
2578 unsigned int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002580 assert(qc->__sg != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581 assert(qc->n_elem > 0);
2582
2583 idx = 0;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002584 ata_for_each_sg(sg, qc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585 u32 addr, offset;
2586 u32 sg_len, len;
2587
2588 /* determine if physical DMA addr spans 64K boundary.
2589 * Note h/w doesn't support 64-bit, so we unconditionally
2590 * truncate dma_addr_t to u32.
2591 */
2592 addr = (u32) sg_dma_address(sg);
2593 sg_len = sg_dma_len(sg);
2594
2595 while (sg_len) {
2596 offset = addr & 0xffff;
2597 len = sg_len;
2598 if ((offset + sg_len) > 0x10000)
2599 len = 0x10000 - offset;
2600
2601 ap->prd[idx].addr = cpu_to_le32(addr);
2602 ap->prd[idx].flags_len = cpu_to_le32(len & 0xffff);
2603 VPRINTK("PRD[%u] = (0x%X, 0x%X)\n", idx, addr, len);
2604
2605 idx++;
2606 sg_len -= len;
2607 addr += len;
2608 }
2609 }
2610
2611 if (idx)
2612 ap->prd[idx - 1].flags_len |= cpu_to_le32(ATA_PRD_EOT);
2613}
2614/**
2615 * ata_check_atapi_dma - Check whether ATAPI DMA can be supported
2616 * @qc: Metadata associated with taskfile to check
2617 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04002618 * Allow low-level driver to filter ATA PACKET commands, returning
2619 * a status indicating whether or not it is OK to use DMA for the
2620 * supplied PACKET command.
2621 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002623 * spin_lock_irqsave(host_set lock)
2624 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625 * RETURNS: 0 when ATAPI DMA can be used
2626 * nonzero otherwise
2627 */
2628int ata_check_atapi_dma(struct ata_queued_cmd *qc)
2629{
2630 struct ata_port *ap = qc->ap;
2631 int rc = 0; /* Assume ATAPI DMA is OK by default */
2632
2633 if (ap->ops->check_atapi_dma)
2634 rc = ap->ops->check_atapi_dma(qc);
2635
2636 return rc;
2637}
2638/**
2639 * ata_qc_prep - Prepare taskfile for submission
2640 * @qc: Metadata associated with taskfile to be prepared
2641 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04002642 * Prepare ATA taskfile for submission.
2643 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644 * LOCKING:
2645 * spin_lock_irqsave(host_set lock)
2646 */
2647void ata_qc_prep(struct ata_queued_cmd *qc)
2648{
2649 if (!(qc->flags & ATA_QCFLAG_DMAMAP))
2650 return;
2651
2652 ata_fill_sg(qc);
2653}
2654
Jeff Garzik0cba6322005-05-30 19:49:12 -04002655/**
2656 * ata_sg_init_one - Associate command with memory buffer
2657 * @qc: Command to be associated
2658 * @buf: Memory buffer
2659 * @buflen: Length of memory buffer, in bytes.
2660 *
2661 * Initialize the data-related elements of queued_cmd @qc
2662 * to point to a single memory buffer, @buf of byte length @buflen.
2663 *
2664 * LOCKING:
2665 * spin_lock_irqsave(host_set lock)
2666 */
2667
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668void ata_sg_init_one(struct ata_queued_cmd *qc, void *buf, unsigned int buflen)
2669{
2670 struct scatterlist *sg;
2671
2672 qc->flags |= ATA_QCFLAG_SINGLE;
2673
2674 memset(&qc->sgent, 0, sizeof(qc->sgent));
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002675 qc->__sg = &qc->sgent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002676 qc->n_elem = 1;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002677 qc->orig_n_elem = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678 qc->buf_virt = buf;
2679
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002680 sg = qc->__sg;
Jeff Garzikf0612bb2005-10-30 01:58:18 -05002681 sg_init_one(sg, buf, buflen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002682}
2683
Jeff Garzik0cba6322005-05-30 19:49:12 -04002684/**
2685 * ata_sg_init - Associate command with scatter-gather table.
2686 * @qc: Command to be associated
2687 * @sg: Scatter-gather table.
2688 * @n_elem: Number of elements in s/g table.
2689 *
2690 * Initialize the data-related elements of queued_cmd @qc
2691 * to point to a scatter-gather table @sg, containing @n_elem
2692 * elements.
2693 *
2694 * LOCKING:
2695 * spin_lock_irqsave(host_set lock)
2696 */
2697
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698void ata_sg_init(struct ata_queued_cmd *qc, struct scatterlist *sg,
2699 unsigned int n_elem)
2700{
2701 qc->flags |= ATA_QCFLAG_SG;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002702 qc->__sg = sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703 qc->n_elem = n_elem;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002704 qc->orig_n_elem = n_elem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705}
2706
2707/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04002708 * ata_sg_setup_one - DMA-map the memory buffer associated with a command.
2709 * @qc: Command with memory buffer to be mapped.
2710 *
2711 * DMA-map the memory buffer associated with queued_cmd @qc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712 *
2713 * LOCKING:
2714 * spin_lock_irqsave(host_set lock)
2715 *
2716 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002717 * Zero on success, negative on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002718 */
2719
2720static int ata_sg_setup_one(struct ata_queued_cmd *qc)
2721{
2722 struct ata_port *ap = qc->ap;
2723 int dir = qc->dma_dir;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002724 struct scatterlist *sg = qc->__sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725 dma_addr_t dma_address;
2726
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002727 /* we must lengthen transfers to end on a 32-bit boundary */
2728 qc->pad_len = sg->length & 3;
2729 if (qc->pad_len) {
2730 void *pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
2731 struct scatterlist *psg = &qc->pad_sgent;
2732
2733 assert(qc->dev->class == ATA_DEV_ATAPI);
2734
2735 memset(pad_buf, 0, ATA_DMA_PAD_SZ);
2736
2737 if (qc->tf.flags & ATA_TFLAG_WRITE)
2738 memcpy(pad_buf, qc->buf_virt + sg->length - qc->pad_len,
2739 qc->pad_len);
2740
2741 sg_dma_address(psg) = ap->pad_dma + (qc->tag * ATA_DMA_PAD_SZ);
2742 sg_dma_len(psg) = ATA_DMA_PAD_SZ;
2743 /* trim sg */
2744 sg->length -= qc->pad_len;
2745
2746 DPRINTK("padding done, sg->length=%u pad_len=%u\n",
2747 sg->length, qc->pad_len);
2748 }
2749
Jeff Garzike1410f22005-11-14 14:06:26 -05002750 if (!sg->length) {
2751 sg_dma_address(sg) = 0;
2752 goto skip_map;
2753 }
2754
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755 dma_address = dma_map_single(ap->host_set->dev, qc->buf_virt,
Albert Lee32529e02005-05-26 03:49:42 -04002756 sg->length, dir);
Tejun Heo537a95d2005-11-05 14:29:01 -05002757 if (dma_mapping_error(dma_address)) {
2758 /* restore sg */
2759 sg->length += qc->pad_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760 return -1;
Tejun Heo537a95d2005-11-05 14:29:01 -05002761 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762
2763 sg_dma_address(sg) = dma_address;
Jeff Garzike1410f22005-11-14 14:06:26 -05002764skip_map:
Albert Lee32529e02005-05-26 03:49:42 -04002765 sg_dma_len(sg) = sg->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766
2767 DPRINTK("mapped buffer of %d bytes for %s\n", sg_dma_len(sg),
2768 qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
2769
2770 return 0;
2771}
2772
2773/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04002774 * ata_sg_setup - DMA-map the scatter-gather table associated with a command.
2775 * @qc: Command with scatter-gather table to be mapped.
2776 *
2777 * DMA-map the scatter-gather table associated with queued_cmd @qc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778 *
2779 * LOCKING:
2780 * spin_lock_irqsave(host_set lock)
2781 *
2782 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002783 * Zero on success, negative on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002784 *
2785 */
2786
2787static int ata_sg_setup(struct ata_queued_cmd *qc)
2788{
2789 struct ata_port *ap = qc->ap;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002790 struct scatterlist *sg = qc->__sg;
2791 struct scatterlist *lsg = &sg[qc->n_elem - 1];
Jeff Garzike1410f22005-11-14 14:06:26 -05002792 int n_elem, pre_n_elem, dir, trim_sg = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002793
2794 VPRINTK("ENTER, ata%u\n", ap->id);
2795 assert(qc->flags & ATA_QCFLAG_SG);
2796
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002797 /* we must lengthen transfers to end on a 32-bit boundary */
2798 qc->pad_len = lsg->length & 3;
2799 if (qc->pad_len) {
2800 void *pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
2801 struct scatterlist *psg = &qc->pad_sgent;
2802 unsigned int offset;
2803
2804 assert(qc->dev->class == ATA_DEV_ATAPI);
2805
2806 memset(pad_buf, 0, ATA_DMA_PAD_SZ);
2807
2808 /*
2809 * psg->page/offset are used to copy to-be-written
2810 * data in this function or read data in ata_sg_clean.
2811 */
2812 offset = lsg->offset + lsg->length - qc->pad_len;
2813 psg->page = nth_page(lsg->page, offset >> PAGE_SHIFT);
2814 psg->offset = offset_in_page(offset);
2815
2816 if (qc->tf.flags & ATA_TFLAG_WRITE) {
2817 void *addr = kmap_atomic(psg->page, KM_IRQ0);
2818 memcpy(pad_buf, addr + psg->offset, qc->pad_len);
Mark Lorddfa15982005-12-12 23:19:28 -05002819 kunmap_atomic(addr, KM_IRQ0);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002820 }
2821
2822 sg_dma_address(psg) = ap->pad_dma + (qc->tag * ATA_DMA_PAD_SZ);
2823 sg_dma_len(psg) = ATA_DMA_PAD_SZ;
2824 /* trim last sg */
2825 lsg->length -= qc->pad_len;
Jeff Garzike1410f22005-11-14 14:06:26 -05002826 if (lsg->length == 0)
2827 trim_sg = 1;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002828
2829 DPRINTK("padding done, sg[%d].length=%u pad_len=%u\n",
2830 qc->n_elem - 1, lsg->length, qc->pad_len);
2831 }
2832
Jeff Garzike1410f22005-11-14 14:06:26 -05002833 pre_n_elem = qc->n_elem;
2834 if (trim_sg && pre_n_elem)
2835 pre_n_elem--;
2836
2837 if (!pre_n_elem) {
2838 n_elem = 0;
2839 goto skip_map;
2840 }
2841
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842 dir = qc->dma_dir;
Jeff Garzike1410f22005-11-14 14:06:26 -05002843 n_elem = dma_map_sg(ap->host_set->dev, sg, pre_n_elem, dir);
Tejun Heo537a95d2005-11-05 14:29:01 -05002844 if (n_elem < 1) {
2845 /* restore last sg */
2846 lsg->length += qc->pad_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002847 return -1;
Tejun Heo537a95d2005-11-05 14:29:01 -05002848 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849
2850 DPRINTK("%d sg elements mapped\n", n_elem);
2851
Jeff Garzike1410f22005-11-14 14:06:26 -05002852skip_map:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853 qc->n_elem = n_elem;
2854
2855 return 0;
2856}
2857
2858/**
Tejun Heo40e8c822005-08-22 17:12:45 +09002859 * ata_poll_qc_complete - turn irq back on and finish qc
2860 * @qc: Command to complete
Randy Dunlap8e8b77d2005-11-01 21:29:27 -08002861 * @err_mask: ATA status register content
Tejun Heo40e8c822005-08-22 17:12:45 +09002862 *
2863 * LOCKING:
2864 * None. (grabs host lock)
2865 */
2866
Albert Leea22e2eb2005-12-05 15:38:02 +08002867void ata_poll_qc_complete(struct ata_queued_cmd *qc)
Tejun Heo40e8c822005-08-22 17:12:45 +09002868{
2869 struct ata_port *ap = qc->ap;
Jeff Garzikb8f61532005-08-25 22:01:20 -04002870 unsigned long flags;
Tejun Heo40e8c822005-08-22 17:12:45 +09002871
Jeff Garzikb8f61532005-08-25 22:01:20 -04002872 spin_lock_irqsave(&ap->host_set->lock, flags);
Tejun Heo40e8c822005-08-22 17:12:45 +09002873 ap->flags &= ~ATA_FLAG_NOINTR;
2874 ata_irq_on(ap);
Albert Leea22e2eb2005-12-05 15:38:02 +08002875 ata_qc_complete(qc);
Jeff Garzikb8f61532005-08-25 22:01:20 -04002876 spin_unlock_irqrestore(&ap->host_set->lock, flags);
Tejun Heo40e8c822005-08-22 17:12:45 +09002877}
2878
2879/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880 * ata_pio_poll -
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04002881 * @ap: the target ata_port
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882 *
2883 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002884 * None. (executing in kernel thread context)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885 *
2886 * RETURNS:
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04002887 * timeout value to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888 */
2889
2890static unsigned long ata_pio_poll(struct ata_port *ap)
2891{
Albert Leec14b8332005-12-05 15:36:08 +08002892 struct ata_queued_cmd *qc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893 u8 status;
Albert Lee14be71f2005-09-27 17:36:35 +08002894 unsigned int poll_state = HSM_ST_UNKNOWN;
2895 unsigned int reg_state = HSM_ST_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002896
Albert Leec14b8332005-12-05 15:36:08 +08002897 qc = ata_qc_from_tag(ap, ap->active_tag);
2898 assert(qc != NULL);
2899
Albert Lee14be71f2005-09-27 17:36:35 +08002900 switch (ap->hsm_task_state) {
2901 case HSM_ST:
2902 case HSM_ST_POLL:
2903 poll_state = HSM_ST_POLL;
2904 reg_state = HSM_ST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002905 break;
Albert Lee14be71f2005-09-27 17:36:35 +08002906 case HSM_ST_LAST:
2907 case HSM_ST_LAST_POLL:
2908 poll_state = HSM_ST_LAST_POLL;
2909 reg_state = HSM_ST_LAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002910 break;
2911 default:
2912 BUG();
2913 break;
2914 }
2915
2916 status = ata_chk_status(ap);
2917 if (status & ATA_BUSY) {
2918 if (time_after(jiffies, ap->pio_task_timeout)) {
Tejun Heo11a56d22006-01-23 13:09:36 +09002919 qc->err_mask |= AC_ERR_TIMEOUT;
Albert Lee7c398332005-11-09 13:03:30 +08002920 ap->hsm_task_state = HSM_ST_TMOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002921 return 0;
2922 }
Albert Lee14be71f2005-09-27 17:36:35 +08002923 ap->hsm_task_state = poll_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924 return ATA_SHORT_PAUSE;
2925 }
2926
Albert Lee14be71f2005-09-27 17:36:35 +08002927 ap->hsm_task_state = reg_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002928 return 0;
2929}
2930
2931/**
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04002932 * ata_pio_complete - check if drive is busy or idle
2933 * @ap: the target ata_port
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934 *
2935 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002936 * None. (executing in kernel thread context)
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04002937 *
2938 * RETURNS:
2939 * Non-zero if qc completed, zero otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002940 */
2941
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04002942static int ata_pio_complete (struct ata_port *ap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943{
2944 struct ata_queued_cmd *qc;
2945 u8 drv_stat;
2946
2947 /*
Alan Cox31433ea2005-08-26 15:56:47 +01002948 * This is purely heuristic. This is a fast path. Sometimes when
2949 * we enter, BSY will be cleared in a chk-status or two. If not,
2950 * the drive is probably seeking or something. Snooze for a couple
2951 * msecs, then chk-status again. If still busy, fall back to
Albert Lee14be71f2005-09-27 17:36:35 +08002952 * HSM_ST_POLL state.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002953 */
Albert Leefe79e682005-12-06 11:34:59 +08002954 drv_stat = ata_busy_wait(ap, ATA_BUSY, 10);
2955 if (drv_stat & ATA_BUSY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002956 msleep(2);
Albert Leefe79e682005-12-06 11:34:59 +08002957 drv_stat = ata_busy_wait(ap, ATA_BUSY, 10);
2958 if (drv_stat & ATA_BUSY) {
Albert Lee14be71f2005-09-27 17:36:35 +08002959 ap->hsm_task_state = HSM_ST_LAST_POLL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002960 ap->pio_task_timeout = jiffies + ATA_TMOUT_PIO;
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04002961 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002962 }
2963 }
2964
Albert Leec14b8332005-12-05 15:36:08 +08002965 qc = ata_qc_from_tag(ap, ap->active_tag);
2966 assert(qc != NULL);
2967
Linus Torvalds1da177e2005-04-16 15:20:36 -07002968 drv_stat = ata_wait_idle(ap);
2969 if (!ata_ok(drv_stat)) {
Albert Lee1c848982005-12-05 15:40:15 +08002970 qc->err_mask |= __ac_err_mask(drv_stat);
Albert Lee14be71f2005-09-27 17:36:35 +08002971 ap->hsm_task_state = HSM_ST_ERR;
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04002972 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973 }
2974
Albert Lee14be71f2005-09-27 17:36:35 +08002975 ap->hsm_task_state = HSM_ST_IDLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976
Albert Leea22e2eb2005-12-05 15:38:02 +08002977 assert(qc->err_mask == 0);
2978 ata_poll_qc_complete(qc);
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04002979
2980 /* another command may start at this point */
2981
2982 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002983}
2984
Edward Falk0baab862005-06-02 18:17:13 -04002985
2986/**
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04002987 * swap_buf_le16 - swap halves of 16-words in place
Edward Falk0baab862005-06-02 18:17:13 -04002988 * @buf: Buffer to swap
2989 * @buf_words: Number of 16-bit words in buffer.
2990 *
2991 * Swap halves of 16-bit words if needed to convert from
2992 * little-endian byte order to native cpu byte order, or
2993 * vice-versa.
2994 *
2995 * LOCKING:
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04002996 * Inherited from caller.
Edward Falk0baab862005-06-02 18:17:13 -04002997 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998void swap_buf_le16(u16 *buf, unsigned int buf_words)
2999{
3000#ifdef __BIG_ENDIAN
3001 unsigned int i;
3002
3003 for (i = 0; i < buf_words; i++)
3004 buf[i] = le16_to_cpu(buf[i]);
3005#endif /* __BIG_ENDIAN */
3006}
3007
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003008/**
3009 * ata_mmio_data_xfer - Transfer data by MMIO
3010 * @ap: port to read/write
3011 * @buf: data buffer
3012 * @buflen: buffer length
Jeff Garzik344baba2005-09-07 01:15:17 -04003013 * @write_data: read/write
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003014 *
3015 * Transfer data from/to the device data register by MMIO.
3016 *
3017 * LOCKING:
3018 * Inherited from caller.
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003019 */
3020
Linus Torvalds1da177e2005-04-16 15:20:36 -07003021static void ata_mmio_data_xfer(struct ata_port *ap, unsigned char *buf,
3022 unsigned int buflen, int write_data)
3023{
3024 unsigned int i;
3025 unsigned int words = buflen >> 1;
3026 u16 *buf16 = (u16 *) buf;
3027 void __iomem *mmio = (void __iomem *)ap->ioaddr.data_addr;
3028
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003029 /* Transfer multiple of 2 bytes */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003030 if (write_data) {
3031 for (i = 0; i < words; i++)
3032 writew(le16_to_cpu(buf16[i]), mmio);
3033 } else {
3034 for (i = 0; i < words; i++)
3035 buf16[i] = cpu_to_le16(readw(mmio));
3036 }
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003037
3038 /* Transfer trailing 1 byte, if any. */
3039 if (unlikely(buflen & 0x01)) {
3040 u16 align_buf[1] = { 0 };
3041 unsigned char *trailing_buf = buf + buflen - 1;
3042
3043 if (write_data) {
3044 memcpy(align_buf, trailing_buf, 1);
3045 writew(le16_to_cpu(align_buf[0]), mmio);
3046 } else {
3047 align_buf[0] = cpu_to_le16(readw(mmio));
3048 memcpy(trailing_buf, align_buf, 1);
3049 }
3050 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003051}
3052
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003053/**
3054 * ata_pio_data_xfer - Transfer data by PIO
3055 * @ap: port to read/write
3056 * @buf: data buffer
3057 * @buflen: buffer length
Jeff Garzik344baba2005-09-07 01:15:17 -04003058 * @write_data: read/write
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003059 *
3060 * Transfer data from/to the device data register by PIO.
3061 *
3062 * LOCKING:
3063 * Inherited from caller.
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003064 */
3065
Linus Torvalds1da177e2005-04-16 15:20:36 -07003066static void ata_pio_data_xfer(struct ata_port *ap, unsigned char *buf,
3067 unsigned int buflen, int write_data)
3068{
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003069 unsigned int words = buflen >> 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003070
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003071 /* Transfer multiple of 2 bytes */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003072 if (write_data)
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003073 outsw(ap->ioaddr.data_addr, buf, words);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003074 else
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003075 insw(ap->ioaddr.data_addr, buf, words);
3076
3077 /* Transfer trailing 1 byte, if any. */
3078 if (unlikely(buflen & 0x01)) {
3079 u16 align_buf[1] = { 0 };
3080 unsigned char *trailing_buf = buf + buflen - 1;
3081
3082 if (write_data) {
3083 memcpy(align_buf, trailing_buf, 1);
3084 outw(le16_to_cpu(align_buf[0]), ap->ioaddr.data_addr);
3085 } else {
3086 align_buf[0] = cpu_to_le16(inw(ap->ioaddr.data_addr));
3087 memcpy(trailing_buf, align_buf, 1);
3088 }
3089 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003090}
3091
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003092/**
3093 * ata_data_xfer - Transfer data from/to the data register.
3094 * @ap: port to read/write
3095 * @buf: data buffer
3096 * @buflen: buffer length
3097 * @do_write: read/write
3098 *
3099 * Transfer data from/to the device data register.
3100 *
3101 * LOCKING:
3102 * Inherited from caller.
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003103 */
3104
Linus Torvalds1da177e2005-04-16 15:20:36 -07003105static void ata_data_xfer(struct ata_port *ap, unsigned char *buf,
3106 unsigned int buflen, int do_write)
3107{
Alan Coxa1bd9e62006-01-17 20:53:50 +00003108 /* Make the crap hardware pay the costs not the good stuff */
3109 if (unlikely(ap->flags & ATA_FLAG_IRQ_MASK)) {
3110 unsigned long flags;
3111 local_irq_save(flags);
3112 if (ap->flags & ATA_FLAG_MMIO)
3113 ata_mmio_data_xfer(ap, buf, buflen, do_write);
3114 else
3115 ata_pio_data_xfer(ap, buf, buflen, do_write);
3116 local_irq_restore(flags);
3117 } else {
3118 if (ap->flags & ATA_FLAG_MMIO)
3119 ata_mmio_data_xfer(ap, buf, buflen, do_write);
3120 else
3121 ata_pio_data_xfer(ap, buf, buflen, do_write);
3122 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003123}
3124
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003125/**
3126 * ata_pio_sector - Transfer ATA_SECT_SIZE (512 bytes) of data.
3127 * @qc: Command on going
3128 *
3129 * Transfer ATA_SECT_SIZE of data from/to the ATA device.
3130 *
3131 * LOCKING:
3132 * Inherited from caller.
3133 */
3134
Linus Torvalds1da177e2005-04-16 15:20:36 -07003135static void ata_pio_sector(struct ata_queued_cmd *qc)
3136{
3137 int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003138 struct scatterlist *sg = qc->__sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003139 struct ata_port *ap = qc->ap;
3140 struct page *page;
3141 unsigned int offset;
3142 unsigned char *buf;
3143
3144 if (qc->cursect == (qc->nsect - 1))
Albert Lee14be71f2005-09-27 17:36:35 +08003145 ap->hsm_task_state = HSM_ST_LAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003146
3147 page = sg[qc->cursg].page;
3148 offset = sg[qc->cursg].offset + qc->cursg_ofs * ATA_SECT_SIZE;
3149
3150 /* get the current page and offset */
3151 page = nth_page(page, (offset >> PAGE_SHIFT));
3152 offset %= PAGE_SIZE;
3153
3154 buf = kmap(page) + offset;
3155
3156 qc->cursect++;
3157 qc->cursg_ofs++;
3158
Albert Lee32529e02005-05-26 03:49:42 -04003159 if ((qc->cursg_ofs * ATA_SECT_SIZE) == (&sg[qc->cursg])->length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003160 qc->cursg++;
3161 qc->cursg_ofs = 0;
3162 }
3163
3164 DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
3165
3166 /* do the actual data transfer */
3167 do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
3168 ata_data_xfer(ap, buf, ATA_SECT_SIZE, do_write);
3169
3170 kunmap(page);
3171}
3172
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003173/**
3174 * __atapi_pio_bytes - Transfer data from/to the ATAPI device.
3175 * @qc: Command on going
3176 * @bytes: number of bytes
3177 *
3178 * Transfer Transfer data from/to the ATAPI device.
3179 *
3180 * LOCKING:
3181 * Inherited from caller.
3182 *
3183 */
3184
Linus Torvalds1da177e2005-04-16 15:20:36 -07003185static void __atapi_pio_bytes(struct ata_queued_cmd *qc, unsigned int bytes)
3186{
3187 int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003188 struct scatterlist *sg = qc->__sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003189 struct ata_port *ap = qc->ap;
3190 struct page *page;
3191 unsigned char *buf;
3192 unsigned int offset, count;
3193
Albert Lee563a6e12005-08-12 14:17:50 +08003194 if (qc->curbytes + bytes >= qc->nbytes)
Albert Lee14be71f2005-09-27 17:36:35 +08003195 ap->hsm_task_state = HSM_ST_LAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003196
3197next_sg:
Albert Lee563a6e12005-08-12 14:17:50 +08003198 if (unlikely(qc->cursg >= qc->n_elem)) {
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003199 /*
Albert Lee563a6e12005-08-12 14:17:50 +08003200 * The end of qc->sg is reached and the device expects
3201 * more data to transfer. In order not to overrun qc->sg
3202 * and fulfill length specified in the byte count register,
3203 * - for read case, discard trailing data from the device
3204 * - for write case, padding zero data to the device
3205 */
3206 u16 pad_buf[1] = { 0 };
3207 unsigned int words = bytes >> 1;
3208 unsigned int i;
3209
3210 if (words) /* warning if bytes > 1 */
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003211 printk(KERN_WARNING "ata%u: %u bytes trailing data\n",
Albert Lee563a6e12005-08-12 14:17:50 +08003212 ap->id, bytes);
3213
3214 for (i = 0; i < words; i++)
3215 ata_data_xfer(ap, (unsigned char*)pad_buf, 2, do_write);
3216
Albert Lee14be71f2005-09-27 17:36:35 +08003217 ap->hsm_task_state = HSM_ST_LAST;
Albert Lee563a6e12005-08-12 14:17:50 +08003218 return;
3219 }
3220
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003221 sg = &qc->__sg[qc->cursg];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003222
Linus Torvalds1da177e2005-04-16 15:20:36 -07003223 page = sg->page;
3224 offset = sg->offset + qc->cursg_ofs;
3225
3226 /* get the current page and offset */
3227 page = nth_page(page, (offset >> PAGE_SHIFT));
3228 offset %= PAGE_SIZE;
3229
Albert Lee6952df02005-06-06 15:56:03 +08003230 /* don't overrun current sg */
Albert Lee32529e02005-05-26 03:49:42 -04003231 count = min(sg->length - qc->cursg_ofs, bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003232
3233 /* don't cross page boundaries */
3234 count = min(count, (unsigned int)PAGE_SIZE - offset);
3235
3236 buf = kmap(page) + offset;
3237
3238 bytes -= count;
3239 qc->curbytes += count;
3240 qc->cursg_ofs += count;
3241
Albert Lee32529e02005-05-26 03:49:42 -04003242 if (qc->cursg_ofs == sg->length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003243 qc->cursg++;
3244 qc->cursg_ofs = 0;
3245 }
3246
3247 DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
3248
3249 /* do the actual data transfer */
3250 ata_data_xfer(ap, buf, count, do_write);
3251
3252 kunmap(page);
3253
Albert Lee563a6e12005-08-12 14:17:50 +08003254 if (bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003255 goto next_sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003256}
3257
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003258/**
3259 * atapi_pio_bytes - Transfer data from/to the ATAPI device.
3260 * @qc: Command on going
3261 *
3262 * Transfer Transfer data from/to the ATAPI device.
3263 *
3264 * LOCKING:
3265 * Inherited from caller.
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003266 */
3267
Linus Torvalds1da177e2005-04-16 15:20:36 -07003268static void atapi_pio_bytes(struct ata_queued_cmd *qc)
3269{
3270 struct ata_port *ap = qc->ap;
3271 struct ata_device *dev = qc->dev;
3272 unsigned int ireason, bc_lo, bc_hi, bytes;
3273 int i_write, do_write = (qc->tf.flags & ATA_TFLAG_WRITE) ? 1 : 0;
3274
3275 ap->ops->tf_read(ap, &qc->tf);
3276 ireason = qc->tf.nsect;
3277 bc_lo = qc->tf.lbam;
3278 bc_hi = qc->tf.lbah;
3279 bytes = (bc_hi << 8) | bc_lo;
3280
3281 /* shall be cleared to zero, indicating xfer of data */
3282 if (ireason & (1 << 0))
3283 goto err_out;
3284
3285 /* make sure transfer direction matches expected */
3286 i_write = ((ireason & (1 << 1)) == 0) ? 1 : 0;
3287 if (do_write != i_write)
3288 goto err_out;
3289
3290 __atapi_pio_bytes(qc, bytes);
3291
3292 return;
3293
3294err_out:
3295 printk(KERN_INFO "ata%u: dev %u: ATAPI check failed\n",
3296 ap->id, dev->devno);
Tejun Heo11a56d22006-01-23 13:09:36 +09003297 qc->err_mask |= AC_ERR_HSM;
Albert Lee14be71f2005-09-27 17:36:35 +08003298 ap->hsm_task_state = HSM_ST_ERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003299}
3300
3301/**
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04003302 * ata_pio_block - start PIO on a block
3303 * @ap: the target ata_port
Linus Torvalds1da177e2005-04-16 15:20:36 -07003304 *
3305 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003306 * None. (executing in kernel thread context)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003307 */
3308
3309static void ata_pio_block(struct ata_port *ap)
3310{
3311 struct ata_queued_cmd *qc;
3312 u8 status;
3313
3314 /*
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04003315 * This is purely heuristic. This is a fast path.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003316 * Sometimes when we enter, BSY will be cleared in
3317 * a chk-status or two. If not, the drive is probably seeking
3318 * or something. Snooze for a couple msecs, then
3319 * chk-status again. If still busy, fall back to
Albert Lee14be71f2005-09-27 17:36:35 +08003320 * HSM_ST_POLL state.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003321 */
3322 status = ata_busy_wait(ap, ATA_BUSY, 5);
3323 if (status & ATA_BUSY) {
3324 msleep(2);
3325 status = ata_busy_wait(ap, ATA_BUSY, 10);
3326 if (status & ATA_BUSY) {
Albert Lee14be71f2005-09-27 17:36:35 +08003327 ap->hsm_task_state = HSM_ST_POLL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003328 ap->pio_task_timeout = jiffies + ATA_TMOUT_PIO;
3329 return;
3330 }
3331 }
3332
3333 qc = ata_qc_from_tag(ap, ap->active_tag);
3334 assert(qc != NULL);
3335
Albert Leefe79e682005-12-06 11:34:59 +08003336 /* check error */
3337 if (status & (ATA_ERR | ATA_DF)) {
3338 qc->err_mask |= AC_ERR_DEV;
3339 ap->hsm_task_state = HSM_ST_ERR;
3340 return;
3341 }
3342
3343 /* transfer data if any */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003344 if (is_atapi_taskfile(&qc->tf)) {
Albert Leefe79e682005-12-06 11:34:59 +08003345 /* DRQ=0 means no more data to transfer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003346 if ((status & ATA_DRQ) == 0) {
Albert Lee14be71f2005-09-27 17:36:35 +08003347 ap->hsm_task_state = HSM_ST_LAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003348 return;
3349 }
3350
3351 atapi_pio_bytes(qc);
3352 } else {
3353 /* handle BSY=0, DRQ=0 as error */
3354 if ((status & ATA_DRQ) == 0) {
Tejun Heo11a56d22006-01-23 13:09:36 +09003355 qc->err_mask |= AC_ERR_HSM;
Albert Lee14be71f2005-09-27 17:36:35 +08003356 ap->hsm_task_state = HSM_ST_ERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003357 return;
3358 }
3359
3360 ata_pio_sector(qc);
3361 }
3362}
3363
3364static void ata_pio_error(struct ata_port *ap)
3365{
3366 struct ata_queued_cmd *qc;
Jeff Garzika7dac442005-10-30 04:44:42 -05003367
3368 printk(KERN_WARNING "ata%u: PIO error\n", ap->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003369
3370 qc = ata_qc_from_tag(ap, ap->active_tag);
3371 assert(qc != NULL);
3372
Albert Lee1c848982005-12-05 15:40:15 +08003373 /* make sure qc->err_mask is available to
3374 * know what's wrong and recover
3375 */
3376 assert(qc->err_mask);
3377
Albert Lee14be71f2005-09-27 17:36:35 +08003378 ap->hsm_task_state = HSM_ST_IDLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003379
Albert Leea22e2eb2005-12-05 15:38:02 +08003380 ata_poll_qc_complete(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003381}
3382
3383static void ata_pio_task(void *_data)
3384{
3385 struct ata_port *ap = _data;
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003386 unsigned long timeout;
3387 int qc_completed;
3388
3389fsm_start:
3390 timeout = 0;
3391 qc_completed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003392
Albert Lee14be71f2005-09-27 17:36:35 +08003393 switch (ap->hsm_task_state) {
3394 case HSM_ST_IDLE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003395 return;
3396
Albert Lee14be71f2005-09-27 17:36:35 +08003397 case HSM_ST:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003398 ata_pio_block(ap);
3399 break;
3400
Albert Lee14be71f2005-09-27 17:36:35 +08003401 case HSM_ST_LAST:
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003402 qc_completed = ata_pio_complete(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003403 break;
3404
Albert Lee14be71f2005-09-27 17:36:35 +08003405 case HSM_ST_POLL:
3406 case HSM_ST_LAST_POLL:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003407 timeout = ata_pio_poll(ap);
3408 break;
3409
Albert Lee14be71f2005-09-27 17:36:35 +08003410 case HSM_ST_TMOUT:
3411 case HSM_ST_ERR:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003412 ata_pio_error(ap);
3413 return;
3414 }
3415
3416 if (timeout)
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003417 queue_delayed_work(ata_wq, &ap->pio_task, timeout);
3418 else if (!qc_completed)
3419 goto fsm_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003420}
3421
Linus Torvalds1da177e2005-04-16 15:20:36 -07003422/**
3423 * ata_qc_timeout - Handle timeout of queued command
3424 * @qc: Command that timed out
3425 *
3426 * Some part of the kernel (currently, only the SCSI layer)
3427 * has noticed that the active command on port @ap has not
3428 * completed after a specified length of time. Handle this
3429 * condition by disabling DMA (if necessary) and completing
3430 * transactions, with error if necessary.
3431 *
3432 * This also handles the case of the "lost interrupt", where
3433 * for some reason (possibly hardware bug, possibly driver bug)
3434 * an interrupt was not delivered to the driver, even though the
3435 * transaction completed successfully.
3436 *
3437 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003438 * Inherited from SCSI layer (none, can sleep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003439 */
3440
3441static void ata_qc_timeout(struct ata_queued_cmd *qc)
3442{
3443 struct ata_port *ap = qc->ap;
Jeff Garzikb8f61532005-08-25 22:01:20 -04003444 struct ata_host_set *host_set = ap->host_set;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003445 u8 host_stat = 0, drv_stat;
Jeff Garzikb8f61532005-08-25 22:01:20 -04003446 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003447
3448 DPRINTK("ENTER\n");
3449
Jeff Garzikb8f61532005-08-25 22:01:20 -04003450 spin_lock_irqsave(&host_set->lock, flags);
3451
Linus Torvalds1da177e2005-04-16 15:20:36 -07003452 switch (qc->tf.protocol) {
3453
3454 case ATA_PROT_DMA:
3455 case ATA_PROT_ATAPI_DMA:
3456 host_stat = ap->ops->bmdma_status(ap);
3457
3458 /* before we do anything else, clear DMA-Start bit */
Alan Coxb73fc892005-08-26 16:03:19 +01003459 ap->ops->bmdma_stop(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003460
3461 /* fall through */
3462
3463 default:
3464 ata_altstatus(ap);
3465 drv_stat = ata_chk_status(ap);
3466
3467 /* ack bmdma irq events */
3468 ap->ops->irq_clear(ap);
3469
3470 printk(KERN_ERR "ata%u: command 0x%x timeout, stat 0x%x host_stat 0x%x\n",
3471 ap->id, qc->tf.command, drv_stat, host_stat);
3472
3473 /* complete taskfile transaction */
Albert Leea22e2eb2005-12-05 15:38:02 +08003474 qc->err_mask |= ac_err_mask(drv_stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003475 break;
3476 }
Jeff Garzikb8f61532005-08-25 22:01:20 -04003477
3478 spin_unlock_irqrestore(&host_set->lock, flags);
3479
Tejun Heoa72ec4c2006-01-23 13:09:37 +09003480 ata_eh_qc_complete(qc);
3481
Linus Torvalds1da177e2005-04-16 15:20:36 -07003482 DPRINTK("EXIT\n");
3483}
3484
3485/**
3486 * ata_eng_timeout - Handle timeout of queued command
3487 * @ap: Port on which timed-out command is active
3488 *
3489 * Some part of the kernel (currently, only the SCSI layer)
3490 * has noticed that the active command on port @ap has not
3491 * completed after a specified length of time. Handle this
3492 * condition by disabling DMA (if necessary) and completing
3493 * transactions, with error if necessary.
3494 *
3495 * This also handles the case of the "lost interrupt", where
3496 * for some reason (possibly hardware bug, possibly driver bug)
3497 * an interrupt was not delivered to the driver, even though the
3498 * transaction completed successfully.
3499 *
3500 * LOCKING:
3501 * Inherited from SCSI layer (none, can sleep)
3502 */
3503
3504void ata_eng_timeout(struct ata_port *ap)
3505{
3506 struct ata_queued_cmd *qc;
3507
3508 DPRINTK("ENTER\n");
3509
3510 qc = ata_qc_from_tag(ap, ap->active_tag);
Jeff Garzike12669e2005-10-05 18:39:23 -04003511 if (qc)
3512 ata_qc_timeout(qc);
3513 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003514 printk(KERN_ERR "ata%u: BUG: timeout without command\n",
3515 ap->id);
3516 goto out;
3517 }
3518
Linus Torvalds1da177e2005-04-16 15:20:36 -07003519out:
3520 DPRINTK("EXIT\n");
3521}
3522
3523/**
3524 * ata_qc_new - Request an available ATA command, for queueing
3525 * @ap: Port associated with device @dev
3526 * @dev: Device from whom we request an available command structure
3527 *
3528 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003529 * None.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003530 */
3531
3532static struct ata_queued_cmd *ata_qc_new(struct ata_port *ap)
3533{
3534 struct ata_queued_cmd *qc = NULL;
3535 unsigned int i;
3536
3537 for (i = 0; i < ATA_MAX_QUEUE; i++)
3538 if (!test_and_set_bit(i, &ap->qactive)) {
3539 qc = ata_qc_from_tag(ap, i);
3540 break;
3541 }
3542
3543 if (qc)
3544 qc->tag = i;
3545
3546 return qc;
3547}
3548
3549/**
3550 * ata_qc_new_init - Request an available ATA command, and initialize it
3551 * @ap: Port associated with device @dev
3552 * @dev: Device from whom we request an available command structure
3553 *
3554 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003555 * None.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003556 */
3557
3558struct ata_queued_cmd *ata_qc_new_init(struct ata_port *ap,
3559 struct ata_device *dev)
3560{
3561 struct ata_queued_cmd *qc;
3562
3563 qc = ata_qc_new(ap);
3564 if (qc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003565 qc->scsicmd = NULL;
3566 qc->ap = ap;
3567 qc->dev = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003568
Jeff Garzik2c13b7c2005-11-14 14:14:16 -05003569 ata_qc_reinit(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003570 }
3571
3572 return qc;
3573}
3574
Linus Torvalds1da177e2005-04-16 15:20:36 -07003575/**
3576 * ata_qc_free - free unused ata_queued_cmd
3577 * @qc: Command to complete
3578 *
3579 * Designed to free unused ata_queued_cmd object
3580 * in case something prevents using it.
3581 *
3582 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003583 * spin_lock_irqsave(host_set lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003584 */
3585void ata_qc_free(struct ata_queued_cmd *qc)
3586{
Tejun Heo4ba946e2006-01-23 13:09:36 +09003587 struct ata_port *ap = qc->ap;
3588 unsigned int tag;
3589
Linus Torvalds1da177e2005-04-16 15:20:36 -07003590 assert(qc != NULL); /* ata_qc_from_tag _might_ return NULL */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003591
Tejun Heo4ba946e2006-01-23 13:09:36 +09003592 qc->flags = 0;
3593 tag = qc->tag;
3594 if (likely(ata_tag_valid(tag))) {
3595 if (tag == ap->active_tag)
3596 ap->active_tag = ATA_TAG_POISON;
3597 qc->tag = ATA_TAG_POISON;
3598 clear_bit(tag, &ap->qactive);
3599 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003600}
3601
3602/**
3603 * ata_qc_complete - Complete an active ATA command
3604 * @qc: Command to complete
Randy Dunlap8e8b77d2005-11-01 21:29:27 -08003605 * @err_mask: ATA Status register contents
Jeff Garzik0cba6322005-05-30 19:49:12 -04003606 *
3607 * Indicate to the mid and upper layers that an ATA
3608 * command has completed, with either an ok or not-ok status.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003609 *
3610 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003611 * spin_lock_irqsave(host_set lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003612 */
3613
Albert Leea22e2eb2005-12-05 15:38:02 +08003614void ata_qc_complete(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003615{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003616 assert(qc != NULL); /* ata_qc_from_tag _might_ return NULL */
3617 assert(qc->flags & ATA_QCFLAG_ACTIVE);
3618
3619 if (likely(qc->flags & ATA_QCFLAG_DMAMAP))
3620 ata_sg_clean(qc);
3621
Albert Lee3f3791d2005-08-16 14:25:38 +08003622 /* atapi: mark qc as inactive to prevent the interrupt handler
3623 * from completing the command twice later, before the error handler
3624 * is called. (when rc != 0 and atapi request sense is needed)
3625 */
3626 qc->flags &= ~ATA_QCFLAG_ACTIVE;
3627
Linus Torvalds1da177e2005-04-16 15:20:36 -07003628 /* call completion callback */
Tejun Heo77853bf2006-01-23 13:09:36 +09003629 qc->complete_fn(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003630}
3631
3632static inline int ata_should_dma_map(struct ata_queued_cmd *qc)
3633{
3634 struct ata_port *ap = qc->ap;
3635
3636 switch (qc->tf.protocol) {
3637 case ATA_PROT_DMA:
3638 case ATA_PROT_ATAPI_DMA:
3639 return 1;
3640
3641 case ATA_PROT_ATAPI:
3642 case ATA_PROT_PIO:
3643 case ATA_PROT_PIO_MULT:
3644 if (ap->flags & ATA_FLAG_PIO_DMA)
3645 return 1;
3646
3647 /* fall through */
3648
3649 default:
3650 return 0;
3651 }
3652
3653 /* never reached */
3654}
3655
3656/**
3657 * ata_qc_issue - issue taskfile to device
3658 * @qc: command to issue to device
3659 *
3660 * Prepare an ATA command to submission to device.
3661 * This includes mapping the data into a DMA-able
3662 * area, filling in the S/G table, and finally
3663 * writing the taskfile to hardware, starting the command.
3664 *
3665 * LOCKING:
3666 * spin_lock_irqsave(host_set lock)
3667 *
3668 * RETURNS:
Tejun Heo9a3d9eb2006-01-23 13:09:36 +09003669 * Zero on success, AC_ERR_* mask on failure
Linus Torvalds1da177e2005-04-16 15:20:36 -07003670 */
3671
Tejun Heo9a3d9eb2006-01-23 13:09:36 +09003672unsigned int ata_qc_issue(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003673{
3674 struct ata_port *ap = qc->ap;
3675
3676 if (ata_should_dma_map(qc)) {
3677 if (qc->flags & ATA_QCFLAG_SG) {
3678 if (ata_sg_setup(qc))
Tejun Heo8e436af2006-01-23 13:09:36 +09003679 goto sg_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003680 } else if (qc->flags & ATA_QCFLAG_SINGLE) {
3681 if (ata_sg_setup_one(qc))
Tejun Heo8e436af2006-01-23 13:09:36 +09003682 goto sg_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003683 }
3684 } else {
3685 qc->flags &= ~ATA_QCFLAG_DMAMAP;
3686 }
3687
3688 ap->ops->qc_prep(qc);
3689
3690 qc->ap->active_tag = qc->tag;
3691 qc->flags |= ATA_QCFLAG_ACTIVE;
3692
3693 return ap->ops->qc_issue(qc);
3694
Tejun Heo8e436af2006-01-23 13:09:36 +09003695sg_err:
3696 qc->flags &= ~ATA_QCFLAG_DMAMAP;
Tejun Heo9a3d9eb2006-01-23 13:09:36 +09003697 return AC_ERR_SYSTEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003698}
3699
Edward Falk0baab862005-06-02 18:17:13 -04003700
Linus Torvalds1da177e2005-04-16 15:20:36 -07003701/**
3702 * ata_qc_issue_prot - issue taskfile to device in proto-dependent manner
3703 * @qc: command to issue to device
3704 *
3705 * Using various libata functions and hooks, this function
3706 * starts an ATA command. ATA commands are grouped into
3707 * classes called "protocols", and issuing each type of protocol
3708 * is slightly different.
3709 *
Edward Falk0baab862005-06-02 18:17:13 -04003710 * May be used as the qc_issue() entry in ata_port_operations.
3711 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003712 * LOCKING:
3713 * spin_lock_irqsave(host_set lock)
3714 *
3715 * RETURNS:
Tejun Heo9a3d9eb2006-01-23 13:09:36 +09003716 * Zero on success, AC_ERR_* mask on failure
Linus Torvalds1da177e2005-04-16 15:20:36 -07003717 */
3718
Tejun Heo9a3d9eb2006-01-23 13:09:36 +09003719unsigned int ata_qc_issue_prot(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003720{
3721 struct ata_port *ap = qc->ap;
3722
3723 ata_dev_select(ap, qc->dev->devno, 1, 0);
3724
3725 switch (qc->tf.protocol) {
3726 case ATA_PROT_NODATA:
Jeff Garzike5338252005-10-30 21:37:17 -05003727 ata_tf_to_host(ap, &qc->tf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003728 break;
3729
3730 case ATA_PROT_DMA:
3731 ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
3732 ap->ops->bmdma_setup(qc); /* set up bmdma */
3733 ap->ops->bmdma_start(qc); /* initiate bmdma */
3734 break;
3735
3736 case ATA_PROT_PIO: /* load tf registers, initiate polling pio */
3737 ata_qc_set_polling(qc);
Jeff Garzike5338252005-10-30 21:37:17 -05003738 ata_tf_to_host(ap, &qc->tf);
Albert Lee14be71f2005-09-27 17:36:35 +08003739 ap->hsm_task_state = HSM_ST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003740 queue_work(ata_wq, &ap->pio_task);
3741 break;
3742
3743 case ATA_PROT_ATAPI:
3744 ata_qc_set_polling(qc);
Jeff Garzike5338252005-10-30 21:37:17 -05003745 ata_tf_to_host(ap, &qc->tf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003746 queue_work(ata_wq, &ap->packet_task);
3747 break;
3748
3749 case ATA_PROT_ATAPI_NODATA:
Tejun Heoc1389502005-08-22 14:59:24 +09003750 ap->flags |= ATA_FLAG_NOINTR;
Jeff Garzike5338252005-10-30 21:37:17 -05003751 ata_tf_to_host(ap, &qc->tf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003752 queue_work(ata_wq, &ap->packet_task);
3753 break;
3754
3755 case ATA_PROT_ATAPI_DMA:
Tejun Heoc1389502005-08-22 14:59:24 +09003756 ap->flags |= ATA_FLAG_NOINTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003757 ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
3758 ap->ops->bmdma_setup(qc); /* set up bmdma */
3759 queue_work(ata_wq, &ap->packet_task);
3760 break;
3761
3762 default:
3763 WARN_ON(1);
Tejun Heo9a3d9eb2006-01-23 13:09:36 +09003764 return AC_ERR_SYSTEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003765 }
3766
3767 return 0;
3768}
3769
3770/**
Edward Falk0baab862005-06-02 18:17:13 -04003771 * ata_bmdma_setup_mmio - Set up PCI IDE BMDMA transaction
Linus Torvalds1da177e2005-04-16 15:20:36 -07003772 * @qc: Info associated with this ATA transaction.
3773 *
3774 * LOCKING:
3775 * spin_lock_irqsave(host_set lock)
3776 */
3777
3778static void ata_bmdma_setup_mmio (struct ata_queued_cmd *qc)
3779{
3780 struct ata_port *ap = qc->ap;
3781 unsigned int rw = (qc->tf.flags & ATA_TFLAG_WRITE);
3782 u8 dmactl;
3783 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
3784
3785 /* load PRD table addr. */
3786 mb(); /* make sure PRD table writes are visible to controller */
3787 writel(ap->prd_dma, mmio + ATA_DMA_TABLE_OFS);
3788
3789 /* specify data direction, triple-check start bit is clear */
3790 dmactl = readb(mmio + ATA_DMA_CMD);
3791 dmactl &= ~(ATA_DMA_WR | ATA_DMA_START);
3792 if (!rw)
3793 dmactl |= ATA_DMA_WR;
3794 writeb(dmactl, mmio + ATA_DMA_CMD);
3795
3796 /* issue r/w command */
3797 ap->ops->exec_command(ap, &qc->tf);
3798}
3799
3800/**
Alan Coxb73fc892005-08-26 16:03:19 +01003801 * ata_bmdma_start_mmio - Start a PCI IDE BMDMA transaction
Linus Torvalds1da177e2005-04-16 15:20:36 -07003802 * @qc: Info associated with this ATA transaction.
3803 *
3804 * LOCKING:
3805 * spin_lock_irqsave(host_set lock)
3806 */
3807
3808static void ata_bmdma_start_mmio (struct ata_queued_cmd *qc)
3809{
3810 struct ata_port *ap = qc->ap;
3811 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
3812 u8 dmactl;
3813
3814 /* start host DMA transaction */
3815 dmactl = readb(mmio + ATA_DMA_CMD);
3816 writeb(dmactl | ATA_DMA_START, mmio + ATA_DMA_CMD);
3817
3818 /* Strictly, one may wish to issue a readb() here, to
3819 * flush the mmio write. However, control also passes
3820 * to the hardware at this point, and it will interrupt
3821 * us when we are to resume control. So, in effect,
3822 * we don't care when the mmio write flushes.
3823 * Further, a read of the DMA status register _immediately_
3824 * following the write may not be what certain flaky hardware
3825 * is expected, so I think it is best to not add a readb()
3826 * without first all the MMIO ATA cards/mobos.
3827 * Or maybe I'm just being paranoid.
3828 */
3829}
3830
3831/**
3832 * ata_bmdma_setup_pio - Set up PCI IDE BMDMA transaction (PIO)
3833 * @qc: Info associated with this ATA transaction.
3834 *
3835 * LOCKING:
3836 * spin_lock_irqsave(host_set lock)
3837 */
3838
3839static void ata_bmdma_setup_pio (struct ata_queued_cmd *qc)
3840{
3841 struct ata_port *ap = qc->ap;
3842 unsigned int rw = (qc->tf.flags & ATA_TFLAG_WRITE);
3843 u8 dmactl;
3844
3845 /* load PRD table addr. */
3846 outl(ap->prd_dma, ap->ioaddr.bmdma_addr + ATA_DMA_TABLE_OFS);
3847
3848 /* specify data direction, triple-check start bit is clear */
3849 dmactl = inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3850 dmactl &= ~(ATA_DMA_WR | ATA_DMA_START);
3851 if (!rw)
3852 dmactl |= ATA_DMA_WR;
3853 outb(dmactl, ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3854
3855 /* issue r/w command */
3856 ap->ops->exec_command(ap, &qc->tf);
3857}
3858
3859/**
3860 * ata_bmdma_start_pio - Start a PCI IDE BMDMA transaction (PIO)
3861 * @qc: Info associated with this ATA transaction.
3862 *
3863 * LOCKING:
3864 * spin_lock_irqsave(host_set lock)
3865 */
3866
3867static void ata_bmdma_start_pio (struct ata_queued_cmd *qc)
3868{
3869 struct ata_port *ap = qc->ap;
3870 u8 dmactl;
3871
3872 /* start host DMA transaction */
3873 dmactl = inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3874 outb(dmactl | ATA_DMA_START,
3875 ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3876}
3877
Edward Falk0baab862005-06-02 18:17:13 -04003878
3879/**
3880 * ata_bmdma_start - Start a PCI IDE BMDMA transaction
3881 * @qc: Info associated with this ATA transaction.
3882 *
3883 * Writes the ATA_DMA_START flag to the DMA command register.
3884 *
3885 * May be used as the bmdma_start() entry in ata_port_operations.
3886 *
3887 * LOCKING:
3888 * spin_lock_irqsave(host_set lock)
3889 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003890void ata_bmdma_start(struct ata_queued_cmd *qc)
3891{
3892 if (qc->ap->flags & ATA_FLAG_MMIO)
3893 ata_bmdma_start_mmio(qc);
3894 else
3895 ata_bmdma_start_pio(qc);
3896}
3897
Edward Falk0baab862005-06-02 18:17:13 -04003898
3899/**
3900 * ata_bmdma_setup - Set up PCI IDE BMDMA transaction
3901 * @qc: Info associated with this ATA transaction.
3902 *
3903 * Writes address of PRD table to device's PRD Table Address
3904 * register, sets the DMA control register, and calls
3905 * ops->exec_command() to start the transfer.
3906 *
3907 * May be used as the bmdma_setup() entry in ata_port_operations.
3908 *
3909 * LOCKING:
3910 * spin_lock_irqsave(host_set lock)
3911 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003912void ata_bmdma_setup(struct ata_queued_cmd *qc)
3913{
3914 if (qc->ap->flags & ATA_FLAG_MMIO)
3915 ata_bmdma_setup_mmio(qc);
3916 else
3917 ata_bmdma_setup_pio(qc);
3918}
3919
Edward Falk0baab862005-06-02 18:17:13 -04003920
3921/**
3922 * ata_bmdma_irq_clear - Clear PCI IDE BMDMA interrupt.
Jeff Garzikdecc6d02005-06-02 18:42:33 -04003923 * @ap: Port associated with this ATA transaction.
Edward Falk0baab862005-06-02 18:17:13 -04003924 *
3925 * Clear interrupt and error flags in DMA status register.
3926 *
3927 * May be used as the irq_clear() entry in ata_port_operations.
3928 *
3929 * LOCKING:
3930 * spin_lock_irqsave(host_set lock)
3931 */
3932
Linus Torvalds1da177e2005-04-16 15:20:36 -07003933void ata_bmdma_irq_clear(struct ata_port *ap)
3934{
3935 if (ap->flags & ATA_FLAG_MMIO) {
3936 void __iomem *mmio = ((void __iomem *) ap->ioaddr.bmdma_addr) + ATA_DMA_STATUS;
3937 writeb(readb(mmio), mmio);
3938 } else {
3939 unsigned long addr = ap->ioaddr.bmdma_addr + ATA_DMA_STATUS;
3940 outb(inb(addr), addr);
3941 }
3942
3943}
3944
Edward Falk0baab862005-06-02 18:17:13 -04003945
3946/**
3947 * ata_bmdma_status - Read PCI IDE BMDMA status
Jeff Garzikdecc6d02005-06-02 18:42:33 -04003948 * @ap: Port associated with this ATA transaction.
Edward Falk0baab862005-06-02 18:17:13 -04003949 *
3950 * Read and return BMDMA status register.
3951 *
3952 * May be used as the bmdma_status() entry in ata_port_operations.
3953 *
3954 * LOCKING:
3955 * spin_lock_irqsave(host_set lock)
3956 */
3957
Linus Torvalds1da177e2005-04-16 15:20:36 -07003958u8 ata_bmdma_status(struct ata_port *ap)
3959{
3960 u8 host_stat;
3961 if (ap->flags & ATA_FLAG_MMIO) {
3962 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
3963 host_stat = readb(mmio + ATA_DMA_STATUS);
3964 } else
Albert Leeee500aa2005-09-27 17:34:38 +08003965 host_stat = inb(ap->ioaddr.bmdma_addr + ATA_DMA_STATUS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003966 return host_stat;
3967}
3968
Edward Falk0baab862005-06-02 18:17:13 -04003969
3970/**
3971 * ata_bmdma_stop - Stop PCI IDE BMDMA transfer
Alan Coxb73fc892005-08-26 16:03:19 +01003972 * @qc: Command we are ending DMA for
Edward Falk0baab862005-06-02 18:17:13 -04003973 *
3974 * Clears the ATA_DMA_START flag in the dma control register
3975 *
3976 * May be used as the bmdma_stop() entry in ata_port_operations.
3977 *
3978 * LOCKING:
3979 * spin_lock_irqsave(host_set lock)
3980 */
3981
Alan Coxb73fc892005-08-26 16:03:19 +01003982void ata_bmdma_stop(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003983{
Alan Coxb73fc892005-08-26 16:03:19 +01003984 struct ata_port *ap = qc->ap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003985 if (ap->flags & ATA_FLAG_MMIO) {
3986 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
3987
3988 /* clear start/stop bit */
3989 writeb(readb(mmio + ATA_DMA_CMD) & ~ATA_DMA_START,
3990 mmio + ATA_DMA_CMD);
3991 } else {
3992 /* clear start/stop bit */
3993 outb(inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD) & ~ATA_DMA_START,
3994 ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3995 }
3996
3997 /* one-PIO-cycle guaranteed wait, per spec, for HDMA1:0 transition */
3998 ata_altstatus(ap); /* dummy read */
3999}
4000
4001/**
4002 * ata_host_intr - Handle host interrupt for given (port, task)
4003 * @ap: Port on which interrupt arrived (possibly...)
4004 * @qc: Taskfile currently active in engine
4005 *
4006 * Handle host interrupt for given queued command. Currently,
4007 * only DMA interrupts are handled. All other commands are
4008 * handled via polling with interrupts disabled (nIEN bit).
4009 *
4010 * LOCKING:
4011 * spin_lock_irqsave(host_set lock)
4012 *
4013 * RETURNS:
4014 * One if interrupt was handled, zero if not (shared irq).
4015 */
4016
4017inline unsigned int ata_host_intr (struct ata_port *ap,
4018 struct ata_queued_cmd *qc)
4019{
4020 u8 status, host_stat;
4021
4022 switch (qc->tf.protocol) {
4023
4024 case ATA_PROT_DMA:
4025 case ATA_PROT_ATAPI_DMA:
4026 case ATA_PROT_ATAPI:
4027 /* check status of DMA engine */
4028 host_stat = ap->ops->bmdma_status(ap);
4029 VPRINTK("ata%u: host_stat 0x%X\n", ap->id, host_stat);
4030
4031 /* if it's not our irq... */
4032 if (!(host_stat & ATA_DMA_INTR))
4033 goto idle_irq;
4034
4035 /* before we do anything else, clear DMA-Start bit */
Alan Coxb73fc892005-08-26 16:03:19 +01004036 ap->ops->bmdma_stop(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004037
4038 /* fall through */
4039
4040 case ATA_PROT_ATAPI_NODATA:
4041 case ATA_PROT_NODATA:
4042 /* check altstatus */
4043 status = ata_altstatus(ap);
4044 if (status & ATA_BUSY)
4045 goto idle_irq;
4046
4047 /* check main status, clearing INTRQ */
4048 status = ata_chk_status(ap);
4049 if (unlikely(status & ATA_BUSY))
4050 goto idle_irq;
4051 DPRINTK("ata%u: protocol %d (dev_stat 0x%X)\n",
4052 ap->id, qc->tf.protocol, status);
4053
4054 /* ack bmdma irq events */
4055 ap->ops->irq_clear(ap);
4056
4057 /* complete taskfile transaction */
Albert Leea22e2eb2005-12-05 15:38:02 +08004058 qc->err_mask |= ac_err_mask(status);
4059 ata_qc_complete(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004060 break;
4061
4062 default:
4063 goto idle_irq;
4064 }
4065
4066 return 1; /* irq handled */
4067
4068idle_irq:
4069 ap->stats.idle_irq++;
4070
4071#ifdef ATA_IRQ_TRAP
4072 if ((ap->stats.idle_irq % 1000) == 0) {
4073 handled = 1;
4074 ata_irq_ack(ap, 0); /* debug trap */
4075 printk(KERN_WARNING "ata%d: irq trap\n", ap->id);
4076 }
4077#endif
4078 return 0; /* irq not handled */
4079}
4080
4081/**
4082 * ata_interrupt - Default ATA host interrupt handler
Jeff Garzik0cba6322005-05-30 19:49:12 -04004083 * @irq: irq line (unused)
4084 * @dev_instance: pointer to our ata_host_set information structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07004085 * @regs: unused
4086 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04004087 * Default interrupt handler for PCI IDE devices. Calls
4088 * ata_host_intr() for each port that is not disabled.
4089 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004090 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004091 * Obtains host_set lock during operation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004092 *
4093 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004094 * IRQ_NONE or IRQ_HANDLED.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004095 */
4096
4097irqreturn_t ata_interrupt (int irq, void *dev_instance, struct pt_regs *regs)
4098{
4099 struct ata_host_set *host_set = dev_instance;
4100 unsigned int i;
4101 unsigned int handled = 0;
4102 unsigned long flags;
4103
4104 /* TODO: make _irqsave conditional on x86 PCI IDE legacy mode */
4105 spin_lock_irqsave(&host_set->lock, flags);
4106
4107 for (i = 0; i < host_set->n_ports; i++) {
4108 struct ata_port *ap;
4109
4110 ap = host_set->ports[i];
Tejun Heoc1389502005-08-22 14:59:24 +09004111 if (ap &&
4112 !(ap->flags & (ATA_FLAG_PORT_DISABLED | ATA_FLAG_NOINTR))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004113 struct ata_queued_cmd *qc;
4114
4115 qc = ata_qc_from_tag(ap, ap->active_tag);
Albert Lee21b1ed72005-04-29 17:34:59 +08004116 if (qc && (!(qc->tf.ctl & ATA_NIEN)) &&
4117 (qc->flags & ATA_QCFLAG_ACTIVE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004118 handled |= ata_host_intr(ap, qc);
4119 }
4120 }
4121
4122 spin_unlock_irqrestore(&host_set->lock, flags);
4123
4124 return IRQ_RETVAL(handled);
4125}
4126
4127/**
4128 * atapi_packet_task - Write CDB bytes to hardware
4129 * @_data: Port to which ATAPI device is attached.
4130 *
4131 * When device has indicated its readiness to accept
4132 * a CDB, this function is called. Send the CDB.
4133 * If DMA is to be performed, exit immediately.
4134 * Otherwise, we are in polling mode, so poll
4135 * status under operation succeeds or fails.
4136 *
4137 * LOCKING:
4138 * Kernel thread context (may sleep)
4139 */
4140
4141static void atapi_packet_task(void *_data)
4142{
4143 struct ata_port *ap = _data;
4144 struct ata_queued_cmd *qc;
4145 u8 status;
4146
4147 qc = ata_qc_from_tag(ap, ap->active_tag);
4148 assert(qc != NULL);
4149 assert(qc->flags & ATA_QCFLAG_ACTIVE);
4150
4151 /* sleep-wait for BSY to clear */
4152 DPRINTK("busy wait\n");
Albert Leed8fe4522005-12-05 15:42:17 +08004153 if (ata_busy_sleep(ap, ATA_TMOUT_CDB_QUICK, ATA_TMOUT_CDB)) {
Tejun Heo11a56d22006-01-23 13:09:36 +09004154 qc->err_mask |= AC_ERR_TIMEOUT;
Albert Leed8fe4522005-12-05 15:42:17 +08004155 goto err_out;
4156 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004157
4158 /* make sure DRQ is set */
4159 status = ata_chk_status(ap);
Albert Leed8fe4522005-12-05 15:42:17 +08004160 if ((status & (ATA_BUSY | ATA_DRQ)) != ATA_DRQ) {
Tejun Heo11a56d22006-01-23 13:09:36 +09004161 qc->err_mask |= AC_ERR_HSM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004162 goto err_out;
Albert Leed8fe4522005-12-05 15:42:17 +08004163 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004164
4165 /* send SCSI cdb */
4166 DPRINTK("send cdb\n");
4167 assert(ap->cdb_len >= 12);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004168
Tejun Heoc1389502005-08-22 14:59:24 +09004169 if (qc->tf.protocol == ATA_PROT_ATAPI_DMA ||
4170 qc->tf.protocol == ATA_PROT_ATAPI_NODATA) {
4171 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004172
Tejun Heoc1389502005-08-22 14:59:24 +09004173 /* Once we're done issuing command and kicking bmdma,
4174 * irq handler takes over. To not lose irq, we need
4175 * to clear NOINTR flag before sending cdb, but
4176 * interrupt handler shouldn't be invoked before we're
4177 * finished. Hence, the following locking.
4178 */
4179 spin_lock_irqsave(&ap->host_set->lock, flags);
4180 ap->flags &= ~ATA_FLAG_NOINTR;
4181 ata_data_xfer(ap, qc->cdb, ap->cdb_len, 1);
4182 if (qc->tf.protocol == ATA_PROT_ATAPI_DMA)
4183 ap->ops->bmdma_start(qc); /* initiate bmdma */
4184 spin_unlock_irqrestore(&ap->host_set->lock, flags);
4185 } else {
4186 ata_data_xfer(ap, qc->cdb, ap->cdb_len, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004187
Tejun Heoc1389502005-08-22 14:59:24 +09004188 /* PIO commands are handled by polling */
Albert Lee14be71f2005-09-27 17:36:35 +08004189 ap->hsm_task_state = HSM_ST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004190 queue_work(ata_wq, &ap->pio_task);
4191 }
4192
4193 return;
4194
4195err_out:
Albert Leea22e2eb2005-12-05 15:38:02 +08004196 ata_poll_qc_complete(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004197}
4198
Edward Falk0baab862005-06-02 18:17:13 -04004199
4200/**
4201 * ata_port_start - Set port up for dma.
4202 * @ap: Port to initialize
4203 *
4204 * Called just after data structures for each port are
4205 * initialized. Allocates space for PRD table.
4206 *
4207 * May be used as the port_start() entry in ata_port_operations.
4208 *
4209 * LOCKING:
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04004210 * Inherited from caller.
Edward Falk0baab862005-06-02 18:17:13 -04004211 */
4212
Jens Axboe9b847542006-01-06 09:28:07 +01004213/*
4214 * Execute a 'simple' command, that only consists of the opcode 'cmd' itself,
4215 * without filling any other registers
4216 */
4217static int ata_do_simple_cmd(struct ata_port *ap, struct ata_device *dev,
4218 u8 cmd)
4219{
4220 struct ata_taskfile tf;
4221 int err;
4222
4223 ata_tf_init(ap, &tf, dev->devno);
4224
4225 tf.command = cmd;
4226 tf.flags |= ATA_TFLAG_DEVICE;
4227 tf.protocol = ATA_PROT_NODATA;
4228
4229 err = ata_exec_internal(ap, dev, &tf, DMA_NONE, NULL, 0);
4230 if (err)
4231 printk(KERN_ERR "%s: ata command failed: %d\n",
4232 __FUNCTION__, err);
4233
4234 return err;
4235}
4236
4237static int ata_flush_cache(struct ata_port *ap, struct ata_device *dev)
4238{
4239 u8 cmd;
4240
4241 if (!ata_try_flush_cache(dev))
4242 return 0;
4243
4244 if (ata_id_has_flush_ext(dev->id))
4245 cmd = ATA_CMD_FLUSH_EXT;
4246 else
4247 cmd = ATA_CMD_FLUSH;
4248
4249 return ata_do_simple_cmd(ap, dev, cmd);
4250}
4251
4252static int ata_standby_drive(struct ata_port *ap, struct ata_device *dev)
4253{
4254 return ata_do_simple_cmd(ap, dev, ATA_CMD_STANDBYNOW1);
4255}
4256
4257static int ata_start_drive(struct ata_port *ap, struct ata_device *dev)
4258{
4259 return ata_do_simple_cmd(ap, dev, ATA_CMD_IDLEIMMEDIATE);
4260}
4261
4262/**
4263 * ata_device_resume - wakeup a previously suspended devices
4264 *
4265 * Kick the drive back into action, by sending it an idle immediate
4266 * command and making sure its transfer mode matches between drive
4267 * and host.
4268 *
4269 */
4270int ata_device_resume(struct ata_port *ap, struct ata_device *dev)
4271{
4272 if (ap->flags & ATA_FLAG_SUSPENDED) {
4273 ap->flags &= ~ATA_FLAG_SUSPENDED;
4274 ata_set_mode(ap);
4275 }
4276 if (!ata_dev_present(dev))
4277 return 0;
4278 if (dev->class == ATA_DEV_ATA)
4279 ata_start_drive(ap, dev);
4280
4281 return 0;
4282}
4283
4284/**
4285 * ata_device_suspend - prepare a device for suspend
4286 *
4287 * Flush the cache on the drive, if appropriate, then issue a
4288 * standbynow command.
4289 *
4290 */
4291int ata_device_suspend(struct ata_port *ap, struct ata_device *dev)
4292{
4293 if (!ata_dev_present(dev))
4294 return 0;
4295 if (dev->class == ATA_DEV_ATA)
4296 ata_flush_cache(ap, dev);
4297
4298 ata_standby_drive(ap, dev);
4299 ap->flags |= ATA_FLAG_SUSPENDED;
4300 return 0;
4301}
4302
Linus Torvalds1da177e2005-04-16 15:20:36 -07004303int ata_port_start (struct ata_port *ap)
4304{
4305 struct device *dev = ap->host_set->dev;
Jeff Garzik6037d6b2005-11-04 22:08:00 -05004306 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004307
4308 ap->prd = dma_alloc_coherent(dev, ATA_PRD_TBL_SZ, &ap->prd_dma, GFP_KERNEL);
4309 if (!ap->prd)
4310 return -ENOMEM;
4311
Jeff Garzik6037d6b2005-11-04 22:08:00 -05004312 rc = ata_pad_alloc(ap, dev);
4313 if (rc) {
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004314 dma_free_coherent(dev, ATA_PRD_TBL_SZ, ap->prd, ap->prd_dma);
Jeff Garzik6037d6b2005-11-04 22:08:00 -05004315 return rc;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004316 }
4317
Linus Torvalds1da177e2005-04-16 15:20:36 -07004318 DPRINTK("prd alloc, virt %p, dma %llx\n", ap->prd, (unsigned long long) ap->prd_dma);
4319
4320 return 0;
4321}
4322
Edward Falk0baab862005-06-02 18:17:13 -04004323
4324/**
4325 * ata_port_stop - Undo ata_port_start()
4326 * @ap: Port to shut down
4327 *
4328 * Frees the PRD table.
4329 *
4330 * May be used as the port_stop() entry in ata_port_operations.
4331 *
4332 * LOCKING:
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04004333 * Inherited from caller.
Edward Falk0baab862005-06-02 18:17:13 -04004334 */
4335
Linus Torvalds1da177e2005-04-16 15:20:36 -07004336void ata_port_stop (struct ata_port *ap)
4337{
4338 struct device *dev = ap->host_set->dev;
4339
4340 dma_free_coherent(dev, ATA_PRD_TBL_SZ, ap->prd, ap->prd_dma);
Jeff Garzik6037d6b2005-11-04 22:08:00 -05004341 ata_pad_free(ap, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004342}
4343
Jeff Garzikaa8f0dc2005-05-26 21:54:27 -04004344void ata_host_stop (struct ata_host_set *host_set)
4345{
4346 if (host_set->mmio_base)
4347 iounmap(host_set->mmio_base);
4348}
4349
4350
Linus Torvalds1da177e2005-04-16 15:20:36 -07004351/**
4352 * ata_host_remove - Unregister SCSI host structure with upper layers
4353 * @ap: Port to unregister
4354 * @do_unregister: 1 if we fully unregister, 0 to just stop the port
4355 *
4356 * LOCKING:
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04004357 * Inherited from caller.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004358 */
4359
4360static void ata_host_remove(struct ata_port *ap, unsigned int do_unregister)
4361{
4362 struct Scsi_Host *sh = ap->host;
4363
4364 DPRINTK("ENTER\n");
4365
4366 if (do_unregister)
4367 scsi_remove_host(sh);
4368
4369 ap->ops->port_stop(ap);
4370}
4371
4372/**
4373 * ata_host_init - Initialize an ata_port structure
4374 * @ap: Structure to initialize
4375 * @host: associated SCSI mid-layer structure
4376 * @host_set: Collection of hosts to which @ap belongs
4377 * @ent: Probe information provided by low-level driver
4378 * @port_no: Port number associated with this ata_port
4379 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04004380 * Initialize a new ata_port structure, and its associated
4381 * scsi_host.
4382 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004383 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004384 * Inherited from caller.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004385 */
4386
4387static void ata_host_init(struct ata_port *ap, struct Scsi_Host *host,
4388 struct ata_host_set *host_set,
Jeff Garzik057ace52005-10-22 14:27:05 -04004389 const struct ata_probe_ent *ent, unsigned int port_no)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004390{
4391 unsigned int i;
4392
4393 host->max_id = 16;
4394 host->max_lun = 1;
4395 host->max_channel = 1;
4396 host->unique_id = ata_unique_id++;
4397 host->max_cmd_len = 12;
Christoph Hellwig12413192005-06-11 01:05:01 +02004398
Linus Torvalds1da177e2005-04-16 15:20:36 -07004399 ap->flags = ATA_FLAG_PORT_DISABLED;
4400 ap->id = host->unique_id;
4401 ap->host = host;
4402 ap->ctl = ATA_DEVCTL_OBS;
4403 ap->host_set = host_set;
4404 ap->port_no = port_no;
4405 ap->hard_port_no =
4406 ent->legacy_mode ? ent->hard_port_no : port_no;
4407 ap->pio_mask = ent->pio_mask;
4408 ap->mwdma_mask = ent->mwdma_mask;
4409 ap->udma_mask = ent->udma_mask;
4410 ap->flags |= ent->host_flags;
4411 ap->ops = ent->port_ops;
4412 ap->cbl = ATA_CBL_NONE;
4413 ap->active_tag = ATA_TAG_POISON;
4414 ap->last_ctl = 0xFF;
4415
4416 INIT_WORK(&ap->packet_task, atapi_packet_task, ap);
4417 INIT_WORK(&ap->pio_task, ata_pio_task, ap);
Tejun Heoa72ec4c2006-01-23 13:09:37 +09004418 INIT_LIST_HEAD(&ap->eh_done_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004419
4420 for (i = 0; i < ATA_MAX_DEVICES; i++)
4421 ap->device[i].devno = i;
4422
4423#ifdef ATA_IRQ_TRAP
4424 ap->stats.unhandled_irq = 1;
4425 ap->stats.idle_irq = 1;
4426#endif
4427
4428 memcpy(&ap->ioaddr, &ent->port[port_no], sizeof(struct ata_ioports));
4429}
4430
4431/**
4432 * ata_host_add - Attach low-level ATA driver to system
4433 * @ent: Information provided by low-level driver
4434 * @host_set: Collections of ports to which we add
4435 * @port_no: Port number associated with this host
4436 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04004437 * Attach low-level ATA driver to system.
4438 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004439 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004440 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004441 *
4442 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004443 * New ata_port on success, for NULL on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004444 */
4445
Jeff Garzik057ace52005-10-22 14:27:05 -04004446static struct ata_port * ata_host_add(const struct ata_probe_ent *ent,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004447 struct ata_host_set *host_set,
4448 unsigned int port_no)
4449{
4450 struct Scsi_Host *host;
4451 struct ata_port *ap;
4452 int rc;
4453
4454 DPRINTK("ENTER\n");
4455 host = scsi_host_alloc(ent->sht, sizeof(struct ata_port));
4456 if (!host)
4457 return NULL;
4458
4459 ap = (struct ata_port *) &host->hostdata[0];
4460
4461 ata_host_init(ap, host, host_set, ent, port_no);
4462
4463 rc = ap->ops->port_start(ap);
4464 if (rc)
4465 goto err_out;
4466
4467 return ap;
4468
4469err_out:
4470 scsi_host_put(host);
4471 return NULL;
4472}
4473
4474/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04004475 * ata_device_add - Register hardware device with ATA and SCSI layers
4476 * @ent: Probe information describing hardware device to be registered
4477 *
4478 * This function processes the information provided in the probe
4479 * information struct @ent, allocates the necessary ATA and SCSI
4480 * host information structures, initializes them, and registers
4481 * everything with requisite kernel subsystems.
4482 *
4483 * This function requests irqs, probes the ATA bus, and probes
4484 * the SCSI bus.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004485 *
4486 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004487 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004488 *
4489 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004490 * Number of ports registered. Zero on error (no ports registered).
Linus Torvalds1da177e2005-04-16 15:20:36 -07004491 */
4492
Jeff Garzik057ace52005-10-22 14:27:05 -04004493int ata_device_add(const struct ata_probe_ent *ent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004494{
4495 unsigned int count = 0, i;
4496 struct device *dev = ent->dev;
4497 struct ata_host_set *host_set;
4498
4499 DPRINTK("ENTER\n");
4500 /* alloc a container for our list of ATA ports (buses) */
Randy Dunlap57f3bda2005-10-28 20:37:23 -07004501 host_set = kzalloc(sizeof(struct ata_host_set) +
Linus Torvalds1da177e2005-04-16 15:20:36 -07004502 (ent->n_ports * sizeof(void *)), GFP_KERNEL);
4503 if (!host_set)
4504 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004505 spin_lock_init(&host_set->lock);
4506
4507 host_set->dev = dev;
4508 host_set->n_ports = ent->n_ports;
4509 host_set->irq = ent->irq;
4510 host_set->mmio_base = ent->mmio_base;
4511 host_set->private_data = ent->private_data;
4512 host_set->ops = ent->port_ops;
4513
4514 /* register each port bound to this device */
4515 for (i = 0; i < ent->n_ports; i++) {
4516 struct ata_port *ap;
4517 unsigned long xfer_mode_mask;
4518
4519 ap = ata_host_add(ent, host_set, i);
4520 if (!ap)
4521 goto err_out;
4522
4523 host_set->ports[i] = ap;
4524 xfer_mode_mask =(ap->udma_mask << ATA_SHIFT_UDMA) |
4525 (ap->mwdma_mask << ATA_SHIFT_MWDMA) |
4526 (ap->pio_mask << ATA_SHIFT_PIO);
4527
4528 /* print per-port info to dmesg */
4529 printk(KERN_INFO "ata%u: %cATA max %s cmd 0x%lX ctl 0x%lX "
4530 "bmdma 0x%lX irq %lu\n",
4531 ap->id,
4532 ap->flags & ATA_FLAG_SATA ? 'S' : 'P',
4533 ata_mode_string(xfer_mode_mask),
4534 ap->ioaddr.cmd_addr,
4535 ap->ioaddr.ctl_addr,
4536 ap->ioaddr.bmdma_addr,
4537 ent->irq);
4538
4539 ata_chk_status(ap);
4540 host_set->ops->irq_clear(ap);
4541 count++;
4542 }
4543
Randy Dunlap57f3bda2005-10-28 20:37:23 -07004544 if (!count)
4545 goto err_free_ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004546
4547 /* obtain irq, that is shared between channels */
4548 if (request_irq(ent->irq, ent->port_ops->irq_handler, ent->irq_flags,
4549 DRV_NAME, host_set))
4550 goto err_out;
4551
4552 /* perform each probe synchronously */
4553 DPRINTK("probe begin\n");
4554 for (i = 0; i < count; i++) {
4555 struct ata_port *ap;
4556 int rc;
4557
4558 ap = host_set->ports[i];
4559
4560 DPRINTK("ata%u: probe begin\n", ap->id);
4561 rc = ata_bus_probe(ap);
4562 DPRINTK("ata%u: probe end\n", ap->id);
4563
4564 if (rc) {
4565 /* FIXME: do something useful here?
4566 * Current libata behavior will
4567 * tear down everything when
4568 * the module is removed
4569 * or the h/w is unplugged.
4570 */
4571 }
4572
4573 rc = scsi_add_host(ap->host, dev);
4574 if (rc) {
4575 printk(KERN_ERR "ata%u: scsi_add_host failed\n",
4576 ap->id);
4577 /* FIXME: do something useful here */
4578 /* FIXME: handle unconditional calls to
4579 * scsi_scan_host and ata_host_remove, below,
4580 * at the very least
4581 */
4582 }
4583 }
4584
4585 /* probes are done, now scan each port's disk(s) */
4586 DPRINTK("probe begin\n");
4587 for (i = 0; i < count; i++) {
4588 struct ata_port *ap = host_set->ports[i];
4589
Jeff Garzik644dd0c2005-10-03 15:55:19 -04004590 ata_scsi_scan_host(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004591 }
4592
4593 dev_set_drvdata(dev, host_set);
4594
4595 VPRINTK("EXIT, returning %u\n", ent->n_ports);
4596 return ent->n_ports; /* success */
4597
4598err_out:
4599 for (i = 0; i < count; i++) {
4600 ata_host_remove(host_set->ports[i], 1);
4601 scsi_host_put(host_set->ports[i]->host);
4602 }
Randy Dunlap57f3bda2005-10-28 20:37:23 -07004603err_free_ret:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004604 kfree(host_set);
4605 VPRINTK("EXIT, returning 0\n");
4606 return 0;
4607}
4608
4609/**
Alan Cox17b14452005-09-15 15:44:00 +01004610 * ata_host_set_remove - PCI layer callback for device removal
4611 * @host_set: ATA host set that was removed
4612 *
4613 * Unregister all objects associated with this host set. Free those
4614 * objects.
4615 *
4616 * LOCKING:
4617 * Inherited from calling layer (may sleep).
4618 */
4619
Alan Cox17b14452005-09-15 15:44:00 +01004620void ata_host_set_remove(struct ata_host_set *host_set)
4621{
4622 struct ata_port *ap;
4623 unsigned int i;
4624
4625 for (i = 0; i < host_set->n_ports; i++) {
4626 ap = host_set->ports[i];
4627 scsi_remove_host(ap->host);
4628 }
4629
4630 free_irq(host_set->irq, host_set);
4631
4632 for (i = 0; i < host_set->n_ports; i++) {
4633 ap = host_set->ports[i];
4634
4635 ata_scsi_release(ap->host);
4636
4637 if ((ap->flags & ATA_FLAG_NO_LEGACY) == 0) {
4638 struct ata_ioports *ioaddr = &ap->ioaddr;
4639
4640 if (ioaddr->cmd_addr == 0x1f0)
4641 release_region(0x1f0, 8);
4642 else if (ioaddr->cmd_addr == 0x170)
4643 release_region(0x170, 8);
4644 }
4645
4646 scsi_host_put(ap->host);
4647 }
4648
4649 if (host_set->ops->host_stop)
4650 host_set->ops->host_stop(host_set);
4651
4652 kfree(host_set);
4653}
4654
4655/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004656 * ata_scsi_release - SCSI layer callback hook for host unload
4657 * @host: libata host to be unloaded
4658 *
4659 * Performs all duties necessary to shut down a libata port...
4660 * Kill port kthread, disable port, and release resources.
4661 *
4662 * LOCKING:
4663 * Inherited from SCSI layer.
4664 *
4665 * RETURNS:
4666 * One.
4667 */
4668
4669int ata_scsi_release(struct Scsi_Host *host)
4670{
4671 struct ata_port *ap = (struct ata_port *) &host->hostdata[0];
4672
4673 DPRINTK("ENTER\n");
4674
4675 ap->ops->port_disable(ap);
4676 ata_host_remove(ap, 0);
4677
4678 DPRINTK("EXIT\n");
4679 return 1;
4680}
4681
4682/**
4683 * ata_std_ports - initialize ioaddr with standard port offsets.
4684 * @ioaddr: IO address structure to be initialized
Edward Falk0baab862005-06-02 18:17:13 -04004685 *
4686 * Utility function which initializes data_addr, error_addr,
4687 * feature_addr, nsect_addr, lbal_addr, lbam_addr, lbah_addr,
4688 * device_addr, status_addr, and command_addr to standard offsets
4689 * relative to cmd_addr.
4690 *
4691 * Does not set ctl_addr, altstatus_addr, bmdma_addr, or scr_addr.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004692 */
Edward Falk0baab862005-06-02 18:17:13 -04004693
Linus Torvalds1da177e2005-04-16 15:20:36 -07004694void ata_std_ports(struct ata_ioports *ioaddr)
4695{
4696 ioaddr->data_addr = ioaddr->cmd_addr + ATA_REG_DATA;
4697 ioaddr->error_addr = ioaddr->cmd_addr + ATA_REG_ERR;
4698 ioaddr->feature_addr = ioaddr->cmd_addr + ATA_REG_FEATURE;
4699 ioaddr->nsect_addr = ioaddr->cmd_addr + ATA_REG_NSECT;
4700 ioaddr->lbal_addr = ioaddr->cmd_addr + ATA_REG_LBAL;
4701 ioaddr->lbam_addr = ioaddr->cmd_addr + ATA_REG_LBAM;
4702 ioaddr->lbah_addr = ioaddr->cmd_addr + ATA_REG_LBAH;
4703 ioaddr->device_addr = ioaddr->cmd_addr + ATA_REG_DEVICE;
4704 ioaddr->status_addr = ioaddr->cmd_addr + ATA_REG_STATUS;
4705 ioaddr->command_addr = ioaddr->cmd_addr + ATA_REG_CMD;
4706}
4707
4708static struct ata_probe_ent *
Jeff Garzik057ace52005-10-22 14:27:05 -04004709ata_probe_ent_alloc(struct device *dev, const struct ata_port_info *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004710{
4711 struct ata_probe_ent *probe_ent;
4712
Randy Dunlap57f3bda2005-10-28 20:37:23 -07004713 probe_ent = kzalloc(sizeof(*probe_ent), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004714 if (!probe_ent) {
4715 printk(KERN_ERR DRV_NAME "(%s): out of memory\n",
4716 kobject_name(&(dev->kobj)));
4717 return NULL;
4718 }
4719
Linus Torvalds1da177e2005-04-16 15:20:36 -07004720 INIT_LIST_HEAD(&probe_ent->node);
4721 probe_ent->dev = dev;
4722
4723 probe_ent->sht = port->sht;
4724 probe_ent->host_flags = port->host_flags;
4725 probe_ent->pio_mask = port->pio_mask;
4726 probe_ent->mwdma_mask = port->mwdma_mask;
4727 probe_ent->udma_mask = port->udma_mask;
4728 probe_ent->port_ops = port->port_ops;
4729
4730 return probe_ent;
4731}
4732
Edward Falk0baab862005-06-02 18:17:13 -04004733
4734
Jeff Garzik374b1872005-08-30 05:42:52 -04004735#ifdef CONFIG_PCI
4736
4737void ata_pci_host_stop (struct ata_host_set *host_set)
4738{
4739 struct pci_dev *pdev = to_pci_dev(host_set->dev);
4740
4741 pci_iounmap(pdev, host_set->mmio_base);
4742}
4743
Edward Falk0baab862005-06-02 18:17:13 -04004744/**
4745 * ata_pci_init_native_mode - Initialize native-mode driver
4746 * @pdev: pci device to be initialized
4747 * @port: array[2] of pointers to port info structures.
Alan Cox47a86592005-10-04 08:09:19 -04004748 * @ports: bitmap of ports present
Edward Falk0baab862005-06-02 18:17:13 -04004749 *
4750 * Utility function which allocates and initializes an
4751 * ata_probe_ent structure for a standard dual-port
4752 * PIO-based IDE controller. The returned ata_probe_ent
4753 * structure can be passed to ata_device_add(). The returned
4754 * ata_probe_ent structure should then be freed with kfree().
Alan Cox47a86592005-10-04 08:09:19 -04004755 *
4756 * The caller need only pass the address of the primary port, the
4757 * secondary will be deduced automatically. If the device has non
4758 * standard secondary port mappings this function can be called twice,
4759 * once for each interface.
Edward Falk0baab862005-06-02 18:17:13 -04004760 */
4761
Linus Torvalds1da177e2005-04-16 15:20:36 -07004762struct ata_probe_ent *
Alan Cox47a86592005-10-04 08:09:19 -04004763ata_pci_init_native_mode(struct pci_dev *pdev, struct ata_port_info **port, int ports)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004764{
4765 struct ata_probe_ent *probe_ent =
4766 ata_probe_ent_alloc(pci_dev_to_dev(pdev), port[0]);
Alan Cox47a86592005-10-04 08:09:19 -04004767 int p = 0;
4768
Linus Torvalds1da177e2005-04-16 15:20:36 -07004769 if (!probe_ent)
4770 return NULL;
4771
Linus Torvalds1da177e2005-04-16 15:20:36 -07004772 probe_ent->irq = pdev->irq;
4773 probe_ent->irq_flags = SA_SHIRQ;
Alan Coxe99f8b52005-11-08 14:09:44 +00004774 probe_ent->private_data = port[0]->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004775
Alan Cox47a86592005-10-04 08:09:19 -04004776 if (ports & ATA_PORT_PRIMARY) {
4777 probe_ent->port[p].cmd_addr = pci_resource_start(pdev, 0);
4778 probe_ent->port[p].altstatus_addr =
4779 probe_ent->port[p].ctl_addr =
4780 pci_resource_start(pdev, 1) | ATA_PCI_CTL_OFS;
4781 probe_ent->port[p].bmdma_addr = pci_resource_start(pdev, 4);
4782 ata_std_ports(&probe_ent->port[p]);
4783 p++;
4784 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004785
Alan Cox47a86592005-10-04 08:09:19 -04004786 if (ports & ATA_PORT_SECONDARY) {
4787 probe_ent->port[p].cmd_addr = pci_resource_start(pdev, 2);
4788 probe_ent->port[p].altstatus_addr =
4789 probe_ent->port[p].ctl_addr =
4790 pci_resource_start(pdev, 3) | ATA_PCI_CTL_OFS;
4791 probe_ent->port[p].bmdma_addr = pci_resource_start(pdev, 4) + 8;
4792 ata_std_ports(&probe_ent->port[p]);
4793 p++;
4794 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004795
Alan Cox47a86592005-10-04 08:09:19 -04004796 probe_ent->n_ports = p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004797 return probe_ent;
4798}
4799
Jeff Garzik0f0d5192005-10-30 06:41:29 -05004800static 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 -07004801{
Alan Cox47a86592005-10-04 08:09:19 -04004802 struct ata_probe_ent *probe_ent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004803
Jeff Garzik0f0d5192005-10-30 06:41:29 -05004804 probe_ent = ata_probe_ent_alloc(pci_dev_to_dev(pdev), port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004805 if (!probe_ent)
4806 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004807
Linus Torvalds1da177e2005-04-16 15:20:36 -07004808 probe_ent->legacy_mode = 1;
Alan Cox47a86592005-10-04 08:09:19 -04004809 probe_ent->n_ports = 1;
4810 probe_ent->hard_port_no = port_num;
Alan Coxe99f8b52005-11-08 14:09:44 +00004811 probe_ent->private_data = port->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004812
Alan Cox47a86592005-10-04 08:09:19 -04004813 switch(port_num)
4814 {
4815 case 0:
4816 probe_ent->irq = 14;
4817 probe_ent->port[0].cmd_addr = 0x1f0;
4818 probe_ent->port[0].altstatus_addr =
4819 probe_ent->port[0].ctl_addr = 0x3f6;
4820 break;
4821 case 1:
4822 probe_ent->irq = 15;
4823 probe_ent->port[0].cmd_addr = 0x170;
4824 probe_ent->port[0].altstatus_addr =
4825 probe_ent->port[0].ctl_addr = 0x376;
4826 break;
4827 }
4828 probe_ent->port[0].bmdma_addr = pci_resource_start(pdev, 4) + 8 * port_num;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004829 ata_std_ports(&probe_ent->port[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004830 return probe_ent;
4831}
4832
4833/**
4834 * ata_pci_init_one - Initialize/register PCI IDE host controller
4835 * @pdev: Controller to be initialized
4836 * @port_info: Information from low-level host driver
4837 * @n_ports: Number of ports attached to host controller
4838 *
Edward Falk0baab862005-06-02 18:17:13 -04004839 * This is a helper function which can be called from a driver's
4840 * xxx_init_one() probe function if the hardware uses traditional
4841 * IDE taskfile registers.
4842 *
4843 * This function calls pci_enable_device(), reserves its register
4844 * regions, sets the dma mask, enables bus master mode, and calls
4845 * ata_device_add()
4846 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004847 * LOCKING:
4848 * Inherited from PCI layer (may sleep).
4849 *
4850 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004851 * Zero on success, negative on errno-based value on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004852 */
4853
4854int ata_pci_init_one (struct pci_dev *pdev, struct ata_port_info **port_info,
4855 unsigned int n_ports)
4856{
Alan Cox47a86592005-10-04 08:09:19 -04004857 struct ata_probe_ent *probe_ent = NULL, *probe_ent2 = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004858 struct ata_port_info *port[2];
4859 u8 tmp8, mask;
4860 unsigned int legacy_mode = 0;
4861 int disable_dev_on_err = 1;
4862 int rc;
4863
4864 DPRINTK("ENTER\n");
4865
4866 port[0] = port_info[0];
4867 if (n_ports > 1)
4868 port[1] = port_info[1];
4869 else
4870 port[1] = port[0];
4871
4872 if ((port[0]->host_flags & ATA_FLAG_NO_LEGACY) == 0
4873 && (pdev->class >> 8) == PCI_CLASS_STORAGE_IDE) {
Alan Cox47a86592005-10-04 08:09:19 -04004874 /* TODO: What if one channel is in native mode ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004875 pci_read_config_byte(pdev, PCI_CLASS_PROG, &tmp8);
4876 mask = (1 << 2) | (1 << 0);
4877 if ((tmp8 & mask) != mask)
4878 legacy_mode = (1 << 3);
4879 }
4880
4881 /* FIXME... */
Alan Cox47a86592005-10-04 08:09:19 -04004882 if ((!legacy_mode) && (n_ports > 2)) {
4883 printk(KERN_ERR "ata: BUG: native mode, n_ports > 2\n");
4884 n_ports = 2;
4885 /* For now */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004886 }
4887
Alan Cox47a86592005-10-04 08:09:19 -04004888 /* FIXME: Really for ATA it isn't safe because the device may be
4889 multi-purpose and we want to leave it alone if it was already
4890 enabled. Secondly for shared use as Arjan says we want refcounting
4891
4892 Checking dev->is_enabled is insufficient as this is not set at
4893 boot for the primary video which is BIOS enabled
4894 */
4895
Linus Torvalds1da177e2005-04-16 15:20:36 -07004896 rc = pci_enable_device(pdev);
4897 if (rc)
4898 return rc;
4899
4900 rc = pci_request_regions(pdev, DRV_NAME);
4901 if (rc) {
4902 disable_dev_on_err = 0;
4903 goto err_out;
4904 }
4905
Alan Cox47a86592005-10-04 08:09:19 -04004906 /* FIXME: Should use platform specific mappers for legacy port ranges */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004907 if (legacy_mode) {
4908 if (!request_region(0x1f0, 8, "libata")) {
4909 struct resource *conflict, res;
4910 res.start = 0x1f0;
4911 res.end = 0x1f0 + 8 - 1;
4912 conflict = ____request_resource(&ioport_resource, &res);
4913 if (!strcmp(conflict->name, "libata"))
4914 legacy_mode |= (1 << 0);
4915 else {
4916 disable_dev_on_err = 0;
4917 printk(KERN_WARNING "ata: 0x1f0 IDE port busy\n");
4918 }
4919 } else
4920 legacy_mode |= (1 << 0);
4921
4922 if (!request_region(0x170, 8, "libata")) {
4923 struct resource *conflict, res;
4924 res.start = 0x170;
4925 res.end = 0x170 + 8 - 1;
4926 conflict = ____request_resource(&ioport_resource, &res);
4927 if (!strcmp(conflict->name, "libata"))
4928 legacy_mode |= (1 << 1);
4929 else {
4930 disable_dev_on_err = 0;
4931 printk(KERN_WARNING "ata: 0x170 IDE port busy\n");
4932 }
4933 } else
4934 legacy_mode |= (1 << 1);
4935 }
4936
4937 /* we have legacy mode, but all ports are unavailable */
4938 if (legacy_mode == (1 << 3)) {
4939 rc = -EBUSY;
4940 goto err_out_regions;
4941 }
4942
4943 rc = pci_set_dma_mask(pdev, ATA_DMA_MASK);
4944 if (rc)
4945 goto err_out_regions;
4946 rc = pci_set_consistent_dma_mask(pdev, ATA_DMA_MASK);
4947 if (rc)
4948 goto err_out_regions;
4949
4950 if (legacy_mode) {
Alan Cox47a86592005-10-04 08:09:19 -04004951 if (legacy_mode & (1 << 0))
Jeff Garzik0f0d5192005-10-30 06:41:29 -05004952 probe_ent = ata_pci_init_legacy_port(pdev, port[0], 0);
Alan Cox47a86592005-10-04 08:09:19 -04004953 if (legacy_mode & (1 << 1))
Jeff Garzik0f0d5192005-10-30 06:41:29 -05004954 probe_ent2 = ata_pci_init_legacy_port(pdev, port[1], 1);
Alan Cox47a86592005-10-04 08:09:19 -04004955 } else {
4956 if (n_ports == 2)
4957 probe_ent = ata_pci_init_native_mode(pdev, port, ATA_PORT_PRIMARY | ATA_PORT_SECONDARY);
4958 else
4959 probe_ent = ata_pci_init_native_mode(pdev, port, ATA_PORT_PRIMARY);
4960 }
4961 if (!probe_ent && !probe_ent2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004962 rc = -ENOMEM;
4963 goto err_out_regions;
4964 }
4965
4966 pci_set_master(pdev);
4967
4968 /* FIXME: check ata_device_add return */
4969 if (legacy_mode) {
4970 if (legacy_mode & (1 << 0))
4971 ata_device_add(probe_ent);
4972 if (legacy_mode & (1 << 1))
4973 ata_device_add(probe_ent2);
4974 } else
4975 ata_device_add(probe_ent);
4976
4977 kfree(probe_ent);
4978 kfree(probe_ent2);
4979
4980 return 0;
4981
4982err_out_regions:
4983 if (legacy_mode & (1 << 0))
4984 release_region(0x1f0, 8);
4985 if (legacy_mode & (1 << 1))
4986 release_region(0x170, 8);
4987 pci_release_regions(pdev);
4988err_out:
4989 if (disable_dev_on_err)
4990 pci_disable_device(pdev);
4991 return rc;
4992}
4993
4994/**
4995 * ata_pci_remove_one - PCI layer callback for device removal
4996 * @pdev: PCI device that was removed
4997 *
4998 * PCI layer indicates to libata via this hook that
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04004999 * hot-unplug or module unload event has occurred.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005000 * Handle this by unregistering all objects associated
5001 * with this PCI device. Free those objects. Then finally
5002 * release PCI resources and disable device.
5003 *
5004 * LOCKING:
5005 * Inherited from PCI layer (may sleep).
5006 */
5007
5008void ata_pci_remove_one (struct pci_dev *pdev)
5009{
5010 struct device *dev = pci_dev_to_dev(pdev);
5011 struct ata_host_set *host_set = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005012
Alan Cox17b14452005-09-15 15:44:00 +01005013 ata_host_set_remove(host_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005014 pci_release_regions(pdev);
5015 pci_disable_device(pdev);
5016 dev_set_drvdata(dev, NULL);
5017}
5018
5019/* move to PCI subsystem */
Jeff Garzik057ace52005-10-22 14:27:05 -04005020int pci_test_config_bits(struct pci_dev *pdev, const struct pci_bits *bits)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005021{
5022 unsigned long tmp = 0;
5023
5024 switch (bits->width) {
5025 case 1: {
5026 u8 tmp8 = 0;
5027 pci_read_config_byte(pdev, bits->reg, &tmp8);
5028 tmp = tmp8;
5029 break;
5030 }
5031 case 2: {
5032 u16 tmp16 = 0;
5033 pci_read_config_word(pdev, bits->reg, &tmp16);
5034 tmp = tmp16;
5035 break;
5036 }
5037 case 4: {
5038 u32 tmp32 = 0;
5039 pci_read_config_dword(pdev, bits->reg, &tmp32);
5040 tmp = tmp32;
5041 break;
5042 }
5043
5044 default:
5045 return -EINVAL;
5046 }
5047
5048 tmp &= bits->mask;
5049
5050 return (tmp == bits->val) ? 1 : 0;
5051}
Jens Axboe9b847542006-01-06 09:28:07 +01005052
5053int ata_pci_device_suspend(struct pci_dev *pdev, pm_message_t state)
5054{
5055 pci_save_state(pdev);
5056 pci_disable_device(pdev);
5057 pci_set_power_state(pdev, PCI_D3hot);
5058 return 0;
5059}
5060
5061int ata_pci_device_resume(struct pci_dev *pdev)
5062{
5063 pci_set_power_state(pdev, PCI_D0);
5064 pci_restore_state(pdev);
5065 pci_enable_device(pdev);
5066 pci_set_master(pdev);
5067 return 0;
5068}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005069#endif /* CONFIG_PCI */
5070
5071
Linus Torvalds1da177e2005-04-16 15:20:36 -07005072static int __init ata_init(void)
5073{
5074 ata_wq = create_workqueue("ata");
5075 if (!ata_wq)
5076 return -ENOMEM;
5077
5078 printk(KERN_DEBUG "libata version " DRV_VERSION " loaded.\n");
5079 return 0;
5080}
5081
5082static void __exit ata_exit(void)
5083{
5084 destroy_workqueue(ata_wq);
5085}
5086
5087module_init(ata_init);
5088module_exit(ata_exit);
5089
Jeff Garzik67846b32005-10-05 02:58:32 -04005090static unsigned long ratelimit_time;
5091static spinlock_t ata_ratelimit_lock = SPIN_LOCK_UNLOCKED;
5092
5093int ata_ratelimit(void)
5094{
5095 int rc;
5096 unsigned long flags;
5097
5098 spin_lock_irqsave(&ata_ratelimit_lock, flags);
5099
5100 if (time_after(jiffies, ratelimit_time)) {
5101 rc = 1;
5102 ratelimit_time = jiffies + (HZ/5);
5103 } else
5104 rc = 0;
5105
5106 spin_unlock_irqrestore(&ata_ratelimit_lock, flags);
5107
5108 return rc;
5109}
5110
Linus Torvalds1da177e2005-04-16 15:20:36 -07005111/*
5112 * libata is essentially a library of internal helper functions for
5113 * low-level ATA host controller drivers. As such, the API/ABI is
5114 * likely to change as new drivers are added and updated.
5115 * Do not depend on ABI/API stability.
5116 */
5117
5118EXPORT_SYMBOL_GPL(ata_std_bios_param);
5119EXPORT_SYMBOL_GPL(ata_std_ports);
5120EXPORT_SYMBOL_GPL(ata_device_add);
Alan Cox17b14452005-09-15 15:44:00 +01005121EXPORT_SYMBOL_GPL(ata_host_set_remove);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005122EXPORT_SYMBOL_GPL(ata_sg_init);
5123EXPORT_SYMBOL_GPL(ata_sg_init_one);
5124EXPORT_SYMBOL_GPL(ata_qc_complete);
5125EXPORT_SYMBOL_GPL(ata_qc_issue_prot);
5126EXPORT_SYMBOL_GPL(ata_eng_timeout);
5127EXPORT_SYMBOL_GPL(ata_tf_load);
5128EXPORT_SYMBOL_GPL(ata_tf_read);
5129EXPORT_SYMBOL_GPL(ata_noop_dev_select);
5130EXPORT_SYMBOL_GPL(ata_std_dev_select);
5131EXPORT_SYMBOL_GPL(ata_tf_to_fis);
5132EXPORT_SYMBOL_GPL(ata_tf_from_fis);
5133EXPORT_SYMBOL_GPL(ata_check_status);
5134EXPORT_SYMBOL_GPL(ata_altstatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005135EXPORT_SYMBOL_GPL(ata_exec_command);
5136EXPORT_SYMBOL_GPL(ata_port_start);
5137EXPORT_SYMBOL_GPL(ata_port_stop);
Jeff Garzikaa8f0dc2005-05-26 21:54:27 -04005138EXPORT_SYMBOL_GPL(ata_host_stop);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005139EXPORT_SYMBOL_GPL(ata_interrupt);
5140EXPORT_SYMBOL_GPL(ata_qc_prep);
5141EXPORT_SYMBOL_GPL(ata_bmdma_setup);
5142EXPORT_SYMBOL_GPL(ata_bmdma_start);
5143EXPORT_SYMBOL_GPL(ata_bmdma_irq_clear);
5144EXPORT_SYMBOL_GPL(ata_bmdma_status);
5145EXPORT_SYMBOL_GPL(ata_bmdma_stop);
5146EXPORT_SYMBOL_GPL(ata_port_probe);
5147EXPORT_SYMBOL_GPL(sata_phy_reset);
5148EXPORT_SYMBOL_GPL(__sata_phy_reset);
5149EXPORT_SYMBOL_GPL(ata_bus_reset);
5150EXPORT_SYMBOL_GPL(ata_port_disable);
Jeff Garzik67846b32005-10-05 02:58:32 -04005151EXPORT_SYMBOL_GPL(ata_ratelimit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005152EXPORT_SYMBOL_GPL(ata_scsi_ioctl);
5153EXPORT_SYMBOL_GPL(ata_scsi_queuecmd);
5154EXPORT_SYMBOL_GPL(ata_scsi_error);
5155EXPORT_SYMBOL_GPL(ata_scsi_slave_config);
5156EXPORT_SYMBOL_GPL(ata_scsi_release);
5157EXPORT_SYMBOL_GPL(ata_host_intr);
5158EXPORT_SYMBOL_GPL(ata_dev_classify);
5159EXPORT_SYMBOL_GPL(ata_dev_id_string);
Brad Campbell6f2f3812005-05-12 15:07:47 -04005160EXPORT_SYMBOL_GPL(ata_dev_config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005161EXPORT_SYMBOL_GPL(ata_scsi_simulate);
Tejun Heoa72ec4c2006-01-23 13:09:37 +09005162EXPORT_SYMBOL_GPL(ata_eh_qc_complete);
5163EXPORT_SYMBOL_GPL(ata_eh_qc_retry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005164
Alan Cox1bc4ccf2006-01-09 17:18:14 +00005165EXPORT_SYMBOL_GPL(ata_pio_need_iordy);
Alan Cox452503f2005-10-21 19:01:32 -04005166EXPORT_SYMBOL_GPL(ata_timing_compute);
5167EXPORT_SYMBOL_GPL(ata_timing_merge);
5168
Linus Torvalds1da177e2005-04-16 15:20:36 -07005169#ifdef CONFIG_PCI
5170EXPORT_SYMBOL_GPL(pci_test_config_bits);
Jeff Garzik374b1872005-08-30 05:42:52 -04005171EXPORT_SYMBOL_GPL(ata_pci_host_stop);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005172EXPORT_SYMBOL_GPL(ata_pci_init_native_mode);
5173EXPORT_SYMBOL_GPL(ata_pci_init_one);
5174EXPORT_SYMBOL_GPL(ata_pci_remove_one);
Jens Axboe9b847542006-01-06 09:28:07 +01005175EXPORT_SYMBOL_GPL(ata_pci_device_suspend);
5176EXPORT_SYMBOL_GPL(ata_pci_device_resume);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005177#endif /* CONFIG_PCI */
Jens Axboe9b847542006-01-06 09:28:07 +01005178
5179EXPORT_SYMBOL_GPL(ata_device_suspend);
5180EXPORT_SYMBOL_GPL(ata_device_resume);
5181EXPORT_SYMBOL_GPL(ata_scsi_device_suspend);
5182EXPORT_SYMBOL_GPL(ata_scsi_device_resume);