blob: 1f78e246f5e07577a4b621e7bd7d6abb8bfb0589 [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
1488 ap->ops->phy_reset(ap);
1489 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1490 goto err_out;
1491
1492 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1493 ata_dev_identify(ap, i);
1494 if (ata_dev_present(&ap->device[i])) {
1495 found = 1;
Brad Campbell6f2f3812005-05-12 15:07:47 -04001496 ata_dev_config(ap,i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 }
1498 }
1499
1500 if ((!found) || (ap->flags & ATA_FLAG_PORT_DISABLED))
1501 goto err_out_disable;
1502
1503 ata_set_mode(ap);
1504 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1505 goto err_out_disable;
1506
1507 return 0;
1508
1509err_out_disable:
1510 ap->ops->port_disable(ap);
1511err_out:
1512 return -1;
1513}
1514
1515/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04001516 * ata_port_probe - Mark port as enabled
1517 * @ap: Port for which we indicate enablement
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04001519 * Modify @ap data structure such that the system
1520 * thinks that the entire port is enabled.
1521 *
1522 * LOCKING: host_set lock, or some other form of
1523 * serialization.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 */
1525
1526void ata_port_probe(struct ata_port *ap)
1527{
1528 ap->flags &= ~ATA_FLAG_PORT_DISABLED;
1529}
1530
1531/**
Tejun Heo3be680b2005-12-19 22:35:02 +09001532 * sata_print_link_status - Print SATA link status
1533 * @ap: SATA port to printk link status about
1534 *
1535 * This function prints link speed and status of a SATA link.
1536 *
1537 * LOCKING:
1538 * None.
1539 */
1540static void sata_print_link_status(struct ata_port *ap)
1541{
1542 u32 sstatus, tmp;
1543 const char *speed;
1544
1545 if (!ap->ops->scr_read)
1546 return;
1547
1548 sstatus = scr_read(ap, SCR_STATUS);
1549
1550 if (sata_dev_present(ap)) {
1551 tmp = (sstatus >> 4) & 0xf;
1552 if (tmp & (1 << 0))
1553 speed = "1.5";
1554 else if (tmp & (1 << 1))
1555 speed = "3.0";
1556 else
1557 speed = "<unknown>";
1558 printk(KERN_INFO "ata%u: SATA link up %s Gbps (SStatus %X)\n",
1559 ap->id, speed, sstatus);
1560 } else {
1561 printk(KERN_INFO "ata%u: SATA link down (SStatus %X)\n",
1562 ap->id, sstatus);
1563 }
1564}
1565
1566/**
Jeff Garzik780a87f2005-05-30 15:41:05 -04001567 * __sata_phy_reset - Wake/reset a low-level SATA PHY
1568 * @ap: SATA port associated with target SATA PHY.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001570 * This function issues commands to standard SATA Sxxx
1571 * PHY registers, to wake up the phy (and device), and
1572 * clear any reset condition.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 *
1574 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001575 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 *
1577 */
1578void __sata_phy_reset(struct ata_port *ap)
1579{
1580 u32 sstatus;
1581 unsigned long timeout = jiffies + (HZ * 5);
1582
1583 if (ap->flags & ATA_FLAG_SATA_RESET) {
Brett Russcdcca892005-03-28 15:10:27 -05001584 /* issue phy wake/reset */
1585 scr_write_flush(ap, SCR_CONTROL, 0x301);
Tejun Heo62ba2842005-06-26 23:27:19 +09001586 /* Couldn't find anything in SATA I/II specs, but
1587 * AHCI-1.1 10.4.2 says at least 1 ms. */
1588 mdelay(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 }
Brett Russcdcca892005-03-28 15:10:27 -05001590 scr_write_flush(ap, SCR_CONTROL, 0x300); /* phy wake/clear reset */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591
1592 /* wait for phy to become ready, if necessary */
1593 do {
1594 msleep(200);
1595 sstatus = scr_read(ap, SCR_STATUS);
1596 if ((sstatus & 0xf) != 1)
1597 break;
1598 } while (time_before(jiffies, timeout));
1599
Tejun Heo3be680b2005-12-19 22:35:02 +09001600 /* print link status */
1601 sata_print_link_status(ap);
Jeff Garzik656563e2005-11-20 03:36:45 -05001602
Tejun Heo3be680b2005-12-19 22:35:02 +09001603 /* TODO: phy layer with polling, timeouts, etc. */
1604 if (sata_dev_present(ap))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 ata_port_probe(ap);
Tejun Heo3be680b2005-12-19 22:35:02 +09001606 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 ata_port_disable(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608
1609 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1610 return;
1611
1612 if (ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT)) {
1613 ata_port_disable(ap);
1614 return;
1615 }
1616
1617 ap->cbl = ATA_CBL_SATA;
1618}
1619
1620/**
Jeff Garzik780a87f2005-05-30 15:41:05 -04001621 * sata_phy_reset - Reset SATA bus.
1622 * @ap: SATA port associated with target SATA PHY.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001624 * This function resets the SATA bus, and then probes
1625 * the bus for devices.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 *
1627 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001628 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 *
1630 */
1631void sata_phy_reset(struct ata_port *ap)
1632{
1633 __sata_phy_reset(ap);
1634 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1635 return;
1636 ata_bus_reset(ap);
1637}
1638
1639/**
Jeff Garzik780a87f2005-05-30 15:41:05 -04001640 * ata_port_disable - Disable port.
1641 * @ap: Port to be disabled.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001643 * Modify @ap data structure such that the system
1644 * thinks that the entire port is disabled, and should
1645 * never attempt to probe or communicate with devices
1646 * on this port.
1647 *
1648 * LOCKING: host_set lock, or some other form of
1649 * serialization.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 */
1651
1652void ata_port_disable(struct ata_port *ap)
1653{
1654 ap->device[0].class = ATA_DEV_NONE;
1655 ap->device[1].class = ATA_DEV_NONE;
1656 ap->flags |= ATA_FLAG_PORT_DISABLED;
1657}
1658
Alan Cox452503f2005-10-21 19:01:32 -04001659/*
1660 * This mode timing computation functionality is ported over from
1661 * drivers/ide/ide-timing.h and was originally written by Vojtech Pavlik
1662 */
1663/*
1664 * PIO 0-5, MWDMA 0-2 and UDMA 0-6 timings (in nanoseconds).
1665 * These were taken from ATA/ATAPI-6 standard, rev 0a, except
1666 * for PIO 5, which is a nonstandard extension and UDMA6, which
1667 * is currently supported only by Maxtor drives.
1668 */
1669
1670static const struct ata_timing ata_timing[] = {
1671
1672 { XFER_UDMA_6, 0, 0, 0, 0, 0, 0, 0, 15 },
1673 { XFER_UDMA_5, 0, 0, 0, 0, 0, 0, 0, 20 },
1674 { XFER_UDMA_4, 0, 0, 0, 0, 0, 0, 0, 30 },
1675 { XFER_UDMA_3, 0, 0, 0, 0, 0, 0, 0, 45 },
1676
1677 { XFER_UDMA_2, 0, 0, 0, 0, 0, 0, 0, 60 },
1678 { XFER_UDMA_1, 0, 0, 0, 0, 0, 0, 0, 80 },
1679 { XFER_UDMA_0, 0, 0, 0, 0, 0, 0, 0, 120 },
1680
1681/* { XFER_UDMA_SLOW, 0, 0, 0, 0, 0, 0, 0, 150 }, */
1682
1683 { XFER_MW_DMA_2, 25, 0, 0, 0, 70, 25, 120, 0 },
1684 { XFER_MW_DMA_1, 45, 0, 0, 0, 80, 50, 150, 0 },
1685 { XFER_MW_DMA_0, 60, 0, 0, 0, 215, 215, 480, 0 },
1686
1687 { XFER_SW_DMA_2, 60, 0, 0, 0, 120, 120, 240, 0 },
1688 { XFER_SW_DMA_1, 90, 0, 0, 0, 240, 240, 480, 0 },
1689 { XFER_SW_DMA_0, 120, 0, 0, 0, 480, 480, 960, 0 },
1690
1691/* { XFER_PIO_5, 20, 50, 30, 100, 50, 30, 100, 0 }, */
1692 { XFER_PIO_4, 25, 70, 25, 120, 70, 25, 120, 0 },
1693 { XFER_PIO_3, 30, 80, 70, 180, 80, 70, 180, 0 },
1694
1695 { XFER_PIO_2, 30, 290, 40, 330, 100, 90, 240, 0 },
1696 { XFER_PIO_1, 50, 290, 93, 383, 125, 100, 383, 0 },
1697 { XFER_PIO_0, 70, 290, 240, 600, 165, 150, 600, 0 },
1698
1699/* { XFER_PIO_SLOW, 120, 290, 240, 960, 290, 240, 960, 0 }, */
1700
1701 { 0xFF }
1702};
1703
1704#define ENOUGH(v,unit) (((v)-1)/(unit)+1)
1705#define EZ(v,unit) ((v)?ENOUGH(v,unit):0)
1706
1707static void ata_timing_quantize(const struct ata_timing *t, struct ata_timing *q, int T, int UT)
1708{
1709 q->setup = EZ(t->setup * 1000, T);
1710 q->act8b = EZ(t->act8b * 1000, T);
1711 q->rec8b = EZ(t->rec8b * 1000, T);
1712 q->cyc8b = EZ(t->cyc8b * 1000, T);
1713 q->active = EZ(t->active * 1000, T);
1714 q->recover = EZ(t->recover * 1000, T);
1715 q->cycle = EZ(t->cycle * 1000, T);
1716 q->udma = EZ(t->udma * 1000, UT);
1717}
1718
1719void ata_timing_merge(const struct ata_timing *a, const struct ata_timing *b,
1720 struct ata_timing *m, unsigned int what)
1721{
1722 if (what & ATA_TIMING_SETUP ) m->setup = max(a->setup, b->setup);
1723 if (what & ATA_TIMING_ACT8B ) m->act8b = max(a->act8b, b->act8b);
1724 if (what & ATA_TIMING_REC8B ) m->rec8b = max(a->rec8b, b->rec8b);
1725 if (what & ATA_TIMING_CYC8B ) m->cyc8b = max(a->cyc8b, b->cyc8b);
1726 if (what & ATA_TIMING_ACTIVE ) m->active = max(a->active, b->active);
1727 if (what & ATA_TIMING_RECOVER) m->recover = max(a->recover, b->recover);
1728 if (what & ATA_TIMING_CYCLE ) m->cycle = max(a->cycle, b->cycle);
1729 if (what & ATA_TIMING_UDMA ) m->udma = max(a->udma, b->udma);
1730}
1731
1732static const struct ata_timing* ata_timing_find_mode(unsigned short speed)
1733{
1734 const struct ata_timing *t;
1735
1736 for (t = ata_timing; t->mode != speed; t++)
Alan Cox91190752005-10-26 12:17:46 -04001737 if (t->mode == 0xFF)
Alan Cox452503f2005-10-21 19:01:32 -04001738 return NULL;
1739 return t;
1740}
1741
1742int ata_timing_compute(struct ata_device *adev, unsigned short speed,
1743 struct ata_timing *t, int T, int UT)
1744{
1745 const struct ata_timing *s;
1746 struct ata_timing p;
1747
1748 /*
1749 * Find the mode.
Albert Lee75b1f2f2005-11-16 17:06:18 +08001750 */
Alan Cox452503f2005-10-21 19:01:32 -04001751
1752 if (!(s = ata_timing_find_mode(speed)))
1753 return -EINVAL;
1754
Albert Lee75b1f2f2005-11-16 17:06:18 +08001755 memcpy(t, s, sizeof(*s));
1756
Alan Cox452503f2005-10-21 19:01:32 -04001757 /*
1758 * If the drive is an EIDE drive, it can tell us it needs extended
1759 * PIO/MW_DMA cycle timing.
1760 */
1761
1762 if (adev->id[ATA_ID_FIELD_VALID] & 2) { /* EIDE drive */
1763 memset(&p, 0, sizeof(p));
1764 if(speed >= XFER_PIO_0 && speed <= XFER_SW_DMA_0) {
1765 if (speed <= XFER_PIO_2) p.cycle = p.cyc8b = adev->id[ATA_ID_EIDE_PIO];
1766 else p.cycle = p.cyc8b = adev->id[ATA_ID_EIDE_PIO_IORDY];
1767 } else if(speed >= XFER_MW_DMA_0 && speed <= XFER_MW_DMA_2) {
1768 p.cycle = adev->id[ATA_ID_EIDE_DMA_MIN];
1769 }
1770 ata_timing_merge(&p, t, t, ATA_TIMING_CYCLE | ATA_TIMING_CYC8B);
1771 }
1772
1773 /*
1774 * Convert the timing to bus clock counts.
1775 */
1776
Albert Lee75b1f2f2005-11-16 17:06:18 +08001777 ata_timing_quantize(t, t, T, UT);
Alan Cox452503f2005-10-21 19:01:32 -04001778
1779 /*
1780 * Even in DMA/UDMA modes we still use PIO access for IDENTIFY, S.M.A.R.T
1781 * and some other commands. We have to ensure that the DMA cycle timing is
1782 * slower/equal than the fastest PIO timing.
1783 */
1784
1785 if (speed > XFER_PIO_4) {
1786 ata_timing_compute(adev, adev->pio_mode, &p, T, UT);
1787 ata_timing_merge(&p, t, t, ATA_TIMING_ALL);
1788 }
1789
1790 /*
1791 * Lenghten active & recovery time so that cycle time is correct.
1792 */
1793
1794 if (t->act8b + t->rec8b < t->cyc8b) {
1795 t->act8b += (t->cyc8b - (t->act8b + t->rec8b)) / 2;
1796 t->rec8b = t->cyc8b - t->act8b;
1797 }
1798
1799 if (t->active + t->recover < t->cycle) {
1800 t->active += (t->cycle - (t->active + t->recover)) / 2;
1801 t->recover = t->cycle - t->active;
1802 }
1803
1804 return 0;
1805}
1806
Jeff Garzik057ace52005-10-22 14:27:05 -04001807static const struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 unsigned int shift;
1809 u8 base;
1810} xfer_mode_classes[] = {
1811 { ATA_SHIFT_UDMA, XFER_UDMA_0 },
1812 { ATA_SHIFT_MWDMA, XFER_MW_DMA_0 },
1813 { ATA_SHIFT_PIO, XFER_PIO_0 },
1814};
1815
Arjan van de Ven858119e2006-01-14 13:20:43 -08001816static u8 base_from_shift(unsigned int shift)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817{
1818 int i;
1819
1820 for (i = 0; i < ARRAY_SIZE(xfer_mode_classes); i++)
1821 if (xfer_mode_classes[i].shift == shift)
1822 return xfer_mode_classes[i].base;
1823
1824 return 0xff;
1825}
1826
1827static void ata_dev_set_mode(struct ata_port *ap, struct ata_device *dev)
1828{
1829 int ofs, idx;
1830 u8 base;
1831
1832 if (!ata_dev_present(dev) || (ap->flags & ATA_FLAG_PORT_DISABLED))
1833 return;
1834
1835 if (dev->xfer_shift == ATA_SHIFT_PIO)
1836 dev->flags |= ATA_DFLAG_PIO;
1837
1838 ata_dev_set_xfermode(ap, dev);
1839
1840 base = base_from_shift(dev->xfer_shift);
1841 ofs = dev->xfer_mode - base;
1842 idx = ofs + dev->xfer_shift;
1843 WARN_ON(idx >= ARRAY_SIZE(xfer_mode_str));
1844
1845 DPRINTK("idx=%d xfer_shift=%u, xfer_mode=0x%x, base=0x%x, offset=%d\n",
1846 idx, dev->xfer_shift, (int)dev->xfer_mode, (int)base, ofs);
1847
1848 printk(KERN_INFO "ata%u: dev %u configured for %s\n",
1849 ap->id, dev->devno, xfer_mode_str[idx]);
1850}
1851
1852static int ata_host_set_pio(struct ata_port *ap)
1853{
1854 unsigned int mask;
1855 int x, i;
1856 u8 base, xfer_mode;
1857
1858 mask = ata_get_mode_mask(ap, ATA_SHIFT_PIO);
1859 x = fgb(mask);
1860 if (x < 0) {
1861 printk(KERN_WARNING "ata%u: no PIO support\n", ap->id);
1862 return -1;
1863 }
1864
1865 base = base_from_shift(ATA_SHIFT_PIO);
1866 xfer_mode = base + x;
1867
1868 DPRINTK("base 0x%x xfer_mode 0x%x mask 0x%x x %d\n",
1869 (int)base, (int)xfer_mode, mask, x);
1870
1871 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1872 struct ata_device *dev = &ap->device[i];
1873 if (ata_dev_present(dev)) {
1874 dev->pio_mode = xfer_mode;
1875 dev->xfer_mode = xfer_mode;
1876 dev->xfer_shift = ATA_SHIFT_PIO;
1877 if (ap->ops->set_piomode)
1878 ap->ops->set_piomode(ap, dev);
1879 }
1880 }
1881
1882 return 0;
1883}
1884
1885static void ata_host_set_dma(struct ata_port *ap, u8 xfer_mode,
1886 unsigned int xfer_shift)
1887{
1888 int i;
1889
1890 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1891 struct ata_device *dev = &ap->device[i];
1892 if (ata_dev_present(dev)) {
1893 dev->dma_mode = xfer_mode;
1894 dev->xfer_mode = xfer_mode;
1895 dev->xfer_shift = xfer_shift;
1896 if (ap->ops->set_dmamode)
1897 ap->ops->set_dmamode(ap, dev);
1898 }
1899 }
1900}
1901
1902/**
1903 * ata_set_mode - Program timings and issue SET FEATURES - XFER
1904 * @ap: port on which timings will be programmed
1905 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001906 * Set ATA device disk transfer mode (PIO3, UDMA6, etc.).
1907 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001909 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 *
1911 */
1912static void ata_set_mode(struct ata_port *ap)
1913{
Albert Lee8cbd6df2005-10-12 15:06:27 +08001914 unsigned int xfer_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 u8 xfer_mode;
1916 int rc;
1917
1918 /* step 1: always set host PIO timings */
1919 rc = ata_host_set_pio(ap);
1920 if (rc)
1921 goto err_out;
1922
1923 /* step 2: choose the best data xfer mode */
1924 xfer_mode = xfer_shift = 0;
1925 rc = ata_choose_xfer_mode(ap, &xfer_mode, &xfer_shift);
1926 if (rc)
1927 goto err_out;
1928
1929 /* step 3: if that xfer mode isn't PIO, set host DMA timings */
1930 if (xfer_shift != ATA_SHIFT_PIO)
1931 ata_host_set_dma(ap, xfer_mode, xfer_shift);
1932
1933 /* step 4: update devices' xfer mode */
1934 ata_dev_set_mode(ap, &ap->device[0]);
1935 ata_dev_set_mode(ap, &ap->device[1]);
1936
1937 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1938 return;
1939
1940 if (ap->ops->post_set_mode)
1941 ap->ops->post_set_mode(ap);
1942
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 return;
1944
1945err_out:
1946 ata_port_disable(ap);
1947}
1948
1949/**
1950 * ata_busy_sleep - sleep until BSY clears, or timeout
1951 * @ap: port containing status register to be polled
1952 * @tmout_pat: impatience timeout
1953 * @tmout: overall timeout
1954 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001955 * Sleep until ATA Status register bit BSY clears,
1956 * or a timeout occurs.
1957 *
1958 * LOCKING: None.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959 *
1960 */
1961
Tejun Heo6f8b9952006-01-24 17:05:21 +09001962unsigned int ata_busy_sleep (struct ata_port *ap,
1963 unsigned long tmout_pat, unsigned long tmout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964{
1965 unsigned long timer_start, timeout;
1966 u8 status;
1967
1968 status = ata_busy_wait(ap, ATA_BUSY, 300);
1969 timer_start = jiffies;
1970 timeout = timer_start + tmout_pat;
1971 while ((status & ATA_BUSY) && (time_before(jiffies, timeout))) {
1972 msleep(50);
1973 status = ata_busy_wait(ap, ATA_BUSY, 3);
1974 }
1975
1976 if (status & ATA_BUSY)
1977 printk(KERN_WARNING "ata%u is slow to respond, "
1978 "please be patient\n", ap->id);
1979
1980 timeout = timer_start + tmout;
1981 while ((status & ATA_BUSY) && (time_before(jiffies, timeout))) {
1982 msleep(50);
1983 status = ata_chk_status(ap);
1984 }
1985
1986 if (status & ATA_BUSY) {
1987 printk(KERN_ERR "ata%u failed to respond (%lu secs)\n",
1988 ap->id, tmout / HZ);
1989 return 1;
1990 }
1991
1992 return 0;
1993}
1994
1995static void ata_bus_post_reset(struct ata_port *ap, unsigned int devmask)
1996{
1997 struct ata_ioports *ioaddr = &ap->ioaddr;
1998 unsigned int dev0 = devmask & (1 << 0);
1999 unsigned int dev1 = devmask & (1 << 1);
2000 unsigned long timeout;
2001
2002 /* if device 0 was found in ata_devchk, wait for its
2003 * BSY bit to clear
2004 */
2005 if (dev0)
2006 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
2007
2008 /* if device 1 was found in ata_devchk, wait for
2009 * register access, then wait for BSY to clear
2010 */
2011 timeout = jiffies + ATA_TMOUT_BOOT;
2012 while (dev1) {
2013 u8 nsect, lbal;
2014
2015 ap->ops->dev_select(ap, 1);
2016 if (ap->flags & ATA_FLAG_MMIO) {
2017 nsect = readb((void __iomem *) ioaddr->nsect_addr);
2018 lbal = readb((void __iomem *) ioaddr->lbal_addr);
2019 } else {
2020 nsect = inb(ioaddr->nsect_addr);
2021 lbal = inb(ioaddr->lbal_addr);
2022 }
2023 if ((nsect == 1) && (lbal == 1))
2024 break;
2025 if (time_after(jiffies, timeout)) {
2026 dev1 = 0;
2027 break;
2028 }
2029 msleep(50); /* give drive a breather */
2030 }
2031 if (dev1)
2032 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
2033
2034 /* is all this really necessary? */
2035 ap->ops->dev_select(ap, 0);
2036 if (dev1)
2037 ap->ops->dev_select(ap, 1);
2038 if (dev0)
2039 ap->ops->dev_select(ap, 0);
2040}
2041
2042/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04002043 * ata_bus_edd - Issue EXECUTE DEVICE DIAGNOSTIC command.
2044 * @ap: Port to reset and probe
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04002046 * Use the EXECUTE DEVICE DIAGNOSTIC command to reset and
2047 * probe the bus. Not often used these days.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 *
2049 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002050 * PCI/etc. bus probe sem.
Jeff Garzike5338252005-10-30 21:37:17 -05002051 * Obtains host_set lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052 *
2053 */
2054
2055static unsigned int ata_bus_edd(struct ata_port *ap)
2056{
2057 struct ata_taskfile tf;
Jeff Garzike5338252005-10-30 21:37:17 -05002058 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059
2060 /* set up execute-device-diag (bus reset) taskfile */
2061 /* also, take interrupts to a known state (disabled) */
2062 DPRINTK("execute-device-diag\n");
2063 ata_tf_init(ap, &tf, 0);
2064 tf.ctl |= ATA_NIEN;
2065 tf.command = ATA_CMD_EDD;
2066 tf.protocol = ATA_PROT_NODATA;
2067
2068 /* do bus reset */
Jeff Garzike5338252005-10-30 21:37:17 -05002069 spin_lock_irqsave(&ap->host_set->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070 ata_tf_to_host(ap, &tf);
Jeff Garzike5338252005-10-30 21:37:17 -05002071 spin_unlock_irqrestore(&ap->host_set->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072
2073 /* spec says at least 2ms. but who knows with those
2074 * crazy ATAPI devices...
2075 */
2076 msleep(150);
2077
2078 return ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
2079}
2080
2081static unsigned int ata_bus_softreset(struct ata_port *ap,
2082 unsigned int devmask)
2083{
2084 struct ata_ioports *ioaddr = &ap->ioaddr;
2085
2086 DPRINTK("ata%u: bus reset via SRST\n", ap->id);
2087
2088 /* software reset. causes dev0 to be selected */
2089 if (ap->flags & ATA_FLAG_MMIO) {
2090 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
2091 udelay(20); /* FIXME: flush */
2092 writeb(ap->ctl | ATA_SRST, (void __iomem *) ioaddr->ctl_addr);
2093 udelay(20); /* FIXME: flush */
2094 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
2095 } else {
2096 outb(ap->ctl, ioaddr->ctl_addr);
2097 udelay(10);
2098 outb(ap->ctl | ATA_SRST, ioaddr->ctl_addr);
2099 udelay(10);
2100 outb(ap->ctl, ioaddr->ctl_addr);
2101 }
2102
2103 /* spec mandates ">= 2ms" before checking status.
2104 * We wait 150ms, because that was the magic delay used for
2105 * ATAPI devices in Hale Landis's ATADRVR, for the period of time
2106 * between when the ATA command register is written, and then
2107 * status is checked. Because waiting for "a while" before
2108 * checking status is fine, post SRST, we perform this magic
2109 * delay here as well.
2110 */
2111 msleep(150);
2112
2113 ata_bus_post_reset(ap, devmask);
2114
2115 return 0;
2116}
2117
2118/**
2119 * ata_bus_reset - reset host port and associated ATA channel
2120 * @ap: port to reset
2121 *
2122 * This is typically the first time we actually start issuing
2123 * commands to the ATA channel. We wait for BSY to clear, then
2124 * issue EXECUTE DEVICE DIAGNOSTIC command, polling for its
2125 * result. Determine what devices, if any, are on the channel
2126 * by looking at the device 0/1 error register. Look at the signature
2127 * stored in each device's taskfile registers, to determine if
2128 * the device is ATA or ATAPI.
2129 *
2130 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002131 * PCI/etc. bus probe sem.
2132 * Obtains host_set lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133 *
2134 * SIDE EFFECTS:
2135 * Sets ATA_FLAG_PORT_DISABLED if bus reset fails.
2136 */
2137
2138void ata_bus_reset(struct ata_port *ap)
2139{
2140 struct ata_ioports *ioaddr = &ap->ioaddr;
2141 unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS;
2142 u8 err;
2143 unsigned int dev0, dev1 = 0, rc = 0, devmask = 0;
2144
2145 DPRINTK("ENTER, host %u, port %u\n", ap->id, ap->port_no);
2146
2147 /* determine if device 0/1 are present */
2148 if (ap->flags & ATA_FLAG_SATA_RESET)
2149 dev0 = 1;
2150 else {
2151 dev0 = ata_devchk(ap, 0);
2152 if (slave_possible)
2153 dev1 = ata_devchk(ap, 1);
2154 }
2155
2156 if (dev0)
2157 devmask |= (1 << 0);
2158 if (dev1)
2159 devmask |= (1 << 1);
2160
2161 /* select device 0 again */
2162 ap->ops->dev_select(ap, 0);
2163
2164 /* issue bus reset */
2165 if (ap->flags & ATA_FLAG_SRST)
2166 rc = ata_bus_softreset(ap, devmask);
2167 else if ((ap->flags & ATA_FLAG_SATA_RESET) == 0) {
2168 /* set up device control */
2169 if (ap->flags & ATA_FLAG_MMIO)
2170 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
2171 else
2172 outb(ap->ctl, ioaddr->ctl_addr);
2173 rc = ata_bus_edd(ap);
2174 }
2175
2176 if (rc)
2177 goto err_out;
2178
2179 /*
2180 * determine by signature whether we have ATA or ATAPI devices
2181 */
Tejun Heob4dc7622006-01-24 17:05:22 +09002182 ap->device[0].class = ata_dev_try_classify(ap, 0, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183 if ((slave_possible) && (err != 0x81))
Tejun Heob4dc7622006-01-24 17:05:22 +09002184 ap->device[1].class = ata_dev_try_classify(ap, 1, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185
2186 /* re-enable interrupts */
2187 if (ap->ioaddr.ctl_addr) /* FIXME: hack. create a hook instead */
2188 ata_irq_on(ap);
2189
2190 /* is double-select really necessary? */
2191 if (ap->device[1].class != ATA_DEV_NONE)
2192 ap->ops->dev_select(ap, 1);
2193 if (ap->device[0].class != ATA_DEV_NONE)
2194 ap->ops->dev_select(ap, 0);
2195
2196 /* if no devices were detected, disable this port */
2197 if ((ap->device[0].class == ATA_DEV_NONE) &&
2198 (ap->device[1].class == ATA_DEV_NONE))
2199 goto err_out;
2200
2201 if (ap->flags & (ATA_FLAG_SATA_RESET | ATA_FLAG_SRST)) {
2202 /* set up device control for ATA_FLAG_SATA_RESET */
2203 if (ap->flags & ATA_FLAG_MMIO)
2204 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
2205 else
2206 outb(ap->ctl, ioaddr->ctl_addr);
2207 }
2208
2209 DPRINTK("EXIT\n");
2210 return;
2211
2212err_out:
2213 printk(KERN_ERR "ata%u: disabling port\n", ap->id);
2214 ap->ops->port_disable(ap);
2215
2216 DPRINTK("EXIT\n");
2217}
2218
Jeff Garzik057ace52005-10-22 14:27:05 -04002219static void ata_pr_blacklisted(const struct ata_port *ap,
2220 const struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221{
2222 printk(KERN_WARNING "ata%u: dev %u is on DMA blacklist, disabling DMA\n",
2223 ap->id, dev->devno);
2224}
2225
Arjan van de Ven98ac62d2005-11-28 10:06:23 +01002226static const char * const ata_dma_blacklist [] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227 "WDC AC11000H",
2228 "WDC AC22100H",
2229 "WDC AC32500H",
2230 "WDC AC33100H",
2231 "WDC AC31600H",
2232 "WDC AC32100H",
2233 "WDC AC23200L",
2234 "Compaq CRD-8241B",
2235 "CRD-8400B",
2236 "CRD-8480B",
2237 "CRD-8482B",
2238 "CRD-84",
2239 "SanDisk SDP3B",
2240 "SanDisk SDP3B-64",
2241 "SANYO CD-ROM CRD",
2242 "HITACHI CDR-8",
2243 "HITACHI CDR-8335",
2244 "HITACHI CDR-8435",
2245 "Toshiba CD-ROM XM-6202B",
Jeff Garzike9222562005-06-28 00:03:37 -04002246 "TOSHIBA CD-ROM XM-1702BC",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247 "CD-532E-A",
2248 "E-IDE CD-ROM CR-840",
2249 "CD-ROM Drive/F5A",
2250 "WPI CDD-820",
2251 "SAMSUNG CD-ROM SC-148C",
2252 "SAMSUNG CD-ROM SC",
2253 "SanDisk SDP3B-64",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254 "ATAPI CD-ROM DRIVE 40X MAXIMUM",
2255 "_NEC DV5800A",
2256};
2257
Jeff Garzik057ace52005-10-22 14:27:05 -04002258static int ata_dma_blacklisted(const struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259{
2260 unsigned char model_num[40];
2261 char *s;
2262 unsigned int len;
2263 int i;
2264
2265 ata_dev_id_string(dev->id, model_num, ATA_ID_PROD_OFS,
2266 sizeof(model_num));
2267 s = &model_num[0];
2268 len = strnlen(s, sizeof(model_num));
2269
2270 /* ATAPI specifies that empty space is blank-filled; remove blanks */
2271 while ((len > 0) && (s[len - 1] == ' ')) {
2272 len--;
2273 s[len] = 0;
2274 }
2275
2276 for (i = 0; i < ARRAY_SIZE(ata_dma_blacklist); i++)
2277 if (!strncmp(ata_dma_blacklist[i], s, len))
2278 return 1;
2279
2280 return 0;
2281}
2282
Jeff Garzik057ace52005-10-22 14:27:05 -04002283static unsigned int ata_get_mode_mask(const struct ata_port *ap, int shift)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284{
Jeff Garzik057ace52005-10-22 14:27:05 -04002285 const struct ata_device *master, *slave;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286 unsigned int mask;
2287
2288 master = &ap->device[0];
2289 slave = &ap->device[1];
2290
2291 assert (ata_dev_present(master) || ata_dev_present(slave));
2292
2293 if (shift == ATA_SHIFT_UDMA) {
2294 mask = ap->udma_mask;
2295 if (ata_dev_present(master)) {
2296 mask &= (master->id[ATA_ID_UDMA_MODES] & 0xff);
Jeff Garzik057ace52005-10-22 14:27:05 -04002297 if (ata_dma_blacklisted(master)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298 mask = 0;
2299 ata_pr_blacklisted(ap, master);
2300 }
2301 }
2302 if (ata_dev_present(slave)) {
2303 mask &= (slave->id[ATA_ID_UDMA_MODES] & 0xff);
Jeff Garzik057ace52005-10-22 14:27:05 -04002304 if (ata_dma_blacklisted(slave)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305 mask = 0;
2306 ata_pr_blacklisted(ap, slave);
2307 }
2308 }
2309 }
2310 else if (shift == ATA_SHIFT_MWDMA) {
2311 mask = ap->mwdma_mask;
2312 if (ata_dev_present(master)) {
2313 mask &= (master->id[ATA_ID_MWDMA_MODES] & 0x07);
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_MWDMA_MODES] & 0x07);
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_PIO) {
2328 mask = ap->pio_mask;
2329 if (ata_dev_present(master)) {
2330 /* spec doesn't return explicit support for
2331 * PIO0-2, so we fake it
2332 */
2333 u16 tmp_mode = master->id[ATA_ID_PIO_MODES] & 0x03;
2334 tmp_mode <<= 3;
2335 tmp_mode |= 0x7;
2336 mask &= tmp_mode;
2337 }
2338 if (ata_dev_present(slave)) {
2339 /* spec doesn't return explicit support for
2340 * PIO0-2, so we fake it
2341 */
2342 u16 tmp_mode = slave->id[ATA_ID_PIO_MODES] & 0x03;
2343 tmp_mode <<= 3;
2344 tmp_mode |= 0x7;
2345 mask &= tmp_mode;
2346 }
2347 }
2348 else {
2349 mask = 0xffffffff; /* shut up compiler warning */
2350 BUG();
2351 }
2352
2353 return mask;
2354}
2355
2356/* find greatest bit */
2357static int fgb(u32 bitmap)
2358{
2359 unsigned int i;
2360 int x = -1;
2361
2362 for (i = 0; i < 32; i++)
2363 if (bitmap & (1 << i))
2364 x = i;
2365
2366 return x;
2367}
2368
2369/**
2370 * ata_choose_xfer_mode - attempt to find best transfer mode
2371 * @ap: Port for which an xfer mode will be selected
2372 * @xfer_mode_out: (output) SET FEATURES - XFER MODE code
2373 * @xfer_shift_out: (output) bit shift that selects this mode
2374 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04002375 * Based on host and device capabilities, determine the
2376 * maximum transfer mode that is amenable to all.
2377 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002379 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380 *
2381 * RETURNS:
2382 * Zero on success, negative on error.
2383 */
2384
Jeff Garzik057ace52005-10-22 14:27:05 -04002385static int ata_choose_xfer_mode(const struct ata_port *ap,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386 u8 *xfer_mode_out,
2387 unsigned int *xfer_shift_out)
2388{
2389 unsigned int mask, shift;
2390 int x, i;
2391
2392 for (i = 0; i < ARRAY_SIZE(xfer_mode_classes); i++) {
2393 shift = xfer_mode_classes[i].shift;
2394 mask = ata_get_mode_mask(ap, shift);
2395
2396 x = fgb(mask);
2397 if (x >= 0) {
2398 *xfer_mode_out = xfer_mode_classes[i].base + x;
2399 *xfer_shift_out = shift;
2400 return 0;
2401 }
2402 }
2403
2404 return -1;
2405}
2406
2407/**
2408 * ata_dev_set_xfermode - Issue SET FEATURES - XFER MODE command
2409 * @ap: Port associated with device @dev
2410 * @dev: Device to which command will be sent
2411 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04002412 * Issue SET FEATURES - XFER MODE command to device @dev
2413 * on port @ap.
2414 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002416 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417 */
2418
2419static void ata_dev_set_xfermode(struct ata_port *ap, struct ata_device *dev)
2420{
Tejun Heoa0123702005-12-13 14:49:31 +09002421 struct ata_taskfile tf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422
2423 /* set up set-features taskfile */
2424 DPRINTK("set features - xfer mode\n");
2425
Tejun Heoa0123702005-12-13 14:49:31 +09002426 ata_tf_init(ap, &tf, dev->devno);
2427 tf.command = ATA_CMD_SET_FEATURES;
2428 tf.feature = SETFEATURES_XFER;
2429 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
2430 tf.protocol = ATA_PROT_NODATA;
2431 tf.nsect = dev->xfer_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432
Tejun Heoa0123702005-12-13 14:49:31 +09002433 if (ata_exec_internal(ap, dev, &tf, DMA_NONE, NULL, 0)) {
2434 printk(KERN_ERR "ata%u: failed to set xfermode, disabled\n",
2435 ap->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436 ata_port_disable(ap);
Tejun Heoa0123702005-12-13 14:49:31 +09002437 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438
2439 DPRINTK("EXIT\n");
2440}
2441
2442/**
Albert Lee59a10b12005-10-12 15:09:42 +08002443 * ata_dev_reread_id - Reread the device identify device info
2444 * @ap: port where the device is
2445 * @dev: device to reread the identify device info
2446 *
2447 * LOCKING:
2448 */
2449
2450static void ata_dev_reread_id(struct ata_port *ap, struct ata_device *dev)
2451{
Tejun Heoa0123702005-12-13 14:49:31 +09002452 struct ata_taskfile tf;
Albert Lee59a10b12005-10-12 15:09:42 +08002453
Tejun Heoa0123702005-12-13 14:49:31 +09002454 ata_tf_init(ap, &tf, dev->devno);
Albert Lee59a10b12005-10-12 15:09:42 +08002455
2456 if (dev->class == ATA_DEV_ATA) {
Tejun Heoa0123702005-12-13 14:49:31 +09002457 tf.command = ATA_CMD_ID_ATA;
Albert Lee59a10b12005-10-12 15:09:42 +08002458 DPRINTK("do ATA identify\n");
2459 } else {
Tejun Heoa0123702005-12-13 14:49:31 +09002460 tf.command = ATA_CMD_ID_ATAPI;
Albert Lee59a10b12005-10-12 15:09:42 +08002461 DPRINTK("do ATAPI identify\n");
2462 }
2463
Tejun Heoa0123702005-12-13 14:49:31 +09002464 tf.flags |= ATA_TFLAG_DEVICE;
2465 tf.protocol = ATA_PROT_PIO;
Albert Lee59a10b12005-10-12 15:09:42 +08002466
Tejun Heoa0123702005-12-13 14:49:31 +09002467 if (ata_exec_internal(ap, dev, &tf, DMA_FROM_DEVICE,
2468 dev->id, sizeof(dev->id)))
Albert Lee59a10b12005-10-12 15:09:42 +08002469 goto err_out;
2470
Albert Lee59a10b12005-10-12 15:09:42 +08002471 swap_buf_le16(dev->id, ATA_ID_WORDS);
2472
2473 ata_dump_id(dev);
2474
2475 DPRINTK("EXIT\n");
2476
2477 return;
2478err_out:
Tejun Heoa0123702005-12-13 14:49:31 +09002479 printk(KERN_ERR "ata%u: failed to reread ID, disabled\n", ap->id);
Albert Lee59a10b12005-10-12 15:09:42 +08002480 ata_port_disable(ap);
2481}
2482
2483/**
Albert Lee8bf62ec2005-05-12 15:29:42 -04002484 * ata_dev_init_params - Issue INIT DEV PARAMS command
2485 * @ap: Port associated with device @dev
2486 * @dev: Device to which command will be sent
2487 *
2488 * LOCKING:
2489 */
2490
2491static void ata_dev_init_params(struct ata_port *ap, struct ata_device *dev)
2492{
Tejun Heoa0123702005-12-13 14:49:31 +09002493 struct ata_taskfile tf;
Albert Lee8bf62ec2005-05-12 15:29:42 -04002494 u16 sectors = dev->id[6];
2495 u16 heads = dev->id[3];
2496
2497 /* Number of sectors per track 1-255. Number of heads 1-16 */
2498 if (sectors < 1 || sectors > 255 || heads < 1 || heads > 16)
2499 return;
2500
2501 /* set up init dev params taskfile */
2502 DPRINTK("init dev params \n");
2503
Tejun Heoa0123702005-12-13 14:49:31 +09002504 ata_tf_init(ap, &tf, dev->devno);
2505 tf.command = ATA_CMD_INIT_DEV_PARAMS;
2506 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
2507 tf.protocol = ATA_PROT_NODATA;
2508 tf.nsect = sectors;
2509 tf.device |= (heads - 1) & 0x0f; /* max head = num. of heads - 1 */
Albert Lee8bf62ec2005-05-12 15:29:42 -04002510
Tejun Heoa0123702005-12-13 14:49:31 +09002511 if (ata_exec_internal(ap, dev, &tf, DMA_NONE, NULL, 0)) {
2512 printk(KERN_ERR "ata%u: failed to init parameters, disabled\n",
2513 ap->id);
Albert Lee8bf62ec2005-05-12 15:29:42 -04002514 ata_port_disable(ap);
Tejun Heoa0123702005-12-13 14:49:31 +09002515 }
Albert Lee8bf62ec2005-05-12 15:29:42 -04002516
2517 DPRINTK("EXIT\n");
2518}
2519
2520/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04002521 * ata_sg_clean - Unmap DMA memory associated with command
2522 * @qc: Command containing DMA memory to be released
2523 *
2524 * Unmap all mapped DMA memory associated with this command.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525 *
2526 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002527 * spin_lock_irqsave(host_set lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528 */
2529
2530static void ata_sg_clean(struct ata_queued_cmd *qc)
2531{
2532 struct ata_port *ap = qc->ap;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002533 struct scatterlist *sg = qc->__sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534 int dir = qc->dma_dir;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002535 void *pad_buf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536
2537 assert(qc->flags & ATA_QCFLAG_DMAMAP);
2538 assert(sg != NULL);
2539
2540 if (qc->flags & ATA_QCFLAG_SINGLE)
2541 assert(qc->n_elem == 1);
2542
Jeff Garzik2c13b7c2005-11-14 14:14:16 -05002543 VPRINTK("unmapping %u sg elements\n", qc->n_elem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002545 /* if we padded the buffer out to 32-bit bound, and data
2546 * xfer direction is from-device, we must copy from the
2547 * pad buffer back into the supplied buffer
2548 */
2549 if (qc->pad_len && !(qc->tf.flags & ATA_TFLAG_WRITE))
2550 pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
2551
2552 if (qc->flags & ATA_QCFLAG_SG) {
Jeff Garzike1410f22005-11-14 14:06:26 -05002553 if (qc->n_elem)
2554 dma_unmap_sg(ap->host_set->dev, sg, qc->n_elem, dir);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002555 /* restore last sg */
2556 sg[qc->orig_n_elem - 1].length += qc->pad_len;
2557 if (pad_buf) {
2558 struct scatterlist *psg = &qc->pad_sgent;
2559 void *addr = kmap_atomic(psg->page, KM_IRQ0);
2560 memcpy(addr + psg->offset, pad_buf, qc->pad_len);
Mark Lorddfa15982005-12-12 23:19:28 -05002561 kunmap_atomic(addr, KM_IRQ0);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002562 }
2563 } else {
Jeff Garzike1410f22005-11-14 14:06:26 -05002564 if (sg_dma_len(&sg[0]) > 0)
2565 dma_unmap_single(ap->host_set->dev,
2566 sg_dma_address(&sg[0]), sg_dma_len(&sg[0]),
2567 dir);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002568 /* restore sg */
2569 sg->length += qc->pad_len;
2570 if (pad_buf)
2571 memcpy(qc->buf_virt + sg->length - qc->pad_len,
2572 pad_buf, qc->pad_len);
2573 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574
2575 qc->flags &= ~ATA_QCFLAG_DMAMAP;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002576 qc->__sg = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577}
2578
2579/**
2580 * ata_fill_sg - Fill PCI IDE PRD table
2581 * @qc: Metadata associated with taskfile to be transferred
2582 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04002583 * Fill PCI IDE PRD (scatter-gather) table with segments
2584 * associated with the current disk command.
2585 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586 * LOCKING:
Jeff Garzik780a87f2005-05-30 15:41:05 -04002587 * spin_lock_irqsave(host_set lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588 *
2589 */
2590static void ata_fill_sg(struct ata_queued_cmd *qc)
2591{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592 struct ata_port *ap = qc->ap;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002593 struct scatterlist *sg;
2594 unsigned int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002596 assert(qc->__sg != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597 assert(qc->n_elem > 0);
2598
2599 idx = 0;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002600 ata_for_each_sg(sg, qc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601 u32 addr, offset;
2602 u32 sg_len, len;
2603
2604 /* determine if physical DMA addr spans 64K boundary.
2605 * Note h/w doesn't support 64-bit, so we unconditionally
2606 * truncate dma_addr_t to u32.
2607 */
2608 addr = (u32) sg_dma_address(sg);
2609 sg_len = sg_dma_len(sg);
2610
2611 while (sg_len) {
2612 offset = addr & 0xffff;
2613 len = sg_len;
2614 if ((offset + sg_len) > 0x10000)
2615 len = 0x10000 - offset;
2616
2617 ap->prd[idx].addr = cpu_to_le32(addr);
2618 ap->prd[idx].flags_len = cpu_to_le32(len & 0xffff);
2619 VPRINTK("PRD[%u] = (0x%X, 0x%X)\n", idx, addr, len);
2620
2621 idx++;
2622 sg_len -= len;
2623 addr += len;
2624 }
2625 }
2626
2627 if (idx)
2628 ap->prd[idx - 1].flags_len |= cpu_to_le32(ATA_PRD_EOT);
2629}
2630/**
2631 * ata_check_atapi_dma - Check whether ATAPI DMA can be supported
2632 * @qc: Metadata associated with taskfile to check
2633 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04002634 * Allow low-level driver to filter ATA PACKET commands, returning
2635 * a status indicating whether or not it is OK to use DMA for the
2636 * supplied PACKET command.
2637 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002639 * spin_lock_irqsave(host_set lock)
2640 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641 * RETURNS: 0 when ATAPI DMA can be used
2642 * nonzero otherwise
2643 */
2644int ata_check_atapi_dma(struct ata_queued_cmd *qc)
2645{
2646 struct ata_port *ap = qc->ap;
2647 int rc = 0; /* Assume ATAPI DMA is OK by default */
2648
2649 if (ap->ops->check_atapi_dma)
2650 rc = ap->ops->check_atapi_dma(qc);
2651
2652 return rc;
2653}
2654/**
2655 * ata_qc_prep - Prepare taskfile for submission
2656 * @qc: Metadata associated with taskfile to be prepared
2657 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04002658 * Prepare ATA taskfile for submission.
2659 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660 * LOCKING:
2661 * spin_lock_irqsave(host_set lock)
2662 */
2663void ata_qc_prep(struct ata_queued_cmd *qc)
2664{
2665 if (!(qc->flags & ATA_QCFLAG_DMAMAP))
2666 return;
2667
2668 ata_fill_sg(qc);
2669}
2670
Jeff Garzik0cba6322005-05-30 19:49:12 -04002671/**
2672 * ata_sg_init_one - Associate command with memory buffer
2673 * @qc: Command to be associated
2674 * @buf: Memory buffer
2675 * @buflen: Length of memory buffer, in bytes.
2676 *
2677 * Initialize the data-related elements of queued_cmd @qc
2678 * to point to a single memory buffer, @buf of byte length @buflen.
2679 *
2680 * LOCKING:
2681 * spin_lock_irqsave(host_set lock)
2682 */
2683
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684void ata_sg_init_one(struct ata_queued_cmd *qc, void *buf, unsigned int buflen)
2685{
2686 struct scatterlist *sg;
2687
2688 qc->flags |= ATA_QCFLAG_SINGLE;
2689
2690 memset(&qc->sgent, 0, sizeof(qc->sgent));
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002691 qc->__sg = &qc->sgent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692 qc->n_elem = 1;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002693 qc->orig_n_elem = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694 qc->buf_virt = buf;
2695
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002696 sg = qc->__sg;
Jeff Garzikf0612bb2005-10-30 01:58:18 -05002697 sg_init_one(sg, buf, buflen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698}
2699
Jeff Garzik0cba6322005-05-30 19:49:12 -04002700/**
2701 * ata_sg_init - Associate command with scatter-gather table.
2702 * @qc: Command to be associated
2703 * @sg: Scatter-gather table.
2704 * @n_elem: Number of elements in s/g table.
2705 *
2706 * Initialize the data-related elements of queued_cmd @qc
2707 * to point to a scatter-gather table @sg, containing @n_elem
2708 * elements.
2709 *
2710 * LOCKING:
2711 * spin_lock_irqsave(host_set lock)
2712 */
2713
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714void ata_sg_init(struct ata_queued_cmd *qc, struct scatterlist *sg,
2715 unsigned int n_elem)
2716{
2717 qc->flags |= ATA_QCFLAG_SG;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002718 qc->__sg = sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719 qc->n_elem = n_elem;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002720 qc->orig_n_elem = n_elem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721}
2722
2723/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04002724 * ata_sg_setup_one - DMA-map the memory buffer associated with a command.
2725 * @qc: Command with memory buffer to be mapped.
2726 *
2727 * DMA-map the memory buffer associated with queued_cmd @qc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728 *
2729 * LOCKING:
2730 * spin_lock_irqsave(host_set lock)
2731 *
2732 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002733 * Zero on success, negative on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734 */
2735
2736static int ata_sg_setup_one(struct ata_queued_cmd *qc)
2737{
2738 struct ata_port *ap = qc->ap;
2739 int dir = qc->dma_dir;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002740 struct scatterlist *sg = qc->__sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741 dma_addr_t dma_address;
2742
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002743 /* we must lengthen transfers to end on a 32-bit boundary */
2744 qc->pad_len = sg->length & 3;
2745 if (qc->pad_len) {
2746 void *pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
2747 struct scatterlist *psg = &qc->pad_sgent;
2748
2749 assert(qc->dev->class == ATA_DEV_ATAPI);
2750
2751 memset(pad_buf, 0, ATA_DMA_PAD_SZ);
2752
2753 if (qc->tf.flags & ATA_TFLAG_WRITE)
2754 memcpy(pad_buf, qc->buf_virt + sg->length - qc->pad_len,
2755 qc->pad_len);
2756
2757 sg_dma_address(psg) = ap->pad_dma + (qc->tag * ATA_DMA_PAD_SZ);
2758 sg_dma_len(psg) = ATA_DMA_PAD_SZ;
2759 /* trim sg */
2760 sg->length -= qc->pad_len;
2761
2762 DPRINTK("padding done, sg->length=%u pad_len=%u\n",
2763 sg->length, qc->pad_len);
2764 }
2765
Jeff Garzike1410f22005-11-14 14:06:26 -05002766 if (!sg->length) {
2767 sg_dma_address(sg) = 0;
2768 goto skip_map;
2769 }
2770
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771 dma_address = dma_map_single(ap->host_set->dev, qc->buf_virt,
Albert Lee32529e02005-05-26 03:49:42 -04002772 sg->length, dir);
Tejun Heo537a95d2005-11-05 14:29:01 -05002773 if (dma_mapping_error(dma_address)) {
2774 /* restore sg */
2775 sg->length += qc->pad_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776 return -1;
Tejun Heo537a95d2005-11-05 14:29:01 -05002777 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778
2779 sg_dma_address(sg) = dma_address;
Jeff Garzike1410f22005-11-14 14:06:26 -05002780skip_map:
Albert Lee32529e02005-05-26 03:49:42 -04002781 sg_dma_len(sg) = sg->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782
2783 DPRINTK("mapped buffer of %d bytes for %s\n", sg_dma_len(sg),
2784 qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
2785
2786 return 0;
2787}
2788
2789/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04002790 * ata_sg_setup - DMA-map the scatter-gather table associated with a command.
2791 * @qc: Command with scatter-gather table to be mapped.
2792 *
2793 * DMA-map the scatter-gather table associated with queued_cmd @qc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794 *
2795 * LOCKING:
2796 * spin_lock_irqsave(host_set lock)
2797 *
2798 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002799 * Zero on success, negative on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002800 *
2801 */
2802
2803static int ata_sg_setup(struct ata_queued_cmd *qc)
2804{
2805 struct ata_port *ap = qc->ap;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002806 struct scatterlist *sg = qc->__sg;
2807 struct scatterlist *lsg = &sg[qc->n_elem - 1];
Jeff Garzike1410f22005-11-14 14:06:26 -05002808 int n_elem, pre_n_elem, dir, trim_sg = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809
2810 VPRINTK("ENTER, ata%u\n", ap->id);
2811 assert(qc->flags & ATA_QCFLAG_SG);
2812
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002813 /* we must lengthen transfers to end on a 32-bit boundary */
2814 qc->pad_len = lsg->length & 3;
2815 if (qc->pad_len) {
2816 void *pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
2817 struct scatterlist *psg = &qc->pad_sgent;
2818 unsigned int offset;
2819
2820 assert(qc->dev->class == ATA_DEV_ATAPI);
2821
2822 memset(pad_buf, 0, ATA_DMA_PAD_SZ);
2823
2824 /*
2825 * psg->page/offset are used to copy to-be-written
2826 * data in this function or read data in ata_sg_clean.
2827 */
2828 offset = lsg->offset + lsg->length - qc->pad_len;
2829 psg->page = nth_page(lsg->page, offset >> PAGE_SHIFT);
2830 psg->offset = offset_in_page(offset);
2831
2832 if (qc->tf.flags & ATA_TFLAG_WRITE) {
2833 void *addr = kmap_atomic(psg->page, KM_IRQ0);
2834 memcpy(pad_buf, addr + psg->offset, qc->pad_len);
Mark Lorddfa15982005-12-12 23:19:28 -05002835 kunmap_atomic(addr, KM_IRQ0);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002836 }
2837
2838 sg_dma_address(psg) = ap->pad_dma + (qc->tag * ATA_DMA_PAD_SZ);
2839 sg_dma_len(psg) = ATA_DMA_PAD_SZ;
2840 /* trim last sg */
2841 lsg->length -= qc->pad_len;
Jeff Garzike1410f22005-11-14 14:06:26 -05002842 if (lsg->length == 0)
2843 trim_sg = 1;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002844
2845 DPRINTK("padding done, sg[%d].length=%u pad_len=%u\n",
2846 qc->n_elem - 1, lsg->length, qc->pad_len);
2847 }
2848
Jeff Garzike1410f22005-11-14 14:06:26 -05002849 pre_n_elem = qc->n_elem;
2850 if (trim_sg && pre_n_elem)
2851 pre_n_elem--;
2852
2853 if (!pre_n_elem) {
2854 n_elem = 0;
2855 goto skip_map;
2856 }
2857
Linus Torvalds1da177e2005-04-16 15:20:36 -07002858 dir = qc->dma_dir;
Jeff Garzike1410f22005-11-14 14:06:26 -05002859 n_elem = dma_map_sg(ap->host_set->dev, sg, pre_n_elem, dir);
Tejun Heo537a95d2005-11-05 14:29:01 -05002860 if (n_elem < 1) {
2861 /* restore last sg */
2862 lsg->length += qc->pad_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863 return -1;
Tejun Heo537a95d2005-11-05 14:29:01 -05002864 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002865
2866 DPRINTK("%d sg elements mapped\n", n_elem);
2867
Jeff Garzike1410f22005-11-14 14:06:26 -05002868skip_map:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869 qc->n_elem = n_elem;
2870
2871 return 0;
2872}
2873
2874/**
Tejun Heo40e8c822005-08-22 17:12:45 +09002875 * ata_poll_qc_complete - turn irq back on and finish qc
2876 * @qc: Command to complete
Randy Dunlap8e8b77d2005-11-01 21:29:27 -08002877 * @err_mask: ATA status register content
Tejun Heo40e8c822005-08-22 17:12:45 +09002878 *
2879 * LOCKING:
2880 * None. (grabs host lock)
2881 */
2882
Albert Leea22e2eb2005-12-05 15:38:02 +08002883void ata_poll_qc_complete(struct ata_queued_cmd *qc)
Tejun Heo40e8c822005-08-22 17:12:45 +09002884{
2885 struct ata_port *ap = qc->ap;
Jeff Garzikb8f61532005-08-25 22:01:20 -04002886 unsigned long flags;
Tejun Heo40e8c822005-08-22 17:12:45 +09002887
Jeff Garzikb8f61532005-08-25 22:01:20 -04002888 spin_lock_irqsave(&ap->host_set->lock, flags);
Tejun Heo40e8c822005-08-22 17:12:45 +09002889 ap->flags &= ~ATA_FLAG_NOINTR;
2890 ata_irq_on(ap);
Albert Leea22e2eb2005-12-05 15:38:02 +08002891 ata_qc_complete(qc);
Jeff Garzikb8f61532005-08-25 22:01:20 -04002892 spin_unlock_irqrestore(&ap->host_set->lock, flags);
Tejun Heo40e8c822005-08-22 17:12:45 +09002893}
2894
2895/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002896 * ata_pio_poll -
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04002897 * @ap: the target ata_port
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898 *
2899 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002900 * None. (executing in kernel thread context)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002901 *
2902 * RETURNS:
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04002903 * timeout value to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07002904 */
2905
2906static unsigned long ata_pio_poll(struct ata_port *ap)
2907{
Albert Leec14b8332005-12-05 15:36:08 +08002908 struct ata_queued_cmd *qc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002909 u8 status;
Albert Lee14be71f2005-09-27 17:36:35 +08002910 unsigned int poll_state = HSM_ST_UNKNOWN;
2911 unsigned int reg_state = HSM_ST_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912
Albert Leec14b8332005-12-05 15:36:08 +08002913 qc = ata_qc_from_tag(ap, ap->active_tag);
2914 assert(qc != NULL);
2915
Albert Lee14be71f2005-09-27 17:36:35 +08002916 switch (ap->hsm_task_state) {
2917 case HSM_ST:
2918 case HSM_ST_POLL:
2919 poll_state = HSM_ST_POLL;
2920 reg_state = HSM_ST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002921 break;
Albert Lee14be71f2005-09-27 17:36:35 +08002922 case HSM_ST_LAST:
2923 case HSM_ST_LAST_POLL:
2924 poll_state = HSM_ST_LAST_POLL;
2925 reg_state = HSM_ST_LAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926 break;
2927 default:
2928 BUG();
2929 break;
2930 }
2931
2932 status = ata_chk_status(ap);
2933 if (status & ATA_BUSY) {
2934 if (time_after(jiffies, ap->pio_task_timeout)) {
Tejun Heo11a56d22006-01-23 13:09:36 +09002935 qc->err_mask |= AC_ERR_TIMEOUT;
Albert Lee7c398332005-11-09 13:03:30 +08002936 ap->hsm_task_state = HSM_ST_TMOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002937 return 0;
2938 }
Albert Lee14be71f2005-09-27 17:36:35 +08002939 ap->hsm_task_state = poll_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002940 return ATA_SHORT_PAUSE;
2941 }
2942
Albert Lee14be71f2005-09-27 17:36:35 +08002943 ap->hsm_task_state = reg_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944 return 0;
2945}
2946
2947/**
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04002948 * ata_pio_complete - check if drive is busy or idle
2949 * @ap: the target ata_port
Linus Torvalds1da177e2005-04-16 15:20:36 -07002950 *
2951 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002952 * None. (executing in kernel thread context)
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04002953 *
2954 * RETURNS:
2955 * Non-zero if qc completed, zero otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002956 */
2957
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04002958static int ata_pio_complete (struct ata_port *ap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959{
2960 struct ata_queued_cmd *qc;
2961 u8 drv_stat;
2962
2963 /*
Alan Cox31433ea2005-08-26 15:56:47 +01002964 * This is purely heuristic. This is a fast path. Sometimes when
2965 * we enter, BSY will be cleared in a chk-status or two. If not,
2966 * the drive is probably seeking or something. Snooze for a couple
2967 * msecs, then chk-status again. If still busy, fall back to
Albert Lee14be71f2005-09-27 17:36:35 +08002968 * HSM_ST_POLL state.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002969 */
Albert Leefe79e682005-12-06 11:34:59 +08002970 drv_stat = ata_busy_wait(ap, ATA_BUSY, 10);
2971 if (drv_stat & ATA_BUSY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972 msleep(2);
Albert Leefe79e682005-12-06 11:34:59 +08002973 drv_stat = ata_busy_wait(ap, ATA_BUSY, 10);
2974 if (drv_stat & ATA_BUSY) {
Albert Lee14be71f2005-09-27 17:36:35 +08002975 ap->hsm_task_state = HSM_ST_LAST_POLL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976 ap->pio_task_timeout = jiffies + ATA_TMOUT_PIO;
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04002977 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978 }
2979 }
2980
Albert Leec14b8332005-12-05 15:36:08 +08002981 qc = ata_qc_from_tag(ap, ap->active_tag);
2982 assert(qc != NULL);
2983
Linus Torvalds1da177e2005-04-16 15:20:36 -07002984 drv_stat = ata_wait_idle(ap);
2985 if (!ata_ok(drv_stat)) {
Albert Lee1c848982005-12-05 15:40:15 +08002986 qc->err_mask |= __ac_err_mask(drv_stat);
Albert Lee14be71f2005-09-27 17:36:35 +08002987 ap->hsm_task_state = HSM_ST_ERR;
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04002988 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989 }
2990
Albert Lee14be71f2005-09-27 17:36:35 +08002991 ap->hsm_task_state = HSM_ST_IDLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002992
Albert Leea22e2eb2005-12-05 15:38:02 +08002993 assert(qc->err_mask == 0);
2994 ata_poll_qc_complete(qc);
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04002995
2996 /* another command may start at this point */
2997
2998 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999}
3000
Edward Falk0baab862005-06-02 18:17:13 -04003001
3002/**
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04003003 * swap_buf_le16 - swap halves of 16-words in place
Edward Falk0baab862005-06-02 18:17:13 -04003004 * @buf: Buffer to swap
3005 * @buf_words: Number of 16-bit words in buffer.
3006 *
3007 * Swap halves of 16-bit words if needed to convert from
3008 * little-endian byte order to native cpu byte order, or
3009 * vice-versa.
3010 *
3011 * LOCKING:
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04003012 * Inherited from caller.
Edward Falk0baab862005-06-02 18:17:13 -04003013 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003014void swap_buf_le16(u16 *buf, unsigned int buf_words)
3015{
3016#ifdef __BIG_ENDIAN
3017 unsigned int i;
3018
3019 for (i = 0; i < buf_words; i++)
3020 buf[i] = le16_to_cpu(buf[i]);
3021#endif /* __BIG_ENDIAN */
3022}
3023
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003024/**
3025 * ata_mmio_data_xfer - Transfer data by MMIO
3026 * @ap: port to read/write
3027 * @buf: data buffer
3028 * @buflen: buffer length
Jeff Garzik344baba2005-09-07 01:15:17 -04003029 * @write_data: read/write
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003030 *
3031 * Transfer data from/to the device data register by MMIO.
3032 *
3033 * LOCKING:
3034 * Inherited from caller.
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003035 */
3036
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037static void ata_mmio_data_xfer(struct ata_port *ap, unsigned char *buf,
3038 unsigned int buflen, int write_data)
3039{
3040 unsigned int i;
3041 unsigned int words = buflen >> 1;
3042 u16 *buf16 = (u16 *) buf;
3043 void __iomem *mmio = (void __iomem *)ap->ioaddr.data_addr;
3044
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003045 /* Transfer multiple of 2 bytes */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003046 if (write_data) {
3047 for (i = 0; i < words; i++)
3048 writew(le16_to_cpu(buf16[i]), mmio);
3049 } else {
3050 for (i = 0; i < words; i++)
3051 buf16[i] = cpu_to_le16(readw(mmio));
3052 }
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003053
3054 /* Transfer trailing 1 byte, if any. */
3055 if (unlikely(buflen & 0x01)) {
3056 u16 align_buf[1] = { 0 };
3057 unsigned char *trailing_buf = buf + buflen - 1;
3058
3059 if (write_data) {
3060 memcpy(align_buf, trailing_buf, 1);
3061 writew(le16_to_cpu(align_buf[0]), mmio);
3062 } else {
3063 align_buf[0] = cpu_to_le16(readw(mmio));
3064 memcpy(trailing_buf, align_buf, 1);
3065 }
3066 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003067}
3068
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003069/**
3070 * ata_pio_data_xfer - Transfer data by PIO
3071 * @ap: port to read/write
3072 * @buf: data buffer
3073 * @buflen: buffer length
Jeff Garzik344baba2005-09-07 01:15:17 -04003074 * @write_data: read/write
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003075 *
3076 * Transfer data from/to the device data register by PIO.
3077 *
3078 * LOCKING:
3079 * Inherited from caller.
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003080 */
3081
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082static void ata_pio_data_xfer(struct ata_port *ap, unsigned char *buf,
3083 unsigned int buflen, int write_data)
3084{
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003085 unsigned int words = buflen >> 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003086
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003087 /* Transfer multiple of 2 bytes */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088 if (write_data)
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003089 outsw(ap->ioaddr.data_addr, buf, words);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003090 else
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003091 insw(ap->ioaddr.data_addr, buf, words);
3092
3093 /* Transfer trailing 1 byte, if any. */
3094 if (unlikely(buflen & 0x01)) {
3095 u16 align_buf[1] = { 0 };
3096 unsigned char *trailing_buf = buf + buflen - 1;
3097
3098 if (write_data) {
3099 memcpy(align_buf, trailing_buf, 1);
3100 outw(le16_to_cpu(align_buf[0]), ap->ioaddr.data_addr);
3101 } else {
3102 align_buf[0] = cpu_to_le16(inw(ap->ioaddr.data_addr));
3103 memcpy(trailing_buf, align_buf, 1);
3104 }
3105 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106}
3107
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003108/**
3109 * ata_data_xfer - Transfer data from/to the data register.
3110 * @ap: port to read/write
3111 * @buf: data buffer
3112 * @buflen: buffer length
3113 * @do_write: read/write
3114 *
3115 * Transfer data from/to the device data register.
3116 *
3117 * LOCKING:
3118 * Inherited from caller.
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003119 */
3120
Linus Torvalds1da177e2005-04-16 15:20:36 -07003121static void ata_data_xfer(struct ata_port *ap, unsigned char *buf,
3122 unsigned int buflen, int do_write)
3123{
Alan Coxa1bd9e62006-01-17 20:53:50 +00003124 /* Make the crap hardware pay the costs not the good stuff */
3125 if (unlikely(ap->flags & ATA_FLAG_IRQ_MASK)) {
3126 unsigned long flags;
3127 local_irq_save(flags);
3128 if (ap->flags & ATA_FLAG_MMIO)
3129 ata_mmio_data_xfer(ap, buf, buflen, do_write);
3130 else
3131 ata_pio_data_xfer(ap, buf, buflen, do_write);
3132 local_irq_restore(flags);
3133 } else {
3134 if (ap->flags & ATA_FLAG_MMIO)
3135 ata_mmio_data_xfer(ap, buf, buflen, do_write);
3136 else
3137 ata_pio_data_xfer(ap, buf, buflen, do_write);
3138 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003139}
3140
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003141/**
3142 * ata_pio_sector - Transfer ATA_SECT_SIZE (512 bytes) of data.
3143 * @qc: Command on going
3144 *
3145 * Transfer ATA_SECT_SIZE of data from/to the ATA device.
3146 *
3147 * LOCKING:
3148 * Inherited from caller.
3149 */
3150
Linus Torvalds1da177e2005-04-16 15:20:36 -07003151static void ata_pio_sector(struct ata_queued_cmd *qc)
3152{
3153 int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003154 struct scatterlist *sg = qc->__sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003155 struct ata_port *ap = qc->ap;
3156 struct page *page;
3157 unsigned int offset;
3158 unsigned char *buf;
3159
3160 if (qc->cursect == (qc->nsect - 1))
Albert Lee14be71f2005-09-27 17:36:35 +08003161 ap->hsm_task_state = HSM_ST_LAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003162
3163 page = sg[qc->cursg].page;
3164 offset = sg[qc->cursg].offset + qc->cursg_ofs * ATA_SECT_SIZE;
3165
3166 /* get the current page and offset */
3167 page = nth_page(page, (offset >> PAGE_SHIFT));
3168 offset %= PAGE_SIZE;
3169
3170 buf = kmap(page) + offset;
3171
3172 qc->cursect++;
3173 qc->cursg_ofs++;
3174
Albert Lee32529e02005-05-26 03:49:42 -04003175 if ((qc->cursg_ofs * ATA_SECT_SIZE) == (&sg[qc->cursg])->length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003176 qc->cursg++;
3177 qc->cursg_ofs = 0;
3178 }
3179
3180 DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
3181
3182 /* do the actual data transfer */
3183 do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
3184 ata_data_xfer(ap, buf, ATA_SECT_SIZE, do_write);
3185
3186 kunmap(page);
3187}
3188
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003189/**
3190 * __atapi_pio_bytes - Transfer data from/to the ATAPI device.
3191 * @qc: Command on going
3192 * @bytes: number of bytes
3193 *
3194 * Transfer Transfer data from/to the ATAPI device.
3195 *
3196 * LOCKING:
3197 * Inherited from caller.
3198 *
3199 */
3200
Linus Torvalds1da177e2005-04-16 15:20:36 -07003201static void __atapi_pio_bytes(struct ata_queued_cmd *qc, unsigned int bytes)
3202{
3203 int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003204 struct scatterlist *sg = qc->__sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003205 struct ata_port *ap = qc->ap;
3206 struct page *page;
3207 unsigned char *buf;
3208 unsigned int offset, count;
3209
Albert Lee563a6e12005-08-12 14:17:50 +08003210 if (qc->curbytes + bytes >= qc->nbytes)
Albert Lee14be71f2005-09-27 17:36:35 +08003211 ap->hsm_task_state = HSM_ST_LAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003212
3213next_sg:
Albert Lee563a6e12005-08-12 14:17:50 +08003214 if (unlikely(qc->cursg >= qc->n_elem)) {
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003215 /*
Albert Lee563a6e12005-08-12 14:17:50 +08003216 * The end of qc->sg is reached and the device expects
3217 * more data to transfer. In order not to overrun qc->sg
3218 * and fulfill length specified in the byte count register,
3219 * - for read case, discard trailing data from the device
3220 * - for write case, padding zero data to the device
3221 */
3222 u16 pad_buf[1] = { 0 };
3223 unsigned int words = bytes >> 1;
3224 unsigned int i;
3225
3226 if (words) /* warning if bytes > 1 */
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003227 printk(KERN_WARNING "ata%u: %u bytes trailing data\n",
Albert Lee563a6e12005-08-12 14:17:50 +08003228 ap->id, bytes);
3229
3230 for (i = 0; i < words; i++)
3231 ata_data_xfer(ap, (unsigned char*)pad_buf, 2, do_write);
3232
Albert Lee14be71f2005-09-27 17:36:35 +08003233 ap->hsm_task_state = HSM_ST_LAST;
Albert Lee563a6e12005-08-12 14:17:50 +08003234 return;
3235 }
3236
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003237 sg = &qc->__sg[qc->cursg];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003238
Linus Torvalds1da177e2005-04-16 15:20:36 -07003239 page = sg->page;
3240 offset = sg->offset + qc->cursg_ofs;
3241
3242 /* get the current page and offset */
3243 page = nth_page(page, (offset >> PAGE_SHIFT));
3244 offset %= PAGE_SIZE;
3245
Albert Lee6952df02005-06-06 15:56:03 +08003246 /* don't overrun current sg */
Albert Lee32529e02005-05-26 03:49:42 -04003247 count = min(sg->length - qc->cursg_ofs, bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003248
3249 /* don't cross page boundaries */
3250 count = min(count, (unsigned int)PAGE_SIZE - offset);
3251
3252 buf = kmap(page) + offset;
3253
3254 bytes -= count;
3255 qc->curbytes += count;
3256 qc->cursg_ofs += count;
3257
Albert Lee32529e02005-05-26 03:49:42 -04003258 if (qc->cursg_ofs == sg->length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003259 qc->cursg++;
3260 qc->cursg_ofs = 0;
3261 }
3262
3263 DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
3264
3265 /* do the actual data transfer */
3266 ata_data_xfer(ap, buf, count, do_write);
3267
3268 kunmap(page);
3269
Albert Lee563a6e12005-08-12 14:17:50 +08003270 if (bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003271 goto next_sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003272}
3273
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003274/**
3275 * atapi_pio_bytes - Transfer data from/to the ATAPI device.
3276 * @qc: Command on going
3277 *
3278 * Transfer Transfer data from/to the ATAPI device.
3279 *
3280 * LOCKING:
3281 * Inherited from caller.
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003282 */
3283
Linus Torvalds1da177e2005-04-16 15:20:36 -07003284static void atapi_pio_bytes(struct ata_queued_cmd *qc)
3285{
3286 struct ata_port *ap = qc->ap;
3287 struct ata_device *dev = qc->dev;
3288 unsigned int ireason, bc_lo, bc_hi, bytes;
3289 int i_write, do_write = (qc->tf.flags & ATA_TFLAG_WRITE) ? 1 : 0;
3290
3291 ap->ops->tf_read(ap, &qc->tf);
3292 ireason = qc->tf.nsect;
3293 bc_lo = qc->tf.lbam;
3294 bc_hi = qc->tf.lbah;
3295 bytes = (bc_hi << 8) | bc_lo;
3296
3297 /* shall be cleared to zero, indicating xfer of data */
3298 if (ireason & (1 << 0))
3299 goto err_out;
3300
3301 /* make sure transfer direction matches expected */
3302 i_write = ((ireason & (1 << 1)) == 0) ? 1 : 0;
3303 if (do_write != i_write)
3304 goto err_out;
3305
3306 __atapi_pio_bytes(qc, bytes);
3307
3308 return;
3309
3310err_out:
3311 printk(KERN_INFO "ata%u: dev %u: ATAPI check failed\n",
3312 ap->id, dev->devno);
Tejun Heo11a56d22006-01-23 13:09:36 +09003313 qc->err_mask |= AC_ERR_HSM;
Albert Lee14be71f2005-09-27 17:36:35 +08003314 ap->hsm_task_state = HSM_ST_ERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003315}
3316
3317/**
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04003318 * ata_pio_block - start PIO on a block
3319 * @ap: the target ata_port
Linus Torvalds1da177e2005-04-16 15:20:36 -07003320 *
3321 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003322 * None. (executing in kernel thread context)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003323 */
3324
3325static void ata_pio_block(struct ata_port *ap)
3326{
3327 struct ata_queued_cmd *qc;
3328 u8 status;
3329
3330 /*
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04003331 * This is purely heuristic. This is a fast path.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003332 * Sometimes when we enter, BSY will be cleared in
3333 * a chk-status or two. If not, the drive is probably seeking
3334 * or something. Snooze for a couple msecs, then
3335 * chk-status again. If still busy, fall back to
Albert Lee14be71f2005-09-27 17:36:35 +08003336 * HSM_ST_POLL state.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003337 */
3338 status = ata_busy_wait(ap, ATA_BUSY, 5);
3339 if (status & ATA_BUSY) {
3340 msleep(2);
3341 status = ata_busy_wait(ap, ATA_BUSY, 10);
3342 if (status & ATA_BUSY) {
Albert Lee14be71f2005-09-27 17:36:35 +08003343 ap->hsm_task_state = HSM_ST_POLL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003344 ap->pio_task_timeout = jiffies + ATA_TMOUT_PIO;
3345 return;
3346 }
3347 }
3348
3349 qc = ata_qc_from_tag(ap, ap->active_tag);
3350 assert(qc != NULL);
3351
Albert Leefe79e682005-12-06 11:34:59 +08003352 /* check error */
3353 if (status & (ATA_ERR | ATA_DF)) {
3354 qc->err_mask |= AC_ERR_DEV;
3355 ap->hsm_task_state = HSM_ST_ERR;
3356 return;
3357 }
3358
3359 /* transfer data if any */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003360 if (is_atapi_taskfile(&qc->tf)) {
Albert Leefe79e682005-12-06 11:34:59 +08003361 /* DRQ=0 means no more data to transfer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003362 if ((status & ATA_DRQ) == 0) {
Albert Lee14be71f2005-09-27 17:36:35 +08003363 ap->hsm_task_state = HSM_ST_LAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003364 return;
3365 }
3366
3367 atapi_pio_bytes(qc);
3368 } else {
3369 /* handle BSY=0, DRQ=0 as error */
3370 if ((status & ATA_DRQ) == 0) {
Tejun Heo11a56d22006-01-23 13:09:36 +09003371 qc->err_mask |= AC_ERR_HSM;
Albert Lee14be71f2005-09-27 17:36:35 +08003372 ap->hsm_task_state = HSM_ST_ERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003373 return;
3374 }
3375
3376 ata_pio_sector(qc);
3377 }
3378}
3379
3380static void ata_pio_error(struct ata_port *ap)
3381{
3382 struct ata_queued_cmd *qc;
Jeff Garzika7dac442005-10-30 04:44:42 -05003383
3384 printk(KERN_WARNING "ata%u: PIO error\n", ap->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003385
3386 qc = ata_qc_from_tag(ap, ap->active_tag);
3387 assert(qc != NULL);
3388
Albert Lee1c848982005-12-05 15:40:15 +08003389 /* make sure qc->err_mask is available to
3390 * know what's wrong and recover
3391 */
3392 assert(qc->err_mask);
3393
Albert Lee14be71f2005-09-27 17:36:35 +08003394 ap->hsm_task_state = HSM_ST_IDLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003395
Albert Leea22e2eb2005-12-05 15:38:02 +08003396 ata_poll_qc_complete(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003397}
3398
3399static void ata_pio_task(void *_data)
3400{
3401 struct ata_port *ap = _data;
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003402 unsigned long timeout;
3403 int qc_completed;
3404
3405fsm_start:
3406 timeout = 0;
3407 qc_completed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003408
Albert Lee14be71f2005-09-27 17:36:35 +08003409 switch (ap->hsm_task_state) {
3410 case HSM_ST_IDLE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003411 return;
3412
Albert Lee14be71f2005-09-27 17:36:35 +08003413 case HSM_ST:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003414 ata_pio_block(ap);
3415 break;
3416
Albert Lee14be71f2005-09-27 17:36:35 +08003417 case HSM_ST_LAST:
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003418 qc_completed = ata_pio_complete(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003419 break;
3420
Albert Lee14be71f2005-09-27 17:36:35 +08003421 case HSM_ST_POLL:
3422 case HSM_ST_LAST_POLL:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003423 timeout = ata_pio_poll(ap);
3424 break;
3425
Albert Lee14be71f2005-09-27 17:36:35 +08003426 case HSM_ST_TMOUT:
3427 case HSM_ST_ERR:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003428 ata_pio_error(ap);
3429 return;
3430 }
3431
3432 if (timeout)
Tejun Heo95064372006-01-23 13:09:37 +09003433 ata_queue_delayed_pio_task(ap, timeout);
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003434 else if (!qc_completed)
3435 goto fsm_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003436}
3437
Linus Torvalds1da177e2005-04-16 15:20:36 -07003438/**
3439 * ata_qc_timeout - Handle timeout of queued command
3440 * @qc: Command that timed out
3441 *
3442 * Some part of the kernel (currently, only the SCSI layer)
3443 * has noticed that the active command on port @ap has not
3444 * completed after a specified length of time. Handle this
3445 * condition by disabling DMA (if necessary) and completing
3446 * transactions, with error if necessary.
3447 *
3448 * This also handles the case of the "lost interrupt", where
3449 * for some reason (possibly hardware bug, possibly driver bug)
3450 * an interrupt was not delivered to the driver, even though the
3451 * transaction completed successfully.
3452 *
3453 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003454 * Inherited from SCSI layer (none, can sleep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003455 */
3456
3457static void ata_qc_timeout(struct ata_queued_cmd *qc)
3458{
3459 struct ata_port *ap = qc->ap;
Jeff Garzikb8f61532005-08-25 22:01:20 -04003460 struct ata_host_set *host_set = ap->host_set;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003461 u8 host_stat = 0, drv_stat;
Jeff Garzikb8f61532005-08-25 22:01:20 -04003462 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003463
3464 DPRINTK("ENTER\n");
3465
Jeff Garzikb8f61532005-08-25 22:01:20 -04003466 spin_lock_irqsave(&host_set->lock, flags);
3467
Linus Torvalds1da177e2005-04-16 15:20:36 -07003468 switch (qc->tf.protocol) {
3469
3470 case ATA_PROT_DMA:
3471 case ATA_PROT_ATAPI_DMA:
3472 host_stat = ap->ops->bmdma_status(ap);
3473
3474 /* before we do anything else, clear DMA-Start bit */
Alan Coxb73fc892005-08-26 16:03:19 +01003475 ap->ops->bmdma_stop(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003476
3477 /* fall through */
3478
3479 default:
3480 ata_altstatus(ap);
3481 drv_stat = ata_chk_status(ap);
3482
3483 /* ack bmdma irq events */
3484 ap->ops->irq_clear(ap);
3485
3486 printk(KERN_ERR "ata%u: command 0x%x timeout, stat 0x%x host_stat 0x%x\n",
3487 ap->id, qc->tf.command, drv_stat, host_stat);
3488
3489 /* complete taskfile transaction */
Albert Leea22e2eb2005-12-05 15:38:02 +08003490 qc->err_mask |= ac_err_mask(drv_stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003491 break;
3492 }
Jeff Garzikb8f61532005-08-25 22:01:20 -04003493
3494 spin_unlock_irqrestore(&host_set->lock, flags);
3495
Tejun Heoa72ec4c2006-01-23 13:09:37 +09003496 ata_eh_qc_complete(qc);
3497
Linus Torvalds1da177e2005-04-16 15:20:36 -07003498 DPRINTK("EXIT\n");
3499}
3500
3501/**
3502 * ata_eng_timeout - Handle timeout of queued command
3503 * @ap: Port on which timed-out command is active
3504 *
3505 * Some part of the kernel (currently, only the SCSI layer)
3506 * has noticed that the active command on port @ap has not
3507 * completed after a specified length of time. Handle this
3508 * condition by disabling DMA (if necessary) and completing
3509 * transactions, with error if necessary.
3510 *
3511 * This also handles the case of the "lost interrupt", where
3512 * for some reason (possibly hardware bug, possibly driver bug)
3513 * an interrupt was not delivered to the driver, even though the
3514 * transaction completed successfully.
3515 *
3516 * LOCKING:
3517 * Inherited from SCSI layer (none, can sleep)
3518 */
3519
3520void ata_eng_timeout(struct ata_port *ap)
3521{
3522 struct ata_queued_cmd *qc;
3523
3524 DPRINTK("ENTER\n");
3525
3526 qc = ata_qc_from_tag(ap, ap->active_tag);
Jeff Garzike12669e2005-10-05 18:39:23 -04003527 if (qc)
3528 ata_qc_timeout(qc);
3529 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003530 printk(KERN_ERR "ata%u: BUG: timeout without command\n",
3531 ap->id);
3532 goto out;
3533 }
3534
Linus Torvalds1da177e2005-04-16 15:20:36 -07003535out:
3536 DPRINTK("EXIT\n");
3537}
3538
3539/**
3540 * ata_qc_new - Request an available ATA command, for queueing
3541 * @ap: Port associated with device @dev
3542 * @dev: Device from whom we request an available command structure
3543 *
3544 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003545 * None.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003546 */
3547
3548static struct ata_queued_cmd *ata_qc_new(struct ata_port *ap)
3549{
3550 struct ata_queued_cmd *qc = NULL;
3551 unsigned int i;
3552
3553 for (i = 0; i < ATA_MAX_QUEUE; i++)
3554 if (!test_and_set_bit(i, &ap->qactive)) {
3555 qc = ata_qc_from_tag(ap, i);
3556 break;
3557 }
3558
3559 if (qc)
3560 qc->tag = i;
3561
3562 return qc;
3563}
3564
3565/**
3566 * ata_qc_new_init - Request an available ATA command, and initialize it
3567 * @ap: Port associated with device @dev
3568 * @dev: Device from whom we request an available command structure
3569 *
3570 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003571 * None.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003572 */
3573
3574struct ata_queued_cmd *ata_qc_new_init(struct ata_port *ap,
3575 struct ata_device *dev)
3576{
3577 struct ata_queued_cmd *qc;
3578
3579 qc = ata_qc_new(ap);
3580 if (qc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003581 qc->scsicmd = NULL;
3582 qc->ap = ap;
3583 qc->dev = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003584
Jeff Garzik2c13b7c2005-11-14 14:14:16 -05003585 ata_qc_reinit(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003586 }
3587
3588 return qc;
3589}
3590
Linus Torvalds1da177e2005-04-16 15:20:36 -07003591/**
3592 * ata_qc_free - free unused ata_queued_cmd
3593 * @qc: Command to complete
3594 *
3595 * Designed to free unused ata_queued_cmd object
3596 * in case something prevents using it.
3597 *
3598 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003599 * spin_lock_irqsave(host_set lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003600 */
3601void ata_qc_free(struct ata_queued_cmd *qc)
3602{
Tejun Heo4ba946e2006-01-23 13:09:36 +09003603 struct ata_port *ap = qc->ap;
3604 unsigned int tag;
3605
Linus Torvalds1da177e2005-04-16 15:20:36 -07003606 assert(qc != NULL); /* ata_qc_from_tag _might_ return NULL */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003607
Tejun Heo4ba946e2006-01-23 13:09:36 +09003608 qc->flags = 0;
3609 tag = qc->tag;
3610 if (likely(ata_tag_valid(tag))) {
3611 if (tag == ap->active_tag)
3612 ap->active_tag = ATA_TAG_POISON;
3613 qc->tag = ATA_TAG_POISON;
3614 clear_bit(tag, &ap->qactive);
3615 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003616}
3617
3618/**
3619 * ata_qc_complete - Complete an active ATA command
3620 * @qc: Command to complete
Randy Dunlap8e8b77d2005-11-01 21:29:27 -08003621 * @err_mask: ATA Status register contents
Jeff Garzik0cba6322005-05-30 19:49:12 -04003622 *
3623 * Indicate to the mid and upper layers that an ATA
3624 * command has completed, with either an ok or not-ok status.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003625 *
3626 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003627 * spin_lock_irqsave(host_set lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003628 */
3629
Albert Leea22e2eb2005-12-05 15:38:02 +08003630void ata_qc_complete(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003631{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003632 assert(qc != NULL); /* ata_qc_from_tag _might_ return NULL */
3633 assert(qc->flags & ATA_QCFLAG_ACTIVE);
3634
3635 if (likely(qc->flags & ATA_QCFLAG_DMAMAP))
3636 ata_sg_clean(qc);
3637
Albert Lee3f3791d2005-08-16 14:25:38 +08003638 /* atapi: mark qc as inactive to prevent the interrupt handler
3639 * from completing the command twice later, before the error handler
3640 * is called. (when rc != 0 and atapi request sense is needed)
3641 */
3642 qc->flags &= ~ATA_QCFLAG_ACTIVE;
3643
Linus Torvalds1da177e2005-04-16 15:20:36 -07003644 /* call completion callback */
Tejun Heo77853bf2006-01-23 13:09:36 +09003645 qc->complete_fn(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003646}
3647
3648static inline int ata_should_dma_map(struct ata_queued_cmd *qc)
3649{
3650 struct ata_port *ap = qc->ap;
3651
3652 switch (qc->tf.protocol) {
3653 case ATA_PROT_DMA:
3654 case ATA_PROT_ATAPI_DMA:
3655 return 1;
3656
3657 case ATA_PROT_ATAPI:
3658 case ATA_PROT_PIO:
3659 case ATA_PROT_PIO_MULT:
3660 if (ap->flags & ATA_FLAG_PIO_DMA)
3661 return 1;
3662
3663 /* fall through */
3664
3665 default:
3666 return 0;
3667 }
3668
3669 /* never reached */
3670}
3671
3672/**
3673 * ata_qc_issue - issue taskfile to device
3674 * @qc: command to issue to device
3675 *
3676 * Prepare an ATA command to submission to device.
3677 * This includes mapping the data into a DMA-able
3678 * area, filling in the S/G table, and finally
3679 * writing the taskfile to hardware, starting the command.
3680 *
3681 * LOCKING:
3682 * spin_lock_irqsave(host_set lock)
3683 *
3684 * RETURNS:
Tejun Heo9a3d9eb2006-01-23 13:09:36 +09003685 * Zero on success, AC_ERR_* mask on failure
Linus Torvalds1da177e2005-04-16 15:20:36 -07003686 */
3687
Tejun Heo9a3d9eb2006-01-23 13:09:36 +09003688unsigned int ata_qc_issue(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003689{
3690 struct ata_port *ap = qc->ap;
3691
3692 if (ata_should_dma_map(qc)) {
3693 if (qc->flags & ATA_QCFLAG_SG) {
3694 if (ata_sg_setup(qc))
Tejun Heo8e436af2006-01-23 13:09:36 +09003695 goto sg_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003696 } else if (qc->flags & ATA_QCFLAG_SINGLE) {
3697 if (ata_sg_setup_one(qc))
Tejun Heo8e436af2006-01-23 13:09:36 +09003698 goto sg_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003699 }
3700 } else {
3701 qc->flags &= ~ATA_QCFLAG_DMAMAP;
3702 }
3703
3704 ap->ops->qc_prep(qc);
3705
3706 qc->ap->active_tag = qc->tag;
3707 qc->flags |= ATA_QCFLAG_ACTIVE;
3708
3709 return ap->ops->qc_issue(qc);
3710
Tejun Heo8e436af2006-01-23 13:09:36 +09003711sg_err:
3712 qc->flags &= ~ATA_QCFLAG_DMAMAP;
Tejun Heo9a3d9eb2006-01-23 13:09:36 +09003713 return AC_ERR_SYSTEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003714}
3715
Edward Falk0baab862005-06-02 18:17:13 -04003716
Linus Torvalds1da177e2005-04-16 15:20:36 -07003717/**
3718 * ata_qc_issue_prot - issue taskfile to device in proto-dependent manner
3719 * @qc: command to issue to device
3720 *
3721 * Using various libata functions and hooks, this function
3722 * starts an ATA command. ATA commands are grouped into
3723 * classes called "protocols", and issuing each type of protocol
3724 * is slightly different.
3725 *
Edward Falk0baab862005-06-02 18:17:13 -04003726 * May be used as the qc_issue() entry in ata_port_operations.
3727 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003728 * LOCKING:
3729 * spin_lock_irqsave(host_set lock)
3730 *
3731 * RETURNS:
Tejun Heo9a3d9eb2006-01-23 13:09:36 +09003732 * Zero on success, AC_ERR_* mask on failure
Linus Torvalds1da177e2005-04-16 15:20:36 -07003733 */
3734
Tejun Heo9a3d9eb2006-01-23 13:09:36 +09003735unsigned int ata_qc_issue_prot(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003736{
3737 struct ata_port *ap = qc->ap;
3738
3739 ata_dev_select(ap, qc->dev->devno, 1, 0);
3740
3741 switch (qc->tf.protocol) {
3742 case ATA_PROT_NODATA:
Jeff Garzike5338252005-10-30 21:37:17 -05003743 ata_tf_to_host(ap, &qc->tf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003744 break;
3745
3746 case ATA_PROT_DMA:
3747 ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
3748 ap->ops->bmdma_setup(qc); /* set up bmdma */
3749 ap->ops->bmdma_start(qc); /* initiate bmdma */
3750 break;
3751
3752 case ATA_PROT_PIO: /* load tf registers, initiate polling pio */
3753 ata_qc_set_polling(qc);
Jeff Garzike5338252005-10-30 21:37:17 -05003754 ata_tf_to_host(ap, &qc->tf);
Albert Lee14be71f2005-09-27 17:36:35 +08003755 ap->hsm_task_state = HSM_ST;
Tejun Heo95064372006-01-23 13:09:37 +09003756 ata_queue_pio_task(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003757 break;
3758
3759 case ATA_PROT_ATAPI:
3760 ata_qc_set_polling(qc);
Jeff Garzike5338252005-10-30 21:37:17 -05003761 ata_tf_to_host(ap, &qc->tf);
Tejun Heo95064372006-01-23 13:09:37 +09003762 ata_queue_packet_task(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003763 break;
3764
3765 case ATA_PROT_ATAPI_NODATA:
Tejun Heoc1389502005-08-22 14:59:24 +09003766 ap->flags |= ATA_FLAG_NOINTR;
Jeff Garzike5338252005-10-30 21:37:17 -05003767 ata_tf_to_host(ap, &qc->tf);
Tejun Heo95064372006-01-23 13:09:37 +09003768 ata_queue_packet_task(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003769 break;
3770
3771 case ATA_PROT_ATAPI_DMA:
Tejun Heoc1389502005-08-22 14:59:24 +09003772 ap->flags |= ATA_FLAG_NOINTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003773 ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
3774 ap->ops->bmdma_setup(qc); /* set up bmdma */
Tejun Heo95064372006-01-23 13:09:37 +09003775 ata_queue_packet_task(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003776 break;
3777
3778 default:
3779 WARN_ON(1);
Tejun Heo9a3d9eb2006-01-23 13:09:36 +09003780 return AC_ERR_SYSTEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003781 }
3782
3783 return 0;
3784}
3785
3786/**
Edward Falk0baab862005-06-02 18:17:13 -04003787 * ata_bmdma_setup_mmio - Set up PCI IDE BMDMA transaction
Linus Torvalds1da177e2005-04-16 15:20:36 -07003788 * @qc: Info associated with this ATA transaction.
3789 *
3790 * LOCKING:
3791 * spin_lock_irqsave(host_set lock)
3792 */
3793
3794static void ata_bmdma_setup_mmio (struct ata_queued_cmd *qc)
3795{
3796 struct ata_port *ap = qc->ap;
3797 unsigned int rw = (qc->tf.flags & ATA_TFLAG_WRITE);
3798 u8 dmactl;
3799 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
3800
3801 /* load PRD table addr. */
3802 mb(); /* make sure PRD table writes are visible to controller */
3803 writel(ap->prd_dma, mmio + ATA_DMA_TABLE_OFS);
3804
3805 /* specify data direction, triple-check start bit is clear */
3806 dmactl = readb(mmio + ATA_DMA_CMD);
3807 dmactl &= ~(ATA_DMA_WR | ATA_DMA_START);
3808 if (!rw)
3809 dmactl |= ATA_DMA_WR;
3810 writeb(dmactl, mmio + ATA_DMA_CMD);
3811
3812 /* issue r/w command */
3813 ap->ops->exec_command(ap, &qc->tf);
3814}
3815
3816/**
Alan Coxb73fc892005-08-26 16:03:19 +01003817 * ata_bmdma_start_mmio - Start a PCI IDE BMDMA transaction
Linus Torvalds1da177e2005-04-16 15:20:36 -07003818 * @qc: Info associated with this ATA transaction.
3819 *
3820 * LOCKING:
3821 * spin_lock_irqsave(host_set lock)
3822 */
3823
3824static void ata_bmdma_start_mmio (struct ata_queued_cmd *qc)
3825{
3826 struct ata_port *ap = qc->ap;
3827 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
3828 u8 dmactl;
3829
3830 /* start host DMA transaction */
3831 dmactl = readb(mmio + ATA_DMA_CMD);
3832 writeb(dmactl | ATA_DMA_START, mmio + ATA_DMA_CMD);
3833
3834 /* Strictly, one may wish to issue a readb() here, to
3835 * flush the mmio write. However, control also passes
3836 * to the hardware at this point, and it will interrupt
3837 * us when we are to resume control. So, in effect,
3838 * we don't care when the mmio write flushes.
3839 * Further, a read of the DMA status register _immediately_
3840 * following the write may not be what certain flaky hardware
3841 * is expected, so I think it is best to not add a readb()
3842 * without first all the MMIO ATA cards/mobos.
3843 * Or maybe I'm just being paranoid.
3844 */
3845}
3846
3847/**
3848 * ata_bmdma_setup_pio - Set up PCI IDE BMDMA transaction (PIO)
3849 * @qc: Info associated with this ATA transaction.
3850 *
3851 * LOCKING:
3852 * spin_lock_irqsave(host_set lock)
3853 */
3854
3855static void ata_bmdma_setup_pio (struct ata_queued_cmd *qc)
3856{
3857 struct ata_port *ap = qc->ap;
3858 unsigned int rw = (qc->tf.flags & ATA_TFLAG_WRITE);
3859 u8 dmactl;
3860
3861 /* load PRD table addr. */
3862 outl(ap->prd_dma, ap->ioaddr.bmdma_addr + ATA_DMA_TABLE_OFS);
3863
3864 /* specify data direction, triple-check start bit is clear */
3865 dmactl = inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3866 dmactl &= ~(ATA_DMA_WR | ATA_DMA_START);
3867 if (!rw)
3868 dmactl |= ATA_DMA_WR;
3869 outb(dmactl, ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3870
3871 /* issue r/w command */
3872 ap->ops->exec_command(ap, &qc->tf);
3873}
3874
3875/**
3876 * ata_bmdma_start_pio - Start a PCI IDE BMDMA transaction (PIO)
3877 * @qc: Info associated with this ATA transaction.
3878 *
3879 * LOCKING:
3880 * spin_lock_irqsave(host_set lock)
3881 */
3882
3883static void ata_bmdma_start_pio (struct ata_queued_cmd *qc)
3884{
3885 struct ata_port *ap = qc->ap;
3886 u8 dmactl;
3887
3888 /* start host DMA transaction */
3889 dmactl = inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3890 outb(dmactl | ATA_DMA_START,
3891 ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3892}
3893
Edward Falk0baab862005-06-02 18:17:13 -04003894
3895/**
3896 * ata_bmdma_start - Start a PCI IDE BMDMA transaction
3897 * @qc: Info associated with this ATA transaction.
3898 *
3899 * Writes the ATA_DMA_START flag to the DMA command register.
3900 *
3901 * May be used as the bmdma_start() entry in ata_port_operations.
3902 *
3903 * LOCKING:
3904 * spin_lock_irqsave(host_set lock)
3905 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003906void ata_bmdma_start(struct ata_queued_cmd *qc)
3907{
3908 if (qc->ap->flags & ATA_FLAG_MMIO)
3909 ata_bmdma_start_mmio(qc);
3910 else
3911 ata_bmdma_start_pio(qc);
3912}
3913
Edward Falk0baab862005-06-02 18:17:13 -04003914
3915/**
3916 * ata_bmdma_setup - Set up PCI IDE BMDMA transaction
3917 * @qc: Info associated with this ATA transaction.
3918 *
3919 * Writes address of PRD table to device's PRD Table Address
3920 * register, sets the DMA control register, and calls
3921 * ops->exec_command() to start the transfer.
3922 *
3923 * May be used as the bmdma_setup() entry in ata_port_operations.
3924 *
3925 * LOCKING:
3926 * spin_lock_irqsave(host_set lock)
3927 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003928void ata_bmdma_setup(struct ata_queued_cmd *qc)
3929{
3930 if (qc->ap->flags & ATA_FLAG_MMIO)
3931 ata_bmdma_setup_mmio(qc);
3932 else
3933 ata_bmdma_setup_pio(qc);
3934}
3935
Edward Falk0baab862005-06-02 18:17:13 -04003936
3937/**
3938 * ata_bmdma_irq_clear - Clear PCI IDE BMDMA interrupt.
Jeff Garzikdecc6d02005-06-02 18:42:33 -04003939 * @ap: Port associated with this ATA transaction.
Edward Falk0baab862005-06-02 18:17:13 -04003940 *
3941 * Clear interrupt and error flags in DMA status register.
3942 *
3943 * May be used as the irq_clear() entry in ata_port_operations.
3944 *
3945 * LOCKING:
3946 * spin_lock_irqsave(host_set lock)
3947 */
3948
Linus Torvalds1da177e2005-04-16 15:20:36 -07003949void ata_bmdma_irq_clear(struct ata_port *ap)
3950{
3951 if (ap->flags & ATA_FLAG_MMIO) {
3952 void __iomem *mmio = ((void __iomem *) ap->ioaddr.bmdma_addr) + ATA_DMA_STATUS;
3953 writeb(readb(mmio), mmio);
3954 } else {
3955 unsigned long addr = ap->ioaddr.bmdma_addr + ATA_DMA_STATUS;
3956 outb(inb(addr), addr);
3957 }
3958
3959}
3960
Edward Falk0baab862005-06-02 18:17:13 -04003961
3962/**
3963 * ata_bmdma_status - Read PCI IDE BMDMA status
Jeff Garzikdecc6d02005-06-02 18:42:33 -04003964 * @ap: Port associated with this ATA transaction.
Edward Falk0baab862005-06-02 18:17:13 -04003965 *
3966 * Read and return BMDMA status register.
3967 *
3968 * May be used as the bmdma_status() entry in ata_port_operations.
3969 *
3970 * LOCKING:
3971 * spin_lock_irqsave(host_set lock)
3972 */
3973
Linus Torvalds1da177e2005-04-16 15:20:36 -07003974u8 ata_bmdma_status(struct ata_port *ap)
3975{
3976 u8 host_stat;
3977 if (ap->flags & ATA_FLAG_MMIO) {
3978 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
3979 host_stat = readb(mmio + ATA_DMA_STATUS);
3980 } else
Albert Leeee500aa2005-09-27 17:34:38 +08003981 host_stat = inb(ap->ioaddr.bmdma_addr + ATA_DMA_STATUS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003982 return host_stat;
3983}
3984
Edward Falk0baab862005-06-02 18:17:13 -04003985
3986/**
3987 * ata_bmdma_stop - Stop PCI IDE BMDMA transfer
Alan Coxb73fc892005-08-26 16:03:19 +01003988 * @qc: Command we are ending DMA for
Edward Falk0baab862005-06-02 18:17:13 -04003989 *
3990 * Clears the ATA_DMA_START flag in the dma control register
3991 *
3992 * May be used as the bmdma_stop() entry in ata_port_operations.
3993 *
3994 * LOCKING:
3995 * spin_lock_irqsave(host_set lock)
3996 */
3997
Alan Coxb73fc892005-08-26 16:03:19 +01003998void ata_bmdma_stop(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003999{
Alan Coxb73fc892005-08-26 16:03:19 +01004000 struct ata_port *ap = qc->ap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004001 if (ap->flags & ATA_FLAG_MMIO) {
4002 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
4003
4004 /* clear start/stop bit */
4005 writeb(readb(mmio + ATA_DMA_CMD) & ~ATA_DMA_START,
4006 mmio + ATA_DMA_CMD);
4007 } else {
4008 /* clear start/stop bit */
4009 outb(inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD) & ~ATA_DMA_START,
4010 ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
4011 }
4012
4013 /* one-PIO-cycle guaranteed wait, per spec, for HDMA1:0 transition */
4014 ata_altstatus(ap); /* dummy read */
4015}
4016
4017/**
4018 * ata_host_intr - Handle host interrupt for given (port, task)
4019 * @ap: Port on which interrupt arrived (possibly...)
4020 * @qc: Taskfile currently active in engine
4021 *
4022 * Handle host interrupt for given queued command. Currently,
4023 * only DMA interrupts are handled. All other commands are
4024 * handled via polling with interrupts disabled (nIEN bit).
4025 *
4026 * LOCKING:
4027 * spin_lock_irqsave(host_set lock)
4028 *
4029 * RETURNS:
4030 * One if interrupt was handled, zero if not (shared irq).
4031 */
4032
4033inline unsigned int ata_host_intr (struct ata_port *ap,
4034 struct ata_queued_cmd *qc)
4035{
4036 u8 status, host_stat;
4037
4038 switch (qc->tf.protocol) {
4039
4040 case ATA_PROT_DMA:
4041 case ATA_PROT_ATAPI_DMA:
4042 case ATA_PROT_ATAPI:
4043 /* check status of DMA engine */
4044 host_stat = ap->ops->bmdma_status(ap);
4045 VPRINTK("ata%u: host_stat 0x%X\n", ap->id, host_stat);
4046
4047 /* if it's not our irq... */
4048 if (!(host_stat & ATA_DMA_INTR))
4049 goto idle_irq;
4050
4051 /* before we do anything else, clear DMA-Start bit */
Alan Coxb73fc892005-08-26 16:03:19 +01004052 ap->ops->bmdma_stop(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004053
4054 /* fall through */
4055
4056 case ATA_PROT_ATAPI_NODATA:
4057 case ATA_PROT_NODATA:
4058 /* check altstatus */
4059 status = ata_altstatus(ap);
4060 if (status & ATA_BUSY)
4061 goto idle_irq;
4062
4063 /* check main status, clearing INTRQ */
4064 status = ata_chk_status(ap);
4065 if (unlikely(status & ATA_BUSY))
4066 goto idle_irq;
4067 DPRINTK("ata%u: protocol %d (dev_stat 0x%X)\n",
4068 ap->id, qc->tf.protocol, status);
4069
4070 /* ack bmdma irq events */
4071 ap->ops->irq_clear(ap);
4072
4073 /* complete taskfile transaction */
Albert Leea22e2eb2005-12-05 15:38:02 +08004074 qc->err_mask |= ac_err_mask(status);
4075 ata_qc_complete(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004076 break;
4077
4078 default:
4079 goto idle_irq;
4080 }
4081
4082 return 1; /* irq handled */
4083
4084idle_irq:
4085 ap->stats.idle_irq++;
4086
4087#ifdef ATA_IRQ_TRAP
4088 if ((ap->stats.idle_irq % 1000) == 0) {
4089 handled = 1;
4090 ata_irq_ack(ap, 0); /* debug trap */
4091 printk(KERN_WARNING "ata%d: irq trap\n", ap->id);
4092 }
4093#endif
4094 return 0; /* irq not handled */
4095}
4096
4097/**
4098 * ata_interrupt - Default ATA host interrupt handler
Jeff Garzik0cba6322005-05-30 19:49:12 -04004099 * @irq: irq line (unused)
4100 * @dev_instance: pointer to our ata_host_set information structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07004101 * @regs: unused
4102 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04004103 * Default interrupt handler for PCI IDE devices. Calls
4104 * ata_host_intr() for each port that is not disabled.
4105 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004106 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004107 * Obtains host_set lock during operation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004108 *
4109 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004110 * IRQ_NONE or IRQ_HANDLED.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004111 */
4112
4113irqreturn_t ata_interrupt (int irq, void *dev_instance, struct pt_regs *regs)
4114{
4115 struct ata_host_set *host_set = dev_instance;
4116 unsigned int i;
4117 unsigned int handled = 0;
4118 unsigned long flags;
4119
4120 /* TODO: make _irqsave conditional on x86 PCI IDE legacy mode */
4121 spin_lock_irqsave(&host_set->lock, flags);
4122
4123 for (i = 0; i < host_set->n_ports; i++) {
4124 struct ata_port *ap;
4125
4126 ap = host_set->ports[i];
Tejun Heoc1389502005-08-22 14:59:24 +09004127 if (ap &&
4128 !(ap->flags & (ATA_FLAG_PORT_DISABLED | ATA_FLAG_NOINTR))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004129 struct ata_queued_cmd *qc;
4130
4131 qc = ata_qc_from_tag(ap, ap->active_tag);
Albert Lee21b1ed72005-04-29 17:34:59 +08004132 if (qc && (!(qc->tf.ctl & ATA_NIEN)) &&
4133 (qc->flags & ATA_QCFLAG_ACTIVE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004134 handled |= ata_host_intr(ap, qc);
4135 }
4136 }
4137
4138 spin_unlock_irqrestore(&host_set->lock, flags);
4139
4140 return IRQ_RETVAL(handled);
4141}
4142
4143/**
4144 * atapi_packet_task - Write CDB bytes to hardware
4145 * @_data: Port to which ATAPI device is attached.
4146 *
4147 * When device has indicated its readiness to accept
4148 * a CDB, this function is called. Send the CDB.
4149 * If DMA is to be performed, exit immediately.
4150 * Otherwise, we are in polling mode, so poll
4151 * status under operation succeeds or fails.
4152 *
4153 * LOCKING:
4154 * Kernel thread context (may sleep)
4155 */
4156
4157static void atapi_packet_task(void *_data)
4158{
4159 struct ata_port *ap = _data;
4160 struct ata_queued_cmd *qc;
4161 u8 status;
4162
4163 qc = ata_qc_from_tag(ap, ap->active_tag);
4164 assert(qc != NULL);
4165 assert(qc->flags & ATA_QCFLAG_ACTIVE);
4166
4167 /* sleep-wait for BSY to clear */
4168 DPRINTK("busy wait\n");
Albert Leed8fe4522005-12-05 15:42:17 +08004169 if (ata_busy_sleep(ap, ATA_TMOUT_CDB_QUICK, ATA_TMOUT_CDB)) {
Tejun Heo11a56d22006-01-23 13:09:36 +09004170 qc->err_mask |= AC_ERR_TIMEOUT;
Albert Leed8fe4522005-12-05 15:42:17 +08004171 goto err_out;
4172 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004173
4174 /* make sure DRQ is set */
4175 status = ata_chk_status(ap);
Albert Leed8fe4522005-12-05 15:42:17 +08004176 if ((status & (ATA_BUSY | ATA_DRQ)) != ATA_DRQ) {
Tejun Heo11a56d22006-01-23 13:09:36 +09004177 qc->err_mask |= AC_ERR_HSM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004178 goto err_out;
Albert Leed8fe4522005-12-05 15:42:17 +08004179 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004180
4181 /* send SCSI cdb */
4182 DPRINTK("send cdb\n");
4183 assert(ap->cdb_len >= 12);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004184
Tejun Heoc1389502005-08-22 14:59:24 +09004185 if (qc->tf.protocol == ATA_PROT_ATAPI_DMA ||
4186 qc->tf.protocol == ATA_PROT_ATAPI_NODATA) {
4187 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004188
Tejun Heoc1389502005-08-22 14:59:24 +09004189 /* Once we're done issuing command and kicking bmdma,
4190 * irq handler takes over. To not lose irq, we need
4191 * to clear NOINTR flag before sending cdb, but
4192 * interrupt handler shouldn't be invoked before we're
4193 * finished. Hence, the following locking.
4194 */
4195 spin_lock_irqsave(&ap->host_set->lock, flags);
4196 ap->flags &= ~ATA_FLAG_NOINTR;
4197 ata_data_xfer(ap, qc->cdb, ap->cdb_len, 1);
4198 if (qc->tf.protocol == ATA_PROT_ATAPI_DMA)
4199 ap->ops->bmdma_start(qc); /* initiate bmdma */
4200 spin_unlock_irqrestore(&ap->host_set->lock, flags);
4201 } else {
4202 ata_data_xfer(ap, qc->cdb, ap->cdb_len, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004203
Tejun Heoc1389502005-08-22 14:59:24 +09004204 /* PIO commands are handled by polling */
Albert Lee14be71f2005-09-27 17:36:35 +08004205 ap->hsm_task_state = HSM_ST;
Tejun Heo95064372006-01-23 13:09:37 +09004206 ata_queue_pio_task(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004207 }
4208
4209 return;
4210
4211err_out:
Albert Leea22e2eb2005-12-05 15:38:02 +08004212 ata_poll_qc_complete(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004213}
4214
Edward Falk0baab862005-06-02 18:17:13 -04004215
4216/**
4217 * ata_port_start - Set port up for dma.
4218 * @ap: Port to initialize
4219 *
4220 * Called just after data structures for each port are
4221 * initialized. Allocates space for PRD table.
4222 *
4223 * May be used as the port_start() entry in ata_port_operations.
4224 *
4225 * LOCKING:
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04004226 * Inherited from caller.
Edward Falk0baab862005-06-02 18:17:13 -04004227 */
4228
Jens Axboe9b847542006-01-06 09:28:07 +01004229/*
4230 * Execute a 'simple' command, that only consists of the opcode 'cmd' itself,
4231 * without filling any other registers
4232 */
4233static int ata_do_simple_cmd(struct ata_port *ap, struct ata_device *dev,
4234 u8 cmd)
4235{
4236 struct ata_taskfile tf;
4237 int err;
4238
4239 ata_tf_init(ap, &tf, dev->devno);
4240
4241 tf.command = cmd;
4242 tf.flags |= ATA_TFLAG_DEVICE;
4243 tf.protocol = ATA_PROT_NODATA;
4244
4245 err = ata_exec_internal(ap, dev, &tf, DMA_NONE, NULL, 0);
4246 if (err)
4247 printk(KERN_ERR "%s: ata command failed: %d\n",
4248 __FUNCTION__, err);
4249
4250 return err;
4251}
4252
4253static int ata_flush_cache(struct ata_port *ap, struct ata_device *dev)
4254{
4255 u8 cmd;
4256
4257 if (!ata_try_flush_cache(dev))
4258 return 0;
4259
4260 if (ata_id_has_flush_ext(dev->id))
4261 cmd = ATA_CMD_FLUSH_EXT;
4262 else
4263 cmd = ATA_CMD_FLUSH;
4264
4265 return ata_do_simple_cmd(ap, dev, cmd);
4266}
4267
4268static int ata_standby_drive(struct ata_port *ap, struct ata_device *dev)
4269{
4270 return ata_do_simple_cmd(ap, dev, ATA_CMD_STANDBYNOW1);
4271}
4272
4273static int ata_start_drive(struct ata_port *ap, struct ata_device *dev)
4274{
4275 return ata_do_simple_cmd(ap, dev, ATA_CMD_IDLEIMMEDIATE);
4276}
4277
4278/**
4279 * ata_device_resume - wakeup a previously suspended devices
4280 *
4281 * Kick the drive back into action, by sending it an idle immediate
4282 * command and making sure its transfer mode matches between drive
4283 * and host.
4284 *
4285 */
4286int ata_device_resume(struct ata_port *ap, struct ata_device *dev)
4287{
4288 if (ap->flags & ATA_FLAG_SUSPENDED) {
4289 ap->flags &= ~ATA_FLAG_SUSPENDED;
4290 ata_set_mode(ap);
4291 }
4292 if (!ata_dev_present(dev))
4293 return 0;
4294 if (dev->class == ATA_DEV_ATA)
4295 ata_start_drive(ap, dev);
4296
4297 return 0;
4298}
4299
4300/**
4301 * ata_device_suspend - prepare a device for suspend
4302 *
4303 * Flush the cache on the drive, if appropriate, then issue a
4304 * standbynow command.
4305 *
4306 */
4307int ata_device_suspend(struct ata_port *ap, struct ata_device *dev)
4308{
4309 if (!ata_dev_present(dev))
4310 return 0;
4311 if (dev->class == ATA_DEV_ATA)
4312 ata_flush_cache(ap, dev);
4313
4314 ata_standby_drive(ap, dev);
4315 ap->flags |= ATA_FLAG_SUSPENDED;
4316 return 0;
4317}
4318
Linus Torvalds1da177e2005-04-16 15:20:36 -07004319int ata_port_start (struct ata_port *ap)
4320{
4321 struct device *dev = ap->host_set->dev;
Jeff Garzik6037d6b2005-11-04 22:08:00 -05004322 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004323
4324 ap->prd = dma_alloc_coherent(dev, ATA_PRD_TBL_SZ, &ap->prd_dma, GFP_KERNEL);
4325 if (!ap->prd)
4326 return -ENOMEM;
4327
Jeff Garzik6037d6b2005-11-04 22:08:00 -05004328 rc = ata_pad_alloc(ap, dev);
4329 if (rc) {
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004330 dma_free_coherent(dev, ATA_PRD_TBL_SZ, ap->prd, ap->prd_dma);
Jeff Garzik6037d6b2005-11-04 22:08:00 -05004331 return rc;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004332 }
4333
Linus Torvalds1da177e2005-04-16 15:20:36 -07004334 DPRINTK("prd alloc, virt %p, dma %llx\n", ap->prd, (unsigned long long) ap->prd_dma);
4335
4336 return 0;
4337}
4338
Edward Falk0baab862005-06-02 18:17:13 -04004339
4340/**
4341 * ata_port_stop - Undo ata_port_start()
4342 * @ap: Port to shut down
4343 *
4344 * Frees the PRD table.
4345 *
4346 * May be used as the port_stop() entry in ata_port_operations.
4347 *
4348 * LOCKING:
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04004349 * Inherited from caller.
Edward Falk0baab862005-06-02 18:17:13 -04004350 */
4351
Linus Torvalds1da177e2005-04-16 15:20:36 -07004352void ata_port_stop (struct ata_port *ap)
4353{
4354 struct device *dev = ap->host_set->dev;
4355
4356 dma_free_coherent(dev, ATA_PRD_TBL_SZ, ap->prd, ap->prd_dma);
Jeff Garzik6037d6b2005-11-04 22:08:00 -05004357 ata_pad_free(ap, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004358}
4359
Jeff Garzikaa8f0dc2005-05-26 21:54:27 -04004360void ata_host_stop (struct ata_host_set *host_set)
4361{
4362 if (host_set->mmio_base)
4363 iounmap(host_set->mmio_base);
4364}
4365
4366
Linus Torvalds1da177e2005-04-16 15:20:36 -07004367/**
4368 * ata_host_remove - Unregister SCSI host structure with upper layers
4369 * @ap: Port to unregister
4370 * @do_unregister: 1 if we fully unregister, 0 to just stop the port
4371 *
4372 * LOCKING:
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04004373 * Inherited from caller.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004374 */
4375
4376static void ata_host_remove(struct ata_port *ap, unsigned int do_unregister)
4377{
4378 struct Scsi_Host *sh = ap->host;
4379
4380 DPRINTK("ENTER\n");
4381
4382 if (do_unregister)
4383 scsi_remove_host(sh);
4384
4385 ap->ops->port_stop(ap);
4386}
4387
4388/**
4389 * ata_host_init - Initialize an ata_port structure
4390 * @ap: Structure to initialize
4391 * @host: associated SCSI mid-layer structure
4392 * @host_set: Collection of hosts to which @ap belongs
4393 * @ent: Probe information provided by low-level driver
4394 * @port_no: Port number associated with this ata_port
4395 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04004396 * Initialize a new ata_port structure, and its associated
4397 * scsi_host.
4398 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004399 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004400 * Inherited from caller.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004401 */
4402
4403static void ata_host_init(struct ata_port *ap, struct Scsi_Host *host,
4404 struct ata_host_set *host_set,
Jeff Garzik057ace52005-10-22 14:27:05 -04004405 const struct ata_probe_ent *ent, unsigned int port_no)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004406{
4407 unsigned int i;
4408
4409 host->max_id = 16;
4410 host->max_lun = 1;
4411 host->max_channel = 1;
4412 host->unique_id = ata_unique_id++;
4413 host->max_cmd_len = 12;
Christoph Hellwig12413192005-06-11 01:05:01 +02004414
Linus Torvalds1da177e2005-04-16 15:20:36 -07004415 ap->flags = ATA_FLAG_PORT_DISABLED;
4416 ap->id = host->unique_id;
4417 ap->host = host;
4418 ap->ctl = ATA_DEVCTL_OBS;
4419 ap->host_set = host_set;
4420 ap->port_no = port_no;
4421 ap->hard_port_no =
4422 ent->legacy_mode ? ent->hard_port_no : port_no;
4423 ap->pio_mask = ent->pio_mask;
4424 ap->mwdma_mask = ent->mwdma_mask;
4425 ap->udma_mask = ent->udma_mask;
4426 ap->flags |= ent->host_flags;
4427 ap->ops = ent->port_ops;
4428 ap->cbl = ATA_CBL_NONE;
4429 ap->active_tag = ATA_TAG_POISON;
4430 ap->last_ctl = 0xFF;
4431
4432 INIT_WORK(&ap->packet_task, atapi_packet_task, ap);
4433 INIT_WORK(&ap->pio_task, ata_pio_task, ap);
Tejun Heoa72ec4c2006-01-23 13:09:37 +09004434 INIT_LIST_HEAD(&ap->eh_done_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004435
4436 for (i = 0; i < ATA_MAX_DEVICES; i++)
4437 ap->device[i].devno = i;
4438
4439#ifdef ATA_IRQ_TRAP
4440 ap->stats.unhandled_irq = 1;
4441 ap->stats.idle_irq = 1;
4442#endif
4443
4444 memcpy(&ap->ioaddr, &ent->port[port_no], sizeof(struct ata_ioports));
4445}
4446
4447/**
4448 * ata_host_add - Attach low-level ATA driver to system
4449 * @ent: Information provided by low-level driver
4450 * @host_set: Collections of ports to which we add
4451 * @port_no: Port number associated with this host
4452 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04004453 * Attach low-level ATA driver to system.
4454 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004455 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004456 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004457 *
4458 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004459 * New ata_port on success, for NULL on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004460 */
4461
Jeff Garzik057ace52005-10-22 14:27:05 -04004462static struct ata_port * ata_host_add(const struct ata_probe_ent *ent,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004463 struct ata_host_set *host_set,
4464 unsigned int port_no)
4465{
4466 struct Scsi_Host *host;
4467 struct ata_port *ap;
4468 int rc;
4469
4470 DPRINTK("ENTER\n");
4471 host = scsi_host_alloc(ent->sht, sizeof(struct ata_port));
4472 if (!host)
4473 return NULL;
4474
4475 ap = (struct ata_port *) &host->hostdata[0];
4476
4477 ata_host_init(ap, host, host_set, ent, port_no);
4478
4479 rc = ap->ops->port_start(ap);
4480 if (rc)
4481 goto err_out;
4482
4483 return ap;
4484
4485err_out:
4486 scsi_host_put(host);
4487 return NULL;
4488}
4489
4490/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04004491 * ata_device_add - Register hardware device with ATA and SCSI layers
4492 * @ent: Probe information describing hardware device to be registered
4493 *
4494 * This function processes the information provided in the probe
4495 * information struct @ent, allocates the necessary ATA and SCSI
4496 * host information structures, initializes them, and registers
4497 * everything with requisite kernel subsystems.
4498 *
4499 * This function requests irqs, probes the ATA bus, and probes
4500 * the SCSI bus.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004501 *
4502 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004503 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004504 *
4505 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004506 * Number of ports registered. Zero on error (no ports registered).
Linus Torvalds1da177e2005-04-16 15:20:36 -07004507 */
4508
Jeff Garzik057ace52005-10-22 14:27:05 -04004509int ata_device_add(const struct ata_probe_ent *ent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004510{
4511 unsigned int count = 0, i;
4512 struct device *dev = ent->dev;
4513 struct ata_host_set *host_set;
4514
4515 DPRINTK("ENTER\n");
4516 /* alloc a container for our list of ATA ports (buses) */
Randy Dunlap57f3bda2005-10-28 20:37:23 -07004517 host_set = kzalloc(sizeof(struct ata_host_set) +
Linus Torvalds1da177e2005-04-16 15:20:36 -07004518 (ent->n_ports * sizeof(void *)), GFP_KERNEL);
4519 if (!host_set)
4520 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004521 spin_lock_init(&host_set->lock);
4522
4523 host_set->dev = dev;
4524 host_set->n_ports = ent->n_ports;
4525 host_set->irq = ent->irq;
4526 host_set->mmio_base = ent->mmio_base;
4527 host_set->private_data = ent->private_data;
4528 host_set->ops = ent->port_ops;
4529
4530 /* register each port bound to this device */
4531 for (i = 0; i < ent->n_ports; i++) {
4532 struct ata_port *ap;
4533 unsigned long xfer_mode_mask;
4534
4535 ap = ata_host_add(ent, host_set, i);
4536 if (!ap)
4537 goto err_out;
4538
4539 host_set->ports[i] = ap;
4540 xfer_mode_mask =(ap->udma_mask << ATA_SHIFT_UDMA) |
4541 (ap->mwdma_mask << ATA_SHIFT_MWDMA) |
4542 (ap->pio_mask << ATA_SHIFT_PIO);
4543
4544 /* print per-port info to dmesg */
4545 printk(KERN_INFO "ata%u: %cATA max %s cmd 0x%lX ctl 0x%lX "
4546 "bmdma 0x%lX irq %lu\n",
4547 ap->id,
4548 ap->flags & ATA_FLAG_SATA ? 'S' : 'P',
4549 ata_mode_string(xfer_mode_mask),
4550 ap->ioaddr.cmd_addr,
4551 ap->ioaddr.ctl_addr,
4552 ap->ioaddr.bmdma_addr,
4553 ent->irq);
4554
4555 ata_chk_status(ap);
4556 host_set->ops->irq_clear(ap);
4557 count++;
4558 }
4559
Randy Dunlap57f3bda2005-10-28 20:37:23 -07004560 if (!count)
4561 goto err_free_ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004562
4563 /* obtain irq, that is shared between channels */
4564 if (request_irq(ent->irq, ent->port_ops->irq_handler, ent->irq_flags,
4565 DRV_NAME, host_set))
4566 goto err_out;
4567
4568 /* perform each probe synchronously */
4569 DPRINTK("probe begin\n");
4570 for (i = 0; i < count; i++) {
4571 struct ata_port *ap;
4572 int rc;
4573
4574 ap = host_set->ports[i];
4575
4576 DPRINTK("ata%u: probe begin\n", ap->id);
4577 rc = ata_bus_probe(ap);
4578 DPRINTK("ata%u: probe end\n", ap->id);
4579
4580 if (rc) {
4581 /* FIXME: do something useful here?
4582 * Current libata behavior will
4583 * tear down everything when
4584 * the module is removed
4585 * or the h/w is unplugged.
4586 */
4587 }
4588
4589 rc = scsi_add_host(ap->host, dev);
4590 if (rc) {
4591 printk(KERN_ERR "ata%u: scsi_add_host failed\n",
4592 ap->id);
4593 /* FIXME: do something useful here */
4594 /* FIXME: handle unconditional calls to
4595 * scsi_scan_host and ata_host_remove, below,
4596 * at the very least
4597 */
4598 }
4599 }
4600
4601 /* probes are done, now scan each port's disk(s) */
4602 DPRINTK("probe begin\n");
4603 for (i = 0; i < count; i++) {
4604 struct ata_port *ap = host_set->ports[i];
4605
Jeff Garzik644dd0c2005-10-03 15:55:19 -04004606 ata_scsi_scan_host(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004607 }
4608
4609 dev_set_drvdata(dev, host_set);
4610
4611 VPRINTK("EXIT, returning %u\n", ent->n_ports);
4612 return ent->n_ports; /* success */
4613
4614err_out:
4615 for (i = 0; i < count; i++) {
4616 ata_host_remove(host_set->ports[i], 1);
4617 scsi_host_put(host_set->ports[i]->host);
4618 }
Randy Dunlap57f3bda2005-10-28 20:37:23 -07004619err_free_ret:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004620 kfree(host_set);
4621 VPRINTK("EXIT, returning 0\n");
4622 return 0;
4623}
4624
4625/**
Alan Cox17b14452005-09-15 15:44:00 +01004626 * ata_host_set_remove - PCI layer callback for device removal
4627 * @host_set: ATA host set that was removed
4628 *
4629 * Unregister all objects associated with this host set. Free those
4630 * objects.
4631 *
4632 * LOCKING:
4633 * Inherited from calling layer (may sleep).
4634 */
4635
Alan Cox17b14452005-09-15 15:44:00 +01004636void ata_host_set_remove(struct ata_host_set *host_set)
4637{
4638 struct ata_port *ap;
4639 unsigned int i;
4640
4641 for (i = 0; i < host_set->n_ports; i++) {
4642 ap = host_set->ports[i];
4643 scsi_remove_host(ap->host);
4644 }
4645
4646 free_irq(host_set->irq, host_set);
4647
4648 for (i = 0; i < host_set->n_ports; i++) {
4649 ap = host_set->ports[i];
4650
4651 ata_scsi_release(ap->host);
4652
4653 if ((ap->flags & ATA_FLAG_NO_LEGACY) == 0) {
4654 struct ata_ioports *ioaddr = &ap->ioaddr;
4655
4656 if (ioaddr->cmd_addr == 0x1f0)
4657 release_region(0x1f0, 8);
4658 else if (ioaddr->cmd_addr == 0x170)
4659 release_region(0x170, 8);
4660 }
4661
4662 scsi_host_put(ap->host);
4663 }
4664
4665 if (host_set->ops->host_stop)
4666 host_set->ops->host_stop(host_set);
4667
4668 kfree(host_set);
4669}
4670
4671/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004672 * ata_scsi_release - SCSI layer callback hook for host unload
4673 * @host: libata host to be unloaded
4674 *
4675 * Performs all duties necessary to shut down a libata port...
4676 * Kill port kthread, disable port, and release resources.
4677 *
4678 * LOCKING:
4679 * Inherited from SCSI layer.
4680 *
4681 * RETURNS:
4682 * One.
4683 */
4684
4685int ata_scsi_release(struct Scsi_Host *host)
4686{
4687 struct ata_port *ap = (struct ata_port *) &host->hostdata[0];
4688
4689 DPRINTK("ENTER\n");
4690
4691 ap->ops->port_disable(ap);
4692 ata_host_remove(ap, 0);
4693
4694 DPRINTK("EXIT\n");
4695 return 1;
4696}
4697
4698/**
4699 * ata_std_ports - initialize ioaddr with standard port offsets.
4700 * @ioaddr: IO address structure to be initialized
Edward Falk0baab862005-06-02 18:17:13 -04004701 *
4702 * Utility function which initializes data_addr, error_addr,
4703 * feature_addr, nsect_addr, lbal_addr, lbam_addr, lbah_addr,
4704 * device_addr, status_addr, and command_addr to standard offsets
4705 * relative to cmd_addr.
4706 *
4707 * Does not set ctl_addr, altstatus_addr, bmdma_addr, or scr_addr.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004708 */
Edward Falk0baab862005-06-02 18:17:13 -04004709
Linus Torvalds1da177e2005-04-16 15:20:36 -07004710void ata_std_ports(struct ata_ioports *ioaddr)
4711{
4712 ioaddr->data_addr = ioaddr->cmd_addr + ATA_REG_DATA;
4713 ioaddr->error_addr = ioaddr->cmd_addr + ATA_REG_ERR;
4714 ioaddr->feature_addr = ioaddr->cmd_addr + ATA_REG_FEATURE;
4715 ioaddr->nsect_addr = ioaddr->cmd_addr + ATA_REG_NSECT;
4716 ioaddr->lbal_addr = ioaddr->cmd_addr + ATA_REG_LBAL;
4717 ioaddr->lbam_addr = ioaddr->cmd_addr + ATA_REG_LBAM;
4718 ioaddr->lbah_addr = ioaddr->cmd_addr + ATA_REG_LBAH;
4719 ioaddr->device_addr = ioaddr->cmd_addr + ATA_REG_DEVICE;
4720 ioaddr->status_addr = ioaddr->cmd_addr + ATA_REG_STATUS;
4721 ioaddr->command_addr = ioaddr->cmd_addr + ATA_REG_CMD;
4722}
4723
4724static struct ata_probe_ent *
Jeff Garzik057ace52005-10-22 14:27:05 -04004725ata_probe_ent_alloc(struct device *dev, const struct ata_port_info *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004726{
4727 struct ata_probe_ent *probe_ent;
4728
Randy Dunlap57f3bda2005-10-28 20:37:23 -07004729 probe_ent = kzalloc(sizeof(*probe_ent), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004730 if (!probe_ent) {
4731 printk(KERN_ERR DRV_NAME "(%s): out of memory\n",
4732 kobject_name(&(dev->kobj)));
4733 return NULL;
4734 }
4735
Linus Torvalds1da177e2005-04-16 15:20:36 -07004736 INIT_LIST_HEAD(&probe_ent->node);
4737 probe_ent->dev = dev;
4738
4739 probe_ent->sht = port->sht;
4740 probe_ent->host_flags = port->host_flags;
4741 probe_ent->pio_mask = port->pio_mask;
4742 probe_ent->mwdma_mask = port->mwdma_mask;
4743 probe_ent->udma_mask = port->udma_mask;
4744 probe_ent->port_ops = port->port_ops;
4745
4746 return probe_ent;
4747}
4748
Edward Falk0baab862005-06-02 18:17:13 -04004749
4750
Jeff Garzik374b1872005-08-30 05:42:52 -04004751#ifdef CONFIG_PCI
4752
4753void ata_pci_host_stop (struct ata_host_set *host_set)
4754{
4755 struct pci_dev *pdev = to_pci_dev(host_set->dev);
4756
4757 pci_iounmap(pdev, host_set->mmio_base);
4758}
4759
Edward Falk0baab862005-06-02 18:17:13 -04004760/**
4761 * ata_pci_init_native_mode - Initialize native-mode driver
4762 * @pdev: pci device to be initialized
4763 * @port: array[2] of pointers to port info structures.
Alan Cox47a86592005-10-04 08:09:19 -04004764 * @ports: bitmap of ports present
Edward Falk0baab862005-06-02 18:17:13 -04004765 *
4766 * Utility function which allocates and initializes an
4767 * ata_probe_ent structure for a standard dual-port
4768 * PIO-based IDE controller. The returned ata_probe_ent
4769 * structure can be passed to ata_device_add(). The returned
4770 * ata_probe_ent structure should then be freed with kfree().
Alan Cox47a86592005-10-04 08:09:19 -04004771 *
4772 * The caller need only pass the address of the primary port, the
4773 * secondary will be deduced automatically. If the device has non
4774 * standard secondary port mappings this function can be called twice,
4775 * once for each interface.
Edward Falk0baab862005-06-02 18:17:13 -04004776 */
4777
Linus Torvalds1da177e2005-04-16 15:20:36 -07004778struct ata_probe_ent *
Alan Cox47a86592005-10-04 08:09:19 -04004779ata_pci_init_native_mode(struct pci_dev *pdev, struct ata_port_info **port, int ports)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004780{
4781 struct ata_probe_ent *probe_ent =
4782 ata_probe_ent_alloc(pci_dev_to_dev(pdev), port[0]);
Alan Cox47a86592005-10-04 08:09:19 -04004783 int p = 0;
4784
Linus Torvalds1da177e2005-04-16 15:20:36 -07004785 if (!probe_ent)
4786 return NULL;
4787
Linus Torvalds1da177e2005-04-16 15:20:36 -07004788 probe_ent->irq = pdev->irq;
4789 probe_ent->irq_flags = SA_SHIRQ;
Alan Coxe99f8b52005-11-08 14:09:44 +00004790 probe_ent->private_data = port[0]->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004791
Alan Cox47a86592005-10-04 08:09:19 -04004792 if (ports & ATA_PORT_PRIMARY) {
4793 probe_ent->port[p].cmd_addr = pci_resource_start(pdev, 0);
4794 probe_ent->port[p].altstatus_addr =
4795 probe_ent->port[p].ctl_addr =
4796 pci_resource_start(pdev, 1) | ATA_PCI_CTL_OFS;
4797 probe_ent->port[p].bmdma_addr = pci_resource_start(pdev, 4);
4798 ata_std_ports(&probe_ent->port[p]);
4799 p++;
4800 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004801
Alan Cox47a86592005-10-04 08:09:19 -04004802 if (ports & ATA_PORT_SECONDARY) {
4803 probe_ent->port[p].cmd_addr = pci_resource_start(pdev, 2);
4804 probe_ent->port[p].altstatus_addr =
4805 probe_ent->port[p].ctl_addr =
4806 pci_resource_start(pdev, 3) | ATA_PCI_CTL_OFS;
4807 probe_ent->port[p].bmdma_addr = pci_resource_start(pdev, 4) + 8;
4808 ata_std_ports(&probe_ent->port[p]);
4809 p++;
4810 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004811
Alan Cox47a86592005-10-04 08:09:19 -04004812 probe_ent->n_ports = p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004813 return probe_ent;
4814}
4815
Jeff Garzik0f0d5192005-10-30 06:41:29 -05004816static 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 -07004817{
Alan Cox47a86592005-10-04 08:09:19 -04004818 struct ata_probe_ent *probe_ent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004819
Jeff Garzik0f0d5192005-10-30 06:41:29 -05004820 probe_ent = ata_probe_ent_alloc(pci_dev_to_dev(pdev), port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004821 if (!probe_ent)
4822 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004823
Linus Torvalds1da177e2005-04-16 15:20:36 -07004824 probe_ent->legacy_mode = 1;
Alan Cox47a86592005-10-04 08:09:19 -04004825 probe_ent->n_ports = 1;
4826 probe_ent->hard_port_no = port_num;
Alan Coxe99f8b52005-11-08 14:09:44 +00004827 probe_ent->private_data = port->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004828
Alan Cox47a86592005-10-04 08:09:19 -04004829 switch(port_num)
4830 {
4831 case 0:
4832 probe_ent->irq = 14;
4833 probe_ent->port[0].cmd_addr = 0x1f0;
4834 probe_ent->port[0].altstatus_addr =
4835 probe_ent->port[0].ctl_addr = 0x3f6;
4836 break;
4837 case 1:
4838 probe_ent->irq = 15;
4839 probe_ent->port[0].cmd_addr = 0x170;
4840 probe_ent->port[0].altstatus_addr =
4841 probe_ent->port[0].ctl_addr = 0x376;
4842 break;
4843 }
4844 probe_ent->port[0].bmdma_addr = pci_resource_start(pdev, 4) + 8 * port_num;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004845 ata_std_ports(&probe_ent->port[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004846 return probe_ent;
4847}
4848
4849/**
4850 * ata_pci_init_one - Initialize/register PCI IDE host controller
4851 * @pdev: Controller to be initialized
4852 * @port_info: Information from low-level host driver
4853 * @n_ports: Number of ports attached to host controller
4854 *
Edward Falk0baab862005-06-02 18:17:13 -04004855 * This is a helper function which can be called from a driver's
4856 * xxx_init_one() probe function if the hardware uses traditional
4857 * IDE taskfile registers.
4858 *
4859 * This function calls pci_enable_device(), reserves its register
4860 * regions, sets the dma mask, enables bus master mode, and calls
4861 * ata_device_add()
4862 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004863 * LOCKING:
4864 * Inherited from PCI layer (may sleep).
4865 *
4866 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004867 * Zero on success, negative on errno-based value on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004868 */
4869
4870int ata_pci_init_one (struct pci_dev *pdev, struct ata_port_info **port_info,
4871 unsigned int n_ports)
4872{
Alan Cox47a86592005-10-04 08:09:19 -04004873 struct ata_probe_ent *probe_ent = NULL, *probe_ent2 = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004874 struct ata_port_info *port[2];
4875 u8 tmp8, mask;
4876 unsigned int legacy_mode = 0;
4877 int disable_dev_on_err = 1;
4878 int rc;
4879
4880 DPRINTK("ENTER\n");
4881
4882 port[0] = port_info[0];
4883 if (n_ports > 1)
4884 port[1] = port_info[1];
4885 else
4886 port[1] = port[0];
4887
4888 if ((port[0]->host_flags & ATA_FLAG_NO_LEGACY) == 0
4889 && (pdev->class >> 8) == PCI_CLASS_STORAGE_IDE) {
Alan Cox47a86592005-10-04 08:09:19 -04004890 /* TODO: What if one channel is in native mode ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004891 pci_read_config_byte(pdev, PCI_CLASS_PROG, &tmp8);
4892 mask = (1 << 2) | (1 << 0);
4893 if ((tmp8 & mask) != mask)
4894 legacy_mode = (1 << 3);
4895 }
4896
4897 /* FIXME... */
Alan Cox47a86592005-10-04 08:09:19 -04004898 if ((!legacy_mode) && (n_ports > 2)) {
4899 printk(KERN_ERR "ata: BUG: native mode, n_ports > 2\n");
4900 n_ports = 2;
4901 /* For now */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004902 }
4903
Alan Cox47a86592005-10-04 08:09:19 -04004904 /* FIXME: Really for ATA it isn't safe because the device may be
4905 multi-purpose and we want to leave it alone if it was already
4906 enabled. Secondly for shared use as Arjan says we want refcounting
4907
4908 Checking dev->is_enabled is insufficient as this is not set at
4909 boot for the primary video which is BIOS enabled
4910 */
4911
Linus Torvalds1da177e2005-04-16 15:20:36 -07004912 rc = pci_enable_device(pdev);
4913 if (rc)
4914 return rc;
4915
4916 rc = pci_request_regions(pdev, DRV_NAME);
4917 if (rc) {
4918 disable_dev_on_err = 0;
4919 goto err_out;
4920 }
4921
Alan Cox47a86592005-10-04 08:09:19 -04004922 /* FIXME: Should use platform specific mappers for legacy port ranges */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004923 if (legacy_mode) {
4924 if (!request_region(0x1f0, 8, "libata")) {
4925 struct resource *conflict, res;
4926 res.start = 0x1f0;
4927 res.end = 0x1f0 + 8 - 1;
4928 conflict = ____request_resource(&ioport_resource, &res);
4929 if (!strcmp(conflict->name, "libata"))
4930 legacy_mode |= (1 << 0);
4931 else {
4932 disable_dev_on_err = 0;
4933 printk(KERN_WARNING "ata: 0x1f0 IDE port busy\n");
4934 }
4935 } else
4936 legacy_mode |= (1 << 0);
4937
4938 if (!request_region(0x170, 8, "libata")) {
4939 struct resource *conflict, res;
4940 res.start = 0x170;
4941 res.end = 0x170 + 8 - 1;
4942 conflict = ____request_resource(&ioport_resource, &res);
4943 if (!strcmp(conflict->name, "libata"))
4944 legacy_mode |= (1 << 1);
4945 else {
4946 disable_dev_on_err = 0;
4947 printk(KERN_WARNING "ata: 0x170 IDE port busy\n");
4948 }
4949 } else
4950 legacy_mode |= (1 << 1);
4951 }
4952
4953 /* we have legacy mode, but all ports are unavailable */
4954 if (legacy_mode == (1 << 3)) {
4955 rc = -EBUSY;
4956 goto err_out_regions;
4957 }
4958
4959 rc = pci_set_dma_mask(pdev, ATA_DMA_MASK);
4960 if (rc)
4961 goto err_out_regions;
4962 rc = pci_set_consistent_dma_mask(pdev, ATA_DMA_MASK);
4963 if (rc)
4964 goto err_out_regions;
4965
4966 if (legacy_mode) {
Alan Cox47a86592005-10-04 08:09:19 -04004967 if (legacy_mode & (1 << 0))
Jeff Garzik0f0d5192005-10-30 06:41:29 -05004968 probe_ent = ata_pci_init_legacy_port(pdev, port[0], 0);
Alan Cox47a86592005-10-04 08:09:19 -04004969 if (legacy_mode & (1 << 1))
Jeff Garzik0f0d5192005-10-30 06:41:29 -05004970 probe_ent2 = ata_pci_init_legacy_port(pdev, port[1], 1);
Alan Cox47a86592005-10-04 08:09:19 -04004971 } else {
4972 if (n_ports == 2)
4973 probe_ent = ata_pci_init_native_mode(pdev, port, ATA_PORT_PRIMARY | ATA_PORT_SECONDARY);
4974 else
4975 probe_ent = ata_pci_init_native_mode(pdev, port, ATA_PORT_PRIMARY);
4976 }
4977 if (!probe_ent && !probe_ent2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004978 rc = -ENOMEM;
4979 goto err_out_regions;
4980 }
4981
4982 pci_set_master(pdev);
4983
4984 /* FIXME: check ata_device_add return */
4985 if (legacy_mode) {
4986 if (legacy_mode & (1 << 0))
4987 ata_device_add(probe_ent);
4988 if (legacy_mode & (1 << 1))
4989 ata_device_add(probe_ent2);
4990 } else
4991 ata_device_add(probe_ent);
4992
4993 kfree(probe_ent);
4994 kfree(probe_ent2);
4995
4996 return 0;
4997
4998err_out_regions:
4999 if (legacy_mode & (1 << 0))
5000 release_region(0x1f0, 8);
5001 if (legacy_mode & (1 << 1))
5002 release_region(0x170, 8);
5003 pci_release_regions(pdev);
5004err_out:
5005 if (disable_dev_on_err)
5006 pci_disable_device(pdev);
5007 return rc;
5008}
5009
5010/**
5011 * ata_pci_remove_one - PCI layer callback for device removal
5012 * @pdev: PCI device that was removed
5013 *
5014 * PCI layer indicates to libata via this hook that
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04005015 * hot-unplug or module unload event has occurred.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005016 * Handle this by unregistering all objects associated
5017 * with this PCI device. Free those objects. Then finally
5018 * release PCI resources and disable device.
5019 *
5020 * LOCKING:
5021 * Inherited from PCI layer (may sleep).
5022 */
5023
5024void ata_pci_remove_one (struct pci_dev *pdev)
5025{
5026 struct device *dev = pci_dev_to_dev(pdev);
5027 struct ata_host_set *host_set = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005028
Alan Cox17b14452005-09-15 15:44:00 +01005029 ata_host_set_remove(host_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005030 pci_release_regions(pdev);
5031 pci_disable_device(pdev);
5032 dev_set_drvdata(dev, NULL);
5033}
5034
5035/* move to PCI subsystem */
Jeff Garzik057ace52005-10-22 14:27:05 -04005036int pci_test_config_bits(struct pci_dev *pdev, const struct pci_bits *bits)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005037{
5038 unsigned long tmp = 0;
5039
5040 switch (bits->width) {
5041 case 1: {
5042 u8 tmp8 = 0;
5043 pci_read_config_byte(pdev, bits->reg, &tmp8);
5044 tmp = tmp8;
5045 break;
5046 }
5047 case 2: {
5048 u16 tmp16 = 0;
5049 pci_read_config_word(pdev, bits->reg, &tmp16);
5050 tmp = tmp16;
5051 break;
5052 }
5053 case 4: {
5054 u32 tmp32 = 0;
5055 pci_read_config_dword(pdev, bits->reg, &tmp32);
5056 tmp = tmp32;
5057 break;
5058 }
5059
5060 default:
5061 return -EINVAL;
5062 }
5063
5064 tmp &= bits->mask;
5065
5066 return (tmp == bits->val) ? 1 : 0;
5067}
Jens Axboe9b847542006-01-06 09:28:07 +01005068
5069int ata_pci_device_suspend(struct pci_dev *pdev, pm_message_t state)
5070{
5071 pci_save_state(pdev);
5072 pci_disable_device(pdev);
5073 pci_set_power_state(pdev, PCI_D3hot);
5074 return 0;
5075}
5076
5077int ata_pci_device_resume(struct pci_dev *pdev)
5078{
5079 pci_set_power_state(pdev, PCI_D0);
5080 pci_restore_state(pdev);
5081 pci_enable_device(pdev);
5082 pci_set_master(pdev);
5083 return 0;
5084}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005085#endif /* CONFIG_PCI */
5086
5087
Linus Torvalds1da177e2005-04-16 15:20:36 -07005088static int __init ata_init(void)
5089{
5090 ata_wq = create_workqueue("ata");
5091 if (!ata_wq)
5092 return -ENOMEM;
5093
5094 printk(KERN_DEBUG "libata version " DRV_VERSION " loaded.\n");
5095 return 0;
5096}
5097
5098static void __exit ata_exit(void)
5099{
5100 destroy_workqueue(ata_wq);
5101}
5102
5103module_init(ata_init);
5104module_exit(ata_exit);
5105
Jeff Garzik67846b32005-10-05 02:58:32 -04005106static unsigned long ratelimit_time;
5107static spinlock_t ata_ratelimit_lock = SPIN_LOCK_UNLOCKED;
5108
5109int ata_ratelimit(void)
5110{
5111 int rc;
5112 unsigned long flags;
5113
5114 spin_lock_irqsave(&ata_ratelimit_lock, flags);
5115
5116 if (time_after(jiffies, ratelimit_time)) {
5117 rc = 1;
5118 ratelimit_time = jiffies + (HZ/5);
5119 } else
5120 rc = 0;
5121
5122 spin_unlock_irqrestore(&ata_ratelimit_lock, flags);
5123
5124 return rc;
5125}
5126
Linus Torvalds1da177e2005-04-16 15:20:36 -07005127/*
5128 * libata is essentially a library of internal helper functions for
5129 * low-level ATA host controller drivers. As such, the API/ABI is
5130 * likely to change as new drivers are added and updated.
5131 * Do not depend on ABI/API stability.
5132 */
5133
5134EXPORT_SYMBOL_GPL(ata_std_bios_param);
5135EXPORT_SYMBOL_GPL(ata_std_ports);
5136EXPORT_SYMBOL_GPL(ata_device_add);
Alan Cox17b14452005-09-15 15:44:00 +01005137EXPORT_SYMBOL_GPL(ata_host_set_remove);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005138EXPORT_SYMBOL_GPL(ata_sg_init);
5139EXPORT_SYMBOL_GPL(ata_sg_init_one);
5140EXPORT_SYMBOL_GPL(ata_qc_complete);
5141EXPORT_SYMBOL_GPL(ata_qc_issue_prot);
5142EXPORT_SYMBOL_GPL(ata_eng_timeout);
5143EXPORT_SYMBOL_GPL(ata_tf_load);
5144EXPORT_SYMBOL_GPL(ata_tf_read);
5145EXPORT_SYMBOL_GPL(ata_noop_dev_select);
5146EXPORT_SYMBOL_GPL(ata_std_dev_select);
5147EXPORT_SYMBOL_GPL(ata_tf_to_fis);
5148EXPORT_SYMBOL_GPL(ata_tf_from_fis);
5149EXPORT_SYMBOL_GPL(ata_check_status);
5150EXPORT_SYMBOL_GPL(ata_altstatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005151EXPORT_SYMBOL_GPL(ata_exec_command);
5152EXPORT_SYMBOL_GPL(ata_port_start);
5153EXPORT_SYMBOL_GPL(ata_port_stop);
Jeff Garzikaa8f0dc2005-05-26 21:54:27 -04005154EXPORT_SYMBOL_GPL(ata_host_stop);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005155EXPORT_SYMBOL_GPL(ata_interrupt);
5156EXPORT_SYMBOL_GPL(ata_qc_prep);
5157EXPORT_SYMBOL_GPL(ata_bmdma_setup);
5158EXPORT_SYMBOL_GPL(ata_bmdma_start);
5159EXPORT_SYMBOL_GPL(ata_bmdma_irq_clear);
5160EXPORT_SYMBOL_GPL(ata_bmdma_status);
5161EXPORT_SYMBOL_GPL(ata_bmdma_stop);
5162EXPORT_SYMBOL_GPL(ata_port_probe);
5163EXPORT_SYMBOL_GPL(sata_phy_reset);
5164EXPORT_SYMBOL_GPL(__sata_phy_reset);
5165EXPORT_SYMBOL_GPL(ata_bus_reset);
5166EXPORT_SYMBOL_GPL(ata_port_disable);
Jeff Garzik67846b32005-10-05 02:58:32 -04005167EXPORT_SYMBOL_GPL(ata_ratelimit);
Tejun Heo6f8b9952006-01-24 17:05:21 +09005168EXPORT_SYMBOL_GPL(ata_busy_sleep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005169EXPORT_SYMBOL_GPL(ata_scsi_ioctl);
5170EXPORT_SYMBOL_GPL(ata_scsi_queuecmd);
5171EXPORT_SYMBOL_GPL(ata_scsi_error);
5172EXPORT_SYMBOL_GPL(ata_scsi_slave_config);
5173EXPORT_SYMBOL_GPL(ata_scsi_release);
5174EXPORT_SYMBOL_GPL(ata_host_intr);
5175EXPORT_SYMBOL_GPL(ata_dev_classify);
5176EXPORT_SYMBOL_GPL(ata_dev_id_string);
Brad Campbell6f2f3812005-05-12 15:07:47 -04005177EXPORT_SYMBOL_GPL(ata_dev_config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005178EXPORT_SYMBOL_GPL(ata_scsi_simulate);
Tejun Heoa72ec4c2006-01-23 13:09:37 +09005179EXPORT_SYMBOL_GPL(ata_eh_qc_complete);
5180EXPORT_SYMBOL_GPL(ata_eh_qc_retry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005181
Alan Cox1bc4ccf2006-01-09 17:18:14 +00005182EXPORT_SYMBOL_GPL(ata_pio_need_iordy);
Alan Cox452503f2005-10-21 19:01:32 -04005183EXPORT_SYMBOL_GPL(ata_timing_compute);
5184EXPORT_SYMBOL_GPL(ata_timing_merge);
5185
Linus Torvalds1da177e2005-04-16 15:20:36 -07005186#ifdef CONFIG_PCI
5187EXPORT_SYMBOL_GPL(pci_test_config_bits);
Jeff Garzik374b1872005-08-30 05:42:52 -04005188EXPORT_SYMBOL_GPL(ata_pci_host_stop);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005189EXPORT_SYMBOL_GPL(ata_pci_init_native_mode);
5190EXPORT_SYMBOL_GPL(ata_pci_init_one);
5191EXPORT_SYMBOL_GPL(ata_pci_remove_one);
Jens Axboe9b847542006-01-06 09:28:07 +01005192EXPORT_SYMBOL_GPL(ata_pci_device_suspend);
5193EXPORT_SYMBOL_GPL(ata_pci_device_resume);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005194#endif /* CONFIG_PCI */
Jens Axboe9b847542006-01-06 09:28:07 +01005195
5196EXPORT_SYMBOL_GPL(ata_device_suspend);
5197EXPORT_SYMBOL_GPL(ata_device_resume);
5198EXPORT_SYMBOL_GPL(ata_scsi_device_suspend);
5199EXPORT_SYMBOL_GPL(ata_scsi_device_resume);