blob: 147e1461062df7d3b6ad9d3bc3e1e9b0c723f286 [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
Albert Lee59a10b12005-10-12 15:09:42 +080064static void ata_dev_reread_id(struct ata_port *ap, struct ata_device *dev);
Albert Lee8bf62ec2005-05-12 15:29:42 -040065static void ata_dev_init_params(struct ata_port *ap, struct ata_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066static void ata_set_mode(struct ata_port *ap);
67static void ata_dev_set_xfermode(struct ata_port *ap, struct ata_device *dev);
Jeff Garzik057ace52005-10-22 14:27:05 -040068static unsigned int ata_get_mode_mask(const struct ata_port *ap, int shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -070069static int fgb(u32 bitmap);
Jeff Garzik057ace52005-10-22 14:27:05 -040070static int ata_choose_xfer_mode(const struct ata_port *ap,
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 u8 *xfer_mode_out,
72 unsigned int *xfer_shift_out);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
74static unsigned int ata_unique_id = 1;
75static struct workqueue_struct *ata_wq;
76
Jeff Garzik1623c812005-08-30 03:37:42 -040077int atapi_enabled = 0;
78module_param(atapi_enabled, int, 0444);
79MODULE_PARM_DESC(atapi_enabled, "Enable discovery of ATAPI devices (0=off, 1=on)");
80
Linus Torvalds1da177e2005-04-16 15:20:36 -070081MODULE_AUTHOR("Jeff Garzik");
82MODULE_DESCRIPTION("Library module for ATA devices");
83MODULE_LICENSE("GPL");
84MODULE_VERSION(DRV_VERSION);
85
86/**
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -040087 * ata_tf_load_pio - send taskfile registers to host controller
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 * @ap: Port to which output is sent
89 * @tf: ATA taskfile register set
90 *
91 * Outputs ATA taskfile to standard ATA host controller.
92 *
93 * LOCKING:
94 * Inherited from caller.
95 */
96
Jeff Garzik057ace52005-10-22 14:27:05 -040097static void ata_tf_load_pio(struct ata_port *ap, const struct ata_taskfile *tf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070098{
99 struct ata_ioports *ioaddr = &ap->ioaddr;
100 unsigned int is_addr = tf->flags & ATA_TFLAG_ISADDR;
101
102 if (tf->ctl != ap->last_ctl) {
103 outb(tf->ctl, ioaddr->ctl_addr);
104 ap->last_ctl = tf->ctl;
105 ata_wait_idle(ap);
106 }
107
108 if (is_addr && (tf->flags & ATA_TFLAG_LBA48)) {
109 outb(tf->hob_feature, ioaddr->feature_addr);
110 outb(tf->hob_nsect, ioaddr->nsect_addr);
111 outb(tf->hob_lbal, ioaddr->lbal_addr);
112 outb(tf->hob_lbam, ioaddr->lbam_addr);
113 outb(tf->hob_lbah, ioaddr->lbah_addr);
114 VPRINTK("hob: feat 0x%X nsect 0x%X, lba 0x%X 0x%X 0x%X\n",
115 tf->hob_feature,
116 tf->hob_nsect,
117 tf->hob_lbal,
118 tf->hob_lbam,
119 tf->hob_lbah);
120 }
121
122 if (is_addr) {
123 outb(tf->feature, ioaddr->feature_addr);
124 outb(tf->nsect, ioaddr->nsect_addr);
125 outb(tf->lbal, ioaddr->lbal_addr);
126 outb(tf->lbam, ioaddr->lbam_addr);
127 outb(tf->lbah, ioaddr->lbah_addr);
128 VPRINTK("feat 0x%X nsect 0x%X lba 0x%X 0x%X 0x%X\n",
129 tf->feature,
130 tf->nsect,
131 tf->lbal,
132 tf->lbam,
133 tf->lbah);
134 }
135
136 if (tf->flags & ATA_TFLAG_DEVICE) {
137 outb(tf->device, ioaddr->device_addr);
138 VPRINTK("device 0x%X\n", tf->device);
139 }
140
141 ata_wait_idle(ap);
142}
143
144/**
145 * ata_tf_load_mmio - send taskfile registers to host controller
146 * @ap: Port to which output is sent
147 * @tf: ATA taskfile register set
148 *
149 * Outputs ATA taskfile to standard ATA host controller using MMIO.
150 *
151 * LOCKING:
152 * Inherited from caller.
153 */
154
Jeff Garzik057ace52005-10-22 14:27:05 -0400155static void ata_tf_load_mmio(struct ata_port *ap, const struct ata_taskfile *tf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156{
157 struct ata_ioports *ioaddr = &ap->ioaddr;
158 unsigned int is_addr = tf->flags & ATA_TFLAG_ISADDR;
159
160 if (tf->ctl != ap->last_ctl) {
161 writeb(tf->ctl, (void __iomem *) ap->ioaddr.ctl_addr);
162 ap->last_ctl = tf->ctl;
163 ata_wait_idle(ap);
164 }
165
166 if (is_addr && (tf->flags & ATA_TFLAG_LBA48)) {
167 writeb(tf->hob_feature, (void __iomem *) ioaddr->feature_addr);
168 writeb(tf->hob_nsect, (void __iomem *) ioaddr->nsect_addr);
169 writeb(tf->hob_lbal, (void __iomem *) ioaddr->lbal_addr);
170 writeb(tf->hob_lbam, (void __iomem *) ioaddr->lbam_addr);
171 writeb(tf->hob_lbah, (void __iomem *) ioaddr->lbah_addr);
172 VPRINTK("hob: feat 0x%X nsect 0x%X, lba 0x%X 0x%X 0x%X\n",
173 tf->hob_feature,
174 tf->hob_nsect,
175 tf->hob_lbal,
176 tf->hob_lbam,
177 tf->hob_lbah);
178 }
179
180 if (is_addr) {
181 writeb(tf->feature, (void __iomem *) ioaddr->feature_addr);
182 writeb(tf->nsect, (void __iomem *) ioaddr->nsect_addr);
183 writeb(tf->lbal, (void __iomem *) ioaddr->lbal_addr);
184 writeb(tf->lbam, (void __iomem *) ioaddr->lbam_addr);
185 writeb(tf->lbah, (void __iomem *) ioaddr->lbah_addr);
186 VPRINTK("feat 0x%X nsect 0x%X lba 0x%X 0x%X 0x%X\n",
187 tf->feature,
188 tf->nsect,
189 tf->lbal,
190 tf->lbam,
191 tf->lbah);
192 }
193
194 if (tf->flags & ATA_TFLAG_DEVICE) {
195 writeb(tf->device, (void __iomem *) ioaddr->device_addr);
196 VPRINTK("device 0x%X\n", tf->device);
197 }
198
199 ata_wait_idle(ap);
200}
201
Edward Falk0baab862005-06-02 18:17:13 -0400202
203/**
204 * ata_tf_load - send taskfile registers to host controller
205 * @ap: Port to which output is sent
206 * @tf: ATA taskfile register set
207 *
208 * Outputs ATA taskfile to standard ATA host controller using MMIO
209 * or PIO as indicated by the ATA_FLAG_MMIO flag.
210 * Writes the control, feature, nsect, lbal, lbam, and lbah registers.
211 * Optionally (ATA_TFLAG_LBA48) writes hob_feature, hob_nsect,
212 * hob_lbal, hob_lbam, and hob_lbah.
213 *
214 * This function waits for idle (!BUSY and !DRQ) after writing
215 * registers. If the control register has a new value, this
216 * function also waits for idle after writing control and before
217 * writing the remaining registers.
218 *
219 * May be used as the tf_load() entry in ata_port_operations.
220 *
221 * LOCKING:
222 * Inherited from caller.
223 */
Jeff Garzik057ace52005-10-22 14:27:05 -0400224void ata_tf_load(struct ata_port *ap, const struct ata_taskfile *tf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225{
226 if (ap->flags & ATA_FLAG_MMIO)
227 ata_tf_load_mmio(ap, tf);
228 else
229 ata_tf_load_pio(ap, tf);
230}
231
232/**
Edward Falk0baab862005-06-02 18:17:13 -0400233 * ata_exec_command_pio - issue ATA command to host controller
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 * @ap: port to which command is being issued
235 * @tf: ATA taskfile register set
236 *
Edward Falk0baab862005-06-02 18:17:13 -0400237 * Issues PIO write to ATA command register, with proper
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 * synchronization with interrupt handler / other threads.
239 *
240 * LOCKING:
241 * spin_lock_irqsave(host_set lock)
242 */
243
Jeff Garzik057ace52005-10-22 14:27:05 -0400244static void ata_exec_command_pio(struct ata_port *ap, const struct ata_taskfile *tf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245{
246 DPRINTK("ata%u: cmd 0x%X\n", ap->id, tf->command);
247
248 outb(tf->command, ap->ioaddr.command_addr);
249 ata_pause(ap);
250}
251
252
253/**
254 * ata_exec_command_mmio - issue ATA command to host controller
255 * @ap: port to which command is being issued
256 * @tf: ATA taskfile register set
257 *
258 * Issues MMIO write to ATA command register, with proper
259 * synchronization with interrupt handler / other threads.
260 *
261 * LOCKING:
262 * spin_lock_irqsave(host_set lock)
263 */
264
Jeff Garzik057ace52005-10-22 14:27:05 -0400265static void ata_exec_command_mmio(struct ata_port *ap, const struct ata_taskfile *tf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266{
267 DPRINTK("ata%u: cmd 0x%X\n", ap->id, tf->command);
268
269 writeb(tf->command, (void __iomem *) ap->ioaddr.command_addr);
270 ata_pause(ap);
271}
272
Edward Falk0baab862005-06-02 18:17:13 -0400273
274/**
275 * ata_exec_command - issue ATA command to host controller
276 * @ap: port to which command is being issued
277 * @tf: ATA taskfile register set
278 *
279 * Issues PIO/MMIO write to ATA command register, with proper
280 * synchronization with interrupt handler / other threads.
281 *
282 * LOCKING:
283 * spin_lock_irqsave(host_set lock)
284 */
Jeff Garzik057ace52005-10-22 14:27:05 -0400285void ata_exec_command(struct ata_port *ap, const struct ata_taskfile *tf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286{
287 if (ap->flags & ATA_FLAG_MMIO)
288 ata_exec_command_mmio(ap, tf);
289 else
290 ata_exec_command_pio(ap, tf);
291}
292
293/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 * ata_tf_to_host - issue ATA taskfile to host controller
295 * @ap: port to which command is being issued
296 * @tf: ATA taskfile register set
297 *
298 * Issues ATA taskfile register set to ATA host controller,
299 * with proper synchronization with interrupt handler and
300 * other threads.
301 *
302 * LOCKING:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 * spin_lock_irqsave(host_set lock)
304 */
305
Jeff Garzike5338252005-10-30 21:37:17 -0500306static inline void ata_tf_to_host(struct ata_port *ap,
307 const struct ata_taskfile *tf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308{
309 ap->ops->tf_load(ap, tf);
310 ap->ops->exec_command(ap, tf);
311}
312
313/**
Edward Falk0baab862005-06-02 18:17:13 -0400314 * ata_tf_read_pio - input device's ATA taskfile shadow registers
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 * @ap: Port from which input is read
316 * @tf: ATA taskfile register set for storing input
317 *
318 * Reads ATA taskfile registers for currently-selected device
319 * into @tf.
320 *
321 * LOCKING:
322 * Inherited from caller.
323 */
324
325static void ata_tf_read_pio(struct ata_port *ap, struct ata_taskfile *tf)
326{
327 struct ata_ioports *ioaddr = &ap->ioaddr;
328
Jeff Garzikac19bff2005-10-29 13:58:21 -0400329 tf->command = ata_check_status(ap);
Jeff Garzik0169e282005-10-29 21:25:10 -0400330 tf->feature = inb(ioaddr->error_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 tf->nsect = inb(ioaddr->nsect_addr);
332 tf->lbal = inb(ioaddr->lbal_addr);
333 tf->lbam = inb(ioaddr->lbam_addr);
334 tf->lbah = inb(ioaddr->lbah_addr);
335 tf->device = inb(ioaddr->device_addr);
336
337 if (tf->flags & ATA_TFLAG_LBA48) {
338 outb(tf->ctl | ATA_HOB, ioaddr->ctl_addr);
339 tf->hob_feature = inb(ioaddr->error_addr);
340 tf->hob_nsect = inb(ioaddr->nsect_addr);
341 tf->hob_lbal = inb(ioaddr->lbal_addr);
342 tf->hob_lbam = inb(ioaddr->lbam_addr);
343 tf->hob_lbah = inb(ioaddr->lbah_addr);
344 }
345}
346
347/**
348 * ata_tf_read_mmio - input device's ATA taskfile shadow registers
349 * @ap: Port from which input is read
350 * @tf: ATA taskfile register set for storing input
351 *
352 * Reads ATA taskfile registers for currently-selected device
353 * into @tf via MMIO.
354 *
355 * LOCKING:
356 * Inherited from caller.
357 */
358
359static void ata_tf_read_mmio(struct ata_port *ap, struct ata_taskfile *tf)
360{
361 struct ata_ioports *ioaddr = &ap->ioaddr;
362
Jeff Garzikac19bff2005-10-29 13:58:21 -0400363 tf->command = ata_check_status(ap);
Jeff Garzik0169e282005-10-29 21:25:10 -0400364 tf->feature = readb((void __iomem *)ioaddr->error_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 tf->nsect = readb((void __iomem *)ioaddr->nsect_addr);
366 tf->lbal = readb((void __iomem *)ioaddr->lbal_addr);
367 tf->lbam = readb((void __iomem *)ioaddr->lbam_addr);
368 tf->lbah = readb((void __iomem *)ioaddr->lbah_addr);
369 tf->device = readb((void __iomem *)ioaddr->device_addr);
370
371 if (tf->flags & ATA_TFLAG_LBA48) {
372 writeb(tf->ctl | ATA_HOB, (void __iomem *) ap->ioaddr.ctl_addr);
373 tf->hob_feature = readb((void __iomem *)ioaddr->error_addr);
374 tf->hob_nsect = readb((void __iomem *)ioaddr->nsect_addr);
375 tf->hob_lbal = readb((void __iomem *)ioaddr->lbal_addr);
376 tf->hob_lbam = readb((void __iomem *)ioaddr->lbam_addr);
377 tf->hob_lbah = readb((void __iomem *)ioaddr->lbah_addr);
378 }
379}
380
Edward Falk0baab862005-06-02 18:17:13 -0400381
382/**
383 * ata_tf_read - input device's ATA taskfile shadow registers
384 * @ap: Port from which input is read
385 * @tf: ATA taskfile register set for storing input
386 *
387 * Reads ATA taskfile registers for currently-selected device
388 * into @tf.
389 *
390 * Reads nsect, lbal, lbam, lbah, and device. If ATA_TFLAG_LBA48
391 * is set, also reads the hob registers.
392 *
393 * May be used as the tf_read() entry in ata_port_operations.
394 *
395 * LOCKING:
396 * Inherited from caller.
397 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398void ata_tf_read(struct ata_port *ap, struct ata_taskfile *tf)
399{
400 if (ap->flags & ATA_FLAG_MMIO)
401 ata_tf_read_mmio(ap, tf);
402 else
403 ata_tf_read_pio(ap, tf);
404}
405
406/**
407 * ata_check_status_pio - Read device status reg & clear interrupt
408 * @ap: port where the device is
409 *
410 * Reads ATA taskfile status register for currently-selected device
Edward Falk0baab862005-06-02 18:17:13 -0400411 * and return its value. This also clears pending interrupts
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 * from this device
413 *
414 * LOCKING:
415 * Inherited from caller.
416 */
417static u8 ata_check_status_pio(struct ata_port *ap)
418{
419 return inb(ap->ioaddr.status_addr);
420}
421
422/**
423 * ata_check_status_mmio - Read device status reg & clear interrupt
424 * @ap: port where the device is
425 *
426 * Reads ATA taskfile status register for currently-selected device
Edward Falk0baab862005-06-02 18:17:13 -0400427 * via MMIO and return its value. This also clears pending interrupts
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 * from this device
429 *
430 * LOCKING:
431 * Inherited from caller.
432 */
433static u8 ata_check_status_mmio(struct ata_port *ap)
434{
435 return readb((void __iomem *) ap->ioaddr.status_addr);
436}
437
Edward Falk0baab862005-06-02 18:17:13 -0400438
439/**
440 * ata_check_status - Read device status reg & clear interrupt
441 * @ap: port where the device is
442 *
443 * Reads ATA taskfile status register for currently-selected device
444 * and return its value. This also clears pending interrupts
445 * from this device
446 *
447 * May be used as the check_status() entry in ata_port_operations.
448 *
449 * LOCKING:
450 * Inherited from caller.
451 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452u8 ata_check_status(struct ata_port *ap)
453{
454 if (ap->flags & ATA_FLAG_MMIO)
455 return ata_check_status_mmio(ap);
456 return ata_check_status_pio(ap);
457}
458
Edward Falk0baab862005-06-02 18:17:13 -0400459
460/**
461 * ata_altstatus - Read device alternate status reg
462 * @ap: port where the device is
463 *
464 * Reads ATA taskfile alternate status register for
465 * currently-selected device and return its value.
466 *
467 * Note: may NOT be used as the check_altstatus() entry in
468 * ata_port_operations.
469 *
470 * LOCKING:
471 * Inherited from caller.
472 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473u8 ata_altstatus(struct ata_port *ap)
474{
475 if (ap->ops->check_altstatus)
476 return ap->ops->check_altstatus(ap);
477
478 if (ap->flags & ATA_FLAG_MMIO)
479 return readb((void __iomem *)ap->ioaddr.altstatus_addr);
480 return inb(ap->ioaddr.altstatus_addr);
481}
482
Edward Falk0baab862005-06-02 18:17:13 -0400483
484/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 * ata_tf_to_fis - Convert ATA taskfile to SATA FIS structure
486 * @tf: Taskfile to convert
487 * @fis: Buffer into which data will output
488 * @pmp: Port multiplier port
489 *
490 * Converts a standard ATA taskfile to a Serial ATA
491 * FIS structure (Register - Host to Device).
492 *
493 * LOCKING:
494 * Inherited from caller.
495 */
496
Jeff Garzik057ace52005-10-22 14:27:05 -0400497void ata_tf_to_fis(const struct ata_taskfile *tf, u8 *fis, u8 pmp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498{
499 fis[0] = 0x27; /* Register - Host to Device FIS */
500 fis[1] = (pmp & 0xf) | (1 << 7); /* Port multiplier number,
501 bit 7 indicates Command FIS */
502 fis[2] = tf->command;
503 fis[3] = tf->feature;
504
505 fis[4] = tf->lbal;
506 fis[5] = tf->lbam;
507 fis[6] = tf->lbah;
508 fis[7] = tf->device;
509
510 fis[8] = tf->hob_lbal;
511 fis[9] = tf->hob_lbam;
512 fis[10] = tf->hob_lbah;
513 fis[11] = tf->hob_feature;
514
515 fis[12] = tf->nsect;
516 fis[13] = tf->hob_nsect;
517 fis[14] = 0;
518 fis[15] = tf->ctl;
519
520 fis[16] = 0;
521 fis[17] = 0;
522 fis[18] = 0;
523 fis[19] = 0;
524}
525
526/**
527 * ata_tf_from_fis - Convert SATA FIS to ATA taskfile
528 * @fis: Buffer from which data will be input
529 * @tf: Taskfile to output
530 *
Mark Lorde12a1be2005-11-12 18:55:45 -0500531 * Converts a serial ATA FIS structure to a standard ATA taskfile.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 *
533 * LOCKING:
534 * Inherited from caller.
535 */
536
Jeff Garzik057ace52005-10-22 14:27:05 -0400537void ata_tf_from_fis(const u8 *fis, struct ata_taskfile *tf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538{
539 tf->command = fis[2]; /* status */
540 tf->feature = fis[3]; /* error */
541
542 tf->lbal = fis[4];
543 tf->lbam = fis[5];
544 tf->lbah = fis[6];
545 tf->device = fis[7];
546
547 tf->hob_lbal = fis[8];
548 tf->hob_lbam = fis[9];
549 tf->hob_lbah = fis[10];
550
551 tf->nsect = fis[12];
552 tf->hob_nsect = fis[13];
553}
554
Albert Lee8cbd6df2005-10-12 15:06:27 +0800555static const u8 ata_rw_cmds[] = {
556 /* pio multi */
557 ATA_CMD_READ_MULTI,
558 ATA_CMD_WRITE_MULTI,
559 ATA_CMD_READ_MULTI_EXT,
560 ATA_CMD_WRITE_MULTI_EXT,
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100561 0,
562 0,
563 0,
564 ATA_CMD_WRITE_MULTI_FUA_EXT,
Albert Lee8cbd6df2005-10-12 15:06:27 +0800565 /* pio */
566 ATA_CMD_PIO_READ,
567 ATA_CMD_PIO_WRITE,
568 ATA_CMD_PIO_READ_EXT,
569 ATA_CMD_PIO_WRITE_EXT,
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100570 0,
571 0,
572 0,
573 0,
Albert Lee8cbd6df2005-10-12 15:06:27 +0800574 /* dma */
575 ATA_CMD_READ,
576 ATA_CMD_WRITE,
577 ATA_CMD_READ_EXT,
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100578 ATA_CMD_WRITE_EXT,
579 0,
580 0,
581 0,
582 ATA_CMD_WRITE_FUA_EXT
Albert Lee8cbd6df2005-10-12 15:06:27 +0800583};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584
585/**
Albert Lee8cbd6df2005-10-12 15:06:27 +0800586 * ata_rwcmd_protocol - set taskfile r/w commands and protocol
587 * @qc: command to examine and configure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 *
Albert Lee8cbd6df2005-10-12 15:06:27 +0800589 * Examine the device configuration and tf->flags to calculate
590 * the proper read/write commands and protocol to use.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 *
592 * LOCKING:
593 * caller.
594 */
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100595int ata_rwcmd_protocol(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596{
Albert Lee8cbd6df2005-10-12 15:06:27 +0800597 struct ata_taskfile *tf = &qc->tf;
598 struct ata_device *dev = qc->dev;
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100599 u8 cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100601 int index, fua, lba48, write;
Albert Lee8cbd6df2005-10-12 15:06:27 +0800602
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100603 fua = (tf->flags & ATA_TFLAG_FUA) ? 4 : 0;
Albert Lee8cbd6df2005-10-12 15:06:27 +0800604 lba48 = (tf->flags & ATA_TFLAG_LBA48) ? 2 : 0;
605 write = (tf->flags & ATA_TFLAG_WRITE) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606
Albert Lee8cbd6df2005-10-12 15:06:27 +0800607 if (dev->flags & ATA_DFLAG_PIO) {
608 tf->protocol = ATA_PROT_PIO;
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100609 index = dev->multi_count ? 0 : 8;
Alan Cox8d238e02006-01-17 20:50:31 +0000610 } else if (lba48 && (qc->ap->flags & ATA_FLAG_PIO_LBA48)) {
611 /* Unable to use DMA due to host limitation */
612 tf->protocol = ATA_PROT_PIO;
613 index = dev->multi_count ? 0 : 4;
Albert Lee8cbd6df2005-10-12 15:06:27 +0800614 } else {
615 tf->protocol = ATA_PROT_DMA;
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100616 index = 16;
Albert Lee8cbd6df2005-10-12 15:06:27 +0800617 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100619 cmd = ata_rw_cmds[index + fua + lba48 + write];
620 if (cmd) {
621 tf->command = cmd;
622 return 0;
623 }
624 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625}
626
Arjan van de Ven98ac62d2005-11-28 10:06:23 +0100627static const char * const xfer_mode_str[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 "UDMA/16",
629 "UDMA/25",
630 "UDMA/33",
631 "UDMA/44",
632 "UDMA/66",
633 "UDMA/100",
634 "UDMA/133",
635 "UDMA7",
636 "MWDMA0",
637 "MWDMA1",
638 "MWDMA2",
639 "PIO0",
640 "PIO1",
641 "PIO2",
642 "PIO3",
643 "PIO4",
644};
645
646/**
647 * ata_udma_string - convert UDMA bit offset to string
648 * @mask: mask of bits supported; only highest bit counts.
649 *
650 * Determine string which represents the highest speed
651 * (highest bit in @udma_mask).
652 *
653 * LOCKING:
654 * None.
655 *
656 * RETURNS:
657 * Constant C string representing highest speed listed in
658 * @udma_mask, or the constant C string "<n/a>".
659 */
660
661static const char *ata_mode_string(unsigned int mask)
662{
663 int i;
664
665 for (i = 7; i >= 0; i--)
666 if (mask & (1 << i))
667 goto out;
668 for (i = ATA_SHIFT_MWDMA + 2; i >= ATA_SHIFT_MWDMA; i--)
669 if (mask & (1 << i))
670 goto out;
671 for (i = ATA_SHIFT_PIO + 4; i >= ATA_SHIFT_PIO; i--)
672 if (mask & (1 << i))
673 goto out;
674
675 return "<n/a>";
676
677out:
678 return xfer_mode_str[i];
679}
680
681/**
682 * ata_pio_devchk - PATA device presence detection
683 * @ap: ATA channel to examine
684 * @device: Device to examine (starting at zero)
685 *
686 * This technique was originally described in
687 * Hale Landis's ATADRVR (www.ata-atapi.com), and
688 * later found its way into the ATA/ATAPI spec.
689 *
690 * Write a pattern to the ATA shadow registers,
691 * and if a device is present, it will respond by
692 * correctly storing and echoing back the
693 * ATA shadow register contents.
694 *
695 * LOCKING:
696 * caller.
697 */
698
699static unsigned int ata_pio_devchk(struct ata_port *ap,
700 unsigned int device)
701{
702 struct ata_ioports *ioaddr = &ap->ioaddr;
703 u8 nsect, lbal;
704
705 ap->ops->dev_select(ap, device);
706
707 outb(0x55, ioaddr->nsect_addr);
708 outb(0xaa, ioaddr->lbal_addr);
709
710 outb(0xaa, ioaddr->nsect_addr);
711 outb(0x55, ioaddr->lbal_addr);
712
713 outb(0x55, ioaddr->nsect_addr);
714 outb(0xaa, ioaddr->lbal_addr);
715
716 nsect = inb(ioaddr->nsect_addr);
717 lbal = inb(ioaddr->lbal_addr);
718
719 if ((nsect == 0x55) && (lbal == 0xaa))
720 return 1; /* we found a device */
721
722 return 0; /* nothing found */
723}
724
725/**
726 * ata_mmio_devchk - PATA device presence detection
727 * @ap: ATA channel to examine
728 * @device: Device to examine (starting at zero)
729 *
730 * This technique was originally described in
731 * Hale Landis's ATADRVR (www.ata-atapi.com), and
732 * later found its way into the ATA/ATAPI spec.
733 *
734 * Write a pattern to the ATA shadow registers,
735 * and if a device is present, it will respond by
736 * correctly storing and echoing back the
737 * ATA shadow register contents.
738 *
739 * LOCKING:
740 * caller.
741 */
742
743static unsigned int ata_mmio_devchk(struct ata_port *ap,
744 unsigned int device)
745{
746 struct ata_ioports *ioaddr = &ap->ioaddr;
747 u8 nsect, lbal;
748
749 ap->ops->dev_select(ap, device);
750
751 writeb(0x55, (void __iomem *) ioaddr->nsect_addr);
752 writeb(0xaa, (void __iomem *) ioaddr->lbal_addr);
753
754 writeb(0xaa, (void __iomem *) ioaddr->nsect_addr);
755 writeb(0x55, (void __iomem *) ioaddr->lbal_addr);
756
757 writeb(0x55, (void __iomem *) ioaddr->nsect_addr);
758 writeb(0xaa, (void __iomem *) ioaddr->lbal_addr);
759
760 nsect = readb((void __iomem *) ioaddr->nsect_addr);
761 lbal = readb((void __iomem *) ioaddr->lbal_addr);
762
763 if ((nsect == 0x55) && (lbal == 0xaa))
764 return 1; /* we found a device */
765
766 return 0; /* nothing found */
767}
768
769/**
770 * ata_devchk - PATA device presence detection
771 * @ap: ATA channel to examine
772 * @device: Device to examine (starting at zero)
773 *
774 * Dispatch ATA device presence detection, depending
775 * on whether we are using PIO or MMIO to talk to the
776 * ATA shadow registers.
777 *
778 * LOCKING:
779 * caller.
780 */
781
782static unsigned int ata_devchk(struct ata_port *ap,
783 unsigned int device)
784{
785 if (ap->flags & ATA_FLAG_MMIO)
786 return ata_mmio_devchk(ap, device);
787 return ata_pio_devchk(ap, device);
788}
789
790/**
791 * ata_dev_classify - determine device type based on ATA-spec signature
792 * @tf: ATA taskfile register set for device to be identified
793 *
794 * Determine from taskfile register contents whether a device is
795 * ATA or ATAPI, as per "Signature and persistence" section
796 * of ATA/PI spec (volume 1, sect 5.14).
797 *
798 * LOCKING:
799 * None.
800 *
801 * RETURNS:
802 * Device type, %ATA_DEV_ATA, %ATA_DEV_ATAPI, or %ATA_DEV_UNKNOWN
803 * the event of failure.
804 */
805
Jeff Garzik057ace52005-10-22 14:27:05 -0400806unsigned int ata_dev_classify(const struct ata_taskfile *tf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807{
808 /* Apple's open source Darwin code hints that some devices only
809 * put a proper signature into the LBA mid/high registers,
810 * So, we only check those. It's sufficient for uniqueness.
811 */
812
813 if (((tf->lbam == 0) && (tf->lbah == 0)) ||
814 ((tf->lbam == 0x3c) && (tf->lbah == 0xc3))) {
815 DPRINTK("found ATA device by sig\n");
816 return ATA_DEV_ATA;
817 }
818
819 if (((tf->lbam == 0x14) && (tf->lbah == 0xeb)) ||
820 ((tf->lbam == 0x69) && (tf->lbah == 0x96))) {
821 DPRINTK("found ATAPI device by sig\n");
822 return ATA_DEV_ATAPI;
823 }
824
825 DPRINTK("unknown device\n");
826 return ATA_DEV_UNKNOWN;
827}
828
829/**
830 * ata_dev_try_classify - Parse returned ATA device signature
831 * @ap: ATA channel to examine
832 * @device: Device to examine (starting at zero)
Tejun Heob4dc7622006-01-24 17:05:22 +0900833 * @r_err: Value of error register on completion
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 *
835 * After an event -- SRST, E.D.D., or SATA COMRESET -- occurs,
836 * an ATA/ATAPI-defined set of values is placed in the ATA
837 * shadow registers, indicating the results of device detection
838 * and diagnostics.
839 *
840 * Select the ATA device, and read the values from the ATA shadow
841 * registers. Then parse according to the Error register value,
842 * and the spec-defined values examined by ata_dev_classify().
843 *
844 * LOCKING:
845 * caller.
Tejun Heob4dc7622006-01-24 17:05:22 +0900846 *
847 * RETURNS:
848 * Device type - %ATA_DEV_ATA, %ATA_DEV_ATAPI or %ATA_DEV_NONE.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 */
850
Tejun Heob4dc7622006-01-24 17:05:22 +0900851static unsigned int
852ata_dev_try_classify(struct ata_port *ap, unsigned int device, u8 *r_err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 struct ata_taskfile tf;
855 unsigned int class;
856 u8 err;
857
858 ap->ops->dev_select(ap, device);
859
860 memset(&tf, 0, sizeof(tf));
861
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 ap->ops->tf_read(ap, &tf);
Jeff Garzik0169e282005-10-29 21:25:10 -0400863 err = tf.feature;
Tejun Heob4dc7622006-01-24 17:05:22 +0900864 if (r_err)
865 *r_err = err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866
867 /* see if device passed diags */
868 if (err == 1)
869 /* do nothing */ ;
870 else if ((device == 0) && (err == 0x81))
871 /* do nothing */ ;
872 else
Tejun Heob4dc7622006-01-24 17:05:22 +0900873 return ATA_DEV_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874
Tejun Heob4dc7622006-01-24 17:05:22 +0900875 /* determine if device is ATA or ATAPI */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 class = ata_dev_classify(&tf);
Tejun Heob4dc7622006-01-24 17:05:22 +0900877
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 if (class == ATA_DEV_UNKNOWN)
Tejun Heob4dc7622006-01-24 17:05:22 +0900879 return ATA_DEV_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 if ((class == ATA_DEV_ATA) && (ata_chk_status(ap) == 0))
Tejun Heob4dc7622006-01-24 17:05:22 +0900881 return ATA_DEV_NONE;
882 return class;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883}
884
885/**
886 * ata_dev_id_string - Convert IDENTIFY DEVICE page into string
887 * @id: IDENTIFY DEVICE results we will examine
888 * @s: string into which data is output
889 * @ofs: offset into identify device page
890 * @len: length of string to return. must be an even number.
891 *
892 * The strings in the IDENTIFY DEVICE page are broken up into
893 * 16-bit chunks. Run through the string, and output each
894 * 8-bit chunk linearly, regardless of platform.
895 *
896 * LOCKING:
897 * caller.
898 */
899
Jeff Garzik057ace52005-10-22 14:27:05 -0400900void ata_dev_id_string(const u16 *id, unsigned char *s,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 unsigned int ofs, unsigned int len)
902{
903 unsigned int c;
904
905 while (len > 0) {
906 c = id[ofs] >> 8;
907 *s = c;
908 s++;
909
910 c = id[ofs] & 0xff;
911 *s = c;
912 s++;
913
914 ofs++;
915 len -= 2;
916 }
917}
918
Edward Falk0baab862005-06-02 18:17:13 -0400919
920/**
921 * ata_noop_dev_select - Select device 0/1 on ATA bus
922 * @ap: ATA channel to manipulate
923 * @device: ATA device (numbered from zero) to select
924 *
925 * This function performs no actual function.
926 *
927 * May be used as the dev_select() entry in ata_port_operations.
928 *
929 * LOCKING:
930 * caller.
931 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932void ata_noop_dev_select (struct ata_port *ap, unsigned int device)
933{
934}
935
Edward Falk0baab862005-06-02 18:17:13 -0400936
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937/**
938 * ata_std_dev_select - Select device 0/1 on ATA bus
939 * @ap: ATA channel to manipulate
940 * @device: ATA device (numbered from zero) to select
941 *
942 * Use the method defined in the ATA specification to
943 * make either device 0, or device 1, active on the
Edward Falk0baab862005-06-02 18:17:13 -0400944 * ATA channel. Works with both PIO and MMIO.
945 *
946 * May be used as the dev_select() entry in ata_port_operations.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 *
948 * LOCKING:
949 * caller.
950 */
951
952void ata_std_dev_select (struct ata_port *ap, unsigned int device)
953{
954 u8 tmp;
955
956 if (device == 0)
957 tmp = ATA_DEVICE_OBS;
958 else
959 tmp = ATA_DEVICE_OBS | ATA_DEV1;
960
961 if (ap->flags & ATA_FLAG_MMIO) {
962 writeb(tmp, (void __iomem *) ap->ioaddr.device_addr);
963 } else {
964 outb(tmp, ap->ioaddr.device_addr);
965 }
966 ata_pause(ap); /* needed; also flushes, for mmio */
967}
968
969/**
970 * ata_dev_select - Select device 0/1 on ATA bus
971 * @ap: ATA channel to manipulate
972 * @device: ATA device (numbered from zero) to select
973 * @wait: non-zero to wait for Status register BSY bit to clear
974 * @can_sleep: non-zero if context allows sleeping
975 *
976 * Use the method defined in the ATA specification to
977 * make either device 0, or device 1, active on the
978 * ATA channel.
979 *
980 * This is a high-level version of ata_std_dev_select(),
981 * which additionally provides the services of inserting
982 * the proper pauses and status polling, where needed.
983 *
984 * LOCKING:
985 * caller.
986 */
987
988void ata_dev_select(struct ata_port *ap, unsigned int device,
989 unsigned int wait, unsigned int can_sleep)
990{
991 VPRINTK("ENTER, ata%u: device %u, wait %u\n",
992 ap->id, device, wait);
993
994 if (wait)
995 ata_wait_idle(ap);
996
997 ap->ops->dev_select(ap, device);
998
999 if (wait) {
1000 if (can_sleep && ap->device[device].class == ATA_DEV_ATAPI)
1001 msleep(150);
1002 ata_wait_idle(ap);
1003 }
1004}
1005
1006/**
1007 * ata_dump_id - IDENTIFY DEVICE info debugging output
1008 * @dev: Device whose IDENTIFY DEVICE page we will dump
1009 *
1010 * Dump selected 16-bit words from a detected device's
1011 * IDENTIFY PAGE page.
1012 *
1013 * LOCKING:
1014 * caller.
1015 */
1016
Jeff Garzik057ace52005-10-22 14:27:05 -04001017static inline void ata_dump_id(const struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018{
1019 DPRINTK("49==0x%04x "
1020 "53==0x%04x "
1021 "63==0x%04x "
1022 "64==0x%04x "
1023 "75==0x%04x \n",
1024 dev->id[49],
1025 dev->id[53],
1026 dev->id[63],
1027 dev->id[64],
1028 dev->id[75]);
1029 DPRINTK("80==0x%04x "
1030 "81==0x%04x "
1031 "82==0x%04x "
1032 "83==0x%04x "
1033 "84==0x%04x \n",
1034 dev->id[80],
1035 dev->id[81],
1036 dev->id[82],
1037 dev->id[83],
1038 dev->id[84]);
1039 DPRINTK("88==0x%04x "
1040 "93==0x%04x\n",
1041 dev->id[88],
1042 dev->id[93]);
1043}
1044
Alan Cox11e29e22005-10-21 18:46:32 -04001045/*
1046 * Compute the PIO modes available for this device. This is not as
1047 * trivial as it seems if we must consider early devices correctly.
1048 *
1049 * FIXME: pre IDE drive timing (do we care ?).
1050 */
1051
Jeff Garzik057ace52005-10-22 14:27:05 -04001052static unsigned int ata_pio_modes(const struct ata_device *adev)
Alan Cox11e29e22005-10-21 18:46:32 -04001053{
1054 u16 modes;
1055
Alan Coxffa29452006-01-09 17:14:40 +00001056 /* Usual case. Word 53 indicates word 64 is valid */
1057 if (adev->id[ATA_ID_FIELD_VALID] & (1 << 1)) {
Alan Cox11e29e22005-10-21 18:46:32 -04001058 modes = adev->id[ATA_ID_PIO_MODES] & 0x03;
1059 modes <<= 3;
1060 modes |= 0x7;
1061 return modes;
1062 }
1063
Alan Coxffa29452006-01-09 17:14:40 +00001064 /* If word 64 isn't valid then Word 51 high byte holds the PIO timing
1065 number for the maximum. Turn it into a mask and return it */
1066 modes = (2 << ((adev->id[ATA_ID_OLD_PIO_MODES] >> 8) & 0xFF)) - 1 ;
Alan Cox11e29e22005-10-21 18:46:32 -04001067 return modes;
Alan Coxffa29452006-01-09 17:14:40 +00001068 /* But wait.. there's more. Design your standards by committee and
1069 you too can get a free iordy field to process. However its the
1070 speeds not the modes that are supported... Note drivers using the
1071 timing API will get this right anyway */
Alan Cox11e29e22005-10-21 18:46:32 -04001072}
1073
Tejun Heo95064372006-01-23 13:09:37 +09001074static inline void
1075ata_queue_packet_task(struct ata_port *ap)
1076{
1077 queue_work(ata_wq, &ap->packet_task);
1078}
1079
1080static inline void
1081ata_queue_pio_task(struct ata_port *ap)
1082{
1083 queue_work(ata_wq, &ap->pio_task);
1084}
1085
1086static inline void
1087ata_queue_delayed_pio_task(struct ata_port *ap, unsigned long delay)
1088{
1089 queue_delayed_work(ata_wq, &ap->pio_task, delay);
1090}
1091
Tejun Heo77853bf2006-01-23 13:09:36 +09001092void ata_qc_complete_internal(struct ata_queued_cmd *qc)
Tejun Heoa2a7a662005-12-13 14:48:31 +09001093{
Tejun Heo77853bf2006-01-23 13:09:36 +09001094 struct completion *waiting = qc->private_data;
Tejun Heoa2a7a662005-12-13 14:48:31 +09001095
Tejun Heo77853bf2006-01-23 13:09:36 +09001096 qc->ap->ops->tf_read(qc->ap, &qc->tf);
Tejun Heoa2a7a662005-12-13 14:48:31 +09001097 complete(waiting);
Tejun Heoa2a7a662005-12-13 14:48:31 +09001098}
1099
1100/**
1101 * ata_exec_internal - execute libata internal command
1102 * @ap: Port to which the command is sent
1103 * @dev: Device to which the command is sent
1104 * @tf: Taskfile registers for the command and the result
1105 * @dma_dir: Data tranfer direction of the command
1106 * @buf: Data buffer of the command
1107 * @buflen: Length of data buffer
1108 *
1109 * Executes libata internal command with timeout. @tf contains
1110 * command on entry and result on return. Timeout and error
1111 * conditions are reported via return value. No recovery action
1112 * is taken after a command times out. It's caller's duty to
1113 * clean up after timeout.
1114 *
1115 * LOCKING:
1116 * None. Should be called with kernel context, might sleep.
1117 */
1118
1119static unsigned
1120ata_exec_internal(struct ata_port *ap, struct ata_device *dev,
1121 struct ata_taskfile *tf,
1122 int dma_dir, void *buf, unsigned int buflen)
1123{
1124 u8 command = tf->command;
1125 struct ata_queued_cmd *qc;
1126 DECLARE_COMPLETION(wait);
1127 unsigned long flags;
Tejun Heo77853bf2006-01-23 13:09:36 +09001128 unsigned int err_mask;
Tejun Heoa2a7a662005-12-13 14:48:31 +09001129
1130 spin_lock_irqsave(&ap->host_set->lock, flags);
1131
1132 qc = ata_qc_new_init(ap, dev);
1133 BUG_ON(qc == NULL);
1134
1135 qc->tf = *tf;
1136 qc->dma_dir = dma_dir;
1137 if (dma_dir != DMA_NONE) {
1138 ata_sg_init_one(qc, buf, buflen);
1139 qc->nsect = buflen / ATA_SECT_SIZE;
1140 }
1141
Tejun Heo77853bf2006-01-23 13:09:36 +09001142 qc->private_data = &wait;
Tejun Heoa2a7a662005-12-13 14:48:31 +09001143 qc->complete_fn = ata_qc_complete_internal;
1144
Tejun Heo9a3d9eb2006-01-23 13:09:36 +09001145 qc->err_mask = ata_qc_issue(qc);
1146 if (qc->err_mask)
Tejun Heo8e436af2006-01-23 13:09:36 +09001147 ata_qc_complete(qc);
Tejun Heoa2a7a662005-12-13 14:48:31 +09001148
1149 spin_unlock_irqrestore(&ap->host_set->lock, flags);
1150
1151 if (!wait_for_completion_timeout(&wait, ATA_TMOUT_INTERNAL)) {
1152 spin_lock_irqsave(&ap->host_set->lock, flags);
1153
1154 /* We're racing with irq here. If we lose, the
1155 * following test prevents us from completing the qc
1156 * again. If completion irq occurs after here but
1157 * before the caller cleans up, it will result in a
1158 * spurious interrupt. We can live with that.
1159 */
Tejun Heo77853bf2006-01-23 13:09:36 +09001160 if (qc->flags & ATA_QCFLAG_ACTIVE) {
Tejun Heo11a56d22006-01-23 13:09:36 +09001161 qc->err_mask = AC_ERR_TIMEOUT;
Tejun Heoa2a7a662005-12-13 14:48:31 +09001162 ata_qc_complete(qc);
1163 printk(KERN_WARNING "ata%u: qc timeout (cmd 0x%x)\n",
1164 ap->id, command);
1165 }
1166
1167 spin_unlock_irqrestore(&ap->host_set->lock, flags);
1168 }
1169
Tejun Heo77853bf2006-01-23 13:09:36 +09001170 *tf = qc->tf;
1171 err_mask = qc->err_mask;
1172
1173 ata_qc_free(qc);
1174
1175 return err_mask;
Tejun Heoa2a7a662005-12-13 14:48:31 +09001176}
1177
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178/**
Alan Cox1bc4ccf2006-01-09 17:18:14 +00001179 * ata_pio_need_iordy - check if iordy needed
1180 * @adev: ATA device
1181 *
1182 * Check if the current speed of the device requires IORDY. Used
1183 * by various controllers for chip configuration.
1184 */
1185
1186unsigned int ata_pio_need_iordy(const struct ata_device *adev)
1187{
1188 int pio;
1189 int speed = adev->pio_mode - XFER_PIO_0;
1190
1191 if (speed < 2)
1192 return 0;
1193 if (speed > 2)
1194 return 1;
1195
1196 /* If we have no drive specific rule, then PIO 2 is non IORDY */
1197
1198 if (adev->id[ATA_ID_FIELD_VALID] & 2) { /* EIDE */
1199 pio = adev->id[ATA_ID_EIDE_PIO];
1200 /* Is the speed faster than the drive allows non IORDY ? */
1201 if (pio) {
1202 /* This is cycle times not frequency - watch the logic! */
1203 if (pio > 240) /* PIO2 is 240nS per cycle */
1204 return 1;
1205 return 0;
1206 }
1207 }
1208 return 0;
1209}
1210
1211/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 * ata_dev_identify - obtain IDENTIFY x DEVICE page
1213 * @ap: port on which device we wish to probe resides
1214 * @device: device bus address, starting at zero
1215 *
1216 * Following bus reset, we issue the IDENTIFY [PACKET] DEVICE
1217 * command, and read back the 512-byte device information page.
1218 * The device information page is fed to us via the standard
1219 * PIO-IN protocol, but we hand-code it here. (TODO: investigate
1220 * using standard PIO-IN paths)
1221 *
1222 * After reading the device information page, we use several
1223 * bits of information from it to initialize data structures
1224 * that will be used during the lifetime of the ata_device.
1225 * Other data from the info page is used to disqualify certain
1226 * older ATA devices we do not wish to support.
1227 *
1228 * LOCKING:
1229 * Inherited from caller. Some functions called by this function
1230 * obtain the host_set lock.
1231 */
1232
1233static void ata_dev_identify(struct ata_port *ap, unsigned int device)
1234{
1235 struct ata_device *dev = &ap->device[device];
Albert Lee8bf62ec2005-05-12 15:29:42 -04001236 unsigned int major_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 u16 tmp;
1238 unsigned long xfer_modes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 unsigned int using_edd;
Tejun Heoa0123702005-12-13 14:49:31 +09001240 struct ata_taskfile tf;
1241 unsigned int err_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 int rc;
1243
1244 if (!ata_dev_present(dev)) {
1245 DPRINTK("ENTER/EXIT (host %u, dev %u) -- nodev\n",
1246 ap->id, device);
1247 return;
1248 }
1249
1250 if (ap->flags & (ATA_FLAG_SRST | ATA_FLAG_SATA_RESET))
1251 using_edd = 0;
1252 else
1253 using_edd = 1;
1254
1255 DPRINTK("ENTER, host %u, dev %u\n", ap->id, device);
1256
1257 assert (dev->class == ATA_DEV_ATA || dev->class == ATA_DEV_ATAPI ||
1258 dev->class == ATA_DEV_NONE);
1259
1260 ata_dev_select(ap, device, 1, 1); /* select device 0/1 */
1261
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262retry:
Tejun Heoa0123702005-12-13 14:49:31 +09001263 ata_tf_init(ap, &tf, device);
1264
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 if (dev->class == ATA_DEV_ATA) {
Tejun Heoa0123702005-12-13 14:49:31 +09001266 tf.command = ATA_CMD_ID_ATA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 DPRINTK("do ATA identify\n");
1268 } else {
Tejun Heoa0123702005-12-13 14:49:31 +09001269 tf.command = ATA_CMD_ID_ATAPI;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 DPRINTK("do ATAPI identify\n");
1271 }
1272
Tejun Heoa0123702005-12-13 14:49:31 +09001273 tf.protocol = ATA_PROT_PIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274
Tejun Heoa0123702005-12-13 14:49:31 +09001275 err_mask = ata_exec_internal(ap, dev, &tf, DMA_FROM_DEVICE,
1276 dev->id, sizeof(dev->id));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277
Tejun Heoa0123702005-12-13 14:49:31 +09001278 if (err_mask) {
1279 if (err_mask & ~AC_ERR_DEV)
1280 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 /*
1283 * arg! EDD works for all test cases, but seems to return
1284 * the ATA signature for some ATAPI devices. Until the
1285 * reason for this is found and fixed, we fix up the mess
1286 * here. If IDENTIFY DEVICE returns command aborted
1287 * (as ATAPI devices do), then we issue an
1288 * IDENTIFY PACKET DEVICE.
1289 *
1290 * ATA software reset (SRST, the default) does not appear
1291 * to have this problem.
1292 */
Albert Lee7c398332005-11-09 13:03:30 +08001293 if ((using_edd) && (dev->class == ATA_DEV_ATA)) {
Tejun Heoa0123702005-12-13 14:49:31 +09001294 u8 err = tf.feature;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 if (err & ATA_ABORTED) {
1296 dev->class = ATA_DEV_ATAPI;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 goto retry;
1298 }
1299 }
1300 goto err_out;
1301 }
1302
1303 swap_buf_le16(dev->id, ATA_ID_WORDS);
1304
1305 /* print device capabilities */
1306 printk(KERN_DEBUG "ata%u: dev %u cfg "
1307 "49:%04x 82:%04x 83:%04x 84:%04x 85:%04x 86:%04x 87:%04x 88:%04x\n",
1308 ap->id, device, dev->id[49],
1309 dev->id[82], dev->id[83], dev->id[84],
1310 dev->id[85], dev->id[86], dev->id[87],
1311 dev->id[88]);
1312
1313 /*
1314 * common ATA, ATAPI feature tests
1315 */
1316
Albert Lee8bf62ec2005-05-12 15:29:42 -04001317 /* we require DMA support (bits 8 of word 49) */
1318 if (!ata_id_has_dma(dev->id)) {
1319 printk(KERN_DEBUG "ata%u: no dma\n", ap->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 goto err_out_nosup;
1321 }
1322
1323 /* quick-n-dirty find max transfer mode; for printk only */
1324 xfer_modes = dev->id[ATA_ID_UDMA_MODES];
1325 if (!xfer_modes)
1326 xfer_modes = (dev->id[ATA_ID_MWDMA_MODES]) << ATA_SHIFT_MWDMA;
Alan Cox11e29e22005-10-21 18:46:32 -04001327 if (!xfer_modes)
1328 xfer_modes = ata_pio_modes(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329
1330 ata_dump_id(dev);
1331
1332 /* ATA-specific feature tests */
1333 if (dev->class == ATA_DEV_ATA) {
1334 if (!ata_id_is_ata(dev->id)) /* sanity check */
1335 goto err_out_nosup;
1336
Albert Lee8bf62ec2005-05-12 15:29:42 -04001337 /* get major version */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 tmp = dev->id[ATA_ID_MAJOR_VER];
Albert Lee8bf62ec2005-05-12 15:29:42 -04001339 for (major_version = 14; major_version >= 1; major_version--)
1340 if (tmp & (1 << major_version))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 break;
1342
Albert Lee8bf62ec2005-05-12 15:29:42 -04001343 /*
1344 * The exact sequence expected by certain pre-ATA4 drives is:
1345 * SRST RESET
1346 * IDENTIFY
1347 * INITIALIZE DEVICE PARAMETERS
1348 * anything else..
1349 * Some drives were very specific about that exact sequence.
1350 */
Albert Lee59a10b12005-10-12 15:09:42 +08001351 if (major_version < 4 || (!ata_id_has_lba(dev->id))) {
Albert Lee8bf62ec2005-05-12 15:29:42 -04001352 ata_dev_init_params(ap, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353
Albert Lee59a10b12005-10-12 15:09:42 +08001354 /* current CHS translation info (id[53-58]) might be
1355 * changed. reread the identify device info.
1356 */
1357 ata_dev_reread_id(ap, dev);
1358 }
1359
Albert Lee8bf62ec2005-05-12 15:29:42 -04001360 if (ata_id_has_lba(dev->id)) {
1361 dev->flags |= ATA_DFLAG_LBA;
1362
1363 if (ata_id_has_lba48(dev->id)) {
1364 dev->flags |= ATA_DFLAG_LBA48;
1365 dev->n_sectors = ata_id_u64(dev->id, 100);
1366 } else {
1367 dev->n_sectors = ata_id_u32(dev->id, 60);
1368 }
1369
1370 /* print device info to dmesg */
1371 printk(KERN_INFO "ata%u: dev %u ATA-%d, max %s, %Lu sectors:%s\n",
1372 ap->id, device,
1373 major_version,
1374 ata_mode_string(xfer_modes),
1375 (unsigned long long)dev->n_sectors,
1376 dev->flags & ATA_DFLAG_LBA48 ? " LBA48" : " LBA");
1377 } else {
1378 /* CHS */
1379
1380 /* Default translation */
1381 dev->cylinders = dev->id[1];
1382 dev->heads = dev->id[3];
1383 dev->sectors = dev->id[6];
1384 dev->n_sectors = dev->cylinders * dev->heads * dev->sectors;
1385
1386 if (ata_id_current_chs_valid(dev->id)) {
1387 /* Current CHS translation is valid. */
1388 dev->cylinders = dev->id[54];
1389 dev->heads = dev->id[55];
1390 dev->sectors = dev->id[56];
1391
1392 dev->n_sectors = ata_id_u32(dev->id, 57);
1393 }
1394
1395 /* print device info to dmesg */
1396 printk(KERN_INFO "ata%u: dev %u ATA-%d, max %s, %Lu sectors: CHS %d/%d/%d\n",
1397 ap->id, device,
1398 major_version,
1399 ata_mode_string(xfer_modes),
1400 (unsigned long long)dev->n_sectors,
1401 (int)dev->cylinders, (int)dev->heads, (int)dev->sectors);
1402
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 }
1404
1405 ap->host->max_cmd_len = 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 }
1407
1408 /* ATAPI-specific feature tests */
Jeff Garzik2c13b7c2005-11-14 14:14:16 -05001409 else if (dev->class == ATA_DEV_ATAPI) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 if (ata_id_is_ata(dev->id)) /* sanity check */
1411 goto err_out_nosup;
1412
1413 rc = atapi_cdb_len(dev->id);
1414 if ((rc < 12) || (rc > ATAPI_CDB_LEN)) {
1415 printk(KERN_WARNING "ata%u: unsupported CDB len\n", ap->id);
1416 goto err_out_nosup;
1417 }
1418 ap->cdb_len = (unsigned int) rc;
1419 ap->host->max_cmd_len = (unsigned char) ap->cdb_len;
1420
1421 /* print device info to dmesg */
1422 printk(KERN_INFO "ata%u: dev %u ATAPI, max %s\n",
1423 ap->id, device,
1424 ata_mode_string(xfer_modes));
1425 }
1426
1427 DPRINTK("EXIT, drv_stat = 0x%x\n", ata_chk_status(ap));
1428 return;
1429
1430err_out_nosup:
1431 printk(KERN_WARNING "ata%u: dev %u not supported, ignoring\n",
1432 ap->id, device);
1433err_out:
1434 dev->class++; /* converts ATA_DEV_xxx into ATA_DEV_xxx_UNSUP */
1435 DPRINTK("EXIT, err\n");
1436}
1437
Brad Campbell6f2f3812005-05-12 15:07:47 -04001438
Jeff Garzik057ace52005-10-22 14:27:05 -04001439static inline u8 ata_dev_knobble(const struct ata_port *ap)
Brad Campbell6f2f3812005-05-12 15:07:47 -04001440{
1441 return ((ap->cbl == ATA_CBL_SATA) && (!ata_id_is_sata(ap->device->id)));
1442}
1443
1444/**
1445 * ata_dev_config - Run device specific handlers and check for
1446 * SATA->PATA bridges
Jeff Garzik8a60a072005-07-31 13:13:24 -04001447 * @ap: Bus
Brad Campbell6f2f3812005-05-12 15:07:47 -04001448 * @i: Device
1449 *
1450 * LOCKING:
1451 */
Jeff Garzik8a60a072005-07-31 13:13:24 -04001452
Brad Campbell6f2f3812005-05-12 15:07:47 -04001453void ata_dev_config(struct ata_port *ap, unsigned int i)
1454{
1455 /* limit bridge transfers to udma5, 200 sectors */
1456 if (ata_dev_knobble(ap)) {
1457 printk(KERN_INFO "ata%u(%u): applying bridge limits\n",
1458 ap->id, ap->device->devno);
1459 ap->udma_mask &= ATA_UDMA5;
1460 ap->host->max_sectors = ATA_MAX_SECTORS;
1461 ap->host->hostt->max_sectors = ATA_MAX_SECTORS;
Alan Cox9d824d02006-01-17 20:51:55 +00001462 ap->device[i].flags |= ATA_DFLAG_LOCK_SECTORS;
Brad Campbell6f2f3812005-05-12 15:07:47 -04001463 }
1464
1465 if (ap->ops->dev_config)
1466 ap->ops->dev_config(ap, &ap->device[i]);
1467}
1468
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469/**
1470 * ata_bus_probe - Reset and probe ATA bus
1471 * @ap: Bus to probe
1472 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04001473 * Master ATA bus probing function. Initiates a hardware-dependent
1474 * bus reset, then attempts to identify any devices found on
1475 * the bus.
1476 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001478 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 *
1480 * RETURNS:
1481 * Zero on success, non-zero on error.
1482 */
1483
1484static int ata_bus_probe(struct ata_port *ap)
1485{
1486 unsigned int i, found = 0;
1487
Tejun Heoc19ba8a2006-01-24 17:05:22 +09001488 if (ap->ops->probe_reset) {
1489 unsigned int classes[ATA_MAX_DEVICES];
1490 int rc;
1491
1492 ata_port_probe(ap);
1493
1494 rc = ap->ops->probe_reset(ap, classes);
1495 if (rc == 0) {
1496 for (i = 0; i < ATA_MAX_DEVICES; i++)
1497 ap->device[i].class = classes[i];
1498 } else {
1499 printk(KERN_ERR "ata%u: probe reset failed, "
1500 "disabling port\n", ap->id);
1501 ata_port_disable(ap);
1502 }
1503 } else
1504 ap->ops->phy_reset(ap);
1505
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1507 goto err_out;
1508
1509 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1510 ata_dev_identify(ap, i);
1511 if (ata_dev_present(&ap->device[i])) {
1512 found = 1;
Brad Campbell6f2f3812005-05-12 15:07:47 -04001513 ata_dev_config(ap,i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 }
1515 }
1516
1517 if ((!found) || (ap->flags & ATA_FLAG_PORT_DISABLED))
1518 goto err_out_disable;
1519
1520 ata_set_mode(ap);
1521 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1522 goto err_out_disable;
1523
1524 return 0;
1525
1526err_out_disable:
1527 ap->ops->port_disable(ap);
1528err_out:
1529 return -1;
1530}
1531
1532/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04001533 * ata_port_probe - Mark port as enabled
1534 * @ap: Port for which we indicate enablement
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04001536 * Modify @ap data structure such that the system
1537 * thinks that the entire port is enabled.
1538 *
1539 * LOCKING: host_set lock, or some other form of
1540 * serialization.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 */
1542
1543void ata_port_probe(struct ata_port *ap)
1544{
1545 ap->flags &= ~ATA_FLAG_PORT_DISABLED;
1546}
1547
1548/**
Tejun Heo3be680b2005-12-19 22:35:02 +09001549 * sata_print_link_status - Print SATA link status
1550 * @ap: SATA port to printk link status about
1551 *
1552 * This function prints link speed and status of a SATA link.
1553 *
1554 * LOCKING:
1555 * None.
1556 */
1557static void sata_print_link_status(struct ata_port *ap)
1558{
1559 u32 sstatus, tmp;
1560 const char *speed;
1561
1562 if (!ap->ops->scr_read)
1563 return;
1564
1565 sstatus = scr_read(ap, SCR_STATUS);
1566
1567 if (sata_dev_present(ap)) {
1568 tmp = (sstatus >> 4) & 0xf;
1569 if (tmp & (1 << 0))
1570 speed = "1.5";
1571 else if (tmp & (1 << 1))
1572 speed = "3.0";
1573 else
1574 speed = "<unknown>";
1575 printk(KERN_INFO "ata%u: SATA link up %s Gbps (SStatus %X)\n",
1576 ap->id, speed, sstatus);
1577 } else {
1578 printk(KERN_INFO "ata%u: SATA link down (SStatus %X)\n",
1579 ap->id, sstatus);
1580 }
1581}
1582
1583/**
Jeff Garzik780a87f2005-05-30 15:41:05 -04001584 * __sata_phy_reset - Wake/reset a low-level SATA PHY
1585 * @ap: SATA port associated with target SATA PHY.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001587 * This function issues commands to standard SATA Sxxx
1588 * PHY registers, to wake up the phy (and device), and
1589 * clear any reset condition.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 *
1591 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001592 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 *
1594 */
1595void __sata_phy_reset(struct ata_port *ap)
1596{
1597 u32 sstatus;
1598 unsigned long timeout = jiffies + (HZ * 5);
1599
1600 if (ap->flags & ATA_FLAG_SATA_RESET) {
Brett Russcdcca892005-03-28 15:10:27 -05001601 /* issue phy wake/reset */
1602 scr_write_flush(ap, SCR_CONTROL, 0x301);
Tejun Heo62ba2842005-06-26 23:27:19 +09001603 /* Couldn't find anything in SATA I/II specs, but
1604 * AHCI-1.1 10.4.2 says at least 1 ms. */
1605 mdelay(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 }
Brett Russcdcca892005-03-28 15:10:27 -05001607 scr_write_flush(ap, SCR_CONTROL, 0x300); /* phy wake/clear reset */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608
1609 /* wait for phy to become ready, if necessary */
1610 do {
1611 msleep(200);
1612 sstatus = scr_read(ap, SCR_STATUS);
1613 if ((sstatus & 0xf) != 1)
1614 break;
1615 } while (time_before(jiffies, timeout));
1616
Tejun Heo3be680b2005-12-19 22:35:02 +09001617 /* print link status */
1618 sata_print_link_status(ap);
Jeff Garzik656563e2005-11-20 03:36:45 -05001619
Tejun Heo3be680b2005-12-19 22:35:02 +09001620 /* TODO: phy layer with polling, timeouts, etc. */
1621 if (sata_dev_present(ap))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 ata_port_probe(ap);
Tejun Heo3be680b2005-12-19 22:35:02 +09001623 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 ata_port_disable(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625
1626 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1627 return;
1628
1629 if (ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT)) {
1630 ata_port_disable(ap);
1631 return;
1632 }
1633
1634 ap->cbl = ATA_CBL_SATA;
1635}
1636
1637/**
Jeff Garzik780a87f2005-05-30 15:41:05 -04001638 * sata_phy_reset - Reset SATA bus.
1639 * @ap: SATA port associated with target SATA PHY.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001641 * This function resets the SATA bus, and then probes
1642 * the bus for devices.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 *
1644 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001645 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 *
1647 */
1648void sata_phy_reset(struct ata_port *ap)
1649{
1650 __sata_phy_reset(ap);
1651 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1652 return;
1653 ata_bus_reset(ap);
1654}
1655
1656/**
Jeff Garzik780a87f2005-05-30 15:41:05 -04001657 * ata_port_disable - Disable port.
1658 * @ap: Port to be disabled.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001660 * Modify @ap data structure such that the system
1661 * thinks that the entire port is disabled, and should
1662 * never attempt to probe or communicate with devices
1663 * on this port.
1664 *
1665 * LOCKING: host_set lock, or some other form of
1666 * serialization.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 */
1668
1669void ata_port_disable(struct ata_port *ap)
1670{
1671 ap->device[0].class = ATA_DEV_NONE;
1672 ap->device[1].class = ATA_DEV_NONE;
1673 ap->flags |= ATA_FLAG_PORT_DISABLED;
1674}
1675
Alan Cox452503f2005-10-21 19:01:32 -04001676/*
1677 * This mode timing computation functionality is ported over from
1678 * drivers/ide/ide-timing.h and was originally written by Vojtech Pavlik
1679 */
1680/*
1681 * PIO 0-5, MWDMA 0-2 and UDMA 0-6 timings (in nanoseconds).
1682 * These were taken from ATA/ATAPI-6 standard, rev 0a, except
1683 * for PIO 5, which is a nonstandard extension and UDMA6, which
1684 * is currently supported only by Maxtor drives.
1685 */
1686
1687static const struct ata_timing ata_timing[] = {
1688
1689 { XFER_UDMA_6, 0, 0, 0, 0, 0, 0, 0, 15 },
1690 { XFER_UDMA_5, 0, 0, 0, 0, 0, 0, 0, 20 },
1691 { XFER_UDMA_4, 0, 0, 0, 0, 0, 0, 0, 30 },
1692 { XFER_UDMA_3, 0, 0, 0, 0, 0, 0, 0, 45 },
1693
1694 { XFER_UDMA_2, 0, 0, 0, 0, 0, 0, 0, 60 },
1695 { XFER_UDMA_1, 0, 0, 0, 0, 0, 0, 0, 80 },
1696 { XFER_UDMA_0, 0, 0, 0, 0, 0, 0, 0, 120 },
1697
1698/* { XFER_UDMA_SLOW, 0, 0, 0, 0, 0, 0, 0, 150 }, */
1699
1700 { XFER_MW_DMA_2, 25, 0, 0, 0, 70, 25, 120, 0 },
1701 { XFER_MW_DMA_1, 45, 0, 0, 0, 80, 50, 150, 0 },
1702 { XFER_MW_DMA_0, 60, 0, 0, 0, 215, 215, 480, 0 },
1703
1704 { XFER_SW_DMA_2, 60, 0, 0, 0, 120, 120, 240, 0 },
1705 { XFER_SW_DMA_1, 90, 0, 0, 0, 240, 240, 480, 0 },
1706 { XFER_SW_DMA_0, 120, 0, 0, 0, 480, 480, 960, 0 },
1707
1708/* { XFER_PIO_5, 20, 50, 30, 100, 50, 30, 100, 0 }, */
1709 { XFER_PIO_4, 25, 70, 25, 120, 70, 25, 120, 0 },
1710 { XFER_PIO_3, 30, 80, 70, 180, 80, 70, 180, 0 },
1711
1712 { XFER_PIO_2, 30, 290, 40, 330, 100, 90, 240, 0 },
1713 { XFER_PIO_1, 50, 290, 93, 383, 125, 100, 383, 0 },
1714 { XFER_PIO_0, 70, 290, 240, 600, 165, 150, 600, 0 },
1715
1716/* { XFER_PIO_SLOW, 120, 290, 240, 960, 290, 240, 960, 0 }, */
1717
1718 { 0xFF }
1719};
1720
1721#define ENOUGH(v,unit) (((v)-1)/(unit)+1)
1722#define EZ(v,unit) ((v)?ENOUGH(v,unit):0)
1723
1724static void ata_timing_quantize(const struct ata_timing *t, struct ata_timing *q, int T, int UT)
1725{
1726 q->setup = EZ(t->setup * 1000, T);
1727 q->act8b = EZ(t->act8b * 1000, T);
1728 q->rec8b = EZ(t->rec8b * 1000, T);
1729 q->cyc8b = EZ(t->cyc8b * 1000, T);
1730 q->active = EZ(t->active * 1000, T);
1731 q->recover = EZ(t->recover * 1000, T);
1732 q->cycle = EZ(t->cycle * 1000, T);
1733 q->udma = EZ(t->udma * 1000, UT);
1734}
1735
1736void ata_timing_merge(const struct ata_timing *a, const struct ata_timing *b,
1737 struct ata_timing *m, unsigned int what)
1738{
1739 if (what & ATA_TIMING_SETUP ) m->setup = max(a->setup, b->setup);
1740 if (what & ATA_TIMING_ACT8B ) m->act8b = max(a->act8b, b->act8b);
1741 if (what & ATA_TIMING_REC8B ) m->rec8b = max(a->rec8b, b->rec8b);
1742 if (what & ATA_TIMING_CYC8B ) m->cyc8b = max(a->cyc8b, b->cyc8b);
1743 if (what & ATA_TIMING_ACTIVE ) m->active = max(a->active, b->active);
1744 if (what & ATA_TIMING_RECOVER) m->recover = max(a->recover, b->recover);
1745 if (what & ATA_TIMING_CYCLE ) m->cycle = max(a->cycle, b->cycle);
1746 if (what & ATA_TIMING_UDMA ) m->udma = max(a->udma, b->udma);
1747}
1748
1749static const struct ata_timing* ata_timing_find_mode(unsigned short speed)
1750{
1751 const struct ata_timing *t;
1752
1753 for (t = ata_timing; t->mode != speed; t++)
Alan Cox91190752005-10-26 12:17:46 -04001754 if (t->mode == 0xFF)
Alan Cox452503f2005-10-21 19:01:32 -04001755 return NULL;
1756 return t;
1757}
1758
1759int ata_timing_compute(struct ata_device *adev, unsigned short speed,
1760 struct ata_timing *t, int T, int UT)
1761{
1762 const struct ata_timing *s;
1763 struct ata_timing p;
1764
1765 /*
1766 * Find the mode.
Albert Lee75b1f2f2005-11-16 17:06:18 +08001767 */
Alan Cox452503f2005-10-21 19:01:32 -04001768
1769 if (!(s = ata_timing_find_mode(speed)))
1770 return -EINVAL;
1771
Albert Lee75b1f2f2005-11-16 17:06:18 +08001772 memcpy(t, s, sizeof(*s));
1773
Alan Cox452503f2005-10-21 19:01:32 -04001774 /*
1775 * If the drive is an EIDE drive, it can tell us it needs extended
1776 * PIO/MW_DMA cycle timing.
1777 */
1778
1779 if (adev->id[ATA_ID_FIELD_VALID] & 2) { /* EIDE drive */
1780 memset(&p, 0, sizeof(p));
1781 if(speed >= XFER_PIO_0 && speed <= XFER_SW_DMA_0) {
1782 if (speed <= XFER_PIO_2) p.cycle = p.cyc8b = adev->id[ATA_ID_EIDE_PIO];
1783 else p.cycle = p.cyc8b = adev->id[ATA_ID_EIDE_PIO_IORDY];
1784 } else if(speed >= XFER_MW_DMA_0 && speed <= XFER_MW_DMA_2) {
1785 p.cycle = adev->id[ATA_ID_EIDE_DMA_MIN];
1786 }
1787 ata_timing_merge(&p, t, t, ATA_TIMING_CYCLE | ATA_TIMING_CYC8B);
1788 }
1789
1790 /*
1791 * Convert the timing to bus clock counts.
1792 */
1793
Albert Lee75b1f2f2005-11-16 17:06:18 +08001794 ata_timing_quantize(t, t, T, UT);
Alan Cox452503f2005-10-21 19:01:32 -04001795
1796 /*
1797 * Even in DMA/UDMA modes we still use PIO access for IDENTIFY, S.M.A.R.T
1798 * and some other commands. We have to ensure that the DMA cycle timing is
1799 * slower/equal than the fastest PIO timing.
1800 */
1801
1802 if (speed > XFER_PIO_4) {
1803 ata_timing_compute(adev, adev->pio_mode, &p, T, UT);
1804 ata_timing_merge(&p, t, t, ATA_TIMING_ALL);
1805 }
1806
1807 /*
1808 * Lenghten active & recovery time so that cycle time is correct.
1809 */
1810
1811 if (t->act8b + t->rec8b < t->cyc8b) {
1812 t->act8b += (t->cyc8b - (t->act8b + t->rec8b)) / 2;
1813 t->rec8b = t->cyc8b - t->act8b;
1814 }
1815
1816 if (t->active + t->recover < t->cycle) {
1817 t->active += (t->cycle - (t->active + t->recover)) / 2;
1818 t->recover = t->cycle - t->active;
1819 }
1820
1821 return 0;
1822}
1823
Jeff Garzik057ace52005-10-22 14:27:05 -04001824static const struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 unsigned int shift;
1826 u8 base;
1827} xfer_mode_classes[] = {
1828 { ATA_SHIFT_UDMA, XFER_UDMA_0 },
1829 { ATA_SHIFT_MWDMA, XFER_MW_DMA_0 },
1830 { ATA_SHIFT_PIO, XFER_PIO_0 },
1831};
1832
Arjan van de Ven858119e2006-01-14 13:20:43 -08001833static u8 base_from_shift(unsigned int shift)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834{
1835 int i;
1836
1837 for (i = 0; i < ARRAY_SIZE(xfer_mode_classes); i++)
1838 if (xfer_mode_classes[i].shift == shift)
1839 return xfer_mode_classes[i].base;
1840
1841 return 0xff;
1842}
1843
1844static void ata_dev_set_mode(struct ata_port *ap, struct ata_device *dev)
1845{
1846 int ofs, idx;
1847 u8 base;
1848
1849 if (!ata_dev_present(dev) || (ap->flags & ATA_FLAG_PORT_DISABLED))
1850 return;
1851
1852 if (dev->xfer_shift == ATA_SHIFT_PIO)
1853 dev->flags |= ATA_DFLAG_PIO;
1854
1855 ata_dev_set_xfermode(ap, dev);
1856
1857 base = base_from_shift(dev->xfer_shift);
1858 ofs = dev->xfer_mode - base;
1859 idx = ofs + dev->xfer_shift;
1860 WARN_ON(idx >= ARRAY_SIZE(xfer_mode_str));
1861
1862 DPRINTK("idx=%d xfer_shift=%u, xfer_mode=0x%x, base=0x%x, offset=%d\n",
1863 idx, dev->xfer_shift, (int)dev->xfer_mode, (int)base, ofs);
1864
1865 printk(KERN_INFO "ata%u: dev %u configured for %s\n",
1866 ap->id, dev->devno, xfer_mode_str[idx]);
1867}
1868
1869static int ata_host_set_pio(struct ata_port *ap)
1870{
1871 unsigned int mask;
1872 int x, i;
1873 u8 base, xfer_mode;
1874
1875 mask = ata_get_mode_mask(ap, ATA_SHIFT_PIO);
1876 x = fgb(mask);
1877 if (x < 0) {
1878 printk(KERN_WARNING "ata%u: no PIO support\n", ap->id);
1879 return -1;
1880 }
1881
1882 base = base_from_shift(ATA_SHIFT_PIO);
1883 xfer_mode = base + x;
1884
1885 DPRINTK("base 0x%x xfer_mode 0x%x mask 0x%x x %d\n",
1886 (int)base, (int)xfer_mode, mask, x);
1887
1888 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1889 struct ata_device *dev = &ap->device[i];
1890 if (ata_dev_present(dev)) {
1891 dev->pio_mode = xfer_mode;
1892 dev->xfer_mode = xfer_mode;
1893 dev->xfer_shift = ATA_SHIFT_PIO;
1894 if (ap->ops->set_piomode)
1895 ap->ops->set_piomode(ap, dev);
1896 }
1897 }
1898
1899 return 0;
1900}
1901
1902static void ata_host_set_dma(struct ata_port *ap, u8 xfer_mode,
1903 unsigned int xfer_shift)
1904{
1905 int i;
1906
1907 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1908 struct ata_device *dev = &ap->device[i];
1909 if (ata_dev_present(dev)) {
1910 dev->dma_mode = xfer_mode;
1911 dev->xfer_mode = xfer_mode;
1912 dev->xfer_shift = xfer_shift;
1913 if (ap->ops->set_dmamode)
1914 ap->ops->set_dmamode(ap, dev);
1915 }
1916 }
1917}
1918
1919/**
1920 * ata_set_mode - Program timings and issue SET FEATURES - XFER
1921 * @ap: port on which timings will be programmed
1922 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001923 * Set ATA device disk transfer mode (PIO3, UDMA6, etc.).
1924 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001926 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 *
1928 */
1929static void ata_set_mode(struct ata_port *ap)
1930{
Albert Lee8cbd6df2005-10-12 15:06:27 +08001931 unsigned int xfer_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 u8 xfer_mode;
1933 int rc;
1934
1935 /* step 1: always set host PIO timings */
1936 rc = ata_host_set_pio(ap);
1937 if (rc)
1938 goto err_out;
1939
1940 /* step 2: choose the best data xfer mode */
1941 xfer_mode = xfer_shift = 0;
1942 rc = ata_choose_xfer_mode(ap, &xfer_mode, &xfer_shift);
1943 if (rc)
1944 goto err_out;
1945
1946 /* step 3: if that xfer mode isn't PIO, set host DMA timings */
1947 if (xfer_shift != ATA_SHIFT_PIO)
1948 ata_host_set_dma(ap, xfer_mode, xfer_shift);
1949
1950 /* step 4: update devices' xfer mode */
1951 ata_dev_set_mode(ap, &ap->device[0]);
1952 ata_dev_set_mode(ap, &ap->device[1]);
1953
1954 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1955 return;
1956
1957 if (ap->ops->post_set_mode)
1958 ap->ops->post_set_mode(ap);
1959
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 return;
1961
1962err_out:
1963 ata_port_disable(ap);
1964}
1965
1966/**
1967 * ata_busy_sleep - sleep until BSY clears, or timeout
1968 * @ap: port containing status register to be polled
1969 * @tmout_pat: impatience timeout
1970 * @tmout: overall timeout
1971 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001972 * Sleep until ATA Status register bit BSY clears,
1973 * or a timeout occurs.
1974 *
1975 * LOCKING: None.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 *
1977 */
1978
Tejun Heo6f8b9952006-01-24 17:05:21 +09001979unsigned int ata_busy_sleep (struct ata_port *ap,
1980 unsigned long tmout_pat, unsigned long tmout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981{
1982 unsigned long timer_start, timeout;
1983 u8 status;
1984
1985 status = ata_busy_wait(ap, ATA_BUSY, 300);
1986 timer_start = jiffies;
1987 timeout = timer_start + tmout_pat;
1988 while ((status & ATA_BUSY) && (time_before(jiffies, timeout))) {
1989 msleep(50);
1990 status = ata_busy_wait(ap, ATA_BUSY, 3);
1991 }
1992
1993 if (status & ATA_BUSY)
1994 printk(KERN_WARNING "ata%u is slow to respond, "
1995 "please be patient\n", ap->id);
1996
1997 timeout = timer_start + tmout;
1998 while ((status & ATA_BUSY) && (time_before(jiffies, timeout))) {
1999 msleep(50);
2000 status = ata_chk_status(ap);
2001 }
2002
2003 if (status & ATA_BUSY) {
2004 printk(KERN_ERR "ata%u failed to respond (%lu secs)\n",
2005 ap->id, tmout / HZ);
2006 return 1;
2007 }
2008
2009 return 0;
2010}
2011
2012static void ata_bus_post_reset(struct ata_port *ap, unsigned int devmask)
2013{
2014 struct ata_ioports *ioaddr = &ap->ioaddr;
2015 unsigned int dev0 = devmask & (1 << 0);
2016 unsigned int dev1 = devmask & (1 << 1);
2017 unsigned long timeout;
2018
2019 /* if device 0 was found in ata_devchk, wait for its
2020 * BSY bit to clear
2021 */
2022 if (dev0)
2023 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
2024
2025 /* if device 1 was found in ata_devchk, wait for
2026 * register access, then wait for BSY to clear
2027 */
2028 timeout = jiffies + ATA_TMOUT_BOOT;
2029 while (dev1) {
2030 u8 nsect, lbal;
2031
2032 ap->ops->dev_select(ap, 1);
2033 if (ap->flags & ATA_FLAG_MMIO) {
2034 nsect = readb((void __iomem *) ioaddr->nsect_addr);
2035 lbal = readb((void __iomem *) ioaddr->lbal_addr);
2036 } else {
2037 nsect = inb(ioaddr->nsect_addr);
2038 lbal = inb(ioaddr->lbal_addr);
2039 }
2040 if ((nsect == 1) && (lbal == 1))
2041 break;
2042 if (time_after(jiffies, timeout)) {
2043 dev1 = 0;
2044 break;
2045 }
2046 msleep(50); /* give drive a breather */
2047 }
2048 if (dev1)
2049 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
2050
2051 /* is all this really necessary? */
2052 ap->ops->dev_select(ap, 0);
2053 if (dev1)
2054 ap->ops->dev_select(ap, 1);
2055 if (dev0)
2056 ap->ops->dev_select(ap, 0);
2057}
2058
2059/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04002060 * ata_bus_edd - Issue EXECUTE DEVICE DIAGNOSTIC command.
2061 * @ap: Port to reset and probe
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04002063 * Use the EXECUTE DEVICE DIAGNOSTIC command to reset and
2064 * probe the bus. Not often used these days.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 *
2066 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002067 * PCI/etc. bus probe sem.
Jeff Garzike5338252005-10-30 21:37:17 -05002068 * Obtains host_set lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069 *
2070 */
2071
2072static unsigned int ata_bus_edd(struct ata_port *ap)
2073{
2074 struct ata_taskfile tf;
Jeff Garzike5338252005-10-30 21:37:17 -05002075 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076
2077 /* set up execute-device-diag (bus reset) taskfile */
2078 /* also, take interrupts to a known state (disabled) */
2079 DPRINTK("execute-device-diag\n");
2080 ata_tf_init(ap, &tf, 0);
2081 tf.ctl |= ATA_NIEN;
2082 tf.command = ATA_CMD_EDD;
2083 tf.protocol = ATA_PROT_NODATA;
2084
2085 /* do bus reset */
Jeff Garzike5338252005-10-30 21:37:17 -05002086 spin_lock_irqsave(&ap->host_set->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087 ata_tf_to_host(ap, &tf);
Jeff Garzike5338252005-10-30 21:37:17 -05002088 spin_unlock_irqrestore(&ap->host_set->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089
2090 /* spec says at least 2ms. but who knows with those
2091 * crazy ATAPI devices...
2092 */
2093 msleep(150);
2094
2095 return ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
2096}
2097
2098static unsigned int ata_bus_softreset(struct ata_port *ap,
2099 unsigned int devmask)
2100{
2101 struct ata_ioports *ioaddr = &ap->ioaddr;
2102
2103 DPRINTK("ata%u: bus reset via SRST\n", ap->id);
2104
2105 /* software reset. causes dev0 to be selected */
2106 if (ap->flags & ATA_FLAG_MMIO) {
2107 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
2108 udelay(20); /* FIXME: flush */
2109 writeb(ap->ctl | ATA_SRST, (void __iomem *) ioaddr->ctl_addr);
2110 udelay(20); /* FIXME: flush */
2111 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
2112 } else {
2113 outb(ap->ctl, ioaddr->ctl_addr);
2114 udelay(10);
2115 outb(ap->ctl | ATA_SRST, ioaddr->ctl_addr);
2116 udelay(10);
2117 outb(ap->ctl, ioaddr->ctl_addr);
2118 }
2119
2120 /* spec mandates ">= 2ms" before checking status.
2121 * We wait 150ms, because that was the magic delay used for
2122 * ATAPI devices in Hale Landis's ATADRVR, for the period of time
2123 * between when the ATA command register is written, and then
2124 * status is checked. Because waiting for "a while" before
2125 * checking status is fine, post SRST, we perform this magic
2126 * delay here as well.
2127 */
2128 msleep(150);
2129
2130 ata_bus_post_reset(ap, devmask);
2131
2132 return 0;
2133}
2134
2135/**
2136 * ata_bus_reset - reset host port and associated ATA channel
2137 * @ap: port to reset
2138 *
2139 * This is typically the first time we actually start issuing
2140 * commands to the ATA channel. We wait for BSY to clear, then
2141 * issue EXECUTE DEVICE DIAGNOSTIC command, polling for its
2142 * result. Determine what devices, if any, are on the channel
2143 * by looking at the device 0/1 error register. Look at the signature
2144 * stored in each device's taskfile registers, to determine if
2145 * the device is ATA or ATAPI.
2146 *
2147 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002148 * PCI/etc. bus probe sem.
2149 * Obtains host_set lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150 *
2151 * SIDE EFFECTS:
2152 * Sets ATA_FLAG_PORT_DISABLED if bus reset fails.
2153 */
2154
2155void ata_bus_reset(struct ata_port *ap)
2156{
2157 struct ata_ioports *ioaddr = &ap->ioaddr;
2158 unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS;
2159 u8 err;
2160 unsigned int dev0, dev1 = 0, rc = 0, devmask = 0;
2161
2162 DPRINTK("ENTER, host %u, port %u\n", ap->id, ap->port_no);
2163
2164 /* determine if device 0/1 are present */
2165 if (ap->flags & ATA_FLAG_SATA_RESET)
2166 dev0 = 1;
2167 else {
2168 dev0 = ata_devchk(ap, 0);
2169 if (slave_possible)
2170 dev1 = ata_devchk(ap, 1);
2171 }
2172
2173 if (dev0)
2174 devmask |= (1 << 0);
2175 if (dev1)
2176 devmask |= (1 << 1);
2177
2178 /* select device 0 again */
2179 ap->ops->dev_select(ap, 0);
2180
2181 /* issue bus reset */
2182 if (ap->flags & ATA_FLAG_SRST)
2183 rc = ata_bus_softreset(ap, devmask);
2184 else if ((ap->flags & ATA_FLAG_SATA_RESET) == 0) {
2185 /* set up device control */
2186 if (ap->flags & ATA_FLAG_MMIO)
2187 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
2188 else
2189 outb(ap->ctl, ioaddr->ctl_addr);
2190 rc = ata_bus_edd(ap);
2191 }
2192
2193 if (rc)
2194 goto err_out;
2195
2196 /*
2197 * determine by signature whether we have ATA or ATAPI devices
2198 */
Tejun Heob4dc7622006-01-24 17:05:22 +09002199 ap->device[0].class = ata_dev_try_classify(ap, 0, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200 if ((slave_possible) && (err != 0x81))
Tejun Heob4dc7622006-01-24 17:05:22 +09002201 ap->device[1].class = ata_dev_try_classify(ap, 1, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202
2203 /* re-enable interrupts */
2204 if (ap->ioaddr.ctl_addr) /* FIXME: hack. create a hook instead */
2205 ata_irq_on(ap);
2206
2207 /* is double-select really necessary? */
2208 if (ap->device[1].class != ATA_DEV_NONE)
2209 ap->ops->dev_select(ap, 1);
2210 if (ap->device[0].class != ATA_DEV_NONE)
2211 ap->ops->dev_select(ap, 0);
2212
2213 /* if no devices were detected, disable this port */
2214 if ((ap->device[0].class == ATA_DEV_NONE) &&
2215 (ap->device[1].class == ATA_DEV_NONE))
2216 goto err_out;
2217
2218 if (ap->flags & (ATA_FLAG_SATA_RESET | ATA_FLAG_SRST)) {
2219 /* set up device control for ATA_FLAG_SATA_RESET */
2220 if (ap->flags & ATA_FLAG_MMIO)
2221 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
2222 else
2223 outb(ap->ctl, ioaddr->ctl_addr);
2224 }
2225
2226 DPRINTK("EXIT\n");
2227 return;
2228
2229err_out:
2230 printk(KERN_ERR "ata%u: disabling port\n", ap->id);
2231 ap->ops->port_disable(ap);
2232
2233 DPRINTK("EXIT\n");
2234}
2235
Jeff Garzik057ace52005-10-22 14:27:05 -04002236static void ata_pr_blacklisted(const struct ata_port *ap,
2237 const struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238{
2239 printk(KERN_WARNING "ata%u: dev %u is on DMA blacklist, disabling DMA\n",
2240 ap->id, dev->devno);
2241}
2242
Arjan van de Ven98ac62d2005-11-28 10:06:23 +01002243static const char * const ata_dma_blacklist [] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244 "WDC AC11000H",
2245 "WDC AC22100H",
2246 "WDC AC32500H",
2247 "WDC AC33100H",
2248 "WDC AC31600H",
2249 "WDC AC32100H",
2250 "WDC AC23200L",
2251 "Compaq CRD-8241B",
2252 "CRD-8400B",
2253 "CRD-8480B",
2254 "CRD-8482B",
2255 "CRD-84",
2256 "SanDisk SDP3B",
2257 "SanDisk SDP3B-64",
2258 "SANYO CD-ROM CRD",
2259 "HITACHI CDR-8",
2260 "HITACHI CDR-8335",
2261 "HITACHI CDR-8435",
2262 "Toshiba CD-ROM XM-6202B",
Jeff Garzike9222562005-06-28 00:03:37 -04002263 "TOSHIBA CD-ROM XM-1702BC",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264 "CD-532E-A",
2265 "E-IDE CD-ROM CR-840",
2266 "CD-ROM Drive/F5A",
2267 "WPI CDD-820",
2268 "SAMSUNG CD-ROM SC-148C",
2269 "SAMSUNG CD-ROM SC",
2270 "SanDisk SDP3B-64",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271 "ATAPI CD-ROM DRIVE 40X MAXIMUM",
2272 "_NEC DV5800A",
2273};
2274
Jeff Garzik057ace52005-10-22 14:27:05 -04002275static int ata_dma_blacklisted(const struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276{
2277 unsigned char model_num[40];
2278 char *s;
2279 unsigned int len;
2280 int i;
2281
2282 ata_dev_id_string(dev->id, model_num, ATA_ID_PROD_OFS,
2283 sizeof(model_num));
2284 s = &model_num[0];
2285 len = strnlen(s, sizeof(model_num));
2286
2287 /* ATAPI specifies that empty space is blank-filled; remove blanks */
2288 while ((len > 0) && (s[len - 1] == ' ')) {
2289 len--;
2290 s[len] = 0;
2291 }
2292
2293 for (i = 0; i < ARRAY_SIZE(ata_dma_blacklist); i++)
2294 if (!strncmp(ata_dma_blacklist[i], s, len))
2295 return 1;
2296
2297 return 0;
2298}
2299
Jeff Garzik057ace52005-10-22 14:27:05 -04002300static unsigned int ata_get_mode_mask(const struct ata_port *ap, int shift)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301{
Jeff Garzik057ace52005-10-22 14:27:05 -04002302 const struct ata_device *master, *slave;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303 unsigned int mask;
2304
2305 master = &ap->device[0];
2306 slave = &ap->device[1];
2307
2308 assert (ata_dev_present(master) || ata_dev_present(slave));
2309
2310 if (shift == ATA_SHIFT_UDMA) {
2311 mask = ap->udma_mask;
2312 if (ata_dev_present(master)) {
2313 mask &= (master->id[ATA_ID_UDMA_MODES] & 0xff);
Jeff Garzik057ace52005-10-22 14:27:05 -04002314 if (ata_dma_blacklisted(master)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315 mask = 0;
2316 ata_pr_blacklisted(ap, master);
2317 }
2318 }
2319 if (ata_dev_present(slave)) {
2320 mask &= (slave->id[ATA_ID_UDMA_MODES] & 0xff);
Jeff Garzik057ace52005-10-22 14:27:05 -04002321 if (ata_dma_blacklisted(slave)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322 mask = 0;
2323 ata_pr_blacklisted(ap, slave);
2324 }
2325 }
2326 }
2327 else if (shift == ATA_SHIFT_MWDMA) {
2328 mask = ap->mwdma_mask;
2329 if (ata_dev_present(master)) {
2330 mask &= (master->id[ATA_ID_MWDMA_MODES] & 0x07);
Jeff Garzik057ace52005-10-22 14:27:05 -04002331 if (ata_dma_blacklisted(master)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332 mask = 0;
2333 ata_pr_blacklisted(ap, master);
2334 }
2335 }
2336 if (ata_dev_present(slave)) {
2337 mask &= (slave->id[ATA_ID_MWDMA_MODES] & 0x07);
Jeff Garzik057ace52005-10-22 14:27:05 -04002338 if (ata_dma_blacklisted(slave)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339 mask = 0;
2340 ata_pr_blacklisted(ap, slave);
2341 }
2342 }
2343 }
2344 else if (shift == ATA_SHIFT_PIO) {
2345 mask = ap->pio_mask;
2346 if (ata_dev_present(master)) {
2347 /* spec doesn't return explicit support for
2348 * PIO0-2, so we fake it
2349 */
2350 u16 tmp_mode = master->id[ATA_ID_PIO_MODES] & 0x03;
2351 tmp_mode <<= 3;
2352 tmp_mode |= 0x7;
2353 mask &= tmp_mode;
2354 }
2355 if (ata_dev_present(slave)) {
2356 /* spec doesn't return explicit support for
2357 * PIO0-2, so we fake it
2358 */
2359 u16 tmp_mode = slave->id[ATA_ID_PIO_MODES] & 0x03;
2360 tmp_mode <<= 3;
2361 tmp_mode |= 0x7;
2362 mask &= tmp_mode;
2363 }
2364 }
2365 else {
2366 mask = 0xffffffff; /* shut up compiler warning */
2367 BUG();
2368 }
2369
2370 return mask;
2371}
2372
2373/* find greatest bit */
2374static int fgb(u32 bitmap)
2375{
2376 unsigned int i;
2377 int x = -1;
2378
2379 for (i = 0; i < 32; i++)
2380 if (bitmap & (1 << i))
2381 x = i;
2382
2383 return x;
2384}
2385
2386/**
2387 * ata_choose_xfer_mode - attempt to find best transfer mode
2388 * @ap: Port for which an xfer mode will be selected
2389 * @xfer_mode_out: (output) SET FEATURES - XFER MODE code
2390 * @xfer_shift_out: (output) bit shift that selects this mode
2391 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04002392 * Based on host and device capabilities, determine the
2393 * maximum transfer mode that is amenable to all.
2394 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002396 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397 *
2398 * RETURNS:
2399 * Zero on success, negative on error.
2400 */
2401
Jeff Garzik057ace52005-10-22 14:27:05 -04002402static int ata_choose_xfer_mode(const struct ata_port *ap,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403 u8 *xfer_mode_out,
2404 unsigned int *xfer_shift_out)
2405{
2406 unsigned int mask, shift;
2407 int x, i;
2408
2409 for (i = 0; i < ARRAY_SIZE(xfer_mode_classes); i++) {
2410 shift = xfer_mode_classes[i].shift;
2411 mask = ata_get_mode_mask(ap, shift);
2412
2413 x = fgb(mask);
2414 if (x >= 0) {
2415 *xfer_mode_out = xfer_mode_classes[i].base + x;
2416 *xfer_shift_out = shift;
2417 return 0;
2418 }
2419 }
2420
2421 return -1;
2422}
2423
2424/**
2425 * ata_dev_set_xfermode - Issue SET FEATURES - XFER MODE command
2426 * @ap: Port associated with device @dev
2427 * @dev: Device to which command will be sent
2428 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04002429 * Issue SET FEATURES - XFER MODE command to device @dev
2430 * on port @ap.
2431 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002433 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434 */
2435
2436static void ata_dev_set_xfermode(struct ata_port *ap, struct ata_device *dev)
2437{
Tejun Heoa0123702005-12-13 14:49:31 +09002438 struct ata_taskfile tf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439
2440 /* set up set-features taskfile */
2441 DPRINTK("set features - xfer mode\n");
2442
Tejun Heoa0123702005-12-13 14:49:31 +09002443 ata_tf_init(ap, &tf, dev->devno);
2444 tf.command = ATA_CMD_SET_FEATURES;
2445 tf.feature = SETFEATURES_XFER;
2446 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
2447 tf.protocol = ATA_PROT_NODATA;
2448 tf.nsect = dev->xfer_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449
Tejun Heoa0123702005-12-13 14:49:31 +09002450 if (ata_exec_internal(ap, dev, &tf, DMA_NONE, NULL, 0)) {
2451 printk(KERN_ERR "ata%u: failed to set xfermode, disabled\n",
2452 ap->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453 ata_port_disable(ap);
Tejun Heoa0123702005-12-13 14:49:31 +09002454 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455
2456 DPRINTK("EXIT\n");
2457}
2458
2459/**
Albert Lee59a10b12005-10-12 15:09:42 +08002460 * ata_dev_reread_id - Reread the device identify device info
2461 * @ap: port where the device is
2462 * @dev: device to reread the identify device info
2463 *
2464 * LOCKING:
2465 */
2466
2467static void ata_dev_reread_id(struct ata_port *ap, struct ata_device *dev)
2468{
Tejun Heoa0123702005-12-13 14:49:31 +09002469 struct ata_taskfile tf;
Albert Lee59a10b12005-10-12 15:09:42 +08002470
Tejun Heoa0123702005-12-13 14:49:31 +09002471 ata_tf_init(ap, &tf, dev->devno);
Albert Lee59a10b12005-10-12 15:09:42 +08002472
2473 if (dev->class == ATA_DEV_ATA) {
Tejun Heoa0123702005-12-13 14:49:31 +09002474 tf.command = ATA_CMD_ID_ATA;
Albert Lee59a10b12005-10-12 15:09:42 +08002475 DPRINTK("do ATA identify\n");
2476 } else {
Tejun Heoa0123702005-12-13 14:49:31 +09002477 tf.command = ATA_CMD_ID_ATAPI;
Albert Lee59a10b12005-10-12 15:09:42 +08002478 DPRINTK("do ATAPI identify\n");
2479 }
2480
Tejun Heoa0123702005-12-13 14:49:31 +09002481 tf.flags |= ATA_TFLAG_DEVICE;
2482 tf.protocol = ATA_PROT_PIO;
Albert Lee59a10b12005-10-12 15:09:42 +08002483
Tejun Heoa0123702005-12-13 14:49:31 +09002484 if (ata_exec_internal(ap, dev, &tf, DMA_FROM_DEVICE,
2485 dev->id, sizeof(dev->id)))
Albert Lee59a10b12005-10-12 15:09:42 +08002486 goto err_out;
2487
Albert Lee59a10b12005-10-12 15:09:42 +08002488 swap_buf_le16(dev->id, ATA_ID_WORDS);
2489
2490 ata_dump_id(dev);
2491
2492 DPRINTK("EXIT\n");
2493
2494 return;
2495err_out:
Tejun Heoa0123702005-12-13 14:49:31 +09002496 printk(KERN_ERR "ata%u: failed to reread ID, disabled\n", ap->id);
Albert Lee59a10b12005-10-12 15:09:42 +08002497 ata_port_disable(ap);
2498}
2499
2500/**
Albert Lee8bf62ec2005-05-12 15:29:42 -04002501 * ata_dev_init_params - Issue INIT DEV PARAMS command
2502 * @ap: Port associated with device @dev
2503 * @dev: Device to which command will be sent
2504 *
2505 * LOCKING:
2506 */
2507
2508static void ata_dev_init_params(struct ata_port *ap, struct ata_device *dev)
2509{
Tejun Heoa0123702005-12-13 14:49:31 +09002510 struct ata_taskfile tf;
Albert Lee8bf62ec2005-05-12 15:29:42 -04002511 u16 sectors = dev->id[6];
2512 u16 heads = dev->id[3];
2513
2514 /* Number of sectors per track 1-255. Number of heads 1-16 */
2515 if (sectors < 1 || sectors > 255 || heads < 1 || heads > 16)
2516 return;
2517
2518 /* set up init dev params taskfile */
2519 DPRINTK("init dev params \n");
2520
Tejun Heoa0123702005-12-13 14:49:31 +09002521 ata_tf_init(ap, &tf, dev->devno);
2522 tf.command = ATA_CMD_INIT_DEV_PARAMS;
2523 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
2524 tf.protocol = ATA_PROT_NODATA;
2525 tf.nsect = sectors;
2526 tf.device |= (heads - 1) & 0x0f; /* max head = num. of heads - 1 */
Albert Lee8bf62ec2005-05-12 15:29:42 -04002527
Tejun Heoa0123702005-12-13 14:49:31 +09002528 if (ata_exec_internal(ap, dev, &tf, DMA_NONE, NULL, 0)) {
2529 printk(KERN_ERR "ata%u: failed to init parameters, disabled\n",
2530 ap->id);
Albert Lee8bf62ec2005-05-12 15:29:42 -04002531 ata_port_disable(ap);
Tejun Heoa0123702005-12-13 14:49:31 +09002532 }
Albert Lee8bf62ec2005-05-12 15:29:42 -04002533
2534 DPRINTK("EXIT\n");
2535}
2536
2537/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04002538 * ata_sg_clean - Unmap DMA memory associated with command
2539 * @qc: Command containing DMA memory to be released
2540 *
2541 * Unmap all mapped DMA memory associated with this command.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002542 *
2543 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002544 * spin_lock_irqsave(host_set lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545 */
2546
2547static void ata_sg_clean(struct ata_queued_cmd *qc)
2548{
2549 struct ata_port *ap = qc->ap;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002550 struct scatterlist *sg = qc->__sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551 int dir = qc->dma_dir;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002552 void *pad_buf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553
2554 assert(qc->flags & ATA_QCFLAG_DMAMAP);
2555 assert(sg != NULL);
2556
2557 if (qc->flags & ATA_QCFLAG_SINGLE)
2558 assert(qc->n_elem == 1);
2559
Jeff Garzik2c13b7c2005-11-14 14:14:16 -05002560 VPRINTK("unmapping %u sg elements\n", qc->n_elem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002562 /* if we padded the buffer out to 32-bit bound, and data
2563 * xfer direction is from-device, we must copy from the
2564 * pad buffer back into the supplied buffer
2565 */
2566 if (qc->pad_len && !(qc->tf.flags & ATA_TFLAG_WRITE))
2567 pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
2568
2569 if (qc->flags & ATA_QCFLAG_SG) {
Jeff Garzike1410f22005-11-14 14:06:26 -05002570 if (qc->n_elem)
2571 dma_unmap_sg(ap->host_set->dev, sg, qc->n_elem, dir);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002572 /* restore last sg */
2573 sg[qc->orig_n_elem - 1].length += qc->pad_len;
2574 if (pad_buf) {
2575 struct scatterlist *psg = &qc->pad_sgent;
2576 void *addr = kmap_atomic(psg->page, KM_IRQ0);
2577 memcpy(addr + psg->offset, pad_buf, qc->pad_len);
Mark Lorddfa15982005-12-12 23:19:28 -05002578 kunmap_atomic(addr, KM_IRQ0);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002579 }
2580 } else {
Jeff Garzike1410f22005-11-14 14:06:26 -05002581 if (sg_dma_len(&sg[0]) > 0)
2582 dma_unmap_single(ap->host_set->dev,
2583 sg_dma_address(&sg[0]), sg_dma_len(&sg[0]),
2584 dir);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002585 /* restore sg */
2586 sg->length += qc->pad_len;
2587 if (pad_buf)
2588 memcpy(qc->buf_virt + sg->length - qc->pad_len,
2589 pad_buf, qc->pad_len);
2590 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591
2592 qc->flags &= ~ATA_QCFLAG_DMAMAP;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002593 qc->__sg = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594}
2595
2596/**
2597 * ata_fill_sg - Fill PCI IDE PRD table
2598 * @qc: Metadata associated with taskfile to be transferred
2599 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04002600 * Fill PCI IDE PRD (scatter-gather) table with segments
2601 * associated with the current disk command.
2602 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603 * LOCKING:
Jeff Garzik780a87f2005-05-30 15:41:05 -04002604 * spin_lock_irqsave(host_set lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605 *
2606 */
2607static void ata_fill_sg(struct ata_queued_cmd *qc)
2608{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609 struct ata_port *ap = qc->ap;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002610 struct scatterlist *sg;
2611 unsigned int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002613 assert(qc->__sg != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614 assert(qc->n_elem > 0);
2615
2616 idx = 0;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002617 ata_for_each_sg(sg, qc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618 u32 addr, offset;
2619 u32 sg_len, len;
2620
2621 /* determine if physical DMA addr spans 64K boundary.
2622 * Note h/w doesn't support 64-bit, so we unconditionally
2623 * truncate dma_addr_t to u32.
2624 */
2625 addr = (u32) sg_dma_address(sg);
2626 sg_len = sg_dma_len(sg);
2627
2628 while (sg_len) {
2629 offset = addr & 0xffff;
2630 len = sg_len;
2631 if ((offset + sg_len) > 0x10000)
2632 len = 0x10000 - offset;
2633
2634 ap->prd[idx].addr = cpu_to_le32(addr);
2635 ap->prd[idx].flags_len = cpu_to_le32(len & 0xffff);
2636 VPRINTK("PRD[%u] = (0x%X, 0x%X)\n", idx, addr, len);
2637
2638 idx++;
2639 sg_len -= len;
2640 addr += len;
2641 }
2642 }
2643
2644 if (idx)
2645 ap->prd[idx - 1].flags_len |= cpu_to_le32(ATA_PRD_EOT);
2646}
2647/**
2648 * ata_check_atapi_dma - Check whether ATAPI DMA can be supported
2649 * @qc: Metadata associated with taskfile to check
2650 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04002651 * Allow low-level driver to filter ATA PACKET commands, returning
2652 * a status indicating whether or not it is OK to use DMA for the
2653 * supplied PACKET command.
2654 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002655 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002656 * spin_lock_irqsave(host_set lock)
2657 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658 * RETURNS: 0 when ATAPI DMA can be used
2659 * nonzero otherwise
2660 */
2661int ata_check_atapi_dma(struct ata_queued_cmd *qc)
2662{
2663 struct ata_port *ap = qc->ap;
2664 int rc = 0; /* Assume ATAPI DMA is OK by default */
2665
2666 if (ap->ops->check_atapi_dma)
2667 rc = ap->ops->check_atapi_dma(qc);
2668
2669 return rc;
2670}
2671/**
2672 * ata_qc_prep - Prepare taskfile for submission
2673 * @qc: Metadata associated with taskfile to be prepared
2674 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04002675 * Prepare ATA taskfile for submission.
2676 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677 * LOCKING:
2678 * spin_lock_irqsave(host_set lock)
2679 */
2680void ata_qc_prep(struct ata_queued_cmd *qc)
2681{
2682 if (!(qc->flags & ATA_QCFLAG_DMAMAP))
2683 return;
2684
2685 ata_fill_sg(qc);
2686}
2687
Jeff Garzik0cba6322005-05-30 19:49:12 -04002688/**
2689 * ata_sg_init_one - Associate command with memory buffer
2690 * @qc: Command to be associated
2691 * @buf: Memory buffer
2692 * @buflen: Length of memory buffer, in bytes.
2693 *
2694 * Initialize the data-related elements of queued_cmd @qc
2695 * to point to a single memory buffer, @buf of byte length @buflen.
2696 *
2697 * LOCKING:
2698 * spin_lock_irqsave(host_set lock)
2699 */
2700
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701void ata_sg_init_one(struct ata_queued_cmd *qc, void *buf, unsigned int buflen)
2702{
2703 struct scatterlist *sg;
2704
2705 qc->flags |= ATA_QCFLAG_SINGLE;
2706
2707 memset(&qc->sgent, 0, sizeof(qc->sgent));
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002708 qc->__sg = &qc->sgent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709 qc->n_elem = 1;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002710 qc->orig_n_elem = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711 qc->buf_virt = buf;
2712
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002713 sg = qc->__sg;
Jeff Garzikf0612bb2005-10-30 01:58:18 -05002714 sg_init_one(sg, buf, buflen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002715}
2716
Jeff Garzik0cba6322005-05-30 19:49:12 -04002717/**
2718 * ata_sg_init - Associate command with scatter-gather table.
2719 * @qc: Command to be associated
2720 * @sg: Scatter-gather table.
2721 * @n_elem: Number of elements in s/g table.
2722 *
2723 * Initialize the data-related elements of queued_cmd @qc
2724 * to point to a scatter-gather table @sg, containing @n_elem
2725 * elements.
2726 *
2727 * LOCKING:
2728 * spin_lock_irqsave(host_set lock)
2729 */
2730
Linus Torvalds1da177e2005-04-16 15:20:36 -07002731void ata_sg_init(struct ata_queued_cmd *qc, struct scatterlist *sg,
2732 unsigned int n_elem)
2733{
2734 qc->flags |= ATA_QCFLAG_SG;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002735 qc->__sg = sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736 qc->n_elem = n_elem;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002737 qc->orig_n_elem = n_elem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738}
2739
2740/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04002741 * ata_sg_setup_one - DMA-map the memory buffer associated with a command.
2742 * @qc: Command with memory buffer to be mapped.
2743 *
2744 * DMA-map the memory buffer associated with queued_cmd @qc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745 *
2746 * LOCKING:
2747 * spin_lock_irqsave(host_set lock)
2748 *
2749 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002750 * Zero on success, negative on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002751 */
2752
2753static int ata_sg_setup_one(struct ata_queued_cmd *qc)
2754{
2755 struct ata_port *ap = qc->ap;
2756 int dir = qc->dma_dir;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002757 struct scatterlist *sg = qc->__sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758 dma_addr_t dma_address;
2759
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002760 /* we must lengthen transfers to end on a 32-bit boundary */
2761 qc->pad_len = sg->length & 3;
2762 if (qc->pad_len) {
2763 void *pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
2764 struct scatterlist *psg = &qc->pad_sgent;
2765
2766 assert(qc->dev->class == ATA_DEV_ATAPI);
2767
2768 memset(pad_buf, 0, ATA_DMA_PAD_SZ);
2769
2770 if (qc->tf.flags & ATA_TFLAG_WRITE)
2771 memcpy(pad_buf, qc->buf_virt + sg->length - qc->pad_len,
2772 qc->pad_len);
2773
2774 sg_dma_address(psg) = ap->pad_dma + (qc->tag * ATA_DMA_PAD_SZ);
2775 sg_dma_len(psg) = ATA_DMA_PAD_SZ;
2776 /* trim sg */
2777 sg->length -= qc->pad_len;
2778
2779 DPRINTK("padding done, sg->length=%u pad_len=%u\n",
2780 sg->length, qc->pad_len);
2781 }
2782
Jeff Garzike1410f22005-11-14 14:06:26 -05002783 if (!sg->length) {
2784 sg_dma_address(sg) = 0;
2785 goto skip_map;
2786 }
2787
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788 dma_address = dma_map_single(ap->host_set->dev, qc->buf_virt,
Albert Lee32529e02005-05-26 03:49:42 -04002789 sg->length, dir);
Tejun Heo537a95d2005-11-05 14:29:01 -05002790 if (dma_mapping_error(dma_address)) {
2791 /* restore sg */
2792 sg->length += qc->pad_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002793 return -1;
Tejun Heo537a95d2005-11-05 14:29:01 -05002794 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795
2796 sg_dma_address(sg) = dma_address;
Jeff Garzike1410f22005-11-14 14:06:26 -05002797skip_map:
Albert Lee32529e02005-05-26 03:49:42 -04002798 sg_dma_len(sg) = sg->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799
2800 DPRINTK("mapped buffer of %d bytes for %s\n", sg_dma_len(sg),
2801 qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
2802
2803 return 0;
2804}
2805
2806/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04002807 * ata_sg_setup - DMA-map the scatter-gather table associated with a command.
2808 * @qc: Command with scatter-gather table to be mapped.
2809 *
2810 * DMA-map the scatter-gather table associated with queued_cmd @qc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811 *
2812 * LOCKING:
2813 * spin_lock_irqsave(host_set lock)
2814 *
2815 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002816 * Zero on success, negative on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817 *
2818 */
2819
2820static int ata_sg_setup(struct ata_queued_cmd *qc)
2821{
2822 struct ata_port *ap = qc->ap;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002823 struct scatterlist *sg = qc->__sg;
2824 struct scatterlist *lsg = &sg[qc->n_elem - 1];
Jeff Garzike1410f22005-11-14 14:06:26 -05002825 int n_elem, pre_n_elem, dir, trim_sg = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826
2827 VPRINTK("ENTER, ata%u\n", ap->id);
2828 assert(qc->flags & ATA_QCFLAG_SG);
2829
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002830 /* we must lengthen transfers to end on a 32-bit boundary */
2831 qc->pad_len = lsg->length & 3;
2832 if (qc->pad_len) {
2833 void *pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
2834 struct scatterlist *psg = &qc->pad_sgent;
2835 unsigned int offset;
2836
2837 assert(qc->dev->class == ATA_DEV_ATAPI);
2838
2839 memset(pad_buf, 0, ATA_DMA_PAD_SZ);
2840
2841 /*
2842 * psg->page/offset are used to copy to-be-written
2843 * data in this function or read data in ata_sg_clean.
2844 */
2845 offset = lsg->offset + lsg->length - qc->pad_len;
2846 psg->page = nth_page(lsg->page, offset >> PAGE_SHIFT);
2847 psg->offset = offset_in_page(offset);
2848
2849 if (qc->tf.flags & ATA_TFLAG_WRITE) {
2850 void *addr = kmap_atomic(psg->page, KM_IRQ0);
2851 memcpy(pad_buf, addr + psg->offset, qc->pad_len);
Mark Lorddfa15982005-12-12 23:19:28 -05002852 kunmap_atomic(addr, KM_IRQ0);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002853 }
2854
2855 sg_dma_address(psg) = ap->pad_dma + (qc->tag * ATA_DMA_PAD_SZ);
2856 sg_dma_len(psg) = ATA_DMA_PAD_SZ;
2857 /* trim last sg */
2858 lsg->length -= qc->pad_len;
Jeff Garzike1410f22005-11-14 14:06:26 -05002859 if (lsg->length == 0)
2860 trim_sg = 1;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002861
2862 DPRINTK("padding done, sg[%d].length=%u pad_len=%u\n",
2863 qc->n_elem - 1, lsg->length, qc->pad_len);
2864 }
2865
Jeff Garzike1410f22005-11-14 14:06:26 -05002866 pre_n_elem = qc->n_elem;
2867 if (trim_sg && pre_n_elem)
2868 pre_n_elem--;
2869
2870 if (!pre_n_elem) {
2871 n_elem = 0;
2872 goto skip_map;
2873 }
2874
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875 dir = qc->dma_dir;
Jeff Garzike1410f22005-11-14 14:06:26 -05002876 n_elem = dma_map_sg(ap->host_set->dev, sg, pre_n_elem, dir);
Tejun Heo537a95d2005-11-05 14:29:01 -05002877 if (n_elem < 1) {
2878 /* restore last sg */
2879 lsg->length += qc->pad_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880 return -1;
Tejun Heo537a95d2005-11-05 14:29:01 -05002881 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882
2883 DPRINTK("%d sg elements mapped\n", n_elem);
2884
Jeff Garzike1410f22005-11-14 14:06:26 -05002885skip_map:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002886 qc->n_elem = n_elem;
2887
2888 return 0;
2889}
2890
2891/**
Tejun Heo40e8c822005-08-22 17:12:45 +09002892 * ata_poll_qc_complete - turn irq back on and finish qc
2893 * @qc: Command to complete
Randy Dunlap8e8b77d2005-11-01 21:29:27 -08002894 * @err_mask: ATA status register content
Tejun Heo40e8c822005-08-22 17:12:45 +09002895 *
2896 * LOCKING:
2897 * None. (grabs host lock)
2898 */
2899
Albert Leea22e2eb2005-12-05 15:38:02 +08002900void ata_poll_qc_complete(struct ata_queued_cmd *qc)
Tejun Heo40e8c822005-08-22 17:12:45 +09002901{
2902 struct ata_port *ap = qc->ap;
Jeff Garzikb8f61532005-08-25 22:01:20 -04002903 unsigned long flags;
Tejun Heo40e8c822005-08-22 17:12:45 +09002904
Jeff Garzikb8f61532005-08-25 22:01:20 -04002905 spin_lock_irqsave(&ap->host_set->lock, flags);
Tejun Heo40e8c822005-08-22 17:12:45 +09002906 ap->flags &= ~ATA_FLAG_NOINTR;
2907 ata_irq_on(ap);
Albert Leea22e2eb2005-12-05 15:38:02 +08002908 ata_qc_complete(qc);
Jeff Garzikb8f61532005-08-25 22:01:20 -04002909 spin_unlock_irqrestore(&ap->host_set->lock, flags);
Tejun Heo40e8c822005-08-22 17:12:45 +09002910}
2911
2912/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002913 * ata_pio_poll -
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04002914 * @ap: the target ata_port
Linus Torvalds1da177e2005-04-16 15:20:36 -07002915 *
2916 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002917 * None. (executing in kernel thread context)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002918 *
2919 * RETURNS:
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04002920 * timeout value to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07002921 */
2922
2923static unsigned long ata_pio_poll(struct ata_port *ap)
2924{
Albert Leec14b8332005-12-05 15:36:08 +08002925 struct ata_queued_cmd *qc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926 u8 status;
Albert Lee14be71f2005-09-27 17:36:35 +08002927 unsigned int poll_state = HSM_ST_UNKNOWN;
2928 unsigned int reg_state = HSM_ST_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002929
Albert Leec14b8332005-12-05 15:36:08 +08002930 qc = ata_qc_from_tag(ap, ap->active_tag);
2931 assert(qc != NULL);
2932
Albert Lee14be71f2005-09-27 17:36:35 +08002933 switch (ap->hsm_task_state) {
2934 case HSM_ST:
2935 case HSM_ST_POLL:
2936 poll_state = HSM_ST_POLL;
2937 reg_state = HSM_ST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938 break;
Albert Lee14be71f2005-09-27 17:36:35 +08002939 case HSM_ST_LAST:
2940 case HSM_ST_LAST_POLL:
2941 poll_state = HSM_ST_LAST_POLL;
2942 reg_state = HSM_ST_LAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943 break;
2944 default:
2945 BUG();
2946 break;
2947 }
2948
2949 status = ata_chk_status(ap);
2950 if (status & ATA_BUSY) {
2951 if (time_after(jiffies, ap->pio_task_timeout)) {
Tejun Heo11a56d22006-01-23 13:09:36 +09002952 qc->err_mask |= AC_ERR_TIMEOUT;
Albert Lee7c398332005-11-09 13:03:30 +08002953 ap->hsm_task_state = HSM_ST_TMOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002954 return 0;
2955 }
Albert Lee14be71f2005-09-27 17:36:35 +08002956 ap->hsm_task_state = poll_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957 return ATA_SHORT_PAUSE;
2958 }
2959
Albert Lee14be71f2005-09-27 17:36:35 +08002960 ap->hsm_task_state = reg_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002961 return 0;
2962}
2963
2964/**
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04002965 * ata_pio_complete - check if drive is busy or idle
2966 * @ap: the target ata_port
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967 *
2968 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002969 * None. (executing in kernel thread context)
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04002970 *
2971 * RETURNS:
2972 * Non-zero if qc completed, zero otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973 */
2974
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04002975static int ata_pio_complete (struct ata_port *ap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976{
2977 struct ata_queued_cmd *qc;
2978 u8 drv_stat;
2979
2980 /*
Alan Cox31433ea2005-08-26 15:56:47 +01002981 * This is purely heuristic. This is a fast path. Sometimes when
2982 * we enter, BSY will be cleared in a chk-status or two. If not,
2983 * the drive is probably seeking or something. Snooze for a couple
2984 * msecs, then chk-status again. If still busy, fall back to
Albert Lee14be71f2005-09-27 17:36:35 +08002985 * HSM_ST_POLL state.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986 */
Albert Leefe79e682005-12-06 11:34:59 +08002987 drv_stat = ata_busy_wait(ap, ATA_BUSY, 10);
2988 if (drv_stat & ATA_BUSY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989 msleep(2);
Albert Leefe79e682005-12-06 11:34:59 +08002990 drv_stat = ata_busy_wait(ap, ATA_BUSY, 10);
2991 if (drv_stat & ATA_BUSY) {
Albert Lee14be71f2005-09-27 17:36:35 +08002992 ap->hsm_task_state = HSM_ST_LAST_POLL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002993 ap->pio_task_timeout = jiffies + ATA_TMOUT_PIO;
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04002994 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002995 }
2996 }
2997
Albert Leec14b8332005-12-05 15:36:08 +08002998 qc = ata_qc_from_tag(ap, ap->active_tag);
2999 assert(qc != NULL);
3000
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001 drv_stat = ata_wait_idle(ap);
3002 if (!ata_ok(drv_stat)) {
Albert Lee1c848982005-12-05 15:40:15 +08003003 qc->err_mask |= __ac_err_mask(drv_stat);
Albert Lee14be71f2005-09-27 17:36:35 +08003004 ap->hsm_task_state = HSM_ST_ERR;
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003005 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006 }
3007
Albert Lee14be71f2005-09-27 17:36:35 +08003008 ap->hsm_task_state = HSM_ST_IDLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009
Albert Leea22e2eb2005-12-05 15:38:02 +08003010 assert(qc->err_mask == 0);
3011 ata_poll_qc_complete(qc);
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003012
3013 /* another command may start at this point */
3014
3015 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016}
3017
Edward Falk0baab862005-06-02 18:17:13 -04003018
3019/**
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04003020 * swap_buf_le16 - swap halves of 16-words in place
Edward Falk0baab862005-06-02 18:17:13 -04003021 * @buf: Buffer to swap
3022 * @buf_words: Number of 16-bit words in buffer.
3023 *
3024 * Swap halves of 16-bit words if needed to convert from
3025 * little-endian byte order to native cpu byte order, or
3026 * vice-versa.
3027 *
3028 * LOCKING:
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04003029 * Inherited from caller.
Edward Falk0baab862005-06-02 18:17:13 -04003030 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031void swap_buf_le16(u16 *buf, unsigned int buf_words)
3032{
3033#ifdef __BIG_ENDIAN
3034 unsigned int i;
3035
3036 for (i = 0; i < buf_words; i++)
3037 buf[i] = le16_to_cpu(buf[i]);
3038#endif /* __BIG_ENDIAN */
3039}
3040
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003041/**
3042 * ata_mmio_data_xfer - Transfer data by MMIO
3043 * @ap: port to read/write
3044 * @buf: data buffer
3045 * @buflen: buffer length
Jeff Garzik344baba2005-09-07 01:15:17 -04003046 * @write_data: read/write
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003047 *
3048 * Transfer data from/to the device data register by MMIO.
3049 *
3050 * LOCKING:
3051 * Inherited from caller.
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003052 */
3053
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054static void ata_mmio_data_xfer(struct ata_port *ap, unsigned char *buf,
3055 unsigned int buflen, int write_data)
3056{
3057 unsigned int i;
3058 unsigned int words = buflen >> 1;
3059 u16 *buf16 = (u16 *) buf;
3060 void __iomem *mmio = (void __iomem *)ap->ioaddr.data_addr;
3061
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003062 /* Transfer multiple of 2 bytes */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003063 if (write_data) {
3064 for (i = 0; i < words; i++)
3065 writew(le16_to_cpu(buf16[i]), mmio);
3066 } else {
3067 for (i = 0; i < words; i++)
3068 buf16[i] = cpu_to_le16(readw(mmio));
3069 }
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003070
3071 /* Transfer trailing 1 byte, if any. */
3072 if (unlikely(buflen & 0x01)) {
3073 u16 align_buf[1] = { 0 };
3074 unsigned char *trailing_buf = buf + buflen - 1;
3075
3076 if (write_data) {
3077 memcpy(align_buf, trailing_buf, 1);
3078 writew(le16_to_cpu(align_buf[0]), mmio);
3079 } else {
3080 align_buf[0] = cpu_to_le16(readw(mmio));
3081 memcpy(trailing_buf, align_buf, 1);
3082 }
3083 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084}
3085
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003086/**
3087 * ata_pio_data_xfer - Transfer data by PIO
3088 * @ap: port to read/write
3089 * @buf: data buffer
3090 * @buflen: buffer length
Jeff Garzik344baba2005-09-07 01:15:17 -04003091 * @write_data: read/write
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003092 *
3093 * Transfer data from/to the device data register by PIO.
3094 *
3095 * LOCKING:
3096 * Inherited from caller.
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003097 */
3098
Linus Torvalds1da177e2005-04-16 15:20:36 -07003099static void ata_pio_data_xfer(struct ata_port *ap, unsigned char *buf,
3100 unsigned int buflen, int write_data)
3101{
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003102 unsigned int words = buflen >> 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003103
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003104 /* Transfer multiple of 2 bytes */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003105 if (write_data)
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003106 outsw(ap->ioaddr.data_addr, buf, words);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107 else
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003108 insw(ap->ioaddr.data_addr, buf, words);
3109
3110 /* Transfer trailing 1 byte, if any. */
3111 if (unlikely(buflen & 0x01)) {
3112 u16 align_buf[1] = { 0 };
3113 unsigned char *trailing_buf = buf + buflen - 1;
3114
3115 if (write_data) {
3116 memcpy(align_buf, trailing_buf, 1);
3117 outw(le16_to_cpu(align_buf[0]), ap->ioaddr.data_addr);
3118 } else {
3119 align_buf[0] = cpu_to_le16(inw(ap->ioaddr.data_addr));
3120 memcpy(trailing_buf, align_buf, 1);
3121 }
3122 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003123}
3124
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003125/**
3126 * ata_data_xfer - Transfer data from/to the data register.
3127 * @ap: port to read/write
3128 * @buf: data buffer
3129 * @buflen: buffer length
3130 * @do_write: read/write
3131 *
3132 * Transfer data from/to the device data register.
3133 *
3134 * LOCKING:
3135 * Inherited from caller.
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003136 */
3137
Linus Torvalds1da177e2005-04-16 15:20:36 -07003138static void ata_data_xfer(struct ata_port *ap, unsigned char *buf,
3139 unsigned int buflen, int do_write)
3140{
Alan Coxa1bd9e62006-01-17 20:53:50 +00003141 /* Make the crap hardware pay the costs not the good stuff */
3142 if (unlikely(ap->flags & ATA_FLAG_IRQ_MASK)) {
3143 unsigned long flags;
3144 local_irq_save(flags);
3145 if (ap->flags & ATA_FLAG_MMIO)
3146 ata_mmio_data_xfer(ap, buf, buflen, do_write);
3147 else
3148 ata_pio_data_xfer(ap, buf, buflen, do_write);
3149 local_irq_restore(flags);
3150 } else {
3151 if (ap->flags & ATA_FLAG_MMIO)
3152 ata_mmio_data_xfer(ap, buf, buflen, do_write);
3153 else
3154 ata_pio_data_xfer(ap, buf, buflen, do_write);
3155 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003156}
3157
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003158/**
3159 * ata_pio_sector - Transfer ATA_SECT_SIZE (512 bytes) of data.
3160 * @qc: Command on going
3161 *
3162 * Transfer ATA_SECT_SIZE of data from/to the ATA device.
3163 *
3164 * LOCKING:
3165 * Inherited from caller.
3166 */
3167
Linus Torvalds1da177e2005-04-16 15:20:36 -07003168static void ata_pio_sector(struct ata_queued_cmd *qc)
3169{
3170 int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003171 struct scatterlist *sg = qc->__sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003172 struct ata_port *ap = qc->ap;
3173 struct page *page;
3174 unsigned int offset;
3175 unsigned char *buf;
3176
3177 if (qc->cursect == (qc->nsect - 1))
Albert Lee14be71f2005-09-27 17:36:35 +08003178 ap->hsm_task_state = HSM_ST_LAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003179
3180 page = sg[qc->cursg].page;
3181 offset = sg[qc->cursg].offset + qc->cursg_ofs * ATA_SECT_SIZE;
3182
3183 /* get the current page and offset */
3184 page = nth_page(page, (offset >> PAGE_SHIFT));
3185 offset %= PAGE_SIZE;
3186
3187 buf = kmap(page) + offset;
3188
3189 qc->cursect++;
3190 qc->cursg_ofs++;
3191
Albert Lee32529e02005-05-26 03:49:42 -04003192 if ((qc->cursg_ofs * ATA_SECT_SIZE) == (&sg[qc->cursg])->length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003193 qc->cursg++;
3194 qc->cursg_ofs = 0;
3195 }
3196
3197 DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
3198
3199 /* do the actual data transfer */
3200 do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
3201 ata_data_xfer(ap, buf, ATA_SECT_SIZE, do_write);
3202
3203 kunmap(page);
3204}
3205
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003206/**
3207 * __atapi_pio_bytes - Transfer data from/to the ATAPI device.
3208 * @qc: Command on going
3209 * @bytes: number of bytes
3210 *
3211 * Transfer Transfer data from/to the ATAPI device.
3212 *
3213 * LOCKING:
3214 * Inherited from caller.
3215 *
3216 */
3217
Linus Torvalds1da177e2005-04-16 15:20:36 -07003218static void __atapi_pio_bytes(struct ata_queued_cmd *qc, unsigned int bytes)
3219{
3220 int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003221 struct scatterlist *sg = qc->__sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003222 struct ata_port *ap = qc->ap;
3223 struct page *page;
3224 unsigned char *buf;
3225 unsigned int offset, count;
3226
Albert Lee563a6e12005-08-12 14:17:50 +08003227 if (qc->curbytes + bytes >= qc->nbytes)
Albert Lee14be71f2005-09-27 17:36:35 +08003228 ap->hsm_task_state = HSM_ST_LAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003229
3230next_sg:
Albert Lee563a6e12005-08-12 14:17:50 +08003231 if (unlikely(qc->cursg >= qc->n_elem)) {
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003232 /*
Albert Lee563a6e12005-08-12 14:17:50 +08003233 * The end of qc->sg is reached and the device expects
3234 * more data to transfer. In order not to overrun qc->sg
3235 * and fulfill length specified in the byte count register,
3236 * - for read case, discard trailing data from the device
3237 * - for write case, padding zero data to the device
3238 */
3239 u16 pad_buf[1] = { 0 };
3240 unsigned int words = bytes >> 1;
3241 unsigned int i;
3242
3243 if (words) /* warning if bytes > 1 */
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003244 printk(KERN_WARNING "ata%u: %u bytes trailing data\n",
Albert Lee563a6e12005-08-12 14:17:50 +08003245 ap->id, bytes);
3246
3247 for (i = 0; i < words; i++)
3248 ata_data_xfer(ap, (unsigned char*)pad_buf, 2, do_write);
3249
Albert Lee14be71f2005-09-27 17:36:35 +08003250 ap->hsm_task_state = HSM_ST_LAST;
Albert Lee563a6e12005-08-12 14:17:50 +08003251 return;
3252 }
3253
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003254 sg = &qc->__sg[qc->cursg];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003255
Linus Torvalds1da177e2005-04-16 15:20:36 -07003256 page = sg->page;
3257 offset = sg->offset + qc->cursg_ofs;
3258
3259 /* get the current page and offset */
3260 page = nth_page(page, (offset >> PAGE_SHIFT));
3261 offset %= PAGE_SIZE;
3262
Albert Lee6952df02005-06-06 15:56:03 +08003263 /* don't overrun current sg */
Albert Lee32529e02005-05-26 03:49:42 -04003264 count = min(sg->length - qc->cursg_ofs, bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265
3266 /* don't cross page boundaries */
3267 count = min(count, (unsigned int)PAGE_SIZE - offset);
3268
3269 buf = kmap(page) + offset;
3270
3271 bytes -= count;
3272 qc->curbytes += count;
3273 qc->cursg_ofs += count;
3274
Albert Lee32529e02005-05-26 03:49:42 -04003275 if (qc->cursg_ofs == sg->length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003276 qc->cursg++;
3277 qc->cursg_ofs = 0;
3278 }
3279
3280 DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
3281
3282 /* do the actual data transfer */
3283 ata_data_xfer(ap, buf, count, do_write);
3284
3285 kunmap(page);
3286
Albert Lee563a6e12005-08-12 14:17:50 +08003287 if (bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003288 goto next_sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003289}
3290
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003291/**
3292 * atapi_pio_bytes - Transfer data from/to the ATAPI device.
3293 * @qc: Command on going
3294 *
3295 * Transfer Transfer data from/to the ATAPI device.
3296 *
3297 * LOCKING:
3298 * Inherited from caller.
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003299 */
3300
Linus Torvalds1da177e2005-04-16 15:20:36 -07003301static void atapi_pio_bytes(struct ata_queued_cmd *qc)
3302{
3303 struct ata_port *ap = qc->ap;
3304 struct ata_device *dev = qc->dev;
3305 unsigned int ireason, bc_lo, bc_hi, bytes;
3306 int i_write, do_write = (qc->tf.flags & ATA_TFLAG_WRITE) ? 1 : 0;
3307
3308 ap->ops->tf_read(ap, &qc->tf);
3309 ireason = qc->tf.nsect;
3310 bc_lo = qc->tf.lbam;
3311 bc_hi = qc->tf.lbah;
3312 bytes = (bc_hi << 8) | bc_lo;
3313
3314 /* shall be cleared to zero, indicating xfer of data */
3315 if (ireason & (1 << 0))
3316 goto err_out;
3317
3318 /* make sure transfer direction matches expected */
3319 i_write = ((ireason & (1 << 1)) == 0) ? 1 : 0;
3320 if (do_write != i_write)
3321 goto err_out;
3322
3323 __atapi_pio_bytes(qc, bytes);
3324
3325 return;
3326
3327err_out:
3328 printk(KERN_INFO "ata%u: dev %u: ATAPI check failed\n",
3329 ap->id, dev->devno);
Tejun Heo11a56d22006-01-23 13:09:36 +09003330 qc->err_mask |= AC_ERR_HSM;
Albert Lee14be71f2005-09-27 17:36:35 +08003331 ap->hsm_task_state = HSM_ST_ERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003332}
3333
3334/**
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04003335 * ata_pio_block - start PIO on a block
3336 * @ap: the target ata_port
Linus Torvalds1da177e2005-04-16 15:20:36 -07003337 *
3338 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003339 * None. (executing in kernel thread context)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003340 */
3341
3342static void ata_pio_block(struct ata_port *ap)
3343{
3344 struct ata_queued_cmd *qc;
3345 u8 status;
3346
3347 /*
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04003348 * This is purely heuristic. This is a fast path.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003349 * Sometimes when we enter, BSY will be cleared in
3350 * a chk-status or two. If not, the drive is probably seeking
3351 * or something. Snooze for a couple msecs, then
3352 * chk-status again. If still busy, fall back to
Albert Lee14be71f2005-09-27 17:36:35 +08003353 * HSM_ST_POLL state.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003354 */
3355 status = ata_busy_wait(ap, ATA_BUSY, 5);
3356 if (status & ATA_BUSY) {
3357 msleep(2);
3358 status = ata_busy_wait(ap, ATA_BUSY, 10);
3359 if (status & ATA_BUSY) {
Albert Lee14be71f2005-09-27 17:36:35 +08003360 ap->hsm_task_state = HSM_ST_POLL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003361 ap->pio_task_timeout = jiffies + ATA_TMOUT_PIO;
3362 return;
3363 }
3364 }
3365
3366 qc = ata_qc_from_tag(ap, ap->active_tag);
3367 assert(qc != NULL);
3368
Albert Leefe79e682005-12-06 11:34:59 +08003369 /* check error */
3370 if (status & (ATA_ERR | ATA_DF)) {
3371 qc->err_mask |= AC_ERR_DEV;
3372 ap->hsm_task_state = HSM_ST_ERR;
3373 return;
3374 }
3375
3376 /* transfer data if any */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003377 if (is_atapi_taskfile(&qc->tf)) {
Albert Leefe79e682005-12-06 11:34:59 +08003378 /* DRQ=0 means no more data to transfer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003379 if ((status & ATA_DRQ) == 0) {
Albert Lee14be71f2005-09-27 17:36:35 +08003380 ap->hsm_task_state = HSM_ST_LAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003381 return;
3382 }
3383
3384 atapi_pio_bytes(qc);
3385 } else {
3386 /* handle BSY=0, DRQ=0 as error */
3387 if ((status & ATA_DRQ) == 0) {
Tejun Heo11a56d22006-01-23 13:09:36 +09003388 qc->err_mask |= AC_ERR_HSM;
Albert Lee14be71f2005-09-27 17:36:35 +08003389 ap->hsm_task_state = HSM_ST_ERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003390 return;
3391 }
3392
3393 ata_pio_sector(qc);
3394 }
3395}
3396
3397static void ata_pio_error(struct ata_port *ap)
3398{
3399 struct ata_queued_cmd *qc;
Jeff Garzika7dac442005-10-30 04:44:42 -05003400
3401 printk(KERN_WARNING "ata%u: PIO error\n", ap->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003402
3403 qc = ata_qc_from_tag(ap, ap->active_tag);
3404 assert(qc != NULL);
3405
Albert Lee1c848982005-12-05 15:40:15 +08003406 /* make sure qc->err_mask is available to
3407 * know what's wrong and recover
3408 */
3409 assert(qc->err_mask);
3410
Albert Lee14be71f2005-09-27 17:36:35 +08003411 ap->hsm_task_state = HSM_ST_IDLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003412
Albert Leea22e2eb2005-12-05 15:38:02 +08003413 ata_poll_qc_complete(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003414}
3415
3416static void ata_pio_task(void *_data)
3417{
3418 struct ata_port *ap = _data;
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003419 unsigned long timeout;
3420 int qc_completed;
3421
3422fsm_start:
3423 timeout = 0;
3424 qc_completed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003425
Albert Lee14be71f2005-09-27 17:36:35 +08003426 switch (ap->hsm_task_state) {
3427 case HSM_ST_IDLE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003428 return;
3429
Albert Lee14be71f2005-09-27 17:36:35 +08003430 case HSM_ST:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003431 ata_pio_block(ap);
3432 break;
3433
Albert Lee14be71f2005-09-27 17:36:35 +08003434 case HSM_ST_LAST:
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003435 qc_completed = ata_pio_complete(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003436 break;
3437
Albert Lee14be71f2005-09-27 17:36:35 +08003438 case HSM_ST_POLL:
3439 case HSM_ST_LAST_POLL:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003440 timeout = ata_pio_poll(ap);
3441 break;
3442
Albert Lee14be71f2005-09-27 17:36:35 +08003443 case HSM_ST_TMOUT:
3444 case HSM_ST_ERR:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003445 ata_pio_error(ap);
3446 return;
3447 }
3448
3449 if (timeout)
Tejun Heo95064372006-01-23 13:09:37 +09003450 ata_queue_delayed_pio_task(ap, timeout);
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003451 else if (!qc_completed)
3452 goto fsm_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003453}
3454
Linus Torvalds1da177e2005-04-16 15:20:36 -07003455/**
3456 * ata_qc_timeout - Handle timeout of queued command
3457 * @qc: Command that timed out
3458 *
3459 * Some part of the kernel (currently, only the SCSI layer)
3460 * has noticed that the active command on port @ap has not
3461 * completed after a specified length of time. Handle this
3462 * condition by disabling DMA (if necessary) and completing
3463 * transactions, with error if necessary.
3464 *
3465 * This also handles the case of the "lost interrupt", where
3466 * for some reason (possibly hardware bug, possibly driver bug)
3467 * an interrupt was not delivered to the driver, even though the
3468 * transaction completed successfully.
3469 *
3470 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003471 * Inherited from SCSI layer (none, can sleep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003472 */
3473
3474static void ata_qc_timeout(struct ata_queued_cmd *qc)
3475{
3476 struct ata_port *ap = qc->ap;
Jeff Garzikb8f61532005-08-25 22:01:20 -04003477 struct ata_host_set *host_set = ap->host_set;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003478 u8 host_stat = 0, drv_stat;
Jeff Garzikb8f61532005-08-25 22:01:20 -04003479 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003480
3481 DPRINTK("ENTER\n");
3482
Jeff Garzikb8f61532005-08-25 22:01:20 -04003483 spin_lock_irqsave(&host_set->lock, flags);
3484
Linus Torvalds1da177e2005-04-16 15:20:36 -07003485 switch (qc->tf.protocol) {
3486
3487 case ATA_PROT_DMA:
3488 case ATA_PROT_ATAPI_DMA:
3489 host_stat = ap->ops->bmdma_status(ap);
3490
3491 /* before we do anything else, clear DMA-Start bit */
Alan Coxb73fc892005-08-26 16:03:19 +01003492 ap->ops->bmdma_stop(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003493
3494 /* fall through */
3495
3496 default:
3497 ata_altstatus(ap);
3498 drv_stat = ata_chk_status(ap);
3499
3500 /* ack bmdma irq events */
3501 ap->ops->irq_clear(ap);
3502
3503 printk(KERN_ERR "ata%u: command 0x%x timeout, stat 0x%x host_stat 0x%x\n",
3504 ap->id, qc->tf.command, drv_stat, host_stat);
3505
3506 /* complete taskfile transaction */
Albert Leea22e2eb2005-12-05 15:38:02 +08003507 qc->err_mask |= ac_err_mask(drv_stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003508 break;
3509 }
Jeff Garzikb8f61532005-08-25 22:01:20 -04003510
3511 spin_unlock_irqrestore(&host_set->lock, flags);
3512
Tejun Heoa72ec4c2006-01-23 13:09:37 +09003513 ata_eh_qc_complete(qc);
3514
Linus Torvalds1da177e2005-04-16 15:20:36 -07003515 DPRINTK("EXIT\n");
3516}
3517
3518/**
3519 * ata_eng_timeout - Handle timeout of queued command
3520 * @ap: Port on which timed-out command is active
3521 *
3522 * Some part of the kernel (currently, only the SCSI layer)
3523 * has noticed that the active command on port @ap has not
3524 * completed after a specified length of time. Handle this
3525 * condition by disabling DMA (if necessary) and completing
3526 * transactions, with error if necessary.
3527 *
3528 * This also handles the case of the "lost interrupt", where
3529 * for some reason (possibly hardware bug, possibly driver bug)
3530 * an interrupt was not delivered to the driver, even though the
3531 * transaction completed successfully.
3532 *
3533 * LOCKING:
3534 * Inherited from SCSI layer (none, can sleep)
3535 */
3536
3537void ata_eng_timeout(struct ata_port *ap)
3538{
3539 struct ata_queued_cmd *qc;
3540
3541 DPRINTK("ENTER\n");
3542
3543 qc = ata_qc_from_tag(ap, ap->active_tag);
Jeff Garzike12669e2005-10-05 18:39:23 -04003544 if (qc)
3545 ata_qc_timeout(qc);
3546 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003547 printk(KERN_ERR "ata%u: BUG: timeout without command\n",
3548 ap->id);
3549 goto out;
3550 }
3551
Linus Torvalds1da177e2005-04-16 15:20:36 -07003552out:
3553 DPRINTK("EXIT\n");
3554}
3555
3556/**
3557 * ata_qc_new - Request an available ATA command, for queueing
3558 * @ap: Port associated with device @dev
3559 * @dev: Device from whom we request an available command structure
3560 *
3561 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003562 * None.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003563 */
3564
3565static struct ata_queued_cmd *ata_qc_new(struct ata_port *ap)
3566{
3567 struct ata_queued_cmd *qc = NULL;
3568 unsigned int i;
3569
3570 for (i = 0; i < ATA_MAX_QUEUE; i++)
3571 if (!test_and_set_bit(i, &ap->qactive)) {
3572 qc = ata_qc_from_tag(ap, i);
3573 break;
3574 }
3575
3576 if (qc)
3577 qc->tag = i;
3578
3579 return qc;
3580}
3581
3582/**
3583 * ata_qc_new_init - Request an available ATA command, and initialize it
3584 * @ap: Port associated with device @dev
3585 * @dev: Device from whom we request an available command structure
3586 *
3587 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003588 * None.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003589 */
3590
3591struct ata_queued_cmd *ata_qc_new_init(struct ata_port *ap,
3592 struct ata_device *dev)
3593{
3594 struct ata_queued_cmd *qc;
3595
3596 qc = ata_qc_new(ap);
3597 if (qc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003598 qc->scsicmd = NULL;
3599 qc->ap = ap;
3600 qc->dev = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003601
Jeff Garzik2c13b7c2005-11-14 14:14:16 -05003602 ata_qc_reinit(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003603 }
3604
3605 return qc;
3606}
3607
Linus Torvalds1da177e2005-04-16 15:20:36 -07003608/**
3609 * ata_qc_free - free unused ata_queued_cmd
3610 * @qc: Command to complete
3611 *
3612 * Designed to free unused ata_queued_cmd object
3613 * in case something prevents using it.
3614 *
3615 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003616 * spin_lock_irqsave(host_set lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003617 */
3618void ata_qc_free(struct ata_queued_cmd *qc)
3619{
Tejun Heo4ba946e2006-01-23 13:09:36 +09003620 struct ata_port *ap = qc->ap;
3621 unsigned int tag;
3622
Linus Torvalds1da177e2005-04-16 15:20:36 -07003623 assert(qc != NULL); /* ata_qc_from_tag _might_ return NULL */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003624
Tejun Heo4ba946e2006-01-23 13:09:36 +09003625 qc->flags = 0;
3626 tag = qc->tag;
3627 if (likely(ata_tag_valid(tag))) {
3628 if (tag == ap->active_tag)
3629 ap->active_tag = ATA_TAG_POISON;
3630 qc->tag = ATA_TAG_POISON;
3631 clear_bit(tag, &ap->qactive);
3632 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003633}
3634
3635/**
3636 * ata_qc_complete - Complete an active ATA command
3637 * @qc: Command to complete
Randy Dunlap8e8b77d2005-11-01 21:29:27 -08003638 * @err_mask: ATA Status register contents
Jeff Garzik0cba6322005-05-30 19:49:12 -04003639 *
3640 * Indicate to the mid and upper layers that an ATA
3641 * command has completed, with either an ok or not-ok status.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003642 *
3643 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003644 * spin_lock_irqsave(host_set lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003645 */
3646
Albert Leea22e2eb2005-12-05 15:38:02 +08003647void ata_qc_complete(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003648{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003649 assert(qc != NULL); /* ata_qc_from_tag _might_ return NULL */
3650 assert(qc->flags & ATA_QCFLAG_ACTIVE);
3651
3652 if (likely(qc->flags & ATA_QCFLAG_DMAMAP))
3653 ata_sg_clean(qc);
3654
Albert Lee3f3791d2005-08-16 14:25:38 +08003655 /* atapi: mark qc as inactive to prevent the interrupt handler
3656 * from completing the command twice later, before the error handler
3657 * is called. (when rc != 0 and atapi request sense is needed)
3658 */
3659 qc->flags &= ~ATA_QCFLAG_ACTIVE;
3660
Linus Torvalds1da177e2005-04-16 15:20:36 -07003661 /* call completion callback */
Tejun Heo77853bf2006-01-23 13:09:36 +09003662 qc->complete_fn(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003663}
3664
3665static inline int ata_should_dma_map(struct ata_queued_cmd *qc)
3666{
3667 struct ata_port *ap = qc->ap;
3668
3669 switch (qc->tf.protocol) {
3670 case ATA_PROT_DMA:
3671 case ATA_PROT_ATAPI_DMA:
3672 return 1;
3673
3674 case ATA_PROT_ATAPI:
3675 case ATA_PROT_PIO:
3676 case ATA_PROT_PIO_MULT:
3677 if (ap->flags & ATA_FLAG_PIO_DMA)
3678 return 1;
3679
3680 /* fall through */
3681
3682 default:
3683 return 0;
3684 }
3685
3686 /* never reached */
3687}
3688
3689/**
3690 * ata_qc_issue - issue taskfile to device
3691 * @qc: command to issue to device
3692 *
3693 * Prepare an ATA command to submission to device.
3694 * This includes mapping the data into a DMA-able
3695 * area, filling in the S/G table, and finally
3696 * writing the taskfile to hardware, starting the command.
3697 *
3698 * LOCKING:
3699 * spin_lock_irqsave(host_set lock)
3700 *
3701 * RETURNS:
Tejun Heo9a3d9eb2006-01-23 13:09:36 +09003702 * Zero on success, AC_ERR_* mask on failure
Linus Torvalds1da177e2005-04-16 15:20:36 -07003703 */
3704
Tejun Heo9a3d9eb2006-01-23 13:09:36 +09003705unsigned int ata_qc_issue(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003706{
3707 struct ata_port *ap = qc->ap;
3708
3709 if (ata_should_dma_map(qc)) {
3710 if (qc->flags & ATA_QCFLAG_SG) {
3711 if (ata_sg_setup(qc))
Tejun Heo8e436af2006-01-23 13:09:36 +09003712 goto sg_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003713 } else if (qc->flags & ATA_QCFLAG_SINGLE) {
3714 if (ata_sg_setup_one(qc))
Tejun Heo8e436af2006-01-23 13:09:36 +09003715 goto sg_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003716 }
3717 } else {
3718 qc->flags &= ~ATA_QCFLAG_DMAMAP;
3719 }
3720
3721 ap->ops->qc_prep(qc);
3722
3723 qc->ap->active_tag = qc->tag;
3724 qc->flags |= ATA_QCFLAG_ACTIVE;
3725
3726 return ap->ops->qc_issue(qc);
3727
Tejun Heo8e436af2006-01-23 13:09:36 +09003728sg_err:
3729 qc->flags &= ~ATA_QCFLAG_DMAMAP;
Tejun Heo9a3d9eb2006-01-23 13:09:36 +09003730 return AC_ERR_SYSTEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003731}
3732
Edward Falk0baab862005-06-02 18:17:13 -04003733
Linus Torvalds1da177e2005-04-16 15:20:36 -07003734/**
3735 * ata_qc_issue_prot - issue taskfile to device in proto-dependent manner
3736 * @qc: command to issue to device
3737 *
3738 * Using various libata functions and hooks, this function
3739 * starts an ATA command. ATA commands are grouped into
3740 * classes called "protocols", and issuing each type of protocol
3741 * is slightly different.
3742 *
Edward Falk0baab862005-06-02 18:17:13 -04003743 * May be used as the qc_issue() entry in ata_port_operations.
3744 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003745 * LOCKING:
3746 * spin_lock_irqsave(host_set lock)
3747 *
3748 * RETURNS:
Tejun Heo9a3d9eb2006-01-23 13:09:36 +09003749 * Zero on success, AC_ERR_* mask on failure
Linus Torvalds1da177e2005-04-16 15:20:36 -07003750 */
3751
Tejun Heo9a3d9eb2006-01-23 13:09:36 +09003752unsigned int ata_qc_issue_prot(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003753{
3754 struct ata_port *ap = qc->ap;
3755
3756 ata_dev_select(ap, qc->dev->devno, 1, 0);
3757
3758 switch (qc->tf.protocol) {
3759 case ATA_PROT_NODATA:
Jeff Garzike5338252005-10-30 21:37:17 -05003760 ata_tf_to_host(ap, &qc->tf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003761 break;
3762
3763 case ATA_PROT_DMA:
3764 ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
3765 ap->ops->bmdma_setup(qc); /* set up bmdma */
3766 ap->ops->bmdma_start(qc); /* initiate bmdma */
3767 break;
3768
3769 case ATA_PROT_PIO: /* load tf registers, initiate polling pio */
3770 ata_qc_set_polling(qc);
Jeff Garzike5338252005-10-30 21:37:17 -05003771 ata_tf_to_host(ap, &qc->tf);
Albert Lee14be71f2005-09-27 17:36:35 +08003772 ap->hsm_task_state = HSM_ST;
Tejun Heo95064372006-01-23 13:09:37 +09003773 ata_queue_pio_task(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003774 break;
3775
3776 case ATA_PROT_ATAPI:
3777 ata_qc_set_polling(qc);
Jeff Garzike5338252005-10-30 21:37:17 -05003778 ata_tf_to_host(ap, &qc->tf);
Tejun Heo95064372006-01-23 13:09:37 +09003779 ata_queue_packet_task(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003780 break;
3781
3782 case ATA_PROT_ATAPI_NODATA:
Tejun Heoc1389502005-08-22 14:59:24 +09003783 ap->flags |= ATA_FLAG_NOINTR;
Jeff Garzike5338252005-10-30 21:37:17 -05003784 ata_tf_to_host(ap, &qc->tf);
Tejun Heo95064372006-01-23 13:09:37 +09003785 ata_queue_packet_task(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003786 break;
3787
3788 case ATA_PROT_ATAPI_DMA:
Tejun Heoc1389502005-08-22 14:59:24 +09003789 ap->flags |= ATA_FLAG_NOINTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003790 ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
3791 ap->ops->bmdma_setup(qc); /* set up bmdma */
Tejun Heo95064372006-01-23 13:09:37 +09003792 ata_queue_packet_task(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003793 break;
3794
3795 default:
3796 WARN_ON(1);
Tejun Heo9a3d9eb2006-01-23 13:09:36 +09003797 return AC_ERR_SYSTEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003798 }
3799
3800 return 0;
3801}
3802
3803/**
Edward Falk0baab862005-06-02 18:17:13 -04003804 * ata_bmdma_setup_mmio - Set up PCI IDE BMDMA transaction
Linus Torvalds1da177e2005-04-16 15:20:36 -07003805 * @qc: Info associated with this ATA transaction.
3806 *
3807 * LOCKING:
3808 * spin_lock_irqsave(host_set lock)
3809 */
3810
3811static void ata_bmdma_setup_mmio (struct ata_queued_cmd *qc)
3812{
3813 struct ata_port *ap = qc->ap;
3814 unsigned int rw = (qc->tf.flags & ATA_TFLAG_WRITE);
3815 u8 dmactl;
3816 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
3817
3818 /* load PRD table addr. */
3819 mb(); /* make sure PRD table writes are visible to controller */
3820 writel(ap->prd_dma, mmio + ATA_DMA_TABLE_OFS);
3821
3822 /* specify data direction, triple-check start bit is clear */
3823 dmactl = readb(mmio + ATA_DMA_CMD);
3824 dmactl &= ~(ATA_DMA_WR | ATA_DMA_START);
3825 if (!rw)
3826 dmactl |= ATA_DMA_WR;
3827 writeb(dmactl, mmio + ATA_DMA_CMD);
3828
3829 /* issue r/w command */
3830 ap->ops->exec_command(ap, &qc->tf);
3831}
3832
3833/**
Alan Coxb73fc892005-08-26 16:03:19 +01003834 * ata_bmdma_start_mmio - Start a PCI IDE BMDMA transaction
Linus Torvalds1da177e2005-04-16 15:20:36 -07003835 * @qc: Info associated with this ATA transaction.
3836 *
3837 * LOCKING:
3838 * spin_lock_irqsave(host_set lock)
3839 */
3840
3841static void ata_bmdma_start_mmio (struct ata_queued_cmd *qc)
3842{
3843 struct ata_port *ap = qc->ap;
3844 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
3845 u8 dmactl;
3846
3847 /* start host DMA transaction */
3848 dmactl = readb(mmio + ATA_DMA_CMD);
3849 writeb(dmactl | ATA_DMA_START, mmio + ATA_DMA_CMD);
3850
3851 /* Strictly, one may wish to issue a readb() here, to
3852 * flush the mmio write. However, control also passes
3853 * to the hardware at this point, and it will interrupt
3854 * us when we are to resume control. So, in effect,
3855 * we don't care when the mmio write flushes.
3856 * Further, a read of the DMA status register _immediately_
3857 * following the write may not be what certain flaky hardware
3858 * is expected, so I think it is best to not add a readb()
3859 * without first all the MMIO ATA cards/mobos.
3860 * Or maybe I'm just being paranoid.
3861 */
3862}
3863
3864/**
3865 * ata_bmdma_setup_pio - Set up PCI IDE BMDMA transaction (PIO)
3866 * @qc: Info associated with this ATA transaction.
3867 *
3868 * LOCKING:
3869 * spin_lock_irqsave(host_set lock)
3870 */
3871
3872static void ata_bmdma_setup_pio (struct ata_queued_cmd *qc)
3873{
3874 struct ata_port *ap = qc->ap;
3875 unsigned int rw = (qc->tf.flags & ATA_TFLAG_WRITE);
3876 u8 dmactl;
3877
3878 /* load PRD table addr. */
3879 outl(ap->prd_dma, ap->ioaddr.bmdma_addr + ATA_DMA_TABLE_OFS);
3880
3881 /* specify data direction, triple-check start bit is clear */
3882 dmactl = inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3883 dmactl &= ~(ATA_DMA_WR | ATA_DMA_START);
3884 if (!rw)
3885 dmactl |= ATA_DMA_WR;
3886 outb(dmactl, ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3887
3888 /* issue r/w command */
3889 ap->ops->exec_command(ap, &qc->tf);
3890}
3891
3892/**
3893 * ata_bmdma_start_pio - Start a PCI IDE BMDMA transaction (PIO)
3894 * @qc: Info associated with this ATA transaction.
3895 *
3896 * LOCKING:
3897 * spin_lock_irqsave(host_set lock)
3898 */
3899
3900static void ata_bmdma_start_pio (struct ata_queued_cmd *qc)
3901{
3902 struct ata_port *ap = qc->ap;
3903 u8 dmactl;
3904
3905 /* start host DMA transaction */
3906 dmactl = inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3907 outb(dmactl | ATA_DMA_START,
3908 ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3909}
3910
Edward Falk0baab862005-06-02 18:17:13 -04003911
3912/**
3913 * ata_bmdma_start - Start a PCI IDE BMDMA transaction
3914 * @qc: Info associated with this ATA transaction.
3915 *
3916 * Writes the ATA_DMA_START flag to the DMA command register.
3917 *
3918 * May be used as the bmdma_start() entry in ata_port_operations.
3919 *
3920 * LOCKING:
3921 * spin_lock_irqsave(host_set lock)
3922 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003923void ata_bmdma_start(struct ata_queued_cmd *qc)
3924{
3925 if (qc->ap->flags & ATA_FLAG_MMIO)
3926 ata_bmdma_start_mmio(qc);
3927 else
3928 ata_bmdma_start_pio(qc);
3929}
3930
Edward Falk0baab862005-06-02 18:17:13 -04003931
3932/**
3933 * ata_bmdma_setup - Set up PCI IDE BMDMA transaction
3934 * @qc: Info associated with this ATA transaction.
3935 *
3936 * Writes address of PRD table to device's PRD Table Address
3937 * register, sets the DMA control register, and calls
3938 * ops->exec_command() to start the transfer.
3939 *
3940 * May be used as the bmdma_setup() entry in ata_port_operations.
3941 *
3942 * LOCKING:
3943 * spin_lock_irqsave(host_set lock)
3944 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003945void ata_bmdma_setup(struct ata_queued_cmd *qc)
3946{
3947 if (qc->ap->flags & ATA_FLAG_MMIO)
3948 ata_bmdma_setup_mmio(qc);
3949 else
3950 ata_bmdma_setup_pio(qc);
3951}
3952
Edward Falk0baab862005-06-02 18:17:13 -04003953
3954/**
3955 * ata_bmdma_irq_clear - Clear PCI IDE BMDMA interrupt.
Jeff Garzikdecc6d02005-06-02 18:42:33 -04003956 * @ap: Port associated with this ATA transaction.
Edward Falk0baab862005-06-02 18:17:13 -04003957 *
3958 * Clear interrupt and error flags in DMA status register.
3959 *
3960 * May be used as the irq_clear() entry in ata_port_operations.
3961 *
3962 * LOCKING:
3963 * spin_lock_irqsave(host_set lock)
3964 */
3965
Linus Torvalds1da177e2005-04-16 15:20:36 -07003966void ata_bmdma_irq_clear(struct ata_port *ap)
3967{
3968 if (ap->flags & ATA_FLAG_MMIO) {
3969 void __iomem *mmio = ((void __iomem *) ap->ioaddr.bmdma_addr) + ATA_DMA_STATUS;
3970 writeb(readb(mmio), mmio);
3971 } else {
3972 unsigned long addr = ap->ioaddr.bmdma_addr + ATA_DMA_STATUS;
3973 outb(inb(addr), addr);
3974 }
3975
3976}
3977
Edward Falk0baab862005-06-02 18:17:13 -04003978
3979/**
3980 * ata_bmdma_status - Read PCI IDE BMDMA status
Jeff Garzikdecc6d02005-06-02 18:42:33 -04003981 * @ap: Port associated with this ATA transaction.
Edward Falk0baab862005-06-02 18:17:13 -04003982 *
3983 * Read and return BMDMA status register.
3984 *
3985 * May be used as the bmdma_status() entry in ata_port_operations.
3986 *
3987 * LOCKING:
3988 * spin_lock_irqsave(host_set lock)
3989 */
3990
Linus Torvalds1da177e2005-04-16 15:20:36 -07003991u8 ata_bmdma_status(struct ata_port *ap)
3992{
3993 u8 host_stat;
3994 if (ap->flags & ATA_FLAG_MMIO) {
3995 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
3996 host_stat = readb(mmio + ATA_DMA_STATUS);
3997 } else
Albert Leeee500aa2005-09-27 17:34:38 +08003998 host_stat = inb(ap->ioaddr.bmdma_addr + ATA_DMA_STATUS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003999 return host_stat;
4000}
4001
Edward Falk0baab862005-06-02 18:17:13 -04004002
4003/**
4004 * ata_bmdma_stop - Stop PCI IDE BMDMA transfer
Alan Coxb73fc892005-08-26 16:03:19 +01004005 * @qc: Command we are ending DMA for
Edward Falk0baab862005-06-02 18:17:13 -04004006 *
4007 * Clears the ATA_DMA_START flag in the dma control register
4008 *
4009 * May be used as the bmdma_stop() entry in ata_port_operations.
4010 *
4011 * LOCKING:
4012 * spin_lock_irqsave(host_set lock)
4013 */
4014
Alan Coxb73fc892005-08-26 16:03:19 +01004015void ata_bmdma_stop(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004016{
Alan Coxb73fc892005-08-26 16:03:19 +01004017 struct ata_port *ap = qc->ap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004018 if (ap->flags & ATA_FLAG_MMIO) {
4019 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
4020
4021 /* clear start/stop bit */
4022 writeb(readb(mmio + ATA_DMA_CMD) & ~ATA_DMA_START,
4023 mmio + ATA_DMA_CMD);
4024 } else {
4025 /* clear start/stop bit */
4026 outb(inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD) & ~ATA_DMA_START,
4027 ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
4028 }
4029
4030 /* one-PIO-cycle guaranteed wait, per spec, for HDMA1:0 transition */
4031 ata_altstatus(ap); /* dummy read */
4032}
4033
4034/**
4035 * ata_host_intr - Handle host interrupt for given (port, task)
4036 * @ap: Port on which interrupt arrived (possibly...)
4037 * @qc: Taskfile currently active in engine
4038 *
4039 * Handle host interrupt for given queued command. Currently,
4040 * only DMA interrupts are handled. All other commands are
4041 * handled via polling with interrupts disabled (nIEN bit).
4042 *
4043 * LOCKING:
4044 * spin_lock_irqsave(host_set lock)
4045 *
4046 * RETURNS:
4047 * One if interrupt was handled, zero if not (shared irq).
4048 */
4049
4050inline unsigned int ata_host_intr (struct ata_port *ap,
4051 struct ata_queued_cmd *qc)
4052{
4053 u8 status, host_stat;
4054
4055 switch (qc->tf.protocol) {
4056
4057 case ATA_PROT_DMA:
4058 case ATA_PROT_ATAPI_DMA:
4059 case ATA_PROT_ATAPI:
4060 /* check status of DMA engine */
4061 host_stat = ap->ops->bmdma_status(ap);
4062 VPRINTK("ata%u: host_stat 0x%X\n", ap->id, host_stat);
4063
4064 /* if it's not our irq... */
4065 if (!(host_stat & ATA_DMA_INTR))
4066 goto idle_irq;
4067
4068 /* before we do anything else, clear DMA-Start bit */
Alan Coxb73fc892005-08-26 16:03:19 +01004069 ap->ops->bmdma_stop(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004070
4071 /* fall through */
4072
4073 case ATA_PROT_ATAPI_NODATA:
4074 case ATA_PROT_NODATA:
4075 /* check altstatus */
4076 status = ata_altstatus(ap);
4077 if (status & ATA_BUSY)
4078 goto idle_irq;
4079
4080 /* check main status, clearing INTRQ */
4081 status = ata_chk_status(ap);
4082 if (unlikely(status & ATA_BUSY))
4083 goto idle_irq;
4084 DPRINTK("ata%u: protocol %d (dev_stat 0x%X)\n",
4085 ap->id, qc->tf.protocol, status);
4086
4087 /* ack bmdma irq events */
4088 ap->ops->irq_clear(ap);
4089
4090 /* complete taskfile transaction */
Albert Leea22e2eb2005-12-05 15:38:02 +08004091 qc->err_mask |= ac_err_mask(status);
4092 ata_qc_complete(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004093 break;
4094
4095 default:
4096 goto idle_irq;
4097 }
4098
4099 return 1; /* irq handled */
4100
4101idle_irq:
4102 ap->stats.idle_irq++;
4103
4104#ifdef ATA_IRQ_TRAP
4105 if ((ap->stats.idle_irq % 1000) == 0) {
4106 handled = 1;
4107 ata_irq_ack(ap, 0); /* debug trap */
4108 printk(KERN_WARNING "ata%d: irq trap\n", ap->id);
4109 }
4110#endif
4111 return 0; /* irq not handled */
4112}
4113
4114/**
4115 * ata_interrupt - Default ATA host interrupt handler
Jeff Garzik0cba6322005-05-30 19:49:12 -04004116 * @irq: irq line (unused)
4117 * @dev_instance: pointer to our ata_host_set information structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07004118 * @regs: unused
4119 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04004120 * Default interrupt handler for PCI IDE devices. Calls
4121 * ata_host_intr() for each port that is not disabled.
4122 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004123 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004124 * Obtains host_set lock during operation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004125 *
4126 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004127 * IRQ_NONE or IRQ_HANDLED.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004128 */
4129
4130irqreturn_t ata_interrupt (int irq, void *dev_instance, struct pt_regs *regs)
4131{
4132 struct ata_host_set *host_set = dev_instance;
4133 unsigned int i;
4134 unsigned int handled = 0;
4135 unsigned long flags;
4136
4137 /* TODO: make _irqsave conditional on x86 PCI IDE legacy mode */
4138 spin_lock_irqsave(&host_set->lock, flags);
4139
4140 for (i = 0; i < host_set->n_ports; i++) {
4141 struct ata_port *ap;
4142
4143 ap = host_set->ports[i];
Tejun Heoc1389502005-08-22 14:59:24 +09004144 if (ap &&
4145 !(ap->flags & (ATA_FLAG_PORT_DISABLED | ATA_FLAG_NOINTR))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004146 struct ata_queued_cmd *qc;
4147
4148 qc = ata_qc_from_tag(ap, ap->active_tag);
Albert Lee21b1ed72005-04-29 17:34:59 +08004149 if (qc && (!(qc->tf.ctl & ATA_NIEN)) &&
4150 (qc->flags & ATA_QCFLAG_ACTIVE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004151 handled |= ata_host_intr(ap, qc);
4152 }
4153 }
4154
4155 spin_unlock_irqrestore(&host_set->lock, flags);
4156
4157 return IRQ_RETVAL(handled);
4158}
4159
4160/**
4161 * atapi_packet_task - Write CDB bytes to hardware
4162 * @_data: Port to which ATAPI device is attached.
4163 *
4164 * When device has indicated its readiness to accept
4165 * a CDB, this function is called. Send the CDB.
4166 * If DMA is to be performed, exit immediately.
4167 * Otherwise, we are in polling mode, so poll
4168 * status under operation succeeds or fails.
4169 *
4170 * LOCKING:
4171 * Kernel thread context (may sleep)
4172 */
4173
4174static void atapi_packet_task(void *_data)
4175{
4176 struct ata_port *ap = _data;
4177 struct ata_queued_cmd *qc;
4178 u8 status;
4179
4180 qc = ata_qc_from_tag(ap, ap->active_tag);
4181 assert(qc != NULL);
4182 assert(qc->flags & ATA_QCFLAG_ACTIVE);
4183
4184 /* sleep-wait for BSY to clear */
4185 DPRINTK("busy wait\n");
Albert Leed8fe4522005-12-05 15:42:17 +08004186 if (ata_busy_sleep(ap, ATA_TMOUT_CDB_QUICK, ATA_TMOUT_CDB)) {
Tejun Heo11a56d22006-01-23 13:09:36 +09004187 qc->err_mask |= AC_ERR_TIMEOUT;
Albert Leed8fe4522005-12-05 15:42:17 +08004188 goto err_out;
4189 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004190
4191 /* make sure DRQ is set */
4192 status = ata_chk_status(ap);
Albert Leed8fe4522005-12-05 15:42:17 +08004193 if ((status & (ATA_BUSY | ATA_DRQ)) != ATA_DRQ) {
Tejun Heo11a56d22006-01-23 13:09:36 +09004194 qc->err_mask |= AC_ERR_HSM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004195 goto err_out;
Albert Leed8fe4522005-12-05 15:42:17 +08004196 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004197
4198 /* send SCSI cdb */
4199 DPRINTK("send cdb\n");
4200 assert(ap->cdb_len >= 12);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004201
Tejun Heoc1389502005-08-22 14:59:24 +09004202 if (qc->tf.protocol == ATA_PROT_ATAPI_DMA ||
4203 qc->tf.protocol == ATA_PROT_ATAPI_NODATA) {
4204 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004205
Tejun Heoc1389502005-08-22 14:59:24 +09004206 /* Once we're done issuing command and kicking bmdma,
4207 * irq handler takes over. To not lose irq, we need
4208 * to clear NOINTR flag before sending cdb, but
4209 * interrupt handler shouldn't be invoked before we're
4210 * finished. Hence, the following locking.
4211 */
4212 spin_lock_irqsave(&ap->host_set->lock, flags);
4213 ap->flags &= ~ATA_FLAG_NOINTR;
4214 ata_data_xfer(ap, qc->cdb, ap->cdb_len, 1);
4215 if (qc->tf.protocol == ATA_PROT_ATAPI_DMA)
4216 ap->ops->bmdma_start(qc); /* initiate bmdma */
4217 spin_unlock_irqrestore(&ap->host_set->lock, flags);
4218 } else {
4219 ata_data_xfer(ap, qc->cdb, ap->cdb_len, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004220
Tejun Heoc1389502005-08-22 14:59:24 +09004221 /* PIO commands are handled by polling */
Albert Lee14be71f2005-09-27 17:36:35 +08004222 ap->hsm_task_state = HSM_ST;
Tejun Heo95064372006-01-23 13:09:37 +09004223 ata_queue_pio_task(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004224 }
4225
4226 return;
4227
4228err_out:
Albert Leea22e2eb2005-12-05 15:38:02 +08004229 ata_poll_qc_complete(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004230}
4231
Edward Falk0baab862005-06-02 18:17:13 -04004232
4233/**
4234 * ata_port_start - Set port up for dma.
4235 * @ap: Port to initialize
4236 *
4237 * Called just after data structures for each port are
4238 * initialized. Allocates space for PRD table.
4239 *
4240 * May be used as the port_start() entry in ata_port_operations.
4241 *
4242 * LOCKING:
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04004243 * Inherited from caller.
Edward Falk0baab862005-06-02 18:17:13 -04004244 */
4245
Jens Axboe9b847542006-01-06 09:28:07 +01004246/*
4247 * Execute a 'simple' command, that only consists of the opcode 'cmd' itself,
4248 * without filling any other registers
4249 */
4250static int ata_do_simple_cmd(struct ata_port *ap, struct ata_device *dev,
4251 u8 cmd)
4252{
4253 struct ata_taskfile tf;
4254 int err;
4255
4256 ata_tf_init(ap, &tf, dev->devno);
4257
4258 tf.command = cmd;
4259 tf.flags |= ATA_TFLAG_DEVICE;
4260 tf.protocol = ATA_PROT_NODATA;
4261
4262 err = ata_exec_internal(ap, dev, &tf, DMA_NONE, NULL, 0);
4263 if (err)
4264 printk(KERN_ERR "%s: ata command failed: %d\n",
4265 __FUNCTION__, err);
4266
4267 return err;
4268}
4269
4270static int ata_flush_cache(struct ata_port *ap, struct ata_device *dev)
4271{
4272 u8 cmd;
4273
4274 if (!ata_try_flush_cache(dev))
4275 return 0;
4276
4277 if (ata_id_has_flush_ext(dev->id))
4278 cmd = ATA_CMD_FLUSH_EXT;
4279 else
4280 cmd = ATA_CMD_FLUSH;
4281
4282 return ata_do_simple_cmd(ap, dev, cmd);
4283}
4284
4285static int ata_standby_drive(struct ata_port *ap, struct ata_device *dev)
4286{
4287 return ata_do_simple_cmd(ap, dev, ATA_CMD_STANDBYNOW1);
4288}
4289
4290static int ata_start_drive(struct ata_port *ap, struct ata_device *dev)
4291{
4292 return ata_do_simple_cmd(ap, dev, ATA_CMD_IDLEIMMEDIATE);
4293}
4294
4295/**
4296 * ata_device_resume - wakeup a previously suspended devices
4297 *
4298 * Kick the drive back into action, by sending it an idle immediate
4299 * command and making sure its transfer mode matches between drive
4300 * and host.
4301 *
4302 */
4303int ata_device_resume(struct ata_port *ap, struct ata_device *dev)
4304{
4305 if (ap->flags & ATA_FLAG_SUSPENDED) {
4306 ap->flags &= ~ATA_FLAG_SUSPENDED;
4307 ata_set_mode(ap);
4308 }
4309 if (!ata_dev_present(dev))
4310 return 0;
4311 if (dev->class == ATA_DEV_ATA)
4312 ata_start_drive(ap, dev);
4313
4314 return 0;
4315}
4316
4317/**
4318 * ata_device_suspend - prepare a device for suspend
4319 *
4320 * Flush the cache on the drive, if appropriate, then issue a
4321 * standbynow command.
4322 *
4323 */
4324int ata_device_suspend(struct ata_port *ap, struct ata_device *dev)
4325{
4326 if (!ata_dev_present(dev))
4327 return 0;
4328 if (dev->class == ATA_DEV_ATA)
4329 ata_flush_cache(ap, dev);
4330
4331 ata_standby_drive(ap, dev);
4332 ap->flags |= ATA_FLAG_SUSPENDED;
4333 return 0;
4334}
4335
Linus Torvalds1da177e2005-04-16 15:20:36 -07004336int ata_port_start (struct ata_port *ap)
4337{
4338 struct device *dev = ap->host_set->dev;
Jeff Garzik6037d6b2005-11-04 22:08:00 -05004339 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004340
4341 ap->prd = dma_alloc_coherent(dev, ATA_PRD_TBL_SZ, &ap->prd_dma, GFP_KERNEL);
4342 if (!ap->prd)
4343 return -ENOMEM;
4344
Jeff Garzik6037d6b2005-11-04 22:08:00 -05004345 rc = ata_pad_alloc(ap, dev);
4346 if (rc) {
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004347 dma_free_coherent(dev, ATA_PRD_TBL_SZ, ap->prd, ap->prd_dma);
Jeff Garzik6037d6b2005-11-04 22:08:00 -05004348 return rc;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004349 }
4350
Linus Torvalds1da177e2005-04-16 15:20:36 -07004351 DPRINTK("prd alloc, virt %p, dma %llx\n", ap->prd, (unsigned long long) ap->prd_dma);
4352
4353 return 0;
4354}
4355
Edward Falk0baab862005-06-02 18:17:13 -04004356
4357/**
4358 * ata_port_stop - Undo ata_port_start()
4359 * @ap: Port to shut down
4360 *
4361 * Frees the PRD table.
4362 *
4363 * May be used as the port_stop() entry in ata_port_operations.
4364 *
4365 * LOCKING:
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04004366 * Inherited from caller.
Edward Falk0baab862005-06-02 18:17:13 -04004367 */
4368
Linus Torvalds1da177e2005-04-16 15:20:36 -07004369void ata_port_stop (struct ata_port *ap)
4370{
4371 struct device *dev = ap->host_set->dev;
4372
4373 dma_free_coherent(dev, ATA_PRD_TBL_SZ, ap->prd, ap->prd_dma);
Jeff Garzik6037d6b2005-11-04 22:08:00 -05004374 ata_pad_free(ap, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004375}
4376
Jeff Garzikaa8f0dc2005-05-26 21:54:27 -04004377void ata_host_stop (struct ata_host_set *host_set)
4378{
4379 if (host_set->mmio_base)
4380 iounmap(host_set->mmio_base);
4381}
4382
4383
Linus Torvalds1da177e2005-04-16 15:20:36 -07004384/**
4385 * ata_host_remove - Unregister SCSI host structure with upper layers
4386 * @ap: Port to unregister
4387 * @do_unregister: 1 if we fully unregister, 0 to just stop the port
4388 *
4389 * LOCKING:
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04004390 * Inherited from caller.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004391 */
4392
4393static void ata_host_remove(struct ata_port *ap, unsigned int do_unregister)
4394{
4395 struct Scsi_Host *sh = ap->host;
4396
4397 DPRINTK("ENTER\n");
4398
4399 if (do_unregister)
4400 scsi_remove_host(sh);
4401
4402 ap->ops->port_stop(ap);
4403}
4404
4405/**
4406 * ata_host_init - Initialize an ata_port structure
4407 * @ap: Structure to initialize
4408 * @host: associated SCSI mid-layer structure
4409 * @host_set: Collection of hosts to which @ap belongs
4410 * @ent: Probe information provided by low-level driver
4411 * @port_no: Port number associated with this ata_port
4412 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04004413 * Initialize a new ata_port structure, and its associated
4414 * scsi_host.
4415 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004416 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004417 * Inherited from caller.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004418 */
4419
4420static void ata_host_init(struct ata_port *ap, struct Scsi_Host *host,
4421 struct ata_host_set *host_set,
Jeff Garzik057ace52005-10-22 14:27:05 -04004422 const struct ata_probe_ent *ent, unsigned int port_no)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004423{
4424 unsigned int i;
4425
4426 host->max_id = 16;
4427 host->max_lun = 1;
4428 host->max_channel = 1;
4429 host->unique_id = ata_unique_id++;
4430 host->max_cmd_len = 12;
Christoph Hellwig12413192005-06-11 01:05:01 +02004431
Linus Torvalds1da177e2005-04-16 15:20:36 -07004432 ap->flags = ATA_FLAG_PORT_DISABLED;
4433 ap->id = host->unique_id;
4434 ap->host = host;
4435 ap->ctl = ATA_DEVCTL_OBS;
4436 ap->host_set = host_set;
4437 ap->port_no = port_no;
4438 ap->hard_port_no =
4439 ent->legacy_mode ? ent->hard_port_no : port_no;
4440 ap->pio_mask = ent->pio_mask;
4441 ap->mwdma_mask = ent->mwdma_mask;
4442 ap->udma_mask = ent->udma_mask;
4443 ap->flags |= ent->host_flags;
4444 ap->ops = ent->port_ops;
4445 ap->cbl = ATA_CBL_NONE;
4446 ap->active_tag = ATA_TAG_POISON;
4447 ap->last_ctl = 0xFF;
4448
4449 INIT_WORK(&ap->packet_task, atapi_packet_task, ap);
4450 INIT_WORK(&ap->pio_task, ata_pio_task, ap);
Tejun Heoa72ec4c2006-01-23 13:09:37 +09004451 INIT_LIST_HEAD(&ap->eh_done_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004452
4453 for (i = 0; i < ATA_MAX_DEVICES; i++)
4454 ap->device[i].devno = i;
4455
4456#ifdef ATA_IRQ_TRAP
4457 ap->stats.unhandled_irq = 1;
4458 ap->stats.idle_irq = 1;
4459#endif
4460
4461 memcpy(&ap->ioaddr, &ent->port[port_no], sizeof(struct ata_ioports));
4462}
4463
4464/**
4465 * ata_host_add - Attach low-level ATA driver to system
4466 * @ent: Information provided by low-level driver
4467 * @host_set: Collections of ports to which we add
4468 * @port_no: Port number associated with this host
4469 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04004470 * Attach low-level ATA driver to system.
4471 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004472 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004473 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004474 *
4475 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004476 * New ata_port on success, for NULL on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004477 */
4478
Jeff Garzik057ace52005-10-22 14:27:05 -04004479static struct ata_port * ata_host_add(const struct ata_probe_ent *ent,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004480 struct ata_host_set *host_set,
4481 unsigned int port_no)
4482{
4483 struct Scsi_Host *host;
4484 struct ata_port *ap;
4485 int rc;
4486
4487 DPRINTK("ENTER\n");
4488 host = scsi_host_alloc(ent->sht, sizeof(struct ata_port));
4489 if (!host)
4490 return NULL;
4491
4492 ap = (struct ata_port *) &host->hostdata[0];
4493
4494 ata_host_init(ap, host, host_set, ent, port_no);
4495
4496 rc = ap->ops->port_start(ap);
4497 if (rc)
4498 goto err_out;
4499
4500 return ap;
4501
4502err_out:
4503 scsi_host_put(host);
4504 return NULL;
4505}
4506
4507/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04004508 * ata_device_add - Register hardware device with ATA and SCSI layers
4509 * @ent: Probe information describing hardware device to be registered
4510 *
4511 * This function processes the information provided in the probe
4512 * information struct @ent, allocates the necessary ATA and SCSI
4513 * host information structures, initializes them, and registers
4514 * everything with requisite kernel subsystems.
4515 *
4516 * This function requests irqs, probes the ATA bus, and probes
4517 * the SCSI bus.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004518 *
4519 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004520 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004521 *
4522 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004523 * Number of ports registered. Zero on error (no ports registered).
Linus Torvalds1da177e2005-04-16 15:20:36 -07004524 */
4525
Jeff Garzik057ace52005-10-22 14:27:05 -04004526int ata_device_add(const struct ata_probe_ent *ent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004527{
4528 unsigned int count = 0, i;
4529 struct device *dev = ent->dev;
4530 struct ata_host_set *host_set;
4531
4532 DPRINTK("ENTER\n");
4533 /* alloc a container for our list of ATA ports (buses) */
Randy Dunlap57f3bda2005-10-28 20:37:23 -07004534 host_set = kzalloc(sizeof(struct ata_host_set) +
Linus Torvalds1da177e2005-04-16 15:20:36 -07004535 (ent->n_ports * sizeof(void *)), GFP_KERNEL);
4536 if (!host_set)
4537 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004538 spin_lock_init(&host_set->lock);
4539
4540 host_set->dev = dev;
4541 host_set->n_ports = ent->n_ports;
4542 host_set->irq = ent->irq;
4543 host_set->mmio_base = ent->mmio_base;
4544 host_set->private_data = ent->private_data;
4545 host_set->ops = ent->port_ops;
4546
4547 /* register each port bound to this device */
4548 for (i = 0; i < ent->n_ports; i++) {
4549 struct ata_port *ap;
4550 unsigned long xfer_mode_mask;
4551
4552 ap = ata_host_add(ent, host_set, i);
4553 if (!ap)
4554 goto err_out;
4555
4556 host_set->ports[i] = ap;
4557 xfer_mode_mask =(ap->udma_mask << ATA_SHIFT_UDMA) |
4558 (ap->mwdma_mask << ATA_SHIFT_MWDMA) |
4559 (ap->pio_mask << ATA_SHIFT_PIO);
4560
4561 /* print per-port info to dmesg */
4562 printk(KERN_INFO "ata%u: %cATA max %s cmd 0x%lX ctl 0x%lX "
4563 "bmdma 0x%lX irq %lu\n",
4564 ap->id,
4565 ap->flags & ATA_FLAG_SATA ? 'S' : 'P',
4566 ata_mode_string(xfer_mode_mask),
4567 ap->ioaddr.cmd_addr,
4568 ap->ioaddr.ctl_addr,
4569 ap->ioaddr.bmdma_addr,
4570 ent->irq);
4571
4572 ata_chk_status(ap);
4573 host_set->ops->irq_clear(ap);
4574 count++;
4575 }
4576
Randy Dunlap57f3bda2005-10-28 20:37:23 -07004577 if (!count)
4578 goto err_free_ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004579
4580 /* obtain irq, that is shared between channels */
4581 if (request_irq(ent->irq, ent->port_ops->irq_handler, ent->irq_flags,
4582 DRV_NAME, host_set))
4583 goto err_out;
4584
4585 /* perform each probe synchronously */
4586 DPRINTK("probe begin\n");
4587 for (i = 0; i < count; i++) {
4588 struct ata_port *ap;
4589 int rc;
4590
4591 ap = host_set->ports[i];
4592
4593 DPRINTK("ata%u: probe begin\n", ap->id);
4594 rc = ata_bus_probe(ap);
4595 DPRINTK("ata%u: probe end\n", ap->id);
4596
4597 if (rc) {
4598 /* FIXME: do something useful here?
4599 * Current libata behavior will
4600 * tear down everything when
4601 * the module is removed
4602 * or the h/w is unplugged.
4603 */
4604 }
4605
4606 rc = scsi_add_host(ap->host, dev);
4607 if (rc) {
4608 printk(KERN_ERR "ata%u: scsi_add_host failed\n",
4609 ap->id);
4610 /* FIXME: do something useful here */
4611 /* FIXME: handle unconditional calls to
4612 * scsi_scan_host and ata_host_remove, below,
4613 * at the very least
4614 */
4615 }
4616 }
4617
4618 /* probes are done, now scan each port's disk(s) */
4619 DPRINTK("probe begin\n");
4620 for (i = 0; i < count; i++) {
4621 struct ata_port *ap = host_set->ports[i];
4622
Jeff Garzik644dd0c2005-10-03 15:55:19 -04004623 ata_scsi_scan_host(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004624 }
4625
4626 dev_set_drvdata(dev, host_set);
4627
4628 VPRINTK("EXIT, returning %u\n", ent->n_ports);
4629 return ent->n_ports; /* success */
4630
4631err_out:
4632 for (i = 0; i < count; i++) {
4633 ata_host_remove(host_set->ports[i], 1);
4634 scsi_host_put(host_set->ports[i]->host);
4635 }
Randy Dunlap57f3bda2005-10-28 20:37:23 -07004636err_free_ret:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004637 kfree(host_set);
4638 VPRINTK("EXIT, returning 0\n");
4639 return 0;
4640}
4641
4642/**
Alan Cox17b14452005-09-15 15:44:00 +01004643 * ata_host_set_remove - PCI layer callback for device removal
4644 * @host_set: ATA host set that was removed
4645 *
4646 * Unregister all objects associated with this host set. Free those
4647 * objects.
4648 *
4649 * LOCKING:
4650 * Inherited from calling layer (may sleep).
4651 */
4652
Alan Cox17b14452005-09-15 15:44:00 +01004653void ata_host_set_remove(struct ata_host_set *host_set)
4654{
4655 struct ata_port *ap;
4656 unsigned int i;
4657
4658 for (i = 0; i < host_set->n_ports; i++) {
4659 ap = host_set->ports[i];
4660 scsi_remove_host(ap->host);
4661 }
4662
4663 free_irq(host_set->irq, host_set);
4664
4665 for (i = 0; i < host_set->n_ports; i++) {
4666 ap = host_set->ports[i];
4667
4668 ata_scsi_release(ap->host);
4669
4670 if ((ap->flags & ATA_FLAG_NO_LEGACY) == 0) {
4671 struct ata_ioports *ioaddr = &ap->ioaddr;
4672
4673 if (ioaddr->cmd_addr == 0x1f0)
4674 release_region(0x1f0, 8);
4675 else if (ioaddr->cmd_addr == 0x170)
4676 release_region(0x170, 8);
4677 }
4678
4679 scsi_host_put(ap->host);
4680 }
4681
4682 if (host_set->ops->host_stop)
4683 host_set->ops->host_stop(host_set);
4684
4685 kfree(host_set);
4686}
4687
4688/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004689 * ata_scsi_release - SCSI layer callback hook for host unload
4690 * @host: libata host to be unloaded
4691 *
4692 * Performs all duties necessary to shut down a libata port...
4693 * Kill port kthread, disable port, and release resources.
4694 *
4695 * LOCKING:
4696 * Inherited from SCSI layer.
4697 *
4698 * RETURNS:
4699 * One.
4700 */
4701
4702int ata_scsi_release(struct Scsi_Host *host)
4703{
4704 struct ata_port *ap = (struct ata_port *) &host->hostdata[0];
4705
4706 DPRINTK("ENTER\n");
4707
4708 ap->ops->port_disable(ap);
4709 ata_host_remove(ap, 0);
4710
4711 DPRINTK("EXIT\n");
4712 return 1;
4713}
4714
4715/**
4716 * ata_std_ports - initialize ioaddr with standard port offsets.
4717 * @ioaddr: IO address structure to be initialized
Edward Falk0baab862005-06-02 18:17:13 -04004718 *
4719 * Utility function which initializes data_addr, error_addr,
4720 * feature_addr, nsect_addr, lbal_addr, lbam_addr, lbah_addr,
4721 * device_addr, status_addr, and command_addr to standard offsets
4722 * relative to cmd_addr.
4723 *
4724 * Does not set ctl_addr, altstatus_addr, bmdma_addr, or scr_addr.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004725 */
Edward Falk0baab862005-06-02 18:17:13 -04004726
Linus Torvalds1da177e2005-04-16 15:20:36 -07004727void ata_std_ports(struct ata_ioports *ioaddr)
4728{
4729 ioaddr->data_addr = ioaddr->cmd_addr + ATA_REG_DATA;
4730 ioaddr->error_addr = ioaddr->cmd_addr + ATA_REG_ERR;
4731 ioaddr->feature_addr = ioaddr->cmd_addr + ATA_REG_FEATURE;
4732 ioaddr->nsect_addr = ioaddr->cmd_addr + ATA_REG_NSECT;
4733 ioaddr->lbal_addr = ioaddr->cmd_addr + ATA_REG_LBAL;
4734 ioaddr->lbam_addr = ioaddr->cmd_addr + ATA_REG_LBAM;
4735 ioaddr->lbah_addr = ioaddr->cmd_addr + ATA_REG_LBAH;
4736 ioaddr->device_addr = ioaddr->cmd_addr + ATA_REG_DEVICE;
4737 ioaddr->status_addr = ioaddr->cmd_addr + ATA_REG_STATUS;
4738 ioaddr->command_addr = ioaddr->cmd_addr + ATA_REG_CMD;
4739}
4740
4741static struct ata_probe_ent *
Jeff Garzik057ace52005-10-22 14:27:05 -04004742ata_probe_ent_alloc(struct device *dev, const struct ata_port_info *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004743{
4744 struct ata_probe_ent *probe_ent;
4745
Randy Dunlap57f3bda2005-10-28 20:37:23 -07004746 probe_ent = kzalloc(sizeof(*probe_ent), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004747 if (!probe_ent) {
4748 printk(KERN_ERR DRV_NAME "(%s): out of memory\n",
4749 kobject_name(&(dev->kobj)));
4750 return NULL;
4751 }
4752
Linus Torvalds1da177e2005-04-16 15:20:36 -07004753 INIT_LIST_HEAD(&probe_ent->node);
4754 probe_ent->dev = dev;
4755
4756 probe_ent->sht = port->sht;
4757 probe_ent->host_flags = port->host_flags;
4758 probe_ent->pio_mask = port->pio_mask;
4759 probe_ent->mwdma_mask = port->mwdma_mask;
4760 probe_ent->udma_mask = port->udma_mask;
4761 probe_ent->port_ops = port->port_ops;
4762
4763 return probe_ent;
4764}
4765
Edward Falk0baab862005-06-02 18:17:13 -04004766
4767
Jeff Garzik374b1872005-08-30 05:42:52 -04004768#ifdef CONFIG_PCI
4769
4770void ata_pci_host_stop (struct ata_host_set *host_set)
4771{
4772 struct pci_dev *pdev = to_pci_dev(host_set->dev);
4773
4774 pci_iounmap(pdev, host_set->mmio_base);
4775}
4776
Edward Falk0baab862005-06-02 18:17:13 -04004777/**
4778 * ata_pci_init_native_mode - Initialize native-mode driver
4779 * @pdev: pci device to be initialized
4780 * @port: array[2] of pointers to port info structures.
Alan Cox47a86592005-10-04 08:09:19 -04004781 * @ports: bitmap of ports present
Edward Falk0baab862005-06-02 18:17:13 -04004782 *
4783 * Utility function which allocates and initializes an
4784 * ata_probe_ent structure for a standard dual-port
4785 * PIO-based IDE controller. The returned ata_probe_ent
4786 * structure can be passed to ata_device_add(). The returned
4787 * ata_probe_ent structure should then be freed with kfree().
Alan Cox47a86592005-10-04 08:09:19 -04004788 *
4789 * The caller need only pass the address of the primary port, the
4790 * secondary will be deduced automatically. If the device has non
4791 * standard secondary port mappings this function can be called twice,
4792 * once for each interface.
Edward Falk0baab862005-06-02 18:17:13 -04004793 */
4794
Linus Torvalds1da177e2005-04-16 15:20:36 -07004795struct ata_probe_ent *
Alan Cox47a86592005-10-04 08:09:19 -04004796ata_pci_init_native_mode(struct pci_dev *pdev, struct ata_port_info **port, int ports)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004797{
4798 struct ata_probe_ent *probe_ent =
4799 ata_probe_ent_alloc(pci_dev_to_dev(pdev), port[0]);
Alan Cox47a86592005-10-04 08:09:19 -04004800 int p = 0;
4801
Linus Torvalds1da177e2005-04-16 15:20:36 -07004802 if (!probe_ent)
4803 return NULL;
4804
Linus Torvalds1da177e2005-04-16 15:20:36 -07004805 probe_ent->irq = pdev->irq;
4806 probe_ent->irq_flags = SA_SHIRQ;
Alan Coxe99f8b52005-11-08 14:09:44 +00004807 probe_ent->private_data = port[0]->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004808
Alan Cox47a86592005-10-04 08:09:19 -04004809 if (ports & ATA_PORT_PRIMARY) {
4810 probe_ent->port[p].cmd_addr = pci_resource_start(pdev, 0);
4811 probe_ent->port[p].altstatus_addr =
4812 probe_ent->port[p].ctl_addr =
4813 pci_resource_start(pdev, 1) | ATA_PCI_CTL_OFS;
4814 probe_ent->port[p].bmdma_addr = pci_resource_start(pdev, 4);
4815 ata_std_ports(&probe_ent->port[p]);
4816 p++;
4817 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004818
Alan Cox47a86592005-10-04 08:09:19 -04004819 if (ports & ATA_PORT_SECONDARY) {
4820 probe_ent->port[p].cmd_addr = pci_resource_start(pdev, 2);
4821 probe_ent->port[p].altstatus_addr =
4822 probe_ent->port[p].ctl_addr =
4823 pci_resource_start(pdev, 3) | ATA_PCI_CTL_OFS;
4824 probe_ent->port[p].bmdma_addr = pci_resource_start(pdev, 4) + 8;
4825 ata_std_ports(&probe_ent->port[p]);
4826 p++;
4827 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004828
Alan Cox47a86592005-10-04 08:09:19 -04004829 probe_ent->n_ports = p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004830 return probe_ent;
4831}
4832
Jeff Garzik0f0d5192005-10-30 06:41:29 -05004833static 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 -07004834{
Alan Cox47a86592005-10-04 08:09:19 -04004835 struct ata_probe_ent *probe_ent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004836
Jeff Garzik0f0d5192005-10-30 06:41:29 -05004837 probe_ent = ata_probe_ent_alloc(pci_dev_to_dev(pdev), port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004838 if (!probe_ent)
4839 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004840
Linus Torvalds1da177e2005-04-16 15:20:36 -07004841 probe_ent->legacy_mode = 1;
Alan Cox47a86592005-10-04 08:09:19 -04004842 probe_ent->n_ports = 1;
4843 probe_ent->hard_port_no = port_num;
Alan Coxe99f8b52005-11-08 14:09:44 +00004844 probe_ent->private_data = port->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004845
Alan Cox47a86592005-10-04 08:09:19 -04004846 switch(port_num)
4847 {
4848 case 0:
4849 probe_ent->irq = 14;
4850 probe_ent->port[0].cmd_addr = 0x1f0;
4851 probe_ent->port[0].altstatus_addr =
4852 probe_ent->port[0].ctl_addr = 0x3f6;
4853 break;
4854 case 1:
4855 probe_ent->irq = 15;
4856 probe_ent->port[0].cmd_addr = 0x170;
4857 probe_ent->port[0].altstatus_addr =
4858 probe_ent->port[0].ctl_addr = 0x376;
4859 break;
4860 }
4861 probe_ent->port[0].bmdma_addr = pci_resource_start(pdev, 4) + 8 * port_num;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004862 ata_std_ports(&probe_ent->port[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004863 return probe_ent;
4864}
4865
4866/**
4867 * ata_pci_init_one - Initialize/register PCI IDE host controller
4868 * @pdev: Controller to be initialized
4869 * @port_info: Information from low-level host driver
4870 * @n_ports: Number of ports attached to host controller
4871 *
Edward Falk0baab862005-06-02 18:17:13 -04004872 * This is a helper function which can be called from a driver's
4873 * xxx_init_one() probe function if the hardware uses traditional
4874 * IDE taskfile registers.
4875 *
4876 * This function calls pci_enable_device(), reserves its register
4877 * regions, sets the dma mask, enables bus master mode, and calls
4878 * ata_device_add()
4879 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004880 * LOCKING:
4881 * Inherited from PCI layer (may sleep).
4882 *
4883 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004884 * Zero on success, negative on errno-based value on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004885 */
4886
4887int ata_pci_init_one (struct pci_dev *pdev, struct ata_port_info **port_info,
4888 unsigned int n_ports)
4889{
Alan Cox47a86592005-10-04 08:09:19 -04004890 struct ata_probe_ent *probe_ent = NULL, *probe_ent2 = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004891 struct ata_port_info *port[2];
4892 u8 tmp8, mask;
4893 unsigned int legacy_mode = 0;
4894 int disable_dev_on_err = 1;
4895 int rc;
4896
4897 DPRINTK("ENTER\n");
4898
4899 port[0] = port_info[0];
4900 if (n_ports > 1)
4901 port[1] = port_info[1];
4902 else
4903 port[1] = port[0];
4904
4905 if ((port[0]->host_flags & ATA_FLAG_NO_LEGACY) == 0
4906 && (pdev->class >> 8) == PCI_CLASS_STORAGE_IDE) {
Alan Cox47a86592005-10-04 08:09:19 -04004907 /* TODO: What if one channel is in native mode ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004908 pci_read_config_byte(pdev, PCI_CLASS_PROG, &tmp8);
4909 mask = (1 << 2) | (1 << 0);
4910 if ((tmp8 & mask) != mask)
4911 legacy_mode = (1 << 3);
4912 }
4913
4914 /* FIXME... */
Alan Cox47a86592005-10-04 08:09:19 -04004915 if ((!legacy_mode) && (n_ports > 2)) {
4916 printk(KERN_ERR "ata: BUG: native mode, n_ports > 2\n");
4917 n_ports = 2;
4918 /* For now */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004919 }
4920
Alan Cox47a86592005-10-04 08:09:19 -04004921 /* FIXME: Really for ATA it isn't safe because the device may be
4922 multi-purpose and we want to leave it alone if it was already
4923 enabled. Secondly for shared use as Arjan says we want refcounting
4924
4925 Checking dev->is_enabled is insufficient as this is not set at
4926 boot for the primary video which is BIOS enabled
4927 */
4928
Linus Torvalds1da177e2005-04-16 15:20:36 -07004929 rc = pci_enable_device(pdev);
4930 if (rc)
4931 return rc;
4932
4933 rc = pci_request_regions(pdev, DRV_NAME);
4934 if (rc) {
4935 disable_dev_on_err = 0;
4936 goto err_out;
4937 }
4938
Alan Cox47a86592005-10-04 08:09:19 -04004939 /* FIXME: Should use platform specific mappers for legacy port ranges */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004940 if (legacy_mode) {
4941 if (!request_region(0x1f0, 8, "libata")) {
4942 struct resource *conflict, res;
4943 res.start = 0x1f0;
4944 res.end = 0x1f0 + 8 - 1;
4945 conflict = ____request_resource(&ioport_resource, &res);
4946 if (!strcmp(conflict->name, "libata"))
4947 legacy_mode |= (1 << 0);
4948 else {
4949 disable_dev_on_err = 0;
4950 printk(KERN_WARNING "ata: 0x1f0 IDE port busy\n");
4951 }
4952 } else
4953 legacy_mode |= (1 << 0);
4954
4955 if (!request_region(0x170, 8, "libata")) {
4956 struct resource *conflict, res;
4957 res.start = 0x170;
4958 res.end = 0x170 + 8 - 1;
4959 conflict = ____request_resource(&ioport_resource, &res);
4960 if (!strcmp(conflict->name, "libata"))
4961 legacy_mode |= (1 << 1);
4962 else {
4963 disable_dev_on_err = 0;
4964 printk(KERN_WARNING "ata: 0x170 IDE port busy\n");
4965 }
4966 } else
4967 legacy_mode |= (1 << 1);
4968 }
4969
4970 /* we have legacy mode, but all ports are unavailable */
4971 if (legacy_mode == (1 << 3)) {
4972 rc = -EBUSY;
4973 goto err_out_regions;
4974 }
4975
4976 rc = pci_set_dma_mask(pdev, ATA_DMA_MASK);
4977 if (rc)
4978 goto err_out_regions;
4979 rc = pci_set_consistent_dma_mask(pdev, ATA_DMA_MASK);
4980 if (rc)
4981 goto err_out_regions;
4982
4983 if (legacy_mode) {
Alan Cox47a86592005-10-04 08:09:19 -04004984 if (legacy_mode & (1 << 0))
Jeff Garzik0f0d5192005-10-30 06:41:29 -05004985 probe_ent = ata_pci_init_legacy_port(pdev, port[0], 0);
Alan Cox47a86592005-10-04 08:09:19 -04004986 if (legacy_mode & (1 << 1))
Jeff Garzik0f0d5192005-10-30 06:41:29 -05004987 probe_ent2 = ata_pci_init_legacy_port(pdev, port[1], 1);
Alan Cox47a86592005-10-04 08:09:19 -04004988 } else {
4989 if (n_ports == 2)
4990 probe_ent = ata_pci_init_native_mode(pdev, port, ATA_PORT_PRIMARY | ATA_PORT_SECONDARY);
4991 else
4992 probe_ent = ata_pci_init_native_mode(pdev, port, ATA_PORT_PRIMARY);
4993 }
4994 if (!probe_ent && !probe_ent2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004995 rc = -ENOMEM;
4996 goto err_out_regions;
4997 }
4998
4999 pci_set_master(pdev);
5000
5001 /* FIXME: check ata_device_add return */
5002 if (legacy_mode) {
5003 if (legacy_mode & (1 << 0))
5004 ata_device_add(probe_ent);
5005 if (legacy_mode & (1 << 1))
5006 ata_device_add(probe_ent2);
5007 } else
5008 ata_device_add(probe_ent);
5009
5010 kfree(probe_ent);
5011 kfree(probe_ent2);
5012
5013 return 0;
5014
5015err_out_regions:
5016 if (legacy_mode & (1 << 0))
5017 release_region(0x1f0, 8);
5018 if (legacy_mode & (1 << 1))
5019 release_region(0x170, 8);
5020 pci_release_regions(pdev);
5021err_out:
5022 if (disable_dev_on_err)
5023 pci_disable_device(pdev);
5024 return rc;
5025}
5026
5027/**
5028 * ata_pci_remove_one - PCI layer callback for device removal
5029 * @pdev: PCI device that was removed
5030 *
5031 * PCI layer indicates to libata via this hook that
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04005032 * hot-unplug or module unload event has occurred.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005033 * Handle this by unregistering all objects associated
5034 * with this PCI device. Free those objects. Then finally
5035 * release PCI resources and disable device.
5036 *
5037 * LOCKING:
5038 * Inherited from PCI layer (may sleep).
5039 */
5040
5041void ata_pci_remove_one (struct pci_dev *pdev)
5042{
5043 struct device *dev = pci_dev_to_dev(pdev);
5044 struct ata_host_set *host_set = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005045
Alan Cox17b14452005-09-15 15:44:00 +01005046 ata_host_set_remove(host_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005047 pci_release_regions(pdev);
5048 pci_disable_device(pdev);
5049 dev_set_drvdata(dev, NULL);
5050}
5051
5052/* move to PCI subsystem */
Jeff Garzik057ace52005-10-22 14:27:05 -04005053int pci_test_config_bits(struct pci_dev *pdev, const struct pci_bits *bits)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005054{
5055 unsigned long tmp = 0;
5056
5057 switch (bits->width) {
5058 case 1: {
5059 u8 tmp8 = 0;
5060 pci_read_config_byte(pdev, bits->reg, &tmp8);
5061 tmp = tmp8;
5062 break;
5063 }
5064 case 2: {
5065 u16 tmp16 = 0;
5066 pci_read_config_word(pdev, bits->reg, &tmp16);
5067 tmp = tmp16;
5068 break;
5069 }
5070 case 4: {
5071 u32 tmp32 = 0;
5072 pci_read_config_dword(pdev, bits->reg, &tmp32);
5073 tmp = tmp32;
5074 break;
5075 }
5076
5077 default:
5078 return -EINVAL;
5079 }
5080
5081 tmp &= bits->mask;
5082
5083 return (tmp == bits->val) ? 1 : 0;
5084}
Jens Axboe9b847542006-01-06 09:28:07 +01005085
5086int ata_pci_device_suspend(struct pci_dev *pdev, pm_message_t state)
5087{
5088 pci_save_state(pdev);
5089 pci_disable_device(pdev);
5090 pci_set_power_state(pdev, PCI_D3hot);
5091 return 0;
5092}
5093
5094int ata_pci_device_resume(struct pci_dev *pdev)
5095{
5096 pci_set_power_state(pdev, PCI_D0);
5097 pci_restore_state(pdev);
5098 pci_enable_device(pdev);
5099 pci_set_master(pdev);
5100 return 0;
5101}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005102#endif /* CONFIG_PCI */
5103
5104
Linus Torvalds1da177e2005-04-16 15:20:36 -07005105static int __init ata_init(void)
5106{
5107 ata_wq = create_workqueue("ata");
5108 if (!ata_wq)
5109 return -ENOMEM;
5110
5111 printk(KERN_DEBUG "libata version " DRV_VERSION " loaded.\n");
5112 return 0;
5113}
5114
5115static void __exit ata_exit(void)
5116{
5117 destroy_workqueue(ata_wq);
5118}
5119
5120module_init(ata_init);
5121module_exit(ata_exit);
5122
Jeff Garzik67846b32005-10-05 02:58:32 -04005123static unsigned long ratelimit_time;
5124static spinlock_t ata_ratelimit_lock = SPIN_LOCK_UNLOCKED;
5125
5126int ata_ratelimit(void)
5127{
5128 int rc;
5129 unsigned long flags;
5130
5131 spin_lock_irqsave(&ata_ratelimit_lock, flags);
5132
5133 if (time_after(jiffies, ratelimit_time)) {
5134 rc = 1;
5135 ratelimit_time = jiffies + (HZ/5);
5136 } else
5137 rc = 0;
5138
5139 spin_unlock_irqrestore(&ata_ratelimit_lock, flags);
5140
5141 return rc;
5142}
5143
Linus Torvalds1da177e2005-04-16 15:20:36 -07005144/*
5145 * libata is essentially a library of internal helper functions for
5146 * low-level ATA host controller drivers. As such, the API/ABI is
5147 * likely to change as new drivers are added and updated.
5148 * Do not depend on ABI/API stability.
5149 */
5150
5151EXPORT_SYMBOL_GPL(ata_std_bios_param);
5152EXPORT_SYMBOL_GPL(ata_std_ports);
5153EXPORT_SYMBOL_GPL(ata_device_add);
Alan Cox17b14452005-09-15 15:44:00 +01005154EXPORT_SYMBOL_GPL(ata_host_set_remove);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005155EXPORT_SYMBOL_GPL(ata_sg_init);
5156EXPORT_SYMBOL_GPL(ata_sg_init_one);
5157EXPORT_SYMBOL_GPL(ata_qc_complete);
5158EXPORT_SYMBOL_GPL(ata_qc_issue_prot);
5159EXPORT_SYMBOL_GPL(ata_eng_timeout);
5160EXPORT_SYMBOL_GPL(ata_tf_load);
5161EXPORT_SYMBOL_GPL(ata_tf_read);
5162EXPORT_SYMBOL_GPL(ata_noop_dev_select);
5163EXPORT_SYMBOL_GPL(ata_std_dev_select);
5164EXPORT_SYMBOL_GPL(ata_tf_to_fis);
5165EXPORT_SYMBOL_GPL(ata_tf_from_fis);
5166EXPORT_SYMBOL_GPL(ata_check_status);
5167EXPORT_SYMBOL_GPL(ata_altstatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005168EXPORT_SYMBOL_GPL(ata_exec_command);
5169EXPORT_SYMBOL_GPL(ata_port_start);
5170EXPORT_SYMBOL_GPL(ata_port_stop);
Jeff Garzikaa8f0dc2005-05-26 21:54:27 -04005171EXPORT_SYMBOL_GPL(ata_host_stop);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005172EXPORT_SYMBOL_GPL(ata_interrupt);
5173EXPORT_SYMBOL_GPL(ata_qc_prep);
5174EXPORT_SYMBOL_GPL(ata_bmdma_setup);
5175EXPORT_SYMBOL_GPL(ata_bmdma_start);
5176EXPORT_SYMBOL_GPL(ata_bmdma_irq_clear);
5177EXPORT_SYMBOL_GPL(ata_bmdma_status);
5178EXPORT_SYMBOL_GPL(ata_bmdma_stop);
5179EXPORT_SYMBOL_GPL(ata_port_probe);
5180EXPORT_SYMBOL_GPL(sata_phy_reset);
5181EXPORT_SYMBOL_GPL(__sata_phy_reset);
5182EXPORT_SYMBOL_GPL(ata_bus_reset);
5183EXPORT_SYMBOL_GPL(ata_port_disable);
Jeff Garzik67846b32005-10-05 02:58:32 -04005184EXPORT_SYMBOL_GPL(ata_ratelimit);
Tejun Heo6f8b9952006-01-24 17:05:21 +09005185EXPORT_SYMBOL_GPL(ata_busy_sleep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005186EXPORT_SYMBOL_GPL(ata_scsi_ioctl);
5187EXPORT_SYMBOL_GPL(ata_scsi_queuecmd);
5188EXPORT_SYMBOL_GPL(ata_scsi_error);
5189EXPORT_SYMBOL_GPL(ata_scsi_slave_config);
5190EXPORT_SYMBOL_GPL(ata_scsi_release);
5191EXPORT_SYMBOL_GPL(ata_host_intr);
5192EXPORT_SYMBOL_GPL(ata_dev_classify);
5193EXPORT_SYMBOL_GPL(ata_dev_id_string);
Brad Campbell6f2f3812005-05-12 15:07:47 -04005194EXPORT_SYMBOL_GPL(ata_dev_config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005195EXPORT_SYMBOL_GPL(ata_scsi_simulate);
Tejun Heoa72ec4c2006-01-23 13:09:37 +09005196EXPORT_SYMBOL_GPL(ata_eh_qc_complete);
5197EXPORT_SYMBOL_GPL(ata_eh_qc_retry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005198
Alan Cox1bc4ccf2006-01-09 17:18:14 +00005199EXPORT_SYMBOL_GPL(ata_pio_need_iordy);
Alan Cox452503f2005-10-21 19:01:32 -04005200EXPORT_SYMBOL_GPL(ata_timing_compute);
5201EXPORT_SYMBOL_GPL(ata_timing_merge);
5202
Linus Torvalds1da177e2005-04-16 15:20:36 -07005203#ifdef CONFIG_PCI
5204EXPORT_SYMBOL_GPL(pci_test_config_bits);
Jeff Garzik374b1872005-08-30 05:42:52 -04005205EXPORT_SYMBOL_GPL(ata_pci_host_stop);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005206EXPORT_SYMBOL_GPL(ata_pci_init_native_mode);
5207EXPORT_SYMBOL_GPL(ata_pci_init_one);
5208EXPORT_SYMBOL_GPL(ata_pci_remove_one);
Jens Axboe9b847542006-01-06 09:28:07 +01005209EXPORT_SYMBOL_GPL(ata_pci_device_suspend);
5210EXPORT_SYMBOL_GPL(ata_pci_device_resume);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005211#endif /* CONFIG_PCI */
Jens Axboe9b847542006-01-06 09:28:07 +01005212
5213EXPORT_SYMBOL_GPL(ata_device_suspend);
5214EXPORT_SYMBOL_GPL(ata_device_resume);
5215EXPORT_SYMBOL_GPL(ata_scsi_device_suspend);
5216EXPORT_SYMBOL_GPL(ata_scsi_device_resume);