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 * const 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 struct ata_exec_internal_arg {
1051 unsigned int err_mask;
1052 struct ata_taskfile *tf;
1053 struct completion *waiting;
1056 int ata_qc_complete_internal(struct ata_queued_cmd *qc)
1058 struct ata_exec_internal_arg *arg = qc->private_data;
1059 struct completion *waiting = arg->waiting;
1061 if (!(qc->err_mask & ~AC_ERR_DEV))
1062 qc->ap->ops->tf_read(qc->ap, arg->tf);
1063 arg->err_mask = qc->err_mask;
1064 arg->waiting = NULL;
1071 * ata_exec_internal - execute libata internal command
1072 * @ap: Port to which the command is sent
1073 * @dev: Device to which the command is sent
1074 * @tf: Taskfile registers for the command and the result
1075 * @dma_dir: Data tranfer direction of the command
1076 * @buf: Data buffer of the command
1077 * @buflen: Length of data buffer
1079 * Executes libata internal command with timeout. @tf contains
1080 * command on entry and result on return. Timeout and error
1081 * conditions are reported via return value. No recovery action
1082 * is taken after a command times out. It's caller's duty to
1083 * clean up after timeout.
1086 * None. Should be called with kernel context, might sleep.
1090 ata_exec_internal(struct ata_port *ap, struct ata_device *dev,
1091 struct ata_taskfile *tf,
1092 int dma_dir, void *buf, unsigned int buflen)
1094 u8 command = tf->command;
1095 struct ata_queued_cmd *qc;
1096 DECLARE_COMPLETION(wait);
1097 unsigned long flags;
1098 struct ata_exec_internal_arg arg;
1100 spin_lock_irqsave(&ap->host_set->lock, flags);
1102 qc = ata_qc_new_init(ap, dev);
1106 qc->dma_dir = dma_dir;
1107 if (dma_dir != DMA_NONE) {
1108 ata_sg_init_one(qc, buf, buflen);
1109 qc->nsect = buflen / ATA_SECT_SIZE;
1112 arg.waiting = &wait;
1114 qc->private_data = &arg;
1115 qc->complete_fn = ata_qc_complete_internal;
1117 if (ata_qc_issue(qc))
1120 spin_unlock_irqrestore(&ap->host_set->lock, flags);
1122 if (!wait_for_completion_timeout(&wait, ATA_TMOUT_INTERNAL)) {
1123 spin_lock_irqsave(&ap->host_set->lock, flags);
1125 /* We're racing with irq here. If we lose, the
1126 * following test prevents us from completing the qc
1127 * again. If completion irq occurs after here but
1128 * before the caller cleans up, it will result in a
1129 * spurious interrupt. We can live with that.
1132 qc->err_mask = AC_ERR_OTHER;
1133 ata_qc_complete(qc);
1134 printk(KERN_WARNING "ata%u: qc timeout (cmd 0x%x)\n",
1138 spin_unlock_irqrestore(&ap->host_set->lock, flags);
1141 return arg.err_mask;
1145 spin_unlock_irqrestore(&ap->host_set->lock, flags);
1146 return AC_ERR_OTHER;
1150 * ata_dev_identify - obtain IDENTIFY x DEVICE page
1151 * @ap: port on which device we wish to probe resides
1152 * @device: device bus address, starting at zero
1154 * Following bus reset, we issue the IDENTIFY [PACKET] DEVICE
1155 * command, and read back the 512-byte device information page.
1156 * The device information page is fed to us via the standard
1157 * PIO-IN protocol, but we hand-code it here. (TODO: investigate
1158 * using standard PIO-IN paths)
1160 * After reading the device information page, we use several
1161 * bits of information from it to initialize data structures
1162 * that will be used during the lifetime of the ata_device.
1163 * Other data from the info page is used to disqualify certain
1164 * older ATA devices we do not wish to support.
1167 * Inherited from caller. Some functions called by this function
1168 * obtain the host_set lock.
1171 static void ata_dev_identify(struct ata_port *ap, unsigned int device)
1173 struct ata_device *dev = &ap->device[device];
1174 unsigned int major_version;
1176 unsigned long xfer_modes;
1177 unsigned int using_edd;
1178 struct ata_taskfile tf;
1179 unsigned int err_mask;
1182 if (!ata_dev_present(dev)) {
1183 DPRINTK("ENTER/EXIT (host %u, dev %u) -- nodev\n",
1188 if (ap->flags & (ATA_FLAG_SRST | ATA_FLAG_SATA_RESET))
1193 DPRINTK("ENTER, host %u, dev %u\n", ap->id, device);
1195 assert (dev->class == ATA_DEV_ATA || dev->class == ATA_DEV_ATAPI ||
1196 dev->class == ATA_DEV_NONE);
1198 ata_dev_select(ap, device, 1, 1); /* select device 0/1 */
1201 ata_tf_init(ap, &tf, device);
1203 if (dev->class == ATA_DEV_ATA) {
1204 tf.command = ATA_CMD_ID_ATA;
1205 DPRINTK("do ATA identify\n");
1207 tf.command = ATA_CMD_ID_ATAPI;
1208 DPRINTK("do ATAPI identify\n");
1211 tf.protocol = ATA_PROT_PIO;
1213 err_mask = ata_exec_internal(ap, dev, &tf, DMA_FROM_DEVICE,
1214 dev->id, sizeof(dev->id));
1217 if (err_mask & ~AC_ERR_DEV)
1221 * arg! EDD works for all test cases, but seems to return
1222 * the ATA signature for some ATAPI devices. Until the
1223 * reason for this is found and fixed, we fix up the mess
1224 * here. If IDENTIFY DEVICE returns command aborted
1225 * (as ATAPI devices do), then we issue an
1226 * IDENTIFY PACKET DEVICE.
1228 * ATA software reset (SRST, the default) does not appear
1229 * to have this problem.
1231 if ((using_edd) && (dev->class == ATA_DEV_ATA)) {
1232 u8 err = tf.feature;
1233 if (err & ATA_ABORTED) {
1234 dev->class = ATA_DEV_ATAPI;
1241 swap_buf_le16(dev->id, ATA_ID_WORDS);
1243 /* print device capabilities */
1244 printk(KERN_DEBUG "ata%u: dev %u cfg "
1245 "49:%04x 82:%04x 83:%04x 84:%04x 85:%04x 86:%04x 87:%04x 88:%04x\n",
1246 ap->id, device, dev->id[49],
1247 dev->id[82], dev->id[83], dev->id[84],
1248 dev->id[85], dev->id[86], dev->id[87],
1252 * common ATA, ATAPI feature tests
1255 /* we require DMA support (bits 8 of word 49) */
1256 if (!ata_id_has_dma(dev->id)) {
1257 printk(KERN_DEBUG "ata%u: no dma\n", ap->id);
1261 /* quick-n-dirty find max transfer mode; for printk only */
1262 xfer_modes = dev->id[ATA_ID_UDMA_MODES];
1264 xfer_modes = (dev->id[ATA_ID_MWDMA_MODES]) << ATA_SHIFT_MWDMA;
1266 xfer_modes = ata_pio_modes(dev);
1270 /* ATA-specific feature tests */
1271 if (dev->class == ATA_DEV_ATA) {
1272 if (!ata_id_is_ata(dev->id)) /* sanity check */
1275 /* get major version */
1276 tmp = dev->id[ATA_ID_MAJOR_VER];
1277 for (major_version = 14; major_version >= 1; major_version--)
1278 if (tmp & (1 << major_version))
1282 * The exact sequence expected by certain pre-ATA4 drives is:
1285 * INITIALIZE DEVICE PARAMETERS
1287 * Some drives were very specific about that exact sequence.
1289 if (major_version < 4 || (!ata_id_has_lba(dev->id))) {
1290 ata_dev_init_params(ap, dev);
1292 /* current CHS translation info (id[53-58]) might be
1293 * changed. reread the identify device info.
1295 ata_dev_reread_id(ap, dev);
1298 if (ata_id_has_lba(dev->id)) {
1299 dev->flags |= ATA_DFLAG_LBA;
1301 if (ata_id_has_lba48(dev->id)) {
1302 dev->flags |= ATA_DFLAG_LBA48;
1303 dev->n_sectors = ata_id_u64(dev->id, 100);
1305 dev->n_sectors = ata_id_u32(dev->id, 60);
1308 /* print device info to dmesg */
1309 printk(KERN_INFO "ata%u: dev %u ATA-%d, max %s, %Lu sectors:%s\n",
1312 ata_mode_string(xfer_modes),
1313 (unsigned long long)dev->n_sectors,
1314 dev->flags & ATA_DFLAG_LBA48 ? " LBA48" : " LBA");
1318 /* Default translation */
1319 dev->cylinders = dev->id[1];
1320 dev->heads = dev->id[3];
1321 dev->sectors = dev->id[6];
1322 dev->n_sectors = dev->cylinders * dev->heads * dev->sectors;
1324 if (ata_id_current_chs_valid(dev->id)) {
1325 /* Current CHS translation is valid. */
1326 dev->cylinders = dev->id[54];
1327 dev->heads = dev->id[55];
1328 dev->sectors = dev->id[56];
1330 dev->n_sectors = ata_id_u32(dev->id, 57);
1333 /* print device info to dmesg */
1334 printk(KERN_INFO "ata%u: dev %u ATA-%d, max %s, %Lu sectors: CHS %d/%d/%d\n",
1337 ata_mode_string(xfer_modes),
1338 (unsigned long long)dev->n_sectors,
1339 (int)dev->cylinders, (int)dev->heads, (int)dev->sectors);
1343 if (dev->id[59] & 0x100) {
1344 dev->multi_count = dev->id[59] & 0xff;
1345 DPRINTK("ata%u: dev %u multi count %u\n",
1346 ap->id, device, dev->multi_count);
1349 ap->host->max_cmd_len = 16;
1352 /* ATAPI-specific feature tests */
1353 else if (dev->class == ATA_DEV_ATAPI) {
1354 if (ata_id_is_ata(dev->id)) /* sanity check */
1357 rc = atapi_cdb_len(dev->id);
1358 if ((rc < 12) || (rc > ATAPI_CDB_LEN)) {
1359 printk(KERN_WARNING "ata%u: unsupported CDB len\n", ap->id);
1362 ap->cdb_len = (unsigned int) rc;
1363 ap->host->max_cmd_len = (unsigned char) ap->cdb_len;
1365 if (ata_id_cdb_intr(dev->id))
1366 dev->flags |= ATA_DFLAG_CDB_INTR;
1368 /* print device info to dmesg */
1369 printk(KERN_INFO "ata%u: dev %u ATAPI, max %s\n",
1371 ata_mode_string(xfer_modes));
1374 DPRINTK("EXIT, drv_stat = 0x%x\n", ata_chk_status(ap));
1378 printk(KERN_WARNING "ata%u: dev %u not supported, ignoring\n",
1381 dev->class++; /* converts ATA_DEV_xxx into ATA_DEV_xxx_UNSUP */
1382 DPRINTK("EXIT, err\n");
1386 static inline u8 ata_dev_knobble(const struct ata_port *ap)
1388 return ((ap->cbl == ATA_CBL_SATA) && (!ata_id_is_sata(ap->device->id)));
1392 * ata_dev_config - Run device specific handlers and check for
1393 * SATA->PATA bridges
1400 void ata_dev_config(struct ata_port *ap, unsigned int i)
1402 /* limit bridge transfers to udma5, 200 sectors */
1403 if (ata_dev_knobble(ap)) {
1404 printk(KERN_INFO "ata%u(%u): applying bridge limits\n",
1405 ap->id, ap->device->devno);
1406 ap->udma_mask &= ATA_UDMA5;
1407 ap->host->max_sectors = ATA_MAX_SECTORS;
1408 ap->host->hostt->max_sectors = ATA_MAX_SECTORS;
1409 ap->device->flags |= ATA_DFLAG_LOCK_SECTORS;
1412 if (ap->ops->dev_config)
1413 ap->ops->dev_config(ap, &ap->device[i]);
1417 * ata_bus_probe - Reset and probe ATA bus
1420 * Master ATA bus probing function. Initiates a hardware-dependent
1421 * bus reset, then attempts to identify any devices found on
1425 * PCI/etc. bus probe sem.
1428 * Zero on success, non-zero on error.
1431 static int ata_bus_probe(struct ata_port *ap)
1433 unsigned int i, found = 0;
1435 ap->ops->phy_reset(ap);
1436 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1439 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1440 ata_dev_identify(ap, i);
1441 if (ata_dev_present(&ap->device[i])) {
1443 ata_dev_config(ap,i);
1447 if ((!found) || (ap->flags & ATA_FLAG_PORT_DISABLED))
1448 goto err_out_disable;
1451 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1452 goto err_out_disable;
1457 ap->ops->port_disable(ap);
1463 * ata_port_probe - Mark port as enabled
1464 * @ap: Port for which we indicate enablement
1466 * Modify @ap data structure such that the system
1467 * thinks that the entire port is enabled.
1469 * LOCKING: host_set lock, or some other form of
1473 void ata_port_probe(struct ata_port *ap)
1475 ap->flags &= ~ATA_FLAG_PORT_DISABLED;
1479 * __sata_phy_reset - Wake/reset a low-level SATA PHY
1480 * @ap: SATA port associated with target SATA PHY.
1482 * This function issues commands to standard SATA Sxxx
1483 * PHY registers, to wake up the phy (and device), and
1484 * clear any reset condition.
1487 * PCI/etc. bus probe sem.
1490 void __sata_phy_reset(struct ata_port *ap)
1493 unsigned long timeout = jiffies + (HZ * 5);
1495 if (ap->flags & ATA_FLAG_SATA_RESET) {
1496 /* issue phy wake/reset */
1497 scr_write_flush(ap, SCR_CONTROL, 0x301);
1498 /* Couldn't find anything in SATA I/II specs, but
1499 * AHCI-1.1 10.4.2 says at least 1 ms. */
1502 scr_write_flush(ap, SCR_CONTROL, 0x300); /* phy wake/clear reset */
1504 /* wait for phy to become ready, if necessary */
1507 sstatus = scr_read(ap, SCR_STATUS);
1508 if ((sstatus & 0xf) != 1)
1510 } while (time_before(jiffies, timeout));
1512 /* TODO: phy layer with polling, timeouts, etc. */
1513 sstatus = scr_read(ap, SCR_STATUS);
1514 if (sata_dev_present(ap)) {
1518 tmp = (sstatus >> 4) & 0xf;
1521 else if (tmp & (1 << 1))
1524 speed = "<unknown>";
1525 printk(KERN_INFO "ata%u: SATA link up %s Gbps (SStatus %X)\n",
1526 ap->id, speed, sstatus);
1529 printk(KERN_INFO "ata%u: SATA link down (SStatus %X)\n",
1531 ata_port_disable(ap);
1534 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1537 if (ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT)) {
1538 ata_port_disable(ap);
1542 ap->cbl = ATA_CBL_SATA;
1546 * sata_phy_reset - Reset SATA bus.
1547 * @ap: SATA port associated with target SATA PHY.
1549 * This function resets the SATA bus, and then probes
1550 * the bus for devices.
1553 * PCI/etc. bus probe sem.
1556 void sata_phy_reset(struct ata_port *ap)
1558 __sata_phy_reset(ap);
1559 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1565 * ata_port_disable - Disable port.
1566 * @ap: Port to be disabled.
1568 * Modify @ap data structure such that the system
1569 * thinks that the entire port is disabled, and should
1570 * never attempt to probe or communicate with devices
1573 * LOCKING: host_set lock, or some other form of
1577 void ata_port_disable(struct ata_port *ap)
1579 ap->device[0].class = ATA_DEV_NONE;
1580 ap->device[1].class = ATA_DEV_NONE;
1581 ap->flags |= ATA_FLAG_PORT_DISABLED;
1585 * This mode timing computation functionality is ported over from
1586 * drivers/ide/ide-timing.h and was originally written by Vojtech Pavlik
1589 * PIO 0-5, MWDMA 0-2 and UDMA 0-6 timings (in nanoseconds).
1590 * These were taken from ATA/ATAPI-6 standard, rev 0a, except
1591 * for PIO 5, which is a nonstandard extension and UDMA6, which
1592 * is currently supported only by Maxtor drives.
1595 static const struct ata_timing ata_timing[] = {
1597 { XFER_UDMA_6, 0, 0, 0, 0, 0, 0, 0, 15 },
1598 { XFER_UDMA_5, 0, 0, 0, 0, 0, 0, 0, 20 },
1599 { XFER_UDMA_4, 0, 0, 0, 0, 0, 0, 0, 30 },
1600 { XFER_UDMA_3, 0, 0, 0, 0, 0, 0, 0, 45 },
1602 { XFER_UDMA_2, 0, 0, 0, 0, 0, 0, 0, 60 },
1603 { XFER_UDMA_1, 0, 0, 0, 0, 0, 0, 0, 80 },
1604 { XFER_UDMA_0, 0, 0, 0, 0, 0, 0, 0, 120 },
1606 /* { XFER_UDMA_SLOW, 0, 0, 0, 0, 0, 0, 0, 150 }, */
1608 { XFER_MW_DMA_2, 25, 0, 0, 0, 70, 25, 120, 0 },
1609 { XFER_MW_DMA_1, 45, 0, 0, 0, 80, 50, 150, 0 },
1610 { XFER_MW_DMA_0, 60, 0, 0, 0, 215, 215, 480, 0 },
1612 { XFER_SW_DMA_2, 60, 0, 0, 0, 120, 120, 240, 0 },
1613 { XFER_SW_DMA_1, 90, 0, 0, 0, 240, 240, 480, 0 },
1614 { XFER_SW_DMA_0, 120, 0, 0, 0, 480, 480, 960, 0 },
1616 /* { XFER_PIO_5, 20, 50, 30, 100, 50, 30, 100, 0 }, */
1617 { XFER_PIO_4, 25, 70, 25, 120, 70, 25, 120, 0 },
1618 { XFER_PIO_3, 30, 80, 70, 180, 80, 70, 180, 0 },
1620 { XFER_PIO_2, 30, 290, 40, 330, 100, 90, 240, 0 },
1621 { XFER_PIO_1, 50, 290, 93, 383, 125, 100, 383, 0 },
1622 { XFER_PIO_0, 70, 290, 240, 600, 165, 150, 600, 0 },
1624 /* { XFER_PIO_SLOW, 120, 290, 240, 960, 290, 240, 960, 0 }, */
1629 #define ENOUGH(v,unit) (((v)-1)/(unit)+1)
1630 #define EZ(v,unit) ((v)?ENOUGH(v,unit):0)
1632 static void ata_timing_quantize(const struct ata_timing *t, struct ata_timing *q, int T, int UT)
1634 q->setup = EZ(t->setup * 1000, T);
1635 q->act8b = EZ(t->act8b * 1000, T);
1636 q->rec8b = EZ(t->rec8b * 1000, T);
1637 q->cyc8b = EZ(t->cyc8b * 1000, T);
1638 q->active = EZ(t->active * 1000, T);
1639 q->recover = EZ(t->recover * 1000, T);
1640 q->cycle = EZ(t->cycle * 1000, T);
1641 q->udma = EZ(t->udma * 1000, UT);
1644 void ata_timing_merge(const struct ata_timing *a, const struct ata_timing *b,
1645 struct ata_timing *m, unsigned int what)
1647 if (what & ATA_TIMING_SETUP ) m->setup = max(a->setup, b->setup);
1648 if (what & ATA_TIMING_ACT8B ) m->act8b = max(a->act8b, b->act8b);
1649 if (what & ATA_TIMING_REC8B ) m->rec8b = max(a->rec8b, b->rec8b);
1650 if (what & ATA_TIMING_CYC8B ) m->cyc8b = max(a->cyc8b, b->cyc8b);
1651 if (what & ATA_TIMING_ACTIVE ) m->active = max(a->active, b->active);
1652 if (what & ATA_TIMING_RECOVER) m->recover = max(a->recover, b->recover);
1653 if (what & ATA_TIMING_CYCLE ) m->cycle = max(a->cycle, b->cycle);
1654 if (what & ATA_TIMING_UDMA ) m->udma = max(a->udma, b->udma);
1657 static const struct ata_timing* ata_timing_find_mode(unsigned short speed)
1659 const struct ata_timing *t;
1661 for (t = ata_timing; t->mode != speed; t++)
1662 if (t->mode == 0xFF)
1667 int ata_timing_compute(struct ata_device *adev, unsigned short speed,
1668 struct ata_timing *t, int T, int UT)
1670 const struct ata_timing *s;
1671 struct ata_timing p;
1677 if (!(s = ata_timing_find_mode(speed)))
1680 memcpy(t, s, sizeof(*s));
1683 * If the drive is an EIDE drive, it can tell us it needs extended
1684 * PIO/MW_DMA cycle timing.
1687 if (adev->id[ATA_ID_FIELD_VALID] & 2) { /* EIDE drive */
1688 memset(&p, 0, sizeof(p));
1689 if(speed >= XFER_PIO_0 && speed <= XFER_SW_DMA_0) {
1690 if (speed <= XFER_PIO_2) p.cycle = p.cyc8b = adev->id[ATA_ID_EIDE_PIO];
1691 else p.cycle = p.cyc8b = adev->id[ATA_ID_EIDE_PIO_IORDY];
1692 } else if(speed >= XFER_MW_DMA_0 && speed <= XFER_MW_DMA_2) {
1693 p.cycle = adev->id[ATA_ID_EIDE_DMA_MIN];
1695 ata_timing_merge(&p, t, t, ATA_TIMING_CYCLE | ATA_TIMING_CYC8B);
1699 * Convert the timing to bus clock counts.
1702 ata_timing_quantize(t, t, T, UT);
1705 * Even in DMA/UDMA modes we still use PIO access for IDENTIFY, S.M.A.R.T
1706 * and some other commands. We have to ensure that the DMA cycle timing is
1707 * slower/equal than the fastest PIO timing.
1710 if (speed > XFER_PIO_4) {
1711 ata_timing_compute(adev, adev->pio_mode, &p, T, UT);
1712 ata_timing_merge(&p, t, t, ATA_TIMING_ALL);
1716 * Lenghten active & recovery time so that cycle time is correct.
1719 if (t->act8b + t->rec8b < t->cyc8b) {
1720 t->act8b += (t->cyc8b - (t->act8b + t->rec8b)) / 2;
1721 t->rec8b = t->cyc8b - t->act8b;
1724 if (t->active + t->recover < t->cycle) {
1725 t->active += (t->cycle - (t->active + t->recover)) / 2;
1726 t->recover = t->cycle - t->active;
1732 static const struct {
1735 } xfer_mode_classes[] = {
1736 { ATA_SHIFT_UDMA, XFER_UDMA_0 },
1737 { ATA_SHIFT_MWDMA, XFER_MW_DMA_0 },
1738 { ATA_SHIFT_PIO, XFER_PIO_0 },
1741 static inline u8 base_from_shift(unsigned int shift)
1745 for (i = 0; i < ARRAY_SIZE(xfer_mode_classes); i++)
1746 if (xfer_mode_classes[i].shift == shift)
1747 return xfer_mode_classes[i].base;
1752 static void ata_dev_set_mode(struct ata_port *ap, struct ata_device *dev)
1757 if (!ata_dev_present(dev) || (ap->flags & ATA_FLAG_PORT_DISABLED))
1760 if (dev->xfer_shift == ATA_SHIFT_PIO)
1761 dev->flags |= ATA_DFLAG_PIO;
1763 ata_dev_set_xfermode(ap, dev);
1765 base = base_from_shift(dev->xfer_shift);
1766 ofs = dev->xfer_mode - base;
1767 idx = ofs + dev->xfer_shift;
1768 WARN_ON(idx >= ARRAY_SIZE(xfer_mode_str));
1770 DPRINTK("idx=%d xfer_shift=%u, xfer_mode=0x%x, base=0x%x, offset=%d\n",
1771 idx, dev->xfer_shift, (int)dev->xfer_mode, (int)base, ofs);
1773 printk(KERN_INFO "ata%u: dev %u configured for %s\n",
1774 ap->id, dev->devno, xfer_mode_str[idx]);
1777 static int ata_host_set_pio(struct ata_port *ap)
1783 mask = ata_get_mode_mask(ap, ATA_SHIFT_PIO);
1786 printk(KERN_WARNING "ata%u: no PIO support\n", ap->id);
1790 base = base_from_shift(ATA_SHIFT_PIO);
1791 xfer_mode = base + x;
1793 DPRINTK("base 0x%x xfer_mode 0x%x mask 0x%x x %d\n",
1794 (int)base, (int)xfer_mode, mask, x);
1796 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1797 struct ata_device *dev = &ap->device[i];
1798 if (ata_dev_present(dev)) {
1799 dev->pio_mode = xfer_mode;
1800 dev->xfer_mode = xfer_mode;
1801 dev->xfer_shift = ATA_SHIFT_PIO;
1802 if (ap->ops->set_piomode)
1803 ap->ops->set_piomode(ap, dev);
1810 static void ata_host_set_dma(struct ata_port *ap, u8 xfer_mode,
1811 unsigned int xfer_shift)
1815 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1816 struct ata_device *dev = &ap->device[i];
1817 if (ata_dev_present(dev)) {
1818 dev->dma_mode = xfer_mode;
1819 dev->xfer_mode = xfer_mode;
1820 dev->xfer_shift = xfer_shift;
1821 if (ap->ops->set_dmamode)
1822 ap->ops->set_dmamode(ap, dev);
1828 * ata_set_mode - Program timings and issue SET FEATURES - XFER
1829 * @ap: port on which timings will be programmed
1831 * Set ATA device disk transfer mode (PIO3, UDMA6, etc.).
1834 * PCI/etc. bus probe sem.
1837 static void ata_set_mode(struct ata_port *ap)
1839 unsigned int xfer_shift;
1843 /* step 1: always set host PIO timings */
1844 rc = ata_host_set_pio(ap);
1848 /* step 2: choose the best data xfer mode */
1849 xfer_mode = xfer_shift = 0;
1850 rc = ata_choose_xfer_mode(ap, &xfer_mode, &xfer_shift);
1854 /* step 3: if that xfer mode isn't PIO, set host DMA timings */
1855 if (xfer_shift != ATA_SHIFT_PIO)
1856 ata_host_set_dma(ap, xfer_mode, xfer_shift);
1858 /* step 4: update devices' xfer mode */
1859 ata_dev_set_mode(ap, &ap->device[0]);
1860 ata_dev_set_mode(ap, &ap->device[1]);
1862 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1865 if (ap->ops->post_set_mode)
1866 ap->ops->post_set_mode(ap);
1871 ata_port_disable(ap);
1875 * ata_busy_sleep - sleep until BSY clears, or timeout
1876 * @ap: port containing status register to be polled
1877 * @tmout_pat: impatience timeout
1878 * @tmout: overall timeout
1880 * Sleep until ATA Status register bit BSY clears,
1881 * or a timeout occurs.
1887 static unsigned int ata_busy_sleep (struct ata_port *ap,
1888 unsigned long tmout_pat,
1889 unsigned long tmout)
1891 unsigned long timer_start, timeout;
1894 status = ata_busy_wait(ap, ATA_BUSY, 300);
1895 timer_start = jiffies;
1896 timeout = timer_start + tmout_pat;
1897 while ((status & ATA_BUSY) && (time_before(jiffies, timeout))) {
1899 status = ata_busy_wait(ap, ATA_BUSY, 3);
1902 if (status & ATA_BUSY)
1903 printk(KERN_WARNING "ata%u is slow to respond, "
1904 "please be patient\n", ap->id);
1906 timeout = timer_start + tmout;
1907 while ((status & ATA_BUSY) && (time_before(jiffies, timeout))) {
1909 status = ata_chk_status(ap);
1912 if (status & ATA_BUSY) {
1913 printk(KERN_ERR "ata%u failed to respond (%lu secs)\n",
1914 ap->id, tmout / HZ);
1921 static void ata_bus_post_reset(struct ata_port *ap, unsigned int devmask)
1923 struct ata_ioports *ioaddr = &ap->ioaddr;
1924 unsigned int dev0 = devmask & (1 << 0);
1925 unsigned int dev1 = devmask & (1 << 1);
1926 unsigned long timeout;
1928 /* if device 0 was found in ata_devchk, wait for its
1932 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
1934 /* if device 1 was found in ata_devchk, wait for
1935 * register access, then wait for BSY to clear
1937 timeout = jiffies + ATA_TMOUT_BOOT;
1941 ap->ops->dev_select(ap, 1);
1942 if (ap->flags & ATA_FLAG_MMIO) {
1943 nsect = readb((void __iomem *) ioaddr->nsect_addr);
1944 lbal = readb((void __iomem *) ioaddr->lbal_addr);
1946 nsect = inb(ioaddr->nsect_addr);
1947 lbal = inb(ioaddr->lbal_addr);
1949 if ((nsect == 1) && (lbal == 1))
1951 if (time_after(jiffies, timeout)) {
1955 msleep(50); /* give drive a breather */
1958 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
1960 /* is all this really necessary? */
1961 ap->ops->dev_select(ap, 0);
1963 ap->ops->dev_select(ap, 1);
1965 ap->ops->dev_select(ap, 0);
1969 * ata_bus_edd - Issue EXECUTE DEVICE DIAGNOSTIC command.
1970 * @ap: Port to reset and probe
1972 * Use the EXECUTE DEVICE DIAGNOSTIC command to reset and
1973 * probe the bus. Not often used these days.
1976 * PCI/etc. bus probe sem.
1977 * Obtains host_set lock.
1981 static unsigned int ata_bus_edd(struct ata_port *ap)
1983 struct ata_taskfile tf;
1984 unsigned long flags;
1986 /* set up execute-device-diag (bus reset) taskfile */
1987 /* also, take interrupts to a known state (disabled) */
1988 DPRINTK("execute-device-diag\n");
1989 ata_tf_init(ap, &tf, 0);
1991 tf.command = ATA_CMD_EDD;
1992 tf.protocol = ATA_PROT_NODATA;
1995 spin_lock_irqsave(&ap->host_set->lock, flags);
1996 ata_tf_to_host(ap, &tf);
1997 spin_unlock_irqrestore(&ap->host_set->lock, flags);
1999 /* spec says at least 2ms. but who knows with those
2000 * crazy ATAPI devices...
2004 return ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
2007 static unsigned int ata_bus_softreset(struct ata_port *ap,
2008 unsigned int devmask)
2010 struct ata_ioports *ioaddr = &ap->ioaddr;
2012 DPRINTK("ata%u: bus reset via SRST\n", ap->id);
2014 /* software reset. causes dev0 to be selected */
2015 if (ap->flags & ATA_FLAG_MMIO) {
2016 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
2017 udelay(20); /* FIXME: flush */
2018 writeb(ap->ctl | ATA_SRST, (void __iomem *) ioaddr->ctl_addr);
2019 udelay(20); /* FIXME: flush */
2020 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
2022 outb(ap->ctl, ioaddr->ctl_addr);
2024 outb(ap->ctl | ATA_SRST, ioaddr->ctl_addr);
2026 outb(ap->ctl, ioaddr->ctl_addr);
2029 /* spec mandates ">= 2ms" before checking status.
2030 * We wait 150ms, because that was the magic delay used for
2031 * ATAPI devices in Hale Landis's ATADRVR, for the period of time
2032 * between when the ATA command register is written, and then
2033 * status is checked. Because waiting for "a while" before
2034 * checking status is fine, post SRST, we perform this magic
2035 * delay here as well.
2039 ata_bus_post_reset(ap, devmask);
2045 * ata_bus_reset - reset host port and associated ATA channel
2046 * @ap: port to reset
2048 * This is typically the first time we actually start issuing
2049 * commands to the ATA channel. We wait for BSY to clear, then
2050 * issue EXECUTE DEVICE DIAGNOSTIC command, polling for its
2051 * result. Determine what devices, if any, are on the channel
2052 * by looking at the device 0/1 error register. Look at the signature
2053 * stored in each device's taskfile registers, to determine if
2054 * the device is ATA or ATAPI.
2057 * PCI/etc. bus probe sem.
2058 * Obtains host_set lock.
2061 * Sets ATA_FLAG_PORT_DISABLED if bus reset fails.
2064 void ata_bus_reset(struct ata_port *ap)
2066 struct ata_ioports *ioaddr = &ap->ioaddr;
2067 unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS;
2069 unsigned int dev0, dev1 = 0, rc = 0, devmask = 0;
2071 DPRINTK("ENTER, host %u, port %u\n", ap->id, ap->port_no);
2073 /* determine if device 0/1 are present */
2074 if (ap->flags & ATA_FLAG_SATA_RESET)
2077 dev0 = ata_devchk(ap, 0);
2079 dev1 = ata_devchk(ap, 1);
2083 devmask |= (1 << 0);
2085 devmask |= (1 << 1);
2087 /* select device 0 again */
2088 ap->ops->dev_select(ap, 0);
2090 /* issue bus reset */
2091 if (ap->flags & ATA_FLAG_SRST)
2092 rc = ata_bus_softreset(ap, devmask);
2093 else if ((ap->flags & ATA_FLAG_SATA_RESET) == 0) {
2094 /* set up device control */
2095 if (ap->flags & ATA_FLAG_MMIO)
2096 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
2098 outb(ap->ctl, ioaddr->ctl_addr);
2099 rc = ata_bus_edd(ap);
2106 * determine by signature whether we have ATA or ATAPI devices
2108 err = ata_dev_try_classify(ap, 0);
2109 if ((slave_possible) && (err != 0x81))
2110 ata_dev_try_classify(ap, 1);
2112 /* re-enable interrupts */
2113 if (ap->ioaddr.ctl_addr) /* FIXME: hack. create a hook instead */
2116 /* is double-select really necessary? */
2117 if (ap->device[1].class != ATA_DEV_NONE)
2118 ap->ops->dev_select(ap, 1);
2119 if (ap->device[0].class != ATA_DEV_NONE)
2120 ap->ops->dev_select(ap, 0);
2122 /* if no devices were detected, disable this port */
2123 if ((ap->device[0].class == ATA_DEV_NONE) &&
2124 (ap->device[1].class == ATA_DEV_NONE))
2127 if (ap->flags & (ATA_FLAG_SATA_RESET | ATA_FLAG_SRST)) {
2128 /* set up device control for ATA_FLAG_SATA_RESET */
2129 if (ap->flags & ATA_FLAG_MMIO)
2130 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
2132 outb(ap->ctl, ioaddr->ctl_addr);
2139 printk(KERN_ERR "ata%u: disabling port\n", ap->id);
2140 ap->ops->port_disable(ap);
2145 static void ata_pr_blacklisted(const struct ata_port *ap,
2146 const struct ata_device *dev)
2148 printk(KERN_WARNING "ata%u: dev %u is on DMA blacklist, disabling DMA\n",
2149 ap->id, dev->devno);
2152 static const char * const ata_dma_blacklist [] = {
2171 "Toshiba CD-ROM XM-6202B",
2172 "TOSHIBA CD-ROM XM-1702BC",
2174 "E-IDE CD-ROM CR-840",
2177 "SAMSUNG CD-ROM SC-148C",
2178 "SAMSUNG CD-ROM SC",
2180 "ATAPI CD-ROM DRIVE 40X MAXIMUM",
2184 static int ata_dma_blacklisted(const struct ata_device *dev)
2186 unsigned char model_num[40];
2191 ata_dev_id_string(dev->id, model_num, ATA_ID_PROD_OFS,
2194 len = strnlen(s, sizeof(model_num));
2196 /* ATAPI specifies that empty space is blank-filled; remove blanks */
2197 while ((len > 0) && (s[len - 1] == ' ')) {
2202 for (i = 0; i < ARRAY_SIZE(ata_dma_blacklist); i++)
2203 if (!strncmp(ata_dma_blacklist[i], s, len))
2209 static unsigned int ata_get_mode_mask(const struct ata_port *ap, int shift)
2211 const struct ata_device *master, *slave;
2214 master = &ap->device[0];
2215 slave = &ap->device[1];
2217 assert (ata_dev_present(master) || ata_dev_present(slave));
2219 if (shift == ATA_SHIFT_UDMA) {
2220 mask = ap->udma_mask;
2221 if (ata_dev_present(master)) {
2222 mask &= (master->id[ATA_ID_UDMA_MODES] & 0xff);
2223 if (ata_dma_blacklisted(master)) {
2225 ata_pr_blacklisted(ap, master);
2228 if (ata_dev_present(slave)) {
2229 mask &= (slave->id[ATA_ID_UDMA_MODES] & 0xff);
2230 if (ata_dma_blacklisted(slave)) {
2232 ata_pr_blacklisted(ap, slave);
2236 else if (shift == ATA_SHIFT_MWDMA) {
2237 mask = ap->mwdma_mask;
2238 if (ata_dev_present(master)) {
2239 mask &= (master->id[ATA_ID_MWDMA_MODES] & 0x07);
2240 if (ata_dma_blacklisted(master)) {
2242 ata_pr_blacklisted(ap, master);
2245 if (ata_dev_present(slave)) {
2246 mask &= (slave->id[ATA_ID_MWDMA_MODES] & 0x07);
2247 if (ata_dma_blacklisted(slave)) {
2249 ata_pr_blacklisted(ap, slave);
2253 else if (shift == ATA_SHIFT_PIO) {
2254 mask = ap->pio_mask;
2255 if (ata_dev_present(master)) {
2256 /* spec doesn't return explicit support for
2257 * PIO0-2, so we fake it
2259 u16 tmp_mode = master->id[ATA_ID_PIO_MODES] & 0x03;
2264 if (ata_dev_present(slave)) {
2265 /* spec doesn't return explicit support for
2266 * PIO0-2, so we fake it
2268 u16 tmp_mode = slave->id[ATA_ID_PIO_MODES] & 0x03;
2275 mask = 0xffffffff; /* shut up compiler warning */
2282 /* find greatest bit */
2283 static int fgb(u32 bitmap)
2288 for (i = 0; i < 32; i++)
2289 if (bitmap & (1 << i))
2296 * ata_choose_xfer_mode - attempt to find best transfer mode
2297 * @ap: Port for which an xfer mode will be selected
2298 * @xfer_mode_out: (output) SET FEATURES - XFER MODE code
2299 * @xfer_shift_out: (output) bit shift that selects this mode
2301 * Based on host and device capabilities, determine the
2302 * maximum transfer mode that is amenable to all.
2305 * PCI/etc. bus probe sem.
2308 * Zero on success, negative on error.
2311 static int ata_choose_xfer_mode(const struct ata_port *ap,
2313 unsigned int *xfer_shift_out)
2315 unsigned int mask, shift;
2318 for (i = 0; i < ARRAY_SIZE(xfer_mode_classes); i++) {
2319 shift = xfer_mode_classes[i].shift;
2320 mask = ata_get_mode_mask(ap, shift);
2324 *xfer_mode_out = xfer_mode_classes[i].base + x;
2325 *xfer_shift_out = shift;
2334 * ata_dev_set_xfermode - Issue SET FEATURES - XFER MODE command
2335 * @ap: Port associated with device @dev
2336 * @dev: Device to which command will be sent
2338 * Issue SET FEATURES - XFER MODE command to device @dev
2342 * PCI/etc. bus probe sem.
2345 static void ata_dev_set_xfermode(struct ata_port *ap, struct ata_device *dev)
2347 struct ata_taskfile tf;
2349 /* set up set-features taskfile */
2350 DPRINTK("set features - xfer mode\n");
2352 ata_tf_init(ap, &tf, dev->devno);
2353 tf.command = ATA_CMD_SET_FEATURES;
2354 tf.feature = SETFEATURES_XFER;
2355 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
2356 tf.protocol = ATA_PROT_NODATA;
2357 tf.nsect = dev->xfer_mode;
2359 if (ata_exec_internal(ap, dev, &tf, DMA_NONE, NULL, 0)) {
2360 printk(KERN_ERR "ata%u: failed to set xfermode, disabled\n",
2362 ata_port_disable(ap);
2369 * ata_dev_reread_id - Reread the device identify device info
2370 * @ap: port where the device is
2371 * @dev: device to reread the identify device info
2376 static void ata_dev_reread_id(struct ata_port *ap, struct ata_device *dev)
2378 struct ata_taskfile tf;
2380 ata_tf_init(ap, &tf, dev->devno);
2382 if (dev->class == ATA_DEV_ATA) {
2383 tf.command = ATA_CMD_ID_ATA;
2384 DPRINTK("do ATA identify\n");
2386 tf.command = ATA_CMD_ID_ATAPI;
2387 DPRINTK("do ATAPI identify\n");
2390 tf.flags |= ATA_TFLAG_DEVICE;
2391 tf.protocol = ATA_PROT_PIO;
2393 if (ata_exec_internal(ap, dev, &tf, DMA_FROM_DEVICE,
2394 dev->id, sizeof(dev->id)))
2397 swap_buf_le16(dev->id, ATA_ID_WORDS);
2405 printk(KERN_ERR "ata%u: failed to reread ID, disabled\n", ap->id);
2406 ata_port_disable(ap);
2410 * ata_dev_init_params - Issue INIT DEV PARAMS command
2411 * @ap: Port associated with device @dev
2412 * @dev: Device to which command will be sent
2417 static void ata_dev_init_params(struct ata_port *ap, struct ata_device *dev)
2419 struct ata_taskfile tf;
2420 u16 sectors = dev->id[6];
2421 u16 heads = dev->id[3];
2423 /* Number of sectors per track 1-255. Number of heads 1-16 */
2424 if (sectors < 1 || sectors > 255 || heads < 1 || heads > 16)
2427 /* set up init dev params taskfile */
2428 DPRINTK("init dev params \n");
2430 ata_tf_init(ap, &tf, dev->devno);
2431 tf.command = ATA_CMD_INIT_DEV_PARAMS;
2432 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
2433 tf.protocol = ATA_PROT_NODATA;
2435 tf.device |= (heads - 1) & 0x0f; /* max head = num. of heads - 1 */
2437 if (ata_exec_internal(ap, dev, &tf, DMA_NONE, NULL, 0)) {
2438 printk(KERN_ERR "ata%u: failed to init parameters, disabled\n",
2440 ata_port_disable(ap);
2447 * ata_sg_clean - Unmap DMA memory associated with command
2448 * @qc: Command containing DMA memory to be released
2450 * Unmap all mapped DMA memory associated with this command.
2453 * spin_lock_irqsave(host_set lock)
2456 static void ata_sg_clean(struct ata_queued_cmd *qc)
2458 struct ata_port *ap = qc->ap;
2459 struct scatterlist *sg = qc->__sg;
2460 int dir = qc->dma_dir;
2461 void *pad_buf = NULL;
2463 assert(qc->flags & ATA_QCFLAG_DMAMAP);
2466 if (qc->flags & ATA_QCFLAG_SINGLE)
2467 assert(qc->n_elem == 1);
2469 VPRINTK("unmapping %u sg elements\n", qc->n_elem);
2471 /* if we padded the buffer out to 32-bit bound, and data
2472 * xfer direction is from-device, we must copy from the
2473 * pad buffer back into the supplied buffer
2475 if (qc->pad_len && !(qc->tf.flags & ATA_TFLAG_WRITE))
2476 pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
2478 if (qc->flags & ATA_QCFLAG_SG) {
2480 dma_unmap_sg(ap->host_set->dev, sg, qc->n_elem, dir);
2481 /* restore last sg */
2482 sg[qc->orig_n_elem - 1].length += qc->pad_len;
2484 struct scatterlist *psg = &qc->pad_sgent;
2485 void *addr = kmap_atomic(psg->page, KM_IRQ0);
2486 memcpy(addr + psg->offset, pad_buf, qc->pad_len);
2487 kunmap_atomic(addr, KM_IRQ0);
2490 if (sg_dma_len(&sg[0]) > 0)
2491 dma_unmap_single(ap->host_set->dev,
2492 sg_dma_address(&sg[0]), sg_dma_len(&sg[0]),
2495 sg->length += qc->pad_len;
2497 memcpy(qc->buf_virt + sg->length - qc->pad_len,
2498 pad_buf, qc->pad_len);
2501 qc->flags &= ~ATA_QCFLAG_DMAMAP;
2506 * ata_fill_sg - Fill PCI IDE PRD table
2507 * @qc: Metadata associated with taskfile to be transferred
2509 * Fill PCI IDE PRD (scatter-gather) table with segments
2510 * associated with the current disk command.
2513 * spin_lock_irqsave(host_set lock)
2516 static void ata_fill_sg(struct ata_queued_cmd *qc)
2518 struct ata_port *ap = qc->ap;
2519 struct scatterlist *sg;
2522 assert(qc->__sg != NULL);
2523 assert(qc->n_elem > 0);
2526 ata_for_each_sg(sg, qc) {
2530 /* determine if physical DMA addr spans 64K boundary.
2531 * Note h/w doesn't support 64-bit, so we unconditionally
2532 * truncate dma_addr_t to u32.
2534 addr = (u32) sg_dma_address(sg);
2535 sg_len = sg_dma_len(sg);
2538 offset = addr & 0xffff;
2540 if ((offset + sg_len) > 0x10000)
2541 len = 0x10000 - offset;
2543 ap->prd[idx].addr = cpu_to_le32(addr);
2544 ap->prd[idx].flags_len = cpu_to_le32(len & 0xffff);
2545 VPRINTK("PRD[%u] = (0x%X, 0x%X)\n", idx, addr, len);
2554 ap->prd[idx - 1].flags_len |= cpu_to_le32(ATA_PRD_EOT);
2557 * ata_check_atapi_dma - Check whether ATAPI DMA can be supported
2558 * @qc: Metadata associated with taskfile to check
2560 * Allow low-level driver to filter ATA PACKET commands, returning
2561 * a status indicating whether or not it is OK to use DMA for the
2562 * supplied PACKET command.
2565 * spin_lock_irqsave(host_set lock)
2567 * RETURNS: 0 when ATAPI DMA can be used
2570 int ata_check_atapi_dma(struct ata_queued_cmd *qc)
2572 struct ata_port *ap = qc->ap;
2573 int rc = 0; /* Assume ATAPI DMA is OK by default */
2575 if (ap->ops->check_atapi_dma)
2576 rc = ap->ops->check_atapi_dma(qc);
2581 * ata_qc_prep - Prepare taskfile for submission
2582 * @qc: Metadata associated with taskfile to be prepared
2584 * Prepare ATA taskfile for submission.
2587 * spin_lock_irqsave(host_set lock)
2589 void ata_qc_prep(struct ata_queued_cmd *qc)
2591 if (!(qc->flags & ATA_QCFLAG_DMAMAP))
2598 * ata_sg_init_one - Associate command with memory buffer
2599 * @qc: Command to be associated
2600 * @buf: Memory buffer
2601 * @buflen: Length of memory buffer, in bytes.
2603 * Initialize the data-related elements of queued_cmd @qc
2604 * to point to a single memory buffer, @buf of byte length @buflen.
2607 * spin_lock_irqsave(host_set lock)
2610 void ata_sg_init_one(struct ata_queued_cmd *qc, void *buf, unsigned int buflen)
2612 struct scatterlist *sg;
2614 qc->flags |= ATA_QCFLAG_SINGLE;
2616 memset(&qc->sgent, 0, sizeof(qc->sgent));
2617 qc->__sg = &qc->sgent;
2619 qc->orig_n_elem = 1;
2623 sg_init_one(sg, buf, buflen);
2627 * ata_sg_init - Associate command with scatter-gather table.
2628 * @qc: Command to be associated
2629 * @sg: Scatter-gather table.
2630 * @n_elem: Number of elements in s/g table.
2632 * Initialize the data-related elements of queued_cmd @qc
2633 * to point to a scatter-gather table @sg, containing @n_elem
2637 * spin_lock_irqsave(host_set lock)
2640 void ata_sg_init(struct ata_queued_cmd *qc, struct scatterlist *sg,
2641 unsigned int n_elem)
2643 qc->flags |= ATA_QCFLAG_SG;
2645 qc->n_elem = n_elem;
2646 qc->orig_n_elem = n_elem;
2650 * ata_sg_setup_one - DMA-map the memory buffer associated with a command.
2651 * @qc: Command with memory buffer to be mapped.
2653 * DMA-map the memory buffer associated with queued_cmd @qc.
2656 * spin_lock_irqsave(host_set lock)
2659 * Zero on success, negative on error.
2662 static int ata_sg_setup_one(struct ata_queued_cmd *qc)
2664 struct ata_port *ap = qc->ap;
2665 int dir = qc->dma_dir;
2666 struct scatterlist *sg = qc->__sg;
2667 dma_addr_t dma_address;
2669 /* we must lengthen transfers to end on a 32-bit boundary */
2670 qc->pad_len = sg->length & 3;
2672 void *pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
2673 struct scatterlist *psg = &qc->pad_sgent;
2675 assert(qc->dev->class == ATA_DEV_ATAPI);
2677 memset(pad_buf, 0, ATA_DMA_PAD_SZ);
2679 if (qc->tf.flags & ATA_TFLAG_WRITE)
2680 memcpy(pad_buf, qc->buf_virt + sg->length - qc->pad_len,
2683 sg_dma_address(psg) = ap->pad_dma + (qc->tag * ATA_DMA_PAD_SZ);
2684 sg_dma_len(psg) = ATA_DMA_PAD_SZ;
2686 sg->length -= qc->pad_len;
2688 DPRINTK("padding done, sg->length=%u pad_len=%u\n",
2689 sg->length, qc->pad_len);
2693 sg_dma_address(sg) = 0;
2697 dma_address = dma_map_single(ap->host_set->dev, qc->buf_virt,
2699 if (dma_mapping_error(dma_address)) {
2701 sg->length += qc->pad_len;
2705 sg_dma_address(sg) = dma_address;
2707 sg_dma_len(sg) = sg->length;
2709 DPRINTK("mapped buffer of %d bytes for %s\n", sg_dma_len(sg),
2710 qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
2716 * ata_sg_setup - DMA-map the scatter-gather table associated with a command.
2717 * @qc: Command with scatter-gather table to be mapped.
2719 * DMA-map the scatter-gather table associated with queued_cmd @qc.
2722 * spin_lock_irqsave(host_set lock)
2725 * Zero on success, negative on error.
2729 static int ata_sg_setup(struct ata_queued_cmd *qc)
2731 struct ata_port *ap = qc->ap;
2732 struct scatterlist *sg = qc->__sg;
2733 struct scatterlist *lsg = &sg[qc->n_elem - 1];
2734 int n_elem, pre_n_elem, dir, trim_sg = 0;
2736 VPRINTK("ENTER, ata%u\n", ap->id);
2737 assert(qc->flags & ATA_QCFLAG_SG);
2739 /* we must lengthen transfers to end on a 32-bit boundary */
2740 qc->pad_len = lsg->length & 3;
2742 void *pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
2743 struct scatterlist *psg = &qc->pad_sgent;
2744 unsigned int offset;
2746 assert(qc->dev->class == ATA_DEV_ATAPI);
2748 memset(pad_buf, 0, ATA_DMA_PAD_SZ);
2751 * psg->page/offset are used to copy to-be-written
2752 * data in this function or read data in ata_sg_clean.
2754 offset = lsg->offset + lsg->length - qc->pad_len;
2755 psg->page = nth_page(lsg->page, offset >> PAGE_SHIFT);
2756 psg->offset = offset_in_page(offset);
2758 if (qc->tf.flags & ATA_TFLAG_WRITE) {
2759 void *addr = kmap_atomic(psg->page, KM_IRQ0);
2760 memcpy(pad_buf, addr + psg->offset, qc->pad_len);
2761 kunmap_atomic(addr, KM_IRQ0);
2764 sg_dma_address(psg) = ap->pad_dma + (qc->tag * ATA_DMA_PAD_SZ);
2765 sg_dma_len(psg) = ATA_DMA_PAD_SZ;
2767 lsg->length -= qc->pad_len;
2768 if (lsg->length == 0)
2771 DPRINTK("padding done, sg[%d].length=%u pad_len=%u\n",
2772 qc->n_elem - 1, lsg->length, qc->pad_len);
2775 pre_n_elem = qc->n_elem;
2776 if (trim_sg && pre_n_elem)
2785 n_elem = dma_map_sg(ap->host_set->dev, sg, pre_n_elem, dir);
2787 /* restore last sg */
2788 lsg->length += qc->pad_len;
2792 DPRINTK("%d sg elements mapped\n", n_elem);
2795 qc->n_elem = n_elem;
2801 * ata_poll_qc_complete - turn irq back on and finish qc
2802 * @qc: Command to complete
2803 * @err_mask: ATA status register content
2806 * None. (grabs host lock)
2809 void ata_poll_qc_complete(struct ata_queued_cmd *qc)
2811 struct ata_port *ap = qc->ap;
2812 unsigned long flags;
2814 spin_lock_irqsave(&ap->host_set->lock, flags);
2816 ata_qc_complete(qc);
2817 spin_unlock_irqrestore(&ap->host_set->lock, flags);
2822 * @ap: the target ata_port
2825 * None. (executing in kernel thread context)
2828 * timeout value to use
2831 static unsigned long ata_pio_poll(struct ata_port *ap)
2833 struct ata_queued_cmd *qc;
2835 unsigned int poll_state = HSM_ST_UNKNOWN;
2836 unsigned int reg_state = HSM_ST_UNKNOWN;
2838 qc = ata_qc_from_tag(ap, ap->active_tag);
2841 switch (ap->hsm_task_state) {
2844 poll_state = HSM_ST_POLL;
2848 case HSM_ST_LAST_POLL:
2849 poll_state = HSM_ST_LAST_POLL;
2850 reg_state = HSM_ST_LAST;
2857 status = ata_chk_status(ap);
2858 if (status & ATA_BUSY) {
2859 if (time_after(jiffies, ap->pio_task_timeout)) {
2860 qc->err_mask |= AC_ERR_ATA_BUS;
2861 ap->hsm_task_state = HSM_ST_TMOUT;
2864 ap->hsm_task_state = poll_state;
2865 return ATA_SHORT_PAUSE;
2868 ap->hsm_task_state = reg_state;
2873 * ata_pio_complete - check if drive is busy or idle
2874 * @ap: the target ata_port
2877 * None. (executing in kernel thread context)
2880 * Zero if qc completed.
2881 * Non-zero if has next.
2884 static int ata_pio_complete (struct ata_port *ap)
2886 struct ata_queued_cmd *qc;
2890 * This is purely heuristic. This is a fast path. Sometimes when
2891 * we enter, BSY will be cleared in a chk-status or two. If not,
2892 * the drive is probably seeking or something. Snooze for a couple
2893 * msecs, then chk-status again. If still busy, fall back to
2894 * HSM_ST_LAST_POLL state.
2896 drv_stat = ata_busy_wait(ap, ATA_BUSY, 10);
2897 if (drv_stat & ATA_BUSY) {
2899 drv_stat = ata_busy_wait(ap, ATA_BUSY, 10);
2900 if (drv_stat & ATA_BUSY) {
2901 ap->hsm_task_state = HSM_ST_LAST_POLL;
2902 ap->pio_task_timeout = jiffies + ATA_TMOUT_PIO;
2907 qc = ata_qc_from_tag(ap, ap->active_tag);
2910 drv_stat = ata_wait_idle(ap);
2911 if (!ata_ok(drv_stat)) {
2912 qc->err_mask |= __ac_err_mask(drv_stat);
2913 ap->hsm_task_state = HSM_ST_ERR;
2917 ap->hsm_task_state = HSM_ST_IDLE;
2919 assert(qc->err_mask == 0);
2920 ata_poll_qc_complete(qc);
2922 /* another command may start at this point */
2929 * swap_buf_le16 - swap halves of 16-words in place
2930 * @buf: Buffer to swap
2931 * @buf_words: Number of 16-bit words in buffer.
2933 * Swap halves of 16-bit words if needed to convert from
2934 * little-endian byte order to native cpu byte order, or
2938 * Inherited from caller.
2940 void swap_buf_le16(u16 *buf, unsigned int buf_words)
2945 for (i = 0; i < buf_words; i++)
2946 buf[i] = le16_to_cpu(buf[i]);
2947 #endif /* __BIG_ENDIAN */
2951 * ata_mmio_data_xfer - Transfer data by MMIO
2952 * @ap: port to read/write
2954 * @buflen: buffer length
2955 * @write_data: read/write
2957 * Transfer data from/to the device data register by MMIO.
2960 * Inherited from caller.
2963 static void ata_mmio_data_xfer(struct ata_port *ap, unsigned char *buf,
2964 unsigned int buflen, int write_data)
2967 unsigned int words = buflen >> 1;
2968 u16 *buf16 = (u16 *) buf;
2969 void __iomem *mmio = (void __iomem *)ap->ioaddr.data_addr;
2971 /* Transfer multiple of 2 bytes */
2973 for (i = 0; i < words; i++)
2974 writew(le16_to_cpu(buf16[i]), mmio);
2976 for (i = 0; i < words; i++)
2977 buf16[i] = cpu_to_le16(readw(mmio));
2980 /* Transfer trailing 1 byte, if any. */
2981 if (unlikely(buflen & 0x01)) {
2982 u16 align_buf[1] = { 0 };
2983 unsigned char *trailing_buf = buf + buflen - 1;
2986 memcpy(align_buf, trailing_buf, 1);
2987 writew(le16_to_cpu(align_buf[0]), mmio);
2989 align_buf[0] = cpu_to_le16(readw(mmio));
2990 memcpy(trailing_buf, align_buf, 1);
2996 * ata_pio_data_xfer - Transfer data by PIO
2997 * @ap: port to read/write
2999 * @buflen: buffer length
3000 * @write_data: read/write
3002 * Transfer data from/to the device data register by PIO.
3005 * Inherited from caller.
3008 static void ata_pio_data_xfer(struct ata_port *ap, unsigned char *buf,
3009 unsigned int buflen, int write_data)
3011 unsigned int words = buflen >> 1;
3013 /* Transfer multiple of 2 bytes */
3015 outsw(ap->ioaddr.data_addr, buf, words);
3017 insw(ap->ioaddr.data_addr, buf, words);
3019 /* Transfer trailing 1 byte, if any. */
3020 if (unlikely(buflen & 0x01)) {
3021 u16 align_buf[1] = { 0 };
3022 unsigned char *trailing_buf = buf + buflen - 1;
3025 memcpy(align_buf, trailing_buf, 1);
3026 outw(le16_to_cpu(align_buf[0]), ap->ioaddr.data_addr);
3028 align_buf[0] = cpu_to_le16(inw(ap->ioaddr.data_addr));
3029 memcpy(trailing_buf, align_buf, 1);
3035 * ata_data_xfer - Transfer data from/to the data register.
3036 * @ap: port to read/write
3038 * @buflen: buffer length
3039 * @do_write: read/write
3041 * Transfer data from/to the device data register.
3044 * Inherited from caller.
3047 static void ata_data_xfer(struct ata_port *ap, unsigned char *buf,
3048 unsigned int buflen, int do_write)
3050 if (ap->flags & ATA_FLAG_MMIO)
3051 ata_mmio_data_xfer(ap, buf, buflen, do_write);
3053 ata_pio_data_xfer(ap, buf, buflen, do_write);
3057 * ata_pio_sector - Transfer ATA_SECT_SIZE (512 bytes) of data.
3058 * @qc: Command on going
3060 * Transfer ATA_SECT_SIZE of data from/to the ATA device.
3063 * Inherited from caller.
3066 static void ata_pio_sector(struct ata_queued_cmd *qc)
3068 int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
3069 struct scatterlist *sg = qc->__sg;
3070 struct ata_port *ap = qc->ap;
3072 unsigned int offset;
3075 if (qc->cursect == (qc->nsect - 1))
3076 ap->hsm_task_state = HSM_ST_LAST;
3078 page = sg[qc->cursg].page;
3079 offset = sg[qc->cursg].offset + qc->cursg_ofs * ATA_SECT_SIZE;
3081 /* get the current page and offset */
3082 page = nth_page(page, (offset >> PAGE_SHIFT));
3083 offset %= PAGE_SIZE;
3085 DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
3087 if (PageHighMem(page)) {
3088 unsigned long flags;
3090 local_irq_save(flags);
3091 buf = kmap_atomic(page, KM_IRQ0);
3093 /* do the actual data transfer */
3094 ata_data_xfer(ap, buf + offset, ATA_SECT_SIZE, do_write);
3096 kunmap_atomic(buf, KM_IRQ0);
3097 local_irq_restore(flags);
3099 buf = page_address(page);
3100 ata_data_xfer(ap, buf + offset, ATA_SECT_SIZE, do_write);
3106 if ((qc->cursg_ofs * ATA_SECT_SIZE) == (&sg[qc->cursg])->length) {
3113 * ata_pio_sectors - Transfer one or many 512-byte sectors.
3114 * @qc: Command on going
3116 * Transfer one or many ATA_SECT_SIZE of data from/to the
3117 * ATA device for the DRQ request.
3120 * Inherited from caller.
3123 static void ata_pio_sectors(struct ata_queued_cmd *qc)
3125 if (is_multi_taskfile(&qc->tf)) {
3126 /* READ/WRITE MULTIPLE */
3129 assert(qc->dev->multi_count);
3131 nsect = min(qc->nsect - qc->cursect, qc->dev->multi_count);
3139 * atapi_send_cdb - Write CDB bytes to hardware
3140 * @ap: Port to which ATAPI device is attached.
3141 * @qc: Taskfile currently active
3143 * When device has indicated its readiness to accept
3144 * a CDB, this function is called. Send the CDB.
3150 static void atapi_send_cdb(struct ata_port *ap, struct ata_queued_cmd *qc)
3153 DPRINTK("send cdb\n");
3154 assert(ap->cdb_len >= 12);
3156 ata_data_xfer(ap, qc->cdb, ap->cdb_len, 1);
3157 ata_altstatus(ap); /* flush */
3159 switch (qc->tf.protocol) {
3160 case ATA_PROT_ATAPI:
3161 ap->hsm_task_state = HSM_ST;
3163 case ATA_PROT_ATAPI_NODATA:
3164 ap->hsm_task_state = HSM_ST_LAST;
3166 case ATA_PROT_ATAPI_DMA:
3167 ap->hsm_task_state = HSM_ST_LAST;
3168 /* initiate bmdma */
3169 ap->ops->bmdma_start(qc);
3175 * ata_pio_first_block - Write first data block to hardware
3176 * @ap: Port to which ATA/ATAPI device is attached.
3178 * When device has indicated its readiness to accept
3179 * the data, this function sends out the CDB or
3180 * the first data block by PIO.
3182 * - If polling, ata_pio_task() handles the rest.
3183 * - Otherwise, interrupt handler takes over.
3186 * Kernel thread context (may sleep)
3189 * Zero if irq handler takes over
3190 * Non-zero if has next (polling).
3193 static int ata_pio_first_block(struct ata_port *ap)
3195 struct ata_queued_cmd *qc;
3197 unsigned long flags;
3200 qc = ata_qc_from_tag(ap, ap->active_tag);
3202 assert(qc->flags & ATA_QCFLAG_ACTIVE);
3204 /* if polling, we will stay in the work queue after sending the data.
3205 * otherwise, interrupt handler takes over after sending the data.
3207 has_next = (qc->tf.flags & ATA_TFLAG_POLLING);
3209 /* sleep-wait for BSY to clear */
3210 DPRINTK("busy wait\n");
3211 if (ata_busy_sleep(ap, ATA_TMOUT_DATAOUT_QUICK, ATA_TMOUT_DATAOUT)) {
3212 ap->hsm_task_state = HSM_ST_TMOUT;
3216 /* make sure DRQ is set */
3217 status = ata_chk_status(ap);
3218 if ((status & (ATA_BUSY | ATA_DRQ)) != ATA_DRQ) {
3219 /* device status error */
3220 ap->hsm_task_state = HSM_ST_ERR;
3224 /* Send the CDB (atapi) or the first data block (ata pio out).
3225 * During the state transition, interrupt handler shouldn't
3226 * be invoked before the data transfer is complete and
3227 * hsm_task_state is changed. Hence, the following locking.
3229 spin_lock_irqsave(&ap->host_set->lock, flags);
3231 if (qc->tf.protocol == ATA_PROT_PIO) {
3232 /* PIO data out protocol.
3233 * send first data block.
3236 /* ata_pio_sectors() might change the state to HSM_ST_LAST.
3237 * so, the state is changed here before ata_pio_sectors().
3239 ap->hsm_task_state = HSM_ST;
3240 ata_pio_sectors(qc);
3241 ata_altstatus(ap); /* flush */
3244 atapi_send_cdb(ap, qc);
3246 spin_unlock_irqrestore(&ap->host_set->lock, flags);
3248 /* if polling, ata_pio_task() handles the rest.
3249 * otherwise, interrupt handler takes over from here.
3254 return 1; /* has next */
3258 * __atapi_pio_bytes - Transfer data from/to the ATAPI device.
3259 * @qc: Command on going
3260 * @bytes: number of bytes
3262 * Transfer Transfer data from/to the ATAPI device.
3265 * Inherited from caller.
3269 static void __atapi_pio_bytes(struct ata_queued_cmd *qc, unsigned int bytes)
3271 int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
3272 struct scatterlist *sg = qc->__sg;
3273 struct ata_port *ap = qc->ap;
3276 unsigned int offset, count;
3278 if (qc->curbytes + bytes >= qc->nbytes)
3279 ap->hsm_task_state = HSM_ST_LAST;
3282 if (unlikely(qc->cursg >= qc->n_elem)) {
3284 * The end of qc->sg is reached and the device expects
3285 * more data to transfer. In order not to overrun qc->sg
3286 * and fulfill length specified in the byte count register,
3287 * - for read case, discard trailing data from the device
3288 * - for write case, padding zero data to the device
3290 u16 pad_buf[1] = { 0 };
3291 unsigned int words = bytes >> 1;
3294 if (words) /* warning if bytes > 1 */
3295 printk(KERN_WARNING "ata%u: %u bytes trailing data\n",
3298 for (i = 0; i < words; i++)
3299 ata_data_xfer(ap, (unsigned char*)pad_buf, 2, do_write);
3301 ap->hsm_task_state = HSM_ST_LAST;
3305 sg = &qc->__sg[qc->cursg];
3308 offset = sg->offset + qc->cursg_ofs;
3310 /* get the current page and offset */
3311 page = nth_page(page, (offset >> PAGE_SHIFT));
3312 offset %= PAGE_SIZE;
3314 /* don't overrun current sg */
3315 count = min(sg->length - qc->cursg_ofs, bytes);
3317 /* don't cross page boundaries */
3318 count = min(count, (unsigned int)PAGE_SIZE - offset);
3320 DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
3322 if (PageHighMem(page)) {
3323 unsigned long flags;
3325 local_irq_save(flags);
3326 buf = kmap_atomic(page, KM_IRQ0);
3328 /* do the actual data transfer */
3329 ata_data_xfer(ap, buf + offset, count, do_write);
3331 kunmap_atomic(buf, KM_IRQ0);
3332 local_irq_restore(flags);
3334 buf = page_address(page);
3335 ata_data_xfer(ap, buf + offset, count, do_write);
3339 qc->curbytes += count;
3340 qc->cursg_ofs += count;
3342 if (qc->cursg_ofs == sg->length) {
3352 * atapi_pio_bytes - Transfer data from/to the ATAPI device.
3353 * @qc: Command on going
3355 * Transfer Transfer data from/to the ATAPI device.
3358 * Inherited from caller.
3361 static void atapi_pio_bytes(struct ata_queued_cmd *qc)
3363 struct ata_port *ap = qc->ap;
3364 struct ata_device *dev = qc->dev;
3365 unsigned int ireason, bc_lo, bc_hi, bytes;
3366 int i_write, do_write = (qc->tf.flags & ATA_TFLAG_WRITE) ? 1 : 0;
3368 ap->ops->tf_read(ap, &qc->tf);
3369 ireason = qc->tf.nsect;
3370 bc_lo = qc->tf.lbam;
3371 bc_hi = qc->tf.lbah;
3372 bytes = (bc_hi << 8) | bc_lo;
3374 /* shall be cleared to zero, indicating xfer of data */
3375 if (ireason & (1 << 0))
3378 /* make sure transfer direction matches expected */
3379 i_write = ((ireason & (1 << 1)) == 0) ? 1 : 0;
3380 if (do_write != i_write)
3383 VPRINTK("ata%u: xfering %d bytes\n", ap->id, bytes);
3385 __atapi_pio_bytes(qc, bytes);
3390 printk(KERN_INFO "ata%u: dev %u: ATAPI check failed\n",
3391 ap->id, dev->devno);
3392 qc->err_mask |= AC_ERR_ATA_BUS;
3393 ap->hsm_task_state = HSM_ST_ERR;
3397 * ata_pio_block - start PIO on a block
3398 * @ap: the target ata_port
3401 * None. (executing in kernel thread context)
3404 static void ata_pio_block(struct ata_port *ap)
3406 struct ata_queued_cmd *qc;
3410 * This is purely heuristic. This is a fast path.
3411 * Sometimes when we enter, BSY will be cleared in
3412 * a chk-status or two. If not, the drive is probably seeking
3413 * or something. Snooze for a couple msecs, then
3414 * chk-status again. If still busy, fall back to
3415 * HSM_ST_POLL state.
3417 status = ata_busy_wait(ap, ATA_BUSY, 5);
3418 if (status & ATA_BUSY) {
3420 status = ata_busy_wait(ap, ATA_BUSY, 10);
3421 if (status & ATA_BUSY) {
3422 ap->hsm_task_state = HSM_ST_POLL;
3423 ap->pio_task_timeout = jiffies + ATA_TMOUT_PIO;
3428 qc = ata_qc_from_tag(ap, ap->active_tag);
3432 if (status & (ATA_ERR | ATA_DF)) {
3433 qc->err_mask |= AC_ERR_DEV;
3434 ap->hsm_task_state = HSM_ST_ERR;
3438 /* transfer data if any */
3439 if (is_atapi_taskfile(&qc->tf)) {
3440 /* DRQ=0 means no more data to transfer */
3441 if ((status & ATA_DRQ) == 0) {
3442 ap->hsm_task_state = HSM_ST_LAST;
3446 atapi_pio_bytes(qc);
3448 /* handle BSY=0, DRQ=0 as error */
3449 if ((status & ATA_DRQ) == 0) {
3450 qc->err_mask |= AC_ERR_ATA_BUS;
3451 ap->hsm_task_state = HSM_ST_ERR;
3455 ata_pio_sectors(qc);
3458 ata_altstatus(ap); /* flush */
3461 static void ata_pio_error(struct ata_port *ap)
3463 struct ata_queued_cmd *qc;
3465 printk(KERN_WARNING "ata%u: PIO error\n", ap->id);
3467 qc = ata_qc_from_tag(ap, ap->active_tag);
3470 /* make sure qc->err_mask is available to
3471 * know what's wrong and recover
3473 assert(qc->err_mask);
3475 ap->hsm_task_state = HSM_ST_IDLE;
3477 ata_poll_qc_complete(qc);
3480 static void ata_pio_task(void *_data)
3482 struct ata_port *ap = _data;
3483 unsigned long timeout;
3490 switch (ap->hsm_task_state) {
3492 has_next = ata_pio_first_block(ap);
3500 has_next = ata_pio_complete(ap);
3504 case HSM_ST_LAST_POLL:
3505 timeout = ata_pio_poll(ap);
3519 queue_delayed_work(ata_wq, &ap->pio_task, timeout);
3525 * ata_qc_timeout - Handle timeout of queued command
3526 * @qc: Command that timed out
3528 * Some part of the kernel (currently, only the SCSI layer)
3529 * has noticed that the active command on port @ap has not
3530 * completed after a specified length of time. Handle this
3531 * condition by disabling DMA (if necessary) and completing
3532 * transactions, with error if necessary.
3534 * This also handles the case of the "lost interrupt", where
3535 * for some reason (possibly hardware bug, possibly driver bug)
3536 * an interrupt was not delivered to the driver, even though the
3537 * transaction completed successfully.
3540 * Inherited from SCSI layer (none, can sleep)
3543 static void ata_qc_timeout(struct ata_queued_cmd *qc)
3545 struct ata_port *ap = qc->ap;
3546 struct ata_host_set *host_set = ap->host_set;
3547 u8 host_stat = 0, drv_stat;
3548 unsigned long flags;
3552 spin_lock_irqsave(&host_set->lock, flags);
3554 /* hack alert! We cannot use the supplied completion
3555 * function from inside the ->eh_strategy_handler() thread.
3556 * libata is the only user of ->eh_strategy_handler() in
3557 * any kernel, so the default scsi_done() assumes it is
3558 * not being called from the SCSI EH.
3560 qc->scsidone = scsi_finish_command;
3562 switch (qc->tf.protocol) {
3565 case ATA_PROT_ATAPI_DMA:
3566 host_stat = ap->ops->bmdma_status(ap);
3568 /* before we do anything else, clear DMA-Start bit */
3569 ap->ops->bmdma_stop(qc);
3575 drv_stat = ata_chk_status(ap);
3577 /* ack bmdma irq events */
3578 ap->ops->irq_clear(ap);
3580 printk(KERN_ERR "ata%u: command 0x%x timeout, stat 0x%x host_stat 0x%x\n",
3581 ap->id, qc->tf.command, drv_stat, host_stat);
3583 ap->hsm_task_state = HSM_ST_IDLE;
3585 /* complete taskfile transaction */
3586 qc->err_mask |= ac_err_mask(drv_stat);
3587 ata_qc_complete(qc);
3591 spin_unlock_irqrestore(&host_set->lock, flags);
3597 * ata_eng_timeout - Handle timeout of queued command
3598 * @ap: Port on which timed-out command is active
3600 * Some part of the kernel (currently, only the SCSI layer)
3601 * has noticed that the active command on port @ap has not
3602 * completed after a specified length of time. Handle this
3603 * condition by disabling DMA (if necessary) and completing
3604 * transactions, with error if necessary.
3606 * This also handles the case of the "lost interrupt", where
3607 * for some reason (possibly hardware bug, possibly driver bug)
3608 * an interrupt was not delivered to the driver, even though the
3609 * transaction completed successfully.
3612 * Inherited from SCSI layer (none, can sleep)
3615 void ata_eng_timeout(struct ata_port *ap)
3617 struct ata_queued_cmd *qc;
3621 qc = ata_qc_from_tag(ap, ap->active_tag);
3625 printk(KERN_ERR "ata%u: BUG: timeout without command\n",
3635 * ata_qc_new - Request an available ATA command, for queueing
3636 * @ap: Port associated with device @dev
3637 * @dev: Device from whom we request an available command structure
3643 static struct ata_queued_cmd *ata_qc_new(struct ata_port *ap)
3645 struct ata_queued_cmd *qc = NULL;
3648 for (i = 0; i < ATA_MAX_QUEUE; i++)
3649 if (!test_and_set_bit(i, &ap->qactive)) {
3650 qc = ata_qc_from_tag(ap, i);
3661 * ata_qc_new_init - Request an available ATA command, and initialize it
3662 * @ap: Port associated with device @dev
3663 * @dev: Device from whom we request an available command structure
3669 struct ata_queued_cmd *ata_qc_new_init(struct ata_port *ap,
3670 struct ata_device *dev)
3672 struct ata_queued_cmd *qc;
3674 qc = ata_qc_new(ap);
3686 static void __ata_qc_complete(struct ata_queued_cmd *qc)
3688 struct ata_port *ap = qc->ap;
3693 if (likely(ata_tag_valid(tag))) {
3694 if (tag == ap->active_tag)
3695 ap->active_tag = ATA_TAG_POISON;
3696 qc->tag = ATA_TAG_POISON;
3697 clear_bit(tag, &ap->qactive);
3702 * ata_qc_free - free unused ata_queued_cmd
3703 * @qc: Command to complete
3705 * Designed to free unused ata_queued_cmd object
3706 * in case something prevents using it.
3709 * spin_lock_irqsave(host_set lock)
3711 void ata_qc_free(struct ata_queued_cmd *qc)
3713 assert(qc != NULL); /* ata_qc_from_tag _might_ return NULL */
3715 __ata_qc_complete(qc);
3719 * ata_qc_complete - Complete an active ATA command
3720 * @qc: Command to complete
3721 * @err_mask: ATA Status register contents
3723 * Indicate to the mid and upper layers that an ATA
3724 * command has completed, with either an ok or not-ok status.
3727 * spin_lock_irqsave(host_set lock)
3730 void ata_qc_complete(struct ata_queued_cmd *qc)
3734 assert(qc != NULL); /* ata_qc_from_tag _might_ return NULL */
3735 assert(qc->flags & ATA_QCFLAG_ACTIVE);
3737 if (likely(qc->flags & ATA_QCFLAG_DMAMAP))
3740 /* atapi: mark qc as inactive to prevent the interrupt handler
3741 * from completing the command twice later, before the error handler
3742 * is called. (when rc != 0 and atapi request sense is needed)
3744 qc->flags &= ~ATA_QCFLAG_ACTIVE;
3746 /* call completion callback */
3747 rc = qc->complete_fn(qc);
3749 /* if callback indicates not to complete command (non-zero),
3750 * return immediately
3755 __ata_qc_complete(qc);
3760 static inline int ata_should_dma_map(struct ata_queued_cmd *qc)
3762 struct ata_port *ap = qc->ap;
3764 switch (qc->tf.protocol) {
3766 case ATA_PROT_ATAPI_DMA:
3769 case ATA_PROT_ATAPI:
3771 case ATA_PROT_PIO_MULT:
3772 if (ap->flags & ATA_FLAG_PIO_DMA)
3785 * ata_qc_issue - issue taskfile to device
3786 * @qc: command to issue to device
3788 * Prepare an ATA command to submission to device.
3789 * This includes mapping the data into a DMA-able
3790 * area, filling in the S/G table, and finally
3791 * writing the taskfile to hardware, starting the command.
3794 * spin_lock_irqsave(host_set lock)
3797 * Zero on success, negative on error.
3800 int ata_qc_issue(struct ata_queued_cmd *qc)
3802 struct ata_port *ap = qc->ap;
3804 if (ata_should_dma_map(qc)) {
3805 if (qc->flags & ATA_QCFLAG_SG) {
3806 if (ata_sg_setup(qc))
3808 } else if (qc->flags & ATA_QCFLAG_SINGLE) {
3809 if (ata_sg_setup_one(qc))
3813 qc->flags &= ~ATA_QCFLAG_DMAMAP;
3816 ap->ops->qc_prep(qc);
3818 qc->ap->active_tag = qc->tag;
3819 qc->flags |= ATA_QCFLAG_ACTIVE;
3821 return ap->ops->qc_issue(qc);
3829 * ata_qc_issue_prot - issue taskfile to device in proto-dependent manner
3830 * @qc: command to issue to device
3832 * Using various libata functions and hooks, this function
3833 * starts an ATA command. ATA commands are grouped into
3834 * classes called "protocols", and issuing each type of protocol
3835 * is slightly different.
3837 * May be used as the qc_issue() entry in ata_port_operations.
3840 * spin_lock_irqsave(host_set lock)
3843 * Zero on success, negative on error.
3846 int ata_qc_issue_prot(struct ata_queued_cmd *qc)
3848 struct ata_port *ap = qc->ap;
3850 /* Use polling pio if the LLD doesn't handle
3851 * interrupt driven pio and atapi CDB interrupt.
3853 if (ap->flags & ATA_FLAG_PIO_POLLING) {
3854 switch (qc->tf.protocol) {
3856 case ATA_PROT_ATAPI:
3857 case ATA_PROT_ATAPI_NODATA:
3858 qc->tf.flags |= ATA_TFLAG_POLLING;
3860 case ATA_PROT_ATAPI_DMA:
3861 if (qc->dev->flags & ATA_DFLAG_CDB_INTR)
3869 /* select the device */
3870 ata_dev_select(ap, qc->dev->devno, 1, 0);
3872 /* start the command */
3873 switch (qc->tf.protocol) {
3874 case ATA_PROT_NODATA:
3875 if (qc->tf.flags & ATA_TFLAG_POLLING)
3876 ata_qc_set_polling(qc);
3878 ata_tf_to_host(ap, &qc->tf);
3879 ap->hsm_task_state = HSM_ST_LAST;
3881 if (qc->tf.flags & ATA_TFLAG_POLLING)
3882 queue_work(ata_wq, &ap->pio_task);
3887 assert(!(qc->tf.flags & ATA_TFLAG_POLLING));
3889 ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
3890 ap->ops->bmdma_setup(qc); /* set up bmdma */
3891 ap->ops->bmdma_start(qc); /* initiate bmdma */
3892 ap->hsm_task_state = HSM_ST_LAST;
3896 if (qc->tf.flags & ATA_TFLAG_POLLING)
3897 ata_qc_set_polling(qc);
3899 ata_tf_to_host(ap, &qc->tf);
3901 if (qc->tf.flags & ATA_TFLAG_WRITE) {
3902 /* PIO data out protocol */
3903 ap->hsm_task_state = HSM_ST_FIRST;
3904 queue_work(ata_wq, &ap->pio_task);
3906 /* always send first data block using
3907 * the ata_pio_task() codepath.
3910 /* PIO data in protocol */
3911 ap->hsm_task_state = HSM_ST;
3913 if (qc->tf.flags & ATA_TFLAG_POLLING)
3914 queue_work(ata_wq, &ap->pio_task);
3916 /* if polling, ata_pio_task() handles the rest.
3917 * otherwise, interrupt handler takes over from here.
3923 case ATA_PROT_ATAPI:
3924 case ATA_PROT_ATAPI_NODATA:
3925 if (qc->tf.flags & ATA_TFLAG_POLLING)
3926 ata_qc_set_polling(qc);
3928 ata_tf_to_host(ap, &qc->tf);
3929 ap->hsm_task_state = HSM_ST_FIRST;
3931 /* send cdb by polling if no cdb interrupt */
3932 if ((!(qc->dev->flags & ATA_DFLAG_CDB_INTR)) ||
3933 (qc->tf.flags & ATA_TFLAG_POLLING))
3934 queue_work(ata_wq, &ap->pio_task);
3937 case ATA_PROT_ATAPI_DMA:
3938 assert(!(qc->tf.flags & ATA_TFLAG_POLLING));
3940 ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
3941 ap->ops->bmdma_setup(qc); /* set up bmdma */
3942 ap->hsm_task_state = HSM_ST_FIRST;
3944 /* send cdb by polling if no cdb interrupt */
3945 if (!(qc->dev->flags & ATA_DFLAG_CDB_INTR))
3946 queue_work(ata_wq, &ap->pio_task);
3958 * ata_bmdma_setup_mmio - Set up PCI IDE BMDMA transaction
3959 * @qc: Info associated with this ATA transaction.
3962 * spin_lock_irqsave(host_set lock)
3965 static void ata_bmdma_setup_mmio (struct ata_queued_cmd *qc)
3967 struct ata_port *ap = qc->ap;
3968 unsigned int rw = (qc->tf.flags & ATA_TFLAG_WRITE);
3970 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
3972 /* load PRD table addr. */
3973 mb(); /* make sure PRD table writes are visible to controller */
3974 writel(ap->prd_dma, mmio + ATA_DMA_TABLE_OFS);
3976 /* specify data direction, triple-check start bit is clear */
3977 dmactl = readb(mmio + ATA_DMA_CMD);
3978 dmactl &= ~(ATA_DMA_WR | ATA_DMA_START);
3980 dmactl |= ATA_DMA_WR;
3981 writeb(dmactl, mmio + ATA_DMA_CMD);
3983 /* issue r/w command */
3984 ap->ops->exec_command(ap, &qc->tf);
3988 * ata_bmdma_start_mmio - Start a PCI IDE BMDMA transaction
3989 * @qc: Info associated with this ATA transaction.
3992 * spin_lock_irqsave(host_set lock)
3995 static void ata_bmdma_start_mmio (struct ata_queued_cmd *qc)
3997 struct ata_port *ap = qc->ap;
3998 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
4001 /* start host DMA transaction */
4002 dmactl = readb(mmio + ATA_DMA_CMD);
4003 writeb(dmactl | ATA_DMA_START, mmio + ATA_DMA_CMD);
4005 /* Strictly, one may wish to issue a readb() here, to
4006 * flush the mmio write. However, control also passes
4007 * to the hardware at this point, and it will interrupt
4008 * us when we are to resume control. So, in effect,
4009 * we don't care when the mmio write flushes.
4010 * Further, a read of the DMA status register _immediately_
4011 * following the write may not be what certain flaky hardware
4012 * is expected, so I think it is best to not add a readb()
4013 * without first all the MMIO ATA cards/mobos.
4014 * Or maybe I'm just being paranoid.
4019 * ata_bmdma_setup_pio - Set up PCI IDE BMDMA transaction (PIO)
4020 * @qc: Info associated with this ATA transaction.
4023 * spin_lock_irqsave(host_set lock)
4026 static void ata_bmdma_setup_pio (struct ata_queued_cmd *qc)
4028 struct ata_port *ap = qc->ap;
4029 unsigned int rw = (qc->tf.flags & ATA_TFLAG_WRITE);
4032 /* load PRD table addr. */
4033 outl(ap->prd_dma, ap->ioaddr.bmdma_addr + ATA_DMA_TABLE_OFS);
4035 /* specify data direction, triple-check start bit is clear */
4036 dmactl = inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
4037 dmactl &= ~(ATA_DMA_WR | ATA_DMA_START);
4039 dmactl |= ATA_DMA_WR;
4040 outb(dmactl, ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
4042 /* issue r/w command */
4043 ap->ops->exec_command(ap, &qc->tf);
4047 * ata_bmdma_start_pio - Start a PCI IDE BMDMA transaction (PIO)
4048 * @qc: Info associated with this ATA transaction.
4051 * spin_lock_irqsave(host_set lock)
4054 static void ata_bmdma_start_pio (struct ata_queued_cmd *qc)
4056 struct ata_port *ap = qc->ap;
4059 /* start host DMA transaction */
4060 dmactl = inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
4061 outb(dmactl | ATA_DMA_START,
4062 ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
4067 * ata_bmdma_start - Start a PCI IDE BMDMA transaction
4068 * @qc: Info associated with this ATA transaction.
4070 * Writes the ATA_DMA_START flag to the DMA command register.
4072 * May be used as the bmdma_start() entry in ata_port_operations.
4075 * spin_lock_irqsave(host_set lock)
4077 void ata_bmdma_start(struct ata_queued_cmd *qc)
4079 if (qc->ap->flags & ATA_FLAG_MMIO)
4080 ata_bmdma_start_mmio(qc);
4082 ata_bmdma_start_pio(qc);
4087 * ata_bmdma_setup - Set up PCI IDE BMDMA transaction
4088 * @qc: Info associated with this ATA transaction.
4090 * Writes address of PRD table to device's PRD Table Address
4091 * register, sets the DMA control register, and calls
4092 * ops->exec_command() to start the transfer.
4094 * May be used as the bmdma_setup() entry in ata_port_operations.
4097 * spin_lock_irqsave(host_set lock)
4099 void ata_bmdma_setup(struct ata_queued_cmd *qc)
4101 if (qc->ap->flags & ATA_FLAG_MMIO)
4102 ata_bmdma_setup_mmio(qc);
4104 ata_bmdma_setup_pio(qc);
4109 * ata_bmdma_irq_clear - Clear PCI IDE BMDMA interrupt.
4110 * @ap: Port associated with this ATA transaction.
4112 * Clear interrupt and error flags in DMA status register.
4114 * May be used as the irq_clear() entry in ata_port_operations.
4117 * spin_lock_irqsave(host_set lock)
4120 void ata_bmdma_irq_clear(struct ata_port *ap)
4122 if (ap->flags & ATA_FLAG_MMIO) {
4123 void __iomem *mmio = ((void __iomem *) ap->ioaddr.bmdma_addr) + ATA_DMA_STATUS;
4124 writeb(readb(mmio), mmio);
4126 unsigned long addr = ap->ioaddr.bmdma_addr + ATA_DMA_STATUS;
4127 outb(inb(addr), addr);
4134 * ata_bmdma_status - Read PCI IDE BMDMA status
4135 * @ap: Port associated with this ATA transaction.
4137 * Read and return BMDMA status register.
4139 * May be used as the bmdma_status() entry in ata_port_operations.
4142 * spin_lock_irqsave(host_set lock)
4145 u8 ata_bmdma_status(struct ata_port *ap)
4148 if (ap->flags & ATA_FLAG_MMIO) {
4149 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
4150 host_stat = readb(mmio + ATA_DMA_STATUS);
4152 host_stat = inb(ap->ioaddr.bmdma_addr + ATA_DMA_STATUS);
4158 * ata_bmdma_stop - Stop PCI IDE BMDMA transfer
4159 * @qc: Command we are ending DMA for
4161 * Clears the ATA_DMA_START flag in the dma control register
4163 * May be used as the bmdma_stop() entry in ata_port_operations.
4166 * spin_lock_irqsave(host_set lock)
4169 void ata_bmdma_stop(struct ata_queued_cmd *qc)
4171 struct ata_port *ap = qc->ap;
4172 if (ap->flags & ATA_FLAG_MMIO) {
4173 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
4175 /* clear start/stop bit */
4176 writeb(readb(mmio + ATA_DMA_CMD) & ~ATA_DMA_START,
4177 mmio + ATA_DMA_CMD);
4179 /* clear start/stop bit */
4180 outb(inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD) & ~ATA_DMA_START,
4181 ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
4184 /* one-PIO-cycle guaranteed wait, per spec, for HDMA1:0 transition */
4185 ata_altstatus(ap); /* dummy read */
4189 * ata_host_intr - Handle host interrupt for given (port, task)
4190 * @ap: Port on which interrupt arrived (possibly...)
4191 * @qc: Taskfile currently active in engine
4193 * Handle host interrupt for given queued command. Currently,
4194 * only DMA interrupts are handled. All other commands are
4195 * handled via polling with interrupts disabled (nIEN bit).
4198 * spin_lock_irqsave(host_set lock)
4201 * One if interrupt was handled, zero if not (shared irq).
4204 inline unsigned int ata_host_intr (struct ata_port *ap,
4205 struct ata_queued_cmd *qc)
4207 u8 status, host_stat = 0;
4209 VPRINTK("ata%u: protocol %d task_state %d\n",
4210 ap->id, qc->tf.protocol, ap->hsm_task_state);
4212 /* Check whether we are expecting interrupt in this state */
4213 switch (ap->hsm_task_state) {
4215 /* Check the ATA_DFLAG_CDB_INTR flag is enough here.
4216 * The flag was turned on only for atapi devices.
4217 * No need to check is_atapi_taskfile(&qc->tf) again.
4219 if (!(qc->dev->flags & ATA_DFLAG_CDB_INTR))
4223 if (qc->tf.protocol == ATA_PROT_DMA ||
4224 qc->tf.protocol == ATA_PROT_ATAPI_DMA) {
4225 /* check status of DMA engine */
4226 host_stat = ap->ops->bmdma_status(ap);
4227 VPRINTK("ata%u: host_stat 0x%X\n", ap->id, host_stat);
4229 /* if it's not our irq... */
4230 if (!(host_stat & ATA_DMA_INTR))
4233 /* before we do anything else, clear DMA-Start bit */
4234 ap->ops->bmdma_stop(qc);
4243 /* check altstatus */
4244 status = ata_altstatus(ap);
4245 if (status & ATA_BUSY)
4248 /* check main status, clearing INTRQ */
4249 status = ata_chk_status(ap);
4250 if (unlikely(status & ATA_BUSY))
4253 DPRINTK("ata%u: protocol %d task_state %d (dev_stat 0x%X)\n",
4254 ap->id, qc->tf.protocol, ap->hsm_task_state, status);
4256 /* ack bmdma irq events */
4257 ap->ops->irq_clear(ap);
4260 if (unlikely((status & ATA_ERR) || (host_stat & ATA_DMA_ERR)))
4261 ap->hsm_task_state = HSM_ST_ERR;
4264 switch (ap->hsm_task_state) {
4266 /* Some pre-ATAPI-4 devices assert INTRQ
4267 * at this state when ready to receive CDB.
4270 /* check device status */
4271 if (unlikely((status & (ATA_BUSY | ATA_DRQ)) != ATA_DRQ)) {
4272 /* Wrong status. Let EH handle this */
4273 ap->hsm_task_state = HSM_ST_ERR;
4277 atapi_send_cdb(ap, qc);
4282 /* complete command or read/write the data register */
4283 if (qc->tf.protocol == ATA_PROT_ATAPI) {
4284 /* ATAPI PIO protocol */
4285 if ((status & ATA_DRQ) == 0) {
4286 /* no more data to transfer */
4287 ap->hsm_task_state = HSM_ST_LAST;
4291 atapi_pio_bytes(qc);
4293 if (unlikely(ap->hsm_task_state == HSM_ST_ERR))
4294 /* bad ireason reported by device */
4298 /* ATA PIO protocol */
4299 if (unlikely((status & ATA_DRQ) == 0)) {
4300 /* handle BSY=0, DRQ=0 as error */
4301 ap->hsm_task_state = HSM_ST_ERR;
4305 ata_pio_sectors(qc);
4307 if (ap->hsm_task_state == HSM_ST_LAST &&
4308 (!(qc->tf.flags & ATA_TFLAG_WRITE))) {
4311 status = ata_chk_status(ap);
4316 ata_altstatus(ap); /* flush */
4320 if (unlikely(status & ATA_DRQ)) {
4321 /* handle DRQ=1 as error */
4322 ap->hsm_task_state = HSM_ST_ERR;
4326 /* no more data to transfer */
4327 DPRINTK("ata%u: command complete, drv_stat 0x%x\n",
4330 ap->hsm_task_state = HSM_ST_IDLE;
4332 /* complete taskfile transaction */
4333 qc->err_mask |= ac_err_mask(status);
4334 ata_qc_complete(qc);
4338 printk(KERN_ERR "ata%u: command error, drv_stat 0x%x host_stat 0x%x\n",
4339 ap->id, status, host_stat);
4341 ap->hsm_task_state = HSM_ST_IDLE;
4342 qc->err_mask |= __ac_err_mask(status);
4343 ata_qc_complete(qc);
4349 return 1; /* irq handled */
4352 ap->stats.idle_irq++;
4355 if ((ap->stats.idle_irq % 1000) == 0) {
4357 ata_irq_ack(ap, 0); /* debug trap */
4358 printk(KERN_WARNING "ata%d: irq trap\n", ap->id);
4361 return 0; /* irq not handled */
4365 * ata_interrupt - Default ATA host interrupt handler
4366 * @irq: irq line (unused)
4367 * @dev_instance: pointer to our ata_host_set information structure
4370 * Default interrupt handler for PCI IDE devices. Calls
4371 * ata_host_intr() for each port that is not disabled.
4374 * Obtains host_set lock during operation.
4377 * IRQ_NONE or IRQ_HANDLED.
4380 irqreturn_t ata_interrupt (int irq, void *dev_instance, struct pt_regs *regs)
4382 struct ata_host_set *host_set = dev_instance;
4384 unsigned int handled = 0;
4385 unsigned long flags;
4387 /* TODO: make _irqsave conditional on x86 PCI IDE legacy mode */
4388 spin_lock_irqsave(&host_set->lock, flags);
4390 for (i = 0; i < host_set->n_ports; i++) {
4391 struct ata_port *ap;
4393 ap = host_set->ports[i];
4395 !(ap->flags & ATA_FLAG_PORT_DISABLED)) {
4396 struct ata_queued_cmd *qc;
4398 qc = ata_qc_from_tag(ap, ap->active_tag);
4399 if (qc && (!(qc->tf.flags & ATA_TFLAG_POLLING)) &&
4400 (qc->flags & ATA_QCFLAG_ACTIVE))
4401 handled |= ata_host_intr(ap, qc);
4405 spin_unlock_irqrestore(&host_set->lock, flags);
4407 return IRQ_RETVAL(handled);
4411 * ata_port_start - Set port up for dma.
4412 * @ap: Port to initialize
4414 * Called just after data structures for each port are
4415 * initialized. Allocates space for PRD table.
4417 * May be used as the port_start() entry in ata_port_operations.
4420 * Inherited from caller.
4423 int ata_port_start (struct ata_port *ap)
4425 struct device *dev = ap->host_set->dev;
4428 ap->prd = dma_alloc_coherent(dev, ATA_PRD_TBL_SZ, &ap->prd_dma, GFP_KERNEL);
4432 rc = ata_pad_alloc(ap, dev);
4434 dma_free_coherent(dev, ATA_PRD_TBL_SZ, ap->prd, ap->prd_dma);
4438 DPRINTK("prd alloc, virt %p, dma %llx\n", ap->prd, (unsigned long long) ap->prd_dma);
4445 * ata_port_stop - Undo ata_port_start()
4446 * @ap: Port to shut down
4448 * Frees the PRD table.
4450 * May be used as the port_stop() entry in ata_port_operations.
4453 * Inherited from caller.
4456 void ata_port_stop (struct ata_port *ap)
4458 struct device *dev = ap->host_set->dev;
4460 dma_free_coherent(dev, ATA_PRD_TBL_SZ, ap->prd, ap->prd_dma);
4461 ata_pad_free(ap, dev);
4464 void ata_host_stop (struct ata_host_set *host_set)
4466 if (host_set->mmio_base)
4467 iounmap(host_set->mmio_base);
4472 * ata_host_remove - Unregister SCSI host structure with upper layers
4473 * @ap: Port to unregister
4474 * @do_unregister: 1 if we fully unregister, 0 to just stop the port
4477 * Inherited from caller.
4480 static void ata_host_remove(struct ata_port *ap, unsigned int do_unregister)
4482 struct Scsi_Host *sh = ap->host;
4487 scsi_remove_host(sh);
4489 ap->ops->port_stop(ap);
4493 * ata_host_init - Initialize an ata_port structure
4494 * @ap: Structure to initialize
4495 * @host: associated SCSI mid-layer structure
4496 * @host_set: Collection of hosts to which @ap belongs
4497 * @ent: Probe information provided by low-level driver
4498 * @port_no: Port number associated with this ata_port
4500 * Initialize a new ata_port structure, and its associated
4504 * Inherited from caller.
4507 static void ata_host_init(struct ata_port *ap, struct Scsi_Host *host,
4508 struct ata_host_set *host_set,
4509 const struct ata_probe_ent *ent, unsigned int port_no)
4515 host->max_channel = 1;
4516 host->unique_id = ata_unique_id++;
4517 host->max_cmd_len = 12;
4519 ap->flags = ATA_FLAG_PORT_DISABLED;
4520 ap->id = host->unique_id;
4522 ap->ctl = ATA_DEVCTL_OBS;
4523 ap->host_set = host_set;
4524 ap->port_no = port_no;
4526 ent->legacy_mode ? ent->hard_port_no : port_no;
4527 ap->pio_mask = ent->pio_mask;
4528 ap->mwdma_mask = ent->mwdma_mask;
4529 ap->udma_mask = ent->udma_mask;
4530 ap->flags |= ent->host_flags;
4531 ap->ops = ent->port_ops;
4532 ap->cbl = ATA_CBL_NONE;
4533 ap->active_tag = ATA_TAG_POISON;
4534 ap->last_ctl = 0xFF;
4536 INIT_WORK(&ap->pio_task, ata_pio_task, ap);
4538 for (i = 0; i < ATA_MAX_DEVICES; i++)
4539 ap->device[i].devno = i;
4542 ap->stats.unhandled_irq = 1;
4543 ap->stats.idle_irq = 1;
4546 memcpy(&ap->ioaddr, &ent->port[port_no], sizeof(struct ata_ioports));
4550 * ata_host_add - Attach low-level ATA driver to system
4551 * @ent: Information provided by low-level driver
4552 * @host_set: Collections of ports to which we add
4553 * @port_no: Port number associated with this host
4555 * Attach low-level ATA driver to system.
4558 * PCI/etc. bus probe sem.
4561 * New ata_port on success, for NULL on error.
4564 static struct ata_port * ata_host_add(const struct ata_probe_ent *ent,
4565 struct ata_host_set *host_set,
4566 unsigned int port_no)
4568 struct Scsi_Host *host;
4569 struct ata_port *ap;
4573 host = scsi_host_alloc(ent->sht, sizeof(struct ata_port));
4577 ap = (struct ata_port *) &host->hostdata[0];
4579 ata_host_init(ap, host, host_set, ent, port_no);
4581 rc = ap->ops->port_start(ap);
4588 scsi_host_put(host);
4593 * ata_device_add - Register hardware device with ATA and SCSI layers
4594 * @ent: Probe information describing hardware device to be registered
4596 * This function processes the information provided in the probe
4597 * information struct @ent, allocates the necessary ATA and SCSI
4598 * host information structures, initializes them, and registers
4599 * everything with requisite kernel subsystems.
4601 * This function requests irqs, probes the ATA bus, and probes
4605 * PCI/etc. bus probe sem.
4608 * Number of ports registered. Zero on error (no ports registered).
4611 int ata_device_add(const struct ata_probe_ent *ent)
4613 unsigned int count = 0, i;
4614 struct device *dev = ent->dev;
4615 struct ata_host_set *host_set;
4618 /* alloc a container for our list of ATA ports (buses) */
4619 host_set = kzalloc(sizeof(struct ata_host_set) +
4620 (ent->n_ports * sizeof(void *)), GFP_KERNEL);
4623 spin_lock_init(&host_set->lock);
4625 host_set->dev = dev;
4626 host_set->n_ports = ent->n_ports;
4627 host_set->irq = ent->irq;
4628 host_set->mmio_base = ent->mmio_base;
4629 host_set->private_data = ent->private_data;
4630 host_set->ops = ent->port_ops;
4632 /* register each port bound to this device */
4633 for (i = 0; i < ent->n_ports; i++) {
4634 struct ata_port *ap;
4635 unsigned long xfer_mode_mask;
4637 ap = ata_host_add(ent, host_set, i);
4641 host_set->ports[i] = ap;
4642 xfer_mode_mask =(ap->udma_mask << ATA_SHIFT_UDMA) |
4643 (ap->mwdma_mask << ATA_SHIFT_MWDMA) |
4644 (ap->pio_mask << ATA_SHIFT_PIO);
4646 /* print per-port info to dmesg */
4647 printk(KERN_INFO "ata%u: %cATA max %s cmd 0x%lX ctl 0x%lX "
4648 "bmdma 0x%lX irq %lu\n",
4650 ap->flags & ATA_FLAG_SATA ? 'S' : 'P',
4651 ata_mode_string(xfer_mode_mask),
4652 ap->ioaddr.cmd_addr,
4653 ap->ioaddr.ctl_addr,
4654 ap->ioaddr.bmdma_addr,
4658 host_set->ops->irq_clear(ap);
4665 /* obtain irq, that is shared between channels */
4666 if (request_irq(ent->irq, ent->port_ops->irq_handler, ent->irq_flags,
4667 DRV_NAME, host_set))
4670 /* perform each probe synchronously */
4671 DPRINTK("probe begin\n");
4672 for (i = 0; i < count; i++) {
4673 struct ata_port *ap;
4676 ap = host_set->ports[i];
4678 DPRINTK("ata%u: probe begin\n", ap->id);
4679 rc = ata_bus_probe(ap);
4680 DPRINTK("ata%u: probe end\n", ap->id);
4683 /* FIXME: do something useful here?
4684 * Current libata behavior will
4685 * tear down everything when
4686 * the module is removed
4687 * or the h/w is unplugged.
4691 rc = scsi_add_host(ap->host, dev);
4693 printk(KERN_ERR "ata%u: scsi_add_host failed\n",
4695 /* FIXME: do something useful here */
4696 /* FIXME: handle unconditional calls to
4697 * scsi_scan_host and ata_host_remove, below,
4703 /* probes are done, now scan each port's disk(s) */
4704 DPRINTK("probe begin\n");
4705 for (i = 0; i < count; i++) {
4706 struct ata_port *ap = host_set->ports[i];
4708 ata_scsi_scan_host(ap);
4711 dev_set_drvdata(dev, host_set);
4713 VPRINTK("EXIT, returning %u\n", ent->n_ports);
4714 return ent->n_ports; /* success */
4717 for (i = 0; i < count; i++) {
4718 ata_host_remove(host_set->ports[i], 1);
4719 scsi_host_put(host_set->ports[i]->host);
4723 VPRINTK("EXIT, returning 0\n");
4728 * ata_host_set_remove - PCI layer callback for device removal
4729 * @host_set: ATA host set that was removed
4731 * Unregister all objects associated with this host set. Free those
4735 * Inherited from calling layer (may sleep).
4738 void ata_host_set_remove(struct ata_host_set *host_set)
4740 struct ata_port *ap;
4743 for (i = 0; i < host_set->n_ports; i++) {
4744 ap = host_set->ports[i];
4745 scsi_remove_host(ap->host);
4748 free_irq(host_set->irq, host_set);
4750 for (i = 0; i < host_set->n_ports; i++) {
4751 ap = host_set->ports[i];
4753 ata_scsi_release(ap->host);
4755 if ((ap->flags & ATA_FLAG_NO_LEGACY) == 0) {
4756 struct ata_ioports *ioaddr = &ap->ioaddr;
4758 if (ioaddr->cmd_addr == 0x1f0)
4759 release_region(0x1f0, 8);
4760 else if (ioaddr->cmd_addr == 0x170)
4761 release_region(0x170, 8);
4764 scsi_host_put(ap->host);
4767 if (host_set->ops->host_stop)
4768 host_set->ops->host_stop(host_set);
4774 * ata_scsi_release - SCSI layer callback hook for host unload
4775 * @host: libata host to be unloaded
4777 * Performs all duties necessary to shut down a libata port...
4778 * Kill port kthread, disable port, and release resources.
4781 * Inherited from SCSI layer.
4787 int ata_scsi_release(struct Scsi_Host *host)
4789 struct ata_port *ap = (struct ata_port *) &host->hostdata[0];
4793 ap->ops->port_disable(ap);
4794 ata_host_remove(ap, 0);
4801 * ata_std_ports - initialize ioaddr with standard port offsets.
4802 * @ioaddr: IO address structure to be initialized
4804 * Utility function which initializes data_addr, error_addr,
4805 * feature_addr, nsect_addr, lbal_addr, lbam_addr, lbah_addr,
4806 * device_addr, status_addr, and command_addr to standard offsets
4807 * relative to cmd_addr.
4809 * Does not set ctl_addr, altstatus_addr, bmdma_addr, or scr_addr.
4812 void ata_std_ports(struct ata_ioports *ioaddr)
4814 ioaddr->data_addr = ioaddr->cmd_addr + ATA_REG_DATA;
4815 ioaddr->error_addr = ioaddr->cmd_addr + ATA_REG_ERR;
4816 ioaddr->feature_addr = ioaddr->cmd_addr + ATA_REG_FEATURE;
4817 ioaddr->nsect_addr = ioaddr->cmd_addr + ATA_REG_NSECT;
4818 ioaddr->lbal_addr = ioaddr->cmd_addr + ATA_REG_LBAL;
4819 ioaddr->lbam_addr = ioaddr->cmd_addr + ATA_REG_LBAM;
4820 ioaddr->lbah_addr = ioaddr->cmd_addr + ATA_REG_LBAH;
4821 ioaddr->device_addr = ioaddr->cmd_addr + ATA_REG_DEVICE;
4822 ioaddr->status_addr = ioaddr->cmd_addr + ATA_REG_STATUS;
4823 ioaddr->command_addr = ioaddr->cmd_addr + ATA_REG_CMD;
4826 static struct ata_probe_ent *
4827 ata_probe_ent_alloc(struct device *dev, const struct ata_port_info *port)
4829 struct ata_probe_ent *probe_ent;
4831 probe_ent = kzalloc(sizeof(*probe_ent), GFP_KERNEL);
4833 printk(KERN_ERR DRV_NAME "(%s): out of memory\n",
4834 kobject_name(&(dev->kobj)));
4838 INIT_LIST_HEAD(&probe_ent->node);
4839 probe_ent->dev = dev;
4841 probe_ent->sht = port->sht;
4842 probe_ent->host_flags = port->host_flags;
4843 probe_ent->pio_mask = port->pio_mask;
4844 probe_ent->mwdma_mask = port->mwdma_mask;
4845 probe_ent->udma_mask = port->udma_mask;
4846 probe_ent->port_ops = port->port_ops;
4855 void ata_pci_host_stop (struct ata_host_set *host_set)
4857 struct pci_dev *pdev = to_pci_dev(host_set->dev);
4859 pci_iounmap(pdev, host_set->mmio_base);
4863 * ata_pci_init_native_mode - Initialize native-mode driver
4864 * @pdev: pci device to be initialized
4865 * @port: array[2] of pointers to port info structures.
4866 * @ports: bitmap of ports present
4868 * Utility function which allocates and initializes an
4869 * ata_probe_ent structure for a standard dual-port
4870 * PIO-based IDE controller. The returned ata_probe_ent
4871 * structure can be passed to ata_device_add(). The returned
4872 * ata_probe_ent structure should then be freed with kfree().
4874 * The caller need only pass the address of the primary port, the
4875 * secondary will be deduced automatically. If the device has non
4876 * standard secondary port mappings this function can be called twice,
4877 * once for each interface.
4880 struct ata_probe_ent *
4881 ata_pci_init_native_mode(struct pci_dev *pdev, struct ata_port_info **port, int ports)
4883 struct ata_probe_ent *probe_ent =
4884 ata_probe_ent_alloc(pci_dev_to_dev(pdev), port[0]);
4890 probe_ent->irq = pdev->irq;
4891 probe_ent->irq_flags = SA_SHIRQ;
4892 probe_ent->private_data = port[0]->private_data;
4894 if (ports & ATA_PORT_PRIMARY) {
4895 probe_ent->port[p].cmd_addr = pci_resource_start(pdev, 0);
4896 probe_ent->port[p].altstatus_addr =
4897 probe_ent->port[p].ctl_addr =
4898 pci_resource_start(pdev, 1) | ATA_PCI_CTL_OFS;
4899 probe_ent->port[p].bmdma_addr = pci_resource_start(pdev, 4);
4900 ata_std_ports(&probe_ent->port[p]);
4904 if (ports & ATA_PORT_SECONDARY) {
4905 probe_ent->port[p].cmd_addr = pci_resource_start(pdev, 2);
4906 probe_ent->port[p].altstatus_addr =
4907 probe_ent->port[p].ctl_addr =
4908 pci_resource_start(pdev, 3) | ATA_PCI_CTL_OFS;
4909 probe_ent->port[p].bmdma_addr = pci_resource_start(pdev, 4) + 8;
4910 ata_std_ports(&probe_ent->port[p]);
4914 probe_ent->n_ports = p;
4918 static struct ata_probe_ent *ata_pci_init_legacy_port(struct pci_dev *pdev, struct ata_port_info *port, int port_num)
4920 struct ata_probe_ent *probe_ent;
4922 probe_ent = ata_probe_ent_alloc(pci_dev_to_dev(pdev), port);
4926 probe_ent->legacy_mode = 1;
4927 probe_ent->n_ports = 1;
4928 probe_ent->hard_port_no = port_num;
4929 probe_ent->private_data = port->private_data;
4934 probe_ent->irq = 14;
4935 probe_ent->port[0].cmd_addr = 0x1f0;
4936 probe_ent->port[0].altstatus_addr =
4937 probe_ent->port[0].ctl_addr = 0x3f6;
4940 probe_ent->irq = 15;
4941 probe_ent->port[0].cmd_addr = 0x170;
4942 probe_ent->port[0].altstatus_addr =
4943 probe_ent->port[0].ctl_addr = 0x376;
4946 probe_ent->port[0].bmdma_addr = pci_resource_start(pdev, 4) + 8 * port_num;
4947 ata_std_ports(&probe_ent->port[0]);
4952 * ata_pci_init_one - Initialize/register PCI IDE host controller
4953 * @pdev: Controller to be initialized
4954 * @port_info: Information from low-level host driver
4955 * @n_ports: Number of ports attached to host controller
4957 * This is a helper function which can be called from a driver's
4958 * xxx_init_one() probe function if the hardware uses traditional
4959 * IDE taskfile registers.
4961 * This function calls pci_enable_device(), reserves its register
4962 * regions, sets the dma mask, enables bus master mode, and calls
4966 * Inherited from PCI layer (may sleep).
4969 * Zero on success, negative on errno-based value on error.
4972 int ata_pci_init_one (struct pci_dev *pdev, struct ata_port_info **port_info,
4973 unsigned int n_ports)
4975 struct ata_probe_ent *probe_ent = NULL, *probe_ent2 = NULL;
4976 struct ata_port_info *port[2];
4978 unsigned int legacy_mode = 0;
4979 int disable_dev_on_err = 1;
4984 port[0] = port_info[0];
4986 port[1] = port_info[1];
4990 if ((port[0]->host_flags & ATA_FLAG_NO_LEGACY) == 0
4991 && (pdev->class >> 8) == PCI_CLASS_STORAGE_IDE) {
4992 /* TODO: What if one channel is in native mode ... */
4993 pci_read_config_byte(pdev, PCI_CLASS_PROG, &tmp8);
4994 mask = (1 << 2) | (1 << 0);
4995 if ((tmp8 & mask) != mask)
4996 legacy_mode = (1 << 3);
5000 if ((!legacy_mode) && (n_ports > 2)) {
5001 printk(KERN_ERR "ata: BUG: native mode, n_ports > 2\n");
5006 /* FIXME: Really for ATA it isn't safe because the device may be
5007 multi-purpose and we want to leave it alone if it was already
5008 enabled. Secondly for shared use as Arjan says we want refcounting
5010 Checking dev->is_enabled is insufficient as this is not set at
5011 boot for the primary video which is BIOS enabled
5014 rc = pci_enable_device(pdev);
5018 rc = pci_request_regions(pdev, DRV_NAME);
5020 disable_dev_on_err = 0;
5024 /* FIXME: Should use platform specific mappers for legacy port ranges */
5026 if (!request_region(0x1f0, 8, "libata")) {
5027 struct resource *conflict, res;
5029 res.end = 0x1f0 + 8 - 1;
5030 conflict = ____request_resource(&ioport_resource, &res);
5031 if (!strcmp(conflict->name, "libata"))
5032 legacy_mode |= (1 << 0);
5034 disable_dev_on_err = 0;
5035 printk(KERN_WARNING "ata: 0x1f0 IDE port busy\n");
5038 legacy_mode |= (1 << 0);
5040 if (!request_region(0x170, 8, "libata")) {
5041 struct resource *conflict, res;
5043 res.end = 0x170 + 8 - 1;
5044 conflict = ____request_resource(&ioport_resource, &res);
5045 if (!strcmp(conflict->name, "libata"))
5046 legacy_mode |= (1 << 1);
5048 disable_dev_on_err = 0;
5049 printk(KERN_WARNING "ata: 0x170 IDE port busy\n");
5052 legacy_mode |= (1 << 1);
5055 /* we have legacy mode, but all ports are unavailable */
5056 if (legacy_mode == (1 << 3)) {
5058 goto err_out_regions;
5061 rc = pci_set_dma_mask(pdev, ATA_DMA_MASK);
5063 goto err_out_regions;
5064 rc = pci_set_consistent_dma_mask(pdev, ATA_DMA_MASK);
5066 goto err_out_regions;
5069 if (legacy_mode & (1 << 0))
5070 probe_ent = ata_pci_init_legacy_port(pdev, port[0], 0);
5071 if (legacy_mode & (1 << 1))
5072 probe_ent2 = ata_pci_init_legacy_port(pdev, port[1], 1);
5075 probe_ent = ata_pci_init_native_mode(pdev, port, ATA_PORT_PRIMARY | ATA_PORT_SECONDARY);
5077 probe_ent = ata_pci_init_native_mode(pdev, port, ATA_PORT_PRIMARY);
5079 if (!probe_ent && !probe_ent2) {
5081 goto err_out_regions;
5084 pci_set_master(pdev);
5086 /* FIXME: check ata_device_add return */
5088 if (legacy_mode & (1 << 0))
5089 ata_device_add(probe_ent);
5090 if (legacy_mode & (1 << 1))
5091 ata_device_add(probe_ent2);
5093 ata_device_add(probe_ent);
5101 if (legacy_mode & (1 << 0))
5102 release_region(0x1f0, 8);
5103 if (legacy_mode & (1 << 1))
5104 release_region(0x170, 8);
5105 pci_release_regions(pdev);
5107 if (disable_dev_on_err)
5108 pci_disable_device(pdev);
5113 * ata_pci_remove_one - PCI layer callback for device removal
5114 * @pdev: PCI device that was removed
5116 * PCI layer indicates to libata via this hook that
5117 * hot-unplug or module unload event has occurred.
5118 * Handle this by unregistering all objects associated
5119 * with this PCI device. Free those objects. Then finally
5120 * release PCI resources and disable device.
5123 * Inherited from PCI layer (may sleep).
5126 void ata_pci_remove_one (struct pci_dev *pdev)
5128 struct device *dev = pci_dev_to_dev(pdev);
5129 struct ata_host_set *host_set = dev_get_drvdata(dev);
5131 ata_host_set_remove(host_set);
5132 pci_release_regions(pdev);
5133 pci_disable_device(pdev);
5134 dev_set_drvdata(dev, NULL);
5137 /* move to PCI subsystem */
5138 int pci_test_config_bits(struct pci_dev *pdev, const struct pci_bits *bits)
5140 unsigned long tmp = 0;
5142 switch (bits->width) {
5145 pci_read_config_byte(pdev, bits->reg, &tmp8);
5151 pci_read_config_word(pdev, bits->reg, &tmp16);
5157 pci_read_config_dword(pdev, bits->reg, &tmp32);
5168 return (tmp == bits->val) ? 1 : 0;
5170 #endif /* CONFIG_PCI */
5173 static int __init ata_init(void)
5175 ata_wq = create_workqueue("ata");
5179 printk(KERN_DEBUG "libata version " DRV_VERSION " loaded.\n");
5183 static void __exit ata_exit(void)
5185 destroy_workqueue(ata_wq);
5188 module_init(ata_init);
5189 module_exit(ata_exit);
5191 static unsigned long ratelimit_time;
5192 static spinlock_t ata_ratelimit_lock = SPIN_LOCK_UNLOCKED;
5194 int ata_ratelimit(void)
5197 unsigned long flags;
5199 spin_lock_irqsave(&ata_ratelimit_lock, flags);
5201 if (time_after(jiffies, ratelimit_time)) {
5203 ratelimit_time = jiffies + (HZ/5);
5207 spin_unlock_irqrestore(&ata_ratelimit_lock, flags);
5213 * libata is essentially a library of internal helper functions for
5214 * low-level ATA host controller drivers. As such, the API/ABI is
5215 * likely to change as new drivers are added and updated.
5216 * Do not depend on ABI/API stability.
5219 EXPORT_SYMBOL_GPL(ata_std_bios_param);
5220 EXPORT_SYMBOL_GPL(ata_std_ports);
5221 EXPORT_SYMBOL_GPL(ata_device_add);
5222 EXPORT_SYMBOL_GPL(ata_host_set_remove);
5223 EXPORT_SYMBOL_GPL(ata_sg_init);
5224 EXPORT_SYMBOL_GPL(ata_sg_init_one);
5225 EXPORT_SYMBOL_GPL(ata_qc_complete);
5226 EXPORT_SYMBOL_GPL(ata_qc_issue_prot);
5227 EXPORT_SYMBOL_GPL(ata_eng_timeout);
5228 EXPORT_SYMBOL_GPL(ata_tf_load);
5229 EXPORT_SYMBOL_GPL(ata_tf_read);
5230 EXPORT_SYMBOL_GPL(ata_noop_dev_select);
5231 EXPORT_SYMBOL_GPL(ata_std_dev_select);
5232 EXPORT_SYMBOL_GPL(ata_tf_to_fis);
5233 EXPORT_SYMBOL_GPL(ata_tf_from_fis);
5234 EXPORT_SYMBOL_GPL(ata_check_status);
5235 EXPORT_SYMBOL_GPL(ata_altstatus);
5236 EXPORT_SYMBOL_GPL(ata_exec_command);
5237 EXPORT_SYMBOL_GPL(ata_port_start);
5238 EXPORT_SYMBOL_GPL(ata_port_stop);
5239 EXPORT_SYMBOL_GPL(ata_host_stop);
5240 EXPORT_SYMBOL_GPL(ata_interrupt);
5241 EXPORT_SYMBOL_GPL(ata_qc_prep);
5242 EXPORT_SYMBOL_GPL(ata_bmdma_setup);
5243 EXPORT_SYMBOL_GPL(ata_bmdma_start);
5244 EXPORT_SYMBOL_GPL(ata_bmdma_irq_clear);
5245 EXPORT_SYMBOL_GPL(ata_bmdma_status);
5246 EXPORT_SYMBOL_GPL(ata_bmdma_stop);
5247 EXPORT_SYMBOL_GPL(ata_port_probe);
5248 EXPORT_SYMBOL_GPL(sata_phy_reset);
5249 EXPORT_SYMBOL_GPL(__sata_phy_reset);
5250 EXPORT_SYMBOL_GPL(ata_bus_reset);
5251 EXPORT_SYMBOL_GPL(ata_port_disable);
5252 EXPORT_SYMBOL_GPL(ata_ratelimit);
5253 EXPORT_SYMBOL_GPL(ata_scsi_ioctl);
5254 EXPORT_SYMBOL_GPL(ata_scsi_queuecmd);
5255 EXPORT_SYMBOL_GPL(ata_scsi_error);
5256 EXPORT_SYMBOL_GPL(ata_scsi_slave_config);
5257 EXPORT_SYMBOL_GPL(ata_scsi_release);
5258 EXPORT_SYMBOL_GPL(ata_host_intr);
5259 EXPORT_SYMBOL_GPL(ata_dev_classify);
5260 EXPORT_SYMBOL_GPL(ata_dev_id_string);
5261 EXPORT_SYMBOL_GPL(ata_dev_config);
5262 EXPORT_SYMBOL_GPL(ata_scsi_simulate);
5264 EXPORT_SYMBOL_GPL(ata_timing_compute);
5265 EXPORT_SYMBOL_GPL(ata_timing_merge);
5268 EXPORT_SYMBOL_GPL(pci_test_config_bits);
5269 EXPORT_SYMBOL_GPL(ata_pci_host_stop);
5270 EXPORT_SYMBOL_GPL(ata_pci_init_native_mode);
5271 EXPORT_SYMBOL_GPL(ata_pci_init_one);
5272 EXPORT_SYMBOL_GPL(ata_pci_remove_one);
5273 #endif /* CONFIG_PCI */