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);
78 static unsigned int ata_unique_id = 1;
79 static struct workqueue_struct *ata_wq;
81 int atapi_enabled = 0;
82 module_param(atapi_enabled, int, 0444);
83 MODULE_PARM_DESC(atapi_enabled, "Enable discovery of ATAPI devices (0=off, 1=on)");
85 MODULE_AUTHOR("Jeff Garzik");
86 MODULE_DESCRIPTION("Library module for ATA devices");
87 MODULE_LICENSE("GPL");
88 MODULE_VERSION(DRV_VERSION);
91 * ata_tf_load_pio - send taskfile registers to host controller
92 * @ap: Port to which output is sent
93 * @tf: ATA taskfile register set
95 * Outputs ATA taskfile to standard ATA host controller.
98 * Inherited from caller.
101 static void ata_tf_load_pio(struct ata_port *ap, const struct ata_taskfile *tf)
103 struct ata_ioports *ioaddr = &ap->ioaddr;
104 unsigned int is_addr = tf->flags & ATA_TFLAG_ISADDR;
106 if (tf->ctl != ap->last_ctl) {
107 outb(tf->ctl, ioaddr->ctl_addr);
108 ap->last_ctl = tf->ctl;
112 if (is_addr && (tf->flags & ATA_TFLAG_LBA48)) {
113 outb(tf->hob_feature, ioaddr->feature_addr);
114 outb(tf->hob_nsect, ioaddr->nsect_addr);
115 outb(tf->hob_lbal, ioaddr->lbal_addr);
116 outb(tf->hob_lbam, ioaddr->lbam_addr);
117 outb(tf->hob_lbah, ioaddr->lbah_addr);
118 VPRINTK("hob: feat 0x%X nsect 0x%X, lba 0x%X 0x%X 0x%X\n",
127 outb(tf->feature, ioaddr->feature_addr);
128 outb(tf->nsect, ioaddr->nsect_addr);
129 outb(tf->lbal, ioaddr->lbal_addr);
130 outb(tf->lbam, ioaddr->lbam_addr);
131 outb(tf->lbah, ioaddr->lbah_addr);
132 VPRINTK("feat 0x%X nsect 0x%X lba 0x%X 0x%X 0x%X\n",
140 if (tf->flags & ATA_TFLAG_DEVICE) {
141 outb(tf->device, ioaddr->device_addr);
142 VPRINTK("device 0x%X\n", tf->device);
149 * ata_tf_load_mmio - send taskfile registers to host controller
150 * @ap: Port to which output is sent
151 * @tf: ATA taskfile register set
153 * Outputs ATA taskfile to standard ATA host controller using MMIO.
156 * Inherited from caller.
159 static void ata_tf_load_mmio(struct ata_port *ap, const struct ata_taskfile *tf)
161 struct ata_ioports *ioaddr = &ap->ioaddr;
162 unsigned int is_addr = tf->flags & ATA_TFLAG_ISADDR;
164 if (tf->ctl != ap->last_ctl) {
165 writeb(tf->ctl, (void __iomem *) ap->ioaddr.ctl_addr);
166 ap->last_ctl = tf->ctl;
170 if (is_addr && (tf->flags & ATA_TFLAG_LBA48)) {
171 writeb(tf->hob_feature, (void __iomem *) ioaddr->feature_addr);
172 writeb(tf->hob_nsect, (void __iomem *) ioaddr->nsect_addr);
173 writeb(tf->hob_lbal, (void __iomem *) ioaddr->lbal_addr);
174 writeb(tf->hob_lbam, (void __iomem *) ioaddr->lbam_addr);
175 writeb(tf->hob_lbah, (void __iomem *) ioaddr->lbah_addr);
176 VPRINTK("hob: feat 0x%X nsect 0x%X, lba 0x%X 0x%X 0x%X\n",
185 writeb(tf->feature, (void __iomem *) ioaddr->feature_addr);
186 writeb(tf->nsect, (void __iomem *) ioaddr->nsect_addr);
187 writeb(tf->lbal, (void __iomem *) ioaddr->lbal_addr);
188 writeb(tf->lbam, (void __iomem *) ioaddr->lbam_addr);
189 writeb(tf->lbah, (void __iomem *) ioaddr->lbah_addr);
190 VPRINTK("feat 0x%X nsect 0x%X lba 0x%X 0x%X 0x%X\n",
198 if (tf->flags & ATA_TFLAG_DEVICE) {
199 writeb(tf->device, (void __iomem *) ioaddr->device_addr);
200 VPRINTK("device 0x%X\n", tf->device);
208 * ata_tf_load - send taskfile registers to host controller
209 * @ap: Port to which output is sent
210 * @tf: ATA taskfile register set
212 * Outputs ATA taskfile to standard ATA host controller using MMIO
213 * or PIO as indicated by the ATA_FLAG_MMIO flag.
214 * Writes the control, feature, nsect, lbal, lbam, and lbah registers.
215 * Optionally (ATA_TFLAG_LBA48) writes hob_feature, hob_nsect,
216 * hob_lbal, hob_lbam, and hob_lbah.
218 * This function waits for idle (!BUSY and !DRQ) after writing
219 * registers. If the control register has a new value, this
220 * function also waits for idle after writing control and before
221 * writing the remaining registers.
223 * May be used as the tf_load() entry in ata_port_operations.
226 * Inherited from caller.
228 void ata_tf_load(struct ata_port *ap, const struct ata_taskfile *tf)
230 if (ap->flags & ATA_FLAG_MMIO)
231 ata_tf_load_mmio(ap, tf);
233 ata_tf_load_pio(ap, tf);
237 * ata_exec_command_pio - issue ATA command to host controller
238 * @ap: port to which command is being issued
239 * @tf: ATA taskfile register set
241 * Issues PIO write to ATA command register, with proper
242 * synchronization with interrupt handler / other threads.
245 * spin_lock_irqsave(host_set lock)
248 static void ata_exec_command_pio(struct ata_port *ap, const struct ata_taskfile *tf)
250 DPRINTK("ata%u: cmd 0x%X\n", ap->id, tf->command);
252 outb(tf->command, ap->ioaddr.command_addr);
258 * ata_exec_command_mmio - issue ATA command to host controller
259 * @ap: port to which command is being issued
260 * @tf: ATA taskfile register set
262 * Issues MMIO write to ATA command register, with proper
263 * synchronization with interrupt handler / other threads.
266 * spin_lock_irqsave(host_set lock)
269 static void ata_exec_command_mmio(struct ata_port *ap, const struct ata_taskfile *tf)
271 DPRINTK("ata%u: cmd 0x%X\n", ap->id, tf->command);
273 writeb(tf->command, (void __iomem *) ap->ioaddr.command_addr);
279 * ata_exec_command - issue ATA command to host controller
280 * @ap: port to which command is being issued
281 * @tf: ATA taskfile register set
283 * Issues PIO/MMIO write to ATA command register, with proper
284 * synchronization with interrupt handler / other threads.
287 * spin_lock_irqsave(host_set lock)
289 void ata_exec_command(struct ata_port *ap, const struct ata_taskfile *tf)
291 if (ap->flags & ATA_FLAG_MMIO)
292 ata_exec_command_mmio(ap, tf);
294 ata_exec_command_pio(ap, tf);
298 * ata_tf_to_host - issue ATA taskfile to host controller
299 * @ap: port to which command is being issued
300 * @tf: ATA taskfile register set
302 * Issues ATA taskfile register set to ATA host controller,
303 * with proper synchronization with interrupt handler and
307 * spin_lock_irqsave(host_set lock)
310 static inline void ata_tf_to_host(struct ata_port *ap,
311 const struct ata_taskfile *tf)
313 ap->ops->tf_load(ap, tf);
314 ap->ops->exec_command(ap, tf);
318 * ata_tf_read_pio - input device's ATA taskfile shadow registers
319 * @ap: Port from which input is read
320 * @tf: ATA taskfile register set for storing input
322 * Reads ATA taskfile registers for currently-selected device
326 * Inherited from caller.
329 static void ata_tf_read_pio(struct ata_port *ap, struct ata_taskfile *tf)
331 struct ata_ioports *ioaddr = &ap->ioaddr;
333 tf->command = ata_check_status(ap);
334 tf->feature = inb(ioaddr->error_addr);
335 tf->nsect = inb(ioaddr->nsect_addr);
336 tf->lbal = inb(ioaddr->lbal_addr);
337 tf->lbam = inb(ioaddr->lbam_addr);
338 tf->lbah = inb(ioaddr->lbah_addr);
339 tf->device = inb(ioaddr->device_addr);
341 if (tf->flags & ATA_TFLAG_LBA48) {
342 outb(tf->ctl | ATA_HOB, ioaddr->ctl_addr);
343 tf->hob_feature = inb(ioaddr->error_addr);
344 tf->hob_nsect = inb(ioaddr->nsect_addr);
345 tf->hob_lbal = inb(ioaddr->lbal_addr);
346 tf->hob_lbam = inb(ioaddr->lbam_addr);
347 tf->hob_lbah = inb(ioaddr->lbah_addr);
352 * ata_tf_read_mmio - input device's ATA taskfile shadow registers
353 * @ap: Port from which input is read
354 * @tf: ATA taskfile register set for storing input
356 * Reads ATA taskfile registers for currently-selected device
360 * Inherited from caller.
363 static void ata_tf_read_mmio(struct ata_port *ap, struct ata_taskfile *tf)
365 struct ata_ioports *ioaddr = &ap->ioaddr;
367 tf->command = ata_check_status(ap);
368 tf->feature = readb((void __iomem *)ioaddr->error_addr);
369 tf->nsect = readb((void __iomem *)ioaddr->nsect_addr);
370 tf->lbal = readb((void __iomem *)ioaddr->lbal_addr);
371 tf->lbam = readb((void __iomem *)ioaddr->lbam_addr);
372 tf->lbah = readb((void __iomem *)ioaddr->lbah_addr);
373 tf->device = readb((void __iomem *)ioaddr->device_addr);
375 if (tf->flags & ATA_TFLAG_LBA48) {
376 writeb(tf->ctl | ATA_HOB, (void __iomem *) ap->ioaddr.ctl_addr);
377 tf->hob_feature = readb((void __iomem *)ioaddr->error_addr);
378 tf->hob_nsect = readb((void __iomem *)ioaddr->nsect_addr);
379 tf->hob_lbal = readb((void __iomem *)ioaddr->lbal_addr);
380 tf->hob_lbam = readb((void __iomem *)ioaddr->lbam_addr);
381 tf->hob_lbah = readb((void __iomem *)ioaddr->lbah_addr);
387 * ata_tf_read - input device's ATA taskfile shadow registers
388 * @ap: Port from which input is read
389 * @tf: ATA taskfile register set for storing input
391 * Reads ATA taskfile registers for currently-selected device
394 * Reads nsect, lbal, lbam, lbah, and device. If ATA_TFLAG_LBA48
395 * is set, also reads the hob registers.
397 * May be used as the tf_read() entry in ata_port_operations.
400 * Inherited from caller.
402 void ata_tf_read(struct ata_port *ap, struct ata_taskfile *tf)
404 if (ap->flags & ATA_FLAG_MMIO)
405 ata_tf_read_mmio(ap, tf);
407 ata_tf_read_pio(ap, tf);
411 * ata_check_status_pio - Read device status reg & clear interrupt
412 * @ap: port where the device is
414 * Reads ATA taskfile status register for currently-selected device
415 * and return its value. This also clears pending interrupts
419 * Inherited from caller.
421 static u8 ata_check_status_pio(struct ata_port *ap)
423 return inb(ap->ioaddr.status_addr);
427 * ata_check_status_mmio - Read device status reg & clear interrupt
428 * @ap: port where the device is
430 * Reads ATA taskfile status register for currently-selected device
431 * via MMIO and return its value. This also clears pending interrupts
435 * Inherited from caller.
437 static u8 ata_check_status_mmio(struct ata_port *ap)
439 return readb((void __iomem *) ap->ioaddr.status_addr);
444 * ata_check_status - Read device status reg & clear interrupt
445 * @ap: port where the device is
447 * Reads ATA taskfile status register for currently-selected device
448 * and return its value. This also clears pending interrupts
451 * May be used as the check_status() entry in ata_port_operations.
454 * Inherited from caller.
456 u8 ata_check_status(struct ata_port *ap)
458 if (ap->flags & ATA_FLAG_MMIO)
459 return ata_check_status_mmio(ap);
460 return ata_check_status_pio(ap);
465 * ata_altstatus - Read device alternate status reg
466 * @ap: port where the device is
468 * Reads ATA taskfile alternate status register for
469 * currently-selected device and return its value.
471 * Note: may NOT be used as the check_altstatus() entry in
472 * ata_port_operations.
475 * Inherited from caller.
477 u8 ata_altstatus(struct ata_port *ap)
479 if (ap->ops->check_altstatus)
480 return ap->ops->check_altstatus(ap);
482 if (ap->flags & ATA_FLAG_MMIO)
483 return readb((void __iomem *)ap->ioaddr.altstatus_addr);
484 return inb(ap->ioaddr.altstatus_addr);
489 * ata_tf_to_fis - Convert ATA taskfile to SATA FIS structure
490 * @tf: Taskfile to convert
491 * @fis: Buffer into which data will output
492 * @pmp: Port multiplier port
494 * Converts a standard ATA taskfile to a Serial ATA
495 * FIS structure (Register - Host to Device).
498 * Inherited from caller.
501 void ata_tf_to_fis(const struct ata_taskfile *tf, u8 *fis, u8 pmp)
503 fis[0] = 0x27; /* Register - Host to Device FIS */
504 fis[1] = (pmp & 0xf) | (1 << 7); /* Port multiplier number,
505 bit 7 indicates Command FIS */
506 fis[2] = tf->command;
507 fis[3] = tf->feature;
514 fis[8] = tf->hob_lbal;
515 fis[9] = tf->hob_lbam;
516 fis[10] = tf->hob_lbah;
517 fis[11] = tf->hob_feature;
520 fis[13] = tf->hob_nsect;
531 * ata_tf_from_fis - Convert SATA FIS to ATA taskfile
532 * @fis: Buffer from which data will be input
533 * @tf: Taskfile to output
535 * Converts a standard ATA taskfile to a Serial ATA
536 * FIS structure (Register - Host to Device).
539 * Inherited from caller.
542 void ata_tf_from_fis(const u8 *fis, struct ata_taskfile *tf)
544 tf->command = fis[2]; /* status */
545 tf->feature = fis[3]; /* error */
552 tf->hob_lbal = fis[8];
553 tf->hob_lbam = fis[9];
554 tf->hob_lbah = fis[10];
557 tf->hob_nsect = fis[13];
560 static const u8 ata_rw_cmds[] = {
564 ATA_CMD_READ_MULTI_EXT,
565 ATA_CMD_WRITE_MULTI_EXT,
569 ATA_CMD_PIO_READ_EXT,
570 ATA_CMD_PIO_WRITE_EXT,
579 * ata_rwcmd_protocol - set taskfile r/w commands and protocol
580 * @qc: command to examine and configure
582 * Examine the device configuration and tf->flags to calculate
583 * the proper read/write commands and protocol to use.
588 void ata_rwcmd_protocol(struct ata_queued_cmd *qc)
590 struct ata_taskfile *tf = &qc->tf;
591 struct ata_device *dev = qc->dev;
593 int index, lba48, write;
595 lba48 = (tf->flags & ATA_TFLAG_LBA48) ? 2 : 0;
596 write = (tf->flags & ATA_TFLAG_WRITE) ? 1 : 0;
598 if (dev->flags & ATA_DFLAG_PIO) {
599 tf->protocol = ATA_PROT_PIO;
600 index = dev->multi_count ? 0 : 4;
602 tf->protocol = ATA_PROT_DMA;
606 tf->command = ata_rw_cmds[index + lba48 + write];
609 static const char * xfer_mode_str[] = {
629 * ata_udma_string - convert UDMA bit offset to string
630 * @mask: mask of bits supported; only highest bit counts.
632 * Determine string which represents the highest speed
633 * (highest bit in @udma_mask).
639 * Constant C string representing highest speed listed in
640 * @udma_mask, or the constant C string "<n/a>".
643 static const char *ata_mode_string(unsigned int mask)
647 for (i = 7; i >= 0; i--)
650 for (i = ATA_SHIFT_MWDMA + 2; i >= ATA_SHIFT_MWDMA; i--)
653 for (i = ATA_SHIFT_PIO + 4; i >= ATA_SHIFT_PIO; i--)
660 return xfer_mode_str[i];
664 * ata_pio_devchk - PATA device presence detection
665 * @ap: ATA channel to examine
666 * @device: Device to examine (starting at zero)
668 * This technique was originally described in
669 * Hale Landis's ATADRVR (www.ata-atapi.com), and
670 * later found its way into the ATA/ATAPI spec.
672 * Write a pattern to the ATA shadow registers,
673 * and if a device is present, it will respond by
674 * correctly storing and echoing back the
675 * ATA shadow register contents.
681 static unsigned int ata_pio_devchk(struct ata_port *ap,
684 struct ata_ioports *ioaddr = &ap->ioaddr;
687 ap->ops->dev_select(ap, device);
689 outb(0x55, ioaddr->nsect_addr);
690 outb(0xaa, ioaddr->lbal_addr);
692 outb(0xaa, ioaddr->nsect_addr);
693 outb(0x55, ioaddr->lbal_addr);
695 outb(0x55, ioaddr->nsect_addr);
696 outb(0xaa, ioaddr->lbal_addr);
698 nsect = inb(ioaddr->nsect_addr);
699 lbal = inb(ioaddr->lbal_addr);
701 if ((nsect == 0x55) && (lbal == 0xaa))
702 return 1; /* we found a device */
704 return 0; /* nothing found */
708 * ata_mmio_devchk - PATA device presence detection
709 * @ap: ATA channel to examine
710 * @device: Device to examine (starting at zero)
712 * This technique was originally described in
713 * Hale Landis's ATADRVR (www.ata-atapi.com), and
714 * later found its way into the ATA/ATAPI spec.
716 * Write a pattern to the ATA shadow registers,
717 * and if a device is present, it will respond by
718 * correctly storing and echoing back the
719 * ATA shadow register contents.
725 static unsigned int ata_mmio_devchk(struct ata_port *ap,
728 struct ata_ioports *ioaddr = &ap->ioaddr;
731 ap->ops->dev_select(ap, device);
733 writeb(0x55, (void __iomem *) ioaddr->nsect_addr);
734 writeb(0xaa, (void __iomem *) ioaddr->lbal_addr);
736 writeb(0xaa, (void __iomem *) ioaddr->nsect_addr);
737 writeb(0x55, (void __iomem *) ioaddr->lbal_addr);
739 writeb(0x55, (void __iomem *) ioaddr->nsect_addr);
740 writeb(0xaa, (void __iomem *) ioaddr->lbal_addr);
742 nsect = readb((void __iomem *) ioaddr->nsect_addr);
743 lbal = readb((void __iomem *) ioaddr->lbal_addr);
745 if ((nsect == 0x55) && (lbal == 0xaa))
746 return 1; /* we found a device */
748 return 0; /* nothing found */
752 * ata_devchk - PATA device presence detection
753 * @ap: ATA channel to examine
754 * @device: Device to examine (starting at zero)
756 * Dispatch ATA device presence detection, depending
757 * on whether we are using PIO or MMIO to talk to the
758 * ATA shadow registers.
764 static unsigned int ata_devchk(struct ata_port *ap,
767 if (ap->flags & ATA_FLAG_MMIO)
768 return ata_mmio_devchk(ap, device);
769 return ata_pio_devchk(ap, device);
773 * ata_dev_classify - determine device type based on ATA-spec signature
774 * @tf: ATA taskfile register set for device to be identified
776 * Determine from taskfile register contents whether a device is
777 * ATA or ATAPI, as per "Signature and persistence" section
778 * of ATA/PI spec (volume 1, sect 5.14).
784 * Device type, %ATA_DEV_ATA, %ATA_DEV_ATAPI, or %ATA_DEV_UNKNOWN
785 * the event of failure.
788 unsigned int ata_dev_classify(const struct ata_taskfile *tf)
790 /* Apple's open source Darwin code hints that some devices only
791 * put a proper signature into the LBA mid/high registers,
792 * So, we only check those. It's sufficient for uniqueness.
795 if (((tf->lbam == 0) && (tf->lbah == 0)) ||
796 ((tf->lbam == 0x3c) && (tf->lbah == 0xc3))) {
797 DPRINTK("found ATA device by sig\n");
801 if (((tf->lbam == 0x14) && (tf->lbah == 0xeb)) ||
802 ((tf->lbam == 0x69) && (tf->lbah == 0x96))) {
803 DPRINTK("found ATAPI device by sig\n");
804 return ATA_DEV_ATAPI;
807 DPRINTK("unknown device\n");
808 return ATA_DEV_UNKNOWN;
812 * ata_dev_try_classify - Parse returned ATA device signature
813 * @ap: ATA channel to examine
814 * @device: Device to examine (starting at zero)
816 * After an event -- SRST, E.D.D., or SATA COMRESET -- occurs,
817 * an ATA/ATAPI-defined set of values is placed in the ATA
818 * shadow registers, indicating the results of device detection
821 * Select the ATA device, and read the values from the ATA shadow
822 * registers. Then parse according to the Error register value,
823 * and the spec-defined values examined by ata_dev_classify().
829 static u8 ata_dev_try_classify(struct ata_port *ap, unsigned int device)
831 struct ata_device *dev = &ap->device[device];
832 struct ata_taskfile tf;
836 ap->ops->dev_select(ap, device);
838 memset(&tf, 0, sizeof(tf));
840 ap->ops->tf_read(ap, &tf);
843 dev->class = ATA_DEV_NONE;
845 /* see if device passed diags */
848 else if ((device == 0) && (err == 0x81))
853 /* determine if device if ATA or ATAPI */
854 class = ata_dev_classify(&tf);
855 if (class == ATA_DEV_UNKNOWN)
857 if ((class == ATA_DEV_ATA) && (ata_chk_status(ap) == 0))
866 * ata_dev_id_string - Convert IDENTIFY DEVICE page into string
867 * @id: IDENTIFY DEVICE results we will examine
868 * @s: string into which data is output
869 * @ofs: offset into identify device page
870 * @len: length of string to return. must be an even number.
872 * The strings in the IDENTIFY DEVICE page are broken up into
873 * 16-bit chunks. Run through the string, and output each
874 * 8-bit chunk linearly, regardless of platform.
880 void ata_dev_id_string(const u16 *id, unsigned char *s,
881 unsigned int ofs, unsigned int len)
901 * ata_noop_dev_select - Select device 0/1 on ATA bus
902 * @ap: ATA channel to manipulate
903 * @device: ATA device (numbered from zero) to select
905 * This function performs no actual function.
907 * May be used as the dev_select() entry in ata_port_operations.
912 void ata_noop_dev_select (struct ata_port *ap, unsigned int device)
918 * ata_std_dev_select - Select device 0/1 on ATA bus
919 * @ap: ATA channel to manipulate
920 * @device: ATA device (numbered from zero) to select
922 * Use the method defined in the ATA specification to
923 * make either device 0, or device 1, active on the
924 * ATA channel. Works with both PIO and MMIO.
926 * May be used as the dev_select() entry in ata_port_operations.
932 void ata_std_dev_select (struct ata_port *ap, unsigned int device)
937 tmp = ATA_DEVICE_OBS;
939 tmp = ATA_DEVICE_OBS | ATA_DEV1;
941 if (ap->flags & ATA_FLAG_MMIO) {
942 writeb(tmp, (void __iomem *) ap->ioaddr.device_addr);
944 outb(tmp, ap->ioaddr.device_addr);
946 ata_pause(ap); /* needed; also flushes, for mmio */
950 * ata_dev_select - Select device 0/1 on ATA bus
951 * @ap: ATA channel to manipulate
952 * @device: ATA device (numbered from zero) to select
953 * @wait: non-zero to wait for Status register BSY bit to clear
954 * @can_sleep: non-zero if context allows sleeping
956 * Use the method defined in the ATA specification to
957 * make either device 0, or device 1, active on the
960 * This is a high-level version of ata_std_dev_select(),
961 * which additionally provides the services of inserting
962 * the proper pauses and status polling, where needed.
968 void ata_dev_select(struct ata_port *ap, unsigned int device,
969 unsigned int wait, unsigned int can_sleep)
971 VPRINTK("ENTER, ata%u: device %u, wait %u\n",
972 ap->id, device, wait);
977 ap->ops->dev_select(ap, device);
980 if (can_sleep && ap->device[device].class == ATA_DEV_ATAPI)
987 * ata_dump_id - IDENTIFY DEVICE info debugging output
988 * @dev: Device whose IDENTIFY DEVICE page we will dump
990 * Dump selected 16-bit words from a detected device's
991 * IDENTIFY PAGE page.
997 static inline void ata_dump_id(const struct ata_device *dev)
999 DPRINTK("49==0x%04x "
1009 DPRINTK("80==0x%04x "
1019 DPRINTK("88==0x%04x "
1026 * Compute the PIO modes available for this device. This is not as
1027 * trivial as it seems if we must consider early devices correctly.
1029 * FIXME: pre IDE drive timing (do we care ?).
1032 static unsigned int ata_pio_modes(const struct ata_device *adev)
1036 /* Usual case. Word 53 indicates word 88 is valid */
1037 if (adev->id[ATA_ID_FIELD_VALID] & (1 << 2)) {
1038 modes = adev->id[ATA_ID_PIO_MODES] & 0x03;
1044 /* If word 88 isn't valid then Word 51 holds the PIO timing number
1045 for the maximum. Turn it into a mask and return it */
1046 modes = (2 << (adev->id[ATA_ID_OLD_PIO_MODES] & 0xFF)) - 1 ;
1051 * ata_dev_identify - obtain IDENTIFY x DEVICE page
1052 * @ap: port on which device we wish to probe resides
1053 * @device: device bus address, starting at zero
1055 * Following bus reset, we issue the IDENTIFY [PACKET] DEVICE
1056 * command, and read back the 512-byte device information page.
1057 * The device information page is fed to us via the standard
1058 * PIO-IN protocol, but we hand-code it here. (TODO: investigate
1059 * using standard PIO-IN paths)
1061 * After reading the device information page, we use several
1062 * bits of information from it to initialize data structures
1063 * that will be used during the lifetime of the ata_device.
1064 * Other data from the info page is used to disqualify certain
1065 * older ATA devices we do not wish to support.
1068 * Inherited from caller. Some functions called by this function
1069 * obtain the host_set lock.
1072 static void ata_dev_identify(struct ata_port *ap, unsigned int device)
1074 struct ata_device *dev = &ap->device[device];
1075 unsigned int major_version;
1077 unsigned long xfer_modes;
1078 unsigned int using_edd;
1079 DECLARE_COMPLETION(wait);
1080 struct ata_queued_cmd *qc;
1081 unsigned long flags;
1084 if (!ata_dev_present(dev)) {
1085 DPRINTK("ENTER/EXIT (host %u, dev %u) -- nodev\n",
1090 if (ap->flags & (ATA_FLAG_SRST | ATA_FLAG_SATA_RESET))
1095 DPRINTK("ENTER, host %u, dev %u\n", ap->id, device);
1097 assert (dev->class == ATA_DEV_ATA || dev->class == ATA_DEV_ATAPI ||
1098 dev->class == ATA_DEV_NONE);
1100 ata_dev_select(ap, device, 1, 1); /* select device 0/1 */
1102 qc = ata_qc_new_init(ap, dev);
1105 ata_sg_init_one(qc, dev->id, sizeof(dev->id));
1106 qc->dma_dir = DMA_FROM_DEVICE;
1107 qc->tf.protocol = ATA_PROT_PIO;
1111 if (dev->class == ATA_DEV_ATA) {
1112 qc->tf.command = ATA_CMD_ID_ATA;
1113 DPRINTK("do ATA identify\n");
1115 qc->tf.command = ATA_CMD_ID_ATAPI;
1116 DPRINTK("do ATAPI identify\n");
1119 qc->waiting = &wait;
1120 qc->complete_fn = ata_qc_complete_noop;
1122 spin_lock_irqsave(&ap->host_set->lock, flags);
1123 rc = ata_qc_issue(qc);
1124 spin_unlock_irqrestore(&ap->host_set->lock, flags);
1129 wait_for_completion(&wait);
1131 spin_lock_irqsave(&ap->host_set->lock, flags);
1132 ap->ops->tf_read(ap, &qc->tf);
1133 spin_unlock_irqrestore(&ap->host_set->lock, flags);
1135 if (qc->tf.command & ATA_ERR) {
1137 * arg! EDD works for all test cases, but seems to return
1138 * the ATA signature for some ATAPI devices. Until the
1139 * reason for this is found and fixed, we fix up the mess
1140 * here. If IDENTIFY DEVICE returns command aborted
1141 * (as ATAPI devices do), then we issue an
1142 * IDENTIFY PACKET DEVICE.
1144 * ATA software reset (SRST, the default) does not appear
1145 * to have this problem.
1147 if ((using_edd) && (qc->tf.command == ATA_CMD_ID_ATA)) {
1148 u8 err = qc->tf.feature;
1149 if (err & ATA_ABORTED) {
1150 dev->class = ATA_DEV_ATAPI;
1161 swap_buf_le16(dev->id, ATA_ID_WORDS);
1163 /* print device capabilities */
1164 printk(KERN_DEBUG "ata%u: dev %u cfg "
1165 "49:%04x 82:%04x 83:%04x 84:%04x 85:%04x 86:%04x 87:%04x 88:%04x\n",
1166 ap->id, device, dev->id[49],
1167 dev->id[82], dev->id[83], dev->id[84],
1168 dev->id[85], dev->id[86], dev->id[87],
1172 * common ATA, ATAPI feature tests
1175 /* we require DMA support (bits 8 of word 49) */
1176 if (!ata_id_has_dma(dev->id)) {
1177 printk(KERN_DEBUG "ata%u: no dma\n", ap->id);
1181 /* quick-n-dirty find max transfer mode; for printk only */
1182 xfer_modes = dev->id[ATA_ID_UDMA_MODES];
1184 xfer_modes = (dev->id[ATA_ID_MWDMA_MODES]) << ATA_SHIFT_MWDMA;
1186 xfer_modes = ata_pio_modes(dev);
1190 /* ATA-specific feature tests */
1191 if (dev->class == ATA_DEV_ATA) {
1192 if (!ata_id_is_ata(dev->id)) /* sanity check */
1195 /* get major version */
1196 tmp = dev->id[ATA_ID_MAJOR_VER];
1197 for (major_version = 14; major_version >= 1; major_version--)
1198 if (tmp & (1 << major_version))
1202 * The exact sequence expected by certain pre-ATA4 drives is:
1205 * INITIALIZE DEVICE PARAMETERS
1207 * Some drives were very specific about that exact sequence.
1209 if (major_version < 4 || (!ata_id_has_lba(dev->id))) {
1210 ata_dev_init_params(ap, dev);
1212 /* current CHS translation info (id[53-58]) might be
1213 * changed. reread the identify device info.
1215 ata_dev_reread_id(ap, dev);
1218 if (ata_id_has_lba(dev->id)) {
1219 dev->flags |= ATA_DFLAG_LBA;
1221 if (ata_id_has_lba48(dev->id)) {
1222 dev->flags |= ATA_DFLAG_LBA48;
1223 dev->n_sectors = ata_id_u64(dev->id, 100);
1225 dev->n_sectors = ata_id_u32(dev->id, 60);
1228 /* print device info to dmesg */
1229 printk(KERN_INFO "ata%u: dev %u ATA-%d, max %s, %Lu sectors:%s\n",
1232 ata_mode_string(xfer_modes),
1233 (unsigned long long)dev->n_sectors,
1234 dev->flags & ATA_DFLAG_LBA48 ? " LBA48" : " LBA");
1238 /* Default translation */
1239 dev->cylinders = dev->id[1];
1240 dev->heads = dev->id[3];
1241 dev->sectors = dev->id[6];
1242 dev->n_sectors = dev->cylinders * dev->heads * dev->sectors;
1244 if (ata_id_current_chs_valid(dev->id)) {
1245 /* Current CHS translation is valid. */
1246 dev->cylinders = dev->id[54];
1247 dev->heads = dev->id[55];
1248 dev->sectors = dev->id[56];
1250 dev->n_sectors = ata_id_u32(dev->id, 57);
1253 /* print device info to dmesg */
1254 printk(KERN_INFO "ata%u: dev %u ATA-%d, max %s, %Lu sectors: CHS %d/%d/%d\n",
1257 ata_mode_string(xfer_modes),
1258 (unsigned long long)dev->n_sectors,
1259 (int)dev->cylinders, (int)dev->heads, (int)dev->sectors);
1263 ap->host->max_cmd_len = 16;
1266 /* ATAPI-specific feature tests */
1268 if (ata_id_is_ata(dev->id)) /* sanity check */
1271 rc = atapi_cdb_len(dev->id);
1272 if ((rc < 12) || (rc > ATAPI_CDB_LEN)) {
1273 printk(KERN_WARNING "ata%u: unsupported CDB len\n", ap->id);
1276 ap->cdb_len = (unsigned int) rc;
1277 ap->host->max_cmd_len = (unsigned char) ap->cdb_len;
1279 /* print device info to dmesg */
1280 printk(KERN_INFO "ata%u: dev %u ATAPI, max %s\n",
1282 ata_mode_string(xfer_modes));
1285 DPRINTK("EXIT, drv_stat = 0x%x\n", ata_chk_status(ap));
1289 printk(KERN_WARNING "ata%u: dev %u not supported, ignoring\n",
1292 dev->class++; /* converts ATA_DEV_xxx into ATA_DEV_xxx_UNSUP */
1293 DPRINTK("EXIT, err\n");
1297 static inline u8 ata_dev_knobble(const struct ata_port *ap)
1299 return ((ap->cbl == ATA_CBL_SATA) && (!ata_id_is_sata(ap->device->id)));
1303 * ata_dev_config - Run device specific handlers and check for
1304 * SATA->PATA bridges
1311 void ata_dev_config(struct ata_port *ap, unsigned int i)
1313 /* limit bridge transfers to udma5, 200 sectors */
1314 if (ata_dev_knobble(ap)) {
1315 printk(KERN_INFO "ata%u(%u): applying bridge limits\n",
1316 ap->id, ap->device->devno);
1317 ap->udma_mask &= ATA_UDMA5;
1318 ap->host->max_sectors = ATA_MAX_SECTORS;
1319 ap->host->hostt->max_sectors = ATA_MAX_SECTORS;
1320 ap->device->flags |= ATA_DFLAG_LOCK_SECTORS;
1323 if (ap->ops->dev_config)
1324 ap->ops->dev_config(ap, &ap->device[i]);
1328 * ata_bus_probe - Reset and probe ATA bus
1331 * Master ATA bus probing function. Initiates a hardware-dependent
1332 * bus reset, then attempts to identify any devices found on
1336 * PCI/etc. bus probe sem.
1339 * Zero on success, non-zero on error.
1342 static int ata_bus_probe(struct ata_port *ap)
1344 unsigned int i, found = 0;
1346 ap->ops->phy_reset(ap);
1347 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1350 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1351 ata_dev_identify(ap, i);
1352 if (ata_dev_present(&ap->device[i])) {
1354 ata_dev_config(ap,i);
1358 if ((!found) || (ap->flags & ATA_FLAG_PORT_DISABLED))
1359 goto err_out_disable;
1362 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1363 goto err_out_disable;
1368 ap->ops->port_disable(ap);
1374 * ata_port_probe - Mark port as enabled
1375 * @ap: Port for which we indicate enablement
1377 * Modify @ap data structure such that the system
1378 * thinks that the entire port is enabled.
1380 * LOCKING: host_set lock, or some other form of
1384 void ata_port_probe(struct ata_port *ap)
1386 ap->flags &= ~ATA_FLAG_PORT_DISABLED;
1390 * __sata_phy_reset - Wake/reset a low-level SATA PHY
1391 * @ap: SATA port associated with target SATA PHY.
1393 * This function issues commands to standard SATA Sxxx
1394 * PHY registers, to wake up the phy (and device), and
1395 * clear any reset condition.
1398 * PCI/etc. bus probe sem.
1401 void __sata_phy_reset(struct ata_port *ap)
1404 unsigned long timeout = jiffies + (HZ * 5);
1406 if (ap->flags & ATA_FLAG_SATA_RESET) {
1407 /* issue phy wake/reset */
1408 scr_write_flush(ap, SCR_CONTROL, 0x301);
1409 /* Couldn't find anything in SATA I/II specs, but
1410 * AHCI-1.1 10.4.2 says at least 1 ms. */
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;
1484 * This mode timing computation functionality is ported over from
1485 * drivers/ide/ide-timing.h and was originally written by Vojtech Pavlik
1488 * PIO 0-5, MWDMA 0-2 and UDMA 0-6 timings (in nanoseconds).
1489 * These were taken from ATA/ATAPI-6 standard, rev 0a, except
1490 * for PIO 5, which is a nonstandard extension and UDMA6, which
1491 * is currently supported only by Maxtor drives.
1494 static const struct ata_timing ata_timing[] = {
1496 { XFER_UDMA_6, 0, 0, 0, 0, 0, 0, 0, 15 },
1497 { XFER_UDMA_5, 0, 0, 0, 0, 0, 0, 0, 20 },
1498 { XFER_UDMA_4, 0, 0, 0, 0, 0, 0, 0, 30 },
1499 { XFER_UDMA_3, 0, 0, 0, 0, 0, 0, 0, 45 },
1501 { XFER_UDMA_2, 0, 0, 0, 0, 0, 0, 0, 60 },
1502 { XFER_UDMA_1, 0, 0, 0, 0, 0, 0, 0, 80 },
1503 { XFER_UDMA_0, 0, 0, 0, 0, 0, 0, 0, 120 },
1505 /* { XFER_UDMA_SLOW, 0, 0, 0, 0, 0, 0, 0, 150 }, */
1507 { XFER_MW_DMA_2, 25, 0, 0, 0, 70, 25, 120, 0 },
1508 { XFER_MW_DMA_1, 45, 0, 0, 0, 80, 50, 150, 0 },
1509 { XFER_MW_DMA_0, 60, 0, 0, 0, 215, 215, 480, 0 },
1511 { XFER_SW_DMA_2, 60, 0, 0, 0, 120, 120, 240, 0 },
1512 { XFER_SW_DMA_1, 90, 0, 0, 0, 240, 240, 480, 0 },
1513 { XFER_SW_DMA_0, 120, 0, 0, 0, 480, 480, 960, 0 },
1515 /* { XFER_PIO_5, 20, 50, 30, 100, 50, 30, 100, 0 }, */
1516 { XFER_PIO_4, 25, 70, 25, 120, 70, 25, 120, 0 },
1517 { XFER_PIO_3, 30, 80, 70, 180, 80, 70, 180, 0 },
1519 { XFER_PIO_2, 30, 290, 40, 330, 100, 90, 240, 0 },
1520 { XFER_PIO_1, 50, 290, 93, 383, 125, 100, 383, 0 },
1521 { XFER_PIO_0, 70, 290, 240, 600, 165, 150, 600, 0 },
1523 /* { XFER_PIO_SLOW, 120, 290, 240, 960, 290, 240, 960, 0 }, */
1528 #define ENOUGH(v,unit) (((v)-1)/(unit)+1)
1529 #define EZ(v,unit) ((v)?ENOUGH(v,unit):0)
1531 static void ata_timing_quantize(const struct ata_timing *t, struct ata_timing *q, int T, int UT)
1533 q->setup = EZ(t->setup * 1000, T);
1534 q->act8b = EZ(t->act8b * 1000, T);
1535 q->rec8b = EZ(t->rec8b * 1000, T);
1536 q->cyc8b = EZ(t->cyc8b * 1000, T);
1537 q->active = EZ(t->active * 1000, T);
1538 q->recover = EZ(t->recover * 1000, T);
1539 q->cycle = EZ(t->cycle * 1000, T);
1540 q->udma = EZ(t->udma * 1000, UT);
1543 void ata_timing_merge(const struct ata_timing *a, const struct ata_timing *b,
1544 struct ata_timing *m, unsigned int what)
1546 if (what & ATA_TIMING_SETUP ) m->setup = max(a->setup, b->setup);
1547 if (what & ATA_TIMING_ACT8B ) m->act8b = max(a->act8b, b->act8b);
1548 if (what & ATA_TIMING_REC8B ) m->rec8b = max(a->rec8b, b->rec8b);
1549 if (what & ATA_TIMING_CYC8B ) m->cyc8b = max(a->cyc8b, b->cyc8b);
1550 if (what & ATA_TIMING_ACTIVE ) m->active = max(a->active, b->active);
1551 if (what & ATA_TIMING_RECOVER) m->recover = max(a->recover, b->recover);
1552 if (what & ATA_TIMING_CYCLE ) m->cycle = max(a->cycle, b->cycle);
1553 if (what & ATA_TIMING_UDMA ) m->udma = max(a->udma, b->udma);
1556 static const struct ata_timing* ata_timing_find_mode(unsigned short speed)
1558 const struct ata_timing *t;
1560 for (t = ata_timing; t->mode != speed; t++)
1561 if (t->mode == 0xFF)
1566 int ata_timing_compute(struct ata_device *adev, unsigned short speed,
1567 struct ata_timing *t, int T, int UT)
1569 const struct ata_timing *s;
1570 struct ata_timing p;
1576 if (!(s = ata_timing_find_mode(speed)))
1580 * If the drive is an EIDE drive, it can tell us it needs extended
1581 * PIO/MW_DMA cycle timing.
1584 if (adev->id[ATA_ID_FIELD_VALID] & 2) { /* EIDE drive */
1585 memset(&p, 0, sizeof(p));
1586 if(speed >= XFER_PIO_0 && speed <= XFER_SW_DMA_0) {
1587 if (speed <= XFER_PIO_2) p.cycle = p.cyc8b = adev->id[ATA_ID_EIDE_PIO];
1588 else p.cycle = p.cyc8b = adev->id[ATA_ID_EIDE_PIO_IORDY];
1589 } else if(speed >= XFER_MW_DMA_0 && speed <= XFER_MW_DMA_2) {
1590 p.cycle = adev->id[ATA_ID_EIDE_DMA_MIN];
1592 ata_timing_merge(&p, t, t, ATA_TIMING_CYCLE | ATA_TIMING_CYC8B);
1596 * Convert the timing to bus clock counts.
1599 ata_timing_quantize(s, t, T, UT);
1602 * Even in DMA/UDMA modes we still use PIO access for IDENTIFY, S.M.A.R.T
1603 * and some other commands. We have to ensure that the DMA cycle timing is
1604 * slower/equal than the fastest PIO timing.
1607 if (speed > XFER_PIO_4) {
1608 ata_timing_compute(adev, adev->pio_mode, &p, T, UT);
1609 ata_timing_merge(&p, t, t, ATA_TIMING_ALL);
1613 * Lenghten active & recovery time so that cycle time is correct.
1616 if (t->act8b + t->rec8b < t->cyc8b) {
1617 t->act8b += (t->cyc8b - (t->act8b + t->rec8b)) / 2;
1618 t->rec8b = t->cyc8b - t->act8b;
1621 if (t->active + t->recover < t->cycle) {
1622 t->active += (t->cycle - (t->active + t->recover)) / 2;
1623 t->recover = t->cycle - t->active;
1629 static const struct {
1632 } xfer_mode_classes[] = {
1633 { ATA_SHIFT_UDMA, XFER_UDMA_0 },
1634 { ATA_SHIFT_MWDMA, XFER_MW_DMA_0 },
1635 { ATA_SHIFT_PIO, XFER_PIO_0 },
1638 static inline u8 base_from_shift(unsigned int shift)
1642 for (i = 0; i < ARRAY_SIZE(xfer_mode_classes); i++)
1643 if (xfer_mode_classes[i].shift == shift)
1644 return xfer_mode_classes[i].base;
1649 static void ata_dev_set_mode(struct ata_port *ap, struct ata_device *dev)
1654 if (!ata_dev_present(dev) || (ap->flags & ATA_FLAG_PORT_DISABLED))
1657 if (dev->xfer_shift == ATA_SHIFT_PIO)
1658 dev->flags |= ATA_DFLAG_PIO;
1660 ata_dev_set_xfermode(ap, dev);
1662 base = base_from_shift(dev->xfer_shift);
1663 ofs = dev->xfer_mode - base;
1664 idx = ofs + dev->xfer_shift;
1665 WARN_ON(idx >= ARRAY_SIZE(xfer_mode_str));
1667 DPRINTK("idx=%d xfer_shift=%u, xfer_mode=0x%x, base=0x%x, offset=%d\n",
1668 idx, dev->xfer_shift, (int)dev->xfer_mode, (int)base, ofs);
1670 printk(KERN_INFO "ata%u: dev %u configured for %s\n",
1671 ap->id, dev->devno, xfer_mode_str[idx]);
1674 static int ata_host_set_pio(struct ata_port *ap)
1680 mask = ata_get_mode_mask(ap, ATA_SHIFT_PIO);
1683 printk(KERN_WARNING "ata%u: no PIO support\n", ap->id);
1687 base = base_from_shift(ATA_SHIFT_PIO);
1688 xfer_mode = base + x;
1690 DPRINTK("base 0x%x xfer_mode 0x%x mask 0x%x x %d\n",
1691 (int)base, (int)xfer_mode, mask, x);
1693 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1694 struct ata_device *dev = &ap->device[i];
1695 if (ata_dev_present(dev)) {
1696 dev->pio_mode = xfer_mode;
1697 dev->xfer_mode = xfer_mode;
1698 dev->xfer_shift = ATA_SHIFT_PIO;
1699 if (ap->ops->set_piomode)
1700 ap->ops->set_piomode(ap, dev);
1707 static void ata_host_set_dma(struct ata_port *ap, u8 xfer_mode,
1708 unsigned int xfer_shift)
1712 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1713 struct ata_device *dev = &ap->device[i];
1714 if (ata_dev_present(dev)) {
1715 dev->dma_mode = xfer_mode;
1716 dev->xfer_mode = xfer_mode;
1717 dev->xfer_shift = xfer_shift;
1718 if (ap->ops->set_dmamode)
1719 ap->ops->set_dmamode(ap, dev);
1725 * ata_set_mode - Program timings and issue SET FEATURES - XFER
1726 * @ap: port on which timings will be programmed
1728 * Set ATA device disk transfer mode (PIO3, UDMA6, etc.).
1731 * PCI/etc. bus probe sem.
1734 static void ata_set_mode(struct ata_port *ap)
1736 unsigned int xfer_shift;
1740 /* step 1: always set host PIO timings */
1741 rc = ata_host_set_pio(ap);
1745 /* step 2: choose the best data xfer mode */
1746 xfer_mode = xfer_shift = 0;
1747 rc = ata_choose_xfer_mode(ap, &xfer_mode, &xfer_shift);
1751 /* step 3: if that xfer mode isn't PIO, set host DMA timings */
1752 if (xfer_shift != ATA_SHIFT_PIO)
1753 ata_host_set_dma(ap, xfer_mode, xfer_shift);
1755 /* step 4: update devices' xfer mode */
1756 ata_dev_set_mode(ap, &ap->device[0]);
1757 ata_dev_set_mode(ap, &ap->device[1]);
1759 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1762 if (ap->ops->post_set_mode)
1763 ap->ops->post_set_mode(ap);
1768 ata_port_disable(ap);
1772 * ata_busy_sleep - sleep until BSY clears, or timeout
1773 * @ap: port containing status register to be polled
1774 * @tmout_pat: impatience timeout
1775 * @tmout: overall timeout
1777 * Sleep until ATA Status register bit BSY clears,
1778 * or a timeout occurs.
1784 static unsigned int ata_busy_sleep (struct ata_port *ap,
1785 unsigned long tmout_pat,
1786 unsigned long tmout)
1788 unsigned long timer_start, timeout;
1791 status = ata_busy_wait(ap, ATA_BUSY, 300);
1792 timer_start = jiffies;
1793 timeout = timer_start + tmout_pat;
1794 while ((status & ATA_BUSY) && (time_before(jiffies, timeout))) {
1796 status = ata_busy_wait(ap, ATA_BUSY, 3);
1799 if (status & ATA_BUSY)
1800 printk(KERN_WARNING "ata%u is slow to respond, "
1801 "please be patient\n", ap->id);
1803 timeout = timer_start + tmout;
1804 while ((status & ATA_BUSY) && (time_before(jiffies, timeout))) {
1806 status = ata_chk_status(ap);
1809 if (status & ATA_BUSY) {
1810 printk(KERN_ERR "ata%u failed to respond (%lu secs)\n",
1811 ap->id, tmout / HZ);
1818 static void ata_bus_post_reset(struct ata_port *ap, unsigned int devmask)
1820 struct ata_ioports *ioaddr = &ap->ioaddr;
1821 unsigned int dev0 = devmask & (1 << 0);
1822 unsigned int dev1 = devmask & (1 << 1);
1823 unsigned long timeout;
1825 /* if device 0 was found in ata_devchk, wait for its
1829 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
1831 /* if device 1 was found in ata_devchk, wait for
1832 * register access, then wait for BSY to clear
1834 timeout = jiffies + ATA_TMOUT_BOOT;
1838 ap->ops->dev_select(ap, 1);
1839 if (ap->flags & ATA_FLAG_MMIO) {
1840 nsect = readb((void __iomem *) ioaddr->nsect_addr);
1841 lbal = readb((void __iomem *) ioaddr->lbal_addr);
1843 nsect = inb(ioaddr->nsect_addr);
1844 lbal = inb(ioaddr->lbal_addr);
1846 if ((nsect == 1) && (lbal == 1))
1848 if (time_after(jiffies, timeout)) {
1852 msleep(50); /* give drive a breather */
1855 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
1857 /* is all this really necessary? */
1858 ap->ops->dev_select(ap, 0);
1860 ap->ops->dev_select(ap, 1);
1862 ap->ops->dev_select(ap, 0);
1866 * ata_bus_edd - Issue EXECUTE DEVICE DIAGNOSTIC command.
1867 * @ap: Port to reset and probe
1869 * Use the EXECUTE DEVICE DIAGNOSTIC command to reset and
1870 * probe the bus. Not often used these days.
1873 * PCI/etc. bus probe sem.
1874 * Obtains host_set lock.
1878 static unsigned int ata_bus_edd(struct ata_port *ap)
1880 struct ata_taskfile tf;
1881 unsigned long flags;
1883 /* set up execute-device-diag (bus reset) taskfile */
1884 /* also, take interrupts to a known state (disabled) */
1885 DPRINTK("execute-device-diag\n");
1886 ata_tf_init(ap, &tf, 0);
1888 tf.command = ATA_CMD_EDD;
1889 tf.protocol = ATA_PROT_NODATA;
1892 spin_lock_irqsave(&ap->host_set->lock, flags);
1893 ata_tf_to_host(ap, &tf);
1894 spin_unlock_irqrestore(&ap->host_set->lock, flags);
1896 /* spec says at least 2ms. but who knows with those
1897 * crazy ATAPI devices...
1901 return ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
1904 static unsigned int ata_bus_softreset(struct ata_port *ap,
1905 unsigned int devmask)
1907 struct ata_ioports *ioaddr = &ap->ioaddr;
1909 DPRINTK("ata%u: bus reset via SRST\n", ap->id);
1911 /* software reset. causes dev0 to be selected */
1912 if (ap->flags & ATA_FLAG_MMIO) {
1913 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
1914 udelay(20); /* FIXME: flush */
1915 writeb(ap->ctl | ATA_SRST, (void __iomem *) ioaddr->ctl_addr);
1916 udelay(20); /* FIXME: flush */
1917 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
1919 outb(ap->ctl, ioaddr->ctl_addr);
1921 outb(ap->ctl | ATA_SRST, ioaddr->ctl_addr);
1923 outb(ap->ctl, ioaddr->ctl_addr);
1926 /* spec mandates ">= 2ms" before checking status.
1927 * We wait 150ms, because that was the magic delay used for
1928 * ATAPI devices in Hale Landis's ATADRVR, for the period of time
1929 * between when the ATA command register is written, and then
1930 * status is checked. Because waiting for "a while" before
1931 * checking status is fine, post SRST, we perform this magic
1932 * delay here as well.
1936 ata_bus_post_reset(ap, devmask);
1942 * ata_bus_reset - reset host port and associated ATA channel
1943 * @ap: port to reset
1945 * This is typically the first time we actually start issuing
1946 * commands to the ATA channel. We wait for BSY to clear, then
1947 * issue EXECUTE DEVICE DIAGNOSTIC command, polling for its
1948 * result. Determine what devices, if any, are on the channel
1949 * by looking at the device 0/1 error register. Look at the signature
1950 * stored in each device's taskfile registers, to determine if
1951 * the device is ATA or ATAPI.
1954 * PCI/etc. bus probe sem.
1955 * Obtains host_set lock.
1958 * Sets ATA_FLAG_PORT_DISABLED if bus reset fails.
1961 void ata_bus_reset(struct ata_port *ap)
1963 struct ata_ioports *ioaddr = &ap->ioaddr;
1964 unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS;
1966 unsigned int dev0, dev1 = 0, rc = 0, devmask = 0;
1968 DPRINTK("ENTER, host %u, port %u\n", ap->id, ap->port_no);
1970 /* determine if device 0/1 are present */
1971 if (ap->flags & ATA_FLAG_SATA_RESET)
1974 dev0 = ata_devchk(ap, 0);
1976 dev1 = ata_devchk(ap, 1);
1980 devmask |= (1 << 0);
1982 devmask |= (1 << 1);
1984 /* select device 0 again */
1985 ap->ops->dev_select(ap, 0);
1987 /* issue bus reset */
1988 if (ap->flags & ATA_FLAG_SRST)
1989 rc = ata_bus_softreset(ap, devmask);
1990 else if ((ap->flags & ATA_FLAG_SATA_RESET) == 0) {
1991 /* set up device control */
1992 if (ap->flags & ATA_FLAG_MMIO)
1993 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
1995 outb(ap->ctl, ioaddr->ctl_addr);
1996 rc = ata_bus_edd(ap);
2003 * determine by signature whether we have ATA or ATAPI devices
2005 err = ata_dev_try_classify(ap, 0);
2006 if ((slave_possible) && (err != 0x81))
2007 ata_dev_try_classify(ap, 1);
2009 /* re-enable interrupts */
2010 if (ap->ioaddr.ctl_addr) /* FIXME: hack. create a hook instead */
2013 /* is double-select really necessary? */
2014 if (ap->device[1].class != ATA_DEV_NONE)
2015 ap->ops->dev_select(ap, 1);
2016 if (ap->device[0].class != ATA_DEV_NONE)
2017 ap->ops->dev_select(ap, 0);
2019 /* if no devices were detected, disable this port */
2020 if ((ap->device[0].class == ATA_DEV_NONE) &&
2021 (ap->device[1].class == ATA_DEV_NONE))
2024 if (ap->flags & (ATA_FLAG_SATA_RESET | ATA_FLAG_SRST)) {
2025 /* set up device control for ATA_FLAG_SATA_RESET */
2026 if (ap->flags & ATA_FLAG_MMIO)
2027 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
2029 outb(ap->ctl, ioaddr->ctl_addr);
2036 printk(KERN_ERR "ata%u: disabling port\n", ap->id);
2037 ap->ops->port_disable(ap);
2042 static void ata_pr_blacklisted(const struct ata_port *ap,
2043 const struct ata_device *dev)
2045 printk(KERN_WARNING "ata%u: dev %u is on DMA blacklist, disabling DMA\n",
2046 ap->id, dev->devno);
2049 static const char * ata_dma_blacklist [] = {
2068 "Toshiba CD-ROM XM-6202B",
2069 "TOSHIBA CD-ROM XM-1702BC",
2071 "E-IDE CD-ROM CR-840",
2074 "SAMSUNG CD-ROM SC-148C",
2075 "SAMSUNG CD-ROM SC",
2077 "ATAPI CD-ROM DRIVE 40X MAXIMUM",
2081 static int ata_dma_blacklisted(const struct ata_device *dev)
2083 unsigned char model_num[40];
2088 ata_dev_id_string(dev->id, model_num, ATA_ID_PROD_OFS,
2091 len = strnlen(s, sizeof(model_num));
2093 /* ATAPI specifies that empty space is blank-filled; remove blanks */
2094 while ((len > 0) && (s[len - 1] == ' ')) {
2099 for (i = 0; i < ARRAY_SIZE(ata_dma_blacklist); i++)
2100 if (!strncmp(ata_dma_blacklist[i], s, len))
2106 static unsigned int ata_get_mode_mask(const struct ata_port *ap, int shift)
2108 const struct ata_device *master, *slave;
2111 master = &ap->device[0];
2112 slave = &ap->device[1];
2114 assert (ata_dev_present(master) || ata_dev_present(slave));
2116 if (shift == ATA_SHIFT_UDMA) {
2117 mask = ap->udma_mask;
2118 if (ata_dev_present(master)) {
2119 mask &= (master->id[ATA_ID_UDMA_MODES] & 0xff);
2120 if (ata_dma_blacklisted(master)) {
2122 ata_pr_blacklisted(ap, master);
2125 if (ata_dev_present(slave)) {
2126 mask &= (slave->id[ATA_ID_UDMA_MODES] & 0xff);
2127 if (ata_dma_blacklisted(slave)) {
2129 ata_pr_blacklisted(ap, slave);
2133 else if (shift == ATA_SHIFT_MWDMA) {
2134 mask = ap->mwdma_mask;
2135 if (ata_dev_present(master)) {
2136 mask &= (master->id[ATA_ID_MWDMA_MODES] & 0x07);
2137 if (ata_dma_blacklisted(master)) {
2139 ata_pr_blacklisted(ap, master);
2142 if (ata_dev_present(slave)) {
2143 mask &= (slave->id[ATA_ID_MWDMA_MODES] & 0x07);
2144 if (ata_dma_blacklisted(slave)) {
2146 ata_pr_blacklisted(ap, slave);
2150 else if (shift == ATA_SHIFT_PIO) {
2151 mask = ap->pio_mask;
2152 if (ata_dev_present(master)) {
2153 /* spec doesn't return explicit support for
2154 * PIO0-2, so we fake it
2156 u16 tmp_mode = master->id[ATA_ID_PIO_MODES] & 0x03;
2161 if (ata_dev_present(slave)) {
2162 /* spec doesn't return explicit support for
2163 * PIO0-2, so we fake it
2165 u16 tmp_mode = slave->id[ATA_ID_PIO_MODES] & 0x03;
2172 mask = 0xffffffff; /* shut up compiler warning */
2179 /* find greatest bit */
2180 static int fgb(u32 bitmap)
2185 for (i = 0; i < 32; i++)
2186 if (bitmap & (1 << i))
2193 * ata_choose_xfer_mode - attempt to find best transfer mode
2194 * @ap: Port for which an xfer mode will be selected
2195 * @xfer_mode_out: (output) SET FEATURES - XFER MODE code
2196 * @xfer_shift_out: (output) bit shift that selects this mode
2198 * Based on host and device capabilities, determine the
2199 * maximum transfer mode that is amenable to all.
2202 * PCI/etc. bus probe sem.
2205 * Zero on success, negative on error.
2208 static int ata_choose_xfer_mode(const struct ata_port *ap,
2210 unsigned int *xfer_shift_out)
2212 unsigned int mask, shift;
2215 for (i = 0; i < ARRAY_SIZE(xfer_mode_classes); i++) {
2216 shift = xfer_mode_classes[i].shift;
2217 mask = ata_get_mode_mask(ap, shift);
2221 *xfer_mode_out = xfer_mode_classes[i].base + x;
2222 *xfer_shift_out = shift;
2231 * ata_dev_set_xfermode - Issue SET FEATURES - XFER MODE command
2232 * @ap: Port associated with device @dev
2233 * @dev: Device to which command will be sent
2235 * Issue SET FEATURES - XFER MODE command to device @dev
2239 * PCI/etc. bus probe sem.
2242 static void ata_dev_set_xfermode(struct ata_port *ap, struct ata_device *dev)
2244 DECLARE_COMPLETION(wait);
2245 struct ata_queued_cmd *qc;
2247 unsigned long flags;
2249 /* set up set-features taskfile */
2250 DPRINTK("set features - xfer mode\n");
2252 qc = ata_qc_new_init(ap, dev);
2255 qc->tf.command = ATA_CMD_SET_FEATURES;
2256 qc->tf.feature = SETFEATURES_XFER;
2257 qc->tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
2258 qc->tf.protocol = ATA_PROT_NODATA;
2259 qc->tf.nsect = dev->xfer_mode;
2261 qc->waiting = &wait;
2262 qc->complete_fn = ata_qc_complete_noop;
2264 spin_lock_irqsave(&ap->host_set->lock, flags);
2265 rc = ata_qc_issue(qc);
2266 spin_unlock_irqrestore(&ap->host_set->lock, flags);
2269 ata_port_disable(ap);
2271 wait_for_completion(&wait);
2277 * ata_dev_reread_id - Reread the device identify device info
2278 * @ap: port where the device is
2279 * @dev: device to reread the identify device info
2284 static void ata_dev_reread_id(struct ata_port *ap, struct ata_device *dev)
2286 DECLARE_COMPLETION(wait);
2287 struct ata_queued_cmd *qc;
2288 unsigned long flags;
2291 qc = ata_qc_new_init(ap, dev);
2294 ata_sg_init_one(qc, dev->id, sizeof(dev->id));
2295 qc->dma_dir = DMA_FROM_DEVICE;
2297 if (dev->class == ATA_DEV_ATA) {
2298 qc->tf.command = ATA_CMD_ID_ATA;
2299 DPRINTK("do ATA identify\n");
2301 qc->tf.command = ATA_CMD_ID_ATAPI;
2302 DPRINTK("do ATAPI identify\n");
2305 qc->tf.flags |= ATA_TFLAG_DEVICE;
2306 qc->tf.protocol = ATA_PROT_PIO;
2309 qc->waiting = &wait;
2310 qc->complete_fn = ata_qc_complete_noop;
2312 spin_lock_irqsave(&ap->host_set->lock, flags);
2313 rc = ata_qc_issue(qc);
2314 spin_unlock_irqrestore(&ap->host_set->lock, flags);
2319 wait_for_completion(&wait);
2321 swap_buf_le16(dev->id, ATA_ID_WORDS);
2329 ata_port_disable(ap);
2333 * ata_dev_init_params - Issue INIT DEV PARAMS command
2334 * @ap: Port associated with device @dev
2335 * @dev: Device to which command will be sent
2340 static void ata_dev_init_params(struct ata_port *ap, struct ata_device *dev)
2342 DECLARE_COMPLETION(wait);
2343 struct ata_queued_cmd *qc;
2345 unsigned long flags;
2346 u16 sectors = dev->id[6];
2347 u16 heads = dev->id[3];
2349 /* Number of sectors per track 1-255. Number of heads 1-16 */
2350 if (sectors < 1 || sectors > 255 || heads < 1 || heads > 16)
2353 /* set up init dev params taskfile */
2354 DPRINTK("init dev params \n");
2356 qc = ata_qc_new_init(ap, dev);
2359 qc->tf.command = ATA_CMD_INIT_DEV_PARAMS;
2360 qc->tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
2361 qc->tf.protocol = ATA_PROT_NODATA;
2362 qc->tf.nsect = sectors;
2363 qc->tf.device |= (heads - 1) & 0x0f; /* max head = num. of heads - 1 */
2365 qc->waiting = &wait;
2366 qc->complete_fn = ata_qc_complete_noop;
2368 spin_lock_irqsave(&ap->host_set->lock, flags);
2369 rc = ata_qc_issue(qc);
2370 spin_unlock_irqrestore(&ap->host_set->lock, flags);
2373 ata_port_disable(ap);
2375 wait_for_completion(&wait);
2381 * ata_sg_clean - Unmap DMA memory associated with command
2382 * @qc: Command containing DMA memory to be released
2384 * Unmap all mapped DMA memory associated with this command.
2387 * spin_lock_irqsave(host_set lock)
2390 static void ata_sg_clean(struct ata_queued_cmd *qc)
2392 struct ata_port *ap = qc->ap;
2393 struct scatterlist *sg = qc->sg;
2394 int dir = qc->dma_dir;
2396 assert(qc->flags & ATA_QCFLAG_DMAMAP);
2399 if (qc->flags & ATA_QCFLAG_SINGLE)
2400 assert(qc->n_elem == 1);
2402 DPRINTK("unmapping %u sg elements\n", qc->n_elem);
2404 if (qc->flags & ATA_QCFLAG_SG)
2405 dma_unmap_sg(ap->host_set->dev, sg, qc->n_elem, dir);
2407 dma_unmap_single(ap->host_set->dev, sg_dma_address(&sg[0]),
2408 sg_dma_len(&sg[0]), dir);
2410 qc->flags &= ~ATA_QCFLAG_DMAMAP;
2415 * ata_fill_sg - Fill PCI IDE PRD table
2416 * @qc: Metadata associated with taskfile to be transferred
2418 * Fill PCI IDE PRD (scatter-gather) table with segments
2419 * associated with the current disk command.
2422 * spin_lock_irqsave(host_set lock)
2425 static void ata_fill_sg(struct ata_queued_cmd *qc)
2427 struct scatterlist *sg = qc->sg;
2428 struct ata_port *ap = qc->ap;
2429 unsigned int idx, nelem;
2432 assert(qc->n_elem > 0);
2435 for (nelem = qc->n_elem; nelem; nelem--,sg++) {
2439 /* determine if physical DMA addr spans 64K boundary.
2440 * Note h/w doesn't support 64-bit, so we unconditionally
2441 * truncate dma_addr_t to u32.
2443 addr = (u32) sg_dma_address(sg);
2444 sg_len = sg_dma_len(sg);
2447 offset = addr & 0xffff;
2449 if ((offset + sg_len) > 0x10000)
2450 len = 0x10000 - offset;
2452 ap->prd[idx].addr = cpu_to_le32(addr);
2453 ap->prd[idx].flags_len = cpu_to_le32(len & 0xffff);
2454 VPRINTK("PRD[%u] = (0x%X, 0x%X)\n", idx, addr, len);
2463 ap->prd[idx - 1].flags_len |= cpu_to_le32(ATA_PRD_EOT);
2466 * ata_check_atapi_dma - Check whether ATAPI DMA can be supported
2467 * @qc: Metadata associated with taskfile to check
2469 * Allow low-level driver to filter ATA PACKET commands, returning
2470 * a status indicating whether or not it is OK to use DMA for the
2471 * supplied PACKET command.
2474 * spin_lock_irqsave(host_set lock)
2476 * RETURNS: 0 when ATAPI DMA can be used
2479 int ata_check_atapi_dma(struct ata_queued_cmd *qc)
2481 struct ata_port *ap = qc->ap;
2482 int rc = 0; /* Assume ATAPI DMA is OK by default */
2484 if (ap->ops->check_atapi_dma)
2485 rc = ap->ops->check_atapi_dma(qc);
2490 * ata_qc_prep - Prepare taskfile for submission
2491 * @qc: Metadata associated with taskfile to be prepared
2493 * Prepare ATA taskfile for submission.
2496 * spin_lock_irqsave(host_set lock)
2498 void ata_qc_prep(struct ata_queued_cmd *qc)
2500 if (!(qc->flags & ATA_QCFLAG_DMAMAP))
2507 * ata_sg_init_one - Associate command with memory buffer
2508 * @qc: Command to be associated
2509 * @buf: Memory buffer
2510 * @buflen: Length of memory buffer, in bytes.
2512 * Initialize the data-related elements of queued_cmd @qc
2513 * to point to a single memory buffer, @buf of byte length @buflen.
2516 * spin_lock_irqsave(host_set lock)
2519 void ata_sg_init_one(struct ata_queued_cmd *qc, void *buf, unsigned int buflen)
2521 qc->flags |= ATA_QCFLAG_SINGLE;
2523 qc->sg = &qc->sgent;
2526 sg_init_one(qc->sg, buf, buflen);
2530 * ata_sg_init - Associate command with scatter-gather table.
2531 * @qc: Command to be associated
2532 * @sg: Scatter-gather table.
2533 * @n_elem: Number of elements in s/g table.
2535 * Initialize the data-related elements of queued_cmd @qc
2536 * to point to a scatter-gather table @sg, containing @n_elem
2540 * spin_lock_irqsave(host_set lock)
2543 void ata_sg_init(struct ata_queued_cmd *qc, struct scatterlist *sg,
2544 unsigned int n_elem)
2546 qc->flags |= ATA_QCFLAG_SG;
2548 qc->n_elem = n_elem;
2552 * ata_sg_setup_one - DMA-map the memory buffer associated with a command.
2553 * @qc: Command with memory buffer to be mapped.
2555 * DMA-map the memory buffer associated with queued_cmd @qc.
2558 * spin_lock_irqsave(host_set lock)
2561 * Zero on success, negative on error.
2564 static int ata_sg_setup_one(struct ata_queued_cmd *qc)
2566 struct ata_port *ap = qc->ap;
2567 int dir = qc->dma_dir;
2568 struct scatterlist *sg = qc->sg;
2569 dma_addr_t dma_address;
2571 dma_address = dma_map_single(ap->host_set->dev, qc->buf_virt,
2573 if (dma_mapping_error(dma_address))
2576 sg_dma_address(sg) = dma_address;
2577 sg_dma_len(sg) = sg->length;
2579 DPRINTK("mapped buffer of %d bytes for %s\n", sg_dma_len(sg),
2580 qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
2586 * ata_sg_setup - DMA-map the scatter-gather table associated with a command.
2587 * @qc: Command with scatter-gather table to be mapped.
2589 * DMA-map the scatter-gather table associated with queued_cmd @qc.
2592 * spin_lock_irqsave(host_set lock)
2595 * Zero on success, negative on error.
2599 static int ata_sg_setup(struct ata_queued_cmd *qc)
2601 struct ata_port *ap = qc->ap;
2602 struct scatterlist *sg = qc->sg;
2605 VPRINTK("ENTER, ata%u\n", ap->id);
2606 assert(qc->flags & ATA_QCFLAG_SG);
2609 n_elem = dma_map_sg(ap->host_set->dev, sg, qc->n_elem, dir);
2613 DPRINTK("%d sg elements mapped\n", n_elem);
2615 qc->n_elem = n_elem;
2621 * ata_poll_qc_complete - turn irq back on and finish qc
2622 * @qc: Command to complete
2623 * @drv_stat: ATA status register content
2626 * None. (grabs host lock)
2629 void ata_poll_qc_complete(struct ata_queued_cmd *qc, unsigned int err_mask)
2631 struct ata_port *ap = qc->ap;
2632 unsigned long flags;
2634 spin_lock_irqsave(&ap->host_set->lock, flags);
2635 ap->flags &= ~ATA_FLAG_NOINTR;
2637 ata_qc_complete(qc, err_mask);
2638 spin_unlock_irqrestore(&ap->host_set->lock, flags);
2643 * @ap: the target ata_port
2646 * None. (executing in kernel thread context)
2649 * timeout value to use
2652 static unsigned long ata_pio_poll(struct ata_port *ap)
2655 unsigned int poll_state = HSM_ST_UNKNOWN;
2656 unsigned int reg_state = HSM_ST_UNKNOWN;
2657 const unsigned int tmout_state = HSM_ST_TMOUT;
2659 switch (ap->hsm_task_state) {
2662 poll_state = HSM_ST_POLL;
2666 case HSM_ST_LAST_POLL:
2667 poll_state = HSM_ST_LAST_POLL;
2668 reg_state = HSM_ST_LAST;
2675 status = ata_chk_status(ap);
2676 if (status & ATA_BUSY) {
2677 if (time_after(jiffies, ap->pio_task_timeout)) {
2678 ap->hsm_task_state = tmout_state;
2681 ap->hsm_task_state = poll_state;
2682 return ATA_SHORT_PAUSE;
2685 ap->hsm_task_state = reg_state;
2690 * ata_pio_complete - check if drive is busy or idle
2691 * @ap: the target ata_port
2694 * None. (executing in kernel thread context)
2697 * Non-zero if qc completed, zero otherwise.
2700 static int ata_pio_complete (struct ata_port *ap)
2702 struct ata_queued_cmd *qc;
2706 * This is purely heuristic. This is a fast path. Sometimes when
2707 * we enter, BSY will be cleared in a chk-status or two. If not,
2708 * the drive is probably seeking or something. Snooze for a couple
2709 * msecs, then chk-status again. If still busy, fall back to
2710 * HSM_ST_POLL state.
2712 drv_stat = ata_busy_wait(ap, ATA_BUSY | ATA_DRQ, 10);
2713 if (drv_stat & (ATA_BUSY | ATA_DRQ)) {
2715 drv_stat = ata_busy_wait(ap, ATA_BUSY | ATA_DRQ, 10);
2716 if (drv_stat & (ATA_BUSY | ATA_DRQ)) {
2717 ap->hsm_task_state = HSM_ST_LAST_POLL;
2718 ap->pio_task_timeout = jiffies + ATA_TMOUT_PIO;
2723 drv_stat = ata_wait_idle(ap);
2724 if (!ata_ok(drv_stat)) {
2725 ap->hsm_task_state = HSM_ST_ERR;
2729 qc = ata_qc_from_tag(ap, ap->active_tag);
2732 ap->hsm_task_state = HSM_ST_IDLE;
2734 ata_poll_qc_complete(qc, 0);
2736 /* another command may start at this point */
2743 * swap_buf_le16 - swap halves of 16-words in place
2744 * @buf: Buffer to swap
2745 * @buf_words: Number of 16-bit words in buffer.
2747 * Swap halves of 16-bit words if needed to convert from
2748 * little-endian byte order to native cpu byte order, or
2752 * Inherited from caller.
2754 void swap_buf_le16(u16 *buf, unsigned int buf_words)
2759 for (i = 0; i < buf_words; i++)
2760 buf[i] = le16_to_cpu(buf[i]);
2761 #endif /* __BIG_ENDIAN */
2765 * ata_mmio_data_xfer - Transfer data by MMIO
2766 * @ap: port to read/write
2768 * @buflen: buffer length
2769 * @write_data: read/write
2771 * Transfer data from/to the device data register by MMIO.
2774 * Inherited from caller.
2777 static void ata_mmio_data_xfer(struct ata_port *ap, unsigned char *buf,
2778 unsigned int buflen, int write_data)
2781 unsigned int words = buflen >> 1;
2782 u16 *buf16 = (u16 *) buf;
2783 void __iomem *mmio = (void __iomem *)ap->ioaddr.data_addr;
2785 /* Transfer multiple of 2 bytes */
2787 for (i = 0; i < words; i++)
2788 writew(le16_to_cpu(buf16[i]), mmio);
2790 for (i = 0; i < words; i++)
2791 buf16[i] = cpu_to_le16(readw(mmio));
2794 /* Transfer trailing 1 byte, if any. */
2795 if (unlikely(buflen & 0x01)) {
2796 u16 align_buf[1] = { 0 };
2797 unsigned char *trailing_buf = buf + buflen - 1;
2800 memcpy(align_buf, trailing_buf, 1);
2801 writew(le16_to_cpu(align_buf[0]), mmio);
2803 align_buf[0] = cpu_to_le16(readw(mmio));
2804 memcpy(trailing_buf, align_buf, 1);
2810 * ata_pio_data_xfer - Transfer data by PIO
2811 * @ap: port to read/write
2813 * @buflen: buffer length
2814 * @write_data: read/write
2816 * Transfer data from/to the device data register by PIO.
2819 * Inherited from caller.
2822 static void ata_pio_data_xfer(struct ata_port *ap, unsigned char *buf,
2823 unsigned int buflen, int write_data)
2825 unsigned int words = buflen >> 1;
2827 /* Transfer multiple of 2 bytes */
2829 outsw(ap->ioaddr.data_addr, buf, words);
2831 insw(ap->ioaddr.data_addr, buf, words);
2833 /* Transfer trailing 1 byte, if any. */
2834 if (unlikely(buflen & 0x01)) {
2835 u16 align_buf[1] = { 0 };
2836 unsigned char *trailing_buf = buf + buflen - 1;
2839 memcpy(align_buf, trailing_buf, 1);
2840 outw(le16_to_cpu(align_buf[0]), ap->ioaddr.data_addr);
2842 align_buf[0] = cpu_to_le16(inw(ap->ioaddr.data_addr));
2843 memcpy(trailing_buf, align_buf, 1);
2849 * ata_data_xfer - Transfer data from/to the data register.
2850 * @ap: port to read/write
2852 * @buflen: buffer length
2853 * @do_write: read/write
2855 * Transfer data from/to the device data register.
2858 * Inherited from caller.
2861 static void ata_data_xfer(struct ata_port *ap, unsigned char *buf,
2862 unsigned int buflen, int do_write)
2864 if (ap->flags & ATA_FLAG_MMIO)
2865 ata_mmio_data_xfer(ap, buf, buflen, do_write);
2867 ata_pio_data_xfer(ap, buf, buflen, do_write);
2871 * ata_pio_sector - Transfer ATA_SECT_SIZE (512 bytes) of data.
2872 * @qc: Command on going
2874 * Transfer ATA_SECT_SIZE of data from/to the ATA device.
2877 * Inherited from caller.
2880 static void ata_pio_sector(struct ata_queued_cmd *qc)
2882 int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
2883 struct scatterlist *sg = qc->sg;
2884 struct ata_port *ap = qc->ap;
2886 unsigned int offset;
2889 if (qc->cursect == (qc->nsect - 1))
2890 ap->hsm_task_state = HSM_ST_LAST;
2892 page = sg[qc->cursg].page;
2893 offset = sg[qc->cursg].offset + qc->cursg_ofs * ATA_SECT_SIZE;
2895 /* get the current page and offset */
2896 page = nth_page(page, (offset >> PAGE_SHIFT));
2897 offset %= PAGE_SIZE;
2899 buf = kmap(page) + offset;
2904 if ((qc->cursg_ofs * ATA_SECT_SIZE) == (&sg[qc->cursg])->length) {
2909 DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
2911 /* do the actual data transfer */
2912 do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
2913 ata_data_xfer(ap, buf, ATA_SECT_SIZE, do_write);
2919 * __atapi_pio_bytes - Transfer data from/to the ATAPI device.
2920 * @qc: Command on going
2921 * @bytes: number of bytes
2923 * Transfer Transfer data from/to the ATAPI device.
2926 * Inherited from caller.
2930 static void __atapi_pio_bytes(struct ata_queued_cmd *qc, unsigned int bytes)
2932 int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
2933 struct scatterlist *sg = qc->sg;
2934 struct ata_port *ap = qc->ap;
2937 unsigned int offset, count;
2939 if (qc->curbytes + bytes >= qc->nbytes)
2940 ap->hsm_task_state = HSM_ST_LAST;
2943 if (unlikely(qc->cursg >= qc->n_elem)) {
2945 * The end of qc->sg is reached and the device expects
2946 * more data to transfer. In order not to overrun qc->sg
2947 * and fulfill length specified in the byte count register,
2948 * - for read case, discard trailing data from the device
2949 * - for write case, padding zero data to the device
2951 u16 pad_buf[1] = { 0 };
2952 unsigned int words = bytes >> 1;
2955 if (words) /* warning if bytes > 1 */
2956 printk(KERN_WARNING "ata%u: %u bytes trailing data\n",
2959 for (i = 0; i < words; i++)
2960 ata_data_xfer(ap, (unsigned char*)pad_buf, 2, do_write);
2962 ap->hsm_task_state = HSM_ST_LAST;
2966 sg = &qc->sg[qc->cursg];
2969 offset = sg->offset + qc->cursg_ofs;
2971 /* get the current page and offset */
2972 page = nth_page(page, (offset >> PAGE_SHIFT));
2973 offset %= PAGE_SIZE;
2975 /* don't overrun current sg */
2976 count = min(sg->length - qc->cursg_ofs, bytes);
2978 /* don't cross page boundaries */
2979 count = min(count, (unsigned int)PAGE_SIZE - offset);
2981 buf = kmap(page) + offset;
2984 qc->curbytes += count;
2985 qc->cursg_ofs += count;
2987 if (qc->cursg_ofs == sg->length) {
2992 DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
2994 /* do the actual data transfer */
2995 ata_data_xfer(ap, buf, count, do_write);
3004 * atapi_pio_bytes - Transfer data from/to the ATAPI device.
3005 * @qc: Command on going
3007 * Transfer Transfer data from/to the ATAPI device.
3010 * Inherited from caller.
3013 static void atapi_pio_bytes(struct ata_queued_cmd *qc)
3015 struct ata_port *ap = qc->ap;
3016 struct ata_device *dev = qc->dev;
3017 unsigned int ireason, bc_lo, bc_hi, bytes;
3018 int i_write, do_write = (qc->tf.flags & ATA_TFLAG_WRITE) ? 1 : 0;
3020 ap->ops->tf_read(ap, &qc->tf);
3021 ireason = qc->tf.nsect;
3022 bc_lo = qc->tf.lbam;
3023 bc_hi = qc->tf.lbah;
3024 bytes = (bc_hi << 8) | bc_lo;
3026 /* shall be cleared to zero, indicating xfer of data */
3027 if (ireason & (1 << 0))
3030 /* make sure transfer direction matches expected */
3031 i_write = ((ireason & (1 << 1)) == 0) ? 1 : 0;
3032 if (do_write != i_write)
3035 __atapi_pio_bytes(qc, bytes);
3040 printk(KERN_INFO "ata%u: dev %u: ATAPI check failed\n",
3041 ap->id, dev->devno);
3042 ap->hsm_task_state = HSM_ST_ERR;
3046 * ata_pio_block - start PIO on a block
3047 * @ap: the target ata_port
3050 * None. (executing in kernel thread context)
3053 static void ata_pio_block(struct ata_port *ap)
3055 struct ata_queued_cmd *qc;
3059 * This is purely heuristic. This is a fast path.
3060 * Sometimes when we enter, BSY will be cleared in
3061 * a chk-status or two. If not, the drive is probably seeking
3062 * or something. Snooze for a couple msecs, then
3063 * chk-status again. If still busy, fall back to
3064 * HSM_ST_POLL state.
3066 status = ata_busy_wait(ap, ATA_BUSY, 5);
3067 if (status & ATA_BUSY) {
3069 status = ata_busy_wait(ap, ATA_BUSY, 10);
3070 if (status & ATA_BUSY) {
3071 ap->hsm_task_state = HSM_ST_POLL;
3072 ap->pio_task_timeout = jiffies + ATA_TMOUT_PIO;
3077 qc = ata_qc_from_tag(ap, ap->active_tag);
3080 if (is_atapi_taskfile(&qc->tf)) {
3081 /* no more data to transfer or unsupported ATAPI command */
3082 if ((status & ATA_DRQ) == 0) {
3083 ap->hsm_task_state = HSM_ST_LAST;
3087 atapi_pio_bytes(qc);
3089 /* handle BSY=0, DRQ=0 as error */
3090 if ((status & ATA_DRQ) == 0) {
3091 ap->hsm_task_state = HSM_ST_ERR;
3099 static void ata_pio_error(struct ata_port *ap)
3101 struct ata_queued_cmd *qc;
3103 printk(KERN_WARNING "ata%u: PIO error\n", ap->id);
3105 qc = ata_qc_from_tag(ap, ap->active_tag);
3108 ap->hsm_task_state = HSM_ST_IDLE;
3110 ata_poll_qc_complete(qc, AC_ERR_ATA_BUS);
3113 static void ata_pio_task(void *_data)
3115 struct ata_port *ap = _data;
3116 unsigned long timeout;
3123 switch (ap->hsm_task_state) {
3132 qc_completed = ata_pio_complete(ap);
3136 case HSM_ST_LAST_POLL:
3137 timeout = ata_pio_poll(ap);
3147 queue_delayed_work(ata_wq, &ap->pio_task, timeout);
3148 else if (!qc_completed)
3153 * ata_qc_timeout - Handle timeout of queued command
3154 * @qc: Command that timed out
3156 * Some part of the kernel (currently, only the SCSI layer)
3157 * has noticed that the active command on port @ap has not
3158 * completed after a specified length of time. Handle this
3159 * condition by disabling DMA (if necessary) and completing
3160 * transactions, with error if necessary.
3162 * This also handles the case of the "lost interrupt", where
3163 * for some reason (possibly hardware bug, possibly driver bug)
3164 * an interrupt was not delivered to the driver, even though the
3165 * transaction completed successfully.
3168 * Inherited from SCSI layer (none, can sleep)
3171 static void ata_qc_timeout(struct ata_queued_cmd *qc)
3173 struct ata_port *ap = qc->ap;
3174 struct ata_host_set *host_set = ap->host_set;
3175 struct ata_device *dev = qc->dev;
3176 u8 host_stat = 0, drv_stat;
3177 unsigned long flags;
3181 /* FIXME: doesn't this conflict with timeout handling? */
3182 if (qc->dev->class == ATA_DEV_ATAPI && qc->scsicmd) {
3183 struct scsi_cmnd *cmd = qc->scsicmd;
3185 if (!(cmd->eh_eflags & SCSI_EH_CANCEL_CMD)) {
3187 /* finish completing original command */
3188 spin_lock_irqsave(&host_set->lock, flags);
3189 __ata_qc_complete(qc);
3190 spin_unlock_irqrestore(&host_set->lock, flags);
3192 atapi_request_sense(ap, dev, cmd);
3194 cmd->result = (CHECK_CONDITION << 1) | (DID_OK << 16);
3195 scsi_finish_command(cmd);
3201 spin_lock_irqsave(&host_set->lock, flags);
3203 /* hack alert! We cannot use the supplied completion
3204 * function from inside the ->eh_strategy_handler() thread.
3205 * libata is the only user of ->eh_strategy_handler() in
3206 * any kernel, so the default scsi_done() assumes it is
3207 * not being called from the SCSI EH.
3209 qc->scsidone = scsi_finish_command;
3211 switch (qc->tf.protocol) {
3214 case ATA_PROT_ATAPI_DMA:
3215 host_stat = ap->ops->bmdma_status(ap);
3217 /* before we do anything else, clear DMA-Start bit */
3218 ap->ops->bmdma_stop(qc);
3224 drv_stat = ata_chk_status(ap);
3226 /* ack bmdma irq events */
3227 ap->ops->irq_clear(ap);
3229 printk(KERN_ERR "ata%u: command 0x%x timeout, stat 0x%x host_stat 0x%x\n",
3230 ap->id, qc->tf.command, drv_stat, host_stat);
3232 /* complete taskfile transaction */
3233 ata_qc_complete(qc, ac_err_mask(drv_stat));
3237 spin_unlock_irqrestore(&host_set->lock, flags);
3244 * ata_eng_timeout - Handle timeout of queued command
3245 * @ap: Port on which timed-out command is active
3247 * Some part of the kernel (currently, only the SCSI layer)
3248 * has noticed that the active command on port @ap has not
3249 * completed after a specified length of time. Handle this
3250 * condition by disabling DMA (if necessary) and completing
3251 * transactions, with error if necessary.
3253 * This also handles the case of the "lost interrupt", where
3254 * for some reason (possibly hardware bug, possibly driver bug)
3255 * an interrupt was not delivered to the driver, even though the
3256 * transaction completed successfully.
3259 * Inherited from SCSI layer (none, can sleep)
3262 void ata_eng_timeout(struct ata_port *ap)
3264 struct ata_queued_cmd *qc;
3268 qc = ata_qc_from_tag(ap, ap->active_tag);
3272 printk(KERN_ERR "ata%u: BUG: timeout without command\n",
3282 * ata_qc_new - Request an available ATA command, for queueing
3283 * @ap: Port associated with device @dev
3284 * @dev: Device from whom we request an available command structure
3290 static struct ata_queued_cmd *ata_qc_new(struct ata_port *ap)
3292 struct ata_queued_cmd *qc = NULL;
3295 for (i = 0; i < ATA_MAX_QUEUE; i++)
3296 if (!test_and_set_bit(i, &ap->qactive)) {
3297 qc = ata_qc_from_tag(ap, i);
3308 * ata_qc_new_init - Request an available ATA command, and initialize it
3309 * @ap: Port associated with device @dev
3310 * @dev: Device from whom we request an available command structure
3316 struct ata_queued_cmd *ata_qc_new_init(struct ata_port *ap,
3317 struct ata_device *dev)
3319 struct ata_queued_cmd *qc;
3321 qc = ata_qc_new(ap);
3328 qc->cursect = qc->cursg = qc->cursg_ofs = 0;
3330 qc->nbytes = qc->curbytes = 0;
3332 ata_tf_init(ap, &qc->tf, dev->devno);
3338 int ata_qc_complete_noop(struct ata_queued_cmd *qc, unsigned int err_mask)
3343 static void __ata_qc_complete(struct ata_queued_cmd *qc)
3345 struct ata_port *ap = qc->ap;
3346 unsigned int tag, do_clear = 0;
3350 if (likely(ata_tag_valid(tag))) {
3351 if (tag == ap->active_tag)
3352 ap->active_tag = ATA_TAG_POISON;
3353 qc->tag = ATA_TAG_POISON;
3358 struct completion *waiting = qc->waiting;
3363 if (likely(do_clear))
3364 clear_bit(tag, &ap->qactive);
3368 * ata_qc_free - free unused ata_queued_cmd
3369 * @qc: Command to complete
3371 * Designed to free unused ata_queued_cmd object
3372 * in case something prevents using it.
3375 * spin_lock_irqsave(host_set lock)
3377 void ata_qc_free(struct ata_queued_cmd *qc)
3379 assert(qc != NULL); /* ata_qc_from_tag _might_ return NULL */
3380 assert(qc->waiting == NULL); /* nothing should be waiting */
3382 __ata_qc_complete(qc);
3386 * ata_qc_complete - Complete an active ATA command
3387 * @qc: Command to complete
3388 * @drv_stat: ATA Status register contents
3390 * Indicate to the mid and upper layers that an ATA
3391 * command has completed, with either an ok or not-ok status.
3394 * spin_lock_irqsave(host_set lock)
3397 void ata_qc_complete(struct ata_queued_cmd *qc, unsigned int err_mask)
3401 assert(qc != NULL); /* ata_qc_from_tag _might_ return NULL */
3402 assert(qc->flags & ATA_QCFLAG_ACTIVE);
3404 if (likely(qc->flags & ATA_QCFLAG_DMAMAP))
3407 /* atapi: mark qc as inactive to prevent the interrupt handler
3408 * from completing the command twice later, before the error handler
3409 * is called. (when rc != 0 and atapi request sense is needed)
3411 qc->flags &= ~ATA_QCFLAG_ACTIVE;
3413 /* call completion callback */
3414 rc = qc->complete_fn(qc, err_mask);
3416 /* if callback indicates not to complete command (non-zero),
3417 * return immediately
3422 __ata_qc_complete(qc);
3427 static inline int ata_should_dma_map(struct ata_queued_cmd *qc)
3429 struct ata_port *ap = qc->ap;
3431 switch (qc->tf.protocol) {
3433 case ATA_PROT_ATAPI_DMA:
3436 case ATA_PROT_ATAPI:
3438 case ATA_PROT_PIO_MULT:
3439 if (ap->flags & ATA_FLAG_PIO_DMA)
3452 * ata_qc_issue - issue taskfile to device
3453 * @qc: command to issue to device
3455 * Prepare an ATA command to submission to device.
3456 * This includes mapping the data into a DMA-able
3457 * area, filling in the S/G table, and finally
3458 * writing the taskfile to hardware, starting the command.
3461 * spin_lock_irqsave(host_set lock)
3464 * Zero on success, negative on error.
3467 int ata_qc_issue(struct ata_queued_cmd *qc)
3469 struct ata_port *ap = qc->ap;
3471 if (ata_should_dma_map(qc)) {
3472 if (qc->flags & ATA_QCFLAG_SG) {
3473 if (ata_sg_setup(qc))
3475 } else if (qc->flags & ATA_QCFLAG_SINGLE) {
3476 if (ata_sg_setup_one(qc))
3480 qc->flags &= ~ATA_QCFLAG_DMAMAP;
3483 ap->ops->qc_prep(qc);
3485 qc->ap->active_tag = qc->tag;
3486 qc->flags |= ATA_QCFLAG_ACTIVE;
3488 return ap->ops->qc_issue(qc);
3496 * ata_qc_issue_prot - issue taskfile to device in proto-dependent manner
3497 * @qc: command to issue to device
3499 * Using various libata functions and hooks, this function
3500 * starts an ATA command. ATA commands are grouped into
3501 * classes called "protocols", and issuing each type of protocol
3502 * is slightly different.
3504 * May be used as the qc_issue() entry in ata_port_operations.
3507 * spin_lock_irqsave(host_set lock)
3510 * Zero on success, negative on error.
3513 int ata_qc_issue_prot(struct ata_queued_cmd *qc)
3515 struct ata_port *ap = qc->ap;
3517 ata_dev_select(ap, qc->dev->devno, 1, 0);
3519 switch (qc->tf.protocol) {
3520 case ATA_PROT_NODATA:
3521 ata_tf_to_host(ap, &qc->tf);
3525 ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
3526 ap->ops->bmdma_setup(qc); /* set up bmdma */
3527 ap->ops->bmdma_start(qc); /* initiate bmdma */
3530 case ATA_PROT_PIO: /* load tf registers, initiate polling pio */
3531 ata_qc_set_polling(qc);
3532 ata_tf_to_host(ap, &qc->tf);
3533 ap->hsm_task_state = HSM_ST;
3534 queue_work(ata_wq, &ap->pio_task);
3537 case ATA_PROT_ATAPI:
3538 ata_qc_set_polling(qc);
3539 ata_tf_to_host(ap, &qc->tf);
3540 queue_work(ata_wq, &ap->packet_task);
3543 case ATA_PROT_ATAPI_NODATA:
3544 ap->flags |= ATA_FLAG_NOINTR;
3545 ata_tf_to_host(ap, &qc->tf);
3546 queue_work(ata_wq, &ap->packet_task);
3549 case ATA_PROT_ATAPI_DMA:
3550 ap->flags |= ATA_FLAG_NOINTR;
3551 ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
3552 ap->ops->bmdma_setup(qc); /* set up bmdma */
3553 queue_work(ata_wq, &ap->packet_task);
3565 * ata_bmdma_setup_mmio - Set up PCI IDE BMDMA transaction
3566 * @qc: Info associated with this ATA transaction.
3569 * spin_lock_irqsave(host_set lock)
3572 static void ata_bmdma_setup_mmio (struct ata_queued_cmd *qc)
3574 struct ata_port *ap = qc->ap;
3575 unsigned int rw = (qc->tf.flags & ATA_TFLAG_WRITE);
3577 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
3579 /* load PRD table addr. */
3580 mb(); /* make sure PRD table writes are visible to controller */
3581 writel(ap->prd_dma, mmio + ATA_DMA_TABLE_OFS);
3583 /* specify data direction, triple-check start bit is clear */
3584 dmactl = readb(mmio + ATA_DMA_CMD);
3585 dmactl &= ~(ATA_DMA_WR | ATA_DMA_START);
3587 dmactl |= ATA_DMA_WR;
3588 writeb(dmactl, mmio + ATA_DMA_CMD);
3590 /* issue r/w command */
3591 ap->ops->exec_command(ap, &qc->tf);
3595 * ata_bmdma_start_mmio - Start a PCI IDE BMDMA transaction
3596 * @qc: Info associated with this ATA transaction.
3599 * spin_lock_irqsave(host_set lock)
3602 static void ata_bmdma_start_mmio (struct ata_queued_cmd *qc)
3604 struct ata_port *ap = qc->ap;
3605 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
3608 /* start host DMA transaction */
3609 dmactl = readb(mmio + ATA_DMA_CMD);
3610 writeb(dmactl | ATA_DMA_START, mmio + ATA_DMA_CMD);
3612 /* Strictly, one may wish to issue a readb() here, to
3613 * flush the mmio write. However, control also passes
3614 * to the hardware at this point, and it will interrupt
3615 * us when we are to resume control. So, in effect,
3616 * we don't care when the mmio write flushes.
3617 * Further, a read of the DMA status register _immediately_
3618 * following the write may not be what certain flaky hardware
3619 * is expected, so I think it is best to not add a readb()
3620 * without first all the MMIO ATA cards/mobos.
3621 * Or maybe I'm just being paranoid.
3626 * ata_bmdma_setup_pio - Set up PCI IDE BMDMA transaction (PIO)
3627 * @qc: Info associated with this ATA transaction.
3630 * spin_lock_irqsave(host_set lock)
3633 static void ata_bmdma_setup_pio (struct ata_queued_cmd *qc)
3635 struct ata_port *ap = qc->ap;
3636 unsigned int rw = (qc->tf.flags & ATA_TFLAG_WRITE);
3639 /* load PRD table addr. */
3640 outl(ap->prd_dma, ap->ioaddr.bmdma_addr + ATA_DMA_TABLE_OFS);
3642 /* specify data direction, triple-check start bit is clear */
3643 dmactl = inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3644 dmactl &= ~(ATA_DMA_WR | ATA_DMA_START);
3646 dmactl |= ATA_DMA_WR;
3647 outb(dmactl, ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3649 /* issue r/w command */
3650 ap->ops->exec_command(ap, &qc->tf);
3654 * ata_bmdma_start_pio - Start a PCI IDE BMDMA transaction (PIO)
3655 * @qc: Info associated with this ATA transaction.
3658 * spin_lock_irqsave(host_set lock)
3661 static void ata_bmdma_start_pio (struct ata_queued_cmd *qc)
3663 struct ata_port *ap = qc->ap;
3666 /* start host DMA transaction */
3667 dmactl = inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3668 outb(dmactl | ATA_DMA_START,
3669 ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3674 * ata_bmdma_start - Start a PCI IDE BMDMA transaction
3675 * @qc: Info associated with this ATA transaction.
3677 * Writes the ATA_DMA_START flag to the DMA command register.
3679 * May be used as the bmdma_start() entry in ata_port_operations.
3682 * spin_lock_irqsave(host_set lock)
3684 void ata_bmdma_start(struct ata_queued_cmd *qc)
3686 if (qc->ap->flags & ATA_FLAG_MMIO)
3687 ata_bmdma_start_mmio(qc);
3689 ata_bmdma_start_pio(qc);
3694 * ata_bmdma_setup - Set up PCI IDE BMDMA transaction
3695 * @qc: Info associated with this ATA transaction.
3697 * Writes address of PRD table to device's PRD Table Address
3698 * register, sets the DMA control register, and calls
3699 * ops->exec_command() to start the transfer.
3701 * May be used as the bmdma_setup() entry in ata_port_operations.
3704 * spin_lock_irqsave(host_set lock)
3706 void ata_bmdma_setup(struct ata_queued_cmd *qc)
3708 if (qc->ap->flags & ATA_FLAG_MMIO)
3709 ata_bmdma_setup_mmio(qc);
3711 ata_bmdma_setup_pio(qc);
3716 * ata_bmdma_irq_clear - Clear PCI IDE BMDMA interrupt.
3717 * @ap: Port associated with this ATA transaction.
3719 * Clear interrupt and error flags in DMA status register.
3721 * May be used as the irq_clear() entry in ata_port_operations.
3724 * spin_lock_irqsave(host_set lock)
3727 void ata_bmdma_irq_clear(struct ata_port *ap)
3729 if (ap->flags & ATA_FLAG_MMIO) {
3730 void __iomem *mmio = ((void __iomem *) ap->ioaddr.bmdma_addr) + ATA_DMA_STATUS;
3731 writeb(readb(mmio), mmio);
3733 unsigned long addr = ap->ioaddr.bmdma_addr + ATA_DMA_STATUS;
3734 outb(inb(addr), addr);
3741 * ata_bmdma_status - Read PCI IDE BMDMA status
3742 * @ap: Port associated with this ATA transaction.
3744 * Read and return BMDMA status register.
3746 * May be used as the bmdma_status() entry in ata_port_operations.
3749 * spin_lock_irqsave(host_set lock)
3752 u8 ata_bmdma_status(struct ata_port *ap)
3755 if (ap->flags & ATA_FLAG_MMIO) {
3756 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
3757 host_stat = readb(mmio + ATA_DMA_STATUS);
3759 host_stat = inb(ap->ioaddr.bmdma_addr + ATA_DMA_STATUS);
3765 * ata_bmdma_stop - Stop PCI IDE BMDMA transfer
3766 * @qc: Command we are ending DMA for
3768 * Clears the ATA_DMA_START flag in the dma control register
3770 * May be used as the bmdma_stop() entry in ata_port_operations.
3773 * spin_lock_irqsave(host_set lock)
3776 void ata_bmdma_stop(struct ata_queued_cmd *qc)
3778 struct ata_port *ap = qc->ap;
3779 if (ap->flags & ATA_FLAG_MMIO) {
3780 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
3782 /* clear start/stop bit */
3783 writeb(readb(mmio + ATA_DMA_CMD) & ~ATA_DMA_START,
3784 mmio + ATA_DMA_CMD);
3786 /* clear start/stop bit */
3787 outb(inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD) & ~ATA_DMA_START,
3788 ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3791 /* one-PIO-cycle guaranteed wait, per spec, for HDMA1:0 transition */
3792 ata_altstatus(ap); /* dummy read */
3796 * ata_host_intr - Handle host interrupt for given (port, task)
3797 * @ap: Port on which interrupt arrived (possibly...)
3798 * @qc: Taskfile currently active in engine
3800 * Handle host interrupt for given queued command. Currently,
3801 * only DMA interrupts are handled. All other commands are
3802 * handled via polling with interrupts disabled (nIEN bit).
3805 * spin_lock_irqsave(host_set lock)
3808 * One if interrupt was handled, zero if not (shared irq).
3811 inline unsigned int ata_host_intr (struct ata_port *ap,
3812 struct ata_queued_cmd *qc)
3814 u8 status, host_stat;
3816 switch (qc->tf.protocol) {
3819 case ATA_PROT_ATAPI_DMA:
3820 case ATA_PROT_ATAPI:
3821 /* check status of DMA engine */
3822 host_stat = ap->ops->bmdma_status(ap);
3823 VPRINTK("ata%u: host_stat 0x%X\n", ap->id, host_stat);
3825 /* if it's not our irq... */
3826 if (!(host_stat & ATA_DMA_INTR))
3829 /* before we do anything else, clear DMA-Start bit */
3830 ap->ops->bmdma_stop(qc);
3834 case ATA_PROT_ATAPI_NODATA:
3835 case ATA_PROT_NODATA:
3836 /* check altstatus */
3837 status = ata_altstatus(ap);
3838 if (status & ATA_BUSY)
3841 /* check main status, clearing INTRQ */
3842 status = ata_chk_status(ap);
3843 if (unlikely(status & ATA_BUSY))
3845 DPRINTK("ata%u: protocol %d (dev_stat 0x%X)\n",
3846 ap->id, qc->tf.protocol, status);
3848 /* ack bmdma irq events */
3849 ap->ops->irq_clear(ap);
3851 /* complete taskfile transaction */
3852 ata_qc_complete(qc, ac_err_mask(status));
3859 return 1; /* irq handled */
3862 ap->stats.idle_irq++;
3865 if ((ap->stats.idle_irq % 1000) == 0) {
3867 ata_irq_ack(ap, 0); /* debug trap */
3868 printk(KERN_WARNING "ata%d: irq trap\n", ap->id);
3871 return 0; /* irq not handled */
3875 * ata_interrupt - Default ATA host interrupt handler
3876 * @irq: irq line (unused)
3877 * @dev_instance: pointer to our ata_host_set information structure
3880 * Default interrupt handler for PCI IDE devices. Calls
3881 * ata_host_intr() for each port that is not disabled.
3884 * Obtains host_set lock during operation.
3887 * IRQ_NONE or IRQ_HANDLED.
3890 irqreturn_t ata_interrupt (int irq, void *dev_instance, struct pt_regs *regs)
3892 struct ata_host_set *host_set = dev_instance;
3894 unsigned int handled = 0;
3895 unsigned long flags;
3897 /* TODO: make _irqsave conditional on x86 PCI IDE legacy mode */
3898 spin_lock_irqsave(&host_set->lock, flags);
3900 for (i = 0; i < host_set->n_ports; i++) {
3901 struct ata_port *ap;
3903 ap = host_set->ports[i];
3905 !(ap->flags & (ATA_FLAG_PORT_DISABLED | ATA_FLAG_NOINTR))) {
3906 struct ata_queued_cmd *qc;
3908 qc = ata_qc_from_tag(ap, ap->active_tag);
3909 if (qc && (!(qc->tf.ctl & ATA_NIEN)) &&
3910 (qc->flags & ATA_QCFLAG_ACTIVE))
3911 handled |= ata_host_intr(ap, qc);
3915 spin_unlock_irqrestore(&host_set->lock, flags);
3917 return IRQ_RETVAL(handled);
3921 * atapi_packet_task - Write CDB bytes to hardware
3922 * @_data: Port to which ATAPI device is attached.
3924 * When device has indicated its readiness to accept
3925 * a CDB, this function is called. Send the CDB.
3926 * If DMA is to be performed, exit immediately.
3927 * Otherwise, we are in polling mode, so poll
3928 * status under operation succeeds or fails.
3931 * Kernel thread context (may sleep)
3934 static void atapi_packet_task(void *_data)
3936 struct ata_port *ap = _data;
3937 struct ata_queued_cmd *qc;
3940 qc = ata_qc_from_tag(ap, ap->active_tag);
3942 assert(qc->flags & ATA_QCFLAG_ACTIVE);
3944 /* sleep-wait for BSY to clear */
3945 DPRINTK("busy wait\n");
3946 if (ata_busy_sleep(ap, ATA_TMOUT_CDB_QUICK, ATA_TMOUT_CDB))
3947 goto err_out_status;
3949 /* make sure DRQ is set */
3950 status = ata_chk_status(ap);
3951 if ((status & (ATA_BUSY | ATA_DRQ)) != ATA_DRQ)
3955 DPRINTK("send cdb\n");
3956 assert(ap->cdb_len >= 12);
3958 if (qc->tf.protocol == ATA_PROT_ATAPI_DMA ||
3959 qc->tf.protocol == ATA_PROT_ATAPI_NODATA) {
3960 unsigned long flags;
3962 /* Once we're done issuing command and kicking bmdma,
3963 * irq handler takes over. To not lose irq, we need
3964 * to clear NOINTR flag before sending cdb, but
3965 * interrupt handler shouldn't be invoked before we're
3966 * finished. Hence, the following locking.
3968 spin_lock_irqsave(&ap->host_set->lock, flags);
3969 ap->flags &= ~ATA_FLAG_NOINTR;
3970 ata_data_xfer(ap, qc->cdb, ap->cdb_len, 1);
3971 if (qc->tf.protocol == ATA_PROT_ATAPI_DMA)
3972 ap->ops->bmdma_start(qc); /* initiate bmdma */
3973 spin_unlock_irqrestore(&ap->host_set->lock, flags);
3975 ata_data_xfer(ap, qc->cdb, ap->cdb_len, 1);
3977 /* PIO commands are handled by polling */
3978 ap->hsm_task_state = HSM_ST;
3979 queue_work(ata_wq, &ap->pio_task);
3985 status = ata_chk_status(ap);
3987 ata_poll_qc_complete(qc, __ac_err_mask(status));
3992 * ata_port_start - Set port up for dma.
3993 * @ap: Port to initialize
3995 * Called just after data structures for each port are
3996 * initialized. Allocates space for PRD table.
3998 * May be used as the port_start() entry in ata_port_operations.
4001 * Inherited from caller.
4004 int ata_port_start (struct ata_port *ap)
4006 struct device *dev = ap->host_set->dev;
4008 ap->prd = dma_alloc_coherent(dev, ATA_PRD_TBL_SZ, &ap->prd_dma, GFP_KERNEL);
4012 DPRINTK("prd alloc, virt %p, dma %llx\n", ap->prd, (unsigned long long) ap->prd_dma);
4019 * ata_port_stop - Undo ata_port_start()
4020 * @ap: Port to shut down
4022 * Frees the PRD table.
4024 * May be used as the port_stop() entry in ata_port_operations.
4027 * Inherited from caller.
4030 void ata_port_stop (struct ata_port *ap)
4032 struct device *dev = ap->host_set->dev;
4034 dma_free_coherent(dev, ATA_PRD_TBL_SZ, ap->prd, ap->prd_dma);
4037 void ata_host_stop (struct ata_host_set *host_set)
4039 if (host_set->mmio_base)
4040 iounmap(host_set->mmio_base);
4045 * ata_host_remove - Unregister SCSI host structure with upper layers
4046 * @ap: Port to unregister
4047 * @do_unregister: 1 if we fully unregister, 0 to just stop the port
4050 * Inherited from caller.
4053 static void ata_host_remove(struct ata_port *ap, unsigned int do_unregister)
4055 struct Scsi_Host *sh = ap->host;
4060 scsi_remove_host(sh);
4062 ap->ops->port_stop(ap);
4066 * ata_host_init - Initialize an ata_port structure
4067 * @ap: Structure to initialize
4068 * @host: associated SCSI mid-layer structure
4069 * @host_set: Collection of hosts to which @ap belongs
4070 * @ent: Probe information provided by low-level driver
4071 * @port_no: Port number associated with this ata_port
4073 * Initialize a new ata_port structure, and its associated
4077 * Inherited from caller.
4080 static void ata_host_init(struct ata_port *ap, struct Scsi_Host *host,
4081 struct ata_host_set *host_set,
4082 const struct ata_probe_ent *ent, unsigned int port_no)
4088 host->max_channel = 1;
4089 host->unique_id = ata_unique_id++;
4090 host->max_cmd_len = 12;
4092 scsi_assign_lock(host, &host_set->lock);
4094 ap->flags = ATA_FLAG_PORT_DISABLED;
4095 ap->id = host->unique_id;
4097 ap->ctl = ATA_DEVCTL_OBS;
4098 ap->host_set = host_set;
4099 ap->port_no = port_no;
4101 ent->legacy_mode ? ent->hard_port_no : port_no;
4102 ap->pio_mask = ent->pio_mask;
4103 ap->mwdma_mask = ent->mwdma_mask;
4104 ap->udma_mask = ent->udma_mask;
4105 ap->flags |= ent->host_flags;
4106 ap->ops = ent->port_ops;
4107 ap->cbl = ATA_CBL_NONE;
4108 ap->active_tag = ATA_TAG_POISON;
4109 ap->last_ctl = 0xFF;
4111 INIT_WORK(&ap->packet_task, atapi_packet_task, ap);
4112 INIT_WORK(&ap->pio_task, ata_pio_task, ap);
4114 for (i = 0; i < ATA_MAX_DEVICES; i++)
4115 ap->device[i].devno = i;
4118 ap->stats.unhandled_irq = 1;
4119 ap->stats.idle_irq = 1;
4122 memcpy(&ap->ioaddr, &ent->port[port_no], sizeof(struct ata_ioports));
4126 * ata_host_add - Attach low-level ATA driver to system
4127 * @ent: Information provided by low-level driver
4128 * @host_set: Collections of ports to which we add
4129 * @port_no: Port number associated with this host
4131 * Attach low-level ATA driver to system.
4134 * PCI/etc. bus probe sem.
4137 * New ata_port on success, for NULL on error.
4140 static struct ata_port * ata_host_add(const struct ata_probe_ent *ent,
4141 struct ata_host_set *host_set,
4142 unsigned int port_no)
4144 struct Scsi_Host *host;
4145 struct ata_port *ap;
4149 host = scsi_host_alloc(ent->sht, sizeof(struct ata_port));
4153 ap = (struct ata_port *) &host->hostdata[0];
4155 ata_host_init(ap, host, host_set, ent, port_no);
4157 rc = ap->ops->port_start(ap);
4164 scsi_host_put(host);
4169 * ata_device_add - Register hardware device with ATA and SCSI layers
4170 * @ent: Probe information describing hardware device to be registered
4172 * This function processes the information provided in the probe
4173 * information struct @ent, allocates the necessary ATA and SCSI
4174 * host information structures, initializes them, and registers
4175 * everything with requisite kernel subsystems.
4177 * This function requests irqs, probes the ATA bus, and probes
4181 * PCI/etc. bus probe sem.
4184 * Number of ports registered. Zero on error (no ports registered).
4187 int ata_device_add(const struct ata_probe_ent *ent)
4189 unsigned int count = 0, i;
4190 struct device *dev = ent->dev;
4191 struct ata_host_set *host_set;
4194 /* alloc a container for our list of ATA ports (buses) */
4195 host_set = kzalloc(sizeof(struct ata_host_set) +
4196 (ent->n_ports * sizeof(void *)), GFP_KERNEL);
4199 spin_lock_init(&host_set->lock);
4201 host_set->dev = dev;
4202 host_set->n_ports = ent->n_ports;
4203 host_set->irq = ent->irq;
4204 host_set->mmio_base = ent->mmio_base;
4205 host_set->private_data = ent->private_data;
4206 host_set->ops = ent->port_ops;
4208 /* register each port bound to this device */
4209 for (i = 0; i < ent->n_ports; i++) {
4210 struct ata_port *ap;
4211 unsigned long xfer_mode_mask;
4213 ap = ata_host_add(ent, host_set, i);
4217 host_set->ports[i] = ap;
4218 xfer_mode_mask =(ap->udma_mask << ATA_SHIFT_UDMA) |
4219 (ap->mwdma_mask << ATA_SHIFT_MWDMA) |
4220 (ap->pio_mask << ATA_SHIFT_PIO);
4222 /* print per-port info to dmesg */
4223 printk(KERN_INFO "ata%u: %cATA max %s cmd 0x%lX ctl 0x%lX "
4224 "bmdma 0x%lX irq %lu\n",
4226 ap->flags & ATA_FLAG_SATA ? 'S' : 'P',
4227 ata_mode_string(xfer_mode_mask),
4228 ap->ioaddr.cmd_addr,
4229 ap->ioaddr.ctl_addr,
4230 ap->ioaddr.bmdma_addr,
4234 host_set->ops->irq_clear(ap);
4241 /* obtain irq, that is shared between channels */
4242 if (request_irq(ent->irq, ent->port_ops->irq_handler, ent->irq_flags,
4243 DRV_NAME, host_set))
4246 /* perform each probe synchronously */
4247 DPRINTK("probe begin\n");
4248 for (i = 0; i < count; i++) {
4249 struct ata_port *ap;
4252 ap = host_set->ports[i];
4254 DPRINTK("ata%u: probe begin\n", ap->id);
4255 rc = ata_bus_probe(ap);
4256 DPRINTK("ata%u: probe end\n", ap->id);
4259 /* FIXME: do something useful here?
4260 * Current libata behavior will
4261 * tear down everything when
4262 * the module is removed
4263 * or the h/w is unplugged.
4267 rc = scsi_add_host(ap->host, dev);
4269 printk(KERN_ERR "ata%u: scsi_add_host failed\n",
4271 /* FIXME: do something useful here */
4272 /* FIXME: handle unconditional calls to
4273 * scsi_scan_host and ata_host_remove, below,
4279 /* probes are done, now scan each port's disk(s) */
4280 DPRINTK("probe begin\n");
4281 for (i = 0; i < count; i++) {
4282 struct ata_port *ap = host_set->ports[i];
4284 ata_scsi_scan_host(ap);
4287 dev_set_drvdata(dev, host_set);
4289 VPRINTK("EXIT, returning %u\n", ent->n_ports);
4290 return ent->n_ports; /* success */
4293 for (i = 0; i < count; i++) {
4294 ata_host_remove(host_set->ports[i], 1);
4295 scsi_host_put(host_set->ports[i]->host);
4299 VPRINTK("EXIT, returning 0\n");
4304 * ata_host_set_remove - PCI layer callback for device removal
4305 * @host_set: ATA host set that was removed
4307 * Unregister all objects associated with this host set. Free those
4311 * Inherited from calling layer (may sleep).
4314 void ata_host_set_remove(struct ata_host_set *host_set)
4316 struct ata_port *ap;
4319 for (i = 0; i < host_set->n_ports; i++) {
4320 ap = host_set->ports[i];
4321 scsi_remove_host(ap->host);
4324 free_irq(host_set->irq, host_set);
4326 for (i = 0; i < host_set->n_ports; i++) {
4327 ap = host_set->ports[i];
4329 ata_scsi_release(ap->host);
4331 if ((ap->flags & ATA_FLAG_NO_LEGACY) == 0) {
4332 struct ata_ioports *ioaddr = &ap->ioaddr;
4334 if (ioaddr->cmd_addr == 0x1f0)
4335 release_region(0x1f0, 8);
4336 else if (ioaddr->cmd_addr == 0x170)
4337 release_region(0x170, 8);
4340 scsi_host_put(ap->host);
4343 if (host_set->ops->host_stop)
4344 host_set->ops->host_stop(host_set);
4350 * ata_scsi_release - SCSI layer callback hook for host unload
4351 * @host: libata host to be unloaded
4353 * Performs all duties necessary to shut down a libata port...
4354 * Kill port kthread, disable port, and release resources.
4357 * Inherited from SCSI layer.
4363 int ata_scsi_release(struct Scsi_Host *host)
4365 struct ata_port *ap = (struct ata_port *) &host->hostdata[0];
4369 ap->ops->port_disable(ap);
4370 ata_host_remove(ap, 0);
4377 * ata_std_ports - initialize ioaddr with standard port offsets.
4378 * @ioaddr: IO address structure to be initialized
4380 * Utility function which initializes data_addr, error_addr,
4381 * feature_addr, nsect_addr, lbal_addr, lbam_addr, lbah_addr,
4382 * device_addr, status_addr, and command_addr to standard offsets
4383 * relative to cmd_addr.
4385 * Does not set ctl_addr, altstatus_addr, bmdma_addr, or scr_addr.
4388 void ata_std_ports(struct ata_ioports *ioaddr)
4390 ioaddr->data_addr = ioaddr->cmd_addr + ATA_REG_DATA;
4391 ioaddr->error_addr = ioaddr->cmd_addr + ATA_REG_ERR;
4392 ioaddr->feature_addr = ioaddr->cmd_addr + ATA_REG_FEATURE;
4393 ioaddr->nsect_addr = ioaddr->cmd_addr + ATA_REG_NSECT;
4394 ioaddr->lbal_addr = ioaddr->cmd_addr + ATA_REG_LBAL;
4395 ioaddr->lbam_addr = ioaddr->cmd_addr + ATA_REG_LBAM;
4396 ioaddr->lbah_addr = ioaddr->cmd_addr + ATA_REG_LBAH;
4397 ioaddr->device_addr = ioaddr->cmd_addr + ATA_REG_DEVICE;
4398 ioaddr->status_addr = ioaddr->cmd_addr + ATA_REG_STATUS;
4399 ioaddr->command_addr = ioaddr->cmd_addr + ATA_REG_CMD;
4402 static struct ata_probe_ent *
4403 ata_probe_ent_alloc(struct device *dev, const struct ata_port_info *port)
4405 struct ata_probe_ent *probe_ent;
4407 probe_ent = kzalloc(sizeof(*probe_ent), GFP_KERNEL);
4409 printk(KERN_ERR DRV_NAME "(%s): out of memory\n",
4410 kobject_name(&(dev->kobj)));
4414 INIT_LIST_HEAD(&probe_ent->node);
4415 probe_ent->dev = dev;
4417 probe_ent->sht = port->sht;
4418 probe_ent->host_flags = port->host_flags;
4419 probe_ent->pio_mask = port->pio_mask;
4420 probe_ent->mwdma_mask = port->mwdma_mask;
4421 probe_ent->udma_mask = port->udma_mask;
4422 probe_ent->port_ops = port->port_ops;
4431 void ata_pci_host_stop (struct ata_host_set *host_set)
4433 struct pci_dev *pdev = to_pci_dev(host_set->dev);
4435 pci_iounmap(pdev, host_set->mmio_base);
4439 * ata_pci_init_native_mode - Initialize native-mode driver
4440 * @pdev: pci device to be initialized
4441 * @port: array[2] of pointers to port info structures.
4442 * @ports: bitmap of ports present
4444 * Utility function which allocates and initializes an
4445 * ata_probe_ent structure for a standard dual-port
4446 * PIO-based IDE controller. The returned ata_probe_ent
4447 * structure can be passed to ata_device_add(). The returned
4448 * ata_probe_ent structure should then be freed with kfree().
4450 * The caller need only pass the address of the primary port, the
4451 * secondary will be deduced automatically. If the device has non
4452 * standard secondary port mappings this function can be called twice,
4453 * once for each interface.
4456 struct ata_probe_ent *
4457 ata_pci_init_native_mode(struct pci_dev *pdev, struct ata_port_info **port, int ports)
4459 struct ata_probe_ent *probe_ent =
4460 ata_probe_ent_alloc(pci_dev_to_dev(pdev), port[0]);
4466 probe_ent->irq = pdev->irq;
4467 probe_ent->irq_flags = SA_SHIRQ;
4469 if (ports & ATA_PORT_PRIMARY) {
4470 probe_ent->port[p].cmd_addr = pci_resource_start(pdev, 0);
4471 probe_ent->port[p].altstatus_addr =
4472 probe_ent->port[p].ctl_addr =
4473 pci_resource_start(pdev, 1) | ATA_PCI_CTL_OFS;
4474 probe_ent->port[p].bmdma_addr = pci_resource_start(pdev, 4);
4475 ata_std_ports(&probe_ent->port[p]);
4479 if (ports & ATA_PORT_SECONDARY) {
4480 probe_ent->port[p].cmd_addr = pci_resource_start(pdev, 2);
4481 probe_ent->port[p].altstatus_addr =
4482 probe_ent->port[p].ctl_addr =
4483 pci_resource_start(pdev, 3) | ATA_PCI_CTL_OFS;
4484 probe_ent->port[p].bmdma_addr = pci_resource_start(pdev, 4) + 8;
4485 ata_std_ports(&probe_ent->port[p]);
4489 probe_ent->n_ports = p;
4493 static struct ata_probe_ent *ata_pci_init_legacy_port(struct pci_dev *pdev, struct ata_port_info *port, int port_num)
4495 struct ata_probe_ent *probe_ent;
4497 probe_ent = ata_probe_ent_alloc(pci_dev_to_dev(pdev), port);
4501 probe_ent->legacy_mode = 1;
4502 probe_ent->n_ports = 1;
4503 probe_ent->hard_port_no = port_num;
4508 probe_ent->irq = 14;
4509 probe_ent->port[0].cmd_addr = 0x1f0;
4510 probe_ent->port[0].altstatus_addr =
4511 probe_ent->port[0].ctl_addr = 0x3f6;
4514 probe_ent->irq = 15;
4515 probe_ent->port[0].cmd_addr = 0x170;
4516 probe_ent->port[0].altstatus_addr =
4517 probe_ent->port[0].ctl_addr = 0x376;
4520 probe_ent->port[0].bmdma_addr = pci_resource_start(pdev, 4) + 8 * port_num;
4521 ata_std_ports(&probe_ent->port[0]);
4526 * ata_pci_init_one - Initialize/register PCI IDE host controller
4527 * @pdev: Controller to be initialized
4528 * @port_info: Information from low-level host driver
4529 * @n_ports: Number of ports attached to host controller
4531 * This is a helper function which can be called from a driver's
4532 * xxx_init_one() probe function if the hardware uses traditional
4533 * IDE taskfile registers.
4535 * This function calls pci_enable_device(), reserves its register
4536 * regions, sets the dma mask, enables bus master mode, and calls
4540 * Inherited from PCI layer (may sleep).
4543 * Zero on success, negative on errno-based value on error.
4546 int ata_pci_init_one (struct pci_dev *pdev, struct ata_port_info **port_info,
4547 unsigned int n_ports)
4549 struct ata_probe_ent *probe_ent = NULL, *probe_ent2 = NULL;
4550 struct ata_port_info *port[2];
4552 unsigned int legacy_mode = 0;
4553 int disable_dev_on_err = 1;
4558 port[0] = port_info[0];
4560 port[1] = port_info[1];
4564 if ((port[0]->host_flags & ATA_FLAG_NO_LEGACY) == 0
4565 && (pdev->class >> 8) == PCI_CLASS_STORAGE_IDE) {
4566 /* TODO: What if one channel is in native mode ... */
4567 pci_read_config_byte(pdev, PCI_CLASS_PROG, &tmp8);
4568 mask = (1 << 2) | (1 << 0);
4569 if ((tmp8 & mask) != mask)
4570 legacy_mode = (1 << 3);
4574 if ((!legacy_mode) && (n_ports > 2)) {
4575 printk(KERN_ERR "ata: BUG: native mode, n_ports > 2\n");
4580 /* FIXME: Really for ATA it isn't safe because the device may be
4581 multi-purpose and we want to leave it alone if it was already
4582 enabled. Secondly for shared use as Arjan says we want refcounting
4584 Checking dev->is_enabled is insufficient as this is not set at
4585 boot for the primary video which is BIOS enabled
4588 rc = pci_enable_device(pdev);
4592 rc = pci_request_regions(pdev, DRV_NAME);
4594 disable_dev_on_err = 0;
4598 /* FIXME: Should use platform specific mappers for legacy port ranges */
4600 if (!request_region(0x1f0, 8, "libata")) {
4601 struct resource *conflict, res;
4603 res.end = 0x1f0 + 8 - 1;
4604 conflict = ____request_resource(&ioport_resource, &res);
4605 if (!strcmp(conflict->name, "libata"))
4606 legacy_mode |= (1 << 0);
4608 disable_dev_on_err = 0;
4609 printk(KERN_WARNING "ata: 0x1f0 IDE port busy\n");
4612 legacy_mode |= (1 << 0);
4614 if (!request_region(0x170, 8, "libata")) {
4615 struct resource *conflict, res;
4617 res.end = 0x170 + 8 - 1;
4618 conflict = ____request_resource(&ioport_resource, &res);
4619 if (!strcmp(conflict->name, "libata"))
4620 legacy_mode |= (1 << 1);
4622 disable_dev_on_err = 0;
4623 printk(KERN_WARNING "ata: 0x170 IDE port busy\n");
4626 legacy_mode |= (1 << 1);
4629 /* we have legacy mode, but all ports are unavailable */
4630 if (legacy_mode == (1 << 3)) {
4632 goto err_out_regions;
4635 rc = pci_set_dma_mask(pdev, ATA_DMA_MASK);
4637 goto err_out_regions;
4638 rc = pci_set_consistent_dma_mask(pdev, ATA_DMA_MASK);
4640 goto err_out_regions;
4643 if (legacy_mode & (1 << 0))
4644 probe_ent = ata_pci_init_legacy_port(pdev, port[0], 0);
4645 if (legacy_mode & (1 << 1))
4646 probe_ent2 = ata_pci_init_legacy_port(pdev, port[1], 1);
4649 probe_ent = ata_pci_init_native_mode(pdev, port, ATA_PORT_PRIMARY | ATA_PORT_SECONDARY);
4651 probe_ent = ata_pci_init_native_mode(pdev, port, ATA_PORT_PRIMARY);
4653 if (!probe_ent && !probe_ent2) {
4655 goto err_out_regions;
4658 pci_set_master(pdev);
4660 /* FIXME: check ata_device_add return */
4662 if (legacy_mode & (1 << 0))
4663 ata_device_add(probe_ent);
4664 if (legacy_mode & (1 << 1))
4665 ata_device_add(probe_ent2);
4667 ata_device_add(probe_ent);
4675 if (legacy_mode & (1 << 0))
4676 release_region(0x1f0, 8);
4677 if (legacy_mode & (1 << 1))
4678 release_region(0x170, 8);
4679 pci_release_regions(pdev);
4681 if (disable_dev_on_err)
4682 pci_disable_device(pdev);
4687 * ata_pci_remove_one - PCI layer callback for device removal
4688 * @pdev: PCI device that was removed
4690 * PCI layer indicates to libata via this hook that
4691 * hot-unplug or module unload event has occurred.
4692 * Handle this by unregistering all objects associated
4693 * with this PCI device. Free those objects. Then finally
4694 * release PCI resources and disable device.
4697 * Inherited from PCI layer (may sleep).
4700 void ata_pci_remove_one (struct pci_dev *pdev)
4702 struct device *dev = pci_dev_to_dev(pdev);
4703 struct ata_host_set *host_set = dev_get_drvdata(dev);
4705 ata_host_set_remove(host_set);
4706 pci_release_regions(pdev);
4707 pci_disable_device(pdev);
4708 dev_set_drvdata(dev, NULL);
4711 /* move to PCI subsystem */
4712 int pci_test_config_bits(struct pci_dev *pdev, const struct pci_bits *bits)
4714 unsigned long tmp = 0;
4716 switch (bits->width) {
4719 pci_read_config_byte(pdev, bits->reg, &tmp8);
4725 pci_read_config_word(pdev, bits->reg, &tmp16);
4731 pci_read_config_dword(pdev, bits->reg, &tmp32);
4742 return (tmp == bits->val) ? 1 : 0;
4744 #endif /* CONFIG_PCI */
4747 static int __init ata_init(void)
4749 ata_wq = create_workqueue("ata");
4753 printk(KERN_DEBUG "libata version " DRV_VERSION " loaded.\n");
4757 static void __exit ata_exit(void)
4759 destroy_workqueue(ata_wq);
4762 module_init(ata_init);
4763 module_exit(ata_exit);
4765 static unsigned long ratelimit_time;
4766 static spinlock_t ata_ratelimit_lock = SPIN_LOCK_UNLOCKED;
4768 int ata_ratelimit(void)
4771 unsigned long flags;
4773 spin_lock_irqsave(&ata_ratelimit_lock, flags);
4775 if (time_after(jiffies, ratelimit_time)) {
4777 ratelimit_time = jiffies + (HZ/5);
4781 spin_unlock_irqrestore(&ata_ratelimit_lock, flags);
4787 * libata is essentially a library of internal helper functions for
4788 * low-level ATA host controller drivers. As such, the API/ABI is
4789 * likely to change as new drivers are added and updated.
4790 * Do not depend on ABI/API stability.
4793 EXPORT_SYMBOL_GPL(ata_std_bios_param);
4794 EXPORT_SYMBOL_GPL(ata_std_ports);
4795 EXPORT_SYMBOL_GPL(ata_device_add);
4796 EXPORT_SYMBOL_GPL(ata_host_set_remove);
4797 EXPORT_SYMBOL_GPL(ata_sg_init);
4798 EXPORT_SYMBOL_GPL(ata_sg_init_one);
4799 EXPORT_SYMBOL_GPL(ata_qc_complete);
4800 EXPORT_SYMBOL_GPL(ata_qc_issue_prot);
4801 EXPORT_SYMBOL_GPL(ata_eng_timeout);
4802 EXPORT_SYMBOL_GPL(ata_tf_load);
4803 EXPORT_SYMBOL_GPL(ata_tf_read);
4804 EXPORT_SYMBOL_GPL(ata_noop_dev_select);
4805 EXPORT_SYMBOL_GPL(ata_std_dev_select);
4806 EXPORT_SYMBOL_GPL(ata_tf_to_fis);
4807 EXPORT_SYMBOL_GPL(ata_tf_from_fis);
4808 EXPORT_SYMBOL_GPL(ata_check_status);
4809 EXPORT_SYMBOL_GPL(ata_altstatus);
4810 EXPORT_SYMBOL_GPL(ata_exec_command);
4811 EXPORT_SYMBOL_GPL(ata_port_start);
4812 EXPORT_SYMBOL_GPL(ata_port_stop);
4813 EXPORT_SYMBOL_GPL(ata_host_stop);
4814 EXPORT_SYMBOL_GPL(ata_interrupt);
4815 EXPORT_SYMBOL_GPL(ata_qc_prep);
4816 EXPORT_SYMBOL_GPL(ata_bmdma_setup);
4817 EXPORT_SYMBOL_GPL(ata_bmdma_start);
4818 EXPORT_SYMBOL_GPL(ata_bmdma_irq_clear);
4819 EXPORT_SYMBOL_GPL(ata_bmdma_status);
4820 EXPORT_SYMBOL_GPL(ata_bmdma_stop);
4821 EXPORT_SYMBOL_GPL(ata_port_probe);
4822 EXPORT_SYMBOL_GPL(sata_phy_reset);
4823 EXPORT_SYMBOL_GPL(__sata_phy_reset);
4824 EXPORT_SYMBOL_GPL(ata_bus_reset);
4825 EXPORT_SYMBOL_GPL(ata_port_disable);
4826 EXPORT_SYMBOL_GPL(ata_ratelimit);
4827 EXPORT_SYMBOL_GPL(ata_scsi_ioctl);
4828 EXPORT_SYMBOL_GPL(ata_scsi_queuecmd);
4829 EXPORT_SYMBOL_GPL(ata_scsi_error);
4830 EXPORT_SYMBOL_GPL(ata_scsi_slave_config);
4831 EXPORT_SYMBOL_GPL(ata_scsi_release);
4832 EXPORT_SYMBOL_GPL(ata_host_intr);
4833 EXPORT_SYMBOL_GPL(ata_dev_classify);
4834 EXPORT_SYMBOL_GPL(ata_dev_id_string);
4835 EXPORT_SYMBOL_GPL(ata_dev_config);
4836 EXPORT_SYMBOL_GPL(ata_scsi_simulate);
4838 EXPORT_SYMBOL_GPL(ata_timing_compute);
4839 EXPORT_SYMBOL_GPL(ata_timing_merge);
4842 EXPORT_SYMBOL_GPL(pci_test_config_bits);
4843 EXPORT_SYMBOL_GPL(ata_pci_host_stop);
4844 EXPORT_SYMBOL_GPL(ata_pci_init_native_mode);
4845 EXPORT_SYMBOL_GPL(ata_pci_init_one);
4846 EXPORT_SYMBOL_GPL(ata_pci_remove_one);
4847 #endif /* CONFIG_PCI */