2 * libata-core.c - helper library for ATA
4 * Maintained by: Jeff Garzik <jgarzik@pobox.com>
5 * Please ALWAYS copy linux-ide@vger.kernel.org
8 * Copyright 2003-2004 Red Hat, Inc. All rights reserved.
9 * Copyright 2003-2004 Jeff Garzik
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2, or (at your option)
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; see the file COPYING. If not, write to
24 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
27 * libata documentation is available via 'make {ps|pdf}docs',
28 * as Documentation/DocBook/libata.*
30 * Hardware documentation available from http://www.t13.org/ and
31 * http://www.sata-io.org/
35 #include <linux/config.h>
36 #include <linux/kernel.h>
37 #include <linux/module.h>
38 #include <linux/pci.h>
39 #include <linux/init.h>
40 #include <linux/list.h>
42 #include <linux/highmem.h>
43 #include <linux/spinlock.h>
44 #include <linux/blkdev.h>
45 #include <linux/delay.h>
46 #include <linux/timer.h>
47 #include <linux/interrupt.h>
48 #include <linux/completion.h>
49 #include <linux/suspend.h>
50 #include <linux/workqueue.h>
51 #include <linux/jiffies.h>
52 #include <scsi/scsi.h>
54 #include "scsi_priv.h"
55 #include <scsi/scsi_host.h>
56 #include <linux/libata.h>
58 #include <asm/semaphore.h>
59 #include <asm/byteorder.h>
63 static unsigned int ata_busy_sleep (struct ata_port *ap,
64 unsigned long tmout_pat,
66 static void ata_dev_reread_id(struct ata_port *ap, struct ata_device *dev);
67 static void ata_dev_init_params(struct ata_port *ap, struct ata_device *dev);
68 static void ata_set_mode(struct ata_port *ap);
69 static void ata_dev_set_xfermode(struct ata_port *ap, struct ata_device *dev);
70 static unsigned int ata_get_mode_mask(const struct ata_port *ap, int shift);
71 static int fgb(u32 bitmap);
72 static int ata_choose_xfer_mode(const struct ata_port *ap,
74 unsigned int *xfer_shift_out);
75 static void __ata_qc_complete(struct ata_queued_cmd *qc);
77 static unsigned int ata_unique_id = 1;
78 static struct workqueue_struct *ata_wq;
80 int atapi_enabled = 0;
81 module_param(atapi_enabled, int, 0444);
82 MODULE_PARM_DESC(atapi_enabled, "Enable discovery of ATAPI devices (0=off, 1=on)");
84 MODULE_AUTHOR("Jeff Garzik");
85 MODULE_DESCRIPTION("Library module for ATA devices");
86 MODULE_LICENSE("GPL");
87 MODULE_VERSION(DRV_VERSION);
90 * ata_tf_load_pio - send taskfile registers to host controller
91 * @ap: Port to which output is sent
92 * @tf: ATA taskfile register set
94 * Outputs ATA taskfile to standard ATA host controller.
97 * Inherited from caller.
100 static void ata_tf_load_pio(struct ata_port *ap, const struct ata_taskfile *tf)
102 struct ata_ioports *ioaddr = &ap->ioaddr;
103 unsigned int is_addr = tf->flags & ATA_TFLAG_ISADDR;
105 if (tf->ctl != ap->last_ctl) {
106 outb(tf->ctl, ioaddr->ctl_addr);
107 ap->last_ctl = tf->ctl;
111 if (is_addr && (tf->flags & ATA_TFLAG_LBA48)) {
112 outb(tf->hob_feature, ioaddr->feature_addr);
113 outb(tf->hob_nsect, ioaddr->nsect_addr);
114 outb(tf->hob_lbal, ioaddr->lbal_addr);
115 outb(tf->hob_lbam, ioaddr->lbam_addr);
116 outb(tf->hob_lbah, ioaddr->lbah_addr);
117 VPRINTK("hob: feat 0x%X nsect 0x%X, lba 0x%X 0x%X 0x%X\n",
126 outb(tf->feature, ioaddr->feature_addr);
127 outb(tf->nsect, ioaddr->nsect_addr);
128 outb(tf->lbal, ioaddr->lbal_addr);
129 outb(tf->lbam, ioaddr->lbam_addr);
130 outb(tf->lbah, ioaddr->lbah_addr);
131 VPRINTK("feat 0x%X nsect 0x%X lba 0x%X 0x%X 0x%X\n",
139 if (tf->flags & ATA_TFLAG_DEVICE) {
140 outb(tf->device, ioaddr->device_addr);
141 VPRINTK("device 0x%X\n", tf->device);
148 * ata_tf_load_mmio - send taskfile registers to host controller
149 * @ap: Port to which output is sent
150 * @tf: ATA taskfile register set
152 * Outputs ATA taskfile to standard ATA host controller using MMIO.
155 * Inherited from caller.
158 static void ata_tf_load_mmio(struct ata_port *ap, const struct ata_taskfile *tf)
160 struct ata_ioports *ioaddr = &ap->ioaddr;
161 unsigned int is_addr = tf->flags & ATA_TFLAG_ISADDR;
163 if (tf->ctl != ap->last_ctl) {
164 writeb(tf->ctl, (void __iomem *) ap->ioaddr.ctl_addr);
165 ap->last_ctl = tf->ctl;
169 if (is_addr && (tf->flags & ATA_TFLAG_LBA48)) {
170 writeb(tf->hob_feature, (void __iomem *) ioaddr->feature_addr);
171 writeb(tf->hob_nsect, (void __iomem *) ioaddr->nsect_addr);
172 writeb(tf->hob_lbal, (void __iomem *) ioaddr->lbal_addr);
173 writeb(tf->hob_lbam, (void __iomem *) ioaddr->lbam_addr);
174 writeb(tf->hob_lbah, (void __iomem *) ioaddr->lbah_addr);
175 VPRINTK("hob: feat 0x%X nsect 0x%X, lba 0x%X 0x%X 0x%X\n",
184 writeb(tf->feature, (void __iomem *) ioaddr->feature_addr);
185 writeb(tf->nsect, (void __iomem *) ioaddr->nsect_addr);
186 writeb(tf->lbal, (void __iomem *) ioaddr->lbal_addr);
187 writeb(tf->lbam, (void __iomem *) ioaddr->lbam_addr);
188 writeb(tf->lbah, (void __iomem *) ioaddr->lbah_addr);
189 VPRINTK("feat 0x%X nsect 0x%X lba 0x%X 0x%X 0x%X\n",
197 if (tf->flags & ATA_TFLAG_DEVICE) {
198 writeb(tf->device, (void __iomem *) ioaddr->device_addr);
199 VPRINTK("device 0x%X\n", tf->device);
207 * ata_tf_load - send taskfile registers to host controller
208 * @ap: Port to which output is sent
209 * @tf: ATA taskfile register set
211 * Outputs ATA taskfile to standard ATA host controller using MMIO
212 * or PIO as indicated by the ATA_FLAG_MMIO flag.
213 * Writes the control, feature, nsect, lbal, lbam, and lbah registers.
214 * Optionally (ATA_TFLAG_LBA48) writes hob_feature, hob_nsect,
215 * hob_lbal, hob_lbam, and hob_lbah.
217 * This function waits for idle (!BUSY and !DRQ) after writing
218 * registers. If the control register has a new value, this
219 * function also waits for idle after writing control and before
220 * writing the remaining registers.
222 * May be used as the tf_load() entry in ata_port_operations.
225 * Inherited from caller.
227 void ata_tf_load(struct ata_port *ap, const struct ata_taskfile *tf)
229 if (ap->flags & ATA_FLAG_MMIO)
230 ata_tf_load_mmio(ap, tf);
232 ata_tf_load_pio(ap, tf);
236 * ata_exec_command_pio - issue ATA command to host controller
237 * @ap: port to which command is being issued
238 * @tf: ATA taskfile register set
240 * Issues PIO write to ATA command register, with proper
241 * synchronization with interrupt handler / other threads.
244 * spin_lock_irqsave(host_set lock)
247 static void ata_exec_command_pio(struct ata_port *ap, const struct ata_taskfile *tf)
249 DPRINTK("ata%u: cmd 0x%X\n", ap->id, tf->command);
251 outb(tf->command, ap->ioaddr.command_addr);
257 * ata_exec_command_mmio - issue ATA command to host controller
258 * @ap: port to which command is being issued
259 * @tf: ATA taskfile register set
261 * Issues MMIO write to ATA command register, with proper
262 * synchronization with interrupt handler / other threads.
265 * spin_lock_irqsave(host_set lock)
268 static void ata_exec_command_mmio(struct ata_port *ap, const struct ata_taskfile *tf)
270 DPRINTK("ata%u: cmd 0x%X\n", ap->id, tf->command);
272 writeb(tf->command, (void __iomem *) ap->ioaddr.command_addr);
278 * ata_exec_command - issue ATA command to host controller
279 * @ap: port to which command is being issued
280 * @tf: ATA taskfile register set
282 * Issues PIO/MMIO write to ATA command register, with proper
283 * synchronization with interrupt handler / other threads.
286 * spin_lock_irqsave(host_set lock)
288 void ata_exec_command(struct ata_port *ap, const struct ata_taskfile *tf)
290 if (ap->flags & ATA_FLAG_MMIO)
291 ata_exec_command_mmio(ap, tf);
293 ata_exec_command_pio(ap, tf);
297 * ata_exec - issue ATA command to host controller
298 * @ap: port to which command is being issued
299 * @tf: ATA taskfile register set
301 * Issues PIO/MMIO write to ATA command register, with proper
302 * synchronization with interrupt handler / other threads.
305 * Obtains host_set lock.
308 static inline void ata_exec(struct ata_port *ap, const struct ata_taskfile *tf)
312 DPRINTK("ata%u: cmd 0x%X\n", ap->id, tf->command);
313 spin_lock_irqsave(&ap->host_set->lock, flags);
314 ap->ops->exec_command(ap, tf);
315 spin_unlock_irqrestore(&ap->host_set->lock, flags);
319 * ata_tf_to_host - issue ATA taskfile to host controller
320 * @ap: port to which command is being issued
321 * @tf: ATA taskfile register set
323 * Issues ATA taskfile register set to ATA host controller,
324 * with proper synchronization with interrupt handler and
328 * Obtains host_set lock.
331 static void ata_tf_to_host(struct ata_port *ap, const struct ata_taskfile *tf)
333 ap->ops->tf_load(ap, tf);
339 * ata_tf_to_host_nolock - issue ATA taskfile to host controller
340 * @ap: port to which command is being issued
341 * @tf: ATA taskfile register set
343 * Issues ATA taskfile register set to ATA host controller,
344 * with proper synchronization with interrupt handler and
348 * spin_lock_irqsave(host_set lock)
351 void ata_tf_to_host_nolock(struct ata_port *ap, const struct ata_taskfile *tf)
353 ap->ops->tf_load(ap, tf);
354 ap->ops->exec_command(ap, tf);
358 * ata_tf_read_pio - input device's ATA taskfile shadow registers
359 * @ap: Port from which input is read
360 * @tf: ATA taskfile register set for storing input
362 * Reads ATA taskfile registers for currently-selected device
366 * Inherited from caller.
369 static void ata_tf_read_pio(struct ata_port *ap, struct ata_taskfile *tf)
371 struct ata_ioports *ioaddr = &ap->ioaddr;
373 tf->command = ata_check_status(ap);
374 tf->feature = inb(ioaddr->error_addr);
375 tf->nsect = inb(ioaddr->nsect_addr);
376 tf->lbal = inb(ioaddr->lbal_addr);
377 tf->lbam = inb(ioaddr->lbam_addr);
378 tf->lbah = inb(ioaddr->lbah_addr);
379 tf->device = inb(ioaddr->device_addr);
381 if (tf->flags & ATA_TFLAG_LBA48) {
382 outb(tf->ctl | ATA_HOB, ioaddr->ctl_addr);
383 tf->hob_feature = inb(ioaddr->error_addr);
384 tf->hob_nsect = inb(ioaddr->nsect_addr);
385 tf->hob_lbal = inb(ioaddr->lbal_addr);
386 tf->hob_lbam = inb(ioaddr->lbam_addr);
387 tf->hob_lbah = inb(ioaddr->lbah_addr);
392 * ata_tf_read_mmio - input device's ATA taskfile shadow registers
393 * @ap: Port from which input is read
394 * @tf: ATA taskfile register set for storing input
396 * Reads ATA taskfile registers for currently-selected device
400 * Inherited from caller.
403 static void ata_tf_read_mmio(struct ata_port *ap, struct ata_taskfile *tf)
405 struct ata_ioports *ioaddr = &ap->ioaddr;
407 tf->command = ata_check_status(ap);
408 tf->feature = readb((void __iomem *)ioaddr->error_addr);
409 tf->nsect = readb((void __iomem *)ioaddr->nsect_addr);
410 tf->lbal = readb((void __iomem *)ioaddr->lbal_addr);
411 tf->lbam = readb((void __iomem *)ioaddr->lbam_addr);
412 tf->lbah = readb((void __iomem *)ioaddr->lbah_addr);
413 tf->device = readb((void __iomem *)ioaddr->device_addr);
415 if (tf->flags & ATA_TFLAG_LBA48) {
416 writeb(tf->ctl | ATA_HOB, (void __iomem *) ap->ioaddr.ctl_addr);
417 tf->hob_feature = readb((void __iomem *)ioaddr->error_addr);
418 tf->hob_nsect = readb((void __iomem *)ioaddr->nsect_addr);
419 tf->hob_lbal = readb((void __iomem *)ioaddr->lbal_addr);
420 tf->hob_lbam = readb((void __iomem *)ioaddr->lbam_addr);
421 tf->hob_lbah = readb((void __iomem *)ioaddr->lbah_addr);
427 * ata_tf_read - input device's ATA taskfile shadow registers
428 * @ap: Port from which input is read
429 * @tf: ATA taskfile register set for storing input
431 * Reads ATA taskfile registers for currently-selected device
434 * Reads nsect, lbal, lbam, lbah, and device. If ATA_TFLAG_LBA48
435 * is set, also reads the hob registers.
437 * May be used as the tf_read() entry in ata_port_operations.
440 * Inherited from caller.
442 void ata_tf_read(struct ata_port *ap, struct ata_taskfile *tf)
444 if (ap->flags & ATA_FLAG_MMIO)
445 ata_tf_read_mmio(ap, tf);
447 ata_tf_read_pio(ap, tf);
451 * ata_check_status_pio - Read device status reg & clear interrupt
452 * @ap: port where the device is
454 * Reads ATA taskfile status register for currently-selected device
455 * and return its value. This also clears pending interrupts
459 * Inherited from caller.
461 static u8 ata_check_status_pio(struct ata_port *ap)
463 return inb(ap->ioaddr.status_addr);
467 * ata_check_status_mmio - Read device status reg & clear interrupt
468 * @ap: port where the device is
470 * Reads ATA taskfile status register for currently-selected device
471 * via MMIO and return its value. This also clears pending interrupts
475 * Inherited from caller.
477 static u8 ata_check_status_mmio(struct ata_port *ap)
479 return readb((void __iomem *) ap->ioaddr.status_addr);
484 * ata_check_status - Read device status reg & clear interrupt
485 * @ap: port where the device is
487 * Reads ATA taskfile status register for currently-selected device
488 * and return its value. This also clears pending interrupts
491 * May be used as the check_status() entry in ata_port_operations.
494 * Inherited from caller.
496 u8 ata_check_status(struct ata_port *ap)
498 if (ap->flags & ATA_FLAG_MMIO)
499 return ata_check_status_mmio(ap);
500 return ata_check_status_pio(ap);
505 * ata_altstatus - Read device alternate status reg
506 * @ap: port where the device is
508 * Reads ATA taskfile alternate status register for
509 * currently-selected device and return its value.
511 * Note: may NOT be used as the check_altstatus() entry in
512 * ata_port_operations.
515 * Inherited from caller.
517 u8 ata_altstatus(struct ata_port *ap)
519 if (ap->ops->check_altstatus)
520 return ap->ops->check_altstatus(ap);
522 if (ap->flags & ATA_FLAG_MMIO)
523 return readb((void __iomem *)ap->ioaddr.altstatus_addr);
524 return inb(ap->ioaddr.altstatus_addr);
529 * ata_tf_to_fis - Convert ATA taskfile to SATA FIS structure
530 * @tf: Taskfile to convert
531 * @fis: Buffer into which data will output
532 * @pmp: Port multiplier port
534 * Converts a standard ATA taskfile to a Serial ATA
535 * FIS structure (Register - Host to Device).
538 * Inherited from caller.
541 void ata_tf_to_fis(const struct ata_taskfile *tf, u8 *fis, u8 pmp)
543 fis[0] = 0x27; /* Register - Host to Device FIS */
544 fis[1] = (pmp & 0xf) | (1 << 7); /* Port multiplier number,
545 bit 7 indicates Command FIS */
546 fis[2] = tf->command;
547 fis[3] = tf->feature;
554 fis[8] = tf->hob_lbal;
555 fis[9] = tf->hob_lbam;
556 fis[10] = tf->hob_lbah;
557 fis[11] = tf->hob_feature;
560 fis[13] = tf->hob_nsect;
571 * ata_tf_from_fis - Convert SATA FIS to ATA taskfile
572 * @fis: Buffer from which data will be input
573 * @tf: Taskfile to output
575 * Converts a standard ATA taskfile to a Serial ATA
576 * FIS structure (Register - Host to Device).
579 * Inherited from caller.
582 void ata_tf_from_fis(const u8 *fis, struct ata_taskfile *tf)
584 tf->command = fis[2]; /* status */
585 tf->feature = fis[3]; /* error */
592 tf->hob_lbal = fis[8];
593 tf->hob_lbam = fis[9];
594 tf->hob_lbah = fis[10];
597 tf->hob_nsect = fis[13];
600 static const u8 ata_rw_cmds[] = {
604 ATA_CMD_READ_MULTI_EXT,
605 ATA_CMD_WRITE_MULTI_EXT,
609 ATA_CMD_PIO_READ_EXT,
610 ATA_CMD_PIO_WRITE_EXT,
619 * ata_rwcmd_protocol - set taskfile r/w commands and protocol
620 * @qc: command to examine and configure
622 * Examine the device configuration and tf->flags to calculate
623 * the proper read/write commands and protocol to use.
628 void ata_rwcmd_protocol(struct ata_queued_cmd *qc)
630 struct ata_taskfile *tf = &qc->tf;
631 struct ata_device *dev = qc->dev;
633 int index, lba48, write;
635 lba48 = (tf->flags & ATA_TFLAG_LBA48) ? 2 : 0;
636 write = (tf->flags & ATA_TFLAG_WRITE) ? 1 : 0;
638 if (dev->flags & ATA_DFLAG_PIO) {
639 tf->protocol = ATA_PROT_PIO;
640 index = dev->multi_count ? 0 : 4;
642 tf->protocol = ATA_PROT_DMA;
646 tf->command = ata_rw_cmds[index + lba48 + write];
649 static const char * xfer_mode_str[] = {
669 * ata_udma_string - convert UDMA bit offset to string
670 * @mask: mask of bits supported; only highest bit counts.
672 * Determine string which represents the highest speed
673 * (highest bit in @udma_mask).
679 * Constant C string representing highest speed listed in
680 * @udma_mask, or the constant C string "<n/a>".
683 static const char *ata_mode_string(unsigned int mask)
687 for (i = 7; i >= 0; i--)
690 for (i = ATA_SHIFT_MWDMA + 2; i >= ATA_SHIFT_MWDMA; i--)
693 for (i = ATA_SHIFT_PIO + 4; i >= ATA_SHIFT_PIO; i--)
700 return xfer_mode_str[i];
704 * ata_pio_devchk - PATA device presence detection
705 * @ap: ATA channel to examine
706 * @device: Device to examine (starting at zero)
708 * This technique was originally described in
709 * Hale Landis's ATADRVR (www.ata-atapi.com), and
710 * later found its way into the ATA/ATAPI spec.
712 * Write a pattern to the ATA shadow registers,
713 * and if a device is present, it will respond by
714 * correctly storing and echoing back the
715 * ATA shadow register contents.
721 static unsigned int ata_pio_devchk(struct ata_port *ap,
724 struct ata_ioports *ioaddr = &ap->ioaddr;
727 ap->ops->dev_select(ap, device);
729 outb(0x55, ioaddr->nsect_addr);
730 outb(0xaa, ioaddr->lbal_addr);
732 outb(0xaa, ioaddr->nsect_addr);
733 outb(0x55, ioaddr->lbal_addr);
735 outb(0x55, ioaddr->nsect_addr);
736 outb(0xaa, ioaddr->lbal_addr);
738 nsect = inb(ioaddr->nsect_addr);
739 lbal = inb(ioaddr->lbal_addr);
741 if ((nsect == 0x55) && (lbal == 0xaa))
742 return 1; /* we found a device */
744 return 0; /* nothing found */
748 * ata_mmio_devchk - PATA device presence detection
749 * @ap: ATA channel to examine
750 * @device: Device to examine (starting at zero)
752 * This technique was originally described in
753 * Hale Landis's ATADRVR (www.ata-atapi.com), and
754 * later found its way into the ATA/ATAPI spec.
756 * Write a pattern to the ATA shadow registers,
757 * and if a device is present, it will respond by
758 * correctly storing and echoing back the
759 * ATA shadow register contents.
765 static unsigned int ata_mmio_devchk(struct ata_port *ap,
768 struct ata_ioports *ioaddr = &ap->ioaddr;
771 ap->ops->dev_select(ap, device);
773 writeb(0x55, (void __iomem *) ioaddr->nsect_addr);
774 writeb(0xaa, (void __iomem *) ioaddr->lbal_addr);
776 writeb(0xaa, (void __iomem *) ioaddr->nsect_addr);
777 writeb(0x55, (void __iomem *) ioaddr->lbal_addr);
779 writeb(0x55, (void __iomem *) ioaddr->nsect_addr);
780 writeb(0xaa, (void __iomem *) ioaddr->lbal_addr);
782 nsect = readb((void __iomem *) ioaddr->nsect_addr);
783 lbal = readb((void __iomem *) ioaddr->lbal_addr);
785 if ((nsect == 0x55) && (lbal == 0xaa))
786 return 1; /* we found a device */
788 return 0; /* nothing found */
792 * ata_devchk - PATA device presence detection
793 * @ap: ATA channel to examine
794 * @device: Device to examine (starting at zero)
796 * Dispatch ATA device presence detection, depending
797 * on whether we are using PIO or MMIO to talk to the
798 * ATA shadow registers.
804 static unsigned int ata_devchk(struct ata_port *ap,
807 if (ap->flags & ATA_FLAG_MMIO)
808 return ata_mmio_devchk(ap, device);
809 return ata_pio_devchk(ap, device);
813 * ata_dev_classify - determine device type based on ATA-spec signature
814 * @tf: ATA taskfile register set for device to be identified
816 * Determine from taskfile register contents whether a device is
817 * ATA or ATAPI, as per "Signature and persistence" section
818 * of ATA/PI spec (volume 1, sect 5.14).
824 * Device type, %ATA_DEV_ATA, %ATA_DEV_ATAPI, or %ATA_DEV_UNKNOWN
825 * the event of failure.
828 unsigned int ata_dev_classify(const struct ata_taskfile *tf)
830 /* Apple's open source Darwin code hints that some devices only
831 * put a proper signature into the LBA mid/high registers,
832 * So, we only check those. It's sufficient for uniqueness.
835 if (((tf->lbam == 0) && (tf->lbah == 0)) ||
836 ((tf->lbam == 0x3c) && (tf->lbah == 0xc3))) {
837 DPRINTK("found ATA device by sig\n");
841 if (((tf->lbam == 0x14) && (tf->lbah == 0xeb)) ||
842 ((tf->lbam == 0x69) && (tf->lbah == 0x96))) {
843 DPRINTK("found ATAPI device by sig\n");
844 return ATA_DEV_ATAPI;
847 DPRINTK("unknown device\n");
848 return ATA_DEV_UNKNOWN;
852 * ata_dev_try_classify - Parse returned ATA device signature
853 * @ap: ATA channel to examine
854 * @device: Device to examine (starting at zero)
856 * After an event -- SRST, E.D.D., or SATA COMRESET -- occurs,
857 * an ATA/ATAPI-defined set of values is placed in the ATA
858 * shadow registers, indicating the results of device detection
861 * Select the ATA device, and read the values from the ATA shadow
862 * registers. Then parse according to the Error register value,
863 * and the spec-defined values examined by ata_dev_classify().
869 static u8 ata_dev_try_classify(struct ata_port *ap, unsigned int device)
871 struct ata_device *dev = &ap->device[device];
872 struct ata_taskfile tf;
876 ap->ops->dev_select(ap, device);
878 memset(&tf, 0, sizeof(tf));
880 ap->ops->tf_read(ap, &tf);
883 dev->class = ATA_DEV_NONE;
885 /* see if device passed diags */
888 else if ((device == 0) && (err == 0x81))
893 /* determine if device if ATA or ATAPI */
894 class = ata_dev_classify(&tf);
895 if (class == ATA_DEV_UNKNOWN)
897 if ((class == ATA_DEV_ATA) && (ata_chk_status(ap) == 0))
906 * ata_dev_id_string - Convert IDENTIFY DEVICE page into string
907 * @id: IDENTIFY DEVICE results we will examine
908 * @s: string into which data is output
909 * @ofs: offset into identify device page
910 * @len: length of string to return. must be an even number.
912 * The strings in the IDENTIFY DEVICE page are broken up into
913 * 16-bit chunks. Run through the string, and output each
914 * 8-bit chunk linearly, regardless of platform.
920 void ata_dev_id_string(const u16 *id, unsigned char *s,
921 unsigned int ofs, unsigned int len)
941 * ata_noop_dev_select - Select device 0/1 on ATA bus
942 * @ap: ATA channel to manipulate
943 * @device: ATA device (numbered from zero) to select
945 * This function performs no actual function.
947 * May be used as the dev_select() entry in ata_port_operations.
952 void ata_noop_dev_select (struct ata_port *ap, unsigned int device)
958 * ata_std_dev_select - Select device 0/1 on ATA bus
959 * @ap: ATA channel to manipulate
960 * @device: ATA device (numbered from zero) to select
962 * Use the method defined in the ATA specification to
963 * make either device 0, or device 1, active on the
964 * ATA channel. Works with both PIO and MMIO.
966 * May be used as the dev_select() entry in ata_port_operations.
972 void ata_std_dev_select (struct ata_port *ap, unsigned int device)
977 tmp = ATA_DEVICE_OBS;
979 tmp = ATA_DEVICE_OBS | ATA_DEV1;
981 if (ap->flags & ATA_FLAG_MMIO) {
982 writeb(tmp, (void __iomem *) ap->ioaddr.device_addr);
984 outb(tmp, ap->ioaddr.device_addr);
986 ata_pause(ap); /* needed; also flushes, for mmio */
990 * ata_dev_select - Select device 0/1 on ATA bus
991 * @ap: ATA channel to manipulate
992 * @device: ATA device (numbered from zero) to select
993 * @wait: non-zero to wait for Status register BSY bit to clear
994 * @can_sleep: non-zero if context allows sleeping
996 * Use the method defined in the ATA specification to
997 * make either device 0, or device 1, active on the
1000 * This is a high-level version of ata_std_dev_select(),
1001 * which additionally provides the services of inserting
1002 * the proper pauses and status polling, where needed.
1008 void ata_dev_select(struct ata_port *ap, unsigned int device,
1009 unsigned int wait, unsigned int can_sleep)
1011 VPRINTK("ENTER, ata%u: device %u, wait %u\n",
1012 ap->id, device, wait);
1017 ap->ops->dev_select(ap, device);
1020 if (can_sleep && ap->device[device].class == ATA_DEV_ATAPI)
1027 * ata_dump_id - IDENTIFY DEVICE info debugging output
1028 * @dev: Device whose IDENTIFY DEVICE page we will dump
1030 * Dump selected 16-bit words from a detected device's
1031 * IDENTIFY PAGE page.
1037 static inline void ata_dump_id(const struct ata_device *dev)
1039 DPRINTK("49==0x%04x "
1049 DPRINTK("80==0x%04x "
1059 DPRINTK("88==0x%04x "
1066 * Compute the PIO modes available for this device. This is not as
1067 * trivial as it seems if we must consider early devices correctly.
1069 * FIXME: pre IDE drive timing (do we care ?).
1072 static unsigned int ata_pio_modes(const struct ata_device *adev)
1076 /* Usual case. Word 53 indicates word 88 is valid */
1077 if (adev->id[ATA_ID_FIELD_VALID] & (1 << 2)) {
1078 modes = adev->id[ATA_ID_PIO_MODES] & 0x03;
1084 /* If word 88 isn't valid then Word 51 holds the PIO timing number
1085 for the maximum. Turn it into a mask and return it */
1086 modes = (2 << (adev->id[ATA_ID_OLD_PIO_MODES] & 0xFF)) - 1 ;
1091 * ata_dev_identify - obtain IDENTIFY x DEVICE page
1092 * @ap: port on which device we wish to probe resides
1093 * @device: device bus address, starting at zero
1095 * Following bus reset, we issue the IDENTIFY [PACKET] DEVICE
1096 * command, and read back the 512-byte device information page.
1097 * The device information page is fed to us via the standard
1098 * PIO-IN protocol, but we hand-code it here. (TODO: investigate
1099 * using standard PIO-IN paths)
1101 * After reading the device information page, we use several
1102 * bits of information from it to initialize data structures
1103 * that will be used during the lifetime of the ata_device.
1104 * Other data from the info page is used to disqualify certain
1105 * older ATA devices we do not wish to support.
1108 * Inherited from caller. Some functions called by this function
1109 * obtain the host_set lock.
1112 static void ata_dev_identify(struct ata_port *ap, unsigned int device)
1114 struct ata_device *dev = &ap->device[device];
1115 unsigned int major_version;
1117 unsigned long xfer_modes;
1118 unsigned int using_edd;
1119 DECLARE_COMPLETION(wait);
1120 struct ata_queued_cmd *qc;
1121 unsigned long flags;
1124 if (!ata_dev_present(dev)) {
1125 DPRINTK("ENTER/EXIT (host %u, dev %u) -- nodev\n",
1130 if (ap->flags & (ATA_FLAG_SRST | ATA_FLAG_SATA_RESET))
1135 DPRINTK("ENTER, host %u, dev %u\n", ap->id, device);
1137 assert (dev->class == ATA_DEV_ATA || dev->class == ATA_DEV_ATAPI ||
1138 dev->class == ATA_DEV_NONE);
1140 ata_dev_select(ap, device, 1, 1); /* select device 0/1 */
1142 qc = ata_qc_new_init(ap, dev);
1145 ata_sg_init_one(qc, dev->id, sizeof(dev->id));
1146 qc->dma_dir = DMA_FROM_DEVICE;
1147 qc->tf.protocol = ATA_PROT_PIO;
1151 if (dev->class == ATA_DEV_ATA) {
1152 qc->tf.command = ATA_CMD_ID_ATA;
1153 DPRINTK("do ATA identify\n");
1155 qc->tf.command = ATA_CMD_ID_ATAPI;
1156 DPRINTK("do ATAPI identify\n");
1159 qc->waiting = &wait;
1160 qc->complete_fn = ata_qc_complete_noop;
1162 spin_lock_irqsave(&ap->host_set->lock, flags);
1163 rc = ata_qc_issue(qc);
1164 spin_unlock_irqrestore(&ap->host_set->lock, flags);
1169 wait_for_completion(&wait);
1171 spin_lock_irqsave(&ap->host_set->lock, flags);
1172 ap->ops->tf_read(ap, &qc->tf);
1173 spin_unlock_irqrestore(&ap->host_set->lock, flags);
1175 if (qc->tf.command & ATA_ERR) {
1177 * arg! EDD works for all test cases, but seems to return
1178 * the ATA signature for some ATAPI devices. Until the
1179 * reason for this is found and fixed, we fix up the mess
1180 * here. If IDENTIFY DEVICE returns command aborted
1181 * (as ATAPI devices do), then we issue an
1182 * IDENTIFY PACKET DEVICE.
1184 * ATA software reset (SRST, the default) does not appear
1185 * to have this problem.
1187 if ((using_edd) && (qc->tf.command == ATA_CMD_ID_ATA)) {
1188 u8 err = qc->tf.feature;
1189 if (err & ATA_ABORTED) {
1190 dev->class = ATA_DEV_ATAPI;
1201 swap_buf_le16(dev->id, ATA_ID_WORDS);
1203 /* print device capabilities */
1204 printk(KERN_DEBUG "ata%u: dev %u cfg "
1205 "49:%04x 82:%04x 83:%04x 84:%04x 85:%04x 86:%04x 87:%04x 88:%04x\n",
1206 ap->id, device, dev->id[49],
1207 dev->id[82], dev->id[83], dev->id[84],
1208 dev->id[85], dev->id[86], dev->id[87],
1212 * common ATA, ATAPI feature tests
1215 /* we require DMA support (bits 8 of word 49) */
1216 if (!ata_id_has_dma(dev->id)) {
1217 printk(KERN_DEBUG "ata%u: no dma\n", ap->id);
1221 /* quick-n-dirty find max transfer mode; for printk only */
1222 xfer_modes = dev->id[ATA_ID_UDMA_MODES];
1224 xfer_modes = (dev->id[ATA_ID_MWDMA_MODES]) << ATA_SHIFT_MWDMA;
1226 xfer_modes = ata_pio_modes(dev);
1230 /* ATA-specific feature tests */
1231 if (dev->class == ATA_DEV_ATA) {
1232 if (!ata_id_is_ata(dev->id)) /* sanity check */
1235 /* get major version */
1236 tmp = dev->id[ATA_ID_MAJOR_VER];
1237 for (major_version = 14; major_version >= 1; major_version--)
1238 if (tmp & (1 << major_version))
1242 * The exact sequence expected by certain pre-ATA4 drives is:
1245 * INITIALIZE DEVICE PARAMETERS
1247 * Some drives were very specific about that exact sequence.
1249 if (major_version < 4 || (!ata_id_has_lba(dev->id))) {
1250 ata_dev_init_params(ap, dev);
1252 /* current CHS translation info (id[53-58]) might be
1253 * changed. reread the identify device info.
1255 ata_dev_reread_id(ap, dev);
1258 if (ata_id_has_lba(dev->id)) {
1259 dev->flags |= ATA_DFLAG_LBA;
1261 if (ata_id_has_lba48(dev->id)) {
1262 dev->flags |= ATA_DFLAG_LBA48;
1263 dev->n_sectors = ata_id_u64(dev->id, 100);
1265 dev->n_sectors = ata_id_u32(dev->id, 60);
1268 /* print device info to dmesg */
1269 printk(KERN_INFO "ata%u: dev %u ATA-%d, max %s, %Lu sectors:%s\n",
1272 ata_mode_string(xfer_modes),
1273 (unsigned long long)dev->n_sectors,
1274 dev->flags & ATA_DFLAG_LBA48 ? " LBA48" : " LBA");
1278 /* Default translation */
1279 dev->cylinders = dev->id[1];
1280 dev->heads = dev->id[3];
1281 dev->sectors = dev->id[6];
1282 dev->n_sectors = dev->cylinders * dev->heads * dev->sectors;
1284 if (ata_id_current_chs_valid(dev->id)) {
1285 /* Current CHS translation is valid. */
1286 dev->cylinders = dev->id[54];
1287 dev->heads = dev->id[55];
1288 dev->sectors = dev->id[56];
1290 dev->n_sectors = ata_id_u32(dev->id, 57);
1293 /* print device info to dmesg */
1294 printk(KERN_INFO "ata%u: dev %u ATA-%d, max %s, %Lu sectors: CHS %d/%d/%d\n",
1297 ata_mode_string(xfer_modes),
1298 (unsigned long long)dev->n_sectors,
1299 (int)dev->cylinders, (int)dev->heads, (int)dev->sectors);
1303 ap->host->max_cmd_len = 16;
1306 /* ATAPI-specific feature tests */
1308 if (ata_id_is_ata(dev->id)) /* sanity check */
1311 rc = atapi_cdb_len(dev->id);
1312 if ((rc < 12) || (rc > ATAPI_CDB_LEN)) {
1313 printk(KERN_WARNING "ata%u: unsupported CDB len\n", ap->id);
1316 ap->cdb_len = (unsigned int) rc;
1317 ap->host->max_cmd_len = (unsigned char) ap->cdb_len;
1319 /* print device info to dmesg */
1320 printk(KERN_INFO "ata%u: dev %u ATAPI, max %s\n",
1322 ata_mode_string(xfer_modes));
1325 DPRINTK("EXIT, drv_stat = 0x%x\n", ata_chk_status(ap));
1329 printk(KERN_WARNING "ata%u: dev %u not supported, ignoring\n",
1332 dev->class++; /* converts ATA_DEV_xxx into ATA_DEV_xxx_UNSUP */
1333 DPRINTK("EXIT, err\n");
1337 static inline u8 ata_dev_knobble(const struct ata_port *ap)
1339 return ((ap->cbl == ATA_CBL_SATA) && (!ata_id_is_sata(ap->device->id)));
1343 * ata_dev_config - Run device specific handlers and check for
1344 * SATA->PATA bridges
1351 void ata_dev_config(struct ata_port *ap, unsigned int i)
1353 /* limit bridge transfers to udma5, 200 sectors */
1354 if (ata_dev_knobble(ap)) {
1355 printk(KERN_INFO "ata%u(%u): applying bridge limits\n",
1356 ap->id, ap->device->devno);
1357 ap->udma_mask &= ATA_UDMA5;
1358 ap->host->max_sectors = ATA_MAX_SECTORS;
1359 ap->host->hostt->max_sectors = ATA_MAX_SECTORS;
1360 ap->device->flags |= ATA_DFLAG_LOCK_SECTORS;
1363 if (ap->ops->dev_config)
1364 ap->ops->dev_config(ap, &ap->device[i]);
1368 * ata_bus_probe - Reset and probe ATA bus
1371 * Master ATA bus probing function. Initiates a hardware-dependent
1372 * bus reset, then attempts to identify any devices found on
1376 * PCI/etc. bus probe sem.
1379 * Zero on success, non-zero on error.
1382 static int ata_bus_probe(struct ata_port *ap)
1384 unsigned int i, found = 0;
1386 ap->ops->phy_reset(ap);
1387 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1390 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1391 ata_dev_identify(ap, i);
1392 if (ata_dev_present(&ap->device[i])) {
1394 ata_dev_config(ap,i);
1398 if ((!found) || (ap->flags & ATA_FLAG_PORT_DISABLED))
1399 goto err_out_disable;
1402 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1403 goto err_out_disable;
1408 ap->ops->port_disable(ap);
1414 * ata_port_probe - Mark port as enabled
1415 * @ap: Port for which we indicate enablement
1417 * Modify @ap data structure such that the system
1418 * thinks that the entire port is enabled.
1420 * LOCKING: host_set lock, or some other form of
1424 void ata_port_probe(struct ata_port *ap)
1426 ap->flags &= ~ATA_FLAG_PORT_DISABLED;
1430 * __sata_phy_reset - Wake/reset a low-level SATA PHY
1431 * @ap: SATA port associated with target SATA PHY.
1433 * This function issues commands to standard SATA Sxxx
1434 * PHY registers, to wake up the phy (and device), and
1435 * clear any reset condition.
1438 * PCI/etc. bus probe sem.
1441 void __sata_phy_reset(struct ata_port *ap)
1444 unsigned long timeout = jiffies + (HZ * 5);
1446 if (ap->flags & ATA_FLAG_SATA_RESET) {
1447 /* issue phy wake/reset */
1448 scr_write_flush(ap, SCR_CONTROL, 0x301);
1449 /* Couldn't find anything in SATA I/II specs, but
1450 * AHCI-1.1 10.4.2 says at least 1 ms. */
1453 scr_write_flush(ap, SCR_CONTROL, 0x300); /* phy wake/clear reset */
1455 /* wait for phy to become ready, if necessary */
1458 sstatus = scr_read(ap, SCR_STATUS);
1459 if ((sstatus & 0xf) != 1)
1461 } while (time_before(jiffies, timeout));
1463 /* TODO: phy layer with polling, timeouts, etc. */
1464 if (sata_dev_present(ap))
1467 sstatus = scr_read(ap, SCR_STATUS);
1468 printk(KERN_INFO "ata%u: no device found (phy stat %08x)\n",
1470 ata_port_disable(ap);
1473 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1476 if (ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT)) {
1477 ata_port_disable(ap);
1481 ap->cbl = ATA_CBL_SATA;
1485 * sata_phy_reset - Reset SATA bus.
1486 * @ap: SATA port associated with target SATA PHY.
1488 * This function resets the SATA bus, and then probes
1489 * the bus for devices.
1492 * PCI/etc. bus probe sem.
1495 void sata_phy_reset(struct ata_port *ap)
1497 __sata_phy_reset(ap);
1498 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1504 * ata_port_disable - Disable port.
1505 * @ap: Port to be disabled.
1507 * Modify @ap data structure such that the system
1508 * thinks that the entire port is disabled, and should
1509 * never attempt to probe or communicate with devices
1512 * LOCKING: host_set lock, or some other form of
1516 void ata_port_disable(struct ata_port *ap)
1518 ap->device[0].class = ATA_DEV_NONE;
1519 ap->device[1].class = ATA_DEV_NONE;
1520 ap->flags |= ATA_FLAG_PORT_DISABLED;
1524 * This mode timing computation functionality is ported over from
1525 * drivers/ide/ide-timing.h and was originally written by Vojtech Pavlik
1528 * PIO 0-5, MWDMA 0-2 and UDMA 0-6 timings (in nanoseconds).
1529 * These were taken from ATA/ATAPI-6 standard, rev 0a, except
1530 * for PIO 5, which is a nonstandard extension and UDMA6, which
1531 * is currently supported only by Maxtor drives.
1534 static const struct ata_timing ata_timing[] = {
1536 { XFER_UDMA_6, 0, 0, 0, 0, 0, 0, 0, 15 },
1537 { XFER_UDMA_5, 0, 0, 0, 0, 0, 0, 0, 20 },
1538 { XFER_UDMA_4, 0, 0, 0, 0, 0, 0, 0, 30 },
1539 { XFER_UDMA_3, 0, 0, 0, 0, 0, 0, 0, 45 },
1541 { XFER_UDMA_2, 0, 0, 0, 0, 0, 0, 0, 60 },
1542 { XFER_UDMA_1, 0, 0, 0, 0, 0, 0, 0, 80 },
1543 { XFER_UDMA_0, 0, 0, 0, 0, 0, 0, 0, 120 },
1545 /* { XFER_UDMA_SLOW, 0, 0, 0, 0, 0, 0, 0, 150 }, */
1547 { XFER_MW_DMA_2, 25, 0, 0, 0, 70, 25, 120, 0 },
1548 { XFER_MW_DMA_1, 45, 0, 0, 0, 80, 50, 150, 0 },
1549 { XFER_MW_DMA_0, 60, 0, 0, 0, 215, 215, 480, 0 },
1551 { XFER_SW_DMA_2, 60, 0, 0, 0, 120, 120, 240, 0 },
1552 { XFER_SW_DMA_1, 90, 0, 0, 0, 240, 240, 480, 0 },
1553 { XFER_SW_DMA_0, 120, 0, 0, 0, 480, 480, 960, 0 },
1555 /* { XFER_PIO_5, 20, 50, 30, 100, 50, 30, 100, 0 }, */
1556 { XFER_PIO_4, 25, 70, 25, 120, 70, 25, 120, 0 },
1557 { XFER_PIO_3, 30, 80, 70, 180, 80, 70, 180, 0 },
1559 { XFER_PIO_2, 30, 290, 40, 330, 100, 90, 240, 0 },
1560 { XFER_PIO_1, 50, 290, 93, 383, 125, 100, 383, 0 },
1561 { XFER_PIO_0, 70, 290, 240, 600, 165, 150, 600, 0 },
1563 /* { XFER_PIO_SLOW, 120, 290, 240, 960, 290, 240, 960, 0 }, */
1568 #define ENOUGH(v,unit) (((v)-1)/(unit)+1)
1569 #define EZ(v,unit) ((v)?ENOUGH(v,unit):0)
1571 static void ata_timing_quantize(const struct ata_timing *t, struct ata_timing *q, int T, int UT)
1573 q->setup = EZ(t->setup * 1000, T);
1574 q->act8b = EZ(t->act8b * 1000, T);
1575 q->rec8b = EZ(t->rec8b * 1000, T);
1576 q->cyc8b = EZ(t->cyc8b * 1000, T);
1577 q->active = EZ(t->active * 1000, T);
1578 q->recover = EZ(t->recover * 1000, T);
1579 q->cycle = EZ(t->cycle * 1000, T);
1580 q->udma = EZ(t->udma * 1000, UT);
1583 void ata_timing_merge(const struct ata_timing *a, const struct ata_timing *b,
1584 struct ata_timing *m, unsigned int what)
1586 if (what & ATA_TIMING_SETUP ) m->setup = max(a->setup, b->setup);
1587 if (what & ATA_TIMING_ACT8B ) m->act8b = max(a->act8b, b->act8b);
1588 if (what & ATA_TIMING_REC8B ) m->rec8b = max(a->rec8b, b->rec8b);
1589 if (what & ATA_TIMING_CYC8B ) m->cyc8b = max(a->cyc8b, b->cyc8b);
1590 if (what & ATA_TIMING_ACTIVE ) m->active = max(a->active, b->active);
1591 if (what & ATA_TIMING_RECOVER) m->recover = max(a->recover, b->recover);
1592 if (what & ATA_TIMING_CYCLE ) m->cycle = max(a->cycle, b->cycle);
1593 if (what & ATA_TIMING_UDMA ) m->udma = max(a->udma, b->udma);
1596 static const struct ata_timing* ata_timing_find_mode(unsigned short speed)
1598 const struct ata_timing *t;
1600 for (t = ata_timing; t->mode != speed; t++)
1601 if (t->mode == 0xFF)
1606 int ata_timing_compute(struct ata_device *adev, unsigned short speed,
1607 struct ata_timing *t, int T, int UT)
1609 const struct ata_timing *s;
1610 struct ata_timing p;
1616 if (!(s = ata_timing_find_mode(speed)))
1620 * If the drive is an EIDE drive, it can tell us it needs extended
1621 * PIO/MW_DMA cycle timing.
1624 if (adev->id[ATA_ID_FIELD_VALID] & 2) { /* EIDE drive */
1625 memset(&p, 0, sizeof(p));
1626 if(speed >= XFER_PIO_0 && speed <= XFER_SW_DMA_0) {
1627 if (speed <= XFER_PIO_2) p.cycle = p.cyc8b = adev->id[ATA_ID_EIDE_PIO];
1628 else p.cycle = p.cyc8b = adev->id[ATA_ID_EIDE_PIO_IORDY];
1629 } else if(speed >= XFER_MW_DMA_0 && speed <= XFER_MW_DMA_2) {
1630 p.cycle = adev->id[ATA_ID_EIDE_DMA_MIN];
1632 ata_timing_merge(&p, t, t, ATA_TIMING_CYCLE | ATA_TIMING_CYC8B);
1636 * Convert the timing to bus clock counts.
1639 ata_timing_quantize(s, t, T, UT);
1642 * Even in DMA/UDMA modes we still use PIO access for IDENTIFY, S.M.A.R.T
1643 * and some other commands. We have to ensure that the DMA cycle timing is
1644 * slower/equal than the fastest PIO timing.
1647 if (speed > XFER_PIO_4) {
1648 ata_timing_compute(adev, adev->pio_mode, &p, T, UT);
1649 ata_timing_merge(&p, t, t, ATA_TIMING_ALL);
1653 * Lenghten active & recovery time so that cycle time is correct.
1656 if (t->act8b + t->rec8b < t->cyc8b) {
1657 t->act8b += (t->cyc8b - (t->act8b + t->rec8b)) / 2;
1658 t->rec8b = t->cyc8b - t->act8b;
1661 if (t->active + t->recover < t->cycle) {
1662 t->active += (t->cycle - (t->active + t->recover)) / 2;
1663 t->recover = t->cycle - t->active;
1669 static const struct {
1672 } xfer_mode_classes[] = {
1673 { ATA_SHIFT_UDMA, XFER_UDMA_0 },
1674 { ATA_SHIFT_MWDMA, XFER_MW_DMA_0 },
1675 { ATA_SHIFT_PIO, XFER_PIO_0 },
1678 static inline u8 base_from_shift(unsigned int shift)
1682 for (i = 0; i < ARRAY_SIZE(xfer_mode_classes); i++)
1683 if (xfer_mode_classes[i].shift == shift)
1684 return xfer_mode_classes[i].base;
1689 static void ata_dev_set_mode(struct ata_port *ap, struct ata_device *dev)
1694 if (!ata_dev_present(dev) || (ap->flags & ATA_FLAG_PORT_DISABLED))
1697 if (dev->xfer_shift == ATA_SHIFT_PIO)
1698 dev->flags |= ATA_DFLAG_PIO;
1700 ata_dev_set_xfermode(ap, dev);
1702 base = base_from_shift(dev->xfer_shift);
1703 ofs = dev->xfer_mode - base;
1704 idx = ofs + dev->xfer_shift;
1705 WARN_ON(idx >= ARRAY_SIZE(xfer_mode_str));
1707 DPRINTK("idx=%d xfer_shift=%u, xfer_mode=0x%x, base=0x%x, offset=%d\n",
1708 idx, dev->xfer_shift, (int)dev->xfer_mode, (int)base, ofs);
1710 printk(KERN_INFO "ata%u: dev %u configured for %s\n",
1711 ap->id, dev->devno, xfer_mode_str[idx]);
1714 static int ata_host_set_pio(struct ata_port *ap)
1720 mask = ata_get_mode_mask(ap, ATA_SHIFT_PIO);
1723 printk(KERN_WARNING "ata%u: no PIO support\n", ap->id);
1727 base = base_from_shift(ATA_SHIFT_PIO);
1728 xfer_mode = base + x;
1730 DPRINTK("base 0x%x xfer_mode 0x%x mask 0x%x x %d\n",
1731 (int)base, (int)xfer_mode, mask, x);
1733 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1734 struct ata_device *dev = &ap->device[i];
1735 if (ata_dev_present(dev)) {
1736 dev->pio_mode = xfer_mode;
1737 dev->xfer_mode = xfer_mode;
1738 dev->xfer_shift = ATA_SHIFT_PIO;
1739 if (ap->ops->set_piomode)
1740 ap->ops->set_piomode(ap, dev);
1747 static void ata_host_set_dma(struct ata_port *ap, u8 xfer_mode,
1748 unsigned int xfer_shift)
1752 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1753 struct ata_device *dev = &ap->device[i];
1754 if (ata_dev_present(dev)) {
1755 dev->dma_mode = xfer_mode;
1756 dev->xfer_mode = xfer_mode;
1757 dev->xfer_shift = xfer_shift;
1758 if (ap->ops->set_dmamode)
1759 ap->ops->set_dmamode(ap, dev);
1765 * ata_set_mode - Program timings and issue SET FEATURES - XFER
1766 * @ap: port on which timings will be programmed
1768 * Set ATA device disk transfer mode (PIO3, UDMA6, etc.).
1771 * PCI/etc. bus probe sem.
1774 static void ata_set_mode(struct ata_port *ap)
1776 unsigned int xfer_shift;
1780 /* step 1: always set host PIO timings */
1781 rc = ata_host_set_pio(ap);
1785 /* step 2: choose the best data xfer mode */
1786 xfer_mode = xfer_shift = 0;
1787 rc = ata_choose_xfer_mode(ap, &xfer_mode, &xfer_shift);
1791 /* step 3: if that xfer mode isn't PIO, set host DMA timings */
1792 if (xfer_shift != ATA_SHIFT_PIO)
1793 ata_host_set_dma(ap, xfer_mode, xfer_shift);
1795 /* step 4: update devices' xfer mode */
1796 ata_dev_set_mode(ap, &ap->device[0]);
1797 ata_dev_set_mode(ap, &ap->device[1]);
1799 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1802 if (ap->ops->post_set_mode)
1803 ap->ops->post_set_mode(ap);
1808 ata_port_disable(ap);
1812 * ata_busy_sleep - sleep until BSY clears, or timeout
1813 * @ap: port containing status register to be polled
1814 * @tmout_pat: impatience timeout
1815 * @tmout: overall timeout
1817 * Sleep until ATA Status register bit BSY clears,
1818 * or a timeout occurs.
1824 static unsigned int ata_busy_sleep (struct ata_port *ap,
1825 unsigned long tmout_pat,
1826 unsigned long tmout)
1828 unsigned long timer_start, timeout;
1831 status = ata_busy_wait(ap, ATA_BUSY, 300);
1832 timer_start = jiffies;
1833 timeout = timer_start + tmout_pat;
1834 while ((status & ATA_BUSY) && (time_before(jiffies, timeout))) {
1836 status = ata_busy_wait(ap, ATA_BUSY, 3);
1839 if (status & ATA_BUSY)
1840 printk(KERN_WARNING "ata%u is slow to respond, "
1841 "please be patient\n", ap->id);
1843 timeout = timer_start + tmout;
1844 while ((status & ATA_BUSY) && (time_before(jiffies, timeout))) {
1846 status = ata_chk_status(ap);
1849 if (status & ATA_BUSY) {
1850 printk(KERN_ERR "ata%u failed to respond (%lu secs)\n",
1851 ap->id, tmout / HZ);
1858 static void ata_bus_post_reset(struct ata_port *ap, unsigned int devmask)
1860 struct ata_ioports *ioaddr = &ap->ioaddr;
1861 unsigned int dev0 = devmask & (1 << 0);
1862 unsigned int dev1 = devmask & (1 << 1);
1863 unsigned long timeout;
1865 /* if device 0 was found in ata_devchk, wait for its
1869 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
1871 /* if device 1 was found in ata_devchk, wait for
1872 * register access, then wait for BSY to clear
1874 timeout = jiffies + ATA_TMOUT_BOOT;
1878 ap->ops->dev_select(ap, 1);
1879 if (ap->flags & ATA_FLAG_MMIO) {
1880 nsect = readb((void __iomem *) ioaddr->nsect_addr);
1881 lbal = readb((void __iomem *) ioaddr->lbal_addr);
1883 nsect = inb(ioaddr->nsect_addr);
1884 lbal = inb(ioaddr->lbal_addr);
1886 if ((nsect == 1) && (lbal == 1))
1888 if (time_after(jiffies, timeout)) {
1892 msleep(50); /* give drive a breather */
1895 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
1897 /* is all this really necessary? */
1898 ap->ops->dev_select(ap, 0);
1900 ap->ops->dev_select(ap, 1);
1902 ap->ops->dev_select(ap, 0);
1906 * ata_bus_edd - Issue EXECUTE DEVICE DIAGNOSTIC command.
1907 * @ap: Port to reset and probe
1909 * Use the EXECUTE DEVICE DIAGNOSTIC command to reset and
1910 * probe the bus. Not often used these days.
1913 * PCI/etc. bus probe sem.
1917 static unsigned int ata_bus_edd(struct ata_port *ap)
1919 struct ata_taskfile tf;
1921 /* set up execute-device-diag (bus reset) taskfile */
1922 /* also, take interrupts to a known state (disabled) */
1923 DPRINTK("execute-device-diag\n");
1924 ata_tf_init(ap, &tf, 0);
1926 tf.command = ATA_CMD_EDD;
1927 tf.protocol = ATA_PROT_NODATA;
1930 ata_tf_to_host(ap, &tf);
1932 /* spec says at least 2ms. but who knows with those
1933 * crazy ATAPI devices...
1937 return ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
1940 static unsigned int ata_bus_softreset(struct ata_port *ap,
1941 unsigned int devmask)
1943 struct ata_ioports *ioaddr = &ap->ioaddr;
1945 DPRINTK("ata%u: bus reset via SRST\n", ap->id);
1947 /* software reset. causes dev0 to be selected */
1948 if (ap->flags & ATA_FLAG_MMIO) {
1949 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
1950 udelay(20); /* FIXME: flush */
1951 writeb(ap->ctl | ATA_SRST, (void __iomem *) ioaddr->ctl_addr);
1952 udelay(20); /* FIXME: flush */
1953 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
1955 outb(ap->ctl, ioaddr->ctl_addr);
1957 outb(ap->ctl | ATA_SRST, ioaddr->ctl_addr);
1959 outb(ap->ctl, ioaddr->ctl_addr);
1962 /* spec mandates ">= 2ms" before checking status.
1963 * We wait 150ms, because that was the magic delay used for
1964 * ATAPI devices in Hale Landis's ATADRVR, for the period of time
1965 * between when the ATA command register is written, and then
1966 * status is checked. Because waiting for "a while" before
1967 * checking status is fine, post SRST, we perform this magic
1968 * delay here as well.
1972 ata_bus_post_reset(ap, devmask);
1978 * ata_bus_reset - reset host port and associated ATA channel
1979 * @ap: port to reset
1981 * This is typically the first time we actually start issuing
1982 * commands to the ATA channel. We wait for BSY to clear, then
1983 * issue EXECUTE DEVICE DIAGNOSTIC command, polling for its
1984 * result. Determine what devices, if any, are on the channel
1985 * by looking at the device 0/1 error register. Look at the signature
1986 * stored in each device's taskfile registers, to determine if
1987 * the device is ATA or ATAPI.
1990 * PCI/etc. bus probe sem.
1991 * Obtains host_set lock.
1994 * Sets ATA_FLAG_PORT_DISABLED if bus reset fails.
1997 void ata_bus_reset(struct ata_port *ap)
1999 struct ata_ioports *ioaddr = &ap->ioaddr;
2000 unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS;
2002 unsigned int dev0, dev1 = 0, rc = 0, devmask = 0;
2004 DPRINTK("ENTER, host %u, port %u\n", ap->id, ap->port_no);
2006 /* determine if device 0/1 are present */
2007 if (ap->flags & ATA_FLAG_SATA_RESET)
2010 dev0 = ata_devchk(ap, 0);
2012 dev1 = ata_devchk(ap, 1);
2016 devmask |= (1 << 0);
2018 devmask |= (1 << 1);
2020 /* select device 0 again */
2021 ap->ops->dev_select(ap, 0);
2023 /* issue bus reset */
2024 if (ap->flags & ATA_FLAG_SRST)
2025 rc = ata_bus_softreset(ap, devmask);
2026 else if ((ap->flags & ATA_FLAG_SATA_RESET) == 0) {
2027 /* set up device control */
2028 if (ap->flags & ATA_FLAG_MMIO)
2029 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
2031 outb(ap->ctl, ioaddr->ctl_addr);
2032 rc = ata_bus_edd(ap);
2039 * determine by signature whether we have ATA or ATAPI devices
2041 err = ata_dev_try_classify(ap, 0);
2042 if ((slave_possible) && (err != 0x81))
2043 ata_dev_try_classify(ap, 1);
2045 /* re-enable interrupts */
2046 if (ap->ioaddr.ctl_addr) /* FIXME: hack. create a hook instead */
2049 /* is double-select really necessary? */
2050 if (ap->device[1].class != ATA_DEV_NONE)
2051 ap->ops->dev_select(ap, 1);
2052 if (ap->device[0].class != ATA_DEV_NONE)
2053 ap->ops->dev_select(ap, 0);
2055 /* if no devices were detected, disable this port */
2056 if ((ap->device[0].class == ATA_DEV_NONE) &&
2057 (ap->device[1].class == ATA_DEV_NONE))
2060 if (ap->flags & (ATA_FLAG_SATA_RESET | ATA_FLAG_SRST)) {
2061 /* set up device control for ATA_FLAG_SATA_RESET */
2062 if (ap->flags & ATA_FLAG_MMIO)
2063 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
2065 outb(ap->ctl, ioaddr->ctl_addr);
2072 printk(KERN_ERR "ata%u: disabling port\n", ap->id);
2073 ap->ops->port_disable(ap);
2078 static void ata_pr_blacklisted(const struct ata_port *ap,
2079 const struct ata_device *dev)
2081 printk(KERN_WARNING "ata%u: dev %u is on DMA blacklist, disabling DMA\n",
2082 ap->id, dev->devno);
2085 static const char * ata_dma_blacklist [] = {
2104 "Toshiba CD-ROM XM-6202B",
2105 "TOSHIBA CD-ROM XM-1702BC",
2107 "E-IDE CD-ROM CR-840",
2110 "SAMSUNG CD-ROM SC-148C",
2111 "SAMSUNG CD-ROM SC",
2113 "ATAPI CD-ROM DRIVE 40X MAXIMUM",
2117 static int ata_dma_blacklisted(const struct ata_device *dev)
2119 unsigned char model_num[40];
2124 ata_dev_id_string(dev->id, model_num, ATA_ID_PROD_OFS,
2127 len = strnlen(s, sizeof(model_num));
2129 /* ATAPI specifies that empty space is blank-filled; remove blanks */
2130 while ((len > 0) && (s[len - 1] == ' ')) {
2135 for (i = 0; i < ARRAY_SIZE(ata_dma_blacklist); i++)
2136 if (!strncmp(ata_dma_blacklist[i], s, len))
2142 static unsigned int ata_get_mode_mask(const struct ata_port *ap, int shift)
2144 const struct ata_device *master, *slave;
2147 master = &ap->device[0];
2148 slave = &ap->device[1];
2150 assert (ata_dev_present(master) || ata_dev_present(slave));
2152 if (shift == ATA_SHIFT_UDMA) {
2153 mask = ap->udma_mask;
2154 if (ata_dev_present(master)) {
2155 mask &= (master->id[ATA_ID_UDMA_MODES] & 0xff);
2156 if (ata_dma_blacklisted(master)) {
2158 ata_pr_blacklisted(ap, master);
2161 if (ata_dev_present(slave)) {
2162 mask &= (slave->id[ATA_ID_UDMA_MODES] & 0xff);
2163 if (ata_dma_blacklisted(slave)) {
2165 ata_pr_blacklisted(ap, slave);
2169 else if (shift == ATA_SHIFT_MWDMA) {
2170 mask = ap->mwdma_mask;
2171 if (ata_dev_present(master)) {
2172 mask &= (master->id[ATA_ID_MWDMA_MODES] & 0x07);
2173 if (ata_dma_blacklisted(master)) {
2175 ata_pr_blacklisted(ap, master);
2178 if (ata_dev_present(slave)) {
2179 mask &= (slave->id[ATA_ID_MWDMA_MODES] & 0x07);
2180 if (ata_dma_blacklisted(slave)) {
2182 ata_pr_blacklisted(ap, slave);
2186 else if (shift == ATA_SHIFT_PIO) {
2187 mask = ap->pio_mask;
2188 if (ata_dev_present(master)) {
2189 /* spec doesn't return explicit support for
2190 * PIO0-2, so we fake it
2192 u16 tmp_mode = master->id[ATA_ID_PIO_MODES] & 0x03;
2197 if (ata_dev_present(slave)) {
2198 /* spec doesn't return explicit support for
2199 * PIO0-2, so we fake it
2201 u16 tmp_mode = slave->id[ATA_ID_PIO_MODES] & 0x03;
2208 mask = 0xffffffff; /* shut up compiler warning */
2215 /* find greatest bit */
2216 static int fgb(u32 bitmap)
2221 for (i = 0; i < 32; i++)
2222 if (bitmap & (1 << i))
2229 * ata_choose_xfer_mode - attempt to find best transfer mode
2230 * @ap: Port for which an xfer mode will be selected
2231 * @xfer_mode_out: (output) SET FEATURES - XFER MODE code
2232 * @xfer_shift_out: (output) bit shift that selects this mode
2234 * Based on host and device capabilities, determine the
2235 * maximum transfer mode that is amenable to all.
2238 * PCI/etc. bus probe sem.
2241 * Zero on success, negative on error.
2244 static int ata_choose_xfer_mode(const struct ata_port *ap,
2246 unsigned int *xfer_shift_out)
2248 unsigned int mask, shift;
2251 for (i = 0; i < ARRAY_SIZE(xfer_mode_classes); i++) {
2252 shift = xfer_mode_classes[i].shift;
2253 mask = ata_get_mode_mask(ap, shift);
2257 *xfer_mode_out = xfer_mode_classes[i].base + x;
2258 *xfer_shift_out = shift;
2267 * ata_dev_set_xfermode - Issue SET FEATURES - XFER MODE command
2268 * @ap: Port associated with device @dev
2269 * @dev: Device to which command will be sent
2271 * Issue SET FEATURES - XFER MODE command to device @dev
2275 * PCI/etc. bus probe sem.
2278 static void ata_dev_set_xfermode(struct ata_port *ap, struct ata_device *dev)
2280 DECLARE_COMPLETION(wait);
2281 struct ata_queued_cmd *qc;
2283 unsigned long flags;
2285 /* set up set-features taskfile */
2286 DPRINTK("set features - xfer mode\n");
2288 qc = ata_qc_new_init(ap, dev);
2291 qc->tf.command = ATA_CMD_SET_FEATURES;
2292 qc->tf.feature = SETFEATURES_XFER;
2293 qc->tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
2294 qc->tf.protocol = ATA_PROT_NODATA;
2295 qc->tf.nsect = dev->xfer_mode;
2297 qc->waiting = &wait;
2298 qc->complete_fn = ata_qc_complete_noop;
2300 spin_lock_irqsave(&ap->host_set->lock, flags);
2301 rc = ata_qc_issue(qc);
2302 spin_unlock_irqrestore(&ap->host_set->lock, flags);
2305 ata_port_disable(ap);
2307 wait_for_completion(&wait);
2313 * ata_dev_reread_id - Reread the device identify device info
2314 * @ap: port where the device is
2315 * @dev: device to reread the identify device info
2320 static void ata_dev_reread_id(struct ata_port *ap, struct ata_device *dev)
2322 DECLARE_COMPLETION(wait);
2323 struct ata_queued_cmd *qc;
2324 unsigned long flags;
2327 qc = ata_qc_new_init(ap, dev);
2330 ata_sg_init_one(qc, dev->id, sizeof(dev->id));
2331 qc->dma_dir = DMA_FROM_DEVICE;
2333 if (dev->class == ATA_DEV_ATA) {
2334 qc->tf.command = ATA_CMD_ID_ATA;
2335 DPRINTK("do ATA identify\n");
2337 qc->tf.command = ATA_CMD_ID_ATAPI;
2338 DPRINTK("do ATAPI identify\n");
2341 qc->tf.flags |= ATA_TFLAG_DEVICE;
2342 qc->tf.protocol = ATA_PROT_PIO;
2345 qc->waiting = &wait;
2346 qc->complete_fn = ata_qc_complete_noop;
2348 spin_lock_irqsave(&ap->host_set->lock, flags);
2349 rc = ata_qc_issue(qc);
2350 spin_unlock_irqrestore(&ap->host_set->lock, flags);
2355 wait_for_completion(&wait);
2357 swap_buf_le16(dev->id, ATA_ID_WORDS);
2365 ata_port_disable(ap);
2369 * ata_dev_init_params - Issue INIT DEV PARAMS command
2370 * @ap: Port associated with device @dev
2371 * @dev: Device to which command will be sent
2376 static void ata_dev_init_params(struct ata_port *ap, struct ata_device *dev)
2378 DECLARE_COMPLETION(wait);
2379 struct ata_queued_cmd *qc;
2381 unsigned long flags;
2382 u16 sectors = dev->id[6];
2383 u16 heads = dev->id[3];
2385 /* Number of sectors per track 1-255. Number of heads 1-16 */
2386 if (sectors < 1 || sectors > 255 || heads < 1 || heads > 16)
2389 /* set up init dev params taskfile */
2390 DPRINTK("init dev params \n");
2392 qc = ata_qc_new_init(ap, dev);
2395 qc->tf.command = ATA_CMD_INIT_DEV_PARAMS;
2396 qc->tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
2397 qc->tf.protocol = ATA_PROT_NODATA;
2398 qc->tf.nsect = sectors;
2399 qc->tf.device |= (heads - 1) & 0x0f; /* max head = num. of heads - 1 */
2401 qc->waiting = &wait;
2402 qc->complete_fn = ata_qc_complete_noop;
2404 spin_lock_irqsave(&ap->host_set->lock, flags);
2405 rc = ata_qc_issue(qc);
2406 spin_unlock_irqrestore(&ap->host_set->lock, flags);
2409 ata_port_disable(ap);
2411 wait_for_completion(&wait);
2417 * ata_sg_clean - Unmap DMA memory associated with command
2418 * @qc: Command containing DMA memory to be released
2420 * Unmap all mapped DMA memory associated with this command.
2423 * spin_lock_irqsave(host_set lock)
2426 static void ata_sg_clean(struct ata_queued_cmd *qc)
2428 struct ata_port *ap = qc->ap;
2429 struct scatterlist *sg = qc->sg;
2430 int dir = qc->dma_dir;
2432 assert(qc->flags & ATA_QCFLAG_DMAMAP);
2435 if (qc->flags & ATA_QCFLAG_SINGLE)
2436 assert(qc->n_elem == 1);
2438 DPRINTK("unmapping %u sg elements\n", qc->n_elem);
2440 if (qc->flags & ATA_QCFLAG_SG)
2441 dma_unmap_sg(ap->host_set->dev, sg, qc->n_elem, dir);
2443 dma_unmap_single(ap->host_set->dev, sg_dma_address(&sg[0]),
2444 sg_dma_len(&sg[0]), dir);
2446 qc->flags &= ~ATA_QCFLAG_DMAMAP;
2451 * ata_fill_sg - Fill PCI IDE PRD table
2452 * @qc: Metadata associated with taskfile to be transferred
2454 * Fill PCI IDE PRD (scatter-gather) table with segments
2455 * associated with the current disk command.
2458 * spin_lock_irqsave(host_set lock)
2461 static void ata_fill_sg(struct ata_queued_cmd *qc)
2463 struct scatterlist *sg = qc->sg;
2464 struct ata_port *ap = qc->ap;
2465 unsigned int idx, nelem;
2468 assert(qc->n_elem > 0);
2471 for (nelem = qc->n_elem; nelem; nelem--,sg++) {
2475 /* determine if physical DMA addr spans 64K boundary.
2476 * Note h/w doesn't support 64-bit, so we unconditionally
2477 * truncate dma_addr_t to u32.
2479 addr = (u32) sg_dma_address(sg);
2480 sg_len = sg_dma_len(sg);
2483 offset = addr & 0xffff;
2485 if ((offset + sg_len) > 0x10000)
2486 len = 0x10000 - offset;
2488 ap->prd[idx].addr = cpu_to_le32(addr);
2489 ap->prd[idx].flags_len = cpu_to_le32(len & 0xffff);
2490 VPRINTK("PRD[%u] = (0x%X, 0x%X)\n", idx, addr, len);
2499 ap->prd[idx - 1].flags_len |= cpu_to_le32(ATA_PRD_EOT);
2502 * ata_check_atapi_dma - Check whether ATAPI DMA can be supported
2503 * @qc: Metadata associated with taskfile to check
2505 * Allow low-level driver to filter ATA PACKET commands, returning
2506 * a status indicating whether or not it is OK to use DMA for the
2507 * supplied PACKET command.
2510 * spin_lock_irqsave(host_set lock)
2512 * RETURNS: 0 when ATAPI DMA can be used
2515 int ata_check_atapi_dma(struct ata_queued_cmd *qc)
2517 struct ata_port *ap = qc->ap;
2518 int rc = 0; /* Assume ATAPI DMA is OK by default */
2520 if (ap->ops->check_atapi_dma)
2521 rc = ap->ops->check_atapi_dma(qc);
2526 * ata_qc_prep - Prepare taskfile for submission
2527 * @qc: Metadata associated with taskfile to be prepared
2529 * Prepare ATA taskfile for submission.
2532 * spin_lock_irqsave(host_set lock)
2534 void ata_qc_prep(struct ata_queued_cmd *qc)
2536 if (!(qc->flags & ATA_QCFLAG_DMAMAP))
2543 * ata_sg_init_one - Associate command with memory buffer
2544 * @qc: Command to be associated
2545 * @buf: Memory buffer
2546 * @buflen: Length of memory buffer, in bytes.
2548 * Initialize the data-related elements of queued_cmd @qc
2549 * to point to a single memory buffer, @buf of byte length @buflen.
2552 * spin_lock_irqsave(host_set lock)
2555 void ata_sg_init_one(struct ata_queued_cmd *qc, void *buf, unsigned int buflen)
2557 struct scatterlist *sg;
2559 qc->flags |= ATA_QCFLAG_SINGLE;
2561 memset(&qc->sgent, 0, sizeof(qc->sgent));
2562 qc->sg = &qc->sgent;
2567 sg->page = virt_to_page(buf);
2568 sg->offset = (unsigned long) buf & ~PAGE_MASK;
2569 sg->length = buflen;
2573 * ata_sg_init - Associate command with scatter-gather table.
2574 * @qc: Command to be associated
2575 * @sg: Scatter-gather table.
2576 * @n_elem: Number of elements in s/g table.
2578 * Initialize the data-related elements of queued_cmd @qc
2579 * to point to a scatter-gather table @sg, containing @n_elem
2583 * spin_lock_irqsave(host_set lock)
2586 void ata_sg_init(struct ata_queued_cmd *qc, struct scatterlist *sg,
2587 unsigned int n_elem)
2589 qc->flags |= ATA_QCFLAG_SG;
2591 qc->n_elem = n_elem;
2595 * ata_sg_setup_one - DMA-map the memory buffer associated with a command.
2596 * @qc: Command with memory buffer to be mapped.
2598 * DMA-map the memory buffer associated with queued_cmd @qc.
2601 * spin_lock_irqsave(host_set lock)
2604 * Zero on success, negative on error.
2607 static int ata_sg_setup_one(struct ata_queued_cmd *qc)
2609 struct ata_port *ap = qc->ap;
2610 int dir = qc->dma_dir;
2611 struct scatterlist *sg = qc->sg;
2612 dma_addr_t dma_address;
2614 dma_address = dma_map_single(ap->host_set->dev, qc->buf_virt,
2616 if (dma_mapping_error(dma_address))
2619 sg_dma_address(sg) = dma_address;
2620 sg_dma_len(sg) = sg->length;
2622 DPRINTK("mapped buffer of %d bytes for %s\n", sg_dma_len(sg),
2623 qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
2629 * ata_sg_setup - DMA-map the scatter-gather table associated with a command.
2630 * @qc: Command with scatter-gather table to be mapped.
2632 * DMA-map the scatter-gather table associated with queued_cmd @qc.
2635 * spin_lock_irqsave(host_set lock)
2638 * Zero on success, negative on error.
2642 static int ata_sg_setup(struct ata_queued_cmd *qc)
2644 struct ata_port *ap = qc->ap;
2645 struct scatterlist *sg = qc->sg;
2648 VPRINTK("ENTER, ata%u\n", ap->id);
2649 assert(qc->flags & ATA_QCFLAG_SG);
2652 n_elem = dma_map_sg(ap->host_set->dev, sg, qc->n_elem, dir);
2656 DPRINTK("%d sg elements mapped\n", n_elem);
2658 qc->n_elem = n_elem;
2664 * ata_poll_qc_complete - turn irq back on and finish qc
2665 * @qc: Command to complete
2666 * @drv_stat: ATA status register content
2669 * None. (grabs host lock)
2672 void ata_poll_qc_complete(struct ata_queued_cmd *qc, u8 drv_stat)
2674 struct ata_port *ap = qc->ap;
2675 unsigned long flags;
2677 spin_lock_irqsave(&ap->host_set->lock, flags);
2678 ap->flags &= ~ATA_FLAG_NOINTR;
2680 ata_qc_complete(qc, drv_stat);
2681 spin_unlock_irqrestore(&ap->host_set->lock, flags);
2686 * @ap: the target ata_port
2689 * None. (executing in kernel thread context)
2692 * timeout value to use
2695 static unsigned long ata_pio_poll(struct ata_port *ap)
2698 unsigned int poll_state = HSM_ST_UNKNOWN;
2699 unsigned int reg_state = HSM_ST_UNKNOWN;
2700 const unsigned int tmout_state = HSM_ST_TMOUT;
2702 switch (ap->hsm_task_state) {
2705 poll_state = HSM_ST_POLL;
2709 case HSM_ST_LAST_POLL:
2710 poll_state = HSM_ST_LAST_POLL;
2711 reg_state = HSM_ST_LAST;
2718 status = ata_chk_status(ap);
2719 if (status & ATA_BUSY) {
2720 if (time_after(jiffies, ap->pio_task_timeout)) {
2721 ap->hsm_task_state = tmout_state;
2724 ap->hsm_task_state = poll_state;
2725 return ATA_SHORT_PAUSE;
2728 ap->hsm_task_state = reg_state;
2733 * ata_pio_complete - check if drive is busy or idle
2734 * @ap: the target ata_port
2737 * None. (executing in kernel thread context)
2740 * Non-zero if qc completed, zero otherwise.
2743 static int ata_pio_complete (struct ata_port *ap)
2745 struct ata_queued_cmd *qc;
2749 * This is purely heuristic. This is a fast path. Sometimes when
2750 * we enter, BSY will be cleared in a chk-status or two. If not,
2751 * the drive is probably seeking or something. Snooze for a couple
2752 * msecs, then chk-status again. If still busy, fall back to
2753 * HSM_ST_POLL state.
2755 drv_stat = ata_busy_wait(ap, ATA_BUSY | ATA_DRQ, 10);
2756 if (drv_stat & (ATA_BUSY | ATA_DRQ)) {
2758 drv_stat = ata_busy_wait(ap, ATA_BUSY | ATA_DRQ, 10);
2759 if (drv_stat & (ATA_BUSY | ATA_DRQ)) {
2760 ap->hsm_task_state = HSM_ST_LAST_POLL;
2761 ap->pio_task_timeout = jiffies + ATA_TMOUT_PIO;
2766 drv_stat = ata_wait_idle(ap);
2767 if (!ata_ok(drv_stat)) {
2768 ap->hsm_task_state = HSM_ST_ERR;
2772 qc = ata_qc_from_tag(ap, ap->active_tag);
2775 ap->hsm_task_state = HSM_ST_IDLE;
2777 ata_poll_qc_complete(qc, drv_stat);
2779 /* another command may start at this point */
2786 * swap_buf_le16 - swap halves of 16-words in place
2787 * @buf: Buffer to swap
2788 * @buf_words: Number of 16-bit words in buffer.
2790 * Swap halves of 16-bit words if needed to convert from
2791 * little-endian byte order to native cpu byte order, or
2795 * Inherited from caller.
2797 void swap_buf_le16(u16 *buf, unsigned int buf_words)
2802 for (i = 0; i < buf_words; i++)
2803 buf[i] = le16_to_cpu(buf[i]);
2804 #endif /* __BIG_ENDIAN */
2808 * ata_mmio_data_xfer - Transfer data by MMIO
2809 * @ap: port to read/write
2811 * @buflen: buffer length
2812 * @write_data: read/write
2814 * Transfer data from/to the device data register by MMIO.
2817 * Inherited from caller.
2820 static void ata_mmio_data_xfer(struct ata_port *ap, unsigned char *buf,
2821 unsigned int buflen, int write_data)
2824 unsigned int words = buflen >> 1;
2825 u16 *buf16 = (u16 *) buf;
2826 void __iomem *mmio = (void __iomem *)ap->ioaddr.data_addr;
2828 /* Transfer multiple of 2 bytes */
2830 for (i = 0; i < words; i++)
2831 writew(le16_to_cpu(buf16[i]), mmio);
2833 for (i = 0; i < words; i++)
2834 buf16[i] = cpu_to_le16(readw(mmio));
2837 /* Transfer trailing 1 byte, if any. */
2838 if (unlikely(buflen & 0x01)) {
2839 u16 align_buf[1] = { 0 };
2840 unsigned char *trailing_buf = buf + buflen - 1;
2843 memcpy(align_buf, trailing_buf, 1);
2844 writew(le16_to_cpu(align_buf[0]), mmio);
2846 align_buf[0] = cpu_to_le16(readw(mmio));
2847 memcpy(trailing_buf, align_buf, 1);
2853 * ata_pio_data_xfer - Transfer data by PIO
2854 * @ap: port to read/write
2856 * @buflen: buffer length
2857 * @write_data: read/write
2859 * Transfer data from/to the device data register by PIO.
2862 * Inherited from caller.
2865 static void ata_pio_data_xfer(struct ata_port *ap, unsigned char *buf,
2866 unsigned int buflen, int write_data)
2868 unsigned int words = buflen >> 1;
2870 /* Transfer multiple of 2 bytes */
2872 outsw(ap->ioaddr.data_addr, buf, words);
2874 insw(ap->ioaddr.data_addr, buf, words);
2876 /* Transfer trailing 1 byte, if any. */
2877 if (unlikely(buflen & 0x01)) {
2878 u16 align_buf[1] = { 0 };
2879 unsigned char *trailing_buf = buf + buflen - 1;
2882 memcpy(align_buf, trailing_buf, 1);
2883 outw(le16_to_cpu(align_buf[0]), ap->ioaddr.data_addr);
2885 align_buf[0] = cpu_to_le16(inw(ap->ioaddr.data_addr));
2886 memcpy(trailing_buf, align_buf, 1);
2892 * ata_data_xfer - Transfer data from/to the data register.
2893 * @ap: port to read/write
2895 * @buflen: buffer length
2896 * @do_write: read/write
2898 * Transfer data from/to the device data register.
2901 * Inherited from caller.
2904 static void ata_data_xfer(struct ata_port *ap, unsigned char *buf,
2905 unsigned int buflen, int do_write)
2907 if (ap->flags & ATA_FLAG_MMIO)
2908 ata_mmio_data_xfer(ap, buf, buflen, do_write);
2910 ata_pio_data_xfer(ap, buf, buflen, do_write);
2914 * ata_pio_sector - Transfer ATA_SECT_SIZE (512 bytes) of data.
2915 * @qc: Command on going
2917 * Transfer ATA_SECT_SIZE of data from/to the ATA device.
2920 * Inherited from caller.
2923 static void ata_pio_sector(struct ata_queued_cmd *qc)
2925 int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
2926 struct scatterlist *sg = qc->sg;
2927 struct ata_port *ap = qc->ap;
2929 unsigned int offset;
2932 if (qc->cursect == (qc->nsect - 1))
2933 ap->hsm_task_state = HSM_ST_LAST;
2935 page = sg[qc->cursg].page;
2936 offset = sg[qc->cursg].offset + qc->cursg_ofs * ATA_SECT_SIZE;
2938 /* get the current page and offset */
2939 page = nth_page(page, (offset >> PAGE_SHIFT));
2940 offset %= PAGE_SIZE;
2942 buf = kmap(page) + offset;
2947 if ((qc->cursg_ofs * ATA_SECT_SIZE) == (&sg[qc->cursg])->length) {
2952 DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
2954 /* do the actual data transfer */
2955 do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
2956 ata_data_xfer(ap, buf, ATA_SECT_SIZE, do_write);
2962 * __atapi_pio_bytes - Transfer data from/to the ATAPI device.
2963 * @qc: Command on going
2964 * @bytes: number of bytes
2966 * Transfer Transfer data from/to the ATAPI device.
2969 * Inherited from caller.
2973 static void __atapi_pio_bytes(struct ata_queued_cmd *qc, unsigned int bytes)
2975 int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
2976 struct scatterlist *sg = qc->sg;
2977 struct ata_port *ap = qc->ap;
2980 unsigned int offset, count;
2982 if (qc->curbytes + bytes >= qc->nbytes)
2983 ap->hsm_task_state = HSM_ST_LAST;
2986 if (unlikely(qc->cursg >= qc->n_elem)) {
2988 * The end of qc->sg is reached and the device expects
2989 * more data to transfer. In order not to overrun qc->sg
2990 * and fulfill length specified in the byte count register,
2991 * - for read case, discard trailing data from the device
2992 * - for write case, padding zero data to the device
2994 u16 pad_buf[1] = { 0 };
2995 unsigned int words = bytes >> 1;
2998 if (words) /* warning if bytes > 1 */
2999 printk(KERN_WARNING "ata%u: %u bytes trailing data\n",
3002 for (i = 0; i < words; i++)
3003 ata_data_xfer(ap, (unsigned char*)pad_buf, 2, do_write);
3005 ap->hsm_task_state = HSM_ST_LAST;
3009 sg = &qc->sg[qc->cursg];
3012 offset = sg->offset + qc->cursg_ofs;
3014 /* get the current page and offset */
3015 page = nth_page(page, (offset >> PAGE_SHIFT));
3016 offset %= PAGE_SIZE;
3018 /* don't overrun current sg */
3019 count = min(sg->length - qc->cursg_ofs, bytes);
3021 /* don't cross page boundaries */
3022 count = min(count, (unsigned int)PAGE_SIZE - offset);
3024 buf = kmap(page) + offset;
3027 qc->curbytes += count;
3028 qc->cursg_ofs += count;
3030 if (qc->cursg_ofs == sg->length) {
3035 DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
3037 /* do the actual data transfer */
3038 ata_data_xfer(ap, buf, count, do_write);
3047 * atapi_pio_bytes - Transfer data from/to the ATAPI device.
3048 * @qc: Command on going
3050 * Transfer Transfer data from/to the ATAPI device.
3053 * Inherited from caller.
3056 static void atapi_pio_bytes(struct ata_queued_cmd *qc)
3058 struct ata_port *ap = qc->ap;
3059 struct ata_device *dev = qc->dev;
3060 unsigned int ireason, bc_lo, bc_hi, bytes;
3061 int i_write, do_write = (qc->tf.flags & ATA_TFLAG_WRITE) ? 1 : 0;
3063 ap->ops->tf_read(ap, &qc->tf);
3064 ireason = qc->tf.nsect;
3065 bc_lo = qc->tf.lbam;
3066 bc_hi = qc->tf.lbah;
3067 bytes = (bc_hi << 8) | bc_lo;
3069 /* shall be cleared to zero, indicating xfer of data */
3070 if (ireason & (1 << 0))
3073 /* make sure transfer direction matches expected */
3074 i_write = ((ireason & (1 << 1)) == 0) ? 1 : 0;
3075 if (do_write != i_write)
3078 __atapi_pio_bytes(qc, bytes);
3083 printk(KERN_INFO "ata%u: dev %u: ATAPI check failed\n",
3084 ap->id, dev->devno);
3085 ap->hsm_task_state = HSM_ST_ERR;
3089 * ata_pio_block - start PIO on a block
3090 * @ap: the target ata_port
3093 * None. (executing in kernel thread context)
3096 static void ata_pio_block(struct ata_port *ap)
3098 struct ata_queued_cmd *qc;
3102 * This is purely heuristic. This is a fast path.
3103 * Sometimes when we enter, BSY will be cleared in
3104 * a chk-status or two. If not, the drive is probably seeking
3105 * or something. Snooze for a couple msecs, then
3106 * chk-status again. If still busy, fall back to
3107 * HSM_ST_POLL state.
3109 status = ata_busy_wait(ap, ATA_BUSY, 5);
3110 if (status & ATA_BUSY) {
3112 status = ata_busy_wait(ap, ATA_BUSY, 10);
3113 if (status & ATA_BUSY) {
3114 ap->hsm_task_state = HSM_ST_POLL;
3115 ap->pio_task_timeout = jiffies + ATA_TMOUT_PIO;
3120 qc = ata_qc_from_tag(ap, ap->active_tag);
3123 if (is_atapi_taskfile(&qc->tf)) {
3124 /* no more data to transfer or unsupported ATAPI command */
3125 if ((status & ATA_DRQ) == 0) {
3126 ap->hsm_task_state = HSM_ST_LAST;
3130 atapi_pio_bytes(qc);
3132 /* handle BSY=0, DRQ=0 as error */
3133 if ((status & ATA_DRQ) == 0) {
3134 ap->hsm_task_state = HSM_ST_ERR;
3142 static void ata_pio_error(struct ata_port *ap)
3144 struct ata_queued_cmd *qc;
3147 qc = ata_qc_from_tag(ap, ap->active_tag);
3150 drv_stat = ata_chk_status(ap);
3151 printk(KERN_WARNING "ata%u: PIO error, drv_stat 0x%x\n",
3154 ap->hsm_task_state = HSM_ST_IDLE;
3156 ata_poll_qc_complete(qc, drv_stat | ATA_ERR);
3159 static void ata_pio_task(void *_data)
3161 struct ata_port *ap = _data;
3162 unsigned long timeout;
3169 switch (ap->hsm_task_state) {
3178 qc_completed = ata_pio_complete(ap);
3182 case HSM_ST_LAST_POLL:
3183 timeout = ata_pio_poll(ap);
3193 queue_delayed_work(ata_wq, &ap->pio_task, timeout);
3194 else if (!qc_completed)
3199 * ata_qc_timeout - Handle timeout of queued command
3200 * @qc: Command that timed out
3202 * Some part of the kernel (currently, only the SCSI layer)
3203 * has noticed that the active command on port @ap has not
3204 * completed after a specified length of time. Handle this
3205 * condition by disabling DMA (if necessary) and completing
3206 * transactions, with error if necessary.
3208 * This also handles the case of the "lost interrupt", where
3209 * for some reason (possibly hardware bug, possibly driver bug)
3210 * an interrupt was not delivered to the driver, even though the
3211 * transaction completed successfully.
3214 * Inherited from SCSI layer (none, can sleep)
3217 static void ata_qc_timeout(struct ata_queued_cmd *qc)
3219 struct ata_port *ap = qc->ap;
3220 struct ata_host_set *host_set = ap->host_set;
3221 struct ata_device *dev = qc->dev;
3222 u8 host_stat = 0, drv_stat;
3223 unsigned long flags;
3227 /* FIXME: doesn't this conflict with timeout handling? */
3228 if (qc->dev->class == ATA_DEV_ATAPI && qc->scsicmd) {
3229 struct scsi_cmnd *cmd = qc->scsicmd;
3231 if (!(cmd->eh_eflags & SCSI_EH_CANCEL_CMD)) {
3233 /* finish completing original command */
3234 spin_lock_irqsave(&host_set->lock, flags);
3235 __ata_qc_complete(qc);
3236 spin_unlock_irqrestore(&host_set->lock, flags);
3238 atapi_request_sense(ap, dev, cmd);
3240 cmd->result = (CHECK_CONDITION << 1) | (DID_OK << 16);
3241 scsi_finish_command(cmd);
3247 spin_lock_irqsave(&host_set->lock, flags);
3249 /* hack alert! We cannot use the supplied completion
3250 * function from inside the ->eh_strategy_handler() thread.
3251 * libata is the only user of ->eh_strategy_handler() in
3252 * any kernel, so the default scsi_done() assumes it is
3253 * not being called from the SCSI EH.
3255 qc->scsidone = scsi_finish_command;
3257 switch (qc->tf.protocol) {
3260 case ATA_PROT_ATAPI_DMA:
3261 host_stat = ap->ops->bmdma_status(ap);
3263 /* before we do anything else, clear DMA-Start bit */
3264 ap->ops->bmdma_stop(qc);
3270 drv_stat = ata_chk_status(ap);
3272 /* ack bmdma irq events */
3273 ap->ops->irq_clear(ap);
3275 printk(KERN_ERR "ata%u: command 0x%x timeout, stat 0x%x host_stat 0x%x\n",
3276 ap->id, qc->tf.command, drv_stat, host_stat);
3278 /* complete taskfile transaction */
3279 ata_qc_complete(qc, drv_stat);
3283 spin_unlock_irqrestore(&host_set->lock, flags);
3290 * ata_eng_timeout - Handle timeout of queued command
3291 * @ap: Port on which timed-out command is active
3293 * Some part of the kernel (currently, only the SCSI layer)
3294 * has noticed that the active command on port @ap has not
3295 * completed after a specified length of time. Handle this
3296 * condition by disabling DMA (if necessary) and completing
3297 * transactions, with error if necessary.
3299 * This also handles the case of the "lost interrupt", where
3300 * for some reason (possibly hardware bug, possibly driver bug)
3301 * an interrupt was not delivered to the driver, even though the
3302 * transaction completed successfully.
3305 * Inherited from SCSI layer (none, can sleep)
3308 void ata_eng_timeout(struct ata_port *ap)
3310 struct ata_queued_cmd *qc;
3314 qc = ata_qc_from_tag(ap, ap->active_tag);
3318 printk(KERN_ERR "ata%u: BUG: timeout without command\n",
3328 * ata_qc_new - Request an available ATA command, for queueing
3329 * @ap: Port associated with device @dev
3330 * @dev: Device from whom we request an available command structure
3336 static struct ata_queued_cmd *ata_qc_new(struct ata_port *ap)
3338 struct ata_queued_cmd *qc = NULL;
3341 for (i = 0; i < ATA_MAX_QUEUE; i++)
3342 if (!test_and_set_bit(i, &ap->qactive)) {
3343 qc = ata_qc_from_tag(ap, i);
3354 * ata_qc_new_init - Request an available ATA command, and initialize it
3355 * @ap: Port associated with device @dev
3356 * @dev: Device from whom we request an available command structure
3362 struct ata_queued_cmd *ata_qc_new_init(struct ata_port *ap,
3363 struct ata_device *dev)
3365 struct ata_queued_cmd *qc;
3367 qc = ata_qc_new(ap);
3374 qc->cursect = qc->cursg = qc->cursg_ofs = 0;
3376 qc->nbytes = qc->curbytes = 0;
3378 ata_tf_init(ap, &qc->tf, dev->devno);
3384 int ata_qc_complete_noop(struct ata_queued_cmd *qc, u8 drv_stat)
3389 static void __ata_qc_complete(struct ata_queued_cmd *qc)
3391 struct ata_port *ap = qc->ap;
3392 unsigned int tag, do_clear = 0;
3396 if (likely(ata_tag_valid(tag))) {
3397 if (tag == ap->active_tag)
3398 ap->active_tag = ATA_TAG_POISON;
3399 qc->tag = ATA_TAG_POISON;
3404 struct completion *waiting = qc->waiting;
3409 if (likely(do_clear))
3410 clear_bit(tag, &ap->qactive);
3414 * ata_qc_free - free unused ata_queued_cmd
3415 * @qc: Command to complete
3417 * Designed to free unused ata_queued_cmd object
3418 * in case something prevents using it.
3421 * spin_lock_irqsave(host_set lock)
3423 void ata_qc_free(struct ata_queued_cmd *qc)
3425 assert(qc != NULL); /* ata_qc_from_tag _might_ return NULL */
3426 assert(qc->waiting == NULL); /* nothing should be waiting */
3428 __ata_qc_complete(qc);
3432 * ata_qc_complete - Complete an active ATA command
3433 * @qc: Command to complete
3434 * @drv_stat: ATA Status register contents
3436 * Indicate to the mid and upper layers that an ATA
3437 * command has completed, with either an ok or not-ok status.
3440 * spin_lock_irqsave(host_set lock)
3443 void ata_qc_complete(struct ata_queued_cmd *qc, u8 drv_stat)
3447 assert(qc != NULL); /* ata_qc_from_tag _might_ return NULL */
3448 assert(qc->flags & ATA_QCFLAG_ACTIVE);
3450 if (likely(qc->flags & ATA_QCFLAG_DMAMAP))
3453 /* atapi: mark qc as inactive to prevent the interrupt handler
3454 * from completing the command twice later, before the error handler
3455 * is called. (when rc != 0 and atapi request sense is needed)
3457 qc->flags &= ~ATA_QCFLAG_ACTIVE;
3459 /* call completion callback */
3460 rc = qc->complete_fn(qc, drv_stat);
3462 /* if callback indicates not to complete command (non-zero),
3463 * return immediately
3468 __ata_qc_complete(qc);
3473 static inline int ata_should_dma_map(struct ata_queued_cmd *qc)
3475 struct ata_port *ap = qc->ap;
3477 switch (qc->tf.protocol) {
3479 case ATA_PROT_ATAPI_DMA:
3482 case ATA_PROT_ATAPI:
3484 case ATA_PROT_PIO_MULT:
3485 if (ap->flags & ATA_FLAG_PIO_DMA)
3498 * ata_qc_issue - issue taskfile to device
3499 * @qc: command to issue to device
3501 * Prepare an ATA command to submission to device.
3502 * This includes mapping the data into a DMA-able
3503 * area, filling in the S/G table, and finally
3504 * writing the taskfile to hardware, starting the command.
3507 * spin_lock_irqsave(host_set lock)
3510 * Zero on success, negative on error.
3513 int ata_qc_issue(struct ata_queued_cmd *qc)
3515 struct ata_port *ap = qc->ap;
3517 if (ata_should_dma_map(qc)) {
3518 if (qc->flags & ATA_QCFLAG_SG) {
3519 if (ata_sg_setup(qc))
3521 } else if (qc->flags & ATA_QCFLAG_SINGLE) {
3522 if (ata_sg_setup_one(qc))
3526 qc->flags &= ~ATA_QCFLAG_DMAMAP;
3529 ap->ops->qc_prep(qc);
3531 qc->ap->active_tag = qc->tag;
3532 qc->flags |= ATA_QCFLAG_ACTIVE;
3534 return ap->ops->qc_issue(qc);
3542 * ata_qc_issue_prot - issue taskfile to device in proto-dependent manner
3543 * @qc: command to issue to device
3545 * Using various libata functions and hooks, this function
3546 * starts an ATA command. ATA commands are grouped into
3547 * classes called "protocols", and issuing each type of protocol
3548 * is slightly different.
3550 * May be used as the qc_issue() entry in ata_port_operations.
3553 * spin_lock_irqsave(host_set lock)
3556 * Zero on success, negative on error.
3559 int ata_qc_issue_prot(struct ata_queued_cmd *qc)
3561 struct ata_port *ap = qc->ap;
3563 ata_dev_select(ap, qc->dev->devno, 1, 0);
3565 switch (qc->tf.protocol) {
3566 case ATA_PROT_NODATA:
3567 ata_tf_to_host_nolock(ap, &qc->tf);
3571 ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
3572 ap->ops->bmdma_setup(qc); /* set up bmdma */
3573 ap->ops->bmdma_start(qc); /* initiate bmdma */
3576 case ATA_PROT_PIO: /* load tf registers, initiate polling pio */
3577 ata_qc_set_polling(qc);
3578 ata_tf_to_host_nolock(ap, &qc->tf);
3579 ap->hsm_task_state = HSM_ST;
3580 queue_work(ata_wq, &ap->pio_task);
3583 case ATA_PROT_ATAPI:
3584 ata_qc_set_polling(qc);
3585 ata_tf_to_host_nolock(ap, &qc->tf);
3586 queue_work(ata_wq, &ap->packet_task);
3589 case ATA_PROT_ATAPI_NODATA:
3590 ap->flags |= ATA_FLAG_NOINTR;
3591 ata_tf_to_host_nolock(ap, &qc->tf);
3592 queue_work(ata_wq, &ap->packet_task);
3595 case ATA_PROT_ATAPI_DMA:
3596 ap->flags |= ATA_FLAG_NOINTR;
3597 ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
3598 ap->ops->bmdma_setup(qc); /* set up bmdma */
3599 queue_work(ata_wq, &ap->packet_task);
3611 * ata_bmdma_setup_mmio - Set up PCI IDE BMDMA transaction
3612 * @qc: Info associated with this ATA transaction.
3615 * spin_lock_irqsave(host_set lock)
3618 static void ata_bmdma_setup_mmio (struct ata_queued_cmd *qc)
3620 struct ata_port *ap = qc->ap;
3621 unsigned int rw = (qc->tf.flags & ATA_TFLAG_WRITE);
3623 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
3625 /* load PRD table addr. */
3626 mb(); /* make sure PRD table writes are visible to controller */
3627 writel(ap->prd_dma, mmio + ATA_DMA_TABLE_OFS);
3629 /* specify data direction, triple-check start bit is clear */
3630 dmactl = readb(mmio + ATA_DMA_CMD);
3631 dmactl &= ~(ATA_DMA_WR | ATA_DMA_START);
3633 dmactl |= ATA_DMA_WR;
3634 writeb(dmactl, mmio + ATA_DMA_CMD);
3636 /* issue r/w command */
3637 ap->ops->exec_command(ap, &qc->tf);
3641 * ata_bmdma_start_mmio - Start a PCI IDE BMDMA transaction
3642 * @qc: Info associated with this ATA transaction.
3645 * spin_lock_irqsave(host_set lock)
3648 static void ata_bmdma_start_mmio (struct ata_queued_cmd *qc)
3650 struct ata_port *ap = qc->ap;
3651 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
3654 /* start host DMA transaction */
3655 dmactl = readb(mmio + ATA_DMA_CMD);
3656 writeb(dmactl | ATA_DMA_START, mmio + ATA_DMA_CMD);
3658 /* Strictly, one may wish to issue a readb() here, to
3659 * flush the mmio write. However, control also passes
3660 * to the hardware at this point, and it will interrupt
3661 * us when we are to resume control. So, in effect,
3662 * we don't care when the mmio write flushes.
3663 * Further, a read of the DMA status register _immediately_
3664 * following the write may not be what certain flaky hardware
3665 * is expected, so I think it is best to not add a readb()
3666 * without first all the MMIO ATA cards/mobos.
3667 * Or maybe I'm just being paranoid.
3672 * ata_bmdma_setup_pio - Set up PCI IDE BMDMA transaction (PIO)
3673 * @qc: Info associated with this ATA transaction.
3676 * spin_lock_irqsave(host_set lock)
3679 static void ata_bmdma_setup_pio (struct ata_queued_cmd *qc)
3681 struct ata_port *ap = qc->ap;
3682 unsigned int rw = (qc->tf.flags & ATA_TFLAG_WRITE);
3685 /* load PRD table addr. */
3686 outl(ap->prd_dma, ap->ioaddr.bmdma_addr + ATA_DMA_TABLE_OFS);
3688 /* specify data direction, triple-check start bit is clear */
3689 dmactl = inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3690 dmactl &= ~(ATA_DMA_WR | ATA_DMA_START);
3692 dmactl |= ATA_DMA_WR;
3693 outb(dmactl, ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3695 /* issue r/w command */
3696 ap->ops->exec_command(ap, &qc->tf);
3700 * ata_bmdma_start_pio - Start a PCI IDE BMDMA transaction (PIO)
3701 * @qc: Info associated with this ATA transaction.
3704 * spin_lock_irqsave(host_set lock)
3707 static void ata_bmdma_start_pio (struct ata_queued_cmd *qc)
3709 struct ata_port *ap = qc->ap;
3712 /* start host DMA transaction */
3713 dmactl = inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3714 outb(dmactl | ATA_DMA_START,
3715 ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3720 * ata_bmdma_start - Start a PCI IDE BMDMA transaction
3721 * @qc: Info associated with this ATA transaction.
3723 * Writes the ATA_DMA_START flag to the DMA command register.
3725 * May be used as the bmdma_start() entry in ata_port_operations.
3728 * spin_lock_irqsave(host_set lock)
3730 void ata_bmdma_start(struct ata_queued_cmd *qc)
3732 if (qc->ap->flags & ATA_FLAG_MMIO)
3733 ata_bmdma_start_mmio(qc);
3735 ata_bmdma_start_pio(qc);
3740 * ata_bmdma_setup - Set up PCI IDE BMDMA transaction
3741 * @qc: Info associated with this ATA transaction.
3743 * Writes address of PRD table to device's PRD Table Address
3744 * register, sets the DMA control register, and calls
3745 * ops->exec_command() to start the transfer.
3747 * May be used as the bmdma_setup() entry in ata_port_operations.
3750 * spin_lock_irqsave(host_set lock)
3752 void ata_bmdma_setup(struct ata_queued_cmd *qc)
3754 if (qc->ap->flags & ATA_FLAG_MMIO)
3755 ata_bmdma_setup_mmio(qc);
3757 ata_bmdma_setup_pio(qc);
3762 * ata_bmdma_irq_clear - Clear PCI IDE BMDMA interrupt.
3763 * @ap: Port associated with this ATA transaction.
3765 * Clear interrupt and error flags in DMA status register.
3767 * May be used as the irq_clear() entry in ata_port_operations.
3770 * spin_lock_irqsave(host_set lock)
3773 void ata_bmdma_irq_clear(struct ata_port *ap)
3775 if (ap->flags & ATA_FLAG_MMIO) {
3776 void __iomem *mmio = ((void __iomem *) ap->ioaddr.bmdma_addr) + ATA_DMA_STATUS;
3777 writeb(readb(mmio), mmio);
3779 unsigned long addr = ap->ioaddr.bmdma_addr + ATA_DMA_STATUS;
3780 outb(inb(addr), addr);
3787 * ata_bmdma_status - Read PCI IDE BMDMA status
3788 * @ap: Port associated with this ATA transaction.
3790 * Read and return BMDMA status register.
3792 * May be used as the bmdma_status() entry in ata_port_operations.
3795 * spin_lock_irqsave(host_set lock)
3798 u8 ata_bmdma_status(struct ata_port *ap)
3801 if (ap->flags & ATA_FLAG_MMIO) {
3802 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
3803 host_stat = readb(mmio + ATA_DMA_STATUS);
3805 host_stat = inb(ap->ioaddr.bmdma_addr + ATA_DMA_STATUS);
3811 * ata_bmdma_stop - Stop PCI IDE BMDMA transfer
3812 * @qc: Command we are ending DMA for
3814 * Clears the ATA_DMA_START flag in the dma control register
3816 * May be used as the bmdma_stop() entry in ata_port_operations.
3819 * spin_lock_irqsave(host_set lock)
3822 void ata_bmdma_stop(struct ata_queued_cmd *qc)
3824 struct ata_port *ap = qc->ap;
3825 if (ap->flags & ATA_FLAG_MMIO) {
3826 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
3828 /* clear start/stop bit */
3829 writeb(readb(mmio + ATA_DMA_CMD) & ~ATA_DMA_START,
3830 mmio + ATA_DMA_CMD);
3832 /* clear start/stop bit */
3833 outb(inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD) & ~ATA_DMA_START,
3834 ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3837 /* one-PIO-cycle guaranteed wait, per spec, for HDMA1:0 transition */
3838 ata_altstatus(ap); /* dummy read */
3842 * ata_host_intr - Handle host interrupt for given (port, task)
3843 * @ap: Port on which interrupt arrived (possibly...)
3844 * @qc: Taskfile currently active in engine
3846 * Handle host interrupt for given queued command. Currently,
3847 * only DMA interrupts are handled. All other commands are
3848 * handled via polling with interrupts disabled (nIEN bit).
3851 * spin_lock_irqsave(host_set lock)
3854 * One if interrupt was handled, zero if not (shared irq).
3857 inline unsigned int ata_host_intr (struct ata_port *ap,
3858 struct ata_queued_cmd *qc)
3860 u8 status, host_stat;
3862 switch (qc->tf.protocol) {
3865 case ATA_PROT_ATAPI_DMA:
3866 case ATA_PROT_ATAPI:
3867 /* check status of DMA engine */
3868 host_stat = ap->ops->bmdma_status(ap);
3869 VPRINTK("ata%u: host_stat 0x%X\n", ap->id, host_stat);
3871 /* if it's not our irq... */
3872 if (!(host_stat & ATA_DMA_INTR))
3875 /* before we do anything else, clear DMA-Start bit */
3876 ap->ops->bmdma_stop(qc);
3880 case ATA_PROT_ATAPI_NODATA:
3881 case ATA_PROT_NODATA:
3882 /* check altstatus */
3883 status = ata_altstatus(ap);
3884 if (status & ATA_BUSY)
3887 /* check main status, clearing INTRQ */
3888 status = ata_chk_status(ap);
3889 if (unlikely(status & ATA_BUSY))
3891 DPRINTK("ata%u: protocol %d (dev_stat 0x%X)\n",
3892 ap->id, qc->tf.protocol, status);
3894 /* ack bmdma irq events */
3895 ap->ops->irq_clear(ap);
3897 /* complete taskfile transaction */
3898 ata_qc_complete(qc, status);
3905 return 1; /* irq handled */
3908 ap->stats.idle_irq++;
3911 if ((ap->stats.idle_irq % 1000) == 0) {
3913 ata_irq_ack(ap, 0); /* debug trap */
3914 printk(KERN_WARNING "ata%d: irq trap\n", ap->id);
3917 return 0; /* irq not handled */
3921 * ata_interrupt - Default ATA host interrupt handler
3922 * @irq: irq line (unused)
3923 * @dev_instance: pointer to our ata_host_set information structure
3926 * Default interrupt handler for PCI IDE devices. Calls
3927 * ata_host_intr() for each port that is not disabled.
3930 * Obtains host_set lock during operation.
3933 * IRQ_NONE or IRQ_HANDLED.
3936 irqreturn_t ata_interrupt (int irq, void *dev_instance, struct pt_regs *regs)
3938 struct ata_host_set *host_set = dev_instance;
3940 unsigned int handled = 0;
3941 unsigned long flags;
3943 /* TODO: make _irqsave conditional on x86 PCI IDE legacy mode */
3944 spin_lock_irqsave(&host_set->lock, flags);
3946 for (i = 0; i < host_set->n_ports; i++) {
3947 struct ata_port *ap;
3949 ap = host_set->ports[i];
3951 !(ap->flags & (ATA_FLAG_PORT_DISABLED | ATA_FLAG_NOINTR))) {
3952 struct ata_queued_cmd *qc;
3954 qc = ata_qc_from_tag(ap, ap->active_tag);
3955 if (qc && (!(qc->tf.ctl & ATA_NIEN)) &&
3956 (qc->flags & ATA_QCFLAG_ACTIVE))
3957 handled |= ata_host_intr(ap, qc);
3961 spin_unlock_irqrestore(&host_set->lock, flags);
3963 return IRQ_RETVAL(handled);
3967 * atapi_packet_task - Write CDB bytes to hardware
3968 * @_data: Port to which ATAPI device is attached.
3970 * When device has indicated its readiness to accept
3971 * a CDB, this function is called. Send the CDB.
3972 * If DMA is to be performed, exit immediately.
3973 * Otherwise, we are in polling mode, so poll
3974 * status under operation succeeds or fails.
3977 * Kernel thread context (may sleep)
3980 static void atapi_packet_task(void *_data)
3982 struct ata_port *ap = _data;
3983 struct ata_queued_cmd *qc;
3986 qc = ata_qc_from_tag(ap, ap->active_tag);
3988 assert(qc->flags & ATA_QCFLAG_ACTIVE);
3990 /* sleep-wait for BSY to clear */
3991 DPRINTK("busy wait\n");
3992 if (ata_busy_sleep(ap, ATA_TMOUT_CDB_QUICK, ATA_TMOUT_CDB))
3995 /* make sure DRQ is set */
3996 status = ata_chk_status(ap);
3997 if ((status & (ATA_BUSY | ATA_DRQ)) != ATA_DRQ)
4001 DPRINTK("send cdb\n");
4002 assert(ap->cdb_len >= 12);
4004 if (qc->tf.protocol == ATA_PROT_ATAPI_DMA ||
4005 qc->tf.protocol == ATA_PROT_ATAPI_NODATA) {
4006 unsigned long flags;
4008 /* Once we're done issuing command and kicking bmdma,
4009 * irq handler takes over. To not lose irq, we need
4010 * to clear NOINTR flag before sending cdb, but
4011 * interrupt handler shouldn't be invoked before we're
4012 * finished. Hence, the following locking.
4014 spin_lock_irqsave(&ap->host_set->lock, flags);
4015 ap->flags &= ~ATA_FLAG_NOINTR;
4016 ata_data_xfer(ap, qc->cdb, ap->cdb_len, 1);
4017 if (qc->tf.protocol == ATA_PROT_ATAPI_DMA)
4018 ap->ops->bmdma_start(qc); /* initiate bmdma */
4019 spin_unlock_irqrestore(&ap->host_set->lock, flags);
4021 ata_data_xfer(ap, qc->cdb, ap->cdb_len, 1);
4023 /* PIO commands are handled by polling */
4024 ap->hsm_task_state = HSM_ST;
4025 queue_work(ata_wq, &ap->pio_task);
4031 ata_poll_qc_complete(qc, ATA_ERR);
4036 * ata_port_start - Set port up for dma.
4037 * @ap: Port to initialize
4039 * Called just after data structures for each port are
4040 * initialized. Allocates space for PRD table.
4042 * May be used as the port_start() entry in ata_port_operations.
4045 * Inherited from caller.
4048 int ata_port_start (struct ata_port *ap)
4050 struct device *dev = ap->host_set->dev;
4052 ap->prd = dma_alloc_coherent(dev, ATA_PRD_TBL_SZ, &ap->prd_dma, GFP_KERNEL);
4056 DPRINTK("prd alloc, virt %p, dma %llx\n", ap->prd, (unsigned long long) ap->prd_dma);
4063 * ata_port_stop - Undo ata_port_start()
4064 * @ap: Port to shut down
4066 * Frees the PRD table.
4068 * May be used as the port_stop() entry in ata_port_operations.
4071 * Inherited from caller.
4074 void ata_port_stop (struct ata_port *ap)
4076 struct device *dev = ap->host_set->dev;
4078 dma_free_coherent(dev, ATA_PRD_TBL_SZ, ap->prd, ap->prd_dma);
4081 void ata_host_stop (struct ata_host_set *host_set)
4083 if (host_set->mmio_base)
4084 iounmap(host_set->mmio_base);
4089 * ata_host_remove - Unregister SCSI host structure with upper layers
4090 * @ap: Port to unregister
4091 * @do_unregister: 1 if we fully unregister, 0 to just stop the port
4094 * Inherited from caller.
4097 static void ata_host_remove(struct ata_port *ap, unsigned int do_unregister)
4099 struct Scsi_Host *sh = ap->host;
4104 scsi_remove_host(sh);
4106 ap->ops->port_stop(ap);
4110 * ata_host_init - Initialize an ata_port structure
4111 * @ap: Structure to initialize
4112 * @host: associated SCSI mid-layer structure
4113 * @host_set: Collection of hosts to which @ap belongs
4114 * @ent: Probe information provided by low-level driver
4115 * @port_no: Port number associated with this ata_port
4117 * Initialize a new ata_port structure, and its associated
4121 * Inherited from caller.
4124 static void ata_host_init(struct ata_port *ap, struct Scsi_Host *host,
4125 struct ata_host_set *host_set,
4126 const struct ata_probe_ent *ent, unsigned int port_no)
4132 host->max_channel = 1;
4133 host->unique_id = ata_unique_id++;
4134 host->max_cmd_len = 12;
4136 scsi_assign_lock(host, &host_set->lock);
4138 ap->flags = ATA_FLAG_PORT_DISABLED;
4139 ap->id = host->unique_id;
4141 ap->ctl = ATA_DEVCTL_OBS;
4142 ap->host_set = host_set;
4143 ap->port_no = port_no;
4145 ent->legacy_mode ? ent->hard_port_no : port_no;
4146 ap->pio_mask = ent->pio_mask;
4147 ap->mwdma_mask = ent->mwdma_mask;
4148 ap->udma_mask = ent->udma_mask;
4149 ap->flags |= ent->host_flags;
4150 ap->ops = ent->port_ops;
4151 ap->cbl = ATA_CBL_NONE;
4152 ap->active_tag = ATA_TAG_POISON;
4153 ap->last_ctl = 0xFF;
4155 INIT_WORK(&ap->packet_task, atapi_packet_task, ap);
4156 INIT_WORK(&ap->pio_task, ata_pio_task, ap);
4158 for (i = 0; i < ATA_MAX_DEVICES; i++)
4159 ap->device[i].devno = i;
4162 ap->stats.unhandled_irq = 1;
4163 ap->stats.idle_irq = 1;
4166 memcpy(&ap->ioaddr, &ent->port[port_no], sizeof(struct ata_ioports));
4170 * ata_host_add - Attach low-level ATA driver to system
4171 * @ent: Information provided by low-level driver
4172 * @host_set: Collections of ports to which we add
4173 * @port_no: Port number associated with this host
4175 * Attach low-level ATA driver to system.
4178 * PCI/etc. bus probe sem.
4181 * New ata_port on success, for NULL on error.
4184 static struct ata_port * ata_host_add(const struct ata_probe_ent *ent,
4185 struct ata_host_set *host_set,
4186 unsigned int port_no)
4188 struct Scsi_Host *host;
4189 struct ata_port *ap;
4193 host = scsi_host_alloc(ent->sht, sizeof(struct ata_port));
4197 ap = (struct ata_port *) &host->hostdata[0];
4199 ata_host_init(ap, host, host_set, ent, port_no);
4201 rc = ap->ops->port_start(ap);
4208 scsi_host_put(host);
4213 * ata_device_add - Register hardware device with ATA and SCSI layers
4214 * @ent: Probe information describing hardware device to be registered
4216 * This function processes the information provided in the probe
4217 * information struct @ent, allocates the necessary ATA and SCSI
4218 * host information structures, initializes them, and registers
4219 * everything with requisite kernel subsystems.
4221 * This function requests irqs, probes the ATA bus, and probes
4225 * PCI/etc. bus probe sem.
4228 * Number of ports registered. Zero on error (no ports registered).
4231 int ata_device_add(const struct ata_probe_ent *ent)
4233 unsigned int count = 0, i;
4234 struct device *dev = ent->dev;
4235 struct ata_host_set *host_set;
4238 /* alloc a container for our list of ATA ports (buses) */
4239 host_set = kzalloc(sizeof(struct ata_host_set) +
4240 (ent->n_ports * sizeof(void *)), GFP_KERNEL);
4243 spin_lock_init(&host_set->lock);
4245 host_set->dev = dev;
4246 host_set->n_ports = ent->n_ports;
4247 host_set->irq = ent->irq;
4248 host_set->mmio_base = ent->mmio_base;
4249 host_set->private_data = ent->private_data;
4250 host_set->ops = ent->port_ops;
4252 /* register each port bound to this device */
4253 for (i = 0; i < ent->n_ports; i++) {
4254 struct ata_port *ap;
4255 unsigned long xfer_mode_mask;
4257 ap = ata_host_add(ent, host_set, i);
4261 host_set->ports[i] = ap;
4262 xfer_mode_mask =(ap->udma_mask << ATA_SHIFT_UDMA) |
4263 (ap->mwdma_mask << ATA_SHIFT_MWDMA) |
4264 (ap->pio_mask << ATA_SHIFT_PIO);
4266 /* print per-port info to dmesg */
4267 printk(KERN_INFO "ata%u: %cATA max %s cmd 0x%lX ctl 0x%lX "
4268 "bmdma 0x%lX irq %lu\n",
4270 ap->flags & ATA_FLAG_SATA ? 'S' : 'P',
4271 ata_mode_string(xfer_mode_mask),
4272 ap->ioaddr.cmd_addr,
4273 ap->ioaddr.ctl_addr,
4274 ap->ioaddr.bmdma_addr,
4278 host_set->ops->irq_clear(ap);
4285 /* obtain irq, that is shared between channels */
4286 if (request_irq(ent->irq, ent->port_ops->irq_handler, ent->irq_flags,
4287 DRV_NAME, host_set))
4290 /* perform each probe synchronously */
4291 DPRINTK("probe begin\n");
4292 for (i = 0; i < count; i++) {
4293 struct ata_port *ap;
4296 ap = host_set->ports[i];
4298 DPRINTK("ata%u: probe begin\n", ap->id);
4299 rc = ata_bus_probe(ap);
4300 DPRINTK("ata%u: probe end\n", ap->id);
4303 /* FIXME: do something useful here?
4304 * Current libata behavior will
4305 * tear down everything when
4306 * the module is removed
4307 * or the h/w is unplugged.
4311 rc = scsi_add_host(ap->host, dev);
4313 printk(KERN_ERR "ata%u: scsi_add_host failed\n",
4315 /* FIXME: do something useful here */
4316 /* FIXME: handle unconditional calls to
4317 * scsi_scan_host and ata_host_remove, below,
4323 /* probes are done, now scan each port's disk(s) */
4324 DPRINTK("probe begin\n");
4325 for (i = 0; i < count; i++) {
4326 struct ata_port *ap = host_set->ports[i];
4328 ata_scsi_scan_host(ap);
4331 dev_set_drvdata(dev, host_set);
4333 VPRINTK("EXIT, returning %u\n", ent->n_ports);
4334 return ent->n_ports; /* success */
4337 for (i = 0; i < count; i++) {
4338 ata_host_remove(host_set->ports[i], 1);
4339 scsi_host_put(host_set->ports[i]->host);
4343 VPRINTK("EXIT, returning 0\n");
4348 * ata_host_set_remove - PCI layer callback for device removal
4349 * @host_set: ATA host set that was removed
4351 * Unregister all objects associated with this host set. Free those
4355 * Inherited from calling layer (may sleep).
4358 void ata_host_set_remove(struct ata_host_set *host_set)
4360 struct ata_port *ap;
4363 for (i = 0; i < host_set->n_ports; i++) {
4364 ap = host_set->ports[i];
4365 scsi_remove_host(ap->host);
4368 free_irq(host_set->irq, host_set);
4370 for (i = 0; i < host_set->n_ports; i++) {
4371 ap = host_set->ports[i];
4373 ata_scsi_release(ap->host);
4375 if ((ap->flags & ATA_FLAG_NO_LEGACY) == 0) {
4376 struct ata_ioports *ioaddr = &ap->ioaddr;
4378 if (ioaddr->cmd_addr == 0x1f0)
4379 release_region(0x1f0, 8);
4380 else if (ioaddr->cmd_addr == 0x170)
4381 release_region(0x170, 8);
4384 scsi_host_put(ap->host);
4387 if (host_set->ops->host_stop)
4388 host_set->ops->host_stop(host_set);
4394 * ata_scsi_release - SCSI layer callback hook for host unload
4395 * @host: libata host to be unloaded
4397 * Performs all duties necessary to shut down a libata port...
4398 * Kill port kthread, disable port, and release resources.
4401 * Inherited from SCSI layer.
4407 int ata_scsi_release(struct Scsi_Host *host)
4409 struct ata_port *ap = (struct ata_port *) &host->hostdata[0];
4413 ap->ops->port_disable(ap);
4414 ata_host_remove(ap, 0);
4421 * ata_std_ports - initialize ioaddr with standard port offsets.
4422 * @ioaddr: IO address structure to be initialized
4424 * Utility function which initializes data_addr, error_addr,
4425 * feature_addr, nsect_addr, lbal_addr, lbam_addr, lbah_addr,
4426 * device_addr, status_addr, and command_addr to standard offsets
4427 * relative to cmd_addr.
4429 * Does not set ctl_addr, altstatus_addr, bmdma_addr, or scr_addr.
4432 void ata_std_ports(struct ata_ioports *ioaddr)
4434 ioaddr->data_addr = ioaddr->cmd_addr + ATA_REG_DATA;
4435 ioaddr->error_addr = ioaddr->cmd_addr + ATA_REG_ERR;
4436 ioaddr->feature_addr = ioaddr->cmd_addr + ATA_REG_FEATURE;
4437 ioaddr->nsect_addr = ioaddr->cmd_addr + ATA_REG_NSECT;
4438 ioaddr->lbal_addr = ioaddr->cmd_addr + ATA_REG_LBAL;
4439 ioaddr->lbam_addr = ioaddr->cmd_addr + ATA_REG_LBAM;
4440 ioaddr->lbah_addr = ioaddr->cmd_addr + ATA_REG_LBAH;
4441 ioaddr->device_addr = ioaddr->cmd_addr + ATA_REG_DEVICE;
4442 ioaddr->status_addr = ioaddr->cmd_addr + ATA_REG_STATUS;
4443 ioaddr->command_addr = ioaddr->cmd_addr + ATA_REG_CMD;
4446 static struct ata_probe_ent *
4447 ata_probe_ent_alloc(struct device *dev, const struct ata_port_info *port)
4449 struct ata_probe_ent *probe_ent;
4451 probe_ent = kzalloc(sizeof(*probe_ent), GFP_KERNEL);
4453 printk(KERN_ERR DRV_NAME "(%s): out of memory\n",
4454 kobject_name(&(dev->kobj)));
4458 INIT_LIST_HEAD(&probe_ent->node);
4459 probe_ent->dev = dev;
4461 probe_ent->sht = port->sht;
4462 probe_ent->host_flags = port->host_flags;
4463 probe_ent->pio_mask = port->pio_mask;
4464 probe_ent->mwdma_mask = port->mwdma_mask;
4465 probe_ent->udma_mask = port->udma_mask;
4466 probe_ent->port_ops = port->port_ops;
4475 void ata_pci_host_stop (struct ata_host_set *host_set)
4477 struct pci_dev *pdev = to_pci_dev(host_set->dev);
4479 pci_iounmap(pdev, host_set->mmio_base);
4483 * ata_pci_init_native_mode - Initialize native-mode driver
4484 * @pdev: pci device to be initialized
4485 * @port: array[2] of pointers to port info structures.
4486 * @ports: bitmap of ports present
4488 * Utility function which allocates and initializes an
4489 * ata_probe_ent structure for a standard dual-port
4490 * PIO-based IDE controller. The returned ata_probe_ent
4491 * structure can be passed to ata_device_add(). The returned
4492 * ata_probe_ent structure should then be freed with kfree().
4494 * The caller need only pass the address of the primary port, the
4495 * secondary will be deduced automatically. If the device has non
4496 * standard secondary port mappings this function can be called twice,
4497 * once for each interface.
4500 struct ata_probe_ent *
4501 ata_pci_init_native_mode(struct pci_dev *pdev, struct ata_port_info **port, int ports)
4503 struct ata_probe_ent *probe_ent =
4504 ata_probe_ent_alloc(pci_dev_to_dev(pdev), port[0]);
4510 probe_ent->irq = pdev->irq;
4511 probe_ent->irq_flags = SA_SHIRQ;
4513 if (ports & ATA_PORT_PRIMARY) {
4514 probe_ent->port[p].cmd_addr = pci_resource_start(pdev, 0);
4515 probe_ent->port[p].altstatus_addr =
4516 probe_ent->port[p].ctl_addr =
4517 pci_resource_start(pdev, 1) | ATA_PCI_CTL_OFS;
4518 probe_ent->port[p].bmdma_addr = pci_resource_start(pdev, 4);
4519 ata_std_ports(&probe_ent->port[p]);
4523 if (ports & ATA_PORT_SECONDARY) {
4524 probe_ent->port[p].cmd_addr = pci_resource_start(pdev, 2);
4525 probe_ent->port[p].altstatus_addr =
4526 probe_ent->port[p].ctl_addr =
4527 pci_resource_start(pdev, 3) | ATA_PCI_CTL_OFS;
4528 probe_ent->port[p].bmdma_addr = pci_resource_start(pdev, 4) + 8;
4529 ata_std_ports(&probe_ent->port[p]);
4533 probe_ent->n_ports = p;
4537 static struct ata_probe_ent *ata_pci_init_legacy_port(struct pci_dev *pdev, struct ata_port_info **port, int port_num)
4539 struct ata_probe_ent *probe_ent;
4541 probe_ent = ata_probe_ent_alloc(pci_dev_to_dev(pdev), port[0]);
4545 probe_ent->legacy_mode = 1;
4546 probe_ent->n_ports = 1;
4547 probe_ent->hard_port_no = port_num;
4552 probe_ent->irq = 14;
4553 probe_ent->port[0].cmd_addr = 0x1f0;
4554 probe_ent->port[0].altstatus_addr =
4555 probe_ent->port[0].ctl_addr = 0x3f6;
4558 probe_ent->irq = 15;
4559 probe_ent->port[0].cmd_addr = 0x170;
4560 probe_ent->port[0].altstatus_addr =
4561 probe_ent->port[0].ctl_addr = 0x376;
4564 probe_ent->port[0].bmdma_addr = pci_resource_start(pdev, 4) + 8 * port_num;
4565 ata_std_ports(&probe_ent->port[0]);
4570 * ata_pci_init_one - Initialize/register PCI IDE host controller
4571 * @pdev: Controller to be initialized
4572 * @port_info: Information from low-level host driver
4573 * @n_ports: Number of ports attached to host controller
4575 * This is a helper function which can be called from a driver's
4576 * xxx_init_one() probe function if the hardware uses traditional
4577 * IDE taskfile registers.
4579 * This function calls pci_enable_device(), reserves its register
4580 * regions, sets the dma mask, enables bus master mode, and calls
4584 * Inherited from PCI layer (may sleep).
4587 * Zero on success, negative on errno-based value on error.
4590 int ata_pci_init_one (struct pci_dev *pdev, struct ata_port_info **port_info,
4591 unsigned int n_ports)
4593 struct ata_probe_ent *probe_ent = NULL, *probe_ent2 = NULL;
4594 struct ata_port_info *port[2];
4596 unsigned int legacy_mode = 0;
4597 int disable_dev_on_err = 1;
4602 port[0] = port_info[0];
4604 port[1] = port_info[1];
4608 if ((port[0]->host_flags & ATA_FLAG_NO_LEGACY) == 0
4609 && (pdev->class >> 8) == PCI_CLASS_STORAGE_IDE) {
4610 /* TODO: What if one channel is in native mode ... */
4611 pci_read_config_byte(pdev, PCI_CLASS_PROG, &tmp8);
4612 mask = (1 << 2) | (1 << 0);
4613 if ((tmp8 & mask) != mask)
4614 legacy_mode = (1 << 3);
4618 if ((!legacy_mode) && (n_ports > 2)) {
4619 printk(KERN_ERR "ata: BUG: native mode, n_ports > 2\n");
4624 /* FIXME: Really for ATA it isn't safe because the device may be
4625 multi-purpose and we want to leave it alone if it was already
4626 enabled. Secondly for shared use as Arjan says we want refcounting
4628 Checking dev->is_enabled is insufficient as this is not set at
4629 boot for the primary video which is BIOS enabled
4632 rc = pci_enable_device(pdev);
4636 rc = pci_request_regions(pdev, DRV_NAME);
4638 disable_dev_on_err = 0;
4642 /* FIXME: Should use platform specific mappers for legacy port ranges */
4644 if (!request_region(0x1f0, 8, "libata")) {
4645 struct resource *conflict, res;
4647 res.end = 0x1f0 + 8 - 1;
4648 conflict = ____request_resource(&ioport_resource, &res);
4649 if (!strcmp(conflict->name, "libata"))
4650 legacy_mode |= (1 << 0);
4652 disable_dev_on_err = 0;
4653 printk(KERN_WARNING "ata: 0x1f0 IDE port busy\n");
4656 legacy_mode |= (1 << 0);
4658 if (!request_region(0x170, 8, "libata")) {
4659 struct resource *conflict, res;
4661 res.end = 0x170 + 8 - 1;
4662 conflict = ____request_resource(&ioport_resource, &res);
4663 if (!strcmp(conflict->name, "libata"))
4664 legacy_mode |= (1 << 1);
4666 disable_dev_on_err = 0;
4667 printk(KERN_WARNING "ata: 0x170 IDE port busy\n");
4670 legacy_mode |= (1 << 1);
4673 /* we have legacy mode, but all ports are unavailable */
4674 if (legacy_mode == (1 << 3)) {
4676 goto err_out_regions;
4679 rc = pci_set_dma_mask(pdev, ATA_DMA_MASK);
4681 goto err_out_regions;
4682 rc = pci_set_consistent_dma_mask(pdev, ATA_DMA_MASK);
4684 goto err_out_regions;
4687 if (legacy_mode & (1 << 0))
4688 probe_ent = ata_pci_init_legacy_port(pdev, port, 0);
4689 if (legacy_mode & (1 << 1))
4690 probe_ent2 = ata_pci_init_legacy_port(pdev, port, 1);
4693 probe_ent = ata_pci_init_native_mode(pdev, port, ATA_PORT_PRIMARY | ATA_PORT_SECONDARY);
4695 probe_ent = ata_pci_init_native_mode(pdev, port, ATA_PORT_PRIMARY);
4697 if (!probe_ent && !probe_ent2) {
4699 goto err_out_regions;
4702 pci_set_master(pdev);
4704 /* FIXME: check ata_device_add return */
4706 if (legacy_mode & (1 << 0))
4707 ata_device_add(probe_ent);
4708 if (legacy_mode & (1 << 1))
4709 ata_device_add(probe_ent2);
4711 ata_device_add(probe_ent);
4719 if (legacy_mode & (1 << 0))
4720 release_region(0x1f0, 8);
4721 if (legacy_mode & (1 << 1))
4722 release_region(0x170, 8);
4723 pci_release_regions(pdev);
4725 if (disable_dev_on_err)
4726 pci_disable_device(pdev);
4731 * ata_pci_remove_one - PCI layer callback for device removal
4732 * @pdev: PCI device that was removed
4734 * PCI layer indicates to libata via this hook that
4735 * hot-unplug or module unload event has occurred.
4736 * Handle this by unregistering all objects associated
4737 * with this PCI device. Free those objects. Then finally
4738 * release PCI resources and disable device.
4741 * Inherited from PCI layer (may sleep).
4744 void ata_pci_remove_one (struct pci_dev *pdev)
4746 struct device *dev = pci_dev_to_dev(pdev);
4747 struct ata_host_set *host_set = dev_get_drvdata(dev);
4749 ata_host_set_remove(host_set);
4750 pci_release_regions(pdev);
4751 pci_disable_device(pdev);
4752 dev_set_drvdata(dev, NULL);
4755 /* move to PCI subsystem */
4756 int pci_test_config_bits(struct pci_dev *pdev, const struct pci_bits *bits)
4758 unsigned long tmp = 0;
4760 switch (bits->width) {
4763 pci_read_config_byte(pdev, bits->reg, &tmp8);
4769 pci_read_config_word(pdev, bits->reg, &tmp16);
4775 pci_read_config_dword(pdev, bits->reg, &tmp32);
4786 return (tmp == bits->val) ? 1 : 0;
4788 #endif /* CONFIG_PCI */
4791 static int __init ata_init(void)
4793 ata_wq = create_workqueue("ata");
4797 printk(KERN_DEBUG "libata version " DRV_VERSION " loaded.\n");
4801 static void __exit ata_exit(void)
4803 destroy_workqueue(ata_wq);
4806 module_init(ata_init);
4807 module_exit(ata_exit);
4809 static unsigned long ratelimit_time;
4810 static spinlock_t ata_ratelimit_lock = SPIN_LOCK_UNLOCKED;
4812 int ata_ratelimit(void)
4815 unsigned long flags;
4817 spin_lock_irqsave(&ata_ratelimit_lock, flags);
4819 if (time_after(jiffies, ratelimit_time)) {
4821 ratelimit_time = jiffies + (HZ/5);
4825 spin_unlock_irqrestore(&ata_ratelimit_lock, flags);
4831 * libata is essentially a library of internal helper functions for
4832 * low-level ATA host controller drivers. As such, the API/ABI is
4833 * likely to change as new drivers are added and updated.
4834 * Do not depend on ABI/API stability.
4837 EXPORT_SYMBOL_GPL(ata_std_bios_param);
4838 EXPORT_SYMBOL_GPL(ata_std_ports);
4839 EXPORT_SYMBOL_GPL(ata_device_add);
4840 EXPORT_SYMBOL_GPL(ata_host_set_remove);
4841 EXPORT_SYMBOL_GPL(ata_sg_init);
4842 EXPORT_SYMBOL_GPL(ata_sg_init_one);
4843 EXPORT_SYMBOL_GPL(ata_qc_complete);
4844 EXPORT_SYMBOL_GPL(ata_qc_issue_prot);
4845 EXPORT_SYMBOL_GPL(ata_eng_timeout);
4846 EXPORT_SYMBOL_GPL(ata_tf_load);
4847 EXPORT_SYMBOL_GPL(ata_tf_read);
4848 EXPORT_SYMBOL_GPL(ata_noop_dev_select);
4849 EXPORT_SYMBOL_GPL(ata_std_dev_select);
4850 EXPORT_SYMBOL_GPL(ata_tf_to_fis);
4851 EXPORT_SYMBOL_GPL(ata_tf_from_fis);
4852 EXPORT_SYMBOL_GPL(ata_check_status);
4853 EXPORT_SYMBOL_GPL(ata_altstatus);
4854 EXPORT_SYMBOL_GPL(ata_exec_command);
4855 EXPORT_SYMBOL_GPL(ata_port_start);
4856 EXPORT_SYMBOL_GPL(ata_port_stop);
4857 EXPORT_SYMBOL_GPL(ata_host_stop);
4858 EXPORT_SYMBOL_GPL(ata_interrupt);
4859 EXPORT_SYMBOL_GPL(ata_qc_prep);
4860 EXPORT_SYMBOL_GPL(ata_bmdma_setup);
4861 EXPORT_SYMBOL_GPL(ata_bmdma_start);
4862 EXPORT_SYMBOL_GPL(ata_bmdma_irq_clear);
4863 EXPORT_SYMBOL_GPL(ata_bmdma_status);
4864 EXPORT_SYMBOL_GPL(ata_bmdma_stop);
4865 EXPORT_SYMBOL_GPL(ata_port_probe);
4866 EXPORT_SYMBOL_GPL(sata_phy_reset);
4867 EXPORT_SYMBOL_GPL(__sata_phy_reset);
4868 EXPORT_SYMBOL_GPL(ata_bus_reset);
4869 EXPORT_SYMBOL_GPL(ata_port_disable);
4870 EXPORT_SYMBOL_GPL(ata_ratelimit);
4871 EXPORT_SYMBOL_GPL(ata_scsi_ioctl);
4872 EXPORT_SYMBOL_GPL(ata_scsi_queuecmd);
4873 EXPORT_SYMBOL_GPL(ata_scsi_error);
4874 EXPORT_SYMBOL_GPL(ata_scsi_slave_config);
4875 EXPORT_SYMBOL_GPL(ata_scsi_release);
4876 EXPORT_SYMBOL_GPL(ata_host_intr);
4877 EXPORT_SYMBOL_GPL(ata_dev_classify);
4878 EXPORT_SYMBOL_GPL(ata_dev_id_string);
4879 EXPORT_SYMBOL_GPL(ata_dev_config);
4880 EXPORT_SYMBOL_GPL(ata_scsi_simulate);
4882 EXPORT_SYMBOL_GPL(ata_timing_compute);
4883 EXPORT_SYMBOL_GPL(ata_timing_merge);
4886 EXPORT_SYMBOL_GPL(pci_test_config_bits);
4887 EXPORT_SYMBOL_GPL(ata_pci_host_stop);
4888 EXPORT_SYMBOL_GPL(ata_pci_init_native_mode);
4889 EXPORT_SYMBOL_GPL(ata_pci_init_one);
4890 EXPORT_SYMBOL_GPL(ata_pci_remove_one);
4891 #endif /* CONFIG_PCI */