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 <scsi/scsi.h>
54 #include "scsi_priv.h"
55 #include <scsi/scsi_host.h>
56 #include <linux/libata.h>
58 #include <asm/semaphore.h>
59 #include <asm/byteorder.h>
63 static unsigned int ata_busy_sleep (struct ata_port *ap,
64 unsigned long tmout_pat,
66 static void ata_dev_init_params(struct ata_port *ap, struct ata_device *dev);
67 static void ata_set_mode(struct ata_port *ap);
68 static void ata_dev_set_xfermode(struct ata_port *ap, struct ata_device *dev);
69 static unsigned int ata_get_mode_mask(struct ata_port *ap, int shift);
70 static int fgb(u32 bitmap);
71 static int ata_choose_xfer_mode(struct ata_port *ap,
73 unsigned int *xfer_shift_out);
74 static void __ata_qc_complete(struct ata_queued_cmd *qc);
76 static unsigned int ata_unique_id = 1;
77 static struct workqueue_struct *ata_wq;
79 int atapi_enabled = 0;
80 module_param(atapi_enabled, int, 0444);
81 MODULE_PARM_DESC(atapi_enabled, "Enable discovery of ATAPI devices (0=off, 1=on)");
83 MODULE_AUTHOR("Jeff Garzik");
84 MODULE_DESCRIPTION("Library module for ATA devices");
85 MODULE_LICENSE("GPL");
86 MODULE_VERSION(DRV_VERSION);
89 * ata_tf_load - send taskfile registers to host controller
90 * @ap: Port to which output is sent
91 * @tf: ATA taskfile register set
93 * Outputs ATA taskfile to standard ATA host controller.
96 * Inherited from caller.
99 static void ata_tf_load_pio(struct ata_port *ap, struct ata_taskfile *tf)
101 struct ata_ioports *ioaddr = &ap->ioaddr;
102 unsigned int is_addr = tf->flags & ATA_TFLAG_ISADDR;
104 if (tf->ctl != ap->last_ctl) {
105 outb(tf->ctl, ioaddr->ctl_addr);
106 ap->last_ctl = tf->ctl;
110 if (is_addr && (tf->flags & ATA_TFLAG_LBA48)) {
111 outb(tf->hob_feature, ioaddr->feature_addr);
112 outb(tf->hob_nsect, ioaddr->nsect_addr);
113 outb(tf->hob_lbal, ioaddr->lbal_addr);
114 outb(tf->hob_lbam, ioaddr->lbam_addr);
115 outb(tf->hob_lbah, ioaddr->lbah_addr);
116 VPRINTK("hob: feat 0x%X nsect 0x%X, lba 0x%X 0x%X 0x%X\n",
125 outb(tf->feature, ioaddr->feature_addr);
126 outb(tf->nsect, ioaddr->nsect_addr);
127 outb(tf->lbal, ioaddr->lbal_addr);
128 outb(tf->lbam, ioaddr->lbam_addr);
129 outb(tf->lbah, ioaddr->lbah_addr);
130 VPRINTK("feat 0x%X nsect 0x%X lba 0x%X 0x%X 0x%X\n",
138 if (tf->flags & ATA_TFLAG_DEVICE) {
139 outb(tf->device, ioaddr->device_addr);
140 VPRINTK("device 0x%X\n", tf->device);
147 * ata_tf_load_mmio - send taskfile registers to host controller
148 * @ap: Port to which output is sent
149 * @tf: ATA taskfile register set
151 * Outputs ATA taskfile to standard ATA host controller using MMIO.
154 * Inherited from caller.
157 static void ata_tf_load_mmio(struct ata_port *ap, struct ata_taskfile *tf)
159 struct ata_ioports *ioaddr = &ap->ioaddr;
160 unsigned int is_addr = tf->flags & ATA_TFLAG_ISADDR;
162 if (tf->ctl != ap->last_ctl) {
163 writeb(tf->ctl, (void __iomem *) ap->ioaddr.ctl_addr);
164 ap->last_ctl = tf->ctl;
168 if (is_addr && (tf->flags & ATA_TFLAG_LBA48)) {
169 writeb(tf->hob_feature, (void __iomem *) ioaddr->feature_addr);
170 writeb(tf->hob_nsect, (void __iomem *) ioaddr->nsect_addr);
171 writeb(tf->hob_lbal, (void __iomem *) ioaddr->lbal_addr);
172 writeb(tf->hob_lbam, (void __iomem *) ioaddr->lbam_addr);
173 writeb(tf->hob_lbah, (void __iomem *) ioaddr->lbah_addr);
174 VPRINTK("hob: feat 0x%X nsect 0x%X, lba 0x%X 0x%X 0x%X\n",
183 writeb(tf->feature, (void __iomem *) ioaddr->feature_addr);
184 writeb(tf->nsect, (void __iomem *) ioaddr->nsect_addr);
185 writeb(tf->lbal, (void __iomem *) ioaddr->lbal_addr);
186 writeb(tf->lbam, (void __iomem *) ioaddr->lbam_addr);
187 writeb(tf->lbah, (void __iomem *) ioaddr->lbah_addr);
188 VPRINTK("feat 0x%X nsect 0x%X lba 0x%X 0x%X 0x%X\n",
196 if (tf->flags & ATA_TFLAG_DEVICE) {
197 writeb(tf->device, (void __iomem *) ioaddr->device_addr);
198 VPRINTK("device 0x%X\n", tf->device);
206 * ata_tf_load - send taskfile registers to host controller
207 * @ap: Port to which output is sent
208 * @tf: ATA taskfile register set
210 * Outputs ATA taskfile to standard ATA host controller using MMIO
211 * or PIO as indicated by the ATA_FLAG_MMIO flag.
212 * Writes the control, feature, nsect, lbal, lbam, and lbah registers.
213 * Optionally (ATA_TFLAG_LBA48) writes hob_feature, hob_nsect,
214 * hob_lbal, hob_lbam, and hob_lbah.
216 * This function waits for idle (!BUSY and !DRQ) after writing
217 * registers. If the control register has a new value, this
218 * function also waits for idle after writing control and before
219 * writing the remaining registers.
221 * May be used as the tf_load() entry in ata_port_operations.
224 * Inherited from caller.
226 void ata_tf_load(struct ata_port *ap, struct ata_taskfile *tf)
228 if (ap->flags & ATA_FLAG_MMIO)
229 ata_tf_load_mmio(ap, tf);
231 ata_tf_load_pio(ap, tf);
235 * ata_exec_command_pio - issue ATA command to host controller
236 * @ap: port to which command is being issued
237 * @tf: ATA taskfile register set
239 * Issues PIO write to ATA command register, with proper
240 * synchronization with interrupt handler / other threads.
243 * spin_lock_irqsave(host_set lock)
246 static void ata_exec_command_pio(struct ata_port *ap, struct ata_taskfile *tf)
248 DPRINTK("ata%u: cmd 0x%X\n", ap->id, tf->command);
250 outb(tf->command, ap->ioaddr.command_addr);
256 * ata_exec_command_mmio - issue ATA command to host controller
257 * @ap: port to which command is being issued
258 * @tf: ATA taskfile register set
260 * Issues MMIO write to ATA command register, with proper
261 * synchronization with interrupt handler / other threads.
264 * spin_lock_irqsave(host_set lock)
267 static void ata_exec_command_mmio(struct ata_port *ap, struct ata_taskfile *tf)
269 DPRINTK("ata%u: cmd 0x%X\n", ap->id, tf->command);
271 writeb(tf->command, (void __iomem *) ap->ioaddr.command_addr);
277 * ata_exec_command - issue ATA command to host controller
278 * @ap: port to which command is being issued
279 * @tf: ATA taskfile register set
281 * Issues PIO/MMIO write to ATA command register, with proper
282 * synchronization with interrupt handler / other threads.
285 * spin_lock_irqsave(host_set lock)
287 void ata_exec_command(struct ata_port *ap, struct ata_taskfile *tf)
289 if (ap->flags & ATA_FLAG_MMIO)
290 ata_exec_command_mmio(ap, tf);
292 ata_exec_command_pio(ap, tf);
296 * ata_exec - issue ATA command to host controller
297 * @ap: port to which command is being issued
298 * @tf: ATA taskfile register set
300 * Issues PIO/MMIO write to ATA command register, with proper
301 * synchronization with interrupt handler / other threads.
304 * Obtains host_set lock.
307 static inline void ata_exec(struct ata_port *ap, struct ata_taskfile *tf)
311 DPRINTK("ata%u: cmd 0x%X\n", ap->id, tf->command);
312 spin_lock_irqsave(&ap->host_set->lock, flags);
313 ap->ops->exec_command(ap, tf);
314 spin_unlock_irqrestore(&ap->host_set->lock, flags);
318 * ata_tf_to_host - issue ATA taskfile to host controller
319 * @ap: port to which command is being issued
320 * @tf: ATA taskfile register set
322 * Issues ATA taskfile register set to ATA host controller,
323 * with proper synchronization with interrupt handler and
327 * Obtains host_set lock.
330 static void ata_tf_to_host(struct ata_port *ap, struct ata_taskfile *tf)
332 ap->ops->tf_load(ap, tf);
338 * ata_tf_to_host_nolock - issue ATA taskfile to host controller
339 * @ap: port to which command is being issued
340 * @tf: ATA taskfile register set
342 * Issues ATA taskfile register set to ATA host controller,
343 * with proper synchronization with interrupt handler and
347 * spin_lock_irqsave(host_set lock)
350 void ata_tf_to_host_nolock(struct ata_port *ap, struct ata_taskfile *tf)
352 ap->ops->tf_load(ap, tf);
353 ap->ops->exec_command(ap, tf);
357 * ata_tf_read_pio - input device's ATA taskfile shadow registers
358 * @ap: Port from which input is read
359 * @tf: ATA taskfile register set for storing input
361 * Reads ATA taskfile registers for currently-selected device
365 * Inherited from caller.
368 static void ata_tf_read_pio(struct ata_port *ap, struct ata_taskfile *tf)
370 struct ata_ioports *ioaddr = &ap->ioaddr;
372 tf->nsect = inb(ioaddr->nsect_addr);
373 tf->lbal = inb(ioaddr->lbal_addr);
374 tf->lbam = inb(ioaddr->lbam_addr);
375 tf->lbah = inb(ioaddr->lbah_addr);
376 tf->device = inb(ioaddr->device_addr);
378 if (tf->flags & ATA_TFLAG_LBA48) {
379 outb(tf->ctl | ATA_HOB, ioaddr->ctl_addr);
380 tf->hob_feature = inb(ioaddr->error_addr);
381 tf->hob_nsect = inb(ioaddr->nsect_addr);
382 tf->hob_lbal = inb(ioaddr->lbal_addr);
383 tf->hob_lbam = inb(ioaddr->lbam_addr);
384 tf->hob_lbah = inb(ioaddr->lbah_addr);
389 * ata_tf_read_mmio - input device's ATA taskfile shadow registers
390 * @ap: Port from which input is read
391 * @tf: ATA taskfile register set for storing input
393 * Reads ATA taskfile registers for currently-selected device
397 * Inherited from caller.
400 static void ata_tf_read_mmio(struct ata_port *ap, struct ata_taskfile *tf)
402 struct ata_ioports *ioaddr = &ap->ioaddr;
404 tf->nsect = readb((void __iomem *)ioaddr->nsect_addr);
405 tf->lbal = readb((void __iomem *)ioaddr->lbal_addr);
406 tf->lbam = readb((void __iomem *)ioaddr->lbam_addr);
407 tf->lbah = readb((void __iomem *)ioaddr->lbah_addr);
408 tf->device = readb((void __iomem *)ioaddr->device_addr);
410 if (tf->flags & ATA_TFLAG_LBA48) {
411 writeb(tf->ctl | ATA_HOB, (void __iomem *) ap->ioaddr.ctl_addr);
412 tf->hob_feature = readb((void __iomem *)ioaddr->error_addr);
413 tf->hob_nsect = readb((void __iomem *)ioaddr->nsect_addr);
414 tf->hob_lbal = readb((void __iomem *)ioaddr->lbal_addr);
415 tf->hob_lbam = readb((void __iomem *)ioaddr->lbam_addr);
416 tf->hob_lbah = readb((void __iomem *)ioaddr->lbah_addr);
422 * ata_tf_read - input device's ATA taskfile shadow registers
423 * @ap: Port from which input is read
424 * @tf: ATA taskfile register set for storing input
426 * Reads ATA taskfile registers for currently-selected device
429 * Reads nsect, lbal, lbam, lbah, and device. If ATA_TFLAG_LBA48
430 * is set, also reads the hob registers.
432 * May be used as the tf_read() entry in ata_port_operations.
435 * Inherited from caller.
437 void ata_tf_read(struct ata_port *ap, struct ata_taskfile *tf)
439 if (ap->flags & ATA_FLAG_MMIO)
440 ata_tf_read_mmio(ap, tf);
442 ata_tf_read_pio(ap, tf);
446 * ata_check_status_pio - Read device status reg & clear interrupt
447 * @ap: port where the device is
449 * Reads ATA taskfile status register for currently-selected device
450 * and return its value. This also clears pending interrupts
454 * Inherited from caller.
456 static u8 ata_check_status_pio(struct ata_port *ap)
458 return inb(ap->ioaddr.status_addr);
462 * ata_check_status_mmio - Read device status reg & clear interrupt
463 * @ap: port where the device is
465 * Reads ATA taskfile status register for currently-selected device
466 * via MMIO and return its value. This also clears pending interrupts
470 * Inherited from caller.
472 static u8 ata_check_status_mmio(struct ata_port *ap)
474 return readb((void __iomem *) ap->ioaddr.status_addr);
479 * ata_check_status - Read device status reg & clear interrupt
480 * @ap: port where the device is
482 * Reads ATA taskfile status register for currently-selected device
483 * and return its value. This also clears pending interrupts
486 * May be used as the check_status() entry in ata_port_operations.
489 * Inherited from caller.
491 u8 ata_check_status(struct ata_port *ap)
493 if (ap->flags & ATA_FLAG_MMIO)
494 return ata_check_status_mmio(ap);
495 return ata_check_status_pio(ap);
500 * ata_altstatus - Read device alternate status reg
501 * @ap: port where the device is
503 * Reads ATA taskfile alternate status register for
504 * currently-selected device and return its value.
506 * Note: may NOT be used as the check_altstatus() entry in
507 * ata_port_operations.
510 * Inherited from caller.
512 u8 ata_altstatus(struct ata_port *ap)
514 if (ap->ops->check_altstatus)
515 return ap->ops->check_altstatus(ap);
517 if (ap->flags & ATA_FLAG_MMIO)
518 return readb((void __iomem *)ap->ioaddr.altstatus_addr);
519 return inb(ap->ioaddr.altstatus_addr);
524 * ata_chk_err - Read device error reg
525 * @ap: port where the device is
527 * Reads ATA taskfile error register for
528 * currently-selected device and return its value.
530 * Note: may NOT be used as the check_err() entry in
531 * ata_port_operations.
534 * Inherited from caller.
536 u8 ata_chk_err(struct ata_port *ap)
538 if (ap->ops->check_err)
539 return ap->ops->check_err(ap);
541 if (ap->flags & ATA_FLAG_MMIO) {
542 return readb((void __iomem *) ap->ioaddr.error_addr);
544 return inb(ap->ioaddr.error_addr);
548 * ata_tf_to_fis - Convert ATA taskfile to SATA FIS structure
549 * @tf: Taskfile to convert
550 * @fis: Buffer into which data will output
551 * @pmp: Port multiplier port
553 * Converts a standard ATA taskfile to a Serial ATA
554 * FIS structure (Register - Host to Device).
557 * Inherited from caller.
560 void ata_tf_to_fis(struct ata_taskfile *tf, u8 *fis, u8 pmp)
562 fis[0] = 0x27; /* Register - Host to Device FIS */
563 fis[1] = (pmp & 0xf) | (1 << 7); /* Port multiplier number,
564 bit 7 indicates Command FIS */
565 fis[2] = tf->command;
566 fis[3] = tf->feature;
573 fis[8] = tf->hob_lbal;
574 fis[9] = tf->hob_lbam;
575 fis[10] = tf->hob_lbah;
576 fis[11] = tf->hob_feature;
579 fis[13] = tf->hob_nsect;
590 * ata_tf_from_fis - Convert SATA FIS to ATA taskfile
591 * @fis: Buffer from which data will be input
592 * @tf: Taskfile to output
594 * Converts a standard ATA taskfile to a Serial ATA
595 * FIS structure (Register - Host to Device).
598 * Inherited from caller.
601 void ata_tf_from_fis(u8 *fis, struct ata_taskfile *tf)
603 tf->command = fis[2]; /* status */
604 tf->feature = fis[3]; /* error */
611 tf->hob_lbal = fis[8];
612 tf->hob_lbam = fis[9];
613 tf->hob_lbah = fis[10];
616 tf->hob_nsect = fis[13];
619 static const u8 ata_rw_cmds[] = {
623 ATA_CMD_READ_MULTI_EXT,
624 ATA_CMD_WRITE_MULTI_EXT,
628 ATA_CMD_PIO_READ_EXT,
629 ATA_CMD_PIO_WRITE_EXT,
638 * ata_rwcmd_protocol - set taskfile r/w commands and protocol
639 * @qc: command to examine and configure
641 * Examine the device configuration and tf->flags to calculate
642 * the proper read/write commands and protocol to use.
647 void ata_rwcmd_protocol(struct ata_queued_cmd *qc)
649 struct ata_taskfile *tf = &qc->tf;
650 struct ata_device *dev = qc->dev;
652 int index, lba48, write;
654 lba48 = (tf->flags & ATA_TFLAG_LBA48) ? 2 : 0;
655 write = (tf->flags & ATA_TFLAG_WRITE) ? 1 : 0;
657 if (dev->flags & ATA_DFLAG_PIO) {
658 tf->protocol = ATA_PROT_PIO;
659 index = dev->multi_count ? 0 : 4;
661 tf->protocol = ATA_PROT_DMA;
665 tf->command = ata_rw_cmds[index + lba48 + write];
668 static const char * xfer_mode_str[] = {
688 * ata_udma_string - convert UDMA bit offset to string
689 * @mask: mask of bits supported; only highest bit counts.
691 * Determine string which represents the highest speed
692 * (highest bit in @udma_mask).
698 * Constant C string representing highest speed listed in
699 * @udma_mask, or the constant C string "<n/a>".
702 static const char *ata_mode_string(unsigned int mask)
706 for (i = 7; i >= 0; i--)
709 for (i = ATA_SHIFT_MWDMA + 2; i >= ATA_SHIFT_MWDMA; i--)
712 for (i = ATA_SHIFT_PIO + 4; i >= ATA_SHIFT_PIO; i--)
719 return xfer_mode_str[i];
723 * ata_pio_devchk - PATA device presence detection
724 * @ap: ATA channel to examine
725 * @device: Device to examine (starting at zero)
727 * This technique was originally described in
728 * Hale Landis's ATADRVR (www.ata-atapi.com), and
729 * later found its way into the ATA/ATAPI spec.
731 * Write a pattern to the ATA shadow registers,
732 * and if a device is present, it will respond by
733 * correctly storing and echoing back the
734 * ATA shadow register contents.
740 static unsigned int ata_pio_devchk(struct ata_port *ap,
743 struct ata_ioports *ioaddr = &ap->ioaddr;
746 ap->ops->dev_select(ap, device);
748 outb(0x55, ioaddr->nsect_addr);
749 outb(0xaa, ioaddr->lbal_addr);
751 outb(0xaa, ioaddr->nsect_addr);
752 outb(0x55, ioaddr->lbal_addr);
754 outb(0x55, ioaddr->nsect_addr);
755 outb(0xaa, ioaddr->lbal_addr);
757 nsect = inb(ioaddr->nsect_addr);
758 lbal = inb(ioaddr->lbal_addr);
760 if ((nsect == 0x55) && (lbal == 0xaa))
761 return 1; /* we found a device */
763 return 0; /* nothing found */
767 * ata_mmio_devchk - PATA device presence detection
768 * @ap: ATA channel to examine
769 * @device: Device to examine (starting at zero)
771 * This technique was originally described in
772 * Hale Landis's ATADRVR (www.ata-atapi.com), and
773 * later found its way into the ATA/ATAPI spec.
775 * Write a pattern to the ATA shadow registers,
776 * and if a device is present, it will respond by
777 * correctly storing and echoing back the
778 * ATA shadow register contents.
784 static unsigned int ata_mmio_devchk(struct ata_port *ap,
787 struct ata_ioports *ioaddr = &ap->ioaddr;
790 ap->ops->dev_select(ap, device);
792 writeb(0x55, (void __iomem *) ioaddr->nsect_addr);
793 writeb(0xaa, (void __iomem *) ioaddr->lbal_addr);
795 writeb(0xaa, (void __iomem *) ioaddr->nsect_addr);
796 writeb(0x55, (void __iomem *) ioaddr->lbal_addr);
798 writeb(0x55, (void __iomem *) ioaddr->nsect_addr);
799 writeb(0xaa, (void __iomem *) ioaddr->lbal_addr);
801 nsect = readb((void __iomem *) ioaddr->nsect_addr);
802 lbal = readb((void __iomem *) ioaddr->lbal_addr);
804 if ((nsect == 0x55) && (lbal == 0xaa))
805 return 1; /* we found a device */
807 return 0; /* nothing found */
811 * ata_devchk - PATA device presence detection
812 * @ap: ATA channel to examine
813 * @device: Device to examine (starting at zero)
815 * Dispatch ATA device presence detection, depending
816 * on whether we are using PIO or MMIO to talk to the
817 * ATA shadow registers.
823 static unsigned int ata_devchk(struct ata_port *ap,
826 if (ap->flags & ATA_FLAG_MMIO)
827 return ata_mmio_devchk(ap, device);
828 return ata_pio_devchk(ap, device);
832 * ata_dev_classify - determine device type based on ATA-spec signature
833 * @tf: ATA taskfile register set for device to be identified
835 * Determine from taskfile register contents whether a device is
836 * ATA or ATAPI, as per "Signature and persistence" section
837 * of ATA/PI spec (volume 1, sect 5.14).
843 * Device type, %ATA_DEV_ATA, %ATA_DEV_ATAPI, or %ATA_DEV_UNKNOWN
844 * the event of failure.
847 unsigned int ata_dev_classify(struct ata_taskfile *tf)
849 /* Apple's open source Darwin code hints that some devices only
850 * put a proper signature into the LBA mid/high registers,
851 * So, we only check those. It's sufficient for uniqueness.
854 if (((tf->lbam == 0) && (tf->lbah == 0)) ||
855 ((tf->lbam == 0x3c) && (tf->lbah == 0xc3))) {
856 DPRINTK("found ATA device by sig\n");
860 if (((tf->lbam == 0x14) && (tf->lbah == 0xeb)) ||
861 ((tf->lbam == 0x69) && (tf->lbah == 0x96))) {
862 DPRINTK("found ATAPI device by sig\n");
863 return ATA_DEV_ATAPI;
866 DPRINTK("unknown device\n");
867 return ATA_DEV_UNKNOWN;
871 * ata_dev_try_classify - Parse returned ATA device signature
872 * @ap: ATA channel to examine
873 * @device: Device to examine (starting at zero)
875 * After an event -- SRST, E.D.D., or SATA COMRESET -- occurs,
876 * an ATA/ATAPI-defined set of values is placed in the ATA
877 * shadow registers, indicating the results of device detection
880 * Select the ATA device, and read the values from the ATA shadow
881 * registers. Then parse according to the Error register value,
882 * and the spec-defined values examined by ata_dev_classify().
888 static u8 ata_dev_try_classify(struct ata_port *ap, unsigned int device)
890 struct ata_device *dev = &ap->device[device];
891 struct ata_taskfile tf;
895 ap->ops->dev_select(ap, device);
897 memset(&tf, 0, sizeof(tf));
899 err = ata_chk_err(ap);
900 ap->ops->tf_read(ap, &tf);
902 dev->class = ATA_DEV_NONE;
904 /* see if device passed diags */
907 else if ((device == 0) && (err == 0x81))
912 /* determine if device if ATA or ATAPI */
913 class = ata_dev_classify(&tf);
914 if (class == ATA_DEV_UNKNOWN)
916 if ((class == ATA_DEV_ATA) && (ata_chk_status(ap) == 0))
925 * ata_dev_id_string - Convert IDENTIFY DEVICE page into string
926 * @id: IDENTIFY DEVICE results we will examine
927 * @s: string into which data is output
928 * @ofs: offset into identify device page
929 * @len: length of string to return. must be an even number.
931 * The strings in the IDENTIFY DEVICE page are broken up into
932 * 16-bit chunks. Run through the string, and output each
933 * 8-bit chunk linearly, regardless of platform.
939 void ata_dev_id_string(u16 *id, unsigned char *s,
940 unsigned int ofs, unsigned int len)
960 * ata_noop_dev_select - Select device 0/1 on ATA bus
961 * @ap: ATA channel to manipulate
962 * @device: ATA device (numbered from zero) to select
964 * This function performs no actual function.
966 * May be used as the dev_select() entry in ata_port_operations.
971 void ata_noop_dev_select (struct ata_port *ap, unsigned int device)
977 * ata_std_dev_select - Select device 0/1 on ATA bus
978 * @ap: ATA channel to manipulate
979 * @device: ATA device (numbered from zero) to select
981 * Use the method defined in the ATA specification to
982 * make either device 0, or device 1, active on the
983 * ATA channel. Works with both PIO and MMIO.
985 * May be used as the dev_select() entry in ata_port_operations.
991 void ata_std_dev_select (struct ata_port *ap, unsigned int device)
996 tmp = ATA_DEVICE_OBS;
998 tmp = ATA_DEVICE_OBS | ATA_DEV1;
1000 if (ap->flags & ATA_FLAG_MMIO) {
1001 writeb(tmp, (void __iomem *) ap->ioaddr.device_addr);
1003 outb(tmp, ap->ioaddr.device_addr);
1005 ata_pause(ap); /* needed; also flushes, for mmio */
1009 * ata_dev_select - Select device 0/1 on ATA bus
1010 * @ap: ATA channel to manipulate
1011 * @device: ATA device (numbered from zero) to select
1012 * @wait: non-zero to wait for Status register BSY bit to clear
1013 * @can_sleep: non-zero if context allows sleeping
1015 * Use the method defined in the ATA specification to
1016 * make either device 0, or device 1, active on the
1019 * This is a high-level version of ata_std_dev_select(),
1020 * which additionally provides the services of inserting
1021 * the proper pauses and status polling, where needed.
1027 void ata_dev_select(struct ata_port *ap, unsigned int device,
1028 unsigned int wait, unsigned int can_sleep)
1030 VPRINTK("ENTER, ata%u: device %u, wait %u\n",
1031 ap->id, device, wait);
1036 ap->ops->dev_select(ap, device);
1039 if (can_sleep && ap->device[device].class == ATA_DEV_ATAPI)
1046 * ata_dump_id - IDENTIFY DEVICE info debugging output
1047 * @dev: Device whose IDENTIFY DEVICE page we will dump
1049 * Dump selected 16-bit words from a detected device's
1050 * IDENTIFY PAGE page.
1056 static inline void ata_dump_id(struct ata_device *dev)
1058 DPRINTK("49==0x%04x "
1068 DPRINTK("80==0x%04x "
1078 DPRINTK("88==0x%04x "
1085 * ata_dev_identify - obtain IDENTIFY x DEVICE page
1086 * @ap: port on which device we wish to probe resides
1087 * @device: device bus address, starting at zero
1089 * Following bus reset, we issue the IDENTIFY [PACKET] DEVICE
1090 * command, and read back the 512-byte device information page.
1091 * The device information page is fed to us via the standard
1092 * PIO-IN protocol, but we hand-code it here. (TODO: investigate
1093 * using standard PIO-IN paths)
1095 * After reading the device information page, we use several
1096 * bits of information from it to initialize data structures
1097 * that will be used during the lifetime of the ata_device.
1098 * Other data from the info page is used to disqualify certain
1099 * older ATA devices we do not wish to support.
1102 * Inherited from caller. Some functions called by this function
1103 * obtain the host_set lock.
1106 static void ata_dev_identify(struct ata_port *ap, unsigned int device)
1108 struct ata_device *dev = &ap->device[device];
1109 unsigned int major_version;
1111 unsigned long xfer_modes;
1113 unsigned int using_edd;
1114 DECLARE_COMPLETION(wait);
1115 struct ata_queued_cmd *qc;
1116 unsigned long flags;
1119 if (!ata_dev_present(dev)) {
1120 DPRINTK("ENTER/EXIT (host %u, dev %u) -- nodev\n",
1125 if (ap->flags & (ATA_FLAG_SRST | ATA_FLAG_SATA_RESET))
1130 DPRINTK("ENTER, host %u, dev %u\n", ap->id, device);
1132 assert (dev->class == ATA_DEV_ATA || dev->class == ATA_DEV_ATAPI ||
1133 dev->class == ATA_DEV_NONE);
1135 ata_dev_select(ap, device, 1, 1); /* select device 0/1 */
1137 qc = ata_qc_new_init(ap, dev);
1140 ata_sg_init_one(qc, dev->id, sizeof(dev->id));
1141 qc->dma_dir = DMA_FROM_DEVICE;
1142 qc->tf.protocol = ATA_PROT_PIO;
1146 if (dev->class == ATA_DEV_ATA) {
1147 qc->tf.command = ATA_CMD_ID_ATA;
1148 DPRINTK("do ATA identify\n");
1150 qc->tf.command = ATA_CMD_ID_ATAPI;
1151 DPRINTK("do ATAPI identify\n");
1154 qc->waiting = &wait;
1155 qc->complete_fn = ata_qc_complete_noop;
1157 spin_lock_irqsave(&ap->host_set->lock, flags);
1158 rc = ata_qc_issue(qc);
1159 spin_unlock_irqrestore(&ap->host_set->lock, flags);
1164 wait_for_completion(&wait);
1166 status = ata_chk_status(ap);
1167 if (status & ATA_ERR) {
1169 * arg! EDD works for all test cases, but seems to return
1170 * the ATA signature for some ATAPI devices. Until the
1171 * reason for this is found and fixed, we fix up the mess
1172 * here. If IDENTIFY DEVICE returns command aborted
1173 * (as ATAPI devices do), then we issue an
1174 * IDENTIFY PACKET DEVICE.
1176 * ATA software reset (SRST, the default) does not appear
1177 * to have this problem.
1179 if ((using_edd) && (qc->tf.command == ATA_CMD_ID_ATA)) {
1180 u8 err = ata_chk_err(ap);
1181 if (err & ATA_ABORTED) {
1182 dev->class = ATA_DEV_ATAPI;
1193 swap_buf_le16(dev->id, ATA_ID_WORDS);
1195 /* print device capabilities */
1196 printk(KERN_DEBUG "ata%u: dev %u cfg "
1197 "49:%04x 82:%04x 83:%04x 84:%04x 85:%04x 86:%04x 87:%04x 88:%04x\n",
1198 ap->id, device, dev->id[49],
1199 dev->id[82], dev->id[83], dev->id[84],
1200 dev->id[85], dev->id[86], dev->id[87],
1204 * common ATA, ATAPI feature tests
1207 /* we require DMA support (bits 8 of word 49) */
1208 if (!ata_id_has_dma(dev->id)) {
1209 printk(KERN_DEBUG "ata%u: no dma\n", ap->id);
1213 /* quick-n-dirty find max transfer mode; for printk only */
1214 xfer_modes = dev->id[ATA_ID_UDMA_MODES];
1216 xfer_modes = (dev->id[ATA_ID_MWDMA_MODES]) << ATA_SHIFT_MWDMA;
1218 xfer_modes = (dev->id[ATA_ID_PIO_MODES]) << (ATA_SHIFT_PIO + 3);
1219 xfer_modes |= (0x7 << ATA_SHIFT_PIO);
1224 /* ATA-specific feature tests */
1225 if (dev->class == ATA_DEV_ATA) {
1226 if (!ata_id_is_ata(dev->id)) /* sanity check */
1229 /* get major version */
1230 tmp = dev->id[ATA_ID_MAJOR_VER];
1231 for (major_version = 14; major_version >= 1; major_version--)
1232 if (tmp & (1 << major_version))
1236 * The exact sequence expected by certain pre-ATA4 drives is:
1239 * INITIALIZE DEVICE PARAMETERS
1241 * Some drives were very specific about that exact sequence.
1243 if (major_version < 4 || (!ata_id_has_lba(dev->id)))
1244 ata_dev_init_params(ap, dev);
1246 if (ata_id_has_lba(dev->id)) {
1247 dev->flags |= ATA_DFLAG_LBA;
1249 if (ata_id_has_lba48(dev->id)) {
1250 dev->flags |= ATA_DFLAG_LBA48;
1251 dev->n_sectors = ata_id_u64(dev->id, 100);
1253 dev->n_sectors = ata_id_u32(dev->id, 60);
1256 /* print device info to dmesg */
1257 printk(KERN_INFO "ata%u: dev %u ATA-%d, max %s, %Lu sectors:%s\n",
1260 ata_mode_string(xfer_modes),
1261 (unsigned long long)dev->n_sectors,
1262 dev->flags & ATA_DFLAG_LBA48 ? " LBA48" : " LBA");
1266 /* Default translation */
1267 dev->cylinders = dev->id[1];
1268 dev->heads = dev->id[3];
1269 dev->sectors = dev->id[6];
1270 dev->n_sectors = dev->cylinders * dev->heads * dev->sectors;
1272 if (ata_id_current_chs_valid(dev->id)) {
1273 /* Current CHS translation is valid. */
1274 dev->cylinders = dev->id[54];
1275 dev->heads = dev->id[55];
1276 dev->sectors = dev->id[56];
1278 dev->n_sectors = ata_id_u32(dev->id, 57);
1281 /* print device info to dmesg */
1282 printk(KERN_INFO "ata%u: dev %u ATA-%d, max %s, %Lu sectors: CHS %d/%d/%d\n",
1285 ata_mode_string(xfer_modes),
1286 (unsigned long long)dev->n_sectors,
1287 (int)dev->cylinders, (int)dev->heads, (int)dev->sectors);
1291 ap->host->max_cmd_len = 16;
1294 /* ATAPI-specific feature tests */
1296 if (ata_id_is_ata(dev->id)) /* sanity check */
1299 rc = atapi_cdb_len(dev->id);
1300 if ((rc < 12) || (rc > ATAPI_CDB_LEN)) {
1301 printk(KERN_WARNING "ata%u: unsupported CDB len\n", ap->id);
1304 ap->cdb_len = (unsigned int) rc;
1305 ap->host->max_cmd_len = (unsigned char) ap->cdb_len;
1307 /* print device info to dmesg */
1308 printk(KERN_INFO "ata%u: dev %u ATAPI, max %s\n",
1310 ata_mode_string(xfer_modes));
1313 DPRINTK("EXIT, drv_stat = 0x%x\n", ata_chk_status(ap));
1317 printk(KERN_WARNING "ata%u: dev %u not supported, ignoring\n",
1320 dev->class++; /* converts ATA_DEV_xxx into ATA_DEV_xxx_UNSUP */
1321 DPRINTK("EXIT, err\n");
1325 static inline u8 ata_dev_knobble(struct ata_port *ap)
1327 return ((ap->cbl == ATA_CBL_SATA) && (!ata_id_is_sata(ap->device->id)));
1331 * ata_dev_config - Run device specific handlers and check for
1332 * SATA->PATA bridges
1339 void ata_dev_config(struct ata_port *ap, unsigned int i)
1341 /* limit bridge transfers to udma5, 200 sectors */
1342 if (ata_dev_knobble(ap)) {
1343 printk(KERN_INFO "ata%u(%u): applying bridge limits\n",
1344 ap->id, ap->device->devno);
1345 ap->udma_mask &= ATA_UDMA5;
1346 ap->host->max_sectors = ATA_MAX_SECTORS;
1347 ap->host->hostt->max_sectors = ATA_MAX_SECTORS;
1348 ap->device->flags |= ATA_DFLAG_LOCK_SECTORS;
1351 if (ap->ops->dev_config)
1352 ap->ops->dev_config(ap, &ap->device[i]);
1356 * ata_bus_probe - Reset and probe ATA bus
1359 * Master ATA bus probing function. Initiates a hardware-dependent
1360 * bus reset, then attempts to identify any devices found on
1364 * PCI/etc. bus probe sem.
1367 * Zero on success, non-zero on error.
1370 static int ata_bus_probe(struct ata_port *ap)
1372 unsigned int i, found = 0;
1374 ap->ops->phy_reset(ap);
1375 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1378 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1379 ata_dev_identify(ap, i);
1380 if (ata_dev_present(&ap->device[i])) {
1382 ata_dev_config(ap,i);
1386 if ((!found) || (ap->flags & ATA_FLAG_PORT_DISABLED))
1387 goto err_out_disable;
1390 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1391 goto err_out_disable;
1396 ap->ops->port_disable(ap);
1402 * ata_port_probe - Mark port as enabled
1403 * @ap: Port for which we indicate enablement
1405 * Modify @ap data structure such that the system
1406 * thinks that the entire port is enabled.
1408 * LOCKING: host_set lock, or some other form of
1412 void ata_port_probe(struct ata_port *ap)
1414 ap->flags &= ~ATA_FLAG_PORT_DISABLED;
1418 * __sata_phy_reset - Wake/reset a low-level SATA PHY
1419 * @ap: SATA port associated with target SATA PHY.
1421 * This function issues commands to standard SATA Sxxx
1422 * PHY registers, to wake up the phy (and device), and
1423 * clear any reset condition.
1426 * PCI/etc. bus probe sem.
1429 void __sata_phy_reset(struct ata_port *ap)
1432 unsigned long timeout = jiffies + (HZ * 5);
1434 if (ap->flags & ATA_FLAG_SATA_RESET) {
1435 /* issue phy wake/reset */
1436 scr_write_flush(ap, SCR_CONTROL, 0x301);
1437 /* Couldn't find anything in SATA I/II specs, but
1438 * AHCI-1.1 10.4.2 says at least 1 ms. */
1441 scr_write_flush(ap, SCR_CONTROL, 0x300); /* phy wake/clear reset */
1443 /* wait for phy to become ready, if necessary */
1446 sstatus = scr_read(ap, SCR_STATUS);
1447 if ((sstatus & 0xf) != 1)
1449 } while (time_before(jiffies, timeout));
1451 /* TODO: phy layer with polling, timeouts, etc. */
1452 if (sata_dev_present(ap))
1455 sstatus = scr_read(ap, SCR_STATUS);
1456 printk(KERN_INFO "ata%u: no device found (phy stat %08x)\n",
1458 ata_port_disable(ap);
1461 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1464 if (ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT)) {
1465 ata_port_disable(ap);
1469 ap->cbl = ATA_CBL_SATA;
1473 * sata_phy_reset - Reset SATA bus.
1474 * @ap: SATA port associated with target SATA PHY.
1476 * This function resets the SATA bus, and then probes
1477 * the bus for devices.
1480 * PCI/etc. bus probe sem.
1483 void sata_phy_reset(struct ata_port *ap)
1485 __sata_phy_reset(ap);
1486 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1492 * ata_port_disable - Disable port.
1493 * @ap: Port to be disabled.
1495 * Modify @ap data structure such that the system
1496 * thinks that the entire port is disabled, and should
1497 * never attempt to probe or communicate with devices
1500 * LOCKING: host_set lock, or some other form of
1504 void ata_port_disable(struct ata_port *ap)
1506 ap->device[0].class = ATA_DEV_NONE;
1507 ap->device[1].class = ATA_DEV_NONE;
1508 ap->flags |= ATA_FLAG_PORT_DISABLED;
1514 } xfer_mode_classes[] = {
1515 { ATA_SHIFT_UDMA, XFER_UDMA_0 },
1516 { ATA_SHIFT_MWDMA, XFER_MW_DMA_0 },
1517 { ATA_SHIFT_PIO, XFER_PIO_0 },
1520 static inline u8 base_from_shift(unsigned int shift)
1524 for (i = 0; i < ARRAY_SIZE(xfer_mode_classes); i++)
1525 if (xfer_mode_classes[i].shift == shift)
1526 return xfer_mode_classes[i].base;
1531 static void ata_dev_set_mode(struct ata_port *ap, struct ata_device *dev)
1536 if (!ata_dev_present(dev) || (ap->flags & ATA_FLAG_PORT_DISABLED))
1539 if (dev->xfer_shift == ATA_SHIFT_PIO)
1540 dev->flags |= ATA_DFLAG_PIO;
1542 ata_dev_set_xfermode(ap, dev);
1544 base = base_from_shift(dev->xfer_shift);
1545 ofs = dev->xfer_mode - base;
1546 idx = ofs + dev->xfer_shift;
1547 WARN_ON(idx >= ARRAY_SIZE(xfer_mode_str));
1549 DPRINTK("idx=%d xfer_shift=%u, xfer_mode=0x%x, base=0x%x, offset=%d\n",
1550 idx, dev->xfer_shift, (int)dev->xfer_mode, (int)base, ofs);
1552 printk(KERN_INFO "ata%u: dev %u configured for %s\n",
1553 ap->id, dev->devno, xfer_mode_str[idx]);
1556 static int ata_host_set_pio(struct ata_port *ap)
1562 mask = ata_get_mode_mask(ap, ATA_SHIFT_PIO);
1565 printk(KERN_WARNING "ata%u: no PIO support\n", ap->id);
1569 base = base_from_shift(ATA_SHIFT_PIO);
1570 xfer_mode = base + x;
1572 DPRINTK("base 0x%x xfer_mode 0x%x mask 0x%x x %d\n",
1573 (int)base, (int)xfer_mode, mask, x);
1575 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1576 struct ata_device *dev = &ap->device[i];
1577 if (ata_dev_present(dev)) {
1578 dev->pio_mode = xfer_mode;
1579 dev->xfer_mode = xfer_mode;
1580 dev->xfer_shift = ATA_SHIFT_PIO;
1581 if (ap->ops->set_piomode)
1582 ap->ops->set_piomode(ap, dev);
1589 static void ata_host_set_dma(struct ata_port *ap, u8 xfer_mode,
1590 unsigned int xfer_shift)
1594 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1595 struct ata_device *dev = &ap->device[i];
1596 if (ata_dev_present(dev)) {
1597 dev->dma_mode = xfer_mode;
1598 dev->xfer_mode = xfer_mode;
1599 dev->xfer_shift = xfer_shift;
1600 if (ap->ops->set_dmamode)
1601 ap->ops->set_dmamode(ap, dev);
1607 * ata_set_mode - Program timings and issue SET FEATURES - XFER
1608 * @ap: port on which timings will be programmed
1610 * Set ATA device disk transfer mode (PIO3, UDMA6, etc.).
1613 * PCI/etc. bus probe sem.
1616 static void ata_set_mode(struct ata_port *ap)
1618 unsigned int xfer_shift;
1622 /* step 1: always set host PIO timings */
1623 rc = ata_host_set_pio(ap);
1627 /* step 2: choose the best data xfer mode */
1628 xfer_mode = xfer_shift = 0;
1629 rc = ata_choose_xfer_mode(ap, &xfer_mode, &xfer_shift);
1633 /* step 3: if that xfer mode isn't PIO, set host DMA timings */
1634 if (xfer_shift != ATA_SHIFT_PIO)
1635 ata_host_set_dma(ap, xfer_mode, xfer_shift);
1637 /* step 4: update devices' xfer mode */
1638 ata_dev_set_mode(ap, &ap->device[0]);
1639 ata_dev_set_mode(ap, &ap->device[1]);
1641 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1644 if (ap->ops->post_set_mode)
1645 ap->ops->post_set_mode(ap);
1650 ata_port_disable(ap);
1654 * ata_busy_sleep - sleep until BSY clears, or timeout
1655 * @ap: port containing status register to be polled
1656 * @tmout_pat: impatience timeout
1657 * @tmout: overall timeout
1659 * Sleep until ATA Status register bit BSY clears,
1660 * or a timeout occurs.
1666 static unsigned int ata_busy_sleep (struct ata_port *ap,
1667 unsigned long tmout_pat,
1668 unsigned long tmout)
1670 unsigned long timer_start, timeout;
1673 status = ata_busy_wait(ap, ATA_BUSY, 300);
1674 timer_start = jiffies;
1675 timeout = timer_start + tmout_pat;
1676 while ((status & ATA_BUSY) && (time_before(jiffies, timeout))) {
1678 status = ata_busy_wait(ap, ATA_BUSY, 3);
1681 if (status & ATA_BUSY)
1682 printk(KERN_WARNING "ata%u is slow to respond, "
1683 "please be patient\n", ap->id);
1685 timeout = timer_start + tmout;
1686 while ((status & ATA_BUSY) && (time_before(jiffies, timeout))) {
1688 status = ata_chk_status(ap);
1691 if (status & ATA_BUSY) {
1692 printk(KERN_ERR "ata%u failed to respond (%lu secs)\n",
1693 ap->id, tmout / HZ);
1700 static void ata_bus_post_reset(struct ata_port *ap, unsigned int devmask)
1702 struct ata_ioports *ioaddr = &ap->ioaddr;
1703 unsigned int dev0 = devmask & (1 << 0);
1704 unsigned int dev1 = devmask & (1 << 1);
1705 unsigned long timeout;
1707 /* if device 0 was found in ata_devchk, wait for its
1711 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
1713 /* if device 1 was found in ata_devchk, wait for
1714 * register access, then wait for BSY to clear
1716 timeout = jiffies + ATA_TMOUT_BOOT;
1720 ap->ops->dev_select(ap, 1);
1721 if (ap->flags & ATA_FLAG_MMIO) {
1722 nsect = readb((void __iomem *) ioaddr->nsect_addr);
1723 lbal = readb((void __iomem *) ioaddr->lbal_addr);
1725 nsect = inb(ioaddr->nsect_addr);
1726 lbal = inb(ioaddr->lbal_addr);
1728 if ((nsect == 1) && (lbal == 1))
1730 if (time_after(jiffies, timeout)) {
1734 msleep(50); /* give drive a breather */
1737 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
1739 /* is all this really necessary? */
1740 ap->ops->dev_select(ap, 0);
1742 ap->ops->dev_select(ap, 1);
1744 ap->ops->dev_select(ap, 0);
1748 * ata_bus_edd - Issue EXECUTE DEVICE DIAGNOSTIC command.
1749 * @ap: Port to reset and probe
1751 * Use the EXECUTE DEVICE DIAGNOSTIC command to reset and
1752 * probe the bus. Not often used these days.
1755 * PCI/etc. bus probe sem.
1759 static unsigned int ata_bus_edd(struct ata_port *ap)
1761 struct ata_taskfile tf;
1763 /* set up execute-device-diag (bus reset) taskfile */
1764 /* also, take interrupts to a known state (disabled) */
1765 DPRINTK("execute-device-diag\n");
1766 ata_tf_init(ap, &tf, 0);
1768 tf.command = ATA_CMD_EDD;
1769 tf.protocol = ATA_PROT_NODATA;
1772 ata_tf_to_host(ap, &tf);
1774 /* spec says at least 2ms. but who knows with those
1775 * crazy ATAPI devices...
1779 return ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
1782 static unsigned int ata_bus_softreset(struct ata_port *ap,
1783 unsigned int devmask)
1785 struct ata_ioports *ioaddr = &ap->ioaddr;
1787 DPRINTK("ata%u: bus reset via SRST\n", ap->id);
1789 /* software reset. causes dev0 to be selected */
1790 if (ap->flags & ATA_FLAG_MMIO) {
1791 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
1792 udelay(20); /* FIXME: flush */
1793 writeb(ap->ctl | ATA_SRST, (void __iomem *) ioaddr->ctl_addr);
1794 udelay(20); /* FIXME: flush */
1795 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
1797 outb(ap->ctl, ioaddr->ctl_addr);
1799 outb(ap->ctl | ATA_SRST, ioaddr->ctl_addr);
1801 outb(ap->ctl, ioaddr->ctl_addr);
1804 /* spec mandates ">= 2ms" before checking status.
1805 * We wait 150ms, because that was the magic delay used for
1806 * ATAPI devices in Hale Landis's ATADRVR, for the period of time
1807 * between when the ATA command register is written, and then
1808 * status is checked. Because waiting for "a while" before
1809 * checking status is fine, post SRST, we perform this magic
1810 * delay here as well.
1814 ata_bus_post_reset(ap, devmask);
1820 * ata_bus_reset - reset host port and associated ATA channel
1821 * @ap: port to reset
1823 * This is typically the first time we actually start issuing
1824 * commands to the ATA channel. We wait for BSY to clear, then
1825 * issue EXECUTE DEVICE DIAGNOSTIC command, polling for its
1826 * result. Determine what devices, if any, are on the channel
1827 * by looking at the device 0/1 error register. Look at the signature
1828 * stored in each device's taskfile registers, to determine if
1829 * the device is ATA or ATAPI.
1832 * PCI/etc. bus probe sem.
1833 * Obtains host_set lock.
1836 * Sets ATA_FLAG_PORT_DISABLED if bus reset fails.
1839 void ata_bus_reset(struct ata_port *ap)
1841 struct ata_ioports *ioaddr = &ap->ioaddr;
1842 unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS;
1844 unsigned int dev0, dev1 = 0, rc = 0, devmask = 0;
1846 DPRINTK("ENTER, host %u, port %u\n", ap->id, ap->port_no);
1848 /* determine if device 0/1 are present */
1849 if (ap->flags & ATA_FLAG_SATA_RESET)
1852 dev0 = ata_devchk(ap, 0);
1854 dev1 = ata_devchk(ap, 1);
1858 devmask |= (1 << 0);
1860 devmask |= (1 << 1);
1862 /* select device 0 again */
1863 ap->ops->dev_select(ap, 0);
1865 /* issue bus reset */
1866 if (ap->flags & ATA_FLAG_SRST)
1867 rc = ata_bus_softreset(ap, devmask);
1868 else if ((ap->flags & ATA_FLAG_SATA_RESET) == 0) {
1869 /* set up device control */
1870 if (ap->flags & ATA_FLAG_MMIO)
1871 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
1873 outb(ap->ctl, ioaddr->ctl_addr);
1874 rc = ata_bus_edd(ap);
1881 * determine by signature whether we have ATA or ATAPI devices
1883 err = ata_dev_try_classify(ap, 0);
1884 if ((slave_possible) && (err != 0x81))
1885 ata_dev_try_classify(ap, 1);
1887 /* re-enable interrupts */
1888 if (ap->ioaddr.ctl_addr) /* FIXME: hack. create a hook instead */
1891 /* is double-select really necessary? */
1892 if (ap->device[1].class != ATA_DEV_NONE)
1893 ap->ops->dev_select(ap, 1);
1894 if (ap->device[0].class != ATA_DEV_NONE)
1895 ap->ops->dev_select(ap, 0);
1897 /* if no devices were detected, disable this port */
1898 if ((ap->device[0].class == ATA_DEV_NONE) &&
1899 (ap->device[1].class == ATA_DEV_NONE))
1902 if (ap->flags & (ATA_FLAG_SATA_RESET | ATA_FLAG_SRST)) {
1903 /* set up device control for ATA_FLAG_SATA_RESET */
1904 if (ap->flags & ATA_FLAG_MMIO)
1905 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
1907 outb(ap->ctl, ioaddr->ctl_addr);
1914 printk(KERN_ERR "ata%u: disabling port\n", ap->id);
1915 ap->ops->port_disable(ap);
1920 static void ata_pr_blacklisted(struct ata_port *ap, struct ata_device *dev)
1922 printk(KERN_WARNING "ata%u: dev %u is on DMA blacklist, disabling DMA\n",
1923 ap->id, dev->devno);
1926 static const char * ata_dma_blacklist [] = {
1945 "Toshiba CD-ROM XM-6202B",
1946 "TOSHIBA CD-ROM XM-1702BC",
1948 "E-IDE CD-ROM CR-840",
1951 "SAMSUNG CD-ROM SC-148C",
1952 "SAMSUNG CD-ROM SC",
1954 "ATAPI CD-ROM DRIVE 40X MAXIMUM",
1958 static int ata_dma_blacklisted(struct ata_port *ap, struct ata_device *dev)
1960 unsigned char model_num[40];
1965 ata_dev_id_string(dev->id, model_num, ATA_ID_PROD_OFS,
1968 len = strnlen(s, sizeof(model_num));
1970 /* ATAPI specifies that empty space is blank-filled; remove blanks */
1971 while ((len > 0) && (s[len - 1] == ' ')) {
1976 for (i = 0; i < ARRAY_SIZE(ata_dma_blacklist); i++)
1977 if (!strncmp(ata_dma_blacklist[i], s, len))
1983 static unsigned int ata_get_mode_mask(struct ata_port *ap, int shift)
1985 struct ata_device *master, *slave;
1988 master = &ap->device[0];
1989 slave = &ap->device[1];
1991 assert (ata_dev_present(master) || ata_dev_present(slave));
1993 if (shift == ATA_SHIFT_UDMA) {
1994 mask = ap->udma_mask;
1995 if (ata_dev_present(master)) {
1996 mask &= (master->id[ATA_ID_UDMA_MODES] & 0xff);
1997 if (ata_dma_blacklisted(ap, master)) {
1999 ata_pr_blacklisted(ap, master);
2002 if (ata_dev_present(slave)) {
2003 mask &= (slave->id[ATA_ID_UDMA_MODES] & 0xff);
2004 if (ata_dma_blacklisted(ap, slave)) {
2006 ata_pr_blacklisted(ap, slave);
2010 else if (shift == ATA_SHIFT_MWDMA) {
2011 mask = ap->mwdma_mask;
2012 if (ata_dev_present(master)) {
2013 mask &= (master->id[ATA_ID_MWDMA_MODES] & 0x07);
2014 if (ata_dma_blacklisted(ap, master)) {
2016 ata_pr_blacklisted(ap, master);
2019 if (ata_dev_present(slave)) {
2020 mask &= (slave->id[ATA_ID_MWDMA_MODES] & 0x07);
2021 if (ata_dma_blacklisted(ap, slave)) {
2023 ata_pr_blacklisted(ap, slave);
2027 else if (shift == ATA_SHIFT_PIO) {
2028 mask = ap->pio_mask;
2029 if (ata_dev_present(master)) {
2030 /* spec doesn't return explicit support for
2031 * PIO0-2, so we fake it
2033 u16 tmp_mode = master->id[ATA_ID_PIO_MODES] & 0x03;
2038 if (ata_dev_present(slave)) {
2039 /* spec doesn't return explicit support for
2040 * PIO0-2, so we fake it
2042 u16 tmp_mode = slave->id[ATA_ID_PIO_MODES] & 0x03;
2049 mask = 0xffffffff; /* shut up compiler warning */
2056 /* find greatest bit */
2057 static int fgb(u32 bitmap)
2062 for (i = 0; i < 32; i++)
2063 if (bitmap & (1 << i))
2070 * ata_choose_xfer_mode - attempt to find best transfer mode
2071 * @ap: Port for which an xfer mode will be selected
2072 * @xfer_mode_out: (output) SET FEATURES - XFER MODE code
2073 * @xfer_shift_out: (output) bit shift that selects this mode
2075 * Based on host and device capabilities, determine the
2076 * maximum transfer mode that is amenable to all.
2079 * PCI/etc. bus probe sem.
2082 * Zero on success, negative on error.
2085 static int ata_choose_xfer_mode(struct ata_port *ap,
2087 unsigned int *xfer_shift_out)
2089 unsigned int mask, shift;
2092 for (i = 0; i < ARRAY_SIZE(xfer_mode_classes); i++) {
2093 shift = xfer_mode_classes[i].shift;
2094 mask = ata_get_mode_mask(ap, shift);
2098 *xfer_mode_out = xfer_mode_classes[i].base + x;
2099 *xfer_shift_out = shift;
2108 * ata_dev_set_xfermode - Issue SET FEATURES - XFER MODE command
2109 * @ap: Port associated with device @dev
2110 * @dev: Device to which command will be sent
2112 * Issue SET FEATURES - XFER MODE command to device @dev
2116 * PCI/etc. bus probe sem.
2119 static void ata_dev_set_xfermode(struct ata_port *ap, struct ata_device *dev)
2121 DECLARE_COMPLETION(wait);
2122 struct ata_queued_cmd *qc;
2124 unsigned long flags;
2126 /* set up set-features taskfile */
2127 DPRINTK("set features - xfer mode\n");
2129 qc = ata_qc_new_init(ap, dev);
2132 qc->tf.command = ATA_CMD_SET_FEATURES;
2133 qc->tf.feature = SETFEATURES_XFER;
2134 qc->tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
2135 qc->tf.protocol = ATA_PROT_NODATA;
2136 qc->tf.nsect = dev->xfer_mode;
2138 qc->waiting = &wait;
2139 qc->complete_fn = ata_qc_complete_noop;
2141 spin_lock_irqsave(&ap->host_set->lock, flags);
2142 rc = ata_qc_issue(qc);
2143 spin_unlock_irqrestore(&ap->host_set->lock, flags);
2146 ata_port_disable(ap);
2148 wait_for_completion(&wait);
2154 * ata_dev_init_params - Issue INIT DEV PARAMS command
2155 * @ap: Port associated with device @dev
2156 * @dev: Device to which command will be sent
2161 static void ata_dev_init_params(struct ata_port *ap, struct ata_device *dev)
2163 DECLARE_COMPLETION(wait);
2164 struct ata_queued_cmd *qc;
2166 unsigned long flags;
2167 u16 sectors = dev->id[6];
2168 u16 heads = dev->id[3];
2170 /* Number of sectors per track 1-255. Number of heads 1-16 */
2171 if (sectors < 1 || sectors > 255 || heads < 1 || heads > 16)
2174 /* set up init dev params taskfile */
2175 DPRINTK("init dev params \n");
2177 qc = ata_qc_new_init(ap, dev);
2180 qc->tf.command = ATA_CMD_INIT_DEV_PARAMS;
2181 qc->tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
2182 qc->tf.protocol = ATA_PROT_NODATA;
2183 qc->tf.nsect = sectors;
2184 qc->tf.device |= (heads - 1) & 0x0f; /* max head = num. of heads - 1 */
2186 qc->waiting = &wait;
2187 qc->complete_fn = ata_qc_complete_noop;
2189 spin_lock_irqsave(&ap->host_set->lock, flags);
2190 rc = ata_qc_issue(qc);
2191 spin_unlock_irqrestore(&ap->host_set->lock, flags);
2194 ata_port_disable(ap);
2196 wait_for_completion(&wait);
2202 * ata_sg_clean - Unmap DMA memory associated with command
2203 * @qc: Command containing DMA memory to be released
2205 * Unmap all mapped DMA memory associated with this command.
2208 * spin_lock_irqsave(host_set lock)
2211 static void ata_sg_clean(struct ata_queued_cmd *qc)
2213 struct ata_port *ap = qc->ap;
2214 struct scatterlist *sg = qc->sg;
2215 int dir = qc->dma_dir;
2217 assert(qc->flags & ATA_QCFLAG_DMAMAP);
2220 if (qc->flags & ATA_QCFLAG_SINGLE)
2221 assert(qc->n_elem == 1);
2223 DPRINTK("unmapping %u sg elements\n", qc->n_elem);
2225 if (qc->flags & ATA_QCFLAG_SG)
2226 dma_unmap_sg(ap->host_set->dev, sg, qc->n_elem, dir);
2228 dma_unmap_single(ap->host_set->dev, sg_dma_address(&sg[0]),
2229 sg_dma_len(&sg[0]), dir);
2231 qc->flags &= ~ATA_QCFLAG_DMAMAP;
2236 * ata_fill_sg - Fill PCI IDE PRD table
2237 * @qc: Metadata associated with taskfile to be transferred
2239 * Fill PCI IDE PRD (scatter-gather) table with segments
2240 * associated with the current disk command.
2243 * spin_lock_irqsave(host_set lock)
2246 static void ata_fill_sg(struct ata_queued_cmd *qc)
2248 struct scatterlist *sg = qc->sg;
2249 struct ata_port *ap = qc->ap;
2250 unsigned int idx, nelem;
2253 assert(qc->n_elem > 0);
2256 for (nelem = qc->n_elem; nelem; nelem--,sg++) {
2260 /* determine if physical DMA addr spans 64K boundary.
2261 * Note h/w doesn't support 64-bit, so we unconditionally
2262 * truncate dma_addr_t to u32.
2264 addr = (u32) sg_dma_address(sg);
2265 sg_len = sg_dma_len(sg);
2268 offset = addr & 0xffff;
2270 if ((offset + sg_len) > 0x10000)
2271 len = 0x10000 - offset;
2273 ap->prd[idx].addr = cpu_to_le32(addr);
2274 ap->prd[idx].flags_len = cpu_to_le32(len & 0xffff);
2275 VPRINTK("PRD[%u] = (0x%X, 0x%X)\n", idx, addr, len);
2284 ap->prd[idx - 1].flags_len |= cpu_to_le32(ATA_PRD_EOT);
2287 * ata_check_atapi_dma - Check whether ATAPI DMA can be supported
2288 * @qc: Metadata associated with taskfile to check
2290 * Allow low-level driver to filter ATA PACKET commands, returning
2291 * a status indicating whether or not it is OK to use DMA for the
2292 * supplied PACKET command.
2295 * spin_lock_irqsave(host_set lock)
2297 * RETURNS: 0 when ATAPI DMA can be used
2300 int ata_check_atapi_dma(struct ata_queued_cmd *qc)
2302 struct ata_port *ap = qc->ap;
2303 int rc = 0; /* Assume ATAPI DMA is OK by default */
2305 if (ap->ops->check_atapi_dma)
2306 rc = ap->ops->check_atapi_dma(qc);
2311 * ata_qc_prep - Prepare taskfile for submission
2312 * @qc: Metadata associated with taskfile to be prepared
2314 * Prepare ATA taskfile for submission.
2317 * spin_lock_irqsave(host_set lock)
2319 void ata_qc_prep(struct ata_queued_cmd *qc)
2321 if (!(qc->flags & ATA_QCFLAG_DMAMAP))
2328 * ata_sg_init_one - Associate command with memory buffer
2329 * @qc: Command to be associated
2330 * @buf: Memory buffer
2331 * @buflen: Length of memory buffer, in bytes.
2333 * Initialize the data-related elements of queued_cmd @qc
2334 * to point to a single memory buffer, @buf of byte length @buflen.
2337 * spin_lock_irqsave(host_set lock)
2340 void ata_sg_init_one(struct ata_queued_cmd *qc, void *buf, unsigned int buflen)
2342 struct scatterlist *sg;
2344 qc->flags |= ATA_QCFLAG_SINGLE;
2346 memset(&qc->sgent, 0, sizeof(qc->sgent));
2347 qc->sg = &qc->sgent;
2352 sg->page = virt_to_page(buf);
2353 sg->offset = (unsigned long) buf & ~PAGE_MASK;
2354 sg->length = buflen;
2358 * ata_sg_init - Associate command with scatter-gather table.
2359 * @qc: Command to be associated
2360 * @sg: Scatter-gather table.
2361 * @n_elem: Number of elements in s/g table.
2363 * Initialize the data-related elements of queued_cmd @qc
2364 * to point to a scatter-gather table @sg, containing @n_elem
2368 * spin_lock_irqsave(host_set lock)
2371 void ata_sg_init(struct ata_queued_cmd *qc, struct scatterlist *sg,
2372 unsigned int n_elem)
2374 qc->flags |= ATA_QCFLAG_SG;
2376 qc->n_elem = n_elem;
2380 * ata_sg_setup_one - DMA-map the memory buffer associated with a command.
2381 * @qc: Command with memory buffer to be mapped.
2383 * DMA-map the memory buffer associated with queued_cmd @qc.
2386 * spin_lock_irqsave(host_set lock)
2389 * Zero on success, negative on error.
2392 static int ata_sg_setup_one(struct ata_queued_cmd *qc)
2394 struct ata_port *ap = qc->ap;
2395 int dir = qc->dma_dir;
2396 struct scatterlist *sg = qc->sg;
2397 dma_addr_t dma_address;
2399 dma_address = dma_map_single(ap->host_set->dev, qc->buf_virt,
2401 if (dma_mapping_error(dma_address))
2404 sg_dma_address(sg) = dma_address;
2405 sg_dma_len(sg) = sg->length;
2407 DPRINTK("mapped buffer of %d bytes for %s\n", sg_dma_len(sg),
2408 qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
2414 * ata_sg_setup - DMA-map the scatter-gather table associated with a command.
2415 * @qc: Command with scatter-gather table to be mapped.
2417 * DMA-map the scatter-gather table associated with queued_cmd @qc.
2420 * spin_lock_irqsave(host_set lock)
2423 * Zero on success, negative on error.
2427 static int ata_sg_setup(struct ata_queued_cmd *qc)
2429 struct ata_port *ap = qc->ap;
2430 struct scatterlist *sg = qc->sg;
2433 VPRINTK("ENTER, ata%u\n", ap->id);
2434 assert(qc->flags & ATA_QCFLAG_SG);
2437 n_elem = dma_map_sg(ap->host_set->dev, sg, qc->n_elem, dir);
2441 DPRINTK("%d sg elements mapped\n", n_elem);
2443 qc->n_elem = n_elem;
2449 * ata_poll_qc_complete - turn irq back on and finish qc
2450 * @qc: Command to complete
2451 * @drv_stat: ATA status register content
2454 * None. (grabs host lock)
2457 void ata_poll_qc_complete(struct ata_queued_cmd *qc, u8 drv_stat)
2459 struct ata_port *ap = qc->ap;
2460 unsigned long flags;
2462 spin_lock_irqsave(&ap->host_set->lock, flags);
2463 ap->flags &= ~ATA_FLAG_NOINTR;
2465 ata_qc_complete(qc, drv_stat);
2466 spin_unlock_irqrestore(&ap->host_set->lock, flags);
2474 * None. (executing in kernel thread context)
2480 static unsigned long ata_pio_poll(struct ata_port *ap)
2483 unsigned int poll_state = HSM_ST_UNKNOWN;
2484 unsigned int reg_state = HSM_ST_UNKNOWN;
2485 const unsigned int tmout_state = HSM_ST_TMOUT;
2487 switch (ap->hsm_task_state) {
2490 poll_state = HSM_ST_POLL;
2494 case HSM_ST_LAST_POLL:
2495 poll_state = HSM_ST_LAST_POLL;
2496 reg_state = HSM_ST_LAST;
2503 status = ata_chk_status(ap);
2504 if (status & ATA_BUSY) {
2505 if (time_after(jiffies, ap->pio_task_timeout)) {
2506 ap->hsm_task_state = tmout_state;
2509 ap->hsm_task_state = poll_state;
2510 return ATA_SHORT_PAUSE;
2513 ap->hsm_task_state = reg_state;
2518 * ata_pio_complete -
2522 * None. (executing in kernel thread context)
2525 * Non-zero if qc completed, zero otherwise.
2528 static int ata_pio_complete (struct ata_port *ap)
2530 struct ata_queued_cmd *qc;
2534 * This is purely heuristic. This is a fast path. Sometimes when
2535 * we enter, BSY will be cleared in a chk-status or two. If not,
2536 * the drive is probably seeking or something. Snooze for a couple
2537 * msecs, then chk-status again. If still busy, fall back to
2538 * HSM_ST_POLL state.
2540 drv_stat = ata_busy_wait(ap, ATA_BUSY | ATA_DRQ, 10);
2541 if (drv_stat & (ATA_BUSY | ATA_DRQ)) {
2543 drv_stat = ata_busy_wait(ap, ATA_BUSY | ATA_DRQ, 10);
2544 if (drv_stat & (ATA_BUSY | ATA_DRQ)) {
2545 ap->hsm_task_state = HSM_ST_LAST_POLL;
2546 ap->pio_task_timeout = jiffies + ATA_TMOUT_PIO;
2551 drv_stat = ata_wait_idle(ap);
2552 if (!ata_ok(drv_stat)) {
2553 ap->hsm_task_state = HSM_ST_ERR;
2557 qc = ata_qc_from_tag(ap, ap->active_tag);
2560 ap->hsm_task_state = HSM_ST_IDLE;
2562 ata_poll_qc_complete(qc, drv_stat);
2564 /* another command may start at this point */
2572 * @buf: Buffer to swap
2573 * @buf_words: Number of 16-bit words in buffer.
2575 * Swap halves of 16-bit words if needed to convert from
2576 * little-endian byte order to native cpu byte order, or
2581 void swap_buf_le16(u16 *buf, unsigned int buf_words)
2586 for (i = 0; i < buf_words; i++)
2587 buf[i] = le16_to_cpu(buf[i]);
2588 #endif /* __BIG_ENDIAN */
2592 * ata_mmio_data_xfer - Transfer data by MMIO
2593 * @ap: port to read/write
2595 * @buflen: buffer length
2596 * @write_data: read/write
2598 * Transfer data from/to the device data register by MMIO.
2601 * Inherited from caller.
2605 static void ata_mmio_data_xfer(struct ata_port *ap, unsigned char *buf,
2606 unsigned int buflen, int write_data)
2609 unsigned int words = buflen >> 1;
2610 u16 *buf16 = (u16 *) buf;
2611 void __iomem *mmio = (void __iomem *)ap->ioaddr.data_addr;
2613 /* Transfer multiple of 2 bytes */
2615 for (i = 0; i < words; i++)
2616 writew(le16_to_cpu(buf16[i]), mmio);
2618 for (i = 0; i < words; i++)
2619 buf16[i] = cpu_to_le16(readw(mmio));
2622 /* Transfer trailing 1 byte, if any. */
2623 if (unlikely(buflen & 0x01)) {
2624 u16 align_buf[1] = { 0 };
2625 unsigned char *trailing_buf = buf + buflen - 1;
2628 memcpy(align_buf, trailing_buf, 1);
2629 writew(le16_to_cpu(align_buf[0]), mmio);
2631 align_buf[0] = cpu_to_le16(readw(mmio));
2632 memcpy(trailing_buf, align_buf, 1);
2638 * ata_pio_data_xfer - Transfer data by PIO
2639 * @ap: port to read/write
2641 * @buflen: buffer length
2642 * @write_data: read/write
2644 * Transfer data from/to the device data register by PIO.
2647 * Inherited from caller.
2651 static void ata_pio_data_xfer(struct ata_port *ap, unsigned char *buf,
2652 unsigned int buflen, int write_data)
2654 unsigned int words = buflen >> 1;
2656 /* Transfer multiple of 2 bytes */
2658 outsw(ap->ioaddr.data_addr, buf, words);
2660 insw(ap->ioaddr.data_addr, buf, words);
2662 /* Transfer trailing 1 byte, if any. */
2663 if (unlikely(buflen & 0x01)) {
2664 u16 align_buf[1] = { 0 };
2665 unsigned char *trailing_buf = buf + buflen - 1;
2668 memcpy(align_buf, trailing_buf, 1);
2669 outw(le16_to_cpu(align_buf[0]), ap->ioaddr.data_addr);
2671 align_buf[0] = cpu_to_le16(inw(ap->ioaddr.data_addr));
2672 memcpy(trailing_buf, align_buf, 1);
2678 * ata_data_xfer - Transfer data from/to the data register.
2679 * @ap: port to read/write
2681 * @buflen: buffer length
2682 * @do_write: read/write
2684 * Transfer data from/to the device data register.
2687 * Inherited from caller.
2691 static void ata_data_xfer(struct ata_port *ap, unsigned char *buf,
2692 unsigned int buflen, int do_write)
2694 if (ap->flags & ATA_FLAG_MMIO)
2695 ata_mmio_data_xfer(ap, buf, buflen, do_write);
2697 ata_pio_data_xfer(ap, buf, buflen, do_write);
2701 * ata_pio_sector - Transfer ATA_SECT_SIZE (512 bytes) of data.
2702 * @qc: Command on going
2704 * Transfer ATA_SECT_SIZE of data from/to the ATA device.
2707 * Inherited from caller.
2710 static void ata_pio_sector(struct ata_queued_cmd *qc)
2712 int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
2713 struct scatterlist *sg = qc->sg;
2714 struct ata_port *ap = qc->ap;
2716 unsigned int offset;
2719 if (qc->cursect == (qc->nsect - 1))
2720 ap->hsm_task_state = HSM_ST_LAST;
2722 page = sg[qc->cursg].page;
2723 offset = sg[qc->cursg].offset + qc->cursg_ofs * ATA_SECT_SIZE;
2725 /* get the current page and offset */
2726 page = nth_page(page, (offset >> PAGE_SHIFT));
2727 offset %= PAGE_SIZE;
2729 buf = kmap(page) + offset;
2734 if ((qc->cursg_ofs * ATA_SECT_SIZE) == (&sg[qc->cursg])->length) {
2739 DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
2741 /* do the actual data transfer */
2742 do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
2743 ata_data_xfer(ap, buf, ATA_SECT_SIZE, do_write);
2749 * __atapi_pio_bytes - Transfer data from/to the ATAPI device.
2750 * @qc: Command on going
2751 * @bytes: number of bytes
2753 * Transfer Transfer data from/to the ATAPI device.
2756 * Inherited from caller.
2760 static void __atapi_pio_bytes(struct ata_queued_cmd *qc, unsigned int bytes)
2762 int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
2763 struct scatterlist *sg = qc->sg;
2764 struct ata_port *ap = qc->ap;
2767 unsigned int offset, count;
2769 if (qc->curbytes + bytes >= qc->nbytes)
2770 ap->hsm_task_state = HSM_ST_LAST;
2773 if (unlikely(qc->cursg >= qc->n_elem)) {
2775 * The end of qc->sg is reached and the device expects
2776 * more data to transfer. In order not to overrun qc->sg
2777 * and fulfill length specified in the byte count register,
2778 * - for read case, discard trailing data from the device
2779 * - for write case, padding zero data to the device
2781 u16 pad_buf[1] = { 0 };
2782 unsigned int words = bytes >> 1;
2785 if (words) /* warning if bytes > 1 */
2786 printk(KERN_WARNING "ata%u: %u bytes trailing data\n",
2789 for (i = 0; i < words; i++)
2790 ata_data_xfer(ap, (unsigned char*)pad_buf, 2, do_write);
2792 ap->hsm_task_state = HSM_ST_LAST;
2796 sg = &qc->sg[qc->cursg];
2799 offset = sg->offset + qc->cursg_ofs;
2801 /* get the current page and offset */
2802 page = nth_page(page, (offset >> PAGE_SHIFT));
2803 offset %= PAGE_SIZE;
2805 /* don't overrun current sg */
2806 count = min(sg->length - qc->cursg_ofs, bytes);
2808 /* don't cross page boundaries */
2809 count = min(count, (unsigned int)PAGE_SIZE - offset);
2811 buf = kmap(page) + offset;
2814 qc->curbytes += count;
2815 qc->cursg_ofs += count;
2817 if (qc->cursg_ofs == sg->length) {
2822 DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
2824 /* do the actual data transfer */
2825 ata_data_xfer(ap, buf, count, do_write);
2834 * atapi_pio_bytes - Transfer data from/to the ATAPI device.
2835 * @qc: Command on going
2837 * Transfer Transfer data from/to the ATAPI device.
2840 * Inherited from caller.
2844 static void atapi_pio_bytes(struct ata_queued_cmd *qc)
2846 struct ata_port *ap = qc->ap;
2847 struct ata_device *dev = qc->dev;
2848 unsigned int ireason, bc_lo, bc_hi, bytes;
2849 int i_write, do_write = (qc->tf.flags & ATA_TFLAG_WRITE) ? 1 : 0;
2851 ap->ops->tf_read(ap, &qc->tf);
2852 ireason = qc->tf.nsect;
2853 bc_lo = qc->tf.lbam;
2854 bc_hi = qc->tf.lbah;
2855 bytes = (bc_hi << 8) | bc_lo;
2857 /* shall be cleared to zero, indicating xfer of data */
2858 if (ireason & (1 << 0))
2861 /* make sure transfer direction matches expected */
2862 i_write = ((ireason & (1 << 1)) == 0) ? 1 : 0;
2863 if (do_write != i_write)
2866 __atapi_pio_bytes(qc, bytes);
2871 printk(KERN_INFO "ata%u: dev %u: ATAPI check failed\n",
2872 ap->id, dev->devno);
2873 ap->hsm_task_state = HSM_ST_ERR;
2881 * None. (executing in kernel thread context)
2884 static void ata_pio_block(struct ata_port *ap)
2886 struct ata_queued_cmd *qc;
2890 * This is purely hueristic. This is a fast path.
2891 * Sometimes when we enter, BSY will be cleared in
2892 * a chk-status or two. If not, the drive is probably seeking
2893 * or something. Snooze for a couple msecs, then
2894 * chk-status again. If still busy, fall back to
2895 * HSM_ST_POLL state.
2897 status = ata_busy_wait(ap, ATA_BUSY, 5);
2898 if (status & ATA_BUSY) {
2900 status = ata_busy_wait(ap, ATA_BUSY, 10);
2901 if (status & ATA_BUSY) {
2902 ap->hsm_task_state = HSM_ST_POLL;
2903 ap->pio_task_timeout = jiffies + ATA_TMOUT_PIO;
2908 qc = ata_qc_from_tag(ap, ap->active_tag);
2911 if (is_atapi_taskfile(&qc->tf)) {
2912 /* no more data to transfer or unsupported ATAPI command */
2913 if ((status & ATA_DRQ) == 0) {
2914 ap->hsm_task_state = HSM_ST_LAST;
2918 atapi_pio_bytes(qc);
2920 /* handle BSY=0, DRQ=0 as error */
2921 if ((status & ATA_DRQ) == 0) {
2922 ap->hsm_task_state = HSM_ST_ERR;
2930 static void ata_pio_error(struct ata_port *ap)
2932 struct ata_queued_cmd *qc;
2935 qc = ata_qc_from_tag(ap, ap->active_tag);
2938 drv_stat = ata_chk_status(ap);
2939 printk(KERN_WARNING "ata%u: PIO error, drv_stat 0x%x\n",
2942 ap->hsm_task_state = HSM_ST_IDLE;
2944 ata_poll_qc_complete(qc, drv_stat | ATA_ERR);
2947 static void ata_pio_task(void *_data)
2949 struct ata_port *ap = _data;
2950 unsigned long timeout;
2957 switch (ap->hsm_task_state) {
2966 qc_completed = ata_pio_complete(ap);
2970 case HSM_ST_LAST_POLL:
2971 timeout = ata_pio_poll(ap);
2981 queue_delayed_work(ata_wq, &ap->pio_task, timeout);
2982 else if (!qc_completed)
2987 * ata_qc_timeout - Handle timeout of queued command
2988 * @qc: Command that timed out
2990 * Some part of the kernel (currently, only the SCSI layer)
2991 * has noticed that the active command on port @ap has not
2992 * completed after a specified length of time. Handle this
2993 * condition by disabling DMA (if necessary) and completing
2994 * transactions, with error if necessary.
2996 * This also handles the case of the "lost interrupt", where
2997 * for some reason (possibly hardware bug, possibly driver bug)
2998 * an interrupt was not delivered to the driver, even though the
2999 * transaction completed successfully.
3002 * Inherited from SCSI layer (none, can sleep)
3005 static void ata_qc_timeout(struct ata_queued_cmd *qc)
3007 struct ata_port *ap = qc->ap;
3008 struct ata_host_set *host_set = ap->host_set;
3009 struct ata_device *dev = qc->dev;
3010 u8 host_stat = 0, drv_stat;
3011 unsigned long flags;
3015 /* FIXME: doesn't this conflict with timeout handling? */
3016 if (qc->dev->class == ATA_DEV_ATAPI && qc->scsicmd) {
3017 struct scsi_cmnd *cmd = qc->scsicmd;
3019 if (!(cmd->eh_eflags & SCSI_EH_CANCEL_CMD)) {
3021 /* finish completing original command */
3022 spin_lock_irqsave(&host_set->lock, flags);
3023 __ata_qc_complete(qc);
3024 spin_unlock_irqrestore(&host_set->lock, flags);
3026 atapi_request_sense(ap, dev, cmd);
3028 cmd->result = (CHECK_CONDITION << 1) | (DID_OK << 16);
3029 scsi_finish_command(cmd);
3035 spin_lock_irqsave(&host_set->lock, flags);
3037 /* hack alert! We cannot use the supplied completion
3038 * function from inside the ->eh_strategy_handler() thread.
3039 * libata is the only user of ->eh_strategy_handler() in
3040 * any kernel, so the default scsi_done() assumes it is
3041 * not being called from the SCSI EH.
3043 qc->scsidone = scsi_finish_command;
3045 switch (qc->tf.protocol) {
3048 case ATA_PROT_ATAPI_DMA:
3049 host_stat = ap->ops->bmdma_status(ap);
3051 /* before we do anything else, clear DMA-Start bit */
3052 ap->ops->bmdma_stop(qc);
3058 drv_stat = ata_chk_status(ap);
3060 /* ack bmdma irq events */
3061 ap->ops->irq_clear(ap);
3063 printk(KERN_ERR "ata%u: command 0x%x timeout, stat 0x%x host_stat 0x%x\n",
3064 ap->id, qc->tf.command, drv_stat, host_stat);
3066 /* complete taskfile transaction */
3067 ata_qc_complete(qc, drv_stat);
3071 spin_unlock_irqrestore(&host_set->lock, flags);
3078 * ata_eng_timeout - Handle timeout of queued command
3079 * @ap: Port on which timed-out command is active
3081 * Some part of the kernel (currently, only the SCSI layer)
3082 * has noticed that the active command on port @ap has not
3083 * completed after a specified length of time. Handle this
3084 * condition by disabling DMA (if necessary) and completing
3085 * transactions, with error if necessary.
3087 * This also handles the case of the "lost interrupt", where
3088 * for some reason (possibly hardware bug, possibly driver bug)
3089 * an interrupt was not delivered to the driver, even though the
3090 * transaction completed successfully.
3093 * Inherited from SCSI layer (none, can sleep)
3096 void ata_eng_timeout(struct ata_port *ap)
3098 struct ata_queued_cmd *qc;
3102 qc = ata_qc_from_tag(ap, ap->active_tag);
3106 printk(KERN_ERR "ata%u: BUG: timeout without command\n",
3116 * ata_qc_new - Request an available ATA command, for queueing
3117 * @ap: Port associated with device @dev
3118 * @dev: Device from whom we request an available command structure
3124 static struct ata_queued_cmd *ata_qc_new(struct ata_port *ap)
3126 struct ata_queued_cmd *qc = NULL;
3129 for (i = 0; i < ATA_MAX_QUEUE; i++)
3130 if (!test_and_set_bit(i, &ap->qactive)) {
3131 qc = ata_qc_from_tag(ap, i);
3142 * ata_qc_new_init - Request an available ATA command, and initialize it
3143 * @ap: Port associated with device @dev
3144 * @dev: Device from whom we request an available command structure
3150 struct ata_queued_cmd *ata_qc_new_init(struct ata_port *ap,
3151 struct ata_device *dev)
3153 struct ata_queued_cmd *qc;
3155 qc = ata_qc_new(ap);
3162 qc->cursect = qc->cursg = qc->cursg_ofs = 0;
3164 qc->nbytes = qc->curbytes = 0;
3166 ata_tf_init(ap, &qc->tf, dev->devno);
3172 int ata_qc_complete_noop(struct ata_queued_cmd *qc, u8 drv_stat)
3177 static void __ata_qc_complete(struct ata_queued_cmd *qc)
3179 struct ata_port *ap = qc->ap;
3180 unsigned int tag, do_clear = 0;
3184 if (likely(ata_tag_valid(tag))) {
3185 if (tag == ap->active_tag)
3186 ap->active_tag = ATA_TAG_POISON;
3187 qc->tag = ATA_TAG_POISON;
3192 struct completion *waiting = qc->waiting;
3197 if (likely(do_clear))
3198 clear_bit(tag, &ap->qactive);
3202 * ata_qc_free - free unused ata_queued_cmd
3203 * @qc: Command to complete
3205 * Designed to free unused ata_queued_cmd object
3206 * in case something prevents using it.
3209 * spin_lock_irqsave(host_set lock)
3212 void ata_qc_free(struct ata_queued_cmd *qc)
3214 assert(qc != NULL); /* ata_qc_from_tag _might_ return NULL */
3215 assert(qc->waiting == NULL); /* nothing should be waiting */
3217 __ata_qc_complete(qc);
3221 * ata_qc_complete - Complete an active ATA command
3222 * @qc: Command to complete
3223 * @drv_stat: ATA Status register contents
3225 * Indicate to the mid and upper layers that an ATA
3226 * command has completed, with either an ok or not-ok status.
3229 * spin_lock_irqsave(host_set lock)
3233 void ata_qc_complete(struct ata_queued_cmd *qc, u8 drv_stat)
3237 assert(qc != NULL); /* ata_qc_from_tag _might_ return NULL */
3238 assert(qc->flags & ATA_QCFLAG_ACTIVE);
3240 if (likely(qc->flags & ATA_QCFLAG_DMAMAP))
3243 /* atapi: mark qc as inactive to prevent the interrupt handler
3244 * from completing the command twice later, before the error handler
3245 * is called. (when rc != 0 and atapi request sense is needed)
3247 qc->flags &= ~ATA_QCFLAG_ACTIVE;
3249 /* call completion callback */
3250 rc = qc->complete_fn(qc, drv_stat);
3252 /* if callback indicates not to complete command (non-zero),
3253 * return immediately
3258 __ata_qc_complete(qc);
3263 static inline int ata_should_dma_map(struct ata_queued_cmd *qc)
3265 struct ata_port *ap = qc->ap;
3267 switch (qc->tf.protocol) {
3269 case ATA_PROT_ATAPI_DMA:
3272 case ATA_PROT_ATAPI:
3274 case ATA_PROT_PIO_MULT:
3275 if (ap->flags & ATA_FLAG_PIO_DMA)
3288 * ata_qc_issue - issue taskfile to device
3289 * @qc: command to issue to device
3291 * Prepare an ATA command to submission to device.
3292 * This includes mapping the data into a DMA-able
3293 * area, filling in the S/G table, and finally
3294 * writing the taskfile to hardware, starting the command.
3297 * spin_lock_irqsave(host_set lock)
3300 * Zero on success, negative on error.
3303 int ata_qc_issue(struct ata_queued_cmd *qc)
3305 struct ata_port *ap = qc->ap;
3307 if (ata_should_dma_map(qc)) {
3308 if (qc->flags & ATA_QCFLAG_SG) {
3309 if (ata_sg_setup(qc))
3311 } else if (qc->flags & ATA_QCFLAG_SINGLE) {
3312 if (ata_sg_setup_one(qc))
3316 qc->flags &= ~ATA_QCFLAG_DMAMAP;
3319 ap->ops->qc_prep(qc);
3321 qc->ap->active_tag = qc->tag;
3322 qc->flags |= ATA_QCFLAG_ACTIVE;
3324 return ap->ops->qc_issue(qc);
3332 * ata_qc_issue_prot - issue taskfile to device in proto-dependent manner
3333 * @qc: command to issue to device
3335 * Using various libata functions and hooks, this function
3336 * starts an ATA command. ATA commands are grouped into
3337 * classes called "protocols", and issuing each type of protocol
3338 * is slightly different.
3340 * May be used as the qc_issue() entry in ata_port_operations.
3343 * spin_lock_irqsave(host_set lock)
3346 * Zero on success, negative on error.
3349 int ata_qc_issue_prot(struct ata_queued_cmd *qc)
3351 struct ata_port *ap = qc->ap;
3353 ata_dev_select(ap, qc->dev->devno, 1, 0);
3355 switch (qc->tf.protocol) {
3356 case ATA_PROT_NODATA:
3357 ata_tf_to_host_nolock(ap, &qc->tf);
3361 ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
3362 ap->ops->bmdma_setup(qc); /* set up bmdma */
3363 ap->ops->bmdma_start(qc); /* initiate bmdma */
3366 case ATA_PROT_PIO: /* load tf registers, initiate polling pio */
3367 ata_qc_set_polling(qc);
3368 ata_tf_to_host_nolock(ap, &qc->tf);
3369 ap->hsm_task_state = HSM_ST;
3370 queue_work(ata_wq, &ap->pio_task);
3373 case ATA_PROT_ATAPI:
3374 ata_qc_set_polling(qc);
3375 ata_tf_to_host_nolock(ap, &qc->tf);
3376 queue_work(ata_wq, &ap->packet_task);
3379 case ATA_PROT_ATAPI_NODATA:
3380 ap->flags |= ATA_FLAG_NOINTR;
3381 ata_tf_to_host_nolock(ap, &qc->tf);
3382 queue_work(ata_wq, &ap->packet_task);
3385 case ATA_PROT_ATAPI_DMA:
3386 ap->flags |= ATA_FLAG_NOINTR;
3387 ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
3388 ap->ops->bmdma_setup(qc); /* set up bmdma */
3389 queue_work(ata_wq, &ap->packet_task);
3401 * ata_bmdma_setup_mmio - Set up PCI IDE BMDMA transaction
3402 * @qc: Info associated with this ATA transaction.
3405 * spin_lock_irqsave(host_set lock)
3408 static void ata_bmdma_setup_mmio (struct ata_queued_cmd *qc)
3410 struct ata_port *ap = qc->ap;
3411 unsigned int rw = (qc->tf.flags & ATA_TFLAG_WRITE);
3413 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
3415 /* load PRD table addr. */
3416 mb(); /* make sure PRD table writes are visible to controller */
3417 writel(ap->prd_dma, mmio + ATA_DMA_TABLE_OFS);
3419 /* specify data direction, triple-check start bit is clear */
3420 dmactl = readb(mmio + ATA_DMA_CMD);
3421 dmactl &= ~(ATA_DMA_WR | ATA_DMA_START);
3423 dmactl |= ATA_DMA_WR;
3424 writeb(dmactl, mmio + ATA_DMA_CMD);
3426 /* issue r/w command */
3427 ap->ops->exec_command(ap, &qc->tf);
3431 * ata_bmdma_start_mmio - Start a PCI IDE BMDMA transaction
3432 * @qc: Info associated with this ATA transaction.
3435 * spin_lock_irqsave(host_set lock)
3438 static void ata_bmdma_start_mmio (struct ata_queued_cmd *qc)
3440 struct ata_port *ap = qc->ap;
3441 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
3444 /* start host DMA transaction */
3445 dmactl = readb(mmio + ATA_DMA_CMD);
3446 writeb(dmactl | ATA_DMA_START, mmio + ATA_DMA_CMD);
3448 /* Strictly, one may wish to issue a readb() here, to
3449 * flush the mmio write. However, control also passes
3450 * to the hardware at this point, and it will interrupt
3451 * us when we are to resume control. So, in effect,
3452 * we don't care when the mmio write flushes.
3453 * Further, a read of the DMA status register _immediately_
3454 * following the write may not be what certain flaky hardware
3455 * is expected, so I think it is best to not add a readb()
3456 * without first all the MMIO ATA cards/mobos.
3457 * Or maybe I'm just being paranoid.
3462 * ata_bmdma_setup_pio - Set up PCI IDE BMDMA transaction (PIO)
3463 * @qc: Info associated with this ATA transaction.
3466 * spin_lock_irqsave(host_set lock)
3469 static void ata_bmdma_setup_pio (struct ata_queued_cmd *qc)
3471 struct ata_port *ap = qc->ap;
3472 unsigned int rw = (qc->tf.flags & ATA_TFLAG_WRITE);
3475 /* load PRD table addr. */
3476 outl(ap->prd_dma, ap->ioaddr.bmdma_addr + ATA_DMA_TABLE_OFS);
3478 /* specify data direction, triple-check start bit is clear */
3479 dmactl = inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3480 dmactl &= ~(ATA_DMA_WR | ATA_DMA_START);
3482 dmactl |= ATA_DMA_WR;
3483 outb(dmactl, ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3485 /* issue r/w command */
3486 ap->ops->exec_command(ap, &qc->tf);
3490 * ata_bmdma_start_pio - Start a PCI IDE BMDMA transaction (PIO)
3491 * @qc: Info associated with this ATA transaction.
3494 * spin_lock_irqsave(host_set lock)
3497 static void ata_bmdma_start_pio (struct ata_queued_cmd *qc)
3499 struct ata_port *ap = qc->ap;
3502 /* start host DMA transaction */
3503 dmactl = inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3504 outb(dmactl | ATA_DMA_START,
3505 ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3510 * ata_bmdma_start - Start a PCI IDE BMDMA transaction
3511 * @qc: Info associated with this ATA transaction.
3513 * Writes the ATA_DMA_START flag to the DMA command register.
3515 * May be used as the bmdma_start() entry in ata_port_operations.
3518 * spin_lock_irqsave(host_set lock)
3520 void ata_bmdma_start(struct ata_queued_cmd *qc)
3522 if (qc->ap->flags & ATA_FLAG_MMIO)
3523 ata_bmdma_start_mmio(qc);
3525 ata_bmdma_start_pio(qc);
3530 * ata_bmdma_setup - Set up PCI IDE BMDMA transaction
3531 * @qc: Info associated with this ATA transaction.
3533 * Writes address of PRD table to device's PRD Table Address
3534 * register, sets the DMA control register, and calls
3535 * ops->exec_command() to start the transfer.
3537 * May be used as the bmdma_setup() entry in ata_port_operations.
3540 * spin_lock_irqsave(host_set lock)
3542 void ata_bmdma_setup(struct ata_queued_cmd *qc)
3544 if (qc->ap->flags & ATA_FLAG_MMIO)
3545 ata_bmdma_setup_mmio(qc);
3547 ata_bmdma_setup_pio(qc);
3552 * ata_bmdma_irq_clear - Clear PCI IDE BMDMA interrupt.
3553 * @ap: Port associated with this ATA transaction.
3555 * Clear interrupt and error flags in DMA status register.
3557 * May be used as the irq_clear() entry in ata_port_operations.
3560 * spin_lock_irqsave(host_set lock)
3563 void ata_bmdma_irq_clear(struct ata_port *ap)
3565 if (ap->flags & ATA_FLAG_MMIO) {
3566 void __iomem *mmio = ((void __iomem *) ap->ioaddr.bmdma_addr) + ATA_DMA_STATUS;
3567 writeb(readb(mmio), mmio);
3569 unsigned long addr = ap->ioaddr.bmdma_addr + ATA_DMA_STATUS;
3570 outb(inb(addr), addr);
3577 * ata_bmdma_status - Read PCI IDE BMDMA status
3578 * @ap: Port associated with this ATA transaction.
3580 * Read and return BMDMA status register.
3582 * May be used as the bmdma_status() entry in ata_port_operations.
3585 * spin_lock_irqsave(host_set lock)
3588 u8 ata_bmdma_status(struct ata_port *ap)
3591 if (ap->flags & ATA_FLAG_MMIO) {
3592 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
3593 host_stat = readb(mmio + ATA_DMA_STATUS);
3595 host_stat = inb(ap->ioaddr.bmdma_addr + ATA_DMA_STATUS);
3601 * ata_bmdma_stop - Stop PCI IDE BMDMA transfer
3602 * @qc: Command we are ending DMA for
3604 * Clears the ATA_DMA_START flag in the dma control register
3606 * May be used as the bmdma_stop() entry in ata_port_operations.
3609 * spin_lock_irqsave(host_set lock)
3612 void ata_bmdma_stop(struct ata_queued_cmd *qc)
3614 struct ata_port *ap = qc->ap;
3615 if (ap->flags & ATA_FLAG_MMIO) {
3616 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
3618 /* clear start/stop bit */
3619 writeb(readb(mmio + ATA_DMA_CMD) & ~ATA_DMA_START,
3620 mmio + ATA_DMA_CMD);
3622 /* clear start/stop bit */
3623 outb(inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD) & ~ATA_DMA_START,
3624 ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3627 /* one-PIO-cycle guaranteed wait, per spec, for HDMA1:0 transition */
3628 ata_altstatus(ap); /* dummy read */
3632 * ata_host_intr - Handle host interrupt for given (port, task)
3633 * @ap: Port on which interrupt arrived (possibly...)
3634 * @qc: Taskfile currently active in engine
3636 * Handle host interrupt for given queued command. Currently,
3637 * only DMA interrupts are handled. All other commands are
3638 * handled via polling with interrupts disabled (nIEN bit).
3641 * spin_lock_irqsave(host_set lock)
3644 * One if interrupt was handled, zero if not (shared irq).
3647 inline unsigned int ata_host_intr (struct ata_port *ap,
3648 struct ata_queued_cmd *qc)
3650 u8 status, host_stat;
3652 switch (qc->tf.protocol) {
3655 case ATA_PROT_ATAPI_DMA:
3656 case ATA_PROT_ATAPI:
3657 /* check status of DMA engine */
3658 host_stat = ap->ops->bmdma_status(ap);
3659 VPRINTK("ata%u: host_stat 0x%X\n", ap->id, host_stat);
3661 /* if it's not our irq... */
3662 if (!(host_stat & ATA_DMA_INTR))
3665 /* before we do anything else, clear DMA-Start bit */
3666 ap->ops->bmdma_stop(qc);
3670 case ATA_PROT_ATAPI_NODATA:
3671 case ATA_PROT_NODATA:
3672 /* check altstatus */
3673 status = ata_altstatus(ap);
3674 if (status & ATA_BUSY)
3677 /* check main status, clearing INTRQ */
3678 status = ata_chk_status(ap);
3679 if (unlikely(status & ATA_BUSY))
3681 DPRINTK("ata%u: protocol %d (dev_stat 0x%X)\n",
3682 ap->id, qc->tf.protocol, status);
3684 /* ack bmdma irq events */
3685 ap->ops->irq_clear(ap);
3687 /* complete taskfile transaction */
3688 ata_qc_complete(qc, status);
3695 return 1; /* irq handled */
3698 ap->stats.idle_irq++;
3701 if ((ap->stats.idle_irq % 1000) == 0) {
3703 ata_irq_ack(ap, 0); /* debug trap */
3704 printk(KERN_WARNING "ata%d: irq trap\n", ap->id);
3707 return 0; /* irq not handled */
3711 * ata_interrupt - Default ATA host interrupt handler
3712 * @irq: irq line (unused)
3713 * @dev_instance: pointer to our ata_host_set information structure
3716 * Default interrupt handler for PCI IDE devices. Calls
3717 * ata_host_intr() for each port that is not disabled.
3720 * Obtains host_set lock during operation.
3723 * IRQ_NONE or IRQ_HANDLED.
3727 irqreturn_t ata_interrupt (int irq, void *dev_instance, struct pt_regs *regs)
3729 struct ata_host_set *host_set = dev_instance;
3731 unsigned int handled = 0;
3732 unsigned long flags;
3734 /* TODO: make _irqsave conditional on x86 PCI IDE legacy mode */
3735 spin_lock_irqsave(&host_set->lock, flags);
3737 for (i = 0; i < host_set->n_ports; i++) {
3738 struct ata_port *ap;
3740 ap = host_set->ports[i];
3742 !(ap->flags & (ATA_FLAG_PORT_DISABLED | ATA_FLAG_NOINTR))) {
3743 struct ata_queued_cmd *qc;
3745 qc = ata_qc_from_tag(ap, ap->active_tag);
3746 if (qc && (!(qc->tf.ctl & ATA_NIEN)) &&
3747 (qc->flags & ATA_QCFLAG_ACTIVE))
3748 handled |= ata_host_intr(ap, qc);
3752 spin_unlock_irqrestore(&host_set->lock, flags);
3754 return IRQ_RETVAL(handled);
3758 * atapi_packet_task - Write CDB bytes to hardware
3759 * @_data: Port to which ATAPI device is attached.
3761 * When device has indicated its readiness to accept
3762 * a CDB, this function is called. Send the CDB.
3763 * If DMA is to be performed, exit immediately.
3764 * Otherwise, we are in polling mode, so poll
3765 * status under operation succeeds or fails.
3768 * Kernel thread context (may sleep)
3771 static void atapi_packet_task(void *_data)
3773 struct ata_port *ap = _data;
3774 struct ata_queued_cmd *qc;
3777 qc = ata_qc_from_tag(ap, ap->active_tag);
3779 assert(qc->flags & ATA_QCFLAG_ACTIVE);
3781 /* sleep-wait for BSY to clear */
3782 DPRINTK("busy wait\n");
3783 if (ata_busy_sleep(ap, ATA_TMOUT_CDB_QUICK, ATA_TMOUT_CDB))
3786 /* make sure DRQ is set */
3787 status = ata_chk_status(ap);
3788 if ((status & (ATA_BUSY | ATA_DRQ)) != ATA_DRQ)
3792 DPRINTK("send cdb\n");
3793 assert(ap->cdb_len >= 12);
3795 if (qc->tf.protocol == ATA_PROT_ATAPI_DMA ||
3796 qc->tf.protocol == ATA_PROT_ATAPI_NODATA) {
3797 unsigned long flags;
3799 /* Once we're done issuing command and kicking bmdma,
3800 * irq handler takes over. To not lose irq, we need
3801 * to clear NOINTR flag before sending cdb, but
3802 * interrupt handler shouldn't be invoked before we're
3803 * finished. Hence, the following locking.
3805 spin_lock_irqsave(&ap->host_set->lock, flags);
3806 ap->flags &= ~ATA_FLAG_NOINTR;
3807 ata_data_xfer(ap, qc->cdb, ap->cdb_len, 1);
3808 if (qc->tf.protocol == ATA_PROT_ATAPI_DMA)
3809 ap->ops->bmdma_start(qc); /* initiate bmdma */
3810 spin_unlock_irqrestore(&ap->host_set->lock, flags);
3812 ata_data_xfer(ap, qc->cdb, ap->cdb_len, 1);
3814 /* PIO commands are handled by polling */
3815 ap->hsm_task_state = HSM_ST;
3816 queue_work(ata_wq, &ap->pio_task);
3822 ata_poll_qc_complete(qc, ATA_ERR);
3827 * ata_port_start - Set port up for dma.
3828 * @ap: Port to initialize
3830 * Called just after data structures for each port are
3831 * initialized. Allocates space for PRD table.
3833 * May be used as the port_start() entry in ata_port_operations.
3838 int ata_port_start (struct ata_port *ap)
3840 struct device *dev = ap->host_set->dev;
3842 ap->prd = dma_alloc_coherent(dev, ATA_PRD_TBL_SZ, &ap->prd_dma, GFP_KERNEL);
3846 DPRINTK("prd alloc, virt %p, dma %llx\n", ap->prd, (unsigned long long) ap->prd_dma);
3853 * ata_port_stop - Undo ata_port_start()
3854 * @ap: Port to shut down
3856 * Frees the PRD table.
3858 * May be used as the port_stop() entry in ata_port_operations.
3863 void ata_port_stop (struct ata_port *ap)
3865 struct device *dev = ap->host_set->dev;
3867 dma_free_coherent(dev, ATA_PRD_TBL_SZ, ap->prd, ap->prd_dma);
3870 void ata_host_stop (struct ata_host_set *host_set)
3872 if (host_set->mmio_base)
3873 iounmap(host_set->mmio_base);
3878 * ata_host_remove - Unregister SCSI host structure with upper layers
3879 * @ap: Port to unregister
3880 * @do_unregister: 1 if we fully unregister, 0 to just stop the port
3885 static void ata_host_remove(struct ata_port *ap, unsigned int do_unregister)
3887 struct Scsi_Host *sh = ap->host;
3892 scsi_remove_host(sh);
3894 ap->ops->port_stop(ap);
3898 * ata_host_init - Initialize an ata_port structure
3899 * @ap: Structure to initialize
3900 * @host: associated SCSI mid-layer structure
3901 * @host_set: Collection of hosts to which @ap belongs
3902 * @ent: Probe information provided by low-level driver
3903 * @port_no: Port number associated with this ata_port
3905 * Initialize a new ata_port structure, and its associated
3909 * Inherited from caller.
3913 static void ata_host_init(struct ata_port *ap, struct Scsi_Host *host,
3914 struct ata_host_set *host_set,
3915 struct ata_probe_ent *ent, unsigned int port_no)
3921 host->max_channel = 1;
3922 host->unique_id = ata_unique_id++;
3923 host->max_cmd_len = 12;
3925 scsi_assign_lock(host, &host_set->lock);
3927 ap->flags = ATA_FLAG_PORT_DISABLED;
3928 ap->id = host->unique_id;
3930 ap->ctl = ATA_DEVCTL_OBS;
3931 ap->host_set = host_set;
3932 ap->port_no = port_no;
3934 ent->legacy_mode ? ent->hard_port_no : port_no;
3935 ap->pio_mask = ent->pio_mask;
3936 ap->mwdma_mask = ent->mwdma_mask;
3937 ap->udma_mask = ent->udma_mask;
3938 ap->flags |= ent->host_flags;
3939 ap->ops = ent->port_ops;
3940 ap->cbl = ATA_CBL_NONE;
3941 ap->active_tag = ATA_TAG_POISON;
3942 ap->last_ctl = 0xFF;
3944 INIT_WORK(&ap->packet_task, atapi_packet_task, ap);
3945 INIT_WORK(&ap->pio_task, ata_pio_task, ap);
3947 for (i = 0; i < ATA_MAX_DEVICES; i++)
3948 ap->device[i].devno = i;
3951 ap->stats.unhandled_irq = 1;
3952 ap->stats.idle_irq = 1;
3955 memcpy(&ap->ioaddr, &ent->port[port_no], sizeof(struct ata_ioports));
3959 * ata_host_add - Attach low-level ATA driver to system
3960 * @ent: Information provided by low-level driver
3961 * @host_set: Collections of ports to which we add
3962 * @port_no: Port number associated with this host
3964 * Attach low-level ATA driver to system.
3967 * PCI/etc. bus probe sem.
3970 * New ata_port on success, for NULL on error.
3974 static struct ata_port * ata_host_add(struct ata_probe_ent *ent,
3975 struct ata_host_set *host_set,
3976 unsigned int port_no)
3978 struct Scsi_Host *host;
3979 struct ata_port *ap;
3983 host = scsi_host_alloc(ent->sht, sizeof(struct ata_port));
3987 ap = (struct ata_port *) &host->hostdata[0];
3989 ata_host_init(ap, host, host_set, ent, port_no);
3991 rc = ap->ops->port_start(ap);
3998 scsi_host_put(host);
4003 * ata_device_add - Register hardware device with ATA and SCSI layers
4004 * @ent: Probe information describing hardware device to be registered
4006 * This function processes the information provided in the probe
4007 * information struct @ent, allocates the necessary ATA and SCSI
4008 * host information structures, initializes them, and registers
4009 * everything with requisite kernel subsystems.
4011 * This function requests irqs, probes the ATA bus, and probes
4015 * PCI/etc. bus probe sem.
4018 * Number of ports registered. Zero on error (no ports registered).
4022 int ata_device_add(struct ata_probe_ent *ent)
4024 unsigned int count = 0, i;
4025 struct device *dev = ent->dev;
4026 struct ata_host_set *host_set;
4029 /* alloc a container for our list of ATA ports (buses) */
4030 host_set = kmalloc(sizeof(struct ata_host_set) +
4031 (ent->n_ports * sizeof(void *)), GFP_KERNEL);
4034 memset(host_set, 0, sizeof(struct ata_host_set) + (ent->n_ports * sizeof(void *)));
4035 spin_lock_init(&host_set->lock);
4037 host_set->dev = dev;
4038 host_set->n_ports = ent->n_ports;
4039 host_set->irq = ent->irq;
4040 host_set->mmio_base = ent->mmio_base;
4041 host_set->private_data = ent->private_data;
4042 host_set->ops = ent->port_ops;
4044 /* register each port bound to this device */
4045 for (i = 0; i < ent->n_ports; i++) {
4046 struct ata_port *ap;
4047 unsigned long xfer_mode_mask;
4049 ap = ata_host_add(ent, host_set, i);
4053 host_set->ports[i] = ap;
4054 xfer_mode_mask =(ap->udma_mask << ATA_SHIFT_UDMA) |
4055 (ap->mwdma_mask << ATA_SHIFT_MWDMA) |
4056 (ap->pio_mask << ATA_SHIFT_PIO);
4058 /* print per-port info to dmesg */
4059 printk(KERN_INFO "ata%u: %cATA max %s cmd 0x%lX ctl 0x%lX "
4060 "bmdma 0x%lX irq %lu\n",
4062 ap->flags & ATA_FLAG_SATA ? 'S' : 'P',
4063 ata_mode_string(xfer_mode_mask),
4064 ap->ioaddr.cmd_addr,
4065 ap->ioaddr.ctl_addr,
4066 ap->ioaddr.bmdma_addr,
4070 host_set->ops->irq_clear(ap);
4079 /* obtain irq, that is shared between channels */
4080 if (request_irq(ent->irq, ent->port_ops->irq_handler, ent->irq_flags,
4081 DRV_NAME, host_set))
4084 /* perform each probe synchronously */
4085 DPRINTK("probe begin\n");
4086 for (i = 0; i < count; i++) {
4087 struct ata_port *ap;
4090 ap = host_set->ports[i];
4092 DPRINTK("ata%u: probe begin\n", ap->id);
4093 rc = ata_bus_probe(ap);
4094 DPRINTK("ata%u: probe end\n", ap->id);
4097 /* FIXME: do something useful here?
4098 * Current libata behavior will
4099 * tear down everything when
4100 * the module is removed
4101 * or the h/w is unplugged.
4105 rc = scsi_add_host(ap->host, dev);
4107 printk(KERN_ERR "ata%u: scsi_add_host failed\n",
4109 /* FIXME: do something useful here */
4110 /* FIXME: handle unconditional calls to
4111 * scsi_scan_host and ata_host_remove, below,
4117 /* probes are done, now scan each port's disk(s) */
4118 DPRINTK("probe begin\n");
4119 for (i = 0; i < count; i++) {
4120 struct ata_port *ap = host_set->ports[i];
4122 ata_scsi_scan_host(ap);
4125 dev_set_drvdata(dev, host_set);
4127 VPRINTK("EXIT, returning %u\n", ent->n_ports);
4128 return ent->n_ports; /* success */
4131 for (i = 0; i < count; i++) {
4132 ata_host_remove(host_set->ports[i], 1);
4133 scsi_host_put(host_set->ports[i]->host);
4136 VPRINTK("EXIT, returning 0\n");
4141 * ata_host_set_remove - PCI layer callback for device removal
4142 * @host_set: ATA host set that was removed
4144 * Unregister all objects associated with this host set. Free those
4148 * Inherited from calling layer (may sleep).
4152 void ata_host_set_remove(struct ata_host_set *host_set)
4154 struct ata_port *ap;
4157 for (i = 0; i < host_set->n_ports; i++) {
4158 ap = host_set->ports[i];
4159 scsi_remove_host(ap->host);
4162 free_irq(host_set->irq, host_set);
4164 for (i = 0; i < host_set->n_ports; i++) {
4165 ap = host_set->ports[i];
4167 ata_scsi_release(ap->host);
4169 if ((ap->flags & ATA_FLAG_NO_LEGACY) == 0) {
4170 struct ata_ioports *ioaddr = &ap->ioaddr;
4172 if (ioaddr->cmd_addr == 0x1f0)
4173 release_region(0x1f0, 8);
4174 else if (ioaddr->cmd_addr == 0x170)
4175 release_region(0x170, 8);
4178 scsi_host_put(ap->host);
4181 if (host_set->ops->host_stop)
4182 host_set->ops->host_stop(host_set);
4188 * ata_scsi_release - SCSI layer callback hook for host unload
4189 * @host: libata host to be unloaded
4191 * Performs all duties necessary to shut down a libata port...
4192 * Kill port kthread, disable port, and release resources.
4195 * Inherited from SCSI layer.
4201 int ata_scsi_release(struct Scsi_Host *host)
4203 struct ata_port *ap = (struct ata_port *) &host->hostdata[0];
4207 ap->ops->port_disable(ap);
4208 ata_host_remove(ap, 0);
4215 * ata_std_ports - initialize ioaddr with standard port offsets.
4216 * @ioaddr: IO address structure to be initialized
4218 * Utility function which initializes data_addr, error_addr,
4219 * feature_addr, nsect_addr, lbal_addr, lbam_addr, lbah_addr,
4220 * device_addr, status_addr, and command_addr to standard offsets
4221 * relative to cmd_addr.
4223 * Does not set ctl_addr, altstatus_addr, bmdma_addr, or scr_addr.
4226 void ata_std_ports(struct ata_ioports *ioaddr)
4228 ioaddr->data_addr = ioaddr->cmd_addr + ATA_REG_DATA;
4229 ioaddr->error_addr = ioaddr->cmd_addr + ATA_REG_ERR;
4230 ioaddr->feature_addr = ioaddr->cmd_addr + ATA_REG_FEATURE;
4231 ioaddr->nsect_addr = ioaddr->cmd_addr + ATA_REG_NSECT;
4232 ioaddr->lbal_addr = ioaddr->cmd_addr + ATA_REG_LBAL;
4233 ioaddr->lbam_addr = ioaddr->cmd_addr + ATA_REG_LBAM;
4234 ioaddr->lbah_addr = ioaddr->cmd_addr + ATA_REG_LBAH;
4235 ioaddr->device_addr = ioaddr->cmd_addr + ATA_REG_DEVICE;
4236 ioaddr->status_addr = ioaddr->cmd_addr + ATA_REG_STATUS;
4237 ioaddr->command_addr = ioaddr->cmd_addr + ATA_REG_CMD;
4240 static struct ata_probe_ent *
4241 ata_probe_ent_alloc(struct device *dev, struct ata_port_info *port)
4243 struct ata_probe_ent *probe_ent;
4245 probe_ent = kmalloc(sizeof(*probe_ent), GFP_KERNEL);
4247 printk(KERN_ERR DRV_NAME "(%s): out of memory\n",
4248 kobject_name(&(dev->kobj)));
4252 memset(probe_ent, 0, sizeof(*probe_ent));
4254 INIT_LIST_HEAD(&probe_ent->node);
4255 probe_ent->dev = dev;
4257 probe_ent->sht = port->sht;
4258 probe_ent->host_flags = port->host_flags;
4259 probe_ent->pio_mask = port->pio_mask;
4260 probe_ent->mwdma_mask = port->mwdma_mask;
4261 probe_ent->udma_mask = port->udma_mask;
4262 probe_ent->port_ops = port->port_ops;
4271 void ata_pci_host_stop (struct ata_host_set *host_set)
4273 struct pci_dev *pdev = to_pci_dev(host_set->dev);
4275 pci_iounmap(pdev, host_set->mmio_base);
4279 * ata_pci_init_native_mode - Initialize native-mode driver
4280 * @pdev: pci device to be initialized
4281 * @port: array[2] of pointers to port info structures.
4282 * @ports: bitmap of ports present
4284 * Utility function which allocates and initializes an
4285 * ata_probe_ent structure for a standard dual-port
4286 * PIO-based IDE controller. The returned ata_probe_ent
4287 * structure can be passed to ata_device_add(). The returned
4288 * ata_probe_ent structure should then be freed with kfree().
4290 * The caller need only pass the address of the primary port, the
4291 * secondary will be deduced automatically. If the device has non
4292 * standard secondary port mappings this function can be called twice,
4293 * once for each interface.
4296 struct ata_probe_ent *
4297 ata_pci_init_native_mode(struct pci_dev *pdev, struct ata_port_info **port, int ports)
4299 struct ata_probe_ent *probe_ent =
4300 ata_probe_ent_alloc(pci_dev_to_dev(pdev), port[0]);
4306 probe_ent->irq = pdev->irq;
4307 probe_ent->irq_flags = SA_SHIRQ;
4309 if (ports & ATA_PORT_PRIMARY) {
4310 probe_ent->port[p].cmd_addr = pci_resource_start(pdev, 0);
4311 probe_ent->port[p].altstatus_addr =
4312 probe_ent->port[p].ctl_addr =
4313 pci_resource_start(pdev, 1) | ATA_PCI_CTL_OFS;
4314 probe_ent->port[p].bmdma_addr = pci_resource_start(pdev, 4);
4315 ata_std_ports(&probe_ent->port[p]);
4319 if (ports & ATA_PORT_SECONDARY) {
4320 probe_ent->port[p].cmd_addr = pci_resource_start(pdev, 2);
4321 probe_ent->port[p].altstatus_addr =
4322 probe_ent->port[p].ctl_addr =
4323 pci_resource_start(pdev, 3) | ATA_PCI_CTL_OFS;
4324 probe_ent->port[p].bmdma_addr = pci_resource_start(pdev, 4) + 8;
4325 ata_std_ports(&probe_ent->port[p]);
4329 probe_ent->n_ports = p;
4333 static struct ata_probe_ent *ata_pci_init_legacy_port(struct pci_dev *pdev, struct ata_port_info **port, int port_num)
4335 struct ata_probe_ent *probe_ent;
4337 probe_ent = ata_probe_ent_alloc(pci_dev_to_dev(pdev), port[0]);
4342 probe_ent->legacy_mode = 1;
4343 probe_ent->n_ports = 1;
4344 probe_ent->hard_port_no = port_num;
4349 probe_ent->irq = 14;
4350 probe_ent->port[0].cmd_addr = 0x1f0;
4351 probe_ent->port[0].altstatus_addr =
4352 probe_ent->port[0].ctl_addr = 0x3f6;
4355 probe_ent->irq = 15;
4356 probe_ent->port[0].cmd_addr = 0x170;
4357 probe_ent->port[0].altstatus_addr =
4358 probe_ent->port[0].ctl_addr = 0x376;
4361 probe_ent->port[0].bmdma_addr = pci_resource_start(pdev, 4) + 8 * port_num;
4362 ata_std_ports(&probe_ent->port[0]);
4367 * ata_pci_init_one - Initialize/register PCI IDE host controller
4368 * @pdev: Controller to be initialized
4369 * @port_info: Information from low-level host driver
4370 * @n_ports: Number of ports attached to host controller
4372 * This is a helper function which can be called from a driver's
4373 * xxx_init_one() probe function if the hardware uses traditional
4374 * IDE taskfile registers.
4376 * This function calls pci_enable_device(), reserves its register
4377 * regions, sets the dma mask, enables bus master mode, and calls
4381 * Inherited from PCI layer (may sleep).
4384 * Zero on success, negative on errno-based value on error.
4388 int ata_pci_init_one (struct pci_dev *pdev, struct ata_port_info **port_info,
4389 unsigned int n_ports)
4391 struct ata_probe_ent *probe_ent = NULL, *probe_ent2 = NULL;
4392 struct ata_port_info *port[2];
4394 unsigned int legacy_mode = 0;
4395 int disable_dev_on_err = 1;
4400 port[0] = port_info[0];
4402 port[1] = port_info[1];
4406 if ((port[0]->host_flags & ATA_FLAG_NO_LEGACY) == 0
4407 && (pdev->class >> 8) == PCI_CLASS_STORAGE_IDE) {
4408 /* TODO: What if one channel is in native mode ... */
4409 pci_read_config_byte(pdev, PCI_CLASS_PROG, &tmp8);
4410 mask = (1 << 2) | (1 << 0);
4411 if ((tmp8 & mask) != mask)
4412 legacy_mode = (1 << 3);
4416 if ((!legacy_mode) && (n_ports > 2)) {
4417 printk(KERN_ERR "ata: BUG: native mode, n_ports > 2\n");
4422 /* FIXME: Really for ATA it isn't safe because the device may be
4423 multi-purpose and we want to leave it alone if it was already
4424 enabled. Secondly for shared use as Arjan says we want refcounting
4426 Checking dev->is_enabled is insufficient as this is not set at
4427 boot for the primary video which is BIOS enabled
4430 rc = pci_enable_device(pdev);
4434 rc = pci_request_regions(pdev, DRV_NAME);
4436 disable_dev_on_err = 0;
4440 /* FIXME: Should use platform specific mappers for legacy port ranges */
4442 if (!request_region(0x1f0, 8, "libata")) {
4443 struct resource *conflict, res;
4445 res.end = 0x1f0 + 8 - 1;
4446 conflict = ____request_resource(&ioport_resource, &res);
4447 if (!strcmp(conflict->name, "libata"))
4448 legacy_mode |= (1 << 0);
4450 disable_dev_on_err = 0;
4451 printk(KERN_WARNING "ata: 0x1f0 IDE port busy\n");
4454 legacy_mode |= (1 << 0);
4456 if (!request_region(0x170, 8, "libata")) {
4457 struct resource *conflict, res;
4459 res.end = 0x170 + 8 - 1;
4460 conflict = ____request_resource(&ioport_resource, &res);
4461 if (!strcmp(conflict->name, "libata"))
4462 legacy_mode |= (1 << 1);
4464 disable_dev_on_err = 0;
4465 printk(KERN_WARNING "ata: 0x170 IDE port busy\n");
4468 legacy_mode |= (1 << 1);
4471 /* we have legacy mode, but all ports are unavailable */
4472 if (legacy_mode == (1 << 3)) {
4474 goto err_out_regions;
4477 rc = pci_set_dma_mask(pdev, ATA_DMA_MASK);
4479 goto err_out_regions;
4480 rc = pci_set_consistent_dma_mask(pdev, ATA_DMA_MASK);
4482 goto err_out_regions;
4485 if (legacy_mode & (1 << 0))
4486 probe_ent = ata_pci_init_legacy_port(pdev, port, 0);
4487 if (legacy_mode & (1 << 1))
4488 probe_ent2 = ata_pci_init_legacy_port(pdev, port, 1);
4491 probe_ent = ata_pci_init_native_mode(pdev, port, ATA_PORT_PRIMARY | ATA_PORT_SECONDARY);
4493 probe_ent = ata_pci_init_native_mode(pdev, port, ATA_PORT_PRIMARY);
4495 if (!probe_ent && !probe_ent2) {
4497 goto err_out_regions;
4500 pci_set_master(pdev);
4502 /* FIXME: check ata_device_add return */
4504 if (legacy_mode & (1 << 0))
4505 ata_device_add(probe_ent);
4506 if (legacy_mode & (1 << 1))
4507 ata_device_add(probe_ent2);
4509 ata_device_add(probe_ent);
4517 if (legacy_mode & (1 << 0))
4518 release_region(0x1f0, 8);
4519 if (legacy_mode & (1 << 1))
4520 release_region(0x170, 8);
4521 pci_release_regions(pdev);
4523 if (disable_dev_on_err)
4524 pci_disable_device(pdev);
4529 * ata_pci_remove_one - PCI layer callback for device removal
4530 * @pdev: PCI device that was removed
4532 * PCI layer indicates to libata via this hook that
4533 * hot-unplug or module unload event has occured.
4534 * Handle this by unregistering all objects associated
4535 * with this PCI device. Free those objects. Then finally
4536 * release PCI resources and disable device.
4539 * Inherited from PCI layer (may sleep).
4542 void ata_pci_remove_one (struct pci_dev *pdev)
4544 struct device *dev = pci_dev_to_dev(pdev);
4545 struct ata_host_set *host_set = dev_get_drvdata(dev);
4547 ata_host_set_remove(host_set);
4548 pci_release_regions(pdev);
4549 pci_disable_device(pdev);
4550 dev_set_drvdata(dev, NULL);
4553 /* move to PCI subsystem */
4554 int pci_test_config_bits(struct pci_dev *pdev, struct pci_bits *bits)
4556 unsigned long tmp = 0;
4558 switch (bits->width) {
4561 pci_read_config_byte(pdev, bits->reg, &tmp8);
4567 pci_read_config_word(pdev, bits->reg, &tmp16);
4573 pci_read_config_dword(pdev, bits->reg, &tmp32);
4584 return (tmp == bits->val) ? 1 : 0;
4586 #endif /* CONFIG_PCI */
4589 static int __init ata_init(void)
4591 ata_wq = create_workqueue("ata");
4595 printk(KERN_DEBUG "libata version " DRV_VERSION " loaded.\n");
4599 static void __exit ata_exit(void)
4601 destroy_workqueue(ata_wq);
4604 module_init(ata_init);
4605 module_exit(ata_exit);
4607 static unsigned long ratelimit_time;
4608 static spinlock_t ata_ratelimit_lock = SPIN_LOCK_UNLOCKED;
4610 int ata_ratelimit(void)
4613 unsigned long flags;
4615 spin_lock_irqsave(&ata_ratelimit_lock, flags);
4617 if (time_after(jiffies, ratelimit_time)) {
4619 ratelimit_time = jiffies + (HZ/5);
4623 spin_unlock_irqrestore(&ata_ratelimit_lock, flags);
4629 * libata is essentially a library of internal helper functions for
4630 * low-level ATA host controller drivers. As such, the API/ABI is
4631 * likely to change as new drivers are added and updated.
4632 * Do not depend on ABI/API stability.
4635 EXPORT_SYMBOL_GPL(ata_std_bios_param);
4636 EXPORT_SYMBOL_GPL(ata_std_ports);
4637 EXPORT_SYMBOL_GPL(ata_device_add);
4638 EXPORT_SYMBOL_GPL(ata_host_set_remove);
4639 EXPORT_SYMBOL_GPL(ata_sg_init);
4640 EXPORT_SYMBOL_GPL(ata_sg_init_one);
4641 EXPORT_SYMBOL_GPL(ata_qc_complete);
4642 EXPORT_SYMBOL_GPL(ata_qc_issue_prot);
4643 EXPORT_SYMBOL_GPL(ata_eng_timeout);
4644 EXPORT_SYMBOL_GPL(ata_tf_load);
4645 EXPORT_SYMBOL_GPL(ata_tf_read);
4646 EXPORT_SYMBOL_GPL(ata_noop_dev_select);
4647 EXPORT_SYMBOL_GPL(ata_std_dev_select);
4648 EXPORT_SYMBOL_GPL(ata_tf_to_fis);
4649 EXPORT_SYMBOL_GPL(ata_tf_from_fis);
4650 EXPORT_SYMBOL_GPL(ata_check_status);
4651 EXPORT_SYMBOL_GPL(ata_altstatus);
4652 EXPORT_SYMBOL_GPL(ata_chk_err);
4653 EXPORT_SYMBOL_GPL(ata_exec_command);
4654 EXPORT_SYMBOL_GPL(ata_port_start);
4655 EXPORT_SYMBOL_GPL(ata_port_stop);
4656 EXPORT_SYMBOL_GPL(ata_host_stop);
4657 EXPORT_SYMBOL_GPL(ata_interrupt);
4658 EXPORT_SYMBOL_GPL(ata_qc_prep);
4659 EXPORT_SYMBOL_GPL(ata_bmdma_setup);
4660 EXPORT_SYMBOL_GPL(ata_bmdma_start);
4661 EXPORT_SYMBOL_GPL(ata_bmdma_irq_clear);
4662 EXPORT_SYMBOL_GPL(ata_bmdma_status);
4663 EXPORT_SYMBOL_GPL(ata_bmdma_stop);
4664 EXPORT_SYMBOL_GPL(ata_port_probe);
4665 EXPORT_SYMBOL_GPL(sata_phy_reset);
4666 EXPORT_SYMBOL_GPL(__sata_phy_reset);
4667 EXPORT_SYMBOL_GPL(ata_bus_reset);
4668 EXPORT_SYMBOL_GPL(ata_port_disable);
4669 EXPORT_SYMBOL_GPL(ata_ratelimit);
4670 EXPORT_SYMBOL_GPL(ata_scsi_ioctl);
4671 EXPORT_SYMBOL_GPL(ata_scsi_queuecmd);
4672 EXPORT_SYMBOL_GPL(ata_scsi_error);
4673 EXPORT_SYMBOL_GPL(ata_scsi_slave_config);
4674 EXPORT_SYMBOL_GPL(ata_scsi_release);
4675 EXPORT_SYMBOL_GPL(ata_host_intr);
4676 EXPORT_SYMBOL_GPL(ata_dev_classify);
4677 EXPORT_SYMBOL_GPL(ata_dev_id_string);
4678 EXPORT_SYMBOL_GPL(ata_dev_config);
4679 EXPORT_SYMBOL_GPL(ata_scsi_simulate);
4682 EXPORT_SYMBOL_GPL(pci_test_config_bits);
4683 EXPORT_SYMBOL_GPL(ata_pci_host_stop);
4684 EXPORT_SYMBOL_GPL(ata_pci_init_native_mode);
4685 EXPORT_SYMBOL_GPL(ata_pci_init_one);
4686 EXPORT_SYMBOL_GPL(ata_pci_remove_one);
4687 #endif /* CONFIG_PCI */