2 libata-core.c - helper library for ATA
4 Copyright 2003-2004 Red Hat, Inc. All rights reserved.
5 Copyright 2003-2004 Jeff Garzik
7 The contents of this file are subject to the Open
8 Software License version 1.1 that can be found at
9 http://www.opensource.org/licenses/osl-1.1.txt and is included herein
12 Alternatively, the contents of this file may be used under the terms
13 of the GNU General Public License version 2 (the "GPL") as distributed
14 in the kernel source COPYING file, in which case the provisions of
15 the GPL are applicable instead of the above. If you wish to allow
16 the use of your version of this file only under the terms of the
17 GPL and not to allow others to use your version of this file under
18 the OSL, indicate your decision by deleting the provisions above and
19 replace them with the notice and other provisions required by the GPL.
20 If you do not delete the provisions above, a recipient may use your
21 version of this file under either the OSL or the GPL.
25 #include <linux/config.h>
26 #include <linux/kernel.h>
27 #include <linux/module.h>
28 #include <linux/pci.h>
29 #include <linux/init.h>
30 #include <linux/list.h>
32 #include <linux/highmem.h>
33 #include <linux/spinlock.h>
34 #include <linux/blkdev.h>
35 #include <linux/delay.h>
36 #include <linux/timer.h>
37 #include <linux/interrupt.h>
38 #include <linux/completion.h>
39 #include <linux/suspend.h>
40 #include <linux/workqueue.h>
41 #include <scsi/scsi.h>
43 #include "scsi_priv.h"
44 #include <scsi/scsi_host.h>
45 #include <linux/libata.h>
47 #include <asm/semaphore.h>
48 #include <asm/byteorder.h>
52 static unsigned int ata_busy_sleep (struct ata_port *ap,
53 unsigned long tmout_pat,
55 static void ata_set_mode(struct ata_port *ap);
56 static void ata_dev_set_xfermode(struct ata_port *ap, struct ata_device *dev);
57 static unsigned int ata_get_mode_mask(struct ata_port *ap, int shift);
58 static int fgb(u32 bitmap);
59 static int ata_choose_xfer_mode(struct ata_port *ap,
61 unsigned int *xfer_shift_out);
62 static int ata_qc_complete_noop(struct ata_queued_cmd *qc, u8 drv_stat);
63 static void __ata_qc_complete(struct ata_queued_cmd *qc);
65 static unsigned int ata_unique_id = 1;
66 static struct workqueue_struct *ata_wq;
68 MODULE_AUTHOR("Jeff Garzik");
69 MODULE_DESCRIPTION("Library module for ATA devices");
70 MODULE_LICENSE("GPL");
71 MODULE_VERSION(DRV_VERSION);
74 * ata_tf_load - send taskfile registers to host controller
75 * @ap: Port to which output is sent
76 * @tf: ATA taskfile register set
78 * Outputs ATA taskfile to standard ATA host controller.
81 * Inherited from caller.
84 static void ata_tf_load_pio(struct ata_port *ap, struct ata_taskfile *tf)
86 struct ata_ioports *ioaddr = &ap->ioaddr;
87 unsigned int is_addr = tf->flags & ATA_TFLAG_ISADDR;
89 if (tf->ctl != ap->last_ctl) {
90 outb(tf->ctl, ioaddr->ctl_addr);
91 ap->last_ctl = tf->ctl;
95 if (is_addr && (tf->flags & ATA_TFLAG_LBA48)) {
96 outb(tf->hob_feature, ioaddr->feature_addr);
97 outb(tf->hob_nsect, ioaddr->nsect_addr);
98 outb(tf->hob_lbal, ioaddr->lbal_addr);
99 outb(tf->hob_lbam, ioaddr->lbam_addr);
100 outb(tf->hob_lbah, ioaddr->lbah_addr);
101 VPRINTK("hob: feat 0x%X nsect 0x%X, lba 0x%X 0x%X 0x%X\n",
110 outb(tf->feature, ioaddr->feature_addr);
111 outb(tf->nsect, ioaddr->nsect_addr);
112 outb(tf->lbal, ioaddr->lbal_addr);
113 outb(tf->lbam, ioaddr->lbam_addr);
114 outb(tf->lbah, ioaddr->lbah_addr);
115 VPRINTK("feat 0x%X nsect 0x%X lba 0x%X 0x%X 0x%X\n",
123 if (tf->flags & ATA_TFLAG_DEVICE) {
124 outb(tf->device, ioaddr->device_addr);
125 VPRINTK("device 0x%X\n", tf->device);
132 * ata_tf_load_mmio - send taskfile registers to host controller
133 * @ap: Port to which output is sent
134 * @tf: ATA taskfile register set
136 * Outputs ATA taskfile to standard ATA host controller using MMIO.
139 * Inherited from caller.
142 static void ata_tf_load_mmio(struct ata_port *ap, struct ata_taskfile *tf)
144 struct ata_ioports *ioaddr = &ap->ioaddr;
145 unsigned int is_addr = tf->flags & ATA_TFLAG_ISADDR;
147 if (tf->ctl != ap->last_ctl) {
148 writeb(tf->ctl, (void __iomem *) ap->ioaddr.ctl_addr);
149 ap->last_ctl = tf->ctl;
153 if (is_addr && (tf->flags & ATA_TFLAG_LBA48)) {
154 writeb(tf->hob_feature, (void __iomem *) ioaddr->feature_addr);
155 writeb(tf->hob_nsect, (void __iomem *) ioaddr->nsect_addr);
156 writeb(tf->hob_lbal, (void __iomem *) ioaddr->lbal_addr);
157 writeb(tf->hob_lbam, (void __iomem *) ioaddr->lbam_addr);
158 writeb(tf->hob_lbah, (void __iomem *) ioaddr->lbah_addr);
159 VPRINTK("hob: feat 0x%X nsect 0x%X, lba 0x%X 0x%X 0x%X\n",
168 writeb(tf->feature, (void __iomem *) ioaddr->feature_addr);
169 writeb(tf->nsect, (void __iomem *) ioaddr->nsect_addr);
170 writeb(tf->lbal, (void __iomem *) ioaddr->lbal_addr);
171 writeb(tf->lbam, (void __iomem *) ioaddr->lbam_addr);
172 writeb(tf->lbah, (void __iomem *) ioaddr->lbah_addr);
173 VPRINTK("feat 0x%X nsect 0x%X lba 0x%X 0x%X 0x%X\n",
181 if (tf->flags & ATA_TFLAG_DEVICE) {
182 writeb(tf->device, (void __iomem *) ioaddr->device_addr);
183 VPRINTK("device 0x%X\n", tf->device);
191 * ata_tf_load - send taskfile registers to host controller
192 * @ap: Port to which output is sent
193 * @tf: ATA taskfile register set
195 * Outputs ATA taskfile to standard ATA host controller using MMIO
196 * or PIO as indicated by the ATA_FLAG_MMIO flag.
197 * Writes the control, feature, nsect, lbal, lbam, and lbah registers.
198 * Optionally (ATA_TFLAG_LBA48) writes hob_feature, hob_nsect,
199 * hob_lbal, hob_lbam, and hob_lbah.
201 * This function waits for idle (!BUSY and !DRQ) after writing
202 * registers. If the control register has a new value, this
203 * function also waits for idle after writing control and before
204 * writing the remaining registers.
206 * May be used as the tf_load() entry in ata_port_operations.
209 * Inherited from caller.
211 void ata_tf_load(struct ata_port *ap, struct ata_taskfile *tf)
213 if (ap->flags & ATA_FLAG_MMIO)
214 ata_tf_load_mmio(ap, tf);
216 ata_tf_load_pio(ap, tf);
220 * ata_exec_command_pio - issue ATA command to host controller
221 * @ap: port to which command is being issued
222 * @tf: ATA taskfile register set
224 * Issues PIO write to ATA command register, with proper
225 * synchronization with interrupt handler / other threads.
228 * spin_lock_irqsave(host_set lock)
231 static void ata_exec_command_pio(struct ata_port *ap, struct ata_taskfile *tf)
233 DPRINTK("ata%u: cmd 0x%X\n", ap->id, tf->command);
235 outb(tf->command, ap->ioaddr.command_addr);
241 * ata_exec_command_mmio - issue ATA command to host controller
242 * @ap: port to which command is being issued
243 * @tf: ATA taskfile register set
245 * Issues MMIO write to ATA command register, with proper
246 * synchronization with interrupt handler / other threads.
249 * spin_lock_irqsave(host_set lock)
252 static void ata_exec_command_mmio(struct ata_port *ap, struct ata_taskfile *tf)
254 DPRINTK("ata%u: cmd 0x%X\n", ap->id, tf->command);
256 writeb(tf->command, (void __iomem *) ap->ioaddr.command_addr);
262 * ata_exec_command - issue ATA command to host controller
263 * @ap: port to which command is being issued
264 * @tf: ATA taskfile register set
266 * Issues PIO/MMIO write to ATA command register, with proper
267 * synchronization with interrupt handler / other threads.
270 * spin_lock_irqsave(host_set lock)
272 void ata_exec_command(struct ata_port *ap, struct ata_taskfile *tf)
274 if (ap->flags & ATA_FLAG_MMIO)
275 ata_exec_command_mmio(ap, tf);
277 ata_exec_command_pio(ap, tf);
281 * ata_exec - issue ATA command to host controller
282 * @ap: port to which command is being issued
283 * @tf: ATA taskfile register set
285 * Issues PIO/MMIO write to ATA command register, with proper
286 * synchronization with interrupt handler / other threads.
289 * Obtains host_set lock.
292 static inline void ata_exec(struct ata_port *ap, struct ata_taskfile *tf)
296 DPRINTK("ata%u: cmd 0x%X\n", ap->id, tf->command);
297 spin_lock_irqsave(&ap->host_set->lock, flags);
298 ap->ops->exec_command(ap, tf);
299 spin_unlock_irqrestore(&ap->host_set->lock, flags);
303 * ata_tf_to_host - issue ATA taskfile to host controller
304 * @ap: port to which command is being issued
305 * @tf: ATA taskfile register set
307 * Issues ATA taskfile register set to ATA host controller,
308 * with proper synchronization with interrupt handler and
312 * Obtains host_set lock.
315 static void ata_tf_to_host(struct ata_port *ap, struct ata_taskfile *tf)
317 ap->ops->tf_load(ap, tf);
323 * ata_tf_to_host_nolock - issue ATA taskfile to host controller
324 * @ap: port to which command is being issued
325 * @tf: ATA taskfile register set
327 * Issues ATA taskfile register set to ATA host controller,
328 * with proper synchronization with interrupt handler and
332 * spin_lock_irqsave(host_set lock)
335 void ata_tf_to_host_nolock(struct ata_port *ap, struct ata_taskfile *tf)
337 ap->ops->tf_load(ap, tf);
338 ap->ops->exec_command(ap, tf);
342 * ata_tf_read_pio - input device's ATA taskfile shadow registers
343 * @ap: Port from which input is read
344 * @tf: ATA taskfile register set for storing input
346 * Reads ATA taskfile registers for currently-selected device
350 * Inherited from caller.
353 static void ata_tf_read_pio(struct ata_port *ap, struct ata_taskfile *tf)
355 struct ata_ioports *ioaddr = &ap->ioaddr;
357 tf->nsect = inb(ioaddr->nsect_addr);
358 tf->lbal = inb(ioaddr->lbal_addr);
359 tf->lbam = inb(ioaddr->lbam_addr);
360 tf->lbah = inb(ioaddr->lbah_addr);
361 tf->device = inb(ioaddr->device_addr);
363 if (tf->flags & ATA_TFLAG_LBA48) {
364 outb(tf->ctl | ATA_HOB, ioaddr->ctl_addr);
365 tf->hob_feature = inb(ioaddr->error_addr);
366 tf->hob_nsect = inb(ioaddr->nsect_addr);
367 tf->hob_lbal = inb(ioaddr->lbal_addr);
368 tf->hob_lbam = inb(ioaddr->lbam_addr);
369 tf->hob_lbah = inb(ioaddr->lbah_addr);
374 * ata_tf_read_mmio - input device's ATA taskfile shadow registers
375 * @ap: Port from which input is read
376 * @tf: ATA taskfile register set for storing input
378 * Reads ATA taskfile registers for currently-selected device
382 * Inherited from caller.
385 static void ata_tf_read_mmio(struct ata_port *ap, struct ata_taskfile *tf)
387 struct ata_ioports *ioaddr = &ap->ioaddr;
389 tf->nsect = readb((void __iomem *)ioaddr->nsect_addr);
390 tf->lbal = readb((void __iomem *)ioaddr->lbal_addr);
391 tf->lbam = readb((void __iomem *)ioaddr->lbam_addr);
392 tf->lbah = readb((void __iomem *)ioaddr->lbah_addr);
393 tf->device = readb((void __iomem *)ioaddr->device_addr);
395 if (tf->flags & ATA_TFLAG_LBA48) {
396 writeb(tf->ctl | ATA_HOB, (void __iomem *) ap->ioaddr.ctl_addr);
397 tf->hob_feature = readb((void __iomem *)ioaddr->error_addr);
398 tf->hob_nsect = readb((void __iomem *)ioaddr->nsect_addr);
399 tf->hob_lbal = readb((void __iomem *)ioaddr->lbal_addr);
400 tf->hob_lbam = readb((void __iomem *)ioaddr->lbam_addr);
401 tf->hob_lbah = readb((void __iomem *)ioaddr->lbah_addr);
407 * ata_tf_read - input device's ATA taskfile shadow registers
408 * @ap: Port from which input is read
409 * @tf: ATA taskfile register set for storing input
411 * Reads ATA taskfile registers for currently-selected device
414 * Reads nsect, lbal, lbam, lbah, and device. If ATA_TFLAG_LBA48
415 * is set, also reads the hob registers.
417 * May be used as the tf_read() entry in ata_port_operations.
420 * Inherited from caller.
422 void ata_tf_read(struct ata_port *ap, struct ata_taskfile *tf)
424 if (ap->flags & ATA_FLAG_MMIO)
425 ata_tf_read_mmio(ap, tf);
427 ata_tf_read_pio(ap, tf);
431 * ata_check_status_pio - Read device status reg & clear interrupt
432 * @ap: port where the device is
434 * Reads ATA taskfile status register for currently-selected device
435 * and return its value. This also clears pending interrupts
439 * Inherited from caller.
441 static u8 ata_check_status_pio(struct ata_port *ap)
443 return inb(ap->ioaddr.status_addr);
447 * ata_check_status_mmio - Read device status reg & clear interrupt
448 * @ap: port where the device is
450 * Reads ATA taskfile status register for currently-selected device
451 * via MMIO and return its value. This also clears pending interrupts
455 * Inherited from caller.
457 static u8 ata_check_status_mmio(struct ata_port *ap)
459 return readb((void __iomem *) ap->ioaddr.status_addr);
464 * ata_check_status - Read device status reg & clear interrupt
465 * @ap: port where the device is
467 * Reads ATA taskfile status register for currently-selected device
468 * and return its value. This also clears pending interrupts
471 * May be used as the check_status() entry in ata_port_operations.
474 * Inherited from caller.
476 u8 ata_check_status(struct ata_port *ap)
478 if (ap->flags & ATA_FLAG_MMIO)
479 return ata_check_status_mmio(ap);
480 return ata_check_status_pio(ap);
485 * ata_altstatus - Read device alternate status reg
486 * @ap: port where the device is
488 * Reads ATA taskfile alternate status register for
489 * currently-selected device and return its value.
491 * Note: may NOT be used as the check_altstatus() entry in
492 * ata_port_operations.
495 * Inherited from caller.
497 u8 ata_altstatus(struct ata_port *ap)
499 if (ap->ops->check_altstatus)
500 return ap->ops->check_altstatus(ap);
502 if (ap->flags & ATA_FLAG_MMIO)
503 return readb((void __iomem *)ap->ioaddr.altstatus_addr);
504 return inb(ap->ioaddr.altstatus_addr);
509 * ata_chk_err - Read device error reg
510 * @ap: port where the device is
512 * Reads ATA taskfile error register for
513 * currently-selected device and return its value.
515 * Note: may NOT be used as the check_err() entry in
516 * ata_port_operations.
519 * Inherited from caller.
521 u8 ata_chk_err(struct ata_port *ap)
523 if (ap->ops->check_err)
524 return ap->ops->check_err(ap);
526 if (ap->flags & ATA_FLAG_MMIO) {
527 return readb((void __iomem *) ap->ioaddr.error_addr);
529 return inb(ap->ioaddr.error_addr);
533 * ata_tf_to_fis - Convert ATA taskfile to SATA FIS structure
534 * @tf: Taskfile to convert
535 * @fis: Buffer into which data will output
536 * @pmp: Port multiplier port
538 * Converts a standard ATA taskfile to a Serial ATA
539 * FIS structure (Register - Host to Device).
542 * Inherited from caller.
545 void ata_tf_to_fis(struct ata_taskfile *tf, u8 *fis, u8 pmp)
547 fis[0] = 0x27; /* Register - Host to Device FIS */
548 fis[1] = (pmp & 0xf) | (1 << 7); /* Port multiplier number,
549 bit 7 indicates Command FIS */
550 fis[2] = tf->command;
551 fis[3] = tf->feature;
558 fis[8] = tf->hob_lbal;
559 fis[9] = tf->hob_lbam;
560 fis[10] = tf->hob_lbah;
561 fis[11] = tf->hob_feature;
564 fis[13] = tf->hob_nsect;
575 * ata_tf_from_fis - Convert SATA FIS to ATA taskfile
576 * @fis: Buffer from which data will be input
577 * @tf: Taskfile to output
579 * Converts a standard ATA taskfile to a Serial ATA
580 * FIS structure (Register - Host to Device).
583 * Inherited from caller.
586 void ata_tf_from_fis(u8 *fis, struct ata_taskfile *tf)
588 tf->command = fis[2]; /* status */
589 tf->feature = fis[3]; /* error */
596 tf->hob_lbal = fis[8];
597 tf->hob_lbam = fis[9];
598 tf->hob_lbah = fis[10];
601 tf->hob_nsect = fis[13];
605 * ata_prot_to_cmd - determine which read/write opcodes to use
606 * @protocol: ATA_PROT_xxx taskfile protocol
607 * @lba48: true is lba48 is present
609 * Given necessary input, determine which read/write commands
610 * to use to transfer data.
615 static int ata_prot_to_cmd(int protocol, int lba48)
617 int rcmd = 0, wcmd = 0;
622 rcmd = ATA_CMD_PIO_READ_EXT;
623 wcmd = ATA_CMD_PIO_WRITE_EXT;
625 rcmd = ATA_CMD_PIO_READ;
626 wcmd = ATA_CMD_PIO_WRITE;
632 rcmd = ATA_CMD_READ_EXT;
633 wcmd = ATA_CMD_WRITE_EXT;
636 wcmd = ATA_CMD_WRITE;
644 return rcmd | (wcmd << 8);
648 * ata_dev_set_protocol - set taskfile protocol and r/w commands
649 * @dev: device to examine and configure
651 * Examine the device configuration, after we have
652 * read the identify-device page and configured the
653 * data transfer mode. Set internal state related to
654 * the ATA taskfile protocol (pio, pio mult, dma, etc.)
655 * and calculate the proper read/write commands to use.
660 static void ata_dev_set_protocol(struct ata_device *dev)
662 int pio = (dev->flags & ATA_DFLAG_PIO);
663 int lba48 = (dev->flags & ATA_DFLAG_LBA48);
667 proto = dev->xfer_protocol = ATA_PROT_PIO;
669 proto = dev->xfer_protocol = ATA_PROT_DMA;
671 cmd = ata_prot_to_cmd(proto, lba48);
675 dev->read_cmd = cmd & 0xff;
676 dev->write_cmd = (cmd >> 8) & 0xff;
679 static const char * xfer_mode_str[] = {
699 * ata_udma_string - convert UDMA bit offset to string
700 * @mask: mask of bits supported; only highest bit counts.
702 * Determine string which represents the highest speed
703 * (highest bit in @udma_mask).
709 * Constant C string representing highest speed listed in
710 * @udma_mask, or the constant C string "<n/a>".
713 static const char *ata_mode_string(unsigned int mask)
717 for (i = 7; i >= 0; i--)
720 for (i = ATA_SHIFT_MWDMA + 2; i >= ATA_SHIFT_MWDMA; i--)
723 for (i = ATA_SHIFT_PIO + 4; i >= ATA_SHIFT_PIO; i--)
730 return xfer_mode_str[i];
734 * ata_pio_devchk - PATA device presence detection
735 * @ap: ATA channel to examine
736 * @device: Device to examine (starting at zero)
738 * This technique was originally described in
739 * Hale Landis's ATADRVR (www.ata-atapi.com), and
740 * later found its way into the ATA/ATAPI spec.
742 * Write a pattern to the ATA shadow registers,
743 * and if a device is present, it will respond by
744 * correctly storing and echoing back the
745 * ATA shadow register contents.
751 static unsigned int ata_pio_devchk(struct ata_port *ap,
754 struct ata_ioports *ioaddr = &ap->ioaddr;
757 ap->ops->dev_select(ap, device);
759 outb(0x55, ioaddr->nsect_addr);
760 outb(0xaa, ioaddr->lbal_addr);
762 outb(0xaa, ioaddr->nsect_addr);
763 outb(0x55, ioaddr->lbal_addr);
765 outb(0x55, ioaddr->nsect_addr);
766 outb(0xaa, ioaddr->lbal_addr);
768 nsect = inb(ioaddr->nsect_addr);
769 lbal = inb(ioaddr->lbal_addr);
771 if ((nsect == 0x55) && (lbal == 0xaa))
772 return 1; /* we found a device */
774 return 0; /* nothing found */
778 * ata_mmio_devchk - PATA device presence detection
779 * @ap: ATA channel to examine
780 * @device: Device to examine (starting at zero)
782 * This technique was originally described in
783 * Hale Landis's ATADRVR (www.ata-atapi.com), and
784 * later found its way into the ATA/ATAPI spec.
786 * Write a pattern to the ATA shadow registers,
787 * and if a device is present, it will respond by
788 * correctly storing and echoing back the
789 * ATA shadow register contents.
795 static unsigned int ata_mmio_devchk(struct ata_port *ap,
798 struct ata_ioports *ioaddr = &ap->ioaddr;
801 ap->ops->dev_select(ap, device);
803 writeb(0x55, (void __iomem *) ioaddr->nsect_addr);
804 writeb(0xaa, (void __iomem *) ioaddr->lbal_addr);
806 writeb(0xaa, (void __iomem *) ioaddr->nsect_addr);
807 writeb(0x55, (void __iomem *) ioaddr->lbal_addr);
809 writeb(0x55, (void __iomem *) ioaddr->nsect_addr);
810 writeb(0xaa, (void __iomem *) ioaddr->lbal_addr);
812 nsect = readb((void __iomem *) ioaddr->nsect_addr);
813 lbal = readb((void __iomem *) ioaddr->lbal_addr);
815 if ((nsect == 0x55) && (lbal == 0xaa))
816 return 1; /* we found a device */
818 return 0; /* nothing found */
822 * ata_devchk - PATA device presence detection
823 * @ap: ATA channel to examine
824 * @device: Device to examine (starting at zero)
826 * Dispatch ATA device presence detection, depending
827 * on whether we are using PIO or MMIO to talk to the
828 * ATA shadow registers.
834 static unsigned int ata_devchk(struct ata_port *ap,
837 if (ap->flags & ATA_FLAG_MMIO)
838 return ata_mmio_devchk(ap, device);
839 return ata_pio_devchk(ap, device);
843 * ata_dev_classify - determine device type based on ATA-spec signature
844 * @tf: ATA taskfile register set for device to be identified
846 * Determine from taskfile register contents whether a device is
847 * ATA or ATAPI, as per "Signature and persistence" section
848 * of ATA/PI spec (volume 1, sect 5.14).
854 * Device type, %ATA_DEV_ATA, %ATA_DEV_ATAPI, or %ATA_DEV_UNKNOWN
855 * the event of failure.
858 unsigned int ata_dev_classify(struct ata_taskfile *tf)
860 /* Apple's open source Darwin code hints that some devices only
861 * put a proper signature into the LBA mid/high registers,
862 * So, we only check those. It's sufficient for uniqueness.
865 if (((tf->lbam == 0) && (tf->lbah == 0)) ||
866 ((tf->lbam == 0x3c) && (tf->lbah == 0xc3))) {
867 DPRINTK("found ATA device by sig\n");
871 if (((tf->lbam == 0x14) && (tf->lbah == 0xeb)) ||
872 ((tf->lbam == 0x69) && (tf->lbah == 0x96))) {
873 DPRINTK("found ATAPI device by sig\n");
874 return ATA_DEV_ATAPI;
877 DPRINTK("unknown device\n");
878 return ATA_DEV_UNKNOWN;
882 * ata_dev_try_classify - Parse returned ATA device signature
883 * @ap: ATA channel to examine
884 * @device: Device to examine (starting at zero)
886 * After an event -- SRST, E.D.D., or SATA COMRESET -- occurs,
887 * an ATA/ATAPI-defined set of values is placed in the ATA
888 * shadow registers, indicating the results of device detection
891 * Select the ATA device, and read the values from the ATA shadow
892 * registers. Then parse according to the Error register value,
893 * and the spec-defined values examined by ata_dev_classify().
899 static u8 ata_dev_try_classify(struct ata_port *ap, unsigned int device)
901 struct ata_device *dev = &ap->device[device];
902 struct ata_taskfile tf;
906 ap->ops->dev_select(ap, device);
908 memset(&tf, 0, sizeof(tf));
910 err = ata_chk_err(ap);
911 ap->ops->tf_read(ap, &tf);
913 dev->class = ATA_DEV_NONE;
915 /* see if device passed diags */
918 else if ((device == 0) && (err == 0x81))
923 /* determine if device if ATA or ATAPI */
924 class = ata_dev_classify(&tf);
925 if (class == ATA_DEV_UNKNOWN)
927 if ((class == ATA_DEV_ATA) && (ata_chk_status(ap) == 0))
936 * ata_dev_id_string - Convert IDENTIFY DEVICE page into string
937 * @id: IDENTIFY DEVICE results we will examine
938 * @s: string into which data is output
939 * @ofs: offset into identify device page
940 * @len: length of string to return. must be an even number.
942 * The strings in the IDENTIFY DEVICE page are broken up into
943 * 16-bit chunks. Run through the string, and output each
944 * 8-bit chunk linearly, regardless of platform.
950 void ata_dev_id_string(u16 *id, unsigned char *s,
951 unsigned int ofs, unsigned int len)
971 * ata_noop_dev_select - Select device 0/1 on ATA bus
972 * @ap: ATA channel to manipulate
973 * @device: ATA device (numbered from zero) to select
975 * This function performs no actual function.
977 * May be used as the dev_select() entry in ata_port_operations.
982 void ata_noop_dev_select (struct ata_port *ap, unsigned int device)
988 * ata_std_dev_select - Select device 0/1 on ATA bus
989 * @ap: ATA channel to manipulate
990 * @device: ATA device (numbered from zero) to select
992 * Use the method defined in the ATA specification to
993 * make either device 0, or device 1, active on the
994 * ATA channel. Works with both PIO and MMIO.
996 * May be used as the dev_select() entry in ata_port_operations.
1002 void ata_std_dev_select (struct ata_port *ap, unsigned int device)
1007 tmp = ATA_DEVICE_OBS;
1009 tmp = ATA_DEVICE_OBS | ATA_DEV1;
1011 if (ap->flags & ATA_FLAG_MMIO) {
1012 writeb(tmp, (void __iomem *) ap->ioaddr.device_addr);
1014 outb(tmp, ap->ioaddr.device_addr);
1016 ata_pause(ap); /* needed; also flushes, for mmio */
1020 * ata_dev_select - Select device 0/1 on ATA bus
1021 * @ap: ATA channel to manipulate
1022 * @device: ATA device (numbered from zero) to select
1023 * @wait: non-zero to wait for Status register BSY bit to clear
1024 * @can_sleep: non-zero if context allows sleeping
1026 * Use the method defined in the ATA specification to
1027 * make either device 0, or device 1, active on the
1030 * This is a high-level version of ata_std_dev_select(),
1031 * which additionally provides the services of inserting
1032 * the proper pauses and status polling, where needed.
1038 void ata_dev_select(struct ata_port *ap, unsigned int device,
1039 unsigned int wait, unsigned int can_sleep)
1041 VPRINTK("ENTER, ata%u: device %u, wait %u\n",
1042 ap->id, device, wait);
1047 ap->ops->dev_select(ap, device);
1050 if (can_sleep && ap->device[device].class == ATA_DEV_ATAPI)
1057 * ata_dump_id - IDENTIFY DEVICE info debugging output
1058 * @dev: Device whose IDENTIFY DEVICE page we will dump
1060 * Dump selected 16-bit words from a detected device's
1061 * IDENTIFY PAGE page.
1067 static inline void ata_dump_id(struct ata_device *dev)
1069 DPRINTK("49==0x%04x "
1079 DPRINTK("80==0x%04x "
1089 DPRINTK("88==0x%04x "
1096 * ata_dev_identify - obtain IDENTIFY x DEVICE page
1097 * @ap: port on which device we wish to probe resides
1098 * @device: device bus address, starting at zero
1100 * Following bus reset, we issue the IDENTIFY [PACKET] DEVICE
1101 * command, and read back the 512-byte device information page.
1102 * The device information page is fed to us via the standard
1103 * PIO-IN protocol, but we hand-code it here. (TODO: investigate
1104 * using standard PIO-IN paths)
1106 * After reading the device information page, we use several
1107 * bits of information from it to initialize data structures
1108 * that will be used during the lifetime of the ata_device.
1109 * Other data from the info page is used to disqualify certain
1110 * older ATA devices we do not wish to support.
1113 * Inherited from caller. Some functions called by this function
1114 * obtain the host_set lock.
1117 static void ata_dev_identify(struct ata_port *ap, unsigned int device)
1119 struct ata_device *dev = &ap->device[device];
1122 unsigned long xfer_modes;
1124 unsigned int using_edd;
1125 DECLARE_COMPLETION(wait);
1126 struct ata_queued_cmd *qc;
1127 unsigned long flags;
1130 if (!ata_dev_present(dev)) {
1131 DPRINTK("ENTER/EXIT (host %u, dev %u) -- nodev\n",
1136 if (ap->flags & (ATA_FLAG_SRST | ATA_FLAG_SATA_RESET))
1141 DPRINTK("ENTER, host %u, dev %u\n", ap->id, device);
1143 assert (dev->class == ATA_DEV_ATA || dev->class == ATA_DEV_ATAPI ||
1144 dev->class == ATA_DEV_NONE);
1146 ata_dev_select(ap, device, 1, 1); /* select device 0/1 */
1148 qc = ata_qc_new_init(ap, dev);
1151 ata_sg_init_one(qc, dev->id, sizeof(dev->id));
1152 qc->dma_dir = DMA_FROM_DEVICE;
1153 qc->tf.protocol = ATA_PROT_PIO;
1157 if (dev->class == ATA_DEV_ATA) {
1158 qc->tf.command = ATA_CMD_ID_ATA;
1159 DPRINTK("do ATA identify\n");
1161 qc->tf.command = ATA_CMD_ID_ATAPI;
1162 DPRINTK("do ATAPI identify\n");
1165 qc->waiting = &wait;
1166 qc->complete_fn = ata_qc_complete_noop;
1168 spin_lock_irqsave(&ap->host_set->lock, flags);
1169 rc = ata_qc_issue(qc);
1170 spin_unlock_irqrestore(&ap->host_set->lock, flags);
1175 wait_for_completion(&wait);
1177 status = ata_chk_status(ap);
1178 if (status & ATA_ERR) {
1180 * arg! EDD works for all test cases, but seems to return
1181 * the ATA signature for some ATAPI devices. Until the
1182 * reason for this is found and fixed, we fix up the mess
1183 * here. If IDENTIFY DEVICE returns command aborted
1184 * (as ATAPI devices do), then we issue an
1185 * IDENTIFY PACKET DEVICE.
1187 * ATA software reset (SRST, the default) does not appear
1188 * to have this problem.
1190 if ((using_edd) && (qc->tf.command == ATA_CMD_ID_ATA)) {
1191 u8 err = ata_chk_err(ap);
1192 if (err & ATA_ABORTED) {
1193 dev->class = ATA_DEV_ATAPI;
1204 swap_buf_le16(dev->id, ATA_ID_WORDS);
1206 /* print device capabilities */
1207 printk(KERN_DEBUG "ata%u: dev %u cfg "
1208 "49:%04x 82:%04x 83:%04x 84:%04x 85:%04x 86:%04x 87:%04x 88:%04x\n",
1209 ap->id, device, dev->id[49],
1210 dev->id[82], dev->id[83], dev->id[84],
1211 dev->id[85], dev->id[86], dev->id[87],
1215 * common ATA, ATAPI feature tests
1218 /* we require LBA and DMA support (bits 8 & 9 of word 49) */
1219 if (!ata_id_has_dma(dev->id) || !ata_id_has_lba(dev->id)) {
1220 printk(KERN_DEBUG "ata%u: no dma/lba\n", ap->id);
1224 /* quick-n-dirty find max transfer mode; for printk only */
1225 xfer_modes = dev->id[ATA_ID_UDMA_MODES];
1227 xfer_modes = (dev->id[ATA_ID_MWDMA_MODES]) << ATA_SHIFT_MWDMA;
1229 xfer_modes = (dev->id[ATA_ID_PIO_MODES]) << (ATA_SHIFT_PIO + 3);
1230 xfer_modes |= (0x7 << ATA_SHIFT_PIO);
1235 /* ATA-specific feature tests */
1236 if (dev->class == ATA_DEV_ATA) {
1237 if (!ata_id_is_ata(dev->id)) /* sanity check */
1240 tmp = dev->id[ATA_ID_MAJOR_VER];
1241 for (i = 14; i >= 1; i--)
1245 /* we require at least ATA-3 */
1247 printk(KERN_DEBUG "ata%u: no ATA-3\n", ap->id);
1251 if (ata_id_has_lba48(dev->id)) {
1252 dev->flags |= ATA_DFLAG_LBA48;
1253 dev->n_sectors = ata_id_u64(dev->id, 100);
1255 dev->n_sectors = ata_id_u32(dev->id, 60);
1258 ap->host->max_cmd_len = 16;
1260 /* print device info to dmesg */
1261 printk(KERN_INFO "ata%u: dev %u ATA, max %s, %Lu sectors:%s\n",
1263 ata_mode_string(xfer_modes),
1264 (unsigned long long)dev->n_sectors,
1265 dev->flags & ATA_DFLAG_LBA48 ? " lba48" : "");
1268 /* ATAPI-specific feature tests */
1270 if (ata_id_is_ata(dev->id)) /* sanity check */
1273 rc = atapi_cdb_len(dev->id);
1274 if ((rc < 12) || (rc > ATAPI_CDB_LEN)) {
1275 printk(KERN_WARNING "ata%u: unsupported CDB len\n", ap->id);
1278 ap->cdb_len = (unsigned int) rc;
1279 ap->host->max_cmd_len = (unsigned char) ap->cdb_len;
1281 /* print device info to dmesg */
1282 printk(KERN_INFO "ata%u: dev %u ATAPI, max %s\n",
1284 ata_mode_string(xfer_modes));
1287 DPRINTK("EXIT, drv_stat = 0x%x\n", ata_chk_status(ap));
1291 printk(KERN_WARNING "ata%u: dev %u not supported, ignoring\n",
1294 dev->class++; /* converts ATA_DEV_xxx into ATA_DEV_xxx_UNSUP */
1295 DPRINTK("EXIT, err\n");
1299 static inline u8 ata_dev_knobble(struct ata_port *ap)
1301 return ((ap->cbl == ATA_CBL_SATA) && (!ata_id_is_sata(ap->device->id)));
1305 * ata_dev_config - Run device specific handlers and check for
1306 * SATA->PATA bridges
1313 void ata_dev_config(struct ata_port *ap, unsigned int i)
1315 /* limit bridge transfers to udma5, 200 sectors */
1316 if (ata_dev_knobble(ap)) {
1317 printk(KERN_INFO "ata%u(%u): applying bridge limits\n",
1318 ap->id, ap->device->devno);
1319 ap->udma_mask &= ATA_UDMA5;
1320 ap->host->max_sectors = ATA_MAX_SECTORS;
1321 ap->host->hostt->max_sectors = ATA_MAX_SECTORS;
1322 ap->device->flags |= ATA_DFLAG_LOCK_SECTORS;
1325 if (ap->ops->dev_config)
1326 ap->ops->dev_config(ap, &ap->device[i]);
1330 * ata_bus_probe - Reset and probe ATA bus
1333 * Master ATA bus probing function. Initiates a hardware-dependent
1334 * bus reset, then attempts to identify any devices found on
1338 * PCI/etc. bus probe sem.
1341 * Zero on success, non-zero on error.
1344 static int ata_bus_probe(struct ata_port *ap)
1346 unsigned int i, found = 0;
1348 ap->ops->phy_reset(ap);
1349 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1352 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1353 ata_dev_identify(ap, i);
1354 if (ata_dev_present(&ap->device[i])) {
1356 ata_dev_config(ap,i);
1360 if ((!found) || (ap->flags & ATA_FLAG_PORT_DISABLED))
1361 goto err_out_disable;
1364 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1365 goto err_out_disable;
1370 ap->ops->port_disable(ap);
1376 * ata_port_probe - Mark port as enabled
1377 * @ap: Port for which we indicate enablement
1379 * Modify @ap data structure such that the system
1380 * thinks that the entire port is enabled.
1382 * LOCKING: host_set lock, or some other form of
1386 void ata_port_probe(struct ata_port *ap)
1388 ap->flags &= ~ATA_FLAG_PORT_DISABLED;
1392 * __sata_phy_reset - Wake/reset a low-level SATA PHY
1393 * @ap: SATA port associated with target SATA PHY.
1395 * This function issues commands to standard SATA Sxxx
1396 * PHY registers, to wake up the phy (and device), and
1397 * clear any reset condition.
1400 * PCI/etc. bus probe sem.
1403 void __sata_phy_reset(struct ata_port *ap)
1406 unsigned long timeout = jiffies + (HZ * 5);
1408 if (ap->flags & ATA_FLAG_SATA_RESET) {
1409 /* issue phy wake/reset */
1410 scr_write_flush(ap, SCR_CONTROL, 0x301);
1411 udelay(400); /* FIXME: a guess */
1413 scr_write_flush(ap, SCR_CONTROL, 0x300); /* phy wake/clear reset */
1415 /* wait for phy to become ready, if necessary */
1418 sstatus = scr_read(ap, SCR_STATUS);
1419 if ((sstatus & 0xf) != 1)
1421 } while (time_before(jiffies, timeout));
1423 /* TODO: phy layer with polling, timeouts, etc. */
1424 if (sata_dev_present(ap))
1427 sstatus = scr_read(ap, SCR_STATUS);
1428 printk(KERN_INFO "ata%u: no device found (phy stat %08x)\n",
1430 ata_port_disable(ap);
1433 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1436 if (ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT)) {
1437 ata_port_disable(ap);
1441 ap->cbl = ATA_CBL_SATA;
1445 * sata_phy_reset - Reset SATA bus.
1446 * @ap: SATA port associated with target SATA PHY.
1448 * This function resets the SATA bus, and then probes
1449 * the bus for devices.
1452 * PCI/etc. bus probe sem.
1455 void sata_phy_reset(struct ata_port *ap)
1457 __sata_phy_reset(ap);
1458 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1464 * ata_port_disable - Disable port.
1465 * @ap: Port to be disabled.
1467 * Modify @ap data structure such that the system
1468 * thinks that the entire port is disabled, and should
1469 * never attempt to probe or communicate with devices
1472 * LOCKING: host_set lock, or some other form of
1476 void ata_port_disable(struct ata_port *ap)
1478 ap->device[0].class = ATA_DEV_NONE;
1479 ap->device[1].class = ATA_DEV_NONE;
1480 ap->flags |= ATA_FLAG_PORT_DISABLED;
1486 } xfer_mode_classes[] = {
1487 { ATA_SHIFT_UDMA, XFER_UDMA_0 },
1488 { ATA_SHIFT_MWDMA, XFER_MW_DMA_0 },
1489 { ATA_SHIFT_PIO, XFER_PIO_0 },
1492 static inline u8 base_from_shift(unsigned int shift)
1496 for (i = 0; i < ARRAY_SIZE(xfer_mode_classes); i++)
1497 if (xfer_mode_classes[i].shift == shift)
1498 return xfer_mode_classes[i].base;
1503 static void ata_dev_set_mode(struct ata_port *ap, struct ata_device *dev)
1508 if (!ata_dev_present(dev) || (ap->flags & ATA_FLAG_PORT_DISABLED))
1511 if (dev->xfer_shift == ATA_SHIFT_PIO)
1512 dev->flags |= ATA_DFLAG_PIO;
1514 ata_dev_set_xfermode(ap, dev);
1516 base = base_from_shift(dev->xfer_shift);
1517 ofs = dev->xfer_mode - base;
1518 idx = ofs + dev->xfer_shift;
1519 WARN_ON(idx >= ARRAY_SIZE(xfer_mode_str));
1521 DPRINTK("idx=%d xfer_shift=%u, xfer_mode=0x%x, base=0x%x, offset=%d\n",
1522 idx, dev->xfer_shift, (int)dev->xfer_mode, (int)base, ofs);
1524 printk(KERN_INFO "ata%u: dev %u configured for %s\n",
1525 ap->id, dev->devno, xfer_mode_str[idx]);
1528 static int ata_host_set_pio(struct ata_port *ap)
1534 mask = ata_get_mode_mask(ap, ATA_SHIFT_PIO);
1537 printk(KERN_WARNING "ata%u: no PIO support\n", ap->id);
1541 base = base_from_shift(ATA_SHIFT_PIO);
1542 xfer_mode = base + x;
1544 DPRINTK("base 0x%x xfer_mode 0x%x mask 0x%x x %d\n",
1545 (int)base, (int)xfer_mode, mask, x);
1547 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1548 struct ata_device *dev = &ap->device[i];
1549 if (ata_dev_present(dev)) {
1550 dev->pio_mode = xfer_mode;
1551 dev->xfer_mode = xfer_mode;
1552 dev->xfer_shift = ATA_SHIFT_PIO;
1553 if (ap->ops->set_piomode)
1554 ap->ops->set_piomode(ap, dev);
1561 static void ata_host_set_dma(struct ata_port *ap, u8 xfer_mode,
1562 unsigned int xfer_shift)
1566 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1567 struct ata_device *dev = &ap->device[i];
1568 if (ata_dev_present(dev)) {
1569 dev->dma_mode = xfer_mode;
1570 dev->xfer_mode = xfer_mode;
1571 dev->xfer_shift = xfer_shift;
1572 if (ap->ops->set_dmamode)
1573 ap->ops->set_dmamode(ap, dev);
1579 * ata_set_mode - Program timings and issue SET FEATURES - XFER
1580 * @ap: port on which timings will be programmed
1582 * Set ATA device disk transfer mode (PIO3, UDMA6, etc.).
1585 * PCI/etc. bus probe sem.
1588 static void ata_set_mode(struct ata_port *ap)
1590 unsigned int i, xfer_shift;
1594 /* step 1: always set host PIO timings */
1595 rc = ata_host_set_pio(ap);
1599 /* step 2: choose the best data xfer mode */
1600 xfer_mode = xfer_shift = 0;
1601 rc = ata_choose_xfer_mode(ap, &xfer_mode, &xfer_shift);
1605 /* step 3: if that xfer mode isn't PIO, set host DMA timings */
1606 if (xfer_shift != ATA_SHIFT_PIO)
1607 ata_host_set_dma(ap, xfer_mode, xfer_shift);
1609 /* step 4: update devices' xfer mode */
1610 ata_dev_set_mode(ap, &ap->device[0]);
1611 ata_dev_set_mode(ap, &ap->device[1]);
1613 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1616 if (ap->ops->post_set_mode)
1617 ap->ops->post_set_mode(ap);
1619 for (i = 0; i < 2; i++) {
1620 struct ata_device *dev = &ap->device[i];
1621 ata_dev_set_protocol(dev);
1627 ata_port_disable(ap);
1631 * ata_busy_sleep - sleep until BSY clears, or timeout
1632 * @ap: port containing status register to be polled
1633 * @tmout_pat: impatience timeout
1634 * @tmout: overall timeout
1636 * Sleep until ATA Status register bit BSY clears,
1637 * or a timeout occurs.
1643 static unsigned int ata_busy_sleep (struct ata_port *ap,
1644 unsigned long tmout_pat,
1645 unsigned long tmout)
1647 unsigned long timer_start, timeout;
1650 status = ata_busy_wait(ap, ATA_BUSY, 300);
1651 timer_start = jiffies;
1652 timeout = timer_start + tmout_pat;
1653 while ((status & ATA_BUSY) && (time_before(jiffies, timeout))) {
1655 status = ata_busy_wait(ap, ATA_BUSY, 3);
1658 if (status & ATA_BUSY)
1659 printk(KERN_WARNING "ata%u is slow to respond, "
1660 "please be patient\n", ap->id);
1662 timeout = timer_start + tmout;
1663 while ((status & ATA_BUSY) && (time_before(jiffies, timeout))) {
1665 status = ata_chk_status(ap);
1668 if (status & ATA_BUSY) {
1669 printk(KERN_ERR "ata%u failed to respond (%lu secs)\n",
1670 ap->id, tmout / HZ);
1677 static void ata_bus_post_reset(struct ata_port *ap, unsigned int devmask)
1679 struct ata_ioports *ioaddr = &ap->ioaddr;
1680 unsigned int dev0 = devmask & (1 << 0);
1681 unsigned int dev1 = devmask & (1 << 1);
1682 unsigned long timeout;
1684 /* if device 0 was found in ata_devchk, wait for its
1688 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
1690 /* if device 1 was found in ata_devchk, wait for
1691 * register access, then wait for BSY to clear
1693 timeout = jiffies + ATA_TMOUT_BOOT;
1697 ap->ops->dev_select(ap, 1);
1698 if (ap->flags & ATA_FLAG_MMIO) {
1699 nsect = readb((void __iomem *) ioaddr->nsect_addr);
1700 lbal = readb((void __iomem *) ioaddr->lbal_addr);
1702 nsect = inb(ioaddr->nsect_addr);
1703 lbal = inb(ioaddr->lbal_addr);
1705 if ((nsect == 1) && (lbal == 1))
1707 if (time_after(jiffies, timeout)) {
1711 msleep(50); /* give drive a breather */
1714 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
1716 /* is all this really necessary? */
1717 ap->ops->dev_select(ap, 0);
1719 ap->ops->dev_select(ap, 1);
1721 ap->ops->dev_select(ap, 0);
1725 * ata_bus_edd - Issue EXECUTE DEVICE DIAGNOSTIC command.
1726 * @ap: Port to reset and probe
1728 * Use the EXECUTE DEVICE DIAGNOSTIC command to reset and
1729 * probe the bus. Not often used these days.
1732 * PCI/etc. bus probe sem.
1736 static unsigned int ata_bus_edd(struct ata_port *ap)
1738 struct ata_taskfile tf;
1740 /* set up execute-device-diag (bus reset) taskfile */
1741 /* also, take interrupts to a known state (disabled) */
1742 DPRINTK("execute-device-diag\n");
1743 ata_tf_init(ap, &tf, 0);
1745 tf.command = ATA_CMD_EDD;
1746 tf.protocol = ATA_PROT_NODATA;
1749 ata_tf_to_host(ap, &tf);
1751 /* spec says at least 2ms. but who knows with those
1752 * crazy ATAPI devices...
1756 return ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
1759 static unsigned int ata_bus_softreset(struct ata_port *ap,
1760 unsigned int devmask)
1762 struct ata_ioports *ioaddr = &ap->ioaddr;
1764 DPRINTK("ata%u: bus reset via SRST\n", ap->id);
1766 /* software reset. causes dev0 to be selected */
1767 if (ap->flags & ATA_FLAG_MMIO) {
1768 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
1769 udelay(20); /* FIXME: flush */
1770 writeb(ap->ctl | ATA_SRST, (void __iomem *) ioaddr->ctl_addr);
1771 udelay(20); /* FIXME: flush */
1772 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
1774 outb(ap->ctl, ioaddr->ctl_addr);
1776 outb(ap->ctl | ATA_SRST, ioaddr->ctl_addr);
1778 outb(ap->ctl, ioaddr->ctl_addr);
1781 /* spec mandates ">= 2ms" before checking status.
1782 * We wait 150ms, because that was the magic delay used for
1783 * ATAPI devices in Hale Landis's ATADRVR, for the period of time
1784 * between when the ATA command register is written, and then
1785 * status is checked. Because waiting for "a while" before
1786 * checking status is fine, post SRST, we perform this magic
1787 * delay here as well.
1791 ata_bus_post_reset(ap, devmask);
1797 * ata_bus_reset - reset host port and associated ATA channel
1798 * @ap: port to reset
1800 * This is typically the first time we actually start issuing
1801 * commands to the ATA channel. We wait for BSY to clear, then
1802 * issue EXECUTE DEVICE DIAGNOSTIC command, polling for its
1803 * result. Determine what devices, if any, are on the channel
1804 * by looking at the device 0/1 error register. Look at the signature
1805 * stored in each device's taskfile registers, to determine if
1806 * the device is ATA or ATAPI.
1809 * PCI/etc. bus probe sem.
1810 * Obtains host_set lock.
1813 * Sets ATA_FLAG_PORT_DISABLED if bus reset fails.
1816 void ata_bus_reset(struct ata_port *ap)
1818 struct ata_ioports *ioaddr = &ap->ioaddr;
1819 unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS;
1821 unsigned int dev0, dev1 = 0, rc = 0, devmask = 0;
1823 DPRINTK("ENTER, host %u, port %u\n", ap->id, ap->port_no);
1825 /* determine if device 0/1 are present */
1826 if (ap->flags & ATA_FLAG_SATA_RESET)
1829 dev0 = ata_devchk(ap, 0);
1831 dev1 = ata_devchk(ap, 1);
1835 devmask |= (1 << 0);
1837 devmask |= (1 << 1);
1839 /* select device 0 again */
1840 ap->ops->dev_select(ap, 0);
1842 /* issue bus reset */
1843 if (ap->flags & ATA_FLAG_SRST)
1844 rc = ata_bus_softreset(ap, devmask);
1845 else if ((ap->flags & ATA_FLAG_SATA_RESET) == 0) {
1846 /* set up device control */
1847 if (ap->flags & ATA_FLAG_MMIO)
1848 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
1850 outb(ap->ctl, ioaddr->ctl_addr);
1851 rc = ata_bus_edd(ap);
1858 * determine by signature whether we have ATA or ATAPI devices
1860 err = ata_dev_try_classify(ap, 0);
1861 if ((slave_possible) && (err != 0x81))
1862 ata_dev_try_classify(ap, 1);
1864 /* re-enable interrupts */
1865 if (ap->ioaddr.ctl_addr) /* FIXME: hack. create a hook instead */
1868 /* is double-select really necessary? */
1869 if (ap->device[1].class != ATA_DEV_NONE)
1870 ap->ops->dev_select(ap, 1);
1871 if (ap->device[0].class != ATA_DEV_NONE)
1872 ap->ops->dev_select(ap, 0);
1874 /* if no devices were detected, disable this port */
1875 if ((ap->device[0].class == ATA_DEV_NONE) &&
1876 (ap->device[1].class == ATA_DEV_NONE))
1879 if (ap->flags & (ATA_FLAG_SATA_RESET | ATA_FLAG_SRST)) {
1880 /* set up device control for ATA_FLAG_SATA_RESET */
1881 if (ap->flags & ATA_FLAG_MMIO)
1882 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
1884 outb(ap->ctl, ioaddr->ctl_addr);
1891 printk(KERN_ERR "ata%u: disabling port\n", ap->id);
1892 ap->ops->port_disable(ap);
1897 static void ata_pr_blacklisted(struct ata_port *ap, struct ata_device *dev)
1899 printk(KERN_WARNING "ata%u: dev %u is on DMA blacklist, disabling DMA\n",
1900 ap->id, dev->devno);
1903 static const char * ata_dma_blacklist [] = {
1922 "Toshiba CD-ROM XM-6202B",
1924 "E-IDE CD-ROM CR-840",
1927 "SAMSUNG CD-ROM SC-148C",
1928 "SAMSUNG CD-ROM SC",
1930 "SAMSUNG CD-ROM SN-124",
1931 "ATAPI CD-ROM DRIVE 40X MAXIMUM",
1935 static int ata_dma_blacklisted(struct ata_port *ap, struct ata_device *dev)
1937 unsigned char model_num[40];
1942 ata_dev_id_string(dev->id, model_num, ATA_ID_PROD_OFS,
1945 len = strnlen(s, sizeof(model_num));
1947 /* ATAPI specifies that empty space is blank-filled; remove blanks */
1948 while ((len > 0) && (s[len - 1] == ' ')) {
1953 for (i = 0; i < ARRAY_SIZE(ata_dma_blacklist); i++)
1954 if (!strncmp(ata_dma_blacklist[i], s, len))
1960 static unsigned int ata_get_mode_mask(struct ata_port *ap, int shift)
1962 struct ata_device *master, *slave;
1965 master = &ap->device[0];
1966 slave = &ap->device[1];
1968 assert (ata_dev_present(master) || ata_dev_present(slave));
1970 if (shift == ATA_SHIFT_UDMA) {
1971 mask = ap->udma_mask;
1972 if (ata_dev_present(master)) {
1973 mask &= (master->id[ATA_ID_UDMA_MODES] & 0xff);
1974 if (ata_dma_blacklisted(ap, master)) {
1976 ata_pr_blacklisted(ap, master);
1979 if (ata_dev_present(slave)) {
1980 mask &= (slave->id[ATA_ID_UDMA_MODES] & 0xff);
1981 if (ata_dma_blacklisted(ap, slave)) {
1983 ata_pr_blacklisted(ap, slave);
1987 else if (shift == ATA_SHIFT_MWDMA) {
1988 mask = ap->mwdma_mask;
1989 if (ata_dev_present(master)) {
1990 mask &= (master->id[ATA_ID_MWDMA_MODES] & 0x07);
1991 if (ata_dma_blacklisted(ap, master)) {
1993 ata_pr_blacklisted(ap, master);
1996 if (ata_dev_present(slave)) {
1997 mask &= (slave->id[ATA_ID_MWDMA_MODES] & 0x07);
1998 if (ata_dma_blacklisted(ap, slave)) {
2000 ata_pr_blacklisted(ap, slave);
2004 else if (shift == ATA_SHIFT_PIO) {
2005 mask = ap->pio_mask;
2006 if (ata_dev_present(master)) {
2007 /* spec doesn't return explicit support for
2008 * PIO0-2, so we fake it
2010 u16 tmp_mode = master->id[ATA_ID_PIO_MODES] & 0x03;
2015 if (ata_dev_present(slave)) {
2016 /* spec doesn't return explicit support for
2017 * PIO0-2, so we fake it
2019 u16 tmp_mode = slave->id[ATA_ID_PIO_MODES] & 0x03;
2026 mask = 0xffffffff; /* shut up compiler warning */
2033 /* find greatest bit */
2034 static int fgb(u32 bitmap)
2039 for (i = 0; i < 32; i++)
2040 if (bitmap & (1 << i))
2047 * ata_choose_xfer_mode - attempt to find best transfer mode
2048 * @ap: Port for which an xfer mode will be selected
2049 * @xfer_mode_out: (output) SET FEATURES - XFER MODE code
2050 * @xfer_shift_out: (output) bit shift that selects this mode
2052 * Based on host and device capabilities, determine the
2053 * maximum transfer mode that is amenable to all.
2056 * PCI/etc. bus probe sem.
2059 * Zero on success, negative on error.
2062 static int ata_choose_xfer_mode(struct ata_port *ap,
2064 unsigned int *xfer_shift_out)
2066 unsigned int mask, shift;
2069 for (i = 0; i < ARRAY_SIZE(xfer_mode_classes); i++) {
2070 shift = xfer_mode_classes[i].shift;
2071 mask = ata_get_mode_mask(ap, shift);
2075 *xfer_mode_out = xfer_mode_classes[i].base + x;
2076 *xfer_shift_out = shift;
2085 * ata_dev_set_xfermode - Issue SET FEATURES - XFER MODE command
2086 * @ap: Port associated with device @dev
2087 * @dev: Device to which command will be sent
2089 * Issue SET FEATURES - XFER MODE command to device @dev
2093 * PCI/etc. bus probe sem.
2096 static void ata_dev_set_xfermode(struct ata_port *ap, struct ata_device *dev)
2098 DECLARE_COMPLETION(wait);
2099 struct ata_queued_cmd *qc;
2101 unsigned long flags;
2103 /* set up set-features taskfile */
2104 DPRINTK("set features - xfer mode\n");
2106 qc = ata_qc_new_init(ap, dev);
2109 qc->tf.command = ATA_CMD_SET_FEATURES;
2110 qc->tf.feature = SETFEATURES_XFER;
2111 qc->tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
2112 qc->tf.protocol = ATA_PROT_NODATA;
2113 qc->tf.nsect = dev->xfer_mode;
2115 qc->waiting = &wait;
2116 qc->complete_fn = ata_qc_complete_noop;
2118 spin_lock_irqsave(&ap->host_set->lock, flags);
2119 rc = ata_qc_issue(qc);
2120 spin_unlock_irqrestore(&ap->host_set->lock, flags);
2123 ata_port_disable(ap);
2125 wait_for_completion(&wait);
2131 * ata_sg_clean - Unmap DMA memory associated with command
2132 * @qc: Command containing DMA memory to be released
2134 * Unmap all mapped DMA memory associated with this command.
2137 * spin_lock_irqsave(host_set lock)
2140 static void ata_sg_clean(struct ata_queued_cmd *qc)
2142 struct ata_port *ap = qc->ap;
2143 struct scatterlist *sg = qc->sg;
2144 int dir = qc->dma_dir;
2146 assert(qc->flags & ATA_QCFLAG_DMAMAP);
2149 if (qc->flags & ATA_QCFLAG_SINGLE)
2150 assert(qc->n_elem == 1);
2152 DPRINTK("unmapping %u sg elements\n", qc->n_elem);
2154 if (qc->flags & ATA_QCFLAG_SG)
2155 dma_unmap_sg(ap->host_set->dev, sg, qc->n_elem, dir);
2157 dma_unmap_single(ap->host_set->dev, sg_dma_address(&sg[0]),
2158 sg_dma_len(&sg[0]), dir);
2160 qc->flags &= ~ATA_QCFLAG_DMAMAP;
2165 * ata_fill_sg - Fill PCI IDE PRD table
2166 * @qc: Metadata associated with taskfile to be transferred
2168 * Fill PCI IDE PRD (scatter-gather) table with segments
2169 * associated with the current disk command.
2172 * spin_lock_irqsave(host_set lock)
2175 static void ata_fill_sg(struct ata_queued_cmd *qc)
2177 struct scatterlist *sg = qc->sg;
2178 struct ata_port *ap = qc->ap;
2179 unsigned int idx, nelem;
2182 assert(qc->n_elem > 0);
2185 for (nelem = qc->n_elem; nelem; nelem--,sg++) {
2189 /* determine if physical DMA addr spans 64K boundary.
2190 * Note h/w doesn't support 64-bit, so we unconditionally
2191 * truncate dma_addr_t to u32.
2193 addr = (u32) sg_dma_address(sg);
2194 sg_len = sg_dma_len(sg);
2197 offset = addr & 0xffff;
2199 if ((offset + sg_len) > 0x10000)
2200 len = 0x10000 - offset;
2202 ap->prd[idx].addr = cpu_to_le32(addr);
2203 ap->prd[idx].flags_len = cpu_to_le32(len & 0xffff);
2204 VPRINTK("PRD[%u] = (0x%X, 0x%X)\n", idx, addr, len);
2213 ap->prd[idx - 1].flags_len |= cpu_to_le32(ATA_PRD_EOT);
2216 * ata_check_atapi_dma - Check whether ATAPI DMA can be supported
2217 * @qc: Metadata associated with taskfile to check
2219 * Allow low-level driver to filter ATA PACKET commands, returning
2220 * a status indicating whether or not it is OK to use DMA for the
2221 * supplied PACKET command.
2224 * spin_lock_irqsave(host_set lock)
2226 * RETURNS: 0 when ATAPI DMA can be used
2229 int ata_check_atapi_dma(struct ata_queued_cmd *qc)
2231 struct ata_port *ap = qc->ap;
2232 int rc = 0; /* Assume ATAPI DMA is OK by default */
2234 if (ap->ops->check_atapi_dma)
2235 rc = ap->ops->check_atapi_dma(qc);
2240 * ata_qc_prep - Prepare taskfile for submission
2241 * @qc: Metadata associated with taskfile to be prepared
2243 * Prepare ATA taskfile for submission.
2246 * spin_lock_irqsave(host_set lock)
2248 void ata_qc_prep(struct ata_queued_cmd *qc)
2250 if (!(qc->flags & ATA_QCFLAG_DMAMAP))
2257 * ata_sg_init_one - Associate command with memory buffer
2258 * @qc: Command to be associated
2259 * @buf: Memory buffer
2260 * @buflen: Length of memory buffer, in bytes.
2262 * Initialize the data-related elements of queued_cmd @qc
2263 * to point to a single memory buffer, @buf of byte length @buflen.
2266 * spin_lock_irqsave(host_set lock)
2272 * ata_sg_init_one - Prepare a one-entry scatter-gather list.
2273 * @qc: Queued command
2274 * @buf: transfer buffer
2275 * @buflen: length of buf
2277 * Builds a single-entry scatter-gather list to initiate a
2278 * transfer utilizing the specified buffer.
2282 void ata_sg_init_one(struct ata_queued_cmd *qc, void *buf, unsigned int buflen)
2284 struct scatterlist *sg;
2286 qc->flags |= ATA_QCFLAG_SINGLE;
2288 memset(&qc->sgent, 0, sizeof(qc->sgent));
2289 qc->sg = &qc->sgent;
2294 sg->page = virt_to_page(buf);
2295 sg->offset = (unsigned long) buf & ~PAGE_MASK;
2296 sg->length = buflen;
2300 * ata_sg_init - Associate command with scatter-gather table.
2301 * @qc: Command to be associated
2302 * @sg: Scatter-gather table.
2303 * @n_elem: Number of elements in s/g table.
2305 * Initialize the data-related elements of queued_cmd @qc
2306 * to point to a scatter-gather table @sg, containing @n_elem
2310 * spin_lock_irqsave(host_set lock)
2315 * ata_sg_init - Assign a scatter gather list to a queued command
2316 * @qc: Queued command
2317 * @sg: Scatter-gather list
2318 * @n_elem: length of sg list
2320 * Attaches a scatter-gather list to a queued command.
2325 void ata_sg_init(struct ata_queued_cmd *qc, struct scatterlist *sg,
2326 unsigned int n_elem)
2328 qc->flags |= ATA_QCFLAG_SG;
2330 qc->n_elem = n_elem;
2334 * ata_sg_setup_one - DMA-map the memory buffer associated with a command.
2335 * @qc: Command with memory buffer to be mapped.
2337 * DMA-map the memory buffer associated with queued_cmd @qc.
2340 * spin_lock_irqsave(host_set lock)
2343 * Zero on success, negative on error.
2346 static int ata_sg_setup_one(struct ata_queued_cmd *qc)
2348 struct ata_port *ap = qc->ap;
2349 int dir = qc->dma_dir;
2350 struct scatterlist *sg = qc->sg;
2351 dma_addr_t dma_address;
2353 dma_address = dma_map_single(ap->host_set->dev, qc->buf_virt,
2355 if (dma_mapping_error(dma_address))
2358 sg_dma_address(sg) = dma_address;
2359 sg_dma_len(sg) = sg->length;
2361 DPRINTK("mapped buffer of %d bytes for %s\n", sg_dma_len(sg),
2362 qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
2368 * ata_sg_setup - DMA-map the scatter-gather table associated with a command.
2369 * @qc: Command with scatter-gather table to be mapped.
2371 * DMA-map the scatter-gather table associated with queued_cmd @qc.
2374 * spin_lock_irqsave(host_set lock)
2377 * Zero on success, negative on error.
2381 static int ata_sg_setup(struct ata_queued_cmd *qc)
2383 struct ata_port *ap = qc->ap;
2384 struct scatterlist *sg = qc->sg;
2387 VPRINTK("ENTER, ata%u\n", ap->id);
2388 assert(qc->flags & ATA_QCFLAG_SG);
2391 n_elem = dma_map_sg(ap->host_set->dev, sg, qc->n_elem, dir);
2395 DPRINTK("%d sg elements mapped\n", n_elem);
2397 qc->n_elem = n_elem;
2407 * None. (executing in kernel thread context)
2413 static unsigned long ata_pio_poll(struct ata_port *ap)
2416 unsigned int poll_state = PIO_ST_UNKNOWN;
2417 unsigned int reg_state = PIO_ST_UNKNOWN;
2418 const unsigned int tmout_state = PIO_ST_TMOUT;
2420 switch (ap->pio_task_state) {
2423 poll_state = PIO_ST_POLL;
2427 case PIO_ST_LAST_POLL:
2428 poll_state = PIO_ST_LAST_POLL;
2429 reg_state = PIO_ST_LAST;
2436 status = ata_chk_status(ap);
2437 if (status & ATA_BUSY) {
2438 if (time_after(jiffies, ap->pio_task_timeout)) {
2439 ap->pio_task_state = tmout_state;
2442 ap->pio_task_state = poll_state;
2443 return ATA_SHORT_PAUSE;
2446 ap->pio_task_state = reg_state;
2451 * ata_pio_complete -
2455 * None. (executing in kernel thread context)
2458 static void ata_pio_complete (struct ata_port *ap)
2460 struct ata_queued_cmd *qc;
2464 * This is purely hueristic. This is a fast path.
2465 * Sometimes when we enter, BSY will be cleared in
2466 * a chk-status or two. If not, the drive is probably seeking
2467 * or something. Snooze for a couple msecs, then
2468 * chk-status again. If still busy, fall back to
2469 * PIO_ST_POLL state.
2471 drv_stat = ata_busy_wait(ap, ATA_BUSY | ATA_DRQ, 10);
2472 if (drv_stat & (ATA_BUSY | ATA_DRQ)) {
2474 drv_stat = ata_busy_wait(ap, ATA_BUSY | ATA_DRQ, 10);
2475 if (drv_stat & (ATA_BUSY | ATA_DRQ)) {
2476 ap->pio_task_state = PIO_ST_LAST_POLL;
2477 ap->pio_task_timeout = jiffies + ATA_TMOUT_PIO;
2482 drv_stat = ata_wait_idle(ap);
2483 if (!ata_ok(drv_stat)) {
2484 ap->pio_task_state = PIO_ST_ERR;
2488 qc = ata_qc_from_tag(ap, ap->active_tag);
2491 ap->pio_task_state = PIO_ST_IDLE;
2495 ata_qc_complete(qc, drv_stat);
2501 * @buf: Buffer to swap
2502 * @buf_words: Number of 16-bit words in buffer.
2504 * Swap halves of 16-bit words if needed to convert from
2505 * little-endian byte order to native cpu byte order, or
2510 void swap_buf_le16(u16 *buf, unsigned int buf_words)
2515 for (i = 0; i < buf_words; i++)
2516 buf[i] = le16_to_cpu(buf[i]);
2517 #endif /* __BIG_ENDIAN */
2520 static void ata_mmio_data_xfer(struct ata_port *ap, unsigned char *buf,
2521 unsigned int buflen, int write_data)
2524 unsigned int words = buflen >> 1;
2525 u16 *buf16 = (u16 *) buf;
2526 void __iomem *mmio = (void __iomem *)ap->ioaddr.data_addr;
2529 for (i = 0; i < words; i++)
2530 writew(le16_to_cpu(buf16[i]), mmio);
2532 for (i = 0; i < words; i++)
2533 buf16[i] = cpu_to_le16(readw(mmio));
2537 static void ata_pio_data_xfer(struct ata_port *ap, unsigned char *buf,
2538 unsigned int buflen, int write_data)
2540 unsigned int dwords = buflen >> 1;
2543 outsw(ap->ioaddr.data_addr, buf, dwords);
2545 insw(ap->ioaddr.data_addr, buf, dwords);
2548 static void ata_data_xfer(struct ata_port *ap, unsigned char *buf,
2549 unsigned int buflen, int do_write)
2551 if (ap->flags & ATA_FLAG_MMIO)
2552 ata_mmio_data_xfer(ap, buf, buflen, do_write);
2554 ata_pio_data_xfer(ap, buf, buflen, do_write);
2557 static void ata_pio_sector(struct ata_queued_cmd *qc)
2559 int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
2560 struct scatterlist *sg = qc->sg;
2561 struct ata_port *ap = qc->ap;
2563 unsigned int offset;
2566 if (qc->cursect == (qc->nsect - 1))
2567 ap->pio_task_state = PIO_ST_LAST;
2569 page = sg[qc->cursg].page;
2570 offset = sg[qc->cursg].offset + qc->cursg_ofs * ATA_SECT_SIZE;
2572 /* get the current page and offset */
2573 page = nth_page(page, (offset >> PAGE_SHIFT));
2574 offset %= PAGE_SIZE;
2576 buf = kmap(page) + offset;
2581 if ((qc->cursg_ofs * ATA_SECT_SIZE) == (&sg[qc->cursg])->length) {
2586 DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
2588 /* do the actual data transfer */
2589 do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
2590 ata_data_xfer(ap, buf, ATA_SECT_SIZE, do_write);
2595 static void __atapi_pio_bytes(struct ata_queued_cmd *qc, unsigned int bytes)
2597 int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
2598 struct scatterlist *sg = qc->sg;
2599 struct ata_port *ap = qc->ap;
2602 unsigned int offset, count;
2604 if (qc->curbytes == qc->nbytes - bytes)
2605 ap->pio_task_state = PIO_ST_LAST;
2608 sg = &qc->sg[qc->cursg];
2611 offset = sg->offset + qc->cursg_ofs;
2613 /* get the current page and offset */
2614 page = nth_page(page, (offset >> PAGE_SHIFT));
2615 offset %= PAGE_SIZE;
2617 /* don't overrun current sg */
2618 count = min(sg->length - qc->cursg_ofs, bytes);
2620 /* don't cross page boundaries */
2621 count = min(count, (unsigned int)PAGE_SIZE - offset);
2623 buf = kmap(page) + offset;
2626 qc->curbytes += count;
2627 qc->cursg_ofs += count;
2629 if (qc->cursg_ofs == sg->length) {
2634 DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
2636 /* do the actual data transfer */
2637 ata_data_xfer(ap, buf, count, do_write);
2646 static void atapi_pio_bytes(struct ata_queued_cmd *qc)
2648 struct ata_port *ap = qc->ap;
2649 struct ata_device *dev = qc->dev;
2650 unsigned int ireason, bc_lo, bc_hi, bytes;
2651 int i_write, do_write = (qc->tf.flags & ATA_TFLAG_WRITE) ? 1 : 0;
2653 ap->ops->tf_read(ap, &qc->tf);
2654 ireason = qc->tf.nsect;
2655 bc_lo = qc->tf.lbam;
2656 bc_hi = qc->tf.lbah;
2657 bytes = (bc_hi << 8) | bc_lo;
2659 /* shall be cleared to zero, indicating xfer of data */
2660 if (ireason & (1 << 0))
2663 /* make sure transfer direction matches expected */
2664 i_write = ((ireason & (1 << 1)) == 0) ? 1 : 0;
2665 if (do_write != i_write)
2668 __atapi_pio_bytes(qc, bytes);
2673 printk(KERN_INFO "ata%u: dev %u: ATAPI check failed\n",
2674 ap->id, dev->devno);
2675 ap->pio_task_state = PIO_ST_ERR;
2683 * None. (executing in kernel thread context)
2686 static void ata_pio_block(struct ata_port *ap)
2688 struct ata_queued_cmd *qc;
2692 * This is purely hueristic. This is a fast path.
2693 * Sometimes when we enter, BSY will be cleared in
2694 * a chk-status or two. If not, the drive is probably seeking
2695 * or something. Snooze for a couple msecs, then
2696 * chk-status again. If still busy, fall back to
2697 * PIO_ST_POLL state.
2699 status = ata_busy_wait(ap, ATA_BUSY, 5);
2700 if (status & ATA_BUSY) {
2702 status = ata_busy_wait(ap, ATA_BUSY, 10);
2703 if (status & ATA_BUSY) {
2704 ap->pio_task_state = PIO_ST_POLL;
2705 ap->pio_task_timeout = jiffies + ATA_TMOUT_PIO;
2710 qc = ata_qc_from_tag(ap, ap->active_tag);
2713 if (is_atapi_taskfile(&qc->tf)) {
2714 /* no more data to transfer or unsupported ATAPI command */
2715 if ((status & ATA_DRQ) == 0) {
2716 ap->pio_task_state = PIO_ST_IDLE;
2720 ata_qc_complete(qc, status);
2724 atapi_pio_bytes(qc);
2726 /* handle BSY=0, DRQ=0 as error */
2727 if ((status & ATA_DRQ) == 0) {
2728 ap->pio_task_state = PIO_ST_ERR;
2736 static void ata_pio_error(struct ata_port *ap)
2738 struct ata_queued_cmd *qc;
2741 qc = ata_qc_from_tag(ap, ap->active_tag);
2744 drv_stat = ata_chk_status(ap);
2745 printk(KERN_WARNING "ata%u: PIO error, drv_stat 0x%x\n",
2748 ap->pio_task_state = PIO_ST_IDLE;
2752 ata_qc_complete(qc, drv_stat | ATA_ERR);
2755 static void ata_pio_task(void *_data)
2757 struct ata_port *ap = _data;
2758 unsigned long timeout = 0;
2760 switch (ap->pio_task_state) {
2769 ata_pio_complete(ap);
2773 case PIO_ST_LAST_POLL:
2774 timeout = ata_pio_poll(ap);
2784 queue_delayed_work(ata_wq, &ap->pio_task,
2787 queue_work(ata_wq, &ap->pio_task);
2790 static void atapi_request_sense(struct ata_port *ap, struct ata_device *dev,
2791 struct scsi_cmnd *cmd)
2793 DECLARE_COMPLETION(wait);
2794 struct ata_queued_cmd *qc;
2795 unsigned long flags;
2798 DPRINTK("ATAPI request sense\n");
2800 qc = ata_qc_new_init(ap, dev);
2803 /* FIXME: is this needed? */
2804 memset(cmd->sense_buffer, 0, sizeof(cmd->sense_buffer));
2806 ata_sg_init_one(qc, cmd->sense_buffer, sizeof(cmd->sense_buffer));
2807 qc->dma_dir = DMA_FROM_DEVICE;
2809 memset(&qc->cdb, 0, ap->cdb_len);
2810 qc->cdb[0] = REQUEST_SENSE;
2811 qc->cdb[4] = SCSI_SENSE_BUFFERSIZE;
2813 qc->tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
2814 qc->tf.command = ATA_CMD_PACKET;
2816 qc->tf.protocol = ATA_PROT_ATAPI;
2817 qc->tf.lbam = (8 * 1024) & 0xff;
2818 qc->tf.lbah = (8 * 1024) >> 8;
2819 qc->nbytes = SCSI_SENSE_BUFFERSIZE;
2821 qc->waiting = &wait;
2822 qc->complete_fn = ata_qc_complete_noop;
2824 spin_lock_irqsave(&ap->host_set->lock, flags);
2825 rc = ata_qc_issue(qc);
2826 spin_unlock_irqrestore(&ap->host_set->lock, flags);
2829 ata_port_disable(ap);
2831 wait_for_completion(&wait);
2837 * ata_qc_timeout - Handle timeout of queued command
2838 * @qc: Command that timed out
2840 * Some part of the kernel (currently, only the SCSI layer)
2841 * has noticed that the active command on port @ap has not
2842 * completed after a specified length of time. Handle this
2843 * condition by disabling DMA (if necessary) and completing
2844 * transactions, with error if necessary.
2846 * This also handles the case of the "lost interrupt", where
2847 * for some reason (possibly hardware bug, possibly driver bug)
2848 * an interrupt was not delivered to the driver, even though the
2849 * transaction completed successfully.
2852 * Inherited from SCSI layer (none, can sleep)
2855 static void ata_qc_timeout(struct ata_queued_cmd *qc)
2857 struct ata_port *ap = qc->ap;
2858 struct ata_device *dev = qc->dev;
2859 u8 host_stat = 0, drv_stat;
2863 /* FIXME: doesn't this conflict with timeout handling? */
2864 if (qc->dev->class == ATA_DEV_ATAPI && qc->scsicmd) {
2865 struct scsi_cmnd *cmd = qc->scsicmd;
2867 if (!scsi_eh_eflags_chk(cmd, SCSI_EH_CANCEL_CMD)) {
2869 /* finish completing original command */
2870 __ata_qc_complete(qc);
2872 atapi_request_sense(ap, dev, cmd);
2874 cmd->result = (CHECK_CONDITION << 1) | (DID_OK << 16);
2875 scsi_finish_command(cmd);
2881 /* hack alert! We cannot use the supplied completion
2882 * function from inside the ->eh_strategy_handler() thread.
2883 * libata is the only user of ->eh_strategy_handler() in
2884 * any kernel, so the default scsi_done() assumes it is
2885 * not being called from the SCSI EH.
2887 qc->scsidone = scsi_finish_command;
2889 switch (qc->tf.protocol) {
2892 case ATA_PROT_ATAPI_DMA:
2893 host_stat = ap->ops->bmdma_status(ap);
2895 /* before we do anything else, clear DMA-Start bit */
2896 ap->ops->bmdma_stop(ap);
2902 drv_stat = ata_chk_status(ap);
2904 /* ack bmdma irq events */
2905 ap->ops->irq_clear(ap);
2907 printk(KERN_ERR "ata%u: command 0x%x timeout, stat 0x%x host_stat 0x%x\n",
2908 ap->id, qc->tf.command, drv_stat, host_stat);
2910 /* complete taskfile transaction */
2911 ata_qc_complete(qc, drv_stat);
2919 * ata_eng_timeout - Handle timeout of queued command
2920 * @ap: Port on which timed-out command is active
2922 * Some part of the kernel (currently, only the SCSI layer)
2923 * has noticed that the active command on port @ap has not
2924 * completed after a specified length of time. Handle this
2925 * condition by disabling DMA (if necessary) and completing
2926 * transactions, with error if necessary.
2928 * This also handles the case of the "lost interrupt", where
2929 * for some reason (possibly hardware bug, possibly driver bug)
2930 * an interrupt was not delivered to the driver, even though the
2931 * transaction completed successfully.
2934 * Inherited from SCSI layer (none, can sleep)
2937 void ata_eng_timeout(struct ata_port *ap)
2939 struct ata_queued_cmd *qc;
2943 qc = ata_qc_from_tag(ap, ap->active_tag);
2945 printk(KERN_ERR "ata%u: BUG: timeout without command\n",
2957 * ata_qc_new - Request an available ATA command, for queueing
2958 * @ap: Port associated with device @dev
2959 * @dev: Device from whom we request an available command structure
2965 static struct ata_queued_cmd *ata_qc_new(struct ata_port *ap)
2967 struct ata_queued_cmd *qc = NULL;
2970 for (i = 0; i < ATA_MAX_QUEUE; i++)
2971 if (!test_and_set_bit(i, &ap->qactive)) {
2972 qc = ata_qc_from_tag(ap, i);
2983 * ata_qc_new_init - Request an available ATA command, and initialize it
2984 * @ap: Port associated with device @dev
2985 * @dev: Device from whom we request an available command structure
2991 struct ata_queued_cmd *ata_qc_new_init(struct ata_port *ap,
2992 struct ata_device *dev)
2994 struct ata_queued_cmd *qc;
2996 qc = ata_qc_new(ap);
3003 qc->cursect = qc->cursg = qc->cursg_ofs = 0;
3005 qc->nbytes = qc->curbytes = 0;
3007 ata_tf_init(ap, &qc->tf, dev->devno);
3009 if (dev->flags & ATA_DFLAG_LBA48)
3010 qc->tf.flags |= ATA_TFLAG_LBA48;
3016 static int ata_qc_complete_noop(struct ata_queued_cmd *qc, u8 drv_stat)
3021 static void __ata_qc_complete(struct ata_queued_cmd *qc)
3023 struct ata_port *ap = qc->ap;
3024 unsigned int tag, do_clear = 0;
3028 if (likely(ata_tag_valid(tag))) {
3029 if (tag == ap->active_tag)
3030 ap->active_tag = ATA_TAG_POISON;
3031 qc->tag = ATA_TAG_POISON;
3036 struct completion *waiting = qc->waiting;
3041 if (likely(do_clear))
3042 clear_bit(tag, &ap->qactive);
3046 * ata_qc_free - free unused ata_queued_cmd
3047 * @qc: Command to complete
3049 * Designed to free unused ata_queued_cmd object
3050 * in case something prevents using it.
3053 * spin_lock_irqsave(host_set lock)
3056 void ata_qc_free(struct ata_queued_cmd *qc)
3058 assert(qc != NULL); /* ata_qc_from_tag _might_ return NULL */
3059 assert(qc->waiting == NULL); /* nothing should be waiting */
3061 __ata_qc_complete(qc);
3065 * ata_qc_complete - Complete an active ATA command
3066 * @qc: Command to complete
3067 * @drv_stat: ATA Status register contents
3069 * Indicate to the mid and upper layers that an ATA
3070 * command has completed, with either an ok or not-ok status.
3073 * spin_lock_irqsave(host_set lock)
3077 void ata_qc_complete(struct ata_queued_cmd *qc, u8 drv_stat)
3081 assert(qc != NULL); /* ata_qc_from_tag _might_ return NULL */
3082 assert(qc->flags & ATA_QCFLAG_ACTIVE);
3084 if (likely(qc->flags & ATA_QCFLAG_DMAMAP))
3087 /* call completion callback */
3088 rc = qc->complete_fn(qc, drv_stat);
3089 qc->flags &= ~ATA_QCFLAG_ACTIVE;
3091 /* if callback indicates not to complete command (non-zero),
3092 * return immediately
3097 __ata_qc_complete(qc);
3102 static inline int ata_should_dma_map(struct ata_queued_cmd *qc)
3104 struct ata_port *ap = qc->ap;
3106 switch (qc->tf.protocol) {
3108 case ATA_PROT_ATAPI_DMA:
3111 case ATA_PROT_ATAPI:
3113 case ATA_PROT_PIO_MULT:
3114 if (ap->flags & ATA_FLAG_PIO_DMA)
3127 * ata_qc_issue - issue taskfile to device
3128 * @qc: command to issue to device
3130 * Prepare an ATA command to submission to device.
3131 * This includes mapping the data into a DMA-able
3132 * area, filling in the S/G table, and finally
3133 * writing the taskfile to hardware, starting the command.
3136 * spin_lock_irqsave(host_set lock)
3139 * Zero on success, negative on error.
3142 int ata_qc_issue(struct ata_queued_cmd *qc)
3144 struct ata_port *ap = qc->ap;
3146 if (ata_should_dma_map(qc)) {
3147 if (qc->flags & ATA_QCFLAG_SG) {
3148 if (ata_sg_setup(qc))
3150 } else if (qc->flags & ATA_QCFLAG_SINGLE) {
3151 if (ata_sg_setup_one(qc))
3155 qc->flags &= ~ATA_QCFLAG_DMAMAP;
3158 ap->ops->qc_prep(qc);
3160 qc->ap->active_tag = qc->tag;
3161 qc->flags |= ATA_QCFLAG_ACTIVE;
3163 return ap->ops->qc_issue(qc);
3171 * ata_qc_issue_prot - issue taskfile to device in proto-dependent manner
3172 * @qc: command to issue to device
3174 * Using various libata functions and hooks, this function
3175 * starts an ATA command. ATA commands are grouped into
3176 * classes called "protocols", and issuing each type of protocol
3177 * is slightly different.
3179 * May be used as the qc_issue() entry in ata_port_operations.
3182 * spin_lock_irqsave(host_set lock)
3185 * Zero on success, negative on error.
3188 int ata_qc_issue_prot(struct ata_queued_cmd *qc)
3190 struct ata_port *ap = qc->ap;
3192 ata_dev_select(ap, qc->dev->devno, 1, 0);
3194 switch (qc->tf.protocol) {
3195 case ATA_PROT_NODATA:
3196 ata_tf_to_host_nolock(ap, &qc->tf);
3200 ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
3201 ap->ops->bmdma_setup(qc); /* set up bmdma */
3202 ap->ops->bmdma_start(qc); /* initiate bmdma */
3205 case ATA_PROT_PIO: /* load tf registers, initiate polling pio */
3206 ata_qc_set_polling(qc);
3207 ata_tf_to_host_nolock(ap, &qc->tf);
3208 ap->pio_task_state = PIO_ST;
3209 queue_work(ata_wq, &ap->pio_task);
3212 case ATA_PROT_ATAPI:
3213 ata_qc_set_polling(qc);
3214 ata_tf_to_host_nolock(ap, &qc->tf);
3215 queue_work(ata_wq, &ap->packet_task);
3218 case ATA_PROT_ATAPI_NODATA:
3219 ata_tf_to_host_nolock(ap, &qc->tf);
3220 queue_work(ata_wq, &ap->packet_task);
3223 case ATA_PROT_ATAPI_DMA:
3224 ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
3225 ap->ops->bmdma_setup(qc); /* set up bmdma */
3226 queue_work(ata_wq, &ap->packet_task);
3238 * ata_bmdma_setup_mmio - Set up PCI IDE BMDMA transaction
3239 * @qc: Info associated with this ATA transaction.
3242 * spin_lock_irqsave(host_set lock)
3245 static void ata_bmdma_setup_mmio (struct ata_queued_cmd *qc)
3247 struct ata_port *ap = qc->ap;
3248 unsigned int rw = (qc->tf.flags & ATA_TFLAG_WRITE);
3250 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
3252 /* load PRD table addr. */
3253 mb(); /* make sure PRD table writes are visible to controller */
3254 writel(ap->prd_dma, mmio + ATA_DMA_TABLE_OFS);
3256 /* specify data direction, triple-check start bit is clear */
3257 dmactl = readb(mmio + ATA_DMA_CMD);
3258 dmactl &= ~(ATA_DMA_WR | ATA_DMA_START);
3260 dmactl |= ATA_DMA_WR;
3261 writeb(dmactl, mmio + ATA_DMA_CMD);
3263 /* issue r/w command */
3264 ap->ops->exec_command(ap, &qc->tf);
3268 * ata_bmdma_start - Start a PCI IDE BMDMA transaction
3269 * @qc: Info associated with this ATA transaction.
3272 * spin_lock_irqsave(host_set lock)
3275 static void ata_bmdma_start_mmio (struct ata_queued_cmd *qc)
3277 struct ata_port *ap = qc->ap;
3278 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
3281 /* start host DMA transaction */
3282 dmactl = readb(mmio + ATA_DMA_CMD);
3283 writeb(dmactl | ATA_DMA_START, mmio + ATA_DMA_CMD);
3285 /* Strictly, one may wish to issue a readb() here, to
3286 * flush the mmio write. However, control also passes
3287 * to the hardware at this point, and it will interrupt
3288 * us when we are to resume control. So, in effect,
3289 * we don't care when the mmio write flushes.
3290 * Further, a read of the DMA status register _immediately_
3291 * following the write may not be what certain flaky hardware
3292 * is expected, so I think it is best to not add a readb()
3293 * without first all the MMIO ATA cards/mobos.
3294 * Or maybe I'm just being paranoid.
3299 * ata_bmdma_setup_pio - Set up PCI IDE BMDMA transaction (PIO)
3300 * @qc: Info associated with this ATA transaction.
3303 * spin_lock_irqsave(host_set lock)
3306 static void ata_bmdma_setup_pio (struct ata_queued_cmd *qc)
3308 struct ata_port *ap = qc->ap;
3309 unsigned int rw = (qc->tf.flags & ATA_TFLAG_WRITE);
3312 /* load PRD table addr. */
3313 outl(ap->prd_dma, ap->ioaddr.bmdma_addr + ATA_DMA_TABLE_OFS);
3315 /* specify data direction, triple-check start bit is clear */
3316 dmactl = inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3317 dmactl &= ~(ATA_DMA_WR | ATA_DMA_START);
3319 dmactl |= ATA_DMA_WR;
3320 outb(dmactl, ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3322 /* issue r/w command */
3323 ap->ops->exec_command(ap, &qc->tf);
3327 * ata_bmdma_start_pio - Start a PCI IDE BMDMA transaction (PIO)
3328 * @qc: Info associated with this ATA transaction.
3331 * spin_lock_irqsave(host_set lock)
3334 static void ata_bmdma_start_pio (struct ata_queued_cmd *qc)
3336 struct ata_port *ap = qc->ap;
3339 /* start host DMA transaction */
3340 dmactl = inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3341 outb(dmactl | ATA_DMA_START,
3342 ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3347 * ata_bmdma_start - Start a PCI IDE BMDMA transaction
3348 * @qc: Info associated with this ATA transaction.
3350 * Writes the ATA_DMA_START flag to the DMA command register.
3352 * May be used as the bmdma_start() entry in ata_port_operations.
3355 * spin_lock_irqsave(host_set lock)
3357 void ata_bmdma_start(struct ata_queued_cmd *qc)
3359 if (qc->ap->flags & ATA_FLAG_MMIO)
3360 ata_bmdma_start_mmio(qc);
3362 ata_bmdma_start_pio(qc);
3367 * ata_bmdma_setup - Set up PCI IDE BMDMA transaction
3368 * @qc: Info associated with this ATA transaction.
3370 * Writes address of PRD table to device's PRD Table Address
3371 * register, sets the DMA control register, and calls
3372 * ops->exec_command() to start the transfer.
3374 * May be used as the bmdma_setup() entry in ata_port_operations.
3377 * spin_lock_irqsave(host_set lock)
3379 void ata_bmdma_setup(struct ata_queued_cmd *qc)
3381 if (qc->ap->flags & ATA_FLAG_MMIO)
3382 ata_bmdma_setup_mmio(qc);
3384 ata_bmdma_setup_pio(qc);
3389 * ata_bmdma_irq_clear - Clear PCI IDE BMDMA interrupt.
3390 * @ap: Port associated with this ATA transaction.
3392 * Clear interrupt and error flags in DMA status register.
3394 * May be used as the irq_clear() entry in ata_port_operations.
3397 * spin_lock_irqsave(host_set lock)
3400 void ata_bmdma_irq_clear(struct ata_port *ap)
3402 if (ap->flags & ATA_FLAG_MMIO) {
3403 void __iomem *mmio = ((void __iomem *) ap->ioaddr.bmdma_addr) + ATA_DMA_STATUS;
3404 writeb(readb(mmio), mmio);
3406 unsigned long addr = ap->ioaddr.bmdma_addr + ATA_DMA_STATUS;
3407 outb(inb(addr), addr);
3414 * ata_bmdma_status - Read PCI IDE BMDMA status
3415 * @ap: Port associated with this ATA transaction.
3417 * Read and return BMDMA status register.
3419 * May be used as the bmdma_status() entry in ata_port_operations.
3422 * spin_lock_irqsave(host_set lock)
3425 u8 ata_bmdma_status(struct ata_port *ap)
3428 if (ap->flags & ATA_FLAG_MMIO) {
3429 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
3430 host_stat = readb(mmio + ATA_DMA_STATUS);
3432 host_stat = inb(ap->ioaddr.bmdma_addr + ATA_DMA_STATUS);
3438 * ata_bmdma_stop - Stop PCI IDE BMDMA transfer
3439 * @ap: Port associated with this ATA transaction.
3441 * Clears the ATA_DMA_START flag in the dma control register
3443 * May be used as the bmdma_stop() entry in ata_port_operations.
3446 * spin_lock_irqsave(host_set lock)
3449 void ata_bmdma_stop(struct ata_port *ap)
3451 if (ap->flags & ATA_FLAG_MMIO) {
3452 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
3454 /* clear start/stop bit */
3455 writeb(readb(mmio + ATA_DMA_CMD) & ~ATA_DMA_START,
3456 mmio + ATA_DMA_CMD);
3458 /* clear start/stop bit */
3459 outb(inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD) & ~ATA_DMA_START,
3460 ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3463 /* one-PIO-cycle guaranteed wait, per spec, for HDMA1:0 transition */
3464 ata_altstatus(ap); /* dummy read */
3468 * ata_host_intr - Handle host interrupt for given (port, task)
3469 * @ap: Port on which interrupt arrived (possibly...)
3470 * @qc: Taskfile currently active in engine