2 * libata-core.c - helper library for ATA
4 * Maintained by: Jeff Garzik <jgarzik@pobox.com>
5 * Please ALWAYS copy linux-ide@vger.kernel.org
8 * Copyright 2003-2004 Red Hat, Inc. All rights reserved.
9 * Copyright 2003-2004 Jeff Garzik
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2, or (at your option)
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; see the file COPYING. If not, write to
24 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
27 * libata documentation is available via 'make {ps|pdf}docs',
28 * as Documentation/DocBook/libata.*
30 * Hardware documentation available from http://www.t13.org/ and
31 * http://www.sata-io.org/
35 #include <linux/config.h>
36 #include <linux/kernel.h>
37 #include <linux/module.h>
38 #include <linux/pci.h>
39 #include <linux/init.h>
40 #include <linux/list.h>
42 #include <linux/highmem.h>
43 #include <linux/spinlock.h>
44 #include <linux/blkdev.h>
45 #include <linux/delay.h>
46 #include <linux/timer.h>
47 #include <linux/interrupt.h>
48 #include <linux/completion.h>
49 #include <linux/suspend.h>
50 #include <linux/workqueue.h>
51 #include <linux/jiffies.h>
52 #include <linux/scatterlist.h>
53 #include <scsi/scsi.h>
55 #include "scsi_priv.h"
56 #include <scsi/scsi_host.h>
57 #include <linux/libata.h>
59 #include <asm/semaphore.h>
60 #include <asm/byteorder.h>
64 static unsigned int ata_busy_sleep (struct ata_port *ap,
65 unsigned long tmout_pat,
67 static void ata_dev_reread_id(struct ata_port *ap, struct ata_device *dev);
68 static void ata_dev_init_params(struct ata_port *ap, struct ata_device *dev);
69 static void ata_set_mode(struct ata_port *ap);
70 static void ata_dev_set_xfermode(struct ata_port *ap, struct ata_device *dev);
71 static unsigned int ata_get_mode_mask(const struct ata_port *ap, int shift);
72 static int fgb(u32 bitmap);
73 static int ata_choose_xfer_mode(const struct ata_port *ap,
75 unsigned int *xfer_shift_out);
76 static void __ata_qc_complete(struct ata_queued_cmd *qc);
78 static unsigned int ata_unique_id = 1;
79 static struct workqueue_struct *ata_wq;
81 int atapi_enabled = 0;
82 module_param(atapi_enabled, int, 0444);
83 MODULE_PARM_DESC(atapi_enabled, "Enable discovery of ATAPI devices (0=off, 1=on)");
85 MODULE_AUTHOR("Jeff Garzik");
86 MODULE_DESCRIPTION("Library module for ATA devices");
87 MODULE_LICENSE("GPL");
88 MODULE_VERSION(DRV_VERSION);
91 * ata_tf_load_pio - send taskfile registers to host controller
92 * @ap: Port to which output is sent
93 * @tf: ATA taskfile register set
95 * Outputs ATA taskfile to standard ATA host controller.
98 * Inherited from caller.
101 static void ata_tf_load_pio(struct ata_port *ap, const struct ata_taskfile *tf)
103 struct ata_ioports *ioaddr = &ap->ioaddr;
104 unsigned int is_addr = tf->flags & ATA_TFLAG_ISADDR;
106 if (tf->ctl != ap->last_ctl) {
107 outb(tf->ctl, ioaddr->ctl_addr);
108 ap->last_ctl = tf->ctl;
112 if (is_addr && (tf->flags & ATA_TFLAG_LBA48)) {
113 outb(tf->hob_feature, ioaddr->feature_addr);
114 outb(tf->hob_nsect, ioaddr->nsect_addr);
115 outb(tf->hob_lbal, ioaddr->lbal_addr);
116 outb(tf->hob_lbam, ioaddr->lbam_addr);
117 outb(tf->hob_lbah, ioaddr->lbah_addr);
118 VPRINTK("hob: feat 0x%X nsect 0x%X, lba 0x%X 0x%X 0x%X\n",
127 outb(tf->feature, ioaddr->feature_addr);
128 outb(tf->nsect, ioaddr->nsect_addr);
129 outb(tf->lbal, ioaddr->lbal_addr);
130 outb(tf->lbam, ioaddr->lbam_addr);
131 outb(tf->lbah, ioaddr->lbah_addr);
132 VPRINTK("feat 0x%X nsect 0x%X lba 0x%X 0x%X 0x%X\n",
140 if (tf->flags & ATA_TFLAG_DEVICE) {
141 outb(tf->device, ioaddr->device_addr);
142 VPRINTK("device 0x%X\n", tf->device);
149 * ata_tf_load_mmio - send taskfile registers to host controller
150 * @ap: Port to which output is sent
151 * @tf: ATA taskfile register set
153 * Outputs ATA taskfile to standard ATA host controller using MMIO.
156 * Inherited from caller.
159 static void ata_tf_load_mmio(struct ata_port *ap, const struct ata_taskfile *tf)
161 struct ata_ioports *ioaddr = &ap->ioaddr;
162 unsigned int is_addr = tf->flags & ATA_TFLAG_ISADDR;
164 if (tf->ctl != ap->last_ctl) {
165 writeb(tf->ctl, (void __iomem *) ap->ioaddr.ctl_addr);
166 ap->last_ctl = tf->ctl;
170 if (is_addr && (tf->flags & ATA_TFLAG_LBA48)) {
171 writeb(tf->hob_feature, (void __iomem *) ioaddr->feature_addr);
172 writeb(tf->hob_nsect, (void __iomem *) ioaddr->nsect_addr);
173 writeb(tf->hob_lbal, (void __iomem *) ioaddr->lbal_addr);
174 writeb(tf->hob_lbam, (void __iomem *) ioaddr->lbam_addr);
175 writeb(tf->hob_lbah, (void __iomem *) ioaddr->lbah_addr);
176 VPRINTK("hob: feat 0x%X nsect 0x%X, lba 0x%X 0x%X 0x%X\n",
185 writeb(tf->feature, (void __iomem *) ioaddr->feature_addr);
186 writeb(tf->nsect, (void __iomem *) ioaddr->nsect_addr);
187 writeb(tf->lbal, (void __iomem *) ioaddr->lbal_addr);
188 writeb(tf->lbam, (void __iomem *) ioaddr->lbam_addr);
189 writeb(tf->lbah, (void __iomem *) ioaddr->lbah_addr);
190 VPRINTK("feat 0x%X nsect 0x%X lba 0x%X 0x%X 0x%X\n",
198 if (tf->flags & ATA_TFLAG_DEVICE) {
199 writeb(tf->device, (void __iomem *) ioaddr->device_addr);
200 VPRINTK("device 0x%X\n", tf->device);
208 * ata_tf_load - send taskfile registers to host controller
209 * @ap: Port to which output is sent
210 * @tf: ATA taskfile register set
212 * Outputs ATA taskfile to standard ATA host controller using MMIO
213 * or PIO as indicated by the ATA_FLAG_MMIO flag.
214 * Writes the control, feature, nsect, lbal, lbam, and lbah registers.
215 * Optionally (ATA_TFLAG_LBA48) writes hob_feature, hob_nsect,
216 * hob_lbal, hob_lbam, and hob_lbah.
218 * This function waits for idle (!BUSY and !DRQ) after writing
219 * registers. If the control register has a new value, this
220 * function also waits for idle after writing control and before
221 * writing the remaining registers.
223 * May be used as the tf_load() entry in ata_port_operations.
226 * Inherited from caller.
228 void ata_tf_load(struct ata_port *ap, const struct ata_taskfile *tf)
230 if (ap->flags & ATA_FLAG_MMIO)
231 ata_tf_load_mmio(ap, tf);
233 ata_tf_load_pio(ap, tf);
237 * ata_exec_command_pio - issue ATA command to host controller
238 * @ap: port to which command is being issued
239 * @tf: ATA taskfile register set
241 * Issues PIO write to ATA command register, with proper
242 * synchronization with interrupt handler / other threads.
245 * spin_lock_irqsave(host_set lock)
248 static void ata_exec_command_pio(struct ata_port *ap, const struct ata_taskfile *tf)
250 DPRINTK("ata%u: cmd 0x%X\n", ap->id, tf->command);
252 outb(tf->command, ap->ioaddr.command_addr);
258 * ata_exec_command_mmio - issue ATA command to host controller
259 * @ap: port to which command is being issued
260 * @tf: ATA taskfile register set
262 * Issues MMIO write to ATA command register, with proper
263 * synchronization with interrupt handler / other threads.
266 * spin_lock_irqsave(host_set lock)
269 static void ata_exec_command_mmio(struct ata_port *ap, const struct ata_taskfile *tf)
271 DPRINTK("ata%u: cmd 0x%X\n", ap->id, tf->command);
273 writeb(tf->command, (void __iomem *) ap->ioaddr.command_addr);
279 * ata_exec_command - issue ATA command to host controller
280 * @ap: port to which command is being issued
281 * @tf: ATA taskfile register set
283 * Issues PIO/MMIO write to ATA command register, with proper
284 * synchronization with interrupt handler / other threads.
287 * spin_lock_irqsave(host_set lock)
289 void ata_exec_command(struct ata_port *ap, const struct ata_taskfile *tf)
291 if (ap->flags & ATA_FLAG_MMIO)
292 ata_exec_command_mmio(ap, tf);
294 ata_exec_command_pio(ap, tf);
298 * ata_exec - issue ATA command to host controller
299 * @ap: port to which command is being issued
300 * @tf: ATA taskfile register set
302 * Issues PIO/MMIO write to ATA command register, with proper
303 * synchronization with interrupt handler / other threads.
306 * Obtains host_set lock.
309 static inline void ata_exec(struct ata_port *ap, const struct ata_taskfile *tf)
313 DPRINTK("ata%u: cmd 0x%X\n", ap->id, tf->command);
314 spin_lock_irqsave(&ap->host_set->lock, flags);
315 ap->ops->exec_command(ap, tf);
316 spin_unlock_irqrestore(&ap->host_set->lock, flags);
320 * ata_tf_to_host - issue ATA taskfile to host controller
321 * @ap: port to which command is being issued
322 * @tf: ATA taskfile register set
324 * Issues ATA taskfile register set to ATA host controller,
325 * with proper synchronization with interrupt handler and
329 * Obtains host_set lock.
332 static void ata_tf_to_host(struct ata_port *ap, const struct ata_taskfile *tf)
334 ap->ops->tf_load(ap, tf);
340 * ata_tf_to_host_nolock - issue ATA taskfile to host controller
341 * @ap: port to which command is being issued
342 * @tf: ATA taskfile register set
344 * Issues ATA taskfile register set to ATA host controller,
345 * with proper synchronization with interrupt handler and
349 * spin_lock_irqsave(host_set lock)
352 void ata_tf_to_host_nolock(struct ata_port *ap, const struct ata_taskfile *tf)
354 ap->ops->tf_load(ap, tf);
355 ap->ops->exec_command(ap, tf);
359 * ata_tf_read_pio - input device's ATA taskfile shadow registers
360 * @ap: Port from which input is read
361 * @tf: ATA taskfile register set for storing input
363 * Reads ATA taskfile registers for currently-selected device
367 * Inherited from caller.
370 static void ata_tf_read_pio(struct ata_port *ap, struct ata_taskfile *tf)
372 struct ata_ioports *ioaddr = &ap->ioaddr;
374 tf->command = ata_check_status(ap);
375 tf->feature = ata_chk_err(ap);
376 tf->nsect = inb(ioaddr->nsect_addr);
377 tf->lbal = inb(ioaddr->lbal_addr);
378 tf->lbam = inb(ioaddr->lbam_addr);
379 tf->lbah = inb(ioaddr->lbah_addr);
380 tf->device = inb(ioaddr->device_addr);
382 if (tf->flags & ATA_TFLAG_LBA48) {
383 outb(tf->ctl | ATA_HOB, ioaddr->ctl_addr);
384 tf->hob_feature = inb(ioaddr->error_addr);
385 tf->hob_nsect = inb(ioaddr->nsect_addr);
386 tf->hob_lbal = inb(ioaddr->lbal_addr);
387 tf->hob_lbam = inb(ioaddr->lbam_addr);
388 tf->hob_lbah = inb(ioaddr->lbah_addr);
393 * ata_tf_read_mmio - input device's ATA taskfile shadow registers
394 * @ap: Port from which input is read
395 * @tf: ATA taskfile register set for storing input
397 * Reads ATA taskfile registers for currently-selected device
401 * Inherited from caller.
404 static void ata_tf_read_mmio(struct ata_port *ap, struct ata_taskfile *tf)
406 struct ata_ioports *ioaddr = &ap->ioaddr;
408 tf->command = ata_check_status(ap);
409 tf->feature = ata_chk_err(ap);
410 tf->nsect = readb((void __iomem *)ioaddr->nsect_addr);
411 tf->lbal = readb((void __iomem *)ioaddr->lbal_addr);
412 tf->lbam = readb((void __iomem *)ioaddr->lbam_addr);
413 tf->lbah = readb((void __iomem *)ioaddr->lbah_addr);
414 tf->device = readb((void __iomem *)ioaddr->device_addr);
416 if (tf->flags & ATA_TFLAG_LBA48) {
417 writeb(tf->ctl | ATA_HOB, (void __iomem *) ap->ioaddr.ctl_addr);
418 tf->hob_feature = readb((void __iomem *)ioaddr->error_addr);
419 tf->hob_nsect = readb((void __iomem *)ioaddr->nsect_addr);
420 tf->hob_lbal = readb((void __iomem *)ioaddr->lbal_addr);
421 tf->hob_lbam = readb((void __iomem *)ioaddr->lbam_addr);
422 tf->hob_lbah = readb((void __iomem *)ioaddr->lbah_addr);
428 * ata_tf_read - input device's ATA taskfile shadow registers
429 * @ap: Port from which input is read
430 * @tf: ATA taskfile register set for storing input
432 * Reads ATA taskfile registers for currently-selected device
435 * Reads nsect, lbal, lbam, lbah, and device. If ATA_TFLAG_LBA48
436 * is set, also reads the hob registers.
438 * May be used as the tf_read() entry in ata_port_operations.
441 * Inherited from caller.
443 void ata_tf_read(struct ata_port *ap, struct ata_taskfile *tf)
445 if (ap->flags & ATA_FLAG_MMIO)
446 ata_tf_read_mmio(ap, tf);
448 ata_tf_read_pio(ap, tf);
452 * ata_check_status_pio - Read device status reg & clear interrupt
453 * @ap: port where the device is
455 * Reads ATA taskfile status register for currently-selected device
456 * and return its value. This also clears pending interrupts
460 * Inherited from caller.
462 static u8 ata_check_status_pio(struct ata_port *ap)
464 return inb(ap->ioaddr.status_addr);
468 * ata_check_status_mmio - Read device status reg & clear interrupt
469 * @ap: port where the device is
471 * Reads ATA taskfile status register for currently-selected device
472 * via MMIO and return its value. This also clears pending interrupts
476 * Inherited from caller.
478 static u8 ata_check_status_mmio(struct ata_port *ap)
480 return readb((void __iomem *) ap->ioaddr.status_addr);
485 * ata_check_status - Read device status reg & clear interrupt
486 * @ap: port where the device is
488 * Reads ATA taskfile status register for currently-selected device
489 * and return its value. This also clears pending interrupts
492 * May be used as the check_status() entry in ata_port_operations.
495 * Inherited from caller.
497 u8 ata_check_status(struct ata_port *ap)
499 if (ap->flags & ATA_FLAG_MMIO)
500 return ata_check_status_mmio(ap);
501 return ata_check_status_pio(ap);
506 * ata_altstatus - Read device alternate status reg
507 * @ap: port where the device is
509 * Reads ATA taskfile alternate status register for
510 * currently-selected device and return its value.
512 * Note: may NOT be used as the check_altstatus() entry in
513 * ata_port_operations.
516 * Inherited from caller.
518 u8 ata_altstatus(struct ata_port *ap)
520 if (ap->ops->check_altstatus)
521 return ap->ops->check_altstatus(ap);
523 if (ap->flags & ATA_FLAG_MMIO)
524 return readb((void __iomem *)ap->ioaddr.altstatus_addr);
525 return inb(ap->ioaddr.altstatus_addr);
530 * ata_chk_err - Read device error reg
531 * @ap: port where the device is
533 * Reads ATA taskfile error register for
534 * currently-selected device and return its value.
536 * Note: may NOT be used as the check_err() entry in
537 * ata_port_operations.
540 * Inherited from caller.
542 u8 ata_chk_err(struct ata_port *ap)
544 if (ap->ops->check_err)
545 return ap->ops->check_err(ap);
547 if (ap->flags & ATA_FLAG_MMIO) {
548 return readb((void __iomem *) ap->ioaddr.error_addr);
550 return inb(ap->ioaddr.error_addr);
554 * ata_tf_to_fis - Convert ATA taskfile to SATA FIS structure
555 * @tf: Taskfile to convert
556 * @fis: Buffer into which data will output
557 * @pmp: Port multiplier port
559 * Converts a standard ATA taskfile to a Serial ATA
560 * FIS structure (Register - Host to Device).
563 * Inherited from caller.
566 void ata_tf_to_fis(const struct ata_taskfile *tf, u8 *fis, u8 pmp)
568 fis[0] = 0x27; /* Register - Host to Device FIS */
569 fis[1] = (pmp & 0xf) | (1 << 7); /* Port multiplier number,
570 bit 7 indicates Command FIS */
571 fis[2] = tf->command;
572 fis[3] = tf->feature;
579 fis[8] = tf->hob_lbal;
580 fis[9] = tf->hob_lbam;
581 fis[10] = tf->hob_lbah;
582 fis[11] = tf->hob_feature;
585 fis[13] = tf->hob_nsect;
596 * ata_tf_from_fis - Convert SATA FIS to ATA taskfile
597 * @fis: Buffer from which data will be input
598 * @tf: Taskfile to output
600 * Converts a standard ATA taskfile to a Serial ATA
601 * FIS structure (Register - Host to Device).
604 * Inherited from caller.
607 void ata_tf_from_fis(const u8 *fis, struct ata_taskfile *tf)
609 tf->command = fis[2]; /* status */
610 tf->feature = fis[3]; /* error */
617 tf->hob_lbal = fis[8];
618 tf->hob_lbam = fis[9];
619 tf->hob_lbah = fis[10];
622 tf->hob_nsect = fis[13];
625 static const u8 ata_rw_cmds[] = {
629 ATA_CMD_READ_MULTI_EXT,
630 ATA_CMD_WRITE_MULTI_EXT,
634 ATA_CMD_PIO_READ_EXT,
635 ATA_CMD_PIO_WRITE_EXT,
644 * ata_rwcmd_protocol - set taskfile r/w commands and protocol
645 * @qc: command to examine and configure
647 * Examine the device configuration and tf->flags to calculate
648 * the proper read/write commands and protocol to use.
653 void ata_rwcmd_protocol(struct ata_queued_cmd *qc)
655 struct ata_taskfile *tf = &qc->tf;
656 struct ata_device *dev = qc->dev;
658 int index, lba48, write;
660 lba48 = (tf->flags & ATA_TFLAG_LBA48) ? 2 : 0;
661 write = (tf->flags & ATA_TFLAG_WRITE) ? 1 : 0;
663 if (dev->flags & ATA_DFLAG_PIO) {
664 tf->protocol = ATA_PROT_PIO;
665 index = dev->multi_count ? 0 : 4;
667 tf->protocol = ATA_PROT_DMA;
671 tf->command = ata_rw_cmds[index + lba48 + write];
674 static const char * xfer_mode_str[] = {
694 * ata_udma_string - convert UDMA bit offset to string
695 * @mask: mask of bits supported; only highest bit counts.
697 * Determine string which represents the highest speed
698 * (highest bit in @udma_mask).
704 * Constant C string representing highest speed listed in
705 * @udma_mask, or the constant C string "<n/a>".
708 static const char *ata_mode_string(unsigned int mask)
712 for (i = 7; i >= 0; i--)
715 for (i = ATA_SHIFT_MWDMA + 2; i >= ATA_SHIFT_MWDMA; i--)
718 for (i = ATA_SHIFT_PIO + 4; i >= ATA_SHIFT_PIO; i--)
725 return xfer_mode_str[i];
729 * ata_pio_devchk - PATA device presence detection
730 * @ap: ATA channel to examine
731 * @device: Device to examine (starting at zero)
733 * This technique was originally described in
734 * Hale Landis's ATADRVR (www.ata-atapi.com), and
735 * later found its way into the ATA/ATAPI spec.
737 * Write a pattern to the ATA shadow registers,
738 * and if a device is present, it will respond by
739 * correctly storing and echoing back the
740 * ATA shadow register contents.
746 static unsigned int ata_pio_devchk(struct ata_port *ap,
749 struct ata_ioports *ioaddr = &ap->ioaddr;
752 ap->ops->dev_select(ap, device);
754 outb(0x55, ioaddr->nsect_addr);
755 outb(0xaa, ioaddr->lbal_addr);
757 outb(0xaa, ioaddr->nsect_addr);
758 outb(0x55, ioaddr->lbal_addr);
760 outb(0x55, ioaddr->nsect_addr);
761 outb(0xaa, ioaddr->lbal_addr);
763 nsect = inb(ioaddr->nsect_addr);
764 lbal = inb(ioaddr->lbal_addr);
766 if ((nsect == 0x55) && (lbal == 0xaa))
767 return 1; /* we found a device */
769 return 0; /* nothing found */
773 * ata_mmio_devchk - PATA device presence detection
774 * @ap: ATA channel to examine
775 * @device: Device to examine (starting at zero)
777 * This technique was originally described in
778 * Hale Landis's ATADRVR (www.ata-atapi.com), and
779 * later found its way into the ATA/ATAPI spec.
781 * Write a pattern to the ATA shadow registers,
782 * and if a device is present, it will respond by
783 * correctly storing and echoing back the
784 * ATA shadow register contents.
790 static unsigned int ata_mmio_devchk(struct ata_port *ap,
793 struct ata_ioports *ioaddr = &ap->ioaddr;
796 ap->ops->dev_select(ap, device);
798 writeb(0x55, (void __iomem *) ioaddr->nsect_addr);
799 writeb(0xaa, (void __iomem *) ioaddr->lbal_addr);
801 writeb(0xaa, (void __iomem *) ioaddr->nsect_addr);
802 writeb(0x55, (void __iomem *) ioaddr->lbal_addr);
804 writeb(0x55, (void __iomem *) ioaddr->nsect_addr);
805 writeb(0xaa, (void __iomem *) ioaddr->lbal_addr);
807 nsect = readb((void __iomem *) ioaddr->nsect_addr);
808 lbal = readb((void __iomem *) ioaddr->lbal_addr);
810 if ((nsect == 0x55) && (lbal == 0xaa))
811 return 1; /* we found a device */
813 return 0; /* nothing found */
817 * ata_devchk - PATA device presence detection
818 * @ap: ATA channel to examine
819 * @device: Device to examine (starting at zero)
821 * Dispatch ATA device presence detection, depending
822 * on whether we are using PIO or MMIO to talk to the
823 * ATA shadow registers.
829 static unsigned int ata_devchk(struct ata_port *ap,
832 if (ap->flags & ATA_FLAG_MMIO)
833 return ata_mmio_devchk(ap, device);
834 return ata_pio_devchk(ap, device);
838 * ata_dev_classify - determine device type based on ATA-spec signature
839 * @tf: ATA taskfile register set for device to be identified
841 * Determine from taskfile register contents whether a device is
842 * ATA or ATAPI, as per "Signature and persistence" section
843 * of ATA/PI spec (volume 1, sect 5.14).
849 * Device type, %ATA_DEV_ATA, %ATA_DEV_ATAPI, or %ATA_DEV_UNKNOWN
850 * the event of failure.
853 unsigned int ata_dev_classify(const struct ata_taskfile *tf)
855 /* Apple's open source Darwin code hints that some devices only
856 * put a proper signature into the LBA mid/high registers,
857 * So, we only check those. It's sufficient for uniqueness.
860 if (((tf->lbam == 0) && (tf->lbah == 0)) ||
861 ((tf->lbam == 0x3c) && (tf->lbah == 0xc3))) {
862 DPRINTK("found ATA device by sig\n");
866 if (((tf->lbam == 0x14) && (tf->lbah == 0xeb)) ||
867 ((tf->lbam == 0x69) && (tf->lbah == 0x96))) {
868 DPRINTK("found ATAPI device by sig\n");
869 return ATA_DEV_ATAPI;
872 DPRINTK("unknown device\n");
873 return ATA_DEV_UNKNOWN;
877 * ata_dev_try_classify - Parse returned ATA device signature
878 * @ap: ATA channel to examine
879 * @device: Device to examine (starting at zero)
881 * After an event -- SRST, E.D.D., or SATA COMRESET -- occurs,
882 * an ATA/ATAPI-defined set of values is placed in the ATA
883 * shadow registers, indicating the results of device detection
886 * Select the ATA device, and read the values from the ATA shadow
887 * registers. Then parse according to the Error register value,
888 * and the spec-defined values examined by ata_dev_classify().
894 static u8 ata_dev_try_classify(struct ata_port *ap, unsigned int device)
896 struct ata_device *dev = &ap->device[device];
897 struct ata_taskfile tf;
901 ap->ops->dev_select(ap, device);
903 memset(&tf, 0, sizeof(tf));
905 err = ata_chk_err(ap);
906 ap->ops->tf_read(ap, &tf);
908 dev->class = ATA_DEV_NONE;
910 /* see if device passed diags */
913 else if ((device == 0) && (err == 0x81))
918 /* determine if device if ATA or ATAPI */
919 class = ata_dev_classify(&tf);
920 if (class == ATA_DEV_UNKNOWN)
922 if ((class == ATA_DEV_ATA) && (ata_chk_status(ap) == 0))
931 * ata_dev_id_string - Convert IDENTIFY DEVICE page into string
932 * @id: IDENTIFY DEVICE results we will examine
933 * @s: string into which data is output
934 * @ofs: offset into identify device page
935 * @len: length of string to return. must be an even number.
937 * The strings in the IDENTIFY DEVICE page are broken up into
938 * 16-bit chunks. Run through the string, and output each
939 * 8-bit chunk linearly, regardless of platform.
945 void ata_dev_id_string(const u16 *id, unsigned char *s,
946 unsigned int ofs, unsigned int len)
966 * ata_noop_dev_select - Select device 0/1 on ATA bus
967 * @ap: ATA channel to manipulate
968 * @device: ATA device (numbered from zero) to select
970 * This function performs no actual function.
972 * May be used as the dev_select() entry in ata_port_operations.
977 void ata_noop_dev_select (struct ata_port *ap, unsigned int device)
983 * ata_std_dev_select - Select device 0/1 on ATA bus
984 * @ap: ATA channel to manipulate
985 * @device: ATA device (numbered from zero) to select
987 * Use the method defined in the ATA specification to
988 * make either device 0, or device 1, active on the
989 * ATA channel. Works with both PIO and MMIO.
991 * May be used as the dev_select() entry in ata_port_operations.
997 void ata_std_dev_select (struct ata_port *ap, unsigned int device)
1002 tmp = ATA_DEVICE_OBS;
1004 tmp = ATA_DEVICE_OBS | ATA_DEV1;
1006 if (ap->flags & ATA_FLAG_MMIO) {
1007 writeb(tmp, (void __iomem *) ap->ioaddr.device_addr);
1009 outb(tmp, ap->ioaddr.device_addr);
1011 ata_pause(ap); /* needed; also flushes, for mmio */
1015 * ata_dev_select - Select device 0/1 on ATA bus
1016 * @ap: ATA channel to manipulate
1017 * @device: ATA device (numbered from zero) to select
1018 * @wait: non-zero to wait for Status register BSY bit to clear
1019 * @can_sleep: non-zero if context allows sleeping
1021 * Use the method defined in the ATA specification to
1022 * make either device 0, or device 1, active on the
1025 * This is a high-level version of ata_std_dev_select(),
1026 * which additionally provides the services of inserting
1027 * the proper pauses and status polling, where needed.
1033 void ata_dev_select(struct ata_port *ap, unsigned int device,
1034 unsigned int wait, unsigned int can_sleep)
1036 VPRINTK("ENTER, ata%u: device %u, wait %u\n",
1037 ap->id, device, wait);
1042 ap->ops->dev_select(ap, device);
1045 if (can_sleep && ap->device[device].class == ATA_DEV_ATAPI)
1052 * ata_dump_id - IDENTIFY DEVICE info debugging output
1053 * @dev: Device whose IDENTIFY DEVICE page we will dump
1055 * Dump selected 16-bit words from a detected device's
1056 * IDENTIFY PAGE page.
1062 static inline void ata_dump_id(const struct ata_device *dev)
1064 DPRINTK("49==0x%04x "
1074 DPRINTK("80==0x%04x "
1084 DPRINTK("88==0x%04x "
1091 * Compute the PIO modes available for this device. This is not as
1092 * trivial as it seems if we must consider early devices correctly.
1094 * FIXME: pre IDE drive timing (do we care ?).
1097 static unsigned int ata_pio_modes(const struct ata_device *adev)
1101 /* Usual case. Word 53 indicates word 88 is valid */
1102 if (adev->id[ATA_ID_FIELD_VALID] & (1 << 2)) {
1103 modes = adev->id[ATA_ID_PIO_MODES] & 0x03;
1109 /* If word 88 isn't valid then Word 51 holds the PIO timing number
1110 for the maximum. Turn it into a mask and return it */
1111 modes = (2 << (adev->id[ATA_ID_OLD_PIO_MODES] & 0xFF)) - 1 ;
1116 * ata_dev_identify - obtain IDENTIFY x DEVICE page
1117 * @ap: port on which device we wish to probe resides
1118 * @device: device bus address, starting at zero
1120 * Following bus reset, we issue the IDENTIFY [PACKET] DEVICE
1121 * command, and read back the 512-byte device information page.
1122 * The device information page is fed to us via the standard
1123 * PIO-IN protocol, but we hand-code it here. (TODO: investigate
1124 * using standard PIO-IN paths)
1126 * After reading the device information page, we use several
1127 * bits of information from it to initialize data structures
1128 * that will be used during the lifetime of the ata_device.
1129 * Other data from the info page is used to disqualify certain
1130 * older ATA devices we do not wish to support.
1133 * Inherited from caller. Some functions called by this function
1134 * obtain the host_set lock.
1137 static void ata_dev_identify(struct ata_port *ap, unsigned int device)
1139 struct ata_device *dev = &ap->device[device];
1140 unsigned int major_version;
1142 unsigned long xfer_modes;
1144 unsigned int using_edd;
1145 DECLARE_COMPLETION(wait);
1146 struct ata_queued_cmd *qc;
1147 unsigned long flags;
1150 if (!ata_dev_present(dev)) {
1151 DPRINTK("ENTER/EXIT (host %u, dev %u) -- nodev\n",
1156 if (ap->flags & (ATA_FLAG_SRST | ATA_FLAG_SATA_RESET))
1161 DPRINTK("ENTER, host %u, dev %u\n", ap->id, device);
1163 assert (dev->class == ATA_DEV_ATA || dev->class == ATA_DEV_ATAPI ||
1164 dev->class == ATA_DEV_NONE);
1166 ata_dev_select(ap, device, 1, 1); /* select device 0/1 */
1168 qc = ata_qc_new_init(ap, dev);
1171 ata_sg_init_one(qc, dev->id, sizeof(dev->id));
1172 qc->dma_dir = DMA_FROM_DEVICE;
1173 qc->tf.protocol = ATA_PROT_PIO;
1177 if (dev->class == ATA_DEV_ATA) {
1178 qc->tf.command = ATA_CMD_ID_ATA;
1179 DPRINTK("do ATA identify\n");
1181 qc->tf.command = ATA_CMD_ID_ATAPI;
1182 DPRINTK("do ATAPI identify\n");
1185 qc->waiting = &wait;
1186 qc->complete_fn = ata_qc_complete_noop;
1188 spin_lock_irqsave(&ap->host_set->lock, flags);
1189 rc = ata_qc_issue(qc);
1190 spin_unlock_irqrestore(&ap->host_set->lock, flags);
1195 wait_for_completion(&wait);
1197 status = ata_chk_status(ap);
1198 if (status & ATA_ERR) {
1200 * arg! EDD works for all test cases, but seems to return
1201 * the ATA signature for some ATAPI devices. Until the
1202 * reason for this is found and fixed, we fix up the mess
1203 * here. If IDENTIFY DEVICE returns command aborted
1204 * (as ATAPI devices do), then we issue an
1205 * IDENTIFY PACKET DEVICE.
1207 * ATA software reset (SRST, the default) does not appear
1208 * to have this problem.
1210 if ((using_edd) && (qc->tf.command == ATA_CMD_ID_ATA)) {
1211 u8 err = ata_chk_err(ap);
1212 if (err & ATA_ABORTED) {
1213 dev->class = ATA_DEV_ATAPI;
1224 swap_buf_le16(dev->id, ATA_ID_WORDS);
1226 /* print device capabilities */
1227 printk(KERN_DEBUG "ata%u: dev %u cfg "
1228 "49:%04x 82:%04x 83:%04x 84:%04x 85:%04x 86:%04x 87:%04x 88:%04x\n",
1229 ap->id, device, dev->id[49],
1230 dev->id[82], dev->id[83], dev->id[84],
1231 dev->id[85], dev->id[86], dev->id[87],
1235 * common ATA, ATAPI feature tests
1238 /* we require DMA support (bits 8 of word 49) */
1239 if (!ata_id_has_dma(dev->id)) {
1240 printk(KERN_DEBUG "ata%u: no dma\n", ap->id);
1244 /* quick-n-dirty find max transfer mode; for printk only */
1245 xfer_modes = dev->id[ATA_ID_UDMA_MODES];
1247 xfer_modes = (dev->id[ATA_ID_MWDMA_MODES]) << ATA_SHIFT_MWDMA;
1249 xfer_modes = ata_pio_modes(dev);
1253 /* ATA-specific feature tests */
1254 if (dev->class == ATA_DEV_ATA) {
1255 if (!ata_id_is_ata(dev->id)) /* sanity check */
1258 /* get major version */
1259 tmp = dev->id[ATA_ID_MAJOR_VER];
1260 for (major_version = 14; major_version >= 1; major_version--)
1261 if (tmp & (1 << major_version))
1265 * The exact sequence expected by certain pre-ATA4 drives is:
1268 * INITIALIZE DEVICE PARAMETERS
1270 * Some drives were very specific about that exact sequence.
1272 if (major_version < 4 || (!ata_id_has_lba(dev->id))) {
1273 ata_dev_init_params(ap, dev);
1275 /* current CHS translation info (id[53-58]) might be
1276 * changed. reread the identify device info.
1278 ata_dev_reread_id(ap, dev);
1281 if (ata_id_has_lba(dev->id)) {
1282 dev->flags |= ATA_DFLAG_LBA;
1284 if (ata_id_has_lba48(dev->id)) {
1285 dev->flags |= ATA_DFLAG_LBA48;
1286 dev->n_sectors = ata_id_u64(dev->id, 100);
1288 dev->n_sectors = ata_id_u32(dev->id, 60);
1291 /* print device info to dmesg */
1292 printk(KERN_INFO "ata%u: dev %u ATA-%d, max %s, %Lu sectors:%s\n",
1295 ata_mode_string(xfer_modes),
1296 (unsigned long long)dev->n_sectors,
1297 dev->flags & ATA_DFLAG_LBA48 ? " LBA48" : " LBA");
1301 /* Default translation */
1302 dev->cylinders = dev->id[1];
1303 dev->heads = dev->id[3];
1304 dev->sectors = dev->id[6];
1305 dev->n_sectors = dev->cylinders * dev->heads * dev->sectors;
1307 if (ata_id_current_chs_valid(dev->id)) {
1308 /* Current CHS translation is valid. */
1309 dev->cylinders = dev->id[54];
1310 dev->heads = dev->id[55];
1311 dev->sectors = dev->id[56];
1313 dev->n_sectors = ata_id_u32(dev->id, 57);
1316 /* print device info to dmesg */
1317 printk(KERN_INFO "ata%u: dev %u ATA-%d, max %s, %Lu sectors: CHS %d/%d/%d\n",
1320 ata_mode_string(xfer_modes),
1321 (unsigned long long)dev->n_sectors,
1322 (int)dev->cylinders, (int)dev->heads, (int)dev->sectors);
1326 ap->host->max_cmd_len = 16;
1329 /* ATAPI-specific feature tests */
1331 if (ata_id_is_ata(dev->id)) /* sanity check */
1334 rc = atapi_cdb_len(dev->id);
1335 if ((rc < 12) || (rc > ATAPI_CDB_LEN)) {
1336 printk(KERN_WARNING "ata%u: unsupported CDB len\n", ap->id);
1339 ap->cdb_len = (unsigned int) rc;
1340 ap->host->max_cmd_len = (unsigned char) ap->cdb_len;
1342 /* print device info to dmesg */
1343 printk(KERN_INFO "ata%u: dev %u ATAPI, max %s\n",
1345 ata_mode_string(xfer_modes));
1348 DPRINTK("EXIT, drv_stat = 0x%x\n", ata_chk_status(ap));
1352 printk(KERN_WARNING "ata%u: dev %u not supported, ignoring\n",
1355 dev->class++; /* converts ATA_DEV_xxx into ATA_DEV_xxx_UNSUP */
1356 DPRINTK("EXIT, err\n");
1360 static inline u8 ata_dev_knobble(const struct ata_port *ap)
1362 return ((ap->cbl == ATA_CBL_SATA) && (!ata_id_is_sata(ap->device->id)));
1366 * ata_dev_config - Run device specific handlers and check for
1367 * SATA->PATA bridges
1374 void ata_dev_config(struct ata_port *ap, unsigned int i)
1376 /* limit bridge transfers to udma5, 200 sectors */
1377 if (ata_dev_knobble(ap)) {
1378 printk(KERN_INFO "ata%u(%u): applying bridge limits\n",
1379 ap->id, ap->device->devno);
1380 ap->udma_mask &= ATA_UDMA5;
1381 ap->host->max_sectors = ATA_MAX_SECTORS;
1382 ap->host->hostt->max_sectors = ATA_MAX_SECTORS;
1383 ap->device->flags |= ATA_DFLAG_LOCK_SECTORS;
1386 if (ap->ops->dev_config)
1387 ap->ops->dev_config(ap, &ap->device[i]);
1391 * ata_bus_probe - Reset and probe ATA bus
1394 * Master ATA bus probing function. Initiates a hardware-dependent
1395 * bus reset, then attempts to identify any devices found on
1399 * PCI/etc. bus probe sem.
1402 * Zero on success, non-zero on error.
1405 static int ata_bus_probe(struct ata_port *ap)
1407 unsigned int i, found = 0;
1409 ap->ops->phy_reset(ap);
1410 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1413 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1414 ata_dev_identify(ap, i);
1415 if (ata_dev_present(&ap->device[i])) {
1417 ata_dev_config(ap,i);
1421 if ((!found) || (ap->flags & ATA_FLAG_PORT_DISABLED))
1422 goto err_out_disable;
1425 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1426 goto err_out_disable;
1431 ap->ops->port_disable(ap);
1437 * ata_port_probe - Mark port as enabled
1438 * @ap: Port for which we indicate enablement
1440 * Modify @ap data structure such that the system
1441 * thinks that the entire port is enabled.
1443 * LOCKING: host_set lock, or some other form of
1447 void ata_port_probe(struct ata_port *ap)
1449 ap->flags &= ~ATA_FLAG_PORT_DISABLED;
1453 * __sata_phy_reset - Wake/reset a low-level SATA PHY
1454 * @ap: SATA port associated with target SATA PHY.
1456 * This function issues commands to standard SATA Sxxx
1457 * PHY registers, to wake up the phy (and device), and
1458 * clear any reset condition.
1461 * PCI/etc. bus probe sem.
1464 void __sata_phy_reset(struct ata_port *ap)
1467 unsigned long timeout = jiffies + (HZ * 5);
1469 if (ap->flags & ATA_FLAG_SATA_RESET) {
1470 /* issue phy wake/reset */
1471 scr_write_flush(ap, SCR_CONTROL, 0x301);
1472 /* Couldn't find anything in SATA I/II specs, but
1473 * AHCI-1.1 10.4.2 says at least 1 ms. */
1476 scr_write_flush(ap, SCR_CONTROL, 0x300); /* phy wake/clear reset */
1478 /* wait for phy to become ready, if necessary */
1481 sstatus = scr_read(ap, SCR_STATUS);
1482 if ((sstatus & 0xf) != 1)
1484 } while (time_before(jiffies, timeout));
1486 /* TODO: phy layer with polling, timeouts, etc. */
1487 if (sata_dev_present(ap))
1490 sstatus = scr_read(ap, SCR_STATUS);
1491 printk(KERN_INFO "ata%u: no device found (phy stat %08x)\n",
1493 ata_port_disable(ap);
1496 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1499 if (ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT)) {
1500 ata_port_disable(ap);
1504 ap->cbl = ATA_CBL_SATA;
1508 * sata_phy_reset - Reset SATA bus.
1509 * @ap: SATA port associated with target SATA PHY.
1511 * This function resets the SATA bus, and then probes
1512 * the bus for devices.
1515 * PCI/etc. bus probe sem.
1518 void sata_phy_reset(struct ata_port *ap)
1520 __sata_phy_reset(ap);
1521 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1527 * ata_port_disable - Disable port.
1528 * @ap: Port to be disabled.
1530 * Modify @ap data structure such that the system
1531 * thinks that the entire port is disabled, and should
1532 * never attempt to probe or communicate with devices
1535 * LOCKING: host_set lock, or some other form of
1539 void ata_port_disable(struct ata_port *ap)
1541 ap->device[0].class = ATA_DEV_NONE;
1542 ap->device[1].class = ATA_DEV_NONE;
1543 ap->flags |= ATA_FLAG_PORT_DISABLED;
1547 * This mode timing computation functionality is ported over from
1548 * drivers/ide/ide-timing.h and was originally written by Vojtech Pavlik
1551 * PIO 0-5, MWDMA 0-2 and UDMA 0-6 timings (in nanoseconds).
1552 * These were taken from ATA/ATAPI-6 standard, rev 0a, except
1553 * for PIO 5, which is a nonstandard extension and UDMA6, which
1554 * is currently supported only by Maxtor drives.
1557 static const struct ata_timing ata_timing[] = {
1559 { XFER_UDMA_6, 0, 0, 0, 0, 0, 0, 0, 15 },
1560 { XFER_UDMA_5, 0, 0, 0, 0, 0, 0, 0, 20 },
1561 { XFER_UDMA_4, 0, 0, 0, 0, 0, 0, 0, 30 },
1562 { XFER_UDMA_3, 0, 0, 0, 0, 0, 0, 0, 45 },
1564 { XFER_UDMA_2, 0, 0, 0, 0, 0, 0, 0, 60 },
1565 { XFER_UDMA_1, 0, 0, 0, 0, 0, 0, 0, 80 },
1566 { XFER_UDMA_0, 0, 0, 0, 0, 0, 0, 0, 120 },
1568 /* { XFER_UDMA_SLOW, 0, 0, 0, 0, 0, 0, 0, 150 }, */
1570 { XFER_MW_DMA_2, 25, 0, 0, 0, 70, 25, 120, 0 },
1571 { XFER_MW_DMA_1, 45, 0, 0, 0, 80, 50, 150, 0 },
1572 { XFER_MW_DMA_0, 60, 0, 0, 0, 215, 215, 480, 0 },
1574 { XFER_SW_DMA_2, 60, 0, 0, 0, 120, 120, 240, 0 },
1575 { XFER_SW_DMA_1, 90, 0, 0, 0, 240, 240, 480, 0 },
1576 { XFER_SW_DMA_0, 120, 0, 0, 0, 480, 480, 960, 0 },
1578 /* { XFER_PIO_5, 20, 50, 30, 100, 50, 30, 100, 0 }, */
1579 { XFER_PIO_4, 25, 70, 25, 120, 70, 25, 120, 0 },
1580 { XFER_PIO_3, 30, 80, 70, 180, 80, 70, 180, 0 },
1582 { XFER_PIO_2, 30, 290, 40, 330, 100, 90, 240, 0 },
1583 { XFER_PIO_1, 50, 290, 93, 383, 125, 100, 383, 0 },
1584 { XFER_PIO_0, 70, 290, 240, 600, 165, 150, 600, 0 },
1586 /* { XFER_PIO_SLOW, 120, 290, 240, 960, 290, 240, 960, 0 }, */
1591 #define ENOUGH(v,unit) (((v)-1)/(unit)+1)
1592 #define EZ(v,unit) ((v)?ENOUGH(v,unit):0)
1594 static void ata_timing_quantize(const struct ata_timing *t, struct ata_timing *q, int T, int UT)
1596 q->setup = EZ(t->setup * 1000, T);
1597 q->act8b = EZ(t->act8b * 1000, T);
1598 q->rec8b = EZ(t->rec8b * 1000, T);
1599 q->cyc8b = EZ(t->cyc8b * 1000, T);
1600 q->active = EZ(t->active * 1000, T);
1601 q->recover = EZ(t->recover * 1000, T);
1602 q->cycle = EZ(t->cycle * 1000, T);
1603 q->udma = EZ(t->udma * 1000, UT);
1606 void ata_timing_merge(const struct ata_timing *a, const struct ata_timing *b,
1607 struct ata_timing *m, unsigned int what)
1609 if (what & ATA_TIMING_SETUP ) m->setup = max(a->setup, b->setup);
1610 if (what & ATA_TIMING_ACT8B ) m->act8b = max(a->act8b, b->act8b);
1611 if (what & ATA_TIMING_REC8B ) m->rec8b = max(a->rec8b, b->rec8b);
1612 if (what & ATA_TIMING_CYC8B ) m->cyc8b = max(a->cyc8b, b->cyc8b);
1613 if (what & ATA_TIMING_ACTIVE ) m->active = max(a->active, b->active);
1614 if (what & ATA_TIMING_RECOVER) m->recover = max(a->recover, b->recover);
1615 if (what & ATA_TIMING_CYCLE ) m->cycle = max(a->cycle, b->cycle);
1616 if (what & ATA_TIMING_UDMA ) m->udma = max(a->udma, b->udma);
1619 static const struct ata_timing* ata_timing_find_mode(unsigned short speed)
1621 const struct ata_timing *t;
1623 for (t = ata_timing; t->mode != speed; t++)
1624 if (t->mode == 0xFF)
1629 int ata_timing_compute(struct ata_device *adev, unsigned short speed,
1630 struct ata_timing *t, int T, int UT)
1632 const struct ata_timing *s;
1633 struct ata_timing p;
1639 if (!(s = ata_timing_find_mode(speed)))
1643 * If the drive is an EIDE drive, it can tell us it needs extended
1644 * PIO/MW_DMA cycle timing.
1647 if (adev->id[ATA_ID_FIELD_VALID] & 2) { /* EIDE drive */
1648 memset(&p, 0, sizeof(p));
1649 if(speed >= XFER_PIO_0 && speed <= XFER_SW_DMA_0) {
1650 if (speed <= XFER_PIO_2) p.cycle = p.cyc8b = adev->id[ATA_ID_EIDE_PIO];
1651 else p.cycle = p.cyc8b = adev->id[ATA_ID_EIDE_PIO_IORDY];
1652 } else if(speed >= XFER_MW_DMA_0 && speed <= XFER_MW_DMA_2) {
1653 p.cycle = adev->id[ATA_ID_EIDE_DMA_MIN];
1655 ata_timing_merge(&p, t, t, ATA_TIMING_CYCLE | ATA_TIMING_CYC8B);
1659 * Convert the timing to bus clock counts.
1662 ata_timing_quantize(s, t, T, UT);
1665 * Even in DMA/UDMA modes we still use PIO access for IDENTIFY, S.M.A.R.T
1666 * and some other commands. We have to ensure that the DMA cycle timing is
1667 * slower/equal than the fastest PIO timing.
1670 if (speed > XFER_PIO_4) {
1671 ata_timing_compute(adev, adev->pio_mode, &p, T, UT);
1672 ata_timing_merge(&p, t, t, ATA_TIMING_ALL);
1676 * Lenghten active & recovery time so that cycle time is correct.
1679 if (t->act8b + t->rec8b < t->cyc8b) {
1680 t->act8b += (t->cyc8b - (t->act8b + t->rec8b)) / 2;
1681 t->rec8b = t->cyc8b - t->act8b;
1684 if (t->active + t->recover < t->cycle) {
1685 t->active += (t->cycle - (t->active + t->recover)) / 2;
1686 t->recover = t->cycle - t->active;
1692 static const struct {
1695 } xfer_mode_classes[] = {
1696 { ATA_SHIFT_UDMA, XFER_UDMA_0 },
1697 { ATA_SHIFT_MWDMA, XFER_MW_DMA_0 },
1698 { ATA_SHIFT_PIO, XFER_PIO_0 },
1701 static inline u8 base_from_shift(unsigned int shift)
1705 for (i = 0; i < ARRAY_SIZE(xfer_mode_classes); i++)
1706 if (xfer_mode_classes[i].shift == shift)
1707 return xfer_mode_classes[i].base;
1712 static void ata_dev_set_mode(struct ata_port *ap, struct ata_device *dev)
1717 if (!ata_dev_present(dev) || (ap->flags & ATA_FLAG_PORT_DISABLED))
1720 if (dev->xfer_shift == ATA_SHIFT_PIO)
1721 dev->flags |= ATA_DFLAG_PIO;
1723 ata_dev_set_xfermode(ap, dev);
1725 base = base_from_shift(dev->xfer_shift);
1726 ofs = dev->xfer_mode - base;
1727 idx = ofs + dev->xfer_shift;
1728 WARN_ON(idx >= ARRAY_SIZE(xfer_mode_str));
1730 DPRINTK("idx=%d xfer_shift=%u, xfer_mode=0x%x, base=0x%x, offset=%d\n",
1731 idx, dev->xfer_shift, (int)dev->xfer_mode, (int)base, ofs);
1733 printk(KERN_INFO "ata%u: dev %u configured for %s\n",
1734 ap->id, dev->devno, xfer_mode_str[idx]);
1737 static int ata_host_set_pio(struct ata_port *ap)
1743 mask = ata_get_mode_mask(ap, ATA_SHIFT_PIO);
1746 printk(KERN_WARNING "ata%u: no PIO support\n", ap->id);
1750 base = base_from_shift(ATA_SHIFT_PIO);
1751 xfer_mode = base + x;
1753 DPRINTK("base 0x%x xfer_mode 0x%x mask 0x%x x %d\n",
1754 (int)base, (int)xfer_mode, mask, x);
1756 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1757 struct ata_device *dev = &ap->device[i];
1758 if (ata_dev_present(dev)) {
1759 dev->pio_mode = xfer_mode;
1760 dev->xfer_mode = xfer_mode;
1761 dev->xfer_shift = ATA_SHIFT_PIO;
1762 if (ap->ops->set_piomode)
1763 ap->ops->set_piomode(ap, dev);
1770 static void ata_host_set_dma(struct ata_port *ap, u8 xfer_mode,
1771 unsigned int xfer_shift)
1775 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1776 struct ata_device *dev = &ap->device[i];
1777 if (ata_dev_present(dev)) {
1778 dev->dma_mode = xfer_mode;
1779 dev->xfer_mode = xfer_mode;
1780 dev->xfer_shift = xfer_shift;
1781 if (ap->ops->set_dmamode)
1782 ap->ops->set_dmamode(ap, dev);
1788 * ata_set_mode - Program timings and issue SET FEATURES - XFER
1789 * @ap: port on which timings will be programmed
1791 * Set ATA device disk transfer mode (PIO3, UDMA6, etc.).
1794 * PCI/etc. bus probe sem.
1797 static void ata_set_mode(struct ata_port *ap)
1799 unsigned int xfer_shift;
1803 /* step 1: always set host PIO timings */
1804 rc = ata_host_set_pio(ap);
1808 /* step 2: choose the best data xfer mode */
1809 xfer_mode = xfer_shift = 0;
1810 rc = ata_choose_xfer_mode(ap, &xfer_mode, &xfer_shift);
1814 /* step 3: if that xfer mode isn't PIO, set host DMA timings */
1815 if (xfer_shift != ATA_SHIFT_PIO)
1816 ata_host_set_dma(ap, xfer_mode, xfer_shift);
1818 /* step 4: update devices' xfer mode */
1819 ata_dev_set_mode(ap, &ap->device[0]);
1820 ata_dev_set_mode(ap, &ap->device[1]);
1822 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1825 if (ap->ops->post_set_mode)
1826 ap->ops->post_set_mode(ap);
1831 ata_port_disable(ap);
1835 * ata_busy_sleep - sleep until BSY clears, or timeout
1836 * @ap: port containing status register to be polled
1837 * @tmout_pat: impatience timeout
1838 * @tmout: overall timeout
1840 * Sleep until ATA Status register bit BSY clears,
1841 * or a timeout occurs.
1847 static unsigned int ata_busy_sleep (struct ata_port *ap,
1848 unsigned long tmout_pat,
1849 unsigned long tmout)
1851 unsigned long timer_start, timeout;
1854 status = ata_busy_wait(ap, ATA_BUSY, 300);
1855 timer_start = jiffies;
1856 timeout = timer_start + tmout_pat;
1857 while ((status & ATA_BUSY) && (time_before(jiffies, timeout))) {
1859 status = ata_busy_wait(ap, ATA_BUSY, 3);
1862 if (status & ATA_BUSY)
1863 printk(KERN_WARNING "ata%u is slow to respond, "
1864 "please be patient\n", ap->id);
1866 timeout = timer_start + tmout;
1867 while ((status & ATA_BUSY) && (time_before(jiffies, timeout))) {
1869 status = ata_chk_status(ap);
1872 if (status & ATA_BUSY) {
1873 printk(KERN_ERR "ata%u failed to respond (%lu secs)\n",
1874 ap->id, tmout / HZ);
1881 static void ata_bus_post_reset(struct ata_port *ap, unsigned int devmask)
1883 struct ata_ioports *ioaddr = &ap->ioaddr;
1884 unsigned int dev0 = devmask & (1 << 0);
1885 unsigned int dev1 = devmask & (1 << 1);
1886 unsigned long timeout;
1888 /* if device 0 was found in ata_devchk, wait for its
1892 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
1894 /* if device 1 was found in ata_devchk, wait for
1895 * register access, then wait for BSY to clear
1897 timeout = jiffies + ATA_TMOUT_BOOT;
1901 ap->ops->dev_select(ap, 1);
1902 if (ap->flags & ATA_FLAG_MMIO) {
1903 nsect = readb((void __iomem *) ioaddr->nsect_addr);
1904 lbal = readb((void __iomem *) ioaddr->lbal_addr);
1906 nsect = inb(ioaddr->nsect_addr);
1907 lbal = inb(ioaddr->lbal_addr);
1909 if ((nsect == 1) && (lbal == 1))
1911 if (time_after(jiffies, timeout)) {
1915 msleep(50); /* give drive a breather */
1918 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
1920 /* is all this really necessary? */
1921 ap->ops->dev_select(ap, 0);
1923 ap->ops->dev_select(ap, 1);
1925 ap->ops->dev_select(ap, 0);
1929 * ata_bus_edd - Issue EXECUTE DEVICE DIAGNOSTIC command.
1930 * @ap: Port to reset and probe
1932 * Use the EXECUTE DEVICE DIAGNOSTIC command to reset and
1933 * probe the bus. Not often used these days.
1936 * PCI/etc. bus probe sem.
1940 static unsigned int ata_bus_edd(struct ata_port *ap)
1942 struct ata_taskfile tf;
1944 /* set up execute-device-diag (bus reset) taskfile */
1945 /* also, take interrupts to a known state (disabled) */
1946 DPRINTK("execute-device-diag\n");
1947 ata_tf_init(ap, &tf, 0);
1949 tf.command = ATA_CMD_EDD;
1950 tf.protocol = ATA_PROT_NODATA;
1953 ata_tf_to_host(ap, &tf);
1955 /* spec says at least 2ms. but who knows with those
1956 * crazy ATAPI devices...
1960 return ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
1963 static unsigned int ata_bus_softreset(struct ata_port *ap,
1964 unsigned int devmask)
1966 struct ata_ioports *ioaddr = &ap->ioaddr;
1968 DPRINTK("ata%u: bus reset via SRST\n", ap->id);
1970 /* software reset. causes dev0 to be selected */
1971 if (ap->flags & ATA_FLAG_MMIO) {
1972 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
1973 udelay(20); /* FIXME: flush */
1974 writeb(ap->ctl | ATA_SRST, (void __iomem *) ioaddr->ctl_addr);
1975 udelay(20); /* FIXME: flush */
1976 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
1978 outb(ap->ctl, ioaddr->ctl_addr);
1980 outb(ap->ctl | ATA_SRST, ioaddr->ctl_addr);
1982 outb(ap->ctl, ioaddr->ctl_addr);
1985 /* spec mandates ">= 2ms" before checking status.
1986 * We wait 150ms, because that was the magic delay used for
1987 * ATAPI devices in Hale Landis's ATADRVR, for the period of time
1988 * between when the ATA command register is written, and then
1989 * status is checked. Because waiting for "a while" before
1990 * checking status is fine, post SRST, we perform this magic
1991 * delay here as well.
1995 ata_bus_post_reset(ap, devmask);
2001 * ata_bus_reset - reset host port and associated ATA channel
2002 * @ap: port to reset
2004 * This is typically the first time we actually start issuing
2005 * commands to the ATA channel. We wait for BSY to clear, then
2006 * issue EXECUTE DEVICE DIAGNOSTIC command, polling for its
2007 * result. Determine what devices, if any, are on the channel
2008 * by looking at the device 0/1 error register. Look at the signature
2009 * stored in each device's taskfile registers, to determine if
2010 * the device is ATA or ATAPI.
2013 * PCI/etc. bus probe sem.
2014 * Obtains host_set lock.
2017 * Sets ATA_FLAG_PORT_DISABLED if bus reset fails.
2020 void ata_bus_reset(struct ata_port *ap)
2022 struct ata_ioports *ioaddr = &ap->ioaddr;
2023 unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS;
2025 unsigned int dev0, dev1 = 0, rc = 0, devmask = 0;
2027 DPRINTK("ENTER, host %u, port %u\n", ap->id, ap->port_no);
2029 /* determine if device 0/1 are present */
2030 if (ap->flags & ATA_FLAG_SATA_RESET)
2033 dev0 = ata_devchk(ap, 0);
2035 dev1 = ata_devchk(ap, 1);
2039 devmask |= (1 << 0);
2041 devmask |= (1 << 1);
2043 /* select device 0 again */
2044 ap->ops->dev_select(ap, 0);
2046 /* issue bus reset */
2047 if (ap->flags & ATA_FLAG_SRST)
2048 rc = ata_bus_softreset(ap, devmask);
2049 else if ((ap->flags & ATA_FLAG_SATA_RESET) == 0) {
2050 /* set up device control */
2051 if (ap->flags & ATA_FLAG_MMIO)
2052 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
2054 outb(ap->ctl, ioaddr->ctl_addr);
2055 rc = ata_bus_edd(ap);
2062 * determine by signature whether we have ATA or ATAPI devices
2064 err = ata_dev_try_classify(ap, 0);
2065 if ((slave_possible) && (err != 0x81))
2066 ata_dev_try_classify(ap, 1);
2068 /* re-enable interrupts */
2069 if (ap->ioaddr.ctl_addr) /* FIXME: hack. create a hook instead */
2072 /* is double-select really necessary? */
2073 if (ap->device[1].class != ATA_DEV_NONE)
2074 ap->ops->dev_select(ap, 1);
2075 if (ap->device[0].class != ATA_DEV_NONE)
2076 ap->ops->dev_select(ap, 0);
2078 /* if no devices were detected, disable this port */
2079 if ((ap->device[0].class == ATA_DEV_NONE) &&
2080 (ap->device[1].class == ATA_DEV_NONE))
2083 if (ap->flags & (ATA_FLAG_SATA_RESET | ATA_FLAG_SRST)) {
2084 /* set up device control for ATA_FLAG_SATA_RESET */
2085 if (ap->flags & ATA_FLAG_MMIO)
2086 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
2088 outb(ap->ctl, ioaddr->ctl_addr);
2095 printk(KERN_ERR "ata%u: disabling port\n", ap->id);
2096 ap->ops->port_disable(ap);
2101 static void ata_pr_blacklisted(const struct ata_port *ap,
2102 const struct ata_device *dev)
2104 printk(KERN_WARNING "ata%u: dev %u is on DMA blacklist, disabling DMA\n",
2105 ap->id, dev->devno);
2108 static const char * ata_dma_blacklist [] = {
2127 "Toshiba CD-ROM XM-6202B",
2128 "TOSHIBA CD-ROM XM-1702BC",
2130 "E-IDE CD-ROM CR-840",
2133 "SAMSUNG CD-ROM SC-148C",
2134 "SAMSUNG CD-ROM SC",
2136 "ATAPI CD-ROM DRIVE 40X MAXIMUM",
2140 static int ata_dma_blacklisted(const struct ata_device *dev)
2142 unsigned char model_num[40];
2147 ata_dev_id_string(dev->id, model_num, ATA_ID_PROD_OFS,
2150 len = strnlen(s, sizeof(model_num));
2152 /* ATAPI specifies that empty space is blank-filled; remove blanks */
2153 while ((len > 0) && (s[len - 1] == ' ')) {
2158 for (i = 0; i < ARRAY_SIZE(ata_dma_blacklist); i++)
2159 if (!strncmp(ata_dma_blacklist[i], s, len))
2165 static unsigned int ata_get_mode_mask(const struct ata_port *ap, int shift)
2167 const struct ata_device *master, *slave;
2170 master = &ap->device[0];
2171 slave = &ap->device[1];
2173 assert (ata_dev_present(master) || ata_dev_present(slave));
2175 if (shift == ATA_SHIFT_UDMA) {
2176 mask = ap->udma_mask;
2177 if (ata_dev_present(master)) {
2178 mask &= (master->id[ATA_ID_UDMA_MODES] & 0xff);
2179 if (ata_dma_blacklisted(master)) {
2181 ata_pr_blacklisted(ap, master);
2184 if (ata_dev_present(slave)) {
2185 mask &= (slave->id[ATA_ID_UDMA_MODES] & 0xff);
2186 if (ata_dma_blacklisted(slave)) {
2188 ata_pr_blacklisted(ap, slave);
2192 else if (shift == ATA_SHIFT_MWDMA) {
2193 mask = ap->mwdma_mask;
2194 if (ata_dev_present(master)) {
2195 mask &= (master->id[ATA_ID_MWDMA_MODES] & 0x07);
2196 if (ata_dma_blacklisted(master)) {
2198 ata_pr_blacklisted(ap, master);
2201 if (ata_dev_present(slave)) {
2202 mask &= (slave->id[ATA_ID_MWDMA_MODES] & 0x07);
2203 if (ata_dma_blacklisted(slave)) {
2205 ata_pr_blacklisted(ap, slave);
2209 else if (shift == ATA_SHIFT_PIO) {
2210 mask = ap->pio_mask;
2211 if (ata_dev_present(master)) {
2212 /* spec doesn't return explicit support for
2213 * PIO0-2, so we fake it
2215 u16 tmp_mode = master->id[ATA_ID_PIO_MODES] & 0x03;
2220 if (ata_dev_present(slave)) {
2221 /* spec doesn't return explicit support for
2222 * PIO0-2, so we fake it
2224 u16 tmp_mode = slave->id[ATA_ID_PIO_MODES] & 0x03;
2231 mask = 0xffffffff; /* shut up compiler warning */
2238 /* find greatest bit */
2239 static int fgb(u32 bitmap)
2244 for (i = 0; i < 32; i++)
2245 if (bitmap & (1 << i))
2252 * ata_choose_xfer_mode - attempt to find best transfer mode
2253 * @ap: Port for which an xfer mode will be selected
2254 * @xfer_mode_out: (output) SET FEATURES - XFER MODE code
2255 * @xfer_shift_out: (output) bit shift that selects this mode
2257 * Based on host and device capabilities, determine the
2258 * maximum transfer mode that is amenable to all.
2261 * PCI/etc. bus probe sem.
2264 * Zero on success, negative on error.
2267 static int ata_choose_xfer_mode(const struct ata_port *ap,
2269 unsigned int *xfer_shift_out)
2271 unsigned int mask, shift;
2274 for (i = 0; i < ARRAY_SIZE(xfer_mode_classes); i++) {
2275 shift = xfer_mode_classes[i].shift;
2276 mask = ata_get_mode_mask(ap, shift);
2280 *xfer_mode_out = xfer_mode_classes[i].base + x;
2281 *xfer_shift_out = shift;
2290 * ata_dev_set_xfermode - Issue SET FEATURES - XFER MODE command
2291 * @ap: Port associated with device @dev
2292 * @dev: Device to which command will be sent
2294 * Issue SET FEATURES - XFER MODE command to device @dev
2298 * PCI/etc. bus probe sem.
2301 static void ata_dev_set_xfermode(struct ata_port *ap, struct ata_device *dev)
2303 DECLARE_COMPLETION(wait);
2304 struct ata_queued_cmd *qc;
2306 unsigned long flags;
2308 /* set up set-features taskfile */
2309 DPRINTK("set features - xfer mode\n");
2311 qc = ata_qc_new_init(ap, dev);
2314 qc->tf.command = ATA_CMD_SET_FEATURES;
2315 qc->tf.feature = SETFEATURES_XFER;
2316 qc->tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
2317 qc->tf.protocol = ATA_PROT_NODATA;
2318 qc->tf.nsect = dev->xfer_mode;
2320 qc->waiting = &wait;
2321 qc->complete_fn = ata_qc_complete_noop;
2323 spin_lock_irqsave(&ap->host_set->lock, flags);
2324 rc = ata_qc_issue(qc);
2325 spin_unlock_irqrestore(&ap->host_set->lock, flags);
2328 ata_port_disable(ap);
2330 wait_for_completion(&wait);
2336 * ata_dev_reread_id - Reread the device identify device info
2337 * @ap: port where the device is
2338 * @dev: device to reread the identify device info
2343 static void ata_dev_reread_id(struct ata_port *ap, struct ata_device *dev)
2345 DECLARE_COMPLETION(wait);
2346 struct ata_queued_cmd *qc;
2347 unsigned long flags;
2350 qc = ata_qc_new_init(ap, dev);
2353 ata_sg_init_one(qc, dev->id, sizeof(dev->id));
2354 qc->dma_dir = DMA_FROM_DEVICE;
2356 if (dev->class == ATA_DEV_ATA) {
2357 qc->tf.command = ATA_CMD_ID_ATA;
2358 DPRINTK("do ATA identify\n");
2360 qc->tf.command = ATA_CMD_ID_ATAPI;
2361 DPRINTK("do ATAPI identify\n");
2364 qc->tf.flags |= ATA_TFLAG_DEVICE;
2365 qc->tf.protocol = ATA_PROT_PIO;
2368 qc->waiting = &wait;
2369 qc->complete_fn = ata_qc_complete_noop;
2371 spin_lock_irqsave(&ap->host_set->lock, flags);
2372 rc = ata_qc_issue(qc);
2373 spin_unlock_irqrestore(&ap->host_set->lock, flags);
2378 wait_for_completion(&wait);
2380 swap_buf_le16(dev->id, ATA_ID_WORDS);
2388 ata_port_disable(ap);
2392 * ata_dev_init_params - Issue INIT DEV PARAMS command
2393 * @ap: Port associated with device @dev
2394 * @dev: Device to which command will be sent
2399 static void ata_dev_init_params(struct ata_port *ap, struct ata_device *dev)
2401 DECLARE_COMPLETION(wait);
2402 struct ata_queued_cmd *qc;
2404 unsigned long flags;
2405 u16 sectors = dev->id[6];
2406 u16 heads = dev->id[3];
2408 /* Number of sectors per track 1-255. Number of heads 1-16 */
2409 if (sectors < 1 || sectors > 255 || heads < 1 || heads > 16)
2412 /* set up init dev params taskfile */
2413 DPRINTK("init dev params \n");
2415 qc = ata_qc_new_init(ap, dev);
2418 qc->tf.command = ATA_CMD_INIT_DEV_PARAMS;
2419 qc->tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
2420 qc->tf.protocol = ATA_PROT_NODATA;
2421 qc->tf.nsect = sectors;
2422 qc->tf.device |= (heads - 1) & 0x0f; /* max head = num. of heads - 1 */
2424 qc->waiting = &wait;
2425 qc->complete_fn = ata_qc_complete_noop;
2427 spin_lock_irqsave(&ap->host_set->lock, flags);
2428 rc = ata_qc_issue(qc);
2429 spin_unlock_irqrestore(&ap->host_set->lock, flags);
2432 ata_port_disable(ap);
2434 wait_for_completion(&wait);
2440 * ata_sg_clean - Unmap DMA memory associated with command
2441 * @qc: Command containing DMA memory to be released
2443 * Unmap all mapped DMA memory associated with this command.
2446 * spin_lock_irqsave(host_set lock)
2449 static void ata_sg_clean(struct ata_queued_cmd *qc)
2451 struct ata_port *ap = qc->ap;
2452 struct scatterlist *sg = qc->sg;
2453 int dir = qc->dma_dir;
2455 assert(qc->flags & ATA_QCFLAG_DMAMAP);
2458 if (qc->flags & ATA_QCFLAG_SINGLE)
2459 assert(qc->n_elem == 1);
2461 DPRINTK("unmapping %u sg elements\n", qc->n_elem);
2463 if (qc->flags & ATA_QCFLAG_SG)
2464 dma_unmap_sg(ap->host_set->dev, sg, qc->n_elem, dir);
2466 dma_unmap_single(ap->host_set->dev, sg_dma_address(&sg[0]),
2467 sg_dma_len(&sg[0]), dir);
2469 qc->flags &= ~ATA_QCFLAG_DMAMAP;
2474 * ata_fill_sg - Fill PCI IDE PRD table
2475 * @qc: Metadata associated with taskfile to be transferred
2477 * Fill PCI IDE PRD (scatter-gather) table with segments
2478 * associated with the current disk command.
2481 * spin_lock_irqsave(host_set lock)
2484 static void ata_fill_sg(struct ata_queued_cmd *qc)
2486 struct scatterlist *sg = qc->sg;
2487 struct ata_port *ap = qc->ap;
2488 unsigned int idx, nelem;
2491 assert(qc->n_elem > 0);
2494 for (nelem = qc->n_elem; nelem; nelem--,sg++) {
2498 /* determine if physical DMA addr spans 64K boundary.
2499 * Note h/w doesn't support 64-bit, so we unconditionally
2500 * truncate dma_addr_t to u32.
2502 addr = (u32) sg_dma_address(sg);
2503 sg_len = sg_dma_len(sg);
2506 offset = addr & 0xffff;
2508 if ((offset + sg_len) > 0x10000)
2509 len = 0x10000 - offset;
2511 ap->prd[idx].addr = cpu_to_le32(addr);
2512 ap->prd[idx].flags_len = cpu_to_le32(len & 0xffff);
2513 VPRINTK("PRD[%u] = (0x%X, 0x%X)\n", idx, addr, len);
2522 ap->prd[idx - 1].flags_len |= cpu_to_le32(ATA_PRD_EOT);
2525 * ata_check_atapi_dma - Check whether ATAPI DMA can be supported
2526 * @qc: Metadata associated with taskfile to check
2528 * Allow low-level driver to filter ATA PACKET commands, returning
2529 * a status indicating whether or not it is OK to use DMA for the
2530 * supplied PACKET command.
2533 * spin_lock_irqsave(host_set lock)
2535 * RETURNS: 0 when ATAPI DMA can be used
2538 int ata_check_atapi_dma(struct ata_queued_cmd *qc)
2540 struct ata_port *ap = qc->ap;
2541 int rc = 0; /* Assume ATAPI DMA is OK by default */
2543 if (ap->ops->check_atapi_dma)
2544 rc = ap->ops->check_atapi_dma(qc);
2549 * ata_qc_prep - Prepare taskfile for submission
2550 * @qc: Metadata associated with taskfile to be prepared
2552 * Prepare ATA taskfile for submission.
2555 * spin_lock_irqsave(host_set lock)
2557 void ata_qc_prep(struct ata_queued_cmd *qc)
2559 if (!(qc->flags & ATA_QCFLAG_DMAMAP))
2566 * ata_sg_init_one - Associate command with memory buffer
2567 * @qc: Command to be associated
2568 * @buf: Memory buffer
2569 * @buflen: Length of memory buffer, in bytes.
2571 * Initialize the data-related elements of queued_cmd @qc
2572 * to point to a single memory buffer, @buf of byte length @buflen.
2575 * spin_lock_irqsave(host_set lock)
2578 void ata_sg_init_one(struct ata_queued_cmd *qc, void *buf, unsigned int buflen)
2580 qc->flags |= ATA_QCFLAG_SINGLE;
2582 qc->sg = &qc->sgent;
2585 sg_init_one(qc->sg, buf, buflen);
2589 * ata_sg_init - Associate command with scatter-gather table.
2590 * @qc: Command to be associated
2591 * @sg: Scatter-gather table.
2592 * @n_elem: Number of elements in s/g table.
2594 * Initialize the data-related elements of queued_cmd @qc
2595 * to point to a scatter-gather table @sg, containing @n_elem
2599 * spin_lock_irqsave(host_set lock)
2602 void ata_sg_init(struct ata_queued_cmd *qc, struct scatterlist *sg,
2603 unsigned int n_elem)
2605 qc->flags |= ATA_QCFLAG_SG;
2607 qc->n_elem = n_elem;
2611 * ata_sg_setup_one - DMA-map the memory buffer associated with a command.
2612 * @qc: Command with memory buffer to be mapped.
2614 * DMA-map the memory buffer associated with queued_cmd @qc.
2617 * spin_lock_irqsave(host_set lock)
2620 * Zero on success, negative on error.
2623 static int ata_sg_setup_one(struct ata_queued_cmd *qc)
2625 struct ata_port *ap = qc->ap;
2626 int dir = qc->dma_dir;
2627 struct scatterlist *sg = qc->sg;
2628 dma_addr_t dma_address;
2630 dma_address = dma_map_single(ap->host_set->dev, qc->buf_virt,
2632 if (dma_mapping_error(dma_address))
2635 sg_dma_address(sg) = dma_address;
2636 sg_dma_len(sg) = sg->length;
2638 DPRINTK("mapped buffer of %d bytes for %s\n", sg_dma_len(sg),
2639 qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
2645 * ata_sg_setup - DMA-map the scatter-gather table associated with a command.
2646 * @qc: Command with scatter-gather table to be mapped.
2648 * DMA-map the scatter-gather table associated with queued_cmd @qc.
2651 * spin_lock_irqsave(host_set lock)
2654 * Zero on success, negative on error.
2658 static int ata_sg_setup(struct ata_queued_cmd *qc)
2660 struct ata_port *ap = qc->ap;
2661 struct scatterlist *sg = qc->sg;
2664 VPRINTK("ENTER, ata%u\n", ap->id);
2665 assert(qc->flags & ATA_QCFLAG_SG);
2668 n_elem = dma_map_sg(ap->host_set->dev, sg, qc->n_elem, dir);
2672 DPRINTK("%d sg elements mapped\n", n_elem);
2674 qc->n_elem = n_elem;
2680 * ata_poll_qc_complete - turn irq back on and finish qc
2681 * @qc: Command to complete
2682 * @drv_stat: ATA status register content
2685 * None. (grabs host lock)
2688 void ata_poll_qc_complete(struct ata_queued_cmd *qc, u8 drv_stat)
2690 struct ata_port *ap = qc->ap;
2691 unsigned long flags;
2693 spin_lock_irqsave(&ap->host_set->lock, flags);
2694 ap->flags &= ~ATA_FLAG_NOINTR;
2696 ata_qc_complete(qc, drv_stat);
2697 spin_unlock_irqrestore(&ap->host_set->lock, flags);
2702 * @ap: the target ata_port
2705 * None. (executing in kernel thread context)
2708 * timeout value to use
2711 static unsigned long ata_pio_poll(struct ata_port *ap)
2714 unsigned int poll_state = HSM_ST_UNKNOWN;
2715 unsigned int reg_state = HSM_ST_UNKNOWN;
2716 const unsigned int tmout_state = HSM_ST_TMOUT;
2718 switch (ap->hsm_task_state) {
2721 poll_state = HSM_ST_POLL;
2725 case HSM_ST_LAST_POLL:
2726 poll_state = HSM_ST_LAST_POLL;
2727 reg_state = HSM_ST_LAST;
2734 status = ata_chk_status(ap);
2735 if (status & ATA_BUSY) {
2736 if (time_after(jiffies, ap->pio_task_timeout)) {
2737 ap->hsm_task_state = tmout_state;
2740 ap->hsm_task_state = poll_state;
2741 return ATA_SHORT_PAUSE;
2744 ap->hsm_task_state = reg_state;
2749 * ata_pio_complete - check if drive is busy or idle
2750 * @ap: the target ata_port
2753 * None. (executing in kernel thread context)
2756 * Non-zero if qc completed, zero otherwise.
2759 static int ata_pio_complete (struct ata_port *ap)
2761 struct ata_queued_cmd *qc;
2765 * This is purely heuristic. This is a fast path. Sometimes when
2766 * we enter, BSY will be cleared in a chk-status or two. If not,
2767 * the drive is probably seeking or something. Snooze for a couple
2768 * msecs, then chk-status again. If still busy, fall back to
2769 * HSM_ST_POLL state.
2771 drv_stat = ata_busy_wait(ap, ATA_BUSY | ATA_DRQ, 10);
2772 if (drv_stat & (ATA_BUSY | ATA_DRQ)) {
2774 drv_stat = ata_busy_wait(ap, ATA_BUSY | ATA_DRQ, 10);
2775 if (drv_stat & (ATA_BUSY | ATA_DRQ)) {
2776 ap->hsm_task_state = HSM_ST_LAST_POLL;
2777 ap->pio_task_timeout = jiffies + ATA_TMOUT_PIO;
2782 drv_stat = ata_wait_idle(ap);
2783 if (!ata_ok(drv_stat)) {
2784 ap->hsm_task_state = HSM_ST_ERR;
2788 qc = ata_qc_from_tag(ap, ap->active_tag);
2791 ap->hsm_task_state = HSM_ST_IDLE;
2793 ata_poll_qc_complete(qc, drv_stat);
2795 /* another command may start at this point */
2802 * swap_buf_le16 - swap halves of 16-words in place
2803 * @buf: Buffer to swap
2804 * @buf_words: Number of 16-bit words in buffer.
2806 * Swap halves of 16-bit words if needed to convert from
2807 * little-endian byte order to native cpu byte order, or
2811 * Inherited from caller.
2813 void swap_buf_le16(u16 *buf, unsigned int buf_words)
2818 for (i = 0; i < buf_words; i++)
2819 buf[i] = le16_to_cpu(buf[i]);
2820 #endif /* __BIG_ENDIAN */
2824 * ata_mmio_data_xfer - Transfer data by MMIO
2825 * @ap: port to read/write
2827 * @buflen: buffer length
2828 * @write_data: read/write
2830 * Transfer data from/to the device data register by MMIO.
2833 * Inherited from caller.
2836 static void ata_mmio_data_xfer(struct ata_port *ap, unsigned char *buf,
2837 unsigned int buflen, int write_data)
2840 unsigned int words = buflen >> 1;
2841 u16 *buf16 = (u16 *) buf;
2842 void __iomem *mmio = (void __iomem *)ap->ioaddr.data_addr;
2844 /* Transfer multiple of 2 bytes */
2846 for (i = 0; i < words; i++)
2847 writew(le16_to_cpu(buf16[i]), mmio);
2849 for (i = 0; i < words; i++)
2850 buf16[i] = cpu_to_le16(readw(mmio));
2853 /* Transfer trailing 1 byte, if any. */
2854 if (unlikely(buflen & 0x01)) {
2855 u16 align_buf[1] = { 0 };
2856 unsigned char *trailing_buf = buf + buflen - 1;
2859 memcpy(align_buf, trailing_buf, 1);
2860 writew(le16_to_cpu(align_buf[0]), mmio);
2862 align_buf[0] = cpu_to_le16(readw(mmio));
2863 memcpy(trailing_buf, align_buf, 1);
2869 * ata_pio_data_xfer - Transfer data by PIO
2870 * @ap: port to read/write
2872 * @buflen: buffer length
2873 * @write_data: read/write
2875 * Transfer data from/to the device data register by PIO.
2878 * Inherited from caller.
2881 static void ata_pio_data_xfer(struct ata_port *ap, unsigned char *buf,
2882 unsigned int buflen, int write_data)
2884 unsigned int words = buflen >> 1;
2886 /* Transfer multiple of 2 bytes */
2888 outsw(ap->ioaddr.data_addr, buf, words);
2890 insw(ap->ioaddr.data_addr, buf, words);
2892 /* Transfer trailing 1 byte, if any. */
2893 if (unlikely(buflen & 0x01)) {
2894 u16 align_buf[1] = { 0 };
2895 unsigned char *trailing_buf = buf + buflen - 1;
2898 memcpy(align_buf, trailing_buf, 1);
2899 outw(le16_to_cpu(align_buf[0]), ap->ioaddr.data_addr);
2901 align_buf[0] = cpu_to_le16(inw(ap->ioaddr.data_addr));
2902 memcpy(trailing_buf, align_buf, 1);
2908 * ata_data_xfer - Transfer data from/to the data register.
2909 * @ap: port to read/write
2911 * @buflen: buffer length
2912 * @do_write: read/write
2914 * Transfer data from/to the device data register.
2917 * Inherited from caller.
2920 static void ata_data_xfer(struct ata_port *ap, unsigned char *buf,
2921 unsigned int buflen, int do_write)
2923 if (ap->flags & ATA_FLAG_MMIO)
2924 ata_mmio_data_xfer(ap, buf, buflen, do_write);
2926 ata_pio_data_xfer(ap, buf, buflen, do_write);
2930 * ata_pio_sector - Transfer ATA_SECT_SIZE (512 bytes) of data.
2931 * @qc: Command on going
2933 * Transfer ATA_SECT_SIZE of data from/to the ATA device.
2936 * Inherited from caller.
2939 static void ata_pio_sector(struct ata_queued_cmd *qc)
2941 int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
2942 struct scatterlist *sg = qc->sg;
2943 struct ata_port *ap = qc->ap;
2945 unsigned int offset;
2948 if (qc->cursect == (qc->nsect - 1))
2949 ap->hsm_task_state = HSM_ST_LAST;
2951 page = sg[qc->cursg].page;
2952 offset = sg[qc->cursg].offset + qc->cursg_ofs * ATA_SECT_SIZE;
2954 /* get the current page and offset */
2955 page = nth_page(page, (offset >> PAGE_SHIFT));
2956 offset %= PAGE_SIZE;
2958 buf = kmap(page) + offset;
2963 if ((qc->cursg_ofs * ATA_SECT_SIZE) == (&sg[qc->cursg])->length) {
2968 DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
2970 /* do the actual data transfer */
2971 do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
2972 ata_data_xfer(ap, buf, ATA_SECT_SIZE, do_write);
2978 * __atapi_pio_bytes - Transfer data from/to the ATAPI device.
2979 * @qc: Command on going
2980 * @bytes: number of bytes
2982 * Transfer Transfer data from/to the ATAPI device.
2985 * Inherited from caller.
2989 static void __atapi_pio_bytes(struct ata_queued_cmd *qc, unsigned int bytes)
2991 int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
2992 struct scatterlist *sg = qc->sg;
2993 struct ata_port *ap = qc->ap;
2996 unsigned int offset, count;
2998 if (qc->curbytes + bytes >= qc->nbytes)
2999 ap->hsm_task_state = HSM_ST_LAST;
3002 if (unlikely(qc->cursg >= qc->n_elem)) {
3004 * The end of qc->sg is reached and the device expects
3005 * more data to transfer. In order not to overrun qc->sg
3006 * and fulfill length specified in the byte count register,
3007 * - for read case, discard trailing data from the device
3008 * - for write case, padding zero data to the device
3010 u16 pad_buf[1] = { 0 };
3011 unsigned int words = bytes >> 1;
3014 if (words) /* warning if bytes > 1 */
3015 printk(KERN_WARNING "ata%u: %u bytes trailing data\n",
3018 for (i = 0; i < words; i++)
3019 ata_data_xfer(ap, (unsigned char*)pad_buf, 2, do_write);
3021 ap->hsm_task_state = HSM_ST_LAST;
3025 sg = &qc->sg[qc->cursg];
3028 offset = sg->offset + qc->cursg_ofs;
3030 /* get the current page and offset */
3031 page = nth_page(page, (offset >> PAGE_SHIFT));
3032 offset %= PAGE_SIZE;
3034 /* don't overrun current sg */
3035 count = min(sg->length - qc->cursg_ofs, bytes);
3037 /* don't cross page boundaries */
3038 count = min(count, (unsigned int)PAGE_SIZE - offset);
3040 buf = kmap(page) + offset;
3043 qc->curbytes += count;
3044 qc->cursg_ofs += count;
3046 if (qc->cursg_ofs == sg->length) {
3051 DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
3053 /* do the actual data transfer */
3054 ata_data_xfer(ap, buf, count, do_write);
3063 * atapi_pio_bytes - Transfer data from/to the ATAPI device.
3064 * @qc: Command on going
3066 * Transfer Transfer data from/to the ATAPI device.
3069 * Inherited from caller.
3072 static void atapi_pio_bytes(struct ata_queued_cmd *qc)
3074 struct ata_port *ap = qc->ap;
3075 struct ata_device *dev = qc->dev;
3076 unsigned int ireason, bc_lo, bc_hi, bytes;
3077 int i_write, do_write = (qc->tf.flags & ATA_TFLAG_WRITE) ? 1 : 0;
3079 ap->ops->tf_read(ap, &qc->tf);
3080 ireason = qc->tf.nsect;
3081 bc_lo = qc->tf.lbam;
3082 bc_hi = qc->tf.lbah;
3083 bytes = (bc_hi << 8) | bc_lo;
3085 /* shall be cleared to zero, indicating xfer of data */
3086 if (ireason & (1 << 0))
3089 /* make sure transfer direction matches expected */
3090 i_write = ((ireason & (1 << 1)) == 0) ? 1 : 0;
3091 if (do_write != i_write)
3094 __atapi_pio_bytes(qc, bytes);
3099 printk(KERN_INFO "ata%u: dev %u: ATAPI check failed\n",
3100 ap->id, dev->devno);
3101 ap->hsm_task_state = HSM_ST_ERR;
3105 * ata_pio_block - start PIO on a block
3106 * @ap: the target ata_port
3109 * None. (executing in kernel thread context)
3112 static void ata_pio_block(struct ata_port *ap)
3114 struct ata_queued_cmd *qc;
3118 * This is purely heuristic. This is a fast path.
3119 * Sometimes when we enter, BSY will be cleared in
3120 * a chk-status or two. If not, the drive is probably seeking
3121 * or something. Snooze for a couple msecs, then
3122 * chk-status again. If still busy, fall back to
3123 * HSM_ST_POLL state.
3125 status = ata_busy_wait(ap, ATA_BUSY, 5);
3126 if (status & ATA_BUSY) {
3128 status = ata_busy_wait(ap, ATA_BUSY, 10);
3129 if (status & ATA_BUSY) {
3130 ap->hsm_task_state = HSM_ST_POLL;
3131 ap->pio_task_timeout = jiffies + ATA_TMOUT_PIO;
3136 qc = ata_qc_from_tag(ap, ap->active_tag);
3139 if (is_atapi_taskfile(&qc->tf)) {
3140 /* no more data to transfer or unsupported ATAPI command */
3141 if ((status & ATA_DRQ) == 0) {
3142 ap->hsm_task_state = HSM_ST_LAST;
3146 atapi_pio_bytes(qc);
3148 /* handle BSY=0, DRQ=0 as error */
3149 if ((status & ATA_DRQ) == 0) {
3150 ap->hsm_task_state = HSM_ST_ERR;
3158 static void ata_pio_error(struct ata_port *ap)
3160 struct ata_queued_cmd *qc;
3163 qc = ata_qc_from_tag(ap, ap->active_tag);
3166 drv_stat = ata_chk_status(ap);
3167 printk(KERN_WARNING "ata%u: PIO error, drv_stat 0x%x\n",
3170 ap->hsm_task_state = HSM_ST_IDLE;
3172 ata_poll_qc_complete(qc, drv_stat | ATA_ERR);
3175 static void ata_pio_task(void *_data)
3177 struct ata_port *ap = _data;
3178 unsigned long timeout;
3185 switch (ap->hsm_task_state) {
3194 qc_completed = ata_pio_complete(ap);
3198 case HSM_ST_LAST_POLL:
3199 timeout = ata_pio_poll(ap);
3209 queue_delayed_work(ata_wq, &ap->pio_task, timeout);
3210 else if (!qc_completed)
3215 * ata_qc_timeout - Handle timeout of queued command
3216 * @qc: Command that timed out
3218 * Some part of the kernel (currently, only the SCSI layer)
3219 * has noticed that the active command on port @ap has not
3220 * completed after a specified length of time. Handle this
3221 * condition by disabling DMA (if necessary) and completing
3222 * transactions, with error if necessary.
3224 * This also handles the case of the "lost interrupt", where
3225 * for some reason (possibly hardware bug, possibly driver bug)
3226 * an interrupt was not delivered to the driver, even though the
3227 * transaction completed successfully.
3230 * Inherited from SCSI layer (none, can sleep)
3233 static void ata_qc_timeout(struct ata_queued_cmd *qc)
3235 struct ata_port *ap = qc->ap;
3236 struct ata_host_set *host_set = ap->host_set;
3237 struct ata_device *dev = qc->dev;
3238 u8 host_stat = 0, drv_stat;
3239 unsigned long flags;
3243 /* FIXME: doesn't this conflict with timeout handling? */
3244 if (qc->dev->class == ATA_DEV_ATAPI && qc->scsicmd) {
3245 struct scsi_cmnd *cmd = qc->scsicmd;
3247 if (!(cmd->eh_eflags & SCSI_EH_CANCEL_CMD)) {
3249 /* finish completing original command */
3250 spin_lock_irqsave(&host_set->lock, flags);
3251 __ata_qc_complete(qc);
3252 spin_unlock_irqrestore(&host_set->lock, flags);
3254 atapi_request_sense(ap, dev, cmd);
3256 cmd->result = (CHECK_CONDITION << 1) | (DID_OK << 16);
3257 scsi_finish_command(cmd);
3263 spin_lock_irqsave(&host_set->lock, flags);
3265 /* hack alert! We cannot use the supplied completion
3266 * function from inside the ->eh_strategy_handler() thread.
3267 * libata is the only user of ->eh_strategy_handler() in
3268 * any kernel, so the default scsi_done() assumes it is
3269 * not being called from the SCSI EH.
3271 qc->scsidone = scsi_finish_command;
3273 switch (qc->tf.protocol) {
3276 case ATA_PROT_ATAPI_DMA:
3277 host_stat = ap->ops->bmdma_status(ap);
3279 /* before we do anything else, clear DMA-Start bit */
3280 ap->ops->bmdma_stop(qc);
3286 drv_stat = ata_chk_status(ap);
3288 /* ack bmdma irq events */
3289 ap->ops->irq_clear(ap);
3291 printk(KERN_ERR "ata%u: command 0x%x timeout, stat 0x%x host_stat 0x%x\n",
3292 ap->id, qc->tf.command, drv_stat, host_stat);
3294 /* complete taskfile transaction */
3295 ata_qc_complete(qc, drv_stat);
3299 spin_unlock_irqrestore(&host_set->lock, flags);
3306 * ata_eng_timeout - Handle timeout of queued command
3307 * @ap: Port on which timed-out command is active
3309 * Some part of the kernel (currently, only the SCSI layer)
3310 * has noticed that the active command on port @ap has not
3311 * completed after a specified length of time. Handle this
3312 * condition by disabling DMA (if necessary) and completing
3313 * transactions, with error if necessary.
3315 * This also handles the case of the "lost interrupt", where
3316 * for some reason (possibly hardware bug, possibly driver bug)
3317 * an interrupt was not delivered to the driver, even though the
3318 * transaction completed successfully.
3321 * Inherited from SCSI layer (none, can sleep)
3324 void ata_eng_timeout(struct ata_port *ap)
3326 struct ata_queued_cmd *qc;
3330 qc = ata_qc_from_tag(ap, ap->active_tag);
3334 printk(KERN_ERR "ata%u: BUG: timeout without command\n",
3344 * ata_qc_new - Request an available ATA command, for queueing
3345 * @ap: Port associated with device @dev
3346 * @dev: Device from whom we request an available command structure
3352 static struct ata_queued_cmd *ata_qc_new(struct ata_port *ap)
3354 struct ata_queued_cmd *qc = NULL;
3357 for (i = 0; i < ATA_MAX_QUEUE; i++)
3358 if (!test_and_set_bit(i, &ap->qactive)) {
3359 qc = ata_qc_from_tag(ap, i);
3370 * ata_qc_new_init - Request an available ATA command, and initialize it
3371 * @ap: Port associated with device @dev
3372 * @dev: Device from whom we request an available command structure
3378 struct ata_queued_cmd *ata_qc_new_init(struct ata_port *ap,
3379 struct ata_device *dev)
3381 struct ata_queued_cmd *qc;
3383 qc = ata_qc_new(ap);
3390 qc->cursect = qc->cursg = qc->cursg_ofs = 0;
3392 qc->nbytes = qc->curbytes = 0;
3394 ata_tf_init(ap, &qc->tf, dev->devno);
3400 int ata_qc_complete_noop(struct ata_queued_cmd *qc, u8 drv_stat)
3405 static void __ata_qc_complete(struct ata_queued_cmd *qc)
3407 struct ata_port *ap = qc->ap;
3408 unsigned int tag, do_clear = 0;
3412 if (likely(ata_tag_valid(tag))) {
3413 if (tag == ap->active_tag)
3414 ap->active_tag = ATA_TAG_POISON;
3415 qc->tag = ATA_TAG_POISON;
3420 struct completion *waiting = qc->waiting;
3425 if (likely(do_clear))
3426 clear_bit(tag, &ap->qactive);
3430 * ata_qc_free - free unused ata_queued_cmd
3431 * @qc: Command to complete
3433 * Designed to free unused ata_queued_cmd object
3434 * in case something prevents using it.
3437 * spin_lock_irqsave(host_set lock)
3439 void ata_qc_free(struct ata_queued_cmd *qc)
3441 assert(qc != NULL); /* ata_qc_from_tag _might_ return NULL */
3442 assert(qc->waiting == NULL); /* nothing should be waiting */
3444 __ata_qc_complete(qc);
3448 * ata_qc_complete - Complete an active ATA command
3449 * @qc: Command to complete
3450 * @drv_stat: ATA Status register contents
3452 * Indicate to the mid and upper layers that an ATA
3453 * command has completed, with either an ok or not-ok status.
3456 * spin_lock_irqsave(host_set lock)
3459 void ata_qc_complete(struct ata_queued_cmd *qc, u8 drv_stat)
3463 assert(qc != NULL); /* ata_qc_from_tag _might_ return NULL */
3464 assert(qc->flags & ATA_QCFLAG_ACTIVE);
3466 if (likely(qc->flags & ATA_QCFLAG_DMAMAP))
3469 /* atapi: mark qc as inactive to prevent the interrupt handler
3470 * from completing the command twice later, before the error handler
3471 * is called. (when rc != 0 and atapi request sense is needed)
3473 qc->flags &= ~ATA_QCFLAG_ACTIVE;
3475 /* call completion callback */
3476 rc = qc->complete_fn(qc, drv_stat);
3478 /* if callback indicates not to complete command (non-zero),
3479 * return immediately
3484 __ata_qc_complete(qc);
3489 static inline int ata_should_dma_map(struct ata_queued_cmd *qc)
3491 struct ata_port *ap = qc->ap;
3493 switch (qc->tf.protocol) {
3495 case ATA_PROT_ATAPI_DMA:
3498 case ATA_PROT_ATAPI:
3500 case ATA_PROT_PIO_MULT:
3501 if (ap->flags & ATA_FLAG_PIO_DMA)
3514 * ata_qc_issue - issue taskfile to device
3515 * @qc: command to issue to device
3517 * Prepare an ATA command to submission to device.
3518 * This includes mapping the data into a DMA-able
3519 * area, filling in the S/G table, and finally
3520 * writing the taskfile to hardware, starting the command.
3523 * spin_lock_irqsave(host_set lock)
3526 * Zero on success, negative on error.
3529 int ata_qc_issue(struct ata_queued_cmd *qc)
3531 struct ata_port *ap = qc->ap;
3533 if (ata_should_dma_map(qc)) {
3534 if (qc->flags & ATA_QCFLAG_SG) {
3535 if (ata_sg_setup(qc))
3537 } else if (qc->flags & ATA_QCFLAG_SINGLE) {
3538 if (ata_sg_setup_one(qc))
3542 qc->flags &= ~ATA_QCFLAG_DMAMAP;
3545 ap->ops->qc_prep(qc);
3547 qc->ap->active_tag = qc->tag;
3548 qc->flags |= ATA_QCFLAG_ACTIVE;
3550 return ap->ops->qc_issue(qc);
3558 * ata_qc_issue_prot - issue taskfile to device in proto-dependent manner
3559 * @qc: command to issue to device
3561 * Using various libata functions and hooks, this function
3562 * starts an ATA command. ATA commands are grouped into
3563 * classes called "protocols", and issuing each type of protocol
3564 * is slightly different.
3566 * May be used as the qc_issue() entry in ata_port_operations.
3569 * spin_lock_irqsave(host_set lock)
3572 * Zero on success, negative on error.
3575 int ata_qc_issue_prot(struct ata_queued_cmd *qc)
3577 struct ata_port *ap = qc->ap;
3579 ata_dev_select(ap, qc->dev->devno, 1, 0);
3581 switch (qc->tf.protocol) {
3582 case ATA_PROT_NODATA:
3583 ata_tf_to_host_nolock(ap, &qc->tf);
3587 ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
3588 ap->ops->bmdma_setup(qc); /* set up bmdma */
3589 ap->ops->bmdma_start(qc); /* initiate bmdma */
3592 case ATA_PROT_PIO: /* load tf registers, initiate polling pio */
3593 ata_qc_set_polling(qc);
3594 ata_tf_to_host_nolock(ap, &qc->tf);
3595 ap->hsm_task_state = HSM_ST;
3596 queue_work(ata_wq, &ap->pio_task);
3599 case ATA_PROT_ATAPI:
3600 ata_qc_set_polling(qc);
3601 ata_tf_to_host_nolock(ap, &qc->tf);
3602 queue_work(ata_wq, &ap->packet_task);
3605 case ATA_PROT_ATAPI_NODATA:
3606 ap->flags |= ATA_FLAG_NOINTR;
3607 ata_tf_to_host_nolock(ap, &qc->tf);
3608 queue_work(ata_wq, &ap->packet_task);
3611 case ATA_PROT_ATAPI_DMA:
3612 ap->flags |= ATA_FLAG_NOINTR;
3613 ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
3614 ap->ops->bmdma_setup(qc); /* set up bmdma */
3615 queue_work(ata_wq, &ap->packet_task);
3627 * ata_bmdma_setup_mmio - Set up PCI IDE BMDMA transaction
3628 * @qc: Info associated with this ATA transaction.
3631 * spin_lock_irqsave(host_set lock)
3634 static void ata_bmdma_setup_mmio (struct ata_queued_cmd *qc)
3636 struct ata_port *ap = qc->ap;
3637 unsigned int rw = (qc->tf.flags & ATA_TFLAG_WRITE);
3639 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
3641 /* load PRD table addr. */
3642 mb(); /* make sure PRD table writes are visible to controller */
3643 writel(ap->prd_dma, mmio + ATA_DMA_TABLE_OFS);
3645 /* specify data direction, triple-check start bit is clear */
3646 dmactl = readb(mmio + ATA_DMA_CMD);
3647 dmactl &= ~(ATA_DMA_WR | ATA_DMA_START);
3649 dmactl |= ATA_DMA_WR;
3650 writeb(dmactl, mmio + ATA_DMA_CMD);
3652 /* issue r/w command */
3653 ap->ops->exec_command(ap, &qc->tf);
3657 * ata_bmdma_start_mmio - Start a PCI IDE BMDMA transaction
3658 * @qc: Info associated with this ATA transaction.
3661 * spin_lock_irqsave(host_set lock)
3664 static void ata_bmdma_start_mmio (struct ata_queued_cmd *qc)
3666 struct ata_port *ap = qc->ap;
3667 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
3670 /* start host DMA transaction */
3671 dmactl = readb(mmio + ATA_DMA_CMD);
3672 writeb(dmactl | ATA_DMA_START, mmio + ATA_DMA_CMD);
3674 /* Strictly, one may wish to issue a readb() here, to
3675 * flush the mmio write. However, control also passes
3676 * to the hardware at this point, and it will interrupt
3677 * us when we are to resume control. So, in effect,
3678 * we don't care when the mmio write flushes.
3679 * Further, a read of the DMA status register _immediately_
3680 * following the write may not be what certain flaky hardware
3681 * is expected, so I think it is best to not add a readb()
3682 * without first all the MMIO ATA cards/mobos.
3683 * Or maybe I'm just being paranoid.
3688 * ata_bmdma_setup_pio - Set up PCI IDE BMDMA transaction (PIO)
3689 * @qc: Info associated with this ATA transaction.
3692 * spin_lock_irqsave(host_set lock)
3695 static void ata_bmdma_setup_pio (struct ata_queued_cmd *qc)
3697 struct ata_port *ap = qc->ap;
3698 unsigned int rw = (qc->tf.flags & ATA_TFLAG_WRITE);
3701 /* load PRD table addr. */
3702 outl(ap->prd_dma, ap->ioaddr.bmdma_addr + ATA_DMA_TABLE_OFS);
3704 /* specify data direction, triple-check start bit is clear */
3705 dmactl = inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3706 dmactl &= ~(ATA_DMA_WR | ATA_DMA_START);
3708 dmactl |= ATA_DMA_WR;
3709 outb(dmactl, ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3711 /* issue r/w command */
3712 ap->ops->exec_command(ap, &qc->tf);
3716 * ata_bmdma_start_pio - Start a PCI IDE BMDMA transaction (PIO)
3717 * @qc: Info associated with this ATA transaction.
3720 * spin_lock_irqsave(host_set lock)
3723 static void ata_bmdma_start_pio (struct ata_queued_cmd *qc)
3725 struct ata_port *ap = qc->ap;
3728 /* start host DMA transaction */
3729 dmactl = inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3730 outb(dmactl | ATA_DMA_START,
3731 ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3736 * ata_bmdma_start - Start a PCI IDE BMDMA transaction
3737 * @qc: Info associated with this ATA transaction.
3739 * Writes the ATA_DMA_START flag to the DMA command register.
3741 * May be used as the bmdma_start() entry in ata_port_operations.
3744 * spin_lock_irqsave(host_set lock)
3746 void ata_bmdma_start(struct ata_queued_cmd *qc)
3748 if (qc->ap->flags & ATA_FLAG_MMIO)
3749 ata_bmdma_start_mmio(qc);
3751 ata_bmdma_start_pio(qc);
3756 * ata_bmdma_setup - Set up PCI IDE BMDMA transaction
3757 * @qc: Info associated with this ATA transaction.
3759 * Writes address of PRD table to device's PRD Table Address
3760 * register, sets the DMA control register, and calls
3761 * ops->exec_command() to start the transfer.
3763 * May be used as the bmdma_setup() entry in ata_port_operations.
3766 * spin_lock_irqsave(host_set lock)
3768 void ata_bmdma_setup(struct ata_queued_cmd *qc)
3770 if (qc->ap->flags & ATA_FLAG_MMIO)
3771 ata_bmdma_setup_mmio(qc);
3773 ata_bmdma_setup_pio(qc);
3778 * ata_bmdma_irq_clear - Clear PCI IDE BMDMA interrupt.
3779 * @ap: Port associated with this ATA transaction.
3781 * Clear interrupt and error flags in DMA status register.
3783 * May be used as the irq_clear() entry in ata_port_operations.
3786 * spin_lock_irqsave(host_set lock)
3789 void ata_bmdma_irq_clear(struct ata_port *ap)
3791 if (ap->flags & ATA_FLAG_MMIO) {
3792 void __iomem *mmio = ((void __iomem *) ap->ioaddr.bmdma_addr) + ATA_DMA_STATUS;
3793 writeb(readb(mmio), mmio);
3795 unsigned long addr = ap->ioaddr.bmdma_addr + ATA_DMA_STATUS;
3796 outb(inb(addr), addr);
3803 * ata_bmdma_status - Read PCI IDE BMDMA status
3804 * @ap: Port associated with this ATA transaction.
3806 * Read and return BMDMA status register.
3808 * May be used as the bmdma_status() entry in ata_port_operations.
3811 * spin_lock_irqsave(host_set lock)
3814 u8 ata_bmdma_status(struct ata_port *ap)
3817 if (ap->flags & ATA_FLAG_MMIO) {
3818 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
3819 host_stat = readb(mmio + ATA_DMA_STATUS);
3821 host_stat = inb(ap->ioaddr.bmdma_addr + ATA_DMA_STATUS);
3827 * ata_bmdma_stop - Stop PCI IDE BMDMA transfer
3828 * @qc: Command we are ending DMA for
3830 * Clears the ATA_DMA_START flag in the dma control register
3832 * May be used as the bmdma_stop() entry in ata_port_operations.
3835 * spin_lock_irqsave(host_set lock)
3838 void ata_bmdma_stop(struct ata_queued_cmd *qc)
3840 struct ata_port *ap = qc->ap;
3841 if (ap->flags & ATA_FLAG_MMIO) {
3842 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
3844 /* clear start/stop bit */
3845 writeb(readb(mmio + ATA_DMA_CMD) & ~ATA_DMA_START,
3846 mmio + ATA_DMA_CMD);
3848 /* clear start/stop bit */
3849 outb(inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD) & ~ATA_DMA_START,
3850 ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3853 /* one-PIO-cycle guaranteed wait, per spec, for HDMA1:0 transition */
3854 ata_altstatus(ap); /* dummy read */
3858 * ata_host_intr - Handle host interrupt for given (port, task)
3859 * @ap: Port on which interrupt arrived (possibly...)
3860 * @qc: Taskfile currently active in engine
3862 * Handle host interrupt for given queued command. Currently,
3863 * only DMA interrupts are handled. All other commands are
3864 * handled via polling with interrupts disabled (nIEN bit).
3867 * spin_lock_irqsave(host_set lock)
3870 * One if interrupt was handled, zero if not (shared irq).
3873 inline unsigned int ata_host_intr (struct ata_port *ap,
3874 struct ata_queued_cmd *qc)
3876 u8 status, host_stat;
3878 switch (qc->tf.protocol) {
3881 case ATA_PROT_ATAPI_DMA:
3882 case ATA_PROT_ATAPI:
3883 /* check status of DMA engine */
3884 host_stat = ap->ops->bmdma_status(ap);
3885 VPRINTK("ata%u: host_stat 0x%X\n", ap->id, host_stat);
3887 /* if it's not our irq... */
3888 if (!(host_stat & ATA_DMA_INTR))
3891 /* before we do anything else, clear DMA-Start bit */
3892 ap->ops->bmdma_stop(qc);
3896 case ATA_PROT_ATAPI_NODATA:
3897 case ATA_PROT_NODATA:
3898 /* check altstatus */
3899 status = ata_altstatus(ap);
3900 if (status & ATA_BUSY)
3903 /* check main status, clearing INTRQ */
3904 status = ata_chk_status(ap);
3905 if (unlikely(status & ATA_BUSY))
3907 DPRINTK("ata%u: protocol %d (dev_stat 0x%X)\n",
3908 ap->id, qc->tf.protocol, status);
3910 /* ack bmdma irq events */
3911 ap->ops->irq_clear(ap);
3913 /* complete taskfile transaction */
3914 ata_qc_complete(qc, status);
3921 return 1; /* irq handled */
3924 ap->stats.idle_irq++;
3927 if ((ap->stats.idle_irq % 1000) == 0) {
3929 ata_irq_ack(ap, 0); /* debug trap */
3930 printk(KERN_WARNING "ata%d: irq trap\n", ap->id);
3933 return 0; /* irq not handled */
3937 * ata_interrupt - Default ATA host interrupt handler
3938 * @irq: irq line (unused)
3939 * @dev_instance: pointer to our ata_host_set information structure
3942 * Default interrupt handler for PCI IDE devices. Calls
3943 * ata_host_intr() for each port that is not disabled.
3946 * Obtains host_set lock during operation.
3949 * IRQ_NONE or IRQ_HANDLED.
3952 irqreturn_t ata_interrupt (int irq, void *dev_instance, struct pt_regs *regs)
3954 struct ata_host_set *host_set = dev_instance;
3956 unsigned int handled = 0;
3957 unsigned long flags;
3959 /* TODO: make _irqsave conditional on x86 PCI IDE legacy mode */
3960 spin_lock_irqsave(&host_set->lock, flags);
3962 for (i = 0; i < host_set->n_ports; i++) {
3963 struct ata_port *ap;
3965 ap = host_set->ports[i];
3967 !(ap->flags & (ATA_FLAG_PORT_DISABLED | ATA_FLAG_NOINTR))) {
3968 struct ata_queued_cmd *qc;
3970 qc = ata_qc_from_tag(ap, ap->active_tag);
3971 if (qc && (!(qc->tf.ctl & ATA_NIEN)) &&
3972 (qc->flags & ATA_QCFLAG_ACTIVE))
3973 handled |= ata_host_intr(ap, qc);
3977 spin_unlock_irqrestore(&host_set->lock, flags);
3979 return IRQ_RETVAL(handled);
3983 * atapi_packet_task - Write CDB bytes to hardware
3984 * @_data: Port to which ATAPI device is attached.
3986 * When device has indicated its readiness to accept
3987 * a CDB, this function is called. Send the CDB.
3988 * If DMA is to be performed, exit immediately.
3989 * Otherwise, we are in polling mode, so poll
3990 * status under operation succeeds or fails.
3993 * Kernel thread context (may sleep)
3996 static void atapi_packet_task(void *_data)
3998 struct ata_port *ap = _data;
3999 struct ata_queued_cmd *qc;
4002 qc = ata_qc_from_tag(ap, ap->active_tag);
4004 assert(qc->flags & ATA_QCFLAG_ACTIVE);
4006 /* sleep-wait for BSY to clear */
4007 DPRINTK("busy wait\n");
4008 if (ata_busy_sleep(ap, ATA_TMOUT_CDB_QUICK, ATA_TMOUT_CDB))
4011 /* make sure DRQ is set */
4012 status = ata_chk_status(ap);
4013 if ((status & (ATA_BUSY | ATA_DRQ)) != ATA_DRQ)
4017 DPRINTK("send cdb\n");
4018 assert(ap->cdb_len >= 12);
4020 if (qc->tf.protocol == ATA_PROT_ATAPI_DMA ||
4021 qc->tf.protocol == ATA_PROT_ATAPI_NODATA) {
4022 unsigned long flags;
4024 /* Once we're done issuing command and kicking bmdma,
4025 * irq handler takes over. To not lose irq, we need
4026 * to clear NOINTR flag before sending cdb, but
4027 * interrupt handler shouldn't be invoked before we're
4028 * finished. Hence, the following locking.
4030 spin_lock_irqsave(&ap->host_set->lock, flags);
4031 ap->flags &= ~ATA_FLAG_NOINTR;
4032 ata_data_xfer(ap, qc->cdb, ap->cdb_len, 1);
4033 if (qc->tf.protocol == ATA_PROT_ATAPI_DMA)
4034 ap->ops->bmdma_start(qc); /* initiate bmdma */
4035 spin_unlock_irqrestore(&ap->host_set->lock, flags);
4037 ata_data_xfer(ap, qc->cdb, ap->cdb_len, 1);
4039 /* PIO commands are handled by polling */
4040 ap->hsm_task_state = HSM_ST;
4041 queue_work(ata_wq, &ap->pio_task);
4047 ata_poll_qc_complete(qc, ATA_ERR);
4052 * ata_port_start - Set port up for dma.
4053 * @ap: Port to initialize
4055 * Called just after data structures for each port are
4056 * initialized. Allocates space for PRD table.
4058 * May be used as the port_start() entry in ata_port_operations.
4061 * Inherited from caller.
4064 int ata_port_start (struct ata_port *ap)
4066 struct device *dev = ap->host_set->dev;
4068 ap->prd = dma_alloc_coherent(dev, ATA_PRD_TBL_SZ, &ap->prd_dma, GFP_KERNEL);
4072 DPRINTK("prd alloc, virt %p, dma %llx\n", ap->prd, (unsigned long long) ap->prd_dma);
4079 * ata_port_stop - Undo ata_port_start()
4080 * @ap: Port to shut down
4082 * Frees the PRD table.
4084 * May be used as the port_stop() entry in ata_port_operations.
4087 * Inherited from caller.
4090 void ata_port_stop (struct ata_port *ap)
4092 struct device *dev = ap->host_set->dev;
4094 dma_free_coherent(dev, ATA_PRD_TBL_SZ, ap->prd, ap->prd_dma);
4097 void ata_host_stop (struct ata_host_set *host_set)
4099 if (host_set->mmio_base)
4100 iounmap(host_set->mmio_base);
4105 * ata_host_remove - Unregister SCSI host structure with upper layers
4106 * @ap: Port to unregister
4107 * @do_unregister: 1 if we fully unregister, 0 to just stop the port
4110 * Inherited from caller.
4113 static void ata_host_remove(struct ata_port *ap, unsigned int do_unregister)
4115 struct Scsi_Host *sh = ap->host;
4120 scsi_remove_host(sh);
4122 ap->ops->port_stop(ap);
4126 * ata_host_init - Initialize an ata_port structure
4127 * @ap: Structure to initialize
4128 * @host: associated SCSI mid-layer structure
4129 * @host_set: Collection of hosts to which @ap belongs
4130 * @ent: Probe information provided by low-level driver
4131 * @port_no: Port number associated with this ata_port
4133 * Initialize a new ata_port structure, and its associated
4137 * Inherited from caller.
4140 static void ata_host_init(struct ata_port *ap, struct Scsi_Host *host,
4141 struct ata_host_set *host_set,
4142 const struct ata_probe_ent *ent, unsigned int port_no)
4148 host->max_channel = 1;
4149 host->unique_id = ata_unique_id++;
4150 host->max_cmd_len = 12;
4152 scsi_assign_lock(host, &host_set->lock);
4154 ap->flags = ATA_FLAG_PORT_DISABLED;
4155 ap->id = host->unique_id;
4157 ap->ctl = ATA_DEVCTL_OBS;
4158 ap->host_set = host_set;
4159 ap->port_no = port_no;
4161 ent->legacy_mode ? ent->hard_port_no : port_no;
4162 ap->pio_mask = ent->pio_mask;
4163 ap->mwdma_mask = ent->mwdma_mask;
4164 ap->udma_mask = ent->udma_mask;
4165 ap->flags |= ent->host_flags;
4166 ap->ops = ent->port_ops;
4167 ap->cbl = ATA_CBL_NONE;
4168 ap->active_tag = ATA_TAG_POISON;
4169 ap->last_ctl = 0xFF;
4171 INIT_WORK(&ap->packet_task, atapi_packet_task, ap);
4172 INIT_WORK(&ap->pio_task, ata_pio_task, ap);
4174 for (i = 0; i < ATA_MAX_DEVICES; i++)
4175 ap->device[i].devno = i;
4178 ap->stats.unhandled_irq = 1;
4179 ap->stats.idle_irq = 1;
4182 memcpy(&ap->ioaddr, &ent->port[port_no], sizeof(struct ata_ioports));
4186 * ata_host_add - Attach low-level ATA driver to system
4187 * @ent: Information provided by low-level driver
4188 * @host_set: Collections of ports to which we add
4189 * @port_no: Port number associated with this host
4191 * Attach low-level ATA driver to system.
4194 * PCI/etc. bus probe sem.
4197 * New ata_port on success, for NULL on error.
4200 static struct ata_port * ata_host_add(const struct ata_probe_ent *ent,
4201 struct ata_host_set *host_set,
4202 unsigned int port_no)
4204 struct Scsi_Host *host;
4205 struct ata_port *ap;
4209 host = scsi_host_alloc(ent->sht, sizeof(struct ata_port));
4213 ap = (struct ata_port *) &host->hostdata[0];
4215 ata_host_init(ap, host, host_set, ent, port_no);
4217 rc = ap->ops->port_start(ap);
4224 scsi_host_put(host);
4229 * ata_device_add - Register hardware device with ATA and SCSI layers
4230 * @ent: Probe information describing hardware device to be registered
4232 * This function processes the information provided in the probe
4233 * information struct @ent, allocates the necessary ATA and SCSI
4234 * host information structures, initializes them, and registers
4235 * everything with requisite kernel subsystems.
4237 * This function requests irqs, probes the ATA bus, and probes
4241 * PCI/etc. bus probe sem.
4244 * Number of ports registered. Zero on error (no ports registered).
4247 int ata_device_add(const struct ata_probe_ent *ent)
4249 unsigned int count = 0, i;
4250 struct device *dev = ent->dev;
4251 struct ata_host_set *host_set;
4254 /* alloc a container for our list of ATA ports (buses) */
4255 host_set = kzalloc(sizeof(struct ata_host_set) +
4256 (ent->n_ports * sizeof(void *)), GFP_KERNEL);
4259 spin_lock_init(&host_set->lock);
4261 host_set->dev = dev;
4262 host_set->n_ports = ent->n_ports;
4263 host_set->irq = ent->irq;
4264 host_set->mmio_base = ent->mmio_base;
4265 host_set->private_data = ent->private_data;
4266 host_set->ops = ent->port_ops;
4268 /* register each port bound to this device */
4269 for (i = 0; i < ent->n_ports; i++) {
4270 struct ata_port *ap;
4271 unsigned long xfer_mode_mask;
4273 ap = ata_host_add(ent, host_set, i);
4277 host_set->ports[i] = ap;
4278 xfer_mode_mask =(ap->udma_mask << ATA_SHIFT_UDMA) |
4279 (ap->mwdma_mask << ATA_SHIFT_MWDMA) |
4280 (ap->pio_mask << ATA_SHIFT_PIO);
4282 /* print per-port info to dmesg */
4283 printk(KERN_INFO "ata%u: %cATA max %s cmd 0x%lX ctl 0x%lX "
4284 "bmdma 0x%lX irq %lu\n",
4286 ap->flags & ATA_FLAG_SATA ? 'S' : 'P',
4287 ata_mode_string(xfer_mode_mask),
4288 ap->ioaddr.cmd_addr,
4289 ap->ioaddr.ctl_addr,
4290 ap->ioaddr.bmdma_addr,
4294 host_set->ops->irq_clear(ap);
4301 /* obtain irq, that is shared between channels */
4302 if (request_irq(ent->irq, ent->port_ops->irq_handler, ent->irq_flags,
4303 DRV_NAME, host_set))
4306 /* perform each probe synchronously */
4307 DPRINTK("probe begin\n");
4308 for (i = 0; i < count; i++) {
4309 struct ata_port *ap;
4312 ap = host_set->ports[i];
4314 DPRINTK("ata%u: probe begin\n", ap->id);
4315 rc = ata_bus_probe(ap);
4316 DPRINTK("ata%u: probe end\n", ap->id);
4319 /* FIXME: do something useful here?
4320 * Current libata behavior will
4321 * tear down everything when
4322 * the module is removed
4323 * or the h/w is unplugged.
4327 rc = scsi_add_host(ap->host, dev);
4329 printk(KERN_ERR "ata%u: scsi_add_host failed\n",
4331 /* FIXME: do something useful here */
4332 /* FIXME: handle unconditional calls to
4333 * scsi_scan_host and ata_host_remove, below,
4339 /* probes are done, now scan each port's disk(s) */
4340 DPRINTK("probe begin\n");
4341 for (i = 0; i < count; i++) {
4342 struct ata_port *ap = host_set->ports[i];
4344 ata_scsi_scan_host(ap);
4347 dev_set_drvdata(dev, host_set);
4349 VPRINTK("EXIT, returning %u\n", ent->n_ports);
4350 return ent->n_ports; /* success */
4353 for (i = 0; i < count; i++) {
4354 ata_host_remove(host_set->ports[i], 1);
4355 scsi_host_put(host_set->ports[i]->host);
4359 VPRINTK("EXIT, returning 0\n");
4364 * ata_host_set_remove - PCI layer callback for device removal
4365 * @host_set: ATA host set that was removed
4367 * Unregister all objects associated with this host set. Free those
4371 * Inherited from calling layer (may sleep).
4374 void ata_host_set_remove(struct ata_host_set *host_set)
4376 struct ata_port *ap;
4379 for (i = 0; i < host_set->n_ports; i++) {
4380 ap = host_set->ports[i];
4381 scsi_remove_host(ap->host);
4384 free_irq(host_set->irq, host_set);
4386 for (i = 0; i < host_set->n_ports; i++) {
4387 ap = host_set->ports[i];
4389 ata_scsi_release(ap->host);
4391 if ((ap->flags & ATA_FLAG_NO_LEGACY) == 0) {
4392 struct ata_ioports *ioaddr = &ap->ioaddr;
4394 if (ioaddr->cmd_addr == 0x1f0)
4395 release_region(0x1f0, 8);
4396 else if (ioaddr->cmd_addr == 0x170)
4397 release_region(0x170, 8);
4400 scsi_host_put(ap->host);
4403 if (host_set->ops->host_stop)
4404 host_set->ops->host_stop(host_set);
4410 * ata_scsi_release - SCSI layer callback hook for host unload
4411 * @host: libata host to be unloaded
4413 * Performs all duties necessary to shut down a libata port...
4414 * Kill port kthread, disable port, and release resources.
4417 * Inherited from SCSI layer.
4423 int ata_scsi_release(struct Scsi_Host *host)
4425 struct ata_port *ap = (struct ata_port *) &host->hostdata[0];
4429 ap->ops->port_disable(ap);
4430 ata_host_remove(ap, 0);
4437 * ata_std_ports - initialize ioaddr with standard port offsets.
4438 * @ioaddr: IO address structure to be initialized
4440 * Utility function which initializes data_addr, error_addr,
4441 * feature_addr, nsect_addr, lbal_addr, lbam_addr, lbah_addr,
4442 * device_addr, status_addr, and command_addr to standard offsets
4443 * relative to cmd_addr.
4445 * Does not set ctl_addr, altstatus_addr, bmdma_addr, or scr_addr.
4448 void ata_std_ports(struct ata_ioports *ioaddr)
4450 ioaddr->data_addr = ioaddr->cmd_addr + ATA_REG_DATA;
4451 ioaddr->error_addr = ioaddr->cmd_addr + ATA_REG_ERR;
4452 ioaddr->feature_addr = ioaddr->cmd_addr + ATA_REG_FEATURE;
4453 ioaddr->nsect_addr = ioaddr->cmd_addr + ATA_REG_NSECT;
4454 ioaddr->lbal_addr = ioaddr->cmd_addr + ATA_REG_LBAL;
4455 ioaddr->lbam_addr = ioaddr->cmd_addr + ATA_REG_LBAM;
4456 ioaddr->lbah_addr = ioaddr->cmd_addr + ATA_REG_LBAH;
4457 ioaddr->device_addr = ioaddr->cmd_addr + ATA_REG_DEVICE;
4458 ioaddr->status_addr = ioaddr->cmd_addr + ATA_REG_STATUS;
4459 ioaddr->command_addr = ioaddr->cmd_addr + ATA_REG_CMD;
4462 static struct ata_probe_ent *
4463 ata_probe_ent_alloc(struct device *dev, const struct ata_port_info *port)
4465 struct ata_probe_ent *probe_ent;
4467 probe_ent = kzalloc(sizeof(*probe_ent), GFP_KERNEL);
4469 printk(KERN_ERR DRV_NAME "(%s): out of memory\n",
4470 kobject_name(&(dev->kobj)));
4474 INIT_LIST_HEAD(&probe_ent->node);
4475 probe_ent->dev = dev;
4477 probe_ent->sht = port->sht;
4478 probe_ent->host_flags = port->host_flags;
4479 probe_ent->pio_mask = port->pio_mask;
4480 probe_ent->mwdma_mask = port->mwdma_mask;
4481 probe_ent->udma_mask = port->udma_mask;
4482 probe_ent->port_ops = port->port_ops;
4491 void ata_pci_host_stop (struct ata_host_set *host_set)
4493 struct pci_dev *pdev = to_pci_dev(host_set->dev);
4495 pci_iounmap(pdev, host_set->mmio_base);
4499 * ata_pci_init_native_mode - Initialize native-mode driver
4500 * @pdev: pci device to be initialized
4501 * @port: array[2] of pointers to port info structures.
4502 * @ports: bitmap of ports present
4504 * Utility function which allocates and initializes an
4505 * ata_probe_ent structure for a standard dual-port
4506 * PIO-based IDE controller. The returned ata_probe_ent
4507 * structure can be passed to ata_device_add(). The returned
4508 * ata_probe_ent structure should then be freed with kfree().
4510 * The caller need only pass the address of the primary port, the
4511 * secondary will be deduced automatically. If the device has non
4512 * standard secondary port mappings this function can be called twice,
4513 * once for each interface.
4516 struct ata_probe_ent *
4517 ata_pci_init_native_mode(struct pci_dev *pdev, struct ata_port_info **port, int ports)
4519 struct ata_probe_ent *probe_ent =
4520 ata_probe_ent_alloc(pci_dev_to_dev(pdev), port[0]);
4526 probe_ent->irq = pdev->irq;
4527 probe_ent->irq_flags = SA_SHIRQ;
4529 if (ports & ATA_PORT_PRIMARY) {
4530 probe_ent->port[p].cmd_addr = pci_resource_start(pdev, 0);
4531 probe_ent->port[p].altstatus_addr =
4532 probe_ent->port[p].ctl_addr =
4533 pci_resource_start(pdev, 1) | ATA_PCI_CTL_OFS;
4534 probe_ent->port[p].bmdma_addr = pci_resource_start(pdev, 4);
4535 ata_std_ports(&probe_ent->port[p]);
4539 if (ports & ATA_PORT_SECONDARY) {
4540 probe_ent->port[p].cmd_addr = pci_resource_start(pdev, 2);
4541 probe_ent->port[p].altstatus_addr =
4542 probe_ent->port[p].ctl_addr =
4543 pci_resource_start(pdev, 3) | ATA_PCI_CTL_OFS;
4544 probe_ent->port[p].bmdma_addr = pci_resource_start(pdev, 4) + 8;
4545 ata_std_ports(&probe_ent->port[p]);
4549 probe_ent->n_ports = p;
4553 static struct ata_probe_ent *ata_pci_init_legacy_port(struct pci_dev *pdev, struct ata_port_info **port, int port_num)
4555 struct ata_probe_ent *probe_ent;
4557 probe_ent = ata_probe_ent_alloc(pci_dev_to_dev(pdev), port[0]);
4561 probe_ent->legacy_mode = 1;
4562 probe_ent->n_ports = 1;
4563 probe_ent->hard_port_no = port_num;
4568 probe_ent->irq = 14;
4569 probe_ent->port[0].cmd_addr = 0x1f0;
4570 probe_ent->port[0].altstatus_addr =
4571 probe_ent->port[0].ctl_addr = 0x3f6;
4574 probe_ent->irq = 15;
4575 probe_ent->port[0].cmd_addr = 0x170;
4576 probe_ent->port[0].altstatus_addr =
4577 probe_ent->port[0].ctl_addr = 0x376;
4580 probe_ent->port[0].bmdma_addr = pci_resource_start(pdev, 4) + 8 * port_num;
4581 ata_std_ports(&probe_ent->port[0]);
4586 * ata_pci_init_one - Initialize/register PCI IDE host controller
4587 * @pdev: Controller to be initialized
4588 * @port_info: Information from low-level host driver
4589 * @n_ports: Number of ports attached to host controller
4591 * This is a helper function which can be called from a driver's
4592 * xxx_init_one() probe function if the hardware uses traditional
4593 * IDE taskfile registers.
4595 * This function calls pci_enable_device(), reserves its register
4596 * regions, sets the dma mask, enables bus master mode, and calls
4600 * Inherited from PCI layer (may sleep).
4603 * Zero on success, negative on errno-based value on error.
4606 int ata_pci_init_one (struct pci_dev *pdev, struct ata_port_info **port_info,
4607 unsigned int n_ports)
4609 struct ata_probe_ent *probe_ent = NULL, *probe_ent2 = NULL;
4610 struct ata_port_info *port[2];
4612 unsigned int legacy_mode = 0;
4613 int disable_dev_on_err = 1;
4618 port[0] = port_info[0];
4620 port[1] = port_info[1];
4624 if ((port[0]->host_flags & ATA_FLAG_NO_LEGACY) == 0
4625 && (pdev->class >> 8) == PCI_CLASS_STORAGE_IDE) {
4626 /* TODO: What if one channel is in native mode ... */
4627 pci_read_config_byte(pdev, PCI_CLASS_PROG, &tmp8);
4628 mask = (1 << 2) | (1 << 0);
4629 if ((tmp8 & mask) != mask)
4630 legacy_mode = (1 << 3);
4634 if ((!legacy_mode) && (n_ports > 2)) {
4635 printk(KERN_ERR "ata: BUG: native mode, n_ports > 2\n");
4640 /* FIXME: Really for ATA it isn't safe because the device may be
4641 multi-purpose and we want to leave it alone if it was already
4642 enabled. Secondly for shared use as Arjan says we want refcounting
4644 Checking dev->is_enabled is insufficient as this is not set at
4645 boot for the primary video which is BIOS enabled
4648 rc = pci_enable_device(pdev);
4652 rc = pci_request_regions(pdev, DRV_NAME);
4654 disable_dev_on_err = 0;
4658 /* FIXME: Should use platform specific mappers for legacy port ranges */
4660 if (!request_region(0x1f0, 8, "libata")) {
4661 struct resource *conflict, res;
4663 res.end = 0x1f0 + 8 - 1;
4664 conflict = ____request_resource(&ioport_resource, &res);
4665 if (!strcmp(conflict->name, "libata"))
4666 legacy_mode |= (1 << 0);
4668 disable_dev_on_err = 0;
4669 printk(KERN_WARNING "ata: 0x1f0 IDE port busy\n");
4672 legacy_mode |= (1 << 0);
4674 if (!request_region(0x170, 8, "libata")) {
4675 struct resource *conflict, res;
4677 res.end = 0x170 + 8 - 1;
4678 conflict = ____request_resource(&ioport_resource, &res);
4679 if (!strcmp(conflict->name, "libata"))
4680 legacy_mode |= (1 << 1);
4682 disable_dev_on_err = 0;
4683 printk(KERN_WARNING "ata: 0x170 IDE port busy\n");
4686 legacy_mode |= (1 << 1);
4689 /* we have legacy mode, but all ports are unavailable */
4690 if (legacy_mode == (1 << 3)) {
4692 goto err_out_regions;
4695 rc = pci_set_dma_mask(pdev, ATA_DMA_MASK);
4697 goto err_out_regions;
4698 rc = pci_set_consistent_dma_mask(pdev, ATA_DMA_MASK);
4700 goto err_out_regions;
4703 if (legacy_mode & (1 << 0))
4704 probe_ent = ata_pci_init_legacy_port(pdev, port, 0);
4705 if (legacy_mode & (1 << 1))
4706 probe_ent2 = ata_pci_init_legacy_port(pdev, port, 1);
4709 probe_ent = ata_pci_init_native_mode(pdev, port, ATA_PORT_PRIMARY | ATA_PORT_SECONDARY);
4711 probe_ent = ata_pci_init_native_mode(pdev, port, ATA_PORT_PRIMARY);
4713 if (!probe_ent && !probe_ent2) {
4715 goto err_out_regions;
4718 pci_set_master(pdev);
4720 /* FIXME: check ata_device_add return */
4722 if (legacy_mode & (1 << 0))
4723 ata_device_add(probe_ent);
4724 if (legacy_mode & (1 << 1))
4725 ata_device_add(probe_ent2);
4727 ata_device_add(probe_ent);
4735 if (legacy_mode & (1 << 0))
4736 release_region(0x1f0, 8);
4737 if (legacy_mode & (1 << 1))
4738 release_region(0x170, 8);
4739 pci_release_regions(pdev);
4741 if (disable_dev_on_err)
4742 pci_disable_device(pdev);
4747 * ata_pci_remove_one - PCI layer callback for device removal
4748 * @pdev: PCI device that was removed
4750 * PCI layer indicates to libata via this hook that
4751 * hot-unplug or module unload event has occurred.
4752 * Handle this by unregistering all objects associated
4753 * with this PCI device. Free those objects. Then finally
4754 * release PCI resources and disable device.
4757 * Inherited from PCI layer (may sleep).
4760 void ata_pci_remove_one (struct pci_dev *pdev)
4762 struct device *dev = pci_dev_to_dev(pdev);
4763 struct ata_host_set *host_set = dev_get_drvdata(dev);
4765 ata_host_set_remove(host_set);
4766 pci_release_regions(pdev);
4767 pci_disable_device(pdev);
4768 dev_set_drvdata(dev, NULL);
4771 /* move to PCI subsystem */
4772 int pci_test_config_bits(struct pci_dev *pdev, const struct pci_bits *bits)
4774 unsigned long tmp = 0;
4776 switch (bits->width) {
4779 pci_read_config_byte(pdev, bits->reg, &tmp8);
4785 pci_read_config_word(pdev, bits->reg, &tmp16);
4791 pci_read_config_dword(pdev, bits->reg, &tmp32);
4802 return (tmp == bits->val) ? 1 : 0;
4804 #endif /* CONFIG_PCI */
4807 static int __init ata_init(void)
4809 ata_wq = create_workqueue("ata");
4813 printk(KERN_DEBUG "libata version " DRV_VERSION " loaded.\n");
4817 static void __exit ata_exit(void)
4819 destroy_workqueue(ata_wq);
4822 module_init(ata_init);
4823 module_exit(ata_exit);
4825 static unsigned long ratelimit_time;
4826 static spinlock_t ata_ratelimit_lock = SPIN_LOCK_UNLOCKED;
4828 int ata_ratelimit(void)
4831 unsigned long flags;
4833 spin_lock_irqsave(&ata_ratelimit_lock, flags);
4835 if (time_after(jiffies, ratelimit_time)) {
4837 ratelimit_time = jiffies + (HZ/5);
4841 spin_unlock_irqrestore(&ata_ratelimit_lock, flags);
4847 * libata is essentially a library of internal helper functions for
4848 * low-level ATA host controller drivers. As such, the API/ABI is
4849 * likely to change as new drivers are added and updated.
4850 * Do not depend on ABI/API stability.
4853 EXPORT_SYMBOL_GPL(ata_std_bios_param);
4854 EXPORT_SYMBOL_GPL(ata_std_ports);
4855 EXPORT_SYMBOL_GPL(ata_device_add);
4856 EXPORT_SYMBOL_GPL(ata_host_set_remove);
4857 EXPORT_SYMBOL_GPL(ata_sg_init);
4858 EXPORT_SYMBOL_GPL(ata_sg_init_one);
4859 EXPORT_SYMBOL_GPL(ata_qc_complete);
4860 EXPORT_SYMBOL_GPL(ata_qc_issue_prot);
4861 EXPORT_SYMBOL_GPL(ata_eng_timeout);
4862 EXPORT_SYMBOL_GPL(ata_tf_load);
4863 EXPORT_SYMBOL_GPL(ata_tf_read);
4864 EXPORT_SYMBOL_GPL(ata_noop_dev_select);
4865 EXPORT_SYMBOL_GPL(ata_std_dev_select);
4866 EXPORT_SYMBOL_GPL(ata_tf_to_fis);
4867 EXPORT_SYMBOL_GPL(ata_tf_from_fis);
4868 EXPORT_SYMBOL_GPL(ata_check_status);
4869 EXPORT_SYMBOL_GPL(ata_altstatus);
4870 EXPORT_SYMBOL_GPL(ata_chk_err);
4871 EXPORT_SYMBOL_GPL(ata_exec_command);
4872 EXPORT_SYMBOL_GPL(ata_port_start);
4873 EXPORT_SYMBOL_GPL(ata_port_stop);
4874 EXPORT_SYMBOL_GPL(ata_host_stop);
4875 EXPORT_SYMBOL_GPL(ata_interrupt);
4876 EXPORT_SYMBOL_GPL(ata_qc_prep);
4877 EXPORT_SYMBOL_GPL(ata_bmdma_setup);
4878 EXPORT_SYMBOL_GPL(ata_bmdma_start);
4879 EXPORT_SYMBOL_GPL(ata_bmdma_irq_clear);
4880 EXPORT_SYMBOL_GPL(ata_bmdma_status);
4881 EXPORT_SYMBOL_GPL(ata_bmdma_stop);
4882 EXPORT_SYMBOL_GPL(ata_port_probe);
4883 EXPORT_SYMBOL_GPL(sata_phy_reset);
4884 EXPORT_SYMBOL_GPL(__sata_phy_reset);
4885 EXPORT_SYMBOL_GPL(ata_bus_reset);
4886 EXPORT_SYMBOL_GPL(ata_port_disable);
4887 EXPORT_SYMBOL_GPL(ata_ratelimit);
4888 EXPORT_SYMBOL_GPL(ata_scsi_ioctl);
4889 EXPORT_SYMBOL_GPL(ata_scsi_queuecmd);
4890 EXPORT_SYMBOL_GPL(ata_scsi_error);
4891 EXPORT_SYMBOL_GPL(ata_scsi_slave_config);
4892 EXPORT_SYMBOL_GPL(ata_scsi_release);
4893 EXPORT_SYMBOL_GPL(ata_host_intr);
4894 EXPORT_SYMBOL_GPL(ata_dev_classify);
4895 EXPORT_SYMBOL_GPL(ata_dev_id_string);
4896 EXPORT_SYMBOL_GPL(ata_dev_config);
4897 EXPORT_SYMBOL_GPL(ata_scsi_simulate);
4899 EXPORT_SYMBOL_GPL(ata_timing_compute);
4900 EXPORT_SYMBOL_GPL(ata_timing_merge);
4903 EXPORT_SYMBOL_GPL(pci_test_config_bits);
4904 EXPORT_SYMBOL_GPL(ata_pci_host_stop);
4905 EXPORT_SYMBOL_GPL(ata_pci_init_native_mode);
4906 EXPORT_SYMBOL_GPL(ata_pci_init_one);
4907 EXPORT_SYMBOL_GPL(ata_pci_remove_one);
4908 #endif /* CONFIG_PCI */