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 <scsi/scsi.h>
53 #include "scsi_priv.h"
54 #include <scsi/scsi_host.h>
55 #include <linux/libata.h>
57 #include <asm/semaphore.h>
58 #include <asm/byteorder.h>
62 static unsigned int ata_busy_sleep (struct ata_port *ap,
63 unsigned long tmout_pat,
65 static void ata_dev_init_params(struct ata_port *ap, struct ata_device *dev);
66 static void ata_set_mode(struct ata_port *ap);
67 static void ata_dev_set_xfermode(struct ata_port *ap, struct ata_device *dev);
68 static unsigned int ata_get_mode_mask(struct ata_port *ap, int shift);
69 static int fgb(u32 bitmap);
70 static int ata_choose_xfer_mode(struct ata_port *ap,
72 unsigned int *xfer_shift_out);
73 static int ata_qc_complete_noop(struct ata_queued_cmd *qc, u8 drv_stat);
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 /* print device info to dmesg */
1334 printk(KERN_INFO "ata%u: dev %u ATAPI, max %s\n",
1336 ata_mode_string(xfer_modes));
1339 DPRINTK("EXIT, drv_stat = 0x%x\n", ata_chk_status(ap));
1343 printk(KERN_WARNING "ata%u: dev %u not supported, ignoring\n",
1346 dev->class++; /* converts ATA_DEV_xxx into ATA_DEV_xxx_UNSUP */
1347 DPRINTK("EXIT, err\n");
1351 static inline u8 ata_dev_knobble(struct ata_port *ap)
1353 return ((ap->cbl == ATA_CBL_SATA) && (!ata_id_is_sata(ap->device->id)));
1357 * ata_dev_config - Run device specific handlers and check for
1358 * SATA->PATA bridges
1365 void ata_dev_config(struct ata_port *ap, unsigned int i)
1367 /* limit bridge transfers to udma5, 200 sectors */
1368 if (ata_dev_knobble(ap)) {
1369 printk(KERN_INFO "ata%u(%u): applying bridge limits\n",
1370 ap->id, ap->device->devno);
1371 ap->udma_mask &= ATA_UDMA5;
1372 ap->host->max_sectors = ATA_MAX_SECTORS;
1373 ap->host->hostt->max_sectors = ATA_MAX_SECTORS;
1374 ap->device->flags |= ATA_DFLAG_LOCK_SECTORS;
1377 if (ap->ops->dev_config)
1378 ap->ops->dev_config(ap, &ap->device[i]);
1382 * ata_bus_probe - Reset and probe ATA bus
1385 * Master ATA bus probing function. Initiates a hardware-dependent
1386 * bus reset, then attempts to identify any devices found on
1390 * PCI/etc. bus probe sem.
1393 * Zero on success, non-zero on error.
1396 static int ata_bus_probe(struct ata_port *ap)
1398 unsigned int i, found = 0;
1400 ap->ops->phy_reset(ap);
1401 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1404 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1405 ata_dev_identify(ap, i);
1406 if (ata_dev_present(&ap->device[i])) {
1408 ata_dev_config(ap,i);
1412 if ((!found) || (ap->flags & ATA_FLAG_PORT_DISABLED))
1413 goto err_out_disable;
1416 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1417 goto err_out_disable;
1422 ap->ops->port_disable(ap);
1428 * ata_port_probe - Mark port as enabled
1429 * @ap: Port for which we indicate enablement
1431 * Modify @ap data structure such that the system
1432 * thinks that the entire port is enabled.
1434 * LOCKING: host_set lock, or some other form of
1438 void ata_port_probe(struct ata_port *ap)
1440 ap->flags &= ~ATA_FLAG_PORT_DISABLED;
1444 * __sata_phy_reset - Wake/reset a low-level SATA PHY
1445 * @ap: SATA port associated with target SATA PHY.
1447 * This function issues commands to standard SATA Sxxx
1448 * PHY registers, to wake up the phy (and device), and
1449 * clear any reset condition.
1452 * PCI/etc. bus probe sem.
1455 void __sata_phy_reset(struct ata_port *ap)
1458 unsigned long timeout = jiffies + (HZ * 5);
1460 if (ap->flags & ATA_FLAG_SATA_RESET) {
1461 /* issue phy wake/reset */
1462 scr_write_flush(ap, SCR_CONTROL, 0x301);
1463 /* Couldn't find anything in SATA I/II specs, but
1464 * AHCI-1.1 10.4.2 says at least 1 ms. */
1467 scr_write_flush(ap, SCR_CONTROL, 0x300); /* phy wake/clear reset */
1469 /* wait for phy to become ready, if necessary */
1472 sstatus = scr_read(ap, SCR_STATUS);
1473 if ((sstatus & 0xf) != 1)
1475 } while (time_before(jiffies, timeout));
1477 /* TODO: phy layer with polling, timeouts, etc. */
1478 if (sata_dev_present(ap))
1481 sstatus = scr_read(ap, SCR_STATUS);
1482 printk(KERN_INFO "ata%u: no device found (phy stat %08x)\n",
1484 ata_port_disable(ap);
1487 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1490 if (ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT)) {
1491 ata_port_disable(ap);
1495 ap->cbl = ATA_CBL_SATA;
1499 * sata_phy_reset - Reset SATA bus.
1500 * @ap: SATA port associated with target SATA PHY.
1502 * This function resets the SATA bus, and then probes
1503 * the bus for devices.
1506 * PCI/etc. bus probe sem.
1509 void sata_phy_reset(struct ata_port *ap)
1511 __sata_phy_reset(ap);
1512 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1518 * ata_port_disable - Disable port.
1519 * @ap: Port to be disabled.
1521 * Modify @ap data structure such that the system
1522 * thinks that the entire port is disabled, and should
1523 * never attempt to probe or communicate with devices
1526 * LOCKING: host_set lock, or some other form of
1530 void ata_port_disable(struct ata_port *ap)
1532 ap->device[0].class = ATA_DEV_NONE;
1533 ap->device[1].class = ATA_DEV_NONE;
1534 ap->flags |= ATA_FLAG_PORT_DISABLED;
1540 } xfer_mode_classes[] = {
1541 { ATA_SHIFT_UDMA, XFER_UDMA_0 },
1542 { ATA_SHIFT_MWDMA, XFER_MW_DMA_0 },
1543 { ATA_SHIFT_PIO, XFER_PIO_0 },
1546 static inline u8 base_from_shift(unsigned int shift)
1550 for (i = 0; i < ARRAY_SIZE(xfer_mode_classes); i++)
1551 if (xfer_mode_classes[i].shift == shift)
1552 return xfer_mode_classes[i].base;
1557 static void ata_dev_set_mode(struct ata_port *ap, struct ata_device *dev)
1562 if (!ata_dev_present(dev) || (ap->flags & ATA_FLAG_PORT_DISABLED))
1565 if (dev->xfer_shift == ATA_SHIFT_PIO)
1566 dev->flags |= ATA_DFLAG_PIO;
1568 ata_dev_set_xfermode(ap, dev);
1570 base = base_from_shift(dev->xfer_shift);
1571 ofs = dev->xfer_mode - base;
1572 idx = ofs + dev->xfer_shift;
1573 WARN_ON(idx >= ARRAY_SIZE(xfer_mode_str));
1575 DPRINTK("idx=%d xfer_shift=%u, xfer_mode=0x%x, base=0x%x, offset=%d\n",
1576 idx, dev->xfer_shift, (int)dev->xfer_mode, (int)base, ofs);
1578 printk(KERN_INFO "ata%u: dev %u configured for %s\n",
1579 ap->id, dev->devno, xfer_mode_str[idx]);
1582 static int ata_host_set_pio(struct ata_port *ap)
1588 mask = ata_get_mode_mask(ap, ATA_SHIFT_PIO);
1591 printk(KERN_WARNING "ata%u: no PIO support\n", ap->id);
1595 base = base_from_shift(ATA_SHIFT_PIO);
1596 xfer_mode = base + x;
1598 DPRINTK("base 0x%x xfer_mode 0x%x mask 0x%x x %d\n",
1599 (int)base, (int)xfer_mode, mask, x);
1601 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1602 struct ata_device *dev = &ap->device[i];
1603 if (ata_dev_present(dev)) {
1604 dev->pio_mode = xfer_mode;
1605 dev->xfer_mode = xfer_mode;
1606 dev->xfer_shift = ATA_SHIFT_PIO;
1607 if (ap->ops->set_piomode)
1608 ap->ops->set_piomode(ap, dev);
1615 static void ata_host_set_dma(struct ata_port *ap, u8 xfer_mode,
1616 unsigned int xfer_shift)
1620 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1621 struct ata_device *dev = &ap->device[i];
1622 if (ata_dev_present(dev)) {
1623 dev->dma_mode = xfer_mode;
1624 dev->xfer_mode = xfer_mode;
1625 dev->xfer_shift = xfer_shift;
1626 if (ap->ops->set_dmamode)
1627 ap->ops->set_dmamode(ap, dev);
1633 * ata_set_mode - Program timings and issue SET FEATURES - XFER
1634 * @ap: port on which timings will be programmed
1636 * Set ATA device disk transfer mode (PIO3, UDMA6, etc.).
1639 * PCI/etc. bus probe sem.
1642 static void ata_set_mode(struct ata_port *ap)
1644 unsigned int i, xfer_shift;
1648 /* step 1: always set host PIO timings */
1649 rc = ata_host_set_pio(ap);
1653 /* step 2: choose the best data xfer mode */
1654 xfer_mode = xfer_shift = 0;
1655 rc = ata_choose_xfer_mode(ap, &xfer_mode, &xfer_shift);
1659 /* step 3: if that xfer mode isn't PIO, set host DMA timings */
1660 if (xfer_shift != ATA_SHIFT_PIO)
1661 ata_host_set_dma(ap, xfer_mode, xfer_shift);
1663 /* step 4: update devices' xfer mode */
1664 ata_dev_set_mode(ap, &ap->device[0]);
1665 ata_dev_set_mode(ap, &ap->device[1]);
1667 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1670 if (ap->ops->post_set_mode)
1671 ap->ops->post_set_mode(ap);
1673 for (i = 0; i < 2; i++) {
1674 struct ata_device *dev = &ap->device[i];
1675 ata_dev_set_protocol(dev);
1681 ata_port_disable(ap);
1685 * ata_busy_sleep - sleep until BSY clears, or timeout
1686 * @ap: port containing status register to be polled
1687 * @tmout_pat: impatience timeout
1688 * @tmout: overall timeout
1690 * Sleep until ATA Status register bit BSY clears,
1691 * or a timeout occurs.
1697 static unsigned int ata_busy_sleep (struct ata_port *ap,
1698 unsigned long tmout_pat,
1699 unsigned long tmout)
1701 unsigned long timer_start, timeout;
1704 status = ata_busy_wait(ap, ATA_BUSY, 300);
1705 timer_start = jiffies;
1706 timeout = timer_start + tmout_pat;
1707 while ((status & ATA_BUSY) && (time_before(jiffies, timeout))) {
1709 status = ata_busy_wait(ap, ATA_BUSY, 3);
1712 if (status & ATA_BUSY)
1713 printk(KERN_WARNING "ata%u is slow to respond, "
1714 "please be patient\n", ap->id);
1716 timeout = timer_start + tmout;
1717 while ((status & ATA_BUSY) && (time_before(jiffies, timeout))) {
1719 status = ata_chk_status(ap);
1722 if (status & ATA_BUSY) {
1723 printk(KERN_ERR "ata%u failed to respond (%lu secs)\n",
1724 ap->id, tmout / HZ);
1731 static void ata_bus_post_reset(struct ata_port *ap, unsigned int devmask)
1733 struct ata_ioports *ioaddr = &ap->ioaddr;
1734 unsigned int dev0 = devmask & (1 << 0);
1735 unsigned int dev1 = devmask & (1 << 1);
1736 unsigned long timeout;
1738 /* if device 0 was found in ata_devchk, wait for its
1742 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
1744 /* if device 1 was found in ata_devchk, wait for
1745 * register access, then wait for BSY to clear
1747 timeout = jiffies + ATA_TMOUT_BOOT;
1751 ap->ops->dev_select(ap, 1);
1752 if (ap->flags & ATA_FLAG_MMIO) {
1753 nsect = readb((void __iomem *) ioaddr->nsect_addr);
1754 lbal = readb((void __iomem *) ioaddr->lbal_addr);
1756 nsect = inb(ioaddr->nsect_addr);
1757 lbal = inb(ioaddr->lbal_addr);
1759 if ((nsect == 1) && (lbal == 1))
1761 if (time_after(jiffies, timeout)) {
1765 msleep(50); /* give drive a breather */
1768 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
1770 /* is all this really necessary? */
1771 ap->ops->dev_select(ap, 0);
1773 ap->ops->dev_select(ap, 1);
1775 ap->ops->dev_select(ap, 0);
1779 * ata_bus_edd - Issue EXECUTE DEVICE DIAGNOSTIC command.
1780 * @ap: Port to reset and probe
1782 * Use the EXECUTE DEVICE DIAGNOSTIC command to reset and
1783 * probe the bus. Not often used these days.
1786 * PCI/etc. bus probe sem.
1790 static unsigned int ata_bus_edd(struct ata_port *ap)
1792 struct ata_taskfile tf;
1794 /* set up execute-device-diag (bus reset) taskfile */
1795 /* also, take interrupts to a known state (disabled) */
1796 DPRINTK("execute-device-diag\n");
1797 ata_tf_init(ap, &tf, 0);
1799 tf.command = ATA_CMD_EDD;
1800 tf.protocol = ATA_PROT_NODATA;
1803 ata_tf_to_host(ap, &tf);
1805 /* spec says at least 2ms. but who knows with those
1806 * crazy ATAPI devices...
1810 return ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
1813 static unsigned int ata_bus_softreset(struct ata_port *ap,
1814 unsigned int devmask)
1816 struct ata_ioports *ioaddr = &ap->ioaddr;
1818 DPRINTK("ata%u: bus reset via SRST\n", ap->id);
1820 /* software reset. causes dev0 to be selected */
1821 if (ap->flags & ATA_FLAG_MMIO) {
1822 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
1823 udelay(20); /* FIXME: flush */
1824 writeb(ap->ctl | ATA_SRST, (void __iomem *) ioaddr->ctl_addr);
1825 udelay(20); /* FIXME: flush */
1826 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
1828 outb(ap->ctl, ioaddr->ctl_addr);
1830 outb(ap->ctl | ATA_SRST, ioaddr->ctl_addr);
1832 outb(ap->ctl, ioaddr->ctl_addr);
1835 /* spec mandates ">= 2ms" before checking status.
1836 * We wait 150ms, because that was the magic delay used for
1837 * ATAPI devices in Hale Landis's ATADRVR, for the period of time
1838 * between when the ATA command register is written, and then
1839 * status is checked. Because waiting for "a while" before
1840 * checking status is fine, post SRST, we perform this magic
1841 * delay here as well.
1845 ata_bus_post_reset(ap, devmask);
1851 * ata_bus_reset - reset host port and associated ATA channel
1852 * @ap: port to reset
1854 * This is typically the first time we actually start issuing
1855 * commands to the ATA channel. We wait for BSY to clear, then
1856 * issue EXECUTE DEVICE DIAGNOSTIC command, polling for its
1857 * result. Determine what devices, if any, are on the channel
1858 * by looking at the device 0/1 error register. Look at the signature
1859 * stored in each device's taskfile registers, to determine if
1860 * the device is ATA or ATAPI.
1863 * PCI/etc. bus probe sem.
1864 * Obtains host_set lock.
1867 * Sets ATA_FLAG_PORT_DISABLED if bus reset fails.
1870 void ata_bus_reset(struct ata_port *ap)
1872 struct ata_ioports *ioaddr = &ap->ioaddr;
1873 unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS;
1875 unsigned int dev0, dev1 = 0, rc = 0, devmask = 0;
1877 DPRINTK("ENTER, host %u, port %u\n", ap->id, ap->port_no);
1879 /* determine if device 0/1 are present */
1880 if (ap->flags & ATA_FLAG_SATA_RESET)
1883 dev0 = ata_devchk(ap, 0);
1885 dev1 = ata_devchk(ap, 1);
1889 devmask |= (1 << 0);
1891 devmask |= (1 << 1);
1893 /* select device 0 again */
1894 ap->ops->dev_select(ap, 0);
1896 /* issue bus reset */
1897 if (ap->flags & ATA_FLAG_SRST)
1898 rc = ata_bus_softreset(ap, devmask);
1899 else if ((ap->flags & ATA_FLAG_SATA_RESET) == 0) {
1900 /* set up device control */
1901 if (ap->flags & ATA_FLAG_MMIO)
1902 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
1904 outb(ap->ctl, ioaddr->ctl_addr);
1905 rc = ata_bus_edd(ap);
1912 * determine by signature whether we have ATA or ATAPI devices
1914 err = ata_dev_try_classify(ap, 0);
1915 if ((slave_possible) && (err != 0x81))
1916 ata_dev_try_classify(ap, 1);
1918 /* re-enable interrupts */
1919 if (ap->ioaddr.ctl_addr) /* FIXME: hack. create a hook instead */
1922 /* is double-select really necessary? */
1923 if (ap->device[1].class != ATA_DEV_NONE)
1924 ap->ops->dev_select(ap, 1);
1925 if (ap->device[0].class != ATA_DEV_NONE)
1926 ap->ops->dev_select(ap, 0);
1928 /* if no devices were detected, disable this port */
1929 if ((ap->device[0].class == ATA_DEV_NONE) &&
1930 (ap->device[1].class == ATA_DEV_NONE))
1933 if (ap->flags & (ATA_FLAG_SATA_RESET | ATA_FLAG_SRST)) {
1934 /* set up device control for ATA_FLAG_SATA_RESET */
1935 if (ap->flags & ATA_FLAG_MMIO)
1936 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
1938 outb(ap->ctl, ioaddr->ctl_addr);
1945 printk(KERN_ERR "ata%u: disabling port\n", ap->id);
1946 ap->ops->port_disable(ap);
1951 static void ata_pr_blacklisted(struct ata_port *ap, struct ata_device *dev)
1953 printk(KERN_WARNING "ata%u: dev %u is on DMA blacklist, disabling DMA\n",
1954 ap->id, dev->devno);
1957 static const char * ata_dma_blacklist [] = {
1976 "Toshiba CD-ROM XM-6202B",
1977 "TOSHIBA CD-ROM XM-1702BC",
1979 "E-IDE CD-ROM CR-840",
1982 "SAMSUNG CD-ROM SC-148C",
1983 "SAMSUNG CD-ROM SC",
1985 "ATAPI CD-ROM DRIVE 40X MAXIMUM",
1989 static int ata_dma_blacklisted(struct ata_port *ap, struct ata_device *dev)
1991 unsigned char model_num[40];
1996 ata_dev_id_string(dev->id, model_num, ATA_ID_PROD_OFS,
1999 len = strnlen(s, sizeof(model_num));
2001 /* ATAPI specifies that empty space is blank-filled; remove blanks */
2002 while ((len > 0) && (s[len - 1] == ' ')) {
2007 for (i = 0; i < ARRAY_SIZE(ata_dma_blacklist); i++)
2008 if (!strncmp(ata_dma_blacklist[i], s, len))
2014 static unsigned int ata_get_mode_mask(struct ata_port *ap, int shift)
2016 struct ata_device *master, *slave;
2019 master = &ap->device[0];
2020 slave = &ap->device[1];
2022 assert (ata_dev_present(master) || ata_dev_present(slave));
2024 if (shift == ATA_SHIFT_UDMA) {
2025 mask = ap->udma_mask;
2026 if (ata_dev_present(master)) {
2027 mask &= (master->id[ATA_ID_UDMA_MODES] & 0xff);
2028 if (ata_dma_blacklisted(ap, master)) {
2030 ata_pr_blacklisted(ap, master);
2033 if (ata_dev_present(slave)) {
2034 mask &= (slave->id[ATA_ID_UDMA_MODES] & 0xff);
2035 if (ata_dma_blacklisted(ap, slave)) {
2037 ata_pr_blacklisted(ap, slave);
2041 else if (shift == ATA_SHIFT_MWDMA) {
2042 mask = ap->mwdma_mask;
2043 if (ata_dev_present(master)) {
2044 mask &= (master->id[ATA_ID_MWDMA_MODES] & 0x07);
2045 if (ata_dma_blacklisted(ap, master)) {
2047 ata_pr_blacklisted(ap, master);
2050 if (ata_dev_present(slave)) {
2051 mask &= (slave->id[ATA_ID_MWDMA_MODES] & 0x07);
2052 if (ata_dma_blacklisted(ap, slave)) {
2054 ata_pr_blacklisted(ap, slave);
2058 else if (shift == ATA_SHIFT_PIO) {
2059 mask = ap->pio_mask;
2060 if (ata_dev_present(master)) {
2061 /* spec doesn't return explicit support for
2062 * PIO0-2, so we fake it
2064 u16 tmp_mode = master->id[ATA_ID_PIO_MODES] & 0x03;
2069 if (ata_dev_present(slave)) {
2070 /* spec doesn't return explicit support for
2071 * PIO0-2, so we fake it
2073 u16 tmp_mode = slave->id[ATA_ID_PIO_MODES] & 0x03;
2080 mask = 0xffffffff; /* shut up compiler warning */
2087 /* find greatest bit */
2088 static int fgb(u32 bitmap)
2093 for (i = 0; i < 32; i++)
2094 if (bitmap & (1 << i))
2101 * ata_choose_xfer_mode - attempt to find best transfer mode
2102 * @ap: Port for which an xfer mode will be selected
2103 * @xfer_mode_out: (output) SET FEATURES - XFER MODE code
2104 * @xfer_shift_out: (output) bit shift that selects this mode
2106 * Based on host and device capabilities, determine the
2107 * maximum transfer mode that is amenable to all.
2110 * PCI/etc. bus probe sem.
2113 * Zero on success, negative on error.
2116 static int ata_choose_xfer_mode(struct ata_port *ap,
2118 unsigned int *xfer_shift_out)
2120 unsigned int mask, shift;
2123 for (i = 0; i < ARRAY_SIZE(xfer_mode_classes); i++) {
2124 shift = xfer_mode_classes[i].shift;
2125 mask = ata_get_mode_mask(ap, shift);
2129 *xfer_mode_out = xfer_mode_classes[i].base + x;
2130 *xfer_shift_out = shift;
2139 * ata_dev_set_xfermode - Issue SET FEATURES - XFER MODE command
2140 * @ap: Port associated with device @dev
2141 * @dev: Device to which command will be sent
2143 * Issue SET FEATURES - XFER MODE command to device @dev
2147 * PCI/etc. bus probe sem.
2150 static void ata_dev_set_xfermode(struct ata_port *ap, struct ata_device *dev)
2152 DECLARE_COMPLETION(wait);
2153 struct ata_queued_cmd *qc;
2155 unsigned long flags;
2157 /* set up set-features taskfile */
2158 DPRINTK("set features - xfer mode\n");
2160 qc = ata_qc_new_init(ap, dev);
2163 qc->tf.command = ATA_CMD_SET_FEATURES;
2164 qc->tf.feature = SETFEATURES_XFER;
2165 qc->tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
2166 qc->tf.protocol = ATA_PROT_NODATA;
2167 qc->tf.nsect = dev->xfer_mode;
2169 qc->waiting = &wait;
2170 qc->complete_fn = ata_qc_complete_noop;
2172 spin_lock_irqsave(&ap->host_set->lock, flags);
2173 rc = ata_qc_issue(qc);
2174 spin_unlock_irqrestore(&ap->host_set->lock, flags);
2177 ata_port_disable(ap);
2179 wait_for_completion(&wait);
2185 * ata_dev_init_params - Issue INIT DEV PARAMS command
2186 * @ap: Port associated with device @dev
2187 * @dev: Device to which command will be sent
2192 static void ata_dev_init_params(struct ata_port *ap, struct ata_device *dev)
2194 DECLARE_COMPLETION(wait);
2195 struct ata_queued_cmd *qc;
2197 unsigned long flags;
2198 u16 sectors = dev->id[6];
2199 u16 heads = dev->id[3];
2201 /* Number of sectors per track 1-255. Number of heads 1-16 */
2202 if (sectors < 1 || sectors > 255 || heads < 1 || heads > 16)
2205 /* set up init dev params taskfile */
2206 DPRINTK("init dev params \n");
2208 qc = ata_qc_new_init(ap, dev);
2211 qc->tf.command = ATA_CMD_INIT_DEV_PARAMS;
2212 qc->tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
2213 qc->tf.protocol = ATA_PROT_NODATA;
2214 qc->tf.nsect = sectors;
2215 qc->tf.device |= (heads - 1) & 0x0f; /* max head = num. of heads - 1 */
2217 qc->waiting = &wait;
2218 qc->complete_fn = ata_qc_complete_noop;
2220 spin_lock_irqsave(&ap->host_set->lock, flags);
2221 rc = ata_qc_issue(qc);
2222 spin_unlock_irqrestore(&ap->host_set->lock, flags);
2225 ata_port_disable(ap);
2227 wait_for_completion(&wait);
2233 * ata_sg_clean - Unmap DMA memory associated with command
2234 * @qc: Command containing DMA memory to be released
2236 * Unmap all mapped DMA memory associated with this command.
2239 * spin_lock_irqsave(host_set lock)
2242 static void ata_sg_clean(struct ata_queued_cmd *qc)
2244 struct ata_port *ap = qc->ap;
2245 struct scatterlist *sg = qc->sg;
2246 int dir = qc->dma_dir;
2248 assert(qc->flags & ATA_QCFLAG_DMAMAP);
2251 if (qc->flags & ATA_QCFLAG_SINGLE)
2252 assert(qc->n_elem == 1);
2254 DPRINTK("unmapping %u sg elements\n", qc->n_elem);
2256 if (qc->flags & ATA_QCFLAG_SG)
2257 dma_unmap_sg(ap->host_set->dev, sg, qc->n_elem, dir);
2259 dma_unmap_single(ap->host_set->dev, sg_dma_address(&sg[0]),
2260 sg_dma_len(&sg[0]), dir);
2262 qc->flags &= ~ATA_QCFLAG_DMAMAP;
2267 * ata_fill_sg - Fill PCI IDE PRD table
2268 * @qc: Metadata associated with taskfile to be transferred
2270 * Fill PCI IDE PRD (scatter-gather) table with segments
2271 * associated with the current disk command.
2274 * spin_lock_irqsave(host_set lock)
2277 static void ata_fill_sg(struct ata_queued_cmd *qc)
2279 struct scatterlist *sg = qc->sg;
2280 struct ata_port *ap = qc->ap;
2281 unsigned int idx, nelem;
2284 assert(qc->n_elem > 0);
2287 for (nelem = qc->n_elem; nelem; nelem--,sg++) {
2291 /* determine if physical DMA addr spans 64K boundary.
2292 * Note h/w doesn't support 64-bit, so we unconditionally
2293 * truncate dma_addr_t to u32.
2295 addr = (u32) sg_dma_address(sg);
2296 sg_len = sg_dma_len(sg);
2299 offset = addr & 0xffff;
2301 if ((offset + sg_len) > 0x10000)
2302 len = 0x10000 - offset;
2304 ap->prd[idx].addr = cpu_to_le32(addr);
2305 ap->prd[idx].flags_len = cpu_to_le32(len & 0xffff);
2306 VPRINTK("PRD[%u] = (0x%X, 0x%X)\n", idx, addr, len);
2315 ap->prd[idx - 1].flags_len |= cpu_to_le32(ATA_PRD_EOT);
2318 * ata_check_atapi_dma - Check whether ATAPI DMA can be supported
2319 * @qc: Metadata associated with taskfile to check
2321 * Allow low-level driver to filter ATA PACKET commands, returning
2322 * a status indicating whether or not it is OK to use DMA for the
2323 * supplied PACKET command.
2326 * spin_lock_irqsave(host_set lock)
2328 * RETURNS: 0 when ATAPI DMA can be used
2331 int ata_check_atapi_dma(struct ata_queued_cmd *qc)
2333 struct ata_port *ap = qc->ap;
2334 int rc = 0; /* Assume ATAPI DMA is OK by default */
2336 if (ap->ops->check_atapi_dma)
2337 rc = ap->ops->check_atapi_dma(qc);
2342 * ata_qc_prep - Prepare taskfile for submission
2343 * @qc: Metadata associated with taskfile to be prepared
2345 * Prepare ATA taskfile for submission.
2348 * spin_lock_irqsave(host_set lock)
2350 void ata_qc_prep(struct ata_queued_cmd *qc)
2352 if (!(qc->flags & ATA_QCFLAG_DMAMAP))
2359 * ata_sg_init_one - Associate command with memory buffer
2360 * @qc: Command to be associated
2361 * @buf: Memory buffer
2362 * @buflen: Length of memory buffer, in bytes.
2364 * Initialize the data-related elements of queued_cmd @qc
2365 * to point to a single memory buffer, @buf of byte length @buflen.
2368 * spin_lock_irqsave(host_set lock)
2371 void ata_sg_init_one(struct ata_queued_cmd *qc, void *buf, unsigned int buflen)
2373 struct scatterlist *sg;
2375 qc->flags |= ATA_QCFLAG_SINGLE;
2377 memset(&qc->sgent, 0, sizeof(qc->sgent));
2378 qc->sg = &qc->sgent;
2383 sg->page = virt_to_page(buf);
2384 sg->offset = (unsigned long) buf & ~PAGE_MASK;
2385 sg->length = buflen;
2389 * ata_sg_init - Associate command with scatter-gather table.
2390 * @qc: Command to be associated
2391 * @sg: Scatter-gather table.
2392 * @n_elem: Number of elements in s/g table.
2394 * Initialize the data-related elements of queued_cmd @qc
2395 * to point to a scatter-gather table @sg, containing @n_elem
2399 * spin_lock_irqsave(host_set lock)
2402 void ata_sg_init(struct ata_queued_cmd *qc, struct scatterlist *sg,
2403 unsigned int n_elem)
2405 qc->flags |= ATA_QCFLAG_SG;
2407 qc->n_elem = n_elem;
2411 * ata_sg_setup_one - DMA-map the memory buffer associated with a command.
2412 * @qc: Command with memory buffer to be mapped.
2414 * DMA-map the memory buffer associated with queued_cmd @qc.
2417 * spin_lock_irqsave(host_set lock)
2420 * Zero on success, negative on error.
2423 static int ata_sg_setup_one(struct ata_queued_cmd *qc)
2425 struct ata_port *ap = qc->ap;
2426 int dir = qc->dma_dir;
2427 struct scatterlist *sg = qc->sg;
2428 dma_addr_t dma_address;
2430 dma_address = dma_map_single(ap->host_set->dev, qc->buf_virt,
2432 if (dma_mapping_error(dma_address))
2435 sg_dma_address(sg) = dma_address;
2436 sg_dma_len(sg) = sg->length;
2438 DPRINTK("mapped buffer of %d bytes for %s\n", sg_dma_len(sg),
2439 qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
2445 * ata_sg_setup - DMA-map the scatter-gather table associated with a command.
2446 * @qc: Command with scatter-gather table to be mapped.
2448 * DMA-map the scatter-gather table associated with queued_cmd @qc.
2451 * spin_lock_irqsave(host_set lock)
2454 * Zero on success, negative on error.
2458 static int ata_sg_setup(struct ata_queued_cmd *qc)
2460 struct ata_port *ap = qc->ap;
2461 struct scatterlist *sg = qc->sg;
2464 VPRINTK("ENTER, ata%u\n", ap->id);
2465 assert(qc->flags & ATA_QCFLAG_SG);
2468 n_elem = dma_map_sg(ap->host_set->dev, sg, qc->n_elem, dir);
2472 DPRINTK("%d sg elements mapped\n", n_elem);
2474 qc->n_elem = n_elem;
2480 * ata_poll_qc_complete - turn irq back on and finish qc
2481 * @qc: Command to complete
2482 * @drv_stat: ATA status register content
2485 * None. (grabs host lock)
2488 void ata_poll_qc_complete(struct ata_queued_cmd *qc, u8 drv_stat)
2490 struct ata_port *ap = qc->ap;
2491 unsigned long flags;
2493 spin_lock_irqsave(&ap->host_set->lock, flags);
2494 ap->flags &= ~ATA_FLAG_NOINTR;
2496 ata_qc_complete(qc, drv_stat);
2497 spin_unlock_irqrestore(&ap->host_set->lock, flags);
2505 * None. (executing in kernel thread context)
2511 static unsigned long ata_pio_poll(struct ata_port *ap)
2514 unsigned int poll_state = PIO_ST_UNKNOWN;
2515 unsigned int reg_state = PIO_ST_UNKNOWN;
2516 const unsigned int tmout_state = PIO_ST_TMOUT;
2518 switch (ap->pio_task_state) {
2521 poll_state = PIO_ST_POLL;
2525 case PIO_ST_LAST_POLL:
2526 poll_state = PIO_ST_LAST_POLL;
2527 reg_state = PIO_ST_LAST;
2534 status = ata_chk_status(ap);
2535 if (status & ATA_BUSY) {
2536 if (time_after(jiffies, ap->pio_task_timeout)) {
2537 ap->pio_task_state = tmout_state;
2540 ap->pio_task_state = poll_state;
2541 return ATA_SHORT_PAUSE;
2544 ap->pio_task_state = reg_state;
2549 * ata_pio_complete -
2553 * None. (executing in kernel thread context)
2556 static void ata_pio_complete (struct ata_port *ap)
2558 struct ata_queued_cmd *qc;
2562 * This is purely heuristic. This is a fast path. Sometimes when
2563 * we enter, BSY will be cleared in a chk-status or two. If not,
2564 * the drive is probably seeking or something. Snooze for a couple
2565 * msecs, then chk-status again. If still busy, fall back to
2566 * PIO_ST_POLL state.
2568 drv_stat = ata_busy_wait(ap, ATA_BUSY | ATA_DRQ, 10);
2569 if (drv_stat & (ATA_BUSY | ATA_DRQ)) {
2571 drv_stat = ata_busy_wait(ap, ATA_BUSY | ATA_DRQ, 10);
2572 if (drv_stat & (ATA_BUSY | ATA_DRQ)) {
2573 ap->pio_task_state = PIO_ST_LAST_POLL;
2574 ap->pio_task_timeout = jiffies + ATA_TMOUT_PIO;
2579 drv_stat = ata_wait_idle(ap);
2580 if (!ata_ok(drv_stat)) {
2581 ap->pio_task_state = PIO_ST_ERR;
2585 qc = ata_qc_from_tag(ap, ap->active_tag);
2588 ap->pio_task_state = PIO_ST_IDLE;
2590 ata_poll_qc_complete(qc, drv_stat);
2596 * @buf: Buffer to swap
2597 * @buf_words: Number of 16-bit words in buffer.
2599 * Swap halves of 16-bit words if needed to convert from
2600 * little-endian byte order to native cpu byte order, or
2605 void swap_buf_le16(u16 *buf, unsigned int buf_words)
2610 for (i = 0; i < buf_words; i++)
2611 buf[i] = le16_to_cpu(buf[i]);
2612 #endif /* __BIG_ENDIAN */
2616 * ata_mmio_data_xfer - Transfer data by MMIO
2617 * @ap: port to read/write
2619 * @buflen: buffer length
2620 * @write_data: read/write
2622 * Transfer data from/to the device data register by MMIO.
2625 * Inherited from caller.
2629 static void ata_mmio_data_xfer(struct ata_port *ap, unsigned char *buf,
2630 unsigned int buflen, int write_data)
2633 unsigned int words = buflen >> 1;
2634 u16 *buf16 = (u16 *) buf;
2635 void __iomem *mmio = (void __iomem *)ap->ioaddr.data_addr;
2637 /* Transfer multiple of 2 bytes */
2639 for (i = 0; i < words; i++)
2640 writew(le16_to_cpu(buf16[i]), mmio);
2642 for (i = 0; i < words; i++)
2643 buf16[i] = cpu_to_le16(readw(mmio));
2646 /* Transfer trailing 1 byte, if any. */
2647 if (unlikely(buflen & 0x01)) {
2648 u16 align_buf[1] = { 0 };
2649 unsigned char *trailing_buf = buf + buflen - 1;
2652 memcpy(align_buf, trailing_buf, 1);
2653 writew(le16_to_cpu(align_buf[0]), mmio);
2655 align_buf[0] = cpu_to_le16(readw(mmio));
2656 memcpy(trailing_buf, align_buf, 1);
2662 * ata_pio_data_xfer - Transfer data by PIO
2663 * @ap: port to read/write
2665 * @buflen: buffer length
2666 * @write_data: read/write
2668 * Transfer data from/to the device data register by PIO.
2671 * Inherited from caller.
2675 static void ata_pio_data_xfer(struct ata_port *ap, unsigned char *buf,
2676 unsigned int buflen, int write_data)
2678 unsigned int words = buflen >> 1;
2680 /* Transfer multiple of 2 bytes */
2682 outsw(ap->ioaddr.data_addr, buf, words);
2684 insw(ap->ioaddr.data_addr, buf, words);
2686 /* Transfer trailing 1 byte, if any. */
2687 if (unlikely(buflen & 0x01)) {
2688 u16 align_buf[1] = { 0 };
2689 unsigned char *trailing_buf = buf + buflen - 1;
2692 memcpy(align_buf, trailing_buf, 1);
2693 outw(le16_to_cpu(align_buf[0]), ap->ioaddr.data_addr);
2695 align_buf[0] = cpu_to_le16(inw(ap->ioaddr.data_addr));
2696 memcpy(trailing_buf, align_buf, 1);
2702 * ata_data_xfer - Transfer data from/to the data register.
2703 * @ap: port to read/write
2705 * @buflen: buffer length
2706 * @do_write: read/write
2708 * Transfer data from/to the device data register.
2711 * Inherited from caller.
2715 static void ata_data_xfer(struct ata_port *ap, unsigned char *buf,
2716 unsigned int buflen, int do_write)
2718 if (ap->flags & ATA_FLAG_MMIO)
2719 ata_mmio_data_xfer(ap, buf, buflen, do_write);
2721 ata_pio_data_xfer(ap, buf, buflen, do_write);
2725 * ata_pio_sector - Transfer ATA_SECT_SIZE (512 bytes) of data.
2726 * @qc: Command on going
2728 * Transfer ATA_SECT_SIZE of data from/to the ATA device.
2731 * Inherited from caller.
2734 static void ata_pio_sector(struct ata_queued_cmd *qc)
2736 int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
2737 struct scatterlist *sg = qc->sg;
2738 struct ata_port *ap = qc->ap;
2740 unsigned int offset;
2743 if (qc->cursect == (qc->nsect - 1))
2744 ap->pio_task_state = PIO_ST_LAST;
2746 page = sg[qc->cursg].page;
2747 offset = sg[qc->cursg].offset + qc->cursg_ofs * ATA_SECT_SIZE;
2749 /* get the current page and offset */
2750 page = nth_page(page, (offset >> PAGE_SHIFT));
2751 offset %= PAGE_SIZE;
2753 buf = kmap(page) + offset;
2758 if ((qc->cursg_ofs * ATA_SECT_SIZE) == (&sg[qc->cursg])->length) {
2763 DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
2765 /* do the actual data transfer */
2766 do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
2767 ata_data_xfer(ap, buf, ATA_SECT_SIZE, do_write);
2773 * __atapi_pio_bytes - Transfer data from/to the ATAPI device.
2774 * @qc: Command on going
2775 * @bytes: number of bytes
2777 * Transfer Transfer data from/to the ATAPI device.
2780 * Inherited from caller.
2784 static void __atapi_pio_bytes(struct ata_queued_cmd *qc, unsigned int bytes)
2786 int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
2787 struct scatterlist *sg = qc->sg;
2788 struct ata_port *ap = qc->ap;
2791 unsigned int offset, count;
2793 if (qc->curbytes + bytes >= qc->nbytes)
2794 ap->pio_task_state = PIO_ST_LAST;
2797 if (unlikely(qc->cursg >= qc->n_elem)) {
2799 * The end of qc->sg is reached and the device expects
2800 * more data to transfer. In order not to overrun qc->sg
2801 * and fulfill length specified in the byte count register,
2802 * - for read case, discard trailing data from the device
2803 * - for write case, padding zero data to the device
2805 u16 pad_buf[1] = { 0 };
2806 unsigned int words = bytes >> 1;
2809 if (words) /* warning if bytes > 1 */
2810 printk(KERN_WARNING "ata%u: %u bytes trailing data\n",
2813 for (i = 0; i < words; i++)
2814 ata_data_xfer(ap, (unsigned char*)pad_buf, 2, do_write);
2816 ap->pio_task_state = PIO_ST_LAST;
2820 sg = &qc->sg[qc->cursg];
2823 offset = sg->offset + qc->cursg_ofs;
2825 /* get the current page and offset */
2826 page = nth_page(page, (offset >> PAGE_SHIFT));
2827 offset %= PAGE_SIZE;
2829 /* don't overrun current sg */
2830 count = min(sg->length - qc->cursg_ofs, bytes);
2832 /* don't cross page boundaries */
2833 count = min(count, (unsigned int)PAGE_SIZE - offset);
2835 buf = kmap(page) + offset;
2838 qc->curbytes += count;
2839 qc->cursg_ofs += count;
2841 if (qc->cursg_ofs == sg->length) {
2846 DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
2848 /* do the actual data transfer */
2849 ata_data_xfer(ap, buf, count, do_write);
2858 * atapi_pio_bytes - Transfer data from/to the ATAPI device.
2859 * @qc: Command on going
2861 * Transfer Transfer data from/to the ATAPI device.
2864 * Inherited from caller.
2868 static void atapi_pio_bytes(struct ata_queued_cmd *qc)
2870 struct ata_port *ap = qc->ap;
2871 struct ata_device *dev = qc->dev;
2872 unsigned int ireason, bc_lo, bc_hi, bytes;
2873 int i_write, do_write = (qc->tf.flags & ATA_TFLAG_WRITE) ? 1 : 0;
2875 ap->ops->tf_read(ap, &qc->tf);
2876 ireason = qc->tf.nsect;
2877 bc_lo = qc->tf.lbam;
2878 bc_hi = qc->tf.lbah;
2879 bytes = (bc_hi << 8) | bc_lo;
2881 /* shall be cleared to zero, indicating xfer of data */
2882 if (ireason & (1 << 0))
2885 /* make sure transfer direction matches expected */
2886 i_write = ((ireason & (1 << 1)) == 0) ? 1 : 0;
2887 if (do_write != i_write)
2890 __atapi_pio_bytes(qc, bytes);
2895 printk(KERN_INFO "ata%u: dev %u: ATAPI check failed\n",
2896 ap->id, dev->devno);
2897 ap->pio_task_state = PIO_ST_ERR;
2905 * None. (executing in kernel thread context)
2908 static void ata_pio_block(struct ata_port *ap)
2910 struct ata_queued_cmd *qc;
2914 * This is purely hueristic. This is a fast path.
2915 * Sometimes when we enter, BSY will be cleared in
2916 * a chk-status or two. If not, the drive is probably seeking
2917 * or something. Snooze for a couple msecs, then
2918 * chk-status again. If still busy, fall back to
2919 * PIO_ST_POLL state.
2921 status = ata_busy_wait(ap, ATA_BUSY, 5);
2922 if (status & ATA_BUSY) {
2924 status = ata_busy_wait(ap, ATA_BUSY, 10);
2925 if (status & ATA_BUSY) {
2926 ap->pio_task_state = PIO_ST_POLL;
2927 ap->pio_task_timeout = jiffies + ATA_TMOUT_PIO;
2932 qc = ata_qc_from_tag(ap, ap->active_tag);
2935 if (is_atapi_taskfile(&qc->tf)) {
2936 /* no more data to transfer or unsupported ATAPI command */
2937 if ((status & ATA_DRQ) == 0) {
2938 ap->pio_task_state = PIO_ST_IDLE;
2940 ata_poll_qc_complete(qc, status);
2944 atapi_pio_bytes(qc);
2946 /* handle BSY=0, DRQ=0 as error */
2947 if ((status & ATA_DRQ) == 0) {
2948 ap->pio_task_state = PIO_ST_ERR;
2956 static void ata_pio_error(struct ata_port *ap)
2958 struct ata_queued_cmd *qc;
2961 qc = ata_qc_from_tag(ap, ap->active_tag);
2964 drv_stat = ata_chk_status(ap);
2965 printk(KERN_WARNING "ata%u: PIO error, drv_stat 0x%x\n",
2968 ap->pio_task_state = PIO_ST_IDLE;
2970 ata_poll_qc_complete(qc, drv_stat | ATA_ERR);
2973 static void ata_pio_task(void *_data)
2975 struct ata_port *ap = _data;
2976 unsigned long timeout = 0;
2978 switch (ap->pio_task_state) {
2987 ata_pio_complete(ap);
2991 case PIO_ST_LAST_POLL:
2992 timeout = ata_pio_poll(ap);
3002 queue_delayed_work(ata_wq, &ap->pio_task,
3005 queue_work(ata_wq, &ap->pio_task);
3008 static void atapi_request_sense(struct ata_port *ap, struct ata_device *dev,
3009 struct scsi_cmnd *cmd)
3011 DECLARE_COMPLETION(wait);
3012 struct ata_queued_cmd *qc;
3013 unsigned long flags;
3016 DPRINTK("ATAPI request sense\n");
3018 qc = ata_qc_new_init(ap, dev);
3021 /* FIXME: is this needed? */
3022 memset(cmd->sense_buffer, 0, sizeof(cmd->sense_buffer));
3024 ata_sg_init_one(qc, cmd->sense_buffer, sizeof(cmd->sense_buffer));
3025 qc->dma_dir = DMA_FROM_DEVICE;
3027 memset(&qc->cdb, 0, ap->cdb_len);
3028 qc->cdb[0] = REQUEST_SENSE;
3029 qc->cdb[4] = SCSI_SENSE_BUFFERSIZE;
3031 qc->tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
3032 qc->tf.command = ATA_CMD_PACKET;
3034 qc->tf.protocol = ATA_PROT_ATAPI;
3035 qc->tf.lbam = (8 * 1024) & 0xff;
3036 qc->tf.lbah = (8 * 1024) >> 8;
3037 qc->nbytes = SCSI_SENSE_BUFFERSIZE;
3039 qc->waiting = &wait;
3040 qc->complete_fn = ata_qc_complete_noop;
3042 spin_lock_irqsave(&ap->host_set->lock, flags);
3043 rc = ata_qc_issue(qc);
3044 spin_unlock_irqrestore(&ap->host_set->lock, flags);
3047 ata_port_disable(ap);
3049 wait_for_completion(&wait);
3055 * ata_qc_timeout - Handle timeout of queued command
3056 * @qc: Command that timed out
3058 * Some part of the kernel (currently, only the SCSI layer)
3059 * has noticed that the active command on port @ap has not
3060 * completed after a specified length of time. Handle this
3061 * condition by disabling DMA (if necessary) and completing
3062 * transactions, with error if necessary.
3064 * This also handles the case of the "lost interrupt", where
3065 * for some reason (possibly hardware bug, possibly driver bug)
3066 * an interrupt was not delivered to the driver, even though the
3067 * transaction completed successfully.
3070 * Inherited from SCSI layer (none, can sleep)
3073 static void ata_qc_timeout(struct ata_queued_cmd *qc)
3075 struct ata_port *ap = qc->ap;
3076 struct ata_host_set *host_set = ap->host_set;
3077 struct ata_device *dev = qc->dev;
3078 u8 host_stat = 0, drv_stat;
3079 unsigned long flags;
3083 /* FIXME: doesn't this conflict with timeout handling? */
3084 if (qc->dev->class == ATA_DEV_ATAPI && qc->scsicmd) {
3085 struct scsi_cmnd *cmd = qc->scsicmd;
3087 if (!(cmd->eh_eflags & SCSI_EH_CANCEL_CMD)) {
3089 /* finish completing original command */
3090 spin_lock_irqsave(&host_set->lock, flags);
3091 __ata_qc_complete(qc);
3092 spin_unlock_irqrestore(&host_set->lock, flags);
3094 atapi_request_sense(ap, dev, cmd);
3096 cmd->result = (CHECK_CONDITION << 1) | (DID_OK << 16);
3097 scsi_finish_command(cmd);
3103 spin_lock_irqsave(&host_set->lock, flags);
3105 /* hack alert! We cannot use the supplied completion
3106 * function from inside the ->eh_strategy_handler() thread.
3107 * libata is the only user of ->eh_strategy_handler() in
3108 * any kernel, so the default scsi_done() assumes it is
3109 * not being called from the SCSI EH.
3111 qc->scsidone = scsi_finish_command;
3113 switch (qc->tf.protocol) {
3116 case ATA_PROT_ATAPI_DMA:
3117 host_stat = ap->ops->bmdma_status(ap);
3119 /* before we do anything else, clear DMA-Start bit */
3120 ap->ops->bmdma_stop(qc);
3126 drv_stat = ata_chk_status(ap);
3128 /* ack bmdma irq events */
3129 ap->ops->irq_clear(ap);
3131 printk(KERN_ERR "ata%u: command 0x%x timeout, stat 0x%x host_stat 0x%x\n",
3132 ap->id, qc->tf.command, drv_stat, host_stat);
3134 /* complete taskfile transaction */
3135 ata_qc_complete(qc, drv_stat);
3139 spin_unlock_irqrestore(&host_set->lock, flags);
3146 * ata_eng_timeout - Handle timeout of queued command
3147 * @ap: Port on which timed-out command is active
3149 * Some part of the kernel (currently, only the SCSI layer)
3150 * has noticed that the active command on port @ap has not
3151 * completed after a specified length of time. Handle this
3152 * condition by disabling DMA (if necessary) and completing
3153 * transactions, with error if necessary.
3155 * This also handles the case of the "lost interrupt", where
3156 * for some reason (possibly hardware bug, possibly driver bug)
3157 * an interrupt was not delivered to the driver, even though the
3158 * transaction completed successfully.
3161 * Inherited from SCSI layer (none, can sleep)
3164 void ata_eng_timeout(struct ata_port *ap)
3166 struct ata_queued_cmd *qc;
3170 qc = ata_qc_from_tag(ap, ap->active_tag);
3172 printk(KERN_ERR "ata%u: BUG: timeout without command\n",
3184 * ata_qc_new - Request an available ATA command, for queueing
3185 * @ap: Port associated with device @dev
3186 * @dev: Device from whom we request an available command structure
3192 static struct ata_queued_cmd *ata_qc_new(struct ata_port *ap)
3194 struct ata_queued_cmd *qc = NULL;
3197 for (i = 0; i < ATA_MAX_QUEUE; i++)
3198 if (!test_and_set_bit(i, &ap->qactive)) {
3199 qc = ata_qc_from_tag(ap, i);
3210 * ata_qc_new_init - Request an available ATA command, and initialize it
3211 * @ap: Port associated with device @dev
3212 * @dev: Device from whom we request an available command structure
3218 struct ata_queued_cmd *ata_qc_new_init(struct ata_port *ap,
3219 struct ata_device *dev)
3221 struct ata_queued_cmd *qc;
3223 qc = ata_qc_new(ap);
3230 qc->cursect = qc->cursg = qc->cursg_ofs = 0;
3232 qc->nbytes = qc->curbytes = 0;
3234 ata_tf_init(ap, &qc->tf, dev->devno);
3236 if (dev->flags & ATA_DFLAG_LBA) {
3237 qc->tf.flags |= ATA_TFLAG_LBA;
3239 if (dev->flags & ATA_DFLAG_LBA48)
3240 qc->tf.flags |= ATA_TFLAG_LBA48;
3247 static int ata_qc_complete_noop(struct ata_queued_cmd *qc, u8 drv_stat)
3252 static void __ata_qc_complete(struct ata_queued_cmd *qc)
3254 struct ata_port *ap = qc->ap;
3255 unsigned int tag, do_clear = 0;
3259 if (likely(ata_tag_valid(tag))) {
3260 if (tag == ap->active_tag)
3261 ap->active_tag = ATA_TAG_POISON;
3262 qc->tag = ATA_TAG_POISON;
3267 struct completion *waiting = qc->waiting;
3272 if (likely(do_clear))
3273 clear_bit(tag, &ap->qactive);
3277 * ata_qc_free - free unused ata_queued_cmd
3278 * @qc: Command to complete
3280 * Designed to free unused ata_queued_cmd object
3281 * in case something prevents using it.
3284 * spin_lock_irqsave(host_set lock)
3287 void ata_qc_free(struct ata_queued_cmd *qc)
3289 assert(qc != NULL); /* ata_qc_from_tag _might_ return NULL */
3290 assert(qc->waiting == NULL); /* nothing should be waiting */
3292 __ata_qc_complete(qc);
3296 * ata_qc_complete - Complete an active ATA command
3297 * @qc: Command to complete
3298 * @drv_stat: ATA Status register contents
3300 * Indicate to the mid and upper layers that an ATA
3301 * command has completed, with either an ok or not-ok status.
3304 * spin_lock_irqsave(host_set lock)
3308 void ata_qc_complete(struct ata_queued_cmd *qc, u8 drv_stat)
3312 assert(qc != NULL); /* ata_qc_from_tag _might_ return NULL */
3313 assert(qc->flags & ATA_QCFLAG_ACTIVE);
3315 if (likely(qc->flags & ATA_QCFLAG_DMAMAP))
3318 /* atapi: mark qc as inactive to prevent the interrupt handler
3319 * from completing the command twice later, before the error handler
3320 * is called. (when rc != 0 and atapi request sense is needed)
3322 qc->flags &= ~ATA_QCFLAG_ACTIVE;
3324 /* call completion callback */
3325 rc = qc->complete_fn(qc, drv_stat);
3327 /* if callback indicates not to complete command (non-zero),
3328 * return immediately
3333 __ata_qc_complete(qc);
3338 static inline int ata_should_dma_map(struct ata_queued_cmd *qc)
3340 struct ata_port *ap = qc->ap;
3342 switch (qc->tf.protocol) {
3344 case ATA_PROT_ATAPI_DMA:
3347 case ATA_PROT_ATAPI:
3349 case ATA_PROT_PIO_MULT:
3350 if (ap->flags & ATA_FLAG_PIO_DMA)
3363 * ata_qc_issue - issue taskfile to device
3364 * @qc: command to issue to device
3366 * Prepare an ATA command to submission to device.
3367 * This includes mapping the data into a DMA-able
3368 * area, filling in the S/G table, and finally
3369 * writing the taskfile to hardware, starting the command.
3372 * spin_lock_irqsave(host_set lock)
3375 * Zero on success, negative on error.
3378 int ata_qc_issue(struct ata_queued_cmd *qc)
3380 struct ata_port *ap = qc->ap;
3382 if (ata_should_dma_map(qc)) {
3383 if (qc->flags & ATA_QCFLAG_SG) {
3384 if (ata_sg_setup(qc))
3386 } else if (qc->flags & ATA_QCFLAG_SINGLE) {
3387 if (ata_sg_setup_one(qc))
3391 qc->flags &= ~ATA_QCFLAG_DMAMAP;
3394 ap->ops->qc_prep(qc);
3396 qc->ap->active_tag = qc->tag;
3397 qc->flags |= ATA_QCFLAG_ACTIVE;
3399 return ap->ops->qc_issue(qc);
3407 * ata_qc_issue_prot - issue taskfile to device in proto-dependent manner
3408 * @qc: command to issue to device
3410 * Using various libata functions and hooks, this function
3411 * starts an ATA command. ATA commands are grouped into
3412 * classes called "protocols", and issuing each type of protocol
3413 * is slightly different.
3415 * May be used as the qc_issue() entry in ata_port_operations.
3418 * spin_lock_irqsave(host_set lock)
3421 * Zero on success, negative on error.
3424 int ata_qc_issue_prot(struct ata_queued_cmd *qc)
3426 struct ata_port *ap = qc->ap;
3428 ata_dev_select(ap, qc->dev->devno, 1, 0);
3430 switch (qc->tf.protocol) {
3431 case ATA_PROT_NODATA:
3432 ata_tf_to_host_nolock(ap, &qc->tf);
3436 ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
3437 ap->ops->bmdma_setup(qc); /* set up bmdma */
3438 ap->ops->bmdma_start(qc); /* initiate bmdma */
3441 case ATA_PROT_PIO: /* load tf registers, initiate polling pio */
3442 ata_qc_set_polling(qc);
3443 ata_tf_to_host_nolock(ap, &qc->tf);
3444 ap->pio_task_state = PIO_ST;
3445 queue_work(ata_wq, &ap->pio_task);
3448 case ATA_PROT_ATAPI:
3449 ata_qc_set_polling(qc);
3450 ata_tf_to_host_nolock(ap, &qc->tf);
3451 queue_work(ata_wq, &ap->packet_task);
3454 case ATA_PROT_ATAPI_NODATA:
3455 ap->flags |= ATA_FLAG_NOINTR;
3456 ata_tf_to_host_nolock(ap, &qc->tf);
3457 queue_work(ata_wq, &ap->packet_task);
3460 case ATA_PROT_ATAPI_DMA:
3461 ap->flags |= ATA_FLAG_NOINTR;
3462 ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
3463 ap->ops->bmdma_setup(qc); /* set up bmdma */
3464 queue_work(ata_wq, &ap->packet_task);
3476 * ata_bmdma_setup_mmio - Set up PCI IDE BMDMA transaction
3477 * @qc: Info associated with this ATA transaction.
3480 * spin_lock_irqsave(host_set lock)
3483 static void ata_bmdma_setup_mmio (struct ata_queued_cmd *qc)
3485 struct ata_port *ap = qc->ap;
3486 unsigned int rw = (qc->tf.flags & ATA_TFLAG_WRITE);
3488 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
3490 /* load PRD table addr. */
3491 mb(); /* make sure PRD table writes are visible to controller */
3492 writel(ap->prd_dma, mmio + ATA_DMA_TABLE_OFS);
3494 /* specify data direction, triple-check start bit is clear */
3495 dmactl = readb(mmio + ATA_DMA_CMD);
3496 dmactl &= ~(ATA_DMA_WR | ATA_DMA_START);
3498 dmactl |= ATA_DMA_WR;
3499 writeb(dmactl, mmio + ATA_DMA_CMD);
3501 /* issue r/w command */
3502 ap->ops->exec_command(ap, &qc->tf);
3506 * ata_bmdma_start_mmio - Start a PCI IDE BMDMA transaction
3507 * @qc: Info associated with this ATA transaction.
3510 * spin_lock_irqsave(host_set lock)
3513 static void ata_bmdma_start_mmio (struct ata_queued_cmd *qc)
3515 struct ata_port *ap = qc->ap;
3516 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
3519 /* start host DMA transaction */
3520 dmactl = readb(mmio + ATA_DMA_CMD);
3521 writeb(dmactl | ATA_DMA_START, mmio + ATA_DMA_CMD);
3523 /* Strictly, one may wish to issue a readb() here, to
3524 * flush the mmio write. However, control also passes
3525 * to the hardware at this point, and it will interrupt
3526 * us when we are to resume control. So, in effect,
3527 * we don't care when the mmio write flushes.
3528 * Further, a read of the DMA status register _immediately_
3529 * following the write may not be what certain flaky hardware
3530 * is expected, so I think it is best to not add a readb()
3531 * without first all the MMIO ATA cards/mobos.
3532 * Or maybe I'm just being paranoid.
3537 * ata_bmdma_setup_pio - Set up PCI IDE BMDMA transaction (PIO)
3538 * @qc: Info associated with this ATA transaction.
3541 * spin_lock_irqsave(host_set lock)
3544 static void ata_bmdma_setup_pio (struct ata_queued_cmd *qc)
3546 struct ata_port *ap = qc->ap;
3547 unsigned int rw = (qc->tf.flags & ATA_TFLAG_WRITE);
3550 /* load PRD table addr. */
3551 outl(ap->prd_dma, ap->ioaddr.bmdma_addr + ATA_DMA_TABLE_OFS);
3553 /* specify data direction, triple-check start bit is clear */
3554 dmactl = inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3555 dmactl &= ~(ATA_DMA_WR | ATA_DMA_START);
3557 dmactl |= ATA_DMA_WR;
3558 outb(dmactl, ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3560 /* issue r/w command */
3561 ap->ops->exec_command(ap, &qc->tf);
3565 * ata_bmdma_start_pio - Start a PCI IDE BMDMA transaction (PIO)
3566 * @qc: Info associated with this ATA transaction.
3569 * spin_lock_irqsave(host_set lock)
3572 static void ata_bmdma_start_pio (struct ata_queued_cmd *qc)
3574 struct ata_port *ap = qc->ap;
3577 /* start host DMA transaction */
3578 dmactl = inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3579 outb(dmactl | ATA_DMA_START,
3580 ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3585 * ata_bmdma_start - Start a PCI IDE BMDMA transaction
3586 * @qc: Info associated with this ATA transaction.
3588 * Writes the ATA_DMA_START flag to the DMA command register.
3590 * May be used as the bmdma_start() entry in ata_port_operations.
3593 * spin_lock_irqsave(host_set lock)
3595 void ata_bmdma_start(struct ata_queued_cmd *qc)
3597 if (qc->ap->flags & ATA_FLAG_MMIO)
3598 ata_bmdma_start_mmio(qc);
3600 ata_bmdma_start_pio(qc);
3605 * ata_bmdma_setup - Set up PCI IDE BMDMA transaction
3606 * @qc: Info associated with this ATA transaction.
3608 * Writes address of PRD table to device's PRD Table Address
3609 * register, sets the DMA control register, and calls
3610 * ops->exec_command() to start the transfer.
3612 * May be used as the bmdma_setup() entry in ata_port_operations.
3615 * spin_lock_irqsave(host_set lock)
3617 void ata_bmdma_setup(struct ata_queued_cmd *qc)
3619 if (qc->ap->flags & ATA_FLAG_MMIO)
3620 ata_bmdma_setup_mmio(qc);
3622 ata_bmdma_setup_pio(qc);
3627 * ata_bmdma_irq_clear - Clear PCI IDE BMDMA interrupt.
3628 * @ap: Port associated with this ATA transaction.
3630 * Clear interrupt and error flags in DMA status register.
3632 * May be used as the irq_clear() entry in ata_port_operations.
3635 * spin_lock_irqsave(host_set lock)
3638 void ata_bmdma_irq_clear(struct ata_port *ap)
3640 if (ap->flags & ATA_FLAG_MMIO) {
3641 void __iomem *mmio = ((void __iomem *) ap->ioaddr.bmdma_addr) + ATA_DMA_STATUS;
3642 writeb(readb(mmio), mmio);
3644 unsigned long addr = ap->ioaddr.bmdma_addr + ATA_DMA_STATUS;
3645 outb(inb(addr), addr);
3652 * ata_bmdma_status - Read PCI IDE BMDMA status
3653 * @ap: Port associated with this ATA transaction.
3655 * Read and return BMDMA status register.
3657 * May be used as the bmdma_status() entry in ata_port_operations.
3660 * spin_lock_irqsave(host_set lock)
3663 u8 ata_bmdma_status(struct ata_port *ap)
3666 if (ap->flags & ATA_FLAG_MMIO) {
3667 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
3668 host_stat = readb(mmio + ATA_DMA_STATUS);
3670 host_stat = inb(ap->ioaddr.bmdma_addr + ATA_DMA_STATUS);
3676 * ata_bmdma_stop - Stop PCI IDE BMDMA transfer
3677 * @qc: Command we are ending DMA for
3679 * Clears the ATA_DMA_START flag in the dma control register
3681 * May be used as the bmdma_stop() entry in ata_port_operations.
3684 * spin_lock_irqsave(host_set lock)
3687 void ata_bmdma_stop(struct ata_queued_cmd *qc)
3689 struct ata_port *ap = qc->ap;
3690 if (ap->flags & ATA_FLAG_MMIO) {
3691 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
3693 /* clear start/stop bit */
3694 writeb(readb(mmio + ATA_DMA_CMD) & ~ATA_DMA_START,
3695 mmio + ATA_DMA_CMD);
3697 /* clear start/stop bit */
3698 outb(inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD) & ~ATA_DMA_START,
3699 ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3702 /* one-PIO-cycle guaranteed wait, per spec, for HDMA1:0 transition */
3703 ata_altstatus(ap); /* dummy read */
3707 * ata_host_intr - Handle host interrupt for given (port, task)
3708 * @ap: Port on which interrupt arrived (possibly...)
3709 * @qc: Taskfile currently active in engine
3711 * Handle host interrupt for given queued command. Currently,
3712 * only DMA interrupts are handled. All other commands are
3713 * handled via polling with interrupts disabled (nIEN bit).
3716 * spin_lock_irqsave(host_set lock)
3719 * One if interrupt was handled, zero if not (shared irq).
3722 inline unsigned int ata_host_intr (struct ata_port *ap,
3723 struct ata_queued_cmd *qc)
3725 u8 status, host_stat;
3727 switch (qc->tf.protocol) {
3730 case ATA_PROT_ATAPI_DMA:
3731 case ATA_PROT_ATAPI:
3732 /* check status of DMA engine */
3733 host_stat = ap->ops->bmdma_status(ap);
3734 VPRINTK("ata%u: host_stat 0x%X\n", ap->id, host_stat);
3736 /* if it's not our irq... */
3737 if (!(host_stat & ATA_DMA_INTR))
3740 /* before we do anything else, clear DMA-Start bit */
3741 ap->ops->bmdma_stop(qc);
3745 case ATA_PROT_ATAPI_NODATA:
3746 case ATA_PROT_NODATA:
3747 /* check altstatus */
3748 status = ata_altstatus(ap);
3749 if (status & ATA_BUSY)
3752 /* check main status, clearing INTRQ */
3753 status = ata_chk_status(ap);
3754 if (unlikely(status & ATA_BUSY))
3756 DPRINTK("ata%u: protocol %d (dev_stat 0x%X)\n",
3757 ap->id, qc->tf.protocol, status);
3759 /* ack bmdma irq events */
3760 ap->ops->irq_clear(ap);
3762 /* complete taskfile transaction */
3763 ata_qc_complete(qc, status);
3770 return 1; /* irq handled */
3773 ap->stats.idle_irq++;
3776 if ((ap->stats.idle_irq % 1000) == 0) {
3778 ata_irq_ack(ap, 0); /* debug trap */
3779 printk(KERN_WARNING "ata%d: irq trap\n", ap->id);
3782 return 0; /* irq not handled */
3786 * ata_interrupt - Default ATA host interrupt handler
3787 * @irq: irq line (unused)
3788 * @dev_instance: pointer to our ata_host_set information structure
3791 * Default interrupt handler for PCI IDE devices. Calls
3792 * ata_host_intr() for each port that is not disabled.
3795 * Obtains host_set lock during operation.
3798 * IRQ_NONE or IRQ_HANDLED.
3802 irqreturn_t ata_interrupt (int irq, void *dev_instance, struct pt_regs *regs)
3804 struct ata_host_set *host_set = dev_instance;
3806 unsigned int handled = 0;
3807 unsigned long flags;
3809 /* TODO: make _irqsave conditional on x86 PCI IDE legacy mode */
3810 spin_lock_irqsave(&host_set->lock, flags);
3812 for (i = 0; i < host_set->n_ports; i++) {
3813 struct ata_port *ap;
3815 ap = host_set->ports[i];
3817 !(ap->flags & (ATA_FLAG_PORT_DISABLED | ATA_FLAG_NOINTR))) {
3818 struct ata_queued_cmd *qc;
3820 qc = ata_qc_from_tag(ap, ap->active_tag);
3821 if (qc && (!(qc->tf.ctl & ATA_NIEN)) &&
3822 (qc->flags & ATA_QCFLAG_ACTIVE))
3823 handled |= ata_host_intr(ap, qc);
3827 spin_unlock_irqrestore(&host_set->lock, flags);
3829 return IRQ_RETVAL(handled);
3833 * atapi_packet_task - Write CDB bytes to hardware
3834 * @_data: Port to which ATAPI device is attached.
3836 * When device has indicated its readiness to accept
3837 * a CDB, this function is called. Send the CDB.
3838 * If DMA is to be performed, exit immediately.
3839 * Otherwise, we are in polling mode, so poll
3840 * status under operation succeeds or fails.
3843 * Kernel thread context (may sleep)
3846 static void atapi_packet_task(void *_data)
3848 struct ata_port *ap = _data;
3849 struct ata_queued_cmd *qc;
3852 qc = ata_qc_from_tag(ap, ap->active_tag);
3854 assert(qc->flags & ATA_QCFLAG_ACTIVE);
3856 /* sleep-wait for BSY to clear */
3857 DPRINTK("busy wait\n");
3858 if (ata_busy_sleep(ap, ATA_TMOUT_CDB_QUICK, ATA_TMOUT_CDB))
3861 /* make sure DRQ is set */
3862 status = ata_chk_status(ap);
3863 if ((status & (ATA_BUSY | ATA_DRQ)) != ATA_DRQ)
3867 DPRINTK("send cdb\n");
3868 assert(ap->cdb_len >= 12);
3870 if (qc->tf.protocol == ATA_PROT_ATAPI_DMA ||
3871 qc->tf.protocol == ATA_PROT_ATAPI_NODATA) {
3872 unsigned long flags;
3874 /* Once we're done issuing command and kicking bmdma,
3875 * irq handler takes over. To not lose irq, we need
3876 * to clear NOINTR flag before sending cdb, but
3877 * interrupt handler shouldn't be invoked before we're
3878 * finished. Hence, the following locking.
3880 spin_lock_irqsave(&ap->host_set->lock, flags);
3881 ap->flags &= ~ATA_FLAG_NOINTR;
3882 ata_data_xfer(ap, qc->cdb, ap->cdb_len, 1);
3883 if (qc->tf.protocol == ATA_PROT_ATAPI_DMA)
3884 ap->ops->bmdma_start(qc); /* initiate bmdma */
3885 spin_unlock_irqrestore(&ap->host_set->lock, flags);
3887 ata_data_xfer(ap, qc->cdb, ap->cdb_len, 1);
3889 /* PIO commands are handled by polling */
3890 ap->pio_task_state = PIO_ST;
3891 queue_work(ata_wq, &ap->pio_task);
3897 ata_poll_qc_complete(qc, ATA_ERR);
3902 * ata_port_start - Set port up for dma.
3903 * @ap: Port to initialize
3905 * Called just after data structures for each port are
3906 * initialized. Allocates space for PRD table.
3908 * May be used as the port_start() entry in ata_port_operations.
3913 int ata_port_start (struct ata_port *ap)
3915 struct device *dev = ap->host_set->dev;
3917 ap->prd = dma_alloc_coherent(dev, ATA_PRD_TBL_SZ, &ap->prd_dma, GFP_KERNEL);
3921 DPRINTK("prd alloc, virt %p, dma %llx\n", ap->prd, (unsigned long long) ap->prd_dma);
3928 * ata_port_stop - Undo ata_port_start()
3929 * @ap: Port to shut down
3931 * Frees the PRD table.
3933 * May be used as the port_stop() entry in ata_port_operations.
3938 void ata_port_stop (struct ata_port *ap)
3940 struct device *dev = ap->host_set->dev;
3942 dma_free_coherent(dev, ATA_PRD_TBL_SZ, ap->prd, ap->prd_dma);
3945 void ata_host_stop (struct ata_host_set *host_set)
3947 if (host_set->mmio_base)
3948 iounmap(host_set->mmio_base);
3953 * ata_host_remove - Unregister SCSI host structure with upper layers
3954 * @ap: Port to unregister
3955 * @do_unregister: 1 if we fully unregister, 0 to just stop the port
3960 static void ata_host_remove(struct ata_port *ap, unsigned int do_unregister)
3962 struct Scsi_Host *sh = ap->host;
3967 scsi_remove_host(sh);
3969 ap->ops->port_stop(ap);
3973 * ata_host_init - Initialize an ata_port structure
3974 * @ap: Structure to initialize
3975 * @host: associated SCSI mid-layer structure
3976 * @host_set: Collection of hosts to which @ap belongs
3977 * @ent: Probe information provided by low-level driver
3978 * @port_no: Port number associated with this ata_port
3980 * Initialize a new ata_port structure, and its associated
3984 * Inherited from caller.
3988 static void ata_host_init(struct ata_port *ap, struct Scsi_Host *host,
3989 struct ata_host_set *host_set,
3990 struct ata_probe_ent *ent, unsigned int port_no)
3996 host->max_channel = 1;
3997 host->unique_id = ata_unique_id++;
3998 host->max_cmd_len = 12;
4000 scsi_assign_lock(host, &host_set->lock);
4002 ap->flags = ATA_FLAG_PORT_DISABLED;
4003 ap->id = host->unique_id;
4005 ap->ctl = ATA_DEVCTL_OBS;
4006 ap->host_set = host_set;
4007 ap->port_no = port_no;
4009 ent->legacy_mode ? ent->hard_port_no : port_no;
4010 ap->pio_mask = ent->pio_mask;
4011 ap->mwdma_mask = ent->mwdma_mask;
4012 ap->udma_mask = ent->udma_mask;
4013 ap->flags |= ent->host_flags;
4014 ap->ops = ent->port_ops;
4015 ap->cbl = ATA_CBL_NONE;
4016 ap->active_tag = ATA_TAG_POISON;
4017 ap->last_ctl = 0xFF;
4019 INIT_WORK(&ap->packet_task, atapi_packet_task, ap);
4020 INIT_WORK(&ap->pio_task, ata_pio_task, ap);
4022 for (i = 0; i < ATA_MAX_DEVICES; i++)
4023 ap->device[i].devno = i;
4026 ap->stats.unhandled_irq = 1;
4027 ap->stats.idle_irq = 1;
4030 memcpy(&ap->ioaddr, &ent->port[port_no], sizeof(struct ata_ioports));
4034 * ata_host_add - Attach low-level ATA driver to system
4035 * @ent: Information provided by low-level driver
4036 * @host_set: Collections of ports to which we add
4037 * @port_no: Port number associated with this host
4039 * Attach low-level ATA driver to system.
4042 * PCI/etc. bus probe sem.
4045 * New ata_port on success, for NULL on error.
4049 static struct ata_port * ata_host_add(struct ata_probe_ent *ent,
4050 struct ata_host_set *host_set,
4051 unsigned int port_no)
4053 struct Scsi_Host *host;
4054 struct ata_port *ap;
4058 host = scsi_host_alloc(ent->sht, sizeof(struct ata_port));
4062 ap = (struct ata_port *) &host->hostdata[0];
4064 ata_host_init(ap, host, host_set, ent, port_no);
4066 rc = ap->ops->port_start(ap);
4073 scsi_host_put(host);
4078 * ata_device_add - Register hardware device with ATA and SCSI layers
4079 * @ent: Probe information describing hardware device to be registered
4081 * This function processes the information provided in the probe
4082 * information struct @ent, allocates the necessary ATA and SCSI
4083 * host information structures, initializes them, and registers
4084 * everything with requisite kernel subsystems.
4086 * This function requests irqs, probes the ATA bus, and probes
4090 * PCI/etc. bus probe sem.
4093 * Number of ports registered. Zero on error (no ports registered).
4097 int ata_device_add(struct ata_probe_ent *ent)
4099 unsigned int count = 0, i;
4100 struct device *dev = ent->dev;
4101 struct ata_host_set *host_set;
4104 /* alloc a container for our list of ATA ports (buses) */
4105 host_set = kmalloc(sizeof(struct ata_host_set) +
4106 (ent->n_ports * sizeof(void *)), GFP_KERNEL);
4109 memset(host_set, 0, sizeof(struct ata_host_set) + (ent->n_ports * sizeof(void *)));
4110 spin_lock_init(&host_set->lock);
4112 host_set->dev = dev;
4113 host_set->n_ports = ent->n_ports;
4114 host_set->irq = ent->irq;
4115 host_set->mmio_base = ent->mmio_base;
4116 host_set->private_data = ent->private_data;
4117 host_set->ops = ent->port_ops;
4119 /* register each port bound to this device */
4120 for (i = 0; i < ent->n_ports; i++) {
4121 struct ata_port *ap;
4122 unsigned long xfer_mode_mask;
4124 ap = ata_host_add(ent, host_set, i);
4128 host_set->ports[i] = ap;
4129 xfer_mode_mask =(ap->udma_mask << ATA_SHIFT_UDMA) |
4130 (ap->mwdma_mask << ATA_SHIFT_MWDMA) |
4131 (ap->pio_mask << ATA_SHIFT_PIO);
4133 /* print per-port info to dmesg */
4134 printk(KERN_INFO "ata%u: %cATA max %s cmd 0x%lX ctl 0x%lX "
4135 "bmdma 0x%lX irq %lu\n",
4137 ap->flags & ATA_FLAG_SATA ? 'S' : 'P',
4138 ata_mode_string(xfer_mode_mask),
4139 ap->ioaddr.cmd_addr,
4140 ap->ioaddr.ctl_addr,
4141 ap->ioaddr.bmdma_addr,
4145 host_set->ops->irq_clear(ap);
4154 /* obtain irq, that is shared between channels */
4155 if (request_irq(ent->irq, ent->port_ops->irq_handler, ent->irq_flags,
4156 DRV_NAME, host_set))
4159 /* perform each probe synchronously */
4160 DPRINTK("probe begin\n");
4161 for (i = 0; i < count; i++) {
4162 struct ata_port *ap;
4165 ap = host_set->ports[i];
4167 DPRINTK("ata%u: probe begin\n", ap->id);
4168 rc = ata_bus_probe(ap);
4169 DPRINTK("ata%u: probe end\n", ap->id);
4172 /* FIXME: do something useful here?
4173 * Current libata behavior will
4174 * tear down everything when
4175 * the module is removed
4176 * or the h/w is unplugged.
4180 rc = scsi_add_host(ap->host, dev);
4182 printk(KERN_ERR "ata%u: scsi_add_host failed\n",
4184 /* FIXME: do something useful here */
4185 /* FIXME: handle unconditional calls to
4186 * scsi_scan_host and ata_host_remove, below,
4192 /* probes are done, now scan each port's disk(s) */
4193 DPRINTK("probe begin\n");
4194 for (i = 0; i < count; i++) {
4195 struct ata_port *ap = host_set->ports[i];
4197 scsi_scan_host(ap->host);
4200 dev_set_drvdata(dev, host_set);
4202 VPRINTK("EXIT, returning %u\n", ent->n_ports);
4203 return ent->n_ports; /* success */
4206 for (i = 0; i < count; i++) {
4207 ata_host_remove(host_set->ports[i], 1);
4208 scsi_host_put(host_set->ports[i]->host);
4211 VPRINTK("EXIT, returning 0\n");
4216 * ata_scsi_release - SCSI layer callback hook for host unload
4217 * @host: libata host to be unloaded
4219 * Performs all duties necessary to shut down a libata port...
4220 * Kill port kthread, disable port, and release resources.
4223 * Inherited from SCSI layer.
4229 int ata_scsi_release(struct Scsi_Host *host)
4231 struct ata_port *ap = (struct ata_port *) &host->hostdata[0];
4235 ap->ops->port_disable(ap);
4236 ata_host_remove(ap, 0);
4243 * ata_std_ports - initialize ioaddr with standard port offsets.
4244 * @ioaddr: IO address structure to be initialized
4246 * Utility function which initializes data_addr, error_addr,
4247 * feature_addr, nsect_addr, lbal_addr, lbam_addr, lbah_addr,
4248 * device_addr, status_addr, and command_addr to standard offsets
4249 * relative to cmd_addr.
4251 * Does not set ctl_addr, altstatus_addr, bmdma_addr, or scr_addr.
4254 void ata_std_ports(struct ata_ioports *ioaddr)
4256 ioaddr->data_addr = ioaddr->cmd_addr + ATA_REG_DATA;
4257 ioaddr->error_addr = ioaddr->cmd_addr + ATA_REG_ERR;
4258 ioaddr->feature_addr = ioaddr->cmd_addr + ATA_REG_FEATURE;
4259 ioaddr->nsect_addr = ioaddr->cmd_addr + ATA_REG_NSECT;
4260 ioaddr->lbal_addr = ioaddr->cmd_addr + ATA_REG_LBAL;
4261 ioaddr->lbam_addr = ioaddr->cmd_addr + ATA_REG_LBAM;
4262 ioaddr->lbah_addr = ioaddr->cmd_addr + ATA_REG_LBAH;
4263 ioaddr->device_addr = ioaddr->cmd_addr + ATA_REG_DEVICE;
4264 ioaddr->status_addr = ioaddr->cmd_addr + ATA_REG_STATUS;
4265 ioaddr->command_addr = ioaddr->cmd_addr + ATA_REG_CMD;
4268 static struct ata_probe_ent *
4269 ata_probe_ent_alloc(struct device *dev, struct ata_port_info *port)
4271 struct ata_probe_ent *probe_ent;
4273 probe_ent = kmalloc(sizeof(*probe_ent), GFP_KERNEL);
4275 printk(KERN_ERR DRV_NAME "(%s): out of memory\n",
4276 kobject_name(&(dev->kobj)));
4280 memset(probe_ent, 0, sizeof(*probe_ent));
4282 INIT_LIST_HEAD(&probe_ent->node);
4283 probe_ent->dev = dev;
4285 probe_ent->sht = port->sht;
4286 probe_ent->host_flags = port->host_flags;
4287 probe_ent->pio_mask = port->pio_mask;
4288 probe_ent->mwdma_mask = port->mwdma_mask;
4289 probe_ent->udma_mask = port->udma_mask;
4290 probe_ent->port_ops = port->port_ops;
4299 void ata_pci_host_stop (struct ata_host_set *host_set)
4301 struct pci_dev *pdev = to_pci_dev(host_set->dev);
4303 pci_iounmap(pdev, host_set->mmio_base);
4307 * ata_pci_init_native_mode - Initialize native-mode driver
4308 * @pdev: pci device to be initialized
4309 * @port: array[2] of pointers to port info structures.
4311 * Utility function which allocates and initializes an
4312 * ata_probe_ent structure for a standard dual-port
4313 * PIO-based IDE controller. The returned ata_probe_ent
4314 * structure can be passed to ata_device_add(). The returned
4315 * ata_probe_ent structure should then be freed with kfree().
4318 struct ata_probe_ent *
4319 ata_pci_init_native_mode(struct pci_dev *pdev, struct ata_port_info **port)
4321 struct ata_probe_ent *probe_ent =
4322 ata_probe_ent_alloc(pci_dev_to_dev(pdev), port[0]);
4326 probe_ent->n_ports = 2;
4327 probe_ent->irq = pdev->irq;
4328 probe_ent->irq_flags = SA_SHIRQ;
4330 probe_ent->port[0].cmd_addr = pci_resource_start(pdev, 0);
4331 probe_ent->port[0].altstatus_addr =
4332 probe_ent->port[0].ctl_addr =
4333 pci_resource_start(pdev, 1) | ATA_PCI_CTL_OFS;
4334 probe_ent->port[0].bmdma_addr = pci_resource_start(pdev, 4);
4336 probe_ent->port[1].cmd_addr = pci_resource_start(pdev, 2);
4337 probe_ent->port[1].altstatus_addr =
4338 probe_ent->port[1].ctl_addr =
4339 pci_resource_start(pdev, 3) | ATA_PCI_CTL_OFS;
4340 probe_ent->port[1].bmdma_addr = pci_resource_start(pdev, 4) + 8;
4342 ata_std_ports(&probe_ent->port[0]);
4343 ata_std_ports(&probe_ent->port[1]);
4348 static struct ata_probe_ent *
4349 ata_pci_init_legacy_mode(struct pci_dev *pdev, struct ata_port_info **port,
4350 struct ata_probe_ent **ppe2)
4352 struct ata_probe_ent *probe_ent, *probe_ent2;
4354 probe_ent = ata_probe_ent_alloc(pci_dev_to_dev(pdev), port[0]);
4357 probe_ent2 = ata_probe_ent_alloc(pci_dev_to_dev(pdev), port[1]);
4363 probe_ent->n_ports = 1;
4364 probe_ent->irq = 14;
4366 probe_ent->hard_port_no = 0;
4367 probe_ent->legacy_mode = 1;
4369 probe_ent2->n_ports = 1;
4370 probe_ent2->irq = 15;
4372 probe_ent2->hard_port_no = 1;
4373 probe_ent2->legacy_mode = 1;
4375 probe_ent->port[0].cmd_addr = 0x1f0;
4376 probe_ent->port[0].altstatus_addr =
4377 probe_ent->port[0].ctl_addr = 0x3f6;
4378 probe_ent->port[0].bmdma_addr = pci_resource_start(pdev, 4);
4380 probe_ent2->port[0].cmd_addr = 0x170;
4381 probe_ent2->port[0].altstatus_addr =
4382 probe_ent2->port[0].ctl_addr = 0x376;
4383 probe_ent2->port[0].bmdma_addr = pci_resource_start(pdev, 4)+8;
4385 ata_std_ports(&probe_ent->port[0]);
4386 ata_std_ports(&probe_ent2->port[0]);
4393 * ata_pci_init_one - Initialize/register PCI IDE host controller
4394 * @pdev: Controller to be initialized
4395 * @port_info: Information from low-level host driver
4396 * @n_ports: Number of ports attached to host controller
4398 * This is a helper function which can be called from a driver's
4399 * xxx_init_one() probe function if the hardware uses traditional
4400 * IDE taskfile registers.
4402 * This function calls pci_enable_device(), reserves its register
4403 * regions, sets the dma mask, enables bus master mode, and calls
4407 * Inherited from PCI layer (may sleep).
4410 * Zero on success, negative on errno-based value on error.
4414 int ata_pci_init_one (struct pci_dev *pdev, struct ata_port_info **port_info,
4415 unsigned int n_ports)
4417 struct ata_probe_ent *probe_ent, *probe_ent2 = NULL;
4418 struct ata_port_info *port[2];
4420 unsigned int legacy_mode = 0;
4421 int disable_dev_on_err = 1;
4426 port[0] = port_info[0];
4428 port[1] = port_info[1];
4432 if ((port[0]->host_flags & ATA_FLAG_NO_LEGACY) == 0
4433 && (pdev->class >> 8) == PCI_CLASS_STORAGE_IDE) {
4434 /* TODO: support transitioning to native mode? */
4435 pci_read_config_byte(pdev, PCI_CLASS_PROG, &tmp8);
4436 mask = (1 << 2) | (1 << 0);
4437 if ((tmp8 & mask) != mask)
4438 legacy_mode = (1 << 3);
4442 if ((!legacy_mode) && (n_ports > 1)) {
4443 printk(KERN_ERR "ata: BUG: native mode, n_ports > 1\n");
4447 rc = pci_enable_device(pdev);
4451 rc = pci_request_regions(pdev, DRV_NAME);
4453 disable_dev_on_err = 0;
4458 if (!request_region(0x1f0, 8, "libata")) {
4459 struct resource *conflict, res;
4461 res.end = 0x1f0 + 8 - 1;
4462 conflict = ____request_resource(&ioport_resource, &res);
4463 if (!strcmp(conflict->name, "libata"))
4464 legacy_mode |= (1 << 0);
4466 disable_dev_on_err = 0;
4467 printk(KERN_WARNING "ata: 0x1f0 IDE port busy\n");
4470 legacy_mode |= (1 << 0);
4472 if (!request_region(0x170, 8, "libata")) {
4473 struct resource *conflict, res;
4475 res.end = 0x170 + 8 - 1;
4476 conflict = ____request_resource(&ioport_resource, &res);
4477 if (!strcmp(conflict->name, "libata"))
4478 legacy_mode |= (1 << 1);
4480 disable_dev_on_err = 0;
4481 printk(KERN_WARNING "ata: 0x170 IDE port busy\n");
4484 legacy_mode |= (1 << 1);
4487 /* we have legacy mode, but all ports are unavailable */
4488 if (legacy_mode == (1 << 3)) {
4490 goto err_out_regions;
4493 rc = pci_set_dma_mask(pdev, ATA_DMA_MASK);
4495 goto err_out_regions;
4496 rc = pci_set_consistent_dma_mask(pdev, ATA_DMA_MASK);
4498 goto err_out_regions;
4501 probe_ent = ata_pci_init_legacy_mode(pdev, port, &probe_ent2);
4503 probe_ent = ata_pci_init_native_mode(pdev, port);
4506 goto err_out_regions;
4509 pci_set_master(pdev);
4511 /* FIXME: check ata_device_add return */
4513 if (legacy_mode & (1 << 0))
4514 ata_device_add(probe_ent);
4515 if (legacy_mode & (1 << 1))
4516 ata_device_add(probe_ent2);
4518 ata_device_add(probe_ent);
4526 if (legacy_mode & (1 << 0))
4527 release_region(0x1f0, 8);
4528 if (legacy_mode & (1 << 1))
4529 release_region(0x170, 8);
4530 pci_release_regions(pdev);
4532 if (disable_dev_on_err)
4533 pci_disable_device(pdev);
4538 * ata_pci_remove_one - PCI layer callback for device removal
4539 * @pdev: PCI device that was removed
4541 * PCI layer indicates to libata via this hook that
4542 * hot-unplug or module unload event has occured.
4543 * Handle this by unregistering all objects associated
4544 * with this PCI device. Free those objects. Then finally
4545 * release PCI resources and disable device.
4548 * Inherited from PCI layer (may sleep).
4551 void ata_pci_remove_one (struct pci_dev *pdev)
4553 struct device *dev = pci_dev_to_dev(pdev);
4554 struct ata_host_set *host_set = dev_get_drvdata(dev);
4555 struct ata_port *ap;
4558 for (i = 0; i < host_set->n_ports; i++) {
4559 ap = host_set->ports[i];
4561 scsi_remove_host(ap->host);
4564 free_irq(host_set->irq, host_set);
4566 for (i = 0; i < host_set->n_ports; i++) {
4567 ap = host_set->ports[i];
4569 ata_scsi_release(ap->host);
4571 if ((ap->flags & ATA_FLAG_NO_LEGACY) == 0) {
4572 struct ata_ioports *ioaddr = &ap->ioaddr;
4574 if (ioaddr->cmd_addr == 0x1f0)
4575 release_region(0x1f0, 8);
4576 else if (ioaddr->cmd_addr == 0x170)
4577 release_region(0x170, 8);
4580 scsi_host_put(ap->host);
4583 if (host_set->ops->host_stop)
4584 host_set->ops->host_stop(host_set);
4588 pci_release_regions(pdev);
4589 pci_disable_device(pdev);
4590 dev_set_drvdata(dev, NULL);
4593 /* move to PCI subsystem */
4594 int pci_test_config_bits(struct pci_dev *pdev, struct pci_bits *bits)
4596 unsigned long tmp = 0;
4598 switch (bits->width) {
4601 pci_read_config_byte(pdev, bits->reg, &tmp8);
4607 pci_read_config_word(pdev, bits->reg, &tmp16);
4613 pci_read_config_dword(pdev, bits->reg, &tmp32);
4624 return (tmp == bits->val) ? 1 : 0;
4626 #endif /* CONFIG_PCI */
4629 static int __init ata_init(void)
4631 ata_wq = create_workqueue("ata");
4635 printk(KERN_DEBUG "libata version " DRV_VERSION " loaded.\n");
4639 static void __exit ata_exit(void)
4641 destroy_workqueue(ata_wq);
4644 module_init(ata_init);
4645 module_exit(ata_exit);
4648 * libata is essentially a library of internal helper functions for
4649 * low-level ATA host controller drivers. As such, the API/ABI is
4650 * likely to change as new drivers are added and updated.
4651 * Do not depend on ABI/API stability.
4654 EXPORT_SYMBOL_GPL(ata_std_bios_param);
4655 EXPORT_SYMBOL_GPL(ata_std_ports);
4656 EXPORT_SYMBOL_GPL(ata_device_add);
4657 EXPORT_SYMBOL_GPL(ata_sg_init);
4658 EXPORT_SYMBOL_GPL(ata_sg_init_one);
4659 EXPORT_SYMBOL_GPL(ata_qc_complete);
4660 EXPORT_SYMBOL_GPL(ata_qc_issue_prot);
4661 EXPORT_SYMBOL_GPL(ata_eng_timeout);
4662 EXPORT_SYMBOL_GPL(ata_tf_load);
4663 EXPORT_SYMBOL_GPL(ata_tf_read);
4664 EXPORT_SYMBOL_GPL(ata_noop_dev_select);
4665 EXPORT_SYMBOL_GPL(ata_std_dev_select);
4666 EXPORT_SYMBOL_GPL(ata_tf_to_fis);
4667 EXPORT_SYMBOL_GPL(ata_tf_from_fis);
4668 EXPORT_SYMBOL_GPL(ata_check_status);
4669 EXPORT_SYMBOL_GPL(ata_altstatus);
4670 EXPORT_SYMBOL_GPL(ata_chk_err);
4671 EXPORT_SYMBOL_GPL(ata_exec_command);
4672 EXPORT_SYMBOL_GPL(ata_port_start);
4673 EXPORT_SYMBOL_GPL(ata_port_stop);
4674 EXPORT_SYMBOL_GPL(ata_host_stop);
4675 EXPORT_SYMBOL_GPL(ata_interrupt);
4676 EXPORT_SYMBOL_GPL(ata_qc_prep);
4677 EXPORT_SYMBOL_GPL(ata_bmdma_setup);
4678 EXPORT_SYMBOL_GPL(ata_bmdma_start);
4679 EXPORT_SYMBOL_GPL(ata_bmdma_irq_clear);
4680 EXPORT_SYMBOL_GPL(ata_bmdma_status);
4681 EXPORT_SYMBOL_GPL(ata_bmdma_stop);
4682 EXPORT_SYMBOL_GPL(ata_port_probe);
4683 EXPORT_SYMBOL_GPL(sata_phy_reset);
4684 EXPORT_SYMBOL_GPL(__sata_phy_reset);
4685 EXPORT_SYMBOL_GPL(ata_bus_reset);
4686 EXPORT_SYMBOL_GPL(ata_port_disable);
4687 EXPORT_SYMBOL_GPL(ata_scsi_ioctl);
4688 EXPORT_SYMBOL_GPL(ata_scsi_queuecmd);
4689 EXPORT_SYMBOL_GPL(ata_scsi_error);
4690 EXPORT_SYMBOL_GPL(ata_scsi_slave_config);
4691 EXPORT_SYMBOL_GPL(ata_scsi_release);
4692 EXPORT_SYMBOL_GPL(ata_host_intr);
4693 EXPORT_SYMBOL_GPL(ata_dev_classify);
4694 EXPORT_SYMBOL_GPL(ata_dev_id_string);
4695 EXPORT_SYMBOL_GPL(ata_dev_config);
4696 EXPORT_SYMBOL_GPL(ata_scsi_simulate);
4699 EXPORT_SYMBOL_GPL(pci_test_config_bits);
4700 EXPORT_SYMBOL_GPL(ata_pci_host_stop);
4701 EXPORT_SYMBOL_GPL(ata_pci_init_native_mode);
4702 EXPORT_SYMBOL_GPL(ata_pci_init_one);
4703 EXPORT_SYMBOL_GPL(ata_pci_remove_one);
4704 #endif /* CONFIG_PCI */