2 * libata-core.c - helper library for ATA
4 * Maintained by: Jeff Garzik <jgarzik@pobox.com>
5 * Please ALWAYS copy linux-ide@vger.kernel.org
8 * Copyright 2003-2004 Red Hat, Inc. All rights reserved.
9 * Copyright 2003-2004 Jeff Garzik
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)
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.
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.
27 * libata documentation is available via 'make {ps|pdf}docs',
28 * as Documentation/DocBook/libata.*
30 * Hardware documentation available from http://www.t13.org/ and
31 * http://www.sata-io.org/
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>
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>
51 #include <linux/jiffies.h>
52 #include <linux/scatterlist.h>
53 #include <scsi/scsi.h>
54 #include "scsi_priv.h"
55 #include <scsi/scsi_cmnd.h>
56 #include <scsi/scsi_host.h>
57 #include <linux/libata.h>
59 #include <asm/semaphore.h>
60 #include <asm/byteorder.h>
64 static unsigned int ata_busy_sleep (struct ata_port *ap,
65 unsigned long tmout_pat,
67 static void ata_dev_reread_id(struct ata_port *ap, struct ata_device *dev);
68 static void ata_dev_init_params(struct ata_port *ap, struct ata_device *dev);
69 static void ata_set_mode(struct ata_port *ap);
70 static void ata_dev_set_xfermode(struct ata_port *ap, struct ata_device *dev);
71 static unsigned int ata_get_mode_mask(const struct ata_port *ap, int shift);
72 static int fgb(u32 bitmap);
73 static int ata_choose_xfer_mode(const struct ata_port *ap,
75 unsigned int *xfer_shift_out);
76 static void __ata_qc_complete(struct ata_queued_cmd *qc);
78 static unsigned int ata_unique_id = 1;
79 static struct workqueue_struct *ata_wq;
81 int atapi_enabled = 0;
82 module_param(atapi_enabled, int, 0444);
83 MODULE_PARM_DESC(atapi_enabled, "Enable discovery of ATAPI devices (0=off, 1=on)");
85 MODULE_AUTHOR("Jeff Garzik");
86 MODULE_DESCRIPTION("Library module for ATA devices");
87 MODULE_LICENSE("GPL");
88 MODULE_VERSION(DRV_VERSION);
91 * ata_tf_load_pio - send taskfile registers to host controller
92 * @ap: Port to which output is sent
93 * @tf: ATA taskfile register set
95 * Outputs ATA taskfile to standard ATA host controller.
98 * Inherited from caller.
101 static void ata_tf_load_pio(struct ata_port *ap, const struct ata_taskfile *tf)
103 struct ata_ioports *ioaddr = &ap->ioaddr;
104 unsigned int is_addr = tf->flags & ATA_TFLAG_ISADDR;
106 if (tf->ctl != ap->last_ctl) {
107 outb(tf->ctl, ioaddr->ctl_addr);
108 ap->last_ctl = tf->ctl;
112 if (is_addr && (tf->flags & ATA_TFLAG_LBA48)) {
113 outb(tf->hob_feature, ioaddr->feature_addr);
114 outb(tf->hob_nsect, ioaddr->nsect_addr);
115 outb(tf->hob_lbal, ioaddr->lbal_addr);
116 outb(tf->hob_lbam, ioaddr->lbam_addr);
117 outb(tf->hob_lbah, ioaddr->lbah_addr);
118 VPRINTK("hob: feat 0x%X nsect 0x%X, lba 0x%X 0x%X 0x%X\n",
127 outb(tf->feature, ioaddr->feature_addr);
128 outb(tf->nsect, ioaddr->nsect_addr);
129 outb(tf->lbal, ioaddr->lbal_addr);
130 outb(tf->lbam, ioaddr->lbam_addr);
131 outb(tf->lbah, ioaddr->lbah_addr);
132 VPRINTK("feat 0x%X nsect 0x%X lba 0x%X 0x%X 0x%X\n",
140 if (tf->flags & ATA_TFLAG_DEVICE) {
141 outb(tf->device, ioaddr->device_addr);
142 VPRINTK("device 0x%X\n", tf->device);
149 * ata_tf_load_mmio - send taskfile registers to host controller
150 * @ap: Port to which output is sent
151 * @tf: ATA taskfile register set
153 * Outputs ATA taskfile to standard ATA host controller using MMIO.
156 * Inherited from caller.
159 static void ata_tf_load_mmio(struct ata_port *ap, const struct ata_taskfile *tf)
161 struct ata_ioports *ioaddr = &ap->ioaddr;
162 unsigned int is_addr = tf->flags & ATA_TFLAG_ISADDR;
164 if (tf->ctl != ap->last_ctl) {
165 writeb(tf->ctl, (void __iomem *) ap->ioaddr.ctl_addr);
166 ap->last_ctl = tf->ctl;
170 if (is_addr && (tf->flags & ATA_TFLAG_LBA48)) {
171 writeb(tf->hob_feature, (void __iomem *) ioaddr->feature_addr);
172 writeb(tf->hob_nsect, (void __iomem *) ioaddr->nsect_addr);
173 writeb(tf->hob_lbal, (void __iomem *) ioaddr->lbal_addr);
174 writeb(tf->hob_lbam, (void __iomem *) ioaddr->lbam_addr);
175 writeb(tf->hob_lbah, (void __iomem *) ioaddr->lbah_addr);
176 VPRINTK("hob: feat 0x%X nsect 0x%X, lba 0x%X 0x%X 0x%X\n",
185 writeb(tf->feature, (void __iomem *) ioaddr->feature_addr);
186 writeb(tf->nsect, (void __iomem *) ioaddr->nsect_addr);
187 writeb(tf->lbal, (void __iomem *) ioaddr->lbal_addr);
188 writeb(tf->lbam, (void __iomem *) ioaddr->lbam_addr);
189 writeb(tf->lbah, (void __iomem *) ioaddr->lbah_addr);
190 VPRINTK("feat 0x%X nsect 0x%X lba 0x%X 0x%X 0x%X\n",
198 if (tf->flags & ATA_TFLAG_DEVICE) {
199 writeb(tf->device, (void __iomem *) ioaddr->device_addr);
200 VPRINTK("device 0x%X\n", tf->device);
208 * ata_tf_load - send taskfile registers to host controller
209 * @ap: Port to which output is sent
210 * @tf: ATA taskfile register set
212 * Outputs ATA taskfile to standard ATA host controller using MMIO
213 * or PIO as indicated by the ATA_FLAG_MMIO flag.
214 * Writes the control, feature, nsect, lbal, lbam, and lbah registers.
215 * Optionally (ATA_TFLAG_LBA48) writes hob_feature, hob_nsect,
216 * hob_lbal, hob_lbam, and hob_lbah.
218 * This function waits for idle (!BUSY and !DRQ) after writing
219 * registers. If the control register has a new value, this
220 * function also waits for idle after writing control and before
221 * writing the remaining registers.
223 * May be used as the tf_load() entry in ata_port_operations.
226 * Inherited from caller.
228 void ata_tf_load(struct ata_port *ap, const struct ata_taskfile *tf)
230 if (ap->flags & ATA_FLAG_MMIO)
231 ata_tf_load_mmio(ap, tf);
233 ata_tf_load_pio(ap, tf);
237 * ata_exec_command_pio - issue ATA command to host controller
238 * @ap: port to which command is being issued
239 * @tf: ATA taskfile register set
241 * Issues PIO write to ATA command register, with proper
242 * synchronization with interrupt handler / other threads.
245 * spin_lock_irqsave(host_set lock)
248 static void ata_exec_command_pio(struct ata_port *ap, const struct ata_taskfile *tf)
250 DPRINTK("ata%u: cmd 0x%X\n", ap->id, tf->command);
252 outb(tf->command, ap->ioaddr.command_addr);
258 * ata_exec_command_mmio - issue ATA command to host controller
259 * @ap: port to which command is being issued
260 * @tf: ATA taskfile register set
262 * Issues MMIO write to ATA command register, with proper
263 * synchronization with interrupt handler / other threads.
266 * spin_lock_irqsave(host_set lock)
269 static void ata_exec_command_mmio(struct ata_port *ap, const struct ata_taskfile *tf)
271 DPRINTK("ata%u: cmd 0x%X\n", ap->id, tf->command);
273 writeb(tf->command, (void __iomem *) ap->ioaddr.command_addr);
279 * ata_exec_command - issue ATA command to host controller
280 * @ap: port to which command is being issued
281 * @tf: ATA taskfile register set
283 * Issues PIO/MMIO write to ATA command register, with proper
284 * synchronization with interrupt handler / other threads.
287 * spin_lock_irqsave(host_set lock)
289 void ata_exec_command(struct ata_port *ap, const struct ata_taskfile *tf)
291 if (ap->flags & ATA_FLAG_MMIO)
292 ata_exec_command_mmio(ap, tf);
294 ata_exec_command_pio(ap, tf);
298 * ata_tf_to_host - issue ATA taskfile to host controller
299 * @ap: port to which command is being issued
300 * @tf: ATA taskfile register set
302 * Issues ATA taskfile register set to ATA host controller,
303 * with proper synchronization with interrupt handler and
307 * spin_lock_irqsave(host_set lock)
310 static inline void ata_tf_to_host(struct ata_port *ap,
311 const struct ata_taskfile *tf)
313 ap->ops->tf_load(ap, tf);
314 ap->ops->exec_command(ap, tf);
318 * ata_tf_read_pio - input device's ATA taskfile shadow registers
319 * @ap: Port from which input is read
320 * @tf: ATA taskfile register set for storing input
322 * Reads ATA taskfile registers for currently-selected device
326 * Inherited from caller.
329 static void ata_tf_read_pio(struct ata_port *ap, struct ata_taskfile *tf)
331 struct ata_ioports *ioaddr = &ap->ioaddr;
333 tf->command = ata_check_status(ap);
334 tf->feature = inb(ioaddr->error_addr);
335 tf->nsect = inb(ioaddr->nsect_addr);
336 tf->lbal = inb(ioaddr->lbal_addr);
337 tf->lbam = inb(ioaddr->lbam_addr);
338 tf->lbah = inb(ioaddr->lbah_addr);
339 tf->device = inb(ioaddr->device_addr);
341 if (tf->flags & ATA_TFLAG_LBA48) {
342 outb(tf->ctl | ATA_HOB, ioaddr->ctl_addr);
343 tf->hob_feature = inb(ioaddr->error_addr);
344 tf->hob_nsect = inb(ioaddr->nsect_addr);
345 tf->hob_lbal = inb(ioaddr->lbal_addr);
346 tf->hob_lbam = inb(ioaddr->lbam_addr);
347 tf->hob_lbah = inb(ioaddr->lbah_addr);
352 * ata_tf_read_mmio - input device's ATA taskfile shadow registers
353 * @ap: Port from which input is read
354 * @tf: ATA taskfile register set for storing input
356 * Reads ATA taskfile registers for currently-selected device
360 * Inherited from caller.
363 static void ata_tf_read_mmio(struct ata_port *ap, struct ata_taskfile *tf)
365 struct ata_ioports *ioaddr = &ap->ioaddr;
367 tf->command = ata_check_status(ap);
368 tf->feature = readb((void __iomem *)ioaddr->error_addr);
369 tf->nsect = readb((void __iomem *)ioaddr->nsect_addr);
370 tf->lbal = readb((void __iomem *)ioaddr->lbal_addr);
371 tf->lbam = readb((void __iomem *)ioaddr->lbam_addr);
372 tf->lbah = readb((void __iomem *)ioaddr->lbah_addr);
373 tf->device = readb((void __iomem *)ioaddr->device_addr);
375 if (tf->flags & ATA_TFLAG_LBA48) {
376 writeb(tf->ctl | ATA_HOB, (void __iomem *) ap->ioaddr.ctl_addr);
377 tf->hob_feature = readb((void __iomem *)ioaddr->error_addr);
378 tf->hob_nsect = readb((void __iomem *)ioaddr->nsect_addr);
379 tf->hob_lbal = readb((void __iomem *)ioaddr->lbal_addr);
380 tf->hob_lbam = readb((void __iomem *)ioaddr->lbam_addr);
381 tf->hob_lbah = readb((void __iomem *)ioaddr->lbah_addr);
387 * ata_tf_read - input device's ATA taskfile shadow registers
388 * @ap: Port from which input is read
389 * @tf: ATA taskfile register set for storing input
391 * Reads ATA taskfile registers for currently-selected device
394 * Reads nsect, lbal, lbam, lbah, and device. If ATA_TFLAG_LBA48
395 * is set, also reads the hob registers.
397 * May be used as the tf_read() entry in ata_port_operations.
400 * Inherited from caller.
402 void ata_tf_read(struct ata_port *ap, struct ata_taskfile *tf)
404 if (ap->flags & ATA_FLAG_MMIO)
405 ata_tf_read_mmio(ap, tf);
407 ata_tf_read_pio(ap, tf);
411 * ata_check_status_pio - Read device status reg & clear interrupt
412 * @ap: port where the device is
414 * Reads ATA taskfile status register for currently-selected device
415 * and return its value. This also clears pending interrupts
419 * Inherited from caller.
421 static u8 ata_check_status_pio(struct ata_port *ap)
423 return inb(ap->ioaddr.status_addr);
427 * ata_check_status_mmio - Read device status reg & clear interrupt
428 * @ap: port where the device is
430 * Reads ATA taskfile status register for currently-selected device
431 * via MMIO and return its value. This also clears pending interrupts
435 * Inherited from caller.
437 static u8 ata_check_status_mmio(struct ata_port *ap)
439 return readb((void __iomem *) ap->ioaddr.status_addr);
444 * ata_check_status - Read device status reg & clear interrupt
445 * @ap: port where the device is
447 * Reads ATA taskfile status register for currently-selected device
448 * and return its value. This also clears pending interrupts
451 * May be used as the check_status() entry in ata_port_operations.
454 * Inherited from caller.
456 u8 ata_check_status(struct ata_port *ap)
458 if (ap->flags & ATA_FLAG_MMIO)
459 return ata_check_status_mmio(ap);
460 return ata_check_status_pio(ap);
465 * ata_altstatus - Read device alternate status reg
466 * @ap: port where the device is
468 * Reads ATA taskfile alternate status register for
469 * currently-selected device and return its value.
471 * Note: may NOT be used as the check_altstatus() entry in
472 * ata_port_operations.
475 * Inherited from caller.
477 u8 ata_altstatus(struct ata_port *ap)
479 if (ap->ops->check_altstatus)
480 return ap->ops->check_altstatus(ap);
482 if (ap->flags & ATA_FLAG_MMIO)
483 return readb((void __iomem *)ap->ioaddr.altstatus_addr);
484 return inb(ap->ioaddr.altstatus_addr);
489 * ata_tf_to_fis - Convert ATA taskfile to SATA FIS structure
490 * @tf: Taskfile to convert
491 * @fis: Buffer into which data will output
492 * @pmp: Port multiplier port
494 * Converts a standard ATA taskfile to a Serial ATA
495 * FIS structure (Register - Host to Device).
498 * Inherited from caller.
501 void ata_tf_to_fis(const struct ata_taskfile *tf, u8 *fis, u8 pmp)
503 fis[0] = 0x27; /* Register - Host to Device FIS */
504 fis[1] = (pmp & 0xf) | (1 << 7); /* Port multiplier number,
505 bit 7 indicates Command FIS */
506 fis[2] = tf->command;
507 fis[3] = tf->feature;
514 fis[8] = tf->hob_lbal;
515 fis[9] = tf->hob_lbam;
516 fis[10] = tf->hob_lbah;
517 fis[11] = tf->hob_feature;
520 fis[13] = tf->hob_nsect;
531 * ata_tf_from_fis - Convert SATA FIS to ATA taskfile
532 * @fis: Buffer from which data will be input
533 * @tf: Taskfile to output
535 * Converts a serial ATA FIS structure to a standard ATA taskfile.
538 * Inherited from caller.
541 void ata_tf_from_fis(const u8 *fis, struct ata_taskfile *tf)
543 tf->command = fis[2]; /* status */
544 tf->feature = fis[3]; /* error */
551 tf->hob_lbal = fis[8];
552 tf->hob_lbam = fis[9];
553 tf->hob_lbah = fis[10];
556 tf->hob_nsect = fis[13];
559 static const u8 ata_rw_cmds[] = {
563 ATA_CMD_READ_MULTI_EXT,
564 ATA_CMD_WRITE_MULTI_EXT,
568 ATA_CMD_WRITE_MULTI_FUA_EXT,
572 ATA_CMD_PIO_READ_EXT,
573 ATA_CMD_PIO_WRITE_EXT,
586 ATA_CMD_WRITE_FUA_EXT
590 * ata_rwcmd_protocol - set taskfile r/w commands and protocol
591 * @qc: command to examine and configure
593 * Examine the device configuration and tf->flags to calculate
594 * the proper read/write commands and protocol to use.
599 int ata_rwcmd_protocol(struct ata_queued_cmd *qc)
601 struct ata_taskfile *tf = &qc->tf;
602 struct ata_device *dev = qc->dev;
605 int index, fua, lba48, write;
607 fua = (tf->flags & ATA_TFLAG_FUA) ? 4 : 0;
608 lba48 = (tf->flags & ATA_TFLAG_LBA48) ? 2 : 0;
609 write = (tf->flags & ATA_TFLAG_WRITE) ? 1 : 0;
611 if (dev->flags & ATA_DFLAG_PIO) {
612 tf->protocol = ATA_PROT_PIO;
613 index = dev->multi_count ? 0 : 8;
614 } else if (lba48 && (qc->ap->flags & ATA_FLAG_PIO_LBA48)) {
615 /* Unable to use DMA due to host limitation */
616 tf->protocol = ATA_PROT_PIO;
617 index = dev->multi_count ? 0 : 4;
619 tf->protocol = ATA_PROT_DMA;
623 cmd = ata_rw_cmds[index + fua + lba48 + write];
631 static const char * const xfer_mode_str[] = {
651 * ata_udma_string - convert UDMA bit offset to string
652 * @mask: mask of bits supported; only highest bit counts.
654 * Determine string which represents the highest speed
655 * (highest bit in @udma_mask).
661 * Constant C string representing highest speed listed in
662 * @udma_mask, or the constant C string "<n/a>".
665 static const char *ata_mode_string(unsigned int mask)
669 for (i = 7; i >= 0; i--)
672 for (i = ATA_SHIFT_MWDMA + 2; i >= ATA_SHIFT_MWDMA; i--)
675 for (i = ATA_SHIFT_PIO + 4; i >= ATA_SHIFT_PIO; i--)
682 return xfer_mode_str[i];
686 * ata_pio_devchk - PATA device presence detection
687 * @ap: ATA channel to examine
688 * @device: Device to examine (starting at zero)
690 * This technique was originally described in
691 * Hale Landis's ATADRVR (www.ata-atapi.com), and
692 * later found its way into the ATA/ATAPI spec.
694 * Write a pattern to the ATA shadow registers,
695 * and if a device is present, it will respond by
696 * correctly storing and echoing back the
697 * ATA shadow register contents.
703 static unsigned int ata_pio_devchk(struct ata_port *ap,
706 struct ata_ioports *ioaddr = &ap->ioaddr;
709 ap->ops->dev_select(ap, device);
711 outb(0x55, ioaddr->nsect_addr);
712 outb(0xaa, ioaddr->lbal_addr);
714 outb(0xaa, ioaddr->nsect_addr);
715 outb(0x55, ioaddr->lbal_addr);
717 outb(0x55, ioaddr->nsect_addr);
718 outb(0xaa, ioaddr->lbal_addr);
720 nsect = inb(ioaddr->nsect_addr);
721 lbal = inb(ioaddr->lbal_addr);
723 if ((nsect == 0x55) && (lbal == 0xaa))
724 return 1; /* we found a device */
726 return 0; /* nothing found */
730 * ata_mmio_devchk - PATA device presence detection
731 * @ap: ATA channel to examine
732 * @device: Device to examine (starting at zero)
734 * This technique was originally described in
735 * Hale Landis's ATADRVR (www.ata-atapi.com), and
736 * later found its way into the ATA/ATAPI spec.
738 * Write a pattern to the ATA shadow registers,
739 * and if a device is present, it will respond by
740 * correctly storing and echoing back the
741 * ATA shadow register contents.
747 static unsigned int ata_mmio_devchk(struct ata_port *ap,
750 struct ata_ioports *ioaddr = &ap->ioaddr;
753 ap->ops->dev_select(ap, device);
755 writeb(0x55, (void __iomem *) ioaddr->nsect_addr);
756 writeb(0xaa, (void __iomem *) ioaddr->lbal_addr);
758 writeb(0xaa, (void __iomem *) ioaddr->nsect_addr);
759 writeb(0x55, (void __iomem *) ioaddr->lbal_addr);
761 writeb(0x55, (void __iomem *) ioaddr->nsect_addr);
762 writeb(0xaa, (void __iomem *) ioaddr->lbal_addr);
764 nsect = readb((void __iomem *) ioaddr->nsect_addr);
765 lbal = readb((void __iomem *) ioaddr->lbal_addr);
767 if ((nsect == 0x55) && (lbal == 0xaa))
768 return 1; /* we found a device */
770 return 0; /* nothing found */
774 * ata_devchk - PATA device presence detection
775 * @ap: ATA channel to examine
776 * @device: Device to examine (starting at zero)
778 * Dispatch ATA device presence detection, depending
779 * on whether we are using PIO or MMIO to talk to the
780 * ATA shadow registers.
786 static unsigned int ata_devchk(struct ata_port *ap,
789 if (ap->flags & ATA_FLAG_MMIO)
790 return ata_mmio_devchk(ap, device);
791 return ata_pio_devchk(ap, device);
795 * ata_dev_classify - determine device type based on ATA-spec signature
796 * @tf: ATA taskfile register set for device to be identified
798 * Determine from taskfile register contents whether a device is
799 * ATA or ATAPI, as per "Signature and persistence" section
800 * of ATA/PI spec (volume 1, sect 5.14).
806 * Device type, %ATA_DEV_ATA, %ATA_DEV_ATAPI, or %ATA_DEV_UNKNOWN
807 * the event of failure.
810 unsigned int ata_dev_classify(const struct ata_taskfile *tf)
812 /* Apple's open source Darwin code hints that some devices only
813 * put a proper signature into the LBA mid/high registers,
814 * So, we only check those. It's sufficient for uniqueness.
817 if (((tf->lbam == 0) && (tf->lbah == 0)) ||
818 ((tf->lbam == 0x3c) && (tf->lbah == 0xc3))) {
819 DPRINTK("found ATA device by sig\n");
823 if (((tf->lbam == 0x14) && (tf->lbah == 0xeb)) ||
824 ((tf->lbam == 0x69) && (tf->lbah == 0x96))) {
825 DPRINTK("found ATAPI device by sig\n");
826 return ATA_DEV_ATAPI;
829 DPRINTK("unknown device\n");
830 return ATA_DEV_UNKNOWN;
834 * ata_dev_try_classify - Parse returned ATA device signature
835 * @ap: ATA channel to examine
836 * @device: Device to examine (starting at zero)
838 * After an event -- SRST, E.D.D., or SATA COMRESET -- occurs,
839 * an ATA/ATAPI-defined set of values is placed in the ATA
840 * shadow registers, indicating the results of device detection
843 * Select the ATA device, and read the values from the ATA shadow
844 * registers. Then parse according to the Error register value,
845 * and the spec-defined values examined by ata_dev_classify().
851 static u8 ata_dev_try_classify(struct ata_port *ap, unsigned int device)
853 struct ata_device *dev = &ap->device[device];
854 struct ata_taskfile tf;
858 ap->ops->dev_select(ap, device);
860 memset(&tf, 0, sizeof(tf));
862 ap->ops->tf_read(ap, &tf);
865 dev->class = ATA_DEV_NONE;
867 /* see if device passed diags */
870 else if ((device == 0) && (err == 0x81))
875 /* determine if device if ATA or ATAPI */
876 class = ata_dev_classify(&tf);
877 if (class == ATA_DEV_UNKNOWN)
879 if ((class == ATA_DEV_ATA) && (ata_chk_status(ap) == 0))
888 * ata_dev_id_string - Convert IDENTIFY DEVICE page into string
889 * @id: IDENTIFY DEVICE results we will examine
890 * @s: string into which data is output
891 * @ofs: offset into identify device page
892 * @len: length of string to return. must be an even number.
894 * The strings in the IDENTIFY DEVICE page are broken up into
895 * 16-bit chunks. Run through the string, and output each
896 * 8-bit chunk linearly, regardless of platform.
902 void ata_dev_id_string(const u16 *id, unsigned char *s,
903 unsigned int ofs, unsigned int len)
923 * ata_noop_dev_select - Select device 0/1 on ATA bus
924 * @ap: ATA channel to manipulate
925 * @device: ATA device (numbered from zero) to select
927 * This function performs no actual function.
929 * May be used as the dev_select() entry in ata_port_operations.
934 void ata_noop_dev_select (struct ata_port *ap, unsigned int device)
940 * ata_std_dev_select - Select device 0/1 on ATA bus
941 * @ap: ATA channel to manipulate
942 * @device: ATA device (numbered from zero) to select
944 * Use the method defined in the ATA specification to
945 * make either device 0, or device 1, active on the
946 * ATA channel. Works with both PIO and MMIO.
948 * May be used as the dev_select() entry in ata_port_operations.
954 void ata_std_dev_select (struct ata_port *ap, unsigned int device)
959 tmp = ATA_DEVICE_OBS;
961 tmp = ATA_DEVICE_OBS | ATA_DEV1;
963 if (ap->flags & ATA_FLAG_MMIO) {
964 writeb(tmp, (void __iomem *) ap->ioaddr.device_addr);
966 outb(tmp, ap->ioaddr.device_addr);
968 ata_pause(ap); /* needed; also flushes, for mmio */
972 * ata_dev_select - Select device 0/1 on ATA bus
973 * @ap: ATA channel to manipulate
974 * @device: ATA device (numbered from zero) to select
975 * @wait: non-zero to wait for Status register BSY bit to clear
976 * @can_sleep: non-zero if context allows sleeping
978 * Use the method defined in the ATA specification to
979 * make either device 0, or device 1, active on the
982 * This is a high-level version of ata_std_dev_select(),
983 * which additionally provides the services of inserting
984 * the proper pauses and status polling, where needed.
990 void ata_dev_select(struct ata_port *ap, unsigned int device,
991 unsigned int wait, unsigned int can_sleep)
993 VPRINTK("ENTER, ata%u: device %u, wait %u\n",
994 ap->id, device, wait);
999 ap->ops->dev_select(ap, device);
1002 if (can_sleep && ap->device[device].class == ATA_DEV_ATAPI)
1009 * ata_dump_id - IDENTIFY DEVICE info debugging output
1010 * @dev: Device whose IDENTIFY DEVICE page we will dump
1012 * Dump selected 16-bit words from a detected device's
1013 * IDENTIFY PAGE page.
1019 static inline void ata_dump_id(const struct ata_device *dev)
1021 DPRINTK("49==0x%04x "
1031 DPRINTK("80==0x%04x "
1041 DPRINTK("88==0x%04x "
1048 * Compute the PIO modes available for this device. This is not as
1049 * trivial as it seems if we must consider early devices correctly.
1051 * FIXME: pre IDE drive timing (do we care ?).
1054 static unsigned int ata_pio_modes(const struct ata_device *adev)
1058 /* Usual case. Word 53 indicates word 64 is valid */
1059 if (adev->id[ATA_ID_FIELD_VALID] & (1 << 1)) {
1060 modes = adev->id[ATA_ID_PIO_MODES] & 0x03;
1066 /* If word 64 isn't valid then Word 51 high byte holds the PIO timing
1067 number for the maximum. Turn it into a mask and return it */
1068 modes = (2 << ((adev->id[ATA_ID_OLD_PIO_MODES] >> 8) & 0xFF)) - 1 ;
1070 /* But wait.. there's more. Design your standards by committee and
1071 you too can get a free iordy field to process. However its the
1072 speeds not the modes that are supported... Note drivers using the
1073 timing API will get this right anyway */
1076 struct ata_exec_internal_arg {
1077 unsigned int err_mask;
1078 struct ata_taskfile *tf;
1079 struct completion *waiting;
1082 int ata_qc_complete_internal(struct ata_queued_cmd *qc)
1084 struct ata_exec_internal_arg *arg = qc->private_data;
1085 struct completion *waiting = arg->waiting;
1087 if (!(qc->err_mask & ~AC_ERR_DEV))
1088 qc->ap->ops->tf_read(qc->ap, arg->tf);
1089 arg->err_mask = qc->err_mask;
1090 arg->waiting = NULL;
1097 * ata_exec_internal - execute libata internal command
1098 * @ap: Port to which the command is sent
1099 * @dev: Device to which the command is sent
1100 * @tf: Taskfile registers for the command and the result
1101 * @dma_dir: Data tranfer direction of the command
1102 * @buf: Data buffer of the command
1103 * @buflen: Length of data buffer
1105 * Executes libata internal command with timeout. @tf contains
1106 * command on entry and result on return. Timeout and error
1107 * conditions are reported via return value. No recovery action
1108 * is taken after a command times out. It's caller's duty to
1109 * clean up after timeout.
1112 * None. Should be called with kernel context, might sleep.
1116 ata_exec_internal(struct ata_port *ap, struct ata_device *dev,
1117 struct ata_taskfile *tf,
1118 int dma_dir, void *buf, unsigned int buflen)
1120 u8 command = tf->command;
1121 struct ata_queued_cmd *qc;
1122 DECLARE_COMPLETION(wait);
1123 unsigned long flags;
1124 struct ata_exec_internal_arg arg;
1126 spin_lock_irqsave(&ap->host_set->lock, flags);
1128 qc = ata_qc_new_init(ap, dev);
1132 qc->dma_dir = dma_dir;
1133 if (dma_dir != DMA_NONE) {
1134 ata_sg_init_one(qc, buf, buflen);
1135 qc->nsect = buflen / ATA_SECT_SIZE;
1138 arg.waiting = &wait;
1140 qc->private_data = &arg;
1141 qc->complete_fn = ata_qc_complete_internal;
1143 if (ata_qc_issue(qc))
1146 spin_unlock_irqrestore(&ap->host_set->lock, flags);
1148 if (!wait_for_completion_timeout(&wait, ATA_TMOUT_INTERNAL)) {
1149 spin_lock_irqsave(&ap->host_set->lock, flags);
1151 /* We're racing with irq here. If we lose, the
1152 * following test prevents us from completing the qc
1153 * again. If completion irq occurs after here but
1154 * before the caller cleans up, it will result in a
1155 * spurious interrupt. We can live with that.
1158 qc->err_mask = AC_ERR_OTHER;
1159 ata_qc_complete(qc);
1160 printk(KERN_WARNING "ata%u: qc timeout (cmd 0x%x)\n",
1164 spin_unlock_irqrestore(&ap->host_set->lock, flags);
1167 return arg.err_mask;
1171 spin_unlock_irqrestore(&ap->host_set->lock, flags);
1172 return AC_ERR_OTHER;
1176 * ata_pio_need_iordy - check if iordy needed
1179 * Check if the current speed of the device requires IORDY. Used
1180 * by various controllers for chip configuration.
1183 unsigned int ata_pio_need_iordy(const struct ata_device *adev)
1186 int speed = adev->pio_mode - XFER_PIO_0;
1193 /* If we have no drive specific rule, then PIO 2 is non IORDY */
1195 if (adev->id[ATA_ID_FIELD_VALID] & 2) { /* EIDE */
1196 pio = adev->id[ATA_ID_EIDE_PIO];
1197 /* Is the speed faster than the drive allows non IORDY ? */
1199 /* This is cycle times not frequency - watch the logic! */
1200 if (pio > 240) /* PIO2 is 240nS per cycle */
1209 * ata_dev_identify - obtain IDENTIFY x DEVICE page
1210 * @ap: port on which device we wish to probe resides
1211 * @device: device bus address, starting at zero
1213 * Following bus reset, we issue the IDENTIFY [PACKET] DEVICE
1214 * command, and read back the 512-byte device information page.
1215 * The device information page is fed to us via the standard
1216 * PIO-IN protocol, but we hand-code it here. (TODO: investigate
1217 * using standard PIO-IN paths)
1219 * After reading the device information page, we use several
1220 * bits of information from it to initialize data structures
1221 * that will be used during the lifetime of the ata_device.
1222 * Other data from the info page is used to disqualify certain
1223 * older ATA devices we do not wish to support.
1226 * Inherited from caller. Some functions called by this function
1227 * obtain the host_set lock.
1230 static void ata_dev_identify(struct ata_port *ap, unsigned int device)
1232 struct ata_device *dev = &ap->device[device];
1233 unsigned int major_version;
1235 unsigned long xfer_modes;
1236 unsigned int using_edd;
1237 struct ata_taskfile tf;
1238 unsigned int err_mask;
1241 if (!ata_dev_present(dev)) {
1242 DPRINTK("ENTER/EXIT (host %u, dev %u) -- nodev\n",
1247 if (ap->flags & (ATA_FLAG_SRST | ATA_FLAG_SATA_RESET))
1252 DPRINTK("ENTER, host %u, dev %u\n", ap->id, device);
1254 assert (dev->class == ATA_DEV_ATA || dev->class == ATA_DEV_ATAPI ||
1255 dev->class == ATA_DEV_NONE);
1257 ata_dev_select(ap, device, 1, 1); /* select device 0/1 */
1260 ata_tf_init(ap, &tf, device);
1262 if (dev->class == ATA_DEV_ATA) {
1263 tf.command = ATA_CMD_ID_ATA;
1264 DPRINTK("do ATA identify\n");
1266 tf.command = ATA_CMD_ID_ATAPI;
1267 DPRINTK("do ATAPI identify\n");
1270 tf.protocol = ATA_PROT_PIO;
1272 err_mask = ata_exec_internal(ap, dev, &tf, DMA_FROM_DEVICE,
1273 dev->id, sizeof(dev->id));
1276 if (err_mask & ~AC_ERR_DEV)
1280 * arg! EDD works for all test cases, but seems to return
1281 * the ATA signature for some ATAPI devices. Until the
1282 * reason for this is found and fixed, we fix up the mess
1283 * here. If IDENTIFY DEVICE returns command aborted
1284 * (as ATAPI devices do), then we issue an
1285 * IDENTIFY PACKET DEVICE.
1287 * ATA software reset (SRST, the default) does not appear
1288 * to have this problem.
1290 if ((using_edd) && (dev->class == ATA_DEV_ATA)) {
1291 u8 err = tf.feature;
1292 if (err & ATA_ABORTED) {
1293 dev->class = ATA_DEV_ATAPI;
1300 swap_buf_le16(dev->id, ATA_ID_WORDS);
1302 /* print device capabilities */
1303 printk(KERN_DEBUG "ata%u: dev %u cfg "
1304 "49:%04x 82:%04x 83:%04x 84:%04x 85:%04x 86:%04x 87:%04x 88:%04x\n",
1305 ap->id, device, dev->id[49],
1306 dev->id[82], dev->id[83], dev->id[84],
1307 dev->id[85], dev->id[86], dev->id[87],
1311 * common ATA, ATAPI feature tests
1314 /* we require DMA support (bits 8 of word 49) */
1315 if (!ata_id_has_dma(dev->id)) {
1316 printk(KERN_DEBUG "ata%u: no dma\n", ap->id);
1320 /* quick-n-dirty find max transfer mode; for printk only */
1321 xfer_modes = dev->id[ATA_ID_UDMA_MODES];
1323 xfer_modes = (dev->id[ATA_ID_MWDMA_MODES]) << ATA_SHIFT_MWDMA;
1325 xfer_modes = ata_pio_modes(dev);
1329 /* ATA-specific feature tests */
1330 if (dev->class == ATA_DEV_ATA) {
1331 if (!ata_id_is_ata(dev->id)) /* sanity check */
1334 /* get major version */
1335 tmp = dev->id[ATA_ID_MAJOR_VER];
1336 for (major_version = 14; major_version >= 1; major_version--)
1337 if (tmp & (1 << major_version))
1341 * The exact sequence expected by certain pre-ATA4 drives is:
1344 * INITIALIZE DEVICE PARAMETERS
1346 * Some drives were very specific about that exact sequence.
1348 if (major_version < 4 || (!ata_id_has_lba(dev->id))) {
1349 ata_dev_init_params(ap, dev);
1351 /* current CHS translation info (id[53-58]) might be
1352 * changed. reread the identify device info.
1354 ata_dev_reread_id(ap, dev);
1357 if (ata_id_has_lba(dev->id)) {
1358 dev->flags |= ATA_DFLAG_LBA;
1360 if (ata_id_has_lba48(dev->id)) {
1361 dev->flags |= ATA_DFLAG_LBA48;
1362 dev->n_sectors = ata_id_u64(dev->id, 100);
1364 dev->n_sectors = ata_id_u32(dev->id, 60);
1367 /* print device info to dmesg */
1368 printk(KERN_INFO "ata%u: dev %u ATA-%d, max %s, %Lu sectors:%s\n",
1371 ata_mode_string(xfer_modes),
1372 (unsigned long long)dev->n_sectors,
1373 dev->flags & ATA_DFLAG_LBA48 ? " LBA48" : " LBA");
1377 /* Default translation */
1378 dev->cylinders = dev->id[1];
1379 dev->heads = dev->id[3];
1380 dev->sectors = dev->id[6];
1381 dev->n_sectors = dev->cylinders * dev->heads * dev->sectors;
1383 if (ata_id_current_chs_valid(dev->id)) {
1384 /* Current CHS translation is valid. */
1385 dev->cylinders = dev->id[54];
1386 dev->heads = dev->id[55];
1387 dev->sectors = dev->id[56];
1389 dev->n_sectors = ata_id_u32(dev->id, 57);
1392 /* print device info to dmesg */
1393 printk(KERN_INFO "ata%u: dev %u ATA-%d, max %s, %Lu sectors: CHS %d/%d/%d\n",
1396 ata_mode_string(xfer_modes),
1397 (unsigned long long)dev->n_sectors,
1398 (int)dev->cylinders, (int)dev->heads, (int)dev->sectors);
1402 ap->host->max_cmd_len = 16;
1405 /* ATAPI-specific feature tests */
1406 else if (dev->class == ATA_DEV_ATAPI) {
1407 if (ata_id_is_ata(dev->id)) /* sanity check */
1410 rc = atapi_cdb_len(dev->id);
1411 if ((rc < 12) || (rc > ATAPI_CDB_LEN)) {
1412 printk(KERN_WARNING "ata%u: unsupported CDB len\n", ap->id);
1415 ap->cdb_len = (unsigned int) rc;
1416 ap->host->max_cmd_len = (unsigned char) ap->cdb_len;
1418 /* print device info to dmesg */
1419 printk(KERN_INFO "ata%u: dev %u ATAPI, max %s\n",
1421 ata_mode_string(xfer_modes));
1424 DPRINTK("EXIT, drv_stat = 0x%x\n", ata_chk_status(ap));
1428 printk(KERN_WARNING "ata%u: dev %u not supported, ignoring\n",
1431 dev->class++; /* converts ATA_DEV_xxx into ATA_DEV_xxx_UNSUP */
1432 DPRINTK("EXIT, err\n");
1436 static inline u8 ata_dev_knobble(const struct ata_port *ap)
1438 return ((ap->cbl == ATA_CBL_SATA) && (!ata_id_is_sata(ap->device->id)));
1442 * ata_dev_config - Run device specific handlers and check for
1443 * SATA->PATA bridges
1450 void ata_dev_config(struct ata_port *ap, unsigned int i)
1452 /* limit bridge transfers to udma5, 200 sectors */
1453 if (ata_dev_knobble(ap)) {
1454 printk(KERN_INFO "ata%u(%u): applying bridge limits\n",
1455 ap->id, ap->device->devno);
1456 ap->udma_mask &= ATA_UDMA5;
1457 ap->host->max_sectors = ATA_MAX_SECTORS;
1458 ap->host->hostt->max_sectors = ATA_MAX_SECTORS;
1459 ap->device[i].flags |= ATA_DFLAG_LOCK_SECTORS;
1462 if (ap->ops->dev_config)
1463 ap->ops->dev_config(ap, &ap->device[i]);
1467 * ata_bus_probe - Reset and probe ATA bus
1470 * Master ATA bus probing function. Initiates a hardware-dependent
1471 * bus reset, then attempts to identify any devices found on
1475 * PCI/etc. bus probe sem.
1478 * Zero on success, non-zero on error.
1481 static int ata_bus_probe(struct ata_port *ap)
1483 unsigned int i, found = 0;
1485 ap->ops->phy_reset(ap);
1486 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1489 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1490 ata_dev_identify(ap, i);
1491 if (ata_dev_present(&ap->device[i])) {
1493 ata_dev_config(ap,i);
1497 if ((!found) || (ap->flags & ATA_FLAG_PORT_DISABLED))
1498 goto err_out_disable;
1501 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1502 goto err_out_disable;
1507 ap->ops->port_disable(ap);
1513 * ata_port_probe - Mark port as enabled
1514 * @ap: Port for which we indicate enablement
1516 * Modify @ap data structure such that the system
1517 * thinks that the entire port is enabled.
1519 * LOCKING: host_set lock, or some other form of
1523 void ata_port_probe(struct ata_port *ap)
1525 ap->flags &= ~ATA_FLAG_PORT_DISABLED;
1529 * sata_print_link_status - Print SATA link status
1530 * @ap: SATA port to printk link status about
1532 * This function prints link speed and status of a SATA link.
1537 static void sata_print_link_status(struct ata_port *ap)
1542 if (!ap->ops->scr_read)
1545 sstatus = scr_read(ap, SCR_STATUS);
1547 if (sata_dev_present(ap)) {
1548 tmp = (sstatus >> 4) & 0xf;
1551 else if (tmp & (1 << 1))
1554 speed = "<unknown>";
1555 printk(KERN_INFO "ata%u: SATA link up %s Gbps (SStatus %X)\n",
1556 ap->id, speed, sstatus);
1558 printk(KERN_INFO "ata%u: SATA link down (SStatus %X)\n",
1564 * __sata_phy_reset - Wake/reset a low-level SATA PHY
1565 * @ap: SATA port associated with target SATA PHY.
1567 * This function issues commands to standard SATA Sxxx
1568 * PHY registers, to wake up the phy (and device), and
1569 * clear any reset condition.
1572 * PCI/etc. bus probe sem.
1575 void __sata_phy_reset(struct ata_port *ap)
1578 unsigned long timeout = jiffies + (HZ * 5);
1580 if (ap->flags & ATA_FLAG_SATA_RESET) {
1581 /* issue phy wake/reset */
1582 scr_write_flush(ap, SCR_CONTROL, 0x301);
1583 /* Couldn't find anything in SATA I/II specs, but
1584 * AHCI-1.1 10.4.2 says at least 1 ms. */
1587 scr_write_flush(ap, SCR_CONTROL, 0x300); /* phy wake/clear reset */
1589 /* wait for phy to become ready, if necessary */
1592 sstatus = scr_read(ap, SCR_STATUS);
1593 if ((sstatus & 0xf) != 1)
1595 } while (time_before(jiffies, timeout));
1597 /* print link status */
1598 sata_print_link_status(ap);
1600 /* TODO: phy layer with polling, timeouts, etc. */
1601 if (sata_dev_present(ap))
1604 ata_port_disable(ap);
1606 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1609 if (ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT)) {
1610 ata_port_disable(ap);
1614 ap->cbl = ATA_CBL_SATA;
1618 * sata_phy_reset - Reset SATA bus.
1619 * @ap: SATA port associated with target SATA PHY.
1621 * This function resets the SATA bus, and then probes
1622 * the bus for devices.
1625 * PCI/etc. bus probe sem.
1628 void sata_phy_reset(struct ata_port *ap)
1630 __sata_phy_reset(ap);
1631 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1637 * ata_port_disable - Disable port.
1638 * @ap: Port to be disabled.
1640 * Modify @ap data structure such that the system
1641 * thinks that the entire port is disabled, and should
1642 * never attempt to probe or communicate with devices
1645 * LOCKING: host_set lock, or some other form of
1649 void ata_port_disable(struct ata_port *ap)
1651 ap->device[0].class = ATA_DEV_NONE;
1652 ap->device[1].class = ATA_DEV_NONE;
1653 ap->flags |= ATA_FLAG_PORT_DISABLED;
1657 * This mode timing computation functionality is ported over from
1658 * drivers/ide/ide-timing.h and was originally written by Vojtech Pavlik
1661 * PIO 0-5, MWDMA 0-2 and UDMA 0-6 timings (in nanoseconds).
1662 * These were taken from ATA/ATAPI-6 standard, rev 0a, except
1663 * for PIO 5, which is a nonstandard extension and UDMA6, which
1664 * is currently supported only by Maxtor drives.
1667 static const struct ata_timing ata_timing[] = {
1669 { XFER_UDMA_6, 0, 0, 0, 0, 0, 0, 0, 15 },
1670 { XFER_UDMA_5, 0, 0, 0, 0, 0, 0, 0, 20 },
1671 { XFER_UDMA_4, 0, 0, 0, 0, 0, 0, 0, 30 },
1672 { XFER_UDMA_3, 0, 0, 0, 0, 0, 0, 0, 45 },
1674 { XFER_UDMA_2, 0, 0, 0, 0, 0, 0, 0, 60 },
1675 { XFER_UDMA_1, 0, 0, 0, 0, 0, 0, 0, 80 },
1676 { XFER_UDMA_0, 0, 0, 0, 0, 0, 0, 0, 120 },
1678 /* { XFER_UDMA_SLOW, 0, 0, 0, 0, 0, 0, 0, 150 }, */
1680 { XFER_MW_DMA_2, 25, 0, 0, 0, 70, 25, 120, 0 },
1681 { XFER_MW_DMA_1, 45, 0, 0, 0, 80, 50, 150, 0 },
1682 { XFER_MW_DMA_0, 60, 0, 0, 0, 215, 215, 480, 0 },
1684 { XFER_SW_DMA_2, 60, 0, 0, 0, 120, 120, 240, 0 },
1685 { XFER_SW_DMA_1, 90, 0, 0, 0, 240, 240, 480, 0 },
1686 { XFER_SW_DMA_0, 120, 0, 0, 0, 480, 480, 960, 0 },
1688 /* { XFER_PIO_5, 20, 50, 30, 100, 50, 30, 100, 0 }, */
1689 { XFER_PIO_4, 25, 70, 25, 120, 70, 25, 120, 0 },
1690 { XFER_PIO_3, 30, 80, 70, 180, 80, 70, 180, 0 },
1692 { XFER_PIO_2, 30, 290, 40, 330, 100, 90, 240, 0 },
1693 { XFER_PIO_1, 50, 290, 93, 383, 125, 100, 383, 0 },
1694 { XFER_PIO_0, 70, 290, 240, 600, 165, 150, 600, 0 },
1696 /* { XFER_PIO_SLOW, 120, 290, 240, 960, 290, 240, 960, 0 }, */
1701 #define ENOUGH(v,unit) (((v)-1)/(unit)+1)
1702 #define EZ(v,unit) ((v)?ENOUGH(v,unit):0)
1704 static void ata_timing_quantize(const struct ata_timing *t, struct ata_timing *q, int T, int UT)
1706 q->setup = EZ(t->setup * 1000, T);
1707 q->act8b = EZ(t->act8b * 1000, T);
1708 q->rec8b = EZ(t->rec8b * 1000, T);
1709 q->cyc8b = EZ(t->cyc8b * 1000, T);
1710 q->active = EZ(t->active * 1000, T);
1711 q->recover = EZ(t->recover * 1000, T);
1712 q->cycle = EZ(t->cycle * 1000, T);
1713 q->udma = EZ(t->udma * 1000, UT);
1716 void ata_timing_merge(const struct ata_timing *a, const struct ata_timing *b,
1717 struct ata_timing *m, unsigned int what)
1719 if (what & ATA_TIMING_SETUP ) m->setup = max(a->setup, b->setup);
1720 if (what & ATA_TIMING_ACT8B ) m->act8b = max(a->act8b, b->act8b);
1721 if (what & ATA_TIMING_REC8B ) m->rec8b = max(a->rec8b, b->rec8b);
1722 if (what & ATA_TIMING_CYC8B ) m->cyc8b = max(a->cyc8b, b->cyc8b);
1723 if (what & ATA_TIMING_ACTIVE ) m->active = max(a->active, b->active);
1724 if (what & ATA_TIMING_RECOVER) m->recover = max(a->recover, b->recover);
1725 if (what & ATA_TIMING_CYCLE ) m->cycle = max(a->cycle, b->cycle);
1726 if (what & ATA_TIMING_UDMA ) m->udma = max(a->udma, b->udma);
1729 static const struct ata_timing* ata_timing_find_mode(unsigned short speed)
1731 const struct ata_timing *t;
1733 for (t = ata_timing; t->mode != speed; t++)
1734 if (t->mode == 0xFF)
1739 int ata_timing_compute(struct ata_device *adev, unsigned short speed,
1740 struct ata_timing *t, int T, int UT)
1742 const struct ata_timing *s;
1743 struct ata_timing p;
1749 if (!(s = ata_timing_find_mode(speed)))
1752 memcpy(t, s, sizeof(*s));
1755 * If the drive is an EIDE drive, it can tell us it needs extended
1756 * PIO/MW_DMA cycle timing.
1759 if (adev->id[ATA_ID_FIELD_VALID] & 2) { /* EIDE drive */
1760 memset(&p, 0, sizeof(p));
1761 if(speed >= XFER_PIO_0 && speed <= XFER_SW_DMA_0) {
1762 if (speed <= XFER_PIO_2) p.cycle = p.cyc8b = adev->id[ATA_ID_EIDE_PIO];
1763 else p.cycle = p.cyc8b = adev->id[ATA_ID_EIDE_PIO_IORDY];
1764 } else if(speed >= XFER_MW_DMA_0 && speed <= XFER_MW_DMA_2) {
1765 p.cycle = adev->id[ATA_ID_EIDE_DMA_MIN];
1767 ata_timing_merge(&p, t, t, ATA_TIMING_CYCLE | ATA_TIMING_CYC8B);
1771 * Convert the timing to bus clock counts.
1774 ata_timing_quantize(t, t, T, UT);
1777 * Even in DMA/UDMA modes we still use PIO access for IDENTIFY, S.M.A.R.T
1778 * and some other commands. We have to ensure that the DMA cycle timing is
1779 * slower/equal than the fastest PIO timing.
1782 if (speed > XFER_PIO_4) {
1783 ata_timing_compute(adev, adev->pio_mode, &p, T, UT);
1784 ata_timing_merge(&p, t, t, ATA_TIMING_ALL);
1788 * Lenghten active & recovery time so that cycle time is correct.
1791 if (t->act8b + t->rec8b < t->cyc8b) {
1792 t->act8b += (t->cyc8b - (t->act8b + t->rec8b)) / 2;
1793 t->rec8b = t->cyc8b - t->act8b;
1796 if (t->active + t->recover < t->cycle) {
1797 t->active += (t->cycle - (t->active + t->recover)) / 2;
1798 t->recover = t->cycle - t->active;
1804 static const struct {
1807 } xfer_mode_classes[] = {
1808 { ATA_SHIFT_UDMA, XFER_UDMA_0 },
1809 { ATA_SHIFT_MWDMA, XFER_MW_DMA_0 },
1810 { ATA_SHIFT_PIO, XFER_PIO_0 },
1813 static u8 base_from_shift(unsigned int shift)
1817 for (i = 0; i < ARRAY_SIZE(xfer_mode_classes); i++)
1818 if (xfer_mode_classes[i].shift == shift)
1819 return xfer_mode_classes[i].base;
1824 static void ata_dev_set_mode(struct ata_port *ap, struct ata_device *dev)
1829 if (!ata_dev_present(dev) || (ap->flags & ATA_FLAG_PORT_DISABLED))
1832 if (dev->xfer_shift == ATA_SHIFT_PIO)
1833 dev->flags |= ATA_DFLAG_PIO;
1835 ata_dev_set_xfermode(ap, dev);
1837 base = base_from_shift(dev->xfer_shift);
1838 ofs = dev->xfer_mode - base;
1839 idx = ofs + dev->xfer_shift;
1840 WARN_ON(idx >= ARRAY_SIZE(xfer_mode_str));
1842 DPRINTK("idx=%d xfer_shift=%u, xfer_mode=0x%x, base=0x%x, offset=%d\n",
1843 idx, dev->xfer_shift, (int)dev->xfer_mode, (int)base, ofs);
1845 printk(KERN_INFO "ata%u: dev %u configured for %s\n",
1846 ap->id, dev->devno, xfer_mode_str[idx]);
1849 static int ata_host_set_pio(struct ata_port *ap)
1855 mask = ata_get_mode_mask(ap, ATA_SHIFT_PIO);
1858 printk(KERN_WARNING "ata%u: no PIO support\n", ap->id);
1862 base = base_from_shift(ATA_SHIFT_PIO);
1863 xfer_mode = base + x;
1865 DPRINTK("base 0x%x xfer_mode 0x%x mask 0x%x x %d\n",
1866 (int)base, (int)xfer_mode, mask, x);
1868 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1869 struct ata_device *dev = &ap->device[i];
1870 if (ata_dev_present(dev)) {
1871 dev->pio_mode = xfer_mode;
1872 dev->xfer_mode = xfer_mode;
1873 dev->xfer_shift = ATA_SHIFT_PIO;
1874 if (ap->ops->set_piomode)
1875 ap->ops->set_piomode(ap, dev);
1882 static void ata_host_set_dma(struct ata_port *ap, u8 xfer_mode,
1883 unsigned int xfer_shift)
1887 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1888 struct ata_device *dev = &ap->device[i];
1889 if (ata_dev_present(dev)) {
1890 dev->dma_mode = xfer_mode;
1891 dev->xfer_mode = xfer_mode;
1892 dev->xfer_shift = xfer_shift;
1893 if (ap->ops->set_dmamode)
1894 ap->ops->set_dmamode(ap, dev);
1900 * ata_set_mode - Program timings and issue SET FEATURES - XFER
1901 * @ap: port on which timings will be programmed
1903 * Set ATA device disk transfer mode (PIO3, UDMA6, etc.).
1906 * PCI/etc. bus probe sem.
1909 static void ata_set_mode(struct ata_port *ap)
1911 unsigned int xfer_shift;
1915 /* step 1: always set host PIO timings */
1916 rc = ata_host_set_pio(ap);
1920 /* step 2: choose the best data xfer mode */
1921 xfer_mode = xfer_shift = 0;
1922 rc = ata_choose_xfer_mode(ap, &xfer_mode, &xfer_shift);
1926 /* step 3: if that xfer mode isn't PIO, set host DMA timings */
1927 if (xfer_shift != ATA_SHIFT_PIO)
1928 ata_host_set_dma(ap, xfer_mode, xfer_shift);
1930 /* step 4: update devices' xfer mode */
1931 ata_dev_set_mode(ap, &ap->device[0]);
1932 ata_dev_set_mode(ap, &ap->device[1]);
1934 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1937 if (ap->ops->post_set_mode)
1938 ap->ops->post_set_mode(ap);
1943 ata_port_disable(ap);
1947 * ata_busy_sleep - sleep until BSY clears, or timeout
1948 * @ap: port containing status register to be polled
1949 * @tmout_pat: impatience timeout
1950 * @tmout: overall timeout
1952 * Sleep until ATA Status register bit BSY clears,
1953 * or a timeout occurs.
1959 static unsigned int ata_busy_sleep (struct ata_port *ap,
1960 unsigned long tmout_pat,
1961 unsigned long tmout)
1963 unsigned long timer_start, timeout;
1966 status = ata_busy_wait(ap, ATA_BUSY, 300);
1967 timer_start = jiffies;
1968 timeout = timer_start + tmout_pat;
1969 while ((status & ATA_BUSY) && (time_before(jiffies, timeout))) {
1971 status = ata_busy_wait(ap, ATA_BUSY, 3);
1974 if (status & ATA_BUSY)
1975 printk(KERN_WARNING "ata%u is slow to respond, "
1976 "please be patient\n", ap->id);
1978 timeout = timer_start + tmout;
1979 while ((status & ATA_BUSY) && (time_before(jiffies, timeout))) {
1981 status = ata_chk_status(ap);
1984 if (status & ATA_BUSY) {
1985 printk(KERN_ERR "ata%u failed to respond (%lu secs)\n",
1986 ap->id, tmout / HZ);
1993 static void ata_bus_post_reset(struct ata_port *ap, unsigned int devmask)
1995 struct ata_ioports *ioaddr = &ap->ioaddr;
1996 unsigned int dev0 = devmask & (1 << 0);
1997 unsigned int dev1 = devmask & (1 << 1);
1998 unsigned long timeout;
2000 /* if device 0 was found in ata_devchk, wait for its
2004 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
2006 /* if device 1 was found in ata_devchk, wait for
2007 * register access, then wait for BSY to clear
2009 timeout = jiffies + ATA_TMOUT_BOOT;
2013 ap->ops->dev_select(ap, 1);
2014 if (ap->flags & ATA_FLAG_MMIO) {
2015 nsect = readb((void __iomem *) ioaddr->nsect_addr);
2016 lbal = readb((void __iomem *) ioaddr->lbal_addr);
2018 nsect = inb(ioaddr->nsect_addr);
2019 lbal = inb(ioaddr->lbal_addr);
2021 if ((nsect == 1) && (lbal == 1))
2023 if (time_after(jiffies, timeout)) {
2027 msleep(50); /* give drive a breather */
2030 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
2032 /* is all this really necessary? */
2033 ap->ops->dev_select(ap, 0);
2035 ap->ops->dev_select(ap, 1);
2037 ap->ops->dev_select(ap, 0);
2041 * ata_bus_edd - Issue EXECUTE DEVICE DIAGNOSTIC command.
2042 * @ap: Port to reset and probe
2044 * Use the EXECUTE DEVICE DIAGNOSTIC command to reset and
2045 * probe the bus. Not often used these days.
2048 * PCI/etc. bus probe sem.
2049 * Obtains host_set lock.
2053 static unsigned int ata_bus_edd(struct ata_port *ap)
2055 struct ata_taskfile tf;
2056 unsigned long flags;
2058 /* set up execute-device-diag (bus reset) taskfile */
2059 /* also, take interrupts to a known state (disabled) */
2060 DPRINTK("execute-device-diag\n");
2061 ata_tf_init(ap, &tf, 0);
2063 tf.command = ATA_CMD_EDD;
2064 tf.protocol = ATA_PROT_NODATA;
2067 spin_lock_irqsave(&ap->host_set->lock, flags);
2068 ata_tf_to_host(ap, &tf);
2069 spin_unlock_irqrestore(&ap->host_set->lock, flags);
2071 /* spec says at least 2ms. but who knows with those
2072 * crazy ATAPI devices...
2076 return ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
2079 static unsigned int ata_bus_softreset(struct ata_port *ap,
2080 unsigned int devmask)
2082 struct ata_ioports *ioaddr = &ap->ioaddr;
2084 DPRINTK("ata%u: bus reset via SRST\n", ap->id);
2086 /* software reset. causes dev0 to be selected */
2087 if (ap->flags & ATA_FLAG_MMIO) {
2088 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
2089 udelay(20); /* FIXME: flush */
2090 writeb(ap->ctl | ATA_SRST, (void __iomem *) ioaddr->ctl_addr);
2091 udelay(20); /* FIXME: flush */
2092 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
2094 outb(ap->ctl, ioaddr->ctl_addr);
2096 outb(ap->ctl | ATA_SRST, ioaddr->ctl_addr);
2098 outb(ap->ctl, ioaddr->ctl_addr);
2101 /* spec mandates ">= 2ms" before checking status.
2102 * We wait 150ms, because that was the magic delay used for
2103 * ATAPI devices in Hale Landis's ATADRVR, for the period of time
2104 * between when the ATA command register is written, and then
2105 * status is checked. Because waiting for "a while" before
2106 * checking status is fine, post SRST, we perform this magic
2107 * delay here as well.
2111 ata_bus_post_reset(ap, devmask);
2117 * ata_bus_reset - reset host port and associated ATA channel
2118 * @ap: port to reset
2120 * This is typically the first time we actually start issuing
2121 * commands to the ATA channel. We wait for BSY to clear, then
2122 * issue EXECUTE DEVICE DIAGNOSTIC command, polling for its
2123 * result. Determine what devices, if any, are on the channel
2124 * by looking at the device 0/1 error register. Look at the signature
2125 * stored in each device's taskfile registers, to determine if
2126 * the device is ATA or ATAPI.
2129 * PCI/etc. bus probe sem.
2130 * Obtains host_set lock.
2133 * Sets ATA_FLAG_PORT_DISABLED if bus reset fails.
2136 void ata_bus_reset(struct ata_port *ap)
2138 struct ata_ioports *ioaddr = &ap->ioaddr;
2139 unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS;
2141 unsigned int dev0, dev1 = 0, rc = 0, devmask = 0;
2143 DPRINTK("ENTER, host %u, port %u\n", ap->id, ap->port_no);
2145 /* determine if device 0/1 are present */
2146 if (ap->flags & ATA_FLAG_SATA_RESET)
2149 dev0 = ata_devchk(ap, 0);
2151 dev1 = ata_devchk(ap, 1);
2155 devmask |= (1 << 0);
2157 devmask |= (1 << 1);
2159 /* select device 0 again */
2160 ap->ops->dev_select(ap, 0);
2162 /* issue bus reset */
2163 if (ap->flags & ATA_FLAG_SRST)
2164 rc = ata_bus_softreset(ap, devmask);
2165 else if ((ap->flags & ATA_FLAG_SATA_RESET) == 0) {
2166 /* set up device control */
2167 if (ap->flags & ATA_FLAG_MMIO)
2168 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
2170 outb(ap->ctl, ioaddr->ctl_addr);
2171 rc = ata_bus_edd(ap);
2178 * determine by signature whether we have ATA or ATAPI devices
2180 err = ata_dev_try_classify(ap, 0);
2181 if ((slave_possible) && (err != 0x81))
2182 ata_dev_try_classify(ap, 1);
2184 /* re-enable interrupts */
2185 if (ap->ioaddr.ctl_addr) /* FIXME: hack. create a hook instead */
2188 /* is double-select really necessary? */
2189 if (ap->device[1].class != ATA_DEV_NONE)
2190 ap->ops->dev_select(ap, 1);
2191 if (ap->device[0].class != ATA_DEV_NONE)
2192 ap->ops->dev_select(ap, 0);
2194 /* if no devices were detected, disable this port */
2195 if ((ap->device[0].class == ATA_DEV_NONE) &&
2196 (ap->device[1].class == ATA_DEV_NONE))
2199 if (ap->flags & (ATA_FLAG_SATA_RESET | ATA_FLAG_SRST)) {
2200 /* set up device control for ATA_FLAG_SATA_RESET */
2201 if (ap->flags & ATA_FLAG_MMIO)
2202 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
2204 outb(ap->ctl, ioaddr->ctl_addr);
2211 printk(KERN_ERR "ata%u: disabling port\n", ap->id);
2212 ap->ops->port_disable(ap);
2217 static void ata_pr_blacklisted(const struct ata_port *ap,
2218 const struct ata_device *dev)
2220 printk(KERN_WARNING "ata%u: dev %u is on DMA blacklist, disabling DMA\n",
2221 ap->id, dev->devno);
2224 static const char * const ata_dma_blacklist [] = {
2243 "Toshiba CD-ROM XM-6202B",
2244 "TOSHIBA CD-ROM XM-1702BC",
2246 "E-IDE CD-ROM CR-840",
2249 "SAMSUNG CD-ROM SC-148C",
2250 "SAMSUNG CD-ROM SC",
2252 "ATAPI CD-ROM DRIVE 40X MAXIMUM",
2256 static int ata_dma_blacklisted(const struct ata_device *dev)
2258 unsigned char model_num[40];
2263 ata_dev_id_string(dev->id, model_num, ATA_ID_PROD_OFS,
2266 len = strnlen(s, sizeof(model_num));
2268 /* ATAPI specifies that empty space is blank-filled; remove blanks */
2269 while ((len > 0) && (s[len - 1] == ' ')) {
2274 for (i = 0; i < ARRAY_SIZE(ata_dma_blacklist); i++)
2275 if (!strncmp(ata_dma_blacklist[i], s, len))
2281 static unsigned int ata_get_mode_mask(const struct ata_port *ap, int shift)
2283 const struct ata_device *master, *slave;
2286 master = &ap->device[0];
2287 slave = &ap->device[1];
2289 assert (ata_dev_present(master) || ata_dev_present(slave));
2291 if (shift == ATA_SHIFT_UDMA) {
2292 mask = ap->udma_mask;
2293 if (ata_dev_present(master)) {
2294 mask &= (master->id[ATA_ID_UDMA_MODES] & 0xff);
2295 if (ata_dma_blacklisted(master)) {
2297 ata_pr_blacklisted(ap, master);
2300 if (ata_dev_present(slave)) {
2301 mask &= (slave->id[ATA_ID_UDMA_MODES] & 0xff);
2302 if (ata_dma_blacklisted(slave)) {
2304 ata_pr_blacklisted(ap, slave);
2308 else if (shift == ATA_SHIFT_MWDMA) {
2309 mask = ap->mwdma_mask;
2310 if (ata_dev_present(master)) {
2311 mask &= (master->id[ATA_ID_MWDMA_MODES] & 0x07);
2312 if (ata_dma_blacklisted(master)) {
2314 ata_pr_blacklisted(ap, master);
2317 if (ata_dev_present(slave)) {
2318 mask &= (slave->id[ATA_ID_MWDMA_MODES] & 0x07);
2319 if (ata_dma_blacklisted(slave)) {
2321 ata_pr_blacklisted(ap, slave);
2325 else if (shift == ATA_SHIFT_PIO) {
2326 mask = ap->pio_mask;
2327 if (ata_dev_present(master)) {
2328 /* spec doesn't return explicit support for
2329 * PIO0-2, so we fake it
2331 u16 tmp_mode = master->id[ATA_ID_PIO_MODES] & 0x03;
2336 if (ata_dev_present(slave)) {
2337 /* spec doesn't return explicit support for
2338 * PIO0-2, so we fake it
2340 u16 tmp_mode = slave->id[ATA_ID_PIO_MODES] & 0x03;
2347 mask = 0xffffffff; /* shut up compiler warning */
2354 /* find greatest bit */
2355 static int fgb(u32 bitmap)
2360 for (i = 0; i < 32; i++)
2361 if (bitmap & (1 << i))
2368 * ata_choose_xfer_mode - attempt to find best transfer mode
2369 * @ap: Port for which an xfer mode will be selected
2370 * @xfer_mode_out: (output) SET FEATURES - XFER MODE code
2371 * @xfer_shift_out: (output) bit shift that selects this mode
2373 * Based on host and device capabilities, determine the
2374 * maximum transfer mode that is amenable to all.
2377 * PCI/etc. bus probe sem.
2380 * Zero on success, negative on error.
2383 static int ata_choose_xfer_mode(const struct ata_port *ap,
2385 unsigned int *xfer_shift_out)
2387 unsigned int mask, shift;
2390 for (i = 0; i < ARRAY_SIZE(xfer_mode_classes); i++) {
2391 shift = xfer_mode_classes[i].shift;
2392 mask = ata_get_mode_mask(ap, shift);
2396 *xfer_mode_out = xfer_mode_classes[i].base + x;
2397 *xfer_shift_out = shift;
2406 * ata_dev_set_xfermode - Issue SET FEATURES - XFER MODE command
2407 * @ap: Port associated with device @dev
2408 * @dev: Device to which command will be sent
2410 * Issue SET FEATURES - XFER MODE command to device @dev
2414 * PCI/etc. bus probe sem.
2417 static void ata_dev_set_xfermode(struct ata_port *ap, struct ata_device *dev)
2419 struct ata_taskfile tf;
2421 /* set up set-features taskfile */
2422 DPRINTK("set features - xfer mode\n");
2424 ata_tf_init(ap, &tf, dev->devno);
2425 tf.command = ATA_CMD_SET_FEATURES;
2426 tf.feature = SETFEATURES_XFER;
2427 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
2428 tf.protocol = ATA_PROT_NODATA;
2429 tf.nsect = dev->xfer_mode;
2431 if (ata_exec_internal(ap, dev, &tf, DMA_NONE, NULL, 0)) {
2432 printk(KERN_ERR "ata%u: failed to set xfermode, disabled\n",
2434 ata_port_disable(ap);
2441 * ata_dev_reread_id - Reread the device identify device info
2442 * @ap: port where the device is
2443 * @dev: device to reread the identify device info
2448 static void ata_dev_reread_id(struct ata_port *ap, struct ata_device *dev)
2450 struct ata_taskfile tf;
2452 ata_tf_init(ap, &tf, dev->devno);
2454 if (dev->class == ATA_DEV_ATA) {
2455 tf.command = ATA_CMD_ID_ATA;
2456 DPRINTK("do ATA identify\n");
2458 tf.command = ATA_CMD_ID_ATAPI;
2459 DPRINTK("do ATAPI identify\n");
2462 tf.flags |= ATA_TFLAG_DEVICE;
2463 tf.protocol = ATA_PROT_PIO;
2465 if (ata_exec_internal(ap, dev, &tf, DMA_FROM_DEVICE,
2466 dev->id, sizeof(dev->id)))
2469 swap_buf_le16(dev->id, ATA_ID_WORDS);
2477 printk(KERN_ERR "ata%u: failed to reread ID, disabled\n", ap->id);
2478 ata_port_disable(ap);
2482 * ata_dev_init_params - Issue INIT DEV PARAMS command
2483 * @ap: Port associated with device @dev
2484 * @dev: Device to which command will be sent
2489 static void ata_dev_init_params(struct ata_port *ap, struct ata_device *dev)
2491 struct ata_taskfile tf;
2492 u16 sectors = dev->id[6];
2493 u16 heads = dev->id[3];
2495 /* Number of sectors per track 1-255. Number of heads 1-16 */
2496 if (sectors < 1 || sectors > 255 || heads < 1 || heads > 16)
2499 /* set up init dev params taskfile */
2500 DPRINTK("init dev params \n");
2502 ata_tf_init(ap, &tf, dev->devno);
2503 tf.command = ATA_CMD_INIT_DEV_PARAMS;
2504 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
2505 tf.protocol = ATA_PROT_NODATA;
2507 tf.device |= (heads - 1) & 0x0f; /* max head = num. of heads - 1 */
2509 if (ata_exec_internal(ap, dev, &tf, DMA_NONE, NULL, 0)) {
2510 printk(KERN_ERR "ata%u: failed to init parameters, disabled\n",
2512 ata_port_disable(ap);
2519 * ata_sg_clean - Unmap DMA memory associated with command
2520 * @qc: Command containing DMA memory to be released
2522 * Unmap all mapped DMA memory associated with this command.
2525 * spin_lock_irqsave(host_set lock)
2528 static void ata_sg_clean(struct ata_queued_cmd *qc)
2530 struct ata_port *ap = qc->ap;
2531 struct scatterlist *sg = qc->__sg;
2532 int dir = qc->dma_dir;
2533 void *pad_buf = NULL;
2535 assert(qc->flags & ATA_QCFLAG_DMAMAP);
2538 if (qc->flags & ATA_QCFLAG_SINGLE)
2539 assert(qc->n_elem == 1);
2541 VPRINTK("unmapping %u sg elements\n", qc->n_elem);
2543 /* if we padded the buffer out to 32-bit bound, and data
2544 * xfer direction is from-device, we must copy from the
2545 * pad buffer back into the supplied buffer
2547 if (qc->pad_len && !(qc->tf.flags & ATA_TFLAG_WRITE))
2548 pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
2550 if (qc->flags & ATA_QCFLAG_SG) {
2552 dma_unmap_sg(ap->host_set->dev, sg, qc->n_elem, dir);
2553 /* restore last sg */
2554 sg[qc->orig_n_elem - 1].length += qc->pad_len;
2556 struct scatterlist *psg = &qc->pad_sgent;
2557 void *addr = kmap_atomic(psg->page, KM_IRQ0);
2558 memcpy(addr + psg->offset, pad_buf, qc->pad_len);
2559 kunmap_atomic(addr, KM_IRQ0);
2562 if (sg_dma_len(&sg[0]) > 0)
2563 dma_unmap_single(ap->host_set->dev,
2564 sg_dma_address(&sg[0]), sg_dma_len(&sg[0]),
2567 sg->length += qc->pad_len;
2569 memcpy(qc->buf_virt + sg->length - qc->pad_len,
2570 pad_buf, qc->pad_len);
2573 qc->flags &= ~ATA_QCFLAG_DMAMAP;
2578 * ata_fill_sg - Fill PCI IDE PRD table
2579 * @qc: Metadata associated with taskfile to be transferred
2581 * Fill PCI IDE PRD (scatter-gather) table with segments
2582 * associated with the current disk command.
2585 * spin_lock_irqsave(host_set lock)
2588 static void ata_fill_sg(struct ata_queued_cmd *qc)
2590 struct ata_port *ap = qc->ap;
2591 struct scatterlist *sg;
2594 assert(qc->__sg != NULL);
2595 assert(qc->n_elem > 0);
2598 ata_for_each_sg(sg, qc) {
2602 /* determine if physical DMA addr spans 64K boundary.
2603 * Note h/w doesn't support 64-bit, so we unconditionally
2604 * truncate dma_addr_t to u32.
2606 addr = (u32) sg_dma_address(sg);
2607 sg_len = sg_dma_len(sg);
2610 offset = addr & 0xffff;
2612 if ((offset + sg_len) > 0x10000)
2613 len = 0x10000 - offset;
2615 ap->prd[idx].addr = cpu_to_le32(addr);
2616 ap->prd[idx].flags_len = cpu_to_le32(len & 0xffff);
2617 VPRINTK("PRD[%u] = (0x%X, 0x%X)\n", idx, addr, len);
2626 ap->prd[idx - 1].flags_len |= cpu_to_le32(ATA_PRD_EOT);
2629 * ata_check_atapi_dma - Check whether ATAPI DMA can be supported
2630 * @qc: Metadata associated with taskfile to check
2632 * Allow low-level driver to filter ATA PACKET commands, returning
2633 * a status indicating whether or not it is OK to use DMA for the
2634 * supplied PACKET command.
2637 * spin_lock_irqsave(host_set lock)
2639 * RETURNS: 0 when ATAPI DMA can be used
2642 int ata_check_atapi_dma(struct ata_queued_cmd *qc)
2644 struct ata_port *ap = qc->ap;
2645 int rc = 0; /* Assume ATAPI DMA is OK by default */
2647 if (ap->ops->check_atapi_dma)
2648 rc = ap->ops->check_atapi_dma(qc);
2653 * ata_qc_prep - Prepare taskfile for submission
2654 * @qc: Metadata associated with taskfile to be prepared
2656 * Prepare ATA taskfile for submission.
2659 * spin_lock_irqsave(host_set lock)
2661 void ata_qc_prep(struct ata_queued_cmd *qc)
2663 if (!(qc->flags & ATA_QCFLAG_DMAMAP))
2670 * ata_sg_init_one - Associate command with memory buffer
2671 * @qc: Command to be associated
2672 * @buf: Memory buffer
2673 * @buflen: Length of memory buffer, in bytes.
2675 * Initialize the data-related elements of queued_cmd @qc
2676 * to point to a single memory buffer, @buf of byte length @buflen.
2679 * spin_lock_irqsave(host_set lock)
2682 void ata_sg_init_one(struct ata_queued_cmd *qc, void *buf, unsigned int buflen)
2684 struct scatterlist *sg;
2686 qc->flags |= ATA_QCFLAG_SINGLE;
2688 memset(&qc->sgent, 0, sizeof(qc->sgent));
2689 qc->__sg = &qc->sgent;
2691 qc->orig_n_elem = 1;
2695 sg_init_one(sg, buf, buflen);
2699 * ata_sg_init - Associate command with scatter-gather table.
2700 * @qc: Command to be associated
2701 * @sg: Scatter-gather table.
2702 * @n_elem: Number of elements in s/g table.
2704 * Initialize the data-related elements of queued_cmd @qc
2705 * to point to a scatter-gather table @sg, containing @n_elem
2709 * spin_lock_irqsave(host_set lock)
2712 void ata_sg_init(struct ata_queued_cmd *qc, struct scatterlist *sg,
2713 unsigned int n_elem)
2715 qc->flags |= ATA_QCFLAG_SG;
2717 qc->n_elem = n_elem;
2718 qc->orig_n_elem = n_elem;
2722 * ata_sg_setup_one - DMA-map the memory buffer associated with a command.
2723 * @qc: Command with memory buffer to be mapped.
2725 * DMA-map the memory buffer associated with queued_cmd @qc.
2728 * spin_lock_irqsave(host_set lock)
2731 * Zero on success, negative on error.
2734 static int ata_sg_setup_one(struct ata_queued_cmd *qc)
2736 struct ata_port *ap = qc->ap;
2737 int dir = qc->dma_dir;
2738 struct scatterlist *sg = qc->__sg;
2739 dma_addr_t dma_address;
2741 /* we must lengthen transfers to end on a 32-bit boundary */
2742 qc->pad_len = sg->length & 3;
2744 void *pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
2745 struct scatterlist *psg = &qc->pad_sgent;
2747 assert(qc->dev->class == ATA_DEV_ATAPI);
2749 memset(pad_buf, 0, ATA_DMA_PAD_SZ);
2751 if (qc->tf.flags & ATA_TFLAG_WRITE)
2752 memcpy(pad_buf, qc->buf_virt + sg->length - qc->pad_len,
2755 sg_dma_address(psg) = ap->pad_dma + (qc->tag * ATA_DMA_PAD_SZ);
2756 sg_dma_len(psg) = ATA_DMA_PAD_SZ;
2758 sg->length -= qc->pad_len;
2760 DPRINTK("padding done, sg->length=%u pad_len=%u\n",
2761 sg->length, qc->pad_len);
2765 sg_dma_address(sg) = 0;
2769 dma_address = dma_map_single(ap->host_set->dev, qc->buf_virt,
2771 if (dma_mapping_error(dma_address)) {
2773 sg->length += qc->pad_len;
2777 sg_dma_address(sg) = dma_address;
2779 sg_dma_len(sg) = sg->length;
2781 DPRINTK("mapped buffer of %d bytes for %s\n", sg_dma_len(sg),
2782 qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
2788 * ata_sg_setup - DMA-map the scatter-gather table associated with a command.
2789 * @qc: Command with scatter-gather table to be mapped.
2791 * DMA-map the scatter-gather table associated with queued_cmd @qc.
2794 * spin_lock_irqsave(host_set lock)
2797 * Zero on success, negative on error.
2801 static int ata_sg_setup(struct ata_queued_cmd *qc)
2803 struct ata_port *ap = qc->ap;
2804 struct scatterlist *sg = qc->__sg;
2805 struct scatterlist *lsg = &sg[qc->n_elem - 1];
2806 int n_elem, pre_n_elem, dir, trim_sg = 0;
2808 VPRINTK("ENTER, ata%u\n", ap->id);
2809 assert(qc->flags & ATA_QCFLAG_SG);
2811 /* we must lengthen transfers to end on a 32-bit boundary */
2812 qc->pad_len = lsg->length & 3;
2814 void *pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
2815 struct scatterlist *psg = &qc->pad_sgent;
2816 unsigned int offset;
2818 assert(qc->dev->class == ATA_DEV_ATAPI);
2820 memset(pad_buf, 0, ATA_DMA_PAD_SZ);
2823 * psg->page/offset are used to copy to-be-written
2824 * data in this function or read data in ata_sg_clean.
2826 offset = lsg->offset + lsg->length - qc->pad_len;
2827 psg->page = nth_page(lsg->page, offset >> PAGE_SHIFT);
2828 psg->offset = offset_in_page(offset);
2830 if (qc->tf.flags & ATA_TFLAG_WRITE) {
2831 void *addr = kmap_atomic(psg->page, KM_IRQ0);
2832 memcpy(pad_buf, addr + psg->offset, qc->pad_len);
2833 kunmap_atomic(addr, KM_IRQ0);
2836 sg_dma_address(psg) = ap->pad_dma + (qc->tag * ATA_DMA_PAD_SZ);
2837 sg_dma_len(psg) = ATA_DMA_PAD_SZ;
2839 lsg->length -= qc->pad_len;
2840 if (lsg->length == 0)
2843 DPRINTK("padding done, sg[%d].length=%u pad_len=%u\n",
2844 qc->n_elem - 1, lsg->length, qc->pad_len);
2847 pre_n_elem = qc->n_elem;
2848 if (trim_sg && pre_n_elem)
2857 n_elem = dma_map_sg(ap->host_set->dev, sg, pre_n_elem, dir);
2859 /* restore last sg */
2860 lsg->length += qc->pad_len;
2864 DPRINTK("%d sg elements mapped\n", n_elem);
2867 qc->n_elem = n_elem;
2873 * ata_poll_qc_complete - turn irq back on and finish qc
2874 * @qc: Command to complete
2875 * @err_mask: ATA status register content
2878 * None. (grabs host lock)
2881 void ata_poll_qc_complete(struct ata_queued_cmd *qc)
2883 struct ata_port *ap = qc->ap;
2884 unsigned long flags;
2886 spin_lock_irqsave(&ap->host_set->lock, flags);
2887 ap->flags &= ~ATA_FLAG_NOINTR;
2889 ata_qc_complete(qc);
2890 spin_unlock_irqrestore(&ap->host_set->lock, flags);
2895 * @ap: the target ata_port
2898 * None. (executing in kernel thread context)
2901 * timeout value to use
2904 static unsigned long ata_pio_poll(struct ata_port *ap)
2906 struct ata_queued_cmd *qc;
2908 unsigned int poll_state = HSM_ST_UNKNOWN;
2909 unsigned int reg_state = HSM_ST_UNKNOWN;
2911 qc = ata_qc_from_tag(ap, ap->active_tag);
2914 switch (ap->hsm_task_state) {
2917 poll_state = HSM_ST_POLL;
2921 case HSM_ST_LAST_POLL:
2922 poll_state = HSM_ST_LAST_POLL;
2923 reg_state = HSM_ST_LAST;
2930 status = ata_chk_status(ap);
2931 if (status & ATA_BUSY) {
2932 if (time_after(jiffies, ap->pio_task_timeout)) {
2933 qc->err_mask |= AC_ERR_ATA_BUS;
2934 ap->hsm_task_state = HSM_ST_TMOUT;
2937 ap->hsm_task_state = poll_state;
2938 return ATA_SHORT_PAUSE;
2941 ap->hsm_task_state = reg_state;
2946 * ata_pio_complete - check if drive is busy or idle
2947 * @ap: the target ata_port
2950 * None. (executing in kernel thread context)
2953 * Non-zero if qc completed, zero otherwise.
2956 static int ata_pio_complete (struct ata_port *ap)
2958 struct ata_queued_cmd *qc;
2962 * This is purely heuristic. This is a fast path. Sometimes when
2963 * we enter, BSY will be cleared in a chk-status or two. If not,
2964 * the drive is probably seeking or something. Snooze for a couple
2965 * msecs, then chk-status again. If still busy, fall back to
2966 * HSM_ST_POLL state.
2968 drv_stat = ata_busy_wait(ap, ATA_BUSY, 10);
2969 if (drv_stat & ATA_BUSY) {
2971 drv_stat = ata_busy_wait(ap, ATA_BUSY, 10);
2972 if (drv_stat & ATA_BUSY) {
2973 ap->hsm_task_state = HSM_ST_LAST_POLL;
2974 ap->pio_task_timeout = jiffies + ATA_TMOUT_PIO;
2979 qc = ata_qc_from_tag(ap, ap->active_tag);
2982 drv_stat = ata_wait_idle(ap);
2983 if (!ata_ok(drv_stat)) {
2984 qc->err_mask |= __ac_err_mask(drv_stat);
2985 ap->hsm_task_state = HSM_ST_ERR;
2989 ap->hsm_task_state = HSM_ST_IDLE;
2991 assert(qc->err_mask == 0);
2992 ata_poll_qc_complete(qc);
2994 /* another command may start at this point */
3001 * swap_buf_le16 - swap halves of 16-words in place
3002 * @buf: Buffer to swap
3003 * @buf_words: Number of 16-bit words in buffer.
3005 * Swap halves of 16-bit words if needed to convert from
3006 * little-endian byte order to native cpu byte order, or
3010 * Inherited from caller.
3012 void swap_buf_le16(u16 *buf, unsigned int buf_words)
3017 for (i = 0; i < buf_words; i++)
3018 buf[i] = le16_to_cpu(buf[i]);
3019 #endif /* __BIG_ENDIAN */
3023 * ata_mmio_data_xfer - Transfer data by MMIO
3024 * @ap: port to read/write
3026 * @buflen: buffer length
3027 * @write_data: read/write
3029 * Transfer data from/to the device data register by MMIO.
3032 * Inherited from caller.
3035 static void ata_mmio_data_xfer(struct ata_port *ap, unsigned char *buf,
3036 unsigned int buflen, int write_data)
3039 unsigned int words = buflen >> 1;
3040 u16 *buf16 = (u16 *) buf;
3041 void __iomem *mmio = (void __iomem *)ap->ioaddr.data_addr;
3043 /* Transfer multiple of 2 bytes */
3045 for (i = 0; i < words; i++)
3046 writew(le16_to_cpu(buf16[i]), mmio);
3048 for (i = 0; i < words; i++)
3049 buf16[i] = cpu_to_le16(readw(mmio));
3052 /* Transfer trailing 1 byte, if any. */
3053 if (unlikely(buflen & 0x01)) {
3054 u16 align_buf[1] = { 0 };
3055 unsigned char *trailing_buf = buf + buflen - 1;
3058 memcpy(align_buf, trailing_buf, 1);
3059 writew(le16_to_cpu(align_buf[0]), mmio);
3061 align_buf[0] = cpu_to_le16(readw(mmio));
3062 memcpy(trailing_buf, align_buf, 1);
3068 * ata_pio_data_xfer - Transfer data by PIO
3069 * @ap: port to read/write
3071 * @buflen: buffer length
3072 * @write_data: read/write
3074 * Transfer data from/to the device data register by PIO.
3077 * Inherited from caller.
3080 static void ata_pio_data_xfer(struct ata_port *ap, unsigned char *buf,
3081 unsigned int buflen, int write_data)
3083 unsigned int words = buflen >> 1;
3085 /* Transfer multiple of 2 bytes */
3087 outsw(ap->ioaddr.data_addr, buf, words);
3089 insw(ap->ioaddr.data_addr, buf, words);
3091 /* Transfer trailing 1 byte, if any. */
3092 if (unlikely(buflen & 0x01)) {
3093 u16 align_buf[1] = { 0 };
3094 unsigned char *trailing_buf = buf + buflen - 1;
3097 memcpy(align_buf, trailing_buf, 1);
3098 outw(le16_to_cpu(align_buf[0]), ap->ioaddr.data_addr);
3100 align_buf[0] = cpu_to_le16(inw(ap->ioaddr.data_addr));
3101 memcpy(trailing_buf, align_buf, 1);
3107 * ata_data_xfer - Transfer data from/to the data register.
3108 * @ap: port to read/write
3110 * @buflen: buffer length
3111 * @do_write: read/write
3113 * Transfer data from/to the device data register.
3116 * Inherited from caller.
3119 static void ata_data_xfer(struct ata_port *ap, unsigned char *buf,
3120 unsigned int buflen, int do_write)
3122 /* Make the crap hardware pay the costs not the good stuff */
3123 if (unlikely(ap->flags & ATA_FLAG_IRQ_MASK)) {
3124 unsigned long flags;
3125 local_irq_save(flags);
3126 if (ap->flags & ATA_FLAG_MMIO)
3127 ata_mmio_data_xfer(ap, buf, buflen, do_write);
3129 ata_pio_data_xfer(ap, buf, buflen, do_write);
3130 local_irq_restore(flags);
3132 if (ap->flags & ATA_FLAG_MMIO)
3133 ata_mmio_data_xfer(ap, buf, buflen, do_write);
3135 ata_pio_data_xfer(ap, buf, buflen, do_write);
3140 * ata_pio_sector - Transfer ATA_SECT_SIZE (512 bytes) of data.
3141 * @qc: Command on going
3143 * Transfer ATA_SECT_SIZE of data from/to the ATA device.
3146 * Inherited from caller.
3149 static void ata_pio_sector(struct ata_queued_cmd *qc)
3151 int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
3152 struct scatterlist *sg = qc->__sg;
3153 struct ata_port *ap = qc->ap;
3155 unsigned int offset;
3158 if (qc->cursect == (qc->nsect - 1))
3159 ap->hsm_task_state = HSM_ST_LAST;
3161 page = sg[qc->cursg].page;
3162 offset = sg[qc->cursg].offset + qc->cursg_ofs * ATA_SECT_SIZE;
3164 /* get the current page and offset */
3165 page = nth_page(page, (offset >> PAGE_SHIFT));
3166 offset %= PAGE_SIZE;
3168 buf = kmap(page) + offset;
3173 if ((qc->cursg_ofs * ATA_SECT_SIZE) == (&sg[qc->cursg])->length) {
3178 DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
3180 /* do the actual data transfer */
3181 do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
3182 ata_data_xfer(ap, buf, ATA_SECT_SIZE, do_write);
3188 * __atapi_pio_bytes - Transfer data from/to the ATAPI device.
3189 * @qc: Command on going
3190 * @bytes: number of bytes
3192 * Transfer Transfer data from/to the ATAPI device.
3195 * Inherited from caller.
3199 static void __atapi_pio_bytes(struct ata_queued_cmd *qc, unsigned int bytes)
3201 int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
3202 struct scatterlist *sg = qc->__sg;
3203 struct ata_port *ap = qc->ap;
3206 unsigned int offset, count;
3208 if (qc->curbytes + bytes >= qc->nbytes)
3209 ap->hsm_task_state = HSM_ST_LAST;
3212 if (unlikely(qc->cursg >= qc->n_elem)) {
3214 * The end of qc->sg is reached and the device expects
3215 * more data to transfer. In order not to overrun qc->sg
3216 * and fulfill length specified in the byte count register,
3217 * - for read case, discard trailing data from the device
3218 * - for write case, padding zero data to the device
3220 u16 pad_buf[1] = { 0 };
3221 unsigned int words = bytes >> 1;
3224 if (words) /* warning if bytes > 1 */
3225 printk(KERN_WARNING "ata%u: %u bytes trailing data\n",
3228 for (i = 0; i < words; i++)
3229 ata_data_xfer(ap, (unsigned char*)pad_buf, 2, do_write);
3231 ap->hsm_task_state = HSM_ST_LAST;
3235 sg = &qc->__sg[qc->cursg];
3238 offset = sg->offset + qc->cursg_ofs;
3240 /* get the current page and offset */
3241 page = nth_page(page, (offset >> PAGE_SHIFT));
3242 offset %= PAGE_SIZE;
3244 /* don't overrun current sg */
3245 count = min(sg->length - qc->cursg_ofs, bytes);
3247 /* don't cross page boundaries */
3248 count = min(count, (unsigned int)PAGE_SIZE - offset);
3250 buf = kmap(page) + offset;
3253 qc->curbytes += count;
3254 qc->cursg_ofs += count;
3256 if (qc->cursg_ofs == sg->length) {
3261 DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
3263 /* do the actual data transfer */
3264 ata_data_xfer(ap, buf, count, do_write);
3273 * atapi_pio_bytes - Transfer data from/to the ATAPI device.
3274 * @qc: Command on going
3276 * Transfer Transfer data from/to the ATAPI device.
3279 * Inherited from caller.
3282 static void atapi_pio_bytes(struct ata_queued_cmd *qc)
3284 struct ata_port *ap = qc->ap;
3285 struct ata_device *dev = qc->dev;
3286 unsigned int ireason, bc_lo, bc_hi, bytes;
3287 int i_write, do_write = (qc->tf.flags & ATA_TFLAG_WRITE) ? 1 : 0;
3289 ap->ops->tf_read(ap, &qc->tf);
3290 ireason = qc->tf.nsect;
3291 bc_lo = qc->tf.lbam;
3292 bc_hi = qc->tf.lbah;
3293 bytes = (bc_hi << 8) | bc_lo;
3295 /* shall be cleared to zero, indicating xfer of data */
3296 if (ireason & (1 << 0))
3299 /* make sure transfer direction matches expected */
3300 i_write = ((ireason & (1 << 1)) == 0) ? 1 : 0;
3301 if (do_write != i_write)
3304 __atapi_pio_bytes(qc, bytes);
3309 printk(KERN_INFO "ata%u: dev %u: ATAPI check failed\n",
3310 ap->id, dev->devno);
3311 qc->err_mask |= AC_ERR_ATA_BUS;
3312 ap->hsm_task_state = HSM_ST_ERR;
3316 * ata_pio_block - start PIO on a block
3317 * @ap: the target ata_port
3320 * None. (executing in kernel thread context)
3323 static void ata_pio_block(struct ata_port *ap)
3325 struct ata_queued_cmd *qc;
3329 * This is purely heuristic. This is a fast path.
3330 * Sometimes when we enter, BSY will be cleared in
3331 * a chk-status or two. If not, the drive is probably seeking
3332 * or something. Snooze for a couple msecs, then
3333 * chk-status again. If still busy, fall back to
3334 * HSM_ST_POLL state.
3336 status = ata_busy_wait(ap, ATA_BUSY, 5);
3337 if (status & ATA_BUSY) {
3339 status = ata_busy_wait(ap, ATA_BUSY, 10);
3340 if (status & ATA_BUSY) {
3341 ap->hsm_task_state = HSM_ST_POLL;
3342 ap->pio_task_timeout = jiffies + ATA_TMOUT_PIO;
3347 qc = ata_qc_from_tag(ap, ap->active_tag);
3351 if (status & (ATA_ERR | ATA_DF)) {
3352 qc->err_mask |= AC_ERR_DEV;
3353 ap->hsm_task_state = HSM_ST_ERR;
3357 /* transfer data if any */
3358 if (is_atapi_taskfile(&qc->tf)) {
3359 /* DRQ=0 means no more data to transfer */
3360 if ((status & ATA_DRQ) == 0) {
3361 ap->hsm_task_state = HSM_ST_LAST;
3365 atapi_pio_bytes(qc);
3367 /* handle BSY=0, DRQ=0 as error */
3368 if ((status & ATA_DRQ) == 0) {
3369 qc->err_mask |= AC_ERR_ATA_BUS;
3370 ap->hsm_task_state = HSM_ST_ERR;
3378 static void ata_pio_error(struct ata_port *ap)
3380 struct ata_queued_cmd *qc;
3382 printk(KERN_WARNING "ata%u: PIO error\n", ap->id);
3384 qc = ata_qc_from_tag(ap, ap->active_tag);
3387 /* make sure qc->err_mask is available to
3388 * know what's wrong and recover
3390 assert(qc->err_mask);
3392 ap->hsm_task_state = HSM_ST_IDLE;
3394 ata_poll_qc_complete(qc);
3397 static void ata_pio_task(void *_data)
3399 struct ata_port *ap = _data;
3400 unsigned long timeout;
3407 switch (ap->hsm_task_state) {
3416 qc_completed = ata_pio_complete(ap);
3420 case HSM_ST_LAST_POLL:
3421 timeout = ata_pio_poll(ap);
3431 queue_delayed_work(ata_wq, &ap->pio_task, timeout);
3432 else if (!qc_completed)
3437 * ata_qc_timeout - Handle timeout of queued command
3438 * @qc: Command that timed out
3440 * Some part of the kernel (currently, only the SCSI layer)
3441 * has noticed that the active command on port @ap has not
3442 * completed after a specified length of time. Handle this
3443 * condition by disabling DMA (if necessary) and completing
3444 * transactions, with error if necessary.
3446 * This also handles the case of the "lost interrupt", where
3447 * for some reason (possibly hardware bug, possibly driver bug)
3448 * an interrupt was not delivered to the driver, even though the
3449 * transaction completed successfully.
3452 * Inherited from SCSI layer (none, can sleep)
3455 static void ata_qc_timeout(struct ata_queued_cmd *qc)
3457 struct ata_port *ap = qc->ap;
3458 struct ata_host_set *host_set = ap->host_set;
3459 u8 host_stat = 0, drv_stat;
3460 unsigned long flags;
3464 spin_lock_irqsave(&host_set->lock, flags);
3466 /* hack alert! We cannot use the supplied completion
3467 * function from inside the ->eh_strategy_handler() thread.
3468 * libata is the only user of ->eh_strategy_handler() in
3469 * any kernel, so the default scsi_done() assumes it is
3470 * not being called from the SCSI EH.
3472 qc->scsidone = scsi_finish_command;
3474 switch (qc->tf.protocol) {
3477 case ATA_PROT_ATAPI_DMA:
3478 host_stat = ap->ops->bmdma_status(ap);
3480 /* before we do anything else, clear DMA-Start bit */
3481 ap->ops->bmdma_stop(qc);
3487 drv_stat = ata_chk_status(ap);
3489 /* ack bmdma irq events */
3490 ap->ops->irq_clear(ap);
3492 printk(KERN_ERR "ata%u: command 0x%x timeout, stat 0x%x host_stat 0x%x\n",
3493 ap->id, qc->tf.command, drv_stat, host_stat);
3495 /* complete taskfile transaction */
3496 qc->err_mask |= ac_err_mask(drv_stat);
3497 ata_qc_complete(qc);
3501 spin_unlock_irqrestore(&host_set->lock, flags);
3507 * ata_eng_timeout - Handle timeout of queued command
3508 * @ap: Port on which timed-out command is active
3510 * Some part of the kernel (currently, only the SCSI layer)
3511 * has noticed that the active command on port @ap has not
3512 * completed after a specified length of time. Handle this
3513 * condition by disabling DMA (if necessary) and completing
3514 * transactions, with error if necessary.
3516 * This also handles the case of the "lost interrupt", where
3517 * for some reason (possibly hardware bug, possibly driver bug)
3518 * an interrupt was not delivered to the driver, even though the
3519 * transaction completed successfully.
3522 * Inherited from SCSI layer (none, can sleep)
3525 void ata_eng_timeout(struct ata_port *ap)
3527 struct ata_queued_cmd *qc;
3531 qc = ata_qc_from_tag(ap, ap->active_tag);
3535 printk(KERN_ERR "ata%u: BUG: timeout without command\n",
3545 * ata_qc_new - Request an available ATA command, for queueing
3546 * @ap: Port associated with device @dev
3547 * @dev: Device from whom we request an available command structure
3553 static struct ata_queued_cmd *ata_qc_new(struct ata_port *ap)
3555 struct ata_queued_cmd *qc = NULL;
3558 for (i = 0; i < ATA_MAX_QUEUE; i++)
3559 if (!test_and_set_bit(i, &ap->qactive)) {
3560 qc = ata_qc_from_tag(ap, i);
3571 * ata_qc_new_init - Request an available ATA command, and initialize it
3572 * @ap: Port associated with device @dev
3573 * @dev: Device from whom we request an available command structure
3579 struct ata_queued_cmd *ata_qc_new_init(struct ata_port *ap,
3580 struct ata_device *dev)
3582 struct ata_queued_cmd *qc;
3584 qc = ata_qc_new(ap);
3596 static void __ata_qc_complete(struct ata_queued_cmd *qc)
3598 struct ata_port *ap = qc->ap;
3603 if (likely(ata_tag_valid(tag))) {
3604 if (tag == ap->active_tag)
3605 ap->active_tag = ATA_TAG_POISON;
3606 qc->tag = ATA_TAG_POISON;
3607 clear_bit(tag, &ap->qactive);
3612 * ata_qc_free - free unused ata_queued_cmd
3613 * @qc: Command to complete
3615 * Designed to free unused ata_queued_cmd object
3616 * in case something prevents using it.
3619 * spin_lock_irqsave(host_set lock)
3621 void ata_qc_free(struct ata_queued_cmd *qc)
3623 assert(qc != NULL); /* ata_qc_from_tag _might_ return NULL */
3625 __ata_qc_complete(qc);
3629 * ata_qc_complete - Complete an active ATA command
3630 * @qc: Command to complete
3631 * @err_mask: ATA Status register contents
3633 * Indicate to the mid and upper layers that an ATA
3634 * command has completed, with either an ok or not-ok status.
3637 * spin_lock_irqsave(host_set lock)
3640 void ata_qc_complete(struct ata_queued_cmd *qc)
3644 assert(qc != NULL); /* ata_qc_from_tag _might_ return NULL */
3645 assert(qc->flags & ATA_QCFLAG_ACTIVE);
3647 if (likely(qc->flags & ATA_QCFLAG_DMAMAP))
3650 /* atapi: mark qc as inactive to prevent the interrupt handler
3651 * from completing the command twice later, before the error handler
3652 * is called. (when rc != 0 and atapi request sense is needed)
3654 qc->flags &= ~ATA_QCFLAG_ACTIVE;
3656 /* call completion callback */
3657 rc = qc->complete_fn(qc);
3659 /* if callback indicates not to complete command (non-zero),
3660 * return immediately
3665 __ata_qc_complete(qc);
3670 static inline int ata_should_dma_map(struct ata_queued_cmd *qc)
3672 struct ata_port *ap = qc->ap;
3674 switch (qc->tf.protocol) {
3676 case ATA_PROT_ATAPI_DMA:
3679 case ATA_PROT_ATAPI:
3681 case ATA_PROT_PIO_MULT:
3682 if (ap->flags & ATA_FLAG_PIO_DMA)
3695 * ata_qc_issue - issue taskfile to device
3696 * @qc: command to issue to device
3698 * Prepare an ATA command to submission to device.
3699 * This includes mapping the data into a DMA-able
3700 * area, filling in the S/G table, and finally
3701 * writing the taskfile to hardware, starting the command.
3704 * spin_lock_irqsave(host_set lock)
3707 * Zero on success, negative on error.
3710 int ata_qc_issue(struct ata_queued_cmd *qc)
3712 struct ata_port *ap = qc->ap;
3714 if (ata_should_dma_map(qc)) {
3715 if (qc->flags & ATA_QCFLAG_SG) {
3716 if (ata_sg_setup(qc))
3718 } else if (qc->flags & ATA_QCFLAG_SINGLE) {
3719 if (ata_sg_setup_one(qc))
3723 qc->flags &= ~ATA_QCFLAG_DMAMAP;
3726 ap->ops->qc_prep(qc);
3728 qc->ap->active_tag = qc->tag;
3729 qc->flags |= ATA_QCFLAG_ACTIVE;
3731 return ap->ops->qc_issue(qc);
3739 * ata_qc_issue_prot - issue taskfile to device in proto-dependent manner
3740 * @qc: command to issue to device
3742 * Using various libata functions and hooks, this function
3743 * starts an ATA command. ATA commands are grouped into
3744 * classes called "protocols", and issuing each type of protocol
3745 * is slightly different.
3747 * May be used as the qc_issue() entry in ata_port_operations.
3750 * spin_lock_irqsave(host_set lock)
3753 * Zero on success, negative on error.
3756 int ata_qc_issue_prot(struct ata_queued_cmd *qc)
3758 struct ata_port *ap = qc->ap;
3760 ata_dev_select(ap, qc->dev->devno, 1, 0);
3762 switch (qc->tf.protocol) {
3763 case ATA_PROT_NODATA:
3764 ata_tf_to_host(ap, &qc->tf);
3768 ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
3769 ap->ops->bmdma_setup(qc); /* set up bmdma */
3770 ap->ops->bmdma_start(qc); /* initiate bmdma */
3773 case ATA_PROT_PIO: /* load tf registers, initiate polling pio */
3774 ata_qc_set_polling(qc);
3775 ata_tf_to_host(ap, &qc->tf);
3776 ap->hsm_task_state = HSM_ST;
3777 queue_work(ata_wq, &ap->pio_task);
3780 case ATA_PROT_ATAPI:
3781 ata_qc_set_polling(qc);
3782 ata_tf_to_host(ap, &qc->tf);
3783 queue_work(ata_wq, &ap->packet_task);
3786 case ATA_PROT_ATAPI_NODATA:
3787 ap->flags |= ATA_FLAG_NOINTR;
3788 ata_tf_to_host(ap, &qc->tf);
3789 queue_work(ata_wq, &ap->packet_task);
3792 case ATA_PROT_ATAPI_DMA:
3793 ap->flags |= ATA_FLAG_NOINTR;
3794 ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
3795 ap->ops->bmdma_setup(qc); /* set up bmdma */
3796 queue_work(ata_wq, &ap->packet_task);
3808 * ata_bmdma_setup_mmio - Set up PCI IDE BMDMA transaction
3809 * @qc: Info associated with this ATA transaction.
3812 * spin_lock_irqsave(host_set lock)
3815 static void ata_bmdma_setup_mmio (struct ata_queued_cmd *qc)
3817 struct ata_port *ap = qc->ap;
3818 unsigned int rw = (qc->tf.flags & ATA_TFLAG_WRITE);
3820 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
3822 /* load PRD table addr. */
3823 mb(); /* make sure PRD table writes are visible to controller */
3824 writel(ap->prd_dma, mmio + ATA_DMA_TABLE_OFS);
3826 /* specify data direction, triple-check start bit is clear */
3827 dmactl = readb(mmio + ATA_DMA_CMD);
3828 dmactl &= ~(ATA_DMA_WR | ATA_DMA_START);
3830 dmactl |= ATA_DMA_WR;
3831 writeb(dmactl, mmio + ATA_DMA_CMD);
3833 /* issue r/w command */
3834 ap->ops->exec_command(ap, &qc->tf);
3838 * ata_bmdma_start_mmio - Start a PCI IDE BMDMA transaction
3839 * @qc: Info associated with this ATA transaction.
3842 * spin_lock_irqsave(host_set lock)
3845 static void ata_bmdma_start_mmio (struct ata_queued_cmd *qc)
3847 struct ata_port *ap = qc->ap;
3848 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
3851 /* start host DMA transaction */
3852 dmactl = readb(mmio + ATA_DMA_CMD);
3853 writeb(dmactl | ATA_DMA_START, mmio + ATA_DMA_CMD);
3855 /* Strictly, one may wish to issue a readb() here, to
3856 * flush the mmio write. However, control also passes
3857 * to the hardware at this point, and it will interrupt
3858 * us when we are to resume control. So, in effect,
3859 * we don't care when the mmio write flushes.
3860 * Further, a read of the DMA status register _immediately_
3861 * following the write may not be what certain flaky hardware
3862 * is expected, so I think it is best to not add a readb()
3863 * without first all the MMIO ATA cards/mobos.
3864 * Or maybe I'm just being paranoid.
3869 * ata_bmdma_setup_pio - Set up PCI IDE BMDMA transaction (PIO)
3870 * @qc: Info associated with this ATA transaction.
3873 * spin_lock_irqsave(host_set lock)
3876 static void ata_bmdma_setup_pio (struct ata_queued_cmd *qc)
3878 struct ata_port *ap = qc->ap;
3879 unsigned int rw = (qc->tf.flags & ATA_TFLAG_WRITE);
3882 /* load PRD table addr. */
3883 outl(ap->prd_dma, ap->ioaddr.bmdma_addr + ATA_DMA_TABLE_OFS);
3885 /* specify data direction, triple-check start bit is clear */
3886 dmactl = inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3887 dmactl &= ~(ATA_DMA_WR | ATA_DMA_START);
3889 dmactl |= ATA_DMA_WR;
3890 outb(dmactl, ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3892 /* issue r/w command */
3893 ap->ops->exec_command(ap, &qc->tf);
3897 * ata_bmdma_start_pio - Start a PCI IDE BMDMA transaction (PIO)
3898 * @qc: Info associated with this ATA transaction.
3901 * spin_lock_irqsave(host_set lock)
3904 static void ata_bmdma_start_pio (struct ata_queued_cmd *qc)
3906 struct ata_port *ap = qc->ap;
3909 /* start host DMA transaction */
3910 dmactl = inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3911 outb(dmactl | ATA_DMA_START,
3912 ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3917 * ata_bmdma_start - Start a PCI IDE BMDMA transaction
3918 * @qc: Info associated with this ATA transaction.
3920 * Writes the ATA_DMA_START flag to the DMA command register.
3922 * May be used as the bmdma_start() entry in ata_port_operations.
3925 * spin_lock_irqsave(host_set lock)
3927 void ata_bmdma_start(struct ata_queued_cmd *qc)
3929 if (qc->ap->flags & ATA_FLAG_MMIO)
3930 ata_bmdma_start_mmio(qc);
3932 ata_bmdma_start_pio(qc);
3937 * ata_bmdma_setup - Set up PCI IDE BMDMA transaction
3938 * @qc: Info associated with this ATA transaction.
3940 * Writes address of PRD table to device's PRD Table Address
3941 * register, sets the DMA control register, and calls
3942 * ops->exec_command() to start the transfer.
3944 * May be used as the bmdma_setup() entry in ata_port_operations.
3947 * spin_lock_irqsave(host_set lock)
3949 void ata_bmdma_setup(struct ata_queued_cmd *qc)
3951 if (qc->ap->flags & ATA_FLAG_MMIO)
3952 ata_bmdma_setup_mmio(qc);
3954 ata_bmdma_setup_pio(qc);
3959 * ata_bmdma_irq_clear - Clear PCI IDE BMDMA interrupt.
3960 * @ap: Port associated with this ATA transaction.
3962 * Clear interrupt and error flags in DMA status register.
3964 * May be used as the irq_clear() entry in ata_port_operations.
3967 * spin_lock_irqsave(host_set lock)
3970 void ata_bmdma_irq_clear(struct ata_port *ap)
3972 if (ap->flags & ATA_FLAG_MMIO) {
3973 void __iomem *mmio = ((void __iomem *) ap->ioaddr.bmdma_addr) + ATA_DMA_STATUS;
3974 writeb(readb(mmio), mmio);
3976 unsigned long addr = ap->ioaddr.bmdma_addr + ATA_DMA_STATUS;
3977 outb(inb(addr), addr);
3984 * ata_bmdma_status - Read PCI IDE BMDMA status
3985 * @ap: Port associated with this ATA transaction.
3987 * Read and return BMDMA status register.
3989 * May be used as the bmdma_status() entry in ata_port_operations.
3992 * spin_lock_irqsave(host_set lock)
3995 u8 ata_bmdma_status(struct ata_port *ap)
3998 if (ap->flags & ATA_FLAG_MMIO) {
3999 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
4000 host_stat = readb(mmio + ATA_DMA_STATUS);
4002 host_stat = inb(ap->ioaddr.bmdma_addr + ATA_DMA_STATUS);
4008 * ata_bmdma_stop - Stop PCI IDE BMDMA transfer
4009 * @qc: Command we are ending DMA for
4011 * Clears the ATA_DMA_START flag in the dma control register
4013 * May be used as the bmdma_stop() entry in ata_port_operations.
4016 * spin_lock_irqsave(host_set lock)
4019 void ata_bmdma_stop(struct ata_queued_cmd *qc)
4021 struct ata_port *ap = qc->ap;
4022 if (ap->flags & ATA_FLAG_MMIO) {
4023 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
4025 /* clear start/stop bit */
4026 writeb(readb(mmio + ATA_DMA_CMD) & ~ATA_DMA_START,
4027 mmio + ATA_DMA_CMD);
4029 /* clear start/stop bit */
4030 outb(inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD) & ~ATA_DMA_START,
4031 ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
4034 /* one-PIO-cycle guaranteed wait, per spec, for HDMA1:0 transition */
4035 ata_altstatus(ap); /* dummy read */
4039 * ata_host_intr - Handle host interrupt for given (port, task)
4040 * @ap: Port on which interrupt arrived (possibly...)
4041 * @qc: Taskfile currently active in engine
4043 * Handle host interrupt for given queued command. Currently,
4044 * only DMA interrupts are handled. All other commands are
4045 * handled via polling with interrupts disabled (nIEN bit).
4048 * spin_lock_irqsave(host_set lock)
4051 * One if interrupt was handled, zero if not (shared irq).
4054 inline unsigned int ata_host_intr (struct ata_port *ap,
4055 struct ata_queued_cmd *qc)
4057 u8 status, host_stat;
4059 switch (qc->tf.protocol) {
4062 case ATA_PROT_ATAPI_DMA:
4063 case ATA_PROT_ATAPI:
4064 /* check status of DMA engine */
4065 host_stat = ap->ops->bmdma_status(ap);
4066 VPRINTK("ata%u: host_stat 0x%X\n", ap->id, host_stat);
4068 /* if it's not our irq... */
4069 if (!(host_stat & ATA_DMA_INTR))
4072 /* before we do anything else, clear DMA-Start bit */
4073 ap->ops->bmdma_stop(qc);
4077 case ATA_PROT_ATAPI_NODATA:
4078 case ATA_PROT_NODATA:
4079 /* check altstatus */
4080 status = ata_altstatus(ap);
4081 if (status & ATA_BUSY)
4084 /* check main status, clearing INTRQ */
4085 status = ata_chk_status(ap);
4086 if (unlikely(status & ATA_BUSY))
4088 DPRINTK("ata%u: protocol %d (dev_stat 0x%X)\n",
4089 ap->id, qc->tf.protocol, status);
4091 /* ack bmdma irq events */
4092 ap->ops->irq_clear(ap);
4094 /* complete taskfile transaction */
4095 qc->err_mask |= ac_err_mask(status);
4096 ata_qc_complete(qc);
4103 return 1; /* irq handled */
4106 ap->stats.idle_irq++;
4109 if ((ap->stats.idle_irq % 1000) == 0) {
4111 ata_irq_ack(ap, 0); /* debug trap */
4112 printk(KERN_WARNING "ata%d: irq trap\n", ap->id);
4115 return 0; /* irq not handled */
4119 * ata_interrupt - Default ATA host interrupt handler
4120 * @irq: irq line (unused)
4121 * @dev_instance: pointer to our ata_host_set information structure
4124 * Default interrupt handler for PCI IDE devices. Calls
4125 * ata_host_intr() for each port that is not disabled.
4128 * Obtains host_set lock during operation.
4131 * IRQ_NONE or IRQ_HANDLED.
4134 irqreturn_t ata_interrupt (int irq, void *dev_instance, struct pt_regs *regs)
4136 struct ata_host_set *host_set = dev_instance;
4138 unsigned int handled = 0;
4139 unsigned long flags;
4141 /* TODO: make _irqsave conditional on x86 PCI IDE legacy mode */
4142 spin_lock_irqsave(&host_set->lock, flags);
4144 for (i = 0; i < host_set->n_ports; i++) {
4145 struct ata_port *ap;
4147 ap = host_set->ports[i];
4149 !(ap->flags & (ATA_FLAG_PORT_DISABLED | ATA_FLAG_NOINTR))) {
4150 struct ata_queued_cmd *qc;
4152 qc = ata_qc_from_tag(ap, ap->active_tag);
4153 if (qc && (!(qc->tf.ctl & ATA_NIEN)) &&
4154 (qc->flags & ATA_QCFLAG_ACTIVE))
4155 handled |= ata_host_intr(ap, qc);
4159 spin_unlock_irqrestore(&host_set->lock, flags);
4161 return IRQ_RETVAL(handled);
4165 * atapi_packet_task - Write CDB bytes to hardware
4166 * @_data: Port to which ATAPI device is attached.
4168 * When device has indicated its readiness to accept
4169 * a CDB, this function is called. Send the CDB.
4170 * If DMA is to be performed, exit immediately.
4171 * Otherwise, we are in polling mode, so poll
4172 * status under operation succeeds or fails.
4175 * Kernel thread context (may sleep)
4178 static void atapi_packet_task(void *_data)
4180 struct ata_port *ap = _data;
4181 struct ata_queued_cmd *qc;
4184 qc = ata_qc_from_tag(ap, ap->active_tag);
4186 assert(qc->flags & ATA_QCFLAG_ACTIVE);
4188 /* sleep-wait for BSY to clear */
4189 DPRINTK("busy wait\n");
4190 if (ata_busy_sleep(ap, ATA_TMOUT_CDB_QUICK, ATA_TMOUT_CDB)) {
4191 qc->err_mask |= AC_ERR_ATA_BUS;
4195 /* make sure DRQ is set */
4196 status = ata_chk_status(ap);
4197 if ((status & (ATA_BUSY | ATA_DRQ)) != ATA_DRQ) {
4198 qc->err_mask |= AC_ERR_ATA_BUS;
4203 DPRINTK("send cdb\n");
4204 assert(ap->cdb_len >= 12);
4206 if (qc->tf.protocol == ATA_PROT_ATAPI_DMA ||
4207 qc->tf.protocol == ATA_PROT_ATAPI_NODATA) {
4208 unsigned long flags;
4210 /* Once we're done issuing command and kicking bmdma,
4211 * irq handler takes over. To not lose irq, we need
4212 * to clear NOINTR flag before sending cdb, but
4213 * interrupt handler shouldn't be invoked before we're
4214 * finished. Hence, the following locking.
4216 spin_lock_irqsave(&ap->host_set->lock, flags);
4217 ap->flags &= ~ATA_FLAG_NOINTR;
4218 ata_data_xfer(ap, qc->cdb, ap->cdb_len, 1);
4219 if (qc->tf.protocol == ATA_PROT_ATAPI_DMA)
4220 ap->ops->bmdma_start(qc); /* initiate bmdma */
4221 spin_unlock_irqrestore(&ap->host_set->lock, flags);
4223 ata_data_xfer(ap, qc->cdb, ap->cdb_len, 1);
4225 /* PIO commands are handled by polling */
4226 ap->hsm_task_state = HSM_ST;
4227 queue_work(ata_wq, &ap->pio_task);
4233 ata_poll_qc_complete(qc);
4238 * ata_port_start - Set port up for dma.
4239 * @ap: Port to initialize
4241 * Called just after data structures for each port are
4242 * initialized. Allocates space for PRD table.
4244 * May be used as the port_start() entry in ata_port_operations.
4247 * Inherited from caller.
4251 * Execute a 'simple' command, that only consists of the opcode 'cmd' itself,
4252 * without filling any other registers
4254 static int ata_do_simple_cmd(struct ata_port *ap, struct ata_device *dev,
4257 struct ata_taskfile tf;
4260 ata_tf_init(ap, &tf, dev->devno);
4263 tf.flags |= ATA_TFLAG_DEVICE;
4264 tf.protocol = ATA_PROT_NODATA;
4266 err = ata_exec_internal(ap, dev, &tf, DMA_NONE, NULL, 0);
4268 printk(KERN_ERR "%s: ata command failed: %d\n",
4274 static int ata_flush_cache(struct ata_port *ap, struct ata_device *dev)
4278 if (!ata_try_flush_cache(dev))
4281 if (ata_id_has_flush_ext(dev->id))
4282 cmd = ATA_CMD_FLUSH_EXT;
4284 cmd = ATA_CMD_FLUSH;
4286 return ata_do_simple_cmd(ap, dev, cmd);
4289 static int ata_standby_drive(struct ata_port *ap, struct ata_device *dev)
4291 return ata_do_simple_cmd(ap, dev, ATA_CMD_STANDBYNOW1);
4294 static int ata_start_drive(struct ata_port *ap, struct ata_device *dev)
4296 return ata_do_simple_cmd(ap, dev, ATA_CMD_IDLEIMMEDIATE);
4300 * ata_device_resume - wakeup a previously suspended devices
4302 * Kick the drive back into action, by sending it an idle immediate
4303 * command and making sure its transfer mode matches between drive
4307 int ata_device_resume(struct ata_port *ap, struct ata_device *dev)
4309 if (ap->flags & ATA_FLAG_SUSPENDED) {
4310 ap->flags &= ~ATA_FLAG_SUSPENDED;
4313 if (!ata_dev_present(dev))
4315 if (dev->class == ATA_DEV_ATA)
4316 ata_start_drive(ap, dev);
4322 * ata_device_suspend - prepare a device for suspend
4324 * Flush the cache on the drive, if appropriate, then issue a
4325 * standbynow command.
4328 int ata_device_suspend(struct ata_port *ap, struct ata_device *dev)
4330 if (!ata_dev_present(dev))
4332 if (dev->class == ATA_DEV_ATA)
4333 ata_flush_cache(ap, dev);
4335 ata_standby_drive(ap, dev);
4336 ap->flags |= ATA_FLAG_SUSPENDED;
4340 int ata_port_start (struct ata_port *ap)
4342 struct device *dev = ap->host_set->dev;
4345 ap->prd = dma_alloc_coherent(dev, ATA_PRD_TBL_SZ, &ap->prd_dma, GFP_KERNEL);
4349 rc = ata_pad_alloc(ap, dev);
4351 dma_free_coherent(dev, ATA_PRD_TBL_SZ, ap->prd, ap->prd_dma);
4355 DPRINTK("prd alloc, virt %p, dma %llx\n", ap->prd, (unsigned long long) ap->prd_dma);
4362 * ata_port_stop - Undo ata_port_start()
4363 * @ap: Port to shut down
4365 * Frees the PRD table.
4367 * May be used as the port_stop() entry in ata_port_operations.
4370 * Inherited from caller.
4373 void ata_port_stop (struct ata_port *ap)
4375 struct device *dev = ap->host_set->dev;
4377 dma_free_coherent(dev, ATA_PRD_TBL_SZ, ap->prd, ap->prd_dma);
4378 ata_pad_free(ap, dev);
4381 void ata_host_stop (struct ata_host_set *host_set)
4383 if (host_set->mmio_base)
4384 iounmap(host_set->mmio_base);
4389 * ata_host_remove - Unregister SCSI host structure with upper layers
4390 * @ap: Port to unregister
4391 * @do_unregister: 1 if we fully unregister, 0 to just stop the port
4394 * Inherited from caller.
4397 static void ata_host_remove(struct ata_port *ap, unsigned int do_unregister)
4399 struct Scsi_Host *sh = ap->host;
4404 scsi_remove_host(sh);
4406 ap->ops->port_stop(ap);
4410 * ata_host_init - Initialize an ata_port structure
4411 * @ap: Structure to initialize
4412 * @host: associated SCSI mid-layer structure
4413 * @host_set: Collection of hosts to which @ap belongs
4414 * @ent: Probe information provided by low-level driver
4415 * @port_no: Port number associated with this ata_port
4417 * Initialize a new ata_port structure, and its associated
4421 * Inherited from caller.
4424 static void ata_host_init(struct ata_port *ap, struct Scsi_Host *host,
4425 struct ata_host_set *host_set,
4426 const struct ata_probe_ent *ent, unsigned int port_no)
4432 host->max_channel = 1;
4433 host->unique_id = ata_unique_id++;
4434 host->max_cmd_len = 12;
4436 ap->flags = ATA_FLAG_PORT_DISABLED;
4437 ap->id = host->unique_id;
4439 ap->ctl = ATA_DEVCTL_OBS;
4440 ap->host_set = host_set;
4441 ap->port_no = port_no;
4443 ent->legacy_mode ? ent->hard_port_no : port_no;
4444 ap->pio_mask = ent->pio_mask;
4445 ap->mwdma_mask = ent->mwdma_mask;
4446 ap->udma_mask = ent->udma_mask;
4447 ap->flags |= ent->host_flags;
4448 ap->ops = ent->port_ops;
4449 ap->cbl = ATA_CBL_NONE;
4450 ap->active_tag = ATA_TAG_POISON;
4451 ap->last_ctl = 0xFF;
4453 INIT_WORK(&ap->packet_task, atapi_packet_task, ap);
4454 INIT_WORK(&ap->pio_task, ata_pio_task, ap);
4456 for (i = 0; i < ATA_MAX_DEVICES; i++)
4457 ap->device[i].devno = i;
4460 ap->stats.unhandled_irq = 1;
4461 ap->stats.idle_irq = 1;
4464 memcpy(&ap->ioaddr, &ent->port[port_no], sizeof(struct ata_ioports));
4468 * ata_host_add - Attach low-level ATA driver to system
4469 * @ent: Information provided by low-level driver
4470 * @host_set: Collections of ports to which we add
4471 * @port_no: Port number associated with this host
4473 * Attach low-level ATA driver to system.
4476 * PCI/etc. bus probe sem.
4479 * New ata_port on success, for NULL on error.
4482 static struct ata_port * ata_host_add(const struct ata_probe_ent *ent,
4483 struct ata_host_set *host_set,
4484 unsigned int port_no)
4486 struct Scsi_Host *host;
4487 struct ata_port *ap;
4491 host = scsi_host_alloc(ent->sht, sizeof(struct ata_port));
4495 ap = (struct ata_port *) &host->hostdata[0];
4497 ata_host_init(ap, host, host_set, ent, port_no);
4499 rc = ap->ops->port_start(ap);
4506 scsi_host_put(host);
4511 * ata_device_add - Register hardware device with ATA and SCSI layers
4512 * @ent: Probe information describing hardware device to be registered
4514 * This function processes the information provided in the probe
4515 * information struct @ent, allocates the necessary ATA and SCSI
4516 * host information structures, initializes them, and registers
4517 * everything with requisite kernel subsystems.
4519 * This function requests irqs, probes the ATA bus, and probes
4523 * PCI/etc. bus probe sem.
4526 * Number of ports registered. Zero on error (no ports registered).
4529 int ata_device_add(const struct ata_probe_ent *ent)
4531 unsigned int count = 0, i;
4532 struct device *dev = ent->dev;
4533 struct ata_host_set *host_set;
4536 /* alloc a container for our list of ATA ports (buses) */
4537 host_set = kzalloc(sizeof(struct ata_host_set) +
4538 (ent->n_ports * sizeof(void *)), GFP_KERNEL);
4541 spin_lock_init(&host_set->lock);
4543 host_set->dev = dev;
4544 host_set->n_ports = ent->n_ports;
4545 host_set->irq = ent->irq;
4546 host_set->mmio_base = ent->mmio_base;
4547 host_set->private_data = ent->private_data;
4548 host_set->ops = ent->port_ops;
4550 /* register each port bound to this device */
4551 for (i = 0; i < ent->n_ports; i++) {
4552 struct ata_port *ap;
4553 unsigned long xfer_mode_mask;
4555 ap = ata_host_add(ent, host_set, i);
4559 host_set->ports[i] = ap;
4560 xfer_mode_mask =(ap->udma_mask << ATA_SHIFT_UDMA) |
4561 (ap->mwdma_mask << ATA_SHIFT_MWDMA) |
4562 (ap->pio_mask << ATA_SHIFT_PIO);
4564 /* print per-port info to dmesg */
4565 printk(KERN_INFO "ata%u: %cATA max %s cmd 0x%lX ctl 0x%lX "
4566 "bmdma 0x%lX irq %lu\n",
4568 ap->flags & ATA_FLAG_SATA ? 'S' : 'P',
4569 ata_mode_string(xfer_mode_mask),
4570 ap->ioaddr.cmd_addr,
4571 ap->ioaddr.ctl_addr,
4572 ap->ioaddr.bmdma_addr,
4576 host_set->ops->irq_clear(ap);
4583 /* obtain irq, that is shared between channels */
4584 if (request_irq(ent->irq, ent->port_ops->irq_handler, ent->irq_flags,
4585 DRV_NAME, host_set))
4588 /* perform each probe synchronously */
4589 DPRINTK("probe begin\n");
4590 for (i = 0; i < count; i++) {
4591 struct ata_port *ap;
4594 ap = host_set->ports[i];
4596 DPRINTK("ata%u: probe begin\n", ap->id);
4597 rc = ata_bus_probe(ap);
4598 DPRINTK("ata%u: probe end\n", ap->id);
4601 /* FIXME: do something useful here?
4602 * Current libata behavior will
4603 * tear down everything when
4604 * the module is removed
4605 * or the h/w is unplugged.
4609 rc = scsi_add_host(ap->host, dev);
4611 printk(KERN_ERR "ata%u: scsi_add_host failed\n",
4613 /* FIXME: do something useful here */
4614 /* FIXME: handle unconditional calls to
4615 * scsi_scan_host and ata_host_remove, below,
4621 /* probes are done, now scan each port's disk(s) */
4622 DPRINTK("probe begin\n");
4623 for (i = 0; i < count; i++) {
4624 struct ata_port *ap = host_set->ports[i];
4626 ata_scsi_scan_host(ap);
4629 dev_set_drvdata(dev, host_set);
4631 VPRINTK("EXIT, returning %u\n", ent->n_ports);
4632 return ent->n_ports; /* success */
4635 for (i = 0; i < count; i++) {
4636 ata_host_remove(host_set->ports[i], 1);
4637 scsi_host_put(host_set->ports[i]->host);
4641 VPRINTK("EXIT, returning 0\n");
4646 * ata_host_set_remove - PCI layer callback for device removal
4647 * @host_set: ATA host set that was removed
4649 * Unregister all objects associated with this host set. Free those
4653 * Inherited from calling layer (may sleep).
4656 void ata_host_set_remove(struct ata_host_set *host_set)
4658 struct ata_port *ap;
4661 for (i = 0; i < host_set->n_ports; i++) {
4662 ap = host_set->ports[i];
4663 scsi_remove_host(ap->host);
4666 free_irq(host_set->irq, host_set);
4668 for (i = 0; i < host_set->n_ports; i++) {
4669 ap = host_set->ports[i];
4671 ata_scsi_release(ap->host);
4673 if ((ap->flags & ATA_FLAG_NO_LEGACY) == 0) {
4674 struct ata_ioports *ioaddr = &ap->ioaddr;
4676 if (ioaddr->cmd_addr == 0x1f0)
4677 release_region(0x1f0, 8);
4678 else if (ioaddr->cmd_addr == 0x170)
4679 release_region(0x170, 8);
4682 scsi_host_put(ap->host);
4685 if (host_set->ops->host_stop)
4686 host_set->ops->host_stop(host_set);
4692 * ata_scsi_release - SCSI layer callback hook for host unload
4693 * @host: libata host to be unloaded
4695 * Performs all duties necessary to shut down a libata port...
4696 * Kill port kthread, disable port, and release resources.
4699 * Inherited from SCSI layer.
4705 int ata_scsi_release(struct Scsi_Host *host)
4707 struct ata_port *ap = (struct ata_port *) &host->hostdata[0];
4711 ap->ops->port_disable(ap);
4712 ata_host_remove(ap, 0);
4719 * ata_std_ports - initialize ioaddr with standard port offsets.
4720 * @ioaddr: IO address structure to be initialized
4722 * Utility function which initializes data_addr, error_addr,
4723 * feature_addr, nsect_addr, lbal_addr, lbam_addr, lbah_addr,
4724 * device_addr, status_addr, and command_addr to standard offsets
4725 * relative to cmd_addr.
4727 * Does not set ctl_addr, altstatus_addr, bmdma_addr, or scr_addr.
4730 void ata_std_ports(struct ata_ioports *ioaddr)
4732 ioaddr->data_addr = ioaddr->cmd_addr + ATA_REG_DATA;
4733 ioaddr->error_addr = ioaddr->cmd_addr + ATA_REG_ERR;
4734 ioaddr->feature_addr = ioaddr->cmd_addr + ATA_REG_FEATURE;
4735 ioaddr->nsect_addr = ioaddr->cmd_addr + ATA_REG_NSECT;
4736 ioaddr->lbal_addr = ioaddr->cmd_addr + ATA_REG_LBAL;
4737 ioaddr->lbam_addr = ioaddr->cmd_addr + ATA_REG_LBAM;
4738 ioaddr->lbah_addr = ioaddr->cmd_addr + ATA_REG_LBAH;
4739 ioaddr->device_addr = ioaddr->cmd_addr + ATA_REG_DEVICE;
4740 ioaddr->status_addr = ioaddr->cmd_addr + ATA_REG_STATUS;
4741 ioaddr->command_addr = ioaddr->cmd_addr + ATA_REG_CMD;
4744 static struct ata_probe_ent *
4745 ata_probe_ent_alloc(struct device *dev, const struct ata_port_info *port)
4747 struct ata_probe_ent *probe_ent;
4749 probe_ent = kzalloc(sizeof(*probe_ent), GFP_KERNEL);
4751 printk(KERN_ERR DRV_NAME "(%s): out of memory\n",
4752 kobject_name(&(dev->kobj)));
4756 INIT_LIST_HEAD(&probe_ent->node);
4757 probe_ent->dev = dev;
4759 probe_ent->sht = port->sht;
4760 probe_ent->host_flags = port->host_flags;
4761 probe_ent->pio_mask = port->pio_mask;
4762 probe_ent->mwdma_mask = port->mwdma_mask;
4763 probe_ent->udma_mask = port->udma_mask;
4764 probe_ent->port_ops = port->port_ops;
4773 void ata_pci_host_stop (struct ata_host_set *host_set)
4775 struct pci_dev *pdev = to_pci_dev(host_set->dev);
4777 pci_iounmap(pdev, host_set->mmio_base);
4781 * ata_pci_init_native_mode - Initialize native-mode driver
4782 * @pdev: pci device to be initialized
4783 * @port: array[2] of pointers to port info structures.
4784 * @ports: bitmap of ports present
4786 * Utility function which allocates and initializes an
4787 * ata_probe_ent structure for a standard dual-port
4788 * PIO-based IDE controller. The returned ata_probe_ent
4789 * structure can be passed to ata_device_add(). The returned
4790 * ata_probe_ent structure should then be freed with kfree().
4792 * The caller need only pass the address of the primary port, the
4793 * secondary will be deduced automatically. If the device has non
4794 * standard secondary port mappings this function can be called twice,
4795 * once for each interface.
4798 struct ata_probe_ent *
4799 ata_pci_init_native_mode(struct pci_dev *pdev, struct ata_port_info **port, int ports)
4801 struct ata_probe_ent *probe_ent =
4802 ata_probe_ent_alloc(pci_dev_to_dev(pdev), port[0]);
4808 probe_ent->irq = pdev->irq;
4809 probe_ent->irq_flags = SA_SHIRQ;
4810 probe_ent->private_data = port[0]->private_data;
4812 if (ports & ATA_PORT_PRIMARY) {
4813 probe_ent->port[p].cmd_addr = pci_resource_start(pdev, 0);
4814 probe_ent->port[p].altstatus_addr =
4815 probe_ent->port[p].ctl_addr =
4816 pci_resource_start(pdev, 1) | ATA_PCI_CTL_OFS;
4817 probe_ent->port[p].bmdma_addr = pci_resource_start(pdev, 4);
4818 ata_std_ports(&probe_ent->port[p]);
4822 if (ports & ATA_PORT_SECONDARY) {
4823 probe_ent->port[p].cmd_addr = pci_resource_start(pdev, 2);
4824 probe_ent->port[p].altstatus_addr =
4825 probe_ent->port[p].ctl_addr =
4826 pci_resource_start(pdev, 3) | ATA_PCI_CTL_OFS;
4827 probe_ent->port[p].bmdma_addr = pci_resource_start(pdev, 4) + 8;
4828 ata_std_ports(&probe_ent->port[p]);
4832 probe_ent->n_ports = p;
4836 static struct ata_probe_ent *ata_pci_init_legacy_port(struct pci_dev *pdev, struct ata_port_info *port, int port_num)
4838 struct ata_probe_ent *probe_ent;
4840 probe_ent = ata_probe_ent_alloc(pci_dev_to_dev(pdev), port);
4844 probe_ent->legacy_mode = 1;
4845 probe_ent->n_ports = 1;
4846 probe_ent->hard_port_no = port_num;
4847 probe_ent->private_data = port->private_data;
4852 probe_ent->irq = 14;
4853 probe_ent->port[0].cmd_addr = 0x1f0;
4854 probe_ent->port[0].altstatus_addr =
4855 probe_ent->port[0].ctl_addr = 0x3f6;
4858 probe_ent->irq = 15;
4859 probe_ent->port[0].cmd_addr = 0x170;
4860 probe_ent->port[0].altstatus_addr =
4861 probe_ent->port[0].ctl_addr = 0x376;
4864 probe_ent->port[0].bmdma_addr = pci_resource_start(pdev, 4) + 8 * port_num;
4865 ata_std_ports(&probe_ent->port[0]);
4870 * ata_pci_init_one - Initialize/register PCI IDE host controller
4871 * @pdev: Controller to be initialized
4872 * @port_info: Information from low-level host driver
4873 * @n_ports: Number of ports attached to host controller
4875 * This is a helper function which can be called from a driver's
4876 * xxx_init_one() probe function if the hardware uses traditional
4877 * IDE taskfile registers.
4879 * This function calls pci_enable_device(), reserves its register
4880 * regions, sets the dma mask, enables bus master mode, and calls
4884 * Inherited from PCI layer (may sleep).
4887 * Zero on success, negative on errno-based value on error.
4890 int ata_pci_init_one (struct pci_dev *pdev, struct ata_port_info **port_info,
4891 unsigned int n_ports)
4893 struct ata_probe_ent *probe_ent = NULL, *probe_ent2 = NULL;
4894 struct ata_port_info *port[2];
4896 unsigned int legacy_mode = 0;
4897 int disable_dev_on_err = 1;
4902 port[0] = port_info[0];
4904 port[1] = port_info[1];
4908 if ((port[0]->host_flags & ATA_FLAG_NO_LEGACY) == 0
4909 && (pdev->class >> 8) == PCI_CLASS_STORAGE_IDE) {
4910 /* TODO: What if one channel is in native mode ... */
4911 pci_read_config_byte(pdev, PCI_CLASS_PROG, &tmp8);
4912 mask = (1 << 2) | (1 << 0);
4913 if ((tmp8 & mask) != mask)
4914 legacy_mode = (1 << 3);
4918 if ((!legacy_mode) && (n_ports > 2)) {
4919 printk(KERN_ERR "ata: BUG: native mode, n_ports > 2\n");
4924 /* FIXME: Really for ATA it isn't safe because the device may be
4925 multi-purpose and we want to leave it alone if it was already
4926 enabled. Secondly for shared use as Arjan says we want refcounting
4928 Checking dev->is_enabled is insufficient as this is not set at
4929 boot for the primary video which is BIOS enabled
4932 rc = pci_enable_device(pdev);
4936 rc = pci_request_regions(pdev, DRV_NAME);
4938 disable_dev_on_err = 0;
4942 /* FIXME: Should use platform specific mappers for legacy port ranges */
4944 if (!request_region(0x1f0, 8, "libata")) {
4945 struct resource *conflict, res;
4947 res.end = 0x1f0 + 8 - 1;
4948 conflict = ____request_resource(&ioport_resource, &res);
4949 if (!strcmp(conflict->name, "libata"))
4950 legacy_mode |= (1 << 0);
4952 disable_dev_on_err = 0;
4953 printk(KERN_WARNING "ata: 0x1f0 IDE port busy\n");
4956 legacy_mode |= (1 << 0);
4958 if (!request_region(0x170, 8, "libata")) {
4959 struct resource *conflict, res;
4961 res.end = 0x170 + 8 - 1;
4962 conflict = ____request_resource(&ioport_resource, &res);
4963 if (!strcmp(conflict->name, "libata"))
4964 legacy_mode |= (1 << 1);
4966 disable_dev_on_err = 0;
4967 printk(KERN_WARNING "ata: 0x170 IDE port busy\n");
4970 legacy_mode |= (1 << 1);
4973 /* we have legacy mode, but all ports are unavailable */
4974 if (legacy_mode == (1 << 3)) {
4976 goto err_out_regions;
4979 rc = pci_set_dma_mask(pdev, ATA_DMA_MASK);
4981 goto err_out_regions;
4982 rc = pci_set_consistent_dma_mask(pdev, ATA_DMA_MASK);
4984 goto err_out_regions;
4987 if (legacy_mode & (1 << 0))
4988 probe_ent = ata_pci_init_legacy_port(pdev, port[0], 0);
4989 if (legacy_mode & (1 << 1))
4990 probe_ent2 = ata_pci_init_legacy_port(pdev, port[1], 1);
4993 probe_ent = ata_pci_init_native_mode(pdev, port, ATA_PORT_PRIMARY | ATA_PORT_SECONDARY);
4995 probe_ent = ata_pci_init_native_mode(pdev, port, ATA_PORT_PRIMARY);
4997 if (!probe_ent && !probe_ent2) {
4999 goto err_out_regions;
5002 pci_set_master(pdev);
5004 /* FIXME: check ata_device_add return */
5006 if (legacy_mode & (1 << 0))
5007 ata_device_add(probe_ent);
5008 if (legacy_mode & (1 << 1))
5009 ata_device_add(probe_ent2);
5011 ata_device_add(probe_ent);
5019 if (legacy_mode & (1 << 0))
5020 release_region(0x1f0, 8);
5021 if (legacy_mode & (1 << 1))
5022 release_region(0x170, 8);
5023 pci_release_regions(pdev);
5025 if (disable_dev_on_err)
5026 pci_disable_device(pdev);
5031 * ata_pci_remove_one - PCI layer callback for device removal
5032 * @pdev: PCI device that was removed
5034 * PCI layer indicates to libata via this hook that
5035 * hot-unplug or module unload event has occurred.
5036 * Handle this by unregistering all objects associated
5037 * with this PCI device. Free those objects. Then finally
5038 * release PCI resources and disable device.
5041 * Inherited from PCI layer (may sleep).
5044 void ata_pci_remove_one (struct pci_dev *pdev)
5046 struct device *dev = pci_dev_to_dev(pdev);
5047 struct ata_host_set *host_set = dev_get_drvdata(dev);
5049 ata_host_set_remove(host_set);
5050 pci_release_regions(pdev);
5051 pci_disable_device(pdev);
5052 dev_set_drvdata(dev, NULL);
5055 /* move to PCI subsystem */
5056 int pci_test_config_bits(struct pci_dev *pdev, const struct pci_bits *bits)
5058 unsigned long tmp = 0;
5060 switch (bits->width) {
5063 pci_read_config_byte(pdev, bits->reg, &tmp8);
5069 pci_read_config_word(pdev, bits->reg, &tmp16);
5075 pci_read_config_dword(pdev, bits->reg, &tmp32);
5086 return (tmp == bits->val) ? 1 : 0;
5089 int ata_pci_device_suspend(struct pci_dev *pdev, pm_message_t state)
5091 pci_save_state(pdev);
5092 pci_disable_device(pdev);
5093 pci_set_power_state(pdev, PCI_D3hot);
5097 int ata_pci_device_resume(struct pci_dev *pdev)
5099 pci_set_power_state(pdev, PCI_D0);
5100 pci_restore_state(pdev);
5101 pci_enable_device(pdev);
5102 pci_set_master(pdev);
5105 #endif /* CONFIG_PCI */
5108 static int __init ata_init(void)
5110 ata_wq = create_workqueue("ata");
5114 printk(KERN_DEBUG "libata version " DRV_VERSION " loaded.\n");
5118 static void __exit ata_exit(void)
5120 destroy_workqueue(ata_wq);
5123 module_init(ata_init);
5124 module_exit(ata_exit);
5126 static unsigned long ratelimit_time;
5127 static spinlock_t ata_ratelimit_lock = SPIN_LOCK_UNLOCKED;
5129 int ata_ratelimit(void)
5132 unsigned long flags;
5134 spin_lock_irqsave(&ata_ratelimit_lock, flags);
5136 if (time_after(jiffies, ratelimit_time)) {
5138 ratelimit_time = jiffies + (HZ/5);
5142 spin_unlock_irqrestore(&ata_ratelimit_lock, flags);
5148 * libata is essentially a library of internal helper functions for
5149 * low-level ATA host controller drivers. As such, the API/ABI is
5150 * likely to change as new drivers are added and updated.
5151 * Do not depend on ABI/API stability.
5154 EXPORT_SYMBOL_GPL(ata_std_bios_param);
5155 EXPORT_SYMBOL_GPL(ata_std_ports);
5156 EXPORT_SYMBOL_GPL(ata_device_add);
5157 EXPORT_SYMBOL_GPL(ata_host_set_remove);
5158 EXPORT_SYMBOL_GPL(ata_sg_init);
5159 EXPORT_SYMBOL_GPL(ata_sg_init_one);
5160 EXPORT_SYMBOL_GPL(ata_qc_complete);
5161 EXPORT_SYMBOL_GPL(ata_qc_issue_prot);
5162 EXPORT_SYMBOL_GPL(ata_eng_timeout);
5163 EXPORT_SYMBOL_GPL(ata_tf_load);
5164 EXPORT_SYMBOL_GPL(ata_tf_read);
5165 EXPORT_SYMBOL_GPL(ata_noop_dev_select);
5166 EXPORT_SYMBOL_GPL(ata_std_dev_select);
5167 EXPORT_SYMBOL_GPL(ata_tf_to_fis);
5168 EXPORT_SYMBOL_GPL(ata_tf_from_fis);
5169 EXPORT_SYMBOL_GPL(ata_check_status);
5170 EXPORT_SYMBOL_GPL(ata_altstatus);
5171 EXPORT_SYMBOL_GPL(ata_exec_command);
5172 EXPORT_SYMBOL_GPL(ata_port_start);
5173 EXPORT_SYMBOL_GPL(ata_port_stop);
5174 EXPORT_SYMBOL_GPL(ata_host_stop);
5175 EXPORT_SYMBOL_GPL(ata_interrupt);
5176 EXPORT_SYMBOL_GPL(ata_qc_prep);
5177 EXPORT_SYMBOL_GPL(ata_bmdma_setup);
5178 EXPORT_SYMBOL_GPL(ata_bmdma_start);
5179 EXPORT_SYMBOL_GPL(ata_bmdma_irq_clear);
5180 EXPORT_SYMBOL_GPL(ata_bmdma_status);
5181 EXPORT_SYMBOL_GPL(ata_bmdma_stop);
5182 EXPORT_SYMBOL_GPL(ata_port_probe);
5183 EXPORT_SYMBOL_GPL(sata_phy_reset);
5184 EXPORT_SYMBOL_GPL(__sata_phy_reset);
5185 EXPORT_SYMBOL_GPL(ata_bus_reset);
5186 EXPORT_SYMBOL_GPL(ata_port_disable);
5187 EXPORT_SYMBOL_GPL(ata_ratelimit);
5188 EXPORT_SYMBOL_GPL(ata_scsi_ioctl);
5189 EXPORT_SYMBOL_GPL(ata_scsi_queuecmd);
5190 EXPORT_SYMBOL_GPL(ata_scsi_error);
5191 EXPORT_SYMBOL_GPL(ata_scsi_slave_config);
5192 EXPORT_SYMBOL_GPL(ata_scsi_release);
5193 EXPORT_SYMBOL_GPL(ata_host_intr);
5194 EXPORT_SYMBOL_GPL(ata_dev_classify);
5195 EXPORT_SYMBOL_GPL(ata_dev_id_string);
5196 EXPORT_SYMBOL_GPL(ata_dev_config);
5197 EXPORT_SYMBOL_GPL(ata_scsi_simulate);
5199 EXPORT_SYMBOL_GPL(ata_pio_need_iordy);
5200 EXPORT_SYMBOL_GPL(ata_timing_compute);
5201 EXPORT_SYMBOL_GPL(ata_timing_merge);
5204 EXPORT_SYMBOL_GPL(pci_test_config_bits);
5205 EXPORT_SYMBOL_GPL(ata_pci_host_stop);
5206 EXPORT_SYMBOL_GPL(ata_pci_init_native_mode);
5207 EXPORT_SYMBOL_GPL(ata_pci_init_one);
5208 EXPORT_SYMBOL_GPL(ata_pci_remove_one);
5209 EXPORT_SYMBOL_GPL(ata_pci_device_suspend);
5210 EXPORT_SYMBOL_GPL(ata_pci_device_resume);
5211 #endif /* CONFIG_PCI */
5213 EXPORT_SYMBOL_GPL(ata_device_suspend);
5214 EXPORT_SYMBOL_GPL(ata_device_resume);
5215 EXPORT_SYMBOL_GPL(ata_scsi_device_suspend);
5216 EXPORT_SYMBOL_GPL(ata_scsi_device_resume);