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);
77 static void ata_pio_error(struct ata_port *ap);
79 static unsigned int ata_unique_id = 1;
80 static struct workqueue_struct *ata_wq;
82 int atapi_enabled = 0;
83 module_param(atapi_enabled, int, 0444);
84 MODULE_PARM_DESC(atapi_enabled, "Enable discovery of ATAPI devices (0=off, 1=on)");
86 MODULE_AUTHOR("Jeff Garzik");
87 MODULE_DESCRIPTION("Library module for ATA devices");
88 MODULE_LICENSE("GPL");
89 MODULE_VERSION(DRV_VERSION);
92 * ata_tf_load_pio - send taskfile registers to host controller
93 * @ap: Port to which output is sent
94 * @tf: ATA taskfile register set
96 * Outputs ATA taskfile to standard ATA host controller.
99 * Inherited from caller.
102 static void ata_tf_load_pio(struct ata_port *ap, const struct ata_taskfile *tf)
104 struct ata_ioports *ioaddr = &ap->ioaddr;
105 unsigned int is_addr = tf->flags & ATA_TFLAG_ISADDR;
107 if (tf->ctl != ap->last_ctl) {
108 outb(tf->ctl, ioaddr->ctl_addr);
109 ap->last_ctl = tf->ctl;
113 if (is_addr && (tf->flags & ATA_TFLAG_LBA48)) {
114 outb(tf->hob_feature, ioaddr->feature_addr);
115 outb(tf->hob_nsect, ioaddr->nsect_addr);
116 outb(tf->hob_lbal, ioaddr->lbal_addr);
117 outb(tf->hob_lbam, ioaddr->lbam_addr);
118 outb(tf->hob_lbah, ioaddr->lbah_addr);
119 VPRINTK("hob: feat 0x%X nsect 0x%X, lba 0x%X 0x%X 0x%X\n",
128 outb(tf->feature, ioaddr->feature_addr);
129 outb(tf->nsect, ioaddr->nsect_addr);
130 outb(tf->lbal, ioaddr->lbal_addr);
131 outb(tf->lbam, ioaddr->lbam_addr);
132 outb(tf->lbah, ioaddr->lbah_addr);
133 VPRINTK("feat 0x%X nsect 0x%X lba 0x%X 0x%X 0x%X\n",
141 if (tf->flags & ATA_TFLAG_DEVICE) {
142 outb(tf->device, ioaddr->device_addr);
143 VPRINTK("device 0x%X\n", tf->device);
150 * ata_tf_load_mmio - send taskfile registers to host controller
151 * @ap: Port to which output is sent
152 * @tf: ATA taskfile register set
154 * Outputs ATA taskfile to standard ATA host controller using MMIO.
157 * Inherited from caller.
160 static void ata_tf_load_mmio(struct ata_port *ap, const struct ata_taskfile *tf)
162 struct ata_ioports *ioaddr = &ap->ioaddr;
163 unsigned int is_addr = tf->flags & ATA_TFLAG_ISADDR;
165 if (tf->ctl != ap->last_ctl) {
166 writeb(tf->ctl, (void __iomem *) ap->ioaddr.ctl_addr);
167 ap->last_ctl = tf->ctl;
171 if (is_addr && (tf->flags & ATA_TFLAG_LBA48)) {
172 writeb(tf->hob_feature, (void __iomem *) ioaddr->feature_addr);
173 writeb(tf->hob_nsect, (void __iomem *) ioaddr->nsect_addr);
174 writeb(tf->hob_lbal, (void __iomem *) ioaddr->lbal_addr);
175 writeb(tf->hob_lbam, (void __iomem *) ioaddr->lbam_addr);
176 writeb(tf->hob_lbah, (void __iomem *) ioaddr->lbah_addr);
177 VPRINTK("hob: feat 0x%X nsect 0x%X, lba 0x%X 0x%X 0x%X\n",
186 writeb(tf->feature, (void __iomem *) ioaddr->feature_addr);
187 writeb(tf->nsect, (void __iomem *) ioaddr->nsect_addr);
188 writeb(tf->lbal, (void __iomem *) ioaddr->lbal_addr);
189 writeb(tf->lbam, (void __iomem *) ioaddr->lbam_addr);
190 writeb(tf->lbah, (void __iomem *) ioaddr->lbah_addr);
191 VPRINTK("feat 0x%X nsect 0x%X lba 0x%X 0x%X 0x%X\n",
199 if (tf->flags & ATA_TFLAG_DEVICE) {
200 writeb(tf->device, (void __iomem *) ioaddr->device_addr);
201 VPRINTK("device 0x%X\n", tf->device);
209 * ata_tf_load - send taskfile registers to host controller
210 * @ap: Port to which output is sent
211 * @tf: ATA taskfile register set
213 * Outputs ATA taskfile to standard ATA host controller using MMIO
214 * or PIO as indicated by the ATA_FLAG_MMIO flag.
215 * Writes the control, feature, nsect, lbal, lbam, and lbah registers.
216 * Optionally (ATA_TFLAG_LBA48) writes hob_feature, hob_nsect,
217 * hob_lbal, hob_lbam, and hob_lbah.
219 * This function waits for idle (!BUSY and !DRQ) after writing
220 * registers. If the control register has a new value, this
221 * function also waits for idle after writing control and before
222 * writing the remaining registers.
224 * May be used as the tf_load() entry in ata_port_operations.
227 * Inherited from caller.
229 void ata_tf_load(struct ata_port *ap, const struct ata_taskfile *tf)
231 if (ap->flags & ATA_FLAG_MMIO)
232 ata_tf_load_mmio(ap, tf);
234 ata_tf_load_pio(ap, tf);
238 * ata_exec_command_pio - issue ATA command to host controller
239 * @ap: port to which command is being issued
240 * @tf: ATA taskfile register set
242 * Issues PIO write to ATA command register, with proper
243 * synchronization with interrupt handler / other threads.
246 * spin_lock_irqsave(host_set lock)
249 static void ata_exec_command_pio(struct ata_port *ap, const struct ata_taskfile *tf)
251 DPRINTK("ata%u: cmd 0x%X\n", ap->id, tf->command);
253 outb(tf->command, ap->ioaddr.command_addr);
259 * ata_exec_command_mmio - issue ATA command to host controller
260 * @ap: port to which command is being issued
261 * @tf: ATA taskfile register set
263 * Issues MMIO write to ATA command register, with proper
264 * synchronization with interrupt handler / other threads.
267 * spin_lock_irqsave(host_set lock)
270 static void ata_exec_command_mmio(struct ata_port *ap, const struct ata_taskfile *tf)
272 DPRINTK("ata%u: cmd 0x%X\n", ap->id, tf->command);
274 writeb(tf->command, (void __iomem *) ap->ioaddr.command_addr);
280 * ata_exec_command - issue ATA command to host controller
281 * @ap: port to which command is being issued
282 * @tf: ATA taskfile register set
284 * Issues PIO/MMIO write to ATA command register, with proper
285 * synchronization with interrupt handler / other threads.
288 * spin_lock_irqsave(host_set lock)
290 void ata_exec_command(struct ata_port *ap, const struct ata_taskfile *tf)
292 if (ap->flags & ATA_FLAG_MMIO)
293 ata_exec_command_mmio(ap, tf);
295 ata_exec_command_pio(ap, tf);
299 * ata_tf_to_host - issue ATA taskfile to host controller
300 * @ap: port to which command is being issued
301 * @tf: ATA taskfile register set
303 * Issues ATA taskfile register set to ATA host controller,
304 * with proper synchronization with interrupt handler and
308 * spin_lock_irqsave(host_set lock)
311 static inline void ata_tf_to_host(struct ata_port *ap,
312 const struct ata_taskfile *tf)
314 ap->ops->tf_load(ap, tf);
315 ap->ops->exec_command(ap, tf);
319 * ata_tf_read_pio - input device's ATA taskfile shadow registers
320 * @ap: Port from which input is read
321 * @tf: ATA taskfile register set for storing input
323 * Reads ATA taskfile registers for currently-selected device
327 * Inherited from caller.
330 static void ata_tf_read_pio(struct ata_port *ap, struct ata_taskfile *tf)
332 struct ata_ioports *ioaddr = &ap->ioaddr;
334 tf->command = ata_check_status(ap);
335 tf->feature = inb(ioaddr->error_addr);
336 tf->nsect = inb(ioaddr->nsect_addr);
337 tf->lbal = inb(ioaddr->lbal_addr);
338 tf->lbam = inb(ioaddr->lbam_addr);
339 tf->lbah = inb(ioaddr->lbah_addr);
340 tf->device = inb(ioaddr->device_addr);
342 if (tf->flags & ATA_TFLAG_LBA48) {
343 outb(tf->ctl | ATA_HOB, ioaddr->ctl_addr);
344 tf->hob_feature = inb(ioaddr->error_addr);
345 tf->hob_nsect = inb(ioaddr->nsect_addr);
346 tf->hob_lbal = inb(ioaddr->lbal_addr);
347 tf->hob_lbam = inb(ioaddr->lbam_addr);
348 tf->hob_lbah = inb(ioaddr->lbah_addr);
353 * ata_tf_read_mmio - input device's ATA taskfile shadow registers
354 * @ap: Port from which input is read
355 * @tf: ATA taskfile register set for storing input
357 * Reads ATA taskfile registers for currently-selected device
361 * Inherited from caller.
364 static void ata_tf_read_mmio(struct ata_port *ap, struct ata_taskfile *tf)
366 struct ata_ioports *ioaddr = &ap->ioaddr;
368 tf->command = ata_check_status(ap);
369 tf->feature = readb((void __iomem *)ioaddr->error_addr);
370 tf->nsect = readb((void __iomem *)ioaddr->nsect_addr);
371 tf->lbal = readb((void __iomem *)ioaddr->lbal_addr);
372 tf->lbam = readb((void __iomem *)ioaddr->lbam_addr);
373 tf->lbah = readb((void __iomem *)ioaddr->lbah_addr);
374 tf->device = readb((void __iomem *)ioaddr->device_addr);
376 if (tf->flags & ATA_TFLAG_LBA48) {
377 writeb(tf->ctl | ATA_HOB, (void __iomem *) ap->ioaddr.ctl_addr);
378 tf->hob_feature = readb((void __iomem *)ioaddr->error_addr);
379 tf->hob_nsect = readb((void __iomem *)ioaddr->nsect_addr);
380 tf->hob_lbal = readb((void __iomem *)ioaddr->lbal_addr);
381 tf->hob_lbam = readb((void __iomem *)ioaddr->lbam_addr);
382 tf->hob_lbah = readb((void __iomem *)ioaddr->lbah_addr);
388 * ata_tf_read - input device's ATA taskfile shadow registers
389 * @ap: Port from which input is read
390 * @tf: ATA taskfile register set for storing input
392 * Reads ATA taskfile registers for currently-selected device
395 * Reads nsect, lbal, lbam, lbah, and device. If ATA_TFLAG_LBA48
396 * is set, also reads the hob registers.
398 * May be used as the tf_read() entry in ata_port_operations.
401 * Inherited from caller.
403 void ata_tf_read(struct ata_port *ap, struct ata_taskfile *tf)
405 if (ap->flags & ATA_FLAG_MMIO)
406 ata_tf_read_mmio(ap, tf);
408 ata_tf_read_pio(ap, tf);
412 * ata_check_status_pio - Read device status reg & clear interrupt
413 * @ap: port where the device is
415 * Reads ATA taskfile status register for currently-selected device
416 * and return its value. This also clears pending interrupts
420 * Inherited from caller.
422 static u8 ata_check_status_pio(struct ata_port *ap)
424 return inb(ap->ioaddr.status_addr);
428 * ata_check_status_mmio - Read device status reg & clear interrupt
429 * @ap: port where the device is
431 * Reads ATA taskfile status register for currently-selected device
432 * via MMIO and return its value. This also clears pending interrupts
436 * Inherited from caller.
438 static u8 ata_check_status_mmio(struct ata_port *ap)
440 return readb((void __iomem *) ap->ioaddr.status_addr);
445 * ata_check_status - Read device status reg & clear interrupt
446 * @ap: port where the device is
448 * Reads ATA taskfile status register for currently-selected device
449 * and return its value. This also clears pending interrupts
452 * May be used as the check_status() entry in ata_port_operations.
455 * Inherited from caller.
457 u8 ata_check_status(struct ata_port *ap)
459 if (ap->flags & ATA_FLAG_MMIO)
460 return ata_check_status_mmio(ap);
461 return ata_check_status_pio(ap);
466 * ata_altstatus - Read device alternate status reg
467 * @ap: port where the device is
469 * Reads ATA taskfile alternate status register for
470 * currently-selected device and return its value.
472 * Note: may NOT be used as the check_altstatus() entry in
473 * ata_port_operations.
476 * Inherited from caller.
478 u8 ata_altstatus(struct ata_port *ap)
480 if (ap->ops->check_altstatus)
481 return ap->ops->check_altstatus(ap);
483 if (ap->flags & ATA_FLAG_MMIO)
484 return readb((void __iomem *)ap->ioaddr.altstatus_addr);
485 return inb(ap->ioaddr.altstatus_addr);
490 * ata_tf_to_fis - Convert ATA taskfile to SATA FIS structure
491 * @tf: Taskfile to convert
492 * @fis: Buffer into which data will output
493 * @pmp: Port multiplier port
495 * Converts a standard ATA taskfile to a Serial ATA
496 * FIS structure (Register - Host to Device).
499 * Inherited from caller.
502 void ata_tf_to_fis(const struct ata_taskfile *tf, u8 *fis, u8 pmp)
504 fis[0] = 0x27; /* Register - Host to Device FIS */
505 fis[1] = (pmp & 0xf) | (1 << 7); /* Port multiplier number,
506 bit 7 indicates Command FIS */
507 fis[2] = tf->command;
508 fis[3] = tf->feature;
515 fis[8] = tf->hob_lbal;
516 fis[9] = tf->hob_lbam;
517 fis[10] = tf->hob_lbah;
518 fis[11] = tf->hob_feature;
521 fis[13] = tf->hob_nsect;
532 * ata_tf_from_fis - Convert SATA FIS to ATA taskfile
533 * @fis: Buffer from which data will be input
534 * @tf: Taskfile to output
536 * Converts a serial ATA FIS structure to a standard ATA taskfile.
539 * Inherited from caller.
542 void ata_tf_from_fis(const u8 *fis, struct ata_taskfile *tf)
544 tf->command = fis[2]; /* status */
545 tf->feature = fis[3]; /* error */
552 tf->hob_lbal = fis[8];
553 tf->hob_lbam = fis[9];
554 tf->hob_lbah = fis[10];
557 tf->hob_nsect = fis[13];
560 static const u8 ata_rw_cmds[] = {
564 ATA_CMD_READ_MULTI_EXT,
565 ATA_CMD_WRITE_MULTI_EXT,
569 ATA_CMD_PIO_READ_EXT,
570 ATA_CMD_PIO_WRITE_EXT,
579 * ata_rwcmd_protocol - set taskfile r/w commands and protocol
580 * @qc: command to examine and configure
582 * Examine the device configuration and tf->flags to calculate
583 * the proper read/write commands and protocol to use.
588 void ata_rwcmd_protocol(struct ata_queued_cmd *qc)
590 struct ata_taskfile *tf = &qc->tf;
591 struct ata_device *dev = qc->dev;
593 int index, lba48, write;
595 lba48 = (tf->flags & ATA_TFLAG_LBA48) ? 2 : 0;
596 write = (tf->flags & ATA_TFLAG_WRITE) ? 1 : 0;
598 if (dev->flags & ATA_DFLAG_PIO) {
599 tf->protocol = ATA_PROT_PIO;
600 index = dev->multi_count ? 0 : 4;
602 tf->protocol = ATA_PROT_DMA;
606 tf->command = ata_rw_cmds[index + lba48 + write];
609 static const char * xfer_mode_str[] = {
629 * ata_udma_string - convert UDMA bit offset to string
630 * @mask: mask of bits supported; only highest bit counts.
632 * Determine string which represents the highest speed
633 * (highest bit in @udma_mask).
639 * Constant C string representing highest speed listed in
640 * @udma_mask, or the constant C string "<n/a>".
643 static const char *ata_mode_string(unsigned int mask)
647 for (i = 7; i >= 0; i--)
650 for (i = ATA_SHIFT_MWDMA + 2; i >= ATA_SHIFT_MWDMA; i--)
653 for (i = ATA_SHIFT_PIO + 4; i >= ATA_SHIFT_PIO; i--)
660 return xfer_mode_str[i];
664 * ata_pio_devchk - PATA device presence detection
665 * @ap: ATA channel to examine
666 * @device: Device to examine (starting at zero)
668 * This technique was originally described in
669 * Hale Landis's ATADRVR (www.ata-atapi.com), and
670 * later found its way into the ATA/ATAPI spec.
672 * Write a pattern to the ATA shadow registers,
673 * and if a device is present, it will respond by
674 * correctly storing and echoing back the
675 * ATA shadow register contents.
681 static unsigned int ata_pio_devchk(struct ata_port *ap,
684 struct ata_ioports *ioaddr = &ap->ioaddr;
687 ap->ops->dev_select(ap, device);
689 outb(0x55, ioaddr->nsect_addr);
690 outb(0xaa, ioaddr->lbal_addr);
692 outb(0xaa, ioaddr->nsect_addr);
693 outb(0x55, ioaddr->lbal_addr);
695 outb(0x55, ioaddr->nsect_addr);
696 outb(0xaa, ioaddr->lbal_addr);
698 nsect = inb(ioaddr->nsect_addr);
699 lbal = inb(ioaddr->lbal_addr);
701 if ((nsect == 0x55) && (lbal == 0xaa))
702 return 1; /* we found a device */
704 return 0; /* nothing found */
708 * ata_mmio_devchk - PATA device presence detection
709 * @ap: ATA channel to examine
710 * @device: Device to examine (starting at zero)
712 * This technique was originally described in
713 * Hale Landis's ATADRVR (www.ata-atapi.com), and
714 * later found its way into the ATA/ATAPI spec.
716 * Write a pattern to the ATA shadow registers,
717 * and if a device is present, it will respond by
718 * correctly storing and echoing back the
719 * ATA shadow register contents.
725 static unsigned int ata_mmio_devchk(struct ata_port *ap,
728 struct ata_ioports *ioaddr = &ap->ioaddr;
731 ap->ops->dev_select(ap, device);
733 writeb(0x55, (void __iomem *) ioaddr->nsect_addr);
734 writeb(0xaa, (void __iomem *) ioaddr->lbal_addr);
736 writeb(0xaa, (void __iomem *) ioaddr->nsect_addr);
737 writeb(0x55, (void __iomem *) ioaddr->lbal_addr);
739 writeb(0x55, (void __iomem *) ioaddr->nsect_addr);
740 writeb(0xaa, (void __iomem *) ioaddr->lbal_addr);
742 nsect = readb((void __iomem *) ioaddr->nsect_addr);
743 lbal = readb((void __iomem *) ioaddr->lbal_addr);
745 if ((nsect == 0x55) && (lbal == 0xaa))
746 return 1; /* we found a device */
748 return 0; /* nothing found */
752 * ata_devchk - PATA device presence detection
753 * @ap: ATA channel to examine
754 * @device: Device to examine (starting at zero)
756 * Dispatch ATA device presence detection, depending
757 * on whether we are using PIO or MMIO to talk to the
758 * ATA shadow registers.
764 static unsigned int ata_devchk(struct ata_port *ap,
767 if (ap->flags & ATA_FLAG_MMIO)
768 return ata_mmio_devchk(ap, device);
769 return ata_pio_devchk(ap, device);
773 * ata_dev_classify - determine device type based on ATA-spec signature
774 * @tf: ATA taskfile register set for device to be identified
776 * Determine from taskfile register contents whether a device is
777 * ATA or ATAPI, as per "Signature and persistence" section
778 * of ATA/PI spec (volume 1, sect 5.14).
784 * Device type, %ATA_DEV_ATA, %ATA_DEV_ATAPI, or %ATA_DEV_UNKNOWN
785 * the event of failure.
788 unsigned int ata_dev_classify(const struct ata_taskfile *tf)
790 /* Apple's open source Darwin code hints that some devices only
791 * put a proper signature into the LBA mid/high registers,
792 * So, we only check those. It's sufficient for uniqueness.
795 if (((tf->lbam == 0) && (tf->lbah == 0)) ||
796 ((tf->lbam == 0x3c) && (tf->lbah == 0xc3))) {
797 DPRINTK("found ATA device by sig\n");
801 if (((tf->lbam == 0x14) && (tf->lbah == 0xeb)) ||
802 ((tf->lbam == 0x69) && (tf->lbah == 0x96))) {
803 DPRINTK("found ATAPI device by sig\n");
804 return ATA_DEV_ATAPI;
807 DPRINTK("unknown device\n");
808 return ATA_DEV_UNKNOWN;
812 * ata_dev_try_classify - Parse returned ATA device signature
813 * @ap: ATA channel to examine
814 * @device: Device to examine (starting at zero)
816 * After an event -- SRST, E.D.D., or SATA COMRESET -- occurs,
817 * an ATA/ATAPI-defined set of values is placed in the ATA
818 * shadow registers, indicating the results of device detection
821 * Select the ATA device, and read the values from the ATA shadow
822 * registers. Then parse according to the Error register value,
823 * and the spec-defined values examined by ata_dev_classify().
829 static u8 ata_dev_try_classify(struct ata_port *ap, unsigned int device)
831 struct ata_device *dev = &ap->device[device];
832 struct ata_taskfile tf;
836 ap->ops->dev_select(ap, device);
838 memset(&tf, 0, sizeof(tf));
840 ap->ops->tf_read(ap, &tf);
843 dev->class = ATA_DEV_NONE;
845 /* see if device passed diags */
848 else if ((device == 0) && (err == 0x81))
853 /* determine if device if ATA or ATAPI */
854 class = ata_dev_classify(&tf);
855 if (class == ATA_DEV_UNKNOWN)
857 if ((class == ATA_DEV_ATA) && (ata_chk_status(ap) == 0))
866 * ata_dev_id_string - Convert IDENTIFY DEVICE page into string
867 * @id: IDENTIFY DEVICE results we will examine
868 * @s: string into which data is output
869 * @ofs: offset into identify device page
870 * @len: length of string to return. must be an even number.
872 * The strings in the IDENTIFY DEVICE page are broken up into
873 * 16-bit chunks. Run through the string, and output each
874 * 8-bit chunk linearly, regardless of platform.
880 void ata_dev_id_string(const u16 *id, unsigned char *s,
881 unsigned int ofs, unsigned int len)
901 * ata_noop_dev_select - Select device 0/1 on ATA bus
902 * @ap: ATA channel to manipulate
903 * @device: ATA device (numbered from zero) to select
905 * This function performs no actual function.
907 * May be used as the dev_select() entry in ata_port_operations.
912 void ata_noop_dev_select (struct ata_port *ap, unsigned int device)
918 * ata_std_dev_select - Select device 0/1 on ATA bus
919 * @ap: ATA channel to manipulate
920 * @device: ATA device (numbered from zero) to select
922 * Use the method defined in the ATA specification to
923 * make either device 0, or device 1, active on the
924 * ATA channel. Works with both PIO and MMIO.
926 * May be used as the dev_select() entry in ata_port_operations.
932 void ata_std_dev_select (struct ata_port *ap, unsigned int device)
937 tmp = ATA_DEVICE_OBS;
939 tmp = ATA_DEVICE_OBS | ATA_DEV1;
941 if (ap->flags & ATA_FLAG_MMIO) {
942 writeb(tmp, (void __iomem *) ap->ioaddr.device_addr);
944 outb(tmp, ap->ioaddr.device_addr);
946 ata_pause(ap); /* needed; also flushes, for mmio */
950 * ata_dev_select - Select device 0/1 on ATA bus
951 * @ap: ATA channel to manipulate
952 * @device: ATA device (numbered from zero) to select
953 * @wait: non-zero to wait for Status register BSY bit to clear
954 * @can_sleep: non-zero if context allows sleeping
956 * Use the method defined in the ATA specification to
957 * make either device 0, or device 1, active on the
960 * This is a high-level version of ata_std_dev_select(),
961 * which additionally provides the services of inserting
962 * the proper pauses and status polling, where needed.
968 void ata_dev_select(struct ata_port *ap, unsigned int device,
969 unsigned int wait, unsigned int can_sleep)
971 VPRINTK("ENTER, ata%u: device %u, wait %u\n",
972 ap->id, device, wait);
977 ap->ops->dev_select(ap, device);
980 if (can_sleep && ap->device[device].class == ATA_DEV_ATAPI)
987 * ata_dump_id - IDENTIFY DEVICE info debugging output
988 * @dev: Device whose IDENTIFY DEVICE page we will dump
990 * Dump selected 16-bit words from a detected device's
991 * IDENTIFY PAGE page.
997 static inline void ata_dump_id(const struct ata_device *dev)
999 DPRINTK("49==0x%04x "
1009 DPRINTK("80==0x%04x "
1019 DPRINTK("88==0x%04x "
1026 * Compute the PIO modes available for this device. This is not as
1027 * trivial as it seems if we must consider early devices correctly.
1029 * FIXME: pre IDE drive timing (do we care ?).
1032 static unsigned int ata_pio_modes(const struct ata_device *adev)
1036 /* Usual case. Word 53 indicates word 88 is valid */
1037 if (adev->id[ATA_ID_FIELD_VALID] & (1 << 2)) {
1038 modes = adev->id[ATA_ID_PIO_MODES] & 0x03;
1044 /* If word 88 isn't valid then Word 51 holds the PIO timing number
1045 for the maximum. Turn it into a mask and return it */
1046 modes = (2 << (adev->id[ATA_ID_OLD_PIO_MODES] & 0xFF)) - 1 ;
1050 static int ata_qc_wait_err(struct ata_queued_cmd *qc,
1051 struct completion *wait)
1055 if (wait_for_completion_timeout(wait, 30 * HZ) < 1) {
1056 /* timeout handling */
1057 unsigned int err_mask = ac_err_mask(ata_chk_status(qc->ap));
1060 printk(KERN_WARNING "ata%u: slow completion (cmd %x)\n",
1061 qc->ap->id, qc->tf.command);
1063 printk(KERN_WARNING "ata%u: qc timeout (cmd %x)\n",
1064 qc->ap->id, qc->tf.command);
1068 ata_qc_complete(qc, err_mask);
1075 * ata_dev_identify - obtain IDENTIFY x DEVICE page
1076 * @ap: port on which device we wish to probe resides
1077 * @device: device bus address, starting at zero
1079 * Following bus reset, we issue the IDENTIFY [PACKET] DEVICE
1080 * command, and read back the 512-byte device information page.
1081 * The device information page is fed to us via the standard
1082 * PIO-IN protocol, but we hand-code it here. (TODO: investigate
1083 * using standard PIO-IN paths)
1085 * After reading the device information page, we use several
1086 * bits of information from it to initialize data structures
1087 * that will be used during the lifetime of the ata_device.
1088 * Other data from the info page is used to disqualify certain
1089 * older ATA devices we do not wish to support.
1092 * Inherited from caller. Some functions called by this function
1093 * obtain the host_set lock.
1096 static void ata_dev_identify(struct ata_port *ap, unsigned int device)
1098 struct ata_device *dev = &ap->device[device];
1099 unsigned int major_version;
1101 unsigned long xfer_modes;
1102 unsigned int using_edd;
1103 DECLARE_COMPLETION(wait);
1104 struct ata_queued_cmd *qc;
1105 unsigned long flags;
1108 if (!ata_dev_present(dev)) {
1109 DPRINTK("ENTER/EXIT (host %u, dev %u) -- nodev\n",
1114 if (ap->flags & (ATA_FLAG_SRST | ATA_FLAG_SATA_RESET))
1119 DPRINTK("ENTER, host %u, dev %u\n", ap->id, device);
1121 assert (dev->class == ATA_DEV_ATA || dev->class == ATA_DEV_ATAPI ||
1122 dev->class == ATA_DEV_NONE);
1124 ata_dev_select(ap, device, 1, 1); /* select device 0/1 */
1126 qc = ata_qc_new_init(ap, dev);
1129 ata_sg_init_one(qc, dev->id, sizeof(dev->id));
1130 qc->dma_dir = DMA_FROM_DEVICE;
1131 qc->tf.protocol = ATA_PROT_PIO;
1135 if (dev->class == ATA_DEV_ATA) {
1136 qc->tf.command = ATA_CMD_ID_ATA;
1137 DPRINTK("do ATA identify\n");
1139 qc->tf.command = ATA_CMD_ID_ATAPI;
1140 DPRINTK("do ATAPI identify\n");
1143 qc->waiting = &wait;
1144 qc->complete_fn = ata_qc_complete_noop;
1146 spin_lock_irqsave(&ap->host_set->lock, flags);
1147 rc = ata_qc_issue(qc);
1148 spin_unlock_irqrestore(&ap->host_set->lock, flags);
1153 ata_qc_wait_err(qc, &wait);
1155 spin_lock_irqsave(&ap->host_set->lock, flags);
1156 ap->ops->tf_read(ap, &qc->tf);
1157 spin_unlock_irqrestore(&ap->host_set->lock, flags);
1159 if (qc->tf.command & ATA_ERR) {
1161 * arg! EDD works for all test cases, but seems to return
1162 * the ATA signature for some ATAPI devices. Until the
1163 * reason for this is found and fixed, we fix up the mess
1164 * here. If IDENTIFY DEVICE returns command aborted
1165 * (as ATAPI devices do), then we issue an
1166 * IDENTIFY PACKET DEVICE.
1168 * ATA software reset (SRST, the default) does not appear
1169 * to have this problem.
1171 if ((using_edd) && (dev->class == ATA_DEV_ATA)) {
1172 u8 err = qc->tf.feature;
1173 if (err & ATA_ABORTED) {
1174 dev->class = ATA_DEV_ATAPI;
1185 swap_buf_le16(dev->id, ATA_ID_WORDS);
1187 /* print device capabilities */
1188 printk(KERN_DEBUG "ata%u: dev %u cfg "
1189 "49:%04x 82:%04x 83:%04x 84:%04x 85:%04x 86:%04x 87:%04x 88:%04x\n",
1190 ap->id, device, dev->id[49],
1191 dev->id[82], dev->id[83], dev->id[84],
1192 dev->id[85], dev->id[86], dev->id[87],
1196 * common ATA, ATAPI feature tests
1199 /* we require DMA support (bits 8 of word 49) */
1200 if (!ata_id_has_dma(dev->id)) {
1201 printk(KERN_DEBUG "ata%u: no dma\n", ap->id);
1205 /* quick-n-dirty find max transfer mode; for printk only */
1206 xfer_modes = dev->id[ATA_ID_UDMA_MODES];
1208 xfer_modes = (dev->id[ATA_ID_MWDMA_MODES]) << ATA_SHIFT_MWDMA;
1210 xfer_modes = ata_pio_modes(dev);
1214 /* ATA-specific feature tests */
1215 if (dev->class == ATA_DEV_ATA) {
1216 if (!ata_id_is_ata(dev->id)) /* sanity check */
1219 /* get major version */
1220 tmp = dev->id[ATA_ID_MAJOR_VER];
1221 for (major_version = 14; major_version >= 1; major_version--)
1222 if (tmp & (1 << major_version))
1226 * The exact sequence expected by certain pre-ATA4 drives is:
1229 * INITIALIZE DEVICE PARAMETERS
1231 * Some drives were very specific about that exact sequence.
1233 if (major_version < 4 || (!ata_id_has_lba(dev->id))) {
1234 ata_dev_init_params(ap, dev);
1236 /* current CHS translation info (id[53-58]) might be
1237 * changed. reread the identify device info.
1239 ata_dev_reread_id(ap, dev);
1242 if (ata_id_has_lba(dev->id)) {
1243 dev->flags |= ATA_DFLAG_LBA;
1245 if (ata_id_has_lba48(dev->id)) {
1246 dev->flags |= ATA_DFLAG_LBA48;
1247 dev->n_sectors = ata_id_u64(dev->id, 100);
1249 dev->n_sectors = ata_id_u32(dev->id, 60);
1252 /* print device info to dmesg */
1253 printk(KERN_INFO "ata%u: dev %u ATA-%d, max %s, %Lu sectors:%s\n",
1256 ata_mode_string(xfer_modes),
1257 (unsigned long long)dev->n_sectors,
1258 dev->flags & ATA_DFLAG_LBA48 ? " LBA48" : " LBA");
1262 /* Default translation */
1263 dev->cylinders = dev->id[1];
1264 dev->heads = dev->id[3];
1265 dev->sectors = dev->id[6];
1266 dev->n_sectors = dev->cylinders * dev->heads * dev->sectors;
1268 if (ata_id_current_chs_valid(dev->id)) {
1269 /* Current CHS translation is valid. */
1270 dev->cylinders = dev->id[54];
1271 dev->heads = dev->id[55];
1272 dev->sectors = dev->id[56];
1274 dev->n_sectors = ata_id_u32(dev->id, 57);
1277 /* print device info to dmesg */
1278 printk(KERN_INFO "ata%u: dev %u ATA-%d, max %s, %Lu sectors: CHS %d/%d/%d\n",
1281 ata_mode_string(xfer_modes),
1282 (unsigned long long)dev->n_sectors,
1283 (int)dev->cylinders, (int)dev->heads, (int)dev->sectors);
1287 if (dev->id[59] & 0x100) {
1288 dev->multi_count = dev->id[59] & 0xff;
1289 DPRINTK("ata%u: dev %u multi count %u\n",
1290 ap->id, device, dev->multi_count);
1293 ap->host->max_cmd_len = 16;
1296 /* ATAPI-specific feature tests */
1297 else if (dev->class == ATA_DEV_ATAPI) {
1298 if (ata_id_is_ata(dev->id)) /* sanity check */
1301 rc = atapi_cdb_len(dev->id);
1302 if ((rc < 12) || (rc > ATAPI_CDB_LEN)) {
1303 printk(KERN_WARNING "ata%u: unsupported CDB len\n", ap->id);
1306 ap->cdb_len = (unsigned int) rc;
1307 ap->host->max_cmd_len = (unsigned char) ap->cdb_len;
1309 if (ata_id_cdb_intr(dev->id))
1310 dev->flags |= ATA_DFLAG_CDB_INTR;
1312 /* print device info to dmesg */
1313 printk(KERN_INFO "ata%u: dev %u ATAPI, max %s\n",
1315 ata_mode_string(xfer_modes));
1318 DPRINTK("EXIT, drv_stat = 0x%x\n", ata_chk_status(ap));
1322 printk(KERN_WARNING "ata%u: dev %u not supported, ignoring\n",
1325 dev->class++; /* converts ATA_DEV_xxx into ATA_DEV_xxx_UNSUP */
1326 DPRINTK("EXIT, err\n");
1330 static inline u8 ata_dev_knobble(const struct ata_port *ap)
1332 return ((ap->cbl == ATA_CBL_SATA) && (!ata_id_is_sata(ap->device->id)));
1336 * ata_dev_config - Run device specific handlers and check for
1337 * SATA->PATA bridges
1344 void ata_dev_config(struct ata_port *ap, unsigned int i)
1346 /* limit bridge transfers to udma5, 200 sectors */
1347 if (ata_dev_knobble(ap)) {
1348 printk(KERN_INFO "ata%u(%u): applying bridge limits\n",
1349 ap->id, ap->device->devno);
1350 ap->udma_mask &= ATA_UDMA5;
1351 ap->host->max_sectors = ATA_MAX_SECTORS;
1352 ap->host->hostt->max_sectors = ATA_MAX_SECTORS;
1353 ap->device->flags |= ATA_DFLAG_LOCK_SECTORS;
1356 if (ap->ops->dev_config)
1357 ap->ops->dev_config(ap, &ap->device[i]);
1361 * ata_bus_probe - Reset and probe ATA bus
1364 * Master ATA bus probing function. Initiates a hardware-dependent
1365 * bus reset, then attempts to identify any devices found on
1369 * PCI/etc. bus probe sem.
1372 * Zero on success, non-zero on error.
1375 static int ata_bus_probe(struct ata_port *ap)
1377 unsigned int i, found = 0;
1379 ap->ops->phy_reset(ap);
1380 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1383 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1384 ata_dev_identify(ap, i);
1385 if (ata_dev_present(&ap->device[i])) {
1387 ata_dev_config(ap,i);
1391 if ((!found) || (ap->flags & ATA_FLAG_PORT_DISABLED))
1392 goto err_out_disable;
1395 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1396 goto err_out_disable;
1401 ap->ops->port_disable(ap);
1407 * ata_port_probe - Mark port as enabled
1408 * @ap: Port for which we indicate enablement
1410 * Modify @ap data structure such that the system
1411 * thinks that the entire port is enabled.
1413 * LOCKING: host_set lock, or some other form of
1417 void ata_port_probe(struct ata_port *ap)
1419 ap->flags &= ~ATA_FLAG_PORT_DISABLED;
1423 * __sata_phy_reset - Wake/reset a low-level SATA PHY
1424 * @ap: SATA port associated with target SATA PHY.
1426 * This function issues commands to standard SATA Sxxx
1427 * PHY registers, to wake up the phy (and device), and
1428 * clear any reset condition.
1431 * PCI/etc. bus probe sem.
1434 void __sata_phy_reset(struct ata_port *ap)
1437 unsigned long timeout = jiffies + (HZ * 5);
1439 if (ap->flags & ATA_FLAG_SATA_RESET) {
1440 /* issue phy wake/reset */
1441 scr_write_flush(ap, SCR_CONTROL, 0x301);
1442 /* Couldn't find anything in SATA I/II specs, but
1443 * AHCI-1.1 10.4.2 says at least 1 ms. */
1446 scr_write_flush(ap, SCR_CONTROL, 0x300); /* phy wake/clear reset */
1448 /* wait for phy to become ready, if necessary */
1451 sstatus = scr_read(ap, SCR_STATUS);
1452 if ((sstatus & 0xf) != 1)
1454 } while (time_before(jiffies, timeout));
1456 /* TODO: phy layer with polling, timeouts, etc. */
1457 sstatus = scr_read(ap, SCR_STATUS);
1458 if (sata_dev_present(ap)) {
1462 tmp = (sstatus >> 4) & 0xf;
1465 else if (tmp & (1 << 1))
1468 speed = "<unknown>";
1469 printk(KERN_INFO "ata%u: SATA link up %s Gbps (SStatus %X)\n",
1470 ap->id, speed, sstatus);
1473 printk(KERN_INFO "ata%u: SATA link down (SStatus %X)\n",
1475 ata_port_disable(ap);
1478 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1481 if (ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT)) {
1482 ata_port_disable(ap);
1486 ap->cbl = ATA_CBL_SATA;
1490 * sata_phy_reset - Reset SATA bus.
1491 * @ap: SATA port associated with target SATA PHY.
1493 * This function resets the SATA bus, and then probes
1494 * the bus for devices.
1497 * PCI/etc. bus probe sem.
1500 void sata_phy_reset(struct ata_port *ap)
1502 __sata_phy_reset(ap);
1503 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1509 * ata_port_disable - Disable port.
1510 * @ap: Port to be disabled.
1512 * Modify @ap data structure such that the system
1513 * thinks that the entire port is disabled, and should
1514 * never attempt to probe or communicate with devices
1517 * LOCKING: host_set lock, or some other form of
1521 void ata_port_disable(struct ata_port *ap)
1523 ap->device[0].class = ATA_DEV_NONE;
1524 ap->device[1].class = ATA_DEV_NONE;
1525 ap->flags |= ATA_FLAG_PORT_DISABLED;
1529 * This mode timing computation functionality is ported over from
1530 * drivers/ide/ide-timing.h and was originally written by Vojtech Pavlik
1533 * PIO 0-5, MWDMA 0-2 and UDMA 0-6 timings (in nanoseconds).
1534 * These were taken from ATA/ATAPI-6 standard, rev 0a, except
1535 * for PIO 5, which is a nonstandard extension and UDMA6, which
1536 * is currently supported only by Maxtor drives.
1539 static const struct ata_timing ata_timing[] = {
1541 { XFER_UDMA_6, 0, 0, 0, 0, 0, 0, 0, 15 },
1542 { XFER_UDMA_5, 0, 0, 0, 0, 0, 0, 0, 20 },
1543 { XFER_UDMA_4, 0, 0, 0, 0, 0, 0, 0, 30 },
1544 { XFER_UDMA_3, 0, 0, 0, 0, 0, 0, 0, 45 },
1546 { XFER_UDMA_2, 0, 0, 0, 0, 0, 0, 0, 60 },
1547 { XFER_UDMA_1, 0, 0, 0, 0, 0, 0, 0, 80 },
1548 { XFER_UDMA_0, 0, 0, 0, 0, 0, 0, 0, 120 },
1550 /* { XFER_UDMA_SLOW, 0, 0, 0, 0, 0, 0, 0, 150 }, */
1552 { XFER_MW_DMA_2, 25, 0, 0, 0, 70, 25, 120, 0 },
1553 { XFER_MW_DMA_1, 45, 0, 0, 0, 80, 50, 150, 0 },
1554 { XFER_MW_DMA_0, 60, 0, 0, 0, 215, 215, 480, 0 },
1556 { XFER_SW_DMA_2, 60, 0, 0, 0, 120, 120, 240, 0 },
1557 { XFER_SW_DMA_1, 90, 0, 0, 0, 240, 240, 480, 0 },
1558 { XFER_SW_DMA_0, 120, 0, 0, 0, 480, 480, 960, 0 },
1560 /* { XFER_PIO_5, 20, 50, 30, 100, 50, 30, 100, 0 }, */
1561 { XFER_PIO_4, 25, 70, 25, 120, 70, 25, 120, 0 },
1562 { XFER_PIO_3, 30, 80, 70, 180, 80, 70, 180, 0 },
1564 { XFER_PIO_2, 30, 290, 40, 330, 100, 90, 240, 0 },
1565 { XFER_PIO_1, 50, 290, 93, 383, 125, 100, 383, 0 },
1566 { XFER_PIO_0, 70, 290, 240, 600, 165, 150, 600, 0 },
1568 /* { XFER_PIO_SLOW, 120, 290, 240, 960, 290, 240, 960, 0 }, */
1573 #define ENOUGH(v,unit) (((v)-1)/(unit)+1)
1574 #define EZ(v,unit) ((v)?ENOUGH(v,unit):0)
1576 static void ata_timing_quantize(const struct ata_timing *t, struct ata_timing *q, int T, int UT)
1578 q->setup = EZ(t->setup * 1000, T);
1579 q->act8b = EZ(t->act8b * 1000, T);
1580 q->rec8b = EZ(t->rec8b * 1000, T);
1581 q->cyc8b = EZ(t->cyc8b * 1000, T);
1582 q->active = EZ(t->active * 1000, T);
1583 q->recover = EZ(t->recover * 1000, T);
1584 q->cycle = EZ(t->cycle * 1000, T);
1585 q->udma = EZ(t->udma * 1000, UT);
1588 void ata_timing_merge(const struct ata_timing *a, const struct ata_timing *b,
1589 struct ata_timing *m, unsigned int what)
1591 if (what & ATA_TIMING_SETUP ) m->setup = max(a->setup, b->setup);
1592 if (what & ATA_TIMING_ACT8B ) m->act8b = max(a->act8b, b->act8b);
1593 if (what & ATA_TIMING_REC8B ) m->rec8b = max(a->rec8b, b->rec8b);
1594 if (what & ATA_TIMING_CYC8B ) m->cyc8b = max(a->cyc8b, b->cyc8b);
1595 if (what & ATA_TIMING_ACTIVE ) m->active = max(a->active, b->active);
1596 if (what & ATA_TIMING_RECOVER) m->recover = max(a->recover, b->recover);
1597 if (what & ATA_TIMING_CYCLE ) m->cycle = max(a->cycle, b->cycle);
1598 if (what & ATA_TIMING_UDMA ) m->udma = max(a->udma, b->udma);
1601 static const struct ata_timing* ata_timing_find_mode(unsigned short speed)
1603 const struct ata_timing *t;
1605 for (t = ata_timing; t->mode != speed; t++)
1606 if (t->mode == 0xFF)
1611 int ata_timing_compute(struct ata_device *adev, unsigned short speed,
1612 struct ata_timing *t, int T, int UT)
1614 const struct ata_timing *s;
1615 struct ata_timing p;
1621 if (!(s = ata_timing_find_mode(speed)))
1624 memcpy(t, s, sizeof(*s));
1627 * If the drive is an EIDE drive, it can tell us it needs extended
1628 * PIO/MW_DMA cycle timing.
1631 if (adev->id[ATA_ID_FIELD_VALID] & 2) { /* EIDE drive */
1632 memset(&p, 0, sizeof(p));
1633 if(speed >= XFER_PIO_0 && speed <= XFER_SW_DMA_0) {
1634 if (speed <= XFER_PIO_2) p.cycle = p.cyc8b = adev->id[ATA_ID_EIDE_PIO];
1635 else p.cycle = p.cyc8b = adev->id[ATA_ID_EIDE_PIO_IORDY];
1636 } else if(speed >= XFER_MW_DMA_0 && speed <= XFER_MW_DMA_2) {
1637 p.cycle = adev->id[ATA_ID_EIDE_DMA_MIN];
1639 ata_timing_merge(&p, t, t, ATA_TIMING_CYCLE | ATA_TIMING_CYC8B);
1643 * Convert the timing to bus clock counts.
1646 ata_timing_quantize(t, t, T, UT);
1649 * Even in DMA/UDMA modes we still use PIO access for IDENTIFY, S.M.A.R.T
1650 * and some other commands. We have to ensure that the DMA cycle timing is
1651 * slower/equal than the fastest PIO timing.
1654 if (speed > XFER_PIO_4) {
1655 ata_timing_compute(adev, adev->pio_mode, &p, T, UT);
1656 ata_timing_merge(&p, t, t, ATA_TIMING_ALL);
1660 * Lenghten active & recovery time so that cycle time is correct.
1663 if (t->act8b + t->rec8b < t->cyc8b) {
1664 t->act8b += (t->cyc8b - (t->act8b + t->rec8b)) / 2;
1665 t->rec8b = t->cyc8b - t->act8b;
1668 if (t->active + t->recover < t->cycle) {
1669 t->active += (t->cycle - (t->active + t->recover)) / 2;
1670 t->recover = t->cycle - t->active;
1676 static const struct {
1679 } xfer_mode_classes[] = {
1680 { ATA_SHIFT_UDMA, XFER_UDMA_0 },
1681 { ATA_SHIFT_MWDMA, XFER_MW_DMA_0 },
1682 { ATA_SHIFT_PIO, XFER_PIO_0 },
1685 static inline u8 base_from_shift(unsigned int shift)
1689 for (i = 0; i < ARRAY_SIZE(xfer_mode_classes); i++)
1690 if (xfer_mode_classes[i].shift == shift)
1691 return xfer_mode_classes[i].base;
1696 static void ata_dev_set_mode(struct ata_port *ap, struct ata_device *dev)
1701 if (!ata_dev_present(dev) || (ap->flags & ATA_FLAG_PORT_DISABLED))
1704 if (dev->xfer_shift == ATA_SHIFT_PIO)
1705 dev->flags |= ATA_DFLAG_PIO;
1707 ata_dev_set_xfermode(ap, dev);
1709 base = base_from_shift(dev->xfer_shift);
1710 ofs = dev->xfer_mode - base;
1711 idx = ofs + dev->xfer_shift;
1712 WARN_ON(idx >= ARRAY_SIZE(xfer_mode_str));
1714 DPRINTK("idx=%d xfer_shift=%u, xfer_mode=0x%x, base=0x%x, offset=%d\n",
1715 idx, dev->xfer_shift, (int)dev->xfer_mode, (int)base, ofs);
1717 printk(KERN_INFO "ata%u: dev %u configured for %s\n",
1718 ap->id, dev->devno, xfer_mode_str[idx]);
1721 static int ata_host_set_pio(struct ata_port *ap)
1727 mask = ata_get_mode_mask(ap, ATA_SHIFT_PIO);
1730 printk(KERN_WARNING "ata%u: no PIO support\n", ap->id);
1734 base = base_from_shift(ATA_SHIFT_PIO);
1735 xfer_mode = base + x;
1737 DPRINTK("base 0x%x xfer_mode 0x%x mask 0x%x x %d\n",
1738 (int)base, (int)xfer_mode, mask, x);
1740 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1741 struct ata_device *dev = &ap->device[i];
1742 if (ata_dev_present(dev)) {
1743 dev->pio_mode = xfer_mode;
1744 dev->xfer_mode = xfer_mode;
1745 dev->xfer_shift = ATA_SHIFT_PIO;
1746 if (ap->ops->set_piomode)
1747 ap->ops->set_piomode(ap, dev);
1754 static void ata_host_set_dma(struct ata_port *ap, u8 xfer_mode,
1755 unsigned int xfer_shift)
1759 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1760 struct ata_device *dev = &ap->device[i];
1761 if (ata_dev_present(dev)) {
1762 dev->dma_mode = xfer_mode;
1763 dev->xfer_mode = xfer_mode;
1764 dev->xfer_shift = xfer_shift;
1765 if (ap->ops->set_dmamode)
1766 ap->ops->set_dmamode(ap, dev);
1772 * ata_set_mode - Program timings and issue SET FEATURES - XFER
1773 * @ap: port on which timings will be programmed
1775 * Set ATA device disk transfer mode (PIO3, UDMA6, etc.).
1778 * PCI/etc. bus probe sem.
1781 static void ata_set_mode(struct ata_port *ap)
1783 unsigned int xfer_shift;
1787 /* step 1: always set host PIO timings */
1788 rc = ata_host_set_pio(ap);
1792 /* step 2: choose the best data xfer mode */
1793 xfer_mode = xfer_shift = 0;
1794 rc = ata_choose_xfer_mode(ap, &xfer_mode, &xfer_shift);
1798 /* step 3: if that xfer mode isn't PIO, set host DMA timings */
1799 if (xfer_shift != ATA_SHIFT_PIO)
1800 ata_host_set_dma(ap, xfer_mode, xfer_shift);
1802 /* step 4: update devices' xfer mode */
1803 ata_dev_set_mode(ap, &ap->device[0]);
1804 ata_dev_set_mode(ap, &ap->device[1]);
1806 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1809 if (ap->ops->post_set_mode)
1810 ap->ops->post_set_mode(ap);
1815 ata_port_disable(ap);
1819 * ata_busy_sleep - sleep until BSY clears, or timeout
1820 * @ap: port containing status register to be polled
1821 * @tmout_pat: impatience timeout
1822 * @tmout: overall timeout
1824 * Sleep until ATA Status register bit BSY clears,
1825 * or a timeout occurs.
1831 static unsigned int ata_busy_sleep (struct ata_port *ap,
1832 unsigned long tmout_pat,
1833 unsigned long tmout)
1835 unsigned long timer_start, timeout;
1838 status = ata_busy_wait(ap, ATA_BUSY, 300);
1839 timer_start = jiffies;
1840 timeout = timer_start + tmout_pat;
1841 while ((status & ATA_BUSY) && (time_before(jiffies, timeout))) {
1843 status = ata_busy_wait(ap, ATA_BUSY, 3);
1846 if (status & ATA_BUSY)
1847 printk(KERN_WARNING "ata%u is slow to respond, "
1848 "please be patient\n", ap->id);
1850 timeout = timer_start + tmout;
1851 while ((status & ATA_BUSY) && (time_before(jiffies, timeout))) {
1853 status = ata_chk_status(ap);
1856 if (status & ATA_BUSY) {
1857 printk(KERN_ERR "ata%u failed to respond (%lu secs)\n",
1858 ap->id, tmout / HZ);
1865 static void ata_bus_post_reset(struct ata_port *ap, unsigned int devmask)
1867 struct ata_ioports *ioaddr = &ap->ioaddr;
1868 unsigned int dev0 = devmask & (1 << 0);
1869 unsigned int dev1 = devmask & (1 << 1);
1870 unsigned long timeout;
1872 /* if device 0 was found in ata_devchk, wait for its
1876 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
1878 /* if device 1 was found in ata_devchk, wait for
1879 * register access, then wait for BSY to clear
1881 timeout = jiffies + ATA_TMOUT_BOOT;
1885 ap->ops->dev_select(ap, 1);
1886 if (ap->flags & ATA_FLAG_MMIO) {
1887 nsect = readb((void __iomem *) ioaddr->nsect_addr);
1888 lbal = readb((void __iomem *) ioaddr->lbal_addr);
1890 nsect = inb(ioaddr->nsect_addr);
1891 lbal = inb(ioaddr->lbal_addr);
1893 if ((nsect == 1) && (lbal == 1))
1895 if (time_after(jiffies, timeout)) {
1899 msleep(50); /* give drive a breather */
1902 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
1904 /* is all this really necessary? */
1905 ap->ops->dev_select(ap, 0);
1907 ap->ops->dev_select(ap, 1);
1909 ap->ops->dev_select(ap, 0);
1913 * ata_bus_edd - Issue EXECUTE DEVICE DIAGNOSTIC command.
1914 * @ap: Port to reset and probe
1916 * Use the EXECUTE DEVICE DIAGNOSTIC command to reset and
1917 * probe the bus. Not often used these days.
1920 * PCI/etc. bus probe sem.
1921 * Obtains host_set lock.
1925 static unsigned int ata_bus_edd(struct ata_port *ap)
1927 struct ata_taskfile tf;
1928 unsigned long flags;
1930 /* set up execute-device-diag (bus reset) taskfile */
1931 /* also, take interrupts to a known state (disabled) */
1932 DPRINTK("execute-device-diag\n");
1933 ata_tf_init(ap, &tf, 0);
1935 tf.command = ATA_CMD_EDD;
1936 tf.protocol = ATA_PROT_NODATA;
1939 spin_lock_irqsave(&ap->host_set->lock, flags);
1940 ata_tf_to_host(ap, &tf);
1941 spin_unlock_irqrestore(&ap->host_set->lock, flags);
1943 /* spec says at least 2ms. but who knows with those
1944 * crazy ATAPI devices...
1948 return ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
1951 static unsigned int ata_bus_softreset(struct ata_port *ap,
1952 unsigned int devmask)
1954 struct ata_ioports *ioaddr = &ap->ioaddr;
1956 DPRINTK("ata%u: bus reset via SRST\n", ap->id);
1958 /* software reset. causes dev0 to be selected */
1959 if (ap->flags & ATA_FLAG_MMIO) {
1960 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
1961 udelay(20); /* FIXME: flush */
1962 writeb(ap->ctl | ATA_SRST, (void __iomem *) ioaddr->ctl_addr);
1963 udelay(20); /* FIXME: flush */
1964 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
1966 outb(ap->ctl, ioaddr->ctl_addr);
1968 outb(ap->ctl | ATA_SRST, ioaddr->ctl_addr);
1970 outb(ap->ctl, ioaddr->ctl_addr);
1973 /* spec mandates ">= 2ms" before checking status.
1974 * We wait 150ms, because that was the magic delay used for
1975 * ATAPI devices in Hale Landis's ATADRVR, for the period of time
1976 * between when the ATA command register is written, and then
1977 * status is checked. Because waiting for "a while" before
1978 * checking status is fine, post SRST, we perform this magic
1979 * delay here as well.
1983 ata_bus_post_reset(ap, devmask);
1989 * ata_bus_reset - reset host port and associated ATA channel
1990 * @ap: port to reset
1992 * This is typically the first time we actually start issuing
1993 * commands to the ATA channel. We wait for BSY to clear, then
1994 * issue EXECUTE DEVICE DIAGNOSTIC command, polling for its
1995 * result. Determine what devices, if any, are on the channel
1996 * by looking at the device 0/1 error register. Look at the signature
1997 * stored in each device's taskfile registers, to determine if
1998 * the device is ATA or ATAPI.
2001 * PCI/etc. bus probe sem.
2002 * Obtains host_set lock.
2005 * Sets ATA_FLAG_PORT_DISABLED if bus reset fails.
2008 void ata_bus_reset(struct ata_port *ap)
2010 struct ata_ioports *ioaddr = &ap->ioaddr;
2011 unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS;
2013 unsigned int dev0, dev1 = 0, rc = 0, devmask = 0;
2015 DPRINTK("ENTER, host %u, port %u\n", ap->id, ap->port_no);
2017 /* determine if device 0/1 are present */
2018 if (ap->flags & ATA_FLAG_SATA_RESET)
2021 dev0 = ata_devchk(ap, 0);
2023 dev1 = ata_devchk(ap, 1);
2027 devmask |= (1 << 0);
2029 devmask |= (1 << 1);
2031 /* select device 0 again */
2032 ap->ops->dev_select(ap, 0);
2034 /* issue bus reset */
2035 if (ap->flags & ATA_FLAG_SRST)
2036 rc = ata_bus_softreset(ap, devmask);
2037 else if ((ap->flags & ATA_FLAG_SATA_RESET) == 0) {
2038 /* set up device control */
2039 if (ap->flags & ATA_FLAG_MMIO)
2040 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
2042 outb(ap->ctl, ioaddr->ctl_addr);
2043 rc = ata_bus_edd(ap);
2050 * determine by signature whether we have ATA or ATAPI devices
2052 err = ata_dev_try_classify(ap, 0);
2053 if ((slave_possible) && (err != 0x81))
2054 ata_dev_try_classify(ap, 1);
2056 /* re-enable interrupts */
2057 if (ap->ioaddr.ctl_addr) /* FIXME: hack. create a hook instead */
2060 /* is double-select really necessary? */
2061 if (ap->device[1].class != ATA_DEV_NONE)
2062 ap->ops->dev_select(ap, 1);
2063 if (ap->device[0].class != ATA_DEV_NONE)
2064 ap->ops->dev_select(ap, 0);
2066 /* if no devices were detected, disable this port */
2067 if ((ap->device[0].class == ATA_DEV_NONE) &&
2068 (ap->device[1].class == ATA_DEV_NONE))
2071 if (ap->flags & (ATA_FLAG_SATA_RESET | ATA_FLAG_SRST)) {
2072 /* set up device control for ATA_FLAG_SATA_RESET */
2073 if (ap->flags & ATA_FLAG_MMIO)
2074 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
2076 outb(ap->ctl, ioaddr->ctl_addr);
2083 printk(KERN_ERR "ata%u: disabling port\n", ap->id);
2084 ap->ops->port_disable(ap);
2089 static void ata_pr_blacklisted(const struct ata_port *ap,
2090 const struct ata_device *dev)
2092 printk(KERN_WARNING "ata%u: dev %u is on DMA blacklist, disabling DMA\n",
2093 ap->id, dev->devno);
2096 static const char * ata_dma_blacklist [] = {
2115 "Toshiba CD-ROM XM-6202B",
2116 "TOSHIBA CD-ROM XM-1702BC",
2118 "E-IDE CD-ROM CR-840",
2121 "SAMSUNG CD-ROM SC-148C",
2122 "SAMSUNG CD-ROM SC",
2124 "ATAPI CD-ROM DRIVE 40X MAXIMUM",
2128 static int ata_dma_blacklisted(const struct ata_device *dev)
2130 unsigned char model_num[40];
2135 ata_dev_id_string(dev->id, model_num, ATA_ID_PROD_OFS,
2138 len = strnlen(s, sizeof(model_num));
2140 /* ATAPI specifies that empty space is blank-filled; remove blanks */
2141 while ((len > 0) && (s[len - 1] == ' ')) {
2146 for (i = 0; i < ARRAY_SIZE(ata_dma_blacklist); i++)
2147 if (!strncmp(ata_dma_blacklist[i], s, len))
2153 static unsigned int ata_get_mode_mask(const struct ata_port *ap, int shift)
2155 const struct ata_device *master, *slave;
2158 master = &ap->device[0];
2159 slave = &ap->device[1];
2161 assert (ata_dev_present(master) || ata_dev_present(slave));
2163 if (shift == ATA_SHIFT_UDMA) {
2164 mask = ap->udma_mask;
2165 if (ata_dev_present(master)) {
2166 mask &= (master->id[ATA_ID_UDMA_MODES] & 0xff);
2167 if (ata_dma_blacklisted(master)) {
2169 ata_pr_blacklisted(ap, master);
2172 if (ata_dev_present(slave)) {
2173 mask &= (slave->id[ATA_ID_UDMA_MODES] & 0xff);
2174 if (ata_dma_blacklisted(slave)) {
2176 ata_pr_blacklisted(ap, slave);
2180 else if (shift == ATA_SHIFT_MWDMA) {
2181 mask = ap->mwdma_mask;
2182 if (ata_dev_present(master)) {
2183 mask &= (master->id[ATA_ID_MWDMA_MODES] & 0x07);
2184 if (ata_dma_blacklisted(master)) {
2186 ata_pr_blacklisted(ap, master);
2189 if (ata_dev_present(slave)) {
2190 mask &= (slave->id[ATA_ID_MWDMA_MODES] & 0x07);
2191 if (ata_dma_blacklisted(slave)) {
2193 ata_pr_blacklisted(ap, slave);
2197 else if (shift == ATA_SHIFT_PIO) {
2198 mask = ap->pio_mask;
2199 if (ata_dev_present(master)) {
2200 /* spec doesn't return explicit support for
2201 * PIO0-2, so we fake it
2203 u16 tmp_mode = master->id[ATA_ID_PIO_MODES] & 0x03;
2208 if (ata_dev_present(slave)) {
2209 /* spec doesn't return explicit support for
2210 * PIO0-2, so we fake it
2212 u16 tmp_mode = slave->id[ATA_ID_PIO_MODES] & 0x03;
2219 mask = 0xffffffff; /* shut up compiler warning */
2226 /* find greatest bit */
2227 static int fgb(u32 bitmap)
2232 for (i = 0; i < 32; i++)
2233 if (bitmap & (1 << i))
2240 * ata_choose_xfer_mode - attempt to find best transfer mode
2241 * @ap: Port for which an xfer mode will be selected
2242 * @xfer_mode_out: (output) SET FEATURES - XFER MODE code
2243 * @xfer_shift_out: (output) bit shift that selects this mode
2245 * Based on host and device capabilities, determine the
2246 * maximum transfer mode that is amenable to all.
2249 * PCI/etc. bus probe sem.
2252 * Zero on success, negative on error.
2255 static int ata_choose_xfer_mode(const struct ata_port *ap,
2257 unsigned int *xfer_shift_out)
2259 unsigned int mask, shift;
2262 for (i = 0; i < ARRAY_SIZE(xfer_mode_classes); i++) {
2263 shift = xfer_mode_classes[i].shift;
2264 mask = ata_get_mode_mask(ap, shift);
2268 *xfer_mode_out = xfer_mode_classes[i].base + x;
2269 *xfer_shift_out = shift;
2278 * ata_dev_set_xfermode - Issue SET FEATURES - XFER MODE command
2279 * @ap: Port associated with device @dev
2280 * @dev: Device to which command will be sent
2282 * Issue SET FEATURES - XFER MODE command to device @dev
2286 * PCI/etc. bus probe sem.
2289 static void ata_dev_set_xfermode(struct ata_port *ap, struct ata_device *dev)
2291 DECLARE_COMPLETION(wait);
2292 struct ata_queued_cmd *qc;
2294 unsigned long flags;
2296 /* set up set-features taskfile */
2297 DPRINTK("set features - xfer mode\n");
2299 qc = ata_qc_new_init(ap, dev);
2302 qc->tf.command = ATA_CMD_SET_FEATURES;
2303 qc->tf.feature = SETFEATURES_XFER;
2304 qc->tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
2305 qc->tf.protocol = ATA_PROT_NODATA;
2306 qc->tf.nsect = dev->xfer_mode;
2308 qc->waiting = &wait;
2309 qc->complete_fn = ata_qc_complete_noop;
2311 spin_lock_irqsave(&ap->host_set->lock, flags);
2312 rc = ata_qc_issue(qc);
2313 spin_unlock_irqrestore(&ap->host_set->lock, flags);
2316 ata_port_disable(ap);
2318 ata_qc_wait_err(qc, &wait);
2324 * ata_dev_reread_id - Reread the device identify device info
2325 * @ap: port where the device is
2326 * @dev: device to reread the identify device info
2331 static void ata_dev_reread_id(struct ata_port *ap, struct ata_device *dev)
2333 DECLARE_COMPLETION(wait);
2334 struct ata_queued_cmd *qc;
2335 unsigned long flags;
2338 qc = ata_qc_new_init(ap, dev);
2341 ata_sg_init_one(qc, dev->id, sizeof(dev->id));
2342 qc->dma_dir = DMA_FROM_DEVICE;
2344 if (dev->class == ATA_DEV_ATA) {
2345 qc->tf.command = ATA_CMD_ID_ATA;
2346 DPRINTK("do ATA identify\n");
2348 qc->tf.command = ATA_CMD_ID_ATAPI;
2349 DPRINTK("do ATAPI identify\n");
2352 qc->tf.flags |= ATA_TFLAG_DEVICE;
2353 qc->tf.protocol = ATA_PROT_PIO;
2356 qc->waiting = &wait;
2357 qc->complete_fn = ata_qc_complete_noop;
2359 spin_lock_irqsave(&ap->host_set->lock, flags);
2360 rc = ata_qc_issue(qc);
2361 spin_unlock_irqrestore(&ap->host_set->lock, flags);
2366 ata_qc_wait_err(qc, &wait);
2368 swap_buf_le16(dev->id, ATA_ID_WORDS);
2376 ata_port_disable(ap);
2380 * ata_dev_init_params - Issue INIT DEV PARAMS command
2381 * @ap: Port associated with device @dev
2382 * @dev: Device to which command will be sent
2387 static void ata_dev_init_params(struct ata_port *ap, struct ata_device *dev)
2389 DECLARE_COMPLETION(wait);
2390 struct ata_queued_cmd *qc;
2392 unsigned long flags;
2393 u16 sectors = dev->id[6];
2394 u16 heads = dev->id[3];
2396 /* Number of sectors per track 1-255. Number of heads 1-16 */
2397 if (sectors < 1 || sectors > 255 || heads < 1 || heads > 16)
2400 /* set up init dev params taskfile */
2401 DPRINTK("init dev params \n");
2403 qc = ata_qc_new_init(ap, dev);
2406 qc->tf.command = ATA_CMD_INIT_DEV_PARAMS;
2407 qc->tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
2408 qc->tf.protocol = ATA_PROT_NODATA;
2409 qc->tf.nsect = sectors;
2410 qc->tf.device |= (heads - 1) & 0x0f; /* max head = num. of heads - 1 */
2412 qc->waiting = &wait;
2413 qc->complete_fn = ata_qc_complete_noop;
2415 spin_lock_irqsave(&ap->host_set->lock, flags);
2416 rc = ata_qc_issue(qc);
2417 spin_unlock_irqrestore(&ap->host_set->lock, flags);
2420 ata_port_disable(ap);
2422 ata_qc_wait_err(qc, &wait);
2428 * ata_sg_clean - Unmap DMA memory associated with command
2429 * @qc: Command containing DMA memory to be released
2431 * Unmap all mapped DMA memory associated with this command.
2434 * spin_lock_irqsave(host_set lock)
2437 static void ata_sg_clean(struct ata_queued_cmd *qc)
2439 struct ata_port *ap = qc->ap;
2440 struct scatterlist *sg = qc->__sg;
2441 int dir = qc->dma_dir;
2442 void *pad_buf = NULL;
2444 assert(qc->flags & ATA_QCFLAG_DMAMAP);
2447 if (qc->flags & ATA_QCFLAG_SINGLE)
2448 assert(qc->n_elem == 1);
2450 VPRINTK("unmapping %u sg elements\n", qc->n_elem);
2452 /* if we padded the buffer out to 32-bit bound, and data
2453 * xfer direction is from-device, we must copy from the
2454 * pad buffer back into the supplied buffer
2456 if (qc->pad_len && !(qc->tf.flags & ATA_TFLAG_WRITE))
2457 pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
2459 if (qc->flags & ATA_QCFLAG_SG) {
2461 dma_unmap_sg(ap->host_set->dev, sg, qc->n_elem, dir);
2462 /* restore last sg */
2463 sg[qc->orig_n_elem - 1].length += qc->pad_len;
2465 struct scatterlist *psg = &qc->pad_sgent;
2466 void *addr = kmap_atomic(psg->page, KM_IRQ0);
2467 memcpy(addr + psg->offset, pad_buf, qc->pad_len);
2468 kunmap_atomic(psg->page, KM_IRQ0);
2471 if (sg_dma_len(&sg[0]) > 0)
2472 dma_unmap_single(ap->host_set->dev,
2473 sg_dma_address(&sg[0]), sg_dma_len(&sg[0]),
2476 sg->length += qc->pad_len;
2478 memcpy(qc->buf_virt + sg->length - qc->pad_len,
2479 pad_buf, qc->pad_len);
2482 qc->flags &= ~ATA_QCFLAG_DMAMAP;
2487 * ata_fill_sg - Fill PCI IDE PRD table
2488 * @qc: Metadata associated with taskfile to be transferred
2490 * Fill PCI IDE PRD (scatter-gather) table with segments
2491 * associated with the current disk command.
2494 * spin_lock_irqsave(host_set lock)
2497 static void ata_fill_sg(struct ata_queued_cmd *qc)
2499 struct ata_port *ap = qc->ap;
2500 struct scatterlist *sg;
2503 assert(qc->__sg != NULL);
2504 assert(qc->n_elem > 0);
2507 ata_for_each_sg(sg, qc) {
2511 /* determine if physical DMA addr spans 64K boundary.
2512 * Note h/w doesn't support 64-bit, so we unconditionally
2513 * truncate dma_addr_t to u32.
2515 addr = (u32) sg_dma_address(sg);
2516 sg_len = sg_dma_len(sg);
2519 offset = addr & 0xffff;
2521 if ((offset + sg_len) > 0x10000)
2522 len = 0x10000 - offset;
2524 ap->prd[idx].addr = cpu_to_le32(addr);
2525 ap->prd[idx].flags_len = cpu_to_le32(len & 0xffff);
2526 VPRINTK("PRD[%u] = (0x%X, 0x%X)\n", idx, addr, len);
2535 ap->prd[idx - 1].flags_len |= cpu_to_le32(ATA_PRD_EOT);
2538 * ata_check_atapi_dma - Check whether ATAPI DMA can be supported
2539 * @qc: Metadata associated with taskfile to check
2541 * Allow low-level driver to filter ATA PACKET commands, returning
2542 * a status indicating whether or not it is OK to use DMA for the
2543 * supplied PACKET command.
2546 * spin_lock_irqsave(host_set lock)
2548 * RETURNS: 0 when ATAPI DMA can be used
2551 int ata_check_atapi_dma(struct ata_queued_cmd *qc)
2553 struct ata_port *ap = qc->ap;
2554 int rc = 0; /* Assume ATAPI DMA is OK by default */
2556 if (ap->ops->check_atapi_dma)
2557 rc = ap->ops->check_atapi_dma(qc);
2562 * ata_qc_prep - Prepare taskfile for submission
2563 * @qc: Metadata associated with taskfile to be prepared
2565 * Prepare ATA taskfile for submission.
2568 * spin_lock_irqsave(host_set lock)
2570 void ata_qc_prep(struct ata_queued_cmd *qc)
2572 if (!(qc->flags & ATA_QCFLAG_DMAMAP))
2579 * ata_sg_init_one - Associate command with memory buffer
2580 * @qc: Command to be associated
2581 * @buf: Memory buffer
2582 * @buflen: Length of memory buffer, in bytes.
2584 * Initialize the data-related elements of queued_cmd @qc
2585 * to point to a single memory buffer, @buf of byte length @buflen.
2588 * spin_lock_irqsave(host_set lock)
2591 void ata_sg_init_one(struct ata_queued_cmd *qc, void *buf, unsigned int buflen)
2593 struct scatterlist *sg;
2595 qc->flags |= ATA_QCFLAG_SINGLE;
2597 memset(&qc->sgent, 0, sizeof(qc->sgent));
2598 qc->__sg = &qc->sgent;
2600 qc->orig_n_elem = 1;
2604 sg_init_one(sg, buf, buflen);
2608 * ata_sg_init - Associate command with scatter-gather table.
2609 * @qc: Command to be associated
2610 * @sg: Scatter-gather table.
2611 * @n_elem: Number of elements in s/g table.
2613 * Initialize the data-related elements of queued_cmd @qc
2614 * to point to a scatter-gather table @sg, containing @n_elem
2618 * spin_lock_irqsave(host_set lock)
2621 void ata_sg_init(struct ata_queued_cmd *qc, struct scatterlist *sg,
2622 unsigned int n_elem)
2624 qc->flags |= ATA_QCFLAG_SG;
2626 qc->n_elem = n_elem;
2627 qc->orig_n_elem = n_elem;
2631 * ata_sg_setup_one - DMA-map the memory buffer associated with a command.
2632 * @qc: Command with memory buffer to be mapped.
2634 * DMA-map the memory buffer associated with queued_cmd @qc.
2637 * spin_lock_irqsave(host_set lock)
2640 * Zero on success, negative on error.
2643 static int ata_sg_setup_one(struct ata_queued_cmd *qc)
2645 struct ata_port *ap = qc->ap;
2646 int dir = qc->dma_dir;
2647 struct scatterlist *sg = qc->__sg;
2648 dma_addr_t dma_address;
2650 /* we must lengthen transfers to end on a 32-bit boundary */
2651 qc->pad_len = sg->length & 3;
2653 void *pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
2654 struct scatterlist *psg = &qc->pad_sgent;
2656 assert(qc->dev->class == ATA_DEV_ATAPI);
2658 memset(pad_buf, 0, ATA_DMA_PAD_SZ);
2660 if (qc->tf.flags & ATA_TFLAG_WRITE)
2661 memcpy(pad_buf, qc->buf_virt + sg->length - qc->pad_len,
2664 sg_dma_address(psg) = ap->pad_dma + (qc->tag * ATA_DMA_PAD_SZ);
2665 sg_dma_len(psg) = ATA_DMA_PAD_SZ;
2667 sg->length -= qc->pad_len;
2669 DPRINTK("padding done, sg->length=%u pad_len=%u\n",
2670 sg->length, qc->pad_len);
2674 sg_dma_address(sg) = 0;
2678 dma_address = dma_map_single(ap->host_set->dev, qc->buf_virt,
2680 if (dma_mapping_error(dma_address)) {
2682 sg->length += qc->pad_len;
2686 sg_dma_address(sg) = dma_address;
2688 sg_dma_len(sg) = sg->length;
2690 DPRINTK("mapped buffer of %d bytes for %s\n", sg_dma_len(sg),
2691 qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
2697 * ata_sg_setup - DMA-map the scatter-gather table associated with a command.
2698 * @qc: Command with scatter-gather table to be mapped.
2700 * DMA-map the scatter-gather table associated with queued_cmd @qc.
2703 * spin_lock_irqsave(host_set lock)
2706 * Zero on success, negative on error.
2710 static int ata_sg_setup(struct ata_queued_cmd *qc)
2712 struct ata_port *ap = qc->ap;
2713 struct scatterlist *sg = qc->__sg;
2714 struct scatterlist *lsg = &sg[qc->n_elem - 1];
2715 int n_elem, pre_n_elem, dir, trim_sg = 0;
2717 VPRINTK("ENTER, ata%u\n", ap->id);
2718 assert(qc->flags & ATA_QCFLAG_SG);
2720 /* we must lengthen transfers to end on a 32-bit boundary */
2721 qc->pad_len = lsg->length & 3;
2723 void *pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
2724 struct scatterlist *psg = &qc->pad_sgent;
2725 unsigned int offset;
2727 assert(qc->dev->class == ATA_DEV_ATAPI);
2729 memset(pad_buf, 0, ATA_DMA_PAD_SZ);
2732 * psg->page/offset are used to copy to-be-written
2733 * data in this function or read data in ata_sg_clean.
2735 offset = lsg->offset + lsg->length - qc->pad_len;
2736 psg->page = nth_page(lsg->page, offset >> PAGE_SHIFT);
2737 psg->offset = offset_in_page(offset);
2739 if (qc->tf.flags & ATA_TFLAG_WRITE) {
2740 void *addr = kmap_atomic(psg->page, KM_IRQ0);
2741 memcpy(pad_buf, addr + psg->offset, qc->pad_len);
2742 kunmap_atomic(psg->page, KM_IRQ0);
2745 sg_dma_address(psg) = ap->pad_dma + (qc->tag * ATA_DMA_PAD_SZ);
2746 sg_dma_len(psg) = ATA_DMA_PAD_SZ;
2748 lsg->length -= qc->pad_len;
2749 if (lsg->length == 0)
2752 DPRINTK("padding done, sg[%d].length=%u pad_len=%u\n",
2753 qc->n_elem - 1, lsg->length, qc->pad_len);
2756 pre_n_elem = qc->n_elem;
2757 if (trim_sg && pre_n_elem)
2766 n_elem = dma_map_sg(ap->host_set->dev, sg, pre_n_elem, dir);
2768 /* restore last sg */
2769 lsg->length += qc->pad_len;
2773 DPRINTK("%d sg elements mapped\n", n_elem);
2776 qc->n_elem = n_elem;
2782 * ata_poll_qc_complete - turn irq back on and finish qc
2783 * @qc: Command to complete
2784 * @err_mask: ATA status register content
2787 * None. (grabs host lock)
2790 void ata_poll_qc_complete(struct ata_queued_cmd *qc, unsigned int err_mask)
2792 struct ata_port *ap = qc->ap;
2793 unsigned long flags;
2795 spin_lock_irqsave(&ap->host_set->lock, flags);
2797 ata_qc_complete(qc, err_mask);
2798 spin_unlock_irqrestore(&ap->host_set->lock, flags);
2803 * @ap: the target ata_port
2806 * None. (executing in kernel thread context)
2809 * timeout value to use
2812 static unsigned long ata_pio_poll(struct ata_port *ap)
2815 unsigned int poll_state = HSM_ST_UNKNOWN;
2816 unsigned int reg_state = HSM_ST_UNKNOWN;
2818 switch (ap->hsm_task_state) {
2821 poll_state = HSM_ST_POLL;
2825 case HSM_ST_LAST_POLL:
2826 poll_state = HSM_ST_LAST_POLL;
2827 reg_state = HSM_ST_LAST;
2834 status = ata_chk_status(ap);
2835 if (status & ATA_BUSY) {
2836 if (time_after(jiffies, ap->pio_task_timeout)) {
2837 ap->hsm_task_state = HSM_ST_TMOUT;
2840 ap->hsm_task_state = poll_state;
2841 return ATA_SHORT_PAUSE;
2844 ap->hsm_task_state = reg_state;
2849 * ata_pio_complete - check if drive is busy or idle
2850 * @ap: the target ata_port
2853 * None. (executing in kernel thread context)
2856 * Zero if qc completed.
2857 * Non-zero if has next.
2860 static int ata_pio_complete (struct ata_port *ap)
2862 struct ata_queued_cmd *qc;
2866 * This is purely heuristic. This is a fast path. Sometimes when
2867 * we enter, BSY will be cleared in a chk-status or two. If not,
2868 * the drive is probably seeking or something. Snooze for a couple
2869 * msecs, then chk-status again. If still busy, fall back to
2870 * HSM_ST_LAST_POLL state.
2872 drv_stat = ata_busy_wait(ap, ATA_BUSY | ATA_DRQ, 10);
2873 if (drv_stat & (ATA_BUSY | ATA_DRQ)) {
2875 drv_stat = ata_busy_wait(ap, ATA_BUSY | ATA_DRQ, 10);
2876 if (drv_stat & (ATA_BUSY | ATA_DRQ)) {
2877 ap->hsm_task_state = HSM_ST_LAST_POLL;
2878 ap->pio_task_timeout = jiffies + ATA_TMOUT_PIO;
2883 drv_stat = ata_wait_idle(ap);
2884 if (!ata_ok(drv_stat)) {
2885 ap->hsm_task_state = HSM_ST_ERR;
2889 qc = ata_qc_from_tag(ap, ap->active_tag);
2892 ap->hsm_task_state = HSM_ST_IDLE;
2894 ata_poll_qc_complete(qc, 0);
2896 /* another command may start at this point */
2903 * swap_buf_le16 - swap halves of 16-words in place
2904 * @buf: Buffer to swap
2905 * @buf_words: Number of 16-bit words in buffer.
2907 * Swap halves of 16-bit words if needed to convert from
2908 * little-endian byte order to native cpu byte order, or
2912 * Inherited from caller.
2914 void swap_buf_le16(u16 *buf, unsigned int buf_words)
2919 for (i = 0; i < buf_words; i++)
2920 buf[i] = le16_to_cpu(buf[i]);
2921 #endif /* __BIG_ENDIAN */
2925 * ata_mmio_data_xfer - Transfer data by MMIO
2926 * @ap: port to read/write
2928 * @buflen: buffer length
2929 * @write_data: read/write
2931 * Transfer data from/to the device data register by MMIO.
2934 * Inherited from caller.
2937 static void ata_mmio_data_xfer(struct ata_port *ap, unsigned char *buf,
2938 unsigned int buflen, int write_data)
2941 unsigned int words = buflen >> 1;
2942 u16 *buf16 = (u16 *) buf;
2943 void __iomem *mmio = (void __iomem *)ap->ioaddr.data_addr;
2945 /* Transfer multiple of 2 bytes */
2947 for (i = 0; i < words; i++)
2948 writew(le16_to_cpu(buf16[i]), mmio);
2950 for (i = 0; i < words; i++)
2951 buf16[i] = cpu_to_le16(readw(mmio));
2954 /* Transfer trailing 1 byte, if any. */
2955 if (unlikely(buflen & 0x01)) {
2956 u16 align_buf[1] = { 0 };
2957 unsigned char *trailing_buf = buf + buflen - 1;
2960 memcpy(align_buf, trailing_buf, 1);
2961 writew(le16_to_cpu(align_buf[0]), mmio);
2963 align_buf[0] = cpu_to_le16(readw(mmio));
2964 memcpy(trailing_buf, align_buf, 1);
2970 * ata_pio_data_xfer - Transfer data by PIO
2971 * @ap: port to read/write
2973 * @buflen: buffer length
2974 * @write_data: read/write
2976 * Transfer data from/to the device data register by PIO.
2979 * Inherited from caller.
2982 static void ata_pio_data_xfer(struct ata_port *ap, unsigned char *buf,
2983 unsigned int buflen, int write_data)
2985 unsigned int words = buflen >> 1;
2987 /* Transfer multiple of 2 bytes */
2989 outsw(ap->ioaddr.data_addr, buf, words);
2991 insw(ap->ioaddr.data_addr, buf, words);
2993 /* Transfer trailing 1 byte, if any. */
2994 if (unlikely(buflen & 0x01)) {
2995 u16 align_buf[1] = { 0 };
2996 unsigned char *trailing_buf = buf + buflen - 1;
2999 memcpy(align_buf, trailing_buf, 1);
3000 outw(le16_to_cpu(align_buf[0]), ap->ioaddr.data_addr);
3002 align_buf[0] = cpu_to_le16(inw(ap->ioaddr.data_addr));
3003 memcpy(trailing_buf, align_buf, 1);
3009 * ata_data_xfer - Transfer data from/to the data register.
3010 * @ap: port to read/write
3012 * @buflen: buffer length
3013 * @do_write: read/write
3015 * Transfer data from/to the device data register.
3018 * Inherited from caller.
3021 static void ata_data_xfer(struct ata_port *ap, unsigned char *buf,
3022 unsigned int buflen, int do_write)
3024 if (ap->flags & ATA_FLAG_MMIO)
3025 ata_mmio_data_xfer(ap, buf, buflen, do_write);
3027 ata_pio_data_xfer(ap, buf, buflen, do_write);
3031 * ata_pio_sector - Transfer ATA_SECT_SIZE (512 bytes) of data.
3032 * @qc: Command on going
3034 * Transfer ATA_SECT_SIZE of data from/to the ATA device.
3037 * Inherited from caller.
3040 static void ata_pio_sector(struct ata_queued_cmd *qc)
3042 int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
3043 struct scatterlist *sg = qc->__sg;
3044 struct ata_port *ap = qc->ap;
3046 unsigned int offset;
3049 if (qc->cursect == (qc->nsect - 1))
3050 ap->hsm_task_state = HSM_ST_LAST;
3052 page = sg[qc->cursg].page;
3053 offset = sg[qc->cursg].offset + qc->cursg_ofs * ATA_SECT_SIZE;
3055 /* get the current page and offset */
3056 page = nth_page(page, (offset >> PAGE_SHIFT));
3057 offset %= PAGE_SIZE;
3059 DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
3061 if (PageHighMem(page)) {
3062 unsigned long flags;
3064 local_irq_save(flags);
3065 buf = kmap_atomic(page, KM_IRQ0);
3067 /* do the actual data transfer */
3068 ata_data_xfer(ap, buf + offset, ATA_SECT_SIZE, do_write);
3070 kunmap_atomic(buf, KM_IRQ0);
3071 local_irq_restore(flags);
3073 buf = page_address(page);
3074 ata_data_xfer(ap, buf + offset, ATA_SECT_SIZE, do_write);
3080 if ((qc->cursg_ofs * ATA_SECT_SIZE) == (&sg[qc->cursg])->length) {
3087 * ata_pio_sectors - Transfer one or many 512-byte sectors.
3088 * @qc: Command on going
3090 * Transfer one or many ATA_SECT_SIZE of data from/to the
3091 * ATA device for the DRQ request.
3094 * Inherited from caller.
3097 static void ata_pio_sectors(struct ata_queued_cmd *qc)
3099 if (is_multi_taskfile(&qc->tf)) {
3100 /* READ/WRITE MULTIPLE */
3103 assert(qc->dev->multi_count);
3105 nsect = min(qc->nsect - qc->cursect, qc->dev->multi_count);
3113 * atapi_send_cdb - Write CDB bytes to hardware
3114 * @ap: Port to which ATAPI device is attached.
3115 * @qc: Taskfile currently active
3117 * When device has indicated its readiness to accept
3118 * a CDB, this function is called. Send the CDB.
3124 static void atapi_send_cdb(struct ata_port *ap, struct ata_queued_cmd *qc)
3127 DPRINTK("send cdb\n");
3128 assert(ap->cdb_len >= 12);
3130 ata_data_xfer(ap, qc->cdb, ap->cdb_len, 1);
3131 ata_altstatus(ap); /* flush */
3133 switch (qc->tf.protocol) {
3134 case ATA_PROT_ATAPI:
3135 ap->hsm_task_state = HSM_ST;
3137 case ATA_PROT_ATAPI_NODATA:
3138 ap->hsm_task_state = HSM_ST_LAST;
3140 case ATA_PROT_ATAPI_DMA:
3141 ap->hsm_task_state = HSM_ST_LAST;
3142 /* initiate bmdma */
3143 ap->ops->bmdma_start(qc);
3149 * ata_pio_first_block - Write first data block to hardware
3150 * @ap: Port to which ATA/ATAPI device is attached.
3152 * When device has indicated its readiness to accept
3153 * the data, this function sends out the CDB or
3154 * the first data block by PIO.
3156 * - If polling, ata_pio_task() handles the rest.
3157 * - Otherwise, interrupt handler takes over.
3160 * Kernel thread context (may sleep)
3163 * Zero if irq handler takes over
3164 * Non-zero if has next (polling).
3167 static int ata_pio_first_block(struct ata_port *ap)
3169 struct ata_queued_cmd *qc;
3171 unsigned long flags;
3174 qc = ata_qc_from_tag(ap, ap->active_tag);
3176 assert(qc->flags & ATA_QCFLAG_ACTIVE);
3178 /* if polling, we will stay in the work queue after sending the data.
3179 * otherwise, interrupt handler takes over after sending the data.
3181 has_next = (qc->tf.flags & ATA_TFLAG_POLLING);
3183 /* sleep-wait for BSY to clear */
3184 DPRINTK("busy wait\n");
3185 if (ata_busy_sleep(ap, ATA_TMOUT_DATAOUT_QUICK, ATA_TMOUT_DATAOUT)) {
3186 ap->hsm_task_state = HSM_ST_TMOUT;
3190 /* make sure DRQ is set */
3191 status = ata_chk_status(ap);
3192 if ((status & (ATA_BUSY | ATA_DRQ)) != ATA_DRQ) {
3193 /* device status error */
3194 ap->hsm_task_state = HSM_ST_ERR;
3198 /* Send the CDB (atapi) or the first data block (ata pio out).
3199 * During the state transition, interrupt handler shouldn't
3200 * be invoked before the data transfer is complete and
3201 * hsm_task_state is changed. Hence, the following locking.
3203 spin_lock_irqsave(&ap->host_set->lock, flags);
3205 if (qc->tf.protocol == ATA_PROT_PIO) {
3206 /* PIO data out protocol.
3207 * send first data block.
3210 /* ata_pio_sectors() might change the state to HSM_ST_LAST.
3211 * so, the state is changed here before ata_pio_sectors().
3213 ap->hsm_task_state = HSM_ST;
3214 ata_pio_sectors(qc);
3215 ata_altstatus(ap); /* flush */
3218 atapi_send_cdb(ap, qc);
3220 spin_unlock_irqrestore(&ap->host_set->lock, flags);
3222 /* if polling, ata_pio_task() handles the rest.
3223 * otherwise, interrupt handler takes over from here.
3228 return 1; /* has next */
3232 * __atapi_pio_bytes - Transfer data from/to the ATAPI device.
3233 * @qc: Command on going
3234 * @bytes: number of bytes
3236 * Transfer Transfer data from/to the ATAPI device.
3239 * Inherited from caller.
3243 static void __atapi_pio_bytes(struct ata_queued_cmd *qc, unsigned int bytes)
3245 int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
3246 struct scatterlist *sg = qc->__sg;
3247 struct ata_port *ap = qc->ap;
3250 unsigned int offset, count;
3252 if (qc->curbytes + bytes >= qc->nbytes)
3253 ap->hsm_task_state = HSM_ST_LAST;
3256 if (unlikely(qc->cursg >= qc->n_elem)) {
3258 * The end of qc->sg is reached and the device expects
3259 * more data to transfer. In order not to overrun qc->sg
3260 * and fulfill length specified in the byte count register,
3261 * - for read case, discard trailing data from the device
3262 * - for write case, padding zero data to the device
3264 u16 pad_buf[1] = { 0 };
3265 unsigned int words = bytes >> 1;
3268 if (words) /* warning if bytes > 1 */
3269 printk(KERN_WARNING "ata%u: %u bytes trailing data\n",
3272 for (i = 0; i < words; i++)
3273 ata_data_xfer(ap, (unsigned char*)pad_buf, 2, do_write);
3275 ap->hsm_task_state = HSM_ST_LAST;
3279 sg = &qc->__sg[qc->cursg];
3282 offset = sg->offset + qc->cursg_ofs;
3284 /* get the current page and offset */
3285 page = nth_page(page, (offset >> PAGE_SHIFT));
3286 offset %= PAGE_SIZE;
3288 /* don't overrun current sg */
3289 count = min(sg->length - qc->cursg_ofs, bytes);
3291 /* don't cross page boundaries */
3292 count = min(count, (unsigned int)PAGE_SIZE - offset);
3294 DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
3296 if (PageHighMem(page)) {
3297 unsigned long flags;
3299 local_irq_save(flags);
3300 buf = kmap_atomic(page, KM_IRQ0);
3302 /* do the actual data transfer */
3303 ata_data_xfer(ap, buf + offset, count, do_write);
3305 kunmap_atomic(buf, KM_IRQ0);
3306 local_irq_restore(flags);
3308 buf = page_address(page);
3309 ata_data_xfer(ap, buf + offset, count, do_write);
3313 qc->curbytes += count;
3314 qc->cursg_ofs += count;
3316 if (qc->cursg_ofs == sg->length) {
3326 * atapi_pio_bytes - Transfer data from/to the ATAPI device.
3327 * @qc: Command on going
3329 * Transfer Transfer data from/to the ATAPI device.
3332 * Inherited from caller.
3335 static void atapi_pio_bytes(struct ata_queued_cmd *qc)
3337 struct ata_port *ap = qc->ap;
3338 struct ata_device *dev = qc->dev;
3339 unsigned int ireason, bc_lo, bc_hi, bytes;
3340 int i_write, do_write = (qc->tf.flags & ATA_TFLAG_WRITE) ? 1 : 0;
3342 ap->ops->tf_read(ap, &qc->tf);
3343 ireason = qc->tf.nsect;
3344 bc_lo = qc->tf.lbam;
3345 bc_hi = qc->tf.lbah;
3346 bytes = (bc_hi << 8) | bc_lo;
3348 /* shall be cleared to zero, indicating xfer of data */
3349 if (ireason & (1 << 0))
3352 /* make sure transfer direction matches expected */
3353 i_write = ((ireason & (1 << 1)) == 0) ? 1 : 0;
3354 if (do_write != i_write)
3357 VPRINTK("ata%u: xfering %d bytes\n", ap->id, bytes);
3359 __atapi_pio_bytes(qc, bytes);
3364 printk(KERN_INFO "ata%u: dev %u: ATAPI check failed\n",
3365 ap->id, dev->devno);
3366 ap->hsm_task_state = HSM_ST_ERR;
3370 * ata_pio_block - start PIO on a block
3371 * @ap: the target ata_port
3374 * None. (executing in kernel thread context)
3377 static void ata_pio_block(struct ata_port *ap)
3379 struct ata_queued_cmd *qc;
3383 * This is purely heuristic. This is a fast path.
3384 * Sometimes when we enter, BSY will be cleared in
3385 * a chk-status or two. If not, the drive is probably seeking
3386 * or something. Snooze for a couple msecs, then
3387 * chk-status again. If still busy, fall back to
3388 * HSM_ST_POLL state.
3390 status = ata_busy_wait(ap, ATA_BUSY, 5);
3391 if (status & ATA_BUSY) {
3393 status = ata_busy_wait(ap, ATA_BUSY, 10);
3394 if (status & ATA_BUSY) {
3395 ap->hsm_task_state = HSM_ST_POLL;
3396 ap->pio_task_timeout = jiffies + ATA_TMOUT_PIO;
3401 qc = ata_qc_from_tag(ap, ap->active_tag);
3404 if (is_atapi_taskfile(&qc->tf)) {
3405 /* no more data to transfer or unsupported ATAPI command */
3406 if ((status & ATA_DRQ) == 0) {
3407 ap->hsm_task_state = HSM_ST_LAST;
3411 atapi_pio_bytes(qc);
3413 /* handle BSY=0, DRQ=0 as error */
3414 if ((status & ATA_DRQ) == 0) {
3415 ap->hsm_task_state = HSM_ST_ERR;
3419 ata_pio_sectors(qc);
3422 ata_altstatus(ap); /* flush */
3425 static void ata_pio_error(struct ata_port *ap)
3427 struct ata_queued_cmd *qc;
3429 printk(KERN_WARNING "ata%u: PIO error\n", ap->id);
3431 qc = ata_qc_from_tag(ap, ap->active_tag);
3434 ap->hsm_task_state = HSM_ST_IDLE;
3436 ata_poll_qc_complete(qc, AC_ERR_ATA_BUS);
3439 static void ata_pio_task(void *_data)
3441 struct ata_port *ap = _data;
3442 unsigned long timeout;
3449 switch (ap->hsm_task_state) {
3451 has_next = ata_pio_first_block(ap);
3459 has_next = ata_pio_complete(ap);
3463 case HSM_ST_LAST_POLL:
3464 timeout = ata_pio_poll(ap);
3478 queue_delayed_work(ata_wq, &ap->pio_task, timeout);
3484 * ata_qc_timeout - Handle timeout of queued command
3485 * @qc: Command that timed out
3487 * Some part of the kernel (currently, only the SCSI layer)
3488 * has noticed that the active command on port @ap has not
3489 * completed after a specified length of time. Handle this
3490 * condition by disabling DMA (if necessary) and completing
3491 * transactions, with error if necessary.
3493 * This also handles the case of the "lost interrupt", where
3494 * for some reason (possibly hardware bug, possibly driver bug)
3495 * an interrupt was not delivered to the driver, even though the
3496 * transaction completed successfully.
3499 * Inherited from SCSI layer (none, can sleep)
3502 static void ata_qc_timeout(struct ata_queued_cmd *qc)
3504 struct ata_port *ap = qc->ap;
3505 struct ata_host_set *host_set = ap->host_set;
3506 u8 host_stat = 0, drv_stat;
3507 unsigned long flags;
3511 spin_lock_irqsave(&host_set->lock, flags);
3513 /* hack alert! We cannot use the supplied completion
3514 * function from inside the ->eh_strategy_handler() thread.
3515 * libata is the only user of ->eh_strategy_handler() in
3516 * any kernel, so the default scsi_done() assumes it is
3517 * not being called from the SCSI EH.
3519 qc->scsidone = scsi_finish_command;
3521 switch (qc->tf.protocol) {
3524 case ATA_PROT_ATAPI_DMA:
3525 host_stat = ap->ops->bmdma_status(ap);
3527 /* before we do anything else, clear DMA-Start bit */
3528 ap->ops->bmdma_stop(qc);
3534 drv_stat = ata_chk_status(ap);
3536 /* ack bmdma irq events */
3537 ap->ops->irq_clear(ap);
3539 printk(KERN_ERR "ata%u: command 0x%x timeout, stat 0x%x host_stat 0x%x\n",
3540 ap->id, qc->tf.command, drv_stat, host_stat);
3542 ap->hsm_task_state = HSM_ST_IDLE;
3544 /* complete taskfile transaction */
3545 ata_qc_complete(qc, ac_err_mask(drv_stat));
3549 spin_unlock_irqrestore(&host_set->lock, flags);
3555 * ata_eng_timeout - Handle timeout of queued command
3556 * @ap: Port on which timed-out command is active
3558 * Some part of the kernel (currently, only the SCSI layer)
3559 * has noticed that the active command on port @ap has not
3560 * completed after a specified length of time. Handle this
3561 * condition by disabling DMA (if necessary) and completing
3562 * transactions, with error if necessary.
3564 * This also handles the case of the "lost interrupt", where
3565 * for some reason (possibly hardware bug, possibly driver bug)
3566 * an interrupt was not delivered to the driver, even though the
3567 * transaction completed successfully.
3570 * Inherited from SCSI layer (none, can sleep)
3573 void ata_eng_timeout(struct ata_port *ap)
3575 struct ata_queued_cmd *qc;
3579 qc = ata_qc_from_tag(ap, ap->active_tag);
3583 printk(KERN_ERR "ata%u: BUG: timeout without command\n",
3593 * ata_qc_new - Request an available ATA command, for queueing
3594 * @ap: Port associated with device @dev
3595 * @dev: Device from whom we request an available command structure
3601 static struct ata_queued_cmd *ata_qc_new(struct ata_port *ap)
3603 struct ata_queued_cmd *qc = NULL;
3606 for (i = 0; i < ATA_MAX_QUEUE; i++)
3607 if (!test_and_set_bit(i, &ap->qactive)) {
3608 qc = ata_qc_from_tag(ap, i);
3619 * ata_qc_new_init - Request an available ATA command, and initialize it
3620 * @ap: Port associated with device @dev
3621 * @dev: Device from whom we request an available command structure
3627 struct ata_queued_cmd *ata_qc_new_init(struct ata_port *ap,
3628 struct ata_device *dev)
3630 struct ata_queued_cmd *qc;
3632 qc = ata_qc_new(ap);
3644 int ata_qc_complete_noop(struct ata_queued_cmd *qc, unsigned int err_mask)
3649 static void __ata_qc_complete(struct ata_queued_cmd *qc)
3651 struct ata_port *ap = qc->ap;
3652 unsigned int tag, do_clear = 0;
3656 if (likely(ata_tag_valid(tag))) {
3657 if (tag == ap->active_tag)
3658 ap->active_tag = ATA_TAG_POISON;
3659 qc->tag = ATA_TAG_POISON;
3664 struct completion *waiting = qc->waiting;
3669 if (likely(do_clear))
3670 clear_bit(tag, &ap->qactive);
3674 * ata_qc_free - free unused ata_queued_cmd
3675 * @qc: Command to complete
3677 * Designed to free unused ata_queued_cmd object
3678 * in case something prevents using it.
3681 * spin_lock_irqsave(host_set lock)
3683 void ata_qc_free(struct ata_queued_cmd *qc)
3685 assert(qc != NULL); /* ata_qc_from_tag _might_ return NULL */
3686 assert(qc->waiting == NULL); /* nothing should be waiting */
3688 __ata_qc_complete(qc);
3692 * ata_qc_complete - Complete an active ATA command
3693 * @qc: Command to complete
3694 * @err_mask: ATA Status register contents
3696 * Indicate to the mid and upper layers that an ATA
3697 * command has completed, with either an ok or not-ok status.
3700 * spin_lock_irqsave(host_set lock)
3703 void ata_qc_complete(struct ata_queued_cmd *qc, unsigned int err_mask)
3707 assert(qc != NULL); /* ata_qc_from_tag _might_ return NULL */
3708 assert(qc->flags & ATA_QCFLAG_ACTIVE);
3710 if (likely(qc->flags & ATA_QCFLAG_DMAMAP))
3713 /* atapi: mark qc as inactive to prevent the interrupt handler
3714 * from completing the command twice later, before the error handler
3715 * is called. (when rc != 0 and atapi request sense is needed)
3717 qc->flags &= ~ATA_QCFLAG_ACTIVE;
3719 /* call completion callback */
3720 rc = qc->complete_fn(qc, err_mask);
3722 /* if callback indicates not to complete command (non-zero),
3723 * return immediately
3728 __ata_qc_complete(qc);
3733 static inline int ata_should_dma_map(struct ata_queued_cmd *qc)
3735 struct ata_port *ap = qc->ap;
3737 switch (qc->tf.protocol) {
3739 case ATA_PROT_ATAPI_DMA:
3742 case ATA_PROT_ATAPI:
3744 case ATA_PROT_PIO_MULT:
3745 if (ap->flags & ATA_FLAG_PIO_DMA)
3758 * ata_qc_issue - issue taskfile to device
3759 * @qc: command to issue to device
3761 * Prepare an ATA command to submission to device.
3762 * This includes mapping the data into a DMA-able
3763 * area, filling in the S/G table, and finally
3764 * writing the taskfile to hardware, starting the command.
3767 * spin_lock_irqsave(host_set lock)
3770 * Zero on success, negative on error.
3773 int ata_qc_issue(struct ata_queued_cmd *qc)
3775 struct ata_port *ap = qc->ap;
3777 if (ata_should_dma_map(qc)) {
3778 if (qc->flags & ATA_QCFLAG_SG) {
3779 if (ata_sg_setup(qc))
3781 } else if (qc->flags & ATA_QCFLAG_SINGLE) {
3782 if (ata_sg_setup_one(qc))
3786 qc->flags &= ~ATA_QCFLAG_DMAMAP;
3789 ap->ops->qc_prep(qc);
3791 qc->ap->active_tag = qc->tag;
3792 qc->flags |= ATA_QCFLAG_ACTIVE;
3794 return ap->ops->qc_issue(qc);
3802 * ata_qc_issue_prot - issue taskfile to device in proto-dependent manner
3803 * @qc: command to issue to device
3805 * Using various libata functions and hooks, this function
3806 * starts an ATA command. ATA commands are grouped into
3807 * classes called "protocols", and issuing each type of protocol
3808 * is slightly different.
3810 * May be used as the qc_issue() entry in ata_port_operations.
3813 * spin_lock_irqsave(host_set lock)
3816 * Zero on success, negative on error.
3819 int ata_qc_issue_prot(struct ata_queued_cmd *qc)
3821 struct ata_port *ap = qc->ap;
3823 /* Use polling pio if the LLD doesn't handle
3824 * interrupt driven pio and atapi CDB interrupt.
3826 if (ap->flags & ATA_FLAG_PIO_POLLING) {
3827 switch (qc->tf.protocol) {
3829 case ATA_PROT_ATAPI:
3830 case ATA_PROT_ATAPI_NODATA:
3831 qc->tf.flags |= ATA_TFLAG_POLLING;
3833 case ATA_PROT_ATAPI_DMA:
3834 if (qc->dev->flags & ATA_DFLAG_CDB_INTR)
3842 /* select the device */
3843 ata_dev_select(ap, qc->dev->devno, 1, 0);
3845 /* start the command */
3846 switch (qc->tf.protocol) {
3847 case ATA_PROT_NODATA:
3848 if (qc->tf.flags & ATA_TFLAG_POLLING)
3849 ata_qc_set_polling(qc);
3851 ata_tf_to_host(ap, &qc->tf);
3852 ap->hsm_task_state = HSM_ST_LAST;
3854 if (qc->tf.flags & ATA_TFLAG_POLLING)
3855 queue_work(ata_wq, &ap->pio_task);
3860 assert(!(qc->tf.flags & ATA_TFLAG_POLLING));
3862 ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
3863 ap->ops->bmdma_setup(qc); /* set up bmdma */
3864 ap->ops->bmdma_start(qc); /* initiate bmdma */
3865 ap->hsm_task_state = HSM_ST_LAST;
3869 if (qc->tf.flags & ATA_TFLAG_POLLING)
3870 ata_qc_set_polling(qc);
3872 ata_tf_to_host(ap, &qc->tf);
3874 if (qc->tf.flags & ATA_TFLAG_WRITE) {
3875 /* PIO data out protocol */
3876 ap->hsm_task_state = HSM_ST_FIRST;
3877 queue_work(ata_wq, &ap->pio_task);
3879 /* always send first data block using
3880 * the ata_pio_task() codepath.
3883 /* PIO data in protocol */
3884 ap->hsm_task_state = HSM_ST;
3886 if (qc->tf.flags & ATA_TFLAG_POLLING)
3887 queue_work(ata_wq, &ap->pio_task);
3889 /* if polling, ata_pio_task() handles the rest.
3890 * otherwise, interrupt handler takes over from here.
3896 case ATA_PROT_ATAPI:
3897 case ATA_PROT_ATAPI_NODATA:
3898 if (qc->tf.flags & ATA_TFLAG_POLLING)
3899 ata_qc_set_polling(qc);
3901 ata_tf_to_host(ap, &qc->tf);
3902 ap->hsm_task_state = HSM_ST_FIRST;
3904 /* send cdb by polling if no cdb interrupt */
3905 if ((!(qc->dev->flags & ATA_DFLAG_CDB_INTR)) ||
3906 (qc->tf.flags & ATA_TFLAG_POLLING))
3907 queue_work(ata_wq, &ap->pio_task);
3910 case ATA_PROT_ATAPI_DMA:
3911 assert(!(qc->tf.flags & ATA_TFLAG_POLLING));
3913 ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
3914 ap->ops->bmdma_setup(qc); /* set up bmdma */
3915 ap->hsm_task_state = HSM_ST_FIRST;
3917 /* send cdb by polling if no cdb interrupt */
3918 if (!(qc->dev->flags & ATA_DFLAG_CDB_INTR))
3919 queue_work(ata_wq, &ap->pio_task);
3931 * ata_bmdma_setup_mmio - Set up PCI IDE BMDMA transaction
3932 * @qc: Info associated with this ATA transaction.
3935 * spin_lock_irqsave(host_set lock)
3938 static void ata_bmdma_setup_mmio (struct ata_queued_cmd *qc)
3940 struct ata_port *ap = qc->ap;
3941 unsigned int rw = (qc->tf.flags & ATA_TFLAG_WRITE);
3943 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
3945 /* load PRD table addr. */
3946 mb(); /* make sure PRD table writes are visible to controller */
3947 writel(ap->prd_dma, mmio + ATA_DMA_TABLE_OFS);
3949 /* specify data direction, triple-check start bit is clear */
3950 dmactl = readb(mmio + ATA_DMA_CMD);
3951 dmactl &= ~(ATA_DMA_WR | ATA_DMA_START);
3953 dmactl |= ATA_DMA_WR;
3954 writeb(dmactl, mmio + ATA_DMA_CMD);
3956 /* issue r/w command */
3957 ap->ops->exec_command(ap, &qc->tf);
3961 * ata_bmdma_start_mmio - Start a PCI IDE BMDMA transaction
3962 * @qc: Info associated with this ATA transaction.
3965 * spin_lock_irqsave(host_set lock)
3968 static void ata_bmdma_start_mmio (struct ata_queued_cmd *qc)
3970 struct ata_port *ap = qc->ap;
3971 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
3974 /* start host DMA transaction */
3975 dmactl = readb(mmio + ATA_DMA_CMD);
3976 writeb(dmactl | ATA_DMA_START, mmio + ATA_DMA_CMD);
3978 /* Strictly, one may wish to issue a readb() here, to
3979 * flush the mmio write. However, control also passes
3980 * to the hardware at this point, and it will interrupt
3981 * us when we are to resume control. So, in effect,
3982 * we don't care when the mmio write flushes.
3983 * Further, a read of the DMA status register _immediately_
3984 * following the write may not be what certain flaky hardware
3985 * is expected, so I think it is best to not add a readb()
3986 * without first all the MMIO ATA cards/mobos.
3987 * Or maybe I'm just being paranoid.
3992 * ata_bmdma_setup_pio - Set up PCI IDE BMDMA transaction (PIO)
3993 * @qc: Info associated with this ATA transaction.
3996 * spin_lock_irqsave(host_set lock)
3999 static void ata_bmdma_setup_pio (struct ata_queued_cmd *qc)
4001 struct ata_port *ap = qc->ap;
4002 unsigned int rw = (qc->tf.flags & ATA_TFLAG_WRITE);
4005 /* load PRD table addr. */
4006 outl(ap->prd_dma, ap->ioaddr.bmdma_addr + ATA_DMA_TABLE_OFS);
4008 /* specify data direction, triple-check start bit is clear */
4009 dmactl = inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
4010 dmactl &= ~(ATA_DMA_WR | ATA_DMA_START);
4012 dmactl |= ATA_DMA_WR;
4013 outb(dmactl, ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
4015 /* issue r/w command */
4016 ap->ops->exec_command(ap, &qc->tf);
4020 * ata_bmdma_start_pio - Start a PCI IDE BMDMA transaction (PIO)
4021 * @qc: Info associated with this ATA transaction.
4024 * spin_lock_irqsave(host_set lock)
4027 static void ata_bmdma_start_pio (struct ata_queued_cmd *qc)
4029 struct ata_port *ap = qc->ap;
4032 /* start host DMA transaction */
4033 dmactl = inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
4034 outb(dmactl | ATA_DMA_START,
4035 ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
4040 * ata_bmdma_start - Start a PCI IDE BMDMA transaction
4041 * @qc: Info associated with this ATA transaction.
4043 * Writes the ATA_DMA_START flag to the DMA command register.
4045 * May be used as the bmdma_start() entry in ata_port_operations.
4048 * spin_lock_irqsave(host_set lock)
4050 void ata_bmdma_start(struct ata_queued_cmd *qc)
4052 if (qc->ap->flags & ATA_FLAG_MMIO)
4053 ata_bmdma_start_mmio(qc);
4055 ata_bmdma_start_pio(qc);
4060 * ata_bmdma_setup - Set up PCI IDE BMDMA transaction
4061 * @qc: Info associated with this ATA transaction.
4063 * Writes address of PRD table to device's PRD Table Address
4064 * register, sets the DMA control register, and calls
4065 * ops->exec_command() to start the transfer.
4067 * May be used as the bmdma_setup() entry in ata_port_operations.
4070 * spin_lock_irqsave(host_set lock)
4072 void ata_bmdma_setup(struct ata_queued_cmd *qc)
4074 if (qc->ap->flags & ATA_FLAG_MMIO)
4075 ata_bmdma_setup_mmio(qc);
4077 ata_bmdma_setup_pio(qc);
4082 * ata_bmdma_irq_clear - Clear PCI IDE BMDMA interrupt.
4083 * @ap: Port associated with this ATA transaction.
4085 * Clear interrupt and error flags in DMA status register.
4087 * May be used as the irq_clear() entry in ata_port_operations.
4090 * spin_lock_irqsave(host_set lock)
4093 void ata_bmdma_irq_clear(struct ata_port *ap)
4095 if (ap->flags & ATA_FLAG_MMIO) {
4096 void __iomem *mmio = ((void __iomem *) ap->ioaddr.bmdma_addr) + ATA_DMA_STATUS;
4097 writeb(readb(mmio), mmio);
4099 unsigned long addr = ap->ioaddr.bmdma_addr + ATA_DMA_STATUS;
4100 outb(inb(addr), addr);
4107 * ata_bmdma_status - Read PCI IDE BMDMA status
4108 * @ap: Port associated with this ATA transaction.
4110 * Read and return BMDMA status register.
4112 * May be used as the bmdma_status() entry in ata_port_operations.
4115 * spin_lock_irqsave(host_set lock)
4118 u8 ata_bmdma_status(struct ata_port *ap)
4121 if (ap->flags & ATA_FLAG_MMIO) {
4122 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
4123 host_stat = readb(mmio + ATA_DMA_STATUS);
4125 host_stat = inb(ap->ioaddr.bmdma_addr + ATA_DMA_STATUS);
4131 * ata_bmdma_stop - Stop PCI IDE BMDMA transfer
4132 * @qc: Command we are ending DMA for
4134 * Clears the ATA_DMA_START flag in the dma control register
4136 * May be used as the bmdma_stop() entry in ata_port_operations.
4139 * spin_lock_irqsave(host_set lock)
4142 void ata_bmdma_stop(struct ata_queued_cmd *qc)
4144 struct ata_port *ap = qc->ap;
4145 if (ap->flags & ATA_FLAG_MMIO) {
4146 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
4148 /* clear start/stop bit */
4149 writeb(readb(mmio + ATA_DMA_CMD) & ~ATA_DMA_START,
4150 mmio + ATA_DMA_CMD);
4152 /* clear start/stop bit */
4153 outb(inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD) & ~ATA_DMA_START,
4154 ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
4157 /* one-PIO-cycle guaranteed wait, per spec, for HDMA1:0 transition */
4158 ata_altstatus(ap); /* dummy read */
4162 * ata_host_intr - Handle host interrupt for given (port, task)
4163 * @ap: Port on which interrupt arrived (possibly...)
4164 * @qc: Taskfile currently active in engine
4166 * Handle host interrupt for given queued command. Currently,
4167 * only DMA interrupts are handled. All other commands are
4168 * handled via polling with interrupts disabled (nIEN bit).
4171 * spin_lock_irqsave(host_set lock)
4174 * One if interrupt was handled, zero if not (shared irq).
4177 inline unsigned int ata_host_intr (struct ata_port *ap,
4178 struct ata_queued_cmd *qc)
4180 u8 status, host_stat = 0;
4182 VPRINTK("ata%u: protocol %d task_state %d\n",
4183 ap->id, qc->tf.protocol, ap->hsm_task_state);
4185 /* Check whether we are expecting interrupt in this state */
4186 switch (ap->hsm_task_state) {
4188 /* Check the ATA_DFLAG_CDB_INTR flag is enough here.
4189 * The flag was turned on only for atapi devices.
4190 * No need to check is_atapi_taskfile(&qc->tf) again.
4192 if (!(qc->dev->flags & ATA_DFLAG_CDB_INTR))
4196 if (qc->tf.protocol == ATA_PROT_DMA ||
4197 qc->tf.protocol == ATA_PROT_ATAPI_DMA) {
4198 /* check status of DMA engine */
4199 host_stat = ap->ops->bmdma_status(ap);
4200 VPRINTK("ata%u: host_stat 0x%X\n", ap->id, host_stat);
4202 /* if it's not our irq... */
4203 if (!(host_stat & ATA_DMA_INTR))
4206 /* before we do anything else, clear DMA-Start bit */
4207 ap->ops->bmdma_stop(qc);
4216 /* check altstatus */
4217 status = ata_altstatus(ap);
4218 if (status & ATA_BUSY)
4221 /* check main status, clearing INTRQ */
4222 status = ata_chk_status(ap);
4223 if (unlikely(status & ATA_BUSY))
4226 DPRINTK("ata%u: protocol %d task_state %d (dev_stat 0x%X)\n",
4227 ap->id, qc->tf.protocol, ap->hsm_task_state, status);
4229 /* ack bmdma irq events */
4230 ap->ops->irq_clear(ap);
4233 if (unlikely((status & ATA_ERR) || (host_stat & ATA_DMA_ERR)))
4234 ap->hsm_task_state = HSM_ST_ERR;
4237 switch (ap->hsm_task_state) {
4239 /* Some pre-ATAPI-4 devices assert INTRQ
4240 * at this state when ready to receive CDB.
4243 /* check device status */
4244 if (unlikely((status & (ATA_BUSY | ATA_DRQ)) != ATA_DRQ)) {
4245 /* Wrong status. Let EH handle this */
4246 ap->hsm_task_state = HSM_ST_ERR;
4250 atapi_send_cdb(ap, qc);
4255 /* complete command or read/write the data register */
4256 if (qc->tf.protocol == ATA_PROT_ATAPI) {
4257 /* ATAPI PIO protocol */
4258 if ((status & ATA_DRQ) == 0) {
4259 /* no more data to transfer */
4260 ap->hsm_task_state = HSM_ST_LAST;
4264 atapi_pio_bytes(qc);
4266 if (unlikely(ap->hsm_task_state == HSM_ST_ERR))
4267 /* bad ireason reported by device */
4271 /* ATA PIO protocol */
4272 if (unlikely((status & ATA_DRQ) == 0)) {
4273 /* handle BSY=0, DRQ=0 as error */
4274 ap->hsm_task_state = HSM_ST_ERR;
4278 ata_pio_sectors(qc);
4280 if (ap->hsm_task_state == HSM_ST_LAST &&
4281 (!(qc->tf.flags & ATA_TFLAG_WRITE))) {
4284 status = ata_chk_status(ap);
4289 ata_altstatus(ap); /* flush */
4293 if (unlikely(status & ATA_DRQ)) {
4294 /* handle DRQ=1 as error */
4295 ap->hsm_task_state = HSM_ST_ERR;
4299 /* no more data to transfer */
4300 DPRINTK("ata%u: command complete, drv_stat 0x%x\n",
4303 ap->hsm_task_state = HSM_ST_IDLE;
4305 /* complete taskfile transaction */
4306 ata_qc_complete(qc, ac_err_mask(status));
4310 printk(KERN_ERR "ata%u: command error, drv_stat 0x%x host_stat 0x%x\n",
4311 ap->id, status, host_stat);
4313 ap->hsm_task_state = HSM_ST_IDLE;
4314 ata_qc_complete(qc, status | ATA_ERR);
4320 return 1; /* irq handled */
4323 ap->stats.idle_irq++;
4326 if ((ap->stats.idle_irq % 1000) == 0) {
4328 ata_irq_ack(ap, 0); /* debug trap */
4329 printk(KERN_WARNING "ata%d: irq trap\n", ap->id);
4332 return 0; /* irq not handled */
4336 * ata_interrupt - Default ATA host interrupt handler
4337 * @irq: irq line (unused)
4338 * @dev_instance: pointer to our ata_host_set information structure
4341 * Default interrupt handler for PCI IDE devices. Calls
4342 * ata_host_intr() for each port that is not disabled.
4345 * Obtains host_set lock during operation.
4348 * IRQ_NONE or IRQ_HANDLED.
4351 irqreturn_t ata_interrupt (int irq, void *dev_instance, struct pt_regs *regs)
4353 struct ata_host_set *host_set = dev_instance;
4355 unsigned int handled = 0;
4356 unsigned long flags;
4358 /* TODO: make _irqsave conditional on x86 PCI IDE legacy mode */
4359 spin_lock_irqsave(&host_set->lock, flags);
4361 for (i = 0; i < host_set->n_ports; i++) {
4362 struct ata_port *ap;
4364 ap = host_set->ports[i];
4366 !(ap->flags & ATA_FLAG_PORT_DISABLED)) {
4367 struct ata_queued_cmd *qc;
4369 qc = ata_qc_from_tag(ap, ap->active_tag);
4370 if (qc && (!(qc->tf.flags & ATA_TFLAG_POLLING)) &&
4371 (qc->flags & ATA_QCFLAG_ACTIVE))
4372 handled |= ata_host_intr(ap, qc);
4376 spin_unlock_irqrestore(&host_set->lock, flags);
4378 return IRQ_RETVAL(handled);
4382 * ata_port_start - Set port up for dma.
4383 * @ap: Port to initialize
4385 * Called just after data structures for each port are
4386 * initialized. Allocates space for PRD table.
4388 * May be used as the port_start() entry in ata_port_operations.
4391 * Inherited from caller.
4394 int ata_port_start (struct ata_port *ap)
4396 struct device *dev = ap->host_set->dev;
4399 ap->prd = dma_alloc_coherent(dev, ATA_PRD_TBL_SZ, &ap->prd_dma, GFP_KERNEL);
4403 rc = ata_pad_alloc(ap, dev);
4405 dma_free_coherent(dev, ATA_PRD_TBL_SZ, ap->prd, ap->prd_dma);
4409 DPRINTK("prd alloc, virt %p, dma %llx\n", ap->prd, (unsigned long long) ap->prd_dma);
4416 * ata_port_stop - Undo ata_port_start()
4417 * @ap: Port to shut down
4419 * Frees the PRD table.
4421 * May be used as the port_stop() entry in ata_port_operations.
4424 * Inherited from caller.
4427 void ata_port_stop (struct ata_port *ap)
4429 struct device *dev = ap->host_set->dev;
4431 dma_free_coherent(dev, ATA_PRD_TBL_SZ, ap->prd, ap->prd_dma);
4432 ata_pad_free(ap, dev);
4435 void ata_host_stop (struct ata_host_set *host_set)
4437 if (host_set->mmio_base)
4438 iounmap(host_set->mmio_base);
4443 * ata_host_remove - Unregister SCSI host structure with upper layers
4444 * @ap: Port to unregister
4445 * @do_unregister: 1 if we fully unregister, 0 to just stop the port
4448 * Inherited from caller.
4451 static void ata_host_remove(struct ata_port *ap, unsigned int do_unregister)
4453 struct Scsi_Host *sh = ap->host;
4458 scsi_remove_host(sh);
4460 ap->ops->port_stop(ap);
4464 * ata_host_init - Initialize an ata_port structure
4465 * @ap: Structure to initialize
4466 * @host: associated SCSI mid-layer structure
4467 * @host_set: Collection of hosts to which @ap belongs
4468 * @ent: Probe information provided by low-level driver
4469 * @port_no: Port number associated with this ata_port
4471 * Initialize a new ata_port structure, and its associated
4475 * Inherited from caller.
4478 static void ata_host_init(struct ata_port *ap, struct Scsi_Host *host,
4479 struct ata_host_set *host_set,
4480 const struct ata_probe_ent *ent, unsigned int port_no)
4486 host->max_channel = 1;
4487 host->unique_id = ata_unique_id++;
4488 host->max_cmd_len = 12;
4490 ap->flags = ATA_FLAG_PORT_DISABLED;
4491 ap->id = host->unique_id;
4493 ap->ctl = ATA_DEVCTL_OBS;
4494 ap->host_set = host_set;
4495 ap->port_no = port_no;
4497 ent->legacy_mode ? ent->hard_port_no : port_no;
4498 ap->pio_mask = ent->pio_mask;
4499 ap->mwdma_mask = ent->mwdma_mask;
4500 ap->udma_mask = ent->udma_mask;
4501 ap->flags |= ent->host_flags;
4502 ap->ops = ent->port_ops;
4503 ap->cbl = ATA_CBL_NONE;
4504 ap->active_tag = ATA_TAG_POISON;
4505 ap->last_ctl = 0xFF;
4507 INIT_WORK(&ap->pio_task, ata_pio_task, ap);
4509 for (i = 0; i < ATA_MAX_DEVICES; i++)
4510 ap->device[i].devno = i;
4513 ap->stats.unhandled_irq = 1;
4514 ap->stats.idle_irq = 1;
4517 memcpy(&ap->ioaddr, &ent->port[port_no], sizeof(struct ata_ioports));
4521 * ata_host_add - Attach low-level ATA driver to system
4522 * @ent: Information provided by low-level driver
4523 * @host_set: Collections of ports to which we add
4524 * @port_no: Port number associated with this host
4526 * Attach low-level ATA driver to system.
4529 * PCI/etc. bus probe sem.
4532 * New ata_port on success, for NULL on error.
4535 static struct ata_port * ata_host_add(const struct ata_probe_ent *ent,
4536 struct ata_host_set *host_set,
4537 unsigned int port_no)
4539 struct Scsi_Host *host;
4540 struct ata_port *ap;
4544 host = scsi_host_alloc(ent->sht, sizeof(struct ata_port));
4548 ap = (struct ata_port *) &host->hostdata[0];
4550 ata_host_init(ap, host, host_set, ent, port_no);
4552 rc = ap->ops->port_start(ap);
4559 scsi_host_put(host);
4564 * ata_device_add - Register hardware device with ATA and SCSI layers
4565 * @ent: Probe information describing hardware device to be registered
4567 * This function processes the information provided in the probe
4568 * information struct @ent, allocates the necessary ATA and SCSI
4569 * host information structures, initializes them, and registers
4570 * everything with requisite kernel subsystems.
4572 * This function requests irqs, probes the ATA bus, and probes
4576 * PCI/etc. bus probe sem.
4579 * Number of ports registered. Zero on error (no ports registered).
4582 int ata_device_add(const struct ata_probe_ent *ent)
4584 unsigned int count = 0, i;
4585 struct device *dev = ent->dev;
4586 struct ata_host_set *host_set;
4589 /* alloc a container for our list of ATA ports (buses) */
4590 host_set = kzalloc(sizeof(struct ata_host_set) +
4591 (ent->n_ports * sizeof(void *)), GFP_KERNEL);
4594 spin_lock_init(&host_set->lock);
4596 host_set->dev = dev;
4597 host_set->n_ports = ent->n_ports;
4598 host_set->irq = ent->irq;
4599 host_set->mmio_base = ent->mmio_base;
4600 host_set->private_data = ent->private_data;
4601 host_set->ops = ent->port_ops;
4603 /* register each port bound to this device */
4604 for (i = 0; i < ent->n_ports; i++) {
4605 struct ata_port *ap;
4606 unsigned long xfer_mode_mask;
4608 ap = ata_host_add(ent, host_set, i);
4612 host_set->ports[i] = ap;
4613 xfer_mode_mask =(ap->udma_mask << ATA_SHIFT_UDMA) |
4614 (ap->mwdma_mask << ATA_SHIFT_MWDMA) |
4615 (ap->pio_mask << ATA_SHIFT_PIO);
4617 /* print per-port info to dmesg */
4618 printk(KERN_INFO "ata%u: %cATA max %s cmd 0x%lX ctl 0x%lX "
4619 "bmdma 0x%lX irq %lu\n",
4621 ap->flags & ATA_FLAG_SATA ? 'S' : 'P',
4622 ata_mode_string(xfer_mode_mask),
4623 ap->ioaddr.cmd_addr,
4624 ap->ioaddr.ctl_addr,
4625 ap->ioaddr.bmdma_addr,
4629 host_set->ops->irq_clear(ap);
4636 /* obtain irq, that is shared between channels */
4637 if (request_irq(ent->irq, ent->port_ops->irq_handler, ent->irq_flags,
4638 DRV_NAME, host_set))
4641 /* perform each probe synchronously */
4642 DPRINTK("probe begin\n");
4643 for (i = 0; i < count; i++) {
4644 struct ata_port *ap;
4647 ap = host_set->ports[i];
4649 DPRINTK("ata%u: probe begin\n", ap->id);
4650 rc = ata_bus_probe(ap);
4651 DPRINTK("ata%u: probe end\n", ap->id);
4654 /* FIXME: do something useful here?
4655 * Current libata behavior will
4656 * tear down everything when
4657 * the module is removed
4658 * or the h/w is unplugged.
4662 rc = scsi_add_host(ap->host, dev);
4664 printk(KERN_ERR "ata%u: scsi_add_host failed\n",
4666 /* FIXME: do something useful here */
4667 /* FIXME: handle unconditional calls to
4668 * scsi_scan_host and ata_host_remove, below,
4674 /* probes are done, now scan each port's disk(s) */
4675 DPRINTK("probe begin\n");
4676 for (i = 0; i < count; i++) {
4677 struct ata_port *ap = host_set->ports[i];
4679 ata_scsi_scan_host(ap);
4682 dev_set_drvdata(dev, host_set);
4684 VPRINTK("EXIT, returning %u\n", ent->n_ports);
4685 return ent->n_ports; /* success */
4688 for (i = 0; i < count; i++) {
4689 ata_host_remove(host_set->ports[i], 1);
4690 scsi_host_put(host_set->ports[i]->host);
4694 VPRINTK("EXIT, returning 0\n");
4699 * ata_host_set_remove - PCI layer callback for device removal
4700 * @host_set: ATA host set that was removed
4702 * Unregister all objects associated with this host set. Free those
4706 * Inherited from calling layer (may sleep).
4709 void ata_host_set_remove(struct ata_host_set *host_set)
4711 struct ata_port *ap;
4714 for (i = 0; i < host_set->n_ports; i++) {
4715 ap = host_set->ports[i];
4716 scsi_remove_host(ap->host);
4719 free_irq(host_set->irq, host_set);
4721 for (i = 0; i < host_set->n_ports; i++) {
4722 ap = host_set->ports[i];
4724 ata_scsi_release(ap->host);
4726 if ((ap->flags & ATA_FLAG_NO_LEGACY) == 0) {
4727 struct ata_ioports *ioaddr = &ap->ioaddr;
4729 if (ioaddr->cmd_addr == 0x1f0)
4730 release_region(0x1f0, 8);
4731 else if (ioaddr->cmd_addr == 0x170)
4732 release_region(0x170, 8);
4735 scsi_host_put(ap->host);
4738 if (host_set->ops->host_stop)
4739 host_set->ops->host_stop(host_set);
4745 * ata_scsi_release - SCSI layer callback hook for host unload
4746 * @host: libata host to be unloaded
4748 * Performs all duties necessary to shut down a libata port...
4749 * Kill port kthread, disable port, and release resources.
4752 * Inherited from SCSI layer.
4758 int ata_scsi_release(struct Scsi_Host *host)
4760 struct ata_port *ap = (struct ata_port *) &host->hostdata[0];
4764 ap->ops->port_disable(ap);
4765 ata_host_remove(ap, 0);
4772 * ata_std_ports - initialize ioaddr with standard port offsets.
4773 * @ioaddr: IO address structure to be initialized
4775 * Utility function which initializes data_addr, error_addr,
4776 * feature_addr, nsect_addr, lbal_addr, lbam_addr, lbah_addr,
4777 * device_addr, status_addr, and command_addr to standard offsets
4778 * relative to cmd_addr.
4780 * Does not set ctl_addr, altstatus_addr, bmdma_addr, or scr_addr.
4783 void ata_std_ports(struct ata_ioports *ioaddr)
4785 ioaddr->data_addr = ioaddr->cmd_addr + ATA_REG_DATA;
4786 ioaddr->error_addr = ioaddr->cmd_addr + ATA_REG_ERR;
4787 ioaddr->feature_addr = ioaddr->cmd_addr + ATA_REG_FEATURE;
4788 ioaddr->nsect_addr = ioaddr->cmd_addr + ATA_REG_NSECT;
4789 ioaddr->lbal_addr = ioaddr->cmd_addr + ATA_REG_LBAL;
4790 ioaddr->lbam_addr = ioaddr->cmd_addr + ATA_REG_LBAM;
4791 ioaddr->lbah_addr = ioaddr->cmd_addr + ATA_REG_LBAH;
4792 ioaddr->device_addr = ioaddr->cmd_addr + ATA_REG_DEVICE;
4793 ioaddr->status_addr = ioaddr->cmd_addr + ATA_REG_STATUS;
4794 ioaddr->command_addr = ioaddr->cmd_addr + ATA_REG_CMD;
4797 static struct ata_probe_ent *
4798 ata_probe_ent_alloc(struct device *dev, const struct ata_port_info *port)
4800 struct ata_probe_ent *probe_ent;
4802 probe_ent = kzalloc(sizeof(*probe_ent), GFP_KERNEL);
4804 printk(KERN_ERR DRV_NAME "(%s): out of memory\n",
4805 kobject_name(&(dev->kobj)));
4809 INIT_LIST_HEAD(&probe_ent->node);
4810 probe_ent->dev = dev;
4812 probe_ent->sht = port->sht;
4813 probe_ent->host_flags = port->host_flags;
4814 probe_ent->pio_mask = port->pio_mask;
4815 probe_ent->mwdma_mask = port->mwdma_mask;
4816 probe_ent->udma_mask = port->udma_mask;
4817 probe_ent->port_ops = port->port_ops;
4826 void ata_pci_host_stop (struct ata_host_set *host_set)
4828 struct pci_dev *pdev = to_pci_dev(host_set->dev);
4830 pci_iounmap(pdev, host_set->mmio_base);
4834 * ata_pci_init_native_mode - Initialize native-mode driver
4835 * @pdev: pci device to be initialized
4836 * @port: array[2] of pointers to port info structures.
4837 * @ports: bitmap of ports present
4839 * Utility function which allocates and initializes an
4840 * ata_probe_ent structure for a standard dual-port
4841 * PIO-based IDE controller. The returned ata_probe_ent
4842 * structure can be passed to ata_device_add(). The returned
4843 * ata_probe_ent structure should then be freed with kfree().
4845 * The caller need only pass the address of the primary port, the
4846 * secondary will be deduced automatically. If the device has non
4847 * standard secondary port mappings this function can be called twice,
4848 * once for each interface.
4851 struct ata_probe_ent *
4852 ata_pci_init_native_mode(struct pci_dev *pdev, struct ata_port_info **port, int ports)
4854 struct ata_probe_ent *probe_ent =
4855 ata_probe_ent_alloc(pci_dev_to_dev(pdev), port[0]);
4861 probe_ent->irq = pdev->irq;
4862 probe_ent->irq_flags = SA_SHIRQ;
4863 probe_ent->private_data = port[0]->private_data;
4865 if (ports & ATA_PORT_PRIMARY) {
4866 probe_ent->port[p].cmd_addr = pci_resource_start(pdev, 0);
4867 probe_ent->port[p].altstatus_addr =
4868 probe_ent->port[p].ctl_addr =
4869 pci_resource_start(pdev, 1) | ATA_PCI_CTL_OFS;
4870 probe_ent->port[p].bmdma_addr = pci_resource_start(pdev, 4);
4871 ata_std_ports(&probe_ent->port[p]);
4875 if (ports & ATA_PORT_SECONDARY) {
4876 probe_ent->port[p].cmd_addr = pci_resource_start(pdev, 2);
4877 probe_ent->port[p].altstatus_addr =
4878 probe_ent->port[p].ctl_addr =
4879 pci_resource_start(pdev, 3) | ATA_PCI_CTL_OFS;
4880 probe_ent->port[p].bmdma_addr = pci_resource_start(pdev, 4) + 8;
4881 ata_std_ports(&probe_ent->port[p]);
4885 probe_ent->n_ports = p;
4889 static struct ata_probe_ent *ata_pci_init_legacy_port(struct pci_dev *pdev, struct ata_port_info *port, int port_num)
4891 struct ata_probe_ent *probe_ent;
4893 probe_ent = ata_probe_ent_alloc(pci_dev_to_dev(pdev), port);
4897 probe_ent->legacy_mode = 1;
4898 probe_ent->n_ports = 1;
4899 probe_ent->hard_port_no = port_num;
4900 probe_ent->private_data = port->private_data;
4905 probe_ent->irq = 14;
4906 probe_ent->port[0].cmd_addr = 0x1f0;
4907 probe_ent->port[0].altstatus_addr =
4908 probe_ent->port[0].ctl_addr = 0x3f6;
4911 probe_ent->irq = 15;
4912 probe_ent->port[0].cmd_addr = 0x170;
4913 probe_ent->port[0].altstatus_addr =
4914 probe_ent->port[0].ctl_addr = 0x376;
4917 probe_ent->port[0].bmdma_addr = pci_resource_start(pdev, 4) + 8 * port_num;
4918 ata_std_ports(&probe_ent->port[0]);
4923 * ata_pci_init_one - Initialize/register PCI IDE host controller
4924 * @pdev: Controller to be initialized
4925 * @port_info: Information from low-level host driver
4926 * @n_ports: Number of ports attached to host controller
4928 * This is a helper function which can be called from a driver's
4929 * xxx_init_one() probe function if the hardware uses traditional
4930 * IDE taskfile registers.
4932 * This function calls pci_enable_device(), reserves its register
4933 * regions, sets the dma mask, enables bus master mode, and calls
4937 * Inherited from PCI layer (may sleep).
4940 * Zero on success, negative on errno-based value on error.
4943 int ata_pci_init_one (struct pci_dev *pdev, struct ata_port_info **port_info,
4944 unsigned int n_ports)
4946 struct ata_probe_ent *probe_ent = NULL, *probe_ent2 = NULL;
4947 struct ata_port_info *port[2];
4949 unsigned int legacy_mode = 0;
4950 int disable_dev_on_err = 1;
4955 port[0] = port_info[0];
4957 port[1] = port_info[1];
4961 if ((port[0]->host_flags & ATA_FLAG_NO_LEGACY) == 0
4962 && (pdev->class >> 8) == PCI_CLASS_STORAGE_IDE) {
4963 /* TODO: What if one channel is in native mode ... */
4964 pci_read_config_byte(pdev, PCI_CLASS_PROG, &tmp8);
4965 mask = (1 << 2) | (1 << 0);
4966 if ((tmp8 & mask) != mask)
4967 legacy_mode = (1 << 3);
4971 if ((!legacy_mode) && (n_ports > 2)) {
4972 printk(KERN_ERR "ata: BUG: native mode, n_ports > 2\n");
4977 /* FIXME: Really for ATA it isn't safe because the device may be
4978 multi-purpose and we want to leave it alone if it was already
4979 enabled. Secondly for shared use as Arjan says we want refcounting
4981 Checking dev->is_enabled is insufficient as this is not set at
4982 boot for the primary video which is BIOS enabled
4985 rc = pci_enable_device(pdev);
4989 rc = pci_request_regions(pdev, DRV_NAME);
4991 disable_dev_on_err = 0;
4995 /* FIXME: Should use platform specific mappers for legacy port ranges */
4997 if (!request_region(0x1f0, 8, "libata")) {
4998 struct resource *conflict, res;
5000 res.end = 0x1f0 + 8 - 1;
5001 conflict = ____request_resource(&ioport_resource, &res);
5002 if (!strcmp(conflict->name, "libata"))
5003 legacy_mode |= (1 << 0);
5005 disable_dev_on_err = 0;
5006 printk(KERN_WARNING "ata: 0x1f0 IDE port busy\n");
5009 legacy_mode |= (1 << 0);
5011 if (!request_region(0x170, 8, "libata")) {
5012 struct resource *conflict, res;
5014 res.end = 0x170 + 8 - 1;
5015 conflict = ____request_resource(&ioport_resource, &res);
5016 if (!strcmp(conflict->name, "libata"))
5017 legacy_mode |= (1 << 1);
5019 disable_dev_on_err = 0;
5020 printk(KERN_WARNING "ata: 0x170 IDE port busy\n");
5023 legacy_mode |= (1 << 1);
5026 /* we have legacy mode, but all ports are unavailable */
5027 if (legacy_mode == (1 << 3)) {
5029 goto err_out_regions;
5032 rc = pci_set_dma_mask(pdev, ATA_DMA_MASK);
5034 goto err_out_regions;
5035 rc = pci_set_consistent_dma_mask(pdev, ATA_DMA_MASK);
5037 goto err_out_regions;
5040 if (legacy_mode & (1 << 0))
5041 probe_ent = ata_pci_init_legacy_port(pdev, port[0], 0);
5042 if (legacy_mode & (1 << 1))
5043 probe_ent2 = ata_pci_init_legacy_port(pdev, port[1], 1);
5046 probe_ent = ata_pci_init_native_mode(pdev, port, ATA_PORT_PRIMARY | ATA_PORT_SECONDARY);
5048 probe_ent = ata_pci_init_native_mode(pdev, port, ATA_PORT_PRIMARY);
5050 if (!probe_ent && !probe_ent2) {
5052 goto err_out_regions;
5055 pci_set_master(pdev);
5057 /* FIXME: check ata_device_add return */
5059 if (legacy_mode & (1 << 0))
5060 ata_device_add(probe_ent);
5061 if (legacy_mode & (1 << 1))
5062 ata_device_add(probe_ent2);
5064 ata_device_add(probe_ent);
5072 if (legacy_mode & (1 << 0))
5073 release_region(0x1f0, 8);
5074 if (legacy_mode & (1 << 1))
5075 release_region(0x170, 8);
5076 pci_release_regions(pdev);
5078 if (disable_dev_on_err)
5079 pci_disable_device(pdev);
5084 * ata_pci_remove_one - PCI layer callback for device removal
5085 * @pdev: PCI device that was removed
5087 * PCI layer indicates to libata via this hook that
5088 * hot-unplug or module unload event has occurred.
5089 * Handle this by unregistering all objects associated
5090 * with this PCI device. Free those objects. Then finally
5091 * release PCI resources and disable device.
5094 * Inherited from PCI layer (may sleep).
5097 void ata_pci_remove_one (struct pci_dev *pdev)
5099 struct device *dev = pci_dev_to_dev(pdev);
5100 struct ata_host_set *host_set = dev_get_drvdata(dev);
5102 ata_host_set_remove(host_set);
5103 pci_release_regions(pdev);
5104 pci_disable_device(pdev);
5105 dev_set_drvdata(dev, NULL);
5108 /* move to PCI subsystem */
5109 int pci_test_config_bits(struct pci_dev *pdev, const struct pci_bits *bits)
5111 unsigned long tmp = 0;
5113 switch (bits->width) {
5116 pci_read_config_byte(pdev, bits->reg, &tmp8);
5122 pci_read_config_word(pdev, bits->reg, &tmp16);
5128 pci_read_config_dword(pdev, bits->reg, &tmp32);
5139 return (tmp == bits->val) ? 1 : 0;
5141 #endif /* CONFIG_PCI */
5144 static int __init ata_init(void)
5146 ata_wq = create_workqueue("ata");
5150 printk(KERN_DEBUG "libata version " DRV_VERSION " loaded.\n");
5154 static void __exit ata_exit(void)
5156 destroy_workqueue(ata_wq);
5159 module_init(ata_init);
5160 module_exit(ata_exit);
5162 static unsigned long ratelimit_time;
5163 static spinlock_t ata_ratelimit_lock = SPIN_LOCK_UNLOCKED;
5165 int ata_ratelimit(void)
5168 unsigned long flags;
5170 spin_lock_irqsave(&ata_ratelimit_lock, flags);
5172 if (time_after(jiffies, ratelimit_time)) {
5174 ratelimit_time = jiffies + (HZ/5);
5178 spin_unlock_irqrestore(&ata_ratelimit_lock, flags);
5184 * libata is essentially a library of internal helper functions for
5185 * low-level ATA host controller drivers. As such, the API/ABI is
5186 * likely to change as new drivers are added and updated.
5187 * Do not depend on ABI/API stability.
5190 EXPORT_SYMBOL_GPL(ata_std_bios_param);
5191 EXPORT_SYMBOL_GPL(ata_std_ports);
5192 EXPORT_SYMBOL_GPL(ata_device_add);
5193 EXPORT_SYMBOL_GPL(ata_host_set_remove);
5194 EXPORT_SYMBOL_GPL(ata_sg_init);
5195 EXPORT_SYMBOL_GPL(ata_sg_init_one);
5196 EXPORT_SYMBOL_GPL(ata_qc_complete);
5197 EXPORT_SYMBOL_GPL(ata_qc_issue_prot);
5198 EXPORT_SYMBOL_GPL(ata_eng_timeout);
5199 EXPORT_SYMBOL_GPL(ata_tf_load);
5200 EXPORT_SYMBOL_GPL(ata_tf_read);
5201 EXPORT_SYMBOL_GPL(ata_noop_dev_select);
5202 EXPORT_SYMBOL_GPL(ata_std_dev_select);
5203 EXPORT_SYMBOL_GPL(ata_tf_to_fis);
5204 EXPORT_SYMBOL_GPL(ata_tf_from_fis);
5205 EXPORT_SYMBOL_GPL(ata_check_status);
5206 EXPORT_SYMBOL_GPL(ata_altstatus);
5207 EXPORT_SYMBOL_GPL(ata_exec_command);
5208 EXPORT_SYMBOL_GPL(ata_port_start);
5209 EXPORT_SYMBOL_GPL(ata_port_stop);
5210 EXPORT_SYMBOL_GPL(ata_host_stop);
5211 EXPORT_SYMBOL_GPL(ata_interrupt);
5212 EXPORT_SYMBOL_GPL(ata_qc_prep);
5213 EXPORT_SYMBOL_GPL(ata_bmdma_setup);
5214 EXPORT_SYMBOL_GPL(ata_bmdma_start);
5215 EXPORT_SYMBOL_GPL(ata_bmdma_irq_clear);
5216 EXPORT_SYMBOL_GPL(ata_bmdma_status);
5217 EXPORT_SYMBOL_GPL(ata_bmdma_stop);
5218 EXPORT_SYMBOL_GPL(ata_port_probe);
5219 EXPORT_SYMBOL_GPL(sata_phy_reset);
5220 EXPORT_SYMBOL_GPL(__sata_phy_reset);
5221 EXPORT_SYMBOL_GPL(ata_bus_reset);
5222 EXPORT_SYMBOL_GPL(ata_port_disable);
5223 EXPORT_SYMBOL_GPL(ata_ratelimit);
5224 EXPORT_SYMBOL_GPL(ata_scsi_ioctl);
5225 EXPORT_SYMBOL_GPL(ata_scsi_queuecmd);
5226 EXPORT_SYMBOL_GPL(ata_scsi_error);
5227 EXPORT_SYMBOL_GPL(ata_scsi_slave_config);
5228 EXPORT_SYMBOL_GPL(ata_scsi_release);
5229 EXPORT_SYMBOL_GPL(ata_host_intr);
5230 EXPORT_SYMBOL_GPL(ata_dev_classify);
5231 EXPORT_SYMBOL_GPL(ata_dev_id_string);
5232 EXPORT_SYMBOL_GPL(ata_dev_config);
5233 EXPORT_SYMBOL_GPL(ata_scsi_simulate);
5235 EXPORT_SYMBOL_GPL(ata_timing_compute);
5236 EXPORT_SYMBOL_GPL(ata_timing_merge);
5239 EXPORT_SYMBOL_GPL(pci_test_config_bits);
5240 EXPORT_SYMBOL_GPL(ata_pci_host_stop);
5241 EXPORT_SYMBOL_GPL(ata_pci_init_native_mode);
5242 EXPORT_SYMBOL_GPL(ata_pci_init_one);
5243 EXPORT_SYMBOL_GPL(ata_pci_remove_one);
5244 #endif /* CONFIG_PCI */