2 * libata-core.c - helper library for ATA
4 * Maintained by: Jeff Garzik <jgarzik@pobox.com>
5 * Please ALWAYS copy linux-ide@vger.kernel.org
8 * Copyright 2003-2004 Red Hat, Inc. All rights reserved.
9 * Copyright 2003-2004 Jeff Garzik
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2, or (at your option)
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; see the file COPYING. If not, write to
24 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
27 * libata documentation is available via 'make {ps|pdf}docs',
28 * as Documentation/DocBook/libata.*
30 * Hardware documentation available from http://www.t13.org/ and
31 * http://www.sata-io.org/
35 #include <linux/config.h>
36 #include <linux/kernel.h>
37 #include <linux/module.h>
38 #include <linux/pci.h>
39 #include <linux/init.h>
40 #include <linux/list.h>
42 #include <linux/highmem.h>
43 #include <linux/spinlock.h>
44 #include <linux/blkdev.h>
45 #include <linux/delay.h>
46 #include <linux/timer.h>
47 #include <linux/interrupt.h>
48 #include <linux/completion.h>
49 #include <linux/suspend.h>
50 #include <linux/workqueue.h>
51 #include <linux/jiffies.h>
52 #include <linux/scatterlist.h>
53 #include <scsi/scsi.h>
55 #include "scsi_priv.h"
56 #include <scsi/scsi_host.h>
57 #include <linux/libata.h>
59 #include <asm/semaphore.h>
60 #include <asm/byteorder.h>
64 static unsigned int ata_busy_sleep (struct ata_port *ap,
65 unsigned long tmout_pat,
67 static void ata_dev_reread_id(struct ata_port *ap, struct ata_device *dev);
68 static void ata_dev_init_params(struct ata_port *ap, struct ata_device *dev);
69 static void ata_set_mode(struct ata_port *ap);
70 static void ata_dev_set_xfermode(struct ata_port *ap, struct ata_device *dev);
71 static unsigned int ata_get_mode_mask(const struct ata_port *ap, int shift);
72 static int fgb(u32 bitmap);
73 static int ata_choose_xfer_mode(const struct ata_port *ap,
75 unsigned int *xfer_shift_out);
76 static void __ata_qc_complete(struct ata_queued_cmd *qc);
77 static void ata_pio_error(struct ata_port *ap);
79 static unsigned int ata_unique_id = 1;
80 static struct workqueue_struct *ata_wq;
82 int atapi_enabled = 0;
83 module_param(atapi_enabled, int, 0444);
84 MODULE_PARM_DESC(atapi_enabled, "Enable discovery of ATAPI devices (0=off, 1=on)");
86 MODULE_AUTHOR("Jeff Garzik");
87 MODULE_DESCRIPTION("Library module for ATA devices");
88 MODULE_LICENSE("GPL");
89 MODULE_VERSION(DRV_VERSION);
92 * ata_tf_load_pio - send taskfile registers to host controller
93 * @ap: Port to which output is sent
94 * @tf: ATA taskfile register set
96 * Outputs ATA taskfile to standard ATA host controller.
99 * Inherited from caller.
102 static void ata_tf_load_pio(struct ata_port *ap, const struct ata_taskfile *tf)
104 struct ata_ioports *ioaddr = &ap->ioaddr;
105 unsigned int is_addr = tf->flags & ATA_TFLAG_ISADDR;
107 if (tf->ctl != ap->last_ctl) {
108 outb(tf->ctl, ioaddr->ctl_addr);
109 ap->last_ctl = tf->ctl;
113 if (is_addr && (tf->flags & ATA_TFLAG_LBA48)) {
114 outb(tf->hob_feature, ioaddr->feature_addr);
115 outb(tf->hob_nsect, ioaddr->nsect_addr);
116 outb(tf->hob_lbal, ioaddr->lbal_addr);
117 outb(tf->hob_lbam, ioaddr->lbam_addr);
118 outb(tf->hob_lbah, ioaddr->lbah_addr);
119 VPRINTK("hob: feat 0x%X nsect 0x%X, lba 0x%X 0x%X 0x%X\n",
128 outb(tf->feature, ioaddr->feature_addr);
129 outb(tf->nsect, ioaddr->nsect_addr);
130 outb(tf->lbal, ioaddr->lbal_addr);
131 outb(tf->lbam, ioaddr->lbam_addr);
132 outb(tf->lbah, ioaddr->lbah_addr);
133 VPRINTK("feat 0x%X nsect 0x%X lba 0x%X 0x%X 0x%X\n",
141 if (tf->flags & ATA_TFLAG_DEVICE) {
142 outb(tf->device, ioaddr->device_addr);
143 VPRINTK("device 0x%X\n", tf->device);
150 * ata_tf_load_mmio - send taskfile registers to host controller
151 * @ap: Port to which output is sent
152 * @tf: ATA taskfile register set
154 * Outputs ATA taskfile to standard ATA host controller using MMIO.
157 * Inherited from caller.
160 static void ata_tf_load_mmio(struct ata_port *ap, const struct ata_taskfile *tf)
162 struct ata_ioports *ioaddr = &ap->ioaddr;
163 unsigned int is_addr = tf->flags & ATA_TFLAG_ISADDR;
165 if (tf->ctl != ap->last_ctl) {
166 writeb(tf->ctl, (void __iomem *) ap->ioaddr.ctl_addr);
167 ap->last_ctl = tf->ctl;
171 if (is_addr && (tf->flags & ATA_TFLAG_LBA48)) {
172 writeb(tf->hob_feature, (void __iomem *) ioaddr->feature_addr);
173 writeb(tf->hob_nsect, (void __iomem *) ioaddr->nsect_addr);
174 writeb(tf->hob_lbal, (void __iomem *) ioaddr->lbal_addr);
175 writeb(tf->hob_lbam, (void __iomem *) ioaddr->lbam_addr);
176 writeb(tf->hob_lbah, (void __iomem *) ioaddr->lbah_addr);
177 VPRINTK("hob: feat 0x%X nsect 0x%X, lba 0x%X 0x%X 0x%X\n",
186 writeb(tf->feature, (void __iomem *) ioaddr->feature_addr);
187 writeb(tf->nsect, (void __iomem *) ioaddr->nsect_addr);
188 writeb(tf->lbal, (void __iomem *) ioaddr->lbal_addr);
189 writeb(tf->lbam, (void __iomem *) ioaddr->lbam_addr);
190 writeb(tf->lbah, (void __iomem *) ioaddr->lbah_addr);
191 VPRINTK("feat 0x%X nsect 0x%X lba 0x%X 0x%X 0x%X\n",
199 if (tf->flags & ATA_TFLAG_DEVICE) {
200 writeb(tf->device, (void __iomem *) ioaddr->device_addr);
201 VPRINTK("device 0x%X\n", tf->device);
209 * ata_tf_load - send taskfile registers to host controller
210 * @ap: Port to which output is sent
211 * @tf: ATA taskfile register set
213 * Outputs ATA taskfile to standard ATA host controller using MMIO
214 * or PIO as indicated by the ATA_FLAG_MMIO flag.
215 * Writes the control, feature, nsect, lbal, lbam, and lbah registers.
216 * Optionally (ATA_TFLAG_LBA48) writes hob_feature, hob_nsect,
217 * hob_lbal, hob_lbam, and hob_lbah.
219 * This function waits for idle (!BUSY and !DRQ) after writing
220 * registers. If the control register has a new value, this
221 * function also waits for idle after writing control and before
222 * writing the remaining registers.
224 * May be used as the tf_load() entry in ata_port_operations.
227 * Inherited from caller.
229 void ata_tf_load(struct ata_port *ap, const struct ata_taskfile *tf)
231 if (ap->flags & ATA_FLAG_MMIO)
232 ata_tf_load_mmio(ap, tf);
234 ata_tf_load_pio(ap, tf);
238 * ata_exec_command_pio - issue ATA command to host controller
239 * @ap: port to which command is being issued
240 * @tf: ATA taskfile register set
242 * Issues PIO write to ATA command register, with proper
243 * synchronization with interrupt handler / other threads.
246 * spin_lock_irqsave(host_set lock)
249 static void ata_exec_command_pio(struct ata_port *ap, const struct ata_taskfile *tf)
251 DPRINTK("ata%u: cmd 0x%X\n", ap->id, tf->command);
253 outb(tf->command, ap->ioaddr.command_addr);
259 * ata_exec_command_mmio - issue ATA command to host controller
260 * @ap: port to which command is being issued
261 * @tf: ATA taskfile register set
263 * Issues MMIO write to ATA command register, with proper
264 * synchronization with interrupt handler / other threads.
267 * spin_lock_irqsave(host_set lock)
270 static void ata_exec_command_mmio(struct ata_port *ap, const struct ata_taskfile *tf)
272 DPRINTK("ata%u: cmd 0x%X\n", ap->id, tf->command);
274 writeb(tf->command, (void __iomem *) ap->ioaddr.command_addr);
280 * ata_exec_command - issue ATA command to host controller
281 * @ap: port to which command is being issued
282 * @tf: ATA taskfile register set
284 * Issues PIO/MMIO write to ATA command register, with proper
285 * synchronization with interrupt handler / other threads.
288 * spin_lock_irqsave(host_set lock)
290 void ata_exec_command(struct ata_port *ap, const struct ata_taskfile *tf)
292 if (ap->flags & ATA_FLAG_MMIO)
293 ata_exec_command_mmio(ap, tf);
295 ata_exec_command_pio(ap, tf);
299 * ata_tf_to_host - issue ATA taskfile to host controller
300 * @ap: port to which command is being issued
301 * @tf: ATA taskfile register set
303 * Issues ATA taskfile register set to ATA host controller,
304 * with proper synchronization with interrupt handler and
308 * spin_lock_irqsave(host_set lock)
311 static inline void ata_tf_to_host(struct ata_port *ap,
312 const struct ata_taskfile *tf)
314 ap->ops->tf_load(ap, tf);
315 ap->ops->exec_command(ap, tf);
319 * ata_tf_read_pio - input device's ATA taskfile shadow registers
320 * @ap: Port from which input is read
321 * @tf: ATA taskfile register set for storing input
323 * Reads ATA taskfile registers for currently-selected device
327 * Inherited from caller.
330 static void ata_tf_read_pio(struct ata_port *ap, struct ata_taskfile *tf)
332 struct ata_ioports *ioaddr = &ap->ioaddr;
334 tf->command = ata_check_status(ap);
335 tf->feature = inb(ioaddr->error_addr);
336 tf->nsect = inb(ioaddr->nsect_addr);
337 tf->lbal = inb(ioaddr->lbal_addr);
338 tf->lbam = inb(ioaddr->lbam_addr);
339 tf->lbah = inb(ioaddr->lbah_addr);
340 tf->device = inb(ioaddr->device_addr);
342 if (tf->flags & ATA_TFLAG_LBA48) {
343 outb(tf->ctl | ATA_HOB, ioaddr->ctl_addr);
344 tf->hob_feature = inb(ioaddr->error_addr);
345 tf->hob_nsect = inb(ioaddr->nsect_addr);
346 tf->hob_lbal = inb(ioaddr->lbal_addr);
347 tf->hob_lbam = inb(ioaddr->lbam_addr);
348 tf->hob_lbah = inb(ioaddr->lbah_addr);
353 * ata_tf_read_mmio - input device's ATA taskfile shadow registers
354 * @ap: Port from which input is read
355 * @tf: ATA taskfile register set for storing input
357 * Reads ATA taskfile registers for currently-selected device
361 * Inherited from caller.
364 static void ata_tf_read_mmio(struct ata_port *ap, struct ata_taskfile *tf)
366 struct ata_ioports *ioaddr = &ap->ioaddr;
368 tf->command = ata_check_status(ap);
369 tf->feature = readb((void __iomem *)ioaddr->error_addr);
370 tf->nsect = readb((void __iomem *)ioaddr->nsect_addr);
371 tf->lbal = readb((void __iomem *)ioaddr->lbal_addr);
372 tf->lbam = readb((void __iomem *)ioaddr->lbam_addr);
373 tf->lbah = readb((void __iomem *)ioaddr->lbah_addr);
374 tf->device = readb((void __iomem *)ioaddr->device_addr);
376 if (tf->flags & ATA_TFLAG_LBA48) {
377 writeb(tf->ctl | ATA_HOB, (void __iomem *) ap->ioaddr.ctl_addr);
378 tf->hob_feature = readb((void __iomem *)ioaddr->error_addr);
379 tf->hob_nsect = readb((void __iomem *)ioaddr->nsect_addr);
380 tf->hob_lbal = readb((void __iomem *)ioaddr->lbal_addr);
381 tf->hob_lbam = readb((void __iomem *)ioaddr->lbam_addr);
382 tf->hob_lbah = readb((void __iomem *)ioaddr->lbah_addr);
388 * ata_tf_read - input device's ATA taskfile shadow registers
389 * @ap: Port from which input is read
390 * @tf: ATA taskfile register set for storing input
392 * Reads ATA taskfile registers for currently-selected device
395 * Reads nsect, lbal, lbam, lbah, and device. If ATA_TFLAG_LBA48
396 * is set, also reads the hob registers.
398 * May be used as the tf_read() entry in ata_port_operations.
401 * Inherited from caller.
403 void ata_tf_read(struct ata_port *ap, struct ata_taskfile *tf)
405 if (ap->flags & ATA_FLAG_MMIO)
406 ata_tf_read_mmio(ap, tf);
408 ata_tf_read_pio(ap, tf);
412 * ata_check_status_pio - Read device status reg & clear interrupt
413 * @ap: port where the device is
415 * Reads ATA taskfile status register for currently-selected device
416 * and return its value. This also clears pending interrupts
420 * Inherited from caller.
422 static u8 ata_check_status_pio(struct ata_port *ap)
424 return inb(ap->ioaddr.status_addr);
428 * ata_check_status_mmio - Read device status reg & clear interrupt
429 * @ap: port where the device is
431 * Reads ATA taskfile status register for currently-selected device
432 * via MMIO and return its value. This also clears pending interrupts
436 * Inherited from caller.
438 static u8 ata_check_status_mmio(struct ata_port *ap)
440 return readb((void __iomem *) ap->ioaddr.status_addr);
445 * ata_check_status - Read device status reg & clear interrupt
446 * @ap: port where the device is
448 * Reads ATA taskfile status register for currently-selected device
449 * and return its value. This also clears pending interrupts
452 * May be used as the check_status() entry in ata_port_operations.
455 * Inherited from caller.
457 u8 ata_check_status(struct ata_port *ap)
459 if (ap->flags & ATA_FLAG_MMIO)
460 return ata_check_status_mmio(ap);
461 return ata_check_status_pio(ap);
466 * ata_altstatus - Read device alternate status reg
467 * @ap: port where the device is
469 * Reads ATA taskfile alternate status register for
470 * currently-selected device and return its value.
472 * Note: may NOT be used as the check_altstatus() entry in
473 * ata_port_operations.
476 * Inherited from caller.
478 u8 ata_altstatus(struct ata_port *ap)
480 if (ap->ops->check_altstatus)
481 return ap->ops->check_altstatus(ap);
483 if (ap->flags & ATA_FLAG_MMIO)
484 return readb((void __iomem *)ap->ioaddr.altstatus_addr);
485 return inb(ap->ioaddr.altstatus_addr);
490 * ata_tf_to_fis - Convert ATA taskfile to SATA FIS structure
491 * @tf: Taskfile to convert
492 * @fis: Buffer into which data will output
493 * @pmp: Port multiplier port
495 * Converts a standard ATA taskfile to a Serial ATA
496 * FIS structure (Register - Host to Device).
499 * Inherited from caller.
502 void ata_tf_to_fis(const struct ata_taskfile *tf, u8 *fis, u8 pmp)
504 fis[0] = 0x27; /* Register - Host to Device FIS */
505 fis[1] = (pmp & 0xf) | (1 << 7); /* Port multiplier number,
506 bit 7 indicates Command FIS */
507 fis[2] = tf->command;
508 fis[3] = tf->feature;
515 fis[8] = tf->hob_lbal;
516 fis[9] = tf->hob_lbam;
517 fis[10] = tf->hob_lbah;
518 fis[11] = tf->hob_feature;
521 fis[13] = tf->hob_nsect;
532 * ata_tf_from_fis - Convert SATA FIS to ATA taskfile
533 * @fis: Buffer from which data will be input
534 * @tf: Taskfile to output
536 * Converts a standard ATA taskfile to a Serial ATA
537 * FIS structure (Register - Host to Device).
540 * Inherited from caller.
543 void ata_tf_from_fis(const u8 *fis, struct ata_taskfile *tf)
545 tf->command = fis[2]; /* status */
546 tf->feature = fis[3]; /* error */
553 tf->hob_lbal = fis[8];
554 tf->hob_lbam = fis[9];
555 tf->hob_lbah = fis[10];
558 tf->hob_nsect = fis[13];
561 static const u8 ata_rw_cmds[] = {
565 ATA_CMD_READ_MULTI_EXT,
566 ATA_CMD_WRITE_MULTI_EXT,
570 ATA_CMD_PIO_READ_EXT,
571 ATA_CMD_PIO_WRITE_EXT,
580 * ata_rwcmd_protocol - set taskfile r/w commands and protocol
581 * @qc: command to examine and configure
583 * Examine the device configuration and tf->flags to calculate
584 * the proper read/write commands and protocol to use.
589 void ata_rwcmd_protocol(struct ata_queued_cmd *qc)
591 struct ata_taskfile *tf = &qc->tf;
592 struct ata_device *dev = qc->dev;
594 int index, lba48, write;
596 lba48 = (tf->flags & ATA_TFLAG_LBA48) ? 2 : 0;
597 write = (tf->flags & ATA_TFLAG_WRITE) ? 1 : 0;
599 if (dev->flags & ATA_DFLAG_PIO) {
600 tf->protocol = ATA_PROT_PIO;
601 index = dev->multi_count ? 0 : 4;
603 tf->protocol = ATA_PROT_DMA;
607 tf->command = ata_rw_cmds[index + lba48 + write];
610 static const char * xfer_mode_str[] = {
630 * ata_udma_string - convert UDMA bit offset to string
631 * @mask: mask of bits supported; only highest bit counts.
633 * Determine string which represents the highest speed
634 * (highest bit in @udma_mask).
640 * Constant C string representing highest speed listed in
641 * @udma_mask, or the constant C string "<n/a>".
644 static const char *ata_mode_string(unsigned int mask)
648 for (i = 7; i >= 0; i--)
651 for (i = ATA_SHIFT_MWDMA + 2; i >= ATA_SHIFT_MWDMA; i--)
654 for (i = ATA_SHIFT_PIO + 4; i >= ATA_SHIFT_PIO; i--)
661 return xfer_mode_str[i];
665 * ata_pio_devchk - PATA device presence detection
666 * @ap: ATA channel to examine
667 * @device: Device to examine (starting at zero)
669 * This technique was originally described in
670 * Hale Landis's ATADRVR (www.ata-atapi.com), and
671 * later found its way into the ATA/ATAPI spec.
673 * Write a pattern to the ATA shadow registers,
674 * and if a device is present, it will respond by
675 * correctly storing and echoing back the
676 * ATA shadow register contents.
682 static unsigned int ata_pio_devchk(struct ata_port *ap,
685 struct ata_ioports *ioaddr = &ap->ioaddr;
688 ap->ops->dev_select(ap, device);
690 outb(0x55, ioaddr->nsect_addr);
691 outb(0xaa, ioaddr->lbal_addr);
693 outb(0xaa, ioaddr->nsect_addr);
694 outb(0x55, ioaddr->lbal_addr);
696 outb(0x55, ioaddr->nsect_addr);
697 outb(0xaa, ioaddr->lbal_addr);
699 nsect = inb(ioaddr->nsect_addr);
700 lbal = inb(ioaddr->lbal_addr);
702 if ((nsect == 0x55) && (lbal == 0xaa))
703 return 1; /* we found a device */
705 return 0; /* nothing found */
709 * ata_mmio_devchk - PATA device presence detection
710 * @ap: ATA channel to examine
711 * @device: Device to examine (starting at zero)
713 * This technique was originally described in
714 * Hale Landis's ATADRVR (www.ata-atapi.com), and
715 * later found its way into the ATA/ATAPI spec.
717 * Write a pattern to the ATA shadow registers,
718 * and if a device is present, it will respond by
719 * correctly storing and echoing back the
720 * ATA shadow register contents.
726 static unsigned int ata_mmio_devchk(struct ata_port *ap,
729 struct ata_ioports *ioaddr = &ap->ioaddr;
732 ap->ops->dev_select(ap, device);
734 writeb(0x55, (void __iomem *) ioaddr->nsect_addr);
735 writeb(0xaa, (void __iomem *) ioaddr->lbal_addr);
737 writeb(0xaa, (void __iomem *) ioaddr->nsect_addr);
738 writeb(0x55, (void __iomem *) ioaddr->lbal_addr);
740 writeb(0x55, (void __iomem *) ioaddr->nsect_addr);
741 writeb(0xaa, (void __iomem *) ioaddr->lbal_addr);
743 nsect = readb((void __iomem *) ioaddr->nsect_addr);
744 lbal = readb((void __iomem *) ioaddr->lbal_addr);
746 if ((nsect == 0x55) && (lbal == 0xaa))
747 return 1; /* we found a device */
749 return 0; /* nothing found */
753 * ata_devchk - PATA device presence detection
754 * @ap: ATA channel to examine
755 * @device: Device to examine (starting at zero)
757 * Dispatch ATA device presence detection, depending
758 * on whether we are using PIO or MMIO to talk to the
759 * ATA shadow registers.
765 static unsigned int ata_devchk(struct ata_port *ap,
768 if (ap->flags & ATA_FLAG_MMIO)
769 return ata_mmio_devchk(ap, device);
770 return ata_pio_devchk(ap, device);
774 * ata_dev_classify - determine device type based on ATA-spec signature
775 * @tf: ATA taskfile register set for device to be identified
777 * Determine from taskfile register contents whether a device is
778 * ATA or ATAPI, as per "Signature and persistence" section
779 * of ATA/PI spec (volume 1, sect 5.14).
785 * Device type, %ATA_DEV_ATA, %ATA_DEV_ATAPI, or %ATA_DEV_UNKNOWN
786 * the event of failure.
789 unsigned int ata_dev_classify(const struct ata_taskfile *tf)
791 /* Apple's open source Darwin code hints that some devices only
792 * put a proper signature into the LBA mid/high registers,
793 * So, we only check those. It's sufficient for uniqueness.
796 if (((tf->lbam == 0) && (tf->lbah == 0)) ||
797 ((tf->lbam == 0x3c) && (tf->lbah == 0xc3))) {
798 DPRINTK("found ATA device by sig\n");
802 if (((tf->lbam == 0x14) && (tf->lbah == 0xeb)) ||
803 ((tf->lbam == 0x69) && (tf->lbah == 0x96))) {
804 DPRINTK("found ATAPI device by sig\n");
805 return ATA_DEV_ATAPI;
808 DPRINTK("unknown device\n");
809 return ATA_DEV_UNKNOWN;
813 * ata_dev_try_classify - Parse returned ATA device signature
814 * @ap: ATA channel to examine
815 * @device: Device to examine (starting at zero)
817 * After an event -- SRST, E.D.D., or SATA COMRESET -- occurs,
818 * an ATA/ATAPI-defined set of values is placed in the ATA
819 * shadow registers, indicating the results of device detection
822 * Select the ATA device, and read the values from the ATA shadow
823 * registers. Then parse according to the Error register value,
824 * and the spec-defined values examined by ata_dev_classify().
830 static u8 ata_dev_try_classify(struct ata_port *ap, unsigned int device)
832 struct ata_device *dev = &ap->device[device];
833 struct ata_taskfile tf;
837 ap->ops->dev_select(ap, device);
839 memset(&tf, 0, sizeof(tf));
841 ap->ops->tf_read(ap, &tf);
844 dev->class = ATA_DEV_NONE;
846 /* see if device passed diags */
849 else if ((device == 0) && (err == 0x81))
854 /* determine if device if ATA or ATAPI */
855 class = ata_dev_classify(&tf);
856 if (class == ATA_DEV_UNKNOWN)
858 if ((class == ATA_DEV_ATA) && (ata_chk_status(ap) == 0))
867 * ata_dev_id_string - Convert IDENTIFY DEVICE page into string
868 * @id: IDENTIFY DEVICE results we will examine
869 * @s: string into which data is output
870 * @ofs: offset into identify device page
871 * @len: length of string to return. must be an even number.
873 * The strings in the IDENTIFY DEVICE page are broken up into
874 * 16-bit chunks. Run through the string, and output each
875 * 8-bit chunk linearly, regardless of platform.
881 void ata_dev_id_string(const u16 *id, unsigned char *s,
882 unsigned int ofs, unsigned int len)
902 * ata_noop_dev_select - Select device 0/1 on ATA bus
903 * @ap: ATA channel to manipulate
904 * @device: ATA device (numbered from zero) to select
906 * This function performs no actual function.
908 * May be used as the dev_select() entry in ata_port_operations.
913 void ata_noop_dev_select (struct ata_port *ap, unsigned int device)
919 * ata_std_dev_select - Select device 0/1 on ATA bus
920 * @ap: ATA channel to manipulate
921 * @device: ATA device (numbered from zero) to select
923 * Use the method defined in the ATA specification to
924 * make either device 0, or device 1, active on the
925 * ATA channel. Works with both PIO and MMIO.
927 * May be used as the dev_select() entry in ata_port_operations.
933 void ata_std_dev_select (struct ata_port *ap, unsigned int device)
938 tmp = ATA_DEVICE_OBS;
940 tmp = ATA_DEVICE_OBS | ATA_DEV1;
942 if (ap->flags & ATA_FLAG_MMIO) {
943 writeb(tmp, (void __iomem *) ap->ioaddr.device_addr);
945 outb(tmp, ap->ioaddr.device_addr);
947 ata_pause(ap); /* needed; also flushes, for mmio */
951 * ata_dev_select - Select device 0/1 on ATA bus
952 * @ap: ATA channel to manipulate
953 * @device: ATA device (numbered from zero) to select
954 * @wait: non-zero to wait for Status register BSY bit to clear
955 * @can_sleep: non-zero if context allows sleeping
957 * Use the method defined in the ATA specification to
958 * make either device 0, or device 1, active on the
961 * This is a high-level version of ata_std_dev_select(),
962 * which additionally provides the services of inserting
963 * the proper pauses and status polling, where needed.
969 void ata_dev_select(struct ata_port *ap, unsigned int device,
970 unsigned int wait, unsigned int can_sleep)
972 VPRINTK("ENTER, ata%u: device %u, wait %u\n",
973 ap->id, device, wait);
978 ap->ops->dev_select(ap, device);
981 if (can_sleep && ap->device[device].class == ATA_DEV_ATAPI)
988 * ata_dump_id - IDENTIFY DEVICE info debugging output
989 * @dev: Device whose IDENTIFY DEVICE page we will dump
991 * Dump selected 16-bit words from a detected device's
992 * IDENTIFY PAGE page.
998 static inline void ata_dump_id(const struct ata_device *dev)
1000 DPRINTK("49==0x%04x "
1010 DPRINTK("80==0x%04x "
1020 DPRINTK("88==0x%04x "
1027 * Compute the PIO modes available for this device. This is not as
1028 * trivial as it seems if we must consider early devices correctly.
1030 * FIXME: pre IDE drive timing (do we care ?).
1033 static unsigned int ata_pio_modes(const struct ata_device *adev)
1037 /* Usual case. Word 53 indicates word 88 is valid */
1038 if (adev->id[ATA_ID_FIELD_VALID] & (1 << 2)) {
1039 modes = adev->id[ATA_ID_PIO_MODES] & 0x03;
1045 /* If word 88 isn't valid then Word 51 holds the PIO timing number
1046 for the maximum. Turn it into a mask and return it */
1047 modes = (2 << (adev->id[ATA_ID_OLD_PIO_MODES] & 0xFF)) - 1 ;
1052 * ata_dev_identify - obtain IDENTIFY x DEVICE page
1053 * @ap: port on which device we wish to probe resides
1054 * @device: device bus address, starting at zero
1056 * Following bus reset, we issue the IDENTIFY [PACKET] DEVICE
1057 * command, and read back the 512-byte device information page.
1058 * The device information page is fed to us via the standard
1059 * PIO-IN protocol, but we hand-code it here. (TODO: investigate
1060 * using standard PIO-IN paths)
1062 * After reading the device information page, we use several
1063 * bits of information from it to initialize data structures
1064 * that will be used during the lifetime of the ata_device.
1065 * Other data from the info page is used to disqualify certain
1066 * older ATA devices we do not wish to support.
1069 * Inherited from caller. Some functions called by this function
1070 * obtain the host_set lock.
1073 static void ata_dev_identify(struct ata_port *ap, unsigned int device)
1075 struct ata_device *dev = &ap->device[device];
1076 unsigned int major_version;
1078 unsigned long xfer_modes;
1079 unsigned int using_edd;
1080 DECLARE_COMPLETION(wait);
1081 struct ata_queued_cmd *qc;
1082 unsigned long flags;
1085 if (!ata_dev_present(dev)) {
1086 DPRINTK("ENTER/EXIT (host %u, dev %u) -- nodev\n",
1091 if (ap->flags & (ATA_FLAG_SRST | ATA_FLAG_SATA_RESET))
1096 DPRINTK("ENTER, host %u, dev %u\n", ap->id, device);
1098 assert (dev->class == ATA_DEV_ATA || dev->class == ATA_DEV_ATAPI ||
1099 dev->class == ATA_DEV_NONE);
1101 ata_dev_select(ap, device, 1, 1); /* select device 0/1 */
1103 qc = ata_qc_new_init(ap, dev);
1106 ata_sg_init_one(qc, dev->id, sizeof(dev->id));
1107 qc->dma_dir = DMA_FROM_DEVICE;
1108 qc->tf.protocol = ATA_PROT_PIO;
1112 if (dev->class == ATA_DEV_ATA) {
1113 qc->tf.command = ATA_CMD_ID_ATA;
1114 DPRINTK("do ATA identify\n");
1116 qc->tf.command = ATA_CMD_ID_ATAPI;
1117 DPRINTK("do ATAPI identify\n");
1120 qc->waiting = &wait;
1121 qc->complete_fn = ata_qc_complete_noop;
1123 spin_lock_irqsave(&ap->host_set->lock, flags);
1124 rc = ata_qc_issue(qc);
1125 spin_unlock_irqrestore(&ap->host_set->lock, flags);
1130 wait_for_completion(&wait);
1132 spin_lock_irqsave(&ap->host_set->lock, flags);
1133 ap->ops->tf_read(ap, &qc->tf);
1134 spin_unlock_irqrestore(&ap->host_set->lock, flags);
1136 if (qc->tf.command & ATA_ERR) {
1138 * arg! EDD works for all test cases, but seems to return
1139 * the ATA signature for some ATAPI devices. Until the
1140 * reason for this is found and fixed, we fix up the mess
1141 * here. If IDENTIFY DEVICE returns command aborted
1142 * (as ATAPI devices do), then we issue an
1143 * IDENTIFY PACKET DEVICE.
1145 * ATA software reset (SRST, the default) does not appear
1146 * to have this problem.
1148 if ((using_edd) && (qc->tf.command == ATA_CMD_ID_ATA)) {
1149 u8 err = qc->tf.feature;
1150 if (err & ATA_ABORTED) {
1151 dev->class = ATA_DEV_ATAPI;
1162 swap_buf_le16(dev->id, ATA_ID_WORDS);
1164 /* print device capabilities */
1165 printk(KERN_DEBUG "ata%u: dev %u cfg "
1166 "49:%04x 82:%04x 83:%04x 84:%04x 85:%04x 86:%04x 87:%04x 88:%04x\n",
1167 ap->id, device, dev->id[49],
1168 dev->id[82], dev->id[83], dev->id[84],
1169 dev->id[85], dev->id[86], dev->id[87],
1173 * common ATA, ATAPI feature tests
1176 /* we require DMA support (bits 8 of word 49) */
1177 if (!ata_id_has_dma(dev->id)) {
1178 printk(KERN_DEBUG "ata%u: no dma\n", ap->id);
1182 /* quick-n-dirty find max transfer mode; for printk only */
1183 xfer_modes = dev->id[ATA_ID_UDMA_MODES];
1185 xfer_modes = (dev->id[ATA_ID_MWDMA_MODES]) << ATA_SHIFT_MWDMA;
1187 xfer_modes = ata_pio_modes(dev);
1191 /* ATA-specific feature tests */
1192 if (dev->class == ATA_DEV_ATA) {
1193 if (!ata_id_is_ata(dev->id)) /* sanity check */
1196 /* get major version */
1197 tmp = dev->id[ATA_ID_MAJOR_VER];
1198 for (major_version = 14; major_version >= 1; major_version--)
1199 if (tmp & (1 << major_version))
1203 * The exact sequence expected by certain pre-ATA4 drives is:
1206 * INITIALIZE DEVICE PARAMETERS
1208 * Some drives were very specific about that exact sequence.
1210 if (major_version < 4 || (!ata_id_has_lba(dev->id))) {
1211 ata_dev_init_params(ap, dev);
1213 /* current CHS translation info (id[53-58]) might be
1214 * changed. reread the identify device info.
1216 ata_dev_reread_id(ap, dev);
1219 if (ata_id_has_lba(dev->id)) {
1220 dev->flags |= ATA_DFLAG_LBA;
1222 if (ata_id_has_lba48(dev->id)) {
1223 dev->flags |= ATA_DFLAG_LBA48;
1224 dev->n_sectors = ata_id_u64(dev->id, 100);
1226 dev->n_sectors = ata_id_u32(dev->id, 60);
1229 /* print device info to dmesg */
1230 printk(KERN_INFO "ata%u: dev %u ATA-%d, max %s, %Lu sectors:%s\n",
1233 ata_mode_string(xfer_modes),
1234 (unsigned long long)dev->n_sectors,
1235 dev->flags & ATA_DFLAG_LBA48 ? " LBA48" : " LBA");
1239 /* Default translation */
1240 dev->cylinders = dev->id[1];
1241 dev->heads = dev->id[3];
1242 dev->sectors = dev->id[6];
1243 dev->n_sectors = dev->cylinders * dev->heads * dev->sectors;
1245 if (ata_id_current_chs_valid(dev->id)) {
1246 /* Current CHS translation is valid. */
1247 dev->cylinders = dev->id[54];
1248 dev->heads = dev->id[55];
1249 dev->sectors = dev->id[56];
1251 dev->n_sectors = ata_id_u32(dev->id, 57);
1254 /* print device info to dmesg */
1255 printk(KERN_INFO "ata%u: dev %u ATA-%d, max %s, %Lu sectors: CHS %d/%d/%d\n",
1258 ata_mode_string(xfer_modes),
1259 (unsigned long long)dev->n_sectors,
1260 (int)dev->cylinders, (int)dev->heads, (int)dev->sectors);
1264 ap->host->max_cmd_len = 16;
1267 /* ATAPI-specific feature tests */
1269 if (ata_id_is_ata(dev->id)) /* sanity check */
1272 rc = atapi_cdb_len(dev->id);
1273 if ((rc < 12) || (rc > ATAPI_CDB_LEN)) {
1274 printk(KERN_WARNING "ata%u: unsupported CDB len\n", ap->id);
1277 ap->cdb_len = (unsigned int) rc;
1278 ap->host->max_cmd_len = (unsigned char) ap->cdb_len;
1280 if (ata_id_cdb_intr(dev->id))
1281 dev->flags |= ATA_DFLAG_CDB_INTR;
1283 /* print device info to dmesg */
1284 printk(KERN_INFO "ata%u: dev %u ATAPI, max %s\n",
1286 ata_mode_string(xfer_modes));
1289 DPRINTK("EXIT, drv_stat = 0x%x\n", ata_chk_status(ap));
1293 printk(KERN_WARNING "ata%u: dev %u not supported, ignoring\n",
1296 dev->class++; /* converts ATA_DEV_xxx into ATA_DEV_xxx_UNSUP */
1297 DPRINTK("EXIT, err\n");
1301 static inline u8 ata_dev_knobble(const struct ata_port *ap)
1303 return ((ap->cbl == ATA_CBL_SATA) && (!ata_id_is_sata(ap->device->id)));
1307 * ata_dev_config - Run device specific handlers and check for
1308 * SATA->PATA bridges
1315 void ata_dev_config(struct ata_port *ap, unsigned int i)
1317 /* limit bridge transfers to udma5, 200 sectors */
1318 if (ata_dev_knobble(ap)) {
1319 printk(KERN_INFO "ata%u(%u): applying bridge limits\n",
1320 ap->id, ap->device->devno);
1321 ap->udma_mask &= ATA_UDMA5;
1322 ap->host->max_sectors = ATA_MAX_SECTORS;
1323 ap->host->hostt->max_sectors = ATA_MAX_SECTORS;
1324 ap->device->flags |= ATA_DFLAG_LOCK_SECTORS;
1327 if (ap->ops->dev_config)
1328 ap->ops->dev_config(ap, &ap->device[i]);
1332 * ata_bus_probe - Reset and probe ATA bus
1335 * Master ATA bus probing function. Initiates a hardware-dependent
1336 * bus reset, then attempts to identify any devices found on
1340 * PCI/etc. bus probe sem.
1343 * Zero on success, non-zero on error.
1346 static int ata_bus_probe(struct ata_port *ap)
1348 unsigned int i, found = 0;
1350 ap->ops->phy_reset(ap);
1351 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1354 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1355 ata_dev_identify(ap, i);
1356 if (ata_dev_present(&ap->device[i])) {
1358 ata_dev_config(ap,i);
1362 if ((!found) || (ap->flags & ATA_FLAG_PORT_DISABLED))
1363 goto err_out_disable;
1366 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1367 goto err_out_disable;
1372 ap->ops->port_disable(ap);
1378 * ata_port_probe - Mark port as enabled
1379 * @ap: Port for which we indicate enablement
1381 * Modify @ap data structure such that the system
1382 * thinks that the entire port is enabled.
1384 * LOCKING: host_set lock, or some other form of
1388 void ata_port_probe(struct ata_port *ap)
1390 ap->flags &= ~ATA_FLAG_PORT_DISABLED;
1394 * __sata_phy_reset - Wake/reset a low-level SATA PHY
1395 * @ap: SATA port associated with target SATA PHY.
1397 * This function issues commands to standard SATA Sxxx
1398 * PHY registers, to wake up the phy (and device), and
1399 * clear any reset condition.
1402 * PCI/etc. bus probe sem.
1405 void __sata_phy_reset(struct ata_port *ap)
1408 unsigned long timeout = jiffies + (HZ * 5);
1410 if (ap->flags & ATA_FLAG_SATA_RESET) {
1411 /* issue phy wake/reset */
1412 scr_write_flush(ap, SCR_CONTROL, 0x301);
1413 /* Couldn't find anything in SATA I/II specs, but
1414 * AHCI-1.1 10.4.2 says at least 1 ms. */
1417 scr_write_flush(ap, SCR_CONTROL, 0x300); /* phy wake/clear reset */
1419 /* wait for phy to become ready, if necessary */
1422 sstatus = scr_read(ap, SCR_STATUS);
1423 if ((sstatus & 0xf) != 1)
1425 } while (time_before(jiffies, timeout));
1427 /* TODO: phy layer with polling, timeouts, etc. */
1428 if (sata_dev_present(ap))
1431 sstatus = scr_read(ap, SCR_STATUS);
1432 printk(KERN_INFO "ata%u: no device found (phy stat %08x)\n",
1434 ata_port_disable(ap);
1437 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1440 if (ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT)) {
1441 ata_port_disable(ap);
1445 ap->cbl = ATA_CBL_SATA;
1449 * sata_phy_reset - Reset SATA bus.
1450 * @ap: SATA port associated with target SATA PHY.
1452 * This function resets the SATA bus, and then probes
1453 * the bus for devices.
1456 * PCI/etc. bus probe sem.
1459 void sata_phy_reset(struct ata_port *ap)
1461 __sata_phy_reset(ap);
1462 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1468 * ata_port_disable - Disable port.
1469 * @ap: Port to be disabled.
1471 * Modify @ap data structure such that the system
1472 * thinks that the entire port is disabled, and should
1473 * never attempt to probe or communicate with devices
1476 * LOCKING: host_set lock, or some other form of
1480 void ata_port_disable(struct ata_port *ap)
1482 ap->device[0].class = ATA_DEV_NONE;
1483 ap->device[1].class = ATA_DEV_NONE;
1484 ap->flags |= ATA_FLAG_PORT_DISABLED;
1488 * This mode timing computation functionality is ported over from
1489 * drivers/ide/ide-timing.h and was originally written by Vojtech Pavlik
1492 * PIO 0-5, MWDMA 0-2 and UDMA 0-6 timings (in nanoseconds).
1493 * These were taken from ATA/ATAPI-6 standard, rev 0a, except
1494 * for PIO 5, which is a nonstandard extension and UDMA6, which
1495 * is currently supported only by Maxtor drives.
1498 static const struct ata_timing ata_timing[] = {
1500 { XFER_UDMA_6, 0, 0, 0, 0, 0, 0, 0, 15 },
1501 { XFER_UDMA_5, 0, 0, 0, 0, 0, 0, 0, 20 },
1502 { XFER_UDMA_4, 0, 0, 0, 0, 0, 0, 0, 30 },
1503 { XFER_UDMA_3, 0, 0, 0, 0, 0, 0, 0, 45 },
1505 { XFER_UDMA_2, 0, 0, 0, 0, 0, 0, 0, 60 },
1506 { XFER_UDMA_1, 0, 0, 0, 0, 0, 0, 0, 80 },
1507 { XFER_UDMA_0, 0, 0, 0, 0, 0, 0, 0, 120 },
1509 /* { XFER_UDMA_SLOW, 0, 0, 0, 0, 0, 0, 0, 150 }, */
1511 { XFER_MW_DMA_2, 25, 0, 0, 0, 70, 25, 120, 0 },
1512 { XFER_MW_DMA_1, 45, 0, 0, 0, 80, 50, 150, 0 },
1513 { XFER_MW_DMA_0, 60, 0, 0, 0, 215, 215, 480, 0 },
1515 { XFER_SW_DMA_2, 60, 0, 0, 0, 120, 120, 240, 0 },
1516 { XFER_SW_DMA_1, 90, 0, 0, 0, 240, 240, 480, 0 },
1517 { XFER_SW_DMA_0, 120, 0, 0, 0, 480, 480, 960, 0 },
1519 /* { XFER_PIO_5, 20, 50, 30, 100, 50, 30, 100, 0 }, */
1520 { XFER_PIO_4, 25, 70, 25, 120, 70, 25, 120, 0 },
1521 { XFER_PIO_3, 30, 80, 70, 180, 80, 70, 180, 0 },
1523 { XFER_PIO_2, 30, 290, 40, 330, 100, 90, 240, 0 },
1524 { XFER_PIO_1, 50, 290, 93, 383, 125, 100, 383, 0 },
1525 { XFER_PIO_0, 70, 290, 240, 600, 165, 150, 600, 0 },
1527 /* { XFER_PIO_SLOW, 120, 290, 240, 960, 290, 240, 960, 0 }, */
1532 #define ENOUGH(v,unit) (((v)-1)/(unit)+1)
1533 #define EZ(v,unit) ((v)?ENOUGH(v,unit):0)
1535 static void ata_timing_quantize(const struct ata_timing *t, struct ata_timing *q, int T, int UT)
1537 q->setup = EZ(t->setup * 1000, T);
1538 q->act8b = EZ(t->act8b * 1000, T);
1539 q->rec8b = EZ(t->rec8b * 1000, T);
1540 q->cyc8b = EZ(t->cyc8b * 1000, T);
1541 q->active = EZ(t->active * 1000, T);
1542 q->recover = EZ(t->recover * 1000, T);
1543 q->cycle = EZ(t->cycle * 1000, T);
1544 q->udma = EZ(t->udma * 1000, UT);
1547 void ata_timing_merge(const struct ata_timing *a, const struct ata_timing *b,
1548 struct ata_timing *m, unsigned int what)
1550 if (what & ATA_TIMING_SETUP ) m->setup = max(a->setup, b->setup);
1551 if (what & ATA_TIMING_ACT8B ) m->act8b = max(a->act8b, b->act8b);
1552 if (what & ATA_TIMING_REC8B ) m->rec8b = max(a->rec8b, b->rec8b);
1553 if (what & ATA_TIMING_CYC8B ) m->cyc8b = max(a->cyc8b, b->cyc8b);
1554 if (what & ATA_TIMING_ACTIVE ) m->active = max(a->active, b->active);
1555 if (what & ATA_TIMING_RECOVER) m->recover = max(a->recover, b->recover);
1556 if (what & ATA_TIMING_CYCLE ) m->cycle = max(a->cycle, b->cycle);
1557 if (what & ATA_TIMING_UDMA ) m->udma = max(a->udma, b->udma);
1560 static const struct ata_timing* ata_timing_find_mode(unsigned short speed)
1562 const struct ata_timing *t;
1564 for (t = ata_timing; t->mode != speed; t++)
1565 if (t->mode == 0xFF)
1570 int ata_timing_compute(struct ata_device *adev, unsigned short speed,
1571 struct ata_timing *t, int T, int UT)
1573 const struct ata_timing *s;
1574 struct ata_timing p;
1580 if (!(s = ata_timing_find_mode(speed)))
1584 * If the drive is an EIDE drive, it can tell us it needs extended
1585 * PIO/MW_DMA cycle timing.
1588 if (adev->id[ATA_ID_FIELD_VALID] & 2) { /* EIDE drive */
1589 memset(&p, 0, sizeof(p));
1590 if(speed >= XFER_PIO_0 && speed <= XFER_SW_DMA_0) {
1591 if (speed <= XFER_PIO_2) p.cycle = p.cyc8b = adev->id[ATA_ID_EIDE_PIO];
1592 else p.cycle = p.cyc8b = adev->id[ATA_ID_EIDE_PIO_IORDY];
1593 } else if(speed >= XFER_MW_DMA_0 && speed <= XFER_MW_DMA_2) {
1594 p.cycle = adev->id[ATA_ID_EIDE_DMA_MIN];
1596 ata_timing_merge(&p, t, t, ATA_TIMING_CYCLE | ATA_TIMING_CYC8B);
1600 * Convert the timing to bus clock counts.
1603 ata_timing_quantize(s, t, T, UT);
1606 * Even in DMA/UDMA modes we still use PIO access for IDENTIFY, S.M.A.R.T
1607 * and some other commands. We have to ensure that the DMA cycle timing is
1608 * slower/equal than the fastest PIO timing.
1611 if (speed > XFER_PIO_4) {
1612 ata_timing_compute(adev, adev->pio_mode, &p, T, UT);
1613 ata_timing_merge(&p, t, t, ATA_TIMING_ALL);
1617 * Lenghten active & recovery time so that cycle time is correct.
1620 if (t->act8b + t->rec8b < t->cyc8b) {
1621 t->act8b += (t->cyc8b - (t->act8b + t->rec8b)) / 2;
1622 t->rec8b = t->cyc8b - t->act8b;
1625 if (t->active + t->recover < t->cycle) {
1626 t->active += (t->cycle - (t->active + t->recover)) / 2;
1627 t->recover = t->cycle - t->active;
1633 static const struct {
1636 } xfer_mode_classes[] = {
1637 { ATA_SHIFT_UDMA, XFER_UDMA_0 },
1638 { ATA_SHIFT_MWDMA, XFER_MW_DMA_0 },
1639 { ATA_SHIFT_PIO, XFER_PIO_0 },
1642 static inline u8 base_from_shift(unsigned int shift)
1646 for (i = 0; i < ARRAY_SIZE(xfer_mode_classes); i++)
1647 if (xfer_mode_classes[i].shift == shift)
1648 return xfer_mode_classes[i].base;
1653 static void ata_dev_set_mode(struct ata_port *ap, struct ata_device *dev)
1658 if (!ata_dev_present(dev) || (ap->flags & ATA_FLAG_PORT_DISABLED))
1661 if (dev->xfer_shift == ATA_SHIFT_PIO)
1662 dev->flags |= ATA_DFLAG_PIO;
1664 ata_dev_set_xfermode(ap, dev);
1666 base = base_from_shift(dev->xfer_shift);
1667 ofs = dev->xfer_mode - base;
1668 idx = ofs + dev->xfer_shift;
1669 WARN_ON(idx >= ARRAY_SIZE(xfer_mode_str));
1671 DPRINTK("idx=%d xfer_shift=%u, xfer_mode=0x%x, base=0x%x, offset=%d\n",
1672 idx, dev->xfer_shift, (int)dev->xfer_mode, (int)base, ofs);
1674 printk(KERN_INFO "ata%u: dev %u configured for %s\n",
1675 ap->id, dev->devno, xfer_mode_str[idx]);
1678 static int ata_host_set_pio(struct ata_port *ap)
1684 mask = ata_get_mode_mask(ap, ATA_SHIFT_PIO);
1687 printk(KERN_WARNING "ata%u: no PIO support\n", ap->id);
1691 base = base_from_shift(ATA_SHIFT_PIO);
1692 xfer_mode = base + x;
1694 DPRINTK("base 0x%x xfer_mode 0x%x mask 0x%x x %d\n",
1695 (int)base, (int)xfer_mode, mask, x);
1697 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1698 struct ata_device *dev = &ap->device[i];
1699 if (ata_dev_present(dev)) {
1700 dev->pio_mode = xfer_mode;
1701 dev->xfer_mode = xfer_mode;
1702 dev->xfer_shift = ATA_SHIFT_PIO;
1703 if (ap->ops->set_piomode)
1704 ap->ops->set_piomode(ap, dev);
1711 static void ata_host_set_dma(struct ata_port *ap, u8 xfer_mode,
1712 unsigned int xfer_shift)
1716 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1717 struct ata_device *dev = &ap->device[i];
1718 if (ata_dev_present(dev)) {
1719 dev->dma_mode = xfer_mode;
1720 dev->xfer_mode = xfer_mode;
1721 dev->xfer_shift = xfer_shift;
1722 if (ap->ops->set_dmamode)
1723 ap->ops->set_dmamode(ap, dev);
1729 * ata_set_mode - Program timings and issue SET FEATURES - XFER
1730 * @ap: port on which timings will be programmed
1732 * Set ATA device disk transfer mode (PIO3, UDMA6, etc.).
1735 * PCI/etc. bus probe sem.
1738 static void ata_set_mode(struct ata_port *ap)
1740 unsigned int xfer_shift;
1744 /* step 1: always set host PIO timings */
1745 rc = ata_host_set_pio(ap);
1749 /* step 2: choose the best data xfer mode */
1750 xfer_mode = xfer_shift = 0;
1751 rc = ata_choose_xfer_mode(ap, &xfer_mode, &xfer_shift);
1755 /* step 3: if that xfer mode isn't PIO, set host DMA timings */
1756 if (xfer_shift != ATA_SHIFT_PIO)
1757 ata_host_set_dma(ap, xfer_mode, xfer_shift);
1759 /* step 4: update devices' xfer mode */
1760 ata_dev_set_mode(ap, &ap->device[0]);
1761 ata_dev_set_mode(ap, &ap->device[1]);
1763 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1766 if (ap->ops->post_set_mode)
1767 ap->ops->post_set_mode(ap);
1772 ata_port_disable(ap);
1776 * ata_busy_sleep - sleep until BSY clears, or timeout
1777 * @ap: port containing status register to be polled
1778 * @tmout_pat: impatience timeout
1779 * @tmout: overall timeout
1781 * Sleep until ATA Status register bit BSY clears,
1782 * or a timeout occurs.
1788 static unsigned int ata_busy_sleep (struct ata_port *ap,
1789 unsigned long tmout_pat,
1790 unsigned long tmout)
1792 unsigned long timer_start, timeout;
1795 status = ata_busy_wait(ap, ATA_BUSY, 300);
1796 timer_start = jiffies;
1797 timeout = timer_start + tmout_pat;
1798 while ((status & ATA_BUSY) && (time_before(jiffies, timeout))) {
1800 status = ata_busy_wait(ap, ATA_BUSY, 3);
1803 if (status & ATA_BUSY)
1804 printk(KERN_WARNING "ata%u is slow to respond, "
1805 "please be patient\n", ap->id);
1807 timeout = timer_start + tmout;
1808 while ((status & ATA_BUSY) && (time_before(jiffies, timeout))) {
1810 status = ata_chk_status(ap);
1813 if (status & ATA_BUSY) {
1814 printk(KERN_ERR "ata%u failed to respond (%lu secs)\n",
1815 ap->id, tmout / HZ);
1822 static void ata_bus_post_reset(struct ata_port *ap, unsigned int devmask)
1824 struct ata_ioports *ioaddr = &ap->ioaddr;
1825 unsigned int dev0 = devmask & (1 << 0);
1826 unsigned int dev1 = devmask & (1 << 1);
1827 unsigned long timeout;
1829 /* if device 0 was found in ata_devchk, wait for its
1833 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
1835 /* if device 1 was found in ata_devchk, wait for
1836 * register access, then wait for BSY to clear
1838 timeout = jiffies + ATA_TMOUT_BOOT;
1842 ap->ops->dev_select(ap, 1);
1843 if (ap->flags & ATA_FLAG_MMIO) {
1844 nsect = readb((void __iomem *) ioaddr->nsect_addr);
1845 lbal = readb((void __iomem *) ioaddr->lbal_addr);
1847 nsect = inb(ioaddr->nsect_addr);
1848 lbal = inb(ioaddr->lbal_addr);
1850 if ((nsect == 1) && (lbal == 1))
1852 if (time_after(jiffies, timeout)) {
1856 msleep(50); /* give drive a breather */
1859 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
1861 /* is all this really necessary? */
1862 ap->ops->dev_select(ap, 0);
1864 ap->ops->dev_select(ap, 1);
1866 ap->ops->dev_select(ap, 0);
1870 * ata_bus_edd - Issue EXECUTE DEVICE DIAGNOSTIC command.
1871 * @ap: Port to reset and probe
1873 * Use the EXECUTE DEVICE DIAGNOSTIC command to reset and
1874 * probe the bus. Not often used these days.
1877 * PCI/etc. bus probe sem.
1878 * Obtains host_set lock.
1882 static unsigned int ata_bus_edd(struct ata_port *ap)
1884 struct ata_taskfile tf;
1885 unsigned long flags;
1887 /* set up execute-device-diag (bus reset) taskfile */
1888 /* also, take interrupts to a known state (disabled) */
1889 DPRINTK("execute-device-diag\n");
1890 ata_tf_init(ap, &tf, 0);
1892 tf.command = ATA_CMD_EDD;
1893 tf.protocol = ATA_PROT_NODATA;
1896 spin_lock_irqsave(&ap->host_set->lock, flags);
1897 ata_tf_to_host(ap, &tf);
1898 spin_unlock_irqrestore(&ap->host_set->lock, flags);
1900 /* spec says at least 2ms. but who knows with those
1901 * crazy ATAPI devices...
1905 return ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
1908 static unsigned int ata_bus_softreset(struct ata_port *ap,
1909 unsigned int devmask)
1911 struct ata_ioports *ioaddr = &ap->ioaddr;
1913 DPRINTK("ata%u: bus reset via SRST\n", ap->id);
1915 /* software reset. causes dev0 to be selected */
1916 if (ap->flags & ATA_FLAG_MMIO) {
1917 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
1918 udelay(20); /* FIXME: flush */
1919 writeb(ap->ctl | ATA_SRST, (void __iomem *) ioaddr->ctl_addr);
1920 udelay(20); /* FIXME: flush */
1921 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
1923 outb(ap->ctl, ioaddr->ctl_addr);
1925 outb(ap->ctl | ATA_SRST, ioaddr->ctl_addr);
1927 outb(ap->ctl, ioaddr->ctl_addr);
1930 /* spec mandates ">= 2ms" before checking status.
1931 * We wait 150ms, because that was the magic delay used for
1932 * ATAPI devices in Hale Landis's ATADRVR, for the period of time
1933 * between when the ATA command register is written, and then
1934 * status is checked. Because waiting for "a while" before
1935 * checking status is fine, post SRST, we perform this magic
1936 * delay here as well.
1940 ata_bus_post_reset(ap, devmask);
1946 * ata_bus_reset - reset host port and associated ATA channel
1947 * @ap: port to reset
1949 * This is typically the first time we actually start issuing
1950 * commands to the ATA channel. We wait for BSY to clear, then
1951 * issue EXECUTE DEVICE DIAGNOSTIC command, polling for its
1952 * result. Determine what devices, if any, are on the channel
1953 * by looking at the device 0/1 error register. Look at the signature
1954 * stored in each device's taskfile registers, to determine if
1955 * the device is ATA or ATAPI.
1958 * PCI/etc. bus probe sem.
1959 * Obtains host_set lock.
1962 * Sets ATA_FLAG_PORT_DISABLED if bus reset fails.
1965 void ata_bus_reset(struct ata_port *ap)
1967 struct ata_ioports *ioaddr = &ap->ioaddr;
1968 unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS;
1970 unsigned int dev0, dev1 = 0, rc = 0, devmask = 0;
1972 DPRINTK("ENTER, host %u, port %u\n", ap->id, ap->port_no);
1974 /* determine if device 0/1 are present */
1975 if (ap->flags & ATA_FLAG_SATA_RESET)
1978 dev0 = ata_devchk(ap, 0);
1980 dev1 = ata_devchk(ap, 1);
1984 devmask |= (1 << 0);
1986 devmask |= (1 << 1);
1988 /* select device 0 again */
1989 ap->ops->dev_select(ap, 0);
1991 /* issue bus reset */
1992 if (ap->flags & ATA_FLAG_SRST)
1993 rc = ata_bus_softreset(ap, devmask);
1994 else if ((ap->flags & ATA_FLAG_SATA_RESET) == 0) {
1995 /* set up device control */
1996 if (ap->flags & ATA_FLAG_MMIO)
1997 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
1999 outb(ap->ctl, ioaddr->ctl_addr);
2000 rc = ata_bus_edd(ap);
2007 * determine by signature whether we have ATA or ATAPI devices
2009 err = ata_dev_try_classify(ap, 0);
2010 if ((slave_possible) && (err != 0x81))
2011 ata_dev_try_classify(ap, 1);
2013 /* re-enable interrupts */
2014 if (ap->ioaddr.ctl_addr) /* FIXME: hack. create a hook instead */
2017 /* is double-select really necessary? */
2018 if (ap->device[1].class != ATA_DEV_NONE)
2019 ap->ops->dev_select(ap, 1);
2020 if (ap->device[0].class != ATA_DEV_NONE)
2021 ap->ops->dev_select(ap, 0);
2023 /* if no devices were detected, disable this port */
2024 if ((ap->device[0].class == ATA_DEV_NONE) &&
2025 (ap->device[1].class == ATA_DEV_NONE))
2028 if (ap->flags & (ATA_FLAG_SATA_RESET | ATA_FLAG_SRST)) {
2029 /* set up device control for ATA_FLAG_SATA_RESET */
2030 if (ap->flags & ATA_FLAG_MMIO)
2031 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
2033 outb(ap->ctl, ioaddr->ctl_addr);
2040 printk(KERN_ERR "ata%u: disabling port\n", ap->id);
2041 ap->ops->port_disable(ap);
2046 static void ata_pr_blacklisted(const struct ata_port *ap,
2047 const struct ata_device *dev)
2049 printk(KERN_WARNING "ata%u: dev %u is on DMA blacklist, disabling DMA\n",
2050 ap->id, dev->devno);
2053 static const char * ata_dma_blacklist [] = {
2072 "Toshiba CD-ROM XM-6202B",
2073 "TOSHIBA CD-ROM XM-1702BC",
2075 "E-IDE CD-ROM CR-840",
2078 "SAMSUNG CD-ROM SC-148C",
2079 "SAMSUNG CD-ROM SC",
2081 "ATAPI CD-ROM DRIVE 40X MAXIMUM",
2085 static int ata_dma_blacklisted(const struct ata_device *dev)
2087 unsigned char model_num[40];
2092 ata_dev_id_string(dev->id, model_num, ATA_ID_PROD_OFS,
2095 len = strnlen(s, sizeof(model_num));
2097 /* ATAPI specifies that empty space is blank-filled; remove blanks */
2098 while ((len > 0) && (s[len - 1] == ' ')) {
2103 for (i = 0; i < ARRAY_SIZE(ata_dma_blacklist); i++)
2104 if (!strncmp(ata_dma_blacklist[i], s, len))
2110 static unsigned int ata_get_mode_mask(const struct ata_port *ap, int shift)
2112 const struct ata_device *master, *slave;
2115 master = &ap->device[0];
2116 slave = &ap->device[1];
2118 assert (ata_dev_present(master) || ata_dev_present(slave));
2120 if (shift == ATA_SHIFT_UDMA) {
2121 mask = ap->udma_mask;
2122 if (ata_dev_present(master)) {
2123 mask &= (master->id[ATA_ID_UDMA_MODES] & 0xff);
2124 if (ata_dma_blacklisted(master)) {
2126 ata_pr_blacklisted(ap, master);
2129 if (ata_dev_present(slave)) {
2130 mask &= (slave->id[ATA_ID_UDMA_MODES] & 0xff);
2131 if (ata_dma_blacklisted(slave)) {
2133 ata_pr_blacklisted(ap, slave);
2137 else if (shift == ATA_SHIFT_MWDMA) {
2138 mask = ap->mwdma_mask;
2139 if (ata_dev_present(master)) {
2140 mask &= (master->id[ATA_ID_MWDMA_MODES] & 0x07);
2141 if (ata_dma_blacklisted(master)) {
2143 ata_pr_blacklisted(ap, master);
2146 if (ata_dev_present(slave)) {
2147 mask &= (slave->id[ATA_ID_MWDMA_MODES] & 0x07);
2148 if (ata_dma_blacklisted(slave)) {
2150 ata_pr_blacklisted(ap, slave);
2154 else if (shift == ATA_SHIFT_PIO) {
2155 mask = ap->pio_mask;
2156 if (ata_dev_present(master)) {
2157 /* spec doesn't return explicit support for
2158 * PIO0-2, so we fake it
2160 u16 tmp_mode = master->id[ATA_ID_PIO_MODES] & 0x03;
2165 if (ata_dev_present(slave)) {
2166 /* spec doesn't return explicit support for
2167 * PIO0-2, so we fake it
2169 u16 tmp_mode = slave->id[ATA_ID_PIO_MODES] & 0x03;
2176 mask = 0xffffffff; /* shut up compiler warning */
2183 /* find greatest bit */
2184 static int fgb(u32 bitmap)
2189 for (i = 0; i < 32; i++)
2190 if (bitmap & (1 << i))
2197 * ata_choose_xfer_mode - attempt to find best transfer mode
2198 * @ap: Port for which an xfer mode will be selected
2199 * @xfer_mode_out: (output) SET FEATURES - XFER MODE code
2200 * @xfer_shift_out: (output) bit shift that selects this mode
2202 * Based on host and device capabilities, determine the
2203 * maximum transfer mode that is amenable to all.
2206 * PCI/etc. bus probe sem.
2209 * Zero on success, negative on error.
2212 static int ata_choose_xfer_mode(const struct ata_port *ap,
2214 unsigned int *xfer_shift_out)
2216 unsigned int mask, shift;
2219 for (i = 0; i < ARRAY_SIZE(xfer_mode_classes); i++) {
2220 shift = xfer_mode_classes[i].shift;
2221 mask = ata_get_mode_mask(ap, shift);
2225 *xfer_mode_out = xfer_mode_classes[i].base + x;
2226 *xfer_shift_out = shift;
2235 * ata_dev_set_xfermode - Issue SET FEATURES - XFER MODE command
2236 * @ap: Port associated with device @dev
2237 * @dev: Device to which command will be sent
2239 * Issue SET FEATURES - XFER MODE command to device @dev
2243 * PCI/etc. bus probe sem.
2246 static void ata_dev_set_xfermode(struct ata_port *ap, struct ata_device *dev)
2248 DECLARE_COMPLETION(wait);
2249 struct ata_queued_cmd *qc;
2251 unsigned long flags;
2253 /* set up set-features taskfile */
2254 DPRINTK("set features - xfer mode\n");
2256 qc = ata_qc_new_init(ap, dev);
2259 qc->tf.command = ATA_CMD_SET_FEATURES;
2260 qc->tf.feature = SETFEATURES_XFER;
2261 qc->tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
2262 qc->tf.protocol = ATA_PROT_NODATA;
2263 qc->tf.nsect = dev->xfer_mode;
2265 qc->waiting = &wait;
2266 qc->complete_fn = ata_qc_complete_noop;
2268 spin_lock_irqsave(&ap->host_set->lock, flags);
2269 rc = ata_qc_issue(qc);
2270 spin_unlock_irqrestore(&ap->host_set->lock, flags);
2273 ata_port_disable(ap);
2275 wait_for_completion(&wait);
2281 * ata_dev_reread_id - Reread the device identify device info
2282 * @ap: port where the device is
2283 * @dev: device to reread the identify device info
2288 static void ata_dev_reread_id(struct ata_port *ap, struct ata_device *dev)
2290 DECLARE_COMPLETION(wait);
2291 struct ata_queued_cmd *qc;
2292 unsigned long flags;
2295 qc = ata_qc_new_init(ap, dev);
2298 ata_sg_init_one(qc, dev->id, sizeof(dev->id));
2299 qc->dma_dir = DMA_FROM_DEVICE;
2301 if (dev->class == ATA_DEV_ATA) {
2302 qc->tf.command = ATA_CMD_ID_ATA;
2303 DPRINTK("do ATA identify\n");
2305 qc->tf.command = ATA_CMD_ID_ATAPI;
2306 DPRINTK("do ATAPI identify\n");
2309 qc->tf.flags |= ATA_TFLAG_DEVICE;
2310 qc->tf.protocol = ATA_PROT_PIO;
2313 qc->waiting = &wait;
2314 qc->complete_fn = ata_qc_complete_noop;
2316 spin_lock_irqsave(&ap->host_set->lock, flags);
2317 rc = ata_qc_issue(qc);
2318 spin_unlock_irqrestore(&ap->host_set->lock, flags);
2323 wait_for_completion(&wait);
2325 swap_buf_le16(dev->id, ATA_ID_WORDS);
2333 ata_port_disable(ap);
2337 * ata_dev_init_params - Issue INIT DEV PARAMS command
2338 * @ap: Port associated with device @dev
2339 * @dev: Device to which command will be sent
2344 static void ata_dev_init_params(struct ata_port *ap, struct ata_device *dev)
2346 DECLARE_COMPLETION(wait);
2347 struct ata_queued_cmd *qc;
2349 unsigned long flags;
2350 u16 sectors = dev->id[6];
2351 u16 heads = dev->id[3];
2353 /* Number of sectors per track 1-255. Number of heads 1-16 */
2354 if (sectors < 1 || sectors > 255 || heads < 1 || heads > 16)
2357 /* set up init dev params taskfile */
2358 DPRINTK("init dev params \n");
2360 qc = ata_qc_new_init(ap, dev);
2363 qc->tf.command = ATA_CMD_INIT_DEV_PARAMS;
2364 qc->tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
2365 qc->tf.protocol = ATA_PROT_NODATA;
2366 qc->tf.nsect = sectors;
2367 qc->tf.device |= (heads - 1) & 0x0f; /* max head = num. of heads - 1 */
2369 qc->waiting = &wait;
2370 qc->complete_fn = ata_qc_complete_noop;
2372 spin_lock_irqsave(&ap->host_set->lock, flags);
2373 rc = ata_qc_issue(qc);
2374 spin_unlock_irqrestore(&ap->host_set->lock, flags);
2377 ata_port_disable(ap);
2379 wait_for_completion(&wait);
2385 * ata_sg_clean - Unmap DMA memory associated with command
2386 * @qc: Command containing DMA memory to be released
2388 * Unmap all mapped DMA memory associated with this command.
2391 * spin_lock_irqsave(host_set lock)
2394 static void ata_sg_clean(struct ata_queued_cmd *qc)
2396 struct ata_port *ap = qc->ap;
2397 struct scatterlist *sg = qc->sg;
2398 int dir = qc->dma_dir;
2400 assert(qc->flags & ATA_QCFLAG_DMAMAP);
2403 if (qc->flags & ATA_QCFLAG_SINGLE)
2404 assert(qc->n_elem == 1);
2406 DPRINTK("unmapping %u sg elements\n", qc->n_elem);
2408 if (qc->flags & ATA_QCFLAG_SG)
2409 dma_unmap_sg(ap->host_set->dev, sg, qc->n_elem, dir);
2411 dma_unmap_single(ap->host_set->dev, sg_dma_address(&sg[0]),
2412 sg_dma_len(&sg[0]), dir);
2414 qc->flags &= ~ATA_QCFLAG_DMAMAP;
2419 * ata_fill_sg - Fill PCI IDE PRD table
2420 * @qc: Metadata associated with taskfile to be transferred
2422 * Fill PCI IDE PRD (scatter-gather) table with segments
2423 * associated with the current disk command.
2426 * spin_lock_irqsave(host_set lock)
2429 static void ata_fill_sg(struct ata_queued_cmd *qc)
2431 struct scatterlist *sg = qc->sg;
2432 struct ata_port *ap = qc->ap;
2433 unsigned int idx, nelem;
2436 assert(qc->n_elem > 0);
2439 for (nelem = qc->n_elem; nelem; nelem--,sg++) {
2443 /* determine if physical DMA addr spans 64K boundary.
2444 * Note h/w doesn't support 64-bit, so we unconditionally
2445 * truncate dma_addr_t to u32.
2447 addr = (u32) sg_dma_address(sg);
2448 sg_len = sg_dma_len(sg);
2451 offset = addr & 0xffff;
2453 if ((offset + sg_len) > 0x10000)
2454 len = 0x10000 - offset;
2456 ap->prd[idx].addr = cpu_to_le32(addr);
2457 ap->prd[idx].flags_len = cpu_to_le32(len & 0xffff);
2458 VPRINTK("PRD[%u] = (0x%X, 0x%X)\n", idx, addr, len);
2467 ap->prd[idx - 1].flags_len |= cpu_to_le32(ATA_PRD_EOT);
2470 * ata_check_atapi_dma - Check whether ATAPI DMA can be supported
2471 * @qc: Metadata associated with taskfile to check
2473 * Allow low-level driver to filter ATA PACKET commands, returning
2474 * a status indicating whether or not it is OK to use DMA for the
2475 * supplied PACKET command.
2478 * spin_lock_irqsave(host_set lock)
2480 * RETURNS: 0 when ATAPI DMA can be used
2483 int ata_check_atapi_dma(struct ata_queued_cmd *qc)
2485 struct ata_port *ap = qc->ap;
2486 int rc = 0; /* Assume ATAPI DMA is OK by default */
2488 if (ap->ops->check_atapi_dma)
2489 rc = ap->ops->check_atapi_dma(qc);
2494 * ata_qc_prep - Prepare taskfile for submission
2495 * @qc: Metadata associated with taskfile to be prepared
2497 * Prepare ATA taskfile for submission.
2500 * spin_lock_irqsave(host_set lock)
2502 void ata_qc_prep(struct ata_queued_cmd *qc)
2504 if (!(qc->flags & ATA_QCFLAG_DMAMAP))
2511 * ata_sg_init_one - Associate command with memory buffer
2512 * @qc: Command to be associated
2513 * @buf: Memory buffer
2514 * @buflen: Length of memory buffer, in bytes.
2516 * Initialize the data-related elements of queued_cmd @qc
2517 * to point to a single memory buffer, @buf of byte length @buflen.
2520 * spin_lock_irqsave(host_set lock)
2523 void ata_sg_init_one(struct ata_queued_cmd *qc, void *buf, unsigned int buflen)
2525 qc->flags |= ATA_QCFLAG_SINGLE;
2527 qc->sg = &qc->sgent;
2530 sg_init_one(qc->sg, buf, buflen);
2534 * ata_sg_init - Associate command with scatter-gather table.
2535 * @qc: Command to be associated
2536 * @sg: Scatter-gather table.
2537 * @n_elem: Number of elements in s/g table.
2539 * Initialize the data-related elements of queued_cmd @qc
2540 * to point to a scatter-gather table @sg, containing @n_elem
2544 * spin_lock_irqsave(host_set lock)
2547 void ata_sg_init(struct ata_queued_cmd *qc, struct scatterlist *sg,
2548 unsigned int n_elem)
2550 qc->flags |= ATA_QCFLAG_SG;
2552 qc->n_elem = n_elem;
2556 * ata_sg_setup_one - DMA-map the memory buffer associated with a command.
2557 * @qc: Command with memory buffer to be mapped.
2559 * DMA-map the memory buffer associated with queued_cmd @qc.
2562 * spin_lock_irqsave(host_set lock)
2565 * Zero on success, negative on error.
2568 static int ata_sg_setup_one(struct ata_queued_cmd *qc)
2570 struct ata_port *ap = qc->ap;
2571 int dir = qc->dma_dir;
2572 struct scatterlist *sg = qc->sg;
2573 dma_addr_t dma_address;
2575 dma_address = dma_map_single(ap->host_set->dev, qc->buf_virt,
2577 if (dma_mapping_error(dma_address))
2580 sg_dma_address(sg) = dma_address;
2581 sg_dma_len(sg) = sg->length;
2583 DPRINTK("mapped buffer of %d bytes for %s\n", sg_dma_len(sg),
2584 qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
2590 * ata_sg_setup - DMA-map the scatter-gather table associated with a command.
2591 * @qc: Command with scatter-gather table to be mapped.
2593 * DMA-map the scatter-gather table associated with queued_cmd @qc.
2596 * spin_lock_irqsave(host_set lock)
2599 * Zero on success, negative on error.
2603 static int ata_sg_setup(struct ata_queued_cmd *qc)
2605 struct ata_port *ap = qc->ap;
2606 struct scatterlist *sg = qc->sg;
2609 VPRINTK("ENTER, ata%u\n", ap->id);
2610 assert(qc->flags & ATA_QCFLAG_SG);
2613 n_elem = dma_map_sg(ap->host_set->dev, sg, qc->n_elem, dir);
2617 DPRINTK("%d sg elements mapped\n", n_elem);
2619 qc->n_elem = n_elem;
2625 * ata_poll_qc_complete - turn irq back on and finish qc
2626 * @qc: Command to complete
2627 * @drv_stat: ATA status register content
2630 * None. (grabs host lock)
2633 void ata_poll_qc_complete(struct ata_queued_cmd *qc, unsigned int err_mask)
2635 struct ata_port *ap = qc->ap;
2636 unsigned long flags;
2638 spin_lock_irqsave(&ap->host_set->lock, flags);
2640 ata_qc_complete(qc, err_mask);
2641 spin_unlock_irqrestore(&ap->host_set->lock, flags);
2646 * @ap: the target ata_port
2649 * None. (executing in kernel thread context)
2652 * timeout value to use
2655 static unsigned long ata_pio_poll(struct ata_port *ap)
2658 unsigned int poll_state = HSM_ST_UNKNOWN;
2659 unsigned int reg_state = HSM_ST_UNKNOWN;
2660 const unsigned int tmout_state = HSM_ST_TMOUT;
2662 switch (ap->hsm_task_state) {
2665 poll_state = HSM_ST_POLL;
2669 case HSM_ST_LAST_POLL:
2670 poll_state = HSM_ST_LAST_POLL;
2671 reg_state = HSM_ST_LAST;
2678 status = ata_chk_status(ap);
2679 if (status & ATA_BUSY) {
2680 if (time_after(jiffies, ap->pio_task_timeout)) {
2681 ap->hsm_task_state = tmout_state;
2684 ap->hsm_task_state = poll_state;
2685 return ATA_SHORT_PAUSE;
2688 ap->hsm_task_state = reg_state;
2693 * ata_pio_complete - check if drive is busy or idle
2694 * @ap: the target ata_port
2697 * None. (executing in kernel thread context)
2700 * Non-zero if qc completed, zero otherwise.
2703 static int ata_pio_complete (struct ata_port *ap)
2705 struct ata_queued_cmd *qc;
2709 * This is purely heuristic. This is a fast path. Sometimes when
2710 * we enter, BSY will be cleared in a chk-status or two. If not,
2711 * the drive is probably seeking or something. Snooze for a couple
2712 * msecs, then chk-status again. If still busy, fall back to
2713 * HSM_ST_POLL state.
2715 drv_stat = ata_busy_wait(ap, ATA_BUSY | ATA_DRQ, 10);
2716 if (drv_stat & (ATA_BUSY | ATA_DRQ)) {
2718 drv_stat = ata_busy_wait(ap, ATA_BUSY | ATA_DRQ, 10);
2719 if (drv_stat & (ATA_BUSY | ATA_DRQ)) {
2720 ap->hsm_task_state = HSM_ST_LAST_POLL;
2721 ap->pio_task_timeout = jiffies + ATA_TMOUT_PIO;
2726 drv_stat = ata_wait_idle(ap);
2727 if (!ata_ok(drv_stat)) {
2728 ap->hsm_task_state = HSM_ST_ERR;
2732 qc = ata_qc_from_tag(ap, ap->active_tag);
2735 ap->hsm_task_state = HSM_ST_IDLE;
2737 ata_poll_qc_complete(qc, 0);
2739 /* another command may start at this point */
2746 * swap_buf_le16 - swap halves of 16-words in place
2747 * @buf: Buffer to swap
2748 * @buf_words: Number of 16-bit words in buffer.
2750 * Swap halves of 16-bit words if needed to convert from
2751 * little-endian byte order to native cpu byte order, or
2755 * Inherited from caller.
2757 void swap_buf_le16(u16 *buf, unsigned int buf_words)
2762 for (i = 0; i < buf_words; i++)
2763 buf[i] = le16_to_cpu(buf[i]);
2764 #endif /* __BIG_ENDIAN */
2768 * ata_mmio_data_xfer - Transfer data by MMIO
2769 * @ap: port to read/write
2771 * @buflen: buffer length
2772 * @write_data: read/write
2774 * Transfer data from/to the device data register by MMIO.
2777 * Inherited from caller.
2780 static void ata_mmio_data_xfer(struct ata_port *ap, unsigned char *buf,
2781 unsigned int buflen, int write_data)
2784 unsigned int words = buflen >> 1;
2785 u16 *buf16 = (u16 *) buf;
2786 void __iomem *mmio = (void __iomem *)ap->ioaddr.data_addr;
2788 /* Transfer multiple of 2 bytes */
2790 for (i = 0; i < words; i++)
2791 writew(le16_to_cpu(buf16[i]), mmio);
2793 for (i = 0; i < words; i++)
2794 buf16[i] = cpu_to_le16(readw(mmio));
2797 /* Transfer trailing 1 byte, if any. */
2798 if (unlikely(buflen & 0x01)) {
2799 u16 align_buf[1] = { 0 };
2800 unsigned char *trailing_buf = buf + buflen - 1;
2803 memcpy(align_buf, trailing_buf, 1);
2804 writew(le16_to_cpu(align_buf[0]), mmio);
2806 align_buf[0] = cpu_to_le16(readw(mmio));
2807 memcpy(trailing_buf, align_buf, 1);
2813 * ata_pio_data_xfer - Transfer data by PIO
2814 * @ap: port to read/write
2816 * @buflen: buffer length
2817 * @write_data: read/write
2819 * Transfer data from/to the device data register by PIO.
2822 * Inherited from caller.
2825 static void ata_pio_data_xfer(struct ata_port *ap, unsigned char *buf,
2826 unsigned int buflen, int write_data)
2828 unsigned int words = buflen >> 1;
2830 /* Transfer multiple of 2 bytes */
2832 outsw(ap->ioaddr.data_addr, buf, words);
2834 insw(ap->ioaddr.data_addr, buf, words);
2836 /* Transfer trailing 1 byte, if any. */
2837 if (unlikely(buflen & 0x01)) {
2838 u16 align_buf[1] = { 0 };
2839 unsigned char *trailing_buf = buf + buflen - 1;
2842 memcpy(align_buf, trailing_buf, 1);
2843 outw(le16_to_cpu(align_buf[0]), ap->ioaddr.data_addr);
2845 align_buf[0] = cpu_to_le16(inw(ap->ioaddr.data_addr));
2846 memcpy(trailing_buf, align_buf, 1);
2852 * ata_data_xfer - Transfer data from/to the data register.
2853 * @ap: port to read/write
2855 * @buflen: buffer length
2856 * @do_write: read/write
2858 * Transfer data from/to the device data register.
2861 * Inherited from caller.
2864 static void ata_data_xfer(struct ata_port *ap, unsigned char *buf,
2865 unsigned int buflen, int do_write)
2867 if (ap->flags & ATA_FLAG_MMIO)
2868 ata_mmio_data_xfer(ap, buf, buflen, do_write);
2870 ata_pio_data_xfer(ap, buf, buflen, do_write);
2874 * ata_pio_sector - Transfer ATA_SECT_SIZE (512 bytes) of data.
2875 * @qc: Command on going
2877 * Transfer ATA_SECT_SIZE of data from/to the ATA device.
2880 * Inherited from caller.
2883 static void ata_pio_sector(struct ata_queued_cmd *qc)
2885 int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
2886 struct scatterlist *sg = qc->sg;
2887 struct ata_port *ap = qc->ap;
2889 unsigned int offset;
2892 if (qc->cursect == (qc->nsect - 1))
2893 ap->hsm_task_state = HSM_ST_LAST;
2895 page = sg[qc->cursg].page;
2896 offset = sg[qc->cursg].offset + qc->cursg_ofs * ATA_SECT_SIZE;
2898 /* get the current page and offset */
2899 page = nth_page(page, (offset >> PAGE_SHIFT));
2900 offset %= PAGE_SIZE;
2902 DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
2904 if (PageHighMem(page)) {
2905 unsigned long flags;
2907 local_irq_save(flags);
2908 buf = kmap_atomic(page, KM_IRQ0);
2910 /* do the actual data transfer */
2911 ata_data_xfer(ap, buf + offset, ATA_SECT_SIZE, do_write);
2913 kunmap_atomic(buf, KM_IRQ0);
2914 local_irq_restore(flags);
2916 buf = page_address(page);
2917 ata_data_xfer(ap, buf + offset, ATA_SECT_SIZE, do_write);
2923 if ((qc->cursg_ofs * ATA_SECT_SIZE) == (&sg[qc->cursg])->length) {
2930 * atapi_send_cdb - Write CDB bytes to hardware
2931 * @ap: Port to which ATAPI device is attached.
2932 * @qc: Taskfile currently active
2934 * When device has indicated its readiness to accept
2935 * a CDB, this function is called. Send the CDB.
2941 static void atapi_send_cdb(struct ata_port *ap, struct ata_queued_cmd *qc)
2944 DPRINTK("send cdb\n");
2945 assert(ap->cdb_len >= 12);
2947 ata_data_xfer(ap, qc->cdb, ap->cdb_len, 1);
2948 ata_altstatus(ap); /* flush */
2950 switch (qc->tf.protocol) {
2951 case ATA_PROT_ATAPI:
2952 ap->hsm_task_state = HSM_ST;
2954 case ATA_PROT_ATAPI_NODATA:
2955 ap->hsm_task_state = HSM_ST_LAST;
2957 case ATA_PROT_ATAPI_DMA:
2958 ap->hsm_task_state = HSM_ST_LAST;
2959 /* initiate bmdma */
2960 ap->ops->bmdma_start(qc);
2966 * ata_dataout_task - Write first data block to hardware
2967 * @_data: Port to which ATA/ATAPI device is attached.
2969 * When device has indicated its readiness to accept
2970 * the data, this function sends out the CDB or
2971 * the first data block by PIO.
2973 * - If polling, ata_pio_task() handles the rest.
2974 * - Otherwise, interrupt handler takes over.
2977 * Kernel thread context (may sleep)
2980 static void ata_dataout_task(void *_data)
2982 struct ata_port *ap = _data;
2983 struct ata_queued_cmd *qc;
2985 unsigned long flags;
2987 qc = ata_qc_from_tag(ap, ap->active_tag);
2989 assert(qc->flags & ATA_QCFLAG_ACTIVE);
2991 /* sleep-wait for BSY to clear */
2992 DPRINTK("busy wait\n");
2993 if (ata_busy_sleep(ap, ATA_TMOUT_DATAOUT_QUICK, ATA_TMOUT_DATAOUT))
2996 /* make sure DRQ is set */
2997 status = ata_chk_status(ap);
2998 if ((status & (ATA_BUSY | ATA_DRQ)) != ATA_DRQ)
3001 /* Send the CDB (atapi) or the first data block (ata pio out).
3002 * During the state transition, interrupt handler shouldn't
3003 * be invoked before the data transfer is complete and
3004 * hsm_task_state is changed. Hence, the following locking.
3006 spin_lock_irqsave(&ap->host_set->lock, flags);
3008 if (qc->tf.protocol == ATA_PROT_PIO) {
3009 /* PIO data out protocol.
3010 * send first data block.
3013 /* ata_pio_sector() might change the state to HSM_ST_LAST.
3014 * so, the state is changed here before ata_pio_sector().
3016 ap->hsm_task_state = HSM_ST;
3018 ata_altstatus(ap); /* flush */
3021 atapi_send_cdb(ap, qc);
3023 /* if polling, ata_pio_task() handles the rest.
3024 * otherwise, interrupt handler takes over from here.
3026 if (qc->tf.flags & ATA_TFLAG_POLLING)
3027 queue_work(ata_wq, &ap->pio_task);
3029 spin_unlock_irqrestore(&ap->host_set->lock, flags);
3038 * __atapi_pio_bytes - Transfer data from/to the ATAPI device.
3039 * @qc: Command on going
3040 * @bytes: number of bytes
3042 * Transfer Transfer data from/to the ATAPI device.
3045 * Inherited from caller.
3049 static void __atapi_pio_bytes(struct ata_queued_cmd *qc, unsigned int bytes)
3051 int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
3052 struct scatterlist *sg = qc->sg;
3053 struct ata_port *ap = qc->ap;
3056 unsigned int offset, count;
3058 if (qc->curbytes + bytes >= qc->nbytes)
3059 ap->hsm_task_state = HSM_ST_LAST;
3062 if (unlikely(qc->cursg >= qc->n_elem)) {
3064 * The end of qc->sg is reached and the device expects
3065 * more data to transfer. In order not to overrun qc->sg
3066 * and fulfill length specified in the byte count register,
3067 * - for read case, discard trailing data from the device
3068 * - for write case, padding zero data to the device
3070 u16 pad_buf[1] = { 0 };
3071 unsigned int words = bytes >> 1;
3074 if (words) /* warning if bytes > 1 */
3075 printk(KERN_WARNING "ata%u: %u bytes trailing data\n",
3078 for (i = 0; i < words; i++)
3079 ata_data_xfer(ap, (unsigned char*)pad_buf, 2, do_write);
3081 ap->hsm_task_state = HSM_ST_LAST;
3085 sg = &qc->sg[qc->cursg];
3088 offset = sg->offset + qc->cursg_ofs;
3090 /* get the current page and offset */
3091 page = nth_page(page, (offset >> PAGE_SHIFT));
3092 offset %= PAGE_SIZE;
3094 /* don't overrun current sg */
3095 count = min(sg->length - qc->cursg_ofs, bytes);
3097 /* don't cross page boundaries */
3098 count = min(count, (unsigned int)PAGE_SIZE - offset);
3100 DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
3102 if (PageHighMem(page)) {
3103 unsigned long flags;
3105 local_irq_save(flags);
3106 buf = kmap_atomic(page, KM_IRQ0);
3108 /* do the actual data transfer */
3109 ata_data_xfer(ap, buf + offset, count, do_write);
3111 kunmap_atomic(buf, KM_IRQ0);
3112 local_irq_restore(flags);
3114 buf = page_address(page);
3115 ata_data_xfer(ap, buf + offset, count, do_write);
3119 qc->curbytes += count;
3120 qc->cursg_ofs += count;
3122 if (qc->cursg_ofs == sg->length) {
3132 * atapi_pio_bytes - Transfer data from/to the ATAPI device.
3133 * @qc: Command on going
3135 * Transfer Transfer data from/to the ATAPI device.
3138 * Inherited from caller.
3141 static void atapi_pio_bytes(struct ata_queued_cmd *qc)
3143 struct ata_port *ap = qc->ap;
3144 struct ata_device *dev = qc->dev;
3145 unsigned int ireason, bc_lo, bc_hi, bytes;
3146 int i_write, do_write = (qc->tf.flags & ATA_TFLAG_WRITE) ? 1 : 0;
3148 ap->ops->tf_read(ap, &qc->tf);
3149 ireason = qc->tf.nsect;
3150 bc_lo = qc->tf.lbam;
3151 bc_hi = qc->tf.lbah;
3152 bytes = (bc_hi << 8) | bc_lo;
3154 /* shall be cleared to zero, indicating xfer of data */
3155 if (ireason & (1 << 0))
3158 /* make sure transfer direction matches expected */
3159 i_write = ((ireason & (1 << 1)) == 0) ? 1 : 0;
3160 if (do_write != i_write)
3163 VPRINTK("ata%u: xfering %d bytes\n", ap->id, bytes);
3165 __atapi_pio_bytes(qc, bytes);
3170 printk(KERN_INFO "ata%u: dev %u: ATAPI check failed\n",
3171 ap->id, dev->devno);
3172 ap->hsm_task_state = HSM_ST_ERR;
3176 * ata_pio_block - start PIO on a block
3177 * @ap: the target ata_port
3180 * None. (executing in kernel thread context)
3183 static void ata_pio_block(struct ata_port *ap)
3185 struct ata_queued_cmd *qc;
3189 * This is purely heuristic. This is a fast path.
3190 * Sometimes when we enter, BSY will be cleared in
3191 * a chk-status or two. If not, the drive is probably seeking
3192 * or something. Snooze for a couple msecs, then
3193 * chk-status again. If still busy, fall back to
3194 * HSM_ST_POLL state.
3196 status = ata_busy_wait(ap, ATA_BUSY, 5);
3197 if (status & ATA_BUSY) {
3199 status = ata_busy_wait(ap, ATA_BUSY, 10);
3200 if (status & ATA_BUSY) {
3201 ap->hsm_task_state = HSM_ST_POLL;
3202 ap->pio_task_timeout = jiffies + ATA_TMOUT_PIO;
3207 qc = ata_qc_from_tag(ap, ap->active_tag);
3210 if (is_atapi_taskfile(&qc->tf)) {
3211 /* no more data to transfer or unsupported ATAPI command */
3212 if ((status & ATA_DRQ) == 0) {
3213 ap->hsm_task_state = HSM_ST_LAST;
3217 atapi_pio_bytes(qc);
3219 /* handle BSY=0, DRQ=0 as error */
3220 if ((status & ATA_DRQ) == 0) {
3221 ap->hsm_task_state = HSM_ST_ERR;
3229 static void ata_pio_error(struct ata_port *ap)
3231 struct ata_queued_cmd *qc;
3233 printk(KERN_WARNING "ata%u: PIO error\n", ap->id);
3235 qc = ata_qc_from_tag(ap, ap->active_tag);
3238 ap->hsm_task_state = HSM_ST_IDLE;
3240 ata_poll_qc_complete(qc, AC_ERR_ATA_BUS);
3243 static void ata_pio_task(void *_data)
3245 struct ata_port *ap = _data;
3246 unsigned long timeout;
3253 switch (ap->hsm_task_state) {
3262 qc_completed = ata_pio_complete(ap);
3266 case HSM_ST_LAST_POLL:
3267 timeout = ata_pio_poll(ap);
3277 queue_delayed_work(ata_wq, &ap->pio_task, timeout);
3278 else if (!qc_completed)
3283 * ata_qc_timeout - Handle timeout of queued command
3284 * @qc: Command that timed out
3286 * Some part of the kernel (currently, only the SCSI layer)
3287 * has noticed that the active command on port @ap has not
3288 * completed after a specified length of time. Handle this
3289 * condition by disabling DMA (if necessary) and completing
3290 * transactions, with error if necessary.
3292 * This also handles the case of the "lost interrupt", where
3293 * for some reason (possibly hardware bug, possibly driver bug)
3294 * an interrupt was not delivered to the driver, even though the
3295 * transaction completed successfully.
3298 * Inherited from SCSI layer (none, can sleep)
3301 static void ata_qc_timeout(struct ata_queued_cmd *qc)
3303 struct ata_port *ap = qc->ap;
3304 struct ata_host_set *host_set = ap->host_set;
3305 struct ata_device *dev = qc->dev;
3306 u8 host_stat = 0, drv_stat;
3307 unsigned long flags;
3311 /* FIXME: doesn't this conflict with timeout handling? */
3312 if (qc->dev->class == ATA_DEV_ATAPI && qc->scsicmd) {
3313 struct scsi_cmnd *cmd = qc->scsicmd;
3315 if (!(cmd->eh_eflags & SCSI_EH_CANCEL_CMD)) {
3317 /* finish completing original command */
3318 spin_lock_irqsave(&host_set->lock, flags);
3319 __ata_qc_complete(qc);
3320 spin_unlock_irqrestore(&host_set->lock, flags);
3322 atapi_request_sense(ap, dev, cmd);
3324 cmd->result = (CHECK_CONDITION << 1) | (DID_OK << 16);
3325 scsi_finish_command(cmd);
3331 spin_lock_irqsave(&host_set->lock, flags);
3333 /* hack alert! We cannot use the supplied completion
3334 * function from inside the ->eh_strategy_handler() thread.
3335 * libata is the only user of ->eh_strategy_handler() in
3336 * any kernel, so the default scsi_done() assumes it is
3337 * not being called from the SCSI EH.
3339 qc->scsidone = scsi_finish_command;
3341 switch (qc->tf.protocol) {
3344 case ATA_PROT_ATAPI_DMA:
3345 host_stat = ap->ops->bmdma_status(ap);
3347 /* before we do anything else, clear DMA-Start bit */
3348 ap->ops->bmdma_stop(qc);
3354 drv_stat = ata_chk_status(ap);
3356 /* ack bmdma irq events */
3357 ap->ops->irq_clear(ap);
3359 printk(KERN_ERR "ata%u: command 0x%x timeout, stat 0x%x host_stat 0x%x\n",
3360 ap->id, qc->tf.command, drv_stat, host_stat);
3362 ap->hsm_task_state = HSM_ST_IDLE;
3364 /* complete taskfile transaction */
3365 ata_qc_complete(qc, ac_err_mask(drv_stat));
3369 spin_unlock_irqrestore(&host_set->lock, flags);
3376 * ata_eng_timeout - Handle timeout of queued command
3377 * @ap: Port on which timed-out command is active
3379 * Some part of the kernel (currently, only the SCSI layer)
3380 * has noticed that the active command on port @ap has not
3381 * completed after a specified length of time. Handle this
3382 * condition by disabling DMA (if necessary) and completing
3383 * transactions, with error if necessary.
3385 * This also handles the case of the "lost interrupt", where
3386 * for some reason (possibly hardware bug, possibly driver bug)
3387 * an interrupt was not delivered to the driver, even though the
3388 * transaction completed successfully.
3391 * Inherited from SCSI layer (none, can sleep)
3394 void ata_eng_timeout(struct ata_port *ap)
3396 struct ata_queued_cmd *qc;
3400 qc = ata_qc_from_tag(ap, ap->active_tag);
3404 printk(KERN_ERR "ata%u: BUG: timeout without command\n",
3414 * ata_qc_new - Request an available ATA command, for queueing
3415 * @ap: Port associated with device @dev
3416 * @dev: Device from whom we request an available command structure