2 * libata-core.c - helper library for ATA
4 * Maintained by: Jeff Garzik <jgarzik@pobox.com>
5 * Please ALWAYS copy linux-ide@vger.kernel.org
8 * Copyright 2003-2004 Red Hat, Inc. All rights reserved.
9 * Copyright 2003-2004 Jeff Garzik
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2, or (at your option)
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; see the file COPYING. If not, write to
24 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
27 * libata documentation is available via 'make {ps|pdf}docs',
28 * as Documentation/DocBook/libata.*
30 * Hardware documentation available from http://www.t13.org/ and
31 * http://www.sata-io.org/
35 #include <linux/config.h>
36 #include <linux/kernel.h>
37 #include <linux/module.h>
38 #include <linux/pci.h>
39 #include <linux/init.h>
40 #include <linux/list.h>
42 #include <linux/highmem.h>
43 #include <linux/spinlock.h>
44 #include <linux/blkdev.h>
45 #include <linux/delay.h>
46 #include <linux/timer.h>
47 #include <linux/interrupt.h>
48 #include <linux/completion.h>
49 #include <linux/suspend.h>
50 #include <linux/workqueue.h>
51 #include <linux/jiffies.h>
52 #include <scsi/scsi.h>
54 #include "scsi_priv.h"
55 #include <scsi/scsi_host.h>
56 #include <linux/libata.h>
58 #include <asm/semaphore.h>
59 #include <asm/byteorder.h>
63 static unsigned int ata_busy_sleep (struct ata_port *ap,
64 unsigned long tmout_pat,
66 static void ata_dev_reread_id(struct ata_port *ap, struct ata_device *dev);
67 static void ata_dev_init_params(struct ata_port *ap, struct ata_device *dev);
68 static void ata_set_mode(struct ata_port *ap);
69 static void ata_dev_set_xfermode(struct ata_port *ap, struct ata_device *dev);
70 static unsigned int ata_get_mode_mask(struct ata_port *ap, int shift);
71 static int fgb(u32 bitmap);
72 static int ata_choose_xfer_mode(struct ata_port *ap,
74 unsigned int *xfer_shift_out);
75 static void __ata_qc_complete(struct ata_queued_cmd *qc);
77 static unsigned int ata_unique_id = 1;
78 static struct workqueue_struct *ata_wq;
80 int atapi_enabled = 0;
81 module_param(atapi_enabled, int, 0444);
82 MODULE_PARM_DESC(atapi_enabled, "Enable discovery of ATAPI devices (0=off, 1=on)");
84 MODULE_AUTHOR("Jeff Garzik");
85 MODULE_DESCRIPTION("Library module for ATA devices");
86 MODULE_LICENSE("GPL");
87 MODULE_VERSION(DRV_VERSION);
90 * ata_tf_load - 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];
620 static const u8 ata_rw_cmds[] = {
624 ATA_CMD_READ_MULTI_EXT,
625 ATA_CMD_WRITE_MULTI_EXT,
629 ATA_CMD_PIO_READ_EXT,
630 ATA_CMD_PIO_WRITE_EXT,
639 * ata_rwcmd_protocol - set taskfile r/w commands and protocol
640 * @qc: command to examine and configure
642 * Examine the device configuration and tf->flags to calculate
643 * the proper read/write commands and protocol to use.
648 void ata_rwcmd_protocol(struct ata_queued_cmd *qc)
650 struct ata_taskfile *tf = &qc->tf;
651 struct ata_device *dev = qc->dev;
653 int index, lba48, write;
655 lba48 = (tf->flags & ATA_TFLAG_LBA48) ? 2 : 0;
656 write = (tf->flags & ATA_TFLAG_WRITE) ? 1 : 0;
658 if (dev->flags & ATA_DFLAG_PIO) {
659 tf->protocol = ATA_PROT_PIO;
660 index = dev->multi_count ? 0 : 4;
662 tf->protocol = ATA_PROT_DMA;
666 tf->command = ata_rw_cmds[index + lba48 + write];
669 static const char * xfer_mode_str[] = {
689 * ata_udma_string - convert UDMA bit offset to string
690 * @mask: mask of bits supported; only highest bit counts.
692 * Determine string which represents the highest speed
693 * (highest bit in @udma_mask).
699 * Constant C string representing highest speed listed in
700 * @udma_mask, or the constant C string "<n/a>".
703 static const char *ata_mode_string(unsigned int mask)
707 for (i = 7; i >= 0; i--)
710 for (i = ATA_SHIFT_MWDMA + 2; i >= ATA_SHIFT_MWDMA; i--)
713 for (i = ATA_SHIFT_PIO + 4; i >= ATA_SHIFT_PIO; i--)
720 return xfer_mode_str[i];
724 * ata_pio_devchk - PATA device presence detection
725 * @ap: ATA channel to examine
726 * @device: Device to examine (starting at zero)
728 * This technique was originally described in
729 * Hale Landis's ATADRVR (www.ata-atapi.com), and
730 * later found its way into the ATA/ATAPI spec.
732 * Write a pattern to the ATA shadow registers,
733 * and if a device is present, it will respond by
734 * correctly storing and echoing back the
735 * ATA shadow register contents.
741 static unsigned int ata_pio_devchk(struct ata_port *ap,
744 struct ata_ioports *ioaddr = &ap->ioaddr;
747 ap->ops->dev_select(ap, device);
749 outb(0x55, ioaddr->nsect_addr);
750 outb(0xaa, ioaddr->lbal_addr);
752 outb(0xaa, ioaddr->nsect_addr);
753 outb(0x55, ioaddr->lbal_addr);
755 outb(0x55, ioaddr->nsect_addr);
756 outb(0xaa, ioaddr->lbal_addr);
758 nsect = inb(ioaddr->nsect_addr);
759 lbal = inb(ioaddr->lbal_addr);
761 if ((nsect == 0x55) && (lbal == 0xaa))
762 return 1; /* we found a device */
764 return 0; /* nothing found */
768 * ata_mmio_devchk - PATA device presence detection
769 * @ap: ATA channel to examine
770 * @device: Device to examine (starting at zero)
772 * This technique was originally described in
773 * Hale Landis's ATADRVR (www.ata-atapi.com), and
774 * later found its way into the ATA/ATAPI spec.
776 * Write a pattern to the ATA shadow registers,
777 * and if a device is present, it will respond by
778 * correctly storing and echoing back the
779 * ATA shadow register contents.
785 static unsigned int ata_mmio_devchk(struct ata_port *ap,
788 struct ata_ioports *ioaddr = &ap->ioaddr;
791 ap->ops->dev_select(ap, device);
793 writeb(0x55, (void __iomem *) ioaddr->nsect_addr);
794 writeb(0xaa, (void __iomem *) ioaddr->lbal_addr);
796 writeb(0xaa, (void __iomem *) ioaddr->nsect_addr);
797 writeb(0x55, (void __iomem *) ioaddr->lbal_addr);
799 writeb(0x55, (void __iomem *) ioaddr->nsect_addr);
800 writeb(0xaa, (void __iomem *) ioaddr->lbal_addr);
802 nsect = readb((void __iomem *) ioaddr->nsect_addr);
803 lbal = readb((void __iomem *) ioaddr->lbal_addr);
805 if ((nsect == 0x55) && (lbal == 0xaa))
806 return 1; /* we found a device */
808 return 0; /* nothing found */
812 * ata_devchk - PATA device presence detection
813 * @ap: ATA channel to examine
814 * @device: Device to examine (starting at zero)
816 * Dispatch ATA device presence detection, depending
817 * on whether we are using PIO or MMIO to talk to the
818 * ATA shadow registers.
824 static unsigned int ata_devchk(struct ata_port *ap,
827 if (ap->flags & ATA_FLAG_MMIO)
828 return ata_mmio_devchk(ap, device);
829 return ata_pio_devchk(ap, device);
833 * ata_dev_classify - determine device type based on ATA-spec signature
834 * @tf: ATA taskfile register set for device to be identified
836 * Determine from taskfile register contents whether a device is
837 * ATA or ATAPI, as per "Signature and persistence" section
838 * of ATA/PI spec (volume 1, sect 5.14).
844 * Device type, %ATA_DEV_ATA, %ATA_DEV_ATAPI, or %ATA_DEV_UNKNOWN
845 * the event of failure.
848 unsigned int ata_dev_classify(struct ata_taskfile *tf)
850 /* Apple's open source Darwin code hints that some devices only
851 * put a proper signature into the LBA mid/high registers,
852 * So, we only check those. It's sufficient for uniqueness.
855 if (((tf->lbam == 0) && (tf->lbah == 0)) ||
856 ((tf->lbam == 0x3c) && (tf->lbah == 0xc3))) {
857 DPRINTK("found ATA device by sig\n");
861 if (((tf->lbam == 0x14) && (tf->lbah == 0xeb)) ||
862 ((tf->lbam == 0x69) && (tf->lbah == 0x96))) {
863 DPRINTK("found ATAPI device by sig\n");
864 return ATA_DEV_ATAPI;
867 DPRINTK("unknown device\n");
868 return ATA_DEV_UNKNOWN;
872 * ata_dev_try_classify - Parse returned ATA device signature
873 * @ap: ATA channel to examine
874 * @device: Device to examine (starting at zero)
876 * After an event -- SRST, E.D.D., or SATA COMRESET -- occurs,
877 * an ATA/ATAPI-defined set of values is placed in the ATA
878 * shadow registers, indicating the results of device detection
881 * Select the ATA device, and read the values from the ATA shadow
882 * registers. Then parse according to the Error register value,
883 * and the spec-defined values examined by ata_dev_classify().
889 static u8 ata_dev_try_classify(struct ata_port *ap, unsigned int device)
891 struct ata_device *dev = &ap->device[device];
892 struct ata_taskfile tf;
896 ap->ops->dev_select(ap, device);
898 memset(&tf, 0, sizeof(tf));
900 err = ata_chk_err(ap);
901 ap->ops->tf_read(ap, &tf);
903 dev->class = ATA_DEV_NONE;
905 /* see if device passed diags */
908 else if ((device == 0) && (err == 0x81))
913 /* determine if device if ATA or ATAPI */
914 class = ata_dev_classify(&tf);
915 if (class == ATA_DEV_UNKNOWN)
917 if ((class == ATA_DEV_ATA) && (ata_chk_status(ap) == 0))
926 * ata_dev_id_string - Convert IDENTIFY DEVICE page into string
927 * @id: IDENTIFY DEVICE results we will examine
928 * @s: string into which data is output
929 * @ofs: offset into identify device page
930 * @len: length of string to return. must be an even number.
932 * The strings in the IDENTIFY DEVICE page are broken up into
933 * 16-bit chunks. Run through the string, and output each
934 * 8-bit chunk linearly, regardless of platform.
940 void ata_dev_id_string(u16 *id, unsigned char *s,
941 unsigned int ofs, unsigned int len)
961 * ata_noop_dev_select - Select device 0/1 on ATA bus
962 * @ap: ATA channel to manipulate
963 * @device: ATA device (numbered from zero) to select
965 * This function performs no actual function.
967 * May be used as the dev_select() entry in ata_port_operations.
972 void ata_noop_dev_select (struct ata_port *ap, unsigned int device)
978 * ata_std_dev_select - Select device 0/1 on ATA bus
979 * @ap: ATA channel to manipulate
980 * @device: ATA device (numbered from zero) to select
982 * Use the method defined in the ATA specification to
983 * make either device 0, or device 1, active on the
984 * ATA channel. Works with both PIO and MMIO.
986 * May be used as the dev_select() entry in ata_port_operations.
992 void ata_std_dev_select (struct ata_port *ap, unsigned int device)
997 tmp = ATA_DEVICE_OBS;
999 tmp = ATA_DEVICE_OBS | ATA_DEV1;
1001 if (ap->flags & ATA_FLAG_MMIO) {
1002 writeb(tmp, (void __iomem *) ap->ioaddr.device_addr);
1004 outb(tmp, ap->ioaddr.device_addr);
1006 ata_pause(ap); /* needed; also flushes, for mmio */
1010 * ata_dev_select - Select device 0/1 on ATA bus
1011 * @ap: ATA channel to manipulate
1012 * @device: ATA device (numbered from zero) to select
1013 * @wait: non-zero to wait for Status register BSY bit to clear
1014 * @can_sleep: non-zero if context allows sleeping
1016 * Use the method defined in the ATA specification to
1017 * make either device 0, or device 1, active on the
1020 * This is a high-level version of ata_std_dev_select(),
1021 * which additionally provides the services of inserting
1022 * the proper pauses and status polling, where needed.
1028 void ata_dev_select(struct ata_port *ap, unsigned int device,
1029 unsigned int wait, unsigned int can_sleep)
1031 VPRINTK("ENTER, ata%u: device %u, wait %u\n",
1032 ap->id, device, wait);
1037 ap->ops->dev_select(ap, device);
1040 if (can_sleep && ap->device[device].class == ATA_DEV_ATAPI)
1047 * ata_dump_id - IDENTIFY DEVICE info debugging output
1048 * @dev: Device whose IDENTIFY DEVICE page we will dump
1050 * Dump selected 16-bit words from a detected device's
1051 * IDENTIFY PAGE page.
1057 static inline void ata_dump_id(struct ata_device *dev)
1059 DPRINTK("49==0x%04x "
1069 DPRINTK("80==0x%04x "
1079 DPRINTK("88==0x%04x "
1086 * ata_dev_identify - obtain IDENTIFY x DEVICE page
1087 * @ap: port on which device we wish to probe resides
1088 * @device: device bus address, starting at zero
1090 * Following bus reset, we issue the IDENTIFY [PACKET] DEVICE
1091 * command, and read back the 512-byte device information page.
1092 * The device information page is fed to us via the standard
1093 * PIO-IN protocol, but we hand-code it here. (TODO: investigate
1094 * using standard PIO-IN paths)
1096 * After reading the device information page, we use several
1097 * bits of information from it to initialize data structures
1098 * that will be used during the lifetime of the ata_device.
1099 * Other data from the info page is used to disqualify certain
1100 * older ATA devices we do not wish to support.
1103 * Inherited from caller. Some functions called by this function
1104 * obtain the host_set lock.
1107 static void ata_dev_identify(struct ata_port *ap, unsigned int device)
1109 struct ata_device *dev = &ap->device[device];
1110 unsigned int major_version;
1112 unsigned long xfer_modes;
1114 unsigned int using_edd;
1115 DECLARE_COMPLETION(wait);
1116 struct ata_queued_cmd *qc;
1117 unsigned long flags;
1120 if (!ata_dev_present(dev)) {
1121 DPRINTK("ENTER/EXIT (host %u, dev %u) -- nodev\n",
1126 if (ap->flags & (ATA_FLAG_SRST | ATA_FLAG_SATA_RESET))
1131 DPRINTK("ENTER, host %u, dev %u\n", ap->id, device);
1133 assert (dev->class == ATA_DEV_ATA || dev->class == ATA_DEV_ATAPI ||
1134 dev->class == ATA_DEV_NONE);
1136 ata_dev_select(ap, device, 1, 1); /* select device 0/1 */
1138 qc = ata_qc_new_init(ap, dev);
1141 ata_sg_init_one(qc, dev->id, sizeof(dev->id));
1142 qc->dma_dir = DMA_FROM_DEVICE;
1143 qc->tf.protocol = ATA_PROT_PIO;
1147 if (dev->class == ATA_DEV_ATA) {
1148 qc->tf.command = ATA_CMD_ID_ATA;
1149 DPRINTK("do ATA identify\n");
1151 qc->tf.command = ATA_CMD_ID_ATAPI;
1152 DPRINTK("do ATAPI identify\n");
1155 qc->waiting = &wait;
1156 qc->complete_fn = ata_qc_complete_noop;
1158 spin_lock_irqsave(&ap->host_set->lock, flags);
1159 rc = ata_qc_issue(qc);
1160 spin_unlock_irqrestore(&ap->host_set->lock, flags);
1165 wait_for_completion(&wait);
1167 status = ata_chk_status(ap);
1168 if (status & ATA_ERR) {
1170 * arg! EDD works for all test cases, but seems to return
1171 * the ATA signature for some ATAPI devices. Until the
1172 * reason for this is found and fixed, we fix up the mess
1173 * here. If IDENTIFY DEVICE returns command aborted
1174 * (as ATAPI devices do), then we issue an
1175 * IDENTIFY PACKET DEVICE.
1177 * ATA software reset (SRST, the default) does not appear
1178 * to have this problem.
1180 if ((using_edd) && (qc->tf.command == ATA_CMD_ID_ATA)) {
1181 u8 err = ata_chk_err(ap);
1182 if (err & ATA_ABORTED) {
1183 dev->class = ATA_DEV_ATAPI;
1194 swap_buf_le16(dev->id, ATA_ID_WORDS);
1196 /* print device capabilities */
1197 printk(KERN_DEBUG "ata%u: dev %u cfg "
1198 "49:%04x 82:%04x 83:%04x 84:%04x 85:%04x 86:%04x 87:%04x 88:%04x\n",
1199 ap->id, device, dev->id[49],
1200 dev->id[82], dev->id[83], dev->id[84],
1201 dev->id[85], dev->id[86], dev->id[87],
1205 * common ATA, ATAPI feature tests
1208 /* we require DMA support (bits 8 of word 49) */
1209 if (!ata_id_has_dma(dev->id)) {
1210 printk(KERN_DEBUG "ata%u: no dma\n", ap->id);
1214 /* quick-n-dirty find max transfer mode; for printk only */
1215 xfer_modes = dev->id[ATA_ID_UDMA_MODES];
1217 xfer_modes = (dev->id[ATA_ID_MWDMA_MODES]) << ATA_SHIFT_MWDMA;
1219 xfer_modes = (dev->id[ATA_ID_PIO_MODES]) << (ATA_SHIFT_PIO + 3);
1220 xfer_modes |= (0x7 << ATA_SHIFT_PIO);
1225 /* ATA-specific feature tests */
1226 if (dev->class == ATA_DEV_ATA) {
1227 if (!ata_id_is_ata(dev->id)) /* sanity check */
1230 /* get major version */
1231 tmp = dev->id[ATA_ID_MAJOR_VER];
1232 for (major_version = 14; major_version >= 1; major_version--)
1233 if (tmp & (1 << major_version))
1237 * The exact sequence expected by certain pre-ATA4 drives is:
1240 * INITIALIZE DEVICE PARAMETERS
1242 * Some drives were very specific about that exact sequence.
1244 if (major_version < 4 || (!ata_id_has_lba(dev->id))) {
1245 ata_dev_init_params(ap, dev);
1247 /* current CHS translation info (id[53-58]) might be
1248 * changed. reread the identify device info.
1250 ata_dev_reread_id(ap, dev);
1253 if (ata_id_has_lba(dev->id)) {
1254 dev->flags |= ATA_DFLAG_LBA;
1256 if (ata_id_has_lba48(dev->id)) {
1257 dev->flags |= ATA_DFLAG_LBA48;
1258 dev->n_sectors = ata_id_u64(dev->id, 100);
1260 dev->n_sectors = ata_id_u32(dev->id, 60);
1263 /* print device info to dmesg */
1264 printk(KERN_INFO "ata%u: dev %u ATA-%d, max %s, %Lu sectors:%s\n",
1267 ata_mode_string(xfer_modes),
1268 (unsigned long long)dev->n_sectors,
1269 dev->flags & ATA_DFLAG_LBA48 ? " LBA48" : " LBA");
1273 /* Default translation */
1274 dev->cylinders = dev->id[1];
1275 dev->heads = dev->id[3];
1276 dev->sectors = dev->id[6];
1277 dev->n_sectors = dev->cylinders * dev->heads * dev->sectors;
1279 if (ata_id_current_chs_valid(dev->id)) {
1280 /* Current CHS translation is valid. */
1281 dev->cylinders = dev->id[54];
1282 dev->heads = dev->id[55];
1283 dev->sectors = dev->id[56];
1285 dev->n_sectors = ata_id_u32(dev->id, 57);
1288 /* print device info to dmesg */
1289 printk(KERN_INFO "ata%u: dev %u ATA-%d, max %s, %Lu sectors: CHS %d/%d/%d\n",
1292 ata_mode_string(xfer_modes),
1293 (unsigned long long)dev->n_sectors,
1294 (int)dev->cylinders, (int)dev->heads, (int)dev->sectors);
1298 ap->host->max_cmd_len = 16;
1301 /* ATAPI-specific feature tests */
1303 if (ata_id_is_ata(dev->id)) /* sanity check */
1306 rc = atapi_cdb_len(dev->id);
1307 if ((rc < 12) || (rc > ATAPI_CDB_LEN)) {
1308 printk(KERN_WARNING "ata%u: unsupported CDB len\n", ap->id);
1311 ap->cdb_len = (unsigned int) rc;
1312 ap->host->max_cmd_len = (unsigned char) ap->cdb_len;
1314 /* print device info to dmesg */
1315 printk(KERN_INFO "ata%u: dev %u ATAPI, max %s\n",
1317 ata_mode_string(xfer_modes));
1320 DPRINTK("EXIT, drv_stat = 0x%x\n", ata_chk_status(ap));
1324 printk(KERN_WARNING "ata%u: dev %u not supported, ignoring\n",
1327 dev->class++; /* converts ATA_DEV_xxx into ATA_DEV_xxx_UNSUP */
1328 DPRINTK("EXIT, err\n");
1332 static inline u8 ata_dev_knobble(struct ata_port *ap)
1334 return ((ap->cbl == ATA_CBL_SATA) && (!ata_id_is_sata(ap->device->id)));
1338 * ata_dev_config - Run device specific handlers and check for
1339 * SATA->PATA bridges
1346 void ata_dev_config(struct ata_port *ap, unsigned int i)
1348 /* limit bridge transfers to udma5, 200 sectors */
1349 if (ata_dev_knobble(ap)) {
1350 printk(KERN_INFO "ata%u(%u): applying bridge limits\n",
1351 ap->id, ap->device->devno);
1352 ap->udma_mask &= ATA_UDMA5;
1353 ap->host->max_sectors = ATA_MAX_SECTORS;
1354 ap->host->hostt->max_sectors = ATA_MAX_SECTORS;
1355 ap->device->flags |= ATA_DFLAG_LOCK_SECTORS;
1358 if (ap->ops->dev_config)
1359 ap->ops->dev_config(ap, &ap->device[i]);
1363 * ata_bus_probe - Reset and probe ATA bus
1366 * Master ATA bus probing function. Initiates a hardware-dependent
1367 * bus reset, then attempts to identify any devices found on
1371 * PCI/etc. bus probe sem.
1374 * Zero on success, non-zero on error.
1377 static int ata_bus_probe(struct ata_port *ap)
1379 unsigned int i, found = 0;
1381 ap->ops->phy_reset(ap);
1382 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1385 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1386 ata_dev_identify(ap, i);
1387 if (ata_dev_present(&ap->device[i])) {
1389 ata_dev_config(ap,i);
1393 if ((!found) || (ap->flags & ATA_FLAG_PORT_DISABLED))
1394 goto err_out_disable;
1397 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1398 goto err_out_disable;
1403 ap->ops->port_disable(ap);
1409 * ata_port_probe - Mark port as enabled
1410 * @ap: Port for which we indicate enablement
1412 * Modify @ap data structure such that the system
1413 * thinks that the entire port is enabled.
1415 * LOCKING: host_set lock, or some other form of
1419 void ata_port_probe(struct ata_port *ap)
1421 ap->flags &= ~ATA_FLAG_PORT_DISABLED;
1425 * __sata_phy_reset - Wake/reset a low-level SATA PHY
1426 * @ap: SATA port associated with target SATA PHY.
1428 * This function issues commands to standard SATA Sxxx
1429 * PHY registers, to wake up the phy (and device), and
1430 * clear any reset condition.
1433 * PCI/etc. bus probe sem.
1436 void __sata_phy_reset(struct ata_port *ap)
1439 unsigned long timeout = jiffies + (HZ * 5);
1441 if (ap->flags & ATA_FLAG_SATA_RESET) {
1442 /* issue phy wake/reset */
1443 scr_write_flush(ap, SCR_CONTROL, 0x301);
1444 /* Couldn't find anything in SATA I/II specs, but
1445 * AHCI-1.1 10.4.2 says at least 1 ms. */
1448 scr_write_flush(ap, SCR_CONTROL, 0x300); /* phy wake/clear reset */
1450 /* wait for phy to become ready, if necessary */
1453 sstatus = scr_read(ap, SCR_STATUS);
1454 if ((sstatus & 0xf) != 1)
1456 } while (time_before(jiffies, timeout));
1458 /* TODO: phy layer with polling, timeouts, etc. */
1459 if (sata_dev_present(ap))
1462 sstatus = scr_read(ap, SCR_STATUS);
1463 printk(KERN_INFO "ata%u: no device found (phy stat %08x)\n",
1465 ata_port_disable(ap);
1468 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1471 if (ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT)) {
1472 ata_port_disable(ap);
1476 ap->cbl = ATA_CBL_SATA;
1480 * sata_phy_reset - Reset SATA bus.
1481 * @ap: SATA port associated with target SATA PHY.
1483 * This function resets the SATA bus, and then probes
1484 * the bus for devices.
1487 * PCI/etc. bus probe sem.
1490 void sata_phy_reset(struct ata_port *ap)
1492 __sata_phy_reset(ap);
1493 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1499 * ata_port_disable - Disable port.
1500 * @ap: Port to be disabled.
1502 * Modify @ap data structure such that the system
1503 * thinks that the entire port is disabled, and should
1504 * never attempt to probe or communicate with devices
1507 * LOCKING: host_set lock, or some other form of
1511 void ata_port_disable(struct ata_port *ap)
1513 ap->device[0].class = ATA_DEV_NONE;
1514 ap->device[1].class = ATA_DEV_NONE;
1515 ap->flags |= ATA_FLAG_PORT_DISABLED;
1521 } xfer_mode_classes[] = {
1522 { ATA_SHIFT_UDMA, XFER_UDMA_0 },
1523 { ATA_SHIFT_MWDMA, XFER_MW_DMA_0 },
1524 { ATA_SHIFT_PIO, XFER_PIO_0 },
1527 static inline u8 base_from_shift(unsigned int shift)
1531 for (i = 0; i < ARRAY_SIZE(xfer_mode_classes); i++)
1532 if (xfer_mode_classes[i].shift == shift)
1533 return xfer_mode_classes[i].base;
1538 static void ata_dev_set_mode(struct ata_port *ap, struct ata_device *dev)
1543 if (!ata_dev_present(dev) || (ap->flags & ATA_FLAG_PORT_DISABLED))
1546 if (dev->xfer_shift == ATA_SHIFT_PIO)
1547 dev->flags |= ATA_DFLAG_PIO;
1549 ata_dev_set_xfermode(ap, dev);
1551 base = base_from_shift(dev->xfer_shift);
1552 ofs = dev->xfer_mode - base;
1553 idx = ofs + dev->xfer_shift;
1554 WARN_ON(idx >= ARRAY_SIZE(xfer_mode_str));
1556 DPRINTK("idx=%d xfer_shift=%u, xfer_mode=0x%x, base=0x%x, offset=%d\n",
1557 idx, dev->xfer_shift, (int)dev->xfer_mode, (int)base, ofs);
1559 printk(KERN_INFO "ata%u: dev %u configured for %s\n",
1560 ap->id, dev->devno, xfer_mode_str[idx]);
1563 static int ata_host_set_pio(struct ata_port *ap)
1569 mask = ata_get_mode_mask(ap, ATA_SHIFT_PIO);
1572 printk(KERN_WARNING "ata%u: no PIO support\n", ap->id);
1576 base = base_from_shift(ATA_SHIFT_PIO);
1577 xfer_mode = base + x;
1579 DPRINTK("base 0x%x xfer_mode 0x%x mask 0x%x x %d\n",
1580 (int)base, (int)xfer_mode, mask, x);
1582 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1583 struct ata_device *dev = &ap->device[i];
1584 if (ata_dev_present(dev)) {
1585 dev->pio_mode = xfer_mode;
1586 dev->xfer_mode = xfer_mode;
1587 dev->xfer_shift = ATA_SHIFT_PIO;
1588 if (ap->ops->set_piomode)
1589 ap->ops->set_piomode(ap, dev);
1596 static void ata_host_set_dma(struct ata_port *ap, u8 xfer_mode,
1597 unsigned int xfer_shift)
1601 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1602 struct ata_device *dev = &ap->device[i];
1603 if (ata_dev_present(dev)) {
1604 dev->dma_mode = xfer_mode;
1605 dev->xfer_mode = xfer_mode;
1606 dev->xfer_shift = xfer_shift;
1607 if (ap->ops->set_dmamode)
1608 ap->ops->set_dmamode(ap, dev);
1614 * ata_set_mode - Program timings and issue SET FEATURES - XFER
1615 * @ap: port on which timings will be programmed
1617 * Set ATA device disk transfer mode (PIO3, UDMA6, etc.).
1620 * PCI/etc. bus probe sem.
1623 static void ata_set_mode(struct ata_port *ap)
1625 unsigned int xfer_shift;
1629 /* step 1: always set host PIO timings */
1630 rc = ata_host_set_pio(ap);
1634 /* step 2: choose the best data xfer mode */
1635 xfer_mode = xfer_shift = 0;
1636 rc = ata_choose_xfer_mode(ap, &xfer_mode, &xfer_shift);
1640 /* step 3: if that xfer mode isn't PIO, set host DMA timings */
1641 if (xfer_shift != ATA_SHIFT_PIO)
1642 ata_host_set_dma(ap, xfer_mode, xfer_shift);
1644 /* step 4: update devices' xfer mode */
1645 ata_dev_set_mode(ap, &ap->device[0]);
1646 ata_dev_set_mode(ap, &ap->device[1]);
1648 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1651 if (ap->ops->post_set_mode)
1652 ap->ops->post_set_mode(ap);
1657 ata_port_disable(ap);
1661 * ata_busy_sleep - sleep until BSY clears, or timeout
1662 * @ap: port containing status register to be polled
1663 * @tmout_pat: impatience timeout
1664 * @tmout: overall timeout
1666 * Sleep until ATA Status register bit BSY clears,
1667 * or a timeout occurs.
1673 static unsigned int ata_busy_sleep (struct ata_port *ap,
1674 unsigned long tmout_pat,
1675 unsigned long tmout)
1677 unsigned long timer_start, timeout;
1680 status = ata_busy_wait(ap, ATA_BUSY, 300);
1681 timer_start = jiffies;
1682 timeout = timer_start + tmout_pat;
1683 while ((status & ATA_BUSY) && (time_before(jiffies, timeout))) {
1685 status = ata_busy_wait(ap, ATA_BUSY, 3);
1688 if (status & ATA_BUSY)
1689 printk(KERN_WARNING "ata%u is slow to respond, "
1690 "please be patient\n", ap->id);
1692 timeout = timer_start + tmout;
1693 while ((status & ATA_BUSY) && (time_before(jiffies, timeout))) {
1695 status = ata_chk_status(ap);
1698 if (status & ATA_BUSY) {
1699 printk(KERN_ERR "ata%u failed to respond (%lu secs)\n",
1700 ap->id, tmout / HZ);
1707 static void ata_bus_post_reset(struct ata_port *ap, unsigned int devmask)
1709 struct ata_ioports *ioaddr = &ap->ioaddr;
1710 unsigned int dev0 = devmask & (1 << 0);
1711 unsigned int dev1 = devmask & (1 << 1);
1712 unsigned long timeout;
1714 /* if device 0 was found in ata_devchk, wait for its
1718 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
1720 /* if device 1 was found in ata_devchk, wait for
1721 * register access, then wait for BSY to clear
1723 timeout = jiffies + ATA_TMOUT_BOOT;
1727 ap->ops->dev_select(ap, 1);
1728 if (ap->flags & ATA_FLAG_MMIO) {
1729 nsect = readb((void __iomem *) ioaddr->nsect_addr);
1730 lbal = readb((void __iomem *) ioaddr->lbal_addr);
1732 nsect = inb(ioaddr->nsect_addr);
1733 lbal = inb(ioaddr->lbal_addr);
1735 if ((nsect == 1) && (lbal == 1))
1737 if (time_after(jiffies, timeout)) {
1741 msleep(50); /* give drive a breather */
1744 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
1746 /* is all this really necessary? */
1747 ap->ops->dev_select(ap, 0);
1749 ap->ops->dev_select(ap, 1);
1751 ap->ops->dev_select(ap, 0);
1755 * ata_bus_edd - Issue EXECUTE DEVICE DIAGNOSTIC command.
1756 * @ap: Port to reset and probe
1758 * Use the EXECUTE DEVICE DIAGNOSTIC command to reset and
1759 * probe the bus. Not often used these days.
1762 * PCI/etc. bus probe sem.
1766 static unsigned int ata_bus_edd(struct ata_port *ap)
1768 struct ata_taskfile tf;
1770 /* set up execute-device-diag (bus reset) taskfile */
1771 /* also, take interrupts to a known state (disabled) */
1772 DPRINTK("execute-device-diag\n");
1773 ata_tf_init(ap, &tf, 0);
1775 tf.command = ATA_CMD_EDD;
1776 tf.protocol = ATA_PROT_NODATA;
1779 ata_tf_to_host(ap, &tf);
1781 /* spec says at least 2ms. but who knows with those
1782 * crazy ATAPI devices...
1786 return ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
1789 static unsigned int ata_bus_softreset(struct ata_port *ap,
1790 unsigned int devmask)
1792 struct ata_ioports *ioaddr = &ap->ioaddr;
1794 DPRINTK("ata%u: bus reset via SRST\n", ap->id);
1796 /* software reset. causes dev0 to be selected */
1797 if (ap->flags & ATA_FLAG_MMIO) {
1798 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
1799 udelay(20); /* FIXME: flush */
1800 writeb(ap->ctl | ATA_SRST, (void __iomem *) ioaddr->ctl_addr);
1801 udelay(20); /* FIXME: flush */
1802 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
1804 outb(ap->ctl, ioaddr->ctl_addr);
1806 outb(ap->ctl | ATA_SRST, ioaddr->ctl_addr);
1808 outb(ap->ctl, ioaddr->ctl_addr);
1811 /* spec mandates ">= 2ms" before checking status.
1812 * We wait 150ms, because that was the magic delay used for
1813 * ATAPI devices in Hale Landis's ATADRVR, for the period of time
1814 * between when the ATA command register is written, and then
1815 * status is checked. Because waiting for "a while" before
1816 * checking status is fine, post SRST, we perform this magic
1817 * delay here as well.
1821 ata_bus_post_reset(ap, devmask);
1827 * ata_bus_reset - reset host port and associated ATA channel
1828 * @ap: port to reset
1830 * This is typically the first time we actually start issuing
1831 * commands to the ATA channel. We wait for BSY to clear, then
1832 * issue EXECUTE DEVICE DIAGNOSTIC command, polling for its
1833 * result. Determine what devices, if any, are on the channel
1834 * by looking at the device 0/1 error register. Look at the signature
1835 * stored in each device's taskfile registers, to determine if
1836 * the device is ATA or ATAPI.
1839 * PCI/etc. bus probe sem.
1840 * Obtains host_set lock.
1843 * Sets ATA_FLAG_PORT_DISABLED if bus reset fails.
1846 void ata_bus_reset(struct ata_port *ap)
1848 struct ata_ioports *ioaddr = &ap->ioaddr;
1849 unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS;
1851 unsigned int dev0, dev1 = 0, rc = 0, devmask = 0;
1853 DPRINTK("ENTER, host %u, port %u\n", ap->id, ap->port_no);
1855 /* determine if device 0/1 are present */
1856 if (ap->flags & ATA_FLAG_SATA_RESET)
1859 dev0 = ata_devchk(ap, 0);
1861 dev1 = ata_devchk(ap, 1);
1865 devmask |= (1 << 0);
1867 devmask |= (1 << 1);
1869 /* select device 0 again */
1870 ap->ops->dev_select(ap, 0);
1872 /* issue bus reset */
1873 if (ap->flags & ATA_FLAG_SRST)
1874 rc = ata_bus_softreset(ap, devmask);
1875 else if ((ap->flags & ATA_FLAG_SATA_RESET) == 0) {
1876 /* set up device control */
1877 if (ap->flags & ATA_FLAG_MMIO)
1878 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
1880 outb(ap->ctl, ioaddr->ctl_addr);
1881 rc = ata_bus_edd(ap);
1888 * determine by signature whether we have ATA or ATAPI devices
1890 err = ata_dev_try_classify(ap, 0);
1891 if ((slave_possible) && (err != 0x81))
1892 ata_dev_try_classify(ap, 1);
1894 /* re-enable interrupts */
1895 if (ap->ioaddr.ctl_addr) /* FIXME: hack. create a hook instead */
1898 /* is double-select really necessary? */
1899 if (ap->device[1].class != ATA_DEV_NONE)
1900 ap->ops->dev_select(ap, 1);
1901 if (ap->device[0].class != ATA_DEV_NONE)
1902 ap->ops->dev_select(ap, 0);
1904 /* if no devices were detected, disable this port */
1905 if ((ap->device[0].class == ATA_DEV_NONE) &&
1906 (ap->device[1].class == ATA_DEV_NONE))
1909 if (ap->flags & (ATA_FLAG_SATA_RESET | ATA_FLAG_SRST)) {
1910 /* set up device control for ATA_FLAG_SATA_RESET */
1911 if (ap->flags & ATA_FLAG_MMIO)
1912 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
1914 outb(ap->ctl, ioaddr->ctl_addr);
1921 printk(KERN_ERR "ata%u: disabling port\n", ap->id);
1922 ap->ops->port_disable(ap);
1927 static void ata_pr_blacklisted(struct ata_port *ap, struct ata_device *dev)
1929 printk(KERN_WARNING "ata%u: dev %u is on DMA blacklist, disabling DMA\n",
1930 ap->id, dev->devno);
1933 static const char * ata_dma_blacklist [] = {
1952 "Toshiba CD-ROM XM-6202B",
1953 "TOSHIBA CD-ROM XM-1702BC",
1955 "E-IDE CD-ROM CR-840",
1958 "SAMSUNG CD-ROM SC-148C",
1959 "SAMSUNG CD-ROM SC",
1961 "ATAPI CD-ROM DRIVE 40X MAXIMUM",
1965 static int ata_dma_blacklisted(struct ata_port *ap, struct ata_device *dev)
1967 unsigned char model_num[40];
1972 ata_dev_id_string(dev->id, model_num, ATA_ID_PROD_OFS,
1975 len = strnlen(s, sizeof(model_num));
1977 /* ATAPI specifies that empty space is blank-filled; remove blanks */
1978 while ((len > 0) && (s[len - 1] == ' ')) {
1983 for (i = 0; i < ARRAY_SIZE(ata_dma_blacklist); i++)
1984 if (!strncmp(ata_dma_blacklist[i], s, len))
1990 static unsigned int ata_get_mode_mask(struct ata_port *ap, int shift)
1992 struct ata_device *master, *slave;
1995 master = &ap->device[0];
1996 slave = &ap->device[1];
1998 assert (ata_dev_present(master) || ata_dev_present(slave));
2000 if (shift == ATA_SHIFT_UDMA) {
2001 mask = ap->udma_mask;
2002 if (ata_dev_present(master)) {
2003 mask &= (master->id[ATA_ID_UDMA_MODES] & 0xff);
2004 if (ata_dma_blacklisted(ap, master)) {
2006 ata_pr_blacklisted(ap, master);
2009 if (ata_dev_present(slave)) {
2010 mask &= (slave->id[ATA_ID_UDMA_MODES] & 0xff);
2011 if (ata_dma_blacklisted(ap, slave)) {
2013 ata_pr_blacklisted(ap, slave);
2017 else if (shift == ATA_SHIFT_MWDMA) {
2018 mask = ap->mwdma_mask;
2019 if (ata_dev_present(master)) {
2020 mask &= (master->id[ATA_ID_MWDMA_MODES] & 0x07);
2021 if (ata_dma_blacklisted(ap, master)) {
2023 ata_pr_blacklisted(ap, master);
2026 if (ata_dev_present(slave)) {
2027 mask &= (slave->id[ATA_ID_MWDMA_MODES] & 0x07);
2028 if (ata_dma_blacklisted(ap, slave)) {
2030 ata_pr_blacklisted(ap, slave);
2034 else if (shift == ATA_SHIFT_PIO) {
2035 mask = ap->pio_mask;
2036 if (ata_dev_present(master)) {
2037 /* spec doesn't return explicit support for
2038 * PIO0-2, so we fake it
2040 u16 tmp_mode = master->id[ATA_ID_PIO_MODES] & 0x03;
2045 if (ata_dev_present(slave)) {
2046 /* spec doesn't return explicit support for
2047 * PIO0-2, so we fake it
2049 u16 tmp_mode = slave->id[ATA_ID_PIO_MODES] & 0x03;
2056 mask = 0xffffffff; /* shut up compiler warning */
2063 /* find greatest bit */
2064 static int fgb(u32 bitmap)
2069 for (i = 0; i < 32; i++)
2070 if (bitmap & (1 << i))
2077 * ata_choose_xfer_mode - attempt to find best transfer mode
2078 * @ap: Port for which an xfer mode will be selected
2079 * @xfer_mode_out: (output) SET FEATURES - XFER MODE code
2080 * @xfer_shift_out: (output) bit shift that selects this mode
2082 * Based on host and device capabilities, determine the
2083 * maximum transfer mode that is amenable to all.
2086 * PCI/etc. bus probe sem.
2089 * Zero on success, negative on error.
2092 static int ata_choose_xfer_mode(struct ata_port *ap,
2094 unsigned int *xfer_shift_out)
2096 unsigned int mask, shift;
2099 for (i = 0; i < ARRAY_SIZE(xfer_mode_classes); i++) {
2100 shift = xfer_mode_classes[i].shift;
2101 mask = ata_get_mode_mask(ap, shift);
2105 *xfer_mode_out = xfer_mode_classes[i].base + x;
2106 *xfer_shift_out = shift;
2115 * ata_dev_set_xfermode - Issue SET FEATURES - XFER MODE command
2116 * @ap: Port associated with device @dev
2117 * @dev: Device to which command will be sent
2119 * Issue SET FEATURES - XFER MODE command to device @dev
2123 * PCI/etc. bus probe sem.
2126 static void ata_dev_set_xfermode(struct ata_port *ap, struct ata_device *dev)
2128 DECLARE_COMPLETION(wait);
2129 struct ata_queued_cmd *qc;
2131 unsigned long flags;
2133 /* set up set-features taskfile */
2134 DPRINTK("set features - xfer mode\n");
2136 qc = ata_qc_new_init(ap, dev);
2139 qc->tf.command = ATA_CMD_SET_FEATURES;
2140 qc->tf.feature = SETFEATURES_XFER;
2141 qc->tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
2142 qc->tf.protocol = ATA_PROT_NODATA;
2143 qc->tf.nsect = dev->xfer_mode;
2145 qc->waiting = &wait;
2146 qc->complete_fn = ata_qc_complete_noop;
2148 spin_lock_irqsave(&ap->host_set->lock, flags);
2149 rc = ata_qc_issue(qc);
2150 spin_unlock_irqrestore(&ap->host_set->lock, flags);
2153 ata_port_disable(ap);
2155 wait_for_completion(&wait);
2161 * ata_dev_reread_id - Reread the device identify device info
2162 * @ap: port where the device is
2163 * @dev: device to reread the identify device info
2168 static void ata_dev_reread_id(struct ata_port *ap, struct ata_device *dev)
2170 DECLARE_COMPLETION(wait);
2171 struct ata_queued_cmd *qc;
2172 unsigned long flags;
2175 qc = ata_qc_new_init(ap, dev);
2178 ata_sg_init_one(qc, dev->id, sizeof(dev->id));
2179 qc->dma_dir = DMA_FROM_DEVICE;
2181 if (dev->class == ATA_DEV_ATA) {
2182 qc->tf.command = ATA_CMD_ID_ATA;
2183 DPRINTK("do ATA identify\n");
2185 qc->tf.command = ATA_CMD_ID_ATAPI;
2186 DPRINTK("do ATAPI identify\n");
2189 qc->tf.flags |= ATA_TFLAG_DEVICE;
2190 qc->tf.protocol = ATA_PROT_PIO;
2193 qc->waiting = &wait;
2194 qc->complete_fn = ata_qc_complete_noop;
2196 spin_lock_irqsave(&ap->host_set->lock, flags);
2197 rc = ata_qc_issue(qc);
2198 spin_unlock_irqrestore(&ap->host_set->lock, flags);
2203 wait_for_completion(&wait);
2205 swap_buf_le16(dev->id, ATA_ID_WORDS);
2213 ata_port_disable(ap);
2217 * ata_dev_init_params - Issue INIT DEV PARAMS command
2218 * @ap: Port associated with device @dev
2219 * @dev: Device to which command will be sent
2224 static void ata_dev_init_params(struct ata_port *ap, struct ata_device *dev)
2226 DECLARE_COMPLETION(wait);
2227 struct ata_queued_cmd *qc;
2229 unsigned long flags;
2230 u16 sectors = dev->id[6];
2231 u16 heads = dev->id[3];
2233 /* Number of sectors per track 1-255. Number of heads 1-16 */
2234 if (sectors < 1 || sectors > 255 || heads < 1 || heads > 16)
2237 /* set up init dev params taskfile */
2238 DPRINTK("init dev params \n");
2240 qc = ata_qc_new_init(ap, dev);
2243 qc->tf.command = ATA_CMD_INIT_DEV_PARAMS;
2244 qc->tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
2245 qc->tf.protocol = ATA_PROT_NODATA;
2246 qc->tf.nsect = sectors;
2247 qc->tf.device |= (heads - 1) & 0x0f; /* max head = num. of heads - 1 */
2249 qc->waiting = &wait;
2250 qc->complete_fn = ata_qc_complete_noop;
2252 spin_lock_irqsave(&ap->host_set->lock, flags);
2253 rc = ata_qc_issue(qc);
2254 spin_unlock_irqrestore(&ap->host_set->lock, flags);
2257 ata_port_disable(ap);
2259 wait_for_completion(&wait);
2265 * ata_sg_clean - Unmap DMA memory associated with command
2266 * @qc: Command containing DMA memory to be released
2268 * Unmap all mapped DMA memory associated with this command.
2271 * spin_lock_irqsave(host_set lock)
2274 static void ata_sg_clean(struct ata_queued_cmd *qc)
2276 struct ata_port *ap = qc->ap;
2277 struct scatterlist *sg = qc->sg;
2278 int dir = qc->dma_dir;
2280 assert(qc->flags & ATA_QCFLAG_DMAMAP);
2283 if (qc->flags & ATA_QCFLAG_SINGLE)
2284 assert(qc->n_elem == 1);
2286 DPRINTK("unmapping %u sg elements\n", qc->n_elem);
2288 if (qc->flags & ATA_QCFLAG_SG)
2289 dma_unmap_sg(ap->host_set->dev, sg, qc->n_elem, dir);
2291 dma_unmap_single(ap->host_set->dev, sg_dma_address(&sg[0]),
2292 sg_dma_len(&sg[0]), dir);
2294 qc->flags &= ~ATA_QCFLAG_DMAMAP;
2299 * ata_fill_sg - Fill PCI IDE PRD table
2300 * @qc: Metadata associated with taskfile to be transferred
2302 * Fill PCI IDE PRD (scatter-gather) table with segments
2303 * associated with the current disk command.
2306 * spin_lock_irqsave(host_set lock)
2309 static void ata_fill_sg(struct ata_queued_cmd *qc)
2311 struct scatterlist *sg = qc->sg;
2312 struct ata_port *ap = qc->ap;
2313 unsigned int idx, nelem;
2316 assert(qc->n_elem > 0);
2319 for (nelem = qc->n_elem; nelem; nelem--,sg++) {
2323 /* determine if physical DMA addr spans 64K boundary.
2324 * Note h/w doesn't support 64-bit, so we unconditionally
2325 * truncate dma_addr_t to u32.
2327 addr = (u32) sg_dma_address(sg);
2328 sg_len = sg_dma_len(sg);
2331 offset = addr & 0xffff;
2333 if ((offset + sg_len) > 0x10000)
2334 len = 0x10000 - offset;
2336 ap->prd[idx].addr = cpu_to_le32(addr);
2337 ap->prd[idx].flags_len = cpu_to_le32(len & 0xffff);
2338 VPRINTK("PRD[%u] = (0x%X, 0x%X)\n", idx, addr, len);
2347 ap->prd[idx - 1].flags_len |= cpu_to_le32(ATA_PRD_EOT);
2350 * ata_check_atapi_dma - Check whether ATAPI DMA can be supported
2351 * @qc: Metadata associated with taskfile to check
2353 * Allow low-level driver to filter ATA PACKET commands, returning
2354 * a status indicating whether or not it is OK to use DMA for the
2355 * supplied PACKET command.
2358 * spin_lock_irqsave(host_set lock)
2360 * RETURNS: 0 when ATAPI DMA can be used
2363 int ata_check_atapi_dma(struct ata_queued_cmd *qc)
2365 struct ata_port *ap = qc->ap;
2366 int rc = 0; /* Assume ATAPI DMA is OK by default */
2368 if (ap->ops->check_atapi_dma)
2369 rc = ap->ops->check_atapi_dma(qc);
2374 * ata_qc_prep - Prepare taskfile for submission
2375 * @qc: Metadata associated with taskfile to be prepared
2377 * Prepare ATA taskfile for submission.
2380 * spin_lock_irqsave(host_set lock)
2382 void ata_qc_prep(struct ata_queued_cmd *qc)
2384 if (!(qc->flags & ATA_QCFLAG_DMAMAP))
2391 * ata_sg_init_one - Associate command with memory buffer
2392 * @qc: Command to be associated
2393 * @buf: Memory buffer
2394 * @buflen: Length of memory buffer, in bytes.
2396 * Initialize the data-related elements of queued_cmd @qc
2397 * to point to a single memory buffer, @buf of byte length @buflen.
2400 * spin_lock_irqsave(host_set lock)
2403 void ata_sg_init_one(struct ata_queued_cmd *qc, void *buf, unsigned int buflen)
2405 struct scatterlist *sg;
2407 qc->flags |= ATA_QCFLAG_SINGLE;
2409 memset(&qc->sgent, 0, sizeof(qc->sgent));
2410 qc->sg = &qc->sgent;
2415 sg->page = virt_to_page(buf);
2416 sg->offset = (unsigned long) buf & ~PAGE_MASK;
2417 sg->length = buflen;
2421 * ata_sg_init - Associate command with scatter-gather table.
2422 * @qc: Command to be associated
2423 * @sg: Scatter-gather table.
2424 * @n_elem: Number of elements in s/g table.
2426 * Initialize the data-related elements of queued_cmd @qc
2427 * to point to a scatter-gather table @sg, containing @n_elem
2431 * spin_lock_irqsave(host_set lock)
2434 void ata_sg_init(struct ata_queued_cmd *qc, struct scatterlist *sg,
2435 unsigned int n_elem)
2437 qc->flags |= ATA_QCFLAG_SG;
2439 qc->n_elem = n_elem;
2443 * ata_sg_setup_one - DMA-map the memory buffer associated with a command.
2444 * @qc: Command with memory buffer to be mapped.
2446 * DMA-map the memory buffer associated with queued_cmd @qc.
2449 * spin_lock_irqsave(host_set lock)
2452 * Zero on success, negative on error.
2455 static int ata_sg_setup_one(struct ata_queued_cmd *qc)
2457 struct ata_port *ap = qc->ap;
2458 int dir = qc->dma_dir;
2459 struct scatterlist *sg = qc->sg;
2460 dma_addr_t dma_address;
2462 dma_address = dma_map_single(ap->host_set->dev, qc->buf_virt,
2464 if (dma_mapping_error(dma_address))
2467 sg_dma_address(sg) = dma_address;
2468 sg_dma_len(sg) = sg->length;
2470 DPRINTK("mapped buffer of %d bytes for %s\n", sg_dma_len(sg),
2471 qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
2477 * ata_sg_setup - DMA-map the scatter-gather table associated with a command.
2478 * @qc: Command with scatter-gather table to be mapped.
2480 * DMA-map the scatter-gather table associated with queued_cmd @qc.
2483 * spin_lock_irqsave(host_set lock)
2486 * Zero on success, negative on error.
2490 static int ata_sg_setup(struct ata_queued_cmd *qc)
2492 struct ata_port *ap = qc->ap;
2493 struct scatterlist *sg = qc->sg;
2496 VPRINTK("ENTER, ata%u\n", ap->id);
2497 assert(qc->flags & ATA_QCFLAG_SG);
2500 n_elem = dma_map_sg(ap->host_set->dev, sg, qc->n_elem, dir);
2504 DPRINTK("%d sg elements mapped\n", n_elem);
2506 qc->n_elem = n_elem;
2512 * ata_poll_qc_complete - turn irq back on and finish qc
2513 * @qc: Command to complete
2514 * @drv_stat: ATA status register content
2517 * None. (grabs host lock)
2520 void ata_poll_qc_complete(struct ata_queued_cmd *qc, u8 drv_stat)
2522 struct ata_port *ap = qc->ap;
2523 unsigned long flags;
2525 spin_lock_irqsave(&ap->host_set->lock, flags);
2526 ap->flags &= ~ATA_FLAG_NOINTR;
2528 ata_qc_complete(qc, drv_stat);
2529 spin_unlock_irqrestore(&ap->host_set->lock, flags);
2537 * None. (executing in kernel thread context)
2543 static unsigned long ata_pio_poll(struct ata_port *ap)
2546 unsigned int poll_state = HSM_ST_UNKNOWN;
2547 unsigned int reg_state = HSM_ST_UNKNOWN;
2548 const unsigned int tmout_state = HSM_ST_TMOUT;
2550 switch (ap->hsm_task_state) {
2553 poll_state = HSM_ST_POLL;
2557 case HSM_ST_LAST_POLL:
2558 poll_state = HSM_ST_LAST_POLL;
2559 reg_state = HSM_ST_LAST;
2566 status = ata_chk_status(ap);
2567 if (status & ATA_BUSY) {
2568 if (time_after(jiffies, ap->pio_task_timeout)) {
2569 ap->hsm_task_state = tmout_state;
2572 ap->hsm_task_state = poll_state;
2573 return ATA_SHORT_PAUSE;
2576 ap->hsm_task_state = reg_state;
2581 * ata_pio_complete -
2585 * None. (executing in kernel thread context)
2588 * Non-zero if qc completed, zero otherwise.
2591 static int ata_pio_complete (struct ata_port *ap)
2593 struct ata_queued_cmd *qc;
2597 * This is purely heuristic. This is a fast path. Sometimes when
2598 * we enter, BSY will be cleared in a chk-status or two. If not,
2599 * the drive is probably seeking or something. Snooze for a couple
2600 * msecs, then chk-status again. If still busy, fall back to
2601 * HSM_ST_POLL state.
2603 drv_stat = ata_busy_wait(ap, ATA_BUSY | ATA_DRQ, 10);
2604 if (drv_stat & (ATA_BUSY | ATA_DRQ)) {
2606 drv_stat = ata_busy_wait(ap, ATA_BUSY | ATA_DRQ, 10);
2607 if (drv_stat & (ATA_BUSY | ATA_DRQ)) {
2608 ap->hsm_task_state = HSM_ST_LAST_POLL;
2609 ap->pio_task_timeout = jiffies + ATA_TMOUT_PIO;
2614 drv_stat = ata_wait_idle(ap);
2615 if (!ata_ok(drv_stat)) {
2616 ap->hsm_task_state = HSM_ST_ERR;
2620 qc = ata_qc_from_tag(ap, ap->active_tag);
2623 ap->hsm_task_state = HSM_ST_IDLE;
2625 ata_poll_qc_complete(qc, drv_stat);
2627 /* another command may start at this point */
2635 * @buf: Buffer to swap
2636 * @buf_words: Number of 16-bit words in buffer.
2638 * Swap halves of 16-bit words if needed to convert from
2639 * little-endian byte order to native cpu byte order, or
2644 void swap_buf_le16(u16 *buf, unsigned int buf_words)
2649 for (i = 0; i < buf_words; i++)
2650 buf[i] = le16_to_cpu(buf[i]);
2651 #endif /* __BIG_ENDIAN */
2655 * ata_mmio_data_xfer - Transfer data by MMIO
2656 * @ap: port to read/write
2658 * @buflen: buffer length
2659 * @write_data: read/write
2661 * Transfer data from/to the device data register by MMIO.
2664 * Inherited from caller.
2668 static void ata_mmio_data_xfer(struct ata_port *ap, unsigned char *buf,
2669 unsigned int buflen, int write_data)
2672 unsigned int words = buflen >> 1;
2673 u16 *buf16 = (u16 *) buf;
2674 void __iomem *mmio = (void __iomem *)ap->ioaddr.data_addr;
2676 /* Transfer multiple of 2 bytes */
2678 for (i = 0; i < words; i++)
2679 writew(le16_to_cpu(buf16[i]), mmio);
2681 for (i = 0; i < words; i++)
2682 buf16[i] = cpu_to_le16(readw(mmio));
2685 /* Transfer trailing 1 byte, if any. */
2686 if (unlikely(buflen & 0x01)) {
2687 u16 align_buf[1] = { 0 };
2688 unsigned char *trailing_buf = buf + buflen - 1;
2691 memcpy(align_buf, trailing_buf, 1);
2692 writew(le16_to_cpu(align_buf[0]), mmio);
2694 align_buf[0] = cpu_to_le16(readw(mmio));
2695 memcpy(trailing_buf, align_buf, 1);
2701 * ata_pio_data_xfer - Transfer data by PIO
2702 * @ap: port to read/write
2704 * @buflen: buffer length
2705 * @write_data: read/write
2707 * Transfer data from/to the device data register by PIO.
2710 * Inherited from caller.
2714 static void ata_pio_data_xfer(struct ata_port *ap, unsigned char *buf,
2715 unsigned int buflen, int write_data)
2717 unsigned int words = buflen >> 1;
2719 /* Transfer multiple of 2 bytes */
2721 outsw(ap->ioaddr.data_addr, buf, words);
2723 insw(ap->ioaddr.data_addr, buf, words);
2725 /* Transfer trailing 1 byte, if any. */
2726 if (unlikely(buflen & 0x01)) {
2727 u16 align_buf[1] = { 0 };
2728 unsigned char *trailing_buf = buf + buflen - 1;
2731 memcpy(align_buf, trailing_buf, 1);
2732 outw(le16_to_cpu(align_buf[0]), ap->ioaddr.data_addr);
2734 align_buf[0] = cpu_to_le16(inw(ap->ioaddr.data_addr));
2735 memcpy(trailing_buf, align_buf, 1);
2741 * ata_data_xfer - Transfer data from/to the data register.
2742 * @ap: port to read/write
2744 * @buflen: buffer length
2745 * @do_write: read/write
2747 * Transfer data from/to the device data register.
2750 * Inherited from caller.
2754 static void ata_data_xfer(struct ata_port *ap, unsigned char *buf,
2755 unsigned int buflen, int do_write)
2757 if (ap->flags & ATA_FLAG_MMIO)
2758 ata_mmio_data_xfer(ap, buf, buflen, do_write);
2760 ata_pio_data_xfer(ap, buf, buflen, do_write);
2764 * ata_pio_sector - Transfer ATA_SECT_SIZE (512 bytes) of data.
2765 * @qc: Command on going
2767 * Transfer ATA_SECT_SIZE of data from/to the ATA device.
2770 * Inherited from caller.
2773 static void ata_pio_sector(struct ata_queued_cmd *qc)
2775 int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
2776 struct scatterlist *sg = qc->sg;
2777 struct ata_port *ap = qc->ap;
2779 unsigned int offset;
2782 if (qc->cursect == (qc->nsect - 1))
2783 ap->hsm_task_state = HSM_ST_LAST;
2785 page = sg[qc->cursg].page;
2786 offset = sg[qc->cursg].offset + qc->cursg_ofs * ATA_SECT_SIZE;
2788 /* get the current page and offset */
2789 page = nth_page(page, (offset >> PAGE_SHIFT));
2790 offset %= PAGE_SIZE;
2792 buf = kmap(page) + offset;
2797 if ((qc->cursg_ofs * ATA_SECT_SIZE) == (&sg[qc->cursg])->length) {
2802 DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
2804 /* do the actual data transfer */
2805 do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
2806 ata_data_xfer(ap, buf, ATA_SECT_SIZE, do_write);
2812 * __atapi_pio_bytes - Transfer data from/to the ATAPI device.
2813 * @qc: Command on going
2814 * @bytes: number of bytes
2816 * Transfer Transfer data from/to the ATAPI device.
2819 * Inherited from caller.
2823 static void __atapi_pio_bytes(struct ata_queued_cmd *qc, unsigned int bytes)
2825 int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
2826 struct scatterlist *sg = qc->sg;
2827 struct ata_port *ap = qc->ap;
2830 unsigned int offset, count;
2832 if (qc->curbytes + bytes >= qc->nbytes)
2833 ap->hsm_task_state = HSM_ST_LAST;
2836 if (unlikely(qc->cursg >= qc->n_elem)) {
2838 * The end of qc->sg is reached and the device expects
2839 * more data to transfer. In order not to overrun qc->sg
2840 * and fulfill length specified in the byte count register,
2841 * - for read case, discard trailing data from the device
2842 * - for write case, padding zero data to the device
2844 u16 pad_buf[1] = { 0 };
2845 unsigned int words = bytes >> 1;
2848 if (words) /* warning if bytes > 1 */
2849 printk(KERN_WARNING "ata%u: %u bytes trailing data\n",
2852 for (i = 0; i < words; i++)
2853 ata_data_xfer(ap, (unsigned char*)pad_buf, 2, do_write);
2855 ap->hsm_task_state = HSM_ST_LAST;
2859 sg = &qc->sg[qc->cursg];
2862 offset = sg->offset + qc->cursg_ofs;
2864 /* get the current page and offset */
2865 page = nth_page(page, (offset >> PAGE_SHIFT));
2866 offset %= PAGE_SIZE;
2868 /* don't overrun current sg */
2869 count = min(sg->length - qc->cursg_ofs, bytes);
2871 /* don't cross page boundaries */
2872 count = min(count, (unsigned int)PAGE_SIZE - offset);
2874 buf = kmap(page) + offset;
2877 qc->curbytes += count;
2878 qc->cursg_ofs += count;
2880 if (qc->cursg_ofs == sg->length) {
2885 DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
2887 /* do the actual data transfer */
2888 ata_data_xfer(ap, buf, count, do_write);
2897 * atapi_pio_bytes - Transfer data from/to the ATAPI device.
2898 * @qc: Command on going
2900 * Transfer Transfer data from/to the ATAPI device.
2903 * Inherited from caller.
2907 static void atapi_pio_bytes(struct ata_queued_cmd *qc)
2909 struct ata_port *ap = qc->ap;
2910 struct ata_device *dev = qc->dev;
2911 unsigned int ireason, bc_lo, bc_hi, bytes;
2912 int i_write, do_write = (qc->tf.flags & ATA_TFLAG_WRITE) ? 1 : 0;
2914 ap->ops->tf_read(ap, &qc->tf);
2915 ireason = qc->tf.nsect;
2916 bc_lo = qc->tf.lbam;
2917 bc_hi = qc->tf.lbah;
2918 bytes = (bc_hi << 8) | bc_lo;
2920 /* shall be cleared to zero, indicating xfer of data */
2921 if (ireason & (1 << 0))
2924 /* make sure transfer direction matches expected */
2925 i_write = ((ireason & (1 << 1)) == 0) ? 1 : 0;
2926 if (do_write != i_write)
2929 __atapi_pio_bytes(qc, bytes);
2934 printk(KERN_INFO "ata%u: dev %u: ATAPI check failed\n",
2935 ap->id, dev->devno);
2936 ap->hsm_task_state = HSM_ST_ERR;
2944 * None. (executing in kernel thread context)
2947 static void ata_pio_block(struct ata_port *ap)
2949 struct ata_queued_cmd *qc;
2953 * This is purely hueristic. This is a fast path.
2954 * Sometimes when we enter, BSY will be cleared in
2955 * a chk-status or two. If not, the drive is probably seeking
2956 * or something. Snooze for a couple msecs, then
2957 * chk-status again. If still busy, fall back to
2958 * HSM_ST_POLL state.
2960 status = ata_busy_wait(ap, ATA_BUSY, 5);
2961 if (status & ATA_BUSY) {
2963 status = ata_busy_wait(ap, ATA_BUSY, 10);
2964 if (status & ATA_BUSY) {
2965 ap->hsm_task_state = HSM_ST_POLL;
2966 ap->pio_task_timeout = jiffies + ATA_TMOUT_PIO;
2971 qc = ata_qc_from_tag(ap, ap->active_tag);
2974 if (is_atapi_taskfile(&qc->tf)) {
2975 /* no more data to transfer or unsupported ATAPI command */
2976 if ((status & ATA_DRQ) == 0) {
2977 ap->hsm_task_state = HSM_ST_LAST;
2981 atapi_pio_bytes(qc);
2983 /* handle BSY=0, DRQ=0 as error */
2984 if ((status & ATA_DRQ) == 0) {
2985 ap->hsm_task_state = HSM_ST_ERR;
2993 static void ata_pio_error(struct ata_port *ap)
2995 struct ata_queued_cmd *qc;
2998 qc = ata_qc_from_tag(ap, ap->active_tag);
3001 drv_stat = ata_chk_status(ap);
3002 printk(KERN_WARNING "ata%u: PIO error, drv_stat 0x%x\n",
3005 ap->hsm_task_state = HSM_ST_IDLE;
3007 ata_poll_qc_complete(qc, drv_stat | ATA_ERR);
3010 static void ata_pio_task(void *_data)
3012 struct ata_port *ap = _data;
3013 unsigned long timeout;
3020 switch (ap->hsm_task_state) {
3029 qc_completed = ata_pio_complete(ap);
3033 case HSM_ST_LAST_POLL:
3034 timeout = ata_pio_poll(ap);
3044 queue_delayed_work(ata_wq, &ap->pio_task, timeout);
3045 else if (!qc_completed)
3050 * ata_qc_timeout - Handle timeout of queued command
3051 * @qc: Command that timed out
3053 * Some part of the kernel (currently, only the SCSI layer)
3054 * has noticed that the active command on port @ap has not
3055 * completed after a specified length of time. Handle this
3056 * condition by disabling DMA (if necessary) and completing
3057 * transactions, with error if necessary.
3059 * This also handles the case of the "lost interrupt", where
3060 * for some reason (possibly hardware bug, possibly driver bug)
3061 * an interrupt was not delivered to the driver, even though the
3062 * transaction completed successfully.
3065 * Inherited from SCSI layer (none, can sleep)
3068 static void ata_qc_timeout(struct ata_queued_cmd *qc)
3070 struct ata_port *ap = qc->ap;
3071 struct ata_host_set *host_set = ap->host_set;
3072 struct ata_device *dev = qc->dev;
3073 u8 host_stat = 0, drv_stat;
3074 unsigned long flags;
3078 /* FIXME: doesn't this conflict with timeout handling? */
3079 if (qc->dev->class == ATA_DEV_ATAPI && qc->scsicmd) {
3080 struct scsi_cmnd *cmd = qc->scsicmd;
3082 if (!(cmd->eh_eflags & SCSI_EH_CANCEL_CMD)) {
3084 /* finish completing original command */
3085 spin_lock_irqsave(&host_set->lock, flags);
3086 __ata_qc_complete(qc);
3087 spin_unlock_irqrestore(&host_set->lock, flags);
3089 atapi_request_sense(ap, dev, cmd);
3091 cmd->result = (CHECK_CONDITION << 1) | (DID_OK << 16);
3092 scsi_finish_command(cmd);
3098 spin_lock_irqsave(&host_set->lock, flags);
3100 /* hack alert! We cannot use the supplied completion
3101 * function from inside the ->eh_strategy_handler() thread.
3102 * libata is the only user of ->eh_strategy_handler() in
3103 * any kernel, so the default scsi_done() assumes it is
3104 * not being called from the SCSI EH.
3106 qc->scsidone = scsi_finish_command;
3108 switch (qc->tf.protocol) {
3111 case ATA_PROT_ATAPI_DMA:
3112 host_stat = ap->ops->bmdma_status(ap);
3114 /* before we do anything else, clear DMA-Start bit */
3115 ap->ops->bmdma_stop(qc);
3121 drv_stat = ata_chk_status(ap);
3123 /* ack bmdma irq events */
3124 ap->ops->irq_clear(ap);
3126 printk(KERN_ERR "ata%u: command 0x%x timeout, stat 0x%x host_stat 0x%x\n",
3127 ap->id, qc->tf.command, drv_stat, host_stat);
3129 /* complete taskfile transaction */
3130 ata_qc_complete(qc, drv_stat);
3134 spin_unlock_irqrestore(&host_set->lock, flags);
3141 * ata_eng_timeout - Handle timeout of queued command
3142 * @ap: Port on which timed-out command is active
3144 * Some part of the kernel (currently, only the SCSI layer)
3145 * has noticed that the active command on port @ap has not
3146 * completed after a specified length of time. Handle this
3147 * condition by disabling DMA (if necessary) and completing
3148 * transactions, with error if necessary.
3150 * This also handles the case of the "lost interrupt", where
3151 * for some reason (possibly hardware bug, possibly driver bug)
3152 * an interrupt was not delivered to the driver, even though the
3153 * transaction completed successfully.
3156 * Inherited from SCSI layer (none, can sleep)
3159 void ata_eng_timeout(struct ata_port *ap)
3161 struct ata_queued_cmd *qc;
3165 qc = ata_qc_from_tag(ap, ap->active_tag);
3169 printk(KERN_ERR "ata%u: BUG: timeout without command\n",
3179 * ata_qc_new - Request an available ATA command, for queueing
3180 * @ap: Port associated with device @dev
3181 * @dev: Device from whom we request an available command structure
3187 static struct ata_queued_cmd *ata_qc_new(struct ata_port *ap)
3189 struct ata_queued_cmd *qc = NULL;
3192 for (i = 0; i < ATA_MAX_QUEUE; i++)
3193 if (!test_and_set_bit(i, &ap->qactive)) {
3194 qc = ata_qc_from_tag(ap, i);
3205 * ata_qc_new_init - Request an available ATA command, and initialize it
3206 * @ap: Port associated with device @dev
3207 * @dev: Device from whom we request an available command structure
3213 struct ata_queued_cmd *ata_qc_new_init(struct ata_port *ap,
3214 struct ata_device *dev)
3216 struct ata_queued_cmd *qc;
3218 qc = ata_qc_new(ap);
3225 qc->cursect = qc->cursg = qc->cursg_ofs = 0;
3227 qc->nbytes = qc->curbytes = 0;
3229 ata_tf_init(ap, &qc->tf, dev->devno);
3235 int ata_qc_complete_noop(struct ata_queued_cmd *qc, u8 drv_stat)
3240 static void __ata_qc_complete(struct ata_queued_cmd *qc)
3242 struct ata_port *ap = qc->ap;
3243 unsigned int tag, do_clear = 0;
3247 if (likely(ata_tag_valid(tag))) {
3248 if (tag == ap->active_tag)
3249 ap->active_tag = ATA_TAG_POISON;
3250 qc->tag = ATA_TAG_POISON;
3255 struct completion *waiting = qc->waiting;
3260 if (likely(do_clear))
3261 clear_bit(tag, &ap->qactive);
3265 * ata_qc_free - free unused ata_queued_cmd
3266 * @qc: Command to complete
3268 * Designed to free unused ata_queued_cmd object
3269 * in case something prevents using it.
3272 * spin_lock_irqsave(host_set lock)
3275 void ata_qc_free(struct ata_queued_cmd *qc)
3277 assert(qc != NULL); /* ata_qc_from_tag _might_ return NULL */
3278 assert(qc->waiting == NULL); /* nothing should be waiting */
3280 __ata_qc_complete(qc);
3284 * ata_qc_complete - Complete an active ATA command
3285 * @qc: Command to complete
3286 * @drv_stat: ATA Status register contents
3288 * Indicate to the mid and upper layers that an ATA
3289 * command has completed, with either an ok or not-ok status.
3292 * spin_lock_irqsave(host_set lock)
3296 void ata_qc_complete(struct ata_queued_cmd *qc, u8 drv_stat)
3300 assert(qc != NULL); /* ata_qc_from_tag _might_ return NULL */
3301 assert(qc->flags & ATA_QCFLAG_ACTIVE);
3303 if (likely(qc->flags & ATA_QCFLAG_DMAMAP))
3306 /* atapi: mark qc as inactive to prevent the interrupt handler
3307 * from completing the command twice later, before the error handler
3308 * is called. (when rc != 0 and atapi request sense is needed)
3310 qc->flags &= ~ATA_QCFLAG_ACTIVE;
3312 /* call completion callback */
3313 rc = qc->complete_fn(qc, drv_stat);
3315 /* if callback indicates not to complete command (non-zero),
3316 * return immediately
3321 __ata_qc_complete(qc);
3326 static inline int ata_should_dma_map(struct ata_queued_cmd *qc)
3328 struct ata_port *ap = qc->ap;
3330 switch (qc->tf.protocol) {
3332 case ATA_PROT_ATAPI_DMA:
3335 case ATA_PROT_ATAPI:
3337 case ATA_PROT_PIO_MULT:
3338 if (ap->flags & ATA_FLAG_PIO_DMA)
3351 * ata_qc_issue - issue taskfile to device
3352 * @qc: command to issue to device
3354 * Prepare an ATA command to submission to device.
3355 * This includes mapping the data into a DMA-able
3356 * area, filling in the S/G table, and finally
3357 * writing the taskfile to hardware, starting the command.
3360 * spin_lock_irqsave(host_set lock)
3363 * Zero on success, negative on error.
3366 int ata_qc_issue(struct ata_queued_cmd *qc)
3368 struct ata_port *ap = qc->ap;
3370 if (ata_should_dma_map(qc)) {
3371 if (qc->flags & ATA_QCFLAG_SG) {
3372 if (ata_sg_setup(qc))
3374 } else if (qc->flags & ATA_QCFLAG_SINGLE) {
3375 if (ata_sg_setup_one(qc))
3379 qc->flags &= ~ATA_QCFLAG_DMAMAP;
3382 ap->ops->qc_prep(qc);
3384 qc->ap->active_tag = qc->tag;
3385 qc->flags |= ATA_QCFLAG_ACTIVE;
3387 return ap->ops->qc_issue(qc);
3395 * ata_qc_issue_prot - issue taskfile to device in proto-dependent manner
3396 * @qc: command to issue to device
3398 * Using various libata functions and hooks, this function
3399 * starts an ATA command. ATA commands are grouped into
3400 * classes called "protocols", and issuing each type of protocol
3401 * is slightly different.
3403 * May be used as the qc_issue() entry in ata_port_operations.
3406 * spin_lock_irqsave(host_set lock)
3409 * Zero on success, negative on error.
3412 int ata_qc_issue_prot(struct ata_queued_cmd *qc)
3414 struct ata_port *ap = qc->ap;
3416 ata_dev_select(ap, qc->dev->devno, 1, 0);
3418 switch (qc->tf.protocol) {
3419 case ATA_PROT_NODATA:
3420 ata_tf_to_host_nolock(ap, &qc->tf);
3424 ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
3425 ap->ops->bmdma_setup(qc); /* set up bmdma */
3426 ap->ops->bmdma_start(qc); /* initiate bmdma */
3429 case ATA_PROT_PIO: /* load tf registers, initiate polling pio */
3430 ata_qc_set_polling(qc);
3431 ata_tf_to_host_nolock(ap, &qc->tf);
3432 ap->hsm_task_state = HSM_ST;
3433 queue_work(ata_wq, &ap->pio_task);
3436 case ATA_PROT_ATAPI:
3437 ata_qc_set_polling(qc);
3438 ata_tf_to_host_nolock(ap, &qc->tf);
3439 queue_work(ata_wq, &ap->packet_task);
3442 case ATA_PROT_ATAPI_NODATA:
3443 ap->flags |= ATA_FLAG_NOINTR;
3444 ata_tf_to_host_nolock(ap, &qc->tf);
3445 queue_work(ata_wq, &ap->packet_task);
3448 case ATA_PROT_ATAPI_DMA:
3449 ap->flags |= ATA_FLAG_NOINTR;
3450 ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
3451 ap->ops->bmdma_setup(qc); /* set up bmdma */
3452 queue_work(ata_wq, &ap->packet_task);
3464 * ata_bmdma_setup_mmio - Set up PCI IDE BMDMA transaction
3465 * @qc: Info associated with this ATA transaction.
3468 * spin_lock_irqsave(host_set lock)
3471 static void ata_bmdma_setup_mmio (struct ata_queued_cmd *qc)
3473 struct ata_port *ap = qc->ap;
3474 unsigned int rw = (qc->tf.flags & ATA_TFLAG_WRITE);
3476 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
3478 /* load PRD table addr. */
3479 mb(); /* make sure PRD table writes are visible to controller */
3480 writel(ap->prd_dma, mmio + ATA_DMA_TABLE_OFS);
3482 /* specify data direction, triple-check start bit is clear */
3483 dmactl = readb(mmio + ATA_DMA_CMD);
3484 dmactl &= ~(ATA_DMA_WR | ATA_DMA_START);
3486 dmactl |= ATA_DMA_WR;
3487 writeb(dmactl, mmio + ATA_DMA_CMD);
3489 /* issue r/w command */
3490 ap->ops->exec_command(ap, &qc->tf);
3494 * ata_bmdma_start_mmio - Start a PCI IDE BMDMA transaction
3495 * @qc: Info associated with this ATA transaction.
3498 * spin_lock_irqsave(host_set lock)
3501 static void ata_bmdma_start_mmio (struct ata_queued_cmd *qc)
3503 struct ata_port *ap = qc->ap;
3504 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
3507 /* start host DMA transaction */
3508 dmactl = readb(mmio + ATA_DMA_CMD);
3509 writeb(dmactl | ATA_DMA_START, mmio + ATA_DMA_CMD);
3511 /* Strictly, one may wish to issue a readb() here, to
3512 * flush the mmio write. However, control also passes
3513 * to the hardware at this point, and it will interrupt
3514 * us when we are to resume control. So, in effect,
3515 * we don't care when the mmio write flushes.
3516 * Further, a read of the DMA status register _immediately_
3517 * following the write may not be what certain flaky hardware
3518 * is expected, so I think it is best to not add a readb()
3519 * without first all the MMIO ATA cards/mobos.
3520 * Or maybe I'm just being paranoid.
3525 * ata_bmdma_setup_pio - Set up PCI IDE BMDMA transaction (PIO)
3526 * @qc: Info associated with this ATA transaction.
3529 * spin_lock_irqsave(host_set lock)
3532 static void ata_bmdma_setup_pio (struct ata_queued_cmd *qc)
3534 struct ata_port *ap = qc->ap;
3535 unsigned int rw = (qc->tf.flags & ATA_TFLAG_WRITE);
3538 /* load PRD table addr. */
3539 outl(ap->prd_dma, ap->ioaddr.bmdma_addr + ATA_DMA_TABLE_OFS);
3541 /* specify data direction, triple-check start bit is clear */
3542 dmactl = inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3543 dmactl &= ~(ATA_DMA_WR | ATA_DMA_START);
3545 dmactl |= ATA_DMA_WR;
3546 outb(dmactl, ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3548 /* issue r/w command */
3549 ap->ops->exec_command(ap, &qc->tf);
3553 * ata_bmdma_start_pio - Start a PCI IDE BMDMA transaction (PIO)
3554 * @qc: Info associated with this ATA transaction.
3557 * spin_lock_irqsave(host_set lock)
3560 static void ata_bmdma_start_pio (struct ata_queued_cmd *qc)
3562 struct ata_port *ap = qc->ap;
3565 /* start host DMA transaction */
3566 dmactl = inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3567 outb(dmactl | ATA_DMA_START,
3568 ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3573 * ata_bmdma_start - Start a PCI IDE BMDMA transaction
3574 * @qc: Info associated with this ATA transaction.
3576 * Writes the ATA_DMA_START flag to the DMA command register.
3578 * May be used as the bmdma_start() entry in ata_port_operations.
3581 * spin_lock_irqsave(host_set lock)
3583 void ata_bmdma_start(struct ata_queued_cmd *qc)
3585 if (qc->ap->flags & ATA_FLAG_MMIO)
3586 ata_bmdma_start_mmio(qc);
3588 ata_bmdma_start_pio(qc);
3593 * ata_bmdma_setup - Set up PCI IDE BMDMA transaction
3594 * @qc: Info associated with this ATA transaction.
3596 * Writes address of PRD table to device's PRD Table Address
3597 * register, sets the DMA control register, and calls
3598 * ops->exec_command() to start the transfer.
3600 * May be used as the bmdma_setup() entry in ata_port_operations.
3603 * spin_lock_irqsave(host_set lock)
3605 void ata_bmdma_setup(struct ata_queued_cmd *qc)
3607 if (qc->ap->flags & ATA_FLAG_MMIO)
3608 ata_bmdma_setup_mmio(qc);
3610 ata_bmdma_setup_pio(qc);
3615 * ata_bmdma_irq_clear - Clear PCI IDE BMDMA interrupt.
3616 * @ap: Port associated with this ATA transaction.
3618 * Clear interrupt and error flags in DMA status register.
3620 * May be used as the irq_clear() entry in ata_port_operations.
3623 * spin_lock_irqsave(host_set lock)
3626 void ata_bmdma_irq_clear(struct ata_port *ap)
3628 if (ap->flags & ATA_FLAG_MMIO) {
3629 void __iomem *mmio = ((void __iomem *) ap->ioaddr.bmdma_addr) + ATA_DMA_STATUS;
3630 writeb(readb(mmio), mmio);
3632 unsigned long addr = ap->ioaddr.bmdma_addr + ATA_DMA_STATUS;
3633 outb(inb(addr), addr);
3640 * ata_bmdma_status - Read PCI IDE BMDMA status
3641 * @ap: Port associated with this ATA transaction.
3643 * Read and return BMDMA status register.
3645 * May be used as the bmdma_status() entry in ata_port_operations.
3648 * spin_lock_irqsave(host_set lock)
3651 u8 ata_bmdma_status(struct ata_port *ap)
3654 if (ap->flags & ATA_FLAG_MMIO) {
3655 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
3656 host_stat = readb(mmio + ATA_DMA_STATUS);
3658 host_stat = inb(ap->ioaddr.bmdma_addr + ATA_DMA_STATUS);
3664 * ata_bmdma_stop - Stop PCI IDE BMDMA transfer
3665 * @qc: Command we are ending DMA for
3667 * Clears the ATA_DMA_START flag in the dma control register
3669 * May be used as the bmdma_stop() entry in ata_port_operations.
3672 * spin_lock_irqsave(host_set lock)
3675 void ata_bmdma_stop(struct ata_queued_cmd *qc)
3677 struct ata_port *ap = qc->ap;
3678 if (ap->flags & ATA_FLAG_MMIO) {
3679 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
3681 /* clear start/stop bit */
3682 writeb(readb(mmio + ATA_DMA_CMD) & ~ATA_DMA_START,
3683 mmio + ATA_DMA_CMD);
3685 /* clear start/stop bit */
3686 outb(inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD) & ~ATA_DMA_START,
3687 ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3690 /* one-PIO-cycle guaranteed wait, per spec, for HDMA1:0 transition */
3691 ata_altstatus(ap); /* dummy read */
3695 * ata_host_intr - Handle host interrupt for given (port, task)
3696 * @ap: Port on which interrupt arrived (possibly...)
3697 * @qc: Taskfile currently active in engine
3699 * Handle host interrupt for given queued command. Currently,
3700 * only DMA interrupts are handled. All other commands are
3701 * handled via polling with interrupts disabled (nIEN bit).
3704 * spin_lock_irqsave(host_set lock)
3707 * One if interrupt was handled, zero if not (shared irq).
3710 inline unsigned int ata_host_intr (struct ata_port *ap,
3711 struct ata_queued_cmd *qc)
3713 u8 status, host_stat;
3715 switch (qc->tf.protocol) {
3718 case ATA_PROT_ATAPI_DMA:
3719 case ATA_PROT_ATAPI:
3720 /* check status of DMA engine */
3721 host_stat = ap->ops->bmdma_status(ap);
3722 VPRINTK("ata%u: host_stat 0x%X\n", ap->id, host_stat);
3724 /* if it's not our irq... */
3725 if (!(host_stat & ATA_DMA_INTR))
3728 /* before we do anything else, clear DMA-Start bit */
3729 ap->ops->bmdma_stop(qc);
3733 case ATA_PROT_ATAPI_NODATA:
3734 case ATA_PROT_NODATA:
3735 /* check altstatus */
3736 status = ata_altstatus(ap);
3737 if (status & ATA_BUSY)
3740 /* check main status, clearing INTRQ */
3741 status = ata_chk_status(ap);
3742 if (unlikely(status & ATA_BUSY))
3744 DPRINTK("ata%u: protocol %d (dev_stat 0x%X)\n",
3745 ap->id, qc->tf.protocol, status);
3747 /* ack bmdma irq events */
3748 ap->ops->irq_clear(ap);
3750 /* complete taskfile transaction */
3751 ata_qc_complete(qc, status);
3758 return 1; /* irq handled */
3761 ap->stats.idle_irq++;
3764 if ((ap->stats.idle_irq % 1000) == 0) {
3766 ata_irq_ack(ap, 0); /* debug trap */
3767 printk(KERN_WARNING "ata%d: irq trap\n", ap->id);
3770 return 0; /* irq not handled */
3774 * ata_interrupt - Default ATA host interrupt handler
3775 * @irq: irq line (unused)
3776 * @dev_instance: pointer to our ata_host_set information structure
3779 * Default interrupt handler for PCI IDE devices. Calls
3780 * ata_host_intr() for each port that is not disabled.
3783 * Obtains host_set lock during operation.
3786 * IRQ_NONE or IRQ_HANDLED.
3790 irqreturn_t ata_interrupt (int irq, void *dev_instance, struct pt_regs *regs)
3792 struct ata_host_set *host_set = dev_instance;
3794 unsigned int handled = 0;
3795 unsigned long flags;
3797 /* TODO: make _irqsave conditional on x86 PCI IDE legacy mode */
3798 spin_lock_irqsave(&host_set->lock, flags);
3800 for (i = 0; i < host_set->n_ports; i++) {
3801 struct ata_port *ap;
3803 ap = host_set->ports[i];
3805 !(ap->flags & (ATA_FLAG_PORT_DISABLED | ATA_FLAG_NOINTR))) {
3806 struct ata_queued_cmd *qc;
3808 qc = ata_qc_from_tag(ap, ap->active_tag);
3809 if (qc && (!(qc->tf.ctl & ATA_NIEN)) &&
3810 (qc->flags & ATA_QCFLAG_ACTIVE))
3811 handled |= ata_host_intr(ap, qc);
3815 spin_unlock_irqrestore(&host_set->lock, flags);
3817 return IRQ_RETVAL(handled);
3821 * atapi_packet_task - Write CDB bytes to hardware
3822 * @_data: Port to which ATAPI device is attached.
3824 * When device has indicated its readiness to accept
3825 * a CDB, this function is called. Send the CDB.
3826 * If DMA is to be performed, exit immediately.
3827 * Otherwise, we are in polling mode, so poll
3828 * status under operation succeeds or fails.
3831 * Kernel thread context (may sleep)
3834 static void atapi_packet_task(void *_data)
3836 struct ata_port *ap = _data;
3837 struct ata_queued_cmd *qc;
3840 qc = ata_qc_from_tag(ap, ap->active_tag);
3842 assert(qc->flags & ATA_QCFLAG_ACTIVE);
3844 /* sleep-wait for BSY to clear */
3845 DPRINTK("busy wait\n");
3846 if (ata_busy_sleep(ap, ATA_TMOUT_CDB_QUICK, ATA_TMOUT_CDB))
3849 /* make sure DRQ is set */
3850 status = ata_chk_status(ap);
3851 if ((status & (ATA_BUSY | ATA_DRQ)) != ATA_DRQ)
3855 DPRINTK("send cdb\n");
3856 assert(ap->cdb_len >= 12);
3858 if (qc->tf.protocol == ATA_PROT_ATAPI_DMA ||
3859 qc->tf.protocol == ATA_PROT_ATAPI_NODATA) {
3860 unsigned long flags;
3862 /* Once we're done issuing command and kicking bmdma,
3863 * irq handler takes over. To not lose irq, we need
3864 * to clear NOINTR flag before sending cdb, but
3865 * interrupt handler shouldn't be invoked before we're
3866 * finished. Hence, the following locking.
3868 spin_lock_irqsave(&ap->host_set->lock, flags);
3869 ap->flags &= ~ATA_FLAG_NOINTR;
3870 ata_data_xfer(ap, qc->cdb, ap->cdb_len, 1);
3871 if (qc->tf.protocol == ATA_PROT_ATAPI_DMA)
3872 ap->ops->bmdma_start(qc); /* initiate bmdma */
3873 spin_unlock_irqrestore(&ap->host_set->lock, flags);
3875 ata_data_xfer(ap, qc->cdb, ap->cdb_len, 1);
3877 /* PIO commands are handled by polling */
3878 ap->hsm_task_state = HSM_ST;
3879 queue_work(ata_wq, &ap->pio_task);
3885 ata_poll_qc_complete(qc, ATA_ERR);
3890 * ata_port_start - Set port up for dma.
3891 * @ap: Port to initialize
3893 * Called just after data structures for each port are
3894 * initialized. Allocates space for PRD table.
3896 * May be used as the port_start() entry in ata_port_operations.
3901 int ata_port_start (struct ata_port *ap)
3903 struct device *dev = ap->host_set->dev;
3905 ap->prd = dma_alloc_coherent(dev, ATA_PRD_TBL_SZ, &ap->prd_dma, GFP_KERNEL);
3909 DPRINTK("prd alloc, virt %p, dma %llx\n", ap->prd, (unsigned long long) ap->prd_dma);
3916 * ata_port_stop - Undo ata_port_start()
3917 * @ap: Port to shut down
3919 * Frees the PRD table.
3921 * May be used as the port_stop() entry in ata_port_operations.
3926 void ata_port_stop (struct ata_port *ap)
3928 struct device *dev = ap->host_set->dev;
3930 dma_free_coherent(dev, ATA_PRD_TBL_SZ, ap->prd, ap->prd_dma);
3933 void ata_host_stop (struct ata_host_set *host_set)
3935 if (host_set->mmio_base)
3936 iounmap(host_set->mmio_base);
3941 * ata_host_remove - Unregister SCSI host structure with upper layers
3942 * @ap: Port to unregister
3943 * @do_unregister: 1 if we fully unregister, 0 to just stop the port
3948 static void ata_host_remove(struct ata_port *ap, unsigned int do_unregister)
3950 struct Scsi_Host *sh = ap->host;
3955 scsi_remove_host(sh);
3957 ap->ops->port_stop(ap);
3961 * ata_host_init - Initialize an ata_port structure
3962 * @ap: Structure to initialize
3963 * @host: associated SCSI mid-layer structure
3964 * @host_set: Collection of hosts to which @ap belongs
3965 * @ent: Probe information provided by low-level driver
3966 * @port_no: Port number associated with this ata_port
3968 * Initialize a new ata_port structure, and its associated
3972 * Inherited from caller.
3976 static void ata_host_init(struct ata_port *ap, struct Scsi_Host *host,
3977 struct ata_host_set *host_set,
3978 struct ata_probe_ent *ent, unsigned int port_no)
3984 host->max_channel = 1;
3985 host->unique_id = ata_unique_id++;
3986 host->max_cmd_len = 12;
3988 scsi_assign_lock(host, &host_set->lock);
3990 ap->flags = ATA_FLAG_PORT_DISABLED;
3991 ap->id = host->unique_id;
3993 ap->ctl = ATA_DEVCTL_OBS;
3994 ap->host_set = host_set;
3995 ap->port_no = port_no;
3997 ent->legacy_mode ? ent->hard_port_no : port_no;
3998 ap->pio_mask = ent->pio_mask;
3999 ap->mwdma_mask = ent->mwdma_mask;
4000 ap->udma_mask = ent->udma_mask;
4001 ap->flags |= ent->host_flags;
4002 ap->ops = ent->port_ops;
4003 ap->cbl = ATA_CBL_NONE;
4004 ap->active_tag = ATA_TAG_POISON;
4005 ap->last_ctl = 0xFF;
4007 INIT_WORK(&ap->packet_task, atapi_packet_task, ap);
4008 INIT_WORK(&ap->pio_task, ata_pio_task, ap);
4010 for (i = 0; i < ATA_MAX_DEVICES; i++)
4011 ap->device[i].devno = i;
4014 ap->stats.unhandled_irq = 1;
4015 ap->stats.idle_irq = 1;
4018 memcpy(&ap->ioaddr, &ent->port[port_no], sizeof(struct ata_ioports));
4022 * ata_host_add - Attach low-level ATA driver to system
4023 * @ent: Information provided by low-level driver
4024 * @host_set: Collections of ports to which we add
4025 * @port_no: Port number associated with this host
4027 * Attach low-level ATA driver to system.
4030 * PCI/etc. bus probe sem.
4033 * New ata_port on success, for NULL on error.
4037 static struct ata_port * ata_host_add(struct ata_probe_ent *ent,
4038 struct ata_host_set *host_set,
4039 unsigned int port_no)
4041 struct Scsi_Host *host;
4042 struct ata_port *ap;
4046 host = scsi_host_alloc(ent->sht, sizeof(struct ata_port));
4050 ap = (struct ata_port *) &host->hostdata[0];
4052 ata_host_init(ap, host, host_set, ent, port_no);
4054 rc = ap->ops->port_start(ap);
4061 scsi_host_put(host);
4066 * ata_device_add - Register hardware device with ATA and SCSI layers
4067 * @ent: Probe information describing hardware device to be registered
4069 * This function processes the information provided in the probe
4070 * information struct @ent, allocates the necessary ATA and SCSI
4071 * host information structures, initializes them, and registers
4072 * everything with requisite kernel subsystems.
4074 * This function requests irqs, probes the ATA bus, and probes
4078 * PCI/etc. bus probe sem.
4081 * Number of ports registered. Zero on error (no ports registered).
4085 int ata_device_add(struct ata_probe_ent *ent)
4087 unsigned int count = 0, i;
4088 struct device *dev = ent->dev;
4089 struct ata_host_set *host_set;
4092 /* alloc a container for our list of ATA ports (buses) */
4093 host_set = kmalloc(sizeof(struct ata_host_set) +
4094 (ent->n_ports * sizeof(void *)), GFP_KERNEL);
4097 memset(host_set, 0, sizeof(struct ata_host_set) + (ent->n_ports * sizeof(void *)));
4098 spin_lock_init(&host_set->lock);
4100 host_set->dev = dev;
4101 host_set->n_ports = ent->n_ports;
4102 host_set->irq = ent->irq;
4103 host_set->mmio_base = ent->mmio_base;
4104 host_set->private_data = ent->private_data;
4105 host_set->ops = ent->port_ops;
4107 /* register each port bound to this device */
4108 for (i = 0; i < ent->n_ports; i++) {
4109 struct ata_port *ap;
4110 unsigned long xfer_mode_mask;
4112 ap = ata_host_add(ent, host_set, i);
4116 host_set->ports[i] = ap;
4117 xfer_mode_mask =(ap->udma_mask << ATA_SHIFT_UDMA) |
4118 (ap->mwdma_mask << ATA_SHIFT_MWDMA) |
4119 (ap->pio_mask << ATA_SHIFT_PIO);
4121 /* print per-port info to dmesg */
4122 printk(KERN_INFO "ata%u: %cATA max %s cmd 0x%lX ctl 0x%lX "
4123 "bmdma 0x%lX irq %lu\n",
4125 ap->flags & ATA_FLAG_SATA ? 'S' : 'P',
4126 ata_mode_string(xfer_mode_mask),
4127 ap->ioaddr.cmd_addr,
4128 ap->ioaddr.ctl_addr,
4129 ap->ioaddr.bmdma_addr,
4133 host_set->ops->irq_clear(ap);
4142 /* obtain irq, that is shared between channels */
4143 if (request_irq(ent->irq, ent->port_ops->irq_handler, ent->irq_flags,
4144 DRV_NAME, host_set))
4147 /* perform each probe synchronously */
4148 DPRINTK("probe begin\n");
4149 for (i = 0; i < count; i++) {
4150 struct ata_port *ap;
4153 ap = host_set->ports[i];
4155 DPRINTK("ata%u: probe begin\n", ap->id);
4156 rc = ata_bus_probe(ap);
4157 DPRINTK("ata%u: probe end\n", ap->id);
4160 /* FIXME: do something useful here?
4161 * Current libata behavior will
4162 * tear down everything when
4163 * the module is removed
4164 * or the h/w is unplugged.
4168 rc = scsi_add_host(ap->host, dev);
4170 printk(KERN_ERR "ata%u: scsi_add_host failed\n",
4172 /* FIXME: do something useful here */
4173 /* FIXME: handle unconditional calls to
4174 * scsi_scan_host and ata_host_remove, below,
4180 /* probes are done, now scan each port's disk(s) */
4181 DPRINTK("probe begin\n");
4182 for (i = 0; i < count; i++) {
4183 struct ata_port *ap = host_set->ports[i];
4185 ata_scsi_scan_host(ap);
4188 dev_set_drvdata(dev, host_set);
4190 VPRINTK("EXIT, returning %u\n", ent->n_ports);
4191 return ent->n_ports; /* success */
4194 for (i = 0; i < count; i++) {
4195 ata_host_remove(host_set->ports[i], 1);
4196 scsi_host_put(host_set->ports[i]->host);
4199 VPRINTK("EXIT, returning 0\n");
4204 * ata_host_set_remove - PCI layer callback for device removal
4205 * @host_set: ATA host set that was removed
4207 * Unregister all objects associated with this host set. Free those
4211 * Inherited from calling layer (may sleep).
4215 void ata_host_set_remove(struct ata_host_set *host_set)
4217 struct ata_port *ap;
4220 for (i = 0; i < host_set->n_ports; i++) {
4221 ap = host_set->ports[i];
4222 scsi_remove_host(ap->host);
4225 free_irq(host_set->irq, host_set);
4227 for (i = 0; i < host_set->n_ports; i++) {
4228 ap = host_set->ports[i];
4230 ata_scsi_release(ap->host);
4232 if ((ap->flags & ATA_FLAG_NO_LEGACY) == 0) {
4233 struct ata_ioports *ioaddr = &ap->ioaddr;
4235 if (ioaddr->cmd_addr == 0x1f0)
4236 release_region(0x1f0, 8);
4237 else if (ioaddr->cmd_addr == 0x170)
4238 release_region(0x170, 8);
4241 scsi_host_put(ap->host);
4244 if (host_set->ops->host_stop)
4245 host_set->ops->host_stop(host_set);
4251 * ata_scsi_release - SCSI layer callback hook for host unload
4252 * @host: libata host to be unloaded
4254 * Performs all duties necessary to shut down a libata port...
4255 * Kill port kthread, disable port, and release resources.
4258 * Inherited from SCSI layer.
4264 int ata_scsi_release(struct Scsi_Host *host)
4266 struct ata_port *ap = (struct ata_port *) &host->hostdata[0];
4270 ap->ops->port_disable(ap);
4271 ata_host_remove(ap, 0);
4278 * ata_std_ports - initialize ioaddr with standard port offsets.
4279 * @ioaddr: IO address structure to be initialized
4281 * Utility function which initializes data_addr, error_addr,
4282 * feature_addr, nsect_addr, lbal_addr, lbam_addr, lbah_addr,
4283 * device_addr, status_addr, and command_addr to standard offsets
4284 * relative to cmd_addr.
4286 * Does not set ctl_addr, altstatus_addr, bmdma_addr, or scr_addr.
4289 void ata_std_ports(struct ata_ioports *ioaddr)
4291 ioaddr->data_addr = ioaddr->cmd_addr + ATA_REG_DATA;
4292 ioaddr->error_addr = ioaddr->cmd_addr + ATA_REG_ERR;
4293 ioaddr->feature_addr = ioaddr->cmd_addr + ATA_REG_FEATURE;
4294 ioaddr->nsect_addr = ioaddr->cmd_addr + ATA_REG_NSECT;
4295 ioaddr->lbal_addr = ioaddr->cmd_addr + ATA_REG_LBAL;
4296 ioaddr->lbam_addr = ioaddr->cmd_addr + ATA_REG_LBAM;
4297 ioaddr->lbah_addr = ioaddr->cmd_addr + ATA_REG_LBAH;
4298 ioaddr->device_addr = ioaddr->cmd_addr + ATA_REG_DEVICE;
4299 ioaddr->status_addr = ioaddr->cmd_addr + ATA_REG_STATUS;
4300 ioaddr->command_addr = ioaddr->cmd_addr + ATA_REG_CMD;
4303 static struct ata_probe_ent *
4304 ata_probe_ent_alloc(struct device *dev, struct ata_port_info *port)
4306 struct ata_probe_ent *probe_ent;
4308 probe_ent = kmalloc(sizeof(*probe_ent), GFP_KERNEL);
4310 printk(KERN_ERR DRV_NAME "(%s): out of memory\n",
4311 kobject_name(&(dev->kobj)));
4315 memset(probe_ent, 0, sizeof(*probe_ent));
4317 INIT_LIST_HEAD(&probe_ent->node);
4318 probe_ent->dev = dev;
4320 probe_ent->sht = port->sht;
4321 probe_ent->host_flags = port->host_flags;
4322 probe_ent->pio_mask = port->pio_mask;
4323 probe_ent->mwdma_mask = port->mwdma_mask;
4324 probe_ent->udma_mask = port->udma_mask;
4325 probe_ent->port_ops = port->port_ops;
4334 void ata_pci_host_stop (struct ata_host_set *host_set)
4336 struct pci_dev *pdev = to_pci_dev(host_set->dev);
4338 pci_iounmap(pdev, host_set->mmio_base);
4342 * ata_pci_init_native_mode - Initialize native-mode driver
4343 * @pdev: pci device to be initialized
4344 * @port: array[2] of pointers to port info structures.
4345 * @ports: bitmap of ports present
4347 * Utility function which allocates and initializes an
4348 * ata_probe_ent structure for a standard dual-port
4349 * PIO-based IDE controller. The returned ata_probe_ent
4350 * structure can be passed to ata_device_add(). The returned
4351 * ata_probe_ent structure should then be freed with kfree().
4353 * The caller need only pass the address of the primary port, the
4354 * secondary will be deduced automatically. If the device has non
4355 * standard secondary port mappings this function can be called twice,
4356 * once for each interface.
4359 struct ata_probe_ent *
4360 ata_pci_init_native_mode(struct pci_dev *pdev, struct ata_port_info **port, int ports)
4362 struct ata_probe_ent *probe_ent =
4363 ata_probe_ent_alloc(pci_dev_to_dev(pdev), port[0]);
4369 probe_ent->irq = pdev->irq;
4370 probe_ent->irq_flags = SA_SHIRQ;
4372 if (ports & ATA_PORT_PRIMARY) {
4373 probe_ent->port[p].cmd_addr = pci_resource_start(pdev, 0);
4374 probe_ent->port[p].altstatus_addr =
4375 probe_ent->port[p].ctl_addr =
4376 pci_resource_start(pdev, 1) | ATA_PCI_CTL_OFS;
4377 probe_ent->port[p].bmdma_addr = pci_resource_start(pdev, 4);
4378 ata_std_ports(&probe_ent->port[p]);
4382 if (ports & ATA_PORT_SECONDARY) {
4383 probe_ent->port[p].cmd_addr = pci_resource_start(pdev, 2);
4384 probe_ent->port[p].altstatus_addr =
4385 probe_ent->port[p].ctl_addr =
4386 pci_resource_start(pdev, 3) | ATA_PCI_CTL_OFS;
4387 probe_ent->port[p].bmdma_addr = pci_resource_start(pdev, 4) + 8;
4388 ata_std_ports(&probe_ent->port[p]);
4392 probe_ent->n_ports = p;
4396 static struct ata_probe_ent *ata_pci_init_legacy_port(struct pci_dev *pdev, struct ata_port_info **port, int port_num)
4398 struct ata_probe_ent *probe_ent;
4400 probe_ent = ata_probe_ent_alloc(pci_dev_to_dev(pdev), port[0]);
4405 probe_ent->legacy_mode = 1;
4406 probe_ent->n_ports = 1;
4407 probe_ent->hard_port_no = port_num;
4412 probe_ent->irq = 14;
4413 probe_ent->port[0].cmd_addr = 0x1f0;
4414 probe_ent->port[0].altstatus_addr =
4415 probe_ent->port[0].ctl_addr = 0x3f6;
4418 probe_ent->irq = 15;
4419 probe_ent->port[0].cmd_addr = 0x170;
4420 probe_ent->port[0].altstatus_addr =
4421 probe_ent->port[0].ctl_addr = 0x376;
4424 probe_ent->port[0].bmdma_addr = pci_resource_start(pdev, 4) + 8 * port_num;
4425 ata_std_ports(&probe_ent->port[0]);
4430 * ata_pci_init_one - Initialize/register PCI IDE host controller
4431 * @pdev: Controller to be initialized
4432 * @port_info: Information from low-level host driver
4433 * @n_ports: Number of ports attached to host controller
4435 * This is a helper function which can be called from a driver's
4436 * xxx_init_one() probe function if the hardware uses traditional
4437 * IDE taskfile registers.
4439 * This function calls pci_enable_device(), reserves its register
4440 * regions, sets the dma mask, enables bus master mode, and calls
4444 * Inherited from PCI layer (may sleep).
4447 * Zero on success, negative on errno-based value on error.
4451 int ata_pci_init_one (struct pci_dev *pdev, struct ata_port_info **port_info,
4452 unsigned int n_ports)
4454 struct ata_probe_ent *probe_ent = NULL, *probe_ent2 = NULL;
4455 struct ata_port_info *port[2];
4457 unsigned int legacy_mode = 0;
4458 int disable_dev_on_err = 1;
4463 port[0] = port_info[0];
4465 port[1] = port_info[1];
4469 if ((port[0]->host_flags & ATA_FLAG_NO_LEGACY) == 0
4470 && (pdev->class >> 8) == PCI_CLASS_STORAGE_IDE) {
4471 /* TODO: What if one channel is in native mode ... */
4472 pci_read_config_byte(pdev, PCI_CLASS_PROG, &tmp8);
4473 mask = (1 << 2) | (1 << 0);
4474 if ((tmp8 & mask) != mask)
4475 legacy_mode = (1 << 3);
4479 if ((!legacy_mode) && (n_ports > 2)) {
4480 printk(KERN_ERR "ata: BUG: native mode, n_ports > 2\n");
4485 /* FIXME: Really for ATA it isn't safe because the device may be
4486 multi-purpose and we want to leave it alone if it was already
4487 enabled. Secondly for shared use as Arjan says we want refcounting
4489 Checking dev->is_enabled is insufficient as this is not set at
4490 boot for the primary video which is BIOS enabled
4493 rc = pci_enable_device(pdev);
4497 rc = pci_request_regions(pdev, DRV_NAME);
4499 disable_dev_on_err = 0;
4503 /* FIXME: Should use platform specific mappers for legacy port ranges */
4505 if (!request_region(0x1f0, 8, "libata")) {
4506 struct resource *conflict, res;
4508 res.end = 0x1f0 + 8 - 1;
4509 conflict = ____request_resource(&ioport_resource, &res);
4510 if (!strcmp(conflict->name, "libata"))
4511 legacy_mode |= (1 << 0);
4513 disable_dev_on_err = 0;
4514 printk(KERN_WARNING "ata: 0x1f0 IDE port busy\n");
4517 legacy_mode |= (1 << 0);
4519 if (!request_region(0x170, 8, "libata")) {
4520 struct resource *conflict, res;
4522 res.end = 0x170 + 8 - 1;
4523 conflict = ____request_resource(&ioport_resource, &res);
4524 if (!strcmp(conflict->name, "libata"))
4525 legacy_mode |= (1 << 1);
4527 disable_dev_on_err = 0;
4528 printk(KERN_WARNING "ata: 0x170 IDE port busy\n");
4531 legacy_mode |= (1 << 1);
4534 /* we have legacy mode, but all ports are unavailable */
4535 if (legacy_mode == (1 << 3)) {
4537 goto err_out_regions;
4540 rc = pci_set_dma_mask(pdev, ATA_DMA_MASK);
4542 goto err_out_regions;
4543 rc = pci_set_consistent_dma_mask(pdev, ATA_DMA_MASK);
4545 goto err_out_regions;
4548 if (legacy_mode & (1 << 0))
4549 probe_ent = ata_pci_init_legacy_port(pdev, port, 0);
4550 if (legacy_mode & (1 << 1))
4551 probe_ent2 = ata_pci_init_legacy_port(pdev, port, 1);
4554 probe_ent = ata_pci_init_native_mode(pdev, port, ATA_PORT_PRIMARY | ATA_PORT_SECONDARY);
4556 probe_ent = ata_pci_init_native_mode(pdev, port, ATA_PORT_PRIMARY);
4558 if (!probe_ent && !probe_ent2) {
4560 goto err_out_regions;
4563 pci_set_master(pdev);
4565 /* FIXME: check ata_device_add return */
4567 if (legacy_mode & (1 << 0))
4568 ata_device_add(probe_ent);
4569 if (legacy_mode & (1 << 1))
4570 ata_device_add(probe_ent2);
4572 ata_device_add(probe_ent);
4580 if (legacy_mode & (1 << 0))
4581 release_region(0x1f0, 8);
4582 if (legacy_mode & (1 << 1))
4583 release_region(0x170, 8);
4584 pci_release_regions(pdev);
4586 if (disable_dev_on_err)
4587 pci_disable_device(pdev);
4592 * ata_pci_remove_one - PCI layer callback for device removal
4593 * @pdev: PCI device that was removed
4595 * PCI layer indicates to libata via this hook that
4596 * hot-unplug or module unload event has occured.
4597 * Handle this by unregistering all objects associated
4598 * with this PCI device. Free those objects. Then finally
4599 * release PCI resources and disable device.
4602 * Inherited from PCI layer (may sleep).
4605 void ata_pci_remove_one (struct pci_dev *pdev)
4607 struct device *dev = pci_dev_to_dev(pdev);
4608 struct ata_host_set *host_set = dev_get_drvdata(dev);
4610 ata_host_set_remove(host_set);
4611 pci_release_regions(pdev);
4612 pci_disable_device(pdev);
4613 dev_set_drvdata(dev, NULL);
4616 /* move to PCI subsystem */
4617 int pci_test_config_bits(struct pci_dev *pdev, struct pci_bits *bits)
4619 unsigned long tmp = 0;
4621 switch (bits->width) {
4624 pci_read_config_byte(pdev, bits->reg, &tmp8);
4630 pci_read_config_word(pdev, bits->reg, &tmp16);
4636 pci_read_config_dword(pdev, bits->reg, &tmp32);
4647 return (tmp == bits->val) ? 1 : 0;
4649 #endif /* CONFIG_PCI */
4652 static int __init ata_init(void)
4654 ata_wq = create_workqueue("ata");
4658 printk(KERN_DEBUG "libata version " DRV_VERSION " loaded.\n");
4662 static void __exit ata_exit(void)
4664 destroy_workqueue(ata_wq);
4667 module_init(ata_init);
4668 module_exit(ata_exit);
4670 static unsigned long ratelimit_time;
4671 static spinlock_t ata_ratelimit_lock = SPIN_LOCK_UNLOCKED;
4673 int ata_ratelimit(void)
4676 unsigned long flags;
4678 spin_lock_irqsave(&ata_ratelimit_lock, flags);
4680 if (time_after(jiffies, ratelimit_time)) {
4682 ratelimit_time = jiffies + (HZ/5);
4686 spin_unlock_irqrestore(&ata_ratelimit_lock, flags);
4692 * libata is essentially a library of internal helper functions for
4693 * low-level ATA host controller drivers. As such, the API/ABI is
4694 * likely to change as new drivers are added and updated.
4695 * Do not depend on ABI/API stability.
4698 EXPORT_SYMBOL_GPL(ata_std_bios_param);
4699 EXPORT_SYMBOL_GPL(ata_std_ports);
4700 EXPORT_SYMBOL_GPL(ata_device_add);
4701 EXPORT_SYMBOL_GPL(ata_host_set_remove);
4702 EXPORT_SYMBOL_GPL(ata_sg_init);
4703 EXPORT_SYMBOL_GPL(ata_sg_init_one);
4704 EXPORT_SYMBOL_GPL(ata_qc_complete);
4705 EXPORT_SYMBOL_GPL(ata_qc_issue_prot);
4706 EXPORT_SYMBOL_GPL(ata_eng_timeout);
4707 EXPORT_SYMBOL_GPL(ata_tf_load);
4708 EXPORT_SYMBOL_GPL(ata_tf_read);
4709 EXPORT_SYMBOL_GPL(ata_noop_dev_select);
4710 EXPORT_SYMBOL_GPL(ata_std_dev_select);
4711 EXPORT_SYMBOL_GPL(ata_tf_to_fis);
4712 EXPORT_SYMBOL_GPL(ata_tf_from_fis);
4713 EXPORT_SYMBOL_GPL(ata_check_status);
4714 EXPORT_SYMBOL_GPL(ata_altstatus);
4715 EXPORT_SYMBOL_GPL(ata_chk_err);
4716 EXPORT_SYMBOL_GPL(ata_exec_command);
4717 EXPORT_SYMBOL_GPL(ata_port_start);
4718 EXPORT_SYMBOL_GPL(ata_port_stop);
4719 EXPORT_SYMBOL_GPL(ata_host_stop);
4720 EXPORT_SYMBOL_GPL(ata_interrupt);
4721 EXPORT_SYMBOL_GPL(ata_qc_prep);
4722 EXPORT_SYMBOL_GPL(ata_bmdma_setup);
4723 EXPORT_SYMBOL_GPL(ata_bmdma_start);
4724 EXPORT_SYMBOL_GPL(ata_bmdma_irq_clear);
4725 EXPORT_SYMBOL_GPL(ata_bmdma_status);
4726 EXPORT_SYMBOL_GPL(ata_bmdma_stop);
4727 EXPORT_SYMBOL_GPL(ata_port_probe);
4728 EXPORT_SYMBOL_GPL(sata_phy_reset);
4729 EXPORT_SYMBOL_GPL(__sata_phy_reset);
4730 EXPORT_SYMBOL_GPL(ata_bus_reset);
4731 EXPORT_SYMBOL_GPL(ata_port_disable);
4732 EXPORT_SYMBOL_GPL(ata_ratelimit);
4733 EXPORT_SYMBOL_GPL(ata_scsi_ioctl);
4734 EXPORT_SYMBOL_GPL(ata_scsi_queuecmd);
4735 EXPORT_SYMBOL_GPL(ata_scsi_error);
4736 EXPORT_SYMBOL_GPL(ata_scsi_slave_config);
4737 EXPORT_SYMBOL_GPL(ata_scsi_release);
4738 EXPORT_SYMBOL_GPL(ata_host_intr);
4739 EXPORT_SYMBOL_GPL(ata_dev_classify);
4740 EXPORT_SYMBOL_GPL(ata_dev_id_string);
4741 EXPORT_SYMBOL_GPL(ata_dev_config);
4742 EXPORT_SYMBOL_GPL(ata_scsi_simulate);
4745 EXPORT_SYMBOL_GPL(pci_test_config_bits);
4746 EXPORT_SYMBOL_GPL(ata_pci_host_stop);
4747 EXPORT_SYMBOL_GPL(ata_pci_init_native_mode);
4748 EXPORT_SYMBOL_GPL(ata_pci_init_one);
4749 EXPORT_SYMBOL_GPL(ata_pci_remove_one);
4750 #endif /* CONFIG_PCI */