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];
620 * ata_prot_to_cmd - determine which read/write opcodes to use
621 * @protocol: ATA_PROT_xxx taskfile protocol
622 * @lba48: true is lba48 is present
624 * Given necessary input, determine which read/write commands
625 * to use to transfer data.
630 static int ata_prot_to_cmd(int protocol, int lba48)
632 int rcmd = 0, wcmd = 0;
637 rcmd = ATA_CMD_PIO_READ_EXT;
638 wcmd = ATA_CMD_PIO_WRITE_EXT;
640 rcmd = ATA_CMD_PIO_READ;
641 wcmd = ATA_CMD_PIO_WRITE;
647 rcmd = ATA_CMD_READ_EXT;
648 wcmd = ATA_CMD_WRITE_EXT;
651 wcmd = ATA_CMD_WRITE;
659 return rcmd | (wcmd << 8);
663 * ata_dev_set_protocol - set taskfile protocol and r/w commands
664 * @dev: device to examine and configure
666 * Examine the device configuration, after we have
667 * read the identify-device page and configured the
668 * data transfer mode. Set internal state related to
669 * the ATA taskfile protocol (pio, pio mult, dma, etc.)
670 * and calculate the proper read/write commands to use.
675 static void ata_dev_set_protocol(struct ata_device *dev)
677 int pio = (dev->flags & ATA_DFLAG_PIO);
678 int lba48 = (dev->flags & ATA_DFLAG_LBA48);
682 proto = dev->xfer_protocol = ATA_PROT_PIO;
684 proto = dev->xfer_protocol = ATA_PROT_DMA;
686 cmd = ata_prot_to_cmd(proto, lba48);
690 dev->read_cmd = cmd & 0xff;
691 dev->write_cmd = (cmd >> 8) & 0xff;
694 static const char * xfer_mode_str[] = {
714 * ata_udma_string - convert UDMA bit offset to string
715 * @mask: mask of bits supported; only highest bit counts.
717 * Determine string which represents the highest speed
718 * (highest bit in @udma_mask).
724 * Constant C string representing highest speed listed in
725 * @udma_mask, or the constant C string "<n/a>".
728 static const char *ata_mode_string(unsigned int mask)
732 for (i = 7; i >= 0; i--)
735 for (i = ATA_SHIFT_MWDMA + 2; i >= ATA_SHIFT_MWDMA; i--)
738 for (i = ATA_SHIFT_PIO + 4; i >= ATA_SHIFT_PIO; i--)
745 return xfer_mode_str[i];
749 * ata_pio_devchk - PATA device presence detection
750 * @ap: ATA channel to examine
751 * @device: Device to examine (starting at zero)
753 * This technique was originally described in
754 * Hale Landis's ATADRVR (www.ata-atapi.com), and
755 * later found its way into the ATA/ATAPI spec.
757 * Write a pattern to the ATA shadow registers,
758 * and if a device is present, it will respond by
759 * correctly storing and echoing back the
760 * ATA shadow register contents.
766 static unsigned int ata_pio_devchk(struct ata_port *ap,
769 struct ata_ioports *ioaddr = &ap->ioaddr;
772 ap->ops->dev_select(ap, device);
774 outb(0x55, ioaddr->nsect_addr);
775 outb(0xaa, ioaddr->lbal_addr);
777 outb(0xaa, ioaddr->nsect_addr);
778 outb(0x55, ioaddr->lbal_addr);
780 outb(0x55, ioaddr->nsect_addr);
781 outb(0xaa, ioaddr->lbal_addr);
783 nsect = inb(ioaddr->nsect_addr);
784 lbal = inb(ioaddr->lbal_addr);
786 if ((nsect == 0x55) && (lbal == 0xaa))
787 return 1; /* we found a device */
789 return 0; /* nothing found */
793 * ata_mmio_devchk - PATA device presence detection
794 * @ap: ATA channel to examine
795 * @device: Device to examine (starting at zero)
797 * This technique was originally described in
798 * Hale Landis's ATADRVR (www.ata-atapi.com), and
799 * later found its way into the ATA/ATAPI spec.
801 * Write a pattern to the ATA shadow registers,
802 * and if a device is present, it will respond by
803 * correctly storing and echoing back the
804 * ATA shadow register contents.
810 static unsigned int ata_mmio_devchk(struct ata_port *ap,
813 struct ata_ioports *ioaddr = &ap->ioaddr;
816 ap->ops->dev_select(ap, device);
818 writeb(0x55, (void __iomem *) ioaddr->nsect_addr);
819 writeb(0xaa, (void __iomem *) ioaddr->lbal_addr);
821 writeb(0xaa, (void __iomem *) ioaddr->nsect_addr);
822 writeb(0x55, (void __iomem *) ioaddr->lbal_addr);
824 writeb(0x55, (void __iomem *) ioaddr->nsect_addr);
825 writeb(0xaa, (void __iomem *) ioaddr->lbal_addr);
827 nsect = readb((void __iomem *) ioaddr->nsect_addr);
828 lbal = readb((void __iomem *) ioaddr->lbal_addr);
830 if ((nsect == 0x55) && (lbal == 0xaa))
831 return 1; /* we found a device */
833 return 0; /* nothing found */
837 * ata_devchk - PATA device presence detection
838 * @ap: ATA channel to examine
839 * @device: Device to examine (starting at zero)
841 * Dispatch ATA device presence detection, depending
842 * on whether we are using PIO or MMIO to talk to the
843 * ATA shadow registers.
849 static unsigned int ata_devchk(struct ata_port *ap,
852 if (ap->flags & ATA_FLAG_MMIO)
853 return ata_mmio_devchk(ap, device);
854 return ata_pio_devchk(ap, device);
858 * ata_dev_classify - determine device type based on ATA-spec signature
859 * @tf: ATA taskfile register set for device to be identified
861 * Determine from taskfile register contents whether a device is
862 * ATA or ATAPI, as per "Signature and persistence" section
863 * of ATA/PI spec (volume 1, sect 5.14).
869 * Device type, %ATA_DEV_ATA, %ATA_DEV_ATAPI, or %ATA_DEV_UNKNOWN
870 * the event of failure.
873 unsigned int ata_dev_classify(struct ata_taskfile *tf)
875 /* Apple's open source Darwin code hints that some devices only
876 * put a proper signature into the LBA mid/high registers,
877 * So, we only check those. It's sufficient for uniqueness.
880 if (((tf->lbam == 0) && (tf->lbah == 0)) ||
881 ((tf->lbam == 0x3c) && (tf->lbah == 0xc3))) {
882 DPRINTK("found ATA device by sig\n");
886 if (((tf->lbam == 0x14) && (tf->lbah == 0xeb)) ||
887 ((tf->lbam == 0x69) && (tf->lbah == 0x96))) {
888 DPRINTK("found ATAPI device by sig\n");
889 return ATA_DEV_ATAPI;
892 DPRINTK("unknown device\n");
893 return ATA_DEV_UNKNOWN;
897 * ata_dev_try_classify - Parse returned ATA device signature
898 * @ap: ATA channel to examine
899 * @device: Device to examine (starting at zero)
901 * After an event -- SRST, E.D.D., or SATA COMRESET -- occurs,
902 * an ATA/ATAPI-defined set of values is placed in the ATA
903 * shadow registers, indicating the results of device detection
906 * Select the ATA device, and read the values from the ATA shadow
907 * registers. Then parse according to the Error register value,
908 * and the spec-defined values examined by ata_dev_classify().
914 static u8 ata_dev_try_classify(struct ata_port *ap, unsigned int device)
916 struct ata_device *dev = &ap->device[device];
917 struct ata_taskfile tf;
921 ap->ops->dev_select(ap, device);
923 memset(&tf, 0, sizeof(tf));
925 err = ata_chk_err(ap);
926 ap->ops->tf_read(ap, &tf);
928 dev->class = ATA_DEV_NONE;
930 /* see if device passed diags */
933 else if ((device == 0) && (err == 0x81))
938 /* determine if device if ATA or ATAPI */
939 class = ata_dev_classify(&tf);
940 if (class == ATA_DEV_UNKNOWN)
942 if ((class == ATA_DEV_ATA) && (ata_chk_status(ap) == 0))
951 * ata_dev_id_string - Convert IDENTIFY DEVICE page into string
952 * @id: IDENTIFY DEVICE results we will examine
953 * @s: string into which data is output
954 * @ofs: offset into identify device page
955 * @len: length of string to return. must be an even number.
957 * The strings in the IDENTIFY DEVICE page are broken up into
958 * 16-bit chunks. Run through the string, and output each
959 * 8-bit chunk linearly, regardless of platform.
965 void ata_dev_id_string(u16 *id, unsigned char *s,
966 unsigned int ofs, unsigned int len)
986 * ata_noop_dev_select - Select device 0/1 on ATA bus
987 * @ap: ATA channel to manipulate
988 * @device: ATA device (numbered from zero) to select
990 * This function performs no actual function.
992 * May be used as the dev_select() entry in ata_port_operations.
997 void ata_noop_dev_select (struct ata_port *ap, unsigned int device)
1003 * ata_std_dev_select - Select device 0/1 on ATA bus
1004 * @ap: ATA channel to manipulate
1005 * @device: ATA device (numbered from zero) to select
1007 * Use the method defined in the ATA specification to
1008 * make either device 0, or device 1, active on the
1009 * ATA channel. Works with both PIO and MMIO.
1011 * May be used as the dev_select() entry in ata_port_operations.
1017 void ata_std_dev_select (struct ata_port *ap, unsigned int device)
1022 tmp = ATA_DEVICE_OBS;
1024 tmp = ATA_DEVICE_OBS | ATA_DEV1;
1026 if (ap->flags & ATA_FLAG_MMIO) {
1027 writeb(tmp, (void __iomem *) ap->ioaddr.device_addr);
1029 outb(tmp, ap->ioaddr.device_addr);
1031 ata_pause(ap); /* needed; also flushes, for mmio */
1035 * ata_dev_select - Select device 0/1 on ATA bus
1036 * @ap: ATA channel to manipulate
1037 * @device: ATA device (numbered from zero) to select
1038 * @wait: non-zero to wait for Status register BSY bit to clear
1039 * @can_sleep: non-zero if context allows sleeping
1041 * Use the method defined in the ATA specification to
1042 * make either device 0, or device 1, active on the
1045 * This is a high-level version of ata_std_dev_select(),
1046 * which additionally provides the services of inserting
1047 * the proper pauses and status polling, where needed.
1053 void ata_dev_select(struct ata_port *ap, unsigned int device,
1054 unsigned int wait, unsigned int can_sleep)
1056 VPRINTK("ENTER, ata%u: device %u, wait %u\n",
1057 ap->id, device, wait);
1062 ap->ops->dev_select(ap, device);
1065 if (can_sleep && ap->device[device].class == ATA_DEV_ATAPI)
1072 * ata_dump_id - IDENTIFY DEVICE info debugging output
1073 * @dev: Device whose IDENTIFY DEVICE page we will dump
1075 * Dump selected 16-bit words from a detected device's
1076 * IDENTIFY PAGE page.
1082 static inline void ata_dump_id(struct ata_device *dev)
1084 DPRINTK("49==0x%04x "
1094 DPRINTK("80==0x%04x "
1104 DPRINTK("88==0x%04x "
1111 * ata_dev_identify - obtain IDENTIFY x DEVICE page
1112 * @ap: port on which device we wish to probe resides
1113 * @device: device bus address, starting at zero
1115 * Following bus reset, we issue the IDENTIFY [PACKET] DEVICE
1116 * command, and read back the 512-byte device information page.
1117 * The device information page is fed to us via the standard
1118 * PIO-IN protocol, but we hand-code it here. (TODO: investigate
1119 * using standard PIO-IN paths)
1121 * After reading the device information page, we use several
1122 * bits of information from it to initialize data structures
1123 * that will be used during the lifetime of the ata_device.
1124 * Other data from the info page is used to disqualify certain
1125 * older ATA devices we do not wish to support.
1128 * Inherited from caller. Some functions called by this function
1129 * obtain the host_set lock.
1132 static void ata_dev_identify(struct ata_port *ap, unsigned int device)
1134 struct ata_device *dev = &ap->device[device];
1135 unsigned int major_version;
1137 unsigned long xfer_modes;
1139 unsigned int using_edd;
1140 DECLARE_COMPLETION(wait);
1141 struct ata_queued_cmd *qc;
1142 unsigned long flags;
1145 if (!ata_dev_present(dev)) {
1146 DPRINTK("ENTER/EXIT (host %u, dev %u) -- nodev\n",
1151 if (ap->flags & (ATA_FLAG_SRST | ATA_FLAG_SATA_RESET))
1156 DPRINTK("ENTER, host %u, dev %u\n", ap->id, device);
1158 assert (dev->class == ATA_DEV_ATA || dev->class == ATA_DEV_ATAPI ||
1159 dev->class == ATA_DEV_NONE);
1161 ata_dev_select(ap, device, 1, 1); /* select device 0/1 */
1163 qc = ata_qc_new_init(ap, dev);
1166 ata_sg_init_one(qc, dev->id, sizeof(dev->id));
1167 qc->dma_dir = DMA_FROM_DEVICE;
1168 qc->tf.protocol = ATA_PROT_PIO;
1172 if (dev->class == ATA_DEV_ATA) {
1173 qc->tf.command = ATA_CMD_ID_ATA;
1174 DPRINTK("do ATA identify\n");
1176 qc->tf.command = ATA_CMD_ID_ATAPI;
1177 DPRINTK("do ATAPI identify\n");
1180 qc->waiting = &wait;
1181 qc->complete_fn = ata_qc_complete_noop;
1183 spin_lock_irqsave(&ap->host_set->lock, flags);
1184 rc = ata_qc_issue(qc);
1185 spin_unlock_irqrestore(&ap->host_set->lock, flags);
1190 wait_for_completion(&wait);
1192 status = ata_chk_status(ap);
1193 if (status & ATA_ERR) {
1195 * arg! EDD works for all test cases, but seems to return
1196 * the ATA signature for some ATAPI devices. Until the
1197 * reason for this is found and fixed, we fix up the mess
1198 * here. If IDENTIFY DEVICE returns command aborted
1199 * (as ATAPI devices do), then we issue an
1200 * IDENTIFY PACKET DEVICE.
1202 * ATA software reset (SRST, the default) does not appear
1203 * to have this problem.
1205 if ((using_edd) && (qc->tf.command == ATA_CMD_ID_ATA)) {
1206 u8 err = ata_chk_err(ap);
1207 if (err & ATA_ABORTED) {
1208 dev->class = ATA_DEV_ATAPI;
1219 swap_buf_le16(dev->id, ATA_ID_WORDS);
1221 /* print device capabilities */
1222 printk(KERN_DEBUG "ata%u: dev %u cfg "
1223 "49:%04x 82:%04x 83:%04x 84:%04x 85:%04x 86:%04x 87:%04x 88:%04x\n",
1224 ap->id, device, dev->id[49],
1225 dev->id[82], dev->id[83], dev->id[84],
1226 dev->id[85], dev->id[86], dev->id[87],
1230 * common ATA, ATAPI feature tests
1233 /* we require DMA support (bits 8 of word 49) */
1234 if (!ata_id_has_dma(dev->id)) {
1235 printk(KERN_DEBUG "ata%u: no dma\n", ap->id);
1239 /* quick-n-dirty find max transfer mode; for printk only */
1240 xfer_modes = dev->id[ATA_ID_UDMA_MODES];
1242 xfer_modes = (dev->id[ATA_ID_MWDMA_MODES]) << ATA_SHIFT_MWDMA;
1244 xfer_modes = (dev->id[ATA_ID_PIO_MODES]) << (ATA_SHIFT_PIO + 3);
1245 xfer_modes |= (0x7 << ATA_SHIFT_PIO);
1250 /* ATA-specific feature tests */
1251 if (dev->class == ATA_DEV_ATA) {
1252 if (!ata_id_is_ata(dev->id)) /* sanity check */
1255 /* get major version */
1256 tmp = dev->id[ATA_ID_MAJOR_VER];
1257 for (major_version = 14; major_version >= 1; major_version--)
1258 if (tmp & (1 << major_version))
1262 * The exact sequence expected by certain pre-ATA4 drives is:
1265 * INITIALIZE DEVICE PARAMETERS
1267 * Some drives were very specific about that exact sequence.
1269 if (major_version < 4 || (!ata_id_has_lba(dev->id)))
1270 ata_dev_init_params(ap, dev);
1272 if (ata_id_has_lba(dev->id)) {
1273 dev->flags |= ATA_DFLAG_LBA;
1275 if (ata_id_has_lba48(dev->id)) {
1276 dev->flags |= ATA_DFLAG_LBA48;
1277 dev->n_sectors = ata_id_u64(dev->id, 100);
1279 dev->n_sectors = ata_id_u32(dev->id, 60);
1282 /* print device info to dmesg */
1283 printk(KERN_INFO "ata%u: dev %u ATA-%d, max %s, %Lu sectors:%s\n",
1286 ata_mode_string(xfer_modes),
1287 (unsigned long long)dev->n_sectors,
1288 dev->flags & ATA_DFLAG_LBA48 ? " LBA48" : " LBA");
1292 /* Default translation */
1293 dev->cylinders = dev->id[1];
1294 dev->heads = dev->id[3];
1295 dev->sectors = dev->id[6];
1296 dev->n_sectors = dev->cylinders * dev->heads * dev->sectors;
1298 if (ata_id_current_chs_valid(dev->id)) {
1299 /* Current CHS translation is valid. */
1300 dev->cylinders = dev->id[54];
1301 dev->heads = dev->id[55];
1302 dev->sectors = dev->id[56];
1304 dev->n_sectors = ata_id_u32(dev->id, 57);
1307 /* print device info to dmesg */
1308 printk(KERN_INFO "ata%u: dev %u ATA-%d, max %s, %Lu sectors: CHS %d/%d/%d\n",
1311 ata_mode_string(xfer_modes),
1312 (unsigned long long)dev->n_sectors,
1313 (int)dev->cylinders, (int)dev->heads, (int)dev->sectors);
1317 ap->host->max_cmd_len = 16;
1320 /* ATAPI-specific feature tests */
1322 if (ata_id_is_ata(dev->id)) /* sanity check */
1325 rc = atapi_cdb_len(dev->id);
1326 if ((rc < 12) || (rc > ATAPI_CDB_LEN)) {
1327 printk(KERN_WARNING "ata%u: unsupported CDB len\n", ap->id);
1330 ap->cdb_len = (unsigned int) rc;
1331 ap->host->max_cmd_len = (unsigned char) ap->cdb_len;
1333 if (ata_id_cdb_intr(dev->id))
1334 dev->flags |= ATA_DFLAG_CDB_INTR;
1336 /* print device info to dmesg */
1337 printk(KERN_INFO "ata%u: dev %u ATAPI, max %s\n",
1339 ata_mode_string(xfer_modes));
1342 DPRINTK("EXIT, drv_stat = 0x%x\n", ata_chk_status(ap));
1346 printk(KERN_WARNING "ata%u: dev %u not supported, ignoring\n",
1349 dev->class++; /* converts ATA_DEV_xxx into ATA_DEV_xxx_UNSUP */
1350 DPRINTK("EXIT, err\n");
1354 static inline u8 ata_dev_knobble(struct ata_port *ap)
1356 return ((ap->cbl == ATA_CBL_SATA) && (!ata_id_is_sata(ap->device->id)));
1360 * ata_dev_config - Run device specific handlers and check for
1361 * SATA->PATA bridges
1368 void ata_dev_config(struct ata_port *ap, unsigned int i)
1370 /* limit bridge transfers to udma5, 200 sectors */
1371 if (ata_dev_knobble(ap)) {
1372 printk(KERN_INFO "ata%u(%u): applying bridge limits\n",
1373 ap->id, ap->device->devno);
1374 ap->udma_mask &= ATA_UDMA5;
1375 ap->host->max_sectors = ATA_MAX_SECTORS;
1376 ap->host->hostt->max_sectors = ATA_MAX_SECTORS;
1377 ap->device->flags |= ATA_DFLAG_LOCK_SECTORS;
1380 if (ap->ops->dev_config)
1381 ap->ops->dev_config(ap, &ap->device[i]);
1385 * ata_bus_probe - Reset and probe ATA bus
1388 * Master ATA bus probing function. Initiates a hardware-dependent
1389 * bus reset, then attempts to identify any devices found on
1393 * PCI/etc. bus probe sem.
1396 * Zero on success, non-zero on error.
1399 static int ata_bus_probe(struct ata_port *ap)
1401 unsigned int i, found = 0;
1403 ap->ops->phy_reset(ap);
1404 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1407 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1408 ata_dev_identify(ap, i);
1409 if (ata_dev_present(&ap->device[i])) {
1411 ata_dev_config(ap,i);
1415 if ((!found) || (ap->flags & ATA_FLAG_PORT_DISABLED))
1416 goto err_out_disable;
1419 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1420 goto err_out_disable;
1425 ap->ops->port_disable(ap);
1431 * ata_port_probe - Mark port as enabled
1432 * @ap: Port for which we indicate enablement
1434 * Modify @ap data structure such that the system
1435 * thinks that the entire port is enabled.
1437 * LOCKING: host_set lock, or some other form of
1441 void ata_port_probe(struct ata_port *ap)
1443 ap->flags &= ~ATA_FLAG_PORT_DISABLED;
1447 * __sata_phy_reset - Wake/reset a low-level SATA PHY
1448 * @ap: SATA port associated with target SATA PHY.
1450 * This function issues commands to standard SATA Sxxx
1451 * PHY registers, to wake up the phy (and device), and
1452 * clear any reset condition.
1455 * PCI/etc. bus probe sem.
1458 void __sata_phy_reset(struct ata_port *ap)
1461 unsigned long timeout = jiffies + (HZ * 5);
1463 if (ap->flags & ATA_FLAG_SATA_RESET) {
1464 /* issue phy wake/reset */
1465 scr_write_flush(ap, SCR_CONTROL, 0x301);
1466 /* Couldn't find anything in SATA I/II specs, but
1467 * AHCI-1.1 10.4.2 says at least 1 ms. */
1470 scr_write_flush(ap, SCR_CONTROL, 0x300); /* phy wake/clear reset */
1472 /* wait for phy to become ready, if necessary */
1475 sstatus = scr_read(ap, SCR_STATUS);
1476 if ((sstatus & 0xf) != 1)
1478 } while (time_before(jiffies, timeout));
1480 /* TODO: phy layer with polling, timeouts, etc. */
1481 if (sata_dev_present(ap))
1484 sstatus = scr_read(ap, SCR_STATUS);
1485 printk(KERN_INFO "ata%u: no device found (phy stat %08x)\n",
1487 ata_port_disable(ap);
1490 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1493 if (ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT)) {
1494 ata_port_disable(ap);
1498 ap->cbl = ATA_CBL_SATA;
1502 * sata_phy_reset - Reset SATA bus.
1503 * @ap: SATA port associated with target SATA PHY.
1505 * This function resets the SATA bus, and then probes
1506 * the bus for devices.
1509 * PCI/etc. bus probe sem.
1512 void sata_phy_reset(struct ata_port *ap)
1514 __sata_phy_reset(ap);
1515 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1521 * ata_port_disable - Disable port.
1522 * @ap: Port to be disabled.
1524 * Modify @ap data structure such that the system
1525 * thinks that the entire port is disabled, and should
1526 * never attempt to probe or communicate with devices
1529 * LOCKING: host_set lock, or some other form of
1533 void ata_port_disable(struct ata_port *ap)
1535 ap->device[0].class = ATA_DEV_NONE;
1536 ap->device[1].class = ATA_DEV_NONE;
1537 ap->flags |= ATA_FLAG_PORT_DISABLED;
1543 } xfer_mode_classes[] = {
1544 { ATA_SHIFT_UDMA, XFER_UDMA_0 },
1545 { ATA_SHIFT_MWDMA, XFER_MW_DMA_0 },
1546 { ATA_SHIFT_PIO, XFER_PIO_0 },
1549 static inline u8 base_from_shift(unsigned int shift)
1553 for (i = 0; i < ARRAY_SIZE(xfer_mode_classes); i++)
1554 if (xfer_mode_classes[i].shift == shift)
1555 return xfer_mode_classes[i].base;
1560 static void ata_dev_set_mode(struct ata_port *ap, struct ata_device *dev)
1565 if (!ata_dev_present(dev) || (ap->flags & ATA_FLAG_PORT_DISABLED))
1568 if (dev->xfer_shift == ATA_SHIFT_PIO)
1569 dev->flags |= ATA_DFLAG_PIO;
1571 ata_dev_set_xfermode(ap, dev);
1573 base = base_from_shift(dev->xfer_shift);
1574 ofs = dev->xfer_mode - base;
1575 idx = ofs + dev->xfer_shift;
1576 WARN_ON(idx >= ARRAY_SIZE(xfer_mode_str));
1578 DPRINTK("idx=%d xfer_shift=%u, xfer_mode=0x%x, base=0x%x, offset=%d\n",
1579 idx, dev->xfer_shift, (int)dev->xfer_mode, (int)base, ofs);
1581 printk(KERN_INFO "ata%u: dev %u configured for %s\n",
1582 ap->id, dev->devno, xfer_mode_str[idx]);
1585 static int ata_host_set_pio(struct ata_port *ap)
1591 mask = ata_get_mode_mask(ap, ATA_SHIFT_PIO);
1594 printk(KERN_WARNING "ata%u: no PIO support\n", ap->id);
1598 base = base_from_shift(ATA_SHIFT_PIO);
1599 xfer_mode = base + x;
1601 DPRINTK("base 0x%x xfer_mode 0x%x mask 0x%x x %d\n",
1602 (int)base, (int)xfer_mode, mask, x);
1604 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1605 struct ata_device *dev = &ap->device[i];
1606 if (ata_dev_present(dev)) {
1607 dev->pio_mode = xfer_mode;
1608 dev->xfer_mode = xfer_mode;
1609 dev->xfer_shift = ATA_SHIFT_PIO;
1610 if (ap->ops->set_piomode)
1611 ap->ops->set_piomode(ap, dev);
1618 static void ata_host_set_dma(struct ata_port *ap, u8 xfer_mode,
1619 unsigned int xfer_shift)
1623 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1624 struct ata_device *dev = &ap->device[i];
1625 if (ata_dev_present(dev)) {
1626 dev->dma_mode = xfer_mode;
1627 dev->xfer_mode = xfer_mode;
1628 dev->xfer_shift = xfer_shift;
1629 if (ap->ops->set_dmamode)
1630 ap->ops->set_dmamode(ap, dev);
1636 * ata_set_mode - Program timings and issue SET FEATURES - XFER
1637 * @ap: port on which timings will be programmed
1639 * Set ATA device disk transfer mode (PIO3, UDMA6, etc.).
1642 * PCI/etc. bus probe sem.
1645 static void ata_set_mode(struct ata_port *ap)
1647 unsigned int i, xfer_shift;
1651 /* step 1: always set host PIO timings */
1652 rc = ata_host_set_pio(ap);
1656 /* step 2: choose the best data xfer mode */
1657 xfer_mode = xfer_shift = 0;
1658 rc = ata_choose_xfer_mode(ap, &xfer_mode, &xfer_shift);
1662 /* step 3: if that xfer mode isn't PIO, set host DMA timings */
1663 if (xfer_shift != ATA_SHIFT_PIO)
1664 ata_host_set_dma(ap, xfer_mode, xfer_shift);
1666 /* step 4: update devices' xfer mode */
1667 ata_dev_set_mode(ap, &ap->device[0]);
1668 ata_dev_set_mode(ap, &ap->device[1]);
1670 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1673 if (ap->ops->post_set_mode)
1674 ap->ops->post_set_mode(ap);
1676 for (i = 0; i < 2; i++) {
1677 struct ata_device *dev = &ap->device[i];
1678 ata_dev_set_protocol(dev);
1684 ata_port_disable(ap);
1688 * ata_busy_sleep - sleep until BSY clears, or timeout
1689 * @ap: port containing status register to be polled
1690 * @tmout_pat: impatience timeout
1691 * @tmout: overall timeout
1693 * Sleep until ATA Status register bit BSY clears,
1694 * or a timeout occurs.
1700 static unsigned int ata_busy_sleep (struct ata_port *ap,
1701 unsigned long tmout_pat,
1702 unsigned long tmout)
1704 unsigned long timer_start, timeout;
1707 status = ata_busy_wait(ap, ATA_BUSY, 300);
1708 timer_start = jiffies;
1709 timeout = timer_start + tmout_pat;
1710 while ((status & ATA_BUSY) && (time_before(jiffies, timeout))) {
1712 status = ata_busy_wait(ap, ATA_BUSY, 3);
1715 if (status & ATA_BUSY)
1716 printk(KERN_WARNING "ata%u is slow to respond, "
1717 "please be patient\n", ap->id);
1719 timeout = timer_start + tmout;
1720 while ((status & ATA_BUSY) && (time_before(jiffies, timeout))) {
1722 status = ata_chk_status(ap);
1725 if (status & ATA_BUSY) {
1726 printk(KERN_ERR "ata%u failed to respond (%lu secs)\n",
1727 ap->id, tmout / HZ);
1734 static void ata_bus_post_reset(struct ata_port *ap, unsigned int devmask)
1736 struct ata_ioports *ioaddr = &ap->ioaddr;
1737 unsigned int dev0 = devmask & (1 << 0);
1738 unsigned int dev1 = devmask & (1 << 1);
1739 unsigned long timeout;
1741 /* if device 0 was found in ata_devchk, wait for its
1745 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
1747 /* if device 1 was found in ata_devchk, wait for
1748 * register access, then wait for BSY to clear
1750 timeout = jiffies + ATA_TMOUT_BOOT;
1754 ap->ops->dev_select(ap, 1);
1755 if (ap->flags & ATA_FLAG_MMIO) {
1756 nsect = readb((void __iomem *) ioaddr->nsect_addr);
1757 lbal = readb((void __iomem *) ioaddr->lbal_addr);
1759 nsect = inb(ioaddr->nsect_addr);
1760 lbal = inb(ioaddr->lbal_addr);
1762 if ((nsect == 1) && (lbal == 1))
1764 if (time_after(jiffies, timeout)) {
1768 msleep(50); /* give drive a breather */
1771 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
1773 /* is all this really necessary? */
1774 ap->ops->dev_select(ap, 0);
1776 ap->ops->dev_select(ap, 1);
1778 ap->ops->dev_select(ap, 0);
1782 * ata_bus_edd - Issue EXECUTE DEVICE DIAGNOSTIC command.
1783 * @ap: Port to reset and probe
1785 * Use the EXECUTE DEVICE DIAGNOSTIC command to reset and
1786 * probe the bus. Not often used these days.
1789 * PCI/etc. bus probe sem.
1793 static unsigned int ata_bus_edd(struct ata_port *ap)
1795 struct ata_taskfile tf;
1797 /* set up execute-device-diag (bus reset) taskfile */
1798 /* also, take interrupts to a known state (disabled) */
1799 DPRINTK("execute-device-diag\n");
1800 ata_tf_init(ap, &tf, 0);
1802 tf.command = ATA_CMD_EDD;
1803 tf.protocol = ATA_PROT_NODATA;
1806 ata_tf_to_host(ap, &tf);
1808 /* spec says at least 2ms. but who knows with those
1809 * crazy ATAPI devices...
1813 return ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
1816 static unsigned int ata_bus_softreset(struct ata_port *ap,
1817 unsigned int devmask)
1819 struct ata_ioports *ioaddr = &ap->ioaddr;
1821 DPRINTK("ata%u: bus reset via SRST\n", ap->id);
1823 /* software reset. causes dev0 to be selected */
1824 if (ap->flags & ATA_FLAG_MMIO) {
1825 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
1826 udelay(20); /* FIXME: flush */
1827 writeb(ap->ctl | ATA_SRST, (void __iomem *) ioaddr->ctl_addr);
1828 udelay(20); /* FIXME: flush */
1829 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
1831 outb(ap->ctl, ioaddr->ctl_addr);
1833 outb(ap->ctl | ATA_SRST, ioaddr->ctl_addr);
1835 outb(ap->ctl, ioaddr->ctl_addr);
1838 /* spec mandates ">= 2ms" before checking status.
1839 * We wait 150ms, because that was the magic delay used for
1840 * ATAPI devices in Hale Landis's ATADRVR, for the period of time
1841 * between when the ATA command register is written, and then
1842 * status is checked. Because waiting for "a while" before
1843 * checking status is fine, post SRST, we perform this magic
1844 * delay here as well.
1848 ata_bus_post_reset(ap, devmask);
1854 * ata_bus_reset - reset host port and associated ATA channel
1855 * @ap: port to reset
1857 * This is typically the first time we actually start issuing
1858 * commands to the ATA channel. We wait for BSY to clear, then
1859 * issue EXECUTE DEVICE DIAGNOSTIC command, polling for its
1860 * result. Determine what devices, if any, are on the channel
1861 * by looking at the device 0/1 error register. Look at the signature
1862 * stored in each device's taskfile registers, to determine if
1863 * the device is ATA or ATAPI.
1866 * PCI/etc. bus probe sem.
1867 * Obtains host_set lock.
1870 * Sets ATA_FLAG_PORT_DISABLED if bus reset fails.
1873 void ata_bus_reset(struct ata_port *ap)
1875 struct ata_ioports *ioaddr = &ap->ioaddr;
1876 unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS;
1878 unsigned int dev0, dev1 = 0, rc = 0, devmask = 0;
1880 DPRINTK("ENTER, host %u, port %u\n", ap->id, ap->port_no);
1882 /* determine if device 0/1 are present */
1883 if (ap->flags & ATA_FLAG_SATA_RESET)
1886 dev0 = ata_devchk(ap, 0);
1888 dev1 = ata_devchk(ap, 1);
1892 devmask |= (1 << 0);
1894 devmask |= (1 << 1);
1896 /* select device 0 again */
1897 ap->ops->dev_select(ap, 0);
1899 /* issue bus reset */
1900 if (ap->flags & ATA_FLAG_SRST)
1901 rc = ata_bus_softreset(ap, devmask);
1902 else if ((ap->flags & ATA_FLAG_SATA_RESET) == 0) {
1903 /* set up device control */
1904 if (ap->flags & ATA_FLAG_MMIO)
1905 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
1907 outb(ap->ctl, ioaddr->ctl_addr);
1908 rc = ata_bus_edd(ap);
1915 * determine by signature whether we have ATA or ATAPI devices
1917 err = ata_dev_try_classify(ap, 0);
1918 if ((slave_possible) && (err != 0x81))
1919 ata_dev_try_classify(ap, 1);
1921 /* re-enable interrupts */
1922 if (ap->ioaddr.ctl_addr) /* FIXME: hack. create a hook instead */
1925 /* is double-select really necessary? */
1926 if (ap->device[1].class != ATA_DEV_NONE)
1927 ap->ops->dev_select(ap, 1);
1928 if (ap->device[0].class != ATA_DEV_NONE)
1929 ap->ops->dev_select(ap, 0);
1931 /* if no devices were detected, disable this port */
1932 if ((ap->device[0].class == ATA_DEV_NONE) &&
1933 (ap->device[1].class == ATA_DEV_NONE))
1936 if (ap->flags & (ATA_FLAG_SATA_RESET | ATA_FLAG_SRST)) {
1937 /* set up device control for ATA_FLAG_SATA_RESET */
1938 if (ap->flags & ATA_FLAG_MMIO)
1939 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
1941 outb(ap->ctl, ioaddr->ctl_addr);
1948 printk(KERN_ERR "ata%u: disabling port\n", ap->id);
1949 ap->ops->port_disable(ap);
1954 static void ata_pr_blacklisted(struct ata_port *ap, struct ata_device *dev)
1956 printk(KERN_WARNING "ata%u: dev %u is on DMA blacklist, disabling DMA\n",
1957 ap->id, dev->devno);
1960 static const char * ata_dma_blacklist [] = {
1979 "Toshiba CD-ROM XM-6202B",
1980 "TOSHIBA CD-ROM XM-1702BC",
1982 "E-IDE CD-ROM CR-840",
1985 "SAMSUNG CD-ROM SC-148C",
1986 "SAMSUNG CD-ROM SC",
1988 "ATAPI CD-ROM DRIVE 40X MAXIMUM",
1992 static int ata_dma_blacklisted(struct ata_port *ap, struct ata_device *dev)
1994 unsigned char model_num[40];
1999 ata_dev_id_string(dev->id, model_num, ATA_ID_PROD_OFS,
2002 len = strnlen(s, sizeof(model_num));
2004 /* ATAPI specifies that empty space is blank-filled; remove blanks */
2005 while ((len > 0) && (s[len - 1] == ' ')) {
2010 for (i = 0; i < ARRAY_SIZE(ata_dma_blacklist); i++)
2011 if (!strncmp(ata_dma_blacklist[i], s, len))
2017 static unsigned int ata_get_mode_mask(struct ata_port *ap, int shift)
2019 struct ata_device *master, *slave;
2022 master = &ap->device[0];
2023 slave = &ap->device[1];
2025 assert (ata_dev_present(master) || ata_dev_present(slave));
2027 if (shift == ATA_SHIFT_UDMA) {
2028 mask = ap->udma_mask;
2029 if (ata_dev_present(master)) {
2030 mask &= (master->id[ATA_ID_UDMA_MODES] & 0xff);
2031 if (ata_dma_blacklisted(ap, master)) {
2033 ata_pr_blacklisted(ap, master);
2036 if (ata_dev_present(slave)) {
2037 mask &= (slave->id[ATA_ID_UDMA_MODES] & 0xff);
2038 if (ata_dma_blacklisted(ap, slave)) {
2040 ata_pr_blacklisted(ap, slave);
2044 else if (shift == ATA_SHIFT_MWDMA) {
2045 mask = ap->mwdma_mask;
2046 if (ata_dev_present(master)) {
2047 mask &= (master->id[ATA_ID_MWDMA_MODES] & 0x07);
2048 if (ata_dma_blacklisted(ap, master)) {
2050 ata_pr_blacklisted(ap, master);
2053 if (ata_dev_present(slave)) {
2054 mask &= (slave->id[ATA_ID_MWDMA_MODES] & 0x07);
2055 if (ata_dma_blacklisted(ap, slave)) {
2057 ata_pr_blacklisted(ap, slave);
2061 else if (shift == ATA_SHIFT_PIO) {
2062 mask = ap->pio_mask;
2063 if (ata_dev_present(master)) {
2064 /* spec doesn't return explicit support for
2065 * PIO0-2, so we fake it
2067 u16 tmp_mode = master->id[ATA_ID_PIO_MODES] & 0x03;
2072 if (ata_dev_present(slave)) {
2073 /* spec doesn't return explicit support for
2074 * PIO0-2, so we fake it
2076 u16 tmp_mode = slave->id[ATA_ID_PIO_MODES] & 0x03;
2083 mask = 0xffffffff; /* shut up compiler warning */
2090 /* find greatest bit */
2091 static int fgb(u32 bitmap)
2096 for (i = 0; i < 32; i++)
2097 if (bitmap & (1 << i))
2104 * ata_choose_xfer_mode - attempt to find best transfer mode
2105 * @ap: Port for which an xfer mode will be selected
2106 * @xfer_mode_out: (output) SET FEATURES - XFER MODE code
2107 * @xfer_shift_out: (output) bit shift that selects this mode
2109 * Based on host and device capabilities, determine the
2110 * maximum transfer mode that is amenable to all.
2113 * PCI/etc. bus probe sem.
2116 * Zero on success, negative on error.
2119 static int ata_choose_xfer_mode(struct ata_port *ap,
2121 unsigned int *xfer_shift_out)
2123 unsigned int mask, shift;
2126 for (i = 0; i < ARRAY_SIZE(xfer_mode_classes); i++) {
2127 shift = xfer_mode_classes[i].shift;
2128 mask = ata_get_mode_mask(ap, shift);
2132 *xfer_mode_out = xfer_mode_classes[i].base + x;
2133 *xfer_shift_out = shift;
2142 * ata_dev_set_xfermode - Issue SET FEATURES - XFER MODE command
2143 * @ap: Port associated with device @dev
2144 * @dev: Device to which command will be sent
2146 * Issue SET FEATURES - XFER MODE command to device @dev
2150 * PCI/etc. bus probe sem.
2153 static void ata_dev_set_xfermode(struct ata_port *ap, struct ata_device *dev)
2155 DECLARE_COMPLETION(wait);
2156 struct ata_queued_cmd *qc;
2158 unsigned long flags;
2160 /* set up set-features taskfile */
2161 DPRINTK("set features - xfer mode\n");
2163 qc = ata_qc_new_init(ap, dev);
2166 qc->tf.command = ATA_CMD_SET_FEATURES;
2167 qc->tf.feature = SETFEATURES_XFER;
2168 qc->tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
2169 qc->tf.protocol = ATA_PROT_NODATA;
2170 qc->tf.nsect = dev->xfer_mode;
2172 qc->waiting = &wait;
2173 qc->complete_fn = ata_qc_complete_noop;
2175 spin_lock_irqsave(&ap->host_set->lock, flags);
2176 rc = ata_qc_issue(qc);
2177 spin_unlock_irqrestore(&ap->host_set->lock, flags);
2180 ata_port_disable(ap);
2182 wait_for_completion(&wait);
2188 * ata_dev_init_params - Issue INIT DEV PARAMS command
2189 * @ap: Port associated with device @dev
2190 * @dev: Device to which command will be sent
2195 static void ata_dev_init_params(struct ata_port *ap, struct ata_device *dev)
2197 DECLARE_COMPLETION(wait);
2198 struct ata_queued_cmd *qc;
2200 unsigned long flags;
2201 u16 sectors = dev->id[6];
2202 u16 heads = dev->id[3];
2204 /* Number of sectors per track 1-255. Number of heads 1-16 */
2205 if (sectors < 1 || sectors > 255 || heads < 1 || heads > 16)
2208 /* set up init dev params taskfile */
2209 DPRINTK("init dev params \n");
2211 qc = ata_qc_new_init(ap, dev);
2214 qc->tf.command = ATA_CMD_INIT_DEV_PARAMS;
2215 qc->tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
2216 qc->tf.protocol = ATA_PROT_NODATA;
2217 qc->tf.nsect = sectors;
2218 qc->tf.device |= (heads - 1) & 0x0f; /* max head = num. of heads - 1 */
2220 qc->waiting = &wait;
2221 qc->complete_fn = ata_qc_complete_noop;
2223 spin_lock_irqsave(&ap->host_set->lock, flags);
2224 rc = ata_qc_issue(qc);
2225 spin_unlock_irqrestore(&ap->host_set->lock, flags);
2228 ata_port_disable(ap);
2230 wait_for_completion(&wait);
2236 * ata_sg_clean - Unmap DMA memory associated with command
2237 * @qc: Command containing DMA memory to be released
2239 * Unmap all mapped DMA memory associated with this command.
2242 * spin_lock_irqsave(host_set lock)
2245 static void ata_sg_clean(struct ata_queued_cmd *qc)
2247 struct ata_port *ap = qc->ap;
2248 struct scatterlist *sg = qc->sg;
2249 int dir = qc->dma_dir;
2251 assert(qc->flags & ATA_QCFLAG_DMAMAP);
2254 if (qc->flags & ATA_QCFLAG_SINGLE)
2255 assert(qc->n_elem == 1);
2257 DPRINTK("unmapping %u sg elements\n", qc->n_elem);
2259 if (qc->flags & ATA_QCFLAG_SG)
2260 dma_unmap_sg(ap->host_set->dev, sg, qc->n_elem, dir);
2262 dma_unmap_single(ap->host_set->dev, sg_dma_address(&sg[0]),
2263 sg_dma_len(&sg[0]), dir);
2265 qc->flags &= ~ATA_QCFLAG_DMAMAP;
2270 * ata_fill_sg - Fill PCI IDE PRD table
2271 * @qc: Metadata associated with taskfile to be transferred
2273 * Fill PCI IDE PRD (scatter-gather) table with segments
2274 * associated with the current disk command.
2277 * spin_lock_irqsave(host_set lock)
2280 static void ata_fill_sg(struct ata_queued_cmd *qc)
2282 struct scatterlist *sg = qc->sg;
2283 struct ata_port *ap = qc->ap;
2284 unsigned int idx, nelem;
2287 assert(qc->n_elem > 0);
2290 for (nelem = qc->n_elem; nelem; nelem--,sg++) {
2294 /* determine if physical DMA addr spans 64K boundary.
2295 * Note h/w doesn't support 64-bit, so we unconditionally
2296 * truncate dma_addr_t to u32.
2298 addr = (u32) sg_dma_address(sg);
2299 sg_len = sg_dma_len(sg);
2302 offset = addr & 0xffff;
2304 if ((offset + sg_len) > 0x10000)
2305 len = 0x10000 - offset;
2307 ap->prd[idx].addr = cpu_to_le32(addr);
2308 ap->prd[idx].flags_len = cpu_to_le32(len & 0xffff);
2309 VPRINTK("PRD[%u] = (0x%X, 0x%X)\n", idx, addr, len);
2318 ap->prd[idx - 1].flags_len |= cpu_to_le32(ATA_PRD_EOT);
2321 * ata_check_atapi_dma - Check whether ATAPI DMA can be supported
2322 * @qc: Metadata associated with taskfile to check
2324 * Allow low-level driver to filter ATA PACKET commands, returning
2325 * a status indicating whether or not it is OK to use DMA for the
2326 * supplied PACKET command.
2329 * spin_lock_irqsave(host_set lock)
2331 * RETURNS: 0 when ATAPI DMA can be used
2334 int ata_check_atapi_dma(struct ata_queued_cmd *qc)
2336 struct ata_port *ap = qc->ap;
2337 int rc = 0; /* Assume ATAPI DMA is OK by default */
2339 if (ap->ops->check_atapi_dma)
2340 rc = ap->ops->check_atapi_dma(qc);
2345 * ata_qc_prep - Prepare taskfile for submission
2346 * @qc: Metadata associated with taskfile to be prepared
2348 * Prepare ATA taskfile for submission.
2351 * spin_lock_irqsave(host_set lock)
2353 void ata_qc_prep(struct ata_queued_cmd *qc)
2355 if (!(qc->flags & ATA_QCFLAG_DMAMAP))
2362 * ata_sg_init_one - Associate command with memory buffer
2363 * @qc: Command to be associated
2364 * @buf: Memory buffer
2365 * @buflen: Length of memory buffer, in bytes.
2367 * Initialize the data-related elements of queued_cmd @qc
2368 * to point to a single memory buffer, @buf of byte length @buflen.
2371 * spin_lock_irqsave(host_set lock)
2374 void ata_sg_init_one(struct ata_queued_cmd *qc, void *buf, unsigned int buflen)
2376 struct scatterlist *sg;
2378 qc->flags |= ATA_QCFLAG_SINGLE;
2380 memset(&qc->sgent, 0, sizeof(qc->sgent));
2381 qc->sg = &qc->sgent;
2386 sg->page = virt_to_page(buf);
2387 sg->offset = (unsigned long) buf & ~PAGE_MASK;
2388 sg->length = buflen;
2392 * ata_sg_init - Associate command with scatter-gather table.
2393 * @qc: Command to be associated
2394 * @sg: Scatter-gather table.
2395 * @n_elem: Number of elements in s/g table.
2397 * Initialize the data-related elements of queued_cmd @qc
2398 * to point to a scatter-gather table @sg, containing @n_elem
2402 * spin_lock_irqsave(host_set lock)
2405 void ata_sg_init(struct ata_queued_cmd *qc, struct scatterlist *sg,
2406 unsigned int n_elem)
2408 qc->flags |= ATA_QCFLAG_SG;
2410 qc->n_elem = n_elem;
2414 * ata_sg_setup_one - DMA-map the memory buffer associated with a command.
2415 * @qc: Command with memory buffer to be mapped.
2417 * DMA-map the memory buffer associated with queued_cmd @qc.
2420 * spin_lock_irqsave(host_set lock)
2423 * Zero on success, negative on error.
2426 static int ata_sg_setup_one(struct ata_queued_cmd *qc)
2428 struct ata_port *ap = qc->ap;
2429 int dir = qc->dma_dir;
2430 struct scatterlist *sg = qc->sg;
2431 dma_addr_t dma_address;
2433 dma_address = dma_map_single(ap->host_set->dev, qc->buf_virt,
2435 if (dma_mapping_error(dma_address))
2438 sg_dma_address(sg) = dma_address;
2439 sg_dma_len(sg) = sg->length;
2441 DPRINTK("mapped buffer of %d bytes for %s\n", sg_dma_len(sg),
2442 qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
2448 * ata_sg_setup - DMA-map the scatter-gather table associated with a command.
2449 * @qc: Command with scatter-gather table to be mapped.
2451 * DMA-map the scatter-gather table associated with queued_cmd @qc.
2454 * spin_lock_irqsave(host_set lock)
2457 * Zero on success, negative on error.
2461 static int ata_sg_setup(struct ata_queued_cmd *qc)
2463 struct ata_port *ap = qc->ap;
2464 struct scatterlist *sg = qc->sg;
2467 VPRINTK("ENTER, ata%u\n", ap->id);
2468 assert(qc->flags & ATA_QCFLAG_SG);
2471 n_elem = dma_map_sg(ap->host_set->dev, sg, qc->n_elem, dir);
2475 DPRINTK("%d sg elements mapped\n", n_elem);
2477 qc->n_elem = n_elem;
2483 * ata_poll_qc_complete - turn irq back on and finish qc
2484 * @qc: Command to complete
2485 * @drv_stat: ATA status register content
2488 * None. (grabs host lock)
2491 void ata_poll_qc_complete(struct ata_queued_cmd *qc, u8 drv_stat)
2493 struct ata_port *ap = qc->ap;
2494 unsigned long flags;
2496 spin_lock_irqsave(&ap->host_set->lock, flags);
2498 ata_qc_complete(qc, drv_stat);
2499 spin_unlock_irqrestore(&ap->host_set->lock, flags);
2507 * None. (executing in kernel thread context)
2513 static unsigned long ata_pio_poll(struct ata_port *ap)
2516 unsigned int poll_state = HSM_ST_UNKNOWN;
2517 unsigned int reg_state = HSM_ST_UNKNOWN;
2518 const unsigned int tmout_state = HSM_ST_TMOUT;
2520 switch (ap->hsm_task_state) {
2523 poll_state = HSM_ST_POLL;
2527 case HSM_ST_LAST_POLL:
2528 poll_state = HSM_ST_LAST_POLL;
2529 reg_state = HSM_ST_LAST;
2536 status = ata_chk_status(ap);
2537 if (status & ATA_BUSY) {
2538 if (time_after(jiffies, ap->pio_task_timeout)) {
2539 ap->hsm_task_state = tmout_state;
2542 ap->hsm_task_state = poll_state;
2543 return ATA_SHORT_PAUSE;
2546 ap->hsm_task_state = reg_state;
2551 * ata_pio_complete -
2555 * None. (executing in kernel thread context)
2558 * Non-zero if qc completed, zero otherwise.
2561 static int ata_pio_complete (struct ata_port *ap)
2563 struct ata_queued_cmd *qc;
2567 * This is purely heuristic. This is a fast path. Sometimes when
2568 * we enter, BSY will be cleared in a chk-status or two. If not,
2569 * the drive is probably seeking or something. Snooze for a couple
2570 * msecs, then chk-status again. If still busy, fall back to
2571 * HSM_ST_POLL state.
2573 drv_stat = ata_busy_wait(ap, ATA_BUSY | ATA_DRQ, 10);
2574 if (drv_stat & (ATA_BUSY | ATA_DRQ)) {
2576 drv_stat = ata_busy_wait(ap, ATA_BUSY | ATA_DRQ, 10);
2577 if (drv_stat & (ATA_BUSY | ATA_DRQ)) {
2578 ap->hsm_task_state = HSM_ST_LAST_POLL;
2579 ap->pio_task_timeout = jiffies + ATA_TMOUT_PIO;
2584 drv_stat = ata_wait_idle(ap);
2585 if (!ata_ok(drv_stat)) {
2586 ap->hsm_task_state = HSM_ST_ERR;
2590 qc = ata_qc_from_tag(ap, ap->active_tag);
2593 ap->hsm_task_state = HSM_ST_IDLE;
2595 ata_poll_qc_complete(qc, drv_stat);
2597 /* another command may start at this point */
2605 * @buf: Buffer to swap
2606 * @buf_words: Number of 16-bit words in buffer.
2608 * Swap halves of 16-bit words if needed to convert from
2609 * little-endian byte order to native cpu byte order, or
2614 void swap_buf_le16(u16 *buf, unsigned int buf_words)
2619 for (i = 0; i < buf_words; i++)
2620 buf[i] = le16_to_cpu(buf[i]);
2621 #endif /* __BIG_ENDIAN */
2625 * ata_mmio_data_xfer - Transfer data by MMIO
2626 * @ap: port to read/write
2628 * @buflen: buffer length
2629 * @write_data: read/write
2631 * Transfer data from/to the device data register by MMIO.
2634 * Inherited from caller.
2638 static void ata_mmio_data_xfer(struct ata_port *ap, unsigned char *buf,
2639 unsigned int buflen, int write_data)
2642 unsigned int words = buflen >> 1;
2643 u16 *buf16 = (u16 *) buf;
2644 void __iomem *mmio = (void __iomem *)ap->ioaddr.data_addr;
2646 /* Transfer multiple of 2 bytes */
2648 for (i = 0; i < words; i++)
2649 writew(le16_to_cpu(buf16[i]), mmio);
2651 for (i = 0; i < words; i++)
2652 buf16[i] = cpu_to_le16(readw(mmio));
2655 /* Transfer trailing 1 byte, if any. */
2656 if (unlikely(buflen & 0x01)) {
2657 u16 align_buf[1] = { 0 };
2658 unsigned char *trailing_buf = buf + buflen - 1;
2661 memcpy(align_buf, trailing_buf, 1);
2662 writew(le16_to_cpu(align_buf[0]), mmio);
2664 align_buf[0] = cpu_to_le16(readw(mmio));
2665 memcpy(trailing_buf, align_buf, 1);
2671 * ata_pio_data_xfer - Transfer data by PIO
2672 * @ap: port to read/write
2674 * @buflen: buffer length
2675 * @write_data: read/write
2677 * Transfer data from/to the device data register by PIO.
2680 * Inherited from caller.
2684 static void ata_pio_data_xfer(struct ata_port *ap, unsigned char *buf,
2685 unsigned int buflen, int write_data)
2687 unsigned int words = buflen >> 1;
2689 /* Transfer multiple of 2 bytes */
2691 outsw(ap->ioaddr.data_addr, buf, words);
2693 insw(ap->ioaddr.data_addr, buf, words);
2695 /* Transfer trailing 1 byte, if any. */
2696 if (unlikely(buflen & 0x01)) {
2697 u16 align_buf[1] = { 0 };
2698 unsigned char *trailing_buf = buf + buflen - 1;
2701 memcpy(align_buf, trailing_buf, 1);
2702 outw(le16_to_cpu(align_buf[0]), ap->ioaddr.data_addr);
2704 align_buf[0] = cpu_to_le16(inw(ap->ioaddr.data_addr));
2705 memcpy(trailing_buf, align_buf, 1);
2711 * ata_data_xfer - Transfer data from/to the data register.
2712 * @ap: port to read/write
2714 * @buflen: buffer length
2715 * @do_write: read/write
2717 * Transfer data from/to the device data register.
2720 * Inherited from caller.
2724 static void ata_data_xfer(struct ata_port *ap, unsigned char *buf,
2725 unsigned int buflen, int do_write)
2727 if (ap->flags & ATA_FLAG_MMIO)
2728 ata_mmio_data_xfer(ap, buf, buflen, do_write);
2730 ata_pio_data_xfer(ap, buf, buflen, do_write);
2734 * ata_pio_sector - Transfer ATA_SECT_SIZE (512 bytes) of data.
2735 * @qc: Command on going
2737 * Transfer ATA_SECT_SIZE of data from/to the ATA device.
2740 * Inherited from caller.
2743 static void ata_pio_sector(struct ata_queued_cmd *qc)
2745 int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
2746 struct scatterlist *sg = qc->sg;
2747 struct ata_port *ap = qc->ap;
2749 unsigned int offset;
2751 unsigned long flags;
2753 if (qc->cursect == (qc->nsect - 1))
2754 ap->hsm_task_state = HSM_ST_LAST;
2756 page = sg[qc->cursg].page;
2757 offset = sg[qc->cursg].offset + qc->cursg_ofs * ATA_SECT_SIZE;
2759 /* get the current page and offset */
2760 page = nth_page(page, (offset >> PAGE_SHIFT));
2761 offset %= PAGE_SIZE;
2763 local_irq_save(flags);
2764 buf = kmap_atomic(page, KM_IRQ0) + offset;
2769 if ((qc->cursg_ofs * ATA_SECT_SIZE) == (&sg[qc->cursg])->length) {
2774 DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
2776 /* do the actual data transfer */
2777 do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
2778 ata_data_xfer(ap, buf, ATA_SECT_SIZE, do_write);
2780 kunmap_atomic(buf - offset, KM_IRQ0);
2781 local_irq_restore(flags);
2785 * atapi_send_cdb - Write CDB bytes to hardware
2786 * @ap: Port to which ATAPI device is attached.
2787 * @qc: Taskfile currently active
2789 * When device has indicated its readiness to accept
2790 * a CDB, this function is called. Send the CDB.
2796 static void atapi_send_cdb(struct ata_port *ap, struct ata_queued_cmd *qc)
2799 DPRINTK("send cdb\n");
2800 assert(ap->cdb_len >= 12);
2802 ata_data_xfer(ap, qc->cdb, ap->cdb_len, 1);
2803 ata_altstatus(ap); /* flush */
2805 switch (qc->tf.protocol) {
2806 case ATA_PROT_ATAPI:
2807 ap->hsm_task_state = HSM_ST;
2809 case ATA_PROT_ATAPI_NODATA:
2810 ap->hsm_task_state = HSM_ST_LAST;
2812 case ATA_PROT_ATAPI_DMA:
2813 ap->hsm_task_state = HSM_ST_LAST;
2814 /* initiate bmdma */
2815 ap->ops->bmdma_start(qc);
2821 * ata_dataout_task - Write first data block to hardware
2822 * @_data: Port to which ATA/ATAPI device is attached.
2824 * When device has indicated its readiness to accept
2825 * the data, this function sends out the CDB or
2826 * the first data block by PIO.
2828 * - If polling, ata_pio_task() handles the rest.
2829 * - Otherwise, interrupt handler takes over.
2832 * Kernel thread context (may sleep)
2835 static void ata_dataout_task(void *_data)
2837 struct ata_port *ap = _data;
2838 struct ata_queued_cmd *qc;
2840 unsigned long flags;
2842 qc = ata_qc_from_tag(ap, ap->active_tag);
2844 assert(qc->flags & ATA_QCFLAG_ACTIVE);
2846 /* sleep-wait for BSY to clear */
2847 DPRINTK("busy wait\n");
2848 if (ata_busy_sleep(ap, ATA_TMOUT_DATAOUT_QUICK, ATA_TMOUT_DATAOUT))
2851 /* make sure DRQ is set */
2852 status = ata_chk_status(ap);
2853 if ((status & (ATA_BUSY | ATA_DRQ)) != ATA_DRQ)
2856 /* Send the CDB (atapi) or the first data block (ata pio out).
2857 * During the state transition, interrupt handler shouldn't
2858 * be invoked before the data transfer is complete and
2859 * hsm_task_state is changed. Hence, the following locking.
2861 spin_lock_irqsave(&ap->host_set->lock, flags);
2863 if (qc->tf.protocol == ATA_PROT_PIO) {
2864 /* PIO data out protocol.
2865 * send first data block.
2868 /* ata_pio_sector() might change the state to HSM_ST_LAST.
2869 * so, the state is changed here before ata_pio_sector().
2871 ap->hsm_task_state = HSM_ST;
2873 ata_altstatus(ap); /* flush */
2876 atapi_send_cdb(ap, qc);
2878 /* if polling, ata_pio_task() handles the rest.
2879 * otherwise, interrupt handler takes over from here.
2881 if (qc->tf.flags & ATA_TFLAG_POLLING)
2882 queue_work(ata_wq, &ap->pio_task);
2884 spin_unlock_irqrestore(&ap->host_set->lock, flags);
2893 * __atapi_pio_bytes - Transfer data from/to the ATAPI device.
2894 * @qc: Command on going
2895 * @bytes: number of bytes
2897 * Transfer Transfer data from/to the ATAPI device.
2900 * Inherited from caller.
2904 static void __atapi_pio_bytes(struct ata_queued_cmd *qc, unsigned int bytes)
2906 int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
2907 struct scatterlist *sg = qc->sg;
2908 struct ata_port *ap = qc->ap;
2911 unsigned int offset, count;
2912 unsigned long flags;
2914 if (qc->curbytes + bytes >= qc->nbytes)
2915 ap->hsm_task_state = HSM_ST_LAST;
2918 if (unlikely(qc->cursg >= qc->n_elem)) {
2920 * The end of qc->sg is reached and the device expects
2921 * more data to transfer. In order not to overrun qc->sg
2922 * and fulfill length specified in the byte count register,
2923 * - for read case, discard trailing data from the device
2924 * - for write case, padding zero data to the device
2926 u16 pad_buf[1] = { 0 };
2927 unsigned int words = bytes >> 1;
2930 if (words) /* warning if bytes > 1 */
2931 printk(KERN_WARNING "ata%u: %u bytes trailing data\n",
2934 for (i = 0; i < words; i++)
2935 ata_data_xfer(ap, (unsigned char*)pad_buf, 2, do_write);
2937 ap->hsm_task_state = HSM_ST_LAST;
2941 sg = &qc->sg[qc->cursg];
2944 offset = sg->offset + qc->cursg_ofs;
2946 /* get the current page and offset */
2947 page = nth_page(page, (offset >> PAGE_SHIFT));
2948 offset %= PAGE_SIZE;
2950 /* don't overrun current sg */
2951 count = min(sg->length - qc->cursg_ofs, bytes);
2953 /* don't cross page boundaries */
2954 count = min(count, (unsigned int)PAGE_SIZE - offset);
2956 local_irq_save(flags);
2957 buf = kmap_atomic(page, KM_IRQ0) + offset;
2960 qc->curbytes += count;
2961 qc->cursg_ofs += count;
2963 if (qc->cursg_ofs == sg->length) {
2968 DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
2970 /* do the actual data transfer */
2971 ata_data_xfer(ap, buf, count, do_write);
2973 kunmap_atomic(buf - offset, KM_IRQ0);
2974 local_irq_restore(flags);
2981 * atapi_pio_bytes - Transfer data from/to the ATAPI device.
2982 * @qc: Command on going
2984 * Transfer Transfer data from/to the ATAPI device.
2987 * Inherited from caller.
2991 static void atapi_pio_bytes(struct ata_queued_cmd *qc)
2993 struct ata_port *ap = qc->ap;
2994 struct ata_device *dev = qc->dev;
2995 unsigned int ireason, bc_lo, bc_hi, bytes;
2996 int i_write, do_write = (qc->tf.flags & ATA_TFLAG_WRITE) ? 1 : 0;
2998 ap->ops->tf_read(ap, &qc->tf);
2999 ireason = qc->tf.nsect;
3000 bc_lo = qc->tf.lbam;
3001 bc_hi = qc->tf.lbah;
3002 bytes = (bc_hi << 8) | bc_lo;
3004 /* shall be cleared to zero, indicating xfer of data */
3005 if (ireason & (1 << 0))
3008 /* make sure transfer direction matches expected */
3009 i_write = ((ireason & (1 << 1)) == 0) ? 1 : 0;
3010 if (do_write != i_write)
3013 VPRINTK("ata%u: xfering %d bytes\n", ap->id, bytes);
3015 __atapi_pio_bytes(qc, bytes);
3020 printk(KERN_INFO "ata%u: dev %u: ATAPI check failed\n",
3021 ap->id, dev->devno);
3022 ap->hsm_task_state = HSM_ST_ERR;
3030 * None. (executing in kernel thread context)
3033 static void ata_pio_block(struct ata_port *ap)
3035 struct ata_queued_cmd *qc;
3039 * This is purely hueristic. This is a fast path.
3040 * Sometimes when we enter, BSY will be cleared in
3041 * a chk-status or two. If not, the drive is probably seeking
3042 * or something. Snooze for a couple msecs, then
3043 * chk-status again. If still busy, fall back to
3044 * HSM_ST_POLL state.
3046 status = ata_busy_wait(ap, ATA_BUSY, 5);
3047 if (status & ATA_BUSY) {
3049 status = ata_busy_wait(ap, ATA_BUSY, 10);
3050 if (status & ATA_BUSY) {
3051 ap->hsm_task_state = HSM_ST_POLL;
3052 ap->pio_task_timeout = jiffies + ATA_TMOUT_PIO;
3057 qc = ata_qc_from_tag(ap, ap->active_tag);
3060 if (is_atapi_taskfile(&qc->tf)) {
3061 /* no more data to transfer or unsupported ATAPI command */
3062 if ((status & ATA_DRQ) == 0) {
3063 ap->hsm_task_state = HSM_ST_LAST;
3067 atapi_pio_bytes(qc);
3069 /* handle BSY=0, DRQ=0 as error */
3070 if ((status & ATA_DRQ) == 0) {
3071 ap->hsm_task_state = HSM_ST_ERR;
3079 static void ata_pio_error(struct ata_port *ap)
3081 struct ata_queued_cmd *qc;
3084 qc = ata_qc_from_tag(ap, ap->active_tag);
3087 drv_stat = ata_chk_status(ap);
3088 printk(KERN_WARNING "ata%u: PIO error, drv_stat 0x%x\n",
3091 ap->hsm_task_state = HSM_ST_IDLE;
3093 ata_poll_qc_complete(qc, drv_stat | ATA_ERR);
3096 static void ata_pio_task(void *_data)
3098 struct ata_port *ap = _data;
3099 unsigned long timeout;
3106 switch (ap->hsm_task_state) {
3115 qc_completed = ata_pio_complete(ap);
3119 case HSM_ST_LAST_POLL:
3120 timeout = ata_pio_poll(ap);
3130 queue_delayed_work(ata_wq, &ap->pio_task, timeout);
3131 else if (!qc_completed)
3136 * ata_qc_timeout - Handle timeout of queued command
3137 * @qc: Command that timed out
3139 * Some part of the kernel (currently, only the SCSI layer)
3140 * has noticed that the active command on port @ap has not
3141 * completed after a specified length of time. Handle this
3142 * condition by disabling DMA (if necessary) and completing
3143 * transactions, with error if necessary.
3145 * This also handles the case of the "lost interrupt", where
3146 * for some reason (possibly hardware bug, possibly driver bug)
3147 * an interrupt was not delivered to the driver, even though the
3148 * transaction completed successfully.
3151 * Inherited from SCSI layer (none, can sleep)
3154 static void ata_qc_timeout(struct ata_queued_cmd *qc)
3156 struct ata_port *ap = qc->ap;
3157 struct ata_host_set *host_set = ap->host_set;
3158 struct ata_device *dev = qc->dev;
3159 u8 host_stat = 0, drv_stat;
3160 unsigned long flags;
3164 /* FIXME: doesn't this conflict with timeout handling? */
3165 if (qc->dev->class == ATA_DEV_ATAPI && qc->scsicmd) {
3166 struct scsi_cmnd *cmd = qc->scsicmd;
3168 if (!(cmd->eh_eflags & SCSI_EH_CANCEL_CMD)) {
3170 /* finish completing original command */
3171 spin_lock_irqsave(&host_set->lock, flags);
3172 __ata_qc_complete(qc);
3173 spin_unlock_irqrestore(&host_set->lock, flags);
3175 atapi_request_sense(ap, dev, cmd);
3177 cmd->result = (CHECK_CONDITION << 1) | (DID_OK << 16);
3178 scsi_finish_command(cmd);
3184 spin_lock_irqsave(&host_set->lock, flags);
3186 /* hack alert! We cannot use the supplied completion
3187 * function from inside the ->eh_strategy_handler() thread.
3188 * libata is the only user of ->eh_strategy_handler() in
3189 * any kernel, so the default scsi_done() assumes it is
3190 * not being called from the SCSI EH.
3192 qc->scsidone = scsi_finish_command;
3194 switch (qc->tf.protocol) {
3197 case ATA_PROT_ATAPI_DMA:
3198 host_stat = ap->ops->bmdma_status(ap);
3200 /* before we do anything else, clear DMA-Start bit */
3201 ap->ops->bmdma_stop(qc);
3207 drv_stat = ata_chk_status(ap);
3209 /* ack bmdma irq events */
3210 ap->ops->irq_clear(ap);
3212 printk(KERN_ERR "ata%u: command 0x%x timeout, stat 0x%x host_stat 0x%x\n",
3213 ap->id, qc->tf.command, drv_stat, host_stat);
3215 ap->hsm_task_state = HSM_ST_IDLE;
3217 /* complete taskfile transaction */
3218 ata_qc_complete(qc, drv_stat);
3222 spin_unlock_irqrestore(&host_set->lock, flags);
3229 * ata_eng_timeout - Handle timeout of queued command
3230 * @ap: Port on which timed-out command is active
3232 * Some part of the kernel (currently, only the SCSI layer)
3233 * has noticed that the active command on port @ap has not
3234 * completed after a specified length of time. Handle this
3235 * condition by disabling DMA (if necessary) and completing
3236 * transactions, with error if necessary.
3238 * This also handles the case of the "lost interrupt", where
3239 * for some reason (possibly hardware bug, possibly driver bug)
3240 * an interrupt was not delivered to the driver, even though the
3241 * transaction completed successfully.
3244 * Inherited from SCSI layer (none, can sleep)
3247 void ata_eng_timeout(struct ata_port *ap)
3249 struct ata_queued_cmd *qc;
3253 qc = ata_qc_from_tag(ap, ap->active_tag);
3257 printk(KERN_ERR "ata%u: BUG: timeout without command\n",
3267 * ata_qc_new - Request an available ATA command, for queueing
3268 * @ap: Port associated with device @dev
3269 * @dev: Device from whom we request an available command structure
3275 static struct ata_queued_cmd *ata_qc_new(struct ata_port *ap)
3277 struct ata_queued_cmd *qc = NULL;
3280 for (i = 0; i < ATA_MAX_QUEUE; i++)
3281 if (!test_and_set_bit(i, &ap->qactive)) {
3282 qc = ata_qc_from_tag(ap, i);
3293 * ata_qc_new_init - Request an available ATA command, and initialize it
3294 * @ap: Port associated with device @dev
3295 * @dev: Device from whom we request an available command structure
3301 struct ata_queued_cmd *ata_qc_new_init(struct ata_port *ap,
3302 struct ata_device *dev)
3304 struct ata_queued_cmd *qc;
3306 qc = ata_qc_new(ap);
3313 qc->cursect = qc->cursg = qc->cursg_ofs = 0;
3315 qc->nbytes = qc->curbytes = 0;
3317 ata_tf_init(ap, &qc->tf, dev->devno);
3319 if (dev->flags & ATA_DFLAG_LBA) {
3320 qc->tf.flags |= ATA_TFLAG_LBA;
3322 if (dev->flags & ATA_DFLAG_LBA48)
3323 qc->tf.flags |= ATA_TFLAG_LBA48;
3330 int ata_qc_complete_noop(struct ata_queued_cmd *qc, u8 drv_stat)
3335 static void __ata_qc_complete(struct ata_queued_cmd *qc)
3337 struct ata_port *ap = qc->ap;
3338 unsigned int tag, do_clear = 0;
3342 if (likely(ata_tag_valid(tag))) {
3343 if (tag == ap->active_tag)
3344 ap->active_tag = ATA_TAG_POISON;
3345 qc->tag = ATA_TAG_POISON;
3350 struct completion *waiting = qc->waiting;
3355 if (likely(do_clear))
3356 clear_bit(tag, &ap->qactive);
3360 * ata_qc_free - free unused ata_queued_cmd
3361 * @qc: Command to complete
3363 * Designed to free unused ata_queued_cmd object
3364 * in case something prevents using it.
3367 * spin_lock_irqsave(host_set lock)
3370 void ata_qc_free(struct ata_queued_cmd *qc)
3372 assert(qc != NULL); /* ata_qc_from_tag _might_ return NULL */
3373 assert(qc->waiting == NULL); /* nothing should be waiting */
3375 __ata_qc_complete(qc);
3379 * ata_qc_complete - Complete an active ATA command
3380 * @qc: Command to complete
3381 * @drv_stat: ATA Status register contents
3383 * Indicate to the mid and upper layers that an ATA
3384 * command has completed, with either an ok or not-ok status.
3387 * spin_lock_irqsave(host_set lock)
3391 void ata_qc_complete(struct ata_queued_cmd *qc, u8 drv_stat)
3395 assert(qc != NULL); /* ata_qc_from_tag _might_ return NULL */
3396 assert(qc->flags & ATA_QCFLAG_ACTIVE);
3398 if (likely(qc->flags & ATA_QCFLAG_DMAMAP))
3401 /* atapi: mark qc as inactive to prevent the interrupt handler
3402 * from completing the command twice later, before the error handler
3403 * is called. (when rc != 0 and atapi request sense is needed)
3405 qc->flags &= ~ATA_QCFLAG_ACTIVE;
3407 /* call completion callback */
3408 rc = qc->complete_fn(qc, drv_stat);
3410 /* if callback indicates not to complete command (non-zero),
3411 * return immediately
3416 __ata_qc_complete(qc);
3421 static inline int ata_should_dma_map(struct ata_queued_cmd *qc)
3423 struct ata_port *ap = qc->ap;
3425 switch (qc->tf.protocol) {
3427 case ATA_PROT_ATAPI_DMA:
3430 case ATA_PROT_ATAPI:
3432 case ATA_PROT_PIO_MULT:
3433 if (ap->flags & ATA_FLAG_PIO_DMA)
3446 * ata_qc_issue - issue taskfile to device
3447 * @qc: command to issue to device
3449 * Prepare an ATA command to submission to device.
3450 * This includes mapping the data into a DMA-able
3451 * area, filling in the S/G table, and finally
3452 * writing the taskfile to hardware, starting the command.
3455 * spin_lock_irqsave(host_set lock)
3458 * Zero on success, negative on error.
3461 int ata_qc_issue(struct ata_queued_cmd *qc)
3463 struct ata_port *ap = qc->ap;
3465 if (ata_should_dma_map(qc)) {
3466 if (qc->flags & ATA_QCFLAG_SG) {
3467 if (ata_sg_setup(qc))
3469 } else if (qc->flags & ATA_QCFLAG_SINGLE) {
3470 if (ata_sg_setup_one(qc))
3474 qc->flags &= ~ATA_QCFLAG_DMAMAP;
3477 ap->ops->qc_prep(qc);
3479 qc->ap->active_tag = qc->tag;
3480 qc->flags |= ATA_QCFLAG_ACTIVE;
3482 return ap->ops->qc_issue(qc);
3490 * ata_qc_issue_prot - issue taskfile to device in proto-dependent manner
3491 * @qc: command to issue to device
3493 * Using various libata functions and hooks, this function
3494 * starts an ATA command. ATA commands are grouped into
3495 * classes called "protocols", and issuing each type of protocol
3496 * is slightly different.
3498 * May be used as the qc_issue() entry in ata_port_operations.
3501 * spin_lock_irqsave(host_set lock)
3504 * Zero on success, negative on error.
3507 int ata_qc_issue_prot(struct ata_queued_cmd *qc)
3509 struct ata_port *ap = qc->ap;
3511 /* Use polling pio if the LLD doesn't handle
3512 * interrupt driven pio and atapi CDB interrupt.
3514 if (ap->flags & ATA_FLAG_PIO_POLLING) {
3515 switch (qc->tf.protocol) {
3517 case ATA_PROT_ATAPI:
3518 case ATA_PROT_ATAPI_NODATA:
3519 qc->tf.flags |= ATA_TFLAG_POLLING;
3521 case ATA_PROT_ATAPI_DMA:
3522 if (qc->dev->flags & ATA_DFLAG_CDB_INTR)
3530 /* select the device */
3531 ata_dev_select(ap, qc->dev->devno, 1, 0);
3533 /* start the command */
3534 switch (qc->tf.protocol) {
3535 case ATA_PROT_NODATA:
3536 if (qc->tf.flags & ATA_TFLAG_POLLING)
3537 ata_qc_set_polling(qc);
3539 ata_tf_to_host_nolock(ap, &qc->tf);
3540 ap->hsm_task_state = HSM_ST_LAST;
3542 if (qc->tf.flags & ATA_TFLAG_POLLING)
3543 queue_work(ata_wq, &ap->pio_task);
3548 assert(!(qc->tf.flags & ATA_TFLAG_POLLING));
3550 ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
3551 ap->ops->bmdma_setup(qc); /* set up bmdma */
3552 ap->ops->bmdma_start(qc); /* initiate bmdma */
3553 ap->hsm_task_state = HSM_ST_LAST;
3557 if (qc->tf.flags & ATA_TFLAG_POLLING)
3558 ata_qc_set_polling(qc);
3560 ata_tf_to_host_nolock(ap, &qc->tf);
3562 if (qc->tf.flags & ATA_TFLAG_WRITE) {
3563 /* PIO data out protocol */
3564 ap->hsm_task_state = HSM_ST_FIRST;
3565 queue_work(ata_wq, &ap->dataout_task);
3567 /* always send first data block using
3568 * the ata_dataout_task() codepath.
3571 /* PIO data in protocol */
3572 ap->hsm_task_state = HSM_ST;
3574 if (qc->tf.flags & ATA_TFLAG_POLLING)
3575 queue_work(ata_wq, &ap->pio_task);
3577 /* if polling, ata_pio_task() handles the rest.
3578 * otherwise, interrupt handler takes over from here.
3584 case ATA_PROT_ATAPI:
3585 case ATA_PROT_ATAPI_NODATA:
3586 if (qc->tf.flags & ATA_TFLAG_POLLING)
3587 ata_qc_set_polling(qc);
3589 ata_tf_to_host_nolock(ap, &qc->tf);
3590 ap->hsm_task_state = HSM_ST_FIRST;
3592 /* send cdb by polling if no cdb interrupt */
3593 if ((!(qc->dev->flags & ATA_DFLAG_CDB_INTR)) ||
3594 (qc->tf.flags & ATA_TFLAG_POLLING))
3595 queue_work(ata_wq, &ap->dataout_task);
3598 case ATA_PROT_ATAPI_DMA:
3599 assert(!(qc->tf.flags & ATA_TFLAG_POLLING));
3601 ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
3602 ap->ops->bmdma_setup(qc); /* set up bmdma */
3603 ap->hsm_task_state = HSM_ST_FIRST;
3605 /* send cdb by polling if no cdb interrupt */
3606 if (!(qc->dev->flags & ATA_DFLAG_CDB_INTR))
3607 queue_work(ata_wq, &ap->dataout_task);
3619 * ata_bmdma_setup_mmio - Set up PCI IDE BMDMA transaction
3620 * @qc: Info associated with this ATA transaction.
3623 * spin_lock_irqsave(host_set lock)
3626 static void ata_bmdma_setup_mmio (struct ata_queued_cmd *qc)
3628 struct ata_port *ap = qc->ap;
3629 unsigned int rw = (qc->tf.flags & ATA_TFLAG_WRITE);
3631 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
3633 /* load PRD table addr. */
3634 mb(); /* make sure PRD table writes are visible to controller */
3635 writel(ap->prd_dma, mmio + ATA_DMA_TABLE_OFS);
3637 /* specify data direction, triple-check start bit is clear */
3638 dmactl = readb(mmio + ATA_DMA_CMD);
3639 dmactl &= ~(ATA_DMA_WR | ATA_DMA_START);
3641 dmactl |= ATA_DMA_WR;
3642 writeb(dmactl, mmio + ATA_DMA_CMD);
3644 /* issue r/w command */
3645 ap->ops->exec_command(ap, &qc->tf);
3649 * ata_bmdma_start_mmio - Start a PCI IDE BMDMA transaction
3650 * @qc: Info associated with this ATA transaction.
3653 * spin_lock_irqsave(host_set lock)
3656 static void ata_bmdma_start_mmio (struct ata_queued_cmd *qc)
3658 struct ata_port *ap = qc->ap;
3659 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
3662 /* start host DMA transaction */
3663 dmactl = readb(mmio + ATA_DMA_CMD);
3664 writeb(dmactl | ATA_DMA_START, mmio + ATA_DMA_CMD);
3666 /* Strictly, one may wish to issue a readb() here, to
3667 * flush the mmio write. However, control also passes
3668 * to the hardware at this point, and it will interrupt
3669 * us when we are to resume control. So, in effect,
3670 * we don't care when the mmio write flushes.
3671 * Further, a read of the DMA status register _immediately_
3672 * following the write may not be what certain flaky hardware
3673 * is expected, so I think it is best to not add a readb()
3674 * without first all the MMIO ATA cards/mobos.
3675 * Or maybe I'm just being paranoid.
3680 * ata_bmdma_setup_pio - Set up PCI IDE BMDMA transaction (PIO)
3681 * @qc: Info associated with this ATA transaction.
3684 * spin_lock_irqsave(host_set lock)
3687 static void ata_bmdma_setup_pio (struct ata_queued_cmd *qc)
3689 struct ata_port *ap = qc->ap;
3690 unsigned int rw = (qc->tf.flags & ATA_TFLAG_WRITE);
3693 /* load PRD table addr. */
3694 outl(ap->prd_dma, ap->ioaddr.bmdma_addr + ATA_DMA_TABLE_OFS);
3696 /* specify data direction, triple-check start bit is clear */
3697 dmactl = inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3698 dmactl &= ~(ATA_DMA_WR | ATA_DMA_START);
3700 dmactl |= ATA_DMA_WR;
3701 outb(dmactl, ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3703 /* issue r/w command */
3704 ap->ops->exec_command(ap, &qc->tf);
3708 * ata_bmdma_start_pio - Start a PCI IDE BMDMA transaction (PIO)
3709 * @qc: Info associated with this ATA transaction.
3712 * spin_lock_irqsave(host_set lock)
3715 static void ata_bmdma_start_pio (struct ata_queued_cmd *qc)
3717 struct ata_port *ap = qc->ap;
3720 /* start host DMA transaction */
3721 dmactl = inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3722 outb(dmactl | ATA_DMA_START,
3723 ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3728 * ata_bmdma_start - Start a PCI IDE BMDMA transaction
3729 * @qc: Info associated with this ATA transaction.
3731 * Writes the ATA_DMA_START flag to the DMA command register.
3733 * May be used as the bmdma_start() entry in ata_port_operations.
3736 * spin_lock_irqsave(host_set lock)
3738 void ata_bmdma_start(struct ata_queued_cmd *qc)
3740 if (qc->ap->flags & ATA_FLAG_MMIO)
3741 ata_bmdma_start_mmio(qc);
3743 ata_bmdma_start_pio(qc);
3748 * ata_bmdma_setup - Set up PCI IDE BMDMA transaction
3749 * @qc: Info associated with this ATA transaction.
3751 * Writes address of PRD table to device's PRD Table Address
3752 * register, sets the DMA control register, and calls
3753 * ops->exec_command() to start the transfer.
3755 * May be used as the bmdma_setup() entry in ata_port_operations.
3758 * spin_lock_irqsave(host_set lock)
3760 void ata_bmdma_setup(struct ata_queued_cmd *qc)
3762 if (qc->ap->flags & ATA_FLAG_MMIO)
3763 ata_bmdma_setup_mmio(qc);
3765 ata_bmdma_setup_pio(qc);
3770 * ata_bmdma_irq_clear - Clear PCI IDE BMDMA interrupt.
3771 * @ap: Port associated with this ATA transaction.
3773 * Clear interrupt and error flags in DMA status register.
3775 * May be used as the irq_clear() entry in ata_port_operations.
3778 * spin_lock_irqsave(host_set lock)
3781 void ata_bmdma_irq_clear(struct ata_port *ap)
3783 if (ap->flags & ATA_FLAG_MMIO) {
3784 void __iomem *mmio = ((void __iomem *) ap->ioaddr.bmdma_addr) + ATA_DMA_STATUS;
3785 writeb(readb(mmio), mmio);
3787 unsigned long addr = ap->ioaddr.bmdma_addr + ATA_DMA_STATUS;
3788 outb(inb(addr), addr);
3795 * ata_bmdma_status - Read PCI IDE BMDMA status
3796 * @ap: Port associated with this ATA transaction.
3798 * Read and return BMDMA status register.
3800 * May be used as the bmdma_status() entry in ata_port_operations.
3803 * spin_lock_irqsave(host_set lock)
3806 u8 ata_bmdma_status(struct ata_port *ap)
3809 if (ap->flags & ATA_FLAG_MMIO) {
3810 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
3811 host_stat = readb(mmio + ATA_DMA_STATUS);
3813 host_stat = inb(ap->ioaddr.bmdma_addr + ATA_DMA_STATUS);
3819 * ata_bmdma_stop - Stop PCI IDE BMDMA transfer
3820 * @qc: Command we are ending DMA for
3822 * Clears the ATA_DMA_START flag in the dma control register
3824 * May be used as the bmdma_stop() entry in ata_port_operations.
3827 * spin_lock_irqsave(host_set lock)
3830 void ata_bmdma_stop(struct ata_queued_cmd *qc)
3832 struct ata_port *ap = qc->ap;
3833 if (ap->flags & ATA_FLAG_MMIO) {
3834 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
3836 /* clear start/stop bit */
3837 writeb(readb(mmio + ATA_DMA_CMD) & ~ATA_DMA_START,
3838 mmio + ATA_DMA_CMD);
3840 /* clear start/stop bit */
3841 outb(inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD) & ~ATA_DMA_START,
3842 ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3845 /* one-PIO-cycle guaranteed wait, per spec, for HDMA1:0 transition */
3846 ata_altstatus(ap); /* dummy read */
3850 * ata_host_intr - Handle host interrupt for given (port, task)
3851 * @ap: Port on which interrupt arrived (possibly...)
3852 * @qc: Taskfile currently active in engine
3854 * Handle host interrupt for given queued command. Currently,
3855 * only DMA interrupts are handled. All other commands are
3856 * handled via polling with interrupts disabled (nIEN bit).
3859 * spin_lock_irqsave(host_set lock)
3862 * One if interrupt was handled, zero if not (shared irq).
3865 inline unsigned int ata_host_intr (struct ata_port *ap,
3866 struct ata_queued_cmd *qc)
3868 u8 status, host_stat = 0;
3870 VPRINTK("ata%u: protocol %d task_state %d\n",
3871 ap->id, qc->tf.protocol, ap->hsm_task_state);
3873 /* Check whether we are expecting interrupt in this state */
3874 switch (ap->hsm_task_state) {
3876 /* Check the ATA_DFLAG_CDB_INTR flag is enough here.
3877 * The flag was turned on only for atapi devices.
3878 * No need to check is_atapi_taskfile(&qc->tf) again.
3880 if (!(qc->dev->flags & ATA_DFLAG_CDB_INTR))
3884 if (qc->tf.protocol == ATA_PROT_DMA ||
3885 qc->tf.protocol == ATA_PROT_ATAPI_DMA) {
3886 /* check status of DMA engine */
3887 host_stat = ap->ops->bmdma_status(ap);
3888 VPRINTK("ata%u: host_stat 0x%X\n", ap->id, host_stat);
3890 /* if it's not our irq... */
3891 if (!(host_stat & ATA_DMA_INTR))
3894 /* before we do anything else, clear DMA-Start bit */
3895 ap->ops->bmdma_stop(qc);
3904 /* check altstatus */
3905 status = ata_altstatus(ap);
3906 if (status & ATA_BUSY)
3909 /* check main status, clearing INTRQ */
3910 status = ata_chk_status(ap);
3911 if (unlikely(status & ATA_BUSY))
3914 DPRINTK("ata%u: protocol %d task_state %d (dev_stat 0x%X)\n",
3915 ap->id, qc->tf.protocol, ap->hsm_task_state, status);
3917 /* ack bmdma irq events */
3918 ap->ops->irq_clear(ap);
3921 if (unlikely((status & ATA_ERR) || (host_stat & ATA_DMA_ERR)))
3922 ap->hsm_task_state = HSM_ST_ERR;
3925 switch (ap->hsm_task_state) {
3927 /* Some pre-ATAPI-4 devices assert INTRQ
3928 * at this state when ready to receive CDB.
3931 /* check device status */
3932 if (unlikely((status & (ATA_BUSY | ATA_DRQ)) != ATA_DRQ)) {
3933 /* Wrong status. Let EH handle this */
3934 ap->hsm_task_state = HSM_ST_ERR;
3938 atapi_send_cdb(ap, qc);
3943 /* complete command or read/write the data register */
3944 if (qc->tf.protocol == ATA_PROT_ATAPI) {
3945 /* ATAPI PIO protocol */
3946 if ((status & ATA_DRQ) == 0) {
3947 /* no more data to transfer */
3948 ap->hsm_task_state = HSM_ST_LAST;
3952 atapi_pio_bytes(qc);
3954 if (unlikely(ap->hsm_task_state == HSM_ST_ERR))
3955 /* bad ireason reported by device */
3959 /* ATA PIO protocol */
3960 if (unlikely((status & ATA_DRQ) == 0)) {
3961 /* handle BSY=0, DRQ=0 as error */
3962 ap->hsm_task_state = HSM_ST_ERR;
3968 if (ap->hsm_task_state == HSM_ST_LAST &&
3969 (!(qc->tf.flags & ATA_TFLAG_WRITE))) {
3972 status = ata_chk_status(ap);
3977 ata_altstatus(ap); /* flush */
3981 if (unlikely(status & ATA_DRQ)) {
3982 /* handle DRQ=1 as error */
3983 ap->hsm_task_state = HSM_ST_ERR;
3987 /* no more data to transfer */
3988 DPRINTK("ata%u: command complete, drv_stat 0x%x\n",
3991 ap->hsm_task_state = HSM_ST_IDLE;
3993 /* complete taskfile transaction */
3994 ata_qc_complete(qc, status);
3998 printk(KERN_ERR "ata%u: command error, drv_stat 0x%x host_stat 0x%x\n",
3999 ap->id, status, host_stat);
4001 ap->hsm_task_state = HSM_ST_IDLE;
4002 ata_qc_complete(qc, status | ATA_ERR);
4008 return 1; /* irq handled */
4011 ap->stats.idle_irq++;
4014 if ((ap->stats.idle_irq % 1000) == 0) {
4016 ata_irq_ack(ap, 0); /* debug trap */
4017 printk(KERN_WARNING "ata%d: irq trap\n", ap->id);
4020 return 0; /* irq not handled */
4024 * ata_interrupt - Default ATA host interrupt handler
4025 * @irq: irq line (unused)
4026 * @dev_instance: pointer to our ata_host_set information structure
4029 * Default interrupt handler for PCI IDE devices. Calls
4030 * ata_host_intr() for each port that is not disabled.
4033 * Obtains host_set lock during operation.
4036 * IRQ_NONE or IRQ_HANDLED.
4040 irqreturn_t ata_interrupt (int irq, void *dev_instance, struct pt_regs *regs)
4042 struct ata_host_set *host_set = dev_instance;
4044 unsigned int handled = 0;
4045 unsigned long flags;
4047 /* TODO: make _irqsave conditional on x86 PCI IDE legacy mode */
4048 spin_lock_irqsave(&host_set->lock, flags);
4050 for (i = 0; i < host_set->n_ports; i++) {
4051 struct ata_port *ap;
4053 ap = host_set->ports[i];
4055 !(ap->flags & ATA_FLAG_PORT_DISABLED)) {
4056 struct ata_queued_cmd *qc;
4058 qc = ata_qc_from_tag(ap, ap->active_tag);
4059 if (qc && (!(qc->tf.flags & ATA_TFLAG_POLLING)) &&
4060 (qc->flags & ATA_QCFLAG_ACTIVE))
4061 handled |= ata_host_intr(ap, qc);
4065 spin_unlock_irqrestore(&host_set->lock, flags);
4067 return IRQ_RETVAL(handled);
4071 * ata_port_start - Set port up for dma.
4072 * @ap: Port to initialize
4074 * Called just after data structures for each port are
4075 * initialized. Allocates space for PRD table.
4077 * May be used as the port_start() entry in ata_port_operations.
4082 int ata_port_start (struct ata_port *ap)
4084 struct device *dev = ap->host_set->dev;
4086 ap->prd = dma_alloc_coherent(dev, ATA_PRD_TBL_SZ, &ap->prd_dma, GFP_KERNEL);
4090 DPRINTK("prd alloc, virt %p, dma %llx\n", ap->prd, (unsigned long long) ap->prd_dma);
4097 * ata_port_stop - Undo ata_port_start()
4098 * @ap: Port to shut down
4100 * Frees the PRD table.
4102 * May be used as the port_stop() entry in ata_port_operations.
4107 void ata_port_stop (struct ata_port *ap)
4109 struct device *dev = ap->host_set->dev;
4111 dma_free_coherent(dev, ATA_PRD_TBL_SZ, ap->prd, ap->prd_dma);
4114 void ata_host_stop (struct ata_host_set *host_set)
4116 if (host_set->mmio_base)
4117 iounmap(host_set->mmio_base);
4122 * ata_host_remove - Unregister SCSI host structure with upper layers
4123 * @ap: Port to unregister
4124 * @do_unregister: 1 if we fully unregister, 0 to just stop the port
4129 static void ata_host_remove(struct ata_port *ap, unsigned int do_unregister)
4131 struct Scsi_Host *sh = ap->host;
4136 scsi_remove_host(sh);
4138 ap->ops->port_stop(ap);
4142 * ata_host_init - Initialize an ata_port structure
4143 * @ap: Structure to initialize
4144 * @host: associated SCSI mid-layer structure
4145 * @host_set: Collection of hosts to which @ap belongs
4146 * @ent: Probe information provided by low-level driver
4147 * @port_no: Port number associated with this ata_port
4149 * Initialize a new ata_port structure, and its associated
4153 * Inherited from caller.
4157 static void ata_host_init(struct ata_port *ap, struct Scsi_Host *host,
4158 struct ata_host_set *host_set,
4159 struct ata_probe_ent *ent, unsigned int port_no)
4165 host->max_channel = 1;
4166 host->unique_id = ata_unique_id++;
4167 host->max_cmd_len = 12;
4169 scsi_assign_lock(host, &host_set->lock);
4171 ap->flags = ATA_FLAG_PORT_DISABLED;
4172 ap->id = host->unique_id;
4174 ap->ctl = ATA_DEVCTL_OBS;
4175 ap->host_set = host_set;
4176 ap->port_no = port_no;
4178 ent->legacy_mode ? ent->hard_port_no : port_no;
4179 ap->pio_mask = ent->pio_mask;
4180 ap->mwdma_mask = ent->mwdma_mask;
4181 ap->udma_mask = ent->udma_mask;
4182 ap->flags |= ent->host_flags;
4183 ap->ops = ent->port_ops;
4184 ap->cbl = ATA_CBL_NONE;
4185 ap->active_tag = ATA_TAG_POISON;
4186 ap->last_ctl = 0xFF;
4188 INIT_WORK(&ap->dataout_task, ata_dataout_task, ap);
4189 INIT_WORK(&ap->pio_task, ata_pio_task, ap);
4191 for (i = 0; i < ATA_MAX_DEVICES; i++)
4192 ap->device[i].devno = i;
4195 ap->stats.unhandled_irq = 1;
4196 ap->stats.idle_irq = 1;
4199 memcpy(&ap->ioaddr, &ent->port[port_no], sizeof(struct ata_ioports));
4203 * ata_host_add - Attach low-level ATA driver to system
4204 * @ent: Information provided by low-level driver
4205 * @host_set: Collections of ports to which we add
4206 * @port_no: Port number associated with this host
4208 * Attach low-level ATA driver to system.
4211 * PCI/etc. bus probe sem.
4214 * New ata_port on success, for NULL on error.
4218 static struct ata_port * ata_host_add(struct ata_probe_ent *ent,
4219 struct ata_host_set *host_set,
4220 unsigned int port_no)
4222 struct Scsi_Host *host;
4223 struct ata_port *ap;
4227 host = scsi_host_alloc(ent->sht, sizeof(struct ata_port));
4231 ap = (struct ata_port *) &host->hostdata[0];
4233 ata_host_init(ap, host, host_set, ent, port_no);
4235 rc = ap->ops->port_start(ap);
4242 scsi_host_put(host);
4247 * ata_device_add - Register hardware device with ATA and SCSI layers
4248 * @ent: Probe information describing hardware device to be registered
4250 * This function processes the information provided in the probe
4251 * information struct @ent, allocates the necessary ATA and SCSI
4252 * host information structures, initializes them, and registers
4253 * everything with requisite kernel subsystems.
4255 * This function requests irqs, probes the ATA bus, and probes
4259 * PCI/etc. bus probe sem.
4262 * Number of ports registered. Zero on error (no ports registered).
4266 int ata_device_add(struct ata_probe_ent *ent)
4268 unsigned int count = 0, i;
4269 struct device *dev = ent->dev;
4270 struct ata_host_set *host_set;
4273 /* alloc a container for our list of ATA ports (buses) */
4274 host_set = kmalloc(sizeof(struct ata_host_set) +
4275 (ent->n_ports * sizeof(void *)), GFP_KERNEL);
4278 memset(host_set, 0, sizeof(struct ata_host_set) + (ent->n_ports * sizeof(void *)));
4279 spin_lock_init(&host_set->lock);
4281 host_set->dev = dev;
4282 host_set->n_ports = ent->n_ports;
4283 host_set->irq = ent->irq;
4284 host_set->mmio_base = ent->mmio_base;
4285 host_set->private_data = ent->private_data;
4286 host_set->ops = ent->port_ops;
4288 /* register each port bound to this device */
4289 for (i = 0; i < ent->n_ports; i++) {
4290 struct ata_port *ap;
4291 unsigned long xfer_mode_mask;
4293 ap = ata_host_add(ent, host_set, i);
4297 host_set->ports[i] = ap;
4298 xfer_mode_mask =(ap->udma_mask << ATA_SHIFT_UDMA) |
4299 (ap->mwdma_mask << ATA_SHIFT_MWDMA) |
4300 (ap->pio_mask << ATA_SHIFT_PIO);
4302 /* print per-port info to dmesg */
4303 printk(KERN_INFO "ata%u: %cATA max %s cmd 0x%lX ctl 0x%lX "
4304 "bmdma 0x%lX irq %lu\n",
4306 ap->flags & ATA_FLAG_SATA ? 'S' : 'P',
4307 ata_mode_string(xfer_mode_mask),
4308 ap->ioaddr.cmd_addr,
4309 ap->ioaddr.ctl_addr,
4310 ap->ioaddr.bmdma_addr,
4314 host_set->ops->irq_clear(ap);
4323 /* obtain irq, that is shared between channels */
4324 if (request_irq(ent->irq, ent->port_ops->irq_handler, ent->irq_flags,
4325 DRV_NAME, host_set))
4328 /* perform each probe synchronously */
4329 DPRINTK("probe begin\n");
4330 for (i = 0; i < count; i++) {
4331 struct ata_port *ap;
4334 ap = host_set->ports[i];
4336 DPRINTK("ata%u: probe begin\n", ap->id);
4337 rc = ata_bus_probe(ap);
4338 DPRINTK("ata%u: probe end\n", ap->id);
4341 /* FIXME: do something useful here?
4342 * Current libata behavior will
4343 * tear down everything when
4344 * the module is removed
4345 * or the h/w is unplugged.
4349 rc = scsi_add_host(ap->host, dev);
4351 printk(KERN_ERR "ata%u: scsi_add_host failed\n",
4353 /* FIXME: do something useful here */
4354 /* FIXME: handle unconditional calls to
4355 * scsi_scan_host and ata_host_remove, below,
4361 /* probes are done, now scan each port's disk(s) */
4362 DPRINTK("probe begin\n");
4363 for (i = 0; i < count; i++) {
4364 struct ata_port *ap = host_set->ports[i];
4366 ata_scsi_scan_host(ap);
4369 dev_set_drvdata(dev, host_set);
4371 VPRINTK("EXIT, returning %u\n", ent->n_ports);
4372 return ent->n_ports; /* success */
4375 for (i = 0; i < count; i++) {
4376 ata_host_remove(host_set->ports[i], 1);
4377 scsi_host_put(host_set->ports[i]->host);
4380 VPRINTK("EXIT, returning 0\n");
4385 * ata_host_set_remove - PCI layer callback for device removal
4386 * @host_set: ATA host set that was removed
4388 * Unregister all objects associated with this host set. Free those
4392 * Inherited from calling layer (may sleep).
4396 void ata_host_set_remove(struct ata_host_set *host_set)
4398 struct ata_port *ap;
4401 for (i = 0; i < host_set->n_ports; i++) {
4402 ap = host_set->ports[i];
4403 scsi_remove_host(ap->host);
4406 free_irq(host_set->irq, host_set);
4408 for (i = 0; i < host_set->n_ports; i++) {
4409 ap = host_set->ports[i];
4411 ata_scsi_release(ap->host);
4413 if ((ap->flags & ATA_FLAG_NO_LEGACY) == 0) {
4414 struct ata_ioports *ioaddr = &ap->ioaddr;
4416 if (ioaddr->cmd_addr == 0x1f0)
4417 release_region(0x1f0, 8);
4418 else if (ioaddr->cmd_addr == 0x170)
4419 release_region(0x170, 8);
4422 scsi_host_put(ap->host);
4425 if (host_set->ops->host_stop)
4426 host_set->ops->host_stop(host_set);
4432 * ata_scsi_release - SCSI layer callback hook for host unload
4433 * @host: libata host to be unloaded
4435 * Performs all duties necessary to shut down a libata port...
4436 * Kill port kthread, disable port, and release resources.
4439 * Inherited from SCSI layer.
4445 int ata_scsi_release(struct Scsi_Host *host)
4447 struct ata_port *ap = (struct ata_port *) &host->hostdata[0];
4451 ap->ops->port_disable(ap);
4452 ata_host_remove(ap, 0);
4459 * ata_std_ports - initialize ioaddr with standard port offsets.
4460 * @ioaddr: IO address structure to be initialized
4462 * Utility function which initializes data_addr, error_addr,
4463 * feature_addr, nsect_addr, lbal_addr, lbam_addr, lbah_addr,
4464 * device_addr, status_addr, and command_addr to standard offsets
4465 * relative to cmd_addr.
4467 * Does not set ctl_addr, altstatus_addr, bmdma_addr, or scr_addr.
4470 void ata_std_ports(struct ata_ioports *ioaddr)
4472 ioaddr->data_addr = ioaddr->cmd_addr + ATA_REG_DATA;
4473 ioaddr->error_addr = ioaddr->cmd_addr + ATA_REG_ERR;
4474 ioaddr->feature_addr = ioaddr->cmd_addr + ATA_REG_FEATURE;
4475 ioaddr->nsect_addr = ioaddr->cmd_addr + ATA_REG_NSECT;
4476 ioaddr->lbal_addr = ioaddr->cmd_addr + ATA_REG_LBAL;
4477 ioaddr->lbam_addr = ioaddr->cmd_addr + ATA_REG_LBAM;
4478 ioaddr->lbah_addr = ioaddr->cmd_addr + ATA_REG_LBAH;
4479 ioaddr->device_addr = ioaddr->cmd_addr + ATA_REG_DEVICE;
4480 ioaddr->status_addr = ioaddr->cmd_addr + ATA_REG_STATUS;
4481 ioaddr->command_addr = ioaddr->cmd_addr + ATA_REG_CMD;
4484 static struct ata_probe_ent *
4485 ata_probe_ent_alloc(struct device *dev, struct ata_port_info *port)
4487 struct ata_probe_ent *probe_ent;
4489 probe_ent = kmalloc(sizeof(*probe_ent), GFP_KERNEL);
4491 printk(KERN_ERR DRV_NAME "(%s): out of memory\n",
4492 kobject_name(&(dev->kobj)));
4496 memset(probe_ent, 0, sizeof(*probe_ent));
4498 INIT_LIST_HEAD(&probe_ent->node);
4499 probe_ent->dev = dev;
4501 probe_ent->sht = port->sht;
4502 probe_ent->host_flags = port->host_flags;
4503 probe_ent->pio_mask = port->pio_mask;
4504 probe_ent->mwdma_mask = port->mwdma_mask;
4505 probe_ent->udma_mask = port->udma_mask;
4506 probe_ent->port_ops = port->port_ops;
4515 void ata_pci_host_stop (struct ata_host_set *host_set)
4517 struct pci_dev *pdev = to_pci_dev(host_set->dev);
4519 pci_iounmap(pdev, host_set->mmio_base);
4523 * ata_pci_init_native_mode - Initialize native-mode driver
4524 * @pdev: pci device to be initialized
4525 * @port: array[2] of pointers to port info structures.
4526 * @ports: bitmap of ports present
4528 * Utility function which allocates and initializes an
4529 * ata_probe_ent structure for a standard dual-port
4530 * PIO-based IDE controller. The returned ata_probe_ent
4531 * structure can be passed to ata_device_add(). The returned
4532 * ata_probe_ent structure should then be freed with kfree().
4534 * The caller need only pass the address of the primary port, the
4535 * secondary will be deduced automatically. If the device has non
4536 * standard secondary port mappings this function can be called twice,
4537 * once for each interface.
4540 struct ata_probe_ent *
4541 ata_pci_init_native_mode(struct pci_dev *pdev, struct ata_port_info **port, int ports)
4543 struct ata_probe_ent *probe_ent =
4544 ata_probe_ent_alloc(pci_dev_to_dev(pdev), port[0]);
4550 probe_ent->irq = pdev->irq;
4551 probe_ent->irq_flags = SA_SHIRQ;
4553 if (ports & ATA_PORT_PRIMARY) {
4554 probe_ent->port[p].cmd_addr = pci_resource_start(pdev, 0);
4555 probe_ent->port[p].altstatus_addr =
4556 probe_ent->port[p].ctl_addr =
4557 pci_resource_start(pdev, 1) | ATA_PCI_CTL_OFS;
4558 probe_ent->port[p].bmdma_addr = pci_resource_start(pdev, 4);
4559 ata_std_ports(&probe_ent->port[p]);
4563 if (ports & ATA_PORT_SECONDARY) {
4564 probe_ent->port[p].cmd_addr = pci_resource_start(pdev, 2);
4565 probe_ent->port[p].altstatus_addr =
4566 probe_ent->port[p].ctl_addr =
4567 pci_resource_start(pdev, 3) | ATA_PCI_CTL_OFS;
4568 probe_ent->port[p].bmdma_addr = pci_resource_start(pdev, 4) + 8;
4569 ata_std_ports(&probe_ent->port[p]);
4573 probe_ent->n_ports = p;
4577 static struct ata_probe_ent *ata_pci_init_legacy_port(struct pci_dev *pdev, struct ata_port_info **port, int port_num)
4579 struct ata_probe_ent *probe_ent;
4581 probe_ent = ata_probe_ent_alloc(pci_dev_to_dev(pdev), port[0]);
4586 probe_ent->legacy_mode = 1;
4587 probe_ent->n_ports = 1;
4588 probe_ent->hard_port_no = port_num;
4593 probe_ent->irq = 14;
4594 probe_ent->port[0].cmd_addr = 0x1f0;
4595 probe_ent->port[0].altstatus_addr =
4596 probe_ent->port[0].ctl_addr = 0x3f6;
4599 probe_ent->irq = 15;
4600 probe_ent->port[0].cmd_addr = 0x170;
4601 probe_ent->port[0].altstatus_addr =
4602 probe_ent->port[0].ctl_addr = 0x376;
4605 probe_ent->port[0].bmdma_addr = pci_resource_start(pdev, 4) + 8 * port_num;
4606 ata_std_ports(&probe_ent->port[0]);
4611 * ata_pci_init_one - Initialize/register PCI IDE host controller
4612 * @pdev: Controller to be initialized
4613 * @port_info: Information from low-level host driver
4614 * @n_ports: Number of ports attached to host controller
4616 * This is a helper function which can be called from a driver's
4617 * xxx_init_one() probe function if the hardware uses traditional
4618 * IDE taskfile registers.
4620 * This function calls pci_enable_device(), reserves its register
4621 * regions, sets the dma mask, enables bus master mode, and calls
4625 * Inherited from PCI layer (may sleep).
4628 * Zero on success, negative on errno-based value on error.
4632 int ata_pci_init_one (struct pci_dev *pdev, struct ata_port_info **port_info,
4633 unsigned int n_ports)
4635 struct ata_probe_ent *probe_ent = NULL, *probe_ent2 = NULL;
4636 struct ata_port_info *port[2];
4638 unsigned int legacy_mode = 0;
4639 int disable_dev_on_err = 1;
4644 port[0] = port_info[0];
4646 port[1] = port_info[1];
4650 if ((port[0]->host_flags & ATA_FLAG_NO_LEGACY) == 0
4651 && (pdev->class >> 8) == PCI_CLASS_STORAGE_IDE) {
4652 /* TODO: What if one channel is in native mode ... */
4653 pci_read_config_byte(pdev, PCI_CLASS_PROG, &tmp8);
4654 mask = (1 << 2) | (1 << 0);
4655 if ((tmp8 & mask) != mask)
4656 legacy_mode = (1 << 3);
4660 if ((!legacy_mode) && (n_ports > 2)) {
4661 printk(KERN_ERR "ata: BUG: native mode, n_ports > 2\n");
4666 /* FIXME: Really for ATA it isn't safe because the device may be
4667 multi-purpose and we want to leave it alone if it was already
4668 enabled. Secondly for shared use as Arjan says we want refcounting
4670 Checking dev->is_enabled is insufficient as this is not set at
4671 boot for the primary video which is BIOS enabled
4674 rc = pci_enable_device(pdev);
4678 rc = pci_request_regions(pdev, DRV_NAME);
4680 disable_dev_on_err = 0;
4684 /* FIXME: Should use platform specific mappers for legacy port ranges */
4686 if (!request_region(0x1f0, 8, "libata")) {
4687 struct resource *conflict, res;
4689 res.end = 0x1f0 + 8 - 1;
4690 conflict = ____request_resource(&ioport_resource, &res);
4691 if (!strcmp(conflict->name, "libata"))
4692 legacy_mode |= (1 << 0);
4694 disable_dev_on_err = 0;
4695 printk(KERN_WARNING "ata: 0x1f0 IDE port busy\n");
4698 legacy_mode |= (1 << 0);
4700 if (!request_region(0x170, 8, "libata")) {
4701 struct resource *conflict, res;
4703 res.end = 0x170 + 8 - 1;
4704 conflict = ____request_resource(&ioport_resource, &res);
4705 if (!strcmp(conflict->name, "libata"))
4706 legacy_mode |= (1 << 1);
4708 disable_dev_on_err = 0;
4709 printk(KERN_WARNING "ata: 0x170 IDE port busy\n");
4712 legacy_mode |= (1 << 1);
4715 /* we have legacy mode, but all ports are unavailable */
4716 if (legacy_mode == (1 << 3)) {
4718 goto err_out_regions;
4721 rc = pci_set_dma_mask(pdev, ATA_DMA_MASK);
4723 goto err_out_regions;
4724 rc = pci_set_consistent_dma_mask(pdev, ATA_DMA_MASK);
4726 goto err_out_regions;
4729 if (legacy_mode & (1 << 0))
4730 probe_ent = ata_pci_init_legacy_port(pdev, port, 0);
4731 if (legacy_mode & (1 << 1))
4732 probe_ent2 = ata_pci_init_legacy_port(pdev, port, 1);
4735 probe_ent = ata_pci_init_native_mode(pdev, port, ATA_PORT_PRIMARY | ATA_PORT_SECONDARY);
4737 probe_ent = ata_pci_init_native_mode(pdev, port, ATA_PORT_PRIMARY);
4739 if (!probe_ent && !probe_ent2) {
4741 goto err_out_regions;
4744 pci_set_master(pdev);
4746 /* FIXME: check ata_device_add return */
4748 if (legacy_mode & (1 << 0))
4749 ata_device_add(probe_ent);
4750 if (legacy_mode & (1 << 1))
4751 ata_device_add(probe_ent2);
4753 ata_device_add(probe_ent);
4761 if (legacy_mode & (1 << 0))
4762 release_region(0x1f0, 8);
4763 if (legacy_mode & (1 << 1))
4764 release_region(0x170, 8);
4765 pci_release_regions(pdev);
4767 if (disable_dev_on_err)
4768 pci_disable_device(pdev);
4773 * ata_pci_remove_one - PCI layer callback for device removal
4774 * @pdev: PCI device that was removed
4776 * PCI layer indicates to libata via this hook that
4777 * hot-unplug or module unload event has occured.
4778 * Handle this by unregistering all objects associated
4779 * with this PCI device. Free those objects. Then finally
4780 * release PCI resources and disable device.
4783 * Inherited from PCI layer (may sleep).
4786 void ata_pci_remove_one (struct pci_dev *pdev)
4788 struct device *dev = pci_dev_to_dev(pdev);
4789 struct ata_host_set *host_set = dev_get_drvdata(dev);
4791 ata_host_set_remove(host_set);
4792 pci_release_regions(pdev);
4793 pci_disable_device(pdev);
4794 dev_set_drvdata(dev, NULL);
4797 /* move to PCI subsystem */
4798 int pci_test_config_bits(struct pci_dev *pdev, struct pci_bits *bits)
4800 unsigned long tmp = 0;
4802 switch (bits->width) {
4805 pci_read_config_byte(pdev, bits->reg, &tmp8);
4811 pci_read_config_word(pdev, bits->reg, &tmp16);
4817 pci_read_config_dword(pdev, bits->reg, &tmp32);
4828 return (tmp == bits->val) ? 1 : 0;
4830 #endif /* CONFIG_PCI */
4833 static int __init ata_init(void)
4835 ata_wq = create_workqueue("ata");
4839 printk(KERN_DEBUG "libata version " DRV_VERSION " loaded.\n");
4843 static void __exit ata_exit(void)
4845 destroy_workqueue(ata_wq);
4848 module_init(ata_init);
4849 module_exit(ata_exit);
4851 static unsigned long ratelimit_time;
4852 static spinlock_t ata_ratelimit_lock = SPIN_LOCK_UNLOCKED;
4854 int ata_ratelimit(void)
4857 unsigned long flags;
4859 spin_lock_irqsave(&ata_ratelimit_lock, flags);
4861 if (time_after(jiffies, ratelimit_time)) {
4863 ratelimit_time = jiffies + (HZ/5);
4867 spin_unlock_irqrestore(&ata_ratelimit_lock, flags);
4873 * libata is essentially a library of internal helper functions for
4874 * low-level ATA host controller drivers. As such, the API/ABI is
4875 * likely to change as new drivers are added and updated.
4876 * Do not depend on ABI/API stability.
4879 EXPORT_SYMBOL_GPL(ata_std_bios_param);
4880 EXPORT_SYMBOL_GPL(ata_std_ports);
4881 EXPORT_SYMBOL_GPL(ata_device_add);
4882 EXPORT_SYMBOL_GPL(ata_host_set_remove);
4883 EXPORT_SYMBOL_GPL(ata_sg_init);
4884 EXPORT_SYMBOL_GPL(ata_sg_init_one);
4885 EXPORT_SYMBOL_GPL(ata_qc_complete);
4886 EXPORT_SYMBOL_GPL(ata_qc_issue_prot);
4887 EXPORT_SYMBOL_GPL(ata_eng_timeout);
4888 EXPORT_SYMBOL_GPL(ata_tf_load);
4889 EXPORT_SYMBOL_GPL(ata_tf_read);
4890 EXPORT_SYMBOL_GPL(ata_noop_dev_select);
4891 EXPORT_SYMBOL_GPL(ata_std_dev_select);
4892 EXPORT_SYMBOL_GPL(ata_tf_to_fis);
4893 EXPORT_SYMBOL_GPL(ata_tf_from_fis);
4894 EXPORT_SYMBOL_GPL(ata_check_status);
4895 EXPORT_SYMBOL_GPL(ata_altstatus);
4896 EXPORT_SYMBOL_GPL(ata_chk_err);
4897 EXPORT_SYMBOL_GPL(ata_exec_command);
4898 EXPORT_SYMBOL_GPL(ata_port_start);
4899 EXPORT_SYMBOL_GPL(ata_port_stop);
4900 EXPORT_SYMBOL_GPL(ata_host_stop);
4901 EXPORT_SYMBOL_GPL(ata_interrupt);
4902 EXPORT_SYMBOL_GPL(ata_qc_prep);
4903 EXPORT_SYMBOL_GPL(ata_bmdma_setup);
4904 EXPORT_SYMBOL_GPL(ata_bmdma_start);
4905 EXPORT_SYMBOL_GPL(ata_bmdma_irq_clear);
4906 EXPORT_SYMBOL_GPL(ata_bmdma_status);
4907 EXPORT_SYMBOL_GPL(ata_bmdma_stop);
4908 EXPORT_SYMBOL_GPL(ata_port_probe);
4909 EXPORT_SYMBOL_GPL(sata_phy_reset);
4910 EXPORT_SYMBOL_GPL(__sata_phy_reset);
4911 EXPORT_SYMBOL_GPL(ata_bus_reset);
4912 EXPORT_SYMBOL_GPL(ata_port_disable);
4913 EXPORT_SYMBOL_GPL(ata_ratelimit);
4914 EXPORT_SYMBOL_GPL(ata_scsi_ioctl);
4915 EXPORT_SYMBOL_GPL(ata_scsi_queuecmd);
4916 EXPORT_SYMBOL_GPL(ata_scsi_error);
4917 EXPORT_SYMBOL_GPL(ata_scsi_slave_config);
4918 EXPORT_SYMBOL_GPL(ata_scsi_release);
4919 EXPORT_SYMBOL_GPL(ata_host_intr);
4920 EXPORT_SYMBOL_GPL(ata_dev_classify);
4921 EXPORT_SYMBOL_GPL(ata_dev_id_string);
4922 EXPORT_SYMBOL_GPL(ata_dev_config);
4923 EXPORT_SYMBOL_GPL(ata_scsi_simulate);
4926 EXPORT_SYMBOL_GPL(pci_test_config_bits);
4927 EXPORT_SYMBOL_GPL(ata_pci_host_stop);
4928 EXPORT_SYMBOL_GPL(ata_pci_init_native_mode);
4929 EXPORT_SYMBOL_GPL(ata_pci_init_one);
4930 EXPORT_SYMBOL_GPL(ata_pci_remove_one);
4931 #endif /* CONFIG_PCI */