2 * libata-core.c - helper library for ATA
4 * Maintained by: Jeff Garzik <jgarzik@pobox.com>
5 * Please ALWAYS copy linux-ide@vger.kernel.org
8 * Copyright 2003-2004 Red Hat, Inc. All rights reserved.
9 * Copyright 2003-2004 Jeff Garzik
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2, or (at your option)
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; see the file COPYING. If not, write to
24 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
27 * libata documentation is available via 'make {ps|pdf}docs',
28 * as Documentation/DocBook/libata.*
30 * Hardware documentation available from http://www.t13.org/ and
31 * http://www.sata-io.org/
35 #include <linux/config.h>
36 #include <linux/kernel.h>
37 #include <linux/module.h>
38 #include <linux/pci.h>
39 #include <linux/init.h>
40 #include <linux/list.h>
42 #include <linux/highmem.h>
43 #include <linux/spinlock.h>
44 #include <linux/blkdev.h>
45 #include <linux/delay.h>
46 #include <linux/timer.h>
47 #include <linux/interrupt.h>
48 #include <linux/completion.h>
49 #include <linux/suspend.h>
50 #include <linux/workqueue.h>
51 #include <scsi/scsi.h>
53 #include "scsi_priv.h"
54 #include <scsi/scsi_host.h>
55 #include <linux/libata.h>
57 #include <asm/semaphore.h>
58 #include <asm/byteorder.h>
62 static unsigned int ata_busy_sleep (struct ata_port *ap,
63 unsigned long tmout_pat,
65 static void ata_set_mode(struct ata_port *ap);
66 static void ata_dev_set_xfermode(struct ata_port *ap, struct ata_device *dev);
67 static unsigned int ata_get_mode_mask(struct ata_port *ap, int shift);
68 static int fgb(u32 bitmap);
69 static int ata_choose_xfer_mode(struct ata_port *ap,
71 unsigned int *xfer_shift_out);
72 static int ata_qc_complete_noop(struct ata_queued_cmd *qc, u8 drv_stat);
73 static void __ata_qc_complete(struct ata_queued_cmd *qc);
75 static unsigned int ata_unique_id = 1;
76 static struct workqueue_struct *ata_wq;
78 int atapi_enabled = 0;
79 module_param(atapi_enabled, int, 0444);
80 MODULE_PARM_DESC(atapi_enabled, "Enable discovery of ATAPI devices (0=off, 1=on)");
82 MODULE_AUTHOR("Jeff Garzik");
83 MODULE_DESCRIPTION("Library module for ATA devices");
84 MODULE_LICENSE("GPL");
85 MODULE_VERSION(DRV_VERSION);
88 * ata_tf_load - send taskfile registers to host controller
89 * @ap: Port to which output is sent
90 * @tf: ATA taskfile register set
92 * Outputs ATA taskfile to standard ATA host controller.
95 * Inherited from caller.
98 static void ata_tf_load_pio(struct ata_port *ap, struct ata_taskfile *tf)
100 struct ata_ioports *ioaddr = &ap->ioaddr;
101 unsigned int is_addr = tf->flags & ATA_TFLAG_ISADDR;
103 if (tf->ctl != ap->last_ctl) {
104 outb(tf->ctl, ioaddr->ctl_addr);
105 ap->last_ctl = tf->ctl;
109 if (is_addr && (tf->flags & ATA_TFLAG_LBA48)) {
110 outb(tf->hob_feature, ioaddr->feature_addr);
111 outb(tf->hob_nsect, ioaddr->nsect_addr);
112 outb(tf->hob_lbal, ioaddr->lbal_addr);
113 outb(tf->hob_lbam, ioaddr->lbam_addr);
114 outb(tf->hob_lbah, ioaddr->lbah_addr);
115 VPRINTK("hob: feat 0x%X nsect 0x%X, lba 0x%X 0x%X 0x%X\n",
124 outb(tf->feature, ioaddr->feature_addr);
125 outb(tf->nsect, ioaddr->nsect_addr);
126 outb(tf->lbal, ioaddr->lbal_addr);
127 outb(tf->lbam, ioaddr->lbam_addr);
128 outb(tf->lbah, ioaddr->lbah_addr);
129 VPRINTK("feat 0x%X nsect 0x%X lba 0x%X 0x%X 0x%X\n",
137 if (tf->flags & ATA_TFLAG_DEVICE) {
138 outb(tf->device, ioaddr->device_addr);
139 VPRINTK("device 0x%X\n", tf->device);
146 * ata_tf_load_mmio - send taskfile registers to host controller
147 * @ap: Port to which output is sent
148 * @tf: ATA taskfile register set
150 * Outputs ATA taskfile to standard ATA host controller using MMIO.
153 * Inherited from caller.
156 static void ata_tf_load_mmio(struct ata_port *ap, struct ata_taskfile *tf)
158 struct ata_ioports *ioaddr = &ap->ioaddr;
159 unsigned int is_addr = tf->flags & ATA_TFLAG_ISADDR;
161 if (tf->ctl != ap->last_ctl) {
162 writeb(tf->ctl, (void __iomem *) ap->ioaddr.ctl_addr);
163 ap->last_ctl = tf->ctl;
167 if (is_addr && (tf->flags & ATA_TFLAG_LBA48)) {
168 writeb(tf->hob_feature, (void __iomem *) ioaddr->feature_addr);
169 writeb(tf->hob_nsect, (void __iomem *) ioaddr->nsect_addr);
170 writeb(tf->hob_lbal, (void __iomem *) ioaddr->lbal_addr);
171 writeb(tf->hob_lbam, (void __iomem *) ioaddr->lbam_addr);
172 writeb(tf->hob_lbah, (void __iomem *) ioaddr->lbah_addr);
173 VPRINTK("hob: feat 0x%X nsect 0x%X, lba 0x%X 0x%X 0x%X\n",
182 writeb(tf->feature, (void __iomem *) ioaddr->feature_addr);
183 writeb(tf->nsect, (void __iomem *) ioaddr->nsect_addr);
184 writeb(tf->lbal, (void __iomem *) ioaddr->lbal_addr);
185 writeb(tf->lbam, (void __iomem *) ioaddr->lbam_addr);
186 writeb(tf->lbah, (void __iomem *) ioaddr->lbah_addr);
187 VPRINTK("feat 0x%X nsect 0x%X lba 0x%X 0x%X 0x%X\n",
195 if (tf->flags & ATA_TFLAG_DEVICE) {
196 writeb(tf->device, (void __iomem *) ioaddr->device_addr);
197 VPRINTK("device 0x%X\n", tf->device);
205 * ata_tf_load - send taskfile registers to host controller
206 * @ap: Port to which output is sent
207 * @tf: ATA taskfile register set
209 * Outputs ATA taskfile to standard ATA host controller using MMIO
210 * or PIO as indicated by the ATA_FLAG_MMIO flag.
211 * Writes the control, feature, nsect, lbal, lbam, and lbah registers.
212 * Optionally (ATA_TFLAG_LBA48) writes hob_feature, hob_nsect,
213 * hob_lbal, hob_lbam, and hob_lbah.
215 * This function waits for idle (!BUSY and !DRQ) after writing
216 * registers. If the control register has a new value, this
217 * function also waits for idle after writing control and before
218 * writing the remaining registers.
220 * May be used as the tf_load() entry in ata_port_operations.
223 * Inherited from caller.
225 void ata_tf_load(struct ata_port *ap, struct ata_taskfile *tf)
227 if (ap->flags & ATA_FLAG_MMIO)
228 ata_tf_load_mmio(ap, tf);
230 ata_tf_load_pio(ap, tf);
234 * ata_exec_command_pio - issue ATA command to host controller
235 * @ap: port to which command is being issued
236 * @tf: ATA taskfile register set
238 * Issues PIO write to ATA command register, with proper
239 * synchronization with interrupt handler / other threads.
242 * spin_lock_irqsave(host_set lock)
245 static void ata_exec_command_pio(struct ata_port *ap, struct ata_taskfile *tf)
247 DPRINTK("ata%u: cmd 0x%X\n", ap->id, tf->command);
249 outb(tf->command, ap->ioaddr.command_addr);
255 * ata_exec_command_mmio - issue ATA command to host controller
256 * @ap: port to which command is being issued
257 * @tf: ATA taskfile register set
259 * Issues MMIO write to ATA command register, with proper
260 * synchronization with interrupt handler / other threads.
263 * spin_lock_irqsave(host_set lock)
266 static void ata_exec_command_mmio(struct ata_port *ap, struct ata_taskfile *tf)
268 DPRINTK("ata%u: cmd 0x%X\n", ap->id, tf->command);
270 writeb(tf->command, (void __iomem *) ap->ioaddr.command_addr);
276 * ata_exec_command - issue ATA command to host controller
277 * @ap: port to which command is being issued
278 * @tf: ATA taskfile register set
280 * Issues PIO/MMIO write to ATA command register, with proper
281 * synchronization with interrupt handler / other threads.
284 * spin_lock_irqsave(host_set lock)
286 void ata_exec_command(struct ata_port *ap, struct ata_taskfile *tf)
288 if (ap->flags & ATA_FLAG_MMIO)
289 ata_exec_command_mmio(ap, tf);
291 ata_exec_command_pio(ap, tf);
295 * ata_exec - issue ATA command to host controller
296 * @ap: port to which command is being issued
297 * @tf: ATA taskfile register set
299 * Issues PIO/MMIO write to ATA command register, with proper
300 * synchronization with interrupt handler / other threads.
303 * Obtains host_set lock.
306 static inline void ata_exec(struct ata_port *ap, struct ata_taskfile *tf)
310 DPRINTK("ata%u: cmd 0x%X\n", ap->id, tf->command);
311 spin_lock_irqsave(&ap->host_set->lock, flags);
312 ap->ops->exec_command(ap, tf);
313 spin_unlock_irqrestore(&ap->host_set->lock, flags);
317 * ata_tf_to_host - issue ATA taskfile to host controller
318 * @ap: port to which command is being issued
319 * @tf: ATA taskfile register set
321 * Issues ATA taskfile register set to ATA host controller,
322 * with proper synchronization with interrupt handler and
326 * Obtains host_set lock.
329 static void ata_tf_to_host(struct ata_port *ap, struct ata_taskfile *tf)
331 ap->ops->tf_load(ap, tf);
337 * ata_tf_to_host_nolock - issue ATA taskfile to host controller
338 * @ap: port to which command is being issued
339 * @tf: ATA taskfile register set
341 * Issues ATA taskfile register set to ATA host controller,
342 * with proper synchronization with interrupt handler and
346 * spin_lock_irqsave(host_set lock)
349 void ata_tf_to_host_nolock(struct ata_port *ap, struct ata_taskfile *tf)
351 ap->ops->tf_load(ap, tf);
352 ap->ops->exec_command(ap, tf);
356 * ata_tf_read_pio - input device's ATA taskfile shadow registers
357 * @ap: Port from which input is read
358 * @tf: ATA taskfile register set for storing input
360 * Reads ATA taskfile registers for currently-selected device
364 * Inherited from caller.
367 static void ata_tf_read_pio(struct ata_port *ap, struct ata_taskfile *tf)
369 struct ata_ioports *ioaddr = &ap->ioaddr;
371 tf->nsect = inb(ioaddr->nsect_addr);
372 tf->lbal = inb(ioaddr->lbal_addr);
373 tf->lbam = inb(ioaddr->lbam_addr);
374 tf->lbah = inb(ioaddr->lbah_addr);
375 tf->device = inb(ioaddr->device_addr);
377 if (tf->flags & ATA_TFLAG_LBA48) {
378 outb(tf->ctl | ATA_HOB, ioaddr->ctl_addr);
379 tf->hob_feature = inb(ioaddr->error_addr);
380 tf->hob_nsect = inb(ioaddr->nsect_addr);
381 tf->hob_lbal = inb(ioaddr->lbal_addr);
382 tf->hob_lbam = inb(ioaddr->lbam_addr);
383 tf->hob_lbah = inb(ioaddr->lbah_addr);
388 * ata_tf_read_mmio - input device's ATA taskfile shadow registers
389 * @ap: Port from which input is read
390 * @tf: ATA taskfile register set for storing input
392 * Reads ATA taskfile registers for currently-selected device
396 * Inherited from caller.
399 static void ata_tf_read_mmio(struct ata_port *ap, struct ata_taskfile *tf)
401 struct ata_ioports *ioaddr = &ap->ioaddr;
403 tf->nsect = readb((void __iomem *)ioaddr->nsect_addr);
404 tf->lbal = readb((void __iomem *)ioaddr->lbal_addr);
405 tf->lbam = readb((void __iomem *)ioaddr->lbam_addr);
406 tf->lbah = readb((void __iomem *)ioaddr->lbah_addr);
407 tf->device = readb((void __iomem *)ioaddr->device_addr);
409 if (tf->flags & ATA_TFLAG_LBA48) {
410 writeb(tf->ctl | ATA_HOB, (void __iomem *) ap->ioaddr.ctl_addr);
411 tf->hob_feature = readb((void __iomem *)ioaddr->error_addr);
412 tf->hob_nsect = readb((void __iomem *)ioaddr->nsect_addr);
413 tf->hob_lbal = readb((void __iomem *)ioaddr->lbal_addr);
414 tf->hob_lbam = readb((void __iomem *)ioaddr->lbam_addr);
415 tf->hob_lbah = readb((void __iomem *)ioaddr->lbah_addr);
421 * ata_tf_read - input device's ATA taskfile shadow registers
422 * @ap: Port from which input is read
423 * @tf: ATA taskfile register set for storing input
425 * Reads ATA taskfile registers for currently-selected device
428 * Reads nsect, lbal, lbam, lbah, and device. If ATA_TFLAG_LBA48
429 * is set, also reads the hob registers.
431 * May be used as the tf_read() entry in ata_port_operations.
434 * Inherited from caller.
436 void ata_tf_read(struct ata_port *ap, struct ata_taskfile *tf)
438 if (ap->flags & ATA_FLAG_MMIO)
439 ata_tf_read_mmio(ap, tf);
441 ata_tf_read_pio(ap, tf);
445 * ata_check_status_pio - Read device status reg & clear interrupt
446 * @ap: port where the device is
448 * Reads ATA taskfile status register for currently-selected device
449 * and return its value. This also clears pending interrupts
453 * Inherited from caller.
455 static u8 ata_check_status_pio(struct ata_port *ap)
457 return inb(ap->ioaddr.status_addr);
461 * ata_check_status_mmio - Read device status reg & clear interrupt
462 * @ap: port where the device is
464 * Reads ATA taskfile status register for currently-selected device
465 * via MMIO and return its value. This also clears pending interrupts
469 * Inherited from caller.
471 static u8 ata_check_status_mmio(struct ata_port *ap)
473 return readb((void __iomem *) ap->ioaddr.status_addr);
478 * ata_check_status - Read device status reg & clear interrupt
479 * @ap: port where the device is
481 * Reads ATA taskfile status register for currently-selected device
482 * and return its value. This also clears pending interrupts
485 * May be used as the check_status() entry in ata_port_operations.
488 * Inherited from caller.
490 u8 ata_check_status(struct ata_port *ap)
492 if (ap->flags & ATA_FLAG_MMIO)
493 return ata_check_status_mmio(ap);
494 return ata_check_status_pio(ap);
499 * ata_altstatus - Read device alternate status reg
500 * @ap: port where the device is
502 * Reads ATA taskfile alternate status register for
503 * currently-selected device and return its value.
505 * Note: may NOT be used as the check_altstatus() entry in
506 * ata_port_operations.
509 * Inherited from caller.
511 u8 ata_altstatus(struct ata_port *ap)
513 if (ap->ops->check_altstatus)
514 return ap->ops->check_altstatus(ap);
516 if (ap->flags & ATA_FLAG_MMIO)
517 return readb((void __iomem *)ap->ioaddr.altstatus_addr);
518 return inb(ap->ioaddr.altstatus_addr);
523 * ata_chk_err - Read device error reg
524 * @ap: port where the device is
526 * Reads ATA taskfile error register for
527 * currently-selected device and return its value.
529 * Note: may NOT be used as the check_err() entry in
530 * ata_port_operations.
533 * Inherited from caller.
535 u8 ata_chk_err(struct ata_port *ap)
537 if (ap->ops->check_err)
538 return ap->ops->check_err(ap);
540 if (ap->flags & ATA_FLAG_MMIO) {
541 return readb((void __iomem *) ap->ioaddr.error_addr);
543 return inb(ap->ioaddr.error_addr);
547 * ata_tf_to_fis - Convert ATA taskfile to SATA FIS structure
548 * @tf: Taskfile to convert
549 * @fis: Buffer into which data will output
550 * @pmp: Port multiplier port
552 * Converts a standard ATA taskfile to a Serial ATA
553 * FIS structure (Register - Host to Device).
556 * Inherited from caller.
559 void ata_tf_to_fis(struct ata_taskfile *tf, u8 *fis, u8 pmp)
561 fis[0] = 0x27; /* Register - Host to Device FIS */
562 fis[1] = (pmp & 0xf) | (1 << 7); /* Port multiplier number,
563 bit 7 indicates Command FIS */
564 fis[2] = tf->command;
565 fis[3] = tf->feature;
572 fis[8] = tf->hob_lbal;
573 fis[9] = tf->hob_lbam;
574 fis[10] = tf->hob_lbah;
575 fis[11] = tf->hob_feature;
578 fis[13] = tf->hob_nsect;
589 * ata_tf_from_fis - Convert SATA FIS to ATA taskfile
590 * @fis: Buffer from which data will be input
591 * @tf: Taskfile to output
593 * Converts a standard ATA taskfile to a Serial ATA
594 * FIS structure (Register - Host to Device).
597 * Inherited from caller.
600 void ata_tf_from_fis(u8 *fis, struct ata_taskfile *tf)
602 tf->command = fis[2]; /* status */
603 tf->feature = fis[3]; /* error */
610 tf->hob_lbal = fis[8];
611 tf->hob_lbam = fis[9];
612 tf->hob_lbah = fis[10];
615 tf->hob_nsect = fis[13];
619 * ata_prot_to_cmd - determine which read/write opcodes to use
620 * @protocol: ATA_PROT_xxx taskfile protocol
621 * @lba48: true is lba48 is present
623 * Given necessary input, determine which read/write commands
624 * to use to transfer data.
629 static int ata_prot_to_cmd(int protocol, int lba48)
631 int rcmd = 0, wcmd = 0;
636 rcmd = ATA_CMD_PIO_READ_EXT;
637 wcmd = ATA_CMD_PIO_WRITE_EXT;
639 rcmd = ATA_CMD_PIO_READ;
640 wcmd = ATA_CMD_PIO_WRITE;
646 rcmd = ATA_CMD_READ_EXT;
647 wcmd = ATA_CMD_WRITE_EXT;
650 wcmd = ATA_CMD_WRITE;
658 return rcmd | (wcmd << 8);
662 * ata_dev_set_protocol - set taskfile protocol and r/w commands
663 * @dev: device to examine and configure
665 * Examine the device configuration, after we have
666 * read the identify-device page and configured the
667 * data transfer mode. Set internal state related to
668 * the ATA taskfile protocol (pio, pio mult, dma, etc.)
669 * and calculate the proper read/write commands to use.
674 static void ata_dev_set_protocol(struct ata_device *dev)
676 int pio = (dev->flags & ATA_DFLAG_PIO);
677 int lba48 = (dev->flags & ATA_DFLAG_LBA48);
681 proto = dev->xfer_protocol = ATA_PROT_PIO;
683 proto = dev->xfer_protocol = ATA_PROT_DMA;
685 cmd = ata_prot_to_cmd(proto, lba48);
689 dev->read_cmd = cmd & 0xff;
690 dev->write_cmd = (cmd >> 8) & 0xff;
693 static const char * xfer_mode_str[] = {
713 * ata_udma_string - convert UDMA bit offset to string
714 * @mask: mask of bits supported; only highest bit counts.
716 * Determine string which represents the highest speed
717 * (highest bit in @udma_mask).
723 * Constant C string representing highest speed listed in
724 * @udma_mask, or the constant C string "<n/a>".
727 static const char *ata_mode_string(unsigned int mask)
731 for (i = 7; i >= 0; i--)
734 for (i = ATA_SHIFT_MWDMA + 2; i >= ATA_SHIFT_MWDMA; i--)
737 for (i = ATA_SHIFT_PIO + 4; i >= ATA_SHIFT_PIO; i--)
744 return xfer_mode_str[i];
748 * ata_pio_devchk - PATA device presence detection
749 * @ap: ATA channel to examine
750 * @device: Device to examine (starting at zero)
752 * This technique was originally described in
753 * Hale Landis's ATADRVR (www.ata-atapi.com), and
754 * later found its way into the ATA/ATAPI spec.
756 * Write a pattern to the ATA shadow registers,
757 * and if a device is present, it will respond by
758 * correctly storing and echoing back the
759 * ATA shadow register contents.
765 static unsigned int ata_pio_devchk(struct ata_port *ap,
768 struct ata_ioports *ioaddr = &ap->ioaddr;
771 ap->ops->dev_select(ap, device);
773 outb(0x55, ioaddr->nsect_addr);
774 outb(0xaa, ioaddr->lbal_addr);
776 outb(0xaa, ioaddr->nsect_addr);
777 outb(0x55, ioaddr->lbal_addr);
779 outb(0x55, ioaddr->nsect_addr);
780 outb(0xaa, ioaddr->lbal_addr);
782 nsect = inb(ioaddr->nsect_addr);
783 lbal = inb(ioaddr->lbal_addr);
785 if ((nsect == 0x55) && (lbal == 0xaa))
786 return 1; /* we found a device */
788 return 0; /* nothing found */
792 * ata_mmio_devchk - PATA device presence detection
793 * @ap: ATA channel to examine
794 * @device: Device to examine (starting at zero)
796 * This technique was originally described in
797 * Hale Landis's ATADRVR (www.ata-atapi.com), and
798 * later found its way into the ATA/ATAPI spec.
800 * Write a pattern to the ATA shadow registers,
801 * and if a device is present, it will respond by
802 * correctly storing and echoing back the
803 * ATA shadow register contents.
809 static unsigned int ata_mmio_devchk(struct ata_port *ap,
812 struct ata_ioports *ioaddr = &ap->ioaddr;
815 ap->ops->dev_select(ap, device);
817 writeb(0x55, (void __iomem *) ioaddr->nsect_addr);
818 writeb(0xaa, (void __iomem *) ioaddr->lbal_addr);
820 writeb(0xaa, (void __iomem *) ioaddr->nsect_addr);
821 writeb(0x55, (void __iomem *) ioaddr->lbal_addr);
823 writeb(0x55, (void __iomem *) ioaddr->nsect_addr);
824 writeb(0xaa, (void __iomem *) ioaddr->lbal_addr);
826 nsect = readb((void __iomem *) ioaddr->nsect_addr);
827 lbal = readb((void __iomem *) ioaddr->lbal_addr);
829 if ((nsect == 0x55) && (lbal == 0xaa))
830 return 1; /* we found a device */
832 return 0; /* nothing found */
836 * ata_devchk - PATA device presence detection
837 * @ap: ATA channel to examine
838 * @device: Device to examine (starting at zero)
840 * Dispatch ATA device presence detection, depending
841 * on whether we are using PIO or MMIO to talk to the
842 * ATA shadow registers.
848 static unsigned int ata_devchk(struct ata_port *ap,
851 if (ap->flags & ATA_FLAG_MMIO)
852 return ata_mmio_devchk(ap, device);
853 return ata_pio_devchk(ap, device);
857 * ata_dev_classify - determine device type based on ATA-spec signature
858 * @tf: ATA taskfile register set for device to be identified
860 * Determine from taskfile register contents whether a device is
861 * ATA or ATAPI, as per "Signature and persistence" section
862 * of ATA/PI spec (volume 1, sect 5.14).
868 * Device type, %ATA_DEV_ATA, %ATA_DEV_ATAPI, or %ATA_DEV_UNKNOWN
869 * the event of failure.
872 unsigned int ata_dev_classify(struct ata_taskfile *tf)
874 /* Apple's open source Darwin code hints that some devices only
875 * put a proper signature into the LBA mid/high registers,
876 * So, we only check those. It's sufficient for uniqueness.
879 if (((tf->lbam == 0) && (tf->lbah == 0)) ||
880 ((tf->lbam == 0x3c) && (tf->lbah == 0xc3))) {
881 DPRINTK("found ATA device by sig\n");
885 if (((tf->lbam == 0x14) && (tf->lbah == 0xeb)) ||
886 ((tf->lbam == 0x69) && (tf->lbah == 0x96))) {
887 DPRINTK("found ATAPI device by sig\n");
888 return ATA_DEV_ATAPI;
891 DPRINTK("unknown device\n");
892 return ATA_DEV_UNKNOWN;
896 * ata_dev_try_classify - Parse returned ATA device signature
897 * @ap: ATA channel to examine
898 * @device: Device to examine (starting at zero)
900 * After an event -- SRST, E.D.D., or SATA COMRESET -- occurs,
901 * an ATA/ATAPI-defined set of values is placed in the ATA
902 * shadow registers, indicating the results of device detection
905 * Select the ATA device, and read the values from the ATA shadow
906 * registers. Then parse according to the Error register value,
907 * and the spec-defined values examined by ata_dev_classify().
913 static u8 ata_dev_try_classify(struct ata_port *ap, unsigned int device)
915 struct ata_device *dev = &ap->device[device];
916 struct ata_taskfile tf;
920 ap->ops->dev_select(ap, device);
922 memset(&tf, 0, sizeof(tf));
924 err = ata_chk_err(ap);
925 ap->ops->tf_read(ap, &tf);
927 dev->class = ATA_DEV_NONE;
929 /* see if device passed diags */
932 else if ((device == 0) && (err == 0x81))
937 /* determine if device if ATA or ATAPI */
938 class = ata_dev_classify(&tf);
939 if (class == ATA_DEV_UNKNOWN)
941 if ((class == ATA_DEV_ATA) && (ata_chk_status(ap) == 0))
950 * ata_dev_id_string - Convert IDENTIFY DEVICE page into string
951 * @id: IDENTIFY DEVICE results we will examine
952 * @s: string into which data is output
953 * @ofs: offset into identify device page
954 * @len: length of string to return. must be an even number.
956 * The strings in the IDENTIFY DEVICE page are broken up into
957 * 16-bit chunks. Run through the string, and output each
958 * 8-bit chunk linearly, regardless of platform.
964 void ata_dev_id_string(u16 *id, unsigned char *s,
965 unsigned int ofs, unsigned int len)
985 * ata_noop_dev_select - Select device 0/1 on ATA bus
986 * @ap: ATA channel to manipulate
987 * @device: ATA device (numbered from zero) to select
989 * This function performs no actual function.
991 * May be used as the dev_select() entry in ata_port_operations.
996 void ata_noop_dev_select (struct ata_port *ap, unsigned int device)
1002 * ata_std_dev_select - Select device 0/1 on ATA bus
1003 * @ap: ATA channel to manipulate
1004 * @device: ATA device (numbered from zero) to select
1006 * Use the method defined in the ATA specification to
1007 * make either device 0, or device 1, active on the
1008 * ATA channel. Works with both PIO and MMIO.
1010 * May be used as the dev_select() entry in ata_port_operations.
1016 void ata_std_dev_select (struct ata_port *ap, unsigned int device)
1021 tmp = ATA_DEVICE_OBS;
1023 tmp = ATA_DEVICE_OBS | ATA_DEV1;
1025 if (ap->flags & ATA_FLAG_MMIO) {
1026 writeb(tmp, (void __iomem *) ap->ioaddr.device_addr);
1028 outb(tmp, ap->ioaddr.device_addr);
1030 ata_pause(ap); /* needed; also flushes, for mmio */
1034 * ata_dev_select - Select device 0/1 on ATA bus
1035 * @ap: ATA channel to manipulate
1036 * @device: ATA device (numbered from zero) to select
1037 * @wait: non-zero to wait for Status register BSY bit to clear
1038 * @can_sleep: non-zero if context allows sleeping
1040 * Use the method defined in the ATA specification to
1041 * make either device 0, or device 1, active on the
1044 * This is a high-level version of ata_std_dev_select(),
1045 * which additionally provides the services of inserting
1046 * the proper pauses and status polling, where needed.
1052 void ata_dev_select(struct ata_port *ap, unsigned int device,
1053 unsigned int wait, unsigned int can_sleep)
1055 VPRINTK("ENTER, ata%u: device %u, wait %u\n",
1056 ap->id, device, wait);
1061 ap->ops->dev_select(ap, device);
1064 if (can_sleep && ap->device[device].class == ATA_DEV_ATAPI)
1071 * ata_dump_id - IDENTIFY DEVICE info debugging output
1072 * @dev: Device whose IDENTIFY DEVICE page we will dump
1074 * Dump selected 16-bit words from a detected device's
1075 * IDENTIFY PAGE page.
1081 static inline void ata_dump_id(struct ata_device *dev)
1083 DPRINTK("49==0x%04x "
1093 DPRINTK("80==0x%04x "
1103 DPRINTK("88==0x%04x "
1110 * ata_dev_identify - obtain IDENTIFY x DEVICE page
1111 * @ap: port on which device we wish to probe resides
1112 * @device: device bus address, starting at zero
1114 * Following bus reset, we issue the IDENTIFY [PACKET] DEVICE
1115 * command, and read back the 512-byte device information page.
1116 * The device information page is fed to us via the standard
1117 * PIO-IN protocol, but we hand-code it here. (TODO: investigate
1118 * using standard PIO-IN paths)
1120 * After reading the device information page, we use several
1121 * bits of information from it to initialize data structures
1122 * that will be used during the lifetime of the ata_device.
1123 * Other data from the info page is used to disqualify certain
1124 * older ATA devices we do not wish to support.
1127 * Inherited from caller. Some functions called by this function
1128 * obtain the host_set lock.
1131 static void ata_dev_identify(struct ata_port *ap, unsigned int device)
1133 struct ata_device *dev = &ap->device[device];
1136 unsigned long xfer_modes;
1138 unsigned int using_edd;
1139 DECLARE_COMPLETION(wait);
1140 struct ata_queued_cmd *qc;
1141 unsigned long flags;
1144 if (!ata_dev_present(dev)) {
1145 DPRINTK("ENTER/EXIT (host %u, dev %u) -- nodev\n",
1150 if (ap->flags & (ATA_FLAG_SRST | ATA_FLAG_SATA_RESET))
1155 DPRINTK("ENTER, host %u, dev %u\n", ap->id, device);
1157 assert (dev->class == ATA_DEV_ATA || dev->class == ATA_DEV_ATAPI ||
1158 dev->class == ATA_DEV_NONE);
1160 ata_dev_select(ap, device, 1, 1); /* select device 0/1 */
1162 qc = ata_qc_new_init(ap, dev);
1165 ata_sg_init_one(qc, dev->id, sizeof(dev->id));
1166 qc->dma_dir = DMA_FROM_DEVICE;
1167 qc->tf.protocol = ATA_PROT_PIO;
1171 if (dev->class == ATA_DEV_ATA) {
1172 qc->tf.command = ATA_CMD_ID_ATA;
1173 DPRINTK("do ATA identify\n");
1175 qc->tf.command = ATA_CMD_ID_ATAPI;
1176 DPRINTK("do ATAPI identify\n");
1179 qc->waiting = &wait;
1180 qc->complete_fn = ata_qc_complete_noop;
1182 spin_lock_irqsave(&ap->host_set->lock, flags);
1183 rc = ata_qc_issue(qc);
1184 spin_unlock_irqrestore(&ap->host_set->lock, flags);
1189 wait_for_completion(&wait);
1191 status = ata_chk_status(ap);
1192 if (status & ATA_ERR) {
1194 * arg! EDD works for all test cases, but seems to return
1195 * the ATA signature for some ATAPI devices. Until the
1196 * reason for this is found and fixed, we fix up the mess
1197 * here. If IDENTIFY DEVICE returns command aborted
1198 * (as ATAPI devices do), then we issue an
1199 * IDENTIFY PACKET DEVICE.
1201 * ATA software reset (SRST, the default) does not appear
1202 * to have this problem.
1204 if ((using_edd) && (qc->tf.command == ATA_CMD_ID_ATA)) {
1205 u8 err = ata_chk_err(ap);
1206 if (err & ATA_ABORTED) {
1207 dev->class = ATA_DEV_ATAPI;
1218 swap_buf_le16(dev->id, ATA_ID_WORDS);
1220 /* print device capabilities */
1221 printk(KERN_DEBUG "ata%u: dev %u cfg "
1222 "49:%04x 82:%04x 83:%04x 84:%04x 85:%04x 86:%04x 87:%04x 88:%04x\n",
1223 ap->id, device, dev->id[49],
1224 dev->id[82], dev->id[83], dev->id[84],
1225 dev->id[85], dev->id[86], dev->id[87],
1229 * common ATA, ATAPI feature tests
1232 /* we require LBA and DMA support (bits 8 & 9 of word 49) */
1233 if (!ata_id_has_dma(dev->id) || !ata_id_has_lba(dev->id)) {
1234 printk(KERN_DEBUG "ata%u: no dma/lba\n", ap->id);
1238 /* quick-n-dirty find max transfer mode; for printk only */
1239 xfer_modes = dev->id[ATA_ID_UDMA_MODES];
1241 xfer_modes = (dev->id[ATA_ID_MWDMA_MODES]) << ATA_SHIFT_MWDMA;
1243 xfer_modes = (dev->id[ATA_ID_PIO_MODES]) << (ATA_SHIFT_PIO + 3);
1244 xfer_modes |= (0x7 << ATA_SHIFT_PIO);
1249 /* ATA-specific feature tests */
1250 if (dev->class == ATA_DEV_ATA) {
1251 if (!ata_id_is_ata(dev->id)) /* sanity check */
1254 tmp = dev->id[ATA_ID_MAJOR_VER];
1255 for (i = 14; i >= 1; i--)
1259 /* we require at least ATA-3 */
1261 printk(KERN_DEBUG "ata%u: no ATA-3\n", ap->id);
1265 if (ata_id_has_lba48(dev->id)) {
1266 dev->flags |= ATA_DFLAG_LBA48;
1267 dev->n_sectors = ata_id_u64(dev->id, 100);
1269 dev->n_sectors = ata_id_u32(dev->id, 60);
1272 ap->host->max_cmd_len = 16;
1274 /* print device info to dmesg */
1275 printk(KERN_INFO "ata%u: dev %u ATA, max %s, %Lu sectors:%s\n",
1277 ata_mode_string(xfer_modes),
1278 (unsigned long long)dev->n_sectors,
1279 dev->flags & ATA_DFLAG_LBA48 ? " lba48" : "");
1282 /* ATAPI-specific feature tests */
1284 if (ata_id_is_ata(dev->id)) /* sanity check */
1287 rc = atapi_cdb_len(dev->id);
1288 if ((rc < 12) || (rc > ATAPI_CDB_LEN)) {
1289 printk(KERN_WARNING "ata%u: unsupported CDB len\n", ap->id);
1292 ap->cdb_len = (unsigned int) rc;
1293 ap->host->max_cmd_len = (unsigned char) ap->cdb_len;
1295 if (ata_id_cdb_intr(dev->id))
1296 dev->flags |= ATA_DFLAG_CDB_INTR;
1298 /* print device info to dmesg */
1299 printk(KERN_INFO "ata%u: dev %u ATAPI, max %s\n",
1301 ata_mode_string(xfer_modes));
1304 DPRINTK("EXIT, drv_stat = 0x%x\n", ata_chk_status(ap));
1308 printk(KERN_WARNING "ata%u: dev %u not supported, ignoring\n",
1311 dev->class++; /* converts ATA_DEV_xxx into ATA_DEV_xxx_UNSUP */
1312 DPRINTK("EXIT, err\n");
1316 static inline u8 ata_dev_knobble(struct ata_port *ap)
1318 return ((ap->cbl == ATA_CBL_SATA) && (!ata_id_is_sata(ap->device->id)));
1322 * ata_dev_config - Run device specific handlers and check for
1323 * SATA->PATA bridges
1330 void ata_dev_config(struct ata_port *ap, unsigned int i)
1332 /* limit bridge transfers to udma5, 200 sectors */
1333 if (ata_dev_knobble(ap)) {
1334 printk(KERN_INFO "ata%u(%u): applying bridge limits\n",
1335 ap->id, ap->device->devno);
1336 ap->udma_mask &= ATA_UDMA5;
1337 ap->host->max_sectors = ATA_MAX_SECTORS;
1338 ap->host->hostt->max_sectors = ATA_MAX_SECTORS;
1339 ap->device->flags |= ATA_DFLAG_LOCK_SECTORS;
1342 if (ap->ops->dev_config)
1343 ap->ops->dev_config(ap, &ap->device[i]);
1347 * ata_bus_probe - Reset and probe ATA bus
1350 * Master ATA bus probing function. Initiates a hardware-dependent
1351 * bus reset, then attempts to identify any devices found on
1355 * PCI/etc. bus probe sem.
1358 * Zero on success, non-zero on error.
1361 static int ata_bus_probe(struct ata_port *ap)
1363 unsigned int i, found = 0;
1365 ap->ops->phy_reset(ap);
1366 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1369 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1370 ata_dev_identify(ap, i);
1371 if (ata_dev_present(&ap->device[i])) {
1373 ata_dev_config(ap,i);
1377 if ((!found) || (ap->flags & ATA_FLAG_PORT_DISABLED))
1378 goto err_out_disable;
1381 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1382 goto err_out_disable;
1387 ap->ops->port_disable(ap);
1393 * ata_port_probe - Mark port as enabled
1394 * @ap: Port for which we indicate enablement
1396 * Modify @ap data structure such that the system
1397 * thinks that the entire port is enabled.
1399 * LOCKING: host_set lock, or some other form of
1403 void ata_port_probe(struct ata_port *ap)
1405 ap->flags &= ~ATA_FLAG_PORT_DISABLED;
1409 * __sata_phy_reset - Wake/reset a low-level SATA PHY
1410 * @ap: SATA port associated with target SATA PHY.
1412 * This function issues commands to standard SATA Sxxx
1413 * PHY registers, to wake up the phy (and device), and
1414 * clear any reset condition.
1417 * PCI/etc. bus probe sem.
1420 void __sata_phy_reset(struct ata_port *ap)
1423 unsigned long timeout = jiffies + (HZ * 5);
1425 if (ap->flags & ATA_FLAG_SATA_RESET) {
1426 /* issue phy wake/reset */
1427 scr_write_flush(ap, SCR_CONTROL, 0x301);
1428 /* Couldn't find anything in SATA I/II specs, but
1429 * AHCI-1.1 10.4.2 says at least 1 ms. */
1432 scr_write_flush(ap, SCR_CONTROL, 0x300); /* phy wake/clear reset */
1434 /* wait for phy to become ready, if necessary */
1437 sstatus = scr_read(ap, SCR_STATUS);
1438 if ((sstatus & 0xf) != 1)
1440 } while (time_before(jiffies, timeout));
1442 /* TODO: phy layer with polling, timeouts, etc. */
1443 if (sata_dev_present(ap))
1446 sstatus = scr_read(ap, SCR_STATUS);
1447 printk(KERN_INFO "ata%u: no device found (phy stat %08x)\n",
1449 ata_port_disable(ap);
1452 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1455 if (ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT)) {
1456 ata_port_disable(ap);
1460 ap->cbl = ATA_CBL_SATA;
1464 * sata_phy_reset - Reset SATA bus.
1465 * @ap: SATA port associated with target SATA PHY.
1467 * This function resets the SATA bus, and then probes
1468 * the bus for devices.
1471 * PCI/etc. bus probe sem.
1474 void sata_phy_reset(struct ata_port *ap)
1476 __sata_phy_reset(ap);
1477 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1483 * ata_port_disable - Disable port.
1484 * @ap: Port to be disabled.
1486 * Modify @ap data structure such that the system
1487 * thinks that the entire port is disabled, and should
1488 * never attempt to probe or communicate with devices
1491 * LOCKING: host_set lock, or some other form of
1495 void ata_port_disable(struct ata_port *ap)
1497 ap->device[0].class = ATA_DEV_NONE;
1498 ap->device[1].class = ATA_DEV_NONE;
1499 ap->flags |= ATA_FLAG_PORT_DISABLED;
1505 } xfer_mode_classes[] = {
1506 { ATA_SHIFT_UDMA, XFER_UDMA_0 },
1507 { ATA_SHIFT_MWDMA, XFER_MW_DMA_0 },
1508 { ATA_SHIFT_PIO, XFER_PIO_0 },
1511 static inline u8 base_from_shift(unsigned int shift)
1515 for (i = 0; i < ARRAY_SIZE(xfer_mode_classes); i++)
1516 if (xfer_mode_classes[i].shift == shift)
1517 return xfer_mode_classes[i].base;
1522 static void ata_dev_set_mode(struct ata_port *ap, struct ata_device *dev)
1527 if (!ata_dev_present(dev) || (ap->flags & ATA_FLAG_PORT_DISABLED))
1530 if (dev->xfer_shift == ATA_SHIFT_PIO)
1531 dev->flags |= ATA_DFLAG_PIO;
1533 ata_dev_set_xfermode(ap, dev);
1535 base = base_from_shift(dev->xfer_shift);
1536 ofs = dev->xfer_mode - base;
1537 idx = ofs + dev->xfer_shift;
1538 WARN_ON(idx >= ARRAY_SIZE(xfer_mode_str));
1540 DPRINTK("idx=%d xfer_shift=%u, xfer_mode=0x%x, base=0x%x, offset=%d\n",
1541 idx, dev->xfer_shift, (int)dev->xfer_mode, (int)base, ofs);
1543 printk(KERN_INFO "ata%u: dev %u configured for %s\n",
1544 ap->id, dev->devno, xfer_mode_str[idx]);
1547 static int ata_host_set_pio(struct ata_port *ap)
1553 mask = ata_get_mode_mask(ap, ATA_SHIFT_PIO);
1556 printk(KERN_WARNING "ata%u: no PIO support\n", ap->id);
1560 base = base_from_shift(ATA_SHIFT_PIO);
1561 xfer_mode = base + x;
1563 DPRINTK("base 0x%x xfer_mode 0x%x mask 0x%x x %d\n",
1564 (int)base, (int)xfer_mode, mask, x);
1566 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1567 struct ata_device *dev = &ap->device[i];
1568 if (ata_dev_present(dev)) {
1569 dev->pio_mode = xfer_mode;
1570 dev->xfer_mode = xfer_mode;
1571 dev->xfer_shift = ATA_SHIFT_PIO;
1572 if (ap->ops->set_piomode)
1573 ap->ops->set_piomode(ap, dev);
1580 static void ata_host_set_dma(struct ata_port *ap, u8 xfer_mode,
1581 unsigned int xfer_shift)
1585 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1586 struct ata_device *dev = &ap->device[i];
1587 if (ata_dev_present(dev)) {
1588 dev->dma_mode = xfer_mode;
1589 dev->xfer_mode = xfer_mode;
1590 dev->xfer_shift = xfer_shift;
1591 if (ap->ops->set_dmamode)
1592 ap->ops->set_dmamode(ap, dev);
1598 * ata_set_mode - Program timings and issue SET FEATURES - XFER
1599 * @ap: port on which timings will be programmed
1601 * Set ATA device disk transfer mode (PIO3, UDMA6, etc.).
1604 * PCI/etc. bus probe sem.
1607 static void ata_set_mode(struct ata_port *ap)
1609 unsigned int i, xfer_shift;
1613 /* step 1: always set host PIO timings */
1614 rc = ata_host_set_pio(ap);
1618 /* step 2: choose the best data xfer mode */
1619 xfer_mode = xfer_shift = 0;
1620 rc = ata_choose_xfer_mode(ap, &xfer_mode, &xfer_shift);
1624 /* step 3: if that xfer mode isn't PIO, set host DMA timings */
1625 if (xfer_shift != ATA_SHIFT_PIO)
1626 ata_host_set_dma(ap, xfer_mode, xfer_shift);
1628 /* step 4: update devices' xfer mode */
1629 ata_dev_set_mode(ap, &ap->device[0]);
1630 ata_dev_set_mode(ap, &ap->device[1]);
1632 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1635 if (ap->ops->post_set_mode)
1636 ap->ops->post_set_mode(ap);
1638 for (i = 0; i < 2; i++) {
1639 struct ata_device *dev = &ap->device[i];
1640 ata_dev_set_protocol(dev);
1646 ata_port_disable(ap);
1650 * ata_busy_sleep - sleep until BSY clears, or timeout
1651 * @ap: port containing status register to be polled
1652 * @tmout_pat: impatience timeout
1653 * @tmout: overall timeout
1655 * Sleep until ATA Status register bit BSY clears,
1656 * or a timeout occurs.
1662 static unsigned int ata_busy_sleep (struct ata_port *ap,
1663 unsigned long tmout_pat,
1664 unsigned long tmout)
1666 unsigned long timer_start, timeout;
1669 status = ata_busy_wait(ap, ATA_BUSY, 300);
1670 timer_start = jiffies;
1671 timeout = timer_start + tmout_pat;
1672 while ((status & ATA_BUSY) && (time_before(jiffies, timeout))) {
1674 status = ata_busy_wait(ap, ATA_BUSY, 3);
1677 if (status & ATA_BUSY)
1678 printk(KERN_WARNING "ata%u is slow to respond, "
1679 "please be patient\n", ap->id);
1681 timeout = timer_start + tmout;
1682 while ((status & ATA_BUSY) && (time_before(jiffies, timeout))) {
1684 status = ata_chk_status(ap);
1687 if (status & ATA_BUSY) {
1688 printk(KERN_ERR "ata%u failed to respond (%lu secs)\n",
1689 ap->id, tmout / HZ);
1696 static void ata_bus_post_reset(struct ata_port *ap, unsigned int devmask)
1698 struct ata_ioports *ioaddr = &ap->ioaddr;
1699 unsigned int dev0 = devmask & (1 << 0);
1700 unsigned int dev1 = devmask & (1 << 1);
1701 unsigned long timeout;
1703 /* if device 0 was found in ata_devchk, wait for its
1707 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
1709 /* if device 1 was found in ata_devchk, wait for
1710 * register access, then wait for BSY to clear
1712 timeout = jiffies + ATA_TMOUT_BOOT;
1716 ap->ops->dev_select(ap, 1);
1717 if (ap->flags & ATA_FLAG_MMIO) {
1718 nsect = readb((void __iomem *) ioaddr->nsect_addr);
1719 lbal = readb((void __iomem *) ioaddr->lbal_addr);
1721 nsect = inb(ioaddr->nsect_addr);
1722 lbal = inb(ioaddr->lbal_addr);
1724 if ((nsect == 1) && (lbal == 1))
1726 if (time_after(jiffies, timeout)) {
1730 msleep(50); /* give drive a breather */
1733 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
1735 /* is all this really necessary? */
1736 ap->ops->dev_select(ap, 0);
1738 ap->ops->dev_select(ap, 1);
1740 ap->ops->dev_select(ap, 0);
1744 * ata_bus_edd - Issue EXECUTE DEVICE DIAGNOSTIC command.
1745 * @ap: Port to reset and probe
1747 * Use the EXECUTE DEVICE DIAGNOSTIC command to reset and
1748 * probe the bus. Not often used these days.
1751 * PCI/etc. bus probe sem.
1755 static unsigned int ata_bus_edd(struct ata_port *ap)
1757 struct ata_taskfile tf;
1759 /* set up execute-device-diag (bus reset) taskfile */
1760 /* also, take interrupts to a known state (disabled) */
1761 DPRINTK("execute-device-diag\n");
1762 ata_tf_init(ap, &tf, 0);
1764 tf.command = ATA_CMD_EDD;
1765 tf.protocol = ATA_PROT_NODATA;
1768 ata_tf_to_host(ap, &tf);
1770 /* spec says at least 2ms. but who knows with those
1771 * crazy ATAPI devices...
1775 return ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
1778 static unsigned int ata_bus_softreset(struct ata_port *ap,
1779 unsigned int devmask)
1781 struct ata_ioports *ioaddr = &ap->ioaddr;
1783 DPRINTK("ata%u: bus reset via SRST\n", ap->id);
1785 /* software reset. causes dev0 to be selected */
1786 if (ap->flags & ATA_FLAG_MMIO) {
1787 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
1788 udelay(20); /* FIXME: flush */
1789 writeb(ap->ctl | ATA_SRST, (void __iomem *) ioaddr->ctl_addr);
1790 udelay(20); /* FIXME: flush */
1791 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
1793 outb(ap->ctl, ioaddr->ctl_addr);
1795 outb(ap->ctl | ATA_SRST, ioaddr->ctl_addr);
1797 outb(ap->ctl, ioaddr->ctl_addr);
1800 /* spec mandates ">= 2ms" before checking status.
1801 * We wait 150ms, because that was the magic delay used for
1802 * ATAPI devices in Hale Landis's ATADRVR, for the period of time
1803 * between when the ATA command register is written, and then
1804 * status is checked. Because waiting for "a while" before
1805 * checking status is fine, post SRST, we perform this magic
1806 * delay here as well.
1810 ata_bus_post_reset(ap, devmask);
1816 * ata_bus_reset - reset host port and associated ATA channel
1817 * @ap: port to reset
1819 * This is typically the first time we actually start issuing
1820 * commands to the ATA channel. We wait for BSY to clear, then
1821 * issue EXECUTE DEVICE DIAGNOSTIC command, polling for its
1822 * result. Determine what devices, if any, are on the channel
1823 * by looking at the device 0/1 error register. Look at the signature
1824 * stored in each device's taskfile registers, to determine if
1825 * the device is ATA or ATAPI.
1828 * PCI/etc. bus probe sem.
1829 * Obtains host_set lock.
1832 * Sets ATA_FLAG_PORT_DISABLED if bus reset fails.
1835 void ata_bus_reset(struct ata_port *ap)
1837 struct ata_ioports *ioaddr = &ap->ioaddr;
1838 unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS;
1840 unsigned int dev0, dev1 = 0, rc = 0, devmask = 0;
1842 DPRINTK("ENTER, host %u, port %u\n", ap->id, ap->port_no);
1844 /* determine if device 0/1 are present */
1845 if (ap->flags & ATA_FLAG_SATA_RESET)
1848 dev0 = ata_devchk(ap, 0);
1850 dev1 = ata_devchk(ap, 1);
1854 devmask |= (1 << 0);
1856 devmask |= (1 << 1);
1858 /* select device 0 again */
1859 ap->ops->dev_select(ap, 0);
1861 /* issue bus reset */
1862 if (ap->flags & ATA_FLAG_SRST)
1863 rc = ata_bus_softreset(ap, devmask);
1864 else if ((ap->flags & ATA_FLAG_SATA_RESET) == 0) {
1865 /* set up device control */
1866 if (ap->flags & ATA_FLAG_MMIO)
1867 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
1869 outb(ap->ctl, ioaddr->ctl_addr);
1870 rc = ata_bus_edd(ap);
1877 * determine by signature whether we have ATA or ATAPI devices
1879 err = ata_dev_try_classify(ap, 0);
1880 if ((slave_possible) && (err != 0x81))
1881 ata_dev_try_classify(ap, 1);
1883 /* re-enable interrupts */
1884 if (ap->ioaddr.ctl_addr) /* FIXME: hack. create a hook instead */
1887 /* is double-select really necessary? */
1888 if (ap->device[1].class != ATA_DEV_NONE)
1889 ap->ops->dev_select(ap, 1);
1890 if (ap->device[0].class != ATA_DEV_NONE)
1891 ap->ops->dev_select(ap, 0);
1893 /* if no devices were detected, disable this port */
1894 if ((ap->device[0].class == ATA_DEV_NONE) &&
1895 (ap->device[1].class == ATA_DEV_NONE))
1898 if (ap->flags & (ATA_FLAG_SATA_RESET | ATA_FLAG_SRST)) {
1899 /* set up device control for ATA_FLAG_SATA_RESET */
1900 if (ap->flags & ATA_FLAG_MMIO)
1901 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
1903 outb(ap->ctl, ioaddr->ctl_addr);
1910 printk(KERN_ERR "ata%u: disabling port\n", ap->id);
1911 ap->ops->port_disable(ap);
1916 static void ata_pr_blacklisted(struct ata_port *ap, struct ata_device *dev)
1918 printk(KERN_WARNING "ata%u: dev %u is on DMA blacklist, disabling DMA\n",
1919 ap->id, dev->devno);
1922 static const char * ata_dma_blacklist [] = {
1941 "Toshiba CD-ROM XM-6202B",
1942 "TOSHIBA CD-ROM XM-1702BC",
1944 "E-IDE CD-ROM CR-840",
1947 "SAMSUNG CD-ROM SC-148C",
1948 "SAMSUNG CD-ROM SC",
1950 "ATAPI CD-ROM DRIVE 40X MAXIMUM",
1954 static int ata_dma_blacklisted(struct ata_port *ap, struct ata_device *dev)
1956 unsigned char model_num[40];
1961 ata_dev_id_string(dev->id, model_num, ATA_ID_PROD_OFS,
1964 len = strnlen(s, sizeof(model_num));
1966 /* ATAPI specifies that empty space is blank-filled; remove blanks */
1967 while ((len > 0) && (s[len - 1] == ' ')) {
1972 for (i = 0; i < ARRAY_SIZE(ata_dma_blacklist); i++)
1973 if (!strncmp(ata_dma_blacklist[i], s, len))
1979 static unsigned int ata_get_mode_mask(struct ata_port *ap, int shift)
1981 struct ata_device *master, *slave;
1984 master = &ap->device[0];
1985 slave = &ap->device[1];
1987 assert (ata_dev_present(master) || ata_dev_present(slave));
1989 if (shift == ATA_SHIFT_UDMA) {
1990 mask = ap->udma_mask;
1991 if (ata_dev_present(master)) {
1992 mask &= (master->id[ATA_ID_UDMA_MODES] & 0xff);
1993 if (ata_dma_blacklisted(ap, master)) {
1995 ata_pr_blacklisted(ap, master);
1998 if (ata_dev_present(slave)) {
1999 mask &= (slave->id[ATA_ID_UDMA_MODES] & 0xff);
2000 if (ata_dma_blacklisted(ap, slave)) {
2002 ata_pr_blacklisted(ap, slave);
2006 else if (shift == ATA_SHIFT_MWDMA) {
2007 mask = ap->mwdma_mask;
2008 if (ata_dev_present(master)) {
2009 mask &= (master->id[ATA_ID_MWDMA_MODES] & 0x07);
2010 if (ata_dma_blacklisted(ap, master)) {
2012 ata_pr_blacklisted(ap, master);
2015 if (ata_dev_present(slave)) {
2016 mask &= (slave->id[ATA_ID_MWDMA_MODES] & 0x07);
2017 if (ata_dma_blacklisted(ap, slave)) {
2019 ata_pr_blacklisted(ap, slave);
2023 else if (shift == ATA_SHIFT_PIO) {
2024 mask = ap->pio_mask;
2025 if (ata_dev_present(master)) {
2026 /* spec doesn't return explicit support for
2027 * PIO0-2, so we fake it
2029 u16 tmp_mode = master->id[ATA_ID_PIO_MODES] & 0x03;
2034 if (ata_dev_present(slave)) {
2035 /* spec doesn't return explicit support for
2036 * PIO0-2, so we fake it
2038 u16 tmp_mode = slave->id[ATA_ID_PIO_MODES] & 0x03;
2045 mask = 0xffffffff; /* shut up compiler warning */
2052 /* find greatest bit */
2053 static int fgb(u32 bitmap)
2058 for (i = 0; i < 32; i++)
2059 if (bitmap & (1 << i))
2066 * ata_choose_xfer_mode - attempt to find best transfer mode
2067 * @ap: Port for which an xfer mode will be selected
2068 * @xfer_mode_out: (output) SET FEATURES - XFER MODE code
2069 * @xfer_shift_out: (output) bit shift that selects this mode
2071 * Based on host and device capabilities, determine the
2072 * maximum transfer mode that is amenable to all.
2075 * PCI/etc. bus probe sem.
2078 * Zero on success, negative on error.
2081 static int ata_choose_xfer_mode(struct ata_port *ap,
2083 unsigned int *xfer_shift_out)
2085 unsigned int mask, shift;
2088 for (i = 0; i < ARRAY_SIZE(xfer_mode_classes); i++) {
2089 shift = xfer_mode_classes[i].shift;
2090 mask = ata_get_mode_mask(ap, shift);
2094 *xfer_mode_out = xfer_mode_classes[i].base + x;
2095 *xfer_shift_out = shift;
2104 * ata_dev_set_xfermode - Issue SET FEATURES - XFER MODE command
2105 * @ap: Port associated with device @dev
2106 * @dev: Device to which command will be sent
2108 * Issue SET FEATURES - XFER MODE command to device @dev
2112 * PCI/etc. bus probe sem.
2115 static void ata_dev_set_xfermode(struct ata_port *ap, struct ata_device *dev)
2117 DECLARE_COMPLETION(wait);
2118 struct ata_queued_cmd *qc;
2120 unsigned long flags;
2122 /* set up set-features taskfile */
2123 DPRINTK("set features - xfer mode\n");
2125 qc = ata_qc_new_init(ap, dev);
2128 qc->tf.command = ATA_CMD_SET_FEATURES;
2129 qc->tf.feature = SETFEATURES_XFER;
2130 qc->tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
2131 qc->tf.protocol = ATA_PROT_NODATA;
2132 qc->tf.nsect = dev->xfer_mode;
2134 qc->waiting = &wait;
2135 qc->complete_fn = ata_qc_complete_noop;
2137 spin_lock_irqsave(&ap->host_set->lock, flags);
2138 rc = ata_qc_issue(qc);
2139 spin_unlock_irqrestore(&ap->host_set->lock, flags);
2142 ata_port_disable(ap);
2144 wait_for_completion(&wait);
2150 * ata_sg_clean - Unmap DMA memory associated with command
2151 * @qc: Command containing DMA memory to be released
2153 * Unmap all mapped DMA memory associated with this command.
2156 * spin_lock_irqsave(host_set lock)
2159 static void ata_sg_clean(struct ata_queued_cmd *qc)
2161 struct ata_port *ap = qc->ap;
2162 struct scatterlist *sg = qc->sg;
2163 int dir = qc->dma_dir;
2165 assert(qc->flags & ATA_QCFLAG_DMAMAP);
2168 if (qc->flags & ATA_QCFLAG_SINGLE)
2169 assert(qc->n_elem == 1);
2171 DPRINTK("unmapping %u sg elements\n", qc->n_elem);
2173 if (qc->flags & ATA_QCFLAG_SG)
2174 dma_unmap_sg(ap->host_set->dev, sg, qc->n_elem, dir);
2176 dma_unmap_single(ap->host_set->dev, sg_dma_address(&sg[0]),
2177 sg_dma_len(&sg[0]), dir);
2179 qc->flags &= ~ATA_QCFLAG_DMAMAP;
2184 * ata_fill_sg - Fill PCI IDE PRD table
2185 * @qc: Metadata associated with taskfile to be transferred
2187 * Fill PCI IDE PRD (scatter-gather) table with segments
2188 * associated with the current disk command.
2191 * spin_lock_irqsave(host_set lock)
2194 static void ata_fill_sg(struct ata_queued_cmd *qc)
2196 struct scatterlist *sg = qc->sg;
2197 struct ata_port *ap = qc->ap;
2198 unsigned int idx, nelem;
2201 assert(qc->n_elem > 0);
2204 for (nelem = qc->n_elem; nelem; nelem--,sg++) {
2208 /* determine if physical DMA addr spans 64K boundary.
2209 * Note h/w doesn't support 64-bit, so we unconditionally
2210 * truncate dma_addr_t to u32.
2212 addr = (u32) sg_dma_address(sg);
2213 sg_len = sg_dma_len(sg);
2216 offset = addr & 0xffff;
2218 if ((offset + sg_len) > 0x10000)
2219 len = 0x10000 - offset;
2221 ap->prd[idx].addr = cpu_to_le32(addr);
2222 ap->prd[idx].flags_len = cpu_to_le32(len & 0xffff);
2223 VPRINTK("PRD[%u] = (0x%X, 0x%X)\n", idx, addr, len);
2232 ap->prd[idx - 1].flags_len |= cpu_to_le32(ATA_PRD_EOT);
2235 * ata_check_atapi_dma - Check whether ATAPI DMA can be supported
2236 * @qc: Metadata associated with taskfile to check
2238 * Allow low-level driver to filter ATA PACKET commands, returning
2239 * a status indicating whether or not it is OK to use DMA for the
2240 * supplied PACKET command.
2243 * spin_lock_irqsave(host_set lock)
2245 * RETURNS: 0 when ATAPI DMA can be used
2248 int ata_check_atapi_dma(struct ata_queued_cmd *qc)
2250 struct ata_port *ap = qc->ap;
2251 int rc = 0; /* Assume ATAPI DMA is OK by default */
2253 if (ap->ops->check_atapi_dma)
2254 rc = ap->ops->check_atapi_dma(qc);
2259 * ata_qc_prep - Prepare taskfile for submission
2260 * @qc: Metadata associated with taskfile to be prepared
2262 * Prepare ATA taskfile for submission.
2265 * spin_lock_irqsave(host_set lock)
2267 void ata_qc_prep(struct ata_queued_cmd *qc)
2269 if (!(qc->flags & ATA_QCFLAG_DMAMAP))
2276 * ata_sg_init_one - Associate command with memory buffer
2277 * @qc: Command to be associated
2278 * @buf: Memory buffer
2279 * @buflen: Length of memory buffer, in bytes.
2281 * Initialize the data-related elements of queued_cmd @qc
2282 * to point to a single memory buffer, @buf of byte length @buflen.
2285 * spin_lock_irqsave(host_set lock)
2288 void ata_sg_init_one(struct ata_queued_cmd *qc, void *buf, unsigned int buflen)
2290 struct scatterlist *sg;
2292 qc->flags |= ATA_QCFLAG_SINGLE;
2294 memset(&qc->sgent, 0, sizeof(qc->sgent));
2295 qc->sg = &qc->sgent;
2300 sg->page = virt_to_page(buf);
2301 sg->offset = (unsigned long) buf & ~PAGE_MASK;
2302 sg->length = buflen;
2306 * ata_sg_init - Associate command with scatter-gather table.
2307 * @qc: Command to be associated
2308 * @sg: Scatter-gather table.
2309 * @n_elem: Number of elements in s/g table.
2311 * Initialize the data-related elements of queued_cmd @qc
2312 * to point to a scatter-gather table @sg, containing @n_elem
2316 * spin_lock_irqsave(host_set lock)
2319 void ata_sg_init(struct ata_queued_cmd *qc, struct scatterlist *sg,
2320 unsigned int n_elem)
2322 qc->flags |= ATA_QCFLAG_SG;
2324 qc->n_elem = n_elem;
2328 * ata_sg_setup_one - DMA-map the memory buffer associated with a command.
2329 * @qc: Command with memory buffer to be mapped.
2331 * DMA-map the memory buffer associated with queued_cmd @qc.
2334 * spin_lock_irqsave(host_set lock)
2337 * Zero on success, negative on error.
2340 static int ata_sg_setup_one(struct ata_queued_cmd *qc)
2342 struct ata_port *ap = qc->ap;
2343 int dir = qc->dma_dir;
2344 struct scatterlist *sg = qc->sg;
2345 dma_addr_t dma_address;
2347 dma_address = dma_map_single(ap->host_set->dev, qc->buf_virt,
2349 if (dma_mapping_error(dma_address))
2352 sg_dma_address(sg) = dma_address;
2353 sg_dma_len(sg) = sg->length;
2355 DPRINTK("mapped buffer of %d bytes for %s\n", sg_dma_len(sg),
2356 qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
2362 * ata_sg_setup - DMA-map the scatter-gather table associated with a command.
2363 * @qc: Command with scatter-gather table to be mapped.
2365 * DMA-map the scatter-gather table associated with queued_cmd @qc.
2368 * spin_lock_irqsave(host_set lock)
2371 * Zero on success, negative on error.
2375 static int ata_sg_setup(struct ata_queued_cmd *qc)
2377 struct ata_port *ap = qc->ap;
2378 struct scatterlist *sg = qc->sg;
2381 VPRINTK("ENTER, ata%u\n", ap->id);
2382 assert(qc->flags & ATA_QCFLAG_SG);
2385 n_elem = dma_map_sg(ap->host_set->dev, sg, qc->n_elem, dir);
2389 DPRINTK("%d sg elements mapped\n", n_elem);
2391 qc->n_elem = n_elem;
2397 * ata_poll_qc_complete - turn irq back on and finish qc
2398 * @qc: Command to complete
2399 * @drv_stat: ATA status register content
2402 * None. (grabs host lock)
2405 void ata_poll_qc_complete(struct ata_queued_cmd *qc, u8 drv_stat)
2407 struct ata_port *ap = qc->ap;
2408 unsigned long flags;
2410 spin_lock_irqsave(&ap->host_set->lock, flags);
2412 ata_qc_complete(qc, drv_stat);
2413 spin_unlock_irqrestore(&ap->host_set->lock, flags);
2421 * None. (executing in kernel thread context)
2427 static unsigned long ata_pio_poll(struct ata_port *ap)
2430 unsigned int poll_state = HSM_ST_UNKNOWN;
2431 unsigned int reg_state = HSM_ST_UNKNOWN;
2432 const unsigned int tmout_state = HSM_ST_TMOUT;
2434 switch (ap->hsm_task_state) {
2437 poll_state = HSM_ST_POLL;
2441 case HSM_ST_LAST_POLL:
2442 poll_state = HSM_ST_LAST_POLL;
2443 reg_state = HSM_ST_LAST;
2450 status = ata_chk_status(ap);
2451 if (status & ATA_BUSY) {
2452 if (time_after(jiffies, ap->pio_task_timeout)) {
2453 ap->hsm_task_state = tmout_state;
2456 ap->hsm_task_state = poll_state;
2457 return ATA_SHORT_PAUSE;
2460 ap->hsm_task_state = reg_state;
2465 * ata_pio_complete -
2469 * None. (executing in kernel thread context)
2472 * Non-zero if qc completed, zero otherwise.
2475 static int ata_pio_complete (struct ata_port *ap)
2477 struct ata_queued_cmd *qc;
2481 * This is purely heuristic. This is a fast path. Sometimes when
2482 * we enter, BSY will be cleared in a chk-status or two. If not,
2483 * the drive is probably seeking or something. Snooze for a couple
2484 * msecs, then chk-status again. If still busy, fall back to
2485 * HSM_ST_POLL state.
2487 drv_stat = ata_busy_wait(ap, ATA_BUSY | ATA_DRQ, 10);
2488 if (drv_stat & (ATA_BUSY | ATA_DRQ)) {
2490 drv_stat = ata_busy_wait(ap, ATA_BUSY | ATA_DRQ, 10);
2491 if (drv_stat & (ATA_BUSY | ATA_DRQ)) {
2492 ap->hsm_task_state = HSM_ST_LAST_POLL;
2493 ap->pio_task_timeout = jiffies + ATA_TMOUT_PIO;
2498 drv_stat = ata_wait_idle(ap);
2499 if (!ata_ok(drv_stat)) {
2500 ap->hsm_task_state = HSM_ST_ERR;
2504 qc = ata_qc_from_tag(ap, ap->active_tag);
2507 ap->hsm_task_state = HSM_ST_IDLE;
2509 ata_poll_qc_complete(qc, drv_stat);
2511 /* another command may start at this point */
2519 * @buf: Buffer to swap
2520 * @buf_words: Number of 16-bit words in buffer.
2522 * Swap halves of 16-bit words if needed to convert from
2523 * little-endian byte order to native cpu byte order, or
2528 void swap_buf_le16(u16 *buf, unsigned int buf_words)
2533 for (i = 0; i < buf_words; i++)
2534 buf[i] = le16_to_cpu(buf[i]);
2535 #endif /* __BIG_ENDIAN */
2539 * ata_mmio_data_xfer - Transfer data by MMIO
2540 * @ap: port to read/write
2542 * @buflen: buffer length
2543 * @write_data: read/write
2545 * Transfer data from/to the device data register by MMIO.
2548 * Inherited from caller.
2552 static void ata_mmio_data_xfer(struct ata_port *ap, unsigned char *buf,
2553 unsigned int buflen, int write_data)
2556 unsigned int words = buflen >> 1;
2557 u16 *buf16 = (u16 *) buf;
2558 void __iomem *mmio = (void __iomem *)ap->ioaddr.data_addr;
2560 /* Transfer multiple of 2 bytes */
2562 for (i = 0; i < words; i++)
2563 writew(le16_to_cpu(buf16[i]), mmio);
2565 for (i = 0; i < words; i++)
2566 buf16[i] = cpu_to_le16(readw(mmio));
2569 /* Transfer trailing 1 byte, if any. */
2570 if (unlikely(buflen & 0x01)) {
2571 u16 align_buf[1] = { 0 };
2572 unsigned char *trailing_buf = buf + buflen - 1;
2575 memcpy(align_buf, trailing_buf, 1);
2576 writew(le16_to_cpu(align_buf[0]), mmio);
2578 align_buf[0] = cpu_to_le16(readw(mmio));
2579 memcpy(trailing_buf, align_buf, 1);
2585 * ata_pio_data_xfer - Transfer data by PIO
2586 * @ap: port to read/write
2588 * @buflen: buffer length
2589 * @write_data: read/write
2591 * Transfer data from/to the device data register by PIO.
2594 * Inherited from caller.
2598 static void ata_pio_data_xfer(struct ata_port *ap, unsigned char *buf,
2599 unsigned int buflen, int write_data)
2601 unsigned int words = buflen >> 1;
2603 /* Transfer multiple of 2 bytes */
2605 outsw(ap->ioaddr.data_addr, buf, words);
2607 insw(ap->ioaddr.data_addr, buf, words);
2609 /* Transfer trailing 1 byte, if any. */
2610 if (unlikely(buflen & 0x01)) {
2611 u16 align_buf[1] = { 0 };
2612 unsigned char *trailing_buf = buf + buflen - 1;
2615 memcpy(align_buf, trailing_buf, 1);
2616 outw(le16_to_cpu(align_buf[0]), ap->ioaddr.data_addr);
2618 align_buf[0] = cpu_to_le16(inw(ap->ioaddr.data_addr));
2619 memcpy(trailing_buf, align_buf, 1);
2625 * ata_data_xfer - Transfer data from/to the data register.
2626 * @ap: port to read/write
2628 * @buflen: buffer length
2629 * @do_write: read/write
2631 * Transfer data from/to the device data register.
2634 * Inherited from caller.
2638 static void ata_data_xfer(struct ata_port *ap, unsigned char *buf,
2639 unsigned int buflen, int do_write)
2641 if (ap->flags & ATA_FLAG_MMIO)
2642 ata_mmio_data_xfer(ap, buf, buflen, do_write);
2644 ata_pio_data_xfer(ap, buf, buflen, do_write);
2648 * ata_pio_sector - Transfer ATA_SECT_SIZE (512 bytes) of data.
2649 * @qc: Command on going
2651 * Transfer ATA_SECT_SIZE of data from/to the ATA device.
2654 * Inherited from caller.
2657 static void ata_pio_sector(struct ata_queued_cmd *qc)
2659 int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
2660 struct scatterlist *sg = qc->sg;
2661 struct ata_port *ap = qc->ap;
2663 unsigned int offset;
2665 unsigned long flags;
2667 if (qc->cursect == (qc->nsect - 1))
2668 ap->hsm_task_state = HSM_ST_LAST;
2670 page = sg[qc->cursg].page;
2671 offset = sg[qc->cursg].offset + qc->cursg_ofs * ATA_SECT_SIZE;
2673 /* get the current page and offset */
2674 page = nth_page(page, (offset >> PAGE_SHIFT));
2675 offset %= PAGE_SIZE;
2677 local_irq_save(flags);
2678 buf = kmap_atomic(page, KM_IRQ0) + offset;
2683 if ((qc->cursg_ofs * ATA_SECT_SIZE) == (&sg[qc->cursg])->length) {
2688 DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
2690 /* do the actual data transfer */
2691 do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
2692 ata_data_xfer(ap, buf, ATA_SECT_SIZE, do_write);
2694 kunmap_atomic(buf - offset, KM_IRQ0);
2695 local_irq_restore(flags);
2699 * __atapi_pio_bytes - Transfer data from/to the ATAPI device.
2700 * @qc: Command on going
2701 * @bytes: number of bytes
2703 * Transfer Transfer data from/to the ATAPI device.
2706 * Inherited from caller.
2710 static void __atapi_pio_bytes(struct ata_queued_cmd *qc, unsigned int bytes)
2712 int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
2713 struct scatterlist *sg = qc->sg;
2714 struct ata_port *ap = qc->ap;
2717 unsigned int offset, count;
2718 unsigned long flags;
2720 if (qc->curbytes + bytes >= qc->nbytes)
2721 ap->hsm_task_state = HSM_ST_LAST;
2724 if (unlikely(qc->cursg >= qc->n_elem)) {
2726 * The end of qc->sg is reached and the device expects
2727 * more data to transfer. In order not to overrun qc->sg
2728 * and fulfill length specified in the byte count register,
2729 * - for read case, discard trailing data from the device
2730 * - for write case, padding zero data to the device
2732 u16 pad_buf[1] = { 0 };
2733 unsigned int words = bytes >> 1;
2736 if (words) /* warning if bytes > 1 */
2737 printk(KERN_WARNING "ata%u: %u bytes trailing data\n",
2740 for (i = 0; i < words; i++)
2741 ata_data_xfer(ap, (unsigned char*)pad_buf, 2, do_write);
2743 ap->hsm_task_state = HSM_ST_LAST;
2747 sg = &qc->sg[qc->cursg];
2750 offset = sg->offset + qc->cursg_ofs;
2752 /* get the current page and offset */
2753 page = nth_page(page, (offset >> PAGE_SHIFT));
2754 offset %= PAGE_SIZE;
2756 /* don't overrun current sg */
2757 count = min(sg->length - qc->cursg_ofs, bytes);
2759 /* don't cross page boundaries */
2760 count = min(count, (unsigned int)PAGE_SIZE - offset);
2762 local_irq_save(flags);
2763 buf = kmap_atomic(page, KM_IRQ0) + offset;
2766 qc->curbytes += count;
2767 qc->cursg_ofs += count;
2769 if (qc->cursg_ofs == sg->length) {
2774 DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
2776 /* do the actual data transfer */
2777 ata_data_xfer(ap, buf, count, do_write);
2779 kunmap_atomic(buf - offset, KM_IRQ0);
2780 local_irq_restore(flags);
2787 * atapi_pio_bytes - Transfer data from/to the ATAPI device.
2788 * @qc: Command on going
2790 * Transfer Transfer data from/to the ATAPI device.
2793 * Inherited from caller.
2797 static void atapi_pio_bytes(struct ata_queued_cmd *qc)
2799 struct ata_port *ap = qc->ap;
2800 struct ata_device *dev = qc->dev;
2801 unsigned int ireason, bc_lo, bc_hi, bytes;
2802 int i_write, do_write = (qc->tf.flags & ATA_TFLAG_WRITE) ? 1 : 0;
2804 ap->ops->tf_read(ap, &qc->tf);
2805 ireason = qc->tf.nsect;
2806 bc_lo = qc->tf.lbam;
2807 bc_hi = qc->tf.lbah;
2808 bytes = (bc_hi << 8) | bc_lo;
2810 /* shall be cleared to zero, indicating xfer of data */
2811 if (ireason & (1 << 0))
2814 /* make sure transfer direction matches expected */
2815 i_write = ((ireason & (1 << 1)) == 0) ? 1 : 0;
2816 if (do_write != i_write)
2819 VPRINTK("ata%u: xfering %d bytes\n", ap->id, bytes);
2821 __atapi_pio_bytes(qc, bytes);
2826 printk(KERN_INFO "ata%u: dev %u: ATAPI check failed\n",
2827 ap->id, dev->devno);
2828 ap->hsm_task_state = HSM_ST_ERR;
2836 * None. (executing in kernel thread context)
2839 static void ata_pio_block(struct ata_port *ap)
2841 struct ata_queued_cmd *qc;
2845 * This is purely hueristic. This is a fast path.
2846 * Sometimes when we enter, BSY will be cleared in
2847 * a chk-status or two. If not, the drive is probably seeking
2848 * or something. Snooze for a couple msecs, then
2849 * chk-status again. If still busy, fall back to
2850 * HSM_ST_POLL state.
2852 status = ata_busy_wait(ap, ATA_BUSY, 5);
2853 if (status & ATA_BUSY) {
2855 status = ata_busy_wait(ap, ATA_BUSY, 10);
2856 if (status & ATA_BUSY) {
2857 ap->hsm_task_state = HSM_ST_POLL;
2858 ap->pio_task_timeout = jiffies + ATA_TMOUT_PIO;
2863 qc = ata_qc_from_tag(ap, ap->active_tag);
2866 if (is_atapi_taskfile(&qc->tf)) {
2867 /* no more data to transfer or unsupported ATAPI command */
2868 if ((status & ATA_DRQ) == 0) {
2869 ap->hsm_task_state = HSM_ST_LAST;
2873 atapi_pio_bytes(qc);
2875 /* handle BSY=0, DRQ=0 as error */
2876 if ((status & ATA_DRQ) == 0) {
2877 ap->hsm_task_state = HSM_ST_ERR;
2885 static void ata_pio_error(struct ata_port *ap)
2887 struct ata_queued_cmd *qc;
2890 qc = ata_qc_from_tag(ap, ap->active_tag);
2893 drv_stat = ata_chk_status(ap);
2894 printk(KERN_WARNING "ata%u: PIO error, drv_stat 0x%x\n",
2897 ap->hsm_task_state = HSM_ST_IDLE;
2899 ata_poll_qc_complete(qc, drv_stat | ATA_ERR);
2902 static void ata_pio_task(void *_data)
2904 struct ata_port *ap = _data;
2905 unsigned long timeout;
2912 switch (ap->hsm_task_state) {
2921 qc_completed = ata_pio_complete(ap);
2925 case HSM_ST_LAST_POLL:
2926 timeout = ata_pio_poll(ap);
2936 queue_delayed_work(ata_wq, &ap->pio_task, timeout);
2937 else if (!qc_completed)
2941 static void atapi_request_sense(struct ata_port *ap, struct ata_device *dev,
2942 struct scsi_cmnd *cmd)
2944 DECLARE_COMPLETION(wait);
2945 struct ata_queued_cmd *qc;
2946 unsigned long flags;
2949 DPRINTK("ATAPI request sense\n");
2951 qc = ata_qc_new_init(ap, dev);
2954 /* FIXME: is this needed? */
2955 memset(cmd->sense_buffer, 0, sizeof(cmd->sense_buffer));
2957 ata_sg_init_one(qc, cmd->sense_buffer, sizeof(cmd->sense_buffer));
2958 qc->dma_dir = DMA_FROM_DEVICE;
2960 memset(&qc->cdb, 0, ap->cdb_len);
2961 qc->cdb[0] = REQUEST_SENSE;
2962 qc->cdb[4] = SCSI_SENSE_BUFFERSIZE;
2964 qc->tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
2965 qc->tf.command = ATA_CMD_PACKET;
2967 qc->tf.protocol = ATA_PROT_ATAPI;
2968 qc->tf.lbam = (8 * 1024) & 0xff;
2969 qc->tf.lbah = (8 * 1024) >> 8;
2970 qc->nbytes = SCSI_SENSE_BUFFERSIZE;
2972 qc->waiting = &wait;
2973 qc->complete_fn = ata_qc_complete_noop;
2975 spin_lock_irqsave(&ap->host_set->lock, flags);
2976 rc = ata_qc_issue(qc);
2977 spin_unlock_irqrestore(&ap->host_set->lock, flags);
2980 ata_port_disable(ap);
2982 wait_for_completion(&wait);
2988 * ata_qc_timeout - Handle timeout of queued command
2989 * @qc: Command that timed out
2991 * Some part of the kernel (currently, only the SCSI layer)
2992 * has noticed that the active command on port @ap has not
2993 * completed after a specified length of time. Handle this
2994 * condition by disabling DMA (if necessary) and completing
2995 * transactions, with error if necessary.
2997 * This also handles the case of the "lost interrupt", where
2998 * for some reason (possibly hardware bug, possibly driver bug)
2999 * an interrupt was not delivered to the driver, even though the
3000 * transaction completed successfully.
3003 * Inherited from SCSI layer (none, can sleep)
3006 static void ata_qc_timeout(struct ata_queued_cmd *qc)
3008 struct ata_port *ap = qc->ap;
3009 struct ata_host_set *host_set = ap->host_set;
3010 struct ata_device *dev = qc->dev;
3011 u8 host_stat = 0, drv_stat;
3012 unsigned long flags;
3016 /* FIXME: doesn't this conflict with timeout handling? */
3017 if (qc->dev->class == ATA_DEV_ATAPI && qc->scsicmd) {
3018 struct scsi_cmnd *cmd = qc->scsicmd;
3020 if (!(cmd->eh_eflags & SCSI_EH_CANCEL_CMD)) {
3022 /* finish completing original command */
3023 spin_lock_irqsave(&host_set->lock, flags);
3024 __ata_qc_complete(qc);
3025 spin_unlock_irqrestore(&host_set->lock, flags);
3027 atapi_request_sense(ap, dev, cmd);
3029 cmd->result = (CHECK_CONDITION << 1) | (DID_OK << 16);
3030 scsi_finish_command(cmd);
3036 spin_lock_irqsave(&host_set->lock, flags);
3038 /* hack alert! We cannot use the supplied completion
3039 * function from inside the ->eh_strategy_handler() thread.
3040 * libata is the only user of ->eh_strategy_handler() in
3041 * any kernel, so the default scsi_done() assumes it is
3042 * not being called from the SCSI EH.
3044 qc->scsidone = scsi_finish_command;
3046 switch (qc->tf.protocol) {
3049 case ATA_PROT_ATAPI_DMA:
3050 host_stat = ap->ops->bmdma_status(ap);
3052 /* before we do anything else, clear DMA-Start bit */
3053 ap->ops->bmdma_stop(qc);
3059 drv_stat = ata_chk_status(ap);
3061 /* ack bmdma irq events */
3062 ap->ops->irq_clear(ap);
3064 printk(KERN_ERR "ata%u: command 0x%x timeout, stat 0x%x host_stat 0x%x\n",
3065 ap->id, qc->tf.command, drv_stat, host_stat);
3067 ap->hsm_task_state = HSM_ST_IDLE;
3069 /* complete taskfile transaction */
3070 ata_qc_complete(qc, drv_stat);
3074 spin_unlock_irqrestore(&host_set->lock, flags);
3081 * ata_eng_timeout - Handle timeout of queued command
3082 * @ap: Port on which timed-out command is active
3084 * Some part of the kernel (currently, only the SCSI layer)
3085 * has noticed that the active command on port @ap has not
3086 * completed after a specified length of time. Handle this
3087 * condition by disabling DMA (if necessary) and completing
3088 * transactions, with error if necessary.
3090 * This also handles the case of the "lost interrupt", where
3091 * for some reason (possibly hardware bug, possibly driver bug)
3092 * an interrupt was not delivered to the driver, even though the
3093 * transaction completed successfully.
3096 * Inherited from SCSI layer (none, can sleep)
3099 void ata_eng_timeout(struct ata_port *ap)
3101 struct ata_queued_cmd *qc;
3105 qc = ata_qc_from_tag(ap, ap->active_tag);
3107 printk(KERN_ERR "ata%u: BUG: timeout without command\n",
3119 * ata_qc_new - Request an available ATA command, for queueing
3120 * @ap: Port associated with device @dev
3121 * @dev: Device from whom we request an available command structure
3127 static struct ata_queued_cmd *ata_qc_new(struct ata_port *ap)
3129 struct ata_queued_cmd *qc = NULL;
3132 for (i = 0; i < ATA_MAX_QUEUE; i++)
3133 if (!test_and_set_bit(i, &ap->qactive)) {
3134 qc = ata_qc_from_tag(ap, i);
3145 * ata_qc_new_init - Request an available ATA command, and initialize it
3146 * @ap: Port associated with device @dev
3147 * @dev: Device from whom we request an available command structure
3153 struct ata_queued_cmd *ata_qc_new_init(struct ata_port *ap,
3154 struct ata_device *dev)
3156 struct ata_queued_cmd *qc;
3158 qc = ata_qc_new(ap);
3165 qc->cursect = qc->cursg = qc->cursg_ofs = 0;
3167 qc->nbytes = qc->curbytes = 0;
3169 ata_tf_init(ap, &qc->tf, dev->devno);
3171 if (dev->flags & ATA_DFLAG_LBA48)
3172 qc->tf.flags |= ATA_TFLAG_LBA48;
3178 static int ata_qc_complete_noop(struct ata_queued_cmd *qc, u8 drv_stat)
3183 static void __ata_qc_complete(struct ata_queued_cmd *qc)
3185 struct ata_port *ap = qc->ap;
3186 unsigned int tag, do_clear = 0;
3190 if (likely(ata_tag_valid(tag))) {
3191 if (tag == ap->active_tag)
3192 ap->active_tag = ATA_TAG_POISON;
3193 qc->tag = ATA_TAG_POISON;
3198 struct completion *waiting = qc->waiting;
3203 if (likely(do_clear))
3204 clear_bit(tag, &ap->qactive);
3208 * ata_qc_free - free unused ata_queued_cmd
3209 * @qc: Command to complete
3211 * Designed to free unused ata_queued_cmd object
3212 * in case something prevents using it.
3215 * spin_lock_irqsave(host_set lock)
3218 void ata_qc_free(struct ata_queued_cmd *qc)
3220 assert(qc != NULL); /* ata_qc_from_tag _might_ return NULL */
3221 assert(qc->waiting == NULL); /* nothing should be waiting */
3223 __ata_qc_complete(qc);
3227 * ata_qc_complete - Complete an active ATA command
3228 * @qc: Command to complete
3229 * @drv_stat: ATA Status register contents
3231 * Indicate to the mid and upper layers that an ATA
3232 * command has completed, with either an ok or not-ok status.
3235 * spin_lock_irqsave(host_set lock)
3239 void ata_qc_complete(struct ata_queued_cmd *qc, u8 drv_stat)
3243 assert(qc != NULL); /* ata_qc_from_tag _might_ return NULL */
3244 assert(qc->flags & ATA_QCFLAG_ACTIVE);
3246 if (likely(qc->flags & ATA_QCFLAG_DMAMAP))
3249 /* atapi: mark qc as inactive to prevent the interrupt handler
3250 * from completing the command twice later, before the error handler
3251 * is called. (when rc != 0 and atapi request sense is needed)
3253 qc->flags &= ~ATA_QCFLAG_ACTIVE;
3255 /* call completion callback */
3256 rc = qc->complete_fn(qc, drv_stat);
3258 /* if callback indicates not to complete command (non-zero),
3259 * return immediately
3264 __ata_qc_complete(qc);
3269 static inline int ata_should_dma_map(struct ata_queued_cmd *qc)
3271 struct ata_port *ap = qc->ap;
3273 switch (qc->tf.protocol) {
3275 case ATA_PROT_ATAPI_DMA:
3278 case ATA_PROT_ATAPI:
3280 case ATA_PROT_PIO_MULT:
3281 if (ap->flags & ATA_FLAG_PIO_DMA)
3294 * ata_qc_issue - issue taskfile to device
3295 * @qc: command to issue to device
3297 * Prepare an ATA command to submission to device.
3298 * This includes mapping the data into a DMA-able
3299 * area, filling in the S/G table, and finally
3300 * writing the taskfile to hardware, starting the command.
3303 * spin_lock_irqsave(host_set lock)
3306 * Zero on success, negative on error.
3309 int ata_qc_issue(struct ata_queued_cmd *qc)
3311 struct ata_port *ap = qc->ap;
3313 if (ata_should_dma_map(qc)) {
3314 if (qc->flags & ATA_QCFLAG_SG) {
3315 if (ata_sg_setup(qc))
3317 } else if (qc->flags & ATA_QCFLAG_SINGLE) {
3318 if (ata_sg_setup_one(qc))
3322 qc->flags &= ~ATA_QCFLAG_DMAMAP;
3325 ap->ops->qc_prep(qc);
3327 qc->ap->active_tag = qc->tag;
3328 qc->flags |= ATA_QCFLAG_ACTIVE;
3330 return ap->ops->qc_issue(qc);
3338 * ata_qc_issue_prot - issue taskfile to device in proto-dependent manner
3339 * @qc: command to issue to device
3341 * Using various libata functions and hooks, this function
3342 * starts an ATA command. ATA commands are grouped into
3343 * classes called "protocols", and issuing each type of protocol
3344 * is slightly different.
3346 * May be used as the qc_issue() entry in ata_port_operations.
3349 * spin_lock_irqsave(host_set lock)
3352 * Zero on success, negative on error.
3355 int ata_qc_issue_prot(struct ata_queued_cmd *qc)
3357 struct ata_port *ap = qc->ap;
3359 /* Use polling pio if the LLD doesn't handle
3360 * interrupt driven pio and atapi CDB interrupt.
3362 if (ap->flags & ATA_FLAG_PIO_POLLING) {
3363 switch (qc->tf.protocol) {
3365 case ATA_PROT_ATAPI:
3366 case ATA_PROT_ATAPI_NODATA:
3367 qc->tf.flags |= ATA_TFLAG_POLLING;
3369 case ATA_PROT_ATAPI_DMA:
3370 if (qc->dev->flags & ATA_DFLAG_CDB_INTR)
3378 /* select the device */
3379 ata_dev_select(ap, qc->dev->devno, 1, 0);
3381 /* start the command */
3382 switch (qc->tf.protocol) {
3383 case ATA_PROT_NODATA:
3384 if (qc->tf.flags & ATA_TFLAG_POLLING)
3385 ata_qc_set_polling(qc);
3387 ata_tf_to_host_nolock(ap, &qc->tf);
3388 ap->hsm_task_state = HSM_ST_LAST;
3390 if (qc->tf.flags & ATA_TFLAG_POLLING)
3391 queue_work(ata_wq, &ap->pio_task);
3396 assert(!(qc->tf.flags & ATA_TFLAG_POLLING));
3398 ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
3399 ap->ops->bmdma_setup(qc); /* set up bmdma */
3400 ap->ops->bmdma_start(qc); /* initiate bmdma */
3401 ap->hsm_task_state = HSM_ST_LAST;
3405 if (qc->tf.flags & ATA_TFLAG_POLLING)
3406 ata_qc_set_polling(qc);
3408 ata_tf_to_host_nolock(ap, &qc->tf);
3410 if (qc->tf.flags & ATA_TFLAG_POLLING) {
3412 ap->hsm_task_state = HSM_ST;
3413 queue_work(ata_wq, &ap->pio_task);
3415 /* interrupt driven PIO */
3416 if (qc->tf.flags & ATA_TFLAG_WRITE) {
3417 /* PIO data out protocol */
3418 ap->hsm_task_state = HSM_ST_FIRST;
3419 queue_work(ata_wq, &ap->packet_task);
3421 /* send first data block by polling */
3423 /* PIO data in protocol */
3424 ap->hsm_task_state = HSM_ST;
3426 /* interrupt handler takes over from here */
3432 case ATA_PROT_ATAPI:
3433 case ATA_PROT_ATAPI_NODATA:
3434 if (qc->tf.flags & ATA_TFLAG_POLLING)
3435 ata_qc_set_polling(qc);
3437 ata_tf_to_host_nolock(ap, &qc->tf);
3438 ap->hsm_task_state = HSM_ST_FIRST;
3440 /* send cdb by polling if no cdb interrupt */
3441 if ((!(qc->dev->flags & ATA_DFLAG_CDB_INTR)) ||
3442 (qc->tf.flags & ATA_TFLAG_POLLING))
3443 queue_work(ata_wq, &ap->packet_task);
3446 case ATA_PROT_ATAPI_DMA:
3447 assert(!(qc->tf.flags & ATA_TFLAG_POLLING));
3449 ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
3450 ap->ops->bmdma_setup(qc); /* set up bmdma */
3451 ap->hsm_task_state = HSM_ST_FIRST;
3453 /* send cdb by polling if no cdb interrupt */
3454 if (!(qc->dev->flags & ATA_DFLAG_CDB_INTR))
3455 queue_work(ata_wq, &ap->packet_task);
3467 * ata_bmdma_setup_mmio - Set up PCI IDE BMDMA transaction
3468 * @qc: Info associated with this ATA transaction.
3471 * spin_lock_irqsave(host_set lock)
3474 static void ata_bmdma_setup_mmio (struct ata_queued_cmd *qc)
3476 struct ata_port *ap = qc->ap;
3477 unsigned int rw = (qc->tf.flags & ATA_TFLAG_WRITE);
3479 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
3481 /* load PRD table addr. */
3482 mb(); /* make sure PRD table writes are visible to controller */
3483 writel(ap->prd_dma, mmio + ATA_DMA_TABLE_OFS);
3485 /* specify data direction, triple-check start bit is clear */
3486 dmactl = readb(mmio + ATA_DMA_CMD);
3487 dmactl &= ~(ATA_DMA_WR | ATA_DMA_START);
3489 dmactl |= ATA_DMA_WR;
3490 writeb(dmactl, mmio + ATA_DMA_CMD);
3492 /* issue r/w command */
3493 ap->ops->exec_command(ap, &qc->tf);
3497 * ata_bmdma_start_mmio - Start a PCI IDE BMDMA transaction
3498 * @qc: Info associated with this ATA transaction.
3501 * spin_lock_irqsave(host_set lock)
3504 static void ata_bmdma_start_mmio (struct ata_queued_cmd *qc)
3506 struct ata_port *ap = qc->ap;
3507 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
3510 /* start host DMA transaction */
3511 dmactl = readb(mmio + ATA_DMA_CMD);
3512 writeb(dmactl | ATA_DMA_START, mmio + ATA_DMA_CMD);
3514 /* Strictly, one may wish to issue a readb() here, to
3515 * flush the mmio write. However, control also passes
3516 * to the hardware at this point, and it will interrupt
3517 * us when we are to resume control. So, in effect,
3518 * we don't care when the mmio write flushes.
3519 * Further, a read of the DMA status register _immediately_
3520 * following the write may not be what certain flaky hardware
3521 * is expected, so I think it is best to not add a readb()
3522 * without first all the MMIO ATA cards/mobos.
3523 * Or maybe I'm just being paranoid.
3528 * ata_bmdma_setup_pio - Set up PCI IDE BMDMA transaction (PIO)
3529 * @qc: Info associated with this ATA transaction.
3532 * spin_lock_irqsave(host_set lock)
3535 static void ata_bmdma_setup_pio (struct ata_queued_cmd *qc)
3537 struct ata_port *ap = qc->ap;
3538 unsigned int rw = (qc->tf.flags & ATA_TFLAG_WRITE);
3541 /* load PRD table addr. */
3542 outl(ap->prd_dma, ap->ioaddr.bmdma_addr + ATA_DMA_TABLE_OFS);
3544 /* specify data direction, triple-check start bit is clear */
3545 dmactl = inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3546 dmactl &= ~(ATA_DMA_WR | ATA_DMA_START);
3548 dmactl |= ATA_DMA_WR;
3549 outb(dmactl, ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3551 /* issue r/w command */
3552 ap->ops->exec_command(ap, &qc->tf);
3556 * ata_bmdma_start_pio - Start a PCI IDE BMDMA transaction (PIO)
3557 * @qc: Info associated with this ATA transaction.
3560 * spin_lock_irqsave(host_set lock)
3563 static void ata_bmdma_start_pio (struct ata_queued_cmd *qc)
3565 struct ata_port *ap = qc->ap;
3568 /* start host DMA transaction */
3569 dmactl = inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3570 outb(dmactl | ATA_DMA_START,
3571 ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3576 * ata_bmdma_start - Start a PCI IDE BMDMA transaction
3577 * @qc: Info associated with this ATA transaction.
3579 * Writes the ATA_DMA_START flag to the DMA command register.
3581 * May be used as the bmdma_start() entry in ata_port_operations.
3584 * spin_lock_irqsave(host_set lock)
3586 void ata_bmdma_start(struct ata_queued_cmd *qc)
3588 if (qc->ap->flags & ATA_FLAG_MMIO)
3589 ata_bmdma_start_mmio(qc);
3591 ata_bmdma_start_pio(qc);
3596 * ata_bmdma_setup - Set up PCI IDE BMDMA transaction
3597 * @qc: Info associated with this ATA transaction.
3599 * Writes address of PRD table to device's PRD Table Address
3600 * register, sets the DMA control register, and calls
3601 * ops->exec_command() to start the transfer.
3603 * May be used as the bmdma_setup() entry in ata_port_operations.
3606 * spin_lock_irqsave(host_set lock)
3608 void ata_bmdma_setup(struct ata_queued_cmd *qc)
3610 if (qc->ap->flags & ATA_FLAG_MMIO)
3611 ata_bmdma_setup_mmio(qc);
3613 ata_bmdma_setup_pio(qc);
3618 * ata_bmdma_irq_clear - Clear PCI IDE BMDMA interrupt.
3619 * @ap: Port associated with this ATA transaction.
3621 * Clear interrupt and error flags in DMA status register.
3623 * May be used as the irq_clear() entry in ata_port_operations.
3626 * spin_lock_irqsave(host_set lock)
3629 void ata_bmdma_irq_clear(struct ata_port *ap)
3631 if (ap->flags & ATA_FLAG_MMIO) {
3632 void __iomem *mmio = ((void __iomem *) ap->ioaddr.bmdma_addr) + ATA_DMA_STATUS;
3633 writeb(readb(mmio), mmio);
3635 unsigned long addr = ap->ioaddr.bmdma_addr + ATA_DMA_STATUS;
3636 outb(inb(addr), addr);
3643 * ata_bmdma_status - Read PCI IDE BMDMA status
3644 * @ap: Port associated with this ATA transaction.
3646 * Read and return BMDMA status register.
3648 * May be used as the bmdma_status() entry in ata_port_operations.
3651 * spin_lock_irqsave(host_set lock)
3654 u8 ata_bmdma_status(struct ata_port *ap)
3657 if (ap->flags & ATA_FLAG_MMIO) {
3658 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
3659 host_stat = readb(mmio + ATA_DMA_STATUS);
3661 host_stat = inb(ap->ioaddr.bmdma_addr + ATA_DMA_STATUS);
3667 * ata_bmdma_stop - Stop PCI IDE BMDMA transfer
3668 * @qc: Command we are ending DMA for
3670 * Clears the ATA_DMA_START flag in the dma control register
3672 * May be used as the bmdma_stop() entry in ata_port_operations.
3675 * spin_lock_irqsave(host_set lock)
3678 void ata_bmdma_stop(struct ata_queued_cmd *qc)
3680 struct ata_port *ap = qc->ap;
3681 if (ap->flags & ATA_FLAG_MMIO) {
3682 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
3684 /* clear start/stop bit */
3685 writeb(readb(mmio + ATA_DMA_CMD) & ~ATA_DMA_START,
3686 mmio + ATA_DMA_CMD);
3688 /* clear start/stop bit */
3689 outb(inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD) & ~ATA_DMA_START,
3690 ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3693 /* one-PIO-cycle guaranteed wait, per spec, for HDMA1:0 transition */
3694 ata_altstatus(ap); /* dummy read */
3698 * atapi_send_cdb - Write CDB bytes to hardware
3699 * @ap: Port to which ATAPI device is attached.
3700 * @qc: Taskfile currently active
3702 * When device has indicated its readiness to accept
3703 * a CDB, this function is called. Send the CDB.
3709 static void atapi_send_cdb(struct ata_port *ap, struct ata_queued_cmd *qc)
3712 DPRINTK("send cdb\n");
3713 assert(ap->cdb_len >= 12);
3715 ata_data_xfer(ap, qc->cdb, ap->cdb_len, 1);
3716 ata_altstatus(ap); /* flush */
3718 switch (qc->tf.protocol) {
3719 case ATA_PROT_ATAPI:
3720 ap->hsm_task_state = HSM_ST;
3722 case ATA_PROT_ATAPI_NODATA:
3723 ap->hsm_task_state = HSM_ST_LAST;
3725 case ATA_PROT_ATAPI_DMA:
3726 ap->hsm_task_state = HSM_ST_LAST;
3727 /* initiate bmdma */
3728 ap->ops->bmdma_start(qc);
3734 * ata_host_intr - Handle host interrupt for given (port, task)
3735 * @ap: Port on which interrupt arrived (possibly...)
3736 * @qc: Taskfile currently active in engine
3738 * Handle host interrupt for given queued command. Currently,
3739 * only DMA interrupts are handled. All other commands are
3740 * handled via polling with interrupts disabled (nIEN bit).
3743 * spin_lock_irqsave(host_set lock)
3746 * One if interrupt was handled, zero if not (shared irq).
3749 inline unsigned int ata_host_intr (struct ata_port *ap,
3750 struct ata_queued_cmd *qc)
3752 u8 status, host_stat = 0;
3754 VPRINTK("ata%u: protocol %d task_state %d\n",
3755 ap->id, qc->tf.protocol, ap->hsm_task_state);
3757 /* Check whether we are expecting interrupt in this state */
3758 switch (ap->hsm_task_state) {
3760 /* Check the ATA_DFLAG_CDB_INTR flag is enough here.
3761 * The flag was turned on only for atapi devices.
3762 * No need to check is_atapi_taskfile(&qc->tf) again.
3764 if (!(qc->dev->flags & ATA_DFLAG_CDB_INTR))
3768 if (qc->tf.protocol == ATA_PROT_DMA ||
3769 qc->tf.protocol == ATA_PROT_ATAPI_DMA) {
3770 /* check status of DMA engine */
3771 host_stat = ap->ops->bmdma_status(ap);
3772 VPRINTK("ata%u: host_stat 0x%X\n", ap->id, host_stat);
3774 /* if it's not our irq... */
3775 if (!(host_stat & ATA_DMA_INTR))
3778 /* before we do anything else, clear DMA-Start bit */
3779 ap->ops->bmdma_stop(qc);
3788 /* check altstatus */
3789 status = ata_altstatus(ap);
3790 if (status & ATA_BUSY)
3793 /* check main status, clearing INTRQ */
3794 status = ata_chk_status(ap);
3795 if (unlikely(status & ATA_BUSY))
3798 DPRINTK("ata%u: protocol %d task_state %d (dev_stat 0x%X)\n",
3799 ap->id, qc->tf.protocol, ap->hsm_task_state, status);
3801 /* ack bmdma irq events */
3802 ap->ops->irq_clear(ap);
3805 if (unlikely((status & ATA_ERR) || (host_stat & ATA_DMA_ERR)))
3806 ap->hsm_task_state = HSM_ST_ERR;
3809 switch (ap->hsm_task_state) {
3811 /* Some pre-ATAPI-4 devices assert INTRQ
3812 * at this state when ready to receive CDB.
3815 /* check device status */
3816 if (unlikely((status & (ATA_BUSY | ATA_DRQ)) != ATA_DRQ)) {
3817 /* Wrong status. Let EH handle this */
3818 ap->hsm_task_state = HSM_ST_ERR;
3822 atapi_send_cdb(ap, qc);
3827 /* complete command or read/write the data register */
3828 if (qc->tf.protocol == ATA_PROT_ATAPI) {
3829 /* ATAPI PIO protocol */
3830 if ((status & ATA_DRQ) == 0) {
3831 /* no more data to transfer */
3832 ap->hsm_task_state = HSM_ST_LAST;
3836 atapi_pio_bytes(qc);
3838 if (unlikely(ap->hsm_task_state == HSM_ST_ERR))
3839 /* bad ireason reported by device */
3843 /* ATA PIO protocol */
3844 if (unlikely((status & ATA_DRQ) == 0)) {
3845 /* handle BSY=0, DRQ=0 as error */
3846 ap->hsm_task_state = HSM_ST_ERR;
3852 if (ap->hsm_task_state == HSM_ST_LAST &&
3853 (!(qc->tf.flags & ATA_TFLAG_WRITE))) {
3856 status = ata_chk_status(ap);
3861 ata_altstatus(ap); /* flush */
3865 if (unlikely(status & ATA_DRQ)) {
3866 /* handle DRQ=1 as error */
3867 ap->hsm_task_state = HSM_ST_ERR;
3871 /* no more data to transfer */
3872 DPRINTK("ata%u: command complete, drv_stat 0x%x\n",
3875 ap->hsm_task_state = HSM_ST_IDLE;
3877 /* complete taskfile transaction */
3878 ata_qc_complete(qc, status);
3882 printk(KERN_ERR "ata%u: command error, drv_stat 0x%x host_stat 0x%x\n",
3883 ap->id, status, host_stat);
3885 ap->hsm_task_state = HSM_ST_IDLE;
3886 ata_qc_complete(qc, status | ATA_ERR);
3892 return 1; /* irq handled */
3895 ap->stats.idle_irq++;
3898 if ((ap->stats.idle_irq % 1000) == 0) {
3900 ata_irq_ack(ap, 0); /* debug trap */
3901 printk(KERN_WARNING "ata%d: irq trap\n", ap->id);
3904 return 0; /* irq not handled */
3908 * ata_interrupt - Default ATA host interrupt handler
3909 * @irq: irq line (unused)
3910 * @dev_instance: pointer to our ata_host_set information structure
3913 * Default interrupt handler for PCI IDE devices. Calls
3914 * ata_host_intr() for each port that is not disabled.
3917 * Obtains host_set lock during operation.
3920 * IRQ_NONE or IRQ_HANDLED.
3924 irqreturn_t ata_interrupt (int irq, void *dev_instance, struct pt_regs *regs)
3926 struct ata_host_set *host_set = dev_instance;
3928 unsigned int handled = 0;
3929 unsigned long flags;
3931 /* TODO: make _irqsave conditional on x86 PCI IDE legacy mode */
3932 spin_lock_irqsave(&host_set->lock, flags);
3934 for (i = 0; i < host_set->n_ports; i++) {
3935 struct ata_port *ap;
3937 ap = host_set->ports[i];
3939 !(ap->flags & ATA_FLAG_PORT_DISABLED)) {
3940 struct ata_queued_cmd *qc;
3942 qc = ata_qc_from_tag(ap, ap->active_tag);
3943 if (qc && (!(qc->tf.flags & ATA_TFLAG_POLLING)) &&
3944 (qc->flags & ATA_QCFLAG_ACTIVE))
3945 handled |= ata_host_intr(ap, qc);
3949 spin_unlock_irqrestore(&host_set->lock, flags);
3951 return IRQ_RETVAL(handled);
3955 * atapi_packet_task - Write CDB bytes to hardware
3956 * @_data: Port to which ATAPI device is attached.
3958 * When device has indicated its readiness to accept
3959 * a CDB, this function is called. Send the CDB.
3960 * If DMA is to be performed, exit immediately.
3961 * Otherwise, we are in polling mode, so poll
3962 * status under operation succeeds or fails.
3965 * Kernel thread context (may sleep)
3968 static void atapi_packet_task(void *_data)
3970 struct ata_port *ap = _data;
3971 struct ata_queued_cmd *qc;
3973 unsigned long flags;
3975 qc = ata_qc_from_tag(ap, ap->active_tag);
3977 assert(qc->flags & ATA_QCFLAG_ACTIVE);
3979 /* sleep-wait for BSY to clear */
3980 DPRINTK("busy wait\n");
3981 if (ata_busy_sleep(ap, ATA_TMOUT_CDB_QUICK, ATA_TMOUT_CDB))
3984 /* make sure DRQ is set */
3985 status = ata_chk_status(ap);
3986 if ((status & (ATA_BUSY | ATA_DRQ)) != ATA_DRQ)
3989 /* Send the CDB (atapi) or the first data block (ata pio out).
3990 * During the state transition, interrupt handler shouldn't
3991 * be invoked before the data transfer is complete and
3992 * hsm_task_state is changed. Hence, the following locking.
3994 spin_lock_irqsave(&ap->host_set->lock, flags);
3996 if (is_atapi_taskfile(&qc->tf)) {
3998 atapi_send_cdb(ap, qc);
4000 if (qc->tf.flags & ATA_TFLAG_POLLING)
4001 queue_work(ata_wq, &ap->pio_task);
4003 /* PIO data out protocol.
4004 * send first data block.
4007 /* ata_pio_sector() might change the state to HSM_ST_LAST.
4008 * so, the state is changed here before ata_pio_sector().
4010 ap->hsm_task_state = HSM_ST;
4012 ata_altstatus(ap); /* flush */
4014 /* interrupt handler takes over from here */
4017 spin_unlock_irqrestore(&ap->host_set->lock, flags);
4027 * ata_port_start - Set port up for dma.
4028 * @ap: Port to initialize
4030 * Called just after data structures for each port are
4031 * initialized. Allocates space for PRD table.
4033 * May be used as the port_start() entry in ata_port_operations.
4038 int ata_port_start (struct ata_port *ap)
4040 struct device *dev = ap->host_set->dev;
4042 ap->prd = dma_alloc_coherent(dev, ATA_PRD_TBL_SZ, &ap->prd_dma, GFP_KERNEL);
4046 DPRINTK("prd alloc, virt %p, dma %llx\n", ap->prd, (unsigned long long) ap->prd_dma);
4053 * ata_port_stop - Undo ata_port_start()
4054 * @ap: Port to shut down
4056 * Frees the PRD table.
4058 * May be used as the port_stop() entry in ata_port_operations.
4063 void ata_port_stop (struct ata_port *ap)
4065 struct device *dev = ap->host_set->dev;
4067 dma_free_coherent(dev, ATA_PRD_TBL_SZ, ap->prd, ap->prd_dma);
4070 void ata_host_stop (struct ata_host_set *host_set)
4072 if (host_set->mmio_base)
4073 iounmap(host_set->mmio_base);
4078 * ata_host_remove - Unregister SCSI host structure with upper layers
4079 * @ap: Port to unregister
4080 * @do_unregister: 1 if we fully unregister, 0 to just stop the port
4085 static void ata_host_remove(struct ata_port *ap, unsigned int do_unregister)
4087 struct Scsi_Host *sh = ap->host;
4092 scsi_remove_host(sh);
4094 ap->ops->port_stop(ap);
4098 * ata_host_init - Initialize an ata_port structure
4099 * @ap: Structure to initialize
4100 * @host: associated SCSI mid-layer structure
4101 * @host_set: Collection of hosts to which @ap belongs
4102 * @ent: Probe information provided by low-level driver
4103 * @port_no: Port number associated with this ata_port
4105 * Initialize a new ata_port structure, and its associated
4109 * Inherited from caller.
4113 static void ata_host_init(struct ata_port *ap, struct Scsi_Host *host,
4114 struct ata_host_set *host_set,
4115 struct ata_probe_ent *ent, unsigned int port_no)
4121 host->max_channel = 1;
4122 host->unique_id = ata_unique_id++;
4123 host->max_cmd_len = 12;
4125 scsi_assign_lock(host, &host_set->lock);
4127 ap->flags = ATA_FLAG_PORT_DISABLED;
4128 ap->id = host->unique_id;
4130 ap->ctl = ATA_DEVCTL_OBS;
4131 ap->host_set = host_set;
4132 ap->port_no = port_no;
4134 ent->legacy_mode ? ent->hard_port_no : port_no;
4135 ap->pio_mask = ent->pio_mask;
4136 ap->mwdma_mask = ent->mwdma_mask;
4137 ap->udma_mask = ent->udma_mask;
4138 ap->flags |= ent->host_flags;
4139 ap->ops = ent->port_ops;
4140 ap->cbl = ATA_CBL_NONE;
4141 ap->active_tag = ATA_TAG_POISON;
4142 ap->last_ctl = 0xFF;
4144 INIT_WORK(&ap->packet_task, atapi_packet_task, ap);
4145 INIT_WORK(&ap->pio_task, ata_pio_task, ap);
4147 for (i = 0; i < ATA_MAX_DEVICES; i++)
4148 ap->device[i].devno = i;
4151 ap->stats.unhandled_irq = 1;
4152 ap->stats.idle_irq = 1;
4155 memcpy(&ap->ioaddr, &ent->port[port_no], sizeof(struct ata_ioports));
4159 * ata_host_add - Attach low-level ATA driver to system
4160 * @ent: Information provided by low-level driver
4161 * @host_set: Collections of ports to which we add
4162 * @port_no: Port number associated with this host
4164 * Attach low-level ATA driver to system.
4167 * PCI/etc. bus probe sem.
4170 * New ata_port on success, for NULL on error.
4174 static struct ata_port * ata_host_add(struct ata_probe_ent *ent,
4175 struct ata_host_set *host_set,
4176 unsigned int port_no)
4178 struct Scsi_Host *host;
4179 struct ata_port *ap;
4183 host = scsi_host_alloc(ent->sht, sizeof(struct ata_port));
4187 ap = (struct ata_port *) &host->hostdata[0];
4189 ata_host_init(ap, host, host_set, ent, port_no);
4191 rc = ap->ops->port_start(ap);
4198 scsi_host_put(host);
4203 * ata_device_add - Register hardware device with ATA and SCSI layers
4204 * @ent: Probe information describing hardware device to be registered
4206 * This function processes the information provided in the probe
4207 * information struct @ent, allocates the necessary ATA and SCSI
4208 * host information structures, initializes them, and registers
4209 * everything with requisite kernel subsystems.
4211 * This function requests irqs, probes the ATA bus, and probes
4215 * PCI/etc. bus probe sem.
4218 * Number of ports registered. Zero on error (no ports registered).
4222 int ata_device_add(struct ata_probe_ent *ent)
4224 unsigned int count = 0, i;
4225 struct device *dev = ent->dev;
4226 struct ata_host_set *host_set;
4229 /* alloc a container for our list of ATA ports (buses) */
4230 host_set = kmalloc(sizeof(struct ata_host_set) +
4231 (ent->n_ports * sizeof(void *)), GFP_KERNEL);
4234 memset(host_set, 0, sizeof(struct ata_host_set) + (ent->n_ports * sizeof(void *)));
4235 spin_lock_init(&host_set->lock);
4237 host_set->dev = dev;
4238 host_set->n_ports = ent->n_ports;
4239 host_set->irq = ent->irq;
4240 host_set->mmio_base = ent->mmio_base;
4241 host_set->private_data = ent->private_data;
4242 host_set->ops = ent->port_ops;
4244 /* register each port bound to this device */
4245 for (i = 0; i < ent->n_ports; i++) {
4246 struct ata_port *ap;
4247 unsigned long xfer_mode_mask;
4249 ap = ata_host_add(ent, host_set, i);
4253 host_set->ports[i] = ap;
4254 xfer_mode_mask =(ap->udma_mask << ATA_SHIFT_UDMA) |
4255 (ap->mwdma_mask << ATA_SHIFT_MWDMA) |
4256 (ap->pio_mask << ATA_SHIFT_PIO);
4258 /* print per-port info to dmesg */
4259 printk(KERN_INFO "ata%u: %cATA max %s cmd 0x%lX ctl 0x%lX "
4260 "bmdma 0x%lX irq %lu\n",
4262 ap->flags & ATA_FLAG_SATA ? 'S' : 'P',
4263 ata_mode_string(xfer_mode_mask),
4264 ap->ioaddr.cmd_addr,
4265 ap->ioaddr.ctl_addr,
4266 ap->ioaddr.bmdma_addr,
4270 host_set->ops->irq_clear(ap);
4279 /* obtain irq, that is shared between channels */
4280 if (request_irq(ent->irq, ent->port_ops->irq_handler, ent->irq_flags,
4281 DRV_NAME, host_set))
4284 /* perform each probe synchronously */
4285 DPRINTK("probe begin\n");
4286 for (i = 0; i < count; i++) {
4287 struct ata_port *ap;
4290 ap = host_set->ports[i];
4292 DPRINTK("ata%u: probe begin\n", ap->id);
4293 rc = ata_bus_probe(ap);
4294 DPRINTK("ata%u: probe end\n", ap->id);
4297 /* FIXME: do something useful here?
4298 * Current libata behavior will
4299 * tear down everything when
4300 * the module is removed
4301 * or the h/w is unplugged.
4305 rc = scsi_add_host(ap->host, dev);
4307 printk(KERN_ERR "ata%u: scsi_add_host failed\n",
4309 /* FIXME: do something useful here */
4310 /* FIXME: handle unconditional calls to
4311 * scsi_scan_host and ata_host_remove, below,
4317 /* probes are done, now scan each port's disk(s) */
4318 DPRINTK("probe begin\n");
4319 for (i = 0; i < count; i++) {
4320 struct ata_port *ap = host_set->ports[i];
4322 scsi_scan_host(ap->host);
4325 dev_set_drvdata(dev, host_set);
4327 VPRINTK("EXIT, returning %u\n", ent->n_ports);
4328 return ent->n_ports; /* success */
4331 for (i = 0; i < count; i++) {
4332 ata_host_remove(host_set->ports[i], 1);
4333 scsi_host_put(host_set->ports[i]->host);
4336 VPRINTK("EXIT, returning 0\n");
4341 * ata_host_set_remove - PCI layer callback for device removal
4342 * @host_set: ATA host set that was removed
4344 * Unregister all objects associated with this host set. Free those
4348 * Inherited from calling layer (may sleep).
4352 void ata_host_set_remove(struct ata_host_set *host_set)
4354 struct ata_port *ap;
4357 for (i = 0; i < host_set->n_ports; i++) {
4358 ap = host_set->ports[i];
4359 scsi_remove_host(ap->host);
4362 free_irq(host_set->irq, host_set);
4364 for (i = 0; i < host_set->n_ports; i++) {
4365 ap = host_set->ports[i];
4367 ata_scsi_release(ap->host);
4369 if ((ap->flags & ATA_FLAG_NO_LEGACY) == 0) {
4370 struct ata_ioports *ioaddr = &ap->ioaddr;
4372 if (ioaddr->cmd_addr == 0x1f0)
4373 release_region(0x1f0, 8);
4374 else if (ioaddr->cmd_addr == 0x170)
4375 release_region(0x170, 8);
4378 scsi_host_put(ap->host);
4381 if (host_set->ops->host_stop)
4382 host_set->ops->host_stop(host_set);
4388 * ata_scsi_release - SCSI layer callback hook for host unload
4389 * @host: libata host to be unloaded
4391 * Performs all duties necessary to shut down a libata port...
4392 * Kill port kthread, disable port, and release resources.
4395 * Inherited from SCSI layer.
4401 int ata_scsi_release(struct Scsi_Host *host)
4403 struct ata_port *ap = (struct ata_port *) &host->hostdata[0];
4407 ap->ops->port_disable(ap);
4408 ata_host_remove(ap, 0);
4415 * ata_std_ports - initialize ioaddr with standard port offsets.
4416 * @ioaddr: IO address structure to be initialized
4418 * Utility function which initializes data_addr, error_addr,
4419 * feature_addr, nsect_addr, lbal_addr, lbam_addr, lbah_addr,
4420 * device_addr, status_addr, and command_addr to standard offsets
4421 * relative to cmd_addr.
4423 * Does not set ctl_addr, altstatus_addr, bmdma_addr, or scr_addr.
4426 void ata_std_ports(struct ata_ioports *ioaddr)
4428 ioaddr->data_addr = ioaddr->cmd_addr + ATA_REG_DATA;
4429 ioaddr->error_addr = ioaddr->cmd_addr + ATA_REG_ERR;
4430 ioaddr->feature_addr = ioaddr->cmd_addr + ATA_REG_FEATURE;
4431 ioaddr->nsect_addr = ioaddr->cmd_addr + ATA_REG_NSECT;
4432 ioaddr->lbal_addr = ioaddr->cmd_addr + ATA_REG_LBAL;
4433 ioaddr->lbam_addr = ioaddr->cmd_addr + ATA_REG_LBAM;
4434 ioaddr->lbah_addr = ioaddr->cmd_addr + ATA_REG_LBAH;
4435 ioaddr->device_addr = ioaddr->cmd_addr + ATA_REG_DEVICE;
4436 ioaddr->status_addr = ioaddr->cmd_addr + ATA_REG_STATUS;
4437 ioaddr->command_addr = ioaddr->cmd_addr + ATA_REG_CMD;
4440 static struct ata_probe_ent *
4441 ata_probe_ent_alloc(struct device *dev, struct ata_port_info *port)
4443 struct ata_probe_ent *probe_ent;
4445 probe_ent = kmalloc(sizeof(*probe_ent), GFP_KERNEL);
4447 printk(KERN_ERR DRV_NAME "(%s): out of memory\n",
4448 kobject_name(&(dev->kobj)));
4452 memset(probe_ent, 0, sizeof(*probe_ent));
4454 INIT_LIST_HEAD(&probe_ent->node);
4455 probe_ent->dev = dev;
4457 probe_ent->sht = port->sht;
4458 probe_ent->host_flags = port->host_flags;
4459 probe_ent->pio_mask = port->pio_mask;
4460 probe_ent->mwdma_mask = port->mwdma_mask;
4461 probe_ent->udma_mask = port->udma_mask;
4462 probe_ent->port_ops = port->port_ops;
4471 void ata_pci_host_stop (struct ata_host_set *host_set)
4473 struct pci_dev *pdev = to_pci_dev(host_set->dev);
4475 pci_iounmap(pdev, host_set->mmio_base);
4479 * ata_pci_init_native_mode - Initialize native-mode driver
4480 * @pdev: pci device to be initialized
4481 * @port: array[2] of pointers to port info structures.
4483 * Utility function which allocates and initializes an
4484 * ata_probe_ent structure for a standard dual-port
4485 * PIO-based IDE controller. The returned ata_probe_ent
4486 * structure can be passed to ata_device_add(). The returned
4487 * ata_probe_ent structure should then be freed with kfree().
4490 struct ata_probe_ent *
4491 ata_pci_init_native_mode(struct pci_dev *pdev, struct ata_port_info **port)
4493 struct ata_probe_ent *probe_ent =
4494 ata_probe_ent_alloc(pci_dev_to_dev(pdev), port[0]);
4498 probe_ent->n_ports = 2;
4499 probe_ent->irq = pdev->irq;
4500 probe_ent->irq_flags = SA_SHIRQ;
4502 probe_ent->port[0].cmd_addr = pci_resource_start(pdev, 0);
4503 probe_ent->port[0].altstatus_addr =
4504 probe_ent->port[0].ctl_addr =
4505 pci_resource_start(pdev, 1) | ATA_PCI_CTL_OFS;
4506 probe_ent->port[0].bmdma_addr = pci_resource_start(pdev, 4);
4508 probe_ent->port[1].cmd_addr = pci_resource_start(pdev, 2);
4509 probe_ent->port[1].altstatus_addr =
4510 probe_ent->port[1].ctl_addr =
4511 pci_resource_start(pdev, 3) | ATA_PCI_CTL_OFS;
4512 probe_ent->port[1].bmdma_addr = pci_resource_start(pdev, 4) + 8;
4514 ata_std_ports(&probe_ent->port[0]);
4515 ata_std_ports(&probe_ent->port[1]);
4520 static struct ata_probe_ent *
4521 ata_pci_init_legacy_mode(struct pci_dev *pdev, struct ata_port_info **port,
4522 struct ata_probe_ent **ppe2)
4524 struct ata_probe_ent *probe_ent, *probe_ent2;
4526 probe_ent = ata_probe_ent_alloc(pci_dev_to_dev(pdev), port[0]);
4529 probe_ent2 = ata_probe_ent_alloc(pci_dev_to_dev(pdev), port[1]);
4535 probe_ent->n_ports = 1;
4536 probe_ent->irq = 14;
4538 probe_ent->hard_port_no = 0;
4539 probe_ent->legacy_mode = 1;
4541 probe_ent2->n_ports = 1;
4542 probe_ent2->irq = 15;
4544 probe_ent2->hard_port_no = 1;
4545 probe_ent2->legacy_mode = 1;
4547 probe_ent->port[0].cmd_addr = 0x1f0;
4548 probe_ent->port[0].altstatus_addr =
4549 probe_ent->port[0].ctl_addr = 0x3f6;
4550 probe_ent->port[0].bmdma_addr = pci_resource_start(pdev, 4);
4552 probe_ent2->port[0].cmd_addr = 0x170;
4553 probe_ent2->port[0].altstatus_addr =
4554 probe_ent2->port[0].ctl_addr = 0x376;
4555 probe_ent2->port[0].bmdma_addr = pci_resource_start(pdev, 4)+8;
4557 ata_std_ports(&probe_ent->port[0]);
4558 ata_std_ports(&probe_ent2->port[0]);
4565 * ata_pci_init_one - Initialize/register PCI IDE host controller
4566 * @pdev: Controller to be initialized
4567 * @port_info: Information from low-level host driver
4568 * @n_ports: Number of ports attached to host controller
4570 * This is a helper function which can be called from a driver's
4571 * xxx_init_one() probe function if the hardware uses traditional
4572 * IDE taskfile registers.
4574 * This function calls pci_enable_device(), reserves its register
4575 * regions, sets the dma mask, enables bus master mode, and calls
4579 * Inherited from PCI layer (may sleep).
4582 * Zero on success, negative on errno-based value on error.
4586 int ata_pci_init_one (struct pci_dev *pdev, struct ata_port_info **port_info,
4587 unsigned int n_ports)
4589 struct ata_probe_ent *probe_ent, *probe_ent2 = NULL;
4590 struct ata_port_info *port[2];
4592 unsigned int legacy_mode = 0;
4593 int disable_dev_on_err = 1;
4598 port[0] = port_info[0];
4600 port[1] = port_info[1];
4604 if ((port[0]->host_flags & ATA_FLAG_NO_LEGACY) == 0
4605 && (pdev->class >> 8) == PCI_CLASS_STORAGE_IDE) {
4606 /* TODO: support transitioning to native mode? */
4607 pci_read_config_byte(pdev, PCI_CLASS_PROG, &tmp8);
4608 mask = (1 << 2) | (1 << 0);
4609 if ((tmp8 & mask) != mask)
4610 legacy_mode = (1 << 3);
4614 if ((!legacy_mode) && (n_ports > 1)) {
4615 printk(KERN_ERR "ata: BUG: native mode, n_ports > 1\n");
4619 rc = pci_enable_device(pdev);
4623 rc = pci_request_regions(pdev, DRV_NAME);
4625 disable_dev_on_err = 0;
4630 if (!request_region(0x1f0, 8, "libata")) {
4631 struct resource *conflict, res;
4633 res.end = 0x1f0 + 8 - 1;
4634 conflict = ____request_resource(&ioport_resource, &res);
4635 if (!strcmp(conflict->name, "libata"))
4636 legacy_mode |= (1 << 0);
4638 disable_dev_on_err = 0;
4639 printk(KERN_WARNING "ata: 0x1f0 IDE port busy\n");
4642 legacy_mode |= (1 << 0);
4644 if (!request_region(0x170, 8, "libata")) {
4645 struct resource *conflict, res;
4647 res.end = 0x170 + 8 - 1;
4648 conflict = ____request_resource(&ioport_resource, &res);
4649 if (!strcmp(conflict->name, "libata"))
4650 legacy_mode |= (1 << 1);
4652 disable_dev_on_err = 0;
4653 printk(KERN_WARNING "ata: 0x170 IDE port busy\n");
4656 legacy_mode |= (1 << 1);
4659 /* we have legacy mode, but all ports are unavailable */
4660 if (legacy_mode == (1 << 3)) {
4662 goto err_out_regions;
4665 rc = pci_set_dma_mask(pdev, ATA_DMA_MASK);
4667 goto err_out_regions;
4668 rc = pci_set_consistent_dma_mask(pdev, ATA_DMA_MASK);
4670 goto err_out_regions;
4673 probe_ent = ata_pci_init_legacy_mode(pdev, port, &probe_ent2);
4675 probe_ent = ata_pci_init_native_mode(pdev, port);
4678 goto err_out_regions;
4681 pci_set_master(pdev);
4683 /* FIXME: check ata_device_add return */
4685 if (legacy_mode & (1 << 0))
4686 ata_device_add(probe_ent);
4687 if (legacy_mode & (1 << 1))
4688 ata_device_add(probe_ent2);
4690 ata_device_add(probe_ent);
4698 if (legacy_mode & (1 << 0))
4699 release_region(0x1f0, 8);
4700 if (legacy_mode & (1 << 1))
4701 release_region(0x170, 8);
4702 pci_release_regions(pdev);
4704 if (disable_dev_on_err)
4705 pci_disable_device(pdev);
4710 * ata_pci_remove_one - PCI layer callback for device removal
4711 * @pdev: PCI device that was removed
4713 * PCI layer indicates to libata via this hook that
4714 * hot-unplug or module unload event has occured.
4715 * Handle this by unregistering all objects associated
4716 * with this PCI device. Free those objects. Then finally
4717 * release PCI resources and disable device.
4720 * Inherited from PCI layer (may sleep).
4723 void ata_pci_remove_one (struct pci_dev *pdev)
4725 struct device *dev = pci_dev_to_dev(pdev);
4726 struct ata_host_set *host_set = dev_get_drvdata(dev);
4728 ata_host_set_remove(host_set);
4729 pci_release_regions(pdev);
4730 pci_disable_device(pdev);
4731 dev_set_drvdata(dev, NULL);
4734 /* move to PCI subsystem */
4735 int pci_test_config_bits(struct pci_dev *pdev, struct pci_bits *bits)
4737 unsigned long tmp = 0;
4739 switch (bits->width) {
4742 pci_read_config_byte(pdev, bits->reg, &tmp8);
4748 pci_read_config_word(pdev, bits->reg, &tmp16);
4754 pci_read_config_dword(pdev, bits->reg, &tmp32);
4765 return (tmp == bits->val) ? 1 : 0;
4767 #endif /* CONFIG_PCI */
4770 static int __init ata_init(void)
4772 ata_wq = create_workqueue("ata");
4776 printk(KERN_DEBUG "libata version " DRV_VERSION " loaded.\n");
4780 static void __exit ata_exit(void)
4782 destroy_workqueue(ata_wq);
4785 module_init(ata_init);
4786 module_exit(ata_exit);
4789 * libata is essentially a library of internal helper functions for
4790 * low-level ATA host controller drivers. As such, the API/ABI is
4791 * likely to change as new drivers are added and updated.
4792 * Do not depend on ABI/API stability.
4795 EXPORT_SYMBOL_GPL(ata_std_bios_param);
4796 EXPORT_SYMBOL_GPL(ata_std_ports);
4797 EXPORT_SYMBOL_GPL(ata_device_add);
4798 EXPORT_SYMBOL_GPL(ata_host_set_remove);
4799 EXPORT_SYMBOL_GPL(ata_sg_init);
4800 EXPORT_SYMBOL_GPL(ata_sg_init_one);
4801 EXPORT_SYMBOL_GPL(ata_qc_complete);
4802 EXPORT_SYMBOL_GPL(ata_qc_issue_prot);
4803 EXPORT_SYMBOL_GPL(ata_eng_timeout);
4804 EXPORT_SYMBOL_GPL(ata_tf_load);
4805 EXPORT_SYMBOL_GPL(ata_tf_read);
4806 EXPORT_SYMBOL_GPL(ata_noop_dev_select);
4807 EXPORT_SYMBOL_GPL(ata_std_dev_select);
4808 EXPORT_SYMBOL_GPL(ata_tf_to_fis);
4809 EXPORT_SYMBOL_GPL(ata_tf_from_fis);
4810 EXPORT_SYMBOL_GPL(ata_check_status);
4811 EXPORT_SYMBOL_GPL(ata_altstatus);
4812 EXPORT_SYMBOL_GPL(ata_chk_err);
4813 EXPORT_SYMBOL_GPL(ata_exec_command);
4814 EXPORT_SYMBOL_GPL(ata_port_start);
4815 EXPORT_SYMBOL_GPL(ata_port_stop);
4816 EXPORT_SYMBOL_GPL(ata_host_stop);
4817 EXPORT_SYMBOL_GPL(ata_interrupt);
4818 EXPORT_SYMBOL_GPL(ata_qc_prep);
4819 EXPORT_SYMBOL_GPL(ata_bmdma_setup);
4820 EXPORT_SYMBOL_GPL(ata_bmdma_start);
4821 EXPORT_SYMBOL_GPL(ata_bmdma_irq_clear);
4822 EXPORT_SYMBOL_GPL(ata_bmdma_status);
4823 EXPORT_SYMBOL_GPL(ata_bmdma_stop);
4824 EXPORT_SYMBOL_GPL(ata_port_probe);
4825 EXPORT_SYMBOL_GPL(sata_phy_reset);
4826 EXPORT_SYMBOL_GPL(__sata_phy_reset);
4827 EXPORT_SYMBOL_GPL(ata_bus_reset);
4828 EXPORT_SYMBOL_GPL(ata_port_disable);
4829 EXPORT_SYMBOL_GPL(ata_scsi_ioctl);
4830 EXPORT_SYMBOL_GPL(ata_scsi_queuecmd);
4831 EXPORT_SYMBOL_GPL(ata_scsi_error);
4832 EXPORT_SYMBOL_GPL(ata_scsi_slave_config);
4833 EXPORT_SYMBOL_GPL(ata_scsi_release);
4834 EXPORT_SYMBOL_GPL(ata_host_intr);
4835 EXPORT_SYMBOL_GPL(ata_dev_classify);
4836 EXPORT_SYMBOL_GPL(ata_dev_id_string);
4837 EXPORT_SYMBOL_GPL(ata_dev_config);
4838 EXPORT_SYMBOL_GPL(ata_scsi_simulate);
4841 EXPORT_SYMBOL_GPL(pci_test_config_bits);
4842 EXPORT_SYMBOL_GPL(ata_pci_host_stop);
4843 EXPORT_SYMBOL_GPL(ata_pci_init_native_mode);
4844 EXPORT_SYMBOL_GPL(ata_pci_init_one);
4845 EXPORT_SYMBOL_GPL(ata_pci_remove_one);
4846 #endif /* CONFIG_PCI */