2 * libata-core.c - helper library for ATA
4 * Maintained by: Jeff Garzik <jgarzik@pobox.com>
5 * Please ALWAYS copy linux-ide@vger.kernel.org
8 * Copyright 2003-2004 Red Hat, Inc. All rights reserved.
9 * Copyright 2003-2004 Jeff Garzik
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2, or (at your option)
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; see the file COPYING. If not, write to
24 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
27 * libata documentation is available via 'make {ps|pdf}docs',
28 * as Documentation/DocBook/libata.*
30 * Hardware documentation available from http://www.t13.org/ and
31 * http://www.sata-io.org/
35 #include <linux/config.h>
36 #include <linux/kernel.h>
37 #include <linux/module.h>
38 #include <linux/pci.h>
39 #include <linux/init.h>
40 #include <linux/list.h>
42 #include <linux/highmem.h>
43 #include <linux/spinlock.h>
44 #include <linux/blkdev.h>
45 #include <linux/delay.h>
46 #include <linux/timer.h>
47 #include <linux/interrupt.h>
48 #include <linux/completion.h>
49 #include <linux/suspend.h>
50 #include <linux/workqueue.h>
51 #include <scsi/scsi.h>
53 #include "scsi_priv.h"
54 #include <scsi/scsi_host.h>
55 #include <linux/libata.h>
57 #include <asm/semaphore.h>
58 #include <asm/byteorder.h>
62 static unsigned int ata_busy_sleep (struct ata_port *ap,
63 unsigned long tmout_pat,
65 static void ata_dev_init_params(struct ata_port *ap, struct ata_device *dev);
66 static void ata_set_mode(struct ata_port *ap);
67 static void ata_dev_set_xfermode(struct ata_port *ap, struct ata_device *dev);
68 static unsigned int ata_get_mode_mask(struct ata_port *ap, int shift);
69 static int fgb(u32 bitmap);
70 static int ata_choose_xfer_mode(struct ata_port *ap,
72 unsigned int *xfer_shift_out);
73 static int ata_qc_complete_noop(struct ata_queued_cmd *qc, u8 drv_stat);
74 static void __ata_qc_complete(struct ata_queued_cmd *qc);
76 static unsigned int ata_unique_id = 1;
77 static struct workqueue_struct *ata_wq;
79 MODULE_AUTHOR("Jeff Garzik");
80 MODULE_DESCRIPTION("Library module for ATA devices");
81 MODULE_LICENSE("GPL");
82 MODULE_VERSION(DRV_VERSION);
85 * ata_tf_load - send taskfile registers to host controller
86 * @ap: Port to which output is sent
87 * @tf: ATA taskfile register set
89 * Outputs ATA taskfile to standard ATA host controller.
92 * Inherited from caller.
95 static void ata_tf_load_pio(struct ata_port *ap, struct ata_taskfile *tf)
97 struct ata_ioports *ioaddr = &ap->ioaddr;
98 unsigned int is_addr = tf->flags & ATA_TFLAG_ISADDR;
100 if (tf->ctl != ap->last_ctl) {
101 outb(tf->ctl, ioaddr->ctl_addr);
102 ap->last_ctl = tf->ctl;
106 if (is_addr && (tf->flags & ATA_TFLAG_LBA48)) {
107 outb(tf->hob_feature, ioaddr->feature_addr);
108 outb(tf->hob_nsect, ioaddr->nsect_addr);
109 outb(tf->hob_lbal, ioaddr->lbal_addr);
110 outb(tf->hob_lbam, ioaddr->lbam_addr);
111 outb(tf->hob_lbah, ioaddr->lbah_addr);
112 VPRINTK("hob: feat 0x%X nsect 0x%X, lba 0x%X 0x%X 0x%X\n",
121 outb(tf->feature, ioaddr->feature_addr);
122 outb(tf->nsect, ioaddr->nsect_addr);
123 outb(tf->lbal, ioaddr->lbal_addr);
124 outb(tf->lbam, ioaddr->lbam_addr);
125 outb(tf->lbah, ioaddr->lbah_addr);
126 VPRINTK("feat 0x%X nsect 0x%X lba 0x%X 0x%X 0x%X\n",
134 if (tf->flags & ATA_TFLAG_DEVICE) {
135 outb(tf->device, ioaddr->device_addr);
136 VPRINTK("device 0x%X\n", tf->device);
143 * ata_tf_load_mmio - send taskfile registers to host controller
144 * @ap: Port to which output is sent
145 * @tf: ATA taskfile register set
147 * Outputs ATA taskfile to standard ATA host controller using MMIO.
150 * Inherited from caller.
153 static void ata_tf_load_mmio(struct ata_port *ap, struct ata_taskfile *tf)
155 struct ata_ioports *ioaddr = &ap->ioaddr;
156 unsigned int is_addr = tf->flags & ATA_TFLAG_ISADDR;
158 if (tf->ctl != ap->last_ctl) {
159 writeb(tf->ctl, (void __iomem *) ap->ioaddr.ctl_addr);
160 ap->last_ctl = tf->ctl;
164 if (is_addr && (tf->flags & ATA_TFLAG_LBA48)) {
165 writeb(tf->hob_feature, (void __iomem *) ioaddr->feature_addr);
166 writeb(tf->hob_nsect, (void __iomem *) ioaddr->nsect_addr);
167 writeb(tf->hob_lbal, (void __iomem *) ioaddr->lbal_addr);
168 writeb(tf->hob_lbam, (void __iomem *) ioaddr->lbam_addr);
169 writeb(tf->hob_lbah, (void __iomem *) ioaddr->lbah_addr);
170 VPRINTK("hob: feat 0x%X nsect 0x%X, lba 0x%X 0x%X 0x%X\n",
179 writeb(tf->feature, (void __iomem *) ioaddr->feature_addr);
180 writeb(tf->nsect, (void __iomem *) ioaddr->nsect_addr);
181 writeb(tf->lbal, (void __iomem *) ioaddr->lbal_addr);
182 writeb(tf->lbam, (void __iomem *) ioaddr->lbam_addr);
183 writeb(tf->lbah, (void __iomem *) ioaddr->lbah_addr);
184 VPRINTK("feat 0x%X nsect 0x%X lba 0x%X 0x%X 0x%X\n",
192 if (tf->flags & ATA_TFLAG_DEVICE) {
193 writeb(tf->device, (void __iomem *) ioaddr->device_addr);
194 VPRINTK("device 0x%X\n", tf->device);
202 * ata_tf_load - send taskfile registers to host controller
203 * @ap: Port to which output is sent
204 * @tf: ATA taskfile register set
206 * Outputs ATA taskfile to standard ATA host controller using MMIO
207 * or PIO as indicated by the ATA_FLAG_MMIO flag.
208 * Writes the control, feature, nsect, lbal, lbam, and lbah registers.
209 * Optionally (ATA_TFLAG_LBA48) writes hob_feature, hob_nsect,
210 * hob_lbal, hob_lbam, and hob_lbah.
212 * This function waits for idle (!BUSY and !DRQ) after writing
213 * registers. If the control register has a new value, this
214 * function also waits for idle after writing control and before
215 * writing the remaining registers.
217 * May be used as the tf_load() entry in ata_port_operations.
220 * Inherited from caller.
222 void ata_tf_load(struct ata_port *ap, struct ata_taskfile *tf)
224 if (ap->flags & ATA_FLAG_MMIO)
225 ata_tf_load_mmio(ap, tf);
227 ata_tf_load_pio(ap, tf);
231 * ata_exec_command_pio - issue ATA command to host controller
232 * @ap: port to which command is being issued
233 * @tf: ATA taskfile register set
235 * Issues PIO write to ATA command register, with proper
236 * synchronization with interrupt handler / other threads.
239 * spin_lock_irqsave(host_set lock)
242 static void ata_exec_command_pio(struct ata_port *ap, struct ata_taskfile *tf)
244 DPRINTK("ata%u: cmd 0x%X\n", ap->id, tf->command);
246 outb(tf->command, ap->ioaddr.command_addr);
252 * ata_exec_command_mmio - issue ATA command to host controller
253 * @ap: port to which command is being issued
254 * @tf: ATA taskfile register set
256 * Issues MMIO write to ATA command register, with proper
257 * synchronization with interrupt handler / other threads.
260 * spin_lock_irqsave(host_set lock)
263 static void ata_exec_command_mmio(struct ata_port *ap, struct ata_taskfile *tf)
265 DPRINTK("ata%u: cmd 0x%X\n", ap->id, tf->command);
267 writeb(tf->command, (void __iomem *) ap->ioaddr.command_addr);
273 * ata_exec_command - issue ATA command to host controller
274 * @ap: port to which command is being issued
275 * @tf: ATA taskfile register set
277 * Issues PIO/MMIO write to ATA command register, with proper
278 * synchronization with interrupt handler / other threads.
281 * spin_lock_irqsave(host_set lock)
283 void ata_exec_command(struct ata_port *ap, struct ata_taskfile *tf)
285 if (ap->flags & ATA_FLAG_MMIO)
286 ata_exec_command_mmio(ap, tf);
288 ata_exec_command_pio(ap, tf);
292 * ata_exec - issue ATA command to host controller
293 * @ap: port to which command is being issued
294 * @tf: ATA taskfile register set
296 * Issues PIO/MMIO write to ATA command register, with proper
297 * synchronization with interrupt handler / other threads.
300 * Obtains host_set lock.
303 static inline void ata_exec(struct ata_port *ap, struct ata_taskfile *tf)
307 DPRINTK("ata%u: cmd 0x%X\n", ap->id, tf->command);
308 spin_lock_irqsave(&ap->host_set->lock, flags);
309 ap->ops->exec_command(ap, tf);
310 spin_unlock_irqrestore(&ap->host_set->lock, flags);
314 * ata_tf_to_host - issue ATA taskfile to host controller
315 * @ap: port to which command is being issued
316 * @tf: ATA taskfile register set
318 * Issues ATA taskfile register set to ATA host controller,
319 * with proper synchronization with interrupt handler and
323 * Obtains host_set lock.
326 static void ata_tf_to_host(struct ata_port *ap, struct ata_taskfile *tf)
328 ap->ops->tf_load(ap, tf);
334 * ata_tf_to_host_nolock - issue ATA taskfile to host controller
335 * @ap: port to which command is being issued
336 * @tf: ATA taskfile register set
338 * Issues ATA taskfile register set to ATA host controller,
339 * with proper synchronization with interrupt handler and
343 * spin_lock_irqsave(host_set lock)
346 void ata_tf_to_host_nolock(struct ata_port *ap, struct ata_taskfile *tf)
348 ap->ops->tf_load(ap, tf);
349 ap->ops->exec_command(ap, tf);
353 * ata_tf_read_pio - input device's ATA taskfile shadow registers
354 * @ap: Port from which input is read
355 * @tf: ATA taskfile register set for storing input
357 * Reads ATA taskfile registers for currently-selected device
361 * Inherited from caller.
364 static void ata_tf_read_pio(struct ata_port *ap, struct ata_taskfile *tf)
366 struct ata_ioports *ioaddr = &ap->ioaddr;
368 tf->nsect = inb(ioaddr->nsect_addr);
369 tf->lbal = inb(ioaddr->lbal_addr);
370 tf->lbam = inb(ioaddr->lbam_addr);
371 tf->lbah = inb(ioaddr->lbah_addr);
372 tf->device = inb(ioaddr->device_addr);
374 if (tf->flags & ATA_TFLAG_LBA48) {
375 outb(tf->ctl | ATA_HOB, ioaddr->ctl_addr);
376 tf->hob_feature = inb(ioaddr->error_addr);
377 tf->hob_nsect = inb(ioaddr->nsect_addr);
378 tf->hob_lbal = inb(ioaddr->lbal_addr);
379 tf->hob_lbam = inb(ioaddr->lbam_addr);
380 tf->hob_lbah = inb(ioaddr->lbah_addr);
385 * ata_tf_read_mmio - input device's ATA taskfile shadow registers
386 * @ap: Port from which input is read
387 * @tf: ATA taskfile register set for storing input
389 * Reads ATA taskfile registers for currently-selected device
393 * Inherited from caller.
396 static void ata_tf_read_mmio(struct ata_port *ap, struct ata_taskfile *tf)
398 struct ata_ioports *ioaddr = &ap->ioaddr;
400 tf->nsect = readb((void __iomem *)ioaddr->nsect_addr);
401 tf->lbal = readb((void __iomem *)ioaddr->lbal_addr);
402 tf->lbam = readb((void __iomem *)ioaddr->lbam_addr);
403 tf->lbah = readb((void __iomem *)ioaddr->lbah_addr);
404 tf->device = readb((void __iomem *)ioaddr->device_addr);
406 if (tf->flags & ATA_TFLAG_LBA48) {
407 writeb(tf->ctl | ATA_HOB, (void __iomem *) ap->ioaddr.ctl_addr);
408 tf->hob_feature = readb((void __iomem *)ioaddr->error_addr);
409 tf->hob_nsect = readb((void __iomem *)ioaddr->nsect_addr);
410 tf->hob_lbal = readb((void __iomem *)ioaddr->lbal_addr);
411 tf->hob_lbam = readb((void __iomem *)ioaddr->lbam_addr);
412 tf->hob_lbah = readb((void __iomem *)ioaddr->lbah_addr);
418 * ata_tf_read - input device's ATA taskfile shadow registers
419 * @ap: Port from which input is read
420 * @tf: ATA taskfile register set for storing input
422 * Reads ATA taskfile registers for currently-selected device
425 * Reads nsect, lbal, lbam, lbah, and device. If ATA_TFLAG_LBA48
426 * is set, also reads the hob registers.
428 * May be used as the tf_read() entry in ata_port_operations.
431 * Inherited from caller.
433 void ata_tf_read(struct ata_port *ap, struct ata_taskfile *tf)
435 if (ap->flags & ATA_FLAG_MMIO)
436 ata_tf_read_mmio(ap, tf);
438 ata_tf_read_pio(ap, tf);
442 * ata_check_status_pio - Read device status reg & clear interrupt
443 * @ap: port where the device is
445 * Reads ATA taskfile status register for currently-selected device
446 * and return its value. This also clears pending interrupts
450 * Inherited from caller.
452 static u8 ata_check_status_pio(struct ata_port *ap)
454 return inb(ap->ioaddr.status_addr);
458 * ata_check_status_mmio - Read device status reg & clear interrupt
459 * @ap: port where the device is
461 * Reads ATA taskfile status register for currently-selected device
462 * via MMIO and return its value. This also clears pending interrupts
466 * Inherited from caller.
468 static u8 ata_check_status_mmio(struct ata_port *ap)
470 return readb((void __iomem *) ap->ioaddr.status_addr);
475 * ata_check_status - Read device status reg & clear interrupt
476 * @ap: port where the device is
478 * Reads ATA taskfile status register for currently-selected device
479 * and return its value. This also clears pending interrupts
482 * May be used as the check_status() entry in ata_port_operations.
485 * Inherited from caller.
487 u8 ata_check_status(struct ata_port *ap)
489 if (ap->flags & ATA_FLAG_MMIO)
490 return ata_check_status_mmio(ap);
491 return ata_check_status_pio(ap);
496 * ata_altstatus - Read device alternate status reg
497 * @ap: port where the device is
499 * Reads ATA taskfile alternate status register for
500 * currently-selected device and return its value.
502 * Note: may NOT be used as the check_altstatus() entry in
503 * ata_port_operations.
506 * Inherited from caller.
508 u8 ata_altstatus(struct ata_port *ap)
510 if (ap->ops->check_altstatus)
511 return ap->ops->check_altstatus(ap);
513 if (ap->flags & ATA_FLAG_MMIO)
514 return readb((void __iomem *)ap->ioaddr.altstatus_addr);
515 return inb(ap->ioaddr.altstatus_addr);
520 * ata_chk_err - Read device error reg
521 * @ap: port where the device is
523 * Reads ATA taskfile error register for
524 * currently-selected device and return its value.
526 * Note: may NOT be used as the check_err() entry in
527 * ata_port_operations.
530 * Inherited from caller.
532 u8 ata_chk_err(struct ata_port *ap)
534 if (ap->ops->check_err)
535 return ap->ops->check_err(ap);
537 if (ap->flags & ATA_FLAG_MMIO) {
538 return readb((void __iomem *) ap->ioaddr.error_addr);
540 return inb(ap->ioaddr.error_addr);
544 * ata_tf_to_fis - Convert ATA taskfile to SATA FIS structure
545 * @tf: Taskfile to convert
546 * @fis: Buffer into which data will output
547 * @pmp: Port multiplier port
549 * Converts a standard ATA taskfile to a Serial ATA
550 * FIS structure (Register - Host to Device).
553 * Inherited from caller.
556 void ata_tf_to_fis(struct ata_taskfile *tf, u8 *fis, u8 pmp)
558 fis[0] = 0x27; /* Register - Host to Device FIS */
559 fis[1] = (pmp & 0xf) | (1 << 7); /* Port multiplier number,
560 bit 7 indicates Command FIS */
561 fis[2] = tf->command;
562 fis[3] = tf->feature;
569 fis[8] = tf->hob_lbal;
570 fis[9] = tf->hob_lbam;
571 fis[10] = tf->hob_lbah;
572 fis[11] = tf->hob_feature;
575 fis[13] = tf->hob_nsect;
586 * ata_tf_from_fis - Convert SATA FIS to ATA taskfile
587 * @fis: Buffer from which data will be input
588 * @tf: Taskfile to output
590 * Converts a standard ATA taskfile to a Serial ATA
591 * FIS structure (Register - Host to Device).
594 * Inherited from caller.
597 void ata_tf_from_fis(u8 *fis, struct ata_taskfile *tf)
599 tf->command = fis[2]; /* status */
600 tf->feature = fis[3]; /* error */
607 tf->hob_lbal = fis[8];
608 tf->hob_lbam = fis[9];
609 tf->hob_lbah = fis[10];
612 tf->hob_nsect = fis[13];
616 * ata_prot_to_cmd - determine which read/write opcodes to use
617 * @protocol: ATA_PROT_xxx taskfile protocol
618 * @lba48: true is lba48 is present
620 * Given necessary input, determine which read/write commands
621 * to use to transfer data.
626 static int ata_prot_to_cmd(int protocol, int lba48)
628 int rcmd = 0, wcmd = 0;
633 rcmd = ATA_CMD_PIO_READ_EXT;
634 wcmd = ATA_CMD_PIO_WRITE_EXT;
636 rcmd = ATA_CMD_PIO_READ;
637 wcmd = ATA_CMD_PIO_WRITE;
643 rcmd = ATA_CMD_READ_EXT;
644 wcmd = ATA_CMD_WRITE_EXT;
647 wcmd = ATA_CMD_WRITE;
655 return rcmd | (wcmd << 8);
659 * ata_dev_set_protocol - set taskfile protocol and r/w commands
660 * @dev: device to examine and configure
662 * Examine the device configuration, after we have
663 * read the identify-device page and configured the
664 * data transfer mode. Set internal state related to
665 * the ATA taskfile protocol (pio, pio mult, dma, etc.)
666 * and calculate the proper read/write commands to use.
671 static void ata_dev_set_protocol(struct ata_device *dev)
673 int pio = (dev->flags & ATA_DFLAG_PIO);
674 int lba48 = (dev->flags & ATA_DFLAG_LBA48);
678 proto = dev->xfer_protocol = ATA_PROT_PIO;
680 proto = dev->xfer_protocol = ATA_PROT_DMA;
682 cmd = ata_prot_to_cmd(proto, lba48);
686 dev->read_cmd = cmd & 0xff;
687 dev->write_cmd = (cmd >> 8) & 0xff;
690 static const char * xfer_mode_str[] = {
710 * ata_udma_string - convert UDMA bit offset to string
711 * @mask: mask of bits supported; only highest bit counts.
713 * Determine string which represents the highest speed
714 * (highest bit in @udma_mask).
720 * Constant C string representing highest speed listed in
721 * @udma_mask, or the constant C string "<n/a>".
724 static const char *ata_mode_string(unsigned int mask)
728 for (i = 7; i >= 0; i--)
731 for (i = ATA_SHIFT_MWDMA + 2; i >= ATA_SHIFT_MWDMA; i--)
734 for (i = ATA_SHIFT_PIO + 4; i >= ATA_SHIFT_PIO; i--)
741 return xfer_mode_str[i];
745 * ata_pio_devchk - PATA device presence detection
746 * @ap: ATA channel to examine
747 * @device: Device to examine (starting at zero)
749 * This technique was originally described in
750 * Hale Landis's ATADRVR (www.ata-atapi.com), and
751 * later found its way into the ATA/ATAPI spec.
753 * Write a pattern to the ATA shadow registers,
754 * and if a device is present, it will respond by
755 * correctly storing and echoing back the
756 * ATA shadow register contents.
762 static unsigned int ata_pio_devchk(struct ata_port *ap,
765 struct ata_ioports *ioaddr = &ap->ioaddr;
768 ap->ops->dev_select(ap, device);
770 outb(0x55, ioaddr->nsect_addr);
771 outb(0xaa, ioaddr->lbal_addr);
773 outb(0xaa, ioaddr->nsect_addr);
774 outb(0x55, ioaddr->lbal_addr);
776 outb(0x55, ioaddr->nsect_addr);
777 outb(0xaa, ioaddr->lbal_addr);
779 nsect = inb(ioaddr->nsect_addr);
780 lbal = inb(ioaddr->lbal_addr);
782 if ((nsect == 0x55) && (lbal == 0xaa))
783 return 1; /* we found a device */
785 return 0; /* nothing found */
789 * ata_mmio_devchk - PATA device presence detection
790 * @ap: ATA channel to examine
791 * @device: Device to examine (starting at zero)
793 * This technique was originally described in
794 * Hale Landis's ATADRVR (www.ata-atapi.com), and
795 * later found its way into the ATA/ATAPI spec.
797 * Write a pattern to the ATA shadow registers,
798 * and if a device is present, it will respond by
799 * correctly storing and echoing back the
800 * ATA shadow register contents.
806 static unsigned int ata_mmio_devchk(struct ata_port *ap,
809 struct ata_ioports *ioaddr = &ap->ioaddr;
812 ap->ops->dev_select(ap, device);
814 writeb(0x55, (void __iomem *) ioaddr->nsect_addr);
815 writeb(0xaa, (void __iomem *) ioaddr->lbal_addr);
817 writeb(0xaa, (void __iomem *) ioaddr->nsect_addr);
818 writeb(0x55, (void __iomem *) ioaddr->lbal_addr);
820 writeb(0x55, (void __iomem *) ioaddr->nsect_addr);
821 writeb(0xaa, (void __iomem *) ioaddr->lbal_addr);
823 nsect = readb((void __iomem *) ioaddr->nsect_addr);
824 lbal = readb((void __iomem *) ioaddr->lbal_addr);
826 if ((nsect == 0x55) && (lbal == 0xaa))
827 return 1; /* we found a device */
829 return 0; /* nothing found */
833 * ata_devchk - PATA device presence detection
834 * @ap: ATA channel to examine
835 * @device: Device to examine (starting at zero)
837 * Dispatch ATA device presence detection, depending
838 * on whether we are using PIO or MMIO to talk to the
839 * ATA shadow registers.
845 static unsigned int ata_devchk(struct ata_port *ap,
848 if (ap->flags & ATA_FLAG_MMIO)
849 return ata_mmio_devchk(ap, device);
850 return ata_pio_devchk(ap, device);
854 * ata_dev_classify - determine device type based on ATA-spec signature
855 * @tf: ATA taskfile register set for device to be identified
857 * Determine from taskfile register contents whether a device is
858 * ATA or ATAPI, as per "Signature and persistence" section
859 * of ATA/PI spec (volume 1, sect 5.14).
865 * Device type, %ATA_DEV_ATA, %ATA_DEV_ATAPI, or %ATA_DEV_UNKNOWN
866 * the event of failure.
869 unsigned int ata_dev_classify(struct ata_taskfile *tf)
871 /* Apple's open source Darwin code hints that some devices only
872 * put a proper signature into the LBA mid/high registers,
873 * So, we only check those. It's sufficient for uniqueness.
876 if (((tf->lbam == 0) && (tf->lbah == 0)) ||
877 ((tf->lbam == 0x3c) && (tf->lbah == 0xc3))) {
878 DPRINTK("found ATA device by sig\n");
882 if (((tf->lbam == 0x14) && (tf->lbah == 0xeb)) ||
883 ((tf->lbam == 0x69) && (tf->lbah == 0x96))) {
884 DPRINTK("found ATAPI device by sig\n");
885 return ATA_DEV_ATAPI;
888 DPRINTK("unknown device\n");
889 return ATA_DEV_UNKNOWN;
893 * ata_dev_try_classify - Parse returned ATA device signature
894 * @ap: ATA channel to examine
895 * @device: Device to examine (starting at zero)
897 * After an event -- SRST, E.D.D., or SATA COMRESET -- occurs,
898 * an ATA/ATAPI-defined set of values is placed in the ATA
899 * shadow registers, indicating the results of device detection
902 * Select the ATA device, and read the values from the ATA shadow
903 * registers. Then parse according to the Error register value,
904 * and the spec-defined values examined by ata_dev_classify().
910 static u8 ata_dev_try_classify(struct ata_port *ap, unsigned int device)
912 struct ata_device *dev = &ap->device[device];
913 struct ata_taskfile tf;
917 ap->ops->dev_select(ap, device);
919 memset(&tf, 0, sizeof(tf));
921 err = ata_chk_err(ap);
922 ap->ops->tf_read(ap, &tf);
924 dev->class = ATA_DEV_NONE;
926 /* see if device passed diags */
929 else if ((device == 0) && (err == 0x81))
934 /* determine if device if ATA or ATAPI */
935 class = ata_dev_classify(&tf);
936 if (class == ATA_DEV_UNKNOWN)
938 if ((class == ATA_DEV_ATA) && (ata_chk_status(ap) == 0))
947 * ata_dev_id_string - Convert IDENTIFY DEVICE page into string
948 * @id: IDENTIFY DEVICE results we will examine
949 * @s: string into which data is output
950 * @ofs: offset into identify device page
951 * @len: length of string to return. must be an even number.
953 * The strings in the IDENTIFY DEVICE page are broken up into
954 * 16-bit chunks. Run through the string, and output each
955 * 8-bit chunk linearly, regardless of platform.
961 void ata_dev_id_string(u16 *id, unsigned char *s,
962 unsigned int ofs, unsigned int len)
982 * ata_noop_dev_select - Select device 0/1 on ATA bus
983 * @ap: ATA channel to manipulate
984 * @device: ATA device (numbered from zero) to select
986 * This function performs no actual function.
988 * May be used as the dev_select() entry in ata_port_operations.
993 void ata_noop_dev_select (struct ata_port *ap, unsigned int device)
999 * ata_std_dev_select - Select device 0/1 on ATA bus
1000 * @ap: ATA channel to manipulate
1001 * @device: ATA device (numbered from zero) to select
1003 * Use the method defined in the ATA specification to
1004 * make either device 0, or device 1, active on the
1005 * ATA channel. Works with both PIO and MMIO.
1007 * May be used as the dev_select() entry in ata_port_operations.
1013 void ata_std_dev_select (struct ata_port *ap, unsigned int device)
1018 tmp = ATA_DEVICE_OBS;
1020 tmp = ATA_DEVICE_OBS | ATA_DEV1;
1022 if (ap->flags & ATA_FLAG_MMIO) {
1023 writeb(tmp, (void __iomem *) ap->ioaddr.device_addr);
1025 outb(tmp, ap->ioaddr.device_addr);
1027 ata_pause(ap); /* needed; also flushes, for mmio */
1031 * ata_dev_select - Select device 0/1 on ATA bus
1032 * @ap: ATA channel to manipulate
1033 * @device: ATA device (numbered from zero) to select
1034 * @wait: non-zero to wait for Status register BSY bit to clear
1035 * @can_sleep: non-zero if context allows sleeping
1037 * Use the method defined in the ATA specification to
1038 * make either device 0, or device 1, active on the
1041 * This is a high-level version of ata_std_dev_select(),
1042 * which additionally provides the services of inserting
1043 * the proper pauses and status polling, where needed.
1049 void ata_dev_select(struct ata_port *ap, unsigned int device,
1050 unsigned int wait, unsigned int can_sleep)
1052 VPRINTK("ENTER, ata%u: device %u, wait %u\n",
1053 ap->id, device, wait);
1058 ap->ops->dev_select(ap, device);
1061 if (can_sleep && ap->device[device].class == ATA_DEV_ATAPI)
1068 * ata_dump_id - IDENTIFY DEVICE info debugging output
1069 * @dev: Device whose IDENTIFY DEVICE page we will dump
1071 * Dump selected 16-bit words from a detected device's
1072 * IDENTIFY PAGE page.
1078 static inline void ata_dump_id(struct ata_device *dev)
1080 DPRINTK("49==0x%04x "
1090 DPRINTK("80==0x%04x "
1100 DPRINTK("88==0x%04x "
1107 * ata_dev_identify - obtain IDENTIFY x DEVICE page
1108 * @ap: port on which device we wish to probe resides
1109 * @device: device bus address, starting at zero
1111 * Following bus reset, we issue the IDENTIFY [PACKET] DEVICE
1112 * command, and read back the 512-byte device information page.
1113 * The device information page is fed to us via the standard
1114 * PIO-IN protocol, but we hand-code it here. (TODO: investigate
1115 * using standard PIO-IN paths)
1117 * After reading the device information page, we use several
1118 * bits of information from it to initialize data structures
1119 * that will be used during the lifetime of the ata_device.
1120 * Other data from the info page is used to disqualify certain
1121 * older ATA devices we do not wish to support.
1124 * Inherited from caller. Some functions called by this function
1125 * obtain the host_set lock.
1128 static void ata_dev_identify(struct ata_port *ap, unsigned int device)
1130 struct ata_device *dev = &ap->device[device];
1131 unsigned int major_version;
1133 unsigned long xfer_modes;
1135 unsigned int using_edd;
1136 DECLARE_COMPLETION(wait);
1137 struct ata_queued_cmd *qc;
1138 unsigned long flags;
1141 if (!ata_dev_present(dev)) {
1142 DPRINTK("ENTER/EXIT (host %u, dev %u) -- nodev\n",
1147 if (ap->flags & (ATA_FLAG_SRST | ATA_FLAG_SATA_RESET))
1152 DPRINTK("ENTER, host %u, dev %u\n", ap->id, device);
1154 assert (dev->class == ATA_DEV_ATA || dev->class == ATA_DEV_ATAPI ||
1155 dev->class == ATA_DEV_NONE);
1157 ata_dev_select(ap, device, 1, 1); /* select device 0/1 */
1159 qc = ata_qc_new_init(ap, dev);
1162 ata_sg_init_one(qc, dev->id, sizeof(dev->id));
1163 qc->dma_dir = DMA_FROM_DEVICE;
1164 qc->tf.protocol = ATA_PROT_PIO;
1168 if (dev->class == ATA_DEV_ATA) {
1169 qc->tf.command = ATA_CMD_ID_ATA;
1170 DPRINTK("do ATA identify\n");
1172 qc->tf.command = ATA_CMD_ID_ATAPI;
1173 DPRINTK("do ATAPI identify\n");
1176 qc->waiting = &wait;
1177 qc->complete_fn = ata_qc_complete_noop;
1179 spin_lock_irqsave(&ap->host_set->lock, flags);
1180 rc = ata_qc_issue(qc);
1181 spin_unlock_irqrestore(&ap->host_set->lock, flags);
1186 wait_for_completion(&wait);
1188 status = ata_chk_status(ap);
1189 if (status & ATA_ERR) {
1191 * arg! EDD works for all test cases, but seems to return
1192 * the ATA signature for some ATAPI devices. Until the
1193 * reason for this is found and fixed, we fix up the mess
1194 * here. If IDENTIFY DEVICE returns command aborted
1195 * (as ATAPI devices do), then we issue an
1196 * IDENTIFY PACKET DEVICE.
1198 * ATA software reset (SRST, the default) does not appear
1199 * to have this problem.
1201 if ((using_edd) && (qc->tf.command == ATA_CMD_ID_ATA)) {
1202 u8 err = ata_chk_err(ap);
1203 if (err & ATA_ABORTED) {
1204 dev->class = ATA_DEV_ATAPI;
1215 swap_buf_le16(dev->id, ATA_ID_WORDS);
1217 /* print device capabilities */
1218 printk(KERN_DEBUG "ata%u: dev %u cfg "
1219 "49:%04x 82:%04x 83:%04x 84:%04x 85:%04x 86:%04x 87:%04x 88:%04x\n",
1220 ap->id, device, dev->id[49],
1221 dev->id[82], dev->id[83], dev->id[84],
1222 dev->id[85], dev->id[86], dev->id[87],
1226 * common ATA, ATAPI feature tests
1229 /* we require DMA support (bits 8 of word 49) */
1230 if (!ata_id_has_dma(dev->id)) {
1231 printk(KERN_DEBUG "ata%u: no dma\n", ap->id);
1235 /* quick-n-dirty find max transfer mode; for printk only */
1236 xfer_modes = dev->id[ATA_ID_UDMA_MODES];
1238 xfer_modes = (dev->id[ATA_ID_MWDMA_MODES]) << ATA_SHIFT_MWDMA;
1240 xfer_modes = (dev->id[ATA_ID_PIO_MODES]) << (ATA_SHIFT_PIO + 3);
1241 xfer_modes |= (0x7 << ATA_SHIFT_PIO);
1246 /* ATA-specific feature tests */
1247 if (dev->class == ATA_DEV_ATA) {
1248 if (!ata_id_is_ata(dev->id)) /* sanity check */
1251 /* get major version */
1252 tmp = dev->id[ATA_ID_MAJOR_VER];
1253 for (major_version = 14; major_version >= 1; major_version--)
1254 if (tmp & (1 << major_version))
1258 * The exact sequence expected by certain pre-ATA4 drives is:
1261 * INITIALIZE DEVICE PARAMETERS
1263 * Some drives were very specific about that exact sequence.
1265 if (major_version < 4 || (!ata_id_has_lba(dev->id)))
1266 ata_dev_init_params(ap, dev);
1268 if (ata_id_has_lba(dev->id)) {
1269 dev->flags |= ATA_DFLAG_LBA;
1271 if (ata_id_has_lba48(dev->id)) {
1272 dev->flags |= ATA_DFLAG_LBA48;
1273 dev->n_sectors = ata_id_u64(dev->id, 100);
1275 dev->n_sectors = ata_id_u32(dev->id, 60);
1278 /* print device info to dmesg */
1279 printk(KERN_INFO "ata%u: dev %u ATA-%d, max %s, %Lu sectors:%s\n",
1282 ata_mode_string(xfer_modes),
1283 (unsigned long long)dev->n_sectors,
1284 dev->flags & ATA_DFLAG_LBA48 ? " LBA48" : " LBA");
1288 /* Default translation */
1289 dev->cylinders = dev->id[1];
1290 dev->heads = dev->id[3];
1291 dev->sectors = dev->id[6];
1292 dev->n_sectors = dev->cylinders * dev->heads * dev->sectors;
1294 if (ata_id_current_chs_valid(dev->id)) {
1295 /* Current CHS translation is valid. */
1296 dev->cylinders = dev->id[54];
1297 dev->heads = dev->id[55];
1298 dev->sectors = dev->id[56];
1300 dev->n_sectors = ata_id_u32(dev->id, 57);
1303 /* print device info to dmesg */
1304 printk(KERN_INFO "ata%u: dev %u ATA-%d, max %s, %Lu sectors: CHS %d/%d/%d\n",
1307 ata_mode_string(xfer_modes),
1308 (unsigned long long)dev->n_sectors,
1309 (int)dev->cylinders, (int)dev->heads, (int)dev->sectors);
1313 ap->host->max_cmd_len = 16;
1316 /* ATAPI-specific feature tests */
1318 if (ata_id_is_ata(dev->id)) /* sanity check */
1321 rc = atapi_cdb_len(dev->id);
1322 if ((rc < 12) || (rc > ATAPI_CDB_LEN)) {
1323 printk(KERN_WARNING "ata%u: unsupported CDB len\n", ap->id);
1326 ap->cdb_len = (unsigned int) rc;
1327 ap->host->max_cmd_len = (unsigned char) ap->cdb_len;
1329 /* print device info to dmesg */
1330 printk(KERN_INFO "ata%u: dev %u ATAPI, max %s\n",
1332 ata_mode_string(xfer_modes));
1335 DPRINTK("EXIT, drv_stat = 0x%x\n", ata_chk_status(ap));
1339 printk(KERN_WARNING "ata%u: dev %u not supported, ignoring\n",
1342 dev->class++; /* converts ATA_DEV_xxx into ATA_DEV_xxx_UNSUP */
1343 DPRINTK("EXIT, err\n");
1347 static inline u8 ata_dev_knobble(struct ata_port *ap)
1349 return ((ap->cbl == ATA_CBL_SATA) && (!ata_id_is_sata(ap->device->id)));
1353 * ata_dev_config - Run device specific handlers and check for
1354 * SATA->PATA bridges
1361 void ata_dev_config(struct ata_port *ap, unsigned int i)
1363 /* limit bridge transfers to udma5, 200 sectors */
1364 if (ata_dev_knobble(ap)) {
1365 printk(KERN_INFO "ata%u(%u): applying bridge limits\n",
1366 ap->id, ap->device->devno);
1367 ap->udma_mask &= ATA_UDMA5;
1368 ap->host->max_sectors = ATA_MAX_SECTORS;
1369 ap->host->hostt->max_sectors = ATA_MAX_SECTORS;
1370 ap->device->flags |= ATA_DFLAG_LOCK_SECTORS;
1373 if (ap->ops->dev_config)
1374 ap->ops->dev_config(ap, &ap->device[i]);
1378 * ata_bus_probe - Reset and probe ATA bus
1381 * Master ATA bus probing function. Initiates a hardware-dependent
1382 * bus reset, then attempts to identify any devices found on
1386 * PCI/etc. bus probe sem.
1389 * Zero on success, non-zero on error.
1392 static int ata_bus_probe(struct ata_port *ap)
1394 unsigned int i, found = 0;
1396 ap->ops->phy_reset(ap);
1397 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1400 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1401 ata_dev_identify(ap, i);
1402 if (ata_dev_present(&ap->device[i])) {
1404 ata_dev_config(ap,i);
1408 if ((!found) || (ap->flags & ATA_FLAG_PORT_DISABLED))
1409 goto err_out_disable;
1412 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1413 goto err_out_disable;
1418 ap->ops->port_disable(ap);
1424 * ata_port_probe - Mark port as enabled
1425 * @ap: Port for which we indicate enablement
1427 * Modify @ap data structure such that the system
1428 * thinks that the entire port is enabled.
1430 * LOCKING: host_set lock, or some other form of
1434 void ata_port_probe(struct ata_port *ap)
1436 ap->flags &= ~ATA_FLAG_PORT_DISABLED;
1440 * __sata_phy_reset - Wake/reset a low-level SATA PHY
1441 * @ap: SATA port associated with target SATA PHY.
1443 * This function issues commands to standard SATA Sxxx
1444 * PHY registers, to wake up the phy (and device), and
1445 * clear any reset condition.
1448 * PCI/etc. bus probe sem.
1451 void __sata_phy_reset(struct ata_port *ap)
1454 unsigned long timeout = jiffies + (HZ * 5);
1456 if (ap->flags & ATA_FLAG_SATA_RESET) {
1457 /* issue phy wake/reset */
1458 scr_write_flush(ap, SCR_CONTROL, 0x301);
1459 /* Couldn't find anything in SATA I/II specs, but
1460 * AHCI-1.1 10.4.2 says at least 1 ms. */
1463 scr_write_flush(ap, SCR_CONTROL, 0x300); /* phy wake/clear reset */
1465 /* wait for phy to become ready, if necessary */
1468 sstatus = scr_read(ap, SCR_STATUS);
1469 if ((sstatus & 0xf) != 1)
1471 } while (time_before(jiffies, timeout));
1473 /* TODO: phy layer with polling, timeouts, etc. */
1474 if (sata_dev_present(ap))
1477 sstatus = scr_read(ap, SCR_STATUS);
1478 printk(KERN_INFO "ata%u: no device found (phy stat %08x)\n",
1480 ata_port_disable(ap);
1483 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1486 if (ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT)) {
1487 ata_port_disable(ap);
1491 ap->cbl = ATA_CBL_SATA;
1495 * sata_phy_reset - Reset SATA bus.
1496 * @ap: SATA port associated with target SATA PHY.
1498 * This function resets the SATA bus, and then probes
1499 * the bus for devices.
1502 * PCI/etc. bus probe sem.
1505 void sata_phy_reset(struct ata_port *ap)
1507 __sata_phy_reset(ap);
1508 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1514 * ata_port_disable - Disable port.
1515 * @ap: Port to be disabled.
1517 * Modify @ap data structure such that the system
1518 * thinks that the entire port is disabled, and should
1519 * never attempt to probe or communicate with devices
1522 * LOCKING: host_set lock, or some other form of
1526 void ata_port_disable(struct ata_port *ap)
1528 ap->device[0].class = ATA_DEV_NONE;
1529 ap->device[1].class = ATA_DEV_NONE;
1530 ap->flags |= ATA_FLAG_PORT_DISABLED;
1536 } xfer_mode_classes[] = {
1537 { ATA_SHIFT_UDMA, XFER_UDMA_0 },
1538 { ATA_SHIFT_MWDMA, XFER_MW_DMA_0 },
1539 { ATA_SHIFT_PIO, XFER_PIO_0 },
1542 static inline u8 base_from_shift(unsigned int shift)
1546 for (i = 0; i < ARRAY_SIZE(xfer_mode_classes); i++)
1547 if (xfer_mode_classes[i].shift == shift)
1548 return xfer_mode_classes[i].base;
1553 static void ata_dev_set_mode(struct ata_port *ap, struct ata_device *dev)
1558 if (!ata_dev_present(dev) || (ap->flags & ATA_FLAG_PORT_DISABLED))
1561 if (dev->xfer_shift == ATA_SHIFT_PIO)
1562 dev->flags |= ATA_DFLAG_PIO;
1564 ata_dev_set_xfermode(ap, dev);
1566 base = base_from_shift(dev->xfer_shift);
1567 ofs = dev->xfer_mode - base;
1568 idx = ofs + dev->xfer_shift;
1569 WARN_ON(idx >= ARRAY_SIZE(xfer_mode_str));
1571 DPRINTK("idx=%d xfer_shift=%u, xfer_mode=0x%x, base=0x%x, offset=%d\n",
1572 idx, dev->xfer_shift, (int)dev->xfer_mode, (int)base, ofs);
1574 printk(KERN_INFO "ata%u: dev %u configured for %s\n",
1575 ap->id, dev->devno, xfer_mode_str[idx]);
1578 static int ata_host_set_pio(struct ata_port *ap)
1584 mask = ata_get_mode_mask(ap, ATA_SHIFT_PIO);
1587 printk(KERN_WARNING "ata%u: no PIO support\n", ap->id);
1591 base = base_from_shift(ATA_SHIFT_PIO);
1592 xfer_mode = base + x;
1594 DPRINTK("base 0x%x xfer_mode 0x%x mask 0x%x x %d\n",
1595 (int)base, (int)xfer_mode, mask, x);
1597 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1598 struct ata_device *dev = &ap->device[i];
1599 if (ata_dev_present(dev)) {
1600 dev->pio_mode = xfer_mode;
1601 dev->xfer_mode = xfer_mode;
1602 dev->xfer_shift = ATA_SHIFT_PIO;
1603 if (ap->ops->set_piomode)
1604 ap->ops->set_piomode(ap, dev);
1611 static void ata_host_set_dma(struct ata_port *ap, u8 xfer_mode,
1612 unsigned int xfer_shift)
1616 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1617 struct ata_device *dev = &ap->device[i];
1618 if (ata_dev_present(dev)) {
1619 dev->dma_mode = xfer_mode;
1620 dev->xfer_mode = xfer_mode;
1621 dev->xfer_shift = xfer_shift;
1622 if (ap->ops->set_dmamode)
1623 ap->ops->set_dmamode(ap, dev);
1629 * ata_set_mode - Program timings and issue SET FEATURES - XFER
1630 * @ap: port on which timings will be programmed
1632 * Set ATA device disk transfer mode (PIO3, UDMA6, etc.).
1635 * PCI/etc. bus probe sem.
1638 static void ata_set_mode(struct ata_port *ap)
1640 unsigned int i, xfer_shift;
1644 /* step 1: always set host PIO timings */
1645 rc = ata_host_set_pio(ap);
1649 /* step 2: choose the best data xfer mode */
1650 xfer_mode = xfer_shift = 0;
1651 rc = ata_choose_xfer_mode(ap, &xfer_mode, &xfer_shift);
1655 /* step 3: if that xfer mode isn't PIO, set host DMA timings */
1656 if (xfer_shift != ATA_SHIFT_PIO)
1657 ata_host_set_dma(ap, xfer_mode, xfer_shift);
1659 /* step 4: update devices' xfer mode */
1660 ata_dev_set_mode(ap, &ap->device[0]);
1661 ata_dev_set_mode(ap, &ap->device[1]);
1663 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1666 if (ap->ops->post_set_mode)
1667 ap->ops->post_set_mode(ap);
1669 for (i = 0; i < 2; i++) {
1670 struct ata_device *dev = &ap->device[i];
1671 ata_dev_set_protocol(dev);
1677 ata_port_disable(ap);
1681 * ata_busy_sleep - sleep until BSY clears, or timeout
1682 * @ap: port containing status register to be polled
1683 * @tmout_pat: impatience timeout
1684 * @tmout: overall timeout
1686 * Sleep until ATA Status register bit BSY clears,
1687 * or a timeout occurs.
1693 static unsigned int ata_busy_sleep (struct ata_port *ap,
1694 unsigned long tmout_pat,
1695 unsigned long tmout)
1697 unsigned long timer_start, timeout;
1700 status = ata_busy_wait(ap, ATA_BUSY, 300);
1701 timer_start = jiffies;
1702 timeout = timer_start + tmout_pat;
1703 while ((status & ATA_BUSY) && (time_before(jiffies, timeout))) {
1705 status = ata_busy_wait(ap, ATA_BUSY, 3);
1708 if (status & ATA_BUSY)
1709 printk(KERN_WARNING "ata%u is slow to respond, "
1710 "please be patient\n", ap->id);
1712 timeout = timer_start + tmout;
1713 while ((status & ATA_BUSY) && (time_before(jiffies, timeout))) {
1715 status = ata_chk_status(ap);
1718 if (status & ATA_BUSY) {
1719 printk(KERN_ERR "ata%u failed to respond (%lu secs)\n",
1720 ap->id, tmout / HZ);
1727 static void ata_bus_post_reset(struct ata_port *ap, unsigned int devmask)
1729 struct ata_ioports *ioaddr = &ap->ioaddr;
1730 unsigned int dev0 = devmask & (1 << 0);
1731 unsigned int dev1 = devmask & (1 << 1);
1732 unsigned long timeout;
1734 /* if device 0 was found in ata_devchk, wait for its
1738 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
1740 /* if device 1 was found in ata_devchk, wait for
1741 * register access, then wait for BSY to clear
1743 timeout = jiffies + ATA_TMOUT_BOOT;
1747 ap->ops->dev_select(ap, 1);
1748 if (ap->flags & ATA_FLAG_MMIO) {
1749 nsect = readb((void __iomem *) ioaddr->nsect_addr);
1750 lbal = readb((void __iomem *) ioaddr->lbal_addr);
1752 nsect = inb(ioaddr->nsect_addr);
1753 lbal = inb(ioaddr->lbal_addr);
1755 if ((nsect == 1) && (lbal == 1))
1757 if (time_after(jiffies, timeout)) {
1761 msleep(50); /* give drive a breather */
1764 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
1766 /* is all this really necessary? */
1767 ap->ops->dev_select(ap, 0);
1769 ap->ops->dev_select(ap, 1);
1771 ap->ops->dev_select(ap, 0);
1775 * ata_bus_edd - Issue EXECUTE DEVICE DIAGNOSTIC command.
1776 * @ap: Port to reset and probe
1778 * Use the EXECUTE DEVICE DIAGNOSTIC command to reset and
1779 * probe the bus. Not often used these days.
1782 * PCI/etc. bus probe sem.
1786 static unsigned int ata_bus_edd(struct ata_port *ap)
1788 struct ata_taskfile tf;
1790 /* set up execute-device-diag (bus reset) taskfile */
1791 /* also, take interrupts to a known state (disabled) */
1792 DPRINTK("execute-device-diag\n");
1793 ata_tf_init(ap, &tf, 0);
1795 tf.command = ATA_CMD_EDD;
1796 tf.protocol = ATA_PROT_NODATA;
1799 ata_tf_to_host(ap, &tf);
1801 /* spec says at least 2ms. but who knows with those
1802 * crazy ATAPI devices...
1806 return ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
1809 static unsigned int ata_bus_softreset(struct ata_port *ap,
1810 unsigned int devmask)
1812 struct ata_ioports *ioaddr = &ap->ioaddr;
1814 DPRINTK("ata%u: bus reset via SRST\n", ap->id);
1816 /* software reset. causes dev0 to be selected */
1817 if (ap->flags & ATA_FLAG_MMIO) {
1818 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
1819 udelay(20); /* FIXME: flush */
1820 writeb(ap->ctl | ATA_SRST, (void __iomem *) ioaddr->ctl_addr);
1821 udelay(20); /* FIXME: flush */
1822 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
1824 outb(ap->ctl, ioaddr->ctl_addr);
1826 outb(ap->ctl | ATA_SRST, ioaddr->ctl_addr);
1828 outb(ap->ctl, ioaddr->ctl_addr);
1831 /* spec mandates ">= 2ms" before checking status.
1832 * We wait 150ms, because that was the magic delay used for
1833 * ATAPI devices in Hale Landis's ATADRVR, for the period of time
1834 * between when the ATA command register is written, and then
1835 * status is checked. Because waiting for "a while" before
1836 * checking status is fine, post SRST, we perform this magic
1837 * delay here as well.
1841 ata_bus_post_reset(ap, devmask);
1847 * ata_bus_reset - reset host port and associated ATA channel
1848 * @ap: port to reset
1850 * This is typically the first time we actually start issuing
1851 * commands to the ATA channel. We wait for BSY to clear, then
1852 * issue EXECUTE DEVICE DIAGNOSTIC command, polling for its
1853 * result. Determine what devices, if any, are on the channel
1854 * by looking at the device 0/1 error register. Look at the signature
1855 * stored in each device's taskfile registers, to determine if
1856 * the device is ATA or ATAPI.
1859 * PCI/etc. bus probe sem.
1860 * Obtains host_set lock.
1863 * Sets ATA_FLAG_PORT_DISABLED if bus reset fails.
1866 void ata_bus_reset(struct ata_port *ap)
1868 struct ata_ioports *ioaddr = &ap->ioaddr;
1869 unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS;
1871 unsigned int dev0, dev1 = 0, rc = 0, devmask = 0;
1873 DPRINTK("ENTER, host %u, port %u\n", ap->id, ap->port_no);
1875 /* determine if device 0/1 are present */
1876 if (ap->flags & ATA_FLAG_SATA_RESET)
1879 dev0 = ata_devchk(ap, 0);
1881 dev1 = ata_devchk(ap, 1);
1885 devmask |= (1 << 0);
1887 devmask |= (1 << 1);
1889 /* select device 0 again */
1890 ap->ops->dev_select(ap, 0);
1892 /* issue bus reset */
1893 if (ap->flags & ATA_FLAG_SRST)
1894 rc = ata_bus_softreset(ap, devmask);
1895 else if ((ap->flags & ATA_FLAG_SATA_RESET) == 0) {
1896 /* set up device control */
1897 if (ap->flags & ATA_FLAG_MMIO)
1898 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
1900 outb(ap->ctl, ioaddr->ctl_addr);
1901 rc = ata_bus_edd(ap);
1908 * determine by signature whether we have ATA or ATAPI devices
1910 err = ata_dev_try_classify(ap, 0);
1911 if ((slave_possible) && (err != 0x81))
1912 ata_dev_try_classify(ap, 1);
1914 /* re-enable interrupts */
1915 if (ap->ioaddr.ctl_addr) /* FIXME: hack. create a hook instead */
1918 /* is double-select really necessary? */
1919 if (ap->device[1].class != ATA_DEV_NONE)
1920 ap->ops->dev_select(ap, 1);
1921 if (ap->device[0].class != ATA_DEV_NONE)
1922 ap->ops->dev_select(ap, 0);
1924 /* if no devices were detected, disable this port */
1925 if ((ap->device[0].class == ATA_DEV_NONE) &&
1926 (ap->device[1].class == ATA_DEV_NONE))
1929 if (ap->flags & (ATA_FLAG_SATA_RESET | ATA_FLAG_SRST)) {
1930 /* set up device control for ATA_FLAG_SATA_RESET */
1931 if (ap->flags & ATA_FLAG_MMIO)
1932 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
1934 outb(ap->ctl, ioaddr->ctl_addr);
1941 printk(KERN_ERR "ata%u: disabling port\n", ap->id);
1942 ap->ops->port_disable(ap);
1947 static void ata_pr_blacklisted(struct ata_port *ap, struct ata_device *dev)
1949 printk(KERN_WARNING "ata%u: dev %u is on DMA blacklist, disabling DMA\n",
1950 ap->id, dev->devno);
1953 static const char * ata_dma_blacklist [] = {
1972 "Toshiba CD-ROM XM-6202B",
1973 "TOSHIBA CD-ROM XM-1702BC",
1975 "E-IDE CD-ROM CR-840",
1978 "SAMSUNG CD-ROM SC-148C",
1979 "SAMSUNG CD-ROM SC",
1981 "ATAPI CD-ROM DRIVE 40X MAXIMUM",
1985 static int ata_dma_blacklisted(struct ata_port *ap, struct ata_device *dev)
1987 unsigned char model_num[40];
1992 ata_dev_id_string(dev->id, model_num, ATA_ID_PROD_OFS,
1995 len = strnlen(s, sizeof(model_num));
1997 /* ATAPI specifies that empty space is blank-filled; remove blanks */
1998 while ((len > 0) && (s[len - 1] == ' ')) {
2003 for (i = 0; i < ARRAY_SIZE(ata_dma_blacklist); i++)
2004 if (!strncmp(ata_dma_blacklist[i], s, len))
2010 static unsigned int ata_get_mode_mask(struct ata_port *ap, int shift)
2012 struct ata_device *master, *slave;
2015 master = &ap->device[0];
2016 slave = &ap->device[1];
2018 assert (ata_dev_present(master) || ata_dev_present(slave));
2020 if (shift == ATA_SHIFT_UDMA) {
2021 mask = ap->udma_mask;
2022 if (ata_dev_present(master)) {
2023 mask &= (master->id[ATA_ID_UDMA_MODES] & 0xff);
2024 if (ata_dma_blacklisted(ap, master)) {
2026 ata_pr_blacklisted(ap, master);
2029 if (ata_dev_present(slave)) {
2030 mask &= (slave->id[ATA_ID_UDMA_MODES] & 0xff);
2031 if (ata_dma_blacklisted(ap, slave)) {
2033 ata_pr_blacklisted(ap, slave);
2037 else if (shift == ATA_SHIFT_MWDMA) {
2038 mask = ap->mwdma_mask;
2039 if (ata_dev_present(master)) {
2040 mask &= (master->id[ATA_ID_MWDMA_MODES] & 0x07);
2041 if (ata_dma_blacklisted(ap, master)) {
2043 ata_pr_blacklisted(ap, master);
2046 if (ata_dev_present(slave)) {
2047 mask &= (slave->id[ATA_ID_MWDMA_MODES] & 0x07);
2048 if (ata_dma_blacklisted(ap, slave)) {
2050 ata_pr_blacklisted(ap, slave);
2054 else if (shift == ATA_SHIFT_PIO) {
2055 mask = ap->pio_mask;
2056 if (ata_dev_present(master)) {
2057 /* spec doesn't return explicit support for
2058 * PIO0-2, so we fake it
2060 u16 tmp_mode = master->id[ATA_ID_PIO_MODES] & 0x03;
2065 if (ata_dev_present(slave)) {
2066 /* spec doesn't return explicit support for
2067 * PIO0-2, so we fake it
2069 u16 tmp_mode = slave->id[ATA_ID_PIO_MODES] & 0x03;
2076 mask = 0xffffffff; /* shut up compiler warning */
2083 /* find greatest bit */
2084 static int fgb(u32 bitmap)
2089 for (i = 0; i < 32; i++)
2090 if (bitmap & (1 << i))
2097 * ata_choose_xfer_mode - attempt to find best transfer mode
2098 * @ap: Port for which an xfer mode will be selected
2099 * @xfer_mode_out: (output) SET FEATURES - XFER MODE code
2100 * @xfer_shift_out: (output) bit shift that selects this mode
2102 * Based on host and device capabilities, determine the
2103 * maximum transfer mode that is amenable to all.
2106 * PCI/etc. bus probe sem.
2109 * Zero on success, negative on error.
2112 static int ata_choose_xfer_mode(struct ata_port *ap,
2114 unsigned int *xfer_shift_out)
2116 unsigned int mask, shift;
2119 for (i = 0; i < ARRAY_SIZE(xfer_mode_classes); i++) {
2120 shift = xfer_mode_classes[i].shift;
2121 mask = ata_get_mode_mask(ap, shift);
2125 *xfer_mode_out = xfer_mode_classes[i].base + x;
2126 *xfer_shift_out = shift;
2135 * ata_dev_set_xfermode - Issue SET FEATURES - XFER MODE command
2136 * @ap: Port associated with device @dev
2137 * @dev: Device to which command will be sent
2139 * Issue SET FEATURES - XFER MODE command to device @dev
2143 * PCI/etc. bus probe sem.
2146 static void ata_dev_set_xfermode(struct ata_port *ap, struct ata_device *dev)
2148 DECLARE_COMPLETION(wait);
2149 struct ata_queued_cmd *qc;
2151 unsigned long flags;
2153 /* set up set-features taskfile */
2154 DPRINTK("set features - xfer mode\n");
2156 qc = ata_qc_new_init(ap, dev);
2159 qc->tf.command = ATA_CMD_SET_FEATURES;
2160 qc->tf.feature = SETFEATURES_XFER;
2161 qc->tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
2162 qc->tf.protocol = ATA_PROT_NODATA;
2163 qc->tf.nsect = dev->xfer_mode;
2165 qc->waiting = &wait;
2166 qc->complete_fn = ata_qc_complete_noop;
2168 spin_lock_irqsave(&ap->host_set->lock, flags);
2169 rc = ata_qc_issue(qc);
2170 spin_unlock_irqrestore(&ap->host_set->lock, flags);
2173 ata_port_disable(ap);
2175 wait_for_completion(&wait);
2181 * ata_dev_init_params - Issue INIT DEV PARAMS command
2182 * @ap: Port associated with device @dev
2183 * @dev: Device to which command will be sent
2188 static void ata_dev_init_params(struct ata_port *ap, struct ata_device *dev)
2190 DECLARE_COMPLETION(wait);
2191 struct ata_queued_cmd *qc;
2193 unsigned long flags;
2194 u16 sectors = dev->id[6];
2195 u16 heads = dev->id[3];
2197 /* Number of sectors per track 1-255. Number of heads 1-16 */
2198 if (sectors < 1 || sectors > 255 || heads < 1 || heads > 16)
2201 /* set up init dev params taskfile */
2202 DPRINTK("init dev params \n");
2204 qc = ata_qc_new_init(ap, dev);
2207 qc->tf.command = ATA_CMD_INIT_DEV_PARAMS;
2208 qc->tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
2209 qc->tf.protocol = ATA_PROT_NODATA;
2210 qc->tf.nsect = sectors;
2211 qc->tf.device |= (heads - 1) & 0x0f; /* max head = num. of heads - 1 */
2213 qc->waiting = &wait;
2214 qc->complete_fn = ata_qc_complete_noop;
2216 spin_lock_irqsave(&ap->host_set->lock, flags);
2217 rc = ata_qc_issue(qc);
2218 spin_unlock_irqrestore(&ap->host_set->lock, flags);
2221 ata_port_disable(ap);
2223 wait_for_completion(&wait);
2229 * ata_sg_clean - Unmap DMA memory associated with command
2230 * @qc: Command containing DMA memory to be released
2232 * Unmap all mapped DMA memory associated with this command.
2235 * spin_lock_irqsave(host_set lock)
2238 static void ata_sg_clean(struct ata_queued_cmd *qc)
2240 struct ata_port *ap = qc->ap;
2241 struct scatterlist *sg = qc->sg;
2242 int dir = qc->dma_dir;
2244 assert(qc->flags & ATA_QCFLAG_DMAMAP);
2247 if (qc->flags & ATA_QCFLAG_SINGLE)
2248 assert(qc->n_elem == 1);
2250 DPRINTK("unmapping %u sg elements\n", qc->n_elem);
2252 if (qc->flags & ATA_QCFLAG_SG)
2253 dma_unmap_sg(ap->host_set->dev, sg, qc->n_elem, dir);
2255 dma_unmap_single(ap->host_set->dev, sg_dma_address(&sg[0]),
2256 sg_dma_len(&sg[0]), dir);
2258 qc->flags &= ~ATA_QCFLAG_DMAMAP;
2263 * ata_fill_sg - Fill PCI IDE PRD table
2264 * @qc: Metadata associated with taskfile to be transferred
2266 * Fill PCI IDE PRD (scatter-gather) table with segments
2267 * associated with the current disk command.
2270 * spin_lock_irqsave(host_set lock)
2273 static void ata_fill_sg(struct ata_queued_cmd *qc)
2275 struct scatterlist *sg = qc->sg;
2276 struct ata_port *ap = qc->ap;
2277 unsigned int idx, nelem;
2280 assert(qc->n_elem > 0);
2283 for (nelem = qc->n_elem; nelem; nelem--,sg++) {
2287 /* determine if physical DMA addr spans 64K boundary.
2288 * Note h/w doesn't support 64-bit, so we unconditionally
2289 * truncate dma_addr_t to u32.
2291 addr = (u32) sg_dma_address(sg);
2292 sg_len = sg_dma_len(sg);
2295 offset = addr & 0xffff;
2297 if ((offset + sg_len) > 0x10000)
2298 len = 0x10000 - offset;
2300 ap->prd[idx].addr = cpu_to_le32(addr);
2301 ap->prd[idx].flags_len = cpu_to_le32(len & 0xffff);
2302 VPRINTK("PRD[%u] = (0x%X, 0x%X)\n", idx, addr, len);
2311 ap->prd[idx - 1].flags_len |= cpu_to_le32(ATA_PRD_EOT);
2314 * ata_check_atapi_dma - Check whether ATAPI DMA can be supported
2315 * @qc: Metadata associated with taskfile to check
2317 * Allow low-level driver to filter ATA PACKET commands, returning
2318 * a status indicating whether or not it is OK to use DMA for the
2319 * supplied PACKET command.
2322 * spin_lock_irqsave(host_set lock)
2324 * RETURNS: 0 when ATAPI DMA can be used
2327 int ata_check_atapi_dma(struct ata_queued_cmd *qc)
2329 struct ata_port *ap = qc->ap;
2330 int rc = 0; /* Assume ATAPI DMA is OK by default */
2332 if (ap->ops->check_atapi_dma)
2333 rc = ap->ops->check_atapi_dma(qc);
2338 * ata_qc_prep - Prepare taskfile for submission
2339 * @qc: Metadata associated with taskfile to be prepared
2341 * Prepare ATA taskfile for submission.
2344 * spin_lock_irqsave(host_set lock)
2346 void ata_qc_prep(struct ata_queued_cmd *qc)
2348 if (!(qc->flags & ATA_QCFLAG_DMAMAP))
2355 * ata_sg_init_one - Associate command with memory buffer
2356 * @qc: Command to be associated
2357 * @buf: Memory buffer
2358 * @buflen: Length of memory buffer, in bytes.
2360 * Initialize the data-related elements of queued_cmd @qc
2361 * to point to a single memory buffer, @buf of byte length @buflen.
2364 * spin_lock_irqsave(host_set lock)
2367 void ata_sg_init_one(struct ata_queued_cmd *qc, void *buf, unsigned int buflen)
2369 struct scatterlist *sg;
2371 qc->flags |= ATA_QCFLAG_SINGLE;
2373 memset(&qc->sgent, 0, sizeof(qc->sgent));
2374 qc->sg = &qc->sgent;
2379 sg->page = virt_to_page(buf);
2380 sg->offset = (unsigned long) buf & ~PAGE_MASK;
2381 sg->length = buflen;
2385 * ata_sg_init - Associate command with scatter-gather table.
2386 * @qc: Command to be associated
2387 * @sg: Scatter-gather table.
2388 * @n_elem: Number of elements in s/g table.
2390 * Initialize the data-related elements of queued_cmd @qc
2391 * to point to a scatter-gather table @sg, containing @n_elem
2395 * spin_lock_irqsave(host_set lock)
2398 void ata_sg_init(struct ata_queued_cmd *qc, struct scatterlist *sg,
2399 unsigned int n_elem)
2401 qc->flags |= ATA_QCFLAG_SG;
2403 qc->n_elem = n_elem;
2407 * ata_sg_setup_one - DMA-map the memory buffer associated with a command.
2408 * @qc: Command with memory buffer to be mapped.
2410 * DMA-map the memory buffer associated with queued_cmd @qc.
2413 * spin_lock_irqsave(host_set lock)
2416 * Zero on success, negative on error.
2419 static int ata_sg_setup_one(struct ata_queued_cmd *qc)
2421 struct ata_port *ap = qc->ap;
2422 int dir = qc->dma_dir;
2423 struct scatterlist *sg = qc->sg;
2424 dma_addr_t dma_address;
2426 dma_address = dma_map_single(ap->host_set->dev, qc->buf_virt,
2428 if (dma_mapping_error(dma_address))
2431 sg_dma_address(sg) = dma_address;
2432 sg_dma_len(sg) = sg->length;
2434 DPRINTK("mapped buffer of %d bytes for %s\n", sg_dma_len(sg),
2435 qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
2441 * ata_sg_setup - DMA-map the scatter-gather table associated with a command.
2442 * @qc: Command with scatter-gather table to be mapped.
2444 * DMA-map the scatter-gather table associated with queued_cmd @qc.
2447 * spin_lock_irqsave(host_set lock)
2450 * Zero on success, negative on error.
2454 static int ata_sg_setup(struct ata_queued_cmd *qc)
2456 struct ata_port *ap = qc->ap;
2457 struct scatterlist *sg = qc->sg;
2460 VPRINTK("ENTER, ata%u\n", ap->id);
2461 assert(qc->flags & ATA_QCFLAG_SG);
2464 n_elem = dma_map_sg(ap->host_set->dev, sg, qc->n_elem, dir);
2468 DPRINTK("%d sg elements mapped\n", n_elem);
2470 qc->n_elem = n_elem;
2476 * ata_poll_qc_complete - turn irq back on and finish qc
2477 * @qc: Command to complete
2478 * @drv_stat: ATA status register content
2481 * None. (grabs host lock)
2484 void ata_poll_qc_complete(struct ata_queued_cmd *qc, u8 drv_stat)
2486 struct ata_port *ap = qc->ap;
2487 unsigned long flags;
2489 spin_lock_irqsave(&ap->host_set->lock, flags);
2490 ap->flags &= ~ATA_FLAG_NOINTR;
2492 ata_qc_complete(qc, drv_stat);
2493 spin_unlock_irqrestore(&ap->host_set->lock, flags);
2501 * None. (executing in kernel thread context)
2507 static unsigned long ata_pio_poll(struct ata_port *ap)
2510 unsigned int poll_state = PIO_ST_UNKNOWN;
2511 unsigned int reg_state = PIO_ST_UNKNOWN;
2512 const unsigned int tmout_state = PIO_ST_TMOUT;
2514 switch (ap->pio_task_state) {
2517 poll_state = PIO_ST_POLL;
2521 case PIO_ST_LAST_POLL:
2522 poll_state = PIO_ST_LAST_POLL;
2523 reg_state = PIO_ST_LAST;
2530 status = ata_chk_status(ap);
2531 if (status & ATA_BUSY) {
2532 if (time_after(jiffies, ap->pio_task_timeout)) {
2533 ap->pio_task_state = tmout_state;
2536 ap->pio_task_state = poll_state;
2537 return ATA_SHORT_PAUSE;
2540 ap->pio_task_state = reg_state;
2545 * ata_pio_complete -
2549 * None. (executing in kernel thread context)
2552 static void ata_pio_complete (struct ata_port *ap)
2554 struct ata_queued_cmd *qc;
2558 * This is purely heuristic. This is a fast path. Sometimes when
2559 * we enter, BSY will be cleared in a chk-status or two. If not,
2560 * the drive is probably seeking or something. Snooze for a couple
2561 * msecs, then chk-status again. If still busy, fall back to
2562 * PIO_ST_POLL state.
2564 drv_stat = ata_busy_wait(ap, ATA_BUSY | ATA_DRQ, 10);
2565 if (drv_stat & (ATA_BUSY | ATA_DRQ)) {
2567 drv_stat = ata_busy_wait(ap, ATA_BUSY | ATA_DRQ, 10);
2568 if (drv_stat & (ATA_BUSY | ATA_DRQ)) {
2569 ap->pio_task_state = PIO_ST_LAST_POLL;
2570 ap->pio_task_timeout = jiffies + ATA_TMOUT_PIO;
2575 drv_stat = ata_wait_idle(ap);
2576 if (!ata_ok(drv_stat)) {
2577 ap->pio_task_state = PIO_ST_ERR;
2581 qc = ata_qc_from_tag(ap, ap->active_tag);
2584 ap->pio_task_state = PIO_ST_IDLE;
2586 ata_poll_qc_complete(qc, drv_stat);
2592 * @buf: Buffer to swap
2593 * @buf_words: Number of 16-bit words in buffer.
2595 * Swap halves of 16-bit words if needed to convert from
2596 * little-endian byte order to native cpu byte order, or
2601 void swap_buf_le16(u16 *buf, unsigned int buf_words)
2606 for (i = 0; i < buf_words; i++)
2607 buf[i] = le16_to_cpu(buf[i]);
2608 #endif /* __BIG_ENDIAN */
2612 * ata_mmio_data_xfer - Transfer data by MMIO
2613 * @ap: port to read/write
2615 * @buflen: buffer length
2616 * @do_write: read/write
2618 * Transfer data from/to the device data register by MMIO.
2621 * Inherited from caller.
2625 static void ata_mmio_data_xfer(struct ata_port *ap, unsigned char *buf,
2626 unsigned int buflen, int write_data)
2629 unsigned int words = buflen >> 1;
2630 u16 *buf16 = (u16 *) buf;
2631 void __iomem *mmio = (void __iomem *)ap->ioaddr.data_addr;
2633 /* Transfer multiple of 2 bytes */
2635 for (i = 0; i < words; i++)
2636 writew(le16_to_cpu(buf16[i]), mmio);
2638 for (i = 0; i < words; i++)
2639 buf16[i] = cpu_to_le16(readw(mmio));
2642 /* Transfer trailing 1 byte, if any. */
2643 if (unlikely(buflen & 0x01)) {
2644 u16 align_buf[1] = { 0 };
2645 unsigned char *trailing_buf = buf + buflen - 1;
2648 memcpy(align_buf, trailing_buf, 1);
2649 writew(le16_to_cpu(align_buf[0]), mmio);
2651 align_buf[0] = cpu_to_le16(readw(mmio));
2652 memcpy(trailing_buf, align_buf, 1);
2658 * ata_pio_data_xfer - Transfer data by PIO
2659 * @ap: port to read/write
2661 * @buflen: buffer length
2662 * @do_write: read/write
2664 * Transfer data from/to the device data register by PIO.
2667 * Inherited from caller.
2671 static void ata_pio_data_xfer(struct ata_port *ap, unsigned char *buf,
2672 unsigned int buflen, int write_data)
2674 unsigned int words = buflen >> 1;
2676 /* Transfer multiple of 2 bytes */
2678 outsw(ap->ioaddr.data_addr, buf, words);
2680 insw(ap->ioaddr.data_addr, buf, words);
2682 /* Transfer trailing 1 byte, if any. */
2683 if (unlikely(buflen & 0x01)) {
2684 u16 align_buf[1] = { 0 };
2685 unsigned char *trailing_buf = buf + buflen - 1;
2688 memcpy(align_buf, trailing_buf, 1);
2689 outw(le16_to_cpu(align_buf[0]), ap->ioaddr.data_addr);
2691 align_buf[0] = cpu_to_le16(inw(ap->ioaddr.data_addr));
2692 memcpy(trailing_buf, align_buf, 1);
2698 * ata_data_xfer - Transfer data from/to the data register.
2699 * @ap: port to read/write
2701 * @buflen: buffer length
2702 * @do_write: read/write
2704 * Transfer data from/to the device data register.
2707 * Inherited from caller.
2711 static void ata_data_xfer(struct ata_port *ap, unsigned char *buf,
2712 unsigned int buflen, int do_write)
2714 if (ap->flags & ATA_FLAG_MMIO)
2715 ata_mmio_data_xfer(ap, buf, buflen, do_write);
2717 ata_pio_data_xfer(ap, buf, buflen, do_write);
2721 * ata_pio_sector - Transfer ATA_SECT_SIZE (512 bytes) of data.
2722 * @qc: Command on going
2724 * Transfer ATA_SECT_SIZE of data from/to the ATA device.
2727 * Inherited from caller.
2730 static void ata_pio_sector(struct ata_queued_cmd *qc)
2732 int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
2733 struct scatterlist *sg = qc->sg;
2734 struct ata_port *ap = qc->ap;
2736 unsigned int offset;
2739 if (qc->cursect == (qc->nsect - 1))
2740 ap->pio_task_state = PIO_ST_LAST;
2742 page = sg[qc->cursg].page;
2743 offset = sg[qc->cursg].offset + qc->cursg_ofs * ATA_SECT_SIZE;
2745 /* get the current page and offset */
2746 page = nth_page(page, (offset >> PAGE_SHIFT));
2747 offset %= PAGE_SIZE;
2749 buf = kmap(page) + offset;
2754 if ((qc->cursg_ofs * ATA_SECT_SIZE) == (&sg[qc->cursg])->length) {
2759 DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
2761 /* do the actual data transfer */
2762 do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
2763 ata_data_xfer(ap, buf, ATA_SECT_SIZE, do_write);
2769 * __atapi_pio_bytes - Transfer data from/to the ATAPI device.
2770 * @qc: Command on going
2771 * @bytes: number of bytes
2773 * Transfer Transfer data from/to the ATAPI device.
2776 * Inherited from caller.
2780 static void __atapi_pio_bytes(struct ata_queued_cmd *qc, unsigned int bytes)
2782 int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
2783 struct scatterlist *sg = qc->sg;
2784 struct ata_port *ap = qc->ap;
2787 unsigned int offset, count;
2789 if (qc->curbytes + bytes >= qc->nbytes)
2790 ap->pio_task_state = PIO_ST_LAST;
2793 if (unlikely(qc->cursg >= qc->n_elem)) {
2795 * The end of qc->sg is reached and the device expects
2796 * more data to transfer. In order not to overrun qc->sg
2797 * and fulfill length specified in the byte count register,
2798 * - for read case, discard trailing data from the device
2799 * - for write case, padding zero data to the device
2801 u16 pad_buf[1] = { 0 };
2802 unsigned int words = bytes >> 1;
2805 if (words) /* warning if bytes > 1 */
2806 printk(KERN_WARNING "ata%u: %u bytes trailing data\n",
2809 for (i = 0; i < words; i++)
2810 ata_data_xfer(ap, (unsigned char*)pad_buf, 2, do_write);
2812 ap->pio_task_state = PIO_ST_LAST;
2816 sg = &qc->sg[qc->cursg];
2819 offset = sg->offset + qc->cursg_ofs;
2821 /* get the current page and offset */
2822 page = nth_page(page, (offset >> PAGE_SHIFT));
2823 offset %= PAGE_SIZE;
2825 /* don't overrun current sg */
2826 count = min(sg->length - qc->cursg_ofs, bytes);
2828 /* don't cross page boundaries */
2829 count = min(count, (unsigned int)PAGE_SIZE - offset);
2831 buf = kmap(page) + offset;
2834 qc->curbytes += count;
2835 qc->cursg_ofs += count;
2837 if (qc->cursg_ofs == sg->length) {
2842 DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
2844 /* do the actual data transfer */
2845 ata_data_xfer(ap, buf, count, do_write);
2854 * atapi_pio_bytes - Transfer data from/to the ATAPI device.
2855 * @qc: Command on going
2857 * Transfer Transfer data from/to the ATAPI device.
2860 * Inherited from caller.
2864 static void atapi_pio_bytes(struct ata_queued_cmd *qc)
2866 struct ata_port *ap = qc->ap;
2867 struct ata_device *dev = qc->dev;
2868 unsigned int ireason, bc_lo, bc_hi, bytes;
2869 int i_write, do_write = (qc->tf.flags & ATA_TFLAG_WRITE) ? 1 : 0;
2871 ap->ops->tf_read(ap, &qc->tf);
2872 ireason = qc->tf.nsect;
2873 bc_lo = qc->tf.lbam;
2874 bc_hi = qc->tf.lbah;
2875 bytes = (bc_hi << 8) | bc_lo;
2877 /* shall be cleared to zero, indicating xfer of data */
2878 if (ireason & (1 << 0))
2881 /* make sure transfer direction matches expected */
2882 i_write = ((ireason & (1 << 1)) == 0) ? 1 : 0;
2883 if (do_write != i_write)
2886 __atapi_pio_bytes(qc, bytes);
2891 printk(KERN_INFO "ata%u: dev %u: ATAPI check failed\n",
2892 ap->id, dev->devno);
2893 ap->pio_task_state = PIO_ST_ERR;
2901 * None. (executing in kernel thread context)
2904 static void ata_pio_block(struct ata_port *ap)
2906 struct ata_queued_cmd *qc;
2910 * This is purely hueristic. This is a fast path.
2911 * Sometimes when we enter, BSY will be cleared in
2912 * a chk-status or two. If not, the drive is probably seeking
2913 * or something. Snooze for a couple msecs, then
2914 * chk-status again. If still busy, fall back to
2915 * PIO_ST_POLL state.
2917 status = ata_busy_wait(ap, ATA_BUSY, 5);
2918 if (status & ATA_BUSY) {
2920 status = ata_busy_wait(ap, ATA_BUSY, 10);
2921 if (status & ATA_BUSY) {
2922 ap->pio_task_state = PIO_ST_POLL;
2923 ap->pio_task_timeout = jiffies + ATA_TMOUT_PIO;
2928 qc = ata_qc_from_tag(ap, ap->active_tag);
2931 if (is_atapi_taskfile(&qc->tf)) {
2932 /* no more data to transfer or unsupported ATAPI command */
2933 if ((status & ATA_DRQ) == 0) {
2934 ap->pio_task_state = PIO_ST_IDLE;
2936 ata_poll_qc_complete(qc, status);
2940 atapi_pio_bytes(qc);
2942 /* handle BSY=0, DRQ=0 as error */
2943 if ((status & ATA_DRQ) == 0) {
2944 ap->pio_task_state = PIO_ST_ERR;
2952 static void ata_pio_error(struct ata_port *ap)
2954 struct ata_queued_cmd *qc;
2957 qc = ata_qc_from_tag(ap, ap->active_tag);
2960 drv_stat = ata_chk_status(ap);
2961 printk(KERN_WARNING "ata%u: PIO error, drv_stat 0x%x\n",
2964 ap->pio_task_state = PIO_ST_IDLE;
2966 ata_poll_qc_complete(qc, drv_stat | ATA_ERR);
2969 static void ata_pio_task(void *_data)
2971 struct ata_port *ap = _data;
2972 unsigned long timeout = 0;
2974 switch (ap->pio_task_state) {
2983 ata_pio_complete(ap);
2987 case PIO_ST_LAST_POLL:
2988 timeout = ata_pio_poll(ap);
2998 queue_delayed_work(ata_wq, &ap->pio_task,
3001 queue_work(ata_wq, &ap->pio_task);
3004 static void atapi_request_sense(struct ata_port *ap, struct ata_device *dev,
3005 struct scsi_cmnd *cmd)
3007 DECLARE_COMPLETION(wait);
3008 struct ata_queued_cmd *qc;
3009 unsigned long flags;
3012 DPRINTK("ATAPI request sense\n");
3014 qc = ata_qc_new_init(ap, dev);
3017 /* FIXME: is this needed? */
3018 memset(cmd->sense_buffer, 0, sizeof(cmd->sense_buffer));
3020 ata_sg_init_one(qc, cmd->sense_buffer, sizeof(cmd->sense_buffer));
3021 qc->dma_dir = DMA_FROM_DEVICE;
3023 memset(&qc->cdb, 0, ap->cdb_len);
3024 qc->cdb[0] = REQUEST_SENSE;
3025 qc->cdb[4] = SCSI_SENSE_BUFFERSIZE;
3027 qc->tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
3028 qc->tf.command = ATA_CMD_PACKET;
3030 qc->tf.protocol = ATA_PROT_ATAPI;
3031 qc->tf.lbam = (8 * 1024) & 0xff;
3032 qc->tf.lbah = (8 * 1024) >> 8;
3033 qc->nbytes = SCSI_SENSE_BUFFERSIZE;
3035 qc->waiting = &wait;
3036 qc->complete_fn = ata_qc_complete_noop;
3038 spin_lock_irqsave(&ap->host_set->lock, flags);
3039 rc = ata_qc_issue(qc);
3040 spin_unlock_irqrestore(&ap->host_set->lock, flags);
3043 ata_port_disable(ap);
3045 wait_for_completion(&wait);
3051 * ata_qc_timeout - Handle timeout of queued command
3052 * @qc: Command that timed out
3054 * Some part of the kernel (currently, only the SCSI layer)
3055 * has noticed that the active command on port @ap has not
3056 * completed after a specified length of time. Handle this
3057 * condition by disabling DMA (if necessary) and completing
3058 * transactions, with error if necessary.
3060 * This also handles the case of the "lost interrupt", where
3061 * for some reason (possibly hardware bug, possibly driver bug)
3062 * an interrupt was not delivered to the driver, even though the
3063 * transaction completed successfully.
3066 * Inherited from SCSI layer (none, can sleep)
3069 static void ata_qc_timeout(struct ata_queued_cmd *qc)
3071 struct ata_port *ap = qc->ap;
3072 struct ata_host_set *host_set = ap->host_set;
3073 struct ata_device *dev = qc->dev;
3074 u8 host_stat = 0, drv_stat;
3075 unsigned long flags;
3079 /* FIXME: doesn't this conflict with timeout handling? */
3080 if (qc->dev->class == ATA_DEV_ATAPI && qc->scsicmd) {
3081 struct scsi_cmnd *cmd = qc->scsicmd;
3083 if (!(cmd->eh_eflags & SCSI_EH_CANCEL_CMD)) {
3085 /* finish completing original command */
3086 spin_lock_irqsave(&host_set->lock, flags);
3087 __ata_qc_complete(qc);
3088 spin_unlock_irqrestore(&host_set->lock, flags);
3090 atapi_request_sense(ap, dev, cmd);
3092 cmd->result = (CHECK_CONDITION << 1) | (DID_OK << 16);
3093 scsi_finish_command(cmd);
3099 spin_lock_irqsave(&host_set->lock, flags);
3101 /* hack alert! We cannot use the supplied completion
3102 * function from inside the ->eh_strategy_handler() thread.
3103 * libata is the only user of ->eh_strategy_handler() in
3104 * any kernel, so the default scsi_done() assumes it is
3105 * not being called from the SCSI EH.
3107 qc->scsidone = scsi_finish_command;
3109 switch (qc->tf.protocol) {
3112 case ATA_PROT_ATAPI_DMA:
3113 host_stat = ap->ops->bmdma_status(ap);
3115 /* before we do anything else, clear DMA-Start bit */
3116 ap->ops->bmdma_stop(qc);
3122 drv_stat = ata_chk_status(ap);
3124 /* ack bmdma irq events */
3125 ap->ops->irq_clear(ap);
3127 printk(KERN_ERR "ata%u: command 0x%x timeout, stat 0x%x host_stat 0x%x\n",
3128 ap->id, qc->tf.command, drv_stat, host_stat);
3130 /* complete taskfile transaction */
3131 ata_qc_complete(qc, drv_stat);
3135 spin_unlock_irqrestore(&host_set->lock, flags);
3142 * ata_eng_timeout - Handle timeout of queued command
3143 * @ap: Port on which timed-out command is active
3145 * Some part of the kernel (currently, only the SCSI layer)
3146 * has noticed that the active command on port @ap has not
3147 * completed after a specified length of time. Handle this
3148 * condition by disabling DMA (if necessary) and completing
3149 * transactions, with error if necessary.
3151 * This also handles the case of the "lost interrupt", where
3152 * for some reason (possibly hardware bug, possibly driver bug)
3153 * an interrupt was not delivered to the driver, even though the
3154 * transaction completed successfully.
3157 * Inherited from SCSI layer (none, can sleep)
3160 void ata_eng_timeout(struct ata_port *ap)
3162 struct ata_queued_cmd *qc;
3166 qc = ata_qc_from_tag(ap, ap->active_tag);
3168 printk(KERN_ERR "ata%u: BUG: timeout without command\n",
3180 * ata_qc_new - Request an available ATA command, for queueing
3181 * @ap: Port associated with device @dev
3182 * @dev: Device from whom we request an available command structure
3188 static struct ata_queued_cmd *ata_qc_new(struct ata_port *ap)
3190 struct ata_queued_cmd *qc = NULL;
3193 for (i = 0; i < ATA_MAX_QUEUE; i++)
3194 if (!test_and_set_bit(i, &ap->qactive)) {
3195 qc = ata_qc_from_tag(ap, i);
3206 * ata_qc_new_init - Request an available ATA command, and initialize it
3207 * @ap: Port associated with device @dev
3208 * @dev: Device from whom we request an available command structure
3214 struct ata_queued_cmd *ata_qc_new_init(struct ata_port *ap,
3215 struct ata_device *dev)
3217 struct ata_queued_cmd *qc;
3219 qc = ata_qc_new(ap);
3226 qc->cursect = qc->cursg = qc->cursg_ofs = 0;
3228 qc->nbytes = qc->curbytes = 0;
3230 ata_tf_init(ap, &qc->tf, dev->devno);
3232 if (dev->flags & ATA_DFLAG_LBA) {
3233 qc->tf.flags |= ATA_TFLAG_LBA;
3235 if (dev->flags & ATA_DFLAG_LBA48)
3236 qc->tf.flags |= ATA_TFLAG_LBA48;
3243 static int ata_qc_complete_noop(struct ata_queued_cmd *qc, u8 drv_stat)
3248 static void __ata_qc_complete(struct ata_queued_cmd *qc)
3250 struct ata_port *ap = qc->ap;
3251 unsigned int tag, do_clear = 0;
3255 if (likely(ata_tag_valid(tag))) {
3256 if (tag == ap->active_tag)
3257 ap->active_tag = ATA_TAG_POISON;
3258 qc->tag = ATA_TAG_POISON;
3263 struct completion *waiting = qc->waiting;
3268 if (likely(do_clear))
3269 clear_bit(tag, &ap->qactive);
3273 * ata_qc_free - free unused ata_queued_cmd
3274 * @qc: Command to complete
3276 * Designed to free unused ata_queued_cmd object
3277 * in case something prevents using it.
3280 * spin_lock_irqsave(host_set lock)
3283 void ata_qc_free(struct ata_queued_cmd *qc)
3285 assert(qc != NULL); /* ata_qc_from_tag _might_ return NULL */
3286 assert(qc->waiting == NULL); /* nothing should be waiting */
3288 __ata_qc_complete(qc);
3292 * ata_qc_complete - Complete an active ATA command
3293 * @qc: Command to complete
3294 * @drv_stat: ATA Status register contents
3296 * Indicate to the mid and upper layers that an ATA
3297 * command has completed, with either an ok or not-ok status.
3300 * spin_lock_irqsave(host_set lock)
3304 void ata_qc_complete(struct ata_queued_cmd *qc, u8 drv_stat)
3308 assert(qc != NULL); /* ata_qc_from_tag _might_ return NULL */
3309 assert(qc->flags & ATA_QCFLAG_ACTIVE);
3311 if (likely(qc->flags & ATA_QCFLAG_DMAMAP))
3314 /* atapi: mark qc as inactive to prevent the interrupt handler
3315 * from completing the command twice later, before the error handler
3316 * is called. (when rc != 0 and atapi request sense is needed)
3318 qc->flags &= ~ATA_QCFLAG_ACTIVE;
3320 /* call completion callback */
3321 rc = qc->complete_fn(qc, drv_stat);
3323 /* if callback indicates not to complete command (non-zero),
3324 * return immediately
3329 __ata_qc_complete(qc);
3334 static inline int ata_should_dma_map(struct ata_queued_cmd *qc)
3336 struct ata_port *ap = qc->ap;
3338 switch (qc->tf.protocol) {
3340 case ATA_PROT_ATAPI_DMA:
3343 case ATA_PROT_ATAPI:
3345 case ATA_PROT_PIO_MULT:
3346 if (ap->flags & ATA_FLAG_PIO_DMA)
3359 * ata_qc_issue - issue taskfile to device
3360 * @qc: command to issue to device
3362 * Prepare an ATA command to submission to device.
3363 * This includes mapping the data into a DMA-able
3364 * area, filling in the S/G table, and finally
3365 * writing the taskfile to hardware, starting the command.
3368 * spin_lock_irqsave(host_set lock)
3371 * Zero on success, negative on error.
3374 int ata_qc_issue(struct ata_queued_cmd *qc)
3376 struct ata_port *ap = qc->ap;
3378 if (ata_should_dma_map(qc)) {
3379 if (qc->flags & ATA_QCFLAG_SG) {
3380 if (ata_sg_setup(qc))
3382 } else if (qc->flags & ATA_QCFLAG_SINGLE) {
3383 if (ata_sg_setup_one(qc))
3387 qc->flags &= ~ATA_QCFLAG_DMAMAP;
3390 ap->ops->qc_prep(qc);
3392 qc->ap->active_tag = qc->tag;
3393 qc->flags |= ATA_QCFLAG_ACTIVE;
3395 return ap->ops->qc_issue(qc);
3403 * ata_qc_issue_prot - issue taskfile to device in proto-dependent manner
3404 * @qc: command to issue to device
3406 * Using various libata functions and hooks, this function
3407 * starts an ATA command. ATA commands are grouped into
3408 * classes called "protocols", and issuing each type of protocol
3409 * is slightly different.
3411 * May be used as the qc_issue() entry in ata_port_operations.
3414 * spin_lock_irqsave(host_set lock)
3417 * Zero on success, negative on error.
3420 int ata_qc_issue_prot(struct ata_queued_cmd *qc)
3422 struct ata_port *ap = qc->ap;
3424 ata_dev_select(ap, qc->dev->devno, 1, 0);
3426 switch (qc->tf.protocol) {
3427 case ATA_PROT_NODATA:
3428 ata_tf_to_host_nolock(ap, &qc->tf);
3432 ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
3433 ap->ops->bmdma_setup(qc); /* set up bmdma */
3434 ap->ops->bmdma_start(qc); /* initiate bmdma */
3437 case ATA_PROT_PIO: /* load tf registers, initiate polling pio */
3438 ata_qc_set_polling(qc);
3439 ata_tf_to_host_nolock(ap, &qc->tf);
3440 ap->pio_task_state = PIO_ST;
3441 queue_work(ata_wq, &ap->pio_task);
3444 case ATA_PROT_ATAPI:
3445 ata_qc_set_polling(qc);
3446 ata_tf_to_host_nolock(ap, &qc->tf);
3447 queue_work(ata_wq, &ap->packet_task);
3450 case ATA_PROT_ATAPI_NODATA:
3451 ap->flags |= ATA_FLAG_NOINTR;
3452 ata_tf_to_host_nolock(ap, &qc->tf);
3453 queue_work(ata_wq, &ap->packet_task);
3456 case ATA_PROT_ATAPI_DMA:
3457 ap->flags |= ATA_FLAG_NOINTR;
3458 ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
3459 ap->ops->bmdma_setup(qc); /* set up bmdma */
3460 queue_work(ata_wq, &ap->packet_task);
3472 * ata_bmdma_setup_mmio - Set up PCI IDE BMDMA transaction
3473 * @qc: Info associated with this ATA transaction.
3476 * spin_lock_irqsave(host_set lock)
3479 static void ata_bmdma_setup_mmio (struct ata_queued_cmd *qc)
3481 struct ata_port *ap = qc->ap;
3482 unsigned int rw = (qc->tf.flags & ATA_TFLAG_WRITE);
3484 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
3486 /* load PRD table addr. */
3487 mb(); /* make sure PRD table writes are visible to controller */
3488 writel(ap->prd_dma, mmio + ATA_DMA_TABLE_OFS);
3490 /* specify data direction, triple-check start bit is clear */
3491 dmactl = readb(mmio + ATA_DMA_CMD);
3492 dmactl &= ~(ATA_DMA_WR | ATA_DMA_START);
3494 dmactl |= ATA_DMA_WR;
3495 writeb(dmactl, mmio + ATA_DMA_CMD);
3497 /* issue r/w command */
3498 ap->ops->exec_command(ap, &qc->tf);
3502 * ata_bmdma_start_mmio - Start a PCI IDE BMDMA transaction
3503 * @qc: Info associated with this ATA transaction.
3506 * spin_lock_irqsave(host_set lock)
3509 static void ata_bmdma_start_mmio (struct ata_queued_cmd *qc)
3511 struct ata_port *ap = qc->ap;
3512 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
3515 /* start host DMA transaction */
3516 dmactl = readb(mmio + ATA_DMA_CMD);
3517 writeb(dmactl | ATA_DMA_START, mmio + ATA_DMA_CMD);
3519 /* Strictly, one may wish to issue a readb() here, to
3520 * flush the mmio write. However, control also passes
3521 * to the hardware at this point, and it will interrupt
3522 * us when we are to resume control. So, in effect,
3523 * we don't care when the mmio write flushes.
3524 * Further, a read of the DMA status register _immediately_
3525 * following the write may not be what certain flaky hardware
3526 * is expected, so I think it is best to not add a readb()
3527 * without first all the MMIO ATA cards/mobos.
3528 * Or maybe I'm just being paranoid.
3533 * ata_bmdma_setup_pio - Set up PCI IDE BMDMA transaction (PIO)
3534 * @qc: Info associated with this ATA transaction.
3537 * spin_lock_irqsave(host_set lock)
3540 static void ata_bmdma_setup_pio (struct ata_queued_cmd *qc)
3542 struct ata_port *ap = qc->ap;
3543 unsigned int rw = (qc->tf.flags & ATA_TFLAG_WRITE);
3546 /* load PRD table addr. */
3547 outl(ap->prd_dma, ap->ioaddr.bmdma_addr + ATA_DMA_TABLE_OFS);
3549 /* specify data direction, triple-check start bit is clear */
3550 dmactl = inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3551 dmactl &= ~(ATA_DMA_WR | ATA_DMA_START);
3553 dmactl |= ATA_DMA_WR;
3554 outb(dmactl, ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3556 /* issue r/w command */
3557 ap->ops->exec_command(ap, &qc->tf);
3561 * ata_bmdma_start_pio - Start a PCI IDE BMDMA transaction (PIO)
3562 * @qc: Info associated with this ATA transaction.
3565 * spin_lock_irqsave(host_set lock)
3568 static void ata_bmdma_start_pio (struct ata_queued_cmd *qc)
3570 struct ata_port *ap = qc->ap;
3573 /* start host DMA transaction */
3574 dmactl = inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3575 outb(dmactl | ATA_DMA_START,
3576 ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3581 * ata_bmdma_start - Start a PCI IDE BMDMA transaction
3582 * @qc: Info associated with this ATA transaction.
3584 * Writes the ATA_DMA_START flag to the DMA command register.
3586 * May be used as the bmdma_start() entry in ata_port_operations.
3589 * spin_lock_irqsave(host_set lock)
3591 void ata_bmdma_start(struct ata_queued_cmd *qc)
3593 if (qc->ap->flags & ATA_FLAG_MMIO)
3594 ata_bmdma_start_mmio(qc);
3596 ata_bmdma_start_pio(qc);
3601 * ata_bmdma_setup - Set up PCI IDE BMDMA transaction
3602 * @qc: Info associated with this ATA transaction.
3604 * Writes address of PRD table to device's PRD Table Address
3605 * register, sets the DMA control register, and calls
3606 * ops->exec_command() to start the transfer.
3608 * May be used as the bmdma_setup() entry in ata_port_operations.
3611 * spin_lock_irqsave(host_set lock)
3613 void ata_bmdma_setup(struct ata_queued_cmd *qc)
3615 if (qc->ap->flags & ATA_FLAG_MMIO)
3616 ata_bmdma_setup_mmio(qc);
3618 ata_bmdma_setup_pio(qc);
3623 * ata_bmdma_irq_clear - Clear PCI IDE BMDMA interrupt.
3624 * @ap: Port associated with this ATA transaction.
3626 * Clear interrupt and error flags in DMA status register.
3628 * May be used as the irq_clear() entry in ata_port_operations.
3631 * spin_lock_irqsave(host_set lock)
3634 void ata_bmdma_irq_clear(struct ata_port *ap)
3636 if (ap->flags & ATA_FLAG_MMIO) {
3637 void __iomem *mmio = ((void __iomem *) ap->ioaddr.bmdma_addr) + ATA_DMA_STATUS;
3638 writeb(readb(mmio), mmio);
3640 unsigned long addr = ap->ioaddr.bmdma_addr + ATA_DMA_STATUS;
3641 outb(inb(addr), addr);
3648 * ata_bmdma_status - Read PCI IDE BMDMA status
3649 * @ap: Port associated with this ATA transaction.
3651 * Read and return BMDMA status register.
3653 * May be used as the bmdma_status() entry in ata_port_operations.
3656 * spin_lock_irqsave(host_set lock)
3659 u8 ata_bmdma_status(struct ata_port *ap)
3662 if (ap->flags & ATA_FLAG_MMIO) {
3663 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
3664 host_stat = readb(mmio + ATA_DMA_STATUS);
3666 host_stat = inb(ap->ioaddr.bmdma_addr + ATA_DMA_STATUS);
3672 * ata_bmdma_stop - Stop PCI IDE BMDMA transfer
3673 * @qc: Command we are ending DMA for
3675 * Clears the ATA_DMA_START flag in the dma control register
3677 * May be used as the bmdma_stop() entry in ata_port_operations.
3680 * spin_lock_irqsave(host_set lock)
3683 void ata_bmdma_stop(struct ata_queued_cmd *qc)
3685 struct ata_port *ap = qc->ap;
3686 if (ap->flags & ATA_FLAG_MMIO) {
3687 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
3689 /* clear start/stop bit */
3690 writeb(readb(mmio + ATA_DMA_CMD) & ~ATA_DMA_START,
3691 mmio + ATA_DMA_CMD);
3693 /* clear start/stop bit */
3694 outb(inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD) & ~ATA_DMA_START,
3695 ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3698 /* one-PIO-cycle guaranteed wait, per spec, for HDMA1:0 transition */
3699 ata_altstatus(ap); /* dummy read */
3703 * ata_host_intr - Handle host interrupt for given (port, task)
3704 * @ap: Port on which interrupt arrived (possibly...)
3705 * @qc: Taskfile currently active in engine
3707 * Handle host interrupt for given queued command. Currently,
3708 * only DMA interrupts are handled. All other commands are
3709 * handled via polling with interrupts disabled (nIEN bit).
3712 * spin_lock_irqsave(host_set lock)
3715 * One if interrupt was handled, zero if not (shared irq).
3718 inline unsigned int ata_host_intr (struct ata_port *ap,
3719 struct ata_queued_cmd *qc)
3721 u8 status, host_stat;
3723 switch (qc->tf.protocol) {
3726 case ATA_PROT_ATAPI_DMA:
3727 case ATA_PROT_ATAPI:
3728 /* check status of DMA engine */
3729 host_stat = ap->ops->bmdma_status(ap);
3730 VPRINTK("ata%u: host_stat 0x%X\n", ap->id, host_stat);
3732 /* if it's not our irq... */
3733 if (!(host_stat & ATA_DMA_INTR))
3736 /* before we do anything else, clear DMA-Start bit */
3737 ap->ops->bmdma_stop(qc);
3741 case ATA_PROT_ATAPI_NODATA:
3742 case ATA_PROT_NODATA:
3743 /* check altstatus */
3744 status = ata_altstatus(ap);
3745 if (status & ATA_BUSY)
3748 /* check main status, clearing INTRQ */
3749 status = ata_chk_status(ap);
3750 if (unlikely(status & ATA_BUSY))
3752 DPRINTK("ata%u: protocol %d (dev_stat 0x%X)\n",
3753 ap->id, qc->tf.protocol, status);
3755 /* ack bmdma irq events */
3756 ap->ops->irq_clear(ap);
3758 /* complete taskfile transaction */
3759 ata_qc_complete(qc, status);
3766 return 1; /* irq handled */
3769 ap->stats.idle_irq++;
3772 if ((ap->stats.idle_irq % 1000) == 0) {
3774 ata_irq_ack(ap, 0); /* debug trap */
3775 printk(KERN_WARNING "ata%d: irq trap\n", ap->id);
3778 return 0; /* irq not handled */
3782 * ata_interrupt - Default ATA host interrupt handler
3783 * @irq: irq line (unused)
3784 * @dev_instance: pointer to our ata_host_set information structure
3787 * Default interrupt handler for PCI IDE devices. Calls
3788 * ata_host_intr() for each port that is not disabled.
3791 * Obtains host_set lock during operation.
3794 * IRQ_NONE or IRQ_HANDLED.
3798 irqreturn_t ata_interrupt (int irq, void *dev_instance, struct pt_regs *regs)
3800 struct ata_host_set *host_set = dev_instance;
3802 unsigned int handled = 0;
3803 unsigned long flags;
3805 /* TODO: make _irqsave conditional on x86 PCI IDE legacy mode */
3806 spin_lock_irqsave(&host_set->lock, flags);
3808 for (i = 0; i < host_set->n_ports; i++) {
3809 struct ata_port *ap;
3811 ap = host_set->ports[i];
3813 !(ap->flags & (ATA_FLAG_PORT_DISABLED | ATA_FLAG_NOINTR))) {
3814 struct ata_queued_cmd *qc;
3816 qc = ata_qc_from_tag(ap, ap->active_tag);
3817 if (qc && (!(qc->tf.ctl & ATA_NIEN)) &&
3818 (qc->flags & ATA_QCFLAG_ACTIVE))
3819 handled |= ata_host_intr(ap, qc);
3823 spin_unlock_irqrestore(&host_set->lock, flags);
3825 return IRQ_RETVAL(handled);
3829 * atapi_packet_task - Write CDB bytes to hardware
3830 * @_data: Port to which ATAPI device is attached.
3832 * When device has indicated its readiness to accept
3833 * a CDB, this function is called. Send the CDB.
3834 * If DMA is to be performed, exit immediately.
3835 * Otherwise, we are in polling mode, so poll
3836 * status under operation succeeds or fails.
3839 * Kernel thread context (may sleep)
3842 static void atapi_packet_task(void *_data)
3844 struct ata_port *ap = _data;
3845 struct ata_queued_cmd *qc;
3848 qc = ata_qc_from_tag(ap, ap->active_tag);
3850 assert(qc->flags & ATA_QCFLAG_ACTIVE);
3852 /* sleep-wait for BSY to clear */
3853 DPRINTK("busy wait\n");
3854 if (ata_busy_sleep(ap, ATA_TMOUT_CDB_QUICK, ATA_TMOUT_CDB))
3857 /* make sure DRQ is set */
3858 status = ata_chk_status(ap);
3859 if ((status & (ATA_BUSY | ATA_DRQ)) != ATA_DRQ)
3863 DPRINTK("send cdb\n");
3864 assert(ap->cdb_len >= 12);
3866 if (qc->tf.protocol == ATA_PROT_ATAPI_DMA ||
3867 qc->tf.protocol == ATA_PROT_ATAPI_NODATA) {
3868 unsigned long flags;
3870 /* Once we're done issuing command and kicking bmdma,
3871 * irq handler takes over. To not lose irq, we need
3872 * to clear NOINTR flag before sending cdb, but
3873 * interrupt handler shouldn't be invoked before we're
3874 * finished. Hence, the following locking.
3876 spin_lock_irqsave(&ap->host_set->lock, flags);
3877 ap->flags &= ~ATA_FLAG_NOINTR;
3878 ata_data_xfer(ap, qc->cdb, ap->cdb_len, 1);
3879 if (qc->tf.protocol == ATA_PROT_ATAPI_DMA)
3880 ap->ops->bmdma_start(qc); /* initiate bmdma */
3881 spin_unlock_irqrestore(&ap->host_set->lock, flags);
3883 ata_data_xfer(ap, qc->cdb, ap->cdb_len, 1);
3885 /* PIO commands are handled by polling */
3886 ap->pio_task_state = PIO_ST;
3887 queue_work(ata_wq, &ap->pio_task);
3893 ata_poll_qc_complete(qc, ATA_ERR);
3898 * ata_port_start - Set port up for dma.
3899 * @ap: Port to initialize
3901 * Called just after data structures for each port are
3902 * initialized. Allocates space for PRD table.
3904 * May be used as the port_start() entry in ata_port_operations.
3909 int ata_port_start (struct ata_port *ap)
3911 struct device *dev = ap->host_set->dev;
3913 ap->prd = dma_alloc_coherent(dev, ATA_PRD_TBL_SZ, &ap->prd_dma, GFP_KERNEL);
3917 DPRINTK("prd alloc, virt %p, dma %llx\n", ap->prd, (unsigned long long) ap->prd_dma);
3924 * ata_port_stop - Undo ata_port_start()
3925 * @ap: Port to shut down
3927 * Frees the PRD table.
3929 * May be used as the port_stop() entry in ata_port_operations.
3934 void ata_port_stop (struct ata_port *ap)
3936 struct device *dev = ap->host_set->dev;
3938 dma_free_coherent(dev, ATA_PRD_TBL_SZ, ap->prd, ap->prd_dma);
3941 void ata_host_stop (struct ata_host_set *host_set)
3943 if (host_set->mmio_base)
3944 iounmap(host_set->mmio_base);
3949 * ata_host_remove - Unregister SCSI host structure with upper layers
3950 * @ap: Port to unregister
3951 * @do_unregister: 1 if we fully unregister, 0 to just stop the port
3956 static void ata_host_remove(struct ata_port *ap, unsigned int do_unregister)
3958 struct Scsi_Host *sh = ap->host;
3963 scsi_remove_host(sh);
3965 ap->ops->port_stop(ap);
3969 * ata_host_init - Initialize an ata_port structure
3970 * @ap: Structure to initialize
3971 * @host: associated SCSI mid-layer structure
3972 * @host_set: Collection of hosts to which @ap belongs
3973 * @ent: Probe information provided by low-level driver
3974 * @port_no: Port number associated with this ata_port
3976 * Initialize a new ata_port structure, and its associated
3980 * Inherited from caller.
3984 static void ata_host_init(struct ata_port *ap, struct Scsi_Host *host,
3985 struct ata_host_set *host_set,
3986 struct ata_probe_ent *ent, unsigned int port_no)
3992 host->max_channel = 1;
3993 host->unique_id = ata_unique_id++;
3994 host->max_cmd_len = 12;
3996 scsi_assign_lock(host, &host_set->lock);
3998 ap->flags = ATA_FLAG_PORT_DISABLED;
3999 ap->id = host->unique_id;
4001 ap->ctl = ATA_DEVCTL_OBS;
4002 ap->host_set = host_set;
4003 ap->port_no = port_no;
4005 ent->legacy_mode ? ent->hard_port_no : port_no;
4006 ap->pio_mask = ent->pio_mask;
4007 ap->mwdma_mask = ent->mwdma_mask;
4008 ap->udma_mask = ent->udma_mask;
4009 ap->flags |= ent->host_flags;
4010 ap->ops = ent->port_ops;
4011 ap->cbl = ATA_CBL_NONE;
4012 ap->active_tag = ATA_TAG_POISON;
4013 ap->last_ctl = 0xFF;
4015 INIT_WORK(&ap->packet_task, atapi_packet_task, ap);
4016 INIT_WORK(&ap->pio_task, ata_pio_task, ap);
4018 for (i = 0; i < ATA_MAX_DEVICES; i++)
4019 ap->device[i].devno = i;
4022 ap->stats.unhandled_irq = 1;
4023 ap->stats.idle_irq = 1;
4026 memcpy(&ap->ioaddr, &ent->port[port_no], sizeof(struct ata_ioports));
4030 * ata_host_add - Attach low-level ATA driver to system
4031 * @ent: Information provided by low-level driver
4032 * @host_set: Collections of ports to which we add
4033 * @port_no: Port number associated with this host
4035 * Attach low-level ATA driver to system.
4038 * PCI/etc. bus probe sem.
4041 * New ata_port on success, for NULL on error.
4045 static struct ata_port * ata_host_add(struct ata_probe_ent *ent,
4046 struct ata_host_set *host_set,
4047 unsigned int port_no)
4049 struct Scsi_Host *host;
4050 struct ata_port *ap;
4054 host = scsi_host_alloc(ent->sht, sizeof(struct ata_port));
4058 ap = (struct ata_port *) &host->hostdata[0];
4060 ata_host_init(ap, host, host_set, ent, port_no);
4062 rc = ap->ops->port_start(ap);
4069 scsi_host_put(host);
4074 * ata_device_add - Register hardware device with ATA and SCSI layers
4075 * @ent: Probe information describing hardware device to be registered
4077 * This function processes the information provided in the probe
4078 * information struct @ent, allocates the necessary ATA and SCSI
4079 * host information structures, initializes them, and registers
4080 * everything with requisite kernel subsystems.
4082 * This function requests irqs, probes the ATA bus, and probes
4086 * PCI/etc. bus probe sem.
4089 * Number of ports registered. Zero on error (no ports registered).
4093 int ata_device_add(struct ata_probe_ent *ent)
4095 unsigned int count = 0, i;
4096 struct device *dev = ent->dev;
4097 struct ata_host_set *host_set;
4100 /* alloc a container for our list of ATA ports (buses) */
4101 host_set = kmalloc(sizeof(struct ata_host_set) +
4102 (ent->n_ports * sizeof(void *)), GFP_KERNEL);
4105 memset(host_set, 0, sizeof(struct ata_host_set) + (ent->n_ports * sizeof(void *)));
4106 spin_lock_init(&host_set->lock);
4108 host_set->dev = dev;
4109 host_set->n_ports = ent->n_ports;
4110 host_set->irq = ent->irq;
4111 host_set->mmio_base = ent->mmio_base;
4112 host_set->private_data = ent->private_data;
4113 host_set->ops = ent->port_ops;
4115 /* register each port bound to this device */
4116 for (i = 0; i < ent->n_ports; i++) {
4117 struct ata_port *ap;
4118 unsigned long xfer_mode_mask;
4120 ap = ata_host_add(ent, host_set, i);
4124 host_set->ports[i] = ap;
4125 xfer_mode_mask =(ap->udma_mask << ATA_SHIFT_UDMA) |
4126 (ap->mwdma_mask << ATA_SHIFT_MWDMA) |
4127 (ap->pio_mask << ATA_SHIFT_PIO);
4129 /* print per-port info to dmesg */
4130 printk(KERN_INFO "ata%u: %cATA max %s cmd 0x%lX ctl 0x%lX "
4131 "bmdma 0x%lX irq %lu\n",
4133 ap->flags & ATA_FLAG_SATA ? 'S' : 'P',
4134 ata_mode_string(xfer_mode_mask),
4135 ap->ioaddr.cmd_addr,
4136 ap->ioaddr.ctl_addr,
4137 ap->ioaddr.bmdma_addr,
4141 host_set->ops->irq_clear(ap);
4150 /* obtain irq, that is shared between channels */
4151 if (request_irq(ent->irq, ent->port_ops->irq_handler, ent->irq_flags,
4152 DRV_NAME, host_set))
4155 /* perform each probe synchronously */
4156 DPRINTK("probe begin\n");
4157 for (i = 0; i < count; i++) {
4158 struct ata_port *ap;
4161 ap = host_set->ports[i];
4163 DPRINTK("ata%u: probe begin\n", ap->id);
4164 rc = ata_bus_probe(ap);
4165 DPRINTK("ata%u: probe end\n", ap->id);
4168 /* FIXME: do something useful here?
4169 * Current libata behavior will
4170 * tear down everything when
4171 * the module is removed
4172 * or the h/w is unplugged.
4176 rc = scsi_add_host(ap->host, dev);
4178 printk(KERN_ERR "ata%u: scsi_add_host failed\n",
4180 /* FIXME: do something useful here */
4181 /* FIXME: handle unconditional calls to
4182 * scsi_scan_host and ata_host_remove, below,
4188 /* probes are done, now scan each port's disk(s) */
4189 DPRINTK("probe begin\n");
4190 for (i = 0; i < count; i++) {
4191 struct ata_port *ap = host_set->ports[i];
4193 scsi_scan_host(ap->host);
4196 dev_set_drvdata(dev, host_set);
4198 VPRINTK("EXIT, returning %u\n", ent->n_ports);
4199 return ent->n_ports; /* success */
4202 for (i = 0; i < count; i++) {
4203 ata_host_remove(host_set->ports[i], 1);
4204 scsi_host_put(host_set->ports[i]->host);
4207 VPRINTK("EXIT, returning 0\n");
4212 * ata_scsi_release - SCSI layer callback hook for host unload
4213 * @host: libata host to be unloaded
4215 * Performs all duties necessary to shut down a libata port...
4216 * Kill port kthread, disable port, and release resources.
4219 * Inherited from SCSI layer.
4225 int ata_scsi_release(struct Scsi_Host *host)
4227 struct ata_port *ap = (struct ata_port *) &host->hostdata[0];
4231 ap->ops->port_disable(ap);
4232 ata_host_remove(ap, 0);
4239 * ata_std_ports - initialize ioaddr with standard port offsets.
4240 * @ioaddr: IO address structure to be initialized
4242 * Utility function which initializes data_addr, error_addr,
4243 * feature_addr, nsect_addr, lbal_addr, lbam_addr, lbah_addr,
4244 * device_addr, status_addr, and command_addr to standard offsets
4245 * relative to cmd_addr.
4247 * Does not set ctl_addr, altstatus_addr, bmdma_addr, or scr_addr.
4250 void ata_std_ports(struct ata_ioports *ioaddr)
4252 ioaddr->data_addr = ioaddr->cmd_addr + ATA_REG_DATA;
4253 ioaddr->error_addr = ioaddr->cmd_addr + ATA_REG_ERR;
4254 ioaddr->feature_addr = ioaddr->cmd_addr + ATA_REG_FEATURE;
4255 ioaddr->nsect_addr = ioaddr->cmd_addr + ATA_REG_NSECT;
4256 ioaddr->lbal_addr = ioaddr->cmd_addr + ATA_REG_LBAL;
4257 ioaddr->lbam_addr = ioaddr->cmd_addr + ATA_REG_LBAM;
4258 ioaddr->lbah_addr = ioaddr->cmd_addr + ATA_REG_LBAH;
4259 ioaddr->device_addr = ioaddr->cmd_addr + ATA_REG_DEVICE;
4260 ioaddr->status_addr = ioaddr->cmd_addr + ATA_REG_STATUS;
4261 ioaddr->command_addr = ioaddr->cmd_addr + ATA_REG_CMD;
4264 static struct ata_probe_ent *
4265 ata_probe_ent_alloc(struct device *dev, struct ata_port_info *port)
4267 struct ata_probe_ent *probe_ent;
4269 probe_ent = kmalloc(sizeof(*probe_ent), GFP_KERNEL);
4271 printk(KERN_ERR DRV_NAME "(%s): out of memory\n",
4272 kobject_name(&(dev->kobj)));
4276 memset(probe_ent, 0, sizeof(*probe_ent));
4278 INIT_LIST_HEAD(&probe_ent->node);
4279 probe_ent->dev = dev;
4281 probe_ent->sht = port->sht;
4282 probe_ent->host_flags = port->host_flags;
4283 probe_ent->pio_mask = port->pio_mask;
4284 probe_ent->mwdma_mask = port->mwdma_mask;
4285 probe_ent->udma_mask = port->udma_mask;
4286 probe_ent->port_ops = port->port_ops;
4294 * ata_pci_init_native_mode - Initialize native-mode driver
4295 * @pdev: pci device to be initialized
4296 * @port: array[2] of pointers to port info structures.
4298 * Utility function which allocates and initializes an
4299 * ata_probe_ent structure for a standard dual-port
4300 * PIO-based IDE controller. The returned ata_probe_ent
4301 * structure can be passed to ata_device_add(). The returned
4302 * ata_probe_ent structure should then be freed with kfree().
4306 struct ata_probe_ent *
4307 ata_pci_init_native_mode(struct pci_dev *pdev, struct ata_port_info **port)
4309 struct ata_probe_ent *probe_ent =
4310 ata_probe_ent_alloc(pci_dev_to_dev(pdev), port[0]);
4314 probe_ent->n_ports = 2;
4315 probe_ent->irq = pdev->irq;
4316 probe_ent->irq_flags = SA_SHIRQ;
4318 probe_ent->port[0].cmd_addr = pci_resource_start(pdev, 0);
4319 probe_ent->port[0].altstatus_addr =
4320 probe_ent->port[0].ctl_addr =
4321 pci_resource_start(pdev, 1) | ATA_PCI_CTL_OFS;
4322 probe_ent->port[0].bmdma_addr = pci_resource_start(pdev, 4);
4324 probe_ent->port[1].cmd_addr = pci_resource_start(pdev, 2);
4325 probe_ent->port[1].altstatus_addr =
4326 probe_ent->port[1].ctl_addr =
4327 pci_resource_start(pdev, 3) | ATA_PCI_CTL_OFS;
4328 probe_ent->port[1].bmdma_addr = pci_resource_start(pdev, 4) + 8;
4330 ata_std_ports(&probe_ent->port[0]);
4331 ata_std_ports(&probe_ent->port[1]);
4336 static struct ata_probe_ent *
4337 ata_pci_init_legacy_mode(struct pci_dev *pdev, struct ata_port_info **port,
4338 struct ata_probe_ent **ppe2)
4340 struct ata_probe_ent *probe_ent, *probe_ent2;
4342 probe_ent = ata_probe_ent_alloc(pci_dev_to_dev(pdev), port[0]);
4345 probe_ent2 = ata_probe_ent_alloc(pci_dev_to_dev(pdev), port[1]);
4351 probe_ent->n_ports = 1;
4352 probe_ent->irq = 14;
4354 probe_ent->hard_port_no = 0;
4355 probe_ent->legacy_mode = 1;
4357 probe_ent2->n_ports = 1;
4358 probe_ent2->irq = 15;
4360 probe_ent2->hard_port_no = 1;
4361 probe_ent2->legacy_mode = 1;
4363 probe_ent->port[0].cmd_addr = 0x1f0;
4364 probe_ent->port[0].altstatus_addr =
4365 probe_ent->port[0].ctl_addr = 0x3f6;
4366 probe_ent->port[0].bmdma_addr = pci_resource_start(pdev, 4);
4368 probe_ent2->port[0].cmd_addr = 0x170;
4369 probe_ent2->port[0].altstatus_addr =
4370 probe_ent2->port[0].ctl_addr = 0x376;
4371 probe_ent2->port[0].bmdma_addr = pci_resource_start(pdev, 4)+8;
4373 ata_std_ports(&probe_ent->port[0]);
4374 ata_std_ports(&probe_ent2->port[0]);
4381 * ata_pci_init_one - Initialize/register PCI IDE host controller
4382 * @pdev: Controller to be initialized
4383 * @port_info: Information from low-level host driver
4384 * @n_ports: Number of ports attached to host controller
4386 * This is a helper function which can be called from a driver's
4387 * xxx_init_one() probe function if the hardware uses traditional
4388 * IDE taskfile registers.
4390 * This function calls pci_enable_device(), reserves its register
4391 * regions, sets the dma mask, enables bus master mode, and calls
4395 * Inherited from PCI layer (may sleep).
4398 * Zero on success, negative on errno-based value on error.
4402 int ata_pci_init_one (struct pci_dev *pdev, struct ata_port_info **port_info,
4403 unsigned int n_ports)
4405 struct ata_probe_ent *probe_ent, *probe_ent2 = NULL;
4406 struct ata_port_info *port[2];
4408 unsigned int legacy_mode = 0;
4409 int disable_dev_on_err = 1;
4414 port[0] = port_info[0];
4416 port[1] = port_info[1];
4420 if ((port[0]->host_flags & ATA_FLAG_NO_LEGACY) == 0
4421 && (pdev->class >> 8) == PCI_CLASS_STORAGE_IDE) {
4422 /* TODO: support transitioning to native mode? */
4423 pci_read_config_byte(pdev, PCI_CLASS_PROG, &tmp8);
4424 mask = (1 << 2) | (1 << 0);
4425 if ((tmp8 & mask) != mask)
4426 legacy_mode = (1 << 3);
4430 if ((!legacy_mode) && (n_ports > 1)) {
4431 printk(KERN_ERR "ata: BUG: native mode, n_ports > 1\n");
4435 rc = pci_enable_device(pdev);
4439 rc = pci_request_regions(pdev, DRV_NAME);
4441 disable_dev_on_err = 0;
4446 if (!request_region(0x1f0, 8, "libata")) {
4447 struct resource *conflict, res;
4449 res.end = 0x1f0 + 8 - 1;
4450 conflict = ____request_resource(&ioport_resource, &res);
4451 if (!strcmp(conflict->name, "libata"))
4452 legacy_mode |= (1 << 0);
4454 disable_dev_on_err = 0;
4455 printk(KERN_WARNING "ata: 0x1f0 IDE port busy\n");
4458 legacy_mode |= (1 << 0);
4460 if (!request_region(0x170, 8, "libata")) {
4461 struct resource *conflict, res;
4463 res.end = 0x170 + 8 - 1;
4464 conflict = ____request_resource(&ioport_resource, &res);
4465 if (!strcmp(conflict->name, "libata"))
4466 legacy_mode |= (1 << 1);
4468 disable_dev_on_err = 0;
4469 printk(KERN_WARNING "ata: 0x170 IDE port busy\n");
4472 legacy_mode |= (1 << 1);
4475 /* we have legacy mode, but all ports are unavailable */
4476 if (legacy_mode == (1 << 3)) {
4478 goto err_out_regions;
4481 rc = pci_set_dma_mask(pdev, ATA_DMA_MASK);
4483 goto err_out_regions;
4484 rc = pci_set_consistent_dma_mask(pdev, ATA_DMA_MASK);
4486 goto err_out_regions;
4489 probe_ent = ata_pci_init_legacy_mode(pdev, port, &probe_ent2);
4491 probe_ent = ata_pci_init_native_mode(pdev, port);
4494 goto err_out_regions;
4497 pci_set_master(pdev);
4499 /* FIXME: check ata_device_add return */
4501 if (legacy_mode & (1 << 0))
4502 ata_device_add(probe_ent);
4503 if (legacy_mode & (1 << 1))
4504 ata_device_add(probe_ent2);
4506 ata_device_add(probe_ent);
4514 if (legacy_mode & (1 << 0))
4515 release_region(0x1f0, 8);
4516 if (legacy_mode & (1 << 1))
4517 release_region(0x170, 8);
4518 pci_release_regions(pdev);
4520 if (disable_dev_on_err)
4521 pci_disable_device(pdev);
4526 * ata_pci_remove_one - PCI layer callback for device removal
4527 * @pdev: PCI device that was removed
4529 * PCI layer indicates to libata via this hook that
4530 * hot-unplug or module unload event has occured.
4531 * Handle this by unregistering all objects associated
4532 * with this PCI device. Free those objects. Then finally
4533 * release PCI resources and disable device.
4536 * Inherited from PCI layer (may sleep).
4539 void ata_pci_remove_one (struct pci_dev *pdev)
4541 struct device *dev = pci_dev_to_dev(pdev);
4542 struct ata_host_set *host_set = dev_get_drvdata(dev);
4543 struct ata_port *ap;
4546 for (i = 0; i < host_set->n_ports; i++) {
4547 ap = host_set->ports[i];
4549 scsi_remove_host(ap->host);
4552 free_irq(host_set->irq, host_set);
4554 for (i = 0; i < host_set->n_ports; i++) {
4555 ap = host_set->ports[i];
4557 ata_scsi_release(ap->host);
4559 if ((ap->flags & ATA_FLAG_NO_LEGACY) == 0) {
4560 struct ata_ioports *ioaddr = &ap->ioaddr;
4562 if (ioaddr->cmd_addr == 0x1f0)
4563 release_region(0x1f0, 8);
4564 else if (ioaddr->cmd_addr == 0x170)
4565 release_region(0x170, 8);
4568 scsi_host_put(ap->host);
4571 if (host_set->ops->host_stop)
4572 host_set->ops->host_stop(host_set);
4576 pci_release_regions(pdev);
4577 pci_disable_device(pdev);
4578 dev_set_drvdata(dev, NULL);
4581 /* move to PCI subsystem */
4582 int pci_test_config_bits(struct pci_dev *pdev, struct pci_bits *bits)
4584 unsigned long tmp = 0;
4586 switch (bits->width) {
4589 pci_read_config_byte(pdev, bits->reg, &tmp8);
4595 pci_read_config_word(pdev, bits->reg, &tmp16);
4601 pci_read_config_dword(pdev, bits->reg, &tmp32);
4612 return (tmp == bits->val) ? 1 : 0;
4614 #endif /* CONFIG_PCI */
4617 static int __init ata_init(void)
4619 ata_wq = create_workqueue("ata");
4623 printk(KERN_DEBUG "libata version " DRV_VERSION " loaded.\n");
4627 static void __exit ata_exit(void)
4629 destroy_workqueue(ata_wq);
4632 module_init(ata_init);
4633 module_exit(ata_exit);
4636 * libata is essentially a library of internal helper functions for
4637 * low-level ATA host controller drivers. As such, the API/ABI is
4638 * likely to change as new drivers are added and updated.
4639 * Do not depend on ABI/API stability.
4642 EXPORT_SYMBOL_GPL(ata_std_bios_param);
4643 EXPORT_SYMBOL_GPL(ata_std_ports);
4644 EXPORT_SYMBOL_GPL(ata_device_add);
4645 EXPORT_SYMBOL_GPL(ata_sg_init);
4646 EXPORT_SYMBOL_GPL(ata_sg_init_one);
4647 EXPORT_SYMBOL_GPL(ata_qc_complete);
4648 EXPORT_SYMBOL_GPL(ata_qc_issue_prot);
4649 EXPORT_SYMBOL_GPL(ata_eng_timeout);
4650 EXPORT_SYMBOL_GPL(ata_tf_load);
4651 EXPORT_SYMBOL_GPL(ata_tf_read);
4652 EXPORT_SYMBOL_GPL(ata_noop_dev_select);
4653 EXPORT_SYMBOL_GPL(ata_std_dev_select);
4654 EXPORT_SYMBOL_GPL(ata_tf_to_fis);
4655 EXPORT_SYMBOL_GPL(ata_tf_from_fis);
4656 EXPORT_SYMBOL_GPL(ata_check_status);
4657 EXPORT_SYMBOL_GPL(ata_altstatus);
4658 EXPORT_SYMBOL_GPL(ata_chk_err);
4659 EXPORT_SYMBOL_GPL(ata_exec_command);
4660 EXPORT_SYMBOL_GPL(ata_port_start);
4661 EXPORT_SYMBOL_GPL(ata_port_stop);
4662 EXPORT_SYMBOL_GPL(ata_host_stop);
4663 EXPORT_SYMBOL_GPL(ata_interrupt);
4664 EXPORT_SYMBOL_GPL(ata_qc_prep);
4665 EXPORT_SYMBOL_GPL(ata_bmdma_setup);
4666 EXPORT_SYMBOL_GPL(ata_bmdma_start);
4667 EXPORT_SYMBOL_GPL(ata_bmdma_irq_clear);
4668 EXPORT_SYMBOL_GPL(ata_bmdma_status);
4669 EXPORT_SYMBOL_GPL(ata_bmdma_stop);
4670 EXPORT_SYMBOL_GPL(ata_port_probe);
4671 EXPORT_SYMBOL_GPL(sata_phy_reset);
4672 EXPORT_SYMBOL_GPL(__sata_phy_reset);
4673 EXPORT_SYMBOL_GPL(ata_bus_reset);
4674 EXPORT_SYMBOL_GPL(ata_port_disable);
4675 EXPORT_SYMBOL_GPL(ata_scsi_ioctl);
4676 EXPORT_SYMBOL_GPL(ata_scsi_queuecmd);
4677 EXPORT_SYMBOL_GPL(ata_scsi_error);
4678 EXPORT_SYMBOL_GPL(ata_scsi_slave_config);
4679 EXPORT_SYMBOL_GPL(ata_scsi_release);
4680 EXPORT_SYMBOL_GPL(ata_host_intr);
4681 EXPORT_SYMBOL_GPL(ata_dev_classify);
4682 EXPORT_SYMBOL_GPL(ata_dev_id_string);
4683 EXPORT_SYMBOL_GPL(ata_dev_config);
4684 EXPORT_SYMBOL_GPL(ata_scsi_simulate);
4687 EXPORT_SYMBOL_GPL(pci_test_config_bits);
4688 EXPORT_SYMBOL_GPL(ata_pci_init_native_mode);
4689 EXPORT_SYMBOL_GPL(ata_pci_init_one);
4690 EXPORT_SYMBOL_GPL(ata_pci_remove_one);
4691 #endif /* CONFIG_PCI */