2 libata-core.c - helper library for ATA
4 Copyright 2003-2004 Red Hat, Inc. All rights reserved.
5 Copyright 2003-2004 Jeff Garzik
7 The contents of this file are subject to the Open
8 Software License version 1.1 that can be found at
9 http://www.opensource.org/licenses/osl-1.1.txt and is included herein
12 Alternatively, the contents of this file may be used under the terms
13 of the GNU General Public License version 2 (the "GPL") as distributed
14 in the kernel source COPYING file, in which case the provisions of
15 the GPL are applicable instead of the above. If you wish to allow
16 the use of your version of this file only under the terms of the
17 GPL and not to allow others to use your version of this file under
18 the OSL, indicate your decision by deleting the provisions above and
19 replace them with the notice and other provisions required by the GPL.
20 If you do not delete the provisions above, a recipient may use your
21 version of this file under either the OSL or the GPL.
25 #include <linux/config.h>
26 #include <linux/kernel.h>
27 #include <linux/module.h>
28 #include <linux/pci.h>
29 #include <linux/init.h>
30 #include <linux/list.h>
32 #include <linux/highmem.h>
33 #include <linux/spinlock.h>
34 #include <linux/blkdev.h>
35 #include <linux/delay.h>
36 #include <linux/timer.h>
37 #include <linux/interrupt.h>
38 #include <linux/completion.h>
39 #include <linux/suspend.h>
40 #include <linux/workqueue.h>
41 #include <scsi/scsi.h>
43 #include "scsi_priv.h"
44 #include <scsi/scsi_host.h>
45 #include <linux/libata.h>
47 #include <asm/semaphore.h>
48 #include <asm/byteorder.h>
52 static unsigned int ata_busy_sleep (struct ata_port *ap,
53 unsigned long tmout_pat,
55 static void ata_set_mode(struct ata_port *ap);
56 static void ata_dev_set_xfermode(struct ata_port *ap, struct ata_device *dev);
57 static unsigned int ata_get_mode_mask(struct ata_port *ap, int shift);
58 static int fgb(u32 bitmap);
59 static int ata_choose_xfer_mode(struct ata_port *ap,
61 unsigned int *xfer_shift_out);
62 static int ata_qc_complete_noop(struct ata_queued_cmd *qc, u8 drv_stat);
63 static void __ata_qc_complete(struct ata_queued_cmd *qc);
65 static unsigned int ata_unique_id = 1;
66 static struct workqueue_struct *ata_wq;
68 MODULE_AUTHOR("Jeff Garzik");
69 MODULE_DESCRIPTION("Library module for ATA devices");
70 MODULE_LICENSE("GPL");
71 MODULE_VERSION(DRV_VERSION);
74 * ata_tf_load - send taskfile registers to host controller
75 * @ap: Port to which output is sent
76 * @tf: ATA taskfile register set
78 * Outputs ATA taskfile to standard ATA host controller.
81 * Inherited from caller.
84 static void ata_tf_load_pio(struct ata_port *ap, struct ata_taskfile *tf)
86 struct ata_ioports *ioaddr = &ap->ioaddr;
87 unsigned int is_addr = tf->flags & ATA_TFLAG_ISADDR;
89 if (tf->ctl != ap->last_ctl) {
90 outb(tf->ctl, ioaddr->ctl_addr);
91 ap->last_ctl = tf->ctl;
95 if (is_addr && (tf->flags & ATA_TFLAG_LBA48)) {
96 outb(tf->hob_feature, ioaddr->feature_addr);
97 outb(tf->hob_nsect, ioaddr->nsect_addr);
98 outb(tf->hob_lbal, ioaddr->lbal_addr);
99 outb(tf->hob_lbam, ioaddr->lbam_addr);
100 outb(tf->hob_lbah, ioaddr->lbah_addr);
101 VPRINTK("hob: feat 0x%X nsect 0x%X, lba 0x%X 0x%X 0x%X\n",
110 outb(tf->feature, ioaddr->feature_addr);
111 outb(tf->nsect, ioaddr->nsect_addr);
112 outb(tf->lbal, ioaddr->lbal_addr);
113 outb(tf->lbam, ioaddr->lbam_addr);
114 outb(tf->lbah, ioaddr->lbah_addr);
115 VPRINTK("feat 0x%X nsect 0x%X lba 0x%X 0x%X 0x%X\n",
123 if (tf->flags & ATA_TFLAG_DEVICE) {
124 outb(tf->device, ioaddr->device_addr);
125 VPRINTK("device 0x%X\n", tf->device);
132 * ata_tf_load_mmio - send taskfile registers to host controller
133 * @ap: Port to which output is sent
134 * @tf: ATA taskfile register set
136 * Outputs ATA taskfile to standard ATA host controller using MMIO.
139 * Inherited from caller.
142 static void ata_tf_load_mmio(struct ata_port *ap, struct ata_taskfile *tf)
144 struct ata_ioports *ioaddr = &ap->ioaddr;
145 unsigned int is_addr = tf->flags & ATA_TFLAG_ISADDR;
147 if (tf->ctl != ap->last_ctl) {
148 writeb(tf->ctl, (void __iomem *) ap->ioaddr.ctl_addr);
149 ap->last_ctl = tf->ctl;
153 if (is_addr && (tf->flags & ATA_TFLAG_LBA48)) {
154 writeb(tf->hob_feature, (void __iomem *) ioaddr->feature_addr);
155 writeb(tf->hob_nsect, (void __iomem *) ioaddr->nsect_addr);
156 writeb(tf->hob_lbal, (void __iomem *) ioaddr->lbal_addr);
157 writeb(tf->hob_lbam, (void __iomem *) ioaddr->lbam_addr);
158 writeb(tf->hob_lbah, (void __iomem *) ioaddr->lbah_addr);
159 VPRINTK("hob: feat 0x%X nsect 0x%X, lba 0x%X 0x%X 0x%X\n",
168 writeb(tf->feature, (void __iomem *) ioaddr->feature_addr);
169 writeb(tf->nsect, (void __iomem *) ioaddr->nsect_addr);
170 writeb(tf->lbal, (void __iomem *) ioaddr->lbal_addr);
171 writeb(tf->lbam, (void __iomem *) ioaddr->lbam_addr);
172 writeb(tf->lbah, (void __iomem *) ioaddr->lbah_addr);
173 VPRINTK("feat 0x%X nsect 0x%X lba 0x%X 0x%X 0x%X\n",
181 if (tf->flags & ATA_TFLAG_DEVICE) {
182 writeb(tf->device, (void __iomem *) ioaddr->device_addr);
183 VPRINTK("device 0x%X\n", tf->device);
191 * ata_tf_load - send taskfile registers to host controller
192 * @ap: Port to which output is sent
193 * @tf: ATA taskfile register set
195 * Outputs ATA taskfile to standard ATA host controller using MMIO
196 * or PIO as indicated by the ATA_FLAG_MMIO flag.
197 * Writes the control, feature, nsect, lbal, lbam, and lbah registers.
198 * Optionally (ATA_TFLAG_LBA48) writes hob_feature, hob_nsect,
199 * hob_lbal, hob_lbam, and hob_lbah.
201 * This function waits for idle (!BUSY and !DRQ) after writing
202 * registers. If the control register has a new value, this
203 * function also waits for idle after writing control and before
204 * writing the remaining registers.
206 * May be used as the tf_load() entry in ata_port_operations.
209 * Inherited from caller.
211 void ata_tf_load(struct ata_port *ap, struct ata_taskfile *tf)
213 if (ap->flags & ATA_FLAG_MMIO)
214 ata_tf_load_mmio(ap, tf);
216 ata_tf_load_pio(ap, tf);
220 * ata_exec_command_pio - issue ATA command to host controller
221 * @ap: port to which command is being issued
222 * @tf: ATA taskfile register set
224 * Issues PIO write to ATA command register, with proper
225 * synchronization with interrupt handler / other threads.
228 * spin_lock_irqsave(host_set lock)
231 static void ata_exec_command_pio(struct ata_port *ap, struct ata_taskfile *tf)
233 DPRINTK("ata%u: cmd 0x%X\n", ap->id, tf->command);
235 outb(tf->command, ap->ioaddr.command_addr);
241 * ata_exec_command_mmio - issue ATA command to host controller
242 * @ap: port to which command is being issued
243 * @tf: ATA taskfile register set
245 * Issues MMIO write to ATA command register, with proper
246 * synchronization with interrupt handler / other threads.
249 * spin_lock_irqsave(host_set lock)
252 static void ata_exec_command_mmio(struct ata_port *ap, struct ata_taskfile *tf)
254 DPRINTK("ata%u: cmd 0x%X\n", ap->id, tf->command);
256 writeb(tf->command, (void __iomem *) ap->ioaddr.command_addr);
262 * ata_exec_command - issue ATA command to host controller
263 * @ap: port to which command is being issued
264 * @tf: ATA taskfile register set
266 * Issues PIO/MMIO write to ATA command register, with proper
267 * synchronization with interrupt handler / other threads.
270 * spin_lock_irqsave(host_set lock)
272 void ata_exec_command(struct ata_port *ap, struct ata_taskfile *tf)
274 if (ap->flags & ATA_FLAG_MMIO)
275 ata_exec_command_mmio(ap, tf);
277 ata_exec_command_pio(ap, tf);
281 * ata_exec - issue ATA command to host controller
282 * @ap: port to which command is being issued
283 * @tf: ATA taskfile register set
285 * Issues PIO/MMIO write to ATA command register, with proper
286 * synchronization with interrupt handler / other threads.
289 * Obtains host_set lock.
292 static inline void ata_exec(struct ata_port *ap, struct ata_taskfile *tf)
296 DPRINTK("ata%u: cmd 0x%X\n", ap->id, tf->command);
297 spin_lock_irqsave(&ap->host_set->lock, flags);
298 ap->ops->exec_command(ap, tf);
299 spin_unlock_irqrestore(&ap->host_set->lock, flags);
303 * ata_tf_to_host - issue ATA taskfile to host controller
304 * @ap: port to which command is being issued
305 * @tf: ATA taskfile register set
307 * Issues ATA taskfile register set to ATA host controller,
308 * with proper synchronization with interrupt handler and
312 * Obtains host_set lock.
315 static void ata_tf_to_host(struct ata_port *ap, struct ata_taskfile *tf)
317 ap->ops->tf_load(ap, tf);
323 * ata_tf_to_host_nolock - issue ATA taskfile to host controller
324 * @ap: port to which command is being issued
325 * @tf: ATA taskfile register set
327 * Issues ATA taskfile register set to ATA host controller,
328 * with proper synchronization with interrupt handler and
332 * spin_lock_irqsave(host_set lock)
335 void ata_tf_to_host_nolock(struct ata_port *ap, struct ata_taskfile *tf)
337 ap->ops->tf_load(ap, tf);
338 ap->ops->exec_command(ap, tf);
342 * ata_tf_read_pio - input device's ATA taskfile shadow registers
343 * @ap: Port from which input is read
344 * @tf: ATA taskfile register set for storing input
346 * Reads ATA taskfile registers for currently-selected device
350 * Inherited from caller.
353 static void ata_tf_read_pio(struct ata_port *ap, struct ata_taskfile *tf)
355 struct ata_ioports *ioaddr = &ap->ioaddr;
357 tf->nsect = inb(ioaddr->nsect_addr);
358 tf->lbal = inb(ioaddr->lbal_addr);
359 tf->lbam = inb(ioaddr->lbam_addr);
360 tf->lbah = inb(ioaddr->lbah_addr);
361 tf->device = inb(ioaddr->device_addr);
363 if (tf->flags & ATA_TFLAG_LBA48) {
364 outb(tf->ctl | ATA_HOB, ioaddr->ctl_addr);
365 tf->hob_feature = inb(ioaddr->error_addr);
366 tf->hob_nsect = inb(ioaddr->nsect_addr);
367 tf->hob_lbal = inb(ioaddr->lbal_addr);
368 tf->hob_lbam = inb(ioaddr->lbam_addr);
369 tf->hob_lbah = inb(ioaddr->lbah_addr);
374 * ata_tf_read_mmio - input device's ATA taskfile shadow registers
375 * @ap: Port from which input is read
376 * @tf: ATA taskfile register set for storing input
378 * Reads ATA taskfile registers for currently-selected device
382 * Inherited from caller.
385 static void ata_tf_read_mmio(struct ata_port *ap, struct ata_taskfile *tf)
387 struct ata_ioports *ioaddr = &ap->ioaddr;
389 tf->nsect = readb((void __iomem *)ioaddr->nsect_addr);
390 tf->lbal = readb((void __iomem *)ioaddr->lbal_addr);
391 tf->lbam = readb((void __iomem *)ioaddr->lbam_addr);
392 tf->lbah = readb((void __iomem *)ioaddr->lbah_addr);
393 tf->device = readb((void __iomem *)ioaddr->device_addr);
395 if (tf->flags & ATA_TFLAG_LBA48) {
396 writeb(tf->ctl | ATA_HOB, (void __iomem *) ap->ioaddr.ctl_addr);
397 tf->hob_feature = readb((void __iomem *)ioaddr->error_addr);
398 tf->hob_nsect = readb((void __iomem *)ioaddr->nsect_addr);
399 tf->hob_lbal = readb((void __iomem *)ioaddr->lbal_addr);
400 tf->hob_lbam = readb((void __iomem *)ioaddr->lbam_addr);
401 tf->hob_lbah = readb((void __iomem *)ioaddr->lbah_addr);
407 * ata_tf_read - input device's ATA taskfile shadow registers
408 * @ap: Port from which input is read
409 * @tf: ATA taskfile register set for storing input
411 * Reads ATA taskfile registers for currently-selected device
414 * Reads nsect, lbal, lbam, lbah, and device. If ATA_TFLAG_LBA48
415 * is set, also reads the hob registers.
417 * May be used as the tf_read() entry in ata_port_operations.
420 * Inherited from caller.
422 void ata_tf_read(struct ata_port *ap, struct ata_taskfile *tf)
424 if (ap->flags & ATA_FLAG_MMIO)
425 ata_tf_read_mmio(ap, tf);
427 ata_tf_read_pio(ap, tf);
431 * ata_check_status_pio - Read device status reg & clear interrupt
432 * @ap: port where the device is
434 * Reads ATA taskfile status register for currently-selected device
435 * and return its value. This also clears pending interrupts
439 * Inherited from caller.
441 static u8 ata_check_status_pio(struct ata_port *ap)
443 return inb(ap->ioaddr.status_addr);
447 * ata_check_status_mmio - Read device status reg & clear interrupt
448 * @ap: port where the device is
450 * Reads ATA taskfile status register for currently-selected device
451 * via MMIO and return its value. This also clears pending interrupts
455 * Inherited from caller.
457 static u8 ata_check_status_mmio(struct ata_port *ap)
459 return readb((void __iomem *) ap->ioaddr.status_addr);
464 * ata_check_status - Read device status reg & clear interrupt
465 * @ap: port where the device is
467 * Reads ATA taskfile status register for currently-selected device
468 * and return its value. This also clears pending interrupts
471 * May be used as the check_status() entry in ata_port_operations.
474 * Inherited from caller.
476 u8 ata_check_status(struct ata_port *ap)
478 if (ap->flags & ATA_FLAG_MMIO)
479 return ata_check_status_mmio(ap);
480 return ata_check_status_pio(ap);
485 * ata_altstatus - Read device alternate status reg
486 * @ap: port where the device is
488 * Reads ATA taskfile alternate status register for
489 * currently-selected device and return its value.
491 * Note: may NOT be used as the check_altstatus() entry in
492 * ata_port_operations.
495 * Inherited from caller.
497 u8 ata_altstatus(struct ata_port *ap)
499 if (ap->ops->check_altstatus)
500 return ap->ops->check_altstatus(ap);
502 if (ap->flags & ATA_FLAG_MMIO)
503 return readb((void __iomem *)ap->ioaddr.altstatus_addr);
504 return inb(ap->ioaddr.altstatus_addr);
509 * ata_chk_err - Read device error reg
510 * @ap: port where the device is
512 * Reads ATA taskfile error register for
513 * currently-selected device and return its value.
515 * Note: may NOT be used as the check_err() entry in
516 * ata_port_operations.
519 * Inherited from caller.
521 u8 ata_chk_err(struct ata_port *ap)
523 if (ap->ops->check_err)
524 return ap->ops->check_err(ap);
526 if (ap->flags & ATA_FLAG_MMIO) {
527 return readb((void __iomem *) ap->ioaddr.error_addr);
529 return inb(ap->ioaddr.error_addr);
533 * ata_tf_to_fis - Convert ATA taskfile to SATA FIS structure
534 * @tf: Taskfile to convert
535 * @fis: Buffer into which data will output
536 * @pmp: Port multiplier port
538 * Converts a standard ATA taskfile to a Serial ATA
539 * FIS structure (Register - Host to Device).
542 * Inherited from caller.
545 void ata_tf_to_fis(struct ata_taskfile *tf, u8 *fis, u8 pmp)
547 fis[0] = 0x27; /* Register - Host to Device FIS */
548 fis[1] = (pmp & 0xf) | (1 << 7); /* Port multiplier number,
549 bit 7 indicates Command FIS */
550 fis[2] = tf->command;
551 fis[3] = tf->feature;
558 fis[8] = tf->hob_lbal;
559 fis[9] = tf->hob_lbam;
560 fis[10] = tf->hob_lbah;
561 fis[11] = tf->hob_feature;
564 fis[13] = tf->hob_nsect;
575 * ata_tf_from_fis - Convert SATA FIS to ATA taskfile
576 * @fis: Buffer from which data will be input
577 * @tf: Taskfile to output
579 * Converts a standard ATA taskfile to a Serial ATA
580 * FIS structure (Register - Host to Device).
583 * Inherited from caller.
586 void ata_tf_from_fis(u8 *fis, struct ata_taskfile *tf)
588 tf->command = fis[2]; /* status */
589 tf->feature = fis[3]; /* error */
596 tf->hob_lbal = fis[8];
597 tf->hob_lbam = fis[9];
598 tf->hob_lbah = fis[10];
601 tf->hob_nsect = fis[13];
605 * ata_prot_to_cmd - determine which read/write opcodes to use
606 * @protocol: ATA_PROT_xxx taskfile protocol
607 * @lba48: true is lba48 is present
609 * Given necessary input, determine which read/write commands
610 * to use to transfer data.
615 static int ata_prot_to_cmd(int protocol, int lba48)
617 int rcmd = 0, wcmd = 0;
622 rcmd = ATA_CMD_PIO_READ_EXT;
623 wcmd = ATA_CMD_PIO_WRITE_EXT;
625 rcmd = ATA_CMD_PIO_READ;
626 wcmd = ATA_CMD_PIO_WRITE;
632 rcmd = ATA_CMD_READ_EXT;
633 wcmd = ATA_CMD_WRITE_EXT;
636 wcmd = ATA_CMD_WRITE;
644 return rcmd | (wcmd << 8);
648 * ata_dev_set_protocol - set taskfile protocol and r/w commands
649 * @dev: device to examine and configure
651 * Examine the device configuration, after we have
652 * read the identify-device page and configured the
653 * data transfer mode. Set internal state related to
654 * the ATA taskfile protocol (pio, pio mult, dma, etc.)
655 * and calculate the proper read/write commands to use.
660 static void ata_dev_set_protocol(struct ata_device *dev)
662 int pio = (dev->flags & ATA_DFLAG_PIO);
663 int lba48 = (dev->flags & ATA_DFLAG_LBA48);
667 proto = dev->xfer_protocol = ATA_PROT_PIO;
669 proto = dev->xfer_protocol = ATA_PROT_DMA;
671 cmd = ata_prot_to_cmd(proto, lba48);
675 dev->read_cmd = cmd & 0xff;
676 dev->write_cmd = (cmd >> 8) & 0xff;
679 static const char * xfer_mode_str[] = {
699 * ata_udma_string - convert UDMA bit offset to string
700 * @mask: mask of bits supported; only highest bit counts.
702 * Determine string which represents the highest speed
703 * (highest bit in @udma_mask).
709 * Constant C string representing highest speed listed in
710 * @udma_mask, or the constant C string "<n/a>".
713 static const char *ata_mode_string(unsigned int mask)
717 for (i = 7; i >= 0; i--)
720 for (i = ATA_SHIFT_MWDMA + 2; i >= ATA_SHIFT_MWDMA; i--)
723 for (i = ATA_SHIFT_PIO + 4; i >= ATA_SHIFT_PIO; i--)
730 return xfer_mode_str[i];
734 * ata_pio_devchk - PATA device presence detection
735 * @ap: ATA channel to examine
736 * @device: Device to examine (starting at zero)
738 * This technique was originally described in
739 * Hale Landis's ATADRVR (www.ata-atapi.com), and
740 * later found its way into the ATA/ATAPI spec.
742 * Write a pattern to the ATA shadow registers,
743 * and if a device is present, it will respond by
744 * correctly storing and echoing back the
745 * ATA shadow register contents.
751 static unsigned int ata_pio_devchk(struct ata_port *ap,
754 struct ata_ioports *ioaddr = &ap->ioaddr;
757 ap->ops->dev_select(ap, device);
759 outb(0x55, ioaddr->nsect_addr);
760 outb(0xaa, ioaddr->lbal_addr);
762 outb(0xaa, ioaddr->nsect_addr);
763 outb(0x55, ioaddr->lbal_addr);
765 outb(0x55, ioaddr->nsect_addr);
766 outb(0xaa, ioaddr->lbal_addr);
768 nsect = inb(ioaddr->nsect_addr);
769 lbal = inb(ioaddr->lbal_addr);
771 if ((nsect == 0x55) && (lbal == 0xaa))
772 return 1; /* we found a device */
774 return 0; /* nothing found */
778 * ata_mmio_devchk - PATA device presence detection
779 * @ap: ATA channel to examine
780 * @device: Device to examine (starting at zero)
782 * This technique was originally described in
783 * Hale Landis's ATADRVR (www.ata-atapi.com), and
784 * later found its way into the ATA/ATAPI spec.
786 * Write a pattern to the ATA shadow registers,
787 * and if a device is present, it will respond by
788 * correctly storing and echoing back the
789 * ATA shadow register contents.
795 static unsigned int ata_mmio_devchk(struct ata_port *ap,
798 struct ata_ioports *ioaddr = &ap->ioaddr;
801 ap->ops->dev_select(ap, device);
803 writeb(0x55, (void __iomem *) ioaddr->nsect_addr);
804 writeb(0xaa, (void __iomem *) ioaddr->lbal_addr);
806 writeb(0xaa, (void __iomem *) ioaddr->nsect_addr);
807 writeb(0x55, (void __iomem *) ioaddr->lbal_addr);
809 writeb(0x55, (void __iomem *) ioaddr->nsect_addr);
810 writeb(0xaa, (void __iomem *) ioaddr->lbal_addr);
812 nsect = readb((void __iomem *) ioaddr->nsect_addr);
813 lbal = readb((void __iomem *) ioaddr->lbal_addr);
815 if ((nsect == 0x55) && (lbal == 0xaa))
816 return 1; /* we found a device */
818 return 0; /* nothing found */
822 * ata_devchk - PATA device presence detection
823 * @ap: ATA channel to examine
824 * @device: Device to examine (starting at zero)
826 * Dispatch ATA device presence detection, depending
827 * on whether we are using PIO or MMIO to talk to the
828 * ATA shadow registers.
834 static unsigned int ata_devchk(struct ata_port *ap,
837 if (ap->flags & ATA_FLAG_MMIO)
838 return ata_mmio_devchk(ap, device);
839 return ata_pio_devchk(ap, device);
843 * ata_dev_classify - determine device type based on ATA-spec signature
844 * @tf: ATA taskfile register set for device to be identified
846 * Determine from taskfile register contents whether a device is
847 * ATA or ATAPI, as per "Signature and persistence" section
848 * of ATA/PI spec (volume 1, sect 5.14).
854 * Device type, %ATA_DEV_ATA, %ATA_DEV_ATAPI, or %ATA_DEV_UNKNOWN
855 * the event of failure.
858 unsigned int ata_dev_classify(struct ata_taskfile *tf)
860 /* Apple's open source Darwin code hints that some devices only
861 * put a proper signature into the LBA mid/high registers,
862 * So, we only check those. It's sufficient for uniqueness.
865 if (((tf->lbam == 0) && (tf->lbah == 0)) ||
866 ((tf->lbam == 0x3c) && (tf->lbah == 0xc3))) {
867 DPRINTK("found ATA device by sig\n");
871 if (((tf->lbam == 0x14) && (tf->lbah == 0xeb)) ||
872 ((tf->lbam == 0x69) && (tf->lbah == 0x96))) {
873 DPRINTK("found ATAPI device by sig\n");
874 return ATA_DEV_ATAPI;
877 DPRINTK("unknown device\n");
878 return ATA_DEV_UNKNOWN;
882 * ata_dev_try_classify - Parse returned ATA device signature
883 * @ap: ATA channel to examine
884 * @device: Device to examine (starting at zero)
886 * After an event -- SRST, E.D.D., or SATA COMRESET -- occurs,
887 * an ATA/ATAPI-defined set of values is placed in the ATA
888 * shadow registers, indicating the results of device detection
891 * Select the ATA device, and read the values from the ATA shadow
892 * registers. Then parse according to the Error register value,
893 * and the spec-defined values examined by ata_dev_classify().
899 static u8 ata_dev_try_classify(struct ata_port *ap, unsigned int device)
901 struct ata_device *dev = &ap->device[device];
902 struct ata_taskfile tf;
906 ap->ops->dev_select(ap, device);
908 memset(&tf, 0, sizeof(tf));
910 err = ata_chk_err(ap);
911 ap->ops->tf_read(ap, &tf);
913 dev->class = ATA_DEV_NONE;
915 /* see if device passed diags */
918 else if ((device == 0) && (err == 0x81))
923 /* determine if device if ATA or ATAPI */
924 class = ata_dev_classify(&tf);
925 if (class == ATA_DEV_UNKNOWN)
927 if ((class == ATA_DEV_ATA) && (ata_chk_status(ap) == 0))
936 * ata_dev_id_string - Convert IDENTIFY DEVICE page into string
937 * @id: IDENTIFY DEVICE results we will examine
938 * @s: string into which data is output
939 * @ofs: offset into identify device page
940 * @len: length of string to return. must be an even number.
942 * The strings in the IDENTIFY DEVICE page are broken up into
943 * 16-bit chunks. Run through the string, and output each
944 * 8-bit chunk linearly, regardless of platform.
950 void ata_dev_id_string(u16 *id, unsigned char *s,
951 unsigned int ofs, unsigned int len)
971 * ata_noop_dev_select - Select device 0/1 on ATA bus
972 * @ap: ATA channel to manipulate
973 * @device: ATA device (numbered from zero) to select
975 * This function performs no actual function.
977 * May be used as the dev_select() entry in ata_port_operations.
982 void ata_noop_dev_select (struct ata_port *ap, unsigned int device)
988 * ata_std_dev_select - Select device 0/1 on ATA bus
989 * @ap: ATA channel to manipulate
990 * @device: ATA device (numbered from zero) to select
992 * Use the method defined in the ATA specification to
993 * make either device 0, or device 1, active on the
994 * ATA channel. Works with both PIO and MMIO.
996 * May be used as the dev_select() entry in ata_port_operations.
1002 void ata_std_dev_select (struct ata_port *ap, unsigned int device)
1007 tmp = ATA_DEVICE_OBS;
1009 tmp = ATA_DEVICE_OBS | ATA_DEV1;
1011 if (ap->flags & ATA_FLAG_MMIO) {
1012 writeb(tmp, (void __iomem *) ap->ioaddr.device_addr);
1014 outb(tmp, ap->ioaddr.device_addr);
1016 ata_pause(ap); /* needed; also flushes, for mmio */
1020 * ata_dev_select - Select device 0/1 on ATA bus
1021 * @ap: ATA channel to manipulate
1022 * @device: ATA device (numbered from zero) to select
1023 * @wait: non-zero to wait for Status register BSY bit to clear
1024 * @can_sleep: non-zero if context allows sleeping
1026 * Use the method defined in the ATA specification to
1027 * make either device 0, or device 1, active on the
1030 * This is a high-level version of ata_std_dev_select(),
1031 * which additionally provides the services of inserting
1032 * the proper pauses and status polling, where needed.
1038 void ata_dev_select(struct ata_port *ap, unsigned int device,
1039 unsigned int wait, unsigned int can_sleep)
1041 VPRINTK("ENTER, ata%u: device %u, wait %u\n",
1042 ap->id, device, wait);
1047 ap->ops->dev_select(ap, device);
1050 if (can_sleep && ap->device[device].class == ATA_DEV_ATAPI)
1057 * ata_dump_id - IDENTIFY DEVICE info debugging output
1058 * @dev: Device whose IDENTIFY DEVICE page we will dump
1060 * Dump selected 16-bit words from a detected device's
1061 * IDENTIFY PAGE page.
1067 static inline void ata_dump_id(struct ata_device *dev)
1069 DPRINTK("49==0x%04x "
1079 DPRINTK("80==0x%04x "
1089 DPRINTK("88==0x%04x "
1096 * ata_dev_identify - obtain IDENTIFY x DEVICE page
1097 * @ap: port on which device we wish to probe resides
1098 * @device: device bus address, starting at zero
1100 * Following bus reset, we issue the IDENTIFY [PACKET] DEVICE
1101 * command, and read back the 512-byte device information page.
1102 * The device information page is fed to us via the standard
1103 * PIO-IN protocol, but we hand-code it here. (TODO: investigate
1104 * using standard PIO-IN paths)
1106 * After reading the device information page, we use several
1107 * bits of information from it to initialize data structures
1108 * that will be used during the lifetime of the ata_device.
1109 * Other data from the info page is used to disqualify certain
1110 * older ATA devices we do not wish to support.
1113 * Inherited from caller. Some functions called by this function
1114 * obtain the host_set lock.
1117 static void ata_dev_identify(struct ata_port *ap, unsigned int device)
1119 struct ata_device *dev = &ap->device[device];
1122 unsigned long xfer_modes;
1124 unsigned int using_edd;
1125 DECLARE_COMPLETION(wait);
1126 struct ata_queued_cmd *qc;
1127 unsigned long flags;
1130 if (!ata_dev_present(dev)) {
1131 DPRINTK("ENTER/EXIT (host %u, dev %u) -- nodev\n",
1136 if (ap->flags & (ATA_FLAG_SRST | ATA_FLAG_SATA_RESET))
1141 DPRINTK("ENTER, host %u, dev %u\n", ap->id, device);
1143 assert (dev->class == ATA_DEV_ATA || dev->class == ATA_DEV_ATAPI ||
1144 dev->class == ATA_DEV_NONE);
1146 ata_dev_select(ap, device, 1, 1); /* select device 0/1 */
1148 qc = ata_qc_new_init(ap, dev);
1151 ata_sg_init_one(qc, dev->id, sizeof(dev->id));
1152 qc->dma_dir = DMA_FROM_DEVICE;
1153 qc->tf.protocol = ATA_PROT_PIO;
1157 if (dev->class == ATA_DEV_ATA) {
1158 qc->tf.command = ATA_CMD_ID_ATA;
1159 DPRINTK("do ATA identify\n");
1161 qc->tf.command = ATA_CMD_ID_ATAPI;
1162 DPRINTK("do ATAPI identify\n");
1165 qc->waiting = &wait;
1166 qc->complete_fn = ata_qc_complete_noop;
1168 spin_lock_irqsave(&ap->host_set->lock, flags);
1169 rc = ata_qc_issue(qc);
1170 spin_unlock_irqrestore(&ap->host_set->lock, flags);
1175 wait_for_completion(&wait);
1177 status = ata_chk_status(ap);
1178 if (status & ATA_ERR) {
1180 * arg! EDD works for all test cases, but seems to return
1181 * the ATA signature for some ATAPI devices. Until the
1182 * reason for this is found and fixed, we fix up the mess
1183 * here. If IDENTIFY DEVICE returns command aborted
1184 * (as ATAPI devices do), then we issue an
1185 * IDENTIFY PACKET DEVICE.
1187 * ATA software reset (SRST, the default) does not appear
1188 * to have this problem.
1190 if ((using_edd) && (qc->tf.command == ATA_CMD_ID_ATA)) {
1191 u8 err = ata_chk_err(ap);
1192 if (err & ATA_ABORTED) {
1193 dev->class = ATA_DEV_ATAPI;
1204 swap_buf_le16(dev->id, ATA_ID_WORDS);
1206 /* print device capabilities */
1207 printk(KERN_DEBUG "ata%u: dev %u cfg "
1208 "49:%04x 82:%04x 83:%04x 84:%04x 85:%04x 86:%04x 87:%04x 88:%04x\n",
1209 ap->id, device, dev->id[49],
1210 dev->id[82], dev->id[83], dev->id[84],
1211 dev->id[85], dev->id[86], dev->id[87],
1215 * common ATA, ATAPI feature tests
1218 /* we require LBA and DMA support (bits 8 & 9 of word 49) */
1219 if (!ata_id_has_dma(dev->id) || !ata_id_has_lba(dev->id)) {
1220 printk(KERN_DEBUG "ata%u: no dma/lba\n", ap->id);
1224 /* quick-n-dirty find max transfer mode; for printk only */
1225 xfer_modes = dev->id[ATA_ID_UDMA_MODES];
1227 xfer_modes = (dev->id[ATA_ID_MWDMA_MODES]) << ATA_SHIFT_MWDMA;
1229 xfer_modes = (dev->id[ATA_ID_PIO_MODES]) << (ATA_SHIFT_PIO + 3);
1230 xfer_modes |= (0x7 << ATA_SHIFT_PIO);
1235 /* ATA-specific feature tests */
1236 if (dev->class == ATA_DEV_ATA) {
1237 if (!ata_id_is_ata(dev->id)) /* sanity check */
1240 tmp = dev->id[ATA_ID_MAJOR_VER];
1241 for (i = 14; i >= 1; i--)
1245 /* we require at least ATA-3 */
1247 printk(KERN_DEBUG "ata%u: no ATA-3\n", ap->id);
1251 if (ata_id_has_lba48(dev->id)) {
1252 dev->flags |= ATA_DFLAG_LBA48;
1253 dev->n_sectors = ata_id_u64(dev->id, 100);
1255 dev->n_sectors = ata_id_u32(dev->id, 60);
1258 ap->host->max_cmd_len = 16;
1260 /* print device info to dmesg */
1261 printk(KERN_INFO "ata%u: dev %u ATA, max %s, %Lu sectors:%s\n",
1263 ata_mode_string(xfer_modes),
1264 (unsigned long long)dev->n_sectors,
1265 dev->flags & ATA_DFLAG_LBA48 ? " lba48" : "");
1268 /* ATAPI-specific feature tests */
1270 if (ata_id_is_ata(dev->id)) /* sanity check */
1273 rc = atapi_cdb_len(dev->id);
1274 if ((rc < 12) || (rc > ATAPI_CDB_LEN)) {
1275 printk(KERN_WARNING "ata%u: unsupported CDB len\n", ap->id);
1278 ap->cdb_len = (unsigned int) rc;
1279 ap->host->max_cmd_len = (unsigned char) ap->cdb_len;
1281 /* print device info to dmesg */
1282 printk(KERN_INFO "ata%u: dev %u ATAPI, max %s\n",
1284 ata_mode_string(xfer_modes));
1287 DPRINTK("EXIT, drv_stat = 0x%x\n", ata_chk_status(ap));
1291 printk(KERN_WARNING "ata%u: dev %u not supported, ignoring\n",
1294 dev->class++; /* converts ATA_DEV_xxx into ATA_DEV_xxx_UNSUP */
1295 DPRINTK("EXIT, err\n");
1299 static inline u8 ata_dev_knobble(struct ata_port *ap)
1301 return ((ap->cbl == ATA_CBL_SATA) && (!ata_id_is_sata(ap->device->id)));
1305 * ata_dev_config - Run device specific handlers and check for
1306 * SATA->PATA bridges
1313 void ata_dev_config(struct ata_port *ap, unsigned int i)
1315 /* limit bridge transfers to udma5, 200 sectors */
1316 if (ata_dev_knobble(ap)) {
1317 printk(KERN_INFO "ata%u(%u): applying bridge limits\n",
1318 ap->id, ap->device->devno);
1319 ap->udma_mask &= ATA_UDMA5;
1320 ap->host->max_sectors = ATA_MAX_SECTORS;
1321 ap->host->hostt->max_sectors = ATA_MAX_SECTORS;
1322 ap->device->flags |= ATA_DFLAG_LOCK_SECTORS;
1325 if (ap->ops->dev_config)
1326 ap->ops->dev_config(ap, &ap->device[i]);
1330 * ata_bus_probe - Reset and probe ATA bus
1333 * Master ATA bus probing function. Initiates a hardware-dependent
1334 * bus reset, then attempts to identify any devices found on
1338 * PCI/etc. bus probe sem.
1341 * Zero on success, non-zero on error.
1344 static int ata_bus_probe(struct ata_port *ap)
1346 unsigned int i, found = 0;
1348 ap->ops->phy_reset(ap);
1349 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1352 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1353 ata_dev_identify(ap, i);
1354 if (ata_dev_present(&ap->device[i])) {
1356 ata_dev_config(ap,i);
1360 if ((!found) || (ap->flags & ATA_FLAG_PORT_DISABLED))
1361 goto err_out_disable;
1364 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1365 goto err_out_disable;
1370 ap->ops->port_disable(ap);
1376 * ata_port_probe - Mark port as enabled
1377 * @ap: Port for which we indicate enablement
1379 * Modify @ap data structure such that the system
1380 * thinks that the entire port is enabled.
1382 * LOCKING: host_set lock, or some other form of
1386 void ata_port_probe(struct ata_port *ap)
1388 ap->flags &= ~ATA_FLAG_PORT_DISABLED;
1392 * __sata_phy_reset - Wake/reset a low-level SATA PHY
1393 * @ap: SATA port associated with target SATA PHY.
1395 * This function issues commands to standard SATA Sxxx
1396 * PHY registers, to wake up the phy (and device), and
1397 * clear any reset condition.
1400 * PCI/etc. bus probe sem.
1403 void __sata_phy_reset(struct ata_port *ap)
1406 unsigned long timeout = jiffies + (HZ * 5);
1408 if (ap->flags & ATA_FLAG_SATA_RESET) {
1409 /* issue phy wake/reset */
1410 scr_write_flush(ap, SCR_CONTROL, 0x301);
1411 /* Couldn't find anything in SATA I/II specs, but
1412 * AHCI-1.1 10.4.2 says at least 1 ms. */
1415 scr_write_flush(ap, SCR_CONTROL, 0x300); /* phy wake/clear reset */
1417 /* wait for phy to become ready, if necessary */
1420 sstatus = scr_read(ap, SCR_STATUS);
1421 if ((sstatus & 0xf) != 1)
1423 } while (time_before(jiffies, timeout));
1425 /* TODO: phy layer with polling, timeouts, etc. */
1426 if (sata_dev_present(ap))
1429 sstatus = scr_read(ap, SCR_STATUS);
1430 printk(KERN_INFO "ata%u: no device found (phy stat %08x)\n",
1432 ata_port_disable(ap);
1435 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1438 if (ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT)) {
1439 ata_port_disable(ap);
1443 ap->cbl = ATA_CBL_SATA;
1447 * sata_phy_reset - Reset SATA bus.
1448 * @ap: SATA port associated with target SATA PHY.
1450 * This function resets the SATA bus, and then probes
1451 * the bus for devices.
1454 * PCI/etc. bus probe sem.
1457 void sata_phy_reset(struct ata_port *ap)
1459 __sata_phy_reset(ap);
1460 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1466 * ata_port_disable - Disable port.
1467 * @ap: Port to be disabled.
1469 * Modify @ap data structure such that the system
1470 * thinks that the entire port is disabled, and should
1471 * never attempt to probe or communicate with devices
1474 * LOCKING: host_set lock, or some other form of
1478 void ata_port_disable(struct ata_port *ap)
1480 ap->device[0].class = ATA_DEV_NONE;
1481 ap->device[1].class = ATA_DEV_NONE;
1482 ap->flags |= ATA_FLAG_PORT_DISABLED;
1488 } xfer_mode_classes[] = {
1489 { ATA_SHIFT_UDMA, XFER_UDMA_0 },
1490 { ATA_SHIFT_MWDMA, XFER_MW_DMA_0 },
1491 { ATA_SHIFT_PIO, XFER_PIO_0 },
1494 static inline u8 base_from_shift(unsigned int shift)
1498 for (i = 0; i < ARRAY_SIZE(xfer_mode_classes); i++)
1499 if (xfer_mode_classes[i].shift == shift)
1500 return xfer_mode_classes[i].base;
1505 static void ata_dev_set_mode(struct ata_port *ap, struct ata_device *dev)
1510 if (!ata_dev_present(dev) || (ap->flags & ATA_FLAG_PORT_DISABLED))
1513 if (dev->xfer_shift == ATA_SHIFT_PIO)
1514 dev->flags |= ATA_DFLAG_PIO;
1516 ata_dev_set_xfermode(ap, dev);
1518 base = base_from_shift(dev->xfer_shift);
1519 ofs = dev->xfer_mode - base;
1520 idx = ofs + dev->xfer_shift;
1521 WARN_ON(idx >= ARRAY_SIZE(xfer_mode_str));
1523 DPRINTK("idx=%d xfer_shift=%u, xfer_mode=0x%x, base=0x%x, offset=%d\n",
1524 idx, dev->xfer_shift, (int)dev->xfer_mode, (int)base, ofs);
1526 printk(KERN_INFO "ata%u: dev %u configured for %s\n",
1527 ap->id, dev->devno, xfer_mode_str[idx]);
1530 static int ata_host_set_pio(struct ata_port *ap)
1536 mask = ata_get_mode_mask(ap, ATA_SHIFT_PIO);
1539 printk(KERN_WARNING "ata%u: no PIO support\n", ap->id);
1543 base = base_from_shift(ATA_SHIFT_PIO);
1544 xfer_mode = base + x;
1546 DPRINTK("base 0x%x xfer_mode 0x%x mask 0x%x x %d\n",
1547 (int)base, (int)xfer_mode, mask, x);
1549 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1550 struct ata_device *dev = &ap->device[i];
1551 if (ata_dev_present(dev)) {
1552 dev->pio_mode = xfer_mode;
1553 dev->xfer_mode = xfer_mode;
1554 dev->xfer_shift = ATA_SHIFT_PIO;
1555 if (ap->ops->set_piomode)
1556 ap->ops->set_piomode(ap, dev);
1563 static void ata_host_set_dma(struct ata_port *ap, u8 xfer_mode,
1564 unsigned int xfer_shift)
1568 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1569 struct ata_device *dev = &ap->device[i];
1570 if (ata_dev_present(dev)) {
1571 dev->dma_mode = xfer_mode;
1572 dev->xfer_mode = xfer_mode;
1573 dev->xfer_shift = xfer_shift;
1574 if (ap->ops->set_dmamode)
1575 ap->ops->set_dmamode(ap, dev);
1581 * ata_set_mode - Program timings and issue SET FEATURES - XFER
1582 * @ap: port on which timings will be programmed
1584 * Set ATA device disk transfer mode (PIO3, UDMA6, etc.).
1587 * PCI/etc. bus probe sem.
1590 static void ata_set_mode(struct ata_port *ap)
1592 unsigned int i, xfer_shift;
1596 /* step 1: always set host PIO timings */
1597 rc = ata_host_set_pio(ap);
1601 /* step 2: choose the best data xfer mode */
1602 xfer_mode = xfer_shift = 0;
1603 rc = ata_choose_xfer_mode(ap, &xfer_mode, &xfer_shift);
1607 /* step 3: if that xfer mode isn't PIO, set host DMA timings */
1608 if (xfer_shift != ATA_SHIFT_PIO)
1609 ata_host_set_dma(ap, xfer_mode, xfer_shift);
1611 /* step 4: update devices' xfer mode */
1612 ata_dev_set_mode(ap, &ap->device[0]);
1613 ata_dev_set_mode(ap, &ap->device[1]);
1615 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1618 if (ap->ops->post_set_mode)
1619 ap->ops->post_set_mode(ap);
1621 for (i = 0; i < 2; i++) {
1622 struct ata_device *dev = &ap->device[i];
1623 ata_dev_set_protocol(dev);
1629 ata_port_disable(ap);
1633 * ata_busy_sleep - sleep until BSY clears, or timeout
1634 * @ap: port containing status register to be polled
1635 * @tmout_pat: impatience timeout
1636 * @tmout: overall timeout
1638 * Sleep until ATA Status register bit BSY clears,
1639 * or a timeout occurs.
1645 static unsigned int ata_busy_sleep (struct ata_port *ap,
1646 unsigned long tmout_pat,
1647 unsigned long tmout)
1649 unsigned long timer_start, timeout;
1652 status = ata_busy_wait(ap, ATA_BUSY, 300);
1653 timer_start = jiffies;
1654 timeout = timer_start + tmout_pat;
1655 while ((status & ATA_BUSY) && (time_before(jiffies, timeout))) {
1657 status = ata_busy_wait(ap, ATA_BUSY, 3);
1660 if (status & ATA_BUSY)
1661 printk(KERN_WARNING "ata%u is slow to respond, "
1662 "please be patient\n", ap->id);
1664 timeout = timer_start + tmout;
1665 while ((status & ATA_BUSY) && (time_before(jiffies, timeout))) {
1667 status = ata_chk_status(ap);
1670 if (status & ATA_BUSY) {
1671 printk(KERN_ERR "ata%u failed to respond (%lu secs)\n",
1672 ap->id, tmout / HZ);
1679 static void ata_bus_post_reset(struct ata_port *ap, unsigned int devmask)
1681 struct ata_ioports *ioaddr = &ap->ioaddr;
1682 unsigned int dev0 = devmask & (1 << 0);
1683 unsigned int dev1 = devmask & (1 << 1);
1684 unsigned long timeout;
1686 /* if device 0 was found in ata_devchk, wait for its
1690 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
1692 /* if device 1 was found in ata_devchk, wait for
1693 * register access, then wait for BSY to clear
1695 timeout = jiffies + ATA_TMOUT_BOOT;
1699 ap->ops->dev_select(ap, 1);
1700 if (ap->flags & ATA_FLAG_MMIO) {
1701 nsect = readb((void __iomem *) ioaddr->nsect_addr);
1702 lbal = readb((void __iomem *) ioaddr->lbal_addr);
1704 nsect = inb(ioaddr->nsect_addr);
1705 lbal = inb(ioaddr->lbal_addr);
1707 if ((nsect == 1) && (lbal == 1))
1709 if (time_after(jiffies, timeout)) {
1713 msleep(50); /* give drive a breather */
1716 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
1718 /* is all this really necessary? */
1719 ap->ops->dev_select(ap, 0);
1721 ap->ops->dev_select(ap, 1);
1723 ap->ops->dev_select(ap, 0);
1727 * ata_bus_edd - Issue EXECUTE DEVICE DIAGNOSTIC command.
1728 * @ap: Port to reset and probe
1730 * Use the EXECUTE DEVICE DIAGNOSTIC command to reset and
1731 * probe the bus. Not often used these days.
1734 * PCI/etc. bus probe sem.
1738 static unsigned int ata_bus_edd(struct ata_port *ap)
1740 struct ata_taskfile tf;
1742 /* set up execute-device-diag (bus reset) taskfile */
1743 /* also, take interrupts to a known state (disabled) */
1744 DPRINTK("execute-device-diag\n");
1745 ata_tf_init(ap, &tf, 0);
1747 tf.command = ATA_CMD_EDD;
1748 tf.protocol = ATA_PROT_NODATA;
1751 ata_tf_to_host(ap, &tf);
1753 /* spec says at least 2ms. but who knows with those
1754 * crazy ATAPI devices...
1758 return ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
1761 static unsigned int ata_bus_softreset(struct ata_port *ap,
1762 unsigned int devmask)
1764 struct ata_ioports *ioaddr = &ap->ioaddr;
1766 DPRINTK("ata%u: bus reset via SRST\n", ap->id);
1768 /* software reset. causes dev0 to be selected */
1769 if (ap->flags & ATA_FLAG_MMIO) {
1770 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
1771 udelay(20); /* FIXME: flush */
1772 writeb(ap->ctl | ATA_SRST, (void __iomem *) ioaddr->ctl_addr);
1773 udelay(20); /* FIXME: flush */
1774 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
1776 outb(ap->ctl, ioaddr->ctl_addr);
1778 outb(ap->ctl | ATA_SRST, ioaddr->ctl_addr);
1780 outb(ap->ctl, ioaddr->ctl_addr);
1783 /* spec mandates ">= 2ms" before checking status.
1784 * We wait 150ms, because that was the magic delay used for
1785 * ATAPI devices in Hale Landis's ATADRVR, for the period of time
1786 * between when the ATA command register is written, and then
1787 * status is checked. Because waiting for "a while" before
1788 * checking status is fine, post SRST, we perform this magic
1789 * delay here as well.
1793 ata_bus_post_reset(ap, devmask);
1799 * ata_bus_reset - reset host port and associated ATA channel
1800 * @ap: port to reset
1802 * This is typically the first time we actually start issuing
1803 * commands to the ATA channel. We wait for BSY to clear, then
1804 * issue EXECUTE DEVICE DIAGNOSTIC command, polling for its
1805 * result. Determine what devices, if any, are on the channel
1806 * by looking at the device 0/1 error register. Look at the signature
1807 * stored in each device's taskfile registers, to determine if
1808 * the device is ATA or ATAPI.
1811 * PCI/etc. bus probe sem.
1812 * Obtains host_set lock.
1815 * Sets ATA_FLAG_PORT_DISABLED if bus reset fails.
1818 void ata_bus_reset(struct ata_port *ap)
1820 struct ata_ioports *ioaddr = &ap->ioaddr;
1821 unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS;
1823 unsigned int dev0, dev1 = 0, rc = 0, devmask = 0;
1825 DPRINTK("ENTER, host %u, port %u\n", ap->id, ap->port_no);
1827 /* determine if device 0/1 are present */
1828 if (ap->flags & ATA_FLAG_SATA_RESET)
1831 dev0 = ata_devchk(ap, 0);
1833 dev1 = ata_devchk(ap, 1);
1837 devmask |= (1 << 0);
1839 devmask |= (1 << 1);
1841 /* select device 0 again */
1842 ap->ops->dev_select(ap, 0);
1844 /* issue bus reset */
1845 if (ap->flags & ATA_FLAG_SRST)
1846 rc = ata_bus_softreset(ap, devmask);
1847 else if ((ap->flags & ATA_FLAG_SATA_RESET) == 0) {
1848 /* set up device control */
1849 if (ap->flags & ATA_FLAG_MMIO)
1850 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
1852 outb(ap->ctl, ioaddr->ctl_addr);
1853 rc = ata_bus_edd(ap);
1860 * determine by signature whether we have ATA or ATAPI devices
1862 err = ata_dev_try_classify(ap, 0);
1863 if ((slave_possible) && (err != 0x81))
1864 ata_dev_try_classify(ap, 1);
1866 /* re-enable interrupts */
1867 if (ap->ioaddr.ctl_addr) /* FIXME: hack. create a hook instead */
1870 /* is double-select really necessary? */
1871 if (ap->device[1].class != ATA_DEV_NONE)
1872 ap->ops->dev_select(ap, 1);
1873 if (ap->device[0].class != ATA_DEV_NONE)
1874 ap->ops->dev_select(ap, 0);
1876 /* if no devices were detected, disable this port */
1877 if ((ap->device[0].class == ATA_DEV_NONE) &&
1878 (ap->device[1].class == ATA_DEV_NONE))
1881 if (ap->flags & (ATA_FLAG_SATA_RESET | ATA_FLAG_SRST)) {
1882 /* set up device control for ATA_FLAG_SATA_RESET */
1883 if (ap->flags & ATA_FLAG_MMIO)
1884 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
1886 outb(ap->ctl, ioaddr->ctl_addr);
1893 printk(KERN_ERR "ata%u: disabling port\n", ap->id);
1894 ap->ops->port_disable(ap);
1899 static void ata_pr_blacklisted(struct ata_port *ap, struct ata_device *dev)
1901 printk(KERN_WARNING "ata%u: dev %u is on DMA blacklist, disabling DMA\n",
1902 ap->id, dev->devno);
1905 static const char * ata_dma_blacklist [] = {
1924 "Toshiba CD-ROM XM-6202B",
1925 "TOSHIBA CD-ROM XM-1702BC",
1927 "E-IDE CD-ROM CR-840",
1930 "SAMSUNG CD-ROM SC-148C",
1931 "SAMSUNG CD-ROM SC",
1933 "ATAPI CD-ROM DRIVE 40X MAXIMUM",
1937 static int ata_dma_blacklisted(struct ata_port *ap, struct ata_device *dev)
1939 unsigned char model_num[40];
1944 ata_dev_id_string(dev->id, model_num, ATA_ID_PROD_OFS,
1947 len = strnlen(s, sizeof(model_num));
1949 /* ATAPI specifies that empty space is blank-filled; remove blanks */
1950 while ((len > 0) && (s[len - 1] == ' ')) {
1955 for (i = 0; i < ARRAY_SIZE(ata_dma_blacklist); i++)
1956 if (!strncmp(ata_dma_blacklist[i], s, len))
1962 static unsigned int ata_get_mode_mask(struct ata_port *ap, int shift)
1964 struct ata_device *master, *slave;
1967 master = &ap->device[0];
1968 slave = &ap->device[1];
1970 assert (ata_dev_present(master) || ata_dev_present(slave));
1972 if (shift == ATA_SHIFT_UDMA) {
1973 mask = ap->udma_mask;
1974 if (ata_dev_present(master)) {
1975 mask &= (master->id[ATA_ID_UDMA_MODES] & 0xff);
1976 if (ata_dma_blacklisted(ap, master)) {
1978 ata_pr_blacklisted(ap, master);
1981 if (ata_dev_present(slave)) {
1982 mask &= (slave->id[ATA_ID_UDMA_MODES] & 0xff);
1983 if (ata_dma_blacklisted(ap, slave)) {
1985 ata_pr_blacklisted(ap, slave);
1989 else if (shift == ATA_SHIFT_MWDMA) {
1990 mask = ap->mwdma_mask;
1991 if (ata_dev_present(master)) {
1992 mask &= (master->id[ATA_ID_MWDMA_MODES] & 0x07);
1993 if (ata_dma_blacklisted(ap, master)) {
1995 ata_pr_blacklisted(ap, master);
1998 if (ata_dev_present(slave)) {
1999 mask &= (slave->id[ATA_ID_MWDMA_MODES] & 0x07);
2000 if (ata_dma_blacklisted(ap, slave)) {
2002 ata_pr_blacklisted(ap, slave);
2006 else if (shift == ATA_SHIFT_PIO) {
2007 mask = ap->pio_mask;
2008 if (ata_dev_present(master)) {
2009 /* spec doesn't return explicit support for
2010 * PIO0-2, so we fake it
2012 u16 tmp_mode = master->id[ATA_ID_PIO_MODES] & 0x03;
2017 if (ata_dev_present(slave)) {
2018 /* spec doesn't return explicit support for
2019 * PIO0-2, so we fake it
2021 u16 tmp_mode = slave->id[ATA_ID_PIO_MODES] & 0x03;
2028 mask = 0xffffffff; /* shut up compiler warning */
2035 /* find greatest bit */
2036 static int fgb(u32 bitmap)
2041 for (i = 0; i < 32; i++)
2042 if (bitmap & (1 << i))
2049 * ata_choose_xfer_mode - attempt to find best transfer mode
2050 * @ap: Port for which an xfer mode will be selected
2051 * @xfer_mode_out: (output) SET FEATURES - XFER MODE code
2052 * @xfer_shift_out: (output) bit shift that selects this mode
2054 * Based on host and device capabilities, determine the
2055 * maximum transfer mode that is amenable to all.
2058 * PCI/etc. bus probe sem.
2061 * Zero on success, negative on error.
2064 static int ata_choose_xfer_mode(struct ata_port *ap,
2066 unsigned int *xfer_shift_out)
2068 unsigned int mask, shift;
2071 for (i = 0; i < ARRAY_SIZE(xfer_mode_classes); i++) {
2072 shift = xfer_mode_classes[i].shift;
2073 mask = ata_get_mode_mask(ap, shift);
2077 *xfer_mode_out = xfer_mode_classes[i].base + x;
2078 *xfer_shift_out = shift;
2087 * ata_dev_set_xfermode - Issue SET FEATURES - XFER MODE command
2088 * @ap: Port associated with device @dev
2089 * @dev: Device to which command will be sent
2091 * Issue SET FEATURES - XFER MODE command to device @dev
2095 * PCI/etc. bus probe sem.
2098 static void ata_dev_set_xfermode(struct ata_port *ap, struct ata_device *dev)
2100 DECLARE_COMPLETION(wait);
2101 struct ata_queued_cmd *qc;
2103 unsigned long flags;
2105 /* set up set-features taskfile */
2106 DPRINTK("set features - xfer mode\n");
2108 qc = ata_qc_new_init(ap, dev);
2111 qc->tf.command = ATA_CMD_SET_FEATURES;
2112 qc->tf.feature = SETFEATURES_XFER;
2113 qc->tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
2114 qc->tf.protocol = ATA_PROT_NODATA;
2115 qc->tf.nsect = dev->xfer_mode;
2117 qc->waiting = &wait;
2118 qc->complete_fn = ata_qc_complete_noop;
2120 spin_lock_irqsave(&ap->host_set->lock, flags);
2121 rc = ata_qc_issue(qc);
2122 spin_unlock_irqrestore(&ap->host_set->lock, flags);
2125 ata_port_disable(ap);
2127 wait_for_completion(&wait);
2133 * ata_sg_clean - Unmap DMA memory associated with command
2134 * @qc: Command containing DMA memory to be released
2136 * Unmap all mapped DMA memory associated with this command.
2139 * spin_lock_irqsave(host_set lock)
2142 static void ata_sg_clean(struct ata_queued_cmd *qc)
2144 struct ata_port *ap = qc->ap;
2145 struct scatterlist *sg = qc->sg;
2146 int dir = qc->dma_dir;
2148 assert(qc->flags & ATA_QCFLAG_DMAMAP);
2151 if (qc->flags & ATA_QCFLAG_SINGLE)
2152 assert(qc->n_elem == 1);
2154 DPRINTK("unmapping %u sg elements\n", qc->n_elem);
2156 if (qc->flags & ATA_QCFLAG_SG)
2157 dma_unmap_sg(ap->host_set->dev, sg, qc->n_elem, dir);
2159 dma_unmap_single(ap->host_set->dev, sg_dma_address(&sg[0]),
2160 sg_dma_len(&sg[0]), dir);
2162 qc->flags &= ~ATA_QCFLAG_DMAMAP;
2167 * ata_fill_sg - Fill PCI IDE PRD table
2168 * @qc: Metadata associated with taskfile to be transferred
2170 * Fill PCI IDE PRD (scatter-gather) table with segments
2171 * associated with the current disk command.
2174 * spin_lock_irqsave(host_set lock)
2177 static void ata_fill_sg(struct ata_queued_cmd *qc)
2179 struct scatterlist *sg = qc->sg;
2180 struct ata_port *ap = qc->ap;
2181 unsigned int idx, nelem;
2184 assert(qc->n_elem > 0);
2187 for (nelem = qc->n_elem; nelem; nelem--,sg++) {
2191 /* determine if physical DMA addr spans 64K boundary.
2192 * Note h/w doesn't support 64-bit, so we unconditionally
2193 * truncate dma_addr_t to u32.
2195 addr = (u32) sg_dma_address(sg);
2196 sg_len = sg_dma_len(sg);
2199 offset = addr & 0xffff;
2201 if ((offset + sg_len) > 0x10000)
2202 len = 0x10000 - offset;
2204 ap->prd[idx].addr = cpu_to_le32(addr);
2205 ap->prd[idx].flags_len = cpu_to_le32(len & 0xffff);
2206 VPRINTK("PRD[%u] = (0x%X, 0x%X)\n", idx, addr, len);
2215 ap->prd[idx - 1].flags_len |= cpu_to_le32(ATA_PRD_EOT);
2218 * ata_check_atapi_dma - Check whether ATAPI DMA can be supported
2219 * @qc: Metadata associated with taskfile to check
2221 * Allow low-level driver to filter ATA PACKET commands, returning
2222 * a status indicating whether or not it is OK to use DMA for the
2223 * supplied PACKET command.
2226 * spin_lock_irqsave(host_set lock)
2228 * RETURNS: 0 when ATAPI DMA can be used
2231 int ata_check_atapi_dma(struct ata_queued_cmd *qc)
2233 struct ata_port *ap = qc->ap;
2234 int rc = 0; /* Assume ATAPI DMA is OK by default */
2236 if (ap->ops->check_atapi_dma)
2237 rc = ap->ops->check_atapi_dma(qc);
2242 * ata_qc_prep - Prepare taskfile for submission
2243 * @qc: Metadata associated with taskfile to be prepared
2245 * Prepare ATA taskfile for submission.
2248 * spin_lock_irqsave(host_set lock)
2250 void ata_qc_prep(struct ata_queued_cmd *qc)
2252 if (!(qc->flags & ATA_QCFLAG_DMAMAP))
2259 * ata_sg_init_one - Associate command with memory buffer
2260 * @qc: Command to be associated
2261 * @buf: Memory buffer
2262 * @buflen: Length of memory buffer, in bytes.
2264 * Initialize the data-related elements of queued_cmd @qc
2265 * to point to a single memory buffer, @buf of byte length @buflen.
2268 * spin_lock_irqsave(host_set lock)
2274 * ata_sg_init_one - Prepare a one-entry scatter-gather list.
2275 * @qc: Queued command
2276 * @buf: transfer buffer
2277 * @buflen: length of buf
2279 * Builds a single-entry scatter-gather list to initiate a
2280 * transfer utilizing the specified buffer.
2284 void ata_sg_init_one(struct ata_queued_cmd *qc, void *buf, unsigned int buflen)
2286 struct scatterlist *sg;
2288 qc->flags |= ATA_QCFLAG_SINGLE;
2290 memset(&qc->sgent, 0, sizeof(qc->sgent));
2291 qc->sg = &qc->sgent;
2296 sg->page = virt_to_page(buf);
2297 sg->offset = (unsigned long) buf & ~PAGE_MASK;
2298 sg->length = buflen;
2302 * ata_sg_init - Associate command with scatter-gather table.
2303 * @qc: Command to be associated
2304 * @sg: Scatter-gather table.
2305 * @n_elem: Number of elements in s/g table.
2307 * Initialize the data-related elements of queued_cmd @qc
2308 * to point to a scatter-gather table @sg, containing @n_elem
2312 * spin_lock_irqsave(host_set lock)
2317 * ata_sg_init - Assign a scatter gather list to a queued command
2318 * @qc: Queued command
2319 * @sg: Scatter-gather list
2320 * @n_elem: length of sg list
2322 * Attaches a scatter-gather list to a queued command.
2327 void ata_sg_init(struct ata_queued_cmd *qc, struct scatterlist *sg,
2328 unsigned int n_elem)
2330 qc->flags |= ATA_QCFLAG_SG;
2332 qc->n_elem = n_elem;
2336 * ata_sg_setup_one - DMA-map the memory buffer associated with a command.
2337 * @qc: Command with memory buffer to be mapped.
2339 * DMA-map the memory buffer associated with queued_cmd @qc.
2342 * spin_lock_irqsave(host_set lock)
2345 * Zero on success, negative on error.
2348 static int ata_sg_setup_one(struct ata_queued_cmd *qc)
2350 struct ata_port *ap = qc->ap;
2351 int dir = qc->dma_dir;
2352 struct scatterlist *sg = qc->sg;
2353 dma_addr_t dma_address;
2355 dma_address = dma_map_single(ap->host_set->dev, qc->buf_virt,
2357 if (dma_mapping_error(dma_address))
2360 sg_dma_address(sg) = dma_address;
2361 sg_dma_len(sg) = sg->length;
2363 DPRINTK("mapped buffer of %d bytes for %s\n", sg_dma_len(sg),
2364 qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
2370 * ata_sg_setup - DMA-map the scatter-gather table associated with a command.
2371 * @qc: Command with scatter-gather table to be mapped.
2373 * DMA-map the scatter-gather table associated with queued_cmd @qc.
2376 * spin_lock_irqsave(host_set lock)
2379 * Zero on success, negative on error.
2383 static int ata_sg_setup(struct ata_queued_cmd *qc)
2385 struct ata_port *ap = qc->ap;
2386 struct scatterlist *sg = qc->sg;
2389 VPRINTK("ENTER, ata%u\n", ap->id);
2390 assert(qc->flags & ATA_QCFLAG_SG);
2393 n_elem = dma_map_sg(ap->host_set->dev, sg, qc->n_elem, dir);
2397 DPRINTK("%d sg elements mapped\n", n_elem);
2399 qc->n_elem = n_elem;
2405 * ata_poll_qc_complete - turn irq back on and finish qc
2406 * @qc: Command to complete
2407 * @drv_stat: ATA status register content
2410 * None. (grabs host lock)
2413 void ata_poll_qc_complete(struct ata_queued_cmd *qc, u8 drv_stat)
2415 struct ata_port *ap = qc->ap;
2417 spin_lock_irq(&ap->host_set->lock);
2418 ap->flags &= ~ATA_FLAG_NOINTR;
2420 ata_qc_complete(qc, drv_stat);
2421 spin_unlock_irq(&ap->host_set->lock);
2429 * None. (executing in kernel thread context)
2435 static unsigned long ata_pio_poll(struct ata_port *ap)
2438 unsigned int poll_state = PIO_ST_UNKNOWN;
2439 unsigned int reg_state = PIO_ST_UNKNOWN;
2440 const unsigned int tmout_state = PIO_ST_TMOUT;
2442 switch (ap->pio_task_state) {
2445 poll_state = PIO_ST_POLL;
2449 case PIO_ST_LAST_POLL:
2450 poll_state = PIO_ST_LAST_POLL;
2451 reg_state = PIO_ST_LAST;
2458 status = ata_chk_status(ap);
2459 if (status & ATA_BUSY) {
2460 if (time_after(jiffies, ap->pio_task_timeout)) {
2461 ap->pio_task_state = tmout_state;
2464 ap->pio_task_state = poll_state;
2465 return ATA_SHORT_PAUSE;
2468 ap->pio_task_state = reg_state;
2473 * ata_pio_complete -
2477 * None. (executing in kernel thread context)
2480 static void ata_pio_complete (struct ata_port *ap)
2482 struct ata_queued_cmd *qc;
2486 * This is purely hueristic. This is a fast path.
2487 * Sometimes when we enter, BSY will be cleared in
2488 * a chk-status or two. If not, the drive is probably seeking
2489 * or something. Snooze for a couple msecs, then
2490 * chk-status again. If still busy, fall back to
2491 * PIO_ST_POLL state.
2493 drv_stat = ata_busy_wait(ap, ATA_BUSY | ATA_DRQ, 10);
2494 if (drv_stat & (ATA_BUSY | ATA_DRQ)) {
2496 drv_stat = ata_busy_wait(ap, ATA_BUSY | ATA_DRQ, 10);
2497 if (drv_stat & (ATA_BUSY | ATA_DRQ)) {
2498 ap->pio_task_state = PIO_ST_LAST_POLL;
2499 ap->pio_task_timeout = jiffies + ATA_TMOUT_PIO;
2504 drv_stat = ata_wait_idle(ap);
2505 if (!ata_ok(drv_stat)) {
2506 ap->pio_task_state = PIO_ST_ERR;
2510 qc = ata_qc_from_tag(ap, ap->active_tag);
2513 ap->pio_task_state = PIO_ST_IDLE;
2515 ata_poll_qc_complete(qc, drv_stat);
2521 * @buf: Buffer to swap
2522 * @buf_words: Number of 16-bit words in buffer.
2524 * Swap halves of 16-bit words if needed to convert from
2525 * little-endian byte order to native cpu byte order, or
2530 void swap_buf_le16(u16 *buf, unsigned int buf_words)
2535 for (i = 0; i < buf_words; i++)
2536 buf[i] = le16_to_cpu(buf[i]);
2537 #endif /* __BIG_ENDIAN */
2541 * ata_mmio_data_xfer - Transfer data by MMIO
2542 * @ap: port to read/write
2544 * @buflen: buffer length
2545 * @do_write: read/write
2547 * Transfer data from/to the device data register by MMIO.
2550 * Inherited from caller.
2554 static void ata_mmio_data_xfer(struct ata_port *ap, unsigned char *buf,
2555 unsigned int buflen, int write_data)
2558 unsigned int words = buflen >> 1;
2559 u16 *buf16 = (u16 *) buf;
2560 void __iomem *mmio = (void __iomem *)ap->ioaddr.data_addr;
2562 /* Transfer multiple of 2 bytes */
2564 for (i = 0; i < words; i++)
2565 writew(le16_to_cpu(buf16[i]), mmio);
2567 for (i = 0; i < words; i++)
2568 buf16[i] = cpu_to_le16(readw(mmio));
2571 /* Transfer trailing 1 byte, if any. */
2572 if (unlikely(buflen & 0x01)) {
2573 u16 align_buf[1] = { 0 };
2574 unsigned char *trailing_buf = buf + buflen - 1;
2577 memcpy(align_buf, trailing_buf, 1);
2578 writew(le16_to_cpu(align_buf[0]), mmio);
2580 align_buf[0] = cpu_to_le16(readw(mmio));
2581 memcpy(trailing_buf, align_buf, 1);
2587 * ata_pio_data_xfer - Transfer data by PIO
2588 * @ap: port to read/write
2590 * @buflen: buffer length
2591 * @do_write: read/write
2593 * Transfer data from/to the device data register by PIO.
2596 * Inherited from caller.
2600 static void ata_pio_data_xfer(struct ata_port *ap, unsigned char *buf,
2601 unsigned int buflen, int write_data)
2603 unsigned int words = buflen >> 1;
2605 /* Transfer multiple of 2 bytes */
2607 outsw(ap->ioaddr.data_addr, buf, words);
2609 insw(ap->ioaddr.data_addr, buf, words);
2611 /* Transfer trailing 1 byte, if any. */
2612 if (unlikely(buflen & 0x01)) {
2613 u16 align_buf[1] = { 0 };
2614 unsigned char *trailing_buf = buf + buflen - 1;
2617 memcpy(align_buf, trailing_buf, 1);
2618 outw(le16_to_cpu(align_buf[0]), ap->ioaddr.data_addr);
2620 align_buf[0] = cpu_to_le16(inw(ap->ioaddr.data_addr));
2621 memcpy(trailing_buf, align_buf, 1);
2627 * ata_data_xfer - Transfer data from/to the data register.
2628 * @ap: port to read/write
2630 * @buflen: buffer length
2631 * @do_write: read/write
2633 * Transfer data from/to the device data register.
2636 * Inherited from caller.
2640 static void ata_data_xfer(struct ata_port *ap, unsigned char *buf,
2641 unsigned int buflen, int do_write)
2643 if (ap->flags & ATA_FLAG_MMIO)
2644 ata_mmio_data_xfer(ap, buf, buflen, do_write);
2646 ata_pio_data_xfer(ap, buf, buflen, do_write);
2650 * ata_pio_sector - Transfer ATA_SECT_SIZE (512 bytes) of data.
2651 * @qc: Command on going
2653 * Transfer ATA_SECT_SIZE of data from/to the ATA device.
2656 * Inherited from caller.
2659 static void ata_pio_sector(struct ata_queued_cmd *qc)
2661 int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
2662 struct scatterlist *sg = qc->sg;
2663 struct ata_port *ap = qc->ap;
2665 unsigned int offset;
2668 if (qc->cursect == (qc->nsect - 1))
2669 ap->pio_task_state = PIO_ST_LAST;
2671 page = sg[qc->cursg].page;
2672 offset = sg[qc->cursg].offset + qc->cursg_ofs * ATA_SECT_SIZE;
2674 /* get the current page and offset */
2675 page = nth_page(page, (offset >> PAGE_SHIFT));
2676 offset %= PAGE_SIZE;
2678 buf = kmap(page) + offset;
2683 if ((qc->cursg_ofs * ATA_SECT_SIZE) == (&sg[qc->cursg])->length) {
2688 DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
2690 /* do the actual data transfer */
2691 do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
2692 ata_data_xfer(ap, buf, ATA_SECT_SIZE, do_write);
2698 * __atapi_pio_bytes - Transfer data from/to the ATAPI device.
2699 * @qc: Command on going
2700 * @bytes: number of bytes
2702 * Transfer Transfer data from/to the ATAPI device.
2705 * Inherited from caller.
2709 static void __atapi_pio_bytes(struct ata_queued_cmd *qc, unsigned int bytes)
2711 int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
2712 struct scatterlist *sg = qc->sg;
2713 struct ata_port *ap = qc->ap;
2716 unsigned int offset, count;
2718 if (qc->curbytes + bytes >= qc->nbytes)
2719 ap->pio_task_state = PIO_ST_LAST;
2722 if (unlikely(qc->cursg >= qc->n_elem)) {
2724 * The end of qc->sg is reached and the device expects
2725 * more data to transfer. In order not to overrun qc->sg
2726 * and fulfill length specified in the byte count register,
2727 * - for read case, discard trailing data from the device
2728 * - for write case, padding zero data to the device
2730 u16 pad_buf[1] = { 0 };
2731 unsigned int words = bytes >> 1;
2734 if (words) /* warning if bytes > 1 */
2735 printk(KERN_WARNING "ata%u: %u bytes trailing data\n",
2738 for (i = 0; i < words; i++)
2739 ata_data_xfer(ap, (unsigned char*)pad_buf, 2, do_write);
2741 ap->pio_task_state = PIO_ST_LAST;
2745 sg = &qc->sg[qc->cursg];
2748 offset = sg->offset + qc->cursg_ofs;
2750 /* get the current page and offset */
2751 page = nth_page(page, (offset >> PAGE_SHIFT));
2752 offset %= PAGE_SIZE;
2754 /* don't overrun current sg */
2755 count = min(sg->length - qc->cursg_ofs, bytes);
2757 /* don't cross page boundaries */
2758 count = min(count, (unsigned int)PAGE_SIZE - offset);
2760 buf = kmap(page) + offset;
2763 qc->curbytes += count;
2764 qc->cursg_ofs += count;
2766 if (qc->cursg_ofs == sg->length) {
2771 DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
2773 /* do the actual data transfer */
2774 ata_data_xfer(ap, buf, count, do_write);
2783 * atapi_pio_bytes - Transfer data from/to the ATAPI device.
2784 * @qc: Command on going
2786 * Transfer Transfer data from/to the ATAPI device.
2789 * Inherited from caller.
2793 static void atapi_pio_bytes(struct ata_queued_cmd *qc)
2795 struct ata_port *ap = qc->ap;
2796 struct ata_device *dev = qc->dev;
2797 unsigned int ireason, bc_lo, bc_hi, bytes;
2798 int i_write, do_write = (qc->tf.flags & ATA_TFLAG_WRITE) ? 1 : 0;
2800 ap->ops->tf_read(ap, &qc->tf);
2801 ireason = qc->tf.nsect;
2802 bc_lo = qc->tf.lbam;
2803 bc_hi = qc->tf.lbah;
2804 bytes = (bc_hi << 8) | bc_lo;
2806 /* shall be cleared to zero, indicating xfer of data */
2807 if (ireason & (1 << 0))
2810 /* make sure transfer direction matches expected */
2811 i_write = ((ireason & (1 << 1)) == 0) ? 1 : 0;
2812 if (do_write != i_write)
2815 __atapi_pio_bytes(qc, bytes);
2820 printk(KERN_INFO "ata%u: dev %u: ATAPI check failed\n",
2821 ap->id, dev->devno);
2822 ap->pio_task_state = PIO_ST_ERR;
2830 * None. (executing in kernel thread context)
2833 static void ata_pio_block(struct ata_port *ap)
2835 struct ata_queued_cmd *qc;
2839 * This is purely hueristic. This is a fast path.
2840 * Sometimes when we enter, BSY will be cleared in
2841 * a chk-status or two. If not, the drive is probably seeking
2842 * or something. Snooze for a couple msecs, then
2843 * chk-status again. If still busy, fall back to
2844 * PIO_ST_POLL state.
2846 status = ata_busy_wait(ap, ATA_BUSY, 5);
2847 if (status & ATA_BUSY) {
2849 status = ata_busy_wait(ap, ATA_BUSY, 10);
2850 if (status & ATA_BUSY) {
2851 ap->pio_task_state = PIO_ST_POLL;
2852 ap->pio_task_timeout = jiffies + ATA_TMOUT_PIO;
2857 qc = ata_qc_from_tag(ap, ap->active_tag);
2860 if (is_atapi_taskfile(&qc->tf)) {
2861 /* no more data to transfer or unsupported ATAPI command */
2862 if ((status & ATA_DRQ) == 0) {
2863 ap->pio_task_state = PIO_ST_IDLE;
2865 ata_poll_qc_complete(qc, status);
2869 atapi_pio_bytes(qc);
2871 /* handle BSY=0, DRQ=0 as error */
2872 if ((status & ATA_DRQ) == 0) {
2873 ap->pio_task_state = PIO_ST_ERR;
2881 static void ata_pio_error(struct ata_port *ap)
2883 struct ata_queued_cmd *qc;
2886 qc = ata_qc_from_tag(ap, ap->active_tag);
2889 drv_stat = ata_chk_status(ap);
2890 printk(KERN_WARNING "ata%u: PIO error, drv_stat 0x%x\n",
2893 ap->pio_task_state = PIO_ST_IDLE;
2895 ata_poll_qc_complete(qc, drv_stat | ATA_ERR);
2898 static void ata_pio_task(void *_data)
2900 struct ata_port *ap = _data;
2901 unsigned long timeout = 0;
2903 switch (ap->pio_task_state) {
2912 ata_pio_complete(ap);
2916 case PIO_ST_LAST_POLL:
2917 timeout = ata_pio_poll(ap);
2927 queue_delayed_work(ata_wq, &ap->pio_task,
2930 queue_work(ata_wq, &ap->pio_task);
2933 static void atapi_request_sense(struct ata_port *ap, struct ata_device *dev,
2934 struct scsi_cmnd *cmd)
2936 DECLARE_COMPLETION(wait);
2937 struct ata_queued_cmd *qc;
2938 unsigned long flags;
2941 DPRINTK("ATAPI request sense\n");
2943 qc = ata_qc_new_init(ap, dev);
2946 /* FIXME: is this needed? */
2947 memset(cmd->sense_buffer, 0, sizeof(cmd->sense_buffer));
2949 ata_sg_init_one(qc, cmd->sense_buffer, sizeof(cmd->sense_buffer));
2950 qc->dma_dir = DMA_FROM_DEVICE;
2952 memset(&qc->cdb, 0, ap->cdb_len);
2953 qc->cdb[0] = REQUEST_SENSE;
2954 qc->cdb[4] = SCSI_SENSE_BUFFERSIZE;
2956 qc->tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
2957 qc->tf.command = ATA_CMD_PACKET;
2959 qc->tf.protocol = ATA_PROT_ATAPI;
2960 qc->tf.lbam = (8 * 1024) & 0xff;
2961 qc->tf.lbah = (8 * 1024) >> 8;
2962 qc->nbytes = SCSI_SENSE_BUFFERSIZE;
2964 qc->waiting = &wait;
2965 qc->complete_fn = ata_qc_complete_noop;
2967 spin_lock_irqsave(&ap->host_set->lock, flags);
2968 rc = ata_qc_issue(qc);
2969 spin_unlock_irqrestore(&ap->host_set->lock, flags);
2972 ata_port_disable(ap);
2974 wait_for_completion(&wait);
2980 * ata_qc_timeout - Handle timeout of queued command
2981 * @qc: Command that timed out
2983 * Some part of the kernel (currently, only the SCSI layer)
2984 * has noticed that the active command on port @ap has not
2985 * completed after a specified length of time. Handle this
2986 * condition by disabling DMA (if necessary) and completing
2987 * transactions, with error if necessary.
2989 * This also handles the case of the "lost interrupt", where
2990 * for some reason (possibly hardware bug, possibly driver bug)
2991 * an interrupt was not delivered to the driver, even though the
2992 * transaction completed successfully.
2995 * Inherited from SCSI layer (none, can sleep)
2998 static void ata_qc_timeout(struct ata_queued_cmd *qc)
3000 struct ata_port *ap = qc->ap;
3001 struct ata_device *dev = qc->dev;
3002 u8 host_stat = 0, drv_stat;
3006 /* FIXME: doesn't this conflict with timeout handling? */
3007 if (qc->dev->class == ATA_DEV_ATAPI && qc->scsicmd) {
3008 struct scsi_cmnd *cmd = qc->scsicmd;
3010 if (!(cmd->eh_eflags & SCSI_EH_CANCEL_CMD)) {
3012 /* finish completing original command */
3013 __ata_qc_complete(qc);
3015 atapi_request_sense(ap, dev, cmd);
3017 cmd->result = (CHECK_CONDITION << 1) | (DID_OK << 16);
3018 scsi_finish_command(cmd);
3024 /* hack alert! We cannot use the supplied completion
3025 * function from inside the ->eh_strategy_handler() thread.
3026 * libata is the only user of ->eh_strategy_handler() in
3027 * any kernel, so the default scsi_done() assumes it is
3028 * not being called from the SCSI EH.
3030 qc->scsidone = scsi_finish_command;
3032 switch (qc->tf.protocol) {
3035 case ATA_PROT_ATAPI_DMA:
3036 host_stat = ap->ops->bmdma_status(ap);
3038 /* before we do anything else, clear DMA-Start bit */
3039 ap->ops->bmdma_stop(ap);
3045 drv_stat = ata_chk_status(ap);
3047 /* ack bmdma irq events */
3048 ap->ops->irq_clear(ap);
3050 printk(KERN_ERR "ata%u: command 0x%x timeout, stat 0x%x host_stat 0x%x\n",
3051 ap->id, qc->tf.command, drv_stat, host_stat);
3053 /* complete taskfile transaction */
3054 ata_qc_complete(qc, drv_stat);
3062 * ata_eng_timeout - Handle timeout of queued command
3063 * @ap: Port on which timed-out command is active
3065 * Some part of the kernel (currently, only the SCSI layer)
3066 * has noticed that the active command on port @ap has not
3067 * completed after a specified length of time. Handle this
3068 * condition by disabling DMA (if necessary) and completing
3069 * transactions, with error if necessary.
3071 * This also handles the case of the "lost interrupt", where
3072 * for some reason (possibly hardware bug, possibly driver bug)
3073 * an interrupt was not delivered to the driver, even though the
3074 * transaction completed successfully.
3077 * Inherited from SCSI layer (none, can sleep)
3080 void ata_eng_timeout(struct ata_port *ap)
3082 struct ata_queued_cmd *qc;
3086 qc = ata_qc_from_tag(ap, ap->active_tag);
3088 printk(KERN_ERR "ata%u: BUG: timeout without command\n",
3100 * ata_qc_new - Request an available ATA command, for queueing
3101 * @ap: Port associated with device @dev
3102 * @dev: Device from whom we request an available command structure
3108 static struct ata_queued_cmd *ata_qc_new(struct ata_port *ap)
3110 struct ata_queued_cmd *qc = NULL;
3113 for (i = 0; i < ATA_MAX_QUEUE; i++)
3114 if (!test_and_set_bit(i, &ap->qactive)) {
3115 qc = ata_qc_from_tag(ap, i);
3126 * ata_qc_new_init - Request an available ATA command, and initialize it
3127 * @ap: Port associated with device @dev
3128 * @dev: Device from whom we request an available command structure
3134 struct ata_queued_cmd *ata_qc_new_init(struct ata_port *ap,
3135 struct ata_device *dev)
3137 struct ata_queued_cmd *qc;
3139 qc = ata_qc_new(ap);
3146 qc->cursect = qc->cursg = qc->cursg_ofs = 0;
3148 qc->nbytes = qc->curbytes = 0;
3150 ata_tf_init(ap, &qc->tf, dev->devno);
3152 if (dev->flags & ATA_DFLAG_LBA48)
3153 qc->tf.flags |= ATA_TFLAG_LBA48;
3159 static int ata_qc_complete_noop(struct ata_queued_cmd *qc, u8 drv_stat)
3164 static void __ata_qc_complete(struct ata_queued_cmd *qc)
3166 struct ata_port *ap = qc->ap;
3167 unsigned int tag, do_clear = 0;
3171 if (likely(ata_tag_valid(tag))) {
3172 if (tag == ap->active_tag)
3173 ap->active_tag = ATA_TAG_POISON;
3174 qc->tag = ATA_TAG_POISON;
3179 struct completion *waiting = qc->waiting;
3184 if (likely(do_clear))
3185 clear_bit(tag, &ap->qactive);
3189 * ata_qc_free - free unused ata_queued_cmd
3190 * @qc: Command to complete
3192 * Designed to free unused ata_queued_cmd object
3193 * in case something prevents using it.
3196 * spin_lock_irqsave(host_set lock)
3199 void ata_qc_free(struct ata_queued_cmd *qc)
3201 assert(qc != NULL); /* ata_qc_from_tag _might_ return NULL */
3202 assert(qc->waiting == NULL); /* nothing should be waiting */
3204 __ata_qc_complete(qc);
3208 * ata_qc_complete - Complete an active ATA command
3209 * @qc: Command to complete
3210 * @drv_stat: ATA Status register contents
3212 * Indicate to the mid and upper layers that an ATA
3213 * command has completed, with either an ok or not-ok status.
3216 * spin_lock_irqsave(host_set lock)
3220 void ata_qc_complete(struct ata_queued_cmd *qc, u8 drv_stat)
3224 assert(qc != NULL); /* ata_qc_from_tag _might_ return NULL */
3225 assert(qc->flags & ATA_QCFLAG_ACTIVE);
3227 if (likely(qc->flags & ATA_QCFLAG_DMAMAP))
3230 /* atapi: mark qc as inactive to prevent the interrupt handler
3231 * from completing the command twice later, before the error handler
3232 * is called. (when rc != 0 and atapi request sense is needed)
3234 qc->flags &= ~ATA_QCFLAG_ACTIVE;
3236 /* call completion callback */
3237 rc = qc->complete_fn(qc, drv_stat);
3239 /* if callback indicates not to complete command (non-zero),
3240 * return immediately
3245 __ata_qc_complete(qc);
3250 static inline int ata_should_dma_map(struct ata_queued_cmd *qc)
3252 struct ata_port *ap = qc->ap;
3254 switch (qc->tf.protocol) {
3256 case ATA_PROT_ATAPI_DMA:
3259 case ATA_PROT_ATAPI:
3261 case ATA_PROT_PIO_MULT:
3262 if (ap->flags & ATA_FLAG_PIO_DMA)
3275 * ata_qc_issue - issue taskfile to device
3276 * @qc: command to issue to device
3278 * Prepare an ATA command to submission to device.
3279 * This includes mapping the data into a DMA-able
3280 * area, filling in the S/G table, and finally
3281 * writing the taskfile to hardware, starting the command.
3284 * spin_lock_irqsave(host_set lock)
3287 * Zero on success, negative on error.
3290 int ata_qc_issue(struct ata_queued_cmd *qc)
3292 struct ata_port *ap = qc->ap;
3294 if (ata_should_dma_map(qc)) {
3295 if (qc->flags & ATA_QCFLAG_SG) {
3296 if (ata_sg_setup(qc))
3298 } else if (qc->flags & ATA_QCFLAG_SINGLE) {
3299 if (ata_sg_setup_one(qc))
3303 qc->flags &= ~ATA_QCFLAG_DMAMAP;
3306 ap->ops->qc_prep(qc);
3308 qc->ap->active_tag = qc->tag;
3309 qc->flags |= ATA_QCFLAG_ACTIVE;
3311 return ap->ops->qc_issue(qc);
3319 * ata_qc_issue_prot - issue taskfile to device in proto-dependent manner
3320 * @qc: command to issue to device
3322 * Using various libata functions and hooks, this function
3323 * starts an ATA command. ATA commands are grouped into
3324 * classes called "protocols", and issuing each type of protocol
3325 * is slightly different.
3327 * May be used as the qc_issue() entry in ata_port_operations.
3330 * spin_lock_irqsave(host_set lock)
3333 * Zero on success, negative on error.
3336 int ata_qc_issue_prot(struct ata_queued_cmd *qc)
3338 struct ata_port *ap = qc->ap;
3340 ata_dev_select(ap, qc->dev->devno, 1, 0);
3342 switch (qc->tf.protocol) {
3343 case ATA_PROT_NODATA:
3344 ata_tf_to_host_nolock(ap, &qc->tf);
3348 ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
3349 ap->ops->bmdma_setup(qc); /* set up bmdma */
3350 ap->ops->bmdma_start(qc); /* initiate bmdma */
3353 case ATA_PROT_PIO: /* load tf registers, initiate polling pio */
3354 ata_qc_set_polling(qc);
3355 ata_tf_to_host_nolock(ap, &qc->tf);
3356 ap->pio_task_state = PIO_ST;
3357 queue_work(ata_wq, &ap->pio_task);
3360 case ATA_PROT_ATAPI:
3361 ata_qc_set_polling(qc);
3362 ata_tf_to_host_nolock(ap, &qc->tf);
3363 queue_work(ata_wq, &ap->packet_task);
3366 case ATA_PROT_ATAPI_NODATA:
3367 ap->flags |= ATA_FLAG_NOINTR;
3368 ata_tf_to_host_nolock(ap, &qc->tf);
3369 queue_work(ata_wq, &ap->packet_task);
3372 case ATA_PROT_ATAPI_DMA:
3373 ap->flags |= ATA_FLAG_NOINTR;
3374 ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
3375 ap->ops->bmdma_setup(qc); /* set up bmdma */
3376 queue_work(ata_wq, &ap->packet_task);
3388 * ata_bmdma_setup_mmio - Set up PCI IDE BMDMA transaction
3389 * @qc: Info associated with this ATA transaction.
3392 * spin_lock_irqsave(host_set lock)
3395 static void ata_bmdma_setup_mmio (struct ata_queued_cmd *qc)
3397 struct ata_port *ap = qc->ap;
3398 unsigned int rw = (qc->tf.flags & ATA_TFLAG_WRITE);
3400 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
3402 /* load PRD table addr. */
3403 mb(); /* make sure PRD table writes are visible to controller */
3404 writel(ap->prd_dma, mmio + ATA_DMA_TABLE_OFS);
3406 /* specify data direction, triple-check start bit is clear */
3407 dmactl = readb(mmio + ATA_DMA_CMD);
3408 dmactl &= ~(ATA_DMA_WR | ATA_DMA_START);
3410 dmactl |= ATA_DMA_WR;
3411 writeb(dmactl, mmio + ATA_DMA_CMD);
3413 /* issue r/w command */
3414 ap->ops->exec_command(ap, &qc->tf);
3418 * ata_bmdma_start - Start a PCI IDE BMDMA transaction
3419 * @qc: Info associated with this ATA transaction.
3422 * spin_lock_irqsave(host_set lock)
3425 static void ata_bmdma_start_mmio (struct ata_queued_cmd *qc)
3427 struct ata_port *ap = qc->ap;
3428 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
3431 /* start host DMA transaction */
3432 dmactl = readb(mmio + ATA_DMA_CMD);
3433 writeb(dmactl | ATA_DMA_START, mmio + ATA_DMA_CMD);
3435 /* Strictly, one may wish to issue a readb() here, to
3436 * flush the mmio write. However, control also passes
3437 * to the hardware at this point, and it will interrupt
3438 * us when we are to resume control. So, in effect,
3439 * we don't care when the mmio write flushes.
3440 * Further, a read of the DMA status register _immediately_
3441 * following the write may not be what certain flaky hardware
3442 * is expected, so I think it is best to not add a readb()
3443 * without first all the MMIO ATA cards/mobos.
3444 * Or maybe I'm just being paranoid.
3449 * ata_bmdma_setup_pio - Set up PCI IDE BMDMA transaction (PIO)
3450 * @qc: Info associated with this ATA transaction.
3453 * spin_lock_irqsave(host_set lock)
3456 static void ata_bmdma_setup_pio (struct ata_queued_cmd *qc)
3458 struct ata_port *ap = qc->ap;
3459 unsigned int rw = (qc->tf.flags & ATA_TFLAG_WRITE);
3462 /* load PRD table addr. */
3463 outl(ap->prd_dma, ap->ioaddr.bmdma_addr + ATA_DMA_TABLE_OFS);
3465 /* specify data direction, triple-check start bit is clear */
3466 dmactl = inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3467 dmactl &= ~(ATA_DMA_WR | ATA_DMA_START);
3469 dmactl |= ATA_DMA_WR;
3470 outb(dmactl, ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3472 /* issue r/w command */
3473 ap->ops->exec_command(ap, &qc->tf);
3477 * ata_bmdma_start_pio - Start a PCI IDE BMDMA transaction (PIO)
3478 * @qc: Info associated with this ATA transaction.
3481 * spin_lock_irqsave(host_set lock)
3484 static void ata_bmdma_start_pio (struct ata_queued_cmd *qc)
3486 struct ata_port *ap = qc->ap;
3489 /* start host DMA transaction */
3490 dmactl = inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3491 outb(dmactl | ATA_DMA_START,
3492 ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3497 * ata_bmdma_start - Start a PCI IDE BMDMA transaction
3498 * @qc: Info associated with this ATA transaction.
3500 * Writes the ATA_DMA_START flag to the DMA command register.
3502 * May be used as the bmdma_start() entry in ata_port_operations.
3505 * spin_lock_irqsave(host_set lock)
3507 void ata_bmdma_start(struct ata_queued_cmd *qc)
3509 if (qc->ap->flags & ATA_FLAG_MMIO)
3510 ata_bmdma_start_mmio(qc);
3512 ata_bmdma_start_pio(qc);
3517 * ata_bmdma_setup - Set up PCI IDE BMDMA transaction
3518 * @qc: Info associated with this ATA transaction.
3520 * Writes address of PRD table to device's PRD Table Address
3521 * register, sets the DMA control register, and calls
3522 * ops->exec_command() to start the transfer.
3524 * May be used as the bmdma_setup() entry in ata_port_operations.
3527 * spin_lock_irqsave(host_set lock)
3529 void ata_bmdma_setup(struct ata_queued_cmd *qc)
3531 if (qc->ap->flags & ATA_FLAG_MMIO)
3532 ata_bmdma_setup_mmio(qc);
3534 ata_bmdma_setup_pio(qc);
3539 * ata_bmdma_irq_clear - Clear PCI IDE BMDMA interrupt.
3540 * @ap: Port associated with this ATA transaction.
3542 * Clear interrupt and error flags in DMA status register.
3544 * May be used as the irq_clear() entry in ata_port_operations.
3547 * spin_lock_irqsave(host_set lock)
3550 void ata_bmdma_irq_clear(struct ata_port *ap)
3552 if (ap->flags & ATA_FLAG_MMIO) {
3553 void __iomem *mmio = ((void __iomem *) ap->ioaddr.bmdma_addr) + ATA_DMA_STATUS;
3554 writeb(readb(mmio), mmio);
3556 unsigned long addr = ap->ioaddr.bmdma_addr + ATA_DMA_STATUS;
3557 outb(inb(addr), addr);
3564 * ata_bmdma_status - Read PCI IDE BMDMA status
3565 * @ap: Port associated with this ATA transaction.
3567 * Read and return BMDMA status register.
3569 * May be used as the bmdma_status() entry in ata_port_operations.
3572 * spin_lock_irqsave(host_set lock)
3575 u8 ata_bmdma_status(struct ata_port *ap)
3578 if (ap->flags & ATA_FLAG_MMIO) {
3579 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
3580 host_stat = readb(mmio + ATA_DMA_STATUS);
3582 host_stat = inb(ap->ioaddr.bmdma_addr + ATA_DMA_STATUS);
3588 * ata_bmdma_stop - Stop PCI IDE BMDMA transfer
3589 * @ap: Port associated with this ATA transaction.
3591 * Clears the ATA_DMA_START flag in the dma control register
3593 * May be used as the bmdma_stop() entry in ata_port_operations.
3596 * spin_lock_irqsave(host_set lock)
3599 void ata_bmdma_stop(struct ata_port *ap)
3601 if (ap->flags & ATA_FLAG_MMIO) {
3602 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
3604 /* clear start/stop bit */
3605 writeb(readb(mmio + ATA_DMA_CMD) & ~ATA_DMA_START,
3606 mmio + ATA_DMA_CMD);
3608 /* clear start/stop bit */
3609 outb(inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD) & ~ATA_DMA_START,
3610 ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3613 /* one-PIO-cycle guaranteed wait, per spec, for HDMA1:0 transition */
3614 ata_altstatus(ap); /* dummy read */
3618 * ata_host_intr - Handle host interrupt for given (port, task)
3619 * @ap: Port on which interrupt arrived (possibly...)
3620 * @qc: Taskfile currently active in engine
3622 * Handle host interrupt for given queued command. Currently,
3623 * only DMA interrupts are handled. All other commands are
3624 * handled via polling with interrupts disabled (nIEN bit).
3627 * spin_lock_irqsave(host_set lock)
3630 * One if interrupt was handled, zero if not (shared irq).
3633 inline unsigned int ata_host_intr (struct ata_port *ap,
3634 struct ata_queued_cmd *qc)
3636 u8 status, host_stat;
3638 switch (qc->tf.protocol) {
3641 case ATA_PROT_ATAPI_DMA:
3642 case ATA_PROT_ATAPI:
3643 /* check status of DMA engine */
3644 host_stat = ap->ops->bmdma_status(ap);
3645 VPRINTK("ata%u: host_stat 0x%X\n", ap->id, host_stat);
3647 /* if it's not our irq... */
3648 if (!(host_stat & ATA_DMA_INTR))
3651 /* before we do anything else, clear DMA-Start bit */
3652 ap->ops->bmdma_stop(ap);
3656 case ATA_PROT_ATAPI_NODATA:
3657 case ATA_PROT_NODATA:
3658 /* check altstatus */
3659 status = ata_altstatus(ap);
3660 if (status & ATA_BUSY)
3663 /* check main status, clearing INTRQ */
3664 status = ata_chk_status(ap);
3665 if (unlikely(status & ATA_BUSY))
3667 DPRINTK("ata%u: protocol %d (dev_stat 0x%X)\n",
3668 ap->id, qc->tf.protocol, status);
3670 /* ack bmdma irq events */
3671 ap->ops->irq_clear(ap);
3673 /* complete taskfile transaction */
3674 ata_qc_complete(qc, status);
3681 return 1; /* irq handled */
3684 ap->stats.idle_irq++;
3687 if ((ap->stats.idle_irq % 1000) == 0) {
3689 ata_irq_ack(ap, 0); /* debug trap */
3690 printk(KERN_WARNING "ata%d: irq trap\n", ap->id);
3693 return 0; /* irq not handled */
3697 * ata_interrupt - Default ATA host interrupt handler
3698 * @irq: irq line (unused)
3699 * @dev_instance: pointer to our ata_host_set information structure
3702 * Default interrupt handler for PCI IDE devices. Calls
3703 * ata_host_intr() for each port that is not disabled.
3706 * Obtains host_set lock during operation.
3709 * IRQ_NONE or IRQ_HANDLED.
3713 irqreturn_t ata_interrupt (int irq, void *dev_instance, struct pt_regs *regs)
3715 struct ata_host_set *host_set = dev_instance;
3717 unsigned int handled = 0;
3718 unsigned long flags;
3720 /* TODO: make _irqsave conditional on x86 PCI IDE legacy mode */
3721 spin_lock_irqsave(&host_set->lock, flags);
3723 for (i = 0; i < host_set->n_ports; i++) {
3724 struct ata_port *ap;
3726 ap = host_set->ports[i];
3728 !(ap->flags & (ATA_FLAG_PORT_DISABLED | ATA_FLAG_NOINTR))) {
3729 struct ata_queued_cmd *qc;
3731 qc = ata_qc_from_tag(ap, ap->active_tag);
3732 if (qc && (!(qc->tf.ctl & ATA_NIEN)) &&
3733 (qc->flags & ATA_QCFLAG_ACTIVE))
3734 handled |= ata_host_intr(ap, qc);
3738 spin_unlock_irqrestore(&host_set->lock, flags);
3740 return IRQ_RETVAL(handled);
3744 * atapi_packet_task - Write CDB bytes to hardware
3745 * @_data: Port to which ATAPI device is attached.
3747 * When device has indicated its readiness to accept
3748 * a CDB, this function is called. Send the CDB.
3749 * If DMA is to be performed, exit immediately.
3750 * Otherwise, we are in polling mode, so poll
3751 * status under operation succeeds or fails.
3754 * Kernel thread context (may sleep)
3757 static void atapi_packet_task(void *_data)
3759 struct ata_port *ap = _data;
3760 struct ata_queued_cmd *qc;
3763 qc = ata_qc_from_tag(ap, ap->active_tag);
3765 assert(qc->flags & ATA_QCFLAG_ACTIVE);
3767 /* sleep-wait for BSY to clear */
3768 DPRINTK("busy wait\n");
3769 if (ata_busy_sleep(ap, ATA_TMOUT_CDB_QUICK, ATA_TMOUT_CDB))
3772 /* make sure DRQ is set */
3773 status = ata_chk_status(ap);
3774 if ((status & (ATA_BUSY | ATA_DRQ)) != ATA_DRQ)
3778 DPRINTK("send cdb\n");
3779 assert(ap->cdb_len >= 12);
3781 if (qc->tf.protocol == ATA_PROT_ATAPI_DMA ||
3782 qc->tf.protocol == ATA_PROT_ATAPI_NODATA) {
3783 unsigned long flags;
3785 /* Once we're done issuing command and kicking bmdma,
3786 * irq handler takes over. To not lose irq, we need
3787 * to clear NOINTR flag before sending cdb, but
3788 * interrupt handler shouldn't be invoked before we're
3789 * finished. Hence, the following locking.
3791 spin_lock_irqsave(&ap->host_set->lock, flags);
3792 ap->flags &= ~ATA_FLAG_NOINTR;
3793 ata_data_xfer(ap, qc->cdb, ap->cdb_len, 1);
3794 if (qc->tf.protocol == ATA_PROT_ATAPI_DMA)
3795 ap->ops->bmdma_start(qc); /* initiate bmdma */
3796 spin_unlock_irqrestore(&ap->host_set->lock, flags);
3798 ata_data_xfer(ap, qc->cdb, ap->cdb_len, 1);
3800 /* PIO commands are handled by polling */
3801 ap->pio_task_state = PIO_ST;
3802 queue_work(ata_wq, &ap->pio_task);
3808 ata_poll_qc_complete(qc, ATA_ERR);
3813 * ata_port_start - Set port up for dma.
3814 * @ap: Port to initialize
3816 * Called just after data structures for each port are
3817 * initialized. Allocates space for PRD table.
3819 * May be used as the port_start() entry in ata_port_operations.
3824 int ata_port_start (struct ata_port *ap)
3826 struct device *dev = ap->host_set->dev;
3828 ap->prd = dma_alloc_coherent(dev, ATA_PRD_TBL_SZ, &ap->prd_dma, GFP_KERNEL);
3832 DPRINTK("prd alloc, virt %p, dma %llx\n", ap->prd, (unsigned long long) ap->prd_dma);
3839 * ata_port_stop - Undo ata_port_start()
3840 * @ap: Port to shut down
3842 * Frees the PRD table.
3844 * May be used as the port_stop() entry in ata_port_operations.
3849 void ata_port_stop (struct ata_port *ap)
3851 struct device *dev = ap->host_set->dev;
3853 dma_free_coherent(dev, ATA_PRD_TBL_SZ, ap->prd, ap->prd_dma);
3856 void ata_host_stop (struct ata_host_set *host_set)
3858 if (host_set->mmio_base)
3859 iounmap(host_set->mmio_base);
3864 * ata_host_remove - Unregister SCSI host structure with upper layers
3865 * @ap: Port to unregister
3866 * @do_unregister: 1 if we fully unregister, 0 to just stop the port
3871 static void ata_host_remove(struct ata_port *ap, unsigned int do_unregister)
3873 struct Scsi_Host *sh = ap->host;
3878 scsi_remove_host(sh);
3880 ap->ops->port_stop(ap);
3884 * ata_host_init - Initialize an ata_port structure
3885 * @ap: Structure to initialize
3886 * @host: associated SCSI mid-layer structure
3887 * @host_set: Collection of hosts to which @ap belongs
3888 * @ent: Probe information provided by low-level driver
3889 * @port_no: Port number associated with this ata_port
3891 * Initialize a new ata_port structure, and its associated
3895 * Inherited from caller.
3899 static void ata_host_init(struct ata_port *ap, struct Scsi_Host *host,
3900 struct ata_host_set *host_set,
3901 struct ata_probe_ent *ent, unsigned int port_no)
3907 host->max_channel = 1;
3908 host->unique_id = ata_unique_id++;
3909 host->max_cmd_len = 12;
3911 scsi_assign_lock(host, &host_set->lock);
3913 ap->flags = ATA_FLAG_PORT_DISABLED;
3914 ap->id = host->unique_id;
3916 ap->ctl = ATA_DEVCTL_OBS;
3917 ap->host_set = host_set;
3918 ap->port_no = port_no;
3920 ent->legacy_mode ? ent->hard_port_no : port_no;
3921 ap->pio_mask = ent->pio_mask;
3922 ap->mwdma_mask = ent->mwdma_mask;
3923 ap->udma_mask = ent->udma_mask;
3924 ap->flags |= ent->host_flags;
3925 ap->ops = ent->port_ops;
3926 ap->cbl = ATA_CBL_NONE;
3927 ap->active_tag = ATA_TAG_POISON;
3928 ap->last_ctl = 0xFF;
3930 INIT_WORK(&ap->packet_task, atapi_packet_task, ap);
3931 INIT_WORK(&ap->pio_task, ata_pio_task, ap);
3933 for (i = 0; i < ATA_MAX_DEVICES; i++)
3934 ap->device[i].devno = i;
3937 ap->stats.unhandled_irq = 1;
3938 ap->stats.idle_irq = 1;
3941 memcpy(&ap->ioaddr, &ent->port[port_no], sizeof(struct ata_ioports));
3945 * ata_host_add - Attach low-level ATA driver to system
3946 * @ent: Information provided by low-level driver
3947 * @host_set: Collections of ports to which we add
3948 * @port_no: Port number associated with this host
3950 * Attach low-level ATA driver to system.
3953 * PCI/etc. bus probe sem.
3956 * New ata_port on success, for NULL on error.
3960 static struct ata_port * ata_host_add(struct ata_probe_ent *ent,
3961 struct ata_host_set *host_set,
3962 unsigned int port_no)
3964 struct Scsi_Host *host;
3965 struct ata_port *ap;
3969 host = scsi_host_alloc(ent->sht, sizeof(struct ata_port));
3973 ap = (struct ata_port *) &host->hostdata[0];
3975 ata_host_init(ap, host, host_set, ent, port_no);
3977 rc = ap->ops->port_start(ap);
3984 scsi_host_put(host);
3989 * ata_device_add - Register hardware device with ATA and SCSI layers
3990 * @ent: Probe information describing hardware device to be registered
3992 * This function processes the information provided in the probe
3993 * information struct @ent, allocates the necessary ATA and SCSI
3994 * host information structures, initializes them, and registers
3995 * everything with requisite kernel subsystems.
3997 * This function requests irqs, probes the ATA bus, and probes
4001 * PCI/etc. bus probe sem.
4004 * Number of ports registered. Zero on error (no ports registered).
4008 int ata_device_add(struct ata_probe_ent *ent)
4010 unsigned int count = 0, i;
4011 struct device *dev = ent->dev;
4012 struct ata_host_set *host_set;
4015 /* alloc a container for our list of ATA ports (buses) */
4016 host_set = kmalloc(sizeof(struct ata_host_set) +
4017 (ent->n_ports * sizeof(void *)), GFP_KERNEL);
4020 memset(host_set, 0, sizeof(struct ata_host_set) + (ent->n_ports * sizeof(void *)));
4021 spin_lock_init(&host_set->lock);
4023 host_set->dev = dev;
4024 host_set->n_ports = ent->n_ports;
4025 host_set->irq = ent->irq;
4026 host_set->mmio_base = ent->mmio_base;
4027 host_set->private_data = ent->private_data;
4028 host_set->ops = ent->port_ops;
4030 /* register each port bound to this device */
4031 for (i = 0; i < ent->n_ports; i++) {
4032 struct ata_port *ap;
4033 unsigned long xfer_mode_mask;
4035 ap = ata_host_add(ent, host_set, i);
4039 host_set->ports[i] = ap;
4040 xfer_mode_mask =(ap->udma_mask << ATA_SHIFT_UDMA) |
4041 (ap->mwdma_mask << ATA_SHIFT_MWDMA) |
4042 (ap->pio_mask << ATA_SHIFT_PIO);
4044 /* print per-port info to dmesg */
4045 printk(KERN_INFO "ata%u: %cATA max %s cmd 0x%lX ctl 0x%lX "
4046 "bmdma 0x%lX irq %lu\n",
4048 ap->flags & ATA_FLAG_SATA ? 'S' : 'P',
4049 ata_mode_string(xfer_mode_mask),
4050 ap->ioaddr.cmd_addr,
4051 ap->ioaddr.ctl_addr,
4052 ap->ioaddr.bmdma_addr,
4056 host_set->ops->irq_clear(ap);
4065 /* obtain irq, that is shared between channels */
4066 if (request_irq(ent->irq, ent->port_ops->irq_handler, ent->irq_flags,
4067 DRV_NAME, host_set))
4070 /* perform each probe synchronously */
4071 DPRINTK("probe begin\n");
4072 for (i = 0; i < count; i++) {
4073 struct ata_port *ap;
4076 ap = host_set->ports[i];
4078 DPRINTK("ata%u: probe begin\n", ap->id);
4079 rc = ata_bus_probe(ap);
4080 DPRINTK("ata%u: probe end\n", ap->id);
4083 /* FIXME: do something useful here?
4084 * Current libata behavior will
4085 * tear down everything when
4086 * the module is removed
4087 * or the h/w is unplugged.
4091 rc = scsi_add_host(ap->host, dev);
4093 printk(KERN_ERR "ata%u: scsi_add_host failed\n",
4095 /* FIXME: do something useful here */
4096 /* FIXME: handle unconditional calls to
4097 * scsi_scan_host and ata_host_remove, below,
4103 /* probes are done, now scan each port's disk(s) */
4104 DPRINTK("probe begin\n");
4105 for (i = 0; i < count; i++) {
4106 struct ata_port *ap = host_set->ports[i];
4108 scsi_scan_host(ap->host);
4111 dev_set_drvdata(dev, host_set);
4113 VPRINTK("EXIT, returning %u\n", ent->n_ports);
4114 return ent->n_ports; /* success */
4117 for (i = 0; i < count; i++) {
4118 ata_host_remove(host_set->ports[i], 1);
4119 scsi_host_put(host_set->ports[i]->host);
4122 VPRINTK("EXIT, returning 0\n");
4127 * ata_scsi_release - SCSI layer callback hook for host unload
4128 * @host: libata host to be unloaded
4130 * Performs all duties necessary to shut down a libata port...
4131 * Kill port kthread, disable port, and release resources.
4134 * Inherited from SCSI layer.
4140 int ata_scsi_release(struct Scsi_Host *host)
4142 struct ata_port *ap = (struct ata_port *) &host->hostdata[0];
4146 ap->ops->port_disable(ap);
4147 ata_host_remove(ap, 0);
4154 * ata_std_ports - initialize ioaddr with standard port offsets.
4155 * @ioaddr: IO address structure to be initialized
4157 * Utility function which initializes data_addr, error_addr,
4158 * feature_addr, nsect_addr, lbal_addr, lbam_addr, lbah_addr,
4159 * device_addr, status_addr, and command_addr to standard offsets
4160 * relative to cmd_addr.
4162 * Does not set ctl_addr, altstatus_addr, bmdma_addr, or scr_addr.
4165 void ata_std_ports(struct ata_ioports *ioaddr)
4167 ioaddr->data_addr = ioaddr->cmd_addr + ATA_REG_DATA;
4168 ioaddr->error_addr = ioaddr->cmd_addr + ATA_REG_ERR;
4169 ioaddr->feature_addr = ioaddr->cmd_addr + ATA_REG_FEATURE;
4170 ioaddr->nsect_addr = ioaddr->cmd_addr + ATA_REG_NSECT;
4171 ioaddr->lbal_addr = ioaddr->cmd_addr + ATA_REG_LBAL;
4172 ioaddr->lbam_addr = ioaddr->cmd_addr + ATA_REG_LBAM;
4173 ioaddr->lbah_addr = ioaddr->cmd_addr + ATA_REG_LBAH;
4174 ioaddr->device_addr = ioaddr->cmd_addr + ATA_REG_DEVICE;
4175 ioaddr->status_addr = ioaddr->cmd_addr + ATA_REG_STATUS;
4176 ioaddr->command_addr = ioaddr->cmd_addr + ATA_REG_CMD;
4179 static struct ata_probe_ent *
4180 ata_probe_ent_alloc(struct device *dev, struct ata_port_info *port)
4182 struct ata_probe_ent *probe_ent;
4184 probe_ent = kmalloc(sizeof(*probe_ent), GFP_KERNEL);
4186 printk(KERN_ERR DRV_NAME "(%s): out of memory\n",
4187 kobject_name(&(dev->kobj)));
4191 memset(probe_ent, 0, sizeof(*probe_ent));
4193 INIT_LIST_HEAD(&probe_ent->node);
4194 probe_ent->dev = dev;
4196 probe_ent->sht = port->sht;
4197 probe_ent->host_flags = port->host_flags;
4198 probe_ent->pio_mask = port->pio_mask;
4199 probe_ent->mwdma_mask = port->mwdma_mask;
4200 probe_ent->udma_mask = port->udma_mask;
4201 probe_ent->port_ops = port->port_ops;
4209 * ata_pci_init_native_mode - Initialize native-mode driver
4210 * @pdev: pci device to be initialized
4211 * @port: array[2] of pointers to port info structures.
4213 * Utility function which allocates and initializes an
4214 * ata_probe_ent structure for a standard dual-port
4215 * PIO-based IDE controller. The returned ata_probe_ent
4216 * structure can be passed to ata_device_add(). The returned
4217 * ata_probe_ent structure should then be freed with kfree().
4221 struct ata_probe_ent *
4222 ata_pci_init_native_mode(struct pci_dev *pdev, struct ata_port_info **port)
4224 struct ata_probe_ent *probe_ent =
4225 ata_probe_ent_alloc(pci_dev_to_dev(pdev), port[0]);
4229 probe_ent->n_ports = 2;
4230 probe_ent->irq = pdev->irq;
4231 probe_ent->irq_flags = SA_SHIRQ;
4233 probe_ent->port[0].cmd_addr = pci_resource_start(pdev, 0);
4234 probe_ent->port[0].altstatus_addr =
4235 probe_ent->port[0].ctl_addr =
4236 pci_resource_start(pdev, 1) | ATA_PCI_CTL_OFS;
4237 probe_ent->port[0].bmdma_addr = pci_resource_start(pdev, 4);
4239 probe_ent->port[1].cmd_addr = pci_resource_start(pdev, 2);
4240 probe_ent->port[1].altstatus_addr =
4241 probe_ent->port[1].ctl_addr =
4242 pci_resource_start(pdev, 3) | ATA_PCI_CTL_OFS;
4243 probe_ent->port[1].bmdma_addr = pci_resource_start(pdev, 4) + 8;
4245 ata_std_ports(&probe_ent->port[0]);
4246 ata_std_ports(&probe_ent->port[1]);
4251 static struct ata_probe_ent *
4252 ata_pci_init_legacy_mode(struct pci_dev *pdev, struct ata_port_info **port,
4253 struct ata_probe_ent **ppe2)
4255 struct ata_probe_ent *probe_ent, *probe_ent2;
4257 probe_ent = ata_probe_ent_alloc(pci_dev_to_dev(pdev), port[0]);
4260 probe_ent2 = ata_probe_ent_alloc(pci_dev_to_dev(pdev), port[1]);
4266 probe_ent->n_ports = 1;
4267 probe_ent->irq = 14;
4269 probe_ent->hard_port_no = 0;
4270 probe_ent->legacy_mode = 1;
4272 probe_ent2->n_ports = 1;
4273 probe_ent2->irq = 15;
4275 probe_ent2->hard_port_no = 1;
4276 probe_ent2->legacy_mode = 1;
4278 probe_ent->port[0].cmd_addr = 0x1f0;
4279 probe_ent->port[0].altstatus_addr =
4280 probe_ent->port[0].ctl_addr = 0x3f6;
4281 probe_ent->port[0].bmdma_addr = pci_resource_start(pdev, 4);
4283 probe_ent2->port[0].cmd_addr = 0x170;
4284 probe_ent2->port[0].altstatus_addr =
4285 probe_ent2->port[0].ctl_addr = 0x376;
4286 probe_ent2->port[0].bmdma_addr = pci_resource_start(pdev, 4)+8;
4288 ata_std_ports(&probe_ent->port[0]);
4289 ata_std_ports(&probe_ent2->port[0]);
4296 * ata_pci_init_one - Initialize/register PCI IDE host controller
4297 * @pdev: Controller to be initialized
4298 * @port_info: Information from low-level host driver
4299 * @n_ports: Number of ports attached to host controller
4301 * This is a helper function which can be called from a driver's
4302 * xxx_init_one() probe function if the hardware uses traditional
4303 * IDE taskfile registers.
4305 * This function calls pci_enable_device(), reserves its register
4306 * regions, sets the dma mask, enables bus master mode, and calls
4310 * Inherited from PCI layer (may sleep).
4313 * Zero on success, negative on errno-based value on error.
4317 int ata_pci_init_one (struct pci_dev *pdev, struct ata_port_info **port_info,
4318 unsigned int n_ports)
4320 struct ata_probe_ent *probe_ent, *probe_ent2 = NULL;
4321 struct ata_port_info *port[2];
4323 unsigned int legacy_mode = 0;
4324 int disable_dev_on_err = 1;
4329 port[0] = port_info[0];
4331 port[1] = port_info[1];
4335 if ((port[0]->host_flags & ATA_FLAG_NO_LEGACY) == 0
4336 && (pdev->class >> 8) == PCI_CLASS_STORAGE_IDE) {
4337 /* TODO: support transitioning to native mode? */
4338 pci_read_config_byte(pdev, PCI_CLASS_PROG, &tmp8);
4339 mask = (1 << 2) | (1 << 0);
4340 if ((tmp8 & mask) != mask)
4341 legacy_mode = (1 << 3);
4345 if ((!legacy_mode) && (n_ports > 1)) {
4346 printk(KERN_ERR "ata: BUG: native mode, n_ports > 1\n");
4350 rc = pci_enable_device(pdev);
4354 rc = pci_request_regions(pdev, DRV_NAME);
4356 disable_dev_on_err = 0;
4361 if (!request_region(0x1f0, 8, "libata")) {
4362 struct resource *conflict, res;
4364 res.end = 0x1f0 + 8 - 1;
4365 conflict = ____request_resource(&ioport_resource, &res);
4366 if (!strcmp(conflict->name, "libata"))
4367 legacy_mode |= (1 << 0);
4369 disable_dev_on_err = 0;
4370 printk(KERN_WARNING "ata: 0x1f0 IDE port busy\n");
4373 legacy_mode |= (1 << 0);
4375 if (!request_region(0x170, 8, "libata")) {
4376 struct resource *conflict, res;
4378 res.end = 0x170 + 8 - 1;
4379 conflict = ____request_resource(&ioport_resource, &res);
4380 if (!strcmp(conflict->name, "libata"))
4381 legacy_mode |= (1 << 1);
4383 disable_dev_on_err = 0;
4384 printk(KERN_WARNING "ata: 0x170 IDE port busy\n");
4387 legacy_mode |= (1 << 1);
4390 /* we have legacy mode, but all ports are unavailable */
4391 if (legacy_mode == (1 << 3)) {
4393 goto err_out_regions;
4396 rc = pci_set_dma_mask(pdev, ATA_DMA_MASK);
4398 goto err_out_regions;
4399 rc = pci_set_consistent_dma_mask(pdev, ATA_DMA_MASK);
4401 goto err_out_regions;
4404 probe_ent = ata_pci_init_legacy_mode(pdev, port, &probe_ent2);
4406 probe_ent = ata_pci_init_native_mode(pdev, port);
4409 goto err_out_regions;
4412 pci_set_master(pdev);
4414 /* FIXME: check ata_device_add return */
4416 if (legacy_mode & (1 << 0))
4417 ata_device_add(probe_ent);
4418 if (legacy_mode & (1 << 1))
4419 ata_device_add(probe_ent2);
4421 ata_device_add(probe_ent);
4429 if (legacy_mode & (1 << 0))
4430 release_region(0x1f0, 8);
4431 if (legacy_mode & (1 << 1))
4432 release_region(0x170, 8);
4433 pci_release_regions(pdev);
4435 if (disable_dev_on_err)
4436 pci_disable_device(pdev);
4441 * ata_pci_remove_one - PCI layer callback for device removal
4442 * @pdev: PCI device that was removed
4444 * PCI layer indicates to libata via this hook that
4445 * hot-unplug or module unload event has occured.
4446 * Handle this by unregistering all objects associated
4447 * with this PCI device. Free those objects. Then finally
4448 * release PCI resources and disable device.
4451 * Inherited from PCI layer (may sleep).
4454 void ata_pci_remove_one (struct pci_dev *pdev)
4456 struct device *dev = pci_dev_to_dev(pdev);
4457 struct ata_host_set *host_set = dev_get_drvdata(dev);
4458 struct ata_port *ap;
4461 for (i = 0; i < host_set->n_ports; i++) {
4462 ap = host_set->ports[i];
4464 scsi_remove_host(ap->host);
4467 free_irq(host_set->irq, host_set);
4469 for (i = 0; i < host_set->n_ports; i++) {
4470 ap = host_set->ports[i];
4472 ata_scsi_release(ap->host);
4474 if ((ap->flags & ATA_FLAG_NO_LEGACY) == 0) {
4475 struct ata_ioports *ioaddr = &ap->ioaddr;
4477 if (ioaddr->cmd_addr == 0x1f0)
4478 release_region(0x1f0, 8);
4479 else if (ioaddr->cmd_addr == 0x170)
4480 release_region(0x170, 8);
4483 scsi_host_put(ap->host);
4486 if (host_set->ops->host_stop)
4487 host_set->ops->host_stop(host_set);
4491 pci_release_regions(pdev);
4492 pci_disable_device(pdev);
4493 dev_set_drvdata(dev, NULL);
4496 /* move to PCI subsystem */
4497 int pci_test_config_bits(struct pci_dev *pdev, struct pci_bits *bits)
4499 unsigned long tmp = 0;
4501 switch (bits->width) {
4504 pci_read_config_byte(pdev, bits->reg, &tmp8);
4510 pci_read_config_word(pdev, bits->reg, &tmp16);
4516 pci_read_config_dword(pdev, bits->reg, &tmp32);
4527 return (tmp == bits->val) ? 1 : 0;
4529 #endif /* CONFIG_PCI */
4532 static int __init ata_init(void)
4534 ata_wq = create_workqueue("ata");
4538 printk(KERN_DEBUG "libata version " DRV_VERSION " loaded.\n");
4542 static void __exit ata_exit(void)
4544 destroy_workqueue(ata_wq);
4547 module_init(ata_init);
4548 module_exit(ata_exit);
4551 * libata is essentially a library of internal helper functions for
4552 * low-level ATA host controller drivers. As such, the API/ABI is
4553 * likely to change as new drivers are added and updated.
4554 * Do not depend on ABI/API stability.
4557 EXPORT_SYMBOL_GPL(ata_std_bios_param);
4558 EXPORT_SYMBOL_GPL(ata_std_ports);
4559 EXPORT_SYMBOL_GPL(ata_device_add);
4560 EXPORT_SYMBOL_GPL(ata_sg_init);
4561 EXPORT_SYMBOL_GPL(ata_sg_init_one);
4562 EXPORT_SYMBOL_GPL(ata_qc_complete);
4563 EXPORT_SYMBOL_GPL(ata_qc_issue_prot);
4564 EXPORT_SYMBOL_GPL(ata_eng_timeout);
4565 EXPORT_SYMBOL_GPL(ata_tf_load);
4566 EXPORT_SYMBOL_GPL(ata_tf_read);
4567 EXPORT_SYMBOL_GPL(ata_noop_dev_select);
4568 EXPORT_SYMBOL_GPL(ata_std_dev_select);
4569 EXPORT_SYMBOL_GPL(ata_tf_to_fis);
4570 EXPORT_SYMBOL_GPL(ata_tf_from_fis);
4571 EXPORT_SYMBOL_GPL(ata_check_status);
4572 EXPORT_SYMBOL_GPL(ata_altstatus);
4573 EXPORT_SYMBOL_GPL(ata_chk_err);
4574 EXPORT_SYMBOL_GPL(ata_exec_command);
4575 EXPORT_SYMBOL_GPL(ata_port_start);
4576 EXPORT_SYMBOL_GPL(ata_port_stop);
4577 EXPORT_SYMBOL_GPL(ata_host_stop);
4578 EXPORT_SYMBOL_GPL(ata_interrupt);
4579 EXPORT_SYMBOL_GPL(ata_qc_prep);
4580 EXPORT_SYMBOL_GPL(ata_bmdma_setup);
4581 EXPORT_SYMBOL_GPL(ata_bmdma_start);
4582 EXPORT_SYMBOL_GPL(ata_bmdma_irq_clear);
4583 EXPORT_SYMBOL_GPL(ata_bmdma_status);
4584 EXPORT_SYMBOL_GPL(ata_bmdma_stop);
4585 EXPORT_SYMBOL_GPL(ata_port_probe);
4586 EXPORT_SYMBOL_GPL(sata_phy_reset);
4587 EXPORT_SYMBOL_GPL(__sata_phy_reset);
4588 EXPORT_SYMBOL_GPL(ata_bus_reset);
4589 EXPORT_SYMBOL_GPL(ata_port_disable);
4590 EXPORT_SYMBOL_GPL(ata_scsi_ioctl);
4591 EXPORT_SYMBOL_GPL(ata_scsi_queuecmd);
4592 EXPORT_SYMBOL_GPL(ata_scsi_error);
4593 EXPORT_SYMBOL_GPL(ata_scsi_slave_config);
4594 EXPORT_SYMBOL_GPL(ata_scsi_release);
4595 EXPORT_SYMBOL_GPL(ata_host_intr);
4596 EXPORT_SYMBOL_GPL(ata_dev_classify);
4597 EXPORT_SYMBOL_GPL(ata_dev_id_string);
4598 EXPORT_SYMBOL_GPL(ata_dev_config);
4599 EXPORT_SYMBOL_GPL(ata_scsi_simulate);
4602 EXPORT_SYMBOL_GPL(pci_test_config_bits);
4603 EXPORT_SYMBOL_GPL(ata_pci_init_native_mode);
4604 EXPORT_SYMBOL_GPL(ata_pci_init_one);
4605 EXPORT_SYMBOL_GPL(ata_pci_remove_one);
4606 #endif /* CONFIG_PCI */