2 * libata-core.c - helper library for ATA
4 * Maintained by: Jeff Garzik <jgarzik@pobox.com>
5 * Please ALWAYS copy linux-ide@vger.kernel.org
8 * Copyright 2003-2004 Red Hat, Inc. All rights reserved.
9 * Copyright 2003-2004 Jeff Garzik
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2, or (at your option)
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; see the file COPYING. If not, write to
24 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
27 * libata documentation is available via 'make {ps|pdf}docs',
28 * as Documentation/DocBook/libata.*
30 * Hardware documentation available from http://www.t13.org/ and
31 * http://www.sata-io.org/
35 #include <linux/config.h>
36 #include <linux/kernel.h>
37 #include <linux/module.h>
38 #include <linux/pci.h>
39 #include <linux/init.h>
40 #include <linux/list.h>
42 #include <linux/highmem.h>
43 #include <linux/spinlock.h>
44 #include <linux/blkdev.h>
45 #include <linux/delay.h>
46 #include <linux/timer.h>
47 #include <linux/interrupt.h>
48 #include <linux/completion.h>
49 #include <linux/suspend.h>
50 #include <linux/workqueue.h>
51 #include <linux/jiffies.h>
52 #include <scsi/scsi.h>
54 #include "scsi_priv.h"
55 #include <scsi/scsi_host.h>
56 #include <linux/libata.h>
58 #include <asm/semaphore.h>
59 #include <asm/byteorder.h>
63 static unsigned int ata_busy_sleep (struct ata_port *ap,
64 unsigned long tmout_pat,
66 static void ata_dev_init_params(struct ata_port *ap, struct ata_device *dev);
67 static void ata_set_mode(struct ata_port *ap);
68 static void ata_dev_set_xfermode(struct ata_port *ap, struct ata_device *dev);
69 static unsigned int ata_get_mode_mask(struct ata_port *ap, int shift);
70 static int fgb(u32 bitmap);
71 static int ata_choose_xfer_mode(struct ata_port *ap,
73 unsigned int *xfer_shift_out);
74 static int ata_qc_complete_noop(struct ata_queued_cmd *qc, u8 drv_stat);
75 static void __ata_qc_complete(struct ata_queued_cmd *qc);
77 static unsigned int ata_unique_id = 1;
78 static struct workqueue_struct *ata_wq;
80 int atapi_enabled = 0;
81 module_param(atapi_enabled, int, 0444);
82 MODULE_PARM_DESC(atapi_enabled, "Enable discovery of ATAPI devices (0=off, 1=on)");
84 MODULE_AUTHOR("Jeff Garzik");
85 MODULE_DESCRIPTION("Library module for ATA devices");
86 MODULE_LICENSE("GPL");
87 MODULE_VERSION(DRV_VERSION);
90 * ata_tf_load - send taskfile registers to host controller
91 * @ap: Port to which output is sent
92 * @tf: ATA taskfile register set
94 * Outputs ATA taskfile to standard ATA host controller.
97 * Inherited from caller.
100 static void ata_tf_load_pio(struct ata_port *ap, struct ata_taskfile *tf)
102 struct ata_ioports *ioaddr = &ap->ioaddr;
103 unsigned int is_addr = tf->flags & ATA_TFLAG_ISADDR;
105 if (tf->ctl != ap->last_ctl) {
106 outb(tf->ctl, ioaddr->ctl_addr);
107 ap->last_ctl = tf->ctl;
111 if (is_addr && (tf->flags & ATA_TFLAG_LBA48)) {
112 outb(tf->hob_feature, ioaddr->feature_addr);
113 outb(tf->hob_nsect, ioaddr->nsect_addr);
114 outb(tf->hob_lbal, ioaddr->lbal_addr);
115 outb(tf->hob_lbam, ioaddr->lbam_addr);
116 outb(tf->hob_lbah, ioaddr->lbah_addr);
117 VPRINTK("hob: feat 0x%X nsect 0x%X, lba 0x%X 0x%X 0x%X\n",
126 outb(tf->feature, ioaddr->feature_addr);
127 outb(tf->nsect, ioaddr->nsect_addr);
128 outb(tf->lbal, ioaddr->lbal_addr);
129 outb(tf->lbam, ioaddr->lbam_addr);
130 outb(tf->lbah, ioaddr->lbah_addr);
131 VPRINTK("feat 0x%X nsect 0x%X lba 0x%X 0x%X 0x%X\n",
139 if (tf->flags & ATA_TFLAG_DEVICE) {
140 outb(tf->device, ioaddr->device_addr);
141 VPRINTK("device 0x%X\n", tf->device);
148 * ata_tf_load_mmio - send taskfile registers to host controller
149 * @ap: Port to which output is sent
150 * @tf: ATA taskfile register set
152 * Outputs ATA taskfile to standard ATA host controller using MMIO.
155 * Inherited from caller.
158 static void ata_tf_load_mmio(struct ata_port *ap, struct ata_taskfile *tf)
160 struct ata_ioports *ioaddr = &ap->ioaddr;
161 unsigned int is_addr = tf->flags & ATA_TFLAG_ISADDR;
163 if (tf->ctl != ap->last_ctl) {
164 writeb(tf->ctl, (void __iomem *) ap->ioaddr.ctl_addr);
165 ap->last_ctl = tf->ctl;
169 if (is_addr && (tf->flags & ATA_TFLAG_LBA48)) {
170 writeb(tf->hob_feature, (void __iomem *) ioaddr->feature_addr);
171 writeb(tf->hob_nsect, (void __iomem *) ioaddr->nsect_addr);
172 writeb(tf->hob_lbal, (void __iomem *) ioaddr->lbal_addr);
173 writeb(tf->hob_lbam, (void __iomem *) ioaddr->lbam_addr);
174 writeb(tf->hob_lbah, (void __iomem *) ioaddr->lbah_addr);
175 VPRINTK("hob: feat 0x%X nsect 0x%X, lba 0x%X 0x%X 0x%X\n",
184 writeb(tf->feature, (void __iomem *) ioaddr->feature_addr);
185 writeb(tf->nsect, (void __iomem *) ioaddr->nsect_addr);
186 writeb(tf->lbal, (void __iomem *) ioaddr->lbal_addr);
187 writeb(tf->lbam, (void __iomem *) ioaddr->lbam_addr);
188 writeb(tf->lbah, (void __iomem *) ioaddr->lbah_addr);
189 VPRINTK("feat 0x%X nsect 0x%X lba 0x%X 0x%X 0x%X\n",
197 if (tf->flags & ATA_TFLAG_DEVICE) {
198 writeb(tf->device, (void __iomem *) ioaddr->device_addr);
199 VPRINTK("device 0x%X\n", tf->device);
207 * ata_tf_load - send taskfile registers to host controller
208 * @ap: Port to which output is sent
209 * @tf: ATA taskfile register set
211 * Outputs ATA taskfile to standard ATA host controller using MMIO
212 * or PIO as indicated by the ATA_FLAG_MMIO flag.
213 * Writes the control, feature, nsect, lbal, lbam, and lbah registers.
214 * Optionally (ATA_TFLAG_LBA48) writes hob_feature, hob_nsect,
215 * hob_lbal, hob_lbam, and hob_lbah.
217 * This function waits for idle (!BUSY and !DRQ) after writing
218 * registers. If the control register has a new value, this
219 * function also waits for idle after writing control and before
220 * writing the remaining registers.
222 * May be used as the tf_load() entry in ata_port_operations.
225 * Inherited from caller.
227 void ata_tf_load(struct ata_port *ap, struct ata_taskfile *tf)
229 if (ap->flags & ATA_FLAG_MMIO)
230 ata_tf_load_mmio(ap, tf);
232 ata_tf_load_pio(ap, tf);
236 * ata_exec_command_pio - issue ATA command to host controller
237 * @ap: port to which command is being issued
238 * @tf: ATA taskfile register set
240 * Issues PIO write to ATA command register, with proper
241 * synchronization with interrupt handler / other threads.
244 * spin_lock_irqsave(host_set lock)
247 static void ata_exec_command_pio(struct ata_port *ap, struct ata_taskfile *tf)
249 DPRINTK("ata%u: cmd 0x%X\n", ap->id, tf->command);
251 outb(tf->command, ap->ioaddr.command_addr);
257 * ata_exec_command_mmio - issue ATA command to host controller
258 * @ap: port to which command is being issued
259 * @tf: ATA taskfile register set
261 * Issues MMIO write to ATA command register, with proper
262 * synchronization with interrupt handler / other threads.
265 * spin_lock_irqsave(host_set lock)
268 static void ata_exec_command_mmio(struct ata_port *ap, struct ata_taskfile *tf)
270 DPRINTK("ata%u: cmd 0x%X\n", ap->id, tf->command);
272 writeb(tf->command, (void __iomem *) ap->ioaddr.command_addr);
278 * ata_exec_command - issue ATA command to host controller
279 * @ap: port to which command is being issued
280 * @tf: ATA taskfile register set
282 * Issues PIO/MMIO write to ATA command register, with proper
283 * synchronization with interrupt handler / other threads.
286 * spin_lock_irqsave(host_set lock)
288 void ata_exec_command(struct ata_port *ap, struct ata_taskfile *tf)
290 if (ap->flags & ATA_FLAG_MMIO)
291 ata_exec_command_mmio(ap, tf);
293 ata_exec_command_pio(ap, tf);
297 * ata_exec - issue ATA command to host controller
298 * @ap: port to which command is being issued
299 * @tf: ATA taskfile register set
301 * Issues PIO/MMIO write to ATA command register, with proper
302 * synchronization with interrupt handler / other threads.
305 * Obtains host_set lock.
308 static inline void ata_exec(struct ata_port *ap, struct ata_taskfile *tf)
312 DPRINTK("ata%u: cmd 0x%X\n", ap->id, tf->command);
313 spin_lock_irqsave(&ap->host_set->lock, flags);
314 ap->ops->exec_command(ap, tf);
315 spin_unlock_irqrestore(&ap->host_set->lock, flags);
319 * ata_tf_to_host - issue ATA taskfile to host controller
320 * @ap: port to which command is being issued
321 * @tf: ATA taskfile register set
323 * Issues ATA taskfile register set to ATA host controller,
324 * with proper synchronization with interrupt handler and
328 * Obtains host_set lock.
331 static void ata_tf_to_host(struct ata_port *ap, struct ata_taskfile *tf)
333 ap->ops->tf_load(ap, tf);
339 * ata_tf_to_host_nolock - issue ATA taskfile to host controller
340 * @ap: port to which command is being issued
341 * @tf: ATA taskfile register set
343 * Issues ATA taskfile register set to ATA host controller,
344 * with proper synchronization with interrupt handler and
348 * spin_lock_irqsave(host_set lock)
351 void ata_tf_to_host_nolock(struct ata_port *ap, struct ata_taskfile *tf)
353 ap->ops->tf_load(ap, tf);
354 ap->ops->exec_command(ap, tf);
358 * ata_tf_read_pio - input device's ATA taskfile shadow registers
359 * @ap: Port from which input is read
360 * @tf: ATA taskfile register set for storing input
362 * Reads ATA taskfile registers for currently-selected device
366 * Inherited from caller.
369 static void ata_tf_read_pio(struct ata_port *ap, struct ata_taskfile *tf)
371 struct ata_ioports *ioaddr = &ap->ioaddr;
373 tf->nsect = inb(ioaddr->nsect_addr);
374 tf->lbal = inb(ioaddr->lbal_addr);
375 tf->lbam = inb(ioaddr->lbam_addr);
376 tf->lbah = inb(ioaddr->lbah_addr);
377 tf->device = inb(ioaddr->device_addr);
379 if (tf->flags & ATA_TFLAG_LBA48) {
380 outb(tf->ctl | ATA_HOB, ioaddr->ctl_addr);
381 tf->hob_feature = inb(ioaddr->error_addr);
382 tf->hob_nsect = inb(ioaddr->nsect_addr);
383 tf->hob_lbal = inb(ioaddr->lbal_addr);
384 tf->hob_lbam = inb(ioaddr->lbam_addr);
385 tf->hob_lbah = inb(ioaddr->lbah_addr);
390 * ata_tf_read_mmio - input device's ATA taskfile shadow registers
391 * @ap: Port from which input is read
392 * @tf: ATA taskfile register set for storing input
394 * Reads ATA taskfile registers for currently-selected device
398 * Inherited from caller.
401 static void ata_tf_read_mmio(struct ata_port *ap, struct ata_taskfile *tf)
403 struct ata_ioports *ioaddr = &ap->ioaddr;
405 tf->nsect = readb((void __iomem *)ioaddr->nsect_addr);
406 tf->lbal = readb((void __iomem *)ioaddr->lbal_addr);
407 tf->lbam = readb((void __iomem *)ioaddr->lbam_addr);
408 tf->lbah = readb((void __iomem *)ioaddr->lbah_addr);
409 tf->device = readb((void __iomem *)ioaddr->device_addr);
411 if (tf->flags & ATA_TFLAG_LBA48) {
412 writeb(tf->ctl | ATA_HOB, (void __iomem *) ap->ioaddr.ctl_addr);
413 tf->hob_feature = readb((void __iomem *)ioaddr->error_addr);
414 tf->hob_nsect = readb((void __iomem *)ioaddr->nsect_addr);
415 tf->hob_lbal = readb((void __iomem *)ioaddr->lbal_addr);
416 tf->hob_lbam = readb((void __iomem *)ioaddr->lbam_addr);
417 tf->hob_lbah = readb((void __iomem *)ioaddr->lbah_addr);
423 * ata_tf_read - input device's ATA taskfile shadow registers
424 * @ap: Port from which input is read
425 * @tf: ATA taskfile register set for storing input
427 * Reads ATA taskfile registers for currently-selected device
430 * Reads nsect, lbal, lbam, lbah, and device. If ATA_TFLAG_LBA48
431 * is set, also reads the hob registers.
433 * May be used as the tf_read() entry in ata_port_operations.
436 * Inherited from caller.
438 void ata_tf_read(struct ata_port *ap, struct ata_taskfile *tf)
440 if (ap->flags & ATA_FLAG_MMIO)
441 ata_tf_read_mmio(ap, tf);
443 ata_tf_read_pio(ap, tf);
447 * ata_check_status_pio - Read device status reg & clear interrupt
448 * @ap: port where the device is
450 * Reads ATA taskfile status register for currently-selected device
451 * and return its value. This also clears pending interrupts
455 * Inherited from caller.
457 static u8 ata_check_status_pio(struct ata_port *ap)
459 return inb(ap->ioaddr.status_addr);
463 * ata_check_status_mmio - Read device status reg & clear interrupt
464 * @ap: port where the device is
466 * Reads ATA taskfile status register for currently-selected device
467 * via MMIO and return its value. This also clears pending interrupts
471 * Inherited from caller.
473 static u8 ata_check_status_mmio(struct ata_port *ap)
475 return readb((void __iomem *) ap->ioaddr.status_addr);
480 * ata_check_status - Read device status reg & clear interrupt
481 * @ap: port where the device is
483 * Reads ATA taskfile status register for currently-selected device
484 * and return its value. This also clears pending interrupts
487 * May be used as the check_status() entry in ata_port_operations.
490 * Inherited from caller.
492 u8 ata_check_status(struct ata_port *ap)
494 if (ap->flags & ATA_FLAG_MMIO)
495 return ata_check_status_mmio(ap);
496 return ata_check_status_pio(ap);
501 * ata_altstatus - Read device alternate status reg
502 * @ap: port where the device is
504 * Reads ATA taskfile alternate status register for
505 * currently-selected device and return its value.
507 * Note: may NOT be used as the check_altstatus() entry in
508 * ata_port_operations.
511 * Inherited from caller.
513 u8 ata_altstatus(struct ata_port *ap)
515 if (ap->ops->check_altstatus)
516 return ap->ops->check_altstatus(ap);
518 if (ap->flags & ATA_FLAG_MMIO)
519 return readb((void __iomem *)ap->ioaddr.altstatus_addr);
520 return inb(ap->ioaddr.altstatus_addr);
525 * ata_chk_err - Read device error reg
526 * @ap: port where the device is
528 * Reads ATA taskfile error register for
529 * currently-selected device and return its value.
531 * Note: may NOT be used as the check_err() entry in
532 * ata_port_operations.
535 * Inherited from caller.
537 u8 ata_chk_err(struct ata_port *ap)
539 if (ap->ops->check_err)
540 return ap->ops->check_err(ap);
542 if (ap->flags & ATA_FLAG_MMIO) {
543 return readb((void __iomem *) ap->ioaddr.error_addr);
545 return inb(ap->ioaddr.error_addr);
549 * ata_tf_to_fis - Convert ATA taskfile to SATA FIS structure
550 * @tf: Taskfile to convert
551 * @fis: Buffer into which data will output
552 * @pmp: Port multiplier port
554 * Converts a standard ATA taskfile to a Serial ATA
555 * FIS structure (Register - Host to Device).
558 * Inherited from caller.
561 void ata_tf_to_fis(struct ata_taskfile *tf, u8 *fis, u8 pmp)
563 fis[0] = 0x27; /* Register - Host to Device FIS */
564 fis[1] = (pmp & 0xf) | (1 << 7); /* Port multiplier number,
565 bit 7 indicates Command FIS */
566 fis[2] = tf->command;
567 fis[3] = tf->feature;
574 fis[8] = tf->hob_lbal;
575 fis[9] = tf->hob_lbam;
576 fis[10] = tf->hob_lbah;
577 fis[11] = tf->hob_feature;
580 fis[13] = tf->hob_nsect;
591 * ata_tf_from_fis - Convert SATA FIS to ATA taskfile
592 * @fis: Buffer from which data will be input
593 * @tf: Taskfile to output
595 * Converts a standard ATA taskfile to a Serial ATA
596 * FIS structure (Register - Host to Device).
599 * Inherited from caller.
602 void ata_tf_from_fis(u8 *fis, struct ata_taskfile *tf)
604 tf->command = fis[2]; /* status */
605 tf->feature = fis[3]; /* error */
612 tf->hob_lbal = fis[8];
613 tf->hob_lbam = fis[9];
614 tf->hob_lbah = fis[10];
617 tf->hob_nsect = fis[13];
621 * ata_prot_to_cmd - determine which read/write opcodes to use
622 * @protocol: ATA_PROT_xxx taskfile protocol
623 * @lba48: true is lba48 is present
625 * Given necessary input, determine which read/write commands
626 * to use to transfer data.
631 static int ata_prot_to_cmd(int protocol, int lba48)
633 int rcmd = 0, wcmd = 0;
638 rcmd = ATA_CMD_PIO_READ_EXT;
639 wcmd = ATA_CMD_PIO_WRITE_EXT;
641 rcmd = ATA_CMD_PIO_READ;
642 wcmd = ATA_CMD_PIO_WRITE;
648 rcmd = ATA_CMD_READ_EXT;
649 wcmd = ATA_CMD_WRITE_EXT;
652 wcmd = ATA_CMD_WRITE;
660 return rcmd | (wcmd << 8);
664 * ata_dev_set_protocol - set taskfile protocol and r/w commands
665 * @dev: device to examine and configure
667 * Examine the device configuration, after we have
668 * read the identify-device page and configured the
669 * data transfer mode. Set internal state related to
670 * the ATA taskfile protocol (pio, pio mult, dma, etc.)
671 * and calculate the proper read/write commands to use.
676 static void ata_dev_set_protocol(struct ata_device *dev)
678 int pio = (dev->flags & ATA_DFLAG_PIO);
679 int lba48 = (dev->flags & ATA_DFLAG_LBA48);
683 proto = dev->xfer_protocol = ATA_PROT_PIO;
685 proto = dev->xfer_protocol = ATA_PROT_DMA;
687 cmd = ata_prot_to_cmd(proto, lba48);
691 dev->read_cmd = cmd & 0xff;
692 dev->write_cmd = (cmd >> 8) & 0xff;
695 static const char * xfer_mode_str[] = {
715 * ata_udma_string - convert UDMA bit offset to string
716 * @mask: mask of bits supported; only highest bit counts.
718 * Determine string which represents the highest speed
719 * (highest bit in @udma_mask).
725 * Constant C string representing highest speed listed in
726 * @udma_mask, or the constant C string "<n/a>".
729 static const char *ata_mode_string(unsigned int mask)
733 for (i = 7; i >= 0; i--)
736 for (i = ATA_SHIFT_MWDMA + 2; i >= ATA_SHIFT_MWDMA; i--)
739 for (i = ATA_SHIFT_PIO + 4; i >= ATA_SHIFT_PIO; i--)
746 return xfer_mode_str[i];
750 * ata_pio_devchk - PATA device presence detection
751 * @ap: ATA channel to examine
752 * @device: Device to examine (starting at zero)
754 * This technique was originally described in
755 * Hale Landis's ATADRVR (www.ata-atapi.com), and
756 * later found its way into the ATA/ATAPI spec.
758 * Write a pattern to the ATA shadow registers,
759 * and if a device is present, it will respond by
760 * correctly storing and echoing back the
761 * ATA shadow register contents.
767 static unsigned int ata_pio_devchk(struct ata_port *ap,
770 struct ata_ioports *ioaddr = &ap->ioaddr;
773 ap->ops->dev_select(ap, device);
775 outb(0x55, ioaddr->nsect_addr);
776 outb(0xaa, ioaddr->lbal_addr);
778 outb(0xaa, ioaddr->nsect_addr);
779 outb(0x55, ioaddr->lbal_addr);
781 outb(0x55, ioaddr->nsect_addr);
782 outb(0xaa, ioaddr->lbal_addr);
784 nsect = inb(ioaddr->nsect_addr);
785 lbal = inb(ioaddr->lbal_addr);
787 if ((nsect == 0x55) && (lbal == 0xaa))
788 return 1; /* we found a device */
790 return 0; /* nothing found */
794 * ata_mmio_devchk - PATA device presence detection
795 * @ap: ATA channel to examine
796 * @device: Device to examine (starting at zero)
798 * This technique was originally described in
799 * Hale Landis's ATADRVR (www.ata-atapi.com), and
800 * later found its way into the ATA/ATAPI spec.
802 * Write a pattern to the ATA shadow registers,
803 * and if a device is present, it will respond by
804 * correctly storing and echoing back the
805 * ATA shadow register contents.
811 static unsigned int ata_mmio_devchk(struct ata_port *ap,
814 struct ata_ioports *ioaddr = &ap->ioaddr;
817 ap->ops->dev_select(ap, device);
819 writeb(0x55, (void __iomem *) ioaddr->nsect_addr);
820 writeb(0xaa, (void __iomem *) ioaddr->lbal_addr);
822 writeb(0xaa, (void __iomem *) ioaddr->nsect_addr);
823 writeb(0x55, (void __iomem *) ioaddr->lbal_addr);
825 writeb(0x55, (void __iomem *) ioaddr->nsect_addr);
826 writeb(0xaa, (void __iomem *) ioaddr->lbal_addr);
828 nsect = readb((void __iomem *) ioaddr->nsect_addr);
829 lbal = readb((void __iomem *) ioaddr->lbal_addr);
831 if ((nsect == 0x55) && (lbal == 0xaa))
832 return 1; /* we found a device */
834 return 0; /* nothing found */
838 * ata_devchk - PATA device presence detection
839 * @ap: ATA channel to examine
840 * @device: Device to examine (starting at zero)
842 * Dispatch ATA device presence detection, depending
843 * on whether we are using PIO or MMIO to talk to the
844 * ATA shadow registers.
850 static unsigned int ata_devchk(struct ata_port *ap,
853 if (ap->flags & ATA_FLAG_MMIO)
854 return ata_mmio_devchk(ap, device);
855 return ata_pio_devchk(ap, device);
859 * ata_dev_classify - determine device type based on ATA-spec signature
860 * @tf: ATA taskfile register set for device to be identified
862 * Determine from taskfile register contents whether a device is
863 * ATA or ATAPI, as per "Signature and persistence" section
864 * of ATA/PI spec (volume 1, sect 5.14).
870 * Device type, %ATA_DEV_ATA, %ATA_DEV_ATAPI, or %ATA_DEV_UNKNOWN
871 * the event of failure.
874 unsigned int ata_dev_classify(struct ata_taskfile *tf)
876 /* Apple's open source Darwin code hints that some devices only
877 * put a proper signature into the LBA mid/high registers,
878 * So, we only check those. It's sufficient for uniqueness.
881 if (((tf->lbam == 0) && (tf->lbah == 0)) ||
882 ((tf->lbam == 0x3c) && (tf->lbah == 0xc3))) {
883 DPRINTK("found ATA device by sig\n");
887 if (((tf->lbam == 0x14) && (tf->lbah == 0xeb)) ||
888 ((tf->lbam == 0x69) && (tf->lbah == 0x96))) {
889 DPRINTK("found ATAPI device by sig\n");
890 return ATA_DEV_ATAPI;
893 DPRINTK("unknown device\n");
894 return ATA_DEV_UNKNOWN;
898 * ata_dev_try_classify - Parse returned ATA device signature
899 * @ap: ATA channel to examine
900 * @device: Device to examine (starting at zero)
902 * After an event -- SRST, E.D.D., or SATA COMRESET -- occurs,
903 * an ATA/ATAPI-defined set of values is placed in the ATA
904 * shadow registers, indicating the results of device detection
907 * Select the ATA device, and read the values from the ATA shadow
908 * registers. Then parse according to the Error register value,
909 * and the spec-defined values examined by ata_dev_classify().
915 static u8 ata_dev_try_classify(struct ata_port *ap, unsigned int device)
917 struct ata_device *dev = &ap->device[device];
918 struct ata_taskfile tf;
922 ap->ops->dev_select(ap, device);
924 memset(&tf, 0, sizeof(tf));
926 err = ata_chk_err(ap);
927 ap->ops->tf_read(ap, &tf);
929 dev->class = ATA_DEV_NONE;
931 /* see if device passed diags */
934 else if ((device == 0) && (err == 0x81))
939 /* determine if device if ATA or ATAPI */
940 class = ata_dev_classify(&tf);
941 if (class == ATA_DEV_UNKNOWN)
943 if ((class == ATA_DEV_ATA) && (ata_chk_status(ap) == 0))
952 * ata_dev_id_string - Convert IDENTIFY DEVICE page into string
953 * @id: IDENTIFY DEVICE results we will examine
954 * @s: string into which data is output
955 * @ofs: offset into identify device page
956 * @len: length of string to return. must be an even number.
958 * The strings in the IDENTIFY DEVICE page are broken up into
959 * 16-bit chunks. Run through the string, and output each
960 * 8-bit chunk linearly, regardless of platform.
966 void ata_dev_id_string(u16 *id, unsigned char *s,
967 unsigned int ofs, unsigned int len)
987 * ata_noop_dev_select - Select device 0/1 on ATA bus
988 * @ap: ATA channel to manipulate
989 * @device: ATA device (numbered from zero) to select
991 * This function performs no actual function.
993 * May be used as the dev_select() entry in ata_port_operations.
998 void ata_noop_dev_select (struct ata_port *ap, unsigned int device)
1004 * ata_std_dev_select - Select device 0/1 on ATA bus
1005 * @ap: ATA channel to manipulate
1006 * @device: ATA device (numbered from zero) to select
1008 * Use the method defined in the ATA specification to
1009 * make either device 0, or device 1, active on the
1010 * ATA channel. Works with both PIO and MMIO.
1012 * May be used as the dev_select() entry in ata_port_operations.
1018 void ata_std_dev_select (struct ata_port *ap, unsigned int device)
1023 tmp = ATA_DEVICE_OBS;
1025 tmp = ATA_DEVICE_OBS | ATA_DEV1;
1027 if (ap->flags & ATA_FLAG_MMIO) {
1028 writeb(tmp, (void __iomem *) ap->ioaddr.device_addr);
1030 outb(tmp, ap->ioaddr.device_addr);
1032 ata_pause(ap); /* needed; also flushes, for mmio */
1036 * ata_dev_select - Select device 0/1 on ATA bus
1037 * @ap: ATA channel to manipulate
1038 * @device: ATA device (numbered from zero) to select
1039 * @wait: non-zero to wait for Status register BSY bit to clear
1040 * @can_sleep: non-zero if context allows sleeping
1042 * Use the method defined in the ATA specification to
1043 * make either device 0, or device 1, active on the
1046 * This is a high-level version of ata_std_dev_select(),
1047 * which additionally provides the services of inserting
1048 * the proper pauses and status polling, where needed.
1054 void ata_dev_select(struct ata_port *ap, unsigned int device,
1055 unsigned int wait, unsigned int can_sleep)
1057 VPRINTK("ENTER, ata%u: device %u, wait %u\n",
1058 ap->id, device, wait);
1063 ap->ops->dev_select(ap, device);
1066 if (can_sleep && ap->device[device].class == ATA_DEV_ATAPI)
1073 * ata_dump_id - IDENTIFY DEVICE info debugging output
1074 * @dev: Device whose IDENTIFY DEVICE page we will dump
1076 * Dump selected 16-bit words from a detected device's
1077 * IDENTIFY PAGE page.
1083 static inline void ata_dump_id(struct ata_device *dev)
1085 DPRINTK("49==0x%04x "
1095 DPRINTK("80==0x%04x "
1105 DPRINTK("88==0x%04x "
1112 * ata_dev_identify - obtain IDENTIFY x DEVICE page
1113 * @ap: port on which device we wish to probe resides
1114 * @device: device bus address, starting at zero
1116 * Following bus reset, we issue the IDENTIFY [PACKET] DEVICE
1117 * command, and read back the 512-byte device information page.
1118 * The device information page is fed to us via the standard
1119 * PIO-IN protocol, but we hand-code it here. (TODO: investigate
1120 * using standard PIO-IN paths)
1122 * After reading the device information page, we use several
1123 * bits of information from it to initialize data structures
1124 * that will be used during the lifetime of the ata_device.
1125 * Other data from the info page is used to disqualify certain
1126 * older ATA devices we do not wish to support.
1129 * Inherited from caller. Some functions called by this function
1130 * obtain the host_set lock.
1133 static void ata_dev_identify(struct ata_port *ap, unsigned int device)
1135 struct ata_device *dev = &ap->device[device];
1136 unsigned int major_version;
1138 unsigned long xfer_modes;
1140 unsigned int using_edd;
1141 DECLARE_COMPLETION(wait);
1142 struct ata_queued_cmd *qc;
1143 unsigned long flags;
1146 if (!ata_dev_present(dev)) {
1147 DPRINTK("ENTER/EXIT (host %u, dev %u) -- nodev\n",
1152 if (ap->flags & (ATA_FLAG_SRST | ATA_FLAG_SATA_RESET))
1157 DPRINTK("ENTER, host %u, dev %u\n", ap->id, device);
1159 assert (dev->class == ATA_DEV_ATA || dev->class == ATA_DEV_ATAPI ||
1160 dev->class == ATA_DEV_NONE);
1162 ata_dev_select(ap, device, 1, 1); /* select device 0/1 */
1164 qc = ata_qc_new_init(ap, dev);
1167 ata_sg_init_one(qc, dev->id, sizeof(dev->id));
1168 qc->dma_dir = DMA_FROM_DEVICE;
1169 qc->tf.protocol = ATA_PROT_PIO;
1173 if (dev->class == ATA_DEV_ATA) {
1174 qc->tf.command = ATA_CMD_ID_ATA;
1175 DPRINTK("do ATA identify\n");
1177 qc->tf.command = ATA_CMD_ID_ATAPI;
1178 DPRINTK("do ATAPI identify\n");
1181 qc->waiting = &wait;
1182 qc->complete_fn = ata_qc_complete_noop;
1184 spin_lock_irqsave(&ap->host_set->lock, flags);
1185 rc = ata_qc_issue(qc);
1186 spin_unlock_irqrestore(&ap->host_set->lock, flags);
1191 wait_for_completion(&wait);
1193 status = ata_chk_status(ap);
1194 if (status & ATA_ERR) {
1196 * arg! EDD works for all test cases, but seems to return
1197 * the ATA signature for some ATAPI devices. Until the
1198 * reason for this is found and fixed, we fix up the mess
1199 * here. If IDENTIFY DEVICE returns command aborted
1200 * (as ATAPI devices do), then we issue an
1201 * IDENTIFY PACKET DEVICE.
1203 * ATA software reset (SRST, the default) does not appear
1204 * to have this problem.
1206 if ((using_edd) && (qc->tf.command == ATA_CMD_ID_ATA)) {
1207 u8 err = ata_chk_err(ap);
1208 if (err & ATA_ABORTED) {
1209 dev->class = ATA_DEV_ATAPI;
1220 swap_buf_le16(dev->id, ATA_ID_WORDS);
1222 /* print device capabilities */
1223 printk(KERN_DEBUG "ata%u: dev %u cfg "
1224 "49:%04x 82:%04x 83:%04x 84:%04x 85:%04x 86:%04x 87:%04x 88:%04x\n",
1225 ap->id, device, dev->id[49],
1226 dev->id[82], dev->id[83], dev->id[84],
1227 dev->id[85], dev->id[86], dev->id[87],
1231 * common ATA, ATAPI feature tests
1234 /* we require DMA support (bits 8 of word 49) */
1235 if (!ata_id_has_dma(dev->id)) {
1236 printk(KERN_DEBUG "ata%u: no dma\n", ap->id);
1240 /* quick-n-dirty find max transfer mode; for printk only */
1241 xfer_modes = dev->id[ATA_ID_UDMA_MODES];
1243 xfer_modes = (dev->id[ATA_ID_MWDMA_MODES]) << ATA_SHIFT_MWDMA;
1245 xfer_modes = (dev->id[ATA_ID_PIO_MODES]) << (ATA_SHIFT_PIO + 3);
1246 xfer_modes |= (0x7 << ATA_SHIFT_PIO);
1251 /* ATA-specific feature tests */
1252 if (dev->class == ATA_DEV_ATA) {
1253 if (!ata_id_is_ata(dev->id)) /* sanity check */
1256 /* get major version */
1257 tmp = dev->id[ATA_ID_MAJOR_VER];
1258 for (major_version = 14; major_version >= 1; major_version--)
1259 if (tmp & (1 << major_version))
1263 * The exact sequence expected by certain pre-ATA4 drives is:
1266 * INITIALIZE DEVICE PARAMETERS
1268 * Some drives were very specific about that exact sequence.
1270 if (major_version < 4 || (!ata_id_has_lba(dev->id)))
1271 ata_dev_init_params(ap, dev);
1273 if (ata_id_has_lba(dev->id)) {
1274 dev->flags |= ATA_DFLAG_LBA;
1276 if (ata_id_has_lba48(dev->id)) {
1277 dev->flags |= ATA_DFLAG_LBA48;
1278 dev->n_sectors = ata_id_u64(dev->id, 100);
1280 dev->n_sectors = ata_id_u32(dev->id, 60);
1283 /* print device info to dmesg */
1284 printk(KERN_INFO "ata%u: dev %u ATA-%d, max %s, %Lu sectors:%s\n",
1287 ata_mode_string(xfer_modes),
1288 (unsigned long long)dev->n_sectors,
1289 dev->flags & ATA_DFLAG_LBA48 ? " LBA48" : " LBA");
1293 /* Default translation */
1294 dev->cylinders = dev->id[1];
1295 dev->heads = dev->id[3];
1296 dev->sectors = dev->id[6];
1297 dev->n_sectors = dev->cylinders * dev->heads * dev->sectors;
1299 if (ata_id_current_chs_valid(dev->id)) {
1300 /* Current CHS translation is valid. */
1301 dev->cylinders = dev->id[54];
1302 dev->heads = dev->id[55];
1303 dev->sectors = dev->id[56];
1305 dev->n_sectors = ata_id_u32(dev->id, 57);
1308 /* print device info to dmesg */
1309 printk(KERN_INFO "ata%u: dev %u ATA-%d, max %s, %Lu sectors: CHS %d/%d/%d\n",
1312 ata_mode_string(xfer_modes),
1313 (unsigned long long)dev->n_sectors,
1314 (int)dev->cylinders, (int)dev->heads, (int)dev->sectors);
1318 ap->host->max_cmd_len = 16;
1321 /* ATAPI-specific feature tests */
1323 if (ata_id_is_ata(dev->id)) /* sanity check */
1326 rc = atapi_cdb_len(dev->id);
1327 if ((rc < 12) || (rc > ATAPI_CDB_LEN)) {
1328 printk(KERN_WARNING "ata%u: unsupported CDB len\n", ap->id);
1331 ap->cdb_len = (unsigned int) rc;
1332 ap->host->max_cmd_len = (unsigned char) ap->cdb_len;
1334 /* print device info to dmesg */
1335 printk(KERN_INFO "ata%u: dev %u ATAPI, max %s\n",
1337 ata_mode_string(xfer_modes));
1340 DPRINTK("EXIT, drv_stat = 0x%x\n", ata_chk_status(ap));
1344 printk(KERN_WARNING "ata%u: dev %u not supported, ignoring\n",
1347 dev->class++; /* converts ATA_DEV_xxx into ATA_DEV_xxx_UNSUP */
1348 DPRINTK("EXIT, err\n");
1352 static inline u8 ata_dev_knobble(struct ata_port *ap)
1354 return ((ap->cbl == ATA_CBL_SATA) && (!ata_id_is_sata(ap->device->id)));
1358 * ata_dev_config - Run device specific handlers and check for
1359 * SATA->PATA bridges
1366 void ata_dev_config(struct ata_port *ap, unsigned int i)
1368 /* limit bridge transfers to udma5, 200 sectors */
1369 if (ata_dev_knobble(ap)) {
1370 printk(KERN_INFO "ata%u(%u): applying bridge limits\n",
1371 ap->id, ap->device->devno);
1372 ap->udma_mask &= ATA_UDMA5;
1373 ap->host->max_sectors = ATA_MAX_SECTORS;
1374 ap->host->hostt->max_sectors = ATA_MAX_SECTORS;
1375 ap->device->flags |= ATA_DFLAG_LOCK_SECTORS;
1378 if (ap->ops->dev_config)
1379 ap->ops->dev_config(ap, &ap->device[i]);
1383 * ata_bus_probe - Reset and probe ATA bus
1386 * Master ATA bus probing function. Initiates a hardware-dependent
1387 * bus reset, then attempts to identify any devices found on
1391 * PCI/etc. bus probe sem.
1394 * Zero on success, non-zero on error.
1397 static int ata_bus_probe(struct ata_port *ap)
1399 unsigned int i, found = 0;
1401 ap->ops->phy_reset(ap);
1402 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1405 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1406 ata_dev_identify(ap, i);
1407 if (ata_dev_present(&ap->device[i])) {
1409 ata_dev_config(ap,i);
1413 if ((!found) || (ap->flags & ATA_FLAG_PORT_DISABLED))
1414 goto err_out_disable;
1417 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1418 goto err_out_disable;
1423 ap->ops->port_disable(ap);
1429 * ata_port_probe - Mark port as enabled
1430 * @ap: Port for which we indicate enablement
1432 * Modify @ap data structure such that the system
1433 * thinks that the entire port is enabled.
1435 * LOCKING: host_set lock, or some other form of
1439 void ata_port_probe(struct ata_port *ap)
1441 ap->flags &= ~ATA_FLAG_PORT_DISABLED;
1445 * __sata_phy_reset - Wake/reset a low-level SATA PHY
1446 * @ap: SATA port associated with target SATA PHY.
1448 * This function issues commands to standard SATA Sxxx
1449 * PHY registers, to wake up the phy (and device), and
1450 * clear any reset condition.
1453 * PCI/etc. bus probe sem.
1456 void __sata_phy_reset(struct ata_port *ap)
1459 unsigned long timeout = jiffies + (HZ * 5);
1461 if (ap->flags & ATA_FLAG_SATA_RESET) {
1462 /* issue phy wake/reset */
1463 scr_write_flush(ap, SCR_CONTROL, 0x301);
1464 /* Couldn't find anything in SATA I/II specs, but
1465 * AHCI-1.1 10.4.2 says at least 1 ms. */
1468 scr_write_flush(ap, SCR_CONTROL, 0x300); /* phy wake/clear reset */
1470 /* wait for phy to become ready, if necessary */
1473 sstatus = scr_read(ap, SCR_STATUS);
1474 if ((sstatus & 0xf) != 1)
1476 } while (time_before(jiffies, timeout));
1478 /* TODO: phy layer with polling, timeouts, etc. */
1479 if (sata_dev_present(ap))
1482 sstatus = scr_read(ap, SCR_STATUS);
1483 printk(KERN_INFO "ata%u: no device found (phy stat %08x)\n",
1485 ata_port_disable(ap);
1488 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1491 if (ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT)) {
1492 ata_port_disable(ap);
1496 ap->cbl = ATA_CBL_SATA;
1500 * sata_phy_reset - Reset SATA bus.
1501 * @ap: SATA port associated with target SATA PHY.
1503 * This function resets the SATA bus, and then probes
1504 * the bus for devices.
1507 * PCI/etc. bus probe sem.
1510 void sata_phy_reset(struct ata_port *ap)
1512 __sata_phy_reset(ap);
1513 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1519 * ata_port_disable - Disable port.
1520 * @ap: Port to be disabled.
1522 * Modify @ap data structure such that the system
1523 * thinks that the entire port is disabled, and should
1524 * never attempt to probe or communicate with devices
1527 * LOCKING: host_set lock, or some other form of
1531 void ata_port_disable(struct ata_port *ap)
1533 ap->device[0].class = ATA_DEV_NONE;
1534 ap->device[1].class = ATA_DEV_NONE;
1535 ap->flags |= ATA_FLAG_PORT_DISABLED;
1541 } xfer_mode_classes[] = {
1542 { ATA_SHIFT_UDMA, XFER_UDMA_0 },
1543 { ATA_SHIFT_MWDMA, XFER_MW_DMA_0 },
1544 { ATA_SHIFT_PIO, XFER_PIO_0 },
1547 static inline u8 base_from_shift(unsigned int shift)
1551 for (i = 0; i < ARRAY_SIZE(xfer_mode_classes); i++)
1552 if (xfer_mode_classes[i].shift == shift)
1553 return xfer_mode_classes[i].base;
1558 static void ata_dev_set_mode(struct ata_port *ap, struct ata_device *dev)
1563 if (!ata_dev_present(dev) || (ap->flags & ATA_FLAG_PORT_DISABLED))
1566 if (dev->xfer_shift == ATA_SHIFT_PIO)
1567 dev->flags |= ATA_DFLAG_PIO;
1569 ata_dev_set_xfermode(ap, dev);
1571 base = base_from_shift(dev->xfer_shift);
1572 ofs = dev->xfer_mode - base;
1573 idx = ofs + dev->xfer_shift;
1574 WARN_ON(idx >= ARRAY_SIZE(xfer_mode_str));
1576 DPRINTK("idx=%d xfer_shift=%u, xfer_mode=0x%x, base=0x%x, offset=%d\n",
1577 idx, dev->xfer_shift, (int)dev->xfer_mode, (int)base, ofs);
1579 printk(KERN_INFO "ata%u: dev %u configured for %s\n",
1580 ap->id, dev->devno, xfer_mode_str[idx]);
1583 static int ata_host_set_pio(struct ata_port *ap)
1589 mask = ata_get_mode_mask(ap, ATA_SHIFT_PIO);
1592 printk(KERN_WARNING "ata%u: no PIO support\n", ap->id);
1596 base = base_from_shift(ATA_SHIFT_PIO);
1597 xfer_mode = base + x;
1599 DPRINTK("base 0x%x xfer_mode 0x%x mask 0x%x x %d\n",
1600 (int)base, (int)xfer_mode, mask, x);
1602 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1603 struct ata_device *dev = &ap->device[i];
1604 if (ata_dev_present(dev)) {
1605 dev->pio_mode = xfer_mode;
1606 dev->xfer_mode = xfer_mode;
1607 dev->xfer_shift = ATA_SHIFT_PIO;
1608 if (ap->ops->set_piomode)
1609 ap->ops->set_piomode(ap, dev);
1616 static void ata_host_set_dma(struct ata_port *ap, u8 xfer_mode,
1617 unsigned int xfer_shift)
1621 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1622 struct ata_device *dev = &ap->device[i];
1623 if (ata_dev_present(dev)) {
1624 dev->dma_mode = xfer_mode;
1625 dev->xfer_mode = xfer_mode;
1626 dev->xfer_shift = xfer_shift;
1627 if (ap->ops->set_dmamode)
1628 ap->ops->set_dmamode(ap, dev);
1634 * ata_set_mode - Program timings and issue SET FEATURES - XFER
1635 * @ap: port on which timings will be programmed
1637 * Set ATA device disk transfer mode (PIO3, UDMA6, etc.).
1640 * PCI/etc. bus probe sem.
1643 static void ata_set_mode(struct ata_port *ap)
1645 unsigned int i, xfer_shift;
1649 /* step 1: always set host PIO timings */
1650 rc = ata_host_set_pio(ap);
1654 /* step 2: choose the best data xfer mode */
1655 xfer_mode = xfer_shift = 0;
1656 rc = ata_choose_xfer_mode(ap, &xfer_mode, &xfer_shift);
1660 /* step 3: if that xfer mode isn't PIO, set host DMA timings */
1661 if (xfer_shift != ATA_SHIFT_PIO)
1662 ata_host_set_dma(ap, xfer_mode, xfer_shift);
1664 /* step 4: update devices' xfer mode */
1665 ata_dev_set_mode(ap, &ap->device[0]);
1666 ata_dev_set_mode(ap, &ap->device[1]);
1668 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1671 if (ap->ops->post_set_mode)
1672 ap->ops->post_set_mode(ap);
1674 for (i = 0; i < 2; i++) {
1675 struct ata_device *dev = &ap->device[i];
1676 ata_dev_set_protocol(dev);
1682 ata_port_disable(ap);
1686 * ata_busy_sleep - sleep until BSY clears, or timeout
1687 * @ap: port containing status register to be polled
1688 * @tmout_pat: impatience timeout
1689 * @tmout: overall timeout
1691 * Sleep until ATA Status register bit BSY clears,
1692 * or a timeout occurs.
1698 static unsigned int ata_busy_sleep (struct ata_port *ap,
1699 unsigned long tmout_pat,
1700 unsigned long tmout)
1702 unsigned long timer_start, timeout;
1705 status = ata_busy_wait(ap, ATA_BUSY, 300);
1706 timer_start = jiffies;
1707 timeout = timer_start + tmout_pat;
1708 while ((status & ATA_BUSY) && (time_before(jiffies, timeout))) {
1710 status = ata_busy_wait(ap, ATA_BUSY, 3);
1713 if (status & ATA_BUSY)
1714 printk(KERN_WARNING "ata%u is slow to respond, "
1715 "please be patient\n", ap->id);
1717 timeout = timer_start + tmout;
1718 while ((status & ATA_BUSY) && (time_before(jiffies, timeout))) {
1720 status = ata_chk_status(ap);
1723 if (status & ATA_BUSY) {
1724 printk(KERN_ERR "ata%u failed to respond (%lu secs)\n",
1725 ap->id, tmout / HZ);
1732 static void ata_bus_post_reset(struct ata_port *ap, unsigned int devmask)
1734 struct ata_ioports *ioaddr = &ap->ioaddr;
1735 unsigned int dev0 = devmask & (1 << 0);
1736 unsigned int dev1 = devmask & (1 << 1);
1737 unsigned long timeout;
1739 /* if device 0 was found in ata_devchk, wait for its
1743 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
1745 /* if device 1 was found in ata_devchk, wait for
1746 * register access, then wait for BSY to clear
1748 timeout = jiffies + ATA_TMOUT_BOOT;
1752 ap->ops->dev_select(ap, 1);
1753 if (ap->flags & ATA_FLAG_MMIO) {
1754 nsect = readb((void __iomem *) ioaddr->nsect_addr);
1755 lbal = readb((void __iomem *) ioaddr->lbal_addr);
1757 nsect = inb(ioaddr->nsect_addr);
1758 lbal = inb(ioaddr->lbal_addr);
1760 if ((nsect == 1) && (lbal == 1))
1762 if (time_after(jiffies, timeout)) {
1766 msleep(50); /* give drive a breather */
1769 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
1771 /* is all this really necessary? */
1772 ap->ops->dev_select(ap, 0);
1774 ap->ops->dev_select(ap, 1);
1776 ap->ops->dev_select(ap, 0);
1780 * ata_bus_edd - Issue EXECUTE DEVICE DIAGNOSTIC command.
1781 * @ap: Port to reset and probe
1783 * Use the EXECUTE DEVICE DIAGNOSTIC command to reset and
1784 * probe the bus. Not often used these days.
1787 * PCI/etc. bus probe sem.
1791 static unsigned int ata_bus_edd(struct ata_port *ap)
1793 struct ata_taskfile tf;
1795 /* set up execute-device-diag (bus reset) taskfile */
1796 /* also, take interrupts to a known state (disabled) */
1797 DPRINTK("execute-device-diag\n");
1798 ata_tf_init(ap, &tf, 0);
1800 tf.command = ATA_CMD_EDD;
1801 tf.protocol = ATA_PROT_NODATA;
1804 ata_tf_to_host(ap, &tf);
1806 /* spec says at least 2ms. but who knows with those
1807 * crazy ATAPI devices...
1811 return ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
1814 static unsigned int ata_bus_softreset(struct ata_port *ap,
1815 unsigned int devmask)
1817 struct ata_ioports *ioaddr = &ap->ioaddr;
1819 DPRINTK("ata%u: bus reset via SRST\n", ap->id);
1821 /* software reset. causes dev0 to be selected */
1822 if (ap->flags & ATA_FLAG_MMIO) {
1823 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
1824 udelay(20); /* FIXME: flush */
1825 writeb(ap->ctl | ATA_SRST, (void __iomem *) ioaddr->ctl_addr);
1826 udelay(20); /* FIXME: flush */
1827 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
1829 outb(ap->ctl, ioaddr->ctl_addr);
1831 outb(ap->ctl | ATA_SRST, ioaddr->ctl_addr);
1833 outb(ap->ctl, ioaddr->ctl_addr);
1836 /* spec mandates ">= 2ms" before checking status.
1837 * We wait 150ms, because that was the magic delay used for
1838 * ATAPI devices in Hale Landis's ATADRVR, for the period of time
1839 * between when the ATA command register is written, and then
1840 * status is checked. Because waiting for "a while" before
1841 * checking status is fine, post SRST, we perform this magic
1842 * delay here as well.
1846 ata_bus_post_reset(ap, devmask);
1852 * ata_bus_reset - reset host port and associated ATA channel
1853 * @ap: port to reset
1855 * This is typically the first time we actually start issuing
1856 * commands to the ATA channel. We wait for BSY to clear, then
1857 * issue EXECUTE DEVICE DIAGNOSTIC command, polling for its
1858 * result. Determine what devices, if any, are on the channel
1859 * by looking at the device 0/1 error register. Look at the signature
1860 * stored in each device's taskfile registers, to determine if
1861 * the device is ATA or ATAPI.
1864 * PCI/etc. bus probe sem.
1865 * Obtains host_set lock.
1868 * Sets ATA_FLAG_PORT_DISABLED if bus reset fails.
1871 void ata_bus_reset(struct ata_port *ap)
1873 struct ata_ioports *ioaddr = &ap->ioaddr;
1874 unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS;
1876 unsigned int dev0, dev1 = 0, rc = 0, devmask = 0;
1878 DPRINTK("ENTER, host %u, port %u\n", ap->id, ap->port_no);
1880 /* determine if device 0/1 are present */
1881 if (ap->flags & ATA_FLAG_SATA_RESET)
1884 dev0 = ata_devchk(ap, 0);
1886 dev1 = ata_devchk(ap, 1);
1890 devmask |= (1 << 0);
1892 devmask |= (1 << 1);
1894 /* select device 0 again */
1895 ap->ops->dev_select(ap, 0);
1897 /* issue bus reset */
1898 if (ap->flags & ATA_FLAG_SRST)
1899 rc = ata_bus_softreset(ap, devmask);
1900 else if ((ap->flags & ATA_FLAG_SATA_RESET) == 0) {
1901 /* set up device control */
1902 if (ap->flags & ATA_FLAG_MMIO)
1903 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
1905 outb(ap->ctl, ioaddr->ctl_addr);
1906 rc = ata_bus_edd(ap);
1913 * determine by signature whether we have ATA or ATAPI devices
1915 err = ata_dev_try_classify(ap, 0);
1916 if ((slave_possible) && (err != 0x81))
1917 ata_dev_try_classify(ap, 1);
1919 /* re-enable interrupts */
1920 if (ap->ioaddr.ctl_addr) /* FIXME: hack. create a hook instead */
1923 /* is double-select really necessary? */
1924 if (ap->device[1].class != ATA_DEV_NONE)
1925 ap->ops->dev_select(ap, 1);
1926 if (ap->device[0].class != ATA_DEV_NONE)
1927 ap->ops->dev_select(ap, 0);
1929 /* if no devices were detected, disable this port */
1930 if ((ap->device[0].class == ATA_DEV_NONE) &&
1931 (ap->device[1].class == ATA_DEV_NONE))
1934 if (ap->flags & (ATA_FLAG_SATA_RESET | ATA_FLAG_SRST)) {
1935 /* set up device control for ATA_FLAG_SATA_RESET */
1936 if (ap->flags & ATA_FLAG_MMIO)
1937 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
1939 outb(ap->ctl, ioaddr->ctl_addr);
1946 printk(KERN_ERR "ata%u: disabling port\n", ap->id);
1947 ap->ops->port_disable(ap);
1952 static void ata_pr_blacklisted(struct ata_port *ap, struct ata_device *dev)
1954 printk(KERN_WARNING "ata%u: dev %u is on DMA blacklist, disabling DMA\n",
1955 ap->id, dev->devno);
1958 static const char * ata_dma_blacklist [] = {
1977 "Toshiba CD-ROM XM-6202B",
1978 "TOSHIBA CD-ROM XM-1702BC",
1980 "E-IDE CD-ROM CR-840",
1983 "SAMSUNG CD-ROM SC-148C",
1984 "SAMSUNG CD-ROM SC",
1986 "ATAPI CD-ROM DRIVE 40X MAXIMUM",
1990 static int ata_dma_blacklisted(struct ata_port *ap, struct ata_device *dev)
1992 unsigned char model_num[40];
1997 ata_dev_id_string(dev->id, model_num, ATA_ID_PROD_OFS,
2000 len = strnlen(s, sizeof(model_num));
2002 /* ATAPI specifies that empty space is blank-filled; remove blanks */
2003 while ((len > 0) && (s[len - 1] == ' ')) {
2008 for (i = 0; i < ARRAY_SIZE(ata_dma_blacklist); i++)
2009 if (!strncmp(ata_dma_blacklist[i], s, len))
2015 static unsigned int ata_get_mode_mask(struct ata_port *ap, int shift)
2017 struct ata_device *master, *slave;
2020 master = &ap->device[0];
2021 slave = &ap->device[1];
2023 assert (ata_dev_present(master) || ata_dev_present(slave));
2025 if (shift == ATA_SHIFT_UDMA) {
2026 mask = ap->udma_mask;
2027 if (ata_dev_present(master)) {
2028 mask &= (master->id[ATA_ID_UDMA_MODES] & 0xff);
2029 if (ata_dma_blacklisted(ap, master)) {
2031 ata_pr_blacklisted(ap, master);
2034 if (ata_dev_present(slave)) {
2035 mask &= (slave->id[ATA_ID_UDMA_MODES] & 0xff);
2036 if (ata_dma_blacklisted(ap, slave)) {
2038 ata_pr_blacklisted(ap, slave);
2042 else if (shift == ATA_SHIFT_MWDMA) {
2043 mask = ap->mwdma_mask;
2044 if (ata_dev_present(master)) {
2045 mask &= (master->id[ATA_ID_MWDMA_MODES] & 0x07);
2046 if (ata_dma_blacklisted(ap, master)) {
2048 ata_pr_blacklisted(ap, master);
2051 if (ata_dev_present(slave)) {
2052 mask &= (slave->id[ATA_ID_MWDMA_MODES] & 0x07);
2053 if (ata_dma_blacklisted(ap, slave)) {
2055 ata_pr_blacklisted(ap, slave);
2059 else if (shift == ATA_SHIFT_PIO) {
2060 mask = ap->pio_mask;
2061 if (ata_dev_present(master)) {
2062 /* spec doesn't return explicit support for
2063 * PIO0-2, so we fake it
2065 u16 tmp_mode = master->id[ATA_ID_PIO_MODES] & 0x03;
2070 if (ata_dev_present(slave)) {
2071 /* spec doesn't return explicit support for
2072 * PIO0-2, so we fake it
2074 u16 tmp_mode = slave->id[ATA_ID_PIO_MODES] & 0x03;
2081 mask = 0xffffffff; /* shut up compiler warning */
2088 /* find greatest bit */
2089 static int fgb(u32 bitmap)
2094 for (i = 0; i < 32; i++)
2095 if (bitmap & (1 << i))
2102 * ata_choose_xfer_mode - attempt to find best transfer mode
2103 * @ap: Port for which an xfer mode will be selected
2104 * @xfer_mode_out: (output) SET FEATURES - XFER MODE code
2105 * @xfer_shift_out: (output) bit shift that selects this mode
2107 * Based on host and device capabilities, determine the
2108 * maximum transfer mode that is amenable to all.
2111 * PCI/etc. bus probe sem.
2114 * Zero on success, negative on error.
2117 static int ata_choose_xfer_mode(struct ata_port *ap,
2119 unsigned int *xfer_shift_out)
2121 unsigned int mask, shift;
2124 for (i = 0; i < ARRAY_SIZE(xfer_mode_classes); i++) {
2125 shift = xfer_mode_classes[i].shift;
2126 mask = ata_get_mode_mask(ap, shift);
2130 *xfer_mode_out = xfer_mode_classes[i].base + x;
2131 *xfer_shift_out = shift;
2140 * ata_dev_set_xfermode - Issue SET FEATURES - XFER MODE command
2141 * @ap: Port associated with device @dev
2142 * @dev: Device to which command will be sent
2144 * Issue SET FEATURES - XFER MODE command to device @dev
2148 * PCI/etc. bus probe sem.
2151 static void ata_dev_set_xfermode(struct ata_port *ap, struct ata_device *dev)
2153 DECLARE_COMPLETION(wait);
2154 struct ata_queued_cmd *qc;
2156 unsigned long flags;
2158 /* set up set-features taskfile */
2159 DPRINTK("set features - xfer mode\n");
2161 qc = ata_qc_new_init(ap, dev);
2164 qc->tf.command = ATA_CMD_SET_FEATURES;
2165 qc->tf.feature = SETFEATURES_XFER;
2166 qc->tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
2167 qc->tf.protocol = ATA_PROT_NODATA;
2168 qc->tf.nsect = dev->xfer_mode;
2170 qc->waiting = &wait;
2171 qc->complete_fn = ata_qc_complete_noop;
2173 spin_lock_irqsave(&ap->host_set->lock, flags);
2174 rc = ata_qc_issue(qc);
2175 spin_unlock_irqrestore(&ap->host_set->lock, flags);
2178 ata_port_disable(ap);
2180 wait_for_completion(&wait);
2186 * ata_dev_init_params - Issue INIT DEV PARAMS command
2187 * @ap: Port associated with device @dev
2188 * @dev: Device to which command will be sent
2193 static void ata_dev_init_params(struct ata_port *ap, struct ata_device *dev)
2195 DECLARE_COMPLETION(wait);
2196 struct ata_queued_cmd *qc;
2198 unsigned long flags;
2199 u16 sectors = dev->id[6];
2200 u16 heads = dev->id[3];
2202 /* Number of sectors per track 1-255. Number of heads 1-16 */
2203 if (sectors < 1 || sectors > 255 || heads < 1 || heads > 16)
2206 /* set up init dev params taskfile */
2207 DPRINTK("init dev params \n");
2209 qc = ata_qc_new_init(ap, dev);
2212 qc->tf.command = ATA_CMD_INIT_DEV_PARAMS;
2213 qc->tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
2214 qc->tf.protocol = ATA_PROT_NODATA;
2215 qc->tf.nsect = sectors;
2216 qc->tf.device |= (heads - 1) & 0x0f; /* max head = num. of heads - 1 */
2218 qc->waiting = &wait;
2219 qc->complete_fn = ata_qc_complete_noop;
2221 spin_lock_irqsave(&ap->host_set->lock, flags);
2222 rc = ata_qc_issue(qc);
2223 spin_unlock_irqrestore(&ap->host_set->lock, flags);
2226 ata_port_disable(ap);
2228 wait_for_completion(&wait);
2234 * ata_sg_clean - Unmap DMA memory associated with command
2235 * @qc: Command containing DMA memory to be released
2237 * Unmap all mapped DMA memory associated with this command.
2240 * spin_lock_irqsave(host_set lock)
2243 static void ata_sg_clean(struct ata_queued_cmd *qc)
2245 struct ata_port *ap = qc->ap;
2246 struct scatterlist *sg = qc->sg;
2247 int dir = qc->dma_dir;
2249 assert(qc->flags & ATA_QCFLAG_DMAMAP);
2252 if (qc->flags & ATA_QCFLAG_SINGLE)
2253 assert(qc->n_elem == 1);
2255 DPRINTK("unmapping %u sg elements\n", qc->n_elem);
2257 if (qc->flags & ATA_QCFLAG_SG)
2258 dma_unmap_sg(ap->host_set->dev, sg, qc->n_elem, dir);
2260 dma_unmap_single(ap->host_set->dev, sg_dma_address(&sg[0]),
2261 sg_dma_len(&sg[0]), dir);
2263 qc->flags &= ~ATA_QCFLAG_DMAMAP;
2268 * ata_fill_sg - Fill PCI IDE PRD table
2269 * @qc: Metadata associated with taskfile to be transferred
2271 * Fill PCI IDE PRD (scatter-gather) table with segments
2272 * associated with the current disk command.
2275 * spin_lock_irqsave(host_set lock)
2278 static void ata_fill_sg(struct ata_queued_cmd *qc)
2280 struct scatterlist *sg = qc->sg;
2281 struct ata_port *ap = qc->ap;
2282 unsigned int idx, nelem;
2285 assert(qc->n_elem > 0);
2288 for (nelem = qc->n_elem; nelem; nelem--,sg++) {
2292 /* determine if physical DMA addr spans 64K boundary.
2293 * Note h/w doesn't support 64-bit, so we unconditionally
2294 * truncate dma_addr_t to u32.
2296 addr = (u32) sg_dma_address(sg);
2297 sg_len = sg_dma_len(sg);
2300 offset = addr & 0xffff;
2302 if ((offset + sg_len) > 0x10000)
2303 len = 0x10000 - offset;
2305 ap->prd[idx].addr = cpu_to_le32(addr);
2306 ap->prd[idx].flags_len = cpu_to_le32(len & 0xffff);
2307 VPRINTK("PRD[%u] = (0x%X, 0x%X)\n", idx, addr, len);
2316 ap->prd[idx - 1].flags_len |= cpu_to_le32(ATA_PRD_EOT);
2319 * ata_check_atapi_dma - Check whether ATAPI DMA can be supported
2320 * @qc: Metadata associated with taskfile to check
2322 * Allow low-level driver to filter ATA PACKET commands, returning
2323 * a status indicating whether or not it is OK to use DMA for the
2324 * supplied PACKET command.
2327 * spin_lock_irqsave(host_set lock)
2329 * RETURNS: 0 when ATAPI DMA can be used
2332 int ata_check_atapi_dma(struct ata_queued_cmd *qc)
2334 struct ata_port *ap = qc->ap;
2335 int rc = 0; /* Assume ATAPI DMA is OK by default */
2337 if (ap->ops->check_atapi_dma)
2338 rc = ap->ops->check_atapi_dma(qc);
2343 * ata_qc_prep - Prepare taskfile for submission
2344 * @qc: Metadata associated with taskfile to be prepared
2346 * Prepare ATA taskfile for submission.
2349 * spin_lock_irqsave(host_set lock)
2351 void ata_qc_prep(struct ata_queued_cmd *qc)
2353 if (!(qc->flags & ATA_QCFLAG_DMAMAP))
2360 * ata_sg_init_one - Associate command with memory buffer
2361 * @qc: Command to be associated
2362 * @buf: Memory buffer
2363 * @buflen: Length of memory buffer, in bytes.
2365 * Initialize the data-related elements of queued_cmd @qc
2366 * to point to a single memory buffer, @buf of byte length @buflen.
2369 * spin_lock_irqsave(host_set lock)
2372 void ata_sg_init_one(struct ata_queued_cmd *qc, void *buf, unsigned int buflen)
2374 struct scatterlist *sg;
2376 qc->flags |= ATA_QCFLAG_SINGLE;
2378 memset(&qc->sgent, 0, sizeof(qc->sgent));
2379 qc->sg = &qc->sgent;
2384 sg->page = virt_to_page(buf);
2385 sg->offset = (unsigned long) buf & ~PAGE_MASK;
2386 sg->length = buflen;
2390 * ata_sg_init - Associate command with scatter-gather table.
2391 * @qc: Command to be associated
2392 * @sg: Scatter-gather table.
2393 * @n_elem: Number of elements in s/g table.
2395 * Initialize the data-related elements of queued_cmd @qc
2396 * to point to a scatter-gather table @sg, containing @n_elem
2400 * spin_lock_irqsave(host_set lock)
2403 void ata_sg_init(struct ata_queued_cmd *qc, struct scatterlist *sg,
2404 unsigned int n_elem)
2406 qc->flags |= ATA_QCFLAG_SG;
2408 qc->n_elem = n_elem;
2412 * ata_sg_setup_one - DMA-map the memory buffer associated with a command.
2413 * @qc: Command with memory buffer to be mapped.
2415 * DMA-map the memory buffer associated with queued_cmd @qc.
2418 * spin_lock_irqsave(host_set lock)
2421 * Zero on success, negative on error.
2424 static int ata_sg_setup_one(struct ata_queued_cmd *qc)
2426 struct ata_port *ap = qc->ap;
2427 int dir = qc->dma_dir;
2428 struct scatterlist *sg = qc->sg;
2429 dma_addr_t dma_address;
2431 dma_address = dma_map_single(ap->host_set->dev, qc->buf_virt,
2433 if (dma_mapping_error(dma_address))
2436 sg_dma_address(sg) = dma_address;
2437 sg_dma_len(sg) = sg->length;
2439 DPRINTK("mapped buffer of %d bytes for %s\n", sg_dma_len(sg),
2440 qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
2446 * ata_sg_setup - DMA-map the scatter-gather table associated with a command.
2447 * @qc: Command with scatter-gather table to be mapped.
2449 * DMA-map the scatter-gather table associated with queued_cmd @qc.
2452 * spin_lock_irqsave(host_set lock)
2455 * Zero on success, negative on error.
2459 static int ata_sg_setup(struct ata_queued_cmd *qc)
2461 struct ata_port *ap = qc->ap;
2462 struct scatterlist *sg = qc->sg;
2465 VPRINTK("ENTER, ata%u\n", ap->id);
2466 assert(qc->flags & ATA_QCFLAG_SG);
2469 n_elem = dma_map_sg(ap->host_set->dev, sg, qc->n_elem, dir);
2473 DPRINTK("%d sg elements mapped\n", n_elem);
2475 qc->n_elem = n_elem;
2481 * ata_poll_qc_complete - turn irq back on and finish qc
2482 * @qc: Command to complete
2483 * @drv_stat: ATA status register content
2486 * None. (grabs host lock)
2489 void ata_poll_qc_complete(struct ata_queued_cmd *qc, u8 drv_stat)
2491 struct ata_port *ap = qc->ap;
2492 unsigned long flags;
2494 spin_lock_irqsave(&ap->host_set->lock, flags);
2495 ap->flags &= ~ATA_FLAG_NOINTR;
2497 ata_qc_complete(qc, drv_stat);
2498 spin_unlock_irqrestore(&ap->host_set->lock, flags);
2506 * None. (executing in kernel thread context)
2512 static unsigned long ata_pio_poll(struct ata_port *ap)
2515 unsigned int poll_state = HSM_ST_UNKNOWN;
2516 unsigned int reg_state = HSM_ST_UNKNOWN;
2517 const unsigned int tmout_state = HSM_ST_TMOUT;
2519 switch (ap->hsm_task_state) {
2522 poll_state = HSM_ST_POLL;
2526 case HSM_ST_LAST_POLL:
2527 poll_state = HSM_ST_LAST_POLL;
2528 reg_state = HSM_ST_LAST;
2535 status = ata_chk_status(ap);
2536 if (status & ATA_BUSY) {
2537 if (time_after(jiffies, ap->pio_task_timeout)) {
2538 ap->hsm_task_state = tmout_state;
2541 ap->hsm_task_state = poll_state;
2542 return ATA_SHORT_PAUSE;
2545 ap->hsm_task_state = reg_state;
2550 * ata_pio_complete -
2554 * None. (executing in kernel thread context)
2557 * Non-zero if qc completed, zero otherwise.
2560 static int ata_pio_complete (struct ata_port *ap)
2562 struct ata_queued_cmd *qc;
2566 * This is purely heuristic. This is a fast path. Sometimes when
2567 * we enter, BSY will be cleared in a chk-status or two. If not,
2568 * the drive is probably seeking or something. Snooze for a couple
2569 * msecs, then chk-status again. If still busy, fall back to
2570 * HSM_ST_POLL state.
2572 drv_stat = ata_busy_wait(ap, ATA_BUSY | ATA_DRQ, 10);
2573 if (drv_stat & (ATA_BUSY | ATA_DRQ)) {
2575 drv_stat = ata_busy_wait(ap, ATA_BUSY | ATA_DRQ, 10);
2576 if (drv_stat & (ATA_BUSY | ATA_DRQ)) {
2577 ap->hsm_task_state = HSM_ST_LAST_POLL;
2578 ap->pio_task_timeout = jiffies + ATA_TMOUT_PIO;
2583 drv_stat = ata_wait_idle(ap);
2584 if (!ata_ok(drv_stat)) {
2585 ap->hsm_task_state = HSM_ST_ERR;
2589 qc = ata_qc_from_tag(ap, ap->active_tag);
2592 ap->hsm_task_state = HSM_ST_IDLE;
2594 ata_poll_qc_complete(qc, drv_stat);
2596 /* another command may start at this point */
2604 * @buf: Buffer to swap
2605 * @buf_words: Number of 16-bit words in buffer.
2607 * Swap halves of 16-bit words if needed to convert from
2608 * little-endian byte order to native cpu byte order, or
2613 void swap_buf_le16(u16 *buf, unsigned int buf_words)
2618 for (i = 0; i < buf_words; i++)
2619 buf[i] = le16_to_cpu(buf[i]);
2620 #endif /* __BIG_ENDIAN */
2624 * ata_mmio_data_xfer - Transfer data by MMIO
2625 * @ap: port to read/write
2627 * @buflen: buffer length
2628 * @write_data: read/write
2630 * Transfer data from/to the device data register by MMIO.
2633 * Inherited from caller.
2637 static void ata_mmio_data_xfer(struct ata_port *ap, unsigned char *buf,
2638 unsigned int buflen, int write_data)
2641 unsigned int words = buflen >> 1;
2642 u16 *buf16 = (u16 *) buf;
2643 void __iomem *mmio = (void __iomem *)ap->ioaddr.data_addr;
2645 /* Transfer multiple of 2 bytes */
2647 for (i = 0; i < words; i++)
2648 writew(le16_to_cpu(buf16[i]), mmio);
2650 for (i = 0; i < words; i++)
2651 buf16[i] = cpu_to_le16(readw(mmio));
2654 /* Transfer trailing 1 byte, if any. */
2655 if (unlikely(buflen & 0x01)) {
2656 u16 align_buf[1] = { 0 };
2657 unsigned char *trailing_buf = buf + buflen - 1;
2660 memcpy(align_buf, trailing_buf, 1);
2661 writew(le16_to_cpu(align_buf[0]), mmio);
2663 align_buf[0] = cpu_to_le16(readw(mmio));
2664 memcpy(trailing_buf, align_buf, 1);
2670 * ata_pio_data_xfer - Transfer data by PIO
2671 * @ap: port to read/write
2673 * @buflen: buffer length
2674 * @write_data: read/write
2676 * Transfer data from/to the device data register by PIO.
2679 * Inherited from caller.
2683 static void ata_pio_data_xfer(struct ata_port *ap, unsigned char *buf,
2684 unsigned int buflen, int write_data)
2686 unsigned int words = buflen >> 1;
2688 /* Transfer multiple of 2 bytes */
2690 outsw(ap->ioaddr.data_addr, buf, words);
2692 insw(ap->ioaddr.data_addr, buf, words);
2694 /* Transfer trailing 1 byte, if any. */
2695 if (unlikely(buflen & 0x01)) {
2696 u16 align_buf[1] = { 0 };
2697 unsigned char *trailing_buf = buf + buflen - 1;
2700 memcpy(align_buf, trailing_buf, 1);
2701 outw(le16_to_cpu(align_buf[0]), ap->ioaddr.data_addr);
2703 align_buf[0] = cpu_to_le16(inw(ap->ioaddr.data_addr));
2704 memcpy(trailing_buf, align_buf, 1);
2710 * ata_data_xfer - Transfer data from/to the data register.
2711 * @ap: port to read/write
2713 * @buflen: buffer length
2714 * @do_write: read/write
2716 * Transfer data from/to the device data register.
2719 * Inherited from caller.
2723 static void ata_data_xfer(struct ata_port *ap, unsigned char *buf,
2724 unsigned int buflen, int do_write)
2726 if (ap->flags & ATA_FLAG_MMIO)
2727 ata_mmio_data_xfer(ap, buf, buflen, do_write);
2729 ata_pio_data_xfer(ap, buf, buflen, do_write);
2733 * ata_pio_sector - Transfer ATA_SECT_SIZE (512 bytes) of data.
2734 * @qc: Command on going
2736 * Transfer ATA_SECT_SIZE of data from/to the ATA device.
2739 * Inherited from caller.
2742 static void ata_pio_sector(struct ata_queued_cmd *qc)
2744 int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
2745 struct scatterlist *sg = qc->sg;
2746 struct ata_port *ap = qc->ap;
2748 unsigned int offset;
2751 if (qc->cursect == (qc->nsect - 1))
2752 ap->hsm_task_state = HSM_ST_LAST;
2754 page = sg[qc->cursg].page;
2755 offset = sg[qc->cursg].offset + qc->cursg_ofs * ATA_SECT_SIZE;
2757 /* get the current page and offset */
2758 page = nth_page(page, (offset >> PAGE_SHIFT));
2759 offset %= PAGE_SIZE;
2761 buf = kmap(page) + offset;
2766 if ((qc->cursg_ofs * ATA_SECT_SIZE) == (&sg[qc->cursg])->length) {
2771 DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
2773 /* do the actual data transfer */
2774 do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
2775 ata_data_xfer(ap, buf, ATA_SECT_SIZE, do_write);
2781 * __atapi_pio_bytes - Transfer data from/to the ATAPI device.
2782 * @qc: Command on going
2783 * @bytes: number of bytes
2785 * Transfer Transfer data from/to the ATAPI device.
2788 * Inherited from caller.
2792 static void __atapi_pio_bytes(struct ata_queued_cmd *qc, unsigned int bytes)
2794 int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
2795 struct scatterlist *sg = qc->sg;
2796 struct ata_port *ap = qc->ap;
2799 unsigned int offset, count;
2801 if (qc->curbytes + bytes >= qc->nbytes)
2802 ap->hsm_task_state = HSM_ST_LAST;
2805 if (unlikely(qc->cursg >= qc->n_elem)) {
2807 * The end of qc->sg is reached and the device expects
2808 * more data to transfer. In order not to overrun qc->sg
2809 * and fulfill length specified in the byte count register,
2810 * - for read case, discard trailing data from the device
2811 * - for write case, padding zero data to the device
2813 u16 pad_buf[1] = { 0 };
2814 unsigned int words = bytes >> 1;
2817 if (words) /* warning if bytes > 1 */
2818 printk(KERN_WARNING "ata%u: %u bytes trailing data\n",
2821 for (i = 0; i < words; i++)
2822 ata_data_xfer(ap, (unsigned char*)pad_buf, 2, do_write);
2824 ap->hsm_task_state = HSM_ST_LAST;
2828 sg = &qc->sg[qc->cursg];
2831 offset = sg->offset + qc->cursg_ofs;
2833 /* get the current page and offset */
2834 page = nth_page(page, (offset >> PAGE_SHIFT));
2835 offset %= PAGE_SIZE;
2837 /* don't overrun current sg */
2838 count = min(sg->length - qc->cursg_ofs, bytes);
2840 /* don't cross page boundaries */
2841 count = min(count, (unsigned int)PAGE_SIZE - offset);
2843 buf = kmap(page) + offset;
2846 qc->curbytes += count;
2847 qc->cursg_ofs += count;
2849 if (qc->cursg_ofs == sg->length) {
2854 DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
2856 /* do the actual data transfer */
2857 ata_data_xfer(ap, buf, count, do_write);
2866 * atapi_pio_bytes - Transfer data from/to the ATAPI device.
2867 * @qc: Command on going
2869 * Transfer Transfer data from/to the ATAPI device.
2872 * Inherited from caller.
2876 static void atapi_pio_bytes(struct ata_queued_cmd *qc)
2878 struct ata_port *ap = qc->ap;
2879 struct ata_device *dev = qc->dev;
2880 unsigned int ireason, bc_lo, bc_hi, bytes;
2881 int i_write, do_write = (qc->tf.flags & ATA_TFLAG_WRITE) ? 1 : 0;
2883 ap->ops->tf_read(ap, &qc->tf);
2884 ireason = qc->tf.nsect;
2885 bc_lo = qc->tf.lbam;
2886 bc_hi = qc->tf.lbah;
2887 bytes = (bc_hi << 8) | bc_lo;
2889 /* shall be cleared to zero, indicating xfer of data */
2890 if (ireason & (1 << 0))
2893 /* make sure transfer direction matches expected */
2894 i_write = ((ireason & (1 << 1)) == 0) ? 1 : 0;
2895 if (do_write != i_write)
2898 __atapi_pio_bytes(qc, bytes);
2903 printk(KERN_INFO "ata%u: dev %u: ATAPI check failed\n",
2904 ap->id, dev->devno);
2905 ap->hsm_task_state = HSM_ST_ERR;
2913 * None. (executing in kernel thread context)
2916 static void ata_pio_block(struct ata_port *ap)
2918 struct ata_queued_cmd *qc;
2922 * This is purely hueristic. This is a fast path.
2923 * Sometimes when we enter, BSY will be cleared in
2924 * a chk-status or two. If not, the drive is probably seeking
2925 * or something. Snooze for a couple msecs, then
2926 * chk-status again. If still busy, fall back to
2927 * HSM_ST_POLL state.
2929 status = ata_busy_wait(ap, ATA_BUSY, 5);
2930 if (status & ATA_BUSY) {
2932 status = ata_busy_wait(ap, ATA_BUSY, 10);
2933 if (status & ATA_BUSY) {
2934 ap->hsm_task_state = HSM_ST_POLL;
2935 ap->pio_task_timeout = jiffies + ATA_TMOUT_PIO;
2940 qc = ata_qc_from_tag(ap, ap->active_tag);
2943 if (is_atapi_taskfile(&qc->tf)) {
2944 /* no more data to transfer or unsupported ATAPI command */
2945 if ((status & ATA_DRQ) == 0) {
2946 ap->hsm_task_state = HSM_ST_LAST;
2950 atapi_pio_bytes(qc);
2952 /* handle BSY=0, DRQ=0 as error */
2953 if ((status & ATA_DRQ) == 0) {
2954 ap->hsm_task_state = HSM_ST_ERR;
2962 static void ata_pio_error(struct ata_port *ap)
2964 struct ata_queued_cmd *qc;
2967 qc = ata_qc_from_tag(ap, ap->active_tag);
2970 drv_stat = ata_chk_status(ap);
2971 printk(KERN_WARNING "ata%u: PIO error, drv_stat 0x%x\n",
2974 ap->hsm_task_state = HSM_ST_IDLE;
2976 ata_poll_qc_complete(qc, drv_stat | ATA_ERR);
2979 static void ata_pio_task(void *_data)
2981 struct ata_port *ap = _data;
2982 unsigned long timeout;
2989 switch (ap->hsm_task_state) {
2998 qc_completed = ata_pio_complete(ap);
3002 case HSM_ST_LAST_POLL:
3003 timeout = ata_pio_poll(ap);
3013 queue_delayed_work(ata_wq, &ap->pio_task, timeout);
3014 else if (!qc_completed)
3018 static void atapi_request_sense(struct ata_port *ap, struct ata_device *dev,
3019 struct scsi_cmnd *cmd)
3021 DECLARE_COMPLETION(wait);
3022 struct ata_queued_cmd *qc;
3023 unsigned long flags;
3026 DPRINTK("ATAPI request sense\n");
3028 qc = ata_qc_new_init(ap, dev);
3031 /* FIXME: is this needed? */
3032 memset(cmd->sense_buffer, 0, sizeof(cmd->sense_buffer));
3034 ata_sg_init_one(qc, cmd->sense_buffer, sizeof(cmd->sense_buffer));
3035 qc->dma_dir = DMA_FROM_DEVICE;
3037 memset(&qc->cdb, 0, ap->cdb_len);
3038 qc->cdb[0] = REQUEST_SENSE;
3039 qc->cdb[4] = SCSI_SENSE_BUFFERSIZE;
3041 qc->tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
3042 qc->tf.command = ATA_CMD_PACKET;
3044 qc->tf.protocol = ATA_PROT_ATAPI;
3045 qc->tf.lbam = (8 * 1024) & 0xff;
3046 qc->tf.lbah = (8 * 1024) >> 8;
3047 qc->nbytes = SCSI_SENSE_BUFFERSIZE;
3049 qc->waiting = &wait;
3050 qc->complete_fn = ata_qc_complete_noop;
3052 spin_lock_irqsave(&ap->host_set->lock, flags);
3053 rc = ata_qc_issue(qc);
3054 spin_unlock_irqrestore(&ap->host_set->lock, flags);
3057 ata_port_disable(ap);
3059 wait_for_completion(&wait);
3065 * ata_qc_timeout - Handle timeout of queued command
3066 * @qc: Command that timed out
3068 * Some part of the kernel (currently, only the SCSI layer)
3069 * has noticed that the active command on port @ap has not
3070 * completed after a specified length of time. Handle this
3071 * condition by disabling DMA (if necessary) and completing
3072 * transactions, with error if necessary.
3074 * This also handles the case of the "lost interrupt", where
3075 * for some reason (possibly hardware bug, possibly driver bug)
3076 * an interrupt was not delivered to the driver, even though the
3077 * transaction completed successfully.
3080 * Inherited from SCSI layer (none, can sleep)
3083 static void ata_qc_timeout(struct ata_queued_cmd *qc)
3085 struct ata_port *ap = qc->ap;
3086 struct ata_host_set *host_set = ap->host_set;
3087 struct ata_device *dev = qc->dev;
3088 u8 host_stat = 0, drv_stat;
3089 unsigned long flags;
3093 /* FIXME: doesn't this conflict with timeout handling? */
3094 if (qc->dev->class == ATA_DEV_ATAPI && qc->scsicmd) {
3095 struct scsi_cmnd *cmd = qc->scsicmd;
3097 if (!(cmd->eh_eflags & SCSI_EH_CANCEL_CMD)) {
3099 /* finish completing original command */
3100 spin_lock_irqsave(&host_set->lock, flags);
3101 __ata_qc_complete(qc);
3102 spin_unlock_irqrestore(&host_set->lock, flags);
3104 atapi_request_sense(ap, dev, cmd);
3106 cmd->result = (CHECK_CONDITION << 1) | (DID_OK << 16);
3107 scsi_finish_command(cmd);
3113 spin_lock_irqsave(&host_set->lock, flags);
3115 /* hack alert! We cannot use the supplied completion
3116 * function from inside the ->eh_strategy_handler() thread.
3117 * libata is the only user of ->eh_strategy_handler() in
3118 * any kernel, so the default scsi_done() assumes it is
3119 * not being called from the SCSI EH.
3121 qc->scsidone = scsi_finish_command;
3123 switch (qc->tf.protocol) {
3126 case ATA_PROT_ATAPI_DMA:
3127 host_stat = ap->ops->bmdma_status(ap);
3129 /* before we do anything else, clear DMA-Start bit */
3130 ap->ops->bmdma_stop(qc);
3136 drv_stat = ata_chk_status(ap);
3138 /* ack bmdma irq events */
3139 ap->ops->irq_clear(ap);
3141 printk(KERN_ERR "ata%u: command 0x%x timeout, stat 0x%x host_stat 0x%x\n",
3142 ap->id, qc->tf.command, drv_stat, host_stat);
3144 /* complete taskfile transaction */
3145 ata_qc_complete(qc, drv_stat);
3149 spin_unlock_irqrestore(&host_set->lock, flags);
3156 * ata_eng_timeout - Handle timeout of queued command
3157 * @ap: Port on which timed-out command is active
3159 * Some part of the kernel (currently, only the SCSI layer)
3160 * has noticed that the active command on port @ap has not
3161 * completed after a specified length of time. Handle this
3162 * condition by disabling DMA (if necessary) and completing
3163 * transactions, with error if necessary.
3165 * This also handles the case of the "lost interrupt", where
3166 * for some reason (possibly hardware bug, possibly driver bug)
3167 * an interrupt was not delivered to the driver, even though the
3168 * transaction completed successfully.
3171 * Inherited from SCSI layer (none, can sleep)
3174 void ata_eng_timeout(struct ata_port *ap)
3176 struct ata_queued_cmd *qc;
3180 qc = ata_qc_from_tag(ap, ap->active_tag);
3182 printk(KERN_ERR "ata%u: BUG: timeout without command\n",
3194 * ata_qc_new - Request an available ATA command, for queueing
3195 * @ap: Port associated with device @dev
3196 * @dev: Device from whom we request an available command structure
3202 static struct ata_queued_cmd *ata_qc_new(struct ata_port *ap)
3204 struct ata_queued_cmd *qc = NULL;
3207 for (i = 0; i < ATA_MAX_QUEUE; i++)
3208 if (!test_and_set_bit(i, &ap->qactive)) {
3209 qc = ata_qc_from_tag(ap, i);
3220 * ata_qc_new_init - Request an available ATA command, and initialize it
3221 * @ap: Port associated with device @dev
3222 * @dev: Device from whom we request an available command structure
3228 struct ata_queued_cmd *ata_qc_new_init(struct ata_port *ap,
3229 struct ata_device *dev)
3231 struct ata_queued_cmd *qc;
3233 qc = ata_qc_new(ap);
3240 qc->cursect = qc->cursg = qc->cursg_ofs = 0;
3242 qc->nbytes = qc->curbytes = 0;
3244 ata_tf_init(ap, &qc->tf, dev->devno);
3246 if (dev->flags & ATA_DFLAG_LBA) {
3247 qc->tf.flags |= ATA_TFLAG_LBA;
3249 if (dev->flags & ATA_DFLAG_LBA48)
3250 qc->tf.flags |= ATA_TFLAG_LBA48;
3257 static int ata_qc_complete_noop(struct ata_queued_cmd *qc, u8 drv_stat)
3262 static void __ata_qc_complete(struct ata_queued_cmd *qc)
3264 struct ata_port *ap = qc->ap;
3265 unsigned int tag, do_clear = 0;
3269 if (likely(ata_tag_valid(tag))) {
3270 if (tag == ap->active_tag)
3271 ap->active_tag = ATA_TAG_POISON;
3272 qc->tag = ATA_TAG_POISON;
3277 struct completion *waiting = qc->waiting;
3282 if (likely(do_clear))
3283 clear_bit(tag, &ap->qactive);
3287 * ata_qc_free - free unused ata_queued_cmd
3288 * @qc: Command to complete
3290 * Designed to free unused ata_queued_cmd object
3291 * in case something prevents using it.
3294 * spin_lock_irqsave(host_set lock)
3297 void ata_qc_free(struct ata_queued_cmd *qc)
3299 assert(qc != NULL); /* ata_qc_from_tag _might_ return NULL */
3300 assert(qc->waiting == NULL); /* nothing should be waiting */
3302 __ata_qc_complete(qc);
3306 * ata_qc_complete - Complete an active ATA command
3307 * @qc: Command to complete
3308 * @drv_stat: ATA Status register contents
3310 * Indicate to the mid and upper layers that an ATA
3311 * command has completed, with either an ok or not-ok status.
3314 * spin_lock_irqsave(host_set lock)
3318 void ata_qc_complete(struct ata_queued_cmd *qc, u8 drv_stat)
3322 assert(qc != NULL); /* ata_qc_from_tag _might_ return NULL */
3323 assert(qc->flags & ATA_QCFLAG_ACTIVE);
3325 if (likely(qc->flags & ATA_QCFLAG_DMAMAP))
3328 /* atapi: mark qc as inactive to prevent the interrupt handler
3329 * from completing the command twice later, before the error handler
3330 * is called. (when rc != 0 and atapi request sense is needed)
3332 qc->flags &= ~ATA_QCFLAG_ACTIVE;
3334 /* call completion callback */
3335 rc = qc->complete_fn(qc, drv_stat);
3337 /* if callback indicates not to complete command (non-zero),
3338 * return immediately
3343 __ata_qc_complete(qc);
3348 static inline int ata_should_dma_map(struct ata_queued_cmd *qc)
3350 struct ata_port *ap = qc->ap;
3352 switch (qc->tf.protocol) {
3354 case ATA_PROT_ATAPI_DMA:
3357 case ATA_PROT_ATAPI:
3359 case ATA_PROT_PIO_MULT:
3360 if (ap->flags & ATA_FLAG_PIO_DMA)
3373 * ata_qc_issue - issue taskfile to device
3374 * @qc: command to issue to device
3376 * Prepare an ATA command to submission to device.
3377 * This includes mapping the data into a DMA-able
3378 * area, filling in the S/G table, and finally
3379 * writing the taskfile to hardware, starting the command.
3382 * spin_lock_irqsave(host_set lock)
3385 * Zero on success, negative on error.
3388 int ata_qc_issue(struct ata_queued_cmd *qc)
3390 struct ata_port *ap = qc->ap;
3392 if (ata_should_dma_map(qc)) {
3393 if (qc->flags & ATA_QCFLAG_SG) {
3394 if (ata_sg_setup(qc))
3396 } else if (qc->flags & ATA_QCFLAG_SINGLE) {
3397 if (ata_sg_setup_one(qc))
3401 qc->flags &= ~ATA_QCFLAG_DMAMAP;
3404 ap->ops->qc_prep(qc);
3406 qc->ap->active_tag = qc->tag;
3407 qc->flags |= ATA_QCFLAG_ACTIVE;
3409 return ap->ops->qc_issue(qc);
3417 * ata_qc_issue_prot - issue taskfile to device in proto-dependent manner
3418 * @qc: command to issue to device
3420 * Using various libata functions and hooks, this function
3421 * starts an ATA command. ATA commands are grouped into
3422 * classes called "protocols", and issuing each type of protocol
3423 * is slightly different.
3425 * May be used as the qc_issue() entry in ata_port_operations.
3428 * spin_lock_irqsave(host_set lock)
3431 * Zero on success, negative on error.
3434 int ata_qc_issue_prot(struct ata_queued_cmd *qc)
3436 struct ata_port *ap = qc->ap;
3438 ata_dev_select(ap, qc->dev->devno, 1, 0);
3440 switch (qc->tf.protocol) {
3441 case ATA_PROT_NODATA:
3442 ata_tf_to_host_nolock(ap, &qc->tf);
3446 ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
3447 ap->ops->bmdma_setup(qc); /* set up bmdma */
3448 ap->ops->bmdma_start(qc); /* initiate bmdma */
3451 case ATA_PROT_PIO: /* load tf registers, initiate polling pio */
3452 ata_qc_set_polling(qc);
3453 ata_tf_to_host_nolock(ap, &qc->tf);
3454 ap->hsm_task_state = HSM_ST;
3455 queue_work(ata_wq, &ap->pio_task);
3458 case ATA_PROT_ATAPI:
3459 ata_qc_set_polling(qc);
3460 ata_tf_to_host_nolock(ap, &qc->tf);
3461 queue_work(ata_wq, &ap->packet_task);
3464 case ATA_PROT_ATAPI_NODATA:
3465 ap->flags |= ATA_FLAG_NOINTR;
3466 ata_tf_to_host_nolock(ap, &qc->tf);
3467 queue_work(ata_wq, &ap->packet_task);
3470 case ATA_PROT_ATAPI_DMA:
3471 ap->flags |= ATA_FLAG_NOINTR;
3472 ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
3473 ap->ops->bmdma_setup(qc); /* set up bmdma */
3474 queue_work(ata_wq, &ap->packet_task);
3486 * ata_bmdma_setup_mmio - Set up PCI IDE BMDMA transaction
3487 * @qc: Info associated with this ATA transaction.
3490 * spin_lock_irqsave(host_set lock)
3493 static void ata_bmdma_setup_mmio (struct ata_queued_cmd *qc)
3495 struct ata_port *ap = qc->ap;
3496 unsigned int rw = (qc->tf.flags & ATA_TFLAG_WRITE);
3498 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
3500 /* load PRD table addr. */
3501 mb(); /* make sure PRD table writes are visible to controller */
3502 writel(ap->prd_dma, mmio + ATA_DMA_TABLE_OFS);
3504 /* specify data direction, triple-check start bit is clear */
3505 dmactl = readb(mmio + ATA_DMA_CMD);
3506 dmactl &= ~(ATA_DMA_WR | ATA_DMA_START);
3508 dmactl |= ATA_DMA_WR;
3509 writeb(dmactl, mmio + ATA_DMA_CMD);
3511 /* issue r/w command */
3512 ap->ops->exec_command(ap, &qc->tf);
3516 * ata_bmdma_start_mmio - Start a PCI IDE BMDMA transaction
3517 * @qc: Info associated with this ATA transaction.
3520 * spin_lock_irqsave(host_set lock)
3523 static void ata_bmdma_start_mmio (struct ata_queued_cmd *qc)
3525 struct ata_port *ap = qc->ap;
3526 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
3529 /* start host DMA transaction */
3530 dmactl = readb(mmio + ATA_DMA_CMD);
3531 writeb(dmactl | ATA_DMA_START, mmio + ATA_DMA_CMD);
3533 /* Strictly, one may wish to issue a readb() here, to
3534 * flush the mmio write. However, control also passes
3535 * to the hardware at this point, and it will interrupt
3536 * us when we are to resume control. So, in effect,
3537 * we don't care when the mmio write flushes.
3538 * Further, a read of the DMA status register _immediately_
3539 * following the write may not be what certain flaky hardware
3540 * is expected, so I think it is best to not add a readb()
3541 * without first all the MMIO ATA cards/mobos.
3542 * Or maybe I'm just being paranoid.
3547 * ata_bmdma_setup_pio - Set up PCI IDE BMDMA transaction (PIO)
3548 * @qc: Info associated with this ATA transaction.
3551 * spin_lock_irqsave(host_set lock)
3554 static void ata_bmdma_setup_pio (struct ata_queued_cmd *qc)
3556 struct ata_port *ap = qc->ap;
3557 unsigned int rw = (qc->tf.flags & ATA_TFLAG_WRITE);
3560 /* load PRD table addr. */
3561 outl(ap->prd_dma, ap->ioaddr.bmdma_addr + ATA_DMA_TABLE_OFS);
3563 /* specify data direction, triple-check start bit is clear */
3564 dmactl = inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3565 dmactl &= ~(ATA_DMA_WR | ATA_DMA_START);
3567 dmactl |= ATA_DMA_WR;
3568 outb(dmactl, ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3570 /* issue r/w command */
3571 ap->ops->exec_command(ap, &qc->tf);
3575 * ata_bmdma_start_pio - Start a PCI IDE BMDMA transaction (PIO)
3576 * @qc: Info associated with this ATA transaction.
3579 * spin_lock_irqsave(host_set lock)
3582 static void ata_bmdma_start_pio (struct ata_queued_cmd *qc)
3584 struct ata_port *ap = qc->ap;
3587 /* start host DMA transaction */
3588 dmactl = inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3589 outb(dmactl | ATA_DMA_START,
3590 ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3595 * ata_bmdma_start - Start a PCI IDE BMDMA transaction
3596 * @qc: Info associated with this ATA transaction.
3598 * Writes the ATA_DMA_START flag to the DMA command register.
3600 * May be used as the bmdma_start() entry in ata_port_operations.
3603 * spin_lock_irqsave(host_set lock)
3605 void ata_bmdma_start(struct ata_queued_cmd *qc)
3607 if (qc->ap->flags & ATA_FLAG_MMIO)
3608 ata_bmdma_start_mmio(qc);
3610 ata_bmdma_start_pio(qc);
3615 * ata_bmdma_setup - Set up PCI IDE BMDMA transaction
3616 * @qc: Info associated with this ATA transaction.
3618 * Writes address of PRD table to device's PRD Table Address
3619 * register, sets the DMA control register, and calls
3620 * ops->exec_command() to start the transfer.
3622 * May be used as the bmdma_setup() entry in ata_port_operations.
3625 * spin_lock_irqsave(host_set lock)
3627 void ata_bmdma_setup(struct ata_queued_cmd *qc)
3629 if (qc->ap->flags & ATA_FLAG_MMIO)
3630 ata_bmdma_setup_mmio(qc);
3632 ata_bmdma_setup_pio(qc);
3637 * ata_bmdma_irq_clear - Clear PCI IDE BMDMA interrupt.
3638 * @ap: Port associated with this ATA transaction.
3640 * Clear interrupt and error flags in DMA status register.
3642 * May be used as the irq_clear() entry in ata_port_operations.
3645 * spin_lock_irqsave(host_set lock)
3648 void ata_bmdma_irq_clear(struct ata_port *ap)
3650 if (ap->flags & ATA_FLAG_MMIO) {
3651 void __iomem *mmio = ((void __iomem *) ap->ioaddr.bmdma_addr) + ATA_DMA_STATUS;
3652 writeb(readb(mmio), mmio);
3654 unsigned long addr = ap->ioaddr.bmdma_addr + ATA_DMA_STATUS;
3655 outb(inb(addr), addr);
3662 * ata_bmdma_status - Read PCI IDE BMDMA status
3663 * @ap: Port associated with this ATA transaction.
3665 * Read and return BMDMA status register.
3667 * May be used as the bmdma_status() entry in ata_port_operations.
3670 * spin_lock_irqsave(host_set lock)
3673 u8 ata_bmdma_status(struct ata_port *ap)
3676 if (ap->flags & ATA_FLAG_MMIO) {
3677 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
3678 host_stat = readb(mmio + ATA_DMA_STATUS);
3680 host_stat = inb(ap->ioaddr.bmdma_addr + ATA_DMA_STATUS);
3686 * ata_bmdma_stop - Stop PCI IDE BMDMA transfer
3687 * @qc: Command we are ending DMA for
3689 * Clears the ATA_DMA_START flag in the dma control register
3691 * May be used as the bmdma_stop() entry in ata_port_operations.
3694 * spin_lock_irqsave(host_set lock)
3697 void ata_bmdma_stop(struct ata_queued_cmd *qc)
3699 struct ata_port *ap = qc->ap;
3700 if (ap->flags & ATA_FLAG_MMIO) {
3701 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
3703 /* clear start/stop bit */
3704 writeb(readb(mmio + ATA_DMA_CMD) & ~ATA_DMA_START,
3705 mmio + ATA_DMA_CMD);
3707 /* clear start/stop bit */
3708 outb(inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD) & ~ATA_DMA_START,
3709 ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3712 /* one-PIO-cycle guaranteed wait, per spec, for HDMA1:0 transition */
3713 ata_altstatus(ap); /* dummy read */
3717 * ata_host_intr - Handle host interrupt for given (port, task)
3718 * @ap: Port on which interrupt arrived (possibly...)
3719 * @qc: Taskfile currently active in engine
3721 * Handle host interrupt for given queued command. Currently,
3722 * only DMA interrupts are handled. All other commands are
3723 * handled via polling with interrupts disabled (nIEN bit).
3726 * spin_lock_irqsave(host_set lock)
3729 * One if interrupt was handled, zero if not (shared irq).
3732 inline unsigned int ata_host_intr (struct ata_port *ap,
3733 struct ata_queued_cmd *qc)
3735 u8 status, host_stat;
3737 switch (qc->tf.protocol) {
3740 case ATA_PROT_ATAPI_DMA:
3741 case ATA_PROT_ATAPI:
3742 /* check status of DMA engine */
3743 host_stat = ap->ops->bmdma_status(ap);
3744 VPRINTK("ata%u: host_stat 0x%X\n", ap->id, host_stat);
3746 /* if it's not our irq... */
3747 if (!(host_stat & ATA_DMA_INTR))
3750 /* before we do anything else, clear DMA-Start bit */
3751 ap->ops->bmdma_stop(qc);
3755 case ATA_PROT_ATAPI_NODATA:
3756 case ATA_PROT_NODATA:
3757 /* check altstatus */
3758 status = ata_altstatus(ap);
3759 if (status & ATA_BUSY)
3762 /* check main status, clearing INTRQ */
3763 status = ata_chk_status(ap);
3764 if (unlikely(status & ATA_BUSY))
3766 DPRINTK("ata%u: protocol %d (dev_stat 0x%X)\n",
3767 ap->id, qc->tf.protocol, status);
3769 /* ack bmdma irq events */
3770 ap->ops->irq_clear(ap);
3772 /* complete taskfile transaction */
3773 ata_qc_complete(qc, status);
3780 return 1; /* irq handled */
3783 ap->stats.idle_irq++;
3786 if ((ap->stats.idle_irq % 1000) == 0) {
3788 ata_irq_ack(ap, 0); /* debug trap */
3789 printk(KERN_WARNING "ata%d: irq trap\n", ap->id);
3792 return 0; /* irq not handled */
3796 * ata_interrupt - Default ATA host interrupt handler
3797 * @irq: irq line (unused)
3798 * @dev_instance: pointer to our ata_host_set information structure
3801 * Default interrupt handler for PCI IDE devices. Calls
3802 * ata_host_intr() for each port that is not disabled.
3805 * Obtains host_set lock during operation.
3808 * IRQ_NONE or IRQ_HANDLED.
3812 irqreturn_t ata_interrupt (int irq, void *dev_instance, struct pt_regs *regs)
3814 struct ata_host_set *host_set = dev_instance;
3816 unsigned int handled = 0;
3817 unsigned long flags;
3819 /* TODO: make _irqsave conditional on x86 PCI IDE legacy mode */
3820 spin_lock_irqsave(&host_set->lock, flags);
3822 for (i = 0; i < host_set->n_ports; i++) {
3823 struct ata_port *ap;
3825 ap = host_set->ports[i];
3827 !(ap->flags & (ATA_FLAG_PORT_DISABLED | ATA_FLAG_NOINTR))) {
3828 struct ata_queued_cmd *qc;
3830 qc = ata_qc_from_tag(ap, ap->active_tag);
3831 if (qc && (!(qc->tf.ctl & ATA_NIEN)) &&
3832 (qc->flags & ATA_QCFLAG_ACTIVE))
3833 handled |= ata_host_intr(ap, qc);
3837 spin_unlock_irqrestore(&host_set->lock, flags);
3839 return IRQ_RETVAL(handled);
3843 * atapi_packet_task - Write CDB bytes to hardware
3844 * @_data: Port to which ATAPI device is attached.
3846 * When device has indicated its readiness to accept
3847 * a CDB, this function is called. Send the CDB.
3848 * If DMA is to be performed, exit immediately.
3849 * Otherwise, we are in polling mode, so poll
3850 * status under operation succeeds or fails.
3853 * Kernel thread context (may sleep)
3856 static void atapi_packet_task(void *_data)
3858 struct ata_port *ap = _data;
3859 struct ata_queued_cmd *qc;
3862 qc = ata_qc_from_tag(ap, ap->active_tag);
3864 assert(qc->flags & ATA_QCFLAG_ACTIVE);
3866 /* sleep-wait for BSY to clear */
3867 DPRINTK("busy wait\n");
3868 if (ata_busy_sleep(ap, ATA_TMOUT_CDB_QUICK, ATA_TMOUT_CDB))
3871 /* make sure DRQ is set */
3872 status = ata_chk_status(ap);
3873 if ((status & (ATA_BUSY | ATA_DRQ)) != ATA_DRQ)
3877 DPRINTK("send cdb\n");
3878 assert(ap->cdb_len >= 12);
3880 if (qc->tf.protocol == ATA_PROT_ATAPI_DMA ||
3881 qc->tf.protocol == ATA_PROT_ATAPI_NODATA) {
3882 unsigned long flags;
3884 /* Once we're done issuing command and kicking bmdma,
3885 * irq handler takes over. To not lose irq, we need
3886 * to clear NOINTR flag before sending cdb, but
3887 * interrupt handler shouldn't be invoked before we're
3888 * finished. Hence, the following locking.
3890 spin_lock_irqsave(&ap->host_set->lock, flags);
3891 ap->flags &= ~ATA_FLAG_NOINTR;
3892 ata_data_xfer(ap, qc->cdb, ap->cdb_len, 1);
3893 if (qc->tf.protocol == ATA_PROT_ATAPI_DMA)
3894 ap->ops->bmdma_start(qc); /* initiate bmdma */
3895 spin_unlock_irqrestore(&ap->host_set->lock, flags);
3897 ata_data_xfer(ap, qc->cdb, ap->cdb_len, 1);
3899 /* PIO commands are handled by polling */
3900 ap->hsm_task_state = HSM_ST;
3901 queue_work(ata_wq, &ap->pio_task);
3907 ata_poll_qc_complete(qc, ATA_ERR);
3912 * ata_port_start - Set port up for dma.
3913 * @ap: Port to initialize
3915 * Called just after data structures for each port are
3916 * initialized. Allocates space for PRD table.
3918 * May be used as the port_start() entry in ata_port_operations.
3923 int ata_port_start (struct ata_port *ap)
3925 struct device *dev = ap->host_set->dev;
3927 ap->prd = dma_alloc_coherent(dev, ATA_PRD_TBL_SZ, &ap->prd_dma, GFP_KERNEL);
3931 DPRINTK("prd alloc, virt %p, dma %llx\n", ap->prd, (unsigned long long) ap->prd_dma);
3938 * ata_port_stop - Undo ata_port_start()
3939 * @ap: Port to shut down
3941 * Frees the PRD table.
3943 * May be used as the port_stop() entry in ata_port_operations.
3948 void ata_port_stop (struct ata_port *ap)
3950 struct device *dev = ap->host_set->dev;
3952 dma_free_coherent(dev, ATA_PRD_TBL_SZ, ap->prd, ap->prd_dma);
3955 void ata_host_stop (struct ata_host_set *host_set)
3957 if (host_set->mmio_base)
3958 iounmap(host_set->mmio_base);
3963 * ata_host_remove - Unregister SCSI host structure with upper layers
3964 * @ap: Port to unregister
3965 * @do_unregister: 1 if we fully unregister, 0 to just stop the port
3970 static void ata_host_remove(struct ata_port *ap, unsigned int do_unregister)
3972 struct Scsi_Host *sh = ap->host;
3977 scsi_remove_host(sh);
3979 ap->ops->port_stop(ap);
3983 * ata_host_init - Initialize an ata_port structure
3984 * @ap: Structure to initialize
3985 * @host: associated SCSI mid-layer structure
3986 * @host_set: Collection of hosts to which @ap belongs
3987 * @ent: Probe information provided by low-level driver
3988 * @port_no: Port number associated with this ata_port
3990 * Initialize a new ata_port structure, and its associated
3994 * Inherited from caller.
3998 static void ata_host_init(struct ata_port *ap, struct Scsi_Host *host,
3999 struct ata_host_set *host_set,
4000 struct ata_probe_ent *ent, unsigned int port_no)
4006 host->max_channel = 1;
4007 host->unique_id = ata_unique_id++;
4008 host->max_cmd_len = 12;
4010 scsi_assign_lock(host, &host_set->lock);
4012 ap->flags = ATA_FLAG_PORT_DISABLED;
4013 ap->id = host->unique_id;
4015 ap->ctl = ATA_DEVCTL_OBS;
4016 ap->host_set = host_set;
4017 ap->port_no = port_no;
4019 ent->legacy_mode ? ent->hard_port_no : port_no;
4020 ap->pio_mask = ent->pio_mask;
4021 ap->mwdma_mask = ent->mwdma_mask;
4022 ap->udma_mask = ent->udma_mask;
4023 ap->flags |= ent->host_flags;
4024 ap->ops = ent->port_ops;
4025 ap->cbl = ATA_CBL_NONE;
4026 ap->active_tag = ATA_TAG_POISON;
4027 ap->last_ctl = 0xFF;
4029 INIT_WORK(&ap->packet_task, atapi_packet_task, ap);
4030 INIT_WORK(&ap->pio_task, ata_pio_task, ap);
4032 for (i = 0; i < ATA_MAX_DEVICES; i++)
4033 ap->device[i].devno = i;
4036 ap->stats.unhandled_irq = 1;
4037 ap->stats.idle_irq = 1;
4040 memcpy(&ap->ioaddr, &ent->port[port_no], sizeof(struct ata_ioports));
4044 * ata_host_add - Attach low-level ATA driver to system
4045 * @ent: Information provided by low-level driver
4046 * @host_set: Collections of ports to which we add
4047 * @port_no: Port number associated with this host
4049 * Attach low-level ATA driver to system.
4052 * PCI/etc. bus probe sem.
4055 * New ata_port on success, for NULL on error.
4059 static struct ata_port * ata_host_add(struct ata_probe_ent *ent,
4060 struct ata_host_set *host_set,
4061 unsigned int port_no)
4063 struct Scsi_Host *host;
4064 struct ata_port *ap;
4068 host = scsi_host_alloc(ent->sht, sizeof(struct ata_port));
4072 ap = (struct ata_port *) &host->hostdata[0];
4074 ata_host_init(ap, host, host_set, ent, port_no);
4076 rc = ap->ops->port_start(ap);
4083 scsi_host_put(host);
4088 * ata_device_add - Register hardware device with ATA and SCSI layers
4089 * @ent: Probe information describing hardware device to be registered
4091 * This function processes the information provided in the probe
4092 * information struct @ent, allocates the necessary ATA and SCSI
4093 * host information structures, initializes them, and registers
4094 * everything with requisite kernel subsystems.
4096 * This function requests irqs, probes the ATA bus, and probes
4100 * PCI/etc. bus probe sem.
4103 * Number of ports registered. Zero on error (no ports registered).
4107 int ata_device_add(struct ata_probe_ent *ent)
4109 unsigned int count = 0, i;
4110 struct device *dev = ent->dev;
4111 struct ata_host_set *host_set;
4114 /* alloc a container for our list of ATA ports (buses) */
4115 host_set = kmalloc(sizeof(struct ata_host_set) +
4116 (ent->n_ports * sizeof(void *)), GFP_KERNEL);
4119 memset(host_set, 0, sizeof(struct ata_host_set) + (ent->n_ports * sizeof(void *)));
4120 spin_lock_init(&host_set->lock);
4122 host_set->dev = dev;
4123 host_set->n_ports = ent->n_ports;
4124 host_set->irq = ent->irq;
4125 host_set->mmio_base = ent->mmio_base;
4126 host_set->private_data = ent->private_data;
4127 host_set->ops = ent->port_ops;
4129 /* register each port bound to this device */
4130 for (i = 0; i < ent->n_ports; i++) {
4131 struct ata_port *ap;
4132 unsigned long xfer_mode_mask;
4134 ap = ata_host_add(ent, host_set, i);
4138 host_set->ports[i] = ap;
4139 xfer_mode_mask =(ap->udma_mask << ATA_SHIFT_UDMA) |
4140 (ap->mwdma_mask << ATA_SHIFT_MWDMA) |
4141 (ap->pio_mask << ATA_SHIFT_PIO);
4143 /* print per-port info to dmesg */
4144 printk(KERN_INFO "ata%u: %cATA max %s cmd 0x%lX ctl 0x%lX "
4145 "bmdma 0x%lX irq %lu\n",
4147 ap->flags & ATA_FLAG_SATA ? 'S' : 'P',
4148 ata_mode_string(xfer_mode_mask),
4149 ap->ioaddr.cmd_addr,
4150 ap->ioaddr.ctl_addr,
4151 ap->ioaddr.bmdma_addr,
4155 host_set->ops->irq_clear(ap);
4164 /* obtain irq, that is shared between channels */
4165 if (request_irq(ent->irq, ent->port_ops->irq_handler, ent->irq_flags,
4166 DRV_NAME, host_set))
4169 /* perform each probe synchronously */
4170 DPRINTK("probe begin\n");
4171 for (i = 0; i < count; i++) {
4172 struct ata_port *ap;
4175 ap = host_set->ports[i];
4177 DPRINTK("ata%u: probe begin\n", ap->id);
4178 rc = ata_bus_probe(ap);
4179 DPRINTK("ata%u: probe end\n", ap->id);
4182 /* FIXME: do something useful here?
4183 * Current libata behavior will
4184 * tear down everything when
4185 * the module is removed
4186 * or the h/w is unplugged.
4190 rc = scsi_add_host(ap->host, dev);
4192 printk(KERN_ERR "ata%u: scsi_add_host failed\n",
4194 /* FIXME: do something useful here */
4195 /* FIXME: handle unconditional calls to
4196 * scsi_scan_host and ata_host_remove, below,
4202 /* probes are done, now scan each port's disk(s) */
4203 DPRINTK("probe begin\n");
4204 for (i = 0; i < count; i++) {
4205 struct ata_port *ap = host_set->ports[i];
4207 ata_scsi_scan_host(ap);
4210 dev_set_drvdata(dev, host_set);
4212 VPRINTK("EXIT, returning %u\n", ent->n_ports);
4213 return ent->n_ports; /* success */
4216 for (i = 0; i < count; i++) {
4217 ata_host_remove(host_set->ports[i], 1);
4218 scsi_host_put(host_set->ports[i]->host);
4221 VPRINTK("EXIT, returning 0\n");
4226 * ata_host_set_remove - PCI layer callback for device removal
4227 * @host_set: ATA host set that was removed
4229 * Unregister all objects associated with this host set. Free those
4233 * Inherited from calling layer (may sleep).
4237 void ata_host_set_remove(struct ata_host_set *host_set)
4239 struct ata_port *ap;
4242 for (i = 0; i < host_set->n_ports; i++) {
4243 ap = host_set->ports[i];
4244 scsi_remove_host(ap->host);
4247 free_irq(host_set->irq, host_set);
4249 for (i = 0; i < host_set->n_ports; i++) {
4250 ap = host_set->ports[i];
4252 ata_scsi_release(ap->host);
4254 if ((ap->flags & ATA_FLAG_NO_LEGACY) == 0) {
4255 struct ata_ioports *ioaddr = &ap->ioaddr;
4257 if (ioaddr->cmd_addr == 0x1f0)
4258 release_region(0x1f0, 8);
4259 else if (ioaddr->cmd_addr == 0x170)
4260 release_region(0x170, 8);
4263 scsi_host_put(ap->host);
4266 if (host_set->ops->host_stop)
4267 host_set->ops->host_stop(host_set);
4273 * ata_scsi_release - SCSI layer callback hook for host unload
4274 * @host: libata host to be unloaded
4276 * Performs all duties necessary to shut down a libata port...
4277 * Kill port kthread, disable port, and release resources.
4280 * Inherited from SCSI layer.
4286 int ata_scsi_release(struct Scsi_Host *host)
4288 struct ata_port *ap = (struct ata_port *) &host->hostdata[0];
4292 ap->ops->port_disable(ap);
4293 ata_host_remove(ap, 0);
4300 * ata_std_ports - initialize ioaddr with standard port offsets.
4301 * @ioaddr: IO address structure to be initialized
4303 * Utility function which initializes data_addr, error_addr,
4304 * feature_addr, nsect_addr, lbal_addr, lbam_addr, lbah_addr,
4305 * device_addr, status_addr, and command_addr to standard offsets
4306 * relative to cmd_addr.
4308 * Does not set ctl_addr, altstatus_addr, bmdma_addr, or scr_addr.
4311 void ata_std_ports(struct ata_ioports *ioaddr)
4313 ioaddr->data_addr = ioaddr->cmd_addr + ATA_REG_DATA;
4314 ioaddr->error_addr = ioaddr->cmd_addr + ATA_REG_ERR;
4315 ioaddr->feature_addr = ioaddr->cmd_addr + ATA_REG_FEATURE;
4316 ioaddr->nsect_addr = ioaddr->cmd_addr + ATA_REG_NSECT;
4317 ioaddr->lbal_addr = ioaddr->cmd_addr + ATA_REG_LBAL;
4318 ioaddr->lbam_addr = ioaddr->cmd_addr + ATA_REG_LBAM;
4319 ioaddr->lbah_addr = ioaddr->cmd_addr + ATA_REG_LBAH;
4320 ioaddr->device_addr = ioaddr->cmd_addr + ATA_REG_DEVICE;
4321 ioaddr->status_addr = ioaddr->cmd_addr + ATA_REG_STATUS;
4322 ioaddr->command_addr = ioaddr->cmd_addr + ATA_REG_CMD;
4325 static struct ata_probe_ent *
4326 ata_probe_ent_alloc(struct device *dev, struct ata_port_info *port)
4328 struct ata_probe_ent *probe_ent;
4330 probe_ent = kmalloc(sizeof(*probe_ent), GFP_KERNEL);
4332 printk(KERN_ERR DRV_NAME "(%s): out of memory\n",
4333 kobject_name(&(dev->kobj)));
4337 memset(probe_ent, 0, sizeof(*probe_ent));
4339 INIT_LIST_HEAD(&probe_ent->node);
4340 probe_ent->dev = dev;
4342 probe_ent->sht = port->sht;
4343 probe_ent->host_flags = port->host_flags;
4344 probe_ent->pio_mask = port->pio_mask;
4345 probe_ent->mwdma_mask = port->mwdma_mask;
4346 probe_ent->udma_mask = port->udma_mask;
4347 probe_ent->port_ops = port->port_ops;
4356 void ata_pci_host_stop (struct ata_host_set *host_set)
4358 struct pci_dev *pdev = to_pci_dev(host_set->dev);
4360 pci_iounmap(pdev, host_set->mmio_base);
4364 * ata_pci_init_native_mode - Initialize native-mode driver
4365 * @pdev: pci device to be initialized
4366 * @port: array[2] of pointers to port info structures.
4367 * @ports: bitmap of ports present
4369 * Utility function which allocates and initializes an
4370 * ata_probe_ent structure for a standard dual-port
4371 * PIO-based IDE controller. The returned ata_probe_ent
4372 * structure can be passed to ata_device_add(). The returned
4373 * ata_probe_ent structure should then be freed with kfree().
4375 * The caller need only pass the address of the primary port, the
4376 * secondary will be deduced automatically. If the device has non
4377 * standard secondary port mappings this function can be called twice,
4378 * once for each interface.
4381 struct ata_probe_ent *
4382 ata_pci_init_native_mode(struct pci_dev *pdev, struct ata_port_info **port, int ports)
4384 struct ata_probe_ent *probe_ent =
4385 ata_probe_ent_alloc(pci_dev_to_dev(pdev), port[0]);
4391 probe_ent->irq = pdev->irq;
4392 probe_ent->irq_flags = SA_SHIRQ;
4394 if (ports & ATA_PORT_PRIMARY) {
4395 probe_ent->port[p].cmd_addr = pci_resource_start(pdev, 0);
4396 probe_ent->port[p].altstatus_addr =
4397 probe_ent->port[p].ctl_addr =
4398 pci_resource_start(pdev, 1) | ATA_PCI_CTL_OFS;
4399 probe_ent->port[p].bmdma_addr = pci_resource_start(pdev, 4);
4400 ata_std_ports(&probe_ent->port[p]);
4404 if (ports & ATA_PORT_SECONDARY) {
4405 probe_ent->port[p].cmd_addr = pci_resource_start(pdev, 2);
4406 probe_ent->port[p].altstatus_addr =
4407 probe_ent->port[p].ctl_addr =
4408 pci_resource_start(pdev, 3) | ATA_PCI_CTL_OFS;
4409 probe_ent->port[p].bmdma_addr = pci_resource_start(pdev, 4) + 8;
4410 ata_std_ports(&probe_ent->port[p]);
4414 probe_ent->n_ports = p;
4418 static struct ata_probe_ent *ata_pci_init_legacy_port(struct pci_dev *pdev, struct ata_port_info **port, int port_num)
4420 struct ata_probe_ent *probe_ent;
4422 probe_ent = ata_probe_ent_alloc(pci_dev_to_dev(pdev), port[0]);
4427 probe_ent->legacy_mode = 1;
4428 probe_ent->n_ports = 1;
4429 probe_ent->hard_port_no = port_num;
4434 probe_ent->irq = 14;
4435 probe_ent->port[0].cmd_addr = 0x1f0;
4436 probe_ent->port[0].altstatus_addr =
4437 probe_ent->port[0].ctl_addr = 0x3f6;
4440 probe_ent->irq = 15;
4441 probe_ent->port[0].cmd_addr = 0x170;
4442 probe_ent->port[0].altstatus_addr =
4443 probe_ent->port[0].ctl_addr = 0x376;
4446 probe_ent->port[0].bmdma_addr = pci_resource_start(pdev, 4) + 8 * port_num;
4447 ata_std_ports(&probe_ent->port[0]);
4452 * ata_pci_init_one - Initialize/register PCI IDE host controller
4453 * @pdev: Controller to be initialized
4454 * @port_info: Information from low-level host driver
4455 * @n_ports: Number of ports attached to host controller
4457 * This is a helper function which can be called from a driver's
4458 * xxx_init_one() probe function if the hardware uses traditional
4459 * IDE taskfile registers.
4461 * This function calls pci_enable_device(), reserves its register
4462 * regions, sets the dma mask, enables bus master mode, and calls
4466 * Inherited from PCI layer (may sleep).
4469 * Zero on success, negative on errno-based value on error.
4473 int ata_pci_init_one (struct pci_dev *pdev, struct ata_port_info **port_info,
4474 unsigned int n_ports)
4476 struct ata_probe_ent *probe_ent = NULL, *probe_ent2 = NULL;
4477 struct ata_port_info *port[2];
4479 unsigned int legacy_mode = 0;
4480 int disable_dev_on_err = 1;
4485 port[0] = port_info[0];
4487 port[1] = port_info[1];
4491 if ((port[0]->host_flags & ATA_FLAG_NO_LEGACY) == 0
4492 && (pdev->class >> 8) == PCI_CLASS_STORAGE_IDE) {
4493 /* TODO: What if one channel is in native mode ... */
4494 pci_read_config_byte(pdev, PCI_CLASS_PROG, &tmp8);
4495 mask = (1 << 2) | (1 << 0);
4496 if ((tmp8 & mask) != mask)
4497 legacy_mode = (1 << 3);
4501 if ((!legacy_mode) && (n_ports > 2)) {
4502 printk(KERN_ERR "ata: BUG: native mode, n_ports > 2\n");
4507 /* FIXME: Really for ATA it isn't safe because the device may be
4508 multi-purpose and we want to leave it alone if it was already
4509 enabled. Secondly for shared use as Arjan says we want refcounting
4511 Checking dev->is_enabled is insufficient as this is not set at
4512 boot for the primary video which is BIOS enabled
4515 rc = pci_enable_device(pdev);
4519 rc = pci_request_regions(pdev, DRV_NAME);
4521 disable_dev_on_err = 0;
4525 /* FIXME: Should use platform specific mappers for legacy port ranges */
4527 if (!request_region(0x1f0, 8, "libata")) {
4528 struct resource *conflict, res;
4530 res.end = 0x1f0 + 8 - 1;
4531 conflict = ____request_resource(&ioport_resource, &res);
4532 if (!strcmp(conflict->name, "libata"))
4533 legacy_mode |= (1 << 0);
4535 disable_dev_on_err = 0;
4536 printk(KERN_WARNING "ata: 0x1f0 IDE port busy\n");
4539 legacy_mode |= (1 << 0);
4541 if (!request_region(0x170, 8, "libata")) {
4542 struct resource *conflict, res;
4544 res.end = 0x170 + 8 - 1;
4545 conflict = ____request_resource(&ioport_resource, &res);
4546 if (!strcmp(conflict->name, "libata"))
4547 legacy_mode |= (1 << 1);
4549 disable_dev_on_err = 0;
4550 printk(KERN_WARNING "ata: 0x170 IDE port busy\n");
4553 legacy_mode |= (1 << 1);
4556 /* we have legacy mode, but all ports are unavailable */
4557 if (legacy_mode == (1 << 3)) {
4559 goto err_out_regions;
4562 rc = pci_set_dma_mask(pdev, ATA_DMA_MASK);
4564 goto err_out_regions;
4565 rc = pci_set_consistent_dma_mask(pdev, ATA_DMA_MASK);
4567 goto err_out_regions;
4570 if (legacy_mode & (1 << 0))
4571 probe_ent = ata_pci_init_legacy_port(pdev, port, 0);
4572 if (legacy_mode & (1 << 1))
4573 probe_ent2 = ata_pci_init_legacy_port(pdev, port, 1);
4576 probe_ent = ata_pci_init_native_mode(pdev, port, ATA_PORT_PRIMARY | ATA_PORT_SECONDARY);
4578 probe_ent = ata_pci_init_native_mode(pdev, port, ATA_PORT_PRIMARY);
4580 if (!probe_ent && !probe_ent2) {
4582 goto err_out_regions;
4585 pci_set_master(pdev);
4587 /* FIXME: check ata_device_add return */
4589 if (legacy_mode & (1 << 0))
4590 ata_device_add(probe_ent);
4591 if (legacy_mode & (1 << 1))
4592 ata_device_add(probe_ent2);
4594 ata_device_add(probe_ent);
4602 if (legacy_mode & (1 << 0))
4603 release_region(0x1f0, 8);
4604 if (legacy_mode & (1 << 1))
4605 release_region(0x170, 8);
4606 pci_release_regions(pdev);
4608 if (disable_dev_on_err)
4609 pci_disable_device(pdev);
4614 * ata_pci_remove_one - PCI layer callback for device removal
4615 * @pdev: PCI device that was removed
4617 * PCI layer indicates to libata via this hook that
4618 * hot-unplug or module unload event has occured.
4619 * Handle this by unregistering all objects associated
4620 * with this PCI device. Free those objects. Then finally
4621 * release PCI resources and disable device.
4624 * Inherited from PCI layer (may sleep).
4627 void ata_pci_remove_one (struct pci_dev *pdev)
4629 struct device *dev = pci_dev_to_dev(pdev);
4630 struct ata_host_set *host_set = dev_get_drvdata(dev);
4632 ata_host_set_remove(host_set);
4633 pci_release_regions(pdev);
4634 pci_disable_device(pdev);
4635 dev_set_drvdata(dev, NULL);
4638 /* move to PCI subsystem */
4639 int pci_test_config_bits(struct pci_dev *pdev, struct pci_bits *bits)
4641 unsigned long tmp = 0;
4643 switch (bits->width) {
4646 pci_read_config_byte(pdev, bits->reg, &tmp8);
4652 pci_read_config_word(pdev, bits->reg, &tmp16);
4658 pci_read_config_dword(pdev, bits->reg, &tmp32);
4669 return (tmp == bits->val) ? 1 : 0;
4671 #endif /* CONFIG_PCI */
4674 static int __init ata_init(void)
4676 ata_wq = create_workqueue("ata");
4680 printk(KERN_DEBUG "libata version " DRV_VERSION " loaded.\n");
4684 static void __exit ata_exit(void)
4686 destroy_workqueue(ata_wq);
4689 module_init(ata_init);
4690 module_exit(ata_exit);
4692 static unsigned long ratelimit_time;
4693 static spinlock_t ata_ratelimit_lock = SPIN_LOCK_UNLOCKED;
4695 int ata_ratelimit(void)
4698 unsigned long flags;
4700 spin_lock_irqsave(&ata_ratelimit_lock, flags);
4702 if (time_after(jiffies, ratelimit_time)) {
4704 ratelimit_time = jiffies + (HZ/5);
4708 spin_unlock_irqrestore(&ata_ratelimit_lock, flags);
4714 * libata is essentially a library of internal helper functions for
4715 * low-level ATA host controller drivers. As such, the API/ABI is
4716 * likely to change as new drivers are added and updated.
4717 * Do not depend on ABI/API stability.
4720 EXPORT_SYMBOL_GPL(ata_std_bios_param);
4721 EXPORT_SYMBOL_GPL(ata_std_ports);
4722 EXPORT_SYMBOL_GPL(ata_device_add);
4723 EXPORT_SYMBOL_GPL(ata_host_set_remove);
4724 EXPORT_SYMBOL_GPL(ata_sg_init);
4725 EXPORT_SYMBOL_GPL(ata_sg_init_one);
4726 EXPORT_SYMBOL_GPL(ata_qc_complete);
4727 EXPORT_SYMBOL_GPL(ata_qc_issue_prot);
4728 EXPORT_SYMBOL_GPL(ata_eng_timeout);
4729 EXPORT_SYMBOL_GPL(ata_tf_load);
4730 EXPORT_SYMBOL_GPL(ata_tf_read);
4731 EXPORT_SYMBOL_GPL(ata_noop_dev_select);
4732 EXPORT_SYMBOL_GPL(ata_std_dev_select);
4733 EXPORT_SYMBOL_GPL(ata_tf_to_fis);
4734 EXPORT_SYMBOL_GPL(ata_tf_from_fis);
4735 EXPORT_SYMBOL_GPL(ata_check_status);
4736 EXPORT_SYMBOL_GPL(ata_altstatus);
4737 EXPORT_SYMBOL_GPL(ata_chk_err);
4738 EXPORT_SYMBOL_GPL(ata_exec_command);
4739 EXPORT_SYMBOL_GPL(ata_port_start);
4740 EXPORT_SYMBOL_GPL(ata_port_stop);
4741 EXPORT_SYMBOL_GPL(ata_host_stop);
4742 EXPORT_SYMBOL_GPL(ata_interrupt);
4743 EXPORT_SYMBOL_GPL(ata_qc_prep);
4744 EXPORT_SYMBOL_GPL(ata_bmdma_setup);
4745 EXPORT_SYMBOL_GPL(ata_bmdma_start);
4746 EXPORT_SYMBOL_GPL(ata_bmdma_irq_clear);
4747 EXPORT_SYMBOL_GPL(ata_bmdma_status);
4748 EXPORT_SYMBOL_GPL(ata_bmdma_stop);
4749 EXPORT_SYMBOL_GPL(ata_port_probe);
4750 EXPORT_SYMBOL_GPL(sata_phy_reset);
4751 EXPORT_SYMBOL_GPL(__sata_phy_reset);
4752 EXPORT_SYMBOL_GPL(ata_bus_reset);
4753 EXPORT_SYMBOL_GPL(ata_port_disable);
4754 EXPORT_SYMBOL_GPL(ata_ratelimit);
4755 EXPORT_SYMBOL_GPL(ata_scsi_ioctl);
4756 EXPORT_SYMBOL_GPL(ata_scsi_queuecmd);
4757 EXPORT_SYMBOL_GPL(ata_scsi_error);
4758 EXPORT_SYMBOL_GPL(ata_scsi_slave_config);
4759 EXPORT_SYMBOL_GPL(ata_scsi_release);
4760 EXPORT_SYMBOL_GPL(ata_host_intr);
4761 EXPORT_SYMBOL_GPL(ata_dev_classify);
4762 EXPORT_SYMBOL_GPL(ata_dev_id_string);
4763 EXPORT_SYMBOL_GPL(ata_dev_config);
4764 EXPORT_SYMBOL_GPL(ata_scsi_simulate);
4767 EXPORT_SYMBOL_GPL(pci_test_config_bits);
4768 EXPORT_SYMBOL_GPL(ata_pci_host_stop);
4769 EXPORT_SYMBOL_GPL(ata_pci_init_native_mode);
4770 EXPORT_SYMBOL_GPL(ata_pci_init_one);
4771 EXPORT_SYMBOL_GPL(ata_pci_remove_one);
4772 #endif /* CONFIG_PCI */