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/
33 * Standards documents from:
34 * http://www.t13.org (ATA standards, PCI DMA IDE spec)
35 * http://www.t10.org (SCSI MMC - for ATAPI MMC)
36 * http://www.sata-io.org (SATA)
37 * http://www.compactflash.org (CF)
38 * http://www.qic.org (QIC157 - Tape and DSC)
39 * http://www.ce-ata.org (CE-ATA: not supported)
43 #include <linux/kernel.h>
44 #include <linux/module.h>
45 #include <linux/pci.h>
46 #include <linux/init.h>
47 #include <linux/list.h>
49 #include <linux/highmem.h>
50 #include <linux/spinlock.h>
51 #include <linux/blkdev.h>
52 #include <linux/delay.h>
53 #include <linux/timer.h>
54 #include <linux/interrupt.h>
55 #include <linux/completion.h>
56 #include <linux/suspend.h>
57 #include <linux/workqueue.h>
58 #include <linux/jiffies.h>
59 #include <linux/scatterlist.h>
61 #include <scsi/scsi.h>
62 #include <scsi/scsi_cmnd.h>
63 #include <scsi/scsi_host.h>
64 #include <linux/libata.h>
65 #include <asm/semaphore.h>
66 #include <asm/byteorder.h>
67 #include <linux/cdrom.h>
72 /* debounce timing parameters in msecs { interval, duration, timeout } */
73 const unsigned long sata_deb_timing_normal[] = { 5, 100, 2000 };
74 const unsigned long sata_deb_timing_hotplug[] = { 25, 500, 2000 };
75 const unsigned long sata_deb_timing_long[] = { 100, 2000, 5000 };
77 static unsigned int ata_dev_init_params(struct ata_device *dev,
78 u16 heads, u16 sectors);
79 static unsigned int ata_dev_set_xfermode(struct ata_device *dev);
80 static unsigned int ata_dev_set_feature(struct ata_device *dev,
81 u8 enable, u8 feature);
82 static void ata_dev_xfermask(struct ata_device *dev);
83 static unsigned long ata_dev_blacklisted(const struct ata_device *dev);
85 unsigned int ata_print_id = 1;
86 static struct workqueue_struct *ata_wq;
88 struct workqueue_struct *ata_aux_wq;
90 int atapi_enabled = 1;
91 module_param(atapi_enabled, int, 0444);
92 MODULE_PARM_DESC(atapi_enabled, "Enable discovery of ATAPI devices (0=off, 1=on)");
95 module_param(atapi_dmadir, int, 0444);
96 MODULE_PARM_DESC(atapi_dmadir, "Enable ATAPI DMADIR bridge support (0=off, 1=on)");
98 int atapi_passthru16 = 1;
99 module_param(atapi_passthru16, int, 0444);
100 MODULE_PARM_DESC(atapi_passthru16, "Enable ATA_16 passthru for ATAPI devices; on by default (0=off, 1=on)");
103 module_param_named(fua, libata_fua, int, 0444);
104 MODULE_PARM_DESC(fua, "FUA support (0=off, 1=on)");
106 static int ata_ignore_hpa;
107 module_param_named(ignore_hpa, ata_ignore_hpa, int, 0644);
108 MODULE_PARM_DESC(ignore_hpa, "Ignore HPA limit (0=keep BIOS limits, 1=ignore limits, using full disk)");
110 static int libata_dma_mask = ATA_DMA_MASK_ATA|ATA_DMA_MASK_ATAPI|ATA_DMA_MASK_CFA;
111 module_param_named(dma, libata_dma_mask, int, 0444);
112 MODULE_PARM_DESC(dma, "DMA enable/disable (0x1==ATA, 0x2==ATAPI, 0x4==CF)");
114 static int ata_probe_timeout = ATA_TMOUT_INTERNAL / HZ;
115 module_param(ata_probe_timeout, int, 0444);
116 MODULE_PARM_DESC(ata_probe_timeout, "Set ATA probing timeout (seconds)");
118 int libata_noacpi = 0;
119 module_param_named(noacpi, libata_noacpi, int, 0444);
120 MODULE_PARM_DESC(noacpi, "Disables the use of ACPI in probe/suspend/resume when set");
122 int libata_allow_tpm = 0;
123 module_param_named(allow_tpm, libata_allow_tpm, int, 0444);
124 MODULE_PARM_DESC(allow_tpm, "Permit the use of TPM commands");
126 MODULE_AUTHOR("Jeff Garzik");
127 MODULE_DESCRIPTION("Library module for ATA devices");
128 MODULE_LICENSE("GPL");
129 MODULE_VERSION(DRV_VERSION);
133 * ata_tf_to_fis - Convert ATA taskfile to SATA FIS structure
134 * @tf: Taskfile to convert
135 * @pmp: Port multiplier port
136 * @is_cmd: This FIS is for command
137 * @fis: Buffer into which data will output
139 * Converts a standard ATA taskfile to a Serial ATA
140 * FIS structure (Register - Host to Device).
143 * Inherited from caller.
145 void ata_tf_to_fis(const struct ata_taskfile *tf, u8 pmp, int is_cmd, u8 *fis)
147 fis[0] = 0x27; /* Register - Host to Device FIS */
148 fis[1] = pmp & 0xf; /* Port multiplier number*/
150 fis[1] |= (1 << 7); /* bit 7 indicates Command FIS */
152 fis[2] = tf->command;
153 fis[3] = tf->feature;
160 fis[8] = tf->hob_lbal;
161 fis[9] = tf->hob_lbam;
162 fis[10] = tf->hob_lbah;
163 fis[11] = tf->hob_feature;
166 fis[13] = tf->hob_nsect;
177 * ata_tf_from_fis - Convert SATA FIS to ATA taskfile
178 * @fis: Buffer from which data will be input
179 * @tf: Taskfile to output
181 * Converts a serial ATA FIS structure to a standard ATA taskfile.
184 * Inherited from caller.
187 void ata_tf_from_fis(const u8 *fis, struct ata_taskfile *tf)
189 tf->command = fis[2]; /* status */
190 tf->feature = fis[3]; /* error */
197 tf->hob_lbal = fis[8];
198 tf->hob_lbam = fis[9];
199 tf->hob_lbah = fis[10];
202 tf->hob_nsect = fis[13];
205 static const u8 ata_rw_cmds[] = {
209 ATA_CMD_READ_MULTI_EXT,
210 ATA_CMD_WRITE_MULTI_EXT,
214 ATA_CMD_WRITE_MULTI_FUA_EXT,
218 ATA_CMD_PIO_READ_EXT,
219 ATA_CMD_PIO_WRITE_EXT,
232 ATA_CMD_WRITE_FUA_EXT
236 * ata_rwcmd_protocol - set taskfile r/w commands and protocol
237 * @tf: command to examine and configure
238 * @dev: device tf belongs to
240 * Examine the device configuration and tf->flags to calculate
241 * the proper read/write commands and protocol to use.
246 static int ata_rwcmd_protocol(struct ata_taskfile *tf, struct ata_device *dev)
250 int index, fua, lba48, write;
252 fua = (tf->flags & ATA_TFLAG_FUA) ? 4 : 0;
253 lba48 = (tf->flags & ATA_TFLAG_LBA48) ? 2 : 0;
254 write = (tf->flags & ATA_TFLAG_WRITE) ? 1 : 0;
256 if (dev->flags & ATA_DFLAG_PIO) {
257 tf->protocol = ATA_PROT_PIO;
258 index = dev->multi_count ? 0 : 8;
259 } else if (lba48 && (dev->link->ap->flags & ATA_FLAG_PIO_LBA48)) {
260 /* Unable to use DMA due to host limitation */
261 tf->protocol = ATA_PROT_PIO;
262 index = dev->multi_count ? 0 : 8;
264 tf->protocol = ATA_PROT_DMA;
268 cmd = ata_rw_cmds[index + fua + lba48 + write];
277 * ata_tf_read_block - Read block address from ATA taskfile
278 * @tf: ATA taskfile of interest
279 * @dev: ATA device @tf belongs to
284 * Read block address from @tf. This function can handle all
285 * three address formats - LBA, LBA48 and CHS. tf->protocol and
286 * flags select the address format to use.
289 * Block address read from @tf.
291 u64 ata_tf_read_block(struct ata_taskfile *tf, struct ata_device *dev)
295 if (tf->flags & ATA_TFLAG_LBA) {
296 if (tf->flags & ATA_TFLAG_LBA48) {
297 block |= (u64)tf->hob_lbah << 40;
298 block |= (u64)tf->hob_lbam << 32;
299 block |= tf->hob_lbal << 24;
301 block |= (tf->device & 0xf) << 24;
303 block |= tf->lbah << 16;
304 block |= tf->lbam << 8;
309 cyl = tf->lbam | (tf->lbah << 8);
310 head = tf->device & 0xf;
313 block = (cyl * dev->heads + head) * dev->sectors + sect;
320 * ata_build_rw_tf - Build ATA taskfile for given read/write request
321 * @tf: Target ATA taskfile
322 * @dev: ATA device @tf belongs to
323 * @block: Block address
324 * @n_block: Number of blocks
325 * @tf_flags: RW/FUA etc...
331 * Build ATA taskfile @tf for read/write request described by
332 * @block, @n_block, @tf_flags and @tag on @dev.
336 * 0 on success, -ERANGE if the request is too large for @dev,
337 * -EINVAL if the request is invalid.
339 int ata_build_rw_tf(struct ata_taskfile *tf, struct ata_device *dev,
340 u64 block, u32 n_block, unsigned int tf_flags,
343 tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
344 tf->flags |= tf_flags;
346 if (ata_ncq_enabled(dev) && likely(tag != ATA_TAG_INTERNAL)) {
348 if (!lba_48_ok(block, n_block))
351 tf->protocol = ATA_PROT_NCQ;
352 tf->flags |= ATA_TFLAG_LBA | ATA_TFLAG_LBA48;
354 if (tf->flags & ATA_TFLAG_WRITE)
355 tf->command = ATA_CMD_FPDMA_WRITE;
357 tf->command = ATA_CMD_FPDMA_READ;
359 tf->nsect = tag << 3;
360 tf->hob_feature = (n_block >> 8) & 0xff;
361 tf->feature = n_block & 0xff;
363 tf->hob_lbah = (block >> 40) & 0xff;
364 tf->hob_lbam = (block >> 32) & 0xff;
365 tf->hob_lbal = (block >> 24) & 0xff;
366 tf->lbah = (block >> 16) & 0xff;
367 tf->lbam = (block >> 8) & 0xff;
368 tf->lbal = block & 0xff;
371 if (tf->flags & ATA_TFLAG_FUA)
372 tf->device |= 1 << 7;
373 } else if (dev->flags & ATA_DFLAG_LBA) {
374 tf->flags |= ATA_TFLAG_LBA;
376 if (lba_28_ok(block, n_block)) {
378 tf->device |= (block >> 24) & 0xf;
379 } else if (lba_48_ok(block, n_block)) {
380 if (!(dev->flags & ATA_DFLAG_LBA48))
384 tf->flags |= ATA_TFLAG_LBA48;
386 tf->hob_nsect = (n_block >> 8) & 0xff;
388 tf->hob_lbah = (block >> 40) & 0xff;
389 tf->hob_lbam = (block >> 32) & 0xff;
390 tf->hob_lbal = (block >> 24) & 0xff;
392 /* request too large even for LBA48 */
395 if (unlikely(ata_rwcmd_protocol(tf, dev) < 0))
398 tf->nsect = n_block & 0xff;
400 tf->lbah = (block >> 16) & 0xff;
401 tf->lbam = (block >> 8) & 0xff;
402 tf->lbal = block & 0xff;
404 tf->device |= ATA_LBA;
407 u32 sect, head, cyl, track;
409 /* The request -may- be too large for CHS addressing. */
410 if (!lba_28_ok(block, n_block))
413 if (unlikely(ata_rwcmd_protocol(tf, dev) < 0))
416 /* Convert LBA to CHS */
417 track = (u32)block / dev->sectors;
418 cyl = track / dev->heads;
419 head = track % dev->heads;
420 sect = (u32)block % dev->sectors + 1;
422 DPRINTK("block %u track %u cyl %u head %u sect %u\n",
423 (u32)block, track, cyl, head, sect);
425 /* Check whether the converted CHS can fit.
429 if ((cyl >> 16) || (head >> 4) || (sect >> 8) || (!sect))
432 tf->nsect = n_block & 0xff; /* Sector count 0 means 256 sectors */
443 * ata_pack_xfermask - Pack pio, mwdma and udma masks into xfer_mask
444 * @pio_mask: pio_mask
445 * @mwdma_mask: mwdma_mask
446 * @udma_mask: udma_mask
448 * Pack @pio_mask, @mwdma_mask and @udma_mask into a single
449 * unsigned int xfer_mask.
457 unsigned int ata_pack_xfermask(unsigned int pio_mask,
458 unsigned int mwdma_mask, unsigned int udma_mask)
460 return ((pio_mask << ATA_SHIFT_PIO) & ATA_MASK_PIO) |
461 ((mwdma_mask << ATA_SHIFT_MWDMA) & ATA_MASK_MWDMA) |
462 ((udma_mask << ATA_SHIFT_UDMA) & ATA_MASK_UDMA);
466 * ata_unpack_xfermask - Unpack xfer_mask into pio, mwdma and udma masks
467 * @xfer_mask: xfer_mask to unpack
468 * @pio_mask: resulting pio_mask
469 * @mwdma_mask: resulting mwdma_mask
470 * @udma_mask: resulting udma_mask
472 * Unpack @xfer_mask into @pio_mask, @mwdma_mask and @udma_mask.
473 * Any NULL distination masks will be ignored.
475 void ata_unpack_xfermask(unsigned int xfer_mask, unsigned int *pio_mask,
476 unsigned int *mwdma_mask, unsigned int *udma_mask)
479 *pio_mask = (xfer_mask & ATA_MASK_PIO) >> ATA_SHIFT_PIO;
481 *mwdma_mask = (xfer_mask & ATA_MASK_MWDMA) >> ATA_SHIFT_MWDMA;
483 *udma_mask = (xfer_mask & ATA_MASK_UDMA) >> ATA_SHIFT_UDMA;
486 static const struct ata_xfer_ent {
490 { ATA_SHIFT_PIO, ATA_BITS_PIO, XFER_PIO_0 },
491 { ATA_SHIFT_MWDMA, ATA_BITS_MWDMA, XFER_MW_DMA_0 },
492 { ATA_SHIFT_UDMA, ATA_BITS_UDMA, XFER_UDMA_0 },
497 * ata_xfer_mask2mode - Find matching XFER_* for the given xfer_mask
498 * @xfer_mask: xfer_mask of interest
500 * Return matching XFER_* value for @xfer_mask. Only the highest
501 * bit of @xfer_mask is considered.
507 * Matching XFER_* value, 0 if no match found.
509 u8 ata_xfer_mask2mode(unsigned int xfer_mask)
511 int highbit = fls(xfer_mask) - 1;
512 const struct ata_xfer_ent *ent;
514 for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
515 if (highbit >= ent->shift && highbit < ent->shift + ent->bits)
516 return ent->base + highbit - ent->shift;
521 * ata_xfer_mode2mask - Find matching xfer_mask for XFER_*
522 * @xfer_mode: XFER_* of interest
524 * Return matching xfer_mask for @xfer_mode.
530 * Matching xfer_mask, 0 if no match found.
532 unsigned int ata_xfer_mode2mask(u8 xfer_mode)
534 const struct ata_xfer_ent *ent;
536 for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
537 if (xfer_mode >= ent->base && xfer_mode < ent->base + ent->bits)
538 return 1 << (ent->shift + xfer_mode - ent->base);
543 * ata_xfer_mode2shift - Find matching xfer_shift for XFER_*
544 * @xfer_mode: XFER_* of interest
546 * Return matching xfer_shift for @xfer_mode.
552 * Matching xfer_shift, -1 if no match found.
554 int ata_xfer_mode2shift(unsigned int xfer_mode)
556 const struct ata_xfer_ent *ent;
558 for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
559 if (xfer_mode >= ent->base && xfer_mode < ent->base + ent->bits)
565 * ata_mode_string - convert xfer_mask to string
566 * @xfer_mask: mask of bits supported; only highest bit counts.
568 * Determine string which represents the highest speed
569 * (highest bit in @modemask).
575 * Constant C string representing highest speed listed in
576 * @mode_mask, or the constant C string "<n/a>".
578 const char *ata_mode_string(unsigned int xfer_mask)
580 static const char * const xfer_mode_str[] = {
604 highbit = fls(xfer_mask) - 1;
605 if (highbit >= 0 && highbit < ARRAY_SIZE(xfer_mode_str))
606 return xfer_mode_str[highbit];
610 static const char *sata_spd_string(unsigned int spd)
612 static const char * const spd_str[] = {
617 if (spd == 0 || (spd - 1) >= ARRAY_SIZE(spd_str))
619 return spd_str[spd - 1];
622 void ata_dev_disable(struct ata_device *dev)
624 if (ata_dev_enabled(dev)) {
625 if (ata_msg_drv(dev->link->ap))
626 ata_dev_printk(dev, KERN_WARNING, "disabled\n");
627 ata_acpi_on_disable(dev);
628 ata_down_xfermask_limit(dev, ATA_DNXFER_FORCE_PIO0 |
634 static int ata_dev_set_dipm(struct ata_device *dev, enum link_pm policy)
636 struct ata_link *link = dev->link;
637 struct ata_port *ap = link->ap;
639 unsigned int err_mask;
643 * disallow DIPM for drivers which haven't set
644 * ATA_FLAG_IPM. This is because when DIPM is enabled,
645 * phy ready will be set in the interrupt status on
646 * state changes, which will cause some drivers to
647 * think there are errors - additionally drivers will
648 * need to disable hot plug.
650 if (!(ap->flags & ATA_FLAG_IPM) || !ata_dev_enabled(dev)) {
651 ap->pm_policy = NOT_AVAILABLE;
656 * For DIPM, we will only enable it for the
659 * Why? Because Disks are too stupid to know that
660 * If the host rejects a request to go to SLUMBER
661 * they should retry at PARTIAL, and instead it
662 * just would give up. So, for medium_power to
663 * work at all, we need to only allow HIPM.
665 rc = sata_scr_read(link, SCR_CONTROL, &scontrol);
671 /* no restrictions on IPM transitions */
672 scontrol &= ~(0x3 << 8);
673 rc = sata_scr_write(link, SCR_CONTROL, scontrol);
678 if (dev->flags & ATA_DFLAG_DIPM)
679 err_mask = ata_dev_set_feature(dev,
680 SETFEATURES_SATA_ENABLE, SATA_DIPM);
683 /* allow IPM to PARTIAL */
684 scontrol &= ~(0x1 << 8);
685 scontrol |= (0x2 << 8);
686 rc = sata_scr_write(link, SCR_CONTROL, scontrol);
691 * we don't have to disable DIPM since IPM flags
692 * disallow transitions to SLUMBER, which effectively
693 * disable DIPM if it does not support PARTIAL
697 case MAX_PERFORMANCE:
698 /* disable all IPM transitions */
699 scontrol |= (0x3 << 8);
700 rc = sata_scr_write(link, SCR_CONTROL, scontrol);
705 * we don't have to disable DIPM since IPM flags
706 * disallow all transitions which effectively
707 * disable DIPM anyway.
712 /* FIXME: handle SET FEATURES failure */
719 * ata_dev_enable_pm - enable SATA interface power management
720 * @dev: device to enable power management
721 * @policy: the link power management policy
723 * Enable SATA Interface power management. This will enable
724 * Device Interface Power Management (DIPM) for min_power
725 * policy, and then call driver specific callbacks for
726 * enabling Host Initiated Power management.
729 * Returns: -EINVAL if IPM is not supported, 0 otherwise.
731 void ata_dev_enable_pm(struct ata_device *dev, enum link_pm policy)
734 struct ata_port *ap = dev->link->ap;
736 /* set HIPM first, then DIPM */
737 if (ap->ops->enable_pm)
738 rc = ap->ops->enable_pm(ap, policy);
741 rc = ata_dev_set_dipm(dev, policy);
745 ap->pm_policy = MAX_PERFORMANCE;
747 ap->pm_policy = policy;
748 return /* rc */; /* hopefully we can use 'rc' eventually */
753 * ata_dev_disable_pm - disable SATA interface power management
754 * @dev: device to disable power management
756 * Disable SATA Interface power management. This will disable
757 * Device Interface Power Management (DIPM) without changing
758 * policy, call driver specific callbacks for disabling Host
759 * Initiated Power management.
764 static void ata_dev_disable_pm(struct ata_device *dev)
766 struct ata_port *ap = dev->link->ap;
768 ata_dev_set_dipm(dev, MAX_PERFORMANCE);
769 if (ap->ops->disable_pm)
770 ap->ops->disable_pm(ap);
772 #endif /* CONFIG_PM */
774 void ata_lpm_schedule(struct ata_port *ap, enum link_pm policy)
776 ap->pm_policy = policy;
777 ap->link.eh_info.action |= ATA_EHI_LPM;
778 ap->link.eh_info.flags |= ATA_EHI_NO_AUTOPSY;
779 ata_port_schedule_eh(ap);
783 static void ata_lpm_enable(struct ata_host *host)
785 struct ata_link *link;
787 struct ata_device *dev;
790 for (i = 0; i < host->n_ports; i++) {
792 ata_port_for_each_link(link, ap) {
793 ata_link_for_each_dev(dev, link)
794 ata_dev_disable_pm(dev);
799 static void ata_lpm_disable(struct ata_host *host)
803 for (i = 0; i < host->n_ports; i++) {
804 struct ata_port *ap = host->ports[i];
805 ata_lpm_schedule(ap, ap->pm_policy);
808 #endif /* CONFIG_PM */
812 * ata_devchk - PATA device presence detection
813 * @ap: ATA channel to examine
814 * @device: Device to examine (starting at zero)
816 * This technique was originally described in
817 * Hale Landis's ATADRVR (www.ata-atapi.com), and
818 * later found its way into the ATA/ATAPI spec.
820 * Write a pattern to the ATA shadow registers,
821 * and if a device is present, it will respond by
822 * correctly storing and echoing back the
823 * ATA shadow register contents.
829 static unsigned int ata_devchk(struct ata_port *ap, unsigned int device)
831 struct ata_ioports *ioaddr = &ap->ioaddr;
834 ap->ops->dev_select(ap, device);
836 iowrite8(0x55, ioaddr->nsect_addr);
837 iowrite8(0xaa, ioaddr->lbal_addr);
839 iowrite8(0xaa, ioaddr->nsect_addr);
840 iowrite8(0x55, ioaddr->lbal_addr);
842 iowrite8(0x55, ioaddr->nsect_addr);
843 iowrite8(0xaa, ioaddr->lbal_addr);
845 nsect = ioread8(ioaddr->nsect_addr);
846 lbal = ioread8(ioaddr->lbal_addr);
848 if ((nsect == 0x55) && (lbal == 0xaa))
849 return 1; /* we found a device */
851 return 0; /* nothing found */
855 * ata_dev_classify - determine device type based on ATA-spec signature
856 * @tf: ATA taskfile register set for device to be identified
858 * Determine from taskfile register contents whether a device is
859 * ATA or ATAPI, as per "Signature and persistence" section
860 * of ATA/PI spec (volume 1, sect 5.14).
866 * Device type, %ATA_DEV_ATA, %ATA_DEV_ATAPI, %ATA_DEV_PMP or
867 * %ATA_DEV_UNKNOWN the event of failure.
869 unsigned int ata_dev_classify(const struct ata_taskfile *tf)
871 /* Apple's open source Darwin code hints that some devices only
872 * put a proper signature into the LBA mid/high registers,
873 * So, we only check those. It's sufficient for uniqueness.
875 * ATA/ATAPI-7 (d1532v1r1: Feb. 19, 2003) specified separate
876 * signatures for ATA and ATAPI devices attached on SerialATA,
877 * 0x3c/0xc3 and 0x69/0x96 respectively. However, SerialATA
878 * spec has never mentioned about using different signatures
879 * for ATA/ATAPI devices. Then, Serial ATA II: Port
880 * Multiplier specification began to use 0x69/0x96 to identify
881 * port multpliers and 0x3c/0xc3 to identify SEMB device.
882 * ATA/ATAPI-7 dropped descriptions about 0x3c/0xc3 and
883 * 0x69/0x96 shortly and described them as reserved for
886 * We follow the current spec and consider that 0x69/0x96
887 * identifies a port multiplier and 0x3c/0xc3 a SEMB device.
889 if ((tf->lbam == 0) && (tf->lbah == 0)) {
890 DPRINTK("found ATA device by sig\n");
894 if ((tf->lbam == 0x14) && (tf->lbah == 0xeb)) {
895 DPRINTK("found ATAPI device by sig\n");
896 return ATA_DEV_ATAPI;
899 if ((tf->lbam == 0x69) && (tf->lbah == 0x96)) {
900 DPRINTK("found PMP device by sig\n");
904 if ((tf->lbam == 0x3c) && (tf->lbah == 0xc3)) {
905 printk(KERN_INFO "ata: SEMB device ignored\n");
906 return ATA_DEV_SEMB_UNSUP; /* not yet */
909 DPRINTK("unknown device\n");
910 return ATA_DEV_UNKNOWN;
914 * ata_dev_try_classify - Parse returned ATA device signature
915 * @dev: ATA device to classify (starting at zero)
916 * @present: device seems present
917 * @r_err: Value of error register on completion
919 * After an event -- SRST, E.D.D., or SATA COMRESET -- occurs,
920 * an ATA/ATAPI-defined set of values is placed in the ATA
921 * shadow registers, indicating the results of device detection
924 * Select the ATA device, and read the values from the ATA shadow
925 * registers. Then parse according to the Error register value,
926 * and the spec-defined values examined by ata_dev_classify().
932 * Device type - %ATA_DEV_ATA, %ATA_DEV_ATAPI or %ATA_DEV_NONE.
934 unsigned int ata_dev_try_classify(struct ata_device *dev, int present,
937 struct ata_port *ap = dev->link->ap;
938 struct ata_taskfile tf;
942 ap->ops->dev_select(ap, dev->devno);
944 memset(&tf, 0, sizeof(tf));
946 ap->ops->tf_read(ap, &tf);
951 /* see if device passed diags: if master then continue and warn later */
952 if (err == 0 && dev->devno == 0)
953 /* diagnostic fail : do nothing _YET_ */
954 dev->horkage |= ATA_HORKAGE_DIAGNOSTIC;
957 else if ((dev->devno == 0) && (err == 0x81))
962 /* determine if device is ATA or ATAPI */
963 class = ata_dev_classify(&tf);
965 if (class == ATA_DEV_UNKNOWN) {
966 /* If the device failed diagnostic, it's likely to
967 * have reported incorrect device signature too.
968 * Assume ATA device if the device seems present but
969 * device signature is invalid with diagnostic
972 if (present && (dev->horkage & ATA_HORKAGE_DIAGNOSTIC))
975 class = ATA_DEV_NONE;
976 } else if ((class == ATA_DEV_ATA) && (ata_chk_status(ap) == 0))
977 class = ATA_DEV_NONE;
983 * ata_id_string - Convert IDENTIFY DEVICE page into string
984 * @id: IDENTIFY DEVICE results we will examine
985 * @s: string into which data is output
986 * @ofs: offset into identify device page
987 * @len: length of string to return. must be an even number.
989 * The strings in the IDENTIFY DEVICE page are broken up into
990 * 16-bit chunks. Run through the string, and output each
991 * 8-bit chunk linearly, regardless of platform.
997 void ata_id_string(const u16 *id, unsigned char *s,
998 unsigned int ofs, unsigned int len)
1017 * ata_id_c_string - Convert IDENTIFY DEVICE page into C string
1018 * @id: IDENTIFY DEVICE results we will examine
1019 * @s: string into which data is output
1020 * @ofs: offset into identify device page
1021 * @len: length of string to return. must be an odd number.
1023 * This function is identical to ata_id_string except that it
1024 * trims trailing spaces and terminates the resulting string with
1025 * null. @len must be actual maximum length (even number) + 1.
1030 void ata_id_c_string(const u16 *id, unsigned char *s,
1031 unsigned int ofs, unsigned int len)
1035 WARN_ON(!(len & 1));
1037 ata_id_string(id, s, ofs, len - 1);
1039 p = s + strnlen(s, len - 1);
1040 while (p > s && p[-1] == ' ')
1045 static u64 ata_id_n_sectors(const u16 *id)
1047 if (ata_id_has_lba(id)) {
1048 if (ata_id_has_lba48(id))
1049 return ata_id_u64(id, 100);
1051 return ata_id_u32(id, 60);
1053 if (ata_id_current_chs_valid(id))
1054 return ata_id_u32(id, 57);
1056 return id[1] * id[3] * id[6];
1060 static u64 ata_tf_to_lba48(struct ata_taskfile *tf)
1064 sectors |= ((u64)(tf->hob_lbah & 0xff)) << 40;
1065 sectors |= ((u64)(tf->hob_lbam & 0xff)) << 32;
1066 sectors |= (tf->hob_lbal & 0xff) << 24;
1067 sectors |= (tf->lbah & 0xff) << 16;
1068 sectors |= (tf->lbam & 0xff) << 8;
1069 sectors |= (tf->lbal & 0xff);
1074 static u64 ata_tf_to_lba(struct ata_taskfile *tf)
1078 sectors |= (tf->device & 0x0f) << 24;
1079 sectors |= (tf->lbah & 0xff) << 16;
1080 sectors |= (tf->lbam & 0xff) << 8;
1081 sectors |= (tf->lbal & 0xff);
1087 * ata_read_native_max_address - Read native max address
1088 * @dev: target device
1089 * @max_sectors: out parameter for the result native max address
1091 * Perform an LBA48 or LBA28 native size query upon the device in
1095 * 0 on success, -EACCES if command is aborted by the drive.
1096 * -EIO on other errors.
1098 static int ata_read_native_max_address(struct ata_device *dev, u64 *max_sectors)
1100 unsigned int err_mask;
1101 struct ata_taskfile tf;
1102 int lba48 = ata_id_has_lba48(dev->id);
1104 ata_tf_init(dev, &tf);
1106 /* always clear all address registers */
1107 tf.flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_ISADDR;
1110 tf.command = ATA_CMD_READ_NATIVE_MAX_EXT;
1111 tf.flags |= ATA_TFLAG_LBA48;
1113 tf.command = ATA_CMD_READ_NATIVE_MAX;
1115 tf.protocol |= ATA_PROT_NODATA;
1116 tf.device |= ATA_LBA;
1118 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
1120 ata_dev_printk(dev, KERN_WARNING, "failed to read native "
1121 "max address (err_mask=0x%x)\n", err_mask);
1122 if (err_mask == AC_ERR_DEV && (tf.feature & ATA_ABORTED))
1128 *max_sectors = ata_tf_to_lba48(&tf);
1130 *max_sectors = ata_tf_to_lba(&tf);
1131 if (dev->horkage & ATA_HORKAGE_HPA_SIZE)
1137 * ata_set_max_sectors - Set max sectors
1138 * @dev: target device
1139 * @new_sectors: new max sectors value to set for the device
1141 * Set max sectors of @dev to @new_sectors.
1144 * 0 on success, -EACCES if command is aborted or denied (due to
1145 * previous non-volatile SET_MAX) by the drive. -EIO on other
1148 static int ata_set_max_sectors(struct ata_device *dev, u64 new_sectors)
1150 unsigned int err_mask;
1151 struct ata_taskfile tf;
1152 int lba48 = ata_id_has_lba48(dev->id);
1156 ata_tf_init(dev, &tf);
1158 tf.flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_ISADDR;
1161 tf.command = ATA_CMD_SET_MAX_EXT;
1162 tf.flags |= ATA_TFLAG_LBA48;
1164 tf.hob_lbal = (new_sectors >> 24) & 0xff;
1165 tf.hob_lbam = (new_sectors >> 32) & 0xff;
1166 tf.hob_lbah = (new_sectors >> 40) & 0xff;
1168 tf.command = ATA_CMD_SET_MAX;
1170 tf.device |= (new_sectors >> 24) & 0xf;
1173 tf.protocol |= ATA_PROT_NODATA;
1174 tf.device |= ATA_LBA;
1176 tf.lbal = (new_sectors >> 0) & 0xff;
1177 tf.lbam = (new_sectors >> 8) & 0xff;
1178 tf.lbah = (new_sectors >> 16) & 0xff;
1180 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
1182 ata_dev_printk(dev, KERN_WARNING, "failed to set "
1183 "max address (err_mask=0x%x)\n", err_mask);
1184 if (err_mask == AC_ERR_DEV &&
1185 (tf.feature & (ATA_ABORTED | ATA_IDNF)))
1194 * ata_hpa_resize - Resize a device with an HPA set
1195 * @dev: Device to resize
1197 * Read the size of an LBA28 or LBA48 disk with HPA features and resize
1198 * it if required to the full size of the media. The caller must check
1199 * the drive has the HPA feature set enabled.
1202 * 0 on success, -errno on failure.
1204 static int ata_hpa_resize(struct ata_device *dev)
1206 struct ata_eh_context *ehc = &dev->link->eh_context;
1207 int print_info = ehc->i.flags & ATA_EHI_PRINTINFO;
1208 u64 sectors = ata_id_n_sectors(dev->id);
1212 /* do we need to do it? */
1213 if (dev->class != ATA_DEV_ATA ||
1214 !ata_id_has_lba(dev->id) || !ata_id_hpa_enabled(dev->id) ||
1215 (dev->horkage & ATA_HORKAGE_BROKEN_HPA))
1218 /* read native max address */
1219 rc = ata_read_native_max_address(dev, &native_sectors);
1221 /* If HPA isn't going to be unlocked, skip HPA
1222 * resizing from the next try.
1224 if (!ata_ignore_hpa) {
1225 ata_dev_printk(dev, KERN_WARNING, "HPA support seems "
1226 "broken, will skip HPA handling\n");
1227 dev->horkage |= ATA_HORKAGE_BROKEN_HPA;
1229 /* we can continue if device aborted the command */
1237 /* nothing to do? */
1238 if (native_sectors <= sectors || !ata_ignore_hpa) {
1239 if (!print_info || native_sectors == sectors)
1242 if (native_sectors > sectors)
1243 ata_dev_printk(dev, KERN_INFO,
1244 "HPA detected: current %llu, native %llu\n",
1245 (unsigned long long)sectors,
1246 (unsigned long long)native_sectors);
1247 else if (native_sectors < sectors)
1248 ata_dev_printk(dev, KERN_WARNING,
1249 "native sectors (%llu) is smaller than "
1251 (unsigned long long)native_sectors,
1252 (unsigned long long)sectors);
1256 /* let's unlock HPA */
1257 rc = ata_set_max_sectors(dev, native_sectors);
1258 if (rc == -EACCES) {
1259 /* if device aborted the command, skip HPA resizing */
1260 ata_dev_printk(dev, KERN_WARNING, "device aborted resize "
1261 "(%llu -> %llu), skipping HPA handling\n",
1262 (unsigned long long)sectors,
1263 (unsigned long long)native_sectors);
1264 dev->horkage |= ATA_HORKAGE_BROKEN_HPA;
1269 /* re-read IDENTIFY data */
1270 rc = ata_dev_reread_id(dev, 0);
1272 ata_dev_printk(dev, KERN_ERR, "failed to re-read IDENTIFY "
1273 "data after HPA resizing\n");
1278 u64 new_sectors = ata_id_n_sectors(dev->id);
1279 ata_dev_printk(dev, KERN_INFO,
1280 "HPA unlocked: %llu -> %llu, native %llu\n",
1281 (unsigned long long)sectors,
1282 (unsigned long long)new_sectors,
1283 (unsigned long long)native_sectors);
1290 * ata_id_to_dma_mode - Identify DMA mode from id block
1291 * @dev: device to identify
1292 * @unknown: mode to assume if we cannot tell
1294 * Set up the timing values for the device based upon the identify
1295 * reported values for the DMA mode. This function is used by drivers
1296 * which rely upon firmware configured modes, but wish to report the
1297 * mode correctly when possible.
1299 * In addition we emit similarly formatted messages to the default
1300 * ata_dev_set_mode handler, in order to provide consistency of
1304 void ata_id_to_dma_mode(struct ata_device *dev, u8 unknown)
1309 /* Pack the DMA modes */
1310 mask = ((dev->id[63] >> 8) << ATA_SHIFT_MWDMA) & ATA_MASK_MWDMA;
1311 if (dev->id[53] & 0x04)
1312 mask |= ((dev->id[88] >> 8) << ATA_SHIFT_UDMA) & ATA_MASK_UDMA;
1314 /* Select the mode in use */
1315 mode = ata_xfer_mask2mode(mask);
1318 ata_dev_printk(dev, KERN_INFO, "configured for %s\n",
1319 ata_mode_string(mask));
1321 /* SWDMA perhaps ? */
1323 ata_dev_printk(dev, KERN_INFO, "configured for DMA\n");
1326 /* Configure the device reporting */
1327 dev->xfer_mode = mode;
1328 dev->xfer_shift = ata_xfer_mode2shift(mode);
1332 * ata_noop_dev_select - Select device 0/1 on ATA bus
1333 * @ap: ATA channel to manipulate
1334 * @device: ATA device (numbered from zero) to select
1336 * This function performs no actual function.
1338 * May be used as the dev_select() entry in ata_port_operations.
1343 void ata_noop_dev_select(struct ata_port *ap, unsigned int device)
1349 * ata_std_dev_select - Select device 0/1 on ATA bus
1350 * @ap: ATA channel to manipulate
1351 * @device: ATA device (numbered from zero) to select
1353 * Use the method defined in the ATA specification to
1354 * make either device 0, or device 1, active on the
1355 * ATA channel. Works with both PIO and MMIO.
1357 * May be used as the dev_select() entry in ata_port_operations.
1363 void ata_std_dev_select(struct ata_port *ap, unsigned int device)
1368 tmp = ATA_DEVICE_OBS;
1370 tmp = ATA_DEVICE_OBS | ATA_DEV1;
1372 iowrite8(tmp, ap->ioaddr.device_addr);
1373 ata_pause(ap); /* needed; also flushes, for mmio */
1377 * ata_dev_select - Select device 0/1 on ATA bus
1378 * @ap: ATA channel to manipulate
1379 * @device: ATA device (numbered from zero) to select
1380 * @wait: non-zero to wait for Status register BSY bit to clear
1381 * @can_sleep: non-zero if context allows sleeping
1383 * Use the method defined in the ATA specification to
1384 * make either device 0, or device 1, active on the
1387 * This is a high-level version of ata_std_dev_select(),
1388 * which additionally provides the services of inserting
1389 * the proper pauses and status polling, where needed.
1395 void ata_dev_select(struct ata_port *ap, unsigned int device,
1396 unsigned int wait, unsigned int can_sleep)
1398 if (ata_msg_probe(ap))
1399 ata_port_printk(ap, KERN_INFO, "ata_dev_select: ENTER, "
1400 "device %u, wait %u\n", device, wait);
1405 ap->ops->dev_select(ap, device);
1408 if (can_sleep && ap->link.device[device].class == ATA_DEV_ATAPI)
1415 * ata_dump_id - IDENTIFY DEVICE info debugging output
1416 * @id: IDENTIFY DEVICE page to dump
1418 * Dump selected 16-bit words from the given IDENTIFY DEVICE
1425 static inline void ata_dump_id(const u16 *id)
1427 DPRINTK("49==0x%04x "
1437 DPRINTK("80==0x%04x "
1447 DPRINTK("88==0x%04x "
1454 * ata_id_xfermask - Compute xfermask from the given IDENTIFY data
1455 * @id: IDENTIFY data to compute xfer mask from
1457 * Compute the xfermask for this device. This is not as trivial
1458 * as it seems if we must consider early devices correctly.
1460 * FIXME: pre IDE drive timing (do we care ?).
1468 unsigned int ata_id_xfermask(const u16 *id)
1470 unsigned int pio_mask, mwdma_mask, udma_mask;
1472 /* Usual case. Word 53 indicates word 64 is valid */
1473 if (id[ATA_ID_FIELD_VALID] & (1 << 1)) {
1474 pio_mask = id[ATA_ID_PIO_MODES] & 0x03;
1478 /* If word 64 isn't valid then Word 51 high byte holds
1479 * the PIO timing number for the maximum. Turn it into
1482 u8 mode = (id[ATA_ID_OLD_PIO_MODES] >> 8) & 0xFF;
1483 if (mode < 5) /* Valid PIO range */
1484 pio_mask = (2 << mode) - 1;
1488 /* But wait.. there's more. Design your standards by
1489 * committee and you too can get a free iordy field to
1490 * process. However its the speeds not the modes that
1491 * are supported... Note drivers using the timing API
1492 * will get this right anyway
1496 mwdma_mask = id[ATA_ID_MWDMA_MODES] & 0x07;
1498 if (ata_id_is_cfa(id)) {
1500 * Process compact flash extended modes
1502 int pio = id[163] & 0x7;
1503 int dma = (id[163] >> 3) & 7;
1506 pio_mask |= (1 << 5);
1508 pio_mask |= (1 << 6);
1510 mwdma_mask |= (1 << 3);
1512 mwdma_mask |= (1 << 4);
1516 if (id[ATA_ID_FIELD_VALID] & (1 << 2))
1517 udma_mask = id[ATA_ID_UDMA_MODES] & 0xff;
1519 return ata_pack_xfermask(pio_mask, mwdma_mask, udma_mask);
1523 * ata_port_queue_task - Queue port_task
1524 * @ap: The ata_port to queue port_task for
1525 * @fn: workqueue function to be scheduled
1526 * @data: data for @fn to use
1527 * @delay: delay time for workqueue function
1529 * Schedule @fn(@data) for execution after @delay jiffies using
1530 * port_task. There is one port_task per port and it's the
1531 * user(low level driver)'s responsibility to make sure that only
1532 * one task is active at any given time.
1534 * libata core layer takes care of synchronization between
1535 * port_task and EH. ata_port_queue_task() may be ignored for EH
1539 * Inherited from caller.
1541 void ata_port_queue_task(struct ata_port *ap, work_func_t fn, void *data,
1542 unsigned long delay)
1544 PREPARE_DELAYED_WORK(&ap->port_task, fn);
1545 ap->port_task_data = data;
1547 /* may fail if ata_port_flush_task() in progress */
1548 queue_delayed_work(ata_wq, &ap->port_task, delay);
1552 * ata_port_flush_task - Flush port_task
1553 * @ap: The ata_port to flush port_task for
1555 * After this function completes, port_task is guranteed not to
1556 * be running or scheduled.
1559 * Kernel thread context (may sleep)
1561 void ata_port_flush_task(struct ata_port *ap)
1565 cancel_rearming_delayed_work(&ap->port_task);
1567 if (ata_msg_ctl(ap))
1568 ata_port_printk(ap, KERN_DEBUG, "%s: EXIT\n", __FUNCTION__);
1571 static void ata_qc_complete_internal(struct ata_queued_cmd *qc)
1573 struct completion *waiting = qc->private_data;
1579 * ata_exec_internal_sg - execute libata internal command
1580 * @dev: Device to which the command is sent
1581 * @tf: Taskfile registers for the command and the result
1582 * @cdb: CDB for packet command
1583 * @dma_dir: Data tranfer direction of the command
1584 * @sgl: sg list for the data buffer of the command
1585 * @n_elem: Number of sg entries
1586 * @timeout: Timeout in msecs (0 for default)
1588 * Executes libata internal command with timeout. @tf contains
1589 * command on entry and result on return. Timeout and error
1590 * conditions are reported via return value. No recovery action
1591 * is taken after a command times out. It's caller's duty to
1592 * clean up after timeout.
1595 * None. Should be called with kernel context, might sleep.
1598 * Zero on success, AC_ERR_* mask on failure
1600 unsigned ata_exec_internal_sg(struct ata_device *dev,
1601 struct ata_taskfile *tf, const u8 *cdb,
1602 int dma_dir, struct scatterlist *sgl,
1603 unsigned int n_elem, unsigned long timeout)
1605 struct ata_link *link = dev->link;
1606 struct ata_port *ap = link->ap;
1607 u8 command = tf->command;
1608 struct ata_queued_cmd *qc;
1609 unsigned int tag, preempted_tag;
1610 u32 preempted_sactive, preempted_qc_active;
1611 int preempted_nr_active_links;
1612 DECLARE_COMPLETION_ONSTACK(wait);
1613 unsigned long flags;
1614 unsigned int err_mask;
1617 spin_lock_irqsave(ap->lock, flags);
1619 /* no internal command while frozen */
1620 if (ap->pflags & ATA_PFLAG_FROZEN) {
1621 spin_unlock_irqrestore(ap->lock, flags);
1622 return AC_ERR_SYSTEM;
1625 /* initialize internal qc */
1627 /* XXX: Tag 0 is used for drivers with legacy EH as some
1628 * drivers choke if any other tag is given. This breaks
1629 * ata_tag_internal() test for those drivers. Don't use new
1630 * EH stuff without converting to it.
1632 if (ap->ops->error_handler)
1633 tag = ATA_TAG_INTERNAL;
1637 if (test_and_set_bit(tag, &ap->qc_allocated))
1639 qc = __ata_qc_from_tag(ap, tag);
1647 preempted_tag = link->active_tag;
1648 preempted_sactive = link->sactive;
1649 preempted_qc_active = ap->qc_active;
1650 preempted_nr_active_links = ap->nr_active_links;
1651 link->active_tag = ATA_TAG_POISON;
1654 ap->nr_active_links = 0;
1656 /* prepare & issue qc */
1659 memcpy(qc->cdb, cdb, ATAPI_CDB_LEN);
1660 qc->flags |= ATA_QCFLAG_RESULT_TF;
1661 qc->dma_dir = dma_dir;
1662 if (dma_dir != DMA_NONE) {
1663 unsigned int i, buflen = 0;
1664 struct scatterlist *sg;
1666 for_each_sg(sgl, sg, n_elem, i)
1667 buflen += sg->length;
1669 ata_sg_init(qc, sgl, n_elem);
1670 qc->nbytes = buflen;
1673 qc->private_data = &wait;
1674 qc->complete_fn = ata_qc_complete_internal;
1678 spin_unlock_irqrestore(ap->lock, flags);
1681 timeout = ata_probe_timeout * 1000 / HZ;
1683 rc = wait_for_completion_timeout(&wait, msecs_to_jiffies(timeout));
1685 ata_port_flush_task(ap);
1688 spin_lock_irqsave(ap->lock, flags);
1690 /* We're racing with irq here. If we lose, the
1691 * following test prevents us from completing the qc
1692 * twice. If we win, the port is frozen and will be
1693 * cleaned up by ->post_internal_cmd().
1695 if (qc->flags & ATA_QCFLAG_ACTIVE) {
1696 qc->err_mask |= AC_ERR_TIMEOUT;
1698 if (ap->ops->error_handler)
1699 ata_port_freeze(ap);
1701 ata_qc_complete(qc);
1703 if (ata_msg_warn(ap))
1704 ata_dev_printk(dev, KERN_WARNING,
1705 "qc timeout (cmd 0x%x)\n", command);
1708 spin_unlock_irqrestore(ap->lock, flags);
1711 /* do post_internal_cmd */
1712 if (ap->ops->post_internal_cmd)
1713 ap->ops->post_internal_cmd(qc);
1715 /* perform minimal error analysis */
1716 if (qc->flags & ATA_QCFLAG_FAILED) {
1717 if (qc->result_tf.command & (ATA_ERR | ATA_DF))
1718 qc->err_mask |= AC_ERR_DEV;
1721 qc->err_mask |= AC_ERR_OTHER;
1723 if (qc->err_mask & ~AC_ERR_OTHER)
1724 qc->err_mask &= ~AC_ERR_OTHER;
1728 spin_lock_irqsave(ap->lock, flags);
1730 *tf = qc->result_tf;
1731 err_mask = qc->err_mask;
1734 link->active_tag = preempted_tag;
1735 link->sactive = preempted_sactive;
1736 ap->qc_active = preempted_qc_active;
1737 ap->nr_active_links = preempted_nr_active_links;
1739 /* XXX - Some LLDDs (sata_mv) disable port on command failure.
1740 * Until those drivers are fixed, we detect the condition
1741 * here, fail the command with AC_ERR_SYSTEM and reenable the
1744 * Note that this doesn't change any behavior as internal
1745 * command failure results in disabling the device in the
1746 * higher layer for LLDDs without new reset/EH callbacks.
1748 * Kill the following code as soon as those drivers are fixed.
1750 if (ap->flags & ATA_FLAG_DISABLED) {
1751 err_mask |= AC_ERR_SYSTEM;
1755 spin_unlock_irqrestore(ap->lock, flags);
1761 * ata_exec_internal - execute libata internal command
1762 * @dev: Device to which the command is sent
1763 * @tf: Taskfile registers for the command and the result
1764 * @cdb: CDB for packet command
1765 * @dma_dir: Data tranfer direction of the command
1766 * @buf: Data buffer of the command
1767 * @buflen: Length of data buffer
1768 * @timeout: Timeout in msecs (0 for default)
1770 * Wrapper around ata_exec_internal_sg() which takes simple
1771 * buffer instead of sg list.
1774 * None. Should be called with kernel context, might sleep.
1777 * Zero on success, AC_ERR_* mask on failure
1779 unsigned ata_exec_internal(struct ata_device *dev,
1780 struct ata_taskfile *tf, const u8 *cdb,
1781 int dma_dir, void *buf, unsigned int buflen,
1782 unsigned long timeout)
1784 struct scatterlist *psg = NULL, sg;
1785 unsigned int n_elem = 0;
1787 if (dma_dir != DMA_NONE) {
1789 sg_init_one(&sg, buf, buflen);
1794 return ata_exec_internal_sg(dev, tf, cdb, dma_dir, psg, n_elem,
1799 * ata_do_simple_cmd - execute simple internal command
1800 * @dev: Device to which the command is sent
1801 * @cmd: Opcode to execute
1803 * Execute a 'simple' command, that only consists of the opcode
1804 * 'cmd' itself, without filling any other registers
1807 * Kernel thread context (may sleep).
1810 * Zero on success, AC_ERR_* mask on failure
1812 unsigned int ata_do_simple_cmd(struct ata_device *dev, u8 cmd)
1814 struct ata_taskfile tf;
1816 ata_tf_init(dev, &tf);
1819 tf.flags |= ATA_TFLAG_DEVICE;
1820 tf.protocol = ATA_PROT_NODATA;
1822 return ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
1826 * ata_pio_need_iordy - check if iordy needed
1829 * Check if the current speed of the device requires IORDY. Used
1830 * by various controllers for chip configuration.
1833 unsigned int ata_pio_need_iordy(const struct ata_device *adev)
1835 /* Controller doesn't support IORDY. Probably a pointless check
1836 as the caller should know this */
1837 if (adev->link->ap->flags & ATA_FLAG_NO_IORDY)
1839 /* PIO3 and higher it is mandatory */
1840 if (adev->pio_mode > XFER_PIO_2)
1842 /* We turn it on when possible */
1843 if (ata_id_has_iordy(adev->id))
1849 * ata_pio_mask_no_iordy - Return the non IORDY mask
1852 * Compute the highest mode possible if we are not using iordy. Return
1853 * -1 if no iordy mode is available.
1856 static u32 ata_pio_mask_no_iordy(const struct ata_device *adev)
1858 /* If we have no drive specific rule, then PIO 2 is non IORDY */
1859 if (adev->id[ATA_ID_FIELD_VALID] & 2) { /* EIDE */
1860 u16 pio = adev->id[ATA_ID_EIDE_PIO];
1861 /* Is the speed faster than the drive allows non IORDY ? */
1863 /* This is cycle times not frequency - watch the logic! */
1864 if (pio > 240) /* PIO2 is 240nS per cycle */
1865 return 3 << ATA_SHIFT_PIO;
1866 return 7 << ATA_SHIFT_PIO;
1869 return 3 << ATA_SHIFT_PIO;
1873 * ata_dev_read_id - Read ID data from the specified device
1874 * @dev: target device
1875 * @p_class: pointer to class of the target device (may be changed)
1876 * @flags: ATA_READID_* flags
1877 * @id: buffer to read IDENTIFY data into
1879 * Read ID data from the specified device. ATA_CMD_ID_ATA is
1880 * performed on ATA devices and ATA_CMD_ID_ATAPI on ATAPI
1881 * devices. This function also issues ATA_CMD_INIT_DEV_PARAMS
1882 * for pre-ATA4 drives.
1884 * FIXME: ATA_CMD_ID_ATA is optional for early drives and right
1885 * now we abort if we hit that case.
1888 * Kernel thread context (may sleep)
1891 * 0 on success, -errno otherwise.
1893 int ata_dev_read_id(struct ata_device *dev, unsigned int *p_class,
1894 unsigned int flags, u16 *id)
1896 struct ata_port *ap = dev->link->ap;
1897 unsigned int class = *p_class;
1898 struct ata_taskfile tf;
1899 unsigned int err_mask = 0;
1901 int may_fallback = 1, tried_spinup = 0;
1904 if (ata_msg_ctl(ap))
1905 ata_dev_printk(dev, KERN_DEBUG, "%s: ENTER\n", __FUNCTION__);
1907 ata_dev_select(ap, dev->devno, 1, 1); /* select device 0/1 */
1909 ata_tf_init(dev, &tf);
1913 tf.command = ATA_CMD_ID_ATA;
1916 tf.command = ATA_CMD_ID_ATAPI;
1920 reason = "unsupported class";
1924 tf.protocol = ATA_PROT_PIO;
1926 /* Some devices choke if TF registers contain garbage. Make
1927 * sure those are properly initialized.
1929 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
1931 /* Device presence detection is unreliable on some
1932 * controllers. Always poll IDENTIFY if available.
1934 tf.flags |= ATA_TFLAG_POLLING;
1936 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_FROM_DEVICE,
1937 id, sizeof(id[0]) * ATA_ID_WORDS, 0);
1939 if (err_mask & AC_ERR_NODEV_HINT) {
1940 DPRINTK("ata%u.%d: NODEV after polling detection\n",
1941 ap->print_id, dev->devno);
1945 /* Device or controller might have reported the wrong
1946 * device class. Give a shot at the other IDENTIFY if
1947 * the current one is aborted by the device.
1950 (err_mask == AC_ERR_DEV) && (tf.feature & ATA_ABORTED)) {
1953 if (class == ATA_DEV_ATA)
1954 class = ATA_DEV_ATAPI;
1956 class = ATA_DEV_ATA;
1961 reason = "I/O error";
1965 /* Falling back doesn't make sense if ID data was read
1966 * successfully at least once.
1970 swap_buf_le16(id, ATA_ID_WORDS);
1974 reason = "device reports invalid type";
1976 if (class == ATA_DEV_ATA) {
1977 if (!ata_id_is_ata(id) && !ata_id_is_cfa(id))
1980 if (ata_id_is_ata(id))
1984 if (!tried_spinup && (id[2] == 0x37c8 || id[2] == 0x738c)) {
1987 * Drive powered-up in standby mode, and requires a specific
1988 * SET_FEATURES spin-up subcommand before it will accept
1989 * anything other than the original IDENTIFY command.
1991 err_mask = ata_dev_set_feature(dev, SETFEATURES_SPINUP, 0);
1992 if (err_mask && id[2] != 0x738c) {
1994 reason = "SPINUP failed";
1998 * If the drive initially returned incomplete IDENTIFY info,
1999 * we now must reissue the IDENTIFY command.
2001 if (id[2] == 0x37c8)
2005 if ((flags & ATA_READID_POSTRESET) && class == ATA_DEV_ATA) {
2007 * The exact sequence expected by certain pre-ATA4 drives is:
2009 * IDENTIFY (optional in early ATA)
2010 * INITIALIZE DEVICE PARAMETERS (later IDE and ATA)
2012 * Some drives were very specific about that exact sequence.
2014 * Note that ATA4 says lba is mandatory so the second check
2015 * shoud never trigger.
2017 if (ata_id_major_version(id) < 4 || !ata_id_has_lba(id)) {
2018 err_mask = ata_dev_init_params(dev, id[3], id[6]);
2021 reason = "INIT_DEV_PARAMS failed";
2025 /* current CHS translation info (id[53-58]) might be
2026 * changed. reread the identify device info.
2028 flags &= ~ATA_READID_POSTRESET;
2038 if (ata_msg_warn(ap))
2039 ata_dev_printk(dev, KERN_WARNING, "failed to IDENTIFY "
2040 "(%s, err_mask=0x%x)\n", reason, err_mask);
2044 static inline u8 ata_dev_knobble(struct ata_device *dev)
2046 struct ata_port *ap = dev->link->ap;
2047 return ((ap->cbl == ATA_CBL_SATA) && (!ata_id_is_sata(dev->id)));
2050 static void ata_dev_config_ncq(struct ata_device *dev,
2051 char *desc, size_t desc_sz)
2053 struct ata_port *ap = dev->link->ap;
2054 int hdepth = 0, ddepth = ata_id_queue_depth(dev->id);
2056 if (!ata_id_has_ncq(dev->id)) {
2060 if (dev->horkage & ATA_HORKAGE_NONCQ) {
2061 snprintf(desc, desc_sz, "NCQ (not used)");
2064 if (ap->flags & ATA_FLAG_NCQ) {
2065 hdepth = min(ap->scsi_host->can_queue, ATA_MAX_QUEUE - 1);
2066 dev->flags |= ATA_DFLAG_NCQ;
2069 if (hdepth >= ddepth)
2070 snprintf(desc, desc_sz, "NCQ (depth %d)", ddepth);
2072 snprintf(desc, desc_sz, "NCQ (depth %d/%d)", hdepth, ddepth);
2076 * ata_dev_configure - Configure the specified ATA/ATAPI device
2077 * @dev: Target device to configure
2079 * Configure @dev according to @dev->id. Generic and low-level
2080 * driver specific fixups are also applied.
2083 * Kernel thread context (may sleep)
2086 * 0 on success, -errno otherwise
2088 int ata_dev_configure(struct ata_device *dev)
2090 struct ata_port *ap = dev->link->ap;
2091 struct ata_eh_context *ehc = &dev->link->eh_context;
2092 int print_info = ehc->i.flags & ATA_EHI_PRINTINFO;
2093 const u16 *id = dev->id;
2094 unsigned int xfer_mask;
2095 char revbuf[7]; /* XYZ-99\0 */
2096 char fwrevbuf[ATA_ID_FW_REV_LEN+1];
2097 char modelbuf[ATA_ID_PROD_LEN+1];
2100 if (!ata_dev_enabled(dev) && ata_msg_info(ap)) {
2101 ata_dev_printk(dev, KERN_INFO, "%s: ENTER/EXIT -- nodev\n",
2106 if (ata_msg_probe(ap))
2107 ata_dev_printk(dev, KERN_DEBUG, "%s: ENTER\n", __FUNCTION__);
2110 dev->horkage |= ata_dev_blacklisted(dev);
2112 /* let ACPI work its magic */
2113 rc = ata_acpi_on_devcfg(dev);
2117 /* massage HPA, do it early as it might change IDENTIFY data */
2118 rc = ata_hpa_resize(dev);
2122 /* print device capabilities */
2123 if (ata_msg_probe(ap))
2124 ata_dev_printk(dev, KERN_DEBUG,
2125 "%s: cfg 49:%04x 82:%04x 83:%04x 84:%04x "
2126 "85:%04x 86:%04x 87:%04x 88:%04x\n",
2128 id[49], id[82], id[83], id[84],
2129 id[85], id[86], id[87], id[88]);
2131 /* initialize to-be-configured parameters */
2132 dev->flags &= ~ATA_DFLAG_CFG_MASK;
2133 dev->max_sectors = 0;
2141 * common ATA, ATAPI feature tests
2144 /* find max transfer mode; for printk only */
2145 xfer_mask = ata_id_xfermask(id);
2147 if (ata_msg_probe(ap))
2150 /* SCSI only uses 4-char revisions, dump full 8 chars from ATA */
2151 ata_id_c_string(dev->id, fwrevbuf, ATA_ID_FW_REV,
2154 ata_id_c_string(dev->id, modelbuf, ATA_ID_PROD,
2157 /* ATA-specific feature tests */
2158 if (dev->class == ATA_DEV_ATA) {
2159 if (ata_id_is_cfa(id)) {
2160 if (id[162] & 1) /* CPRM may make this media unusable */
2161 ata_dev_printk(dev, KERN_WARNING,
2162 "supports DRM functions and may "
2163 "not be fully accessable.\n");
2164 snprintf(revbuf, 7, "CFA");
2166 snprintf(revbuf, 7, "ATA-%d", ata_id_major_version(id));
2167 /* Warn the user if the device has TPM extensions */
2168 if (ata_id_has_tpm(id))
2169 ata_dev_printk(dev, KERN_WARNING,
2170 "supports DRM functions and may "
2171 "not be fully accessable.\n");
2174 dev->n_sectors = ata_id_n_sectors(id);
2176 if (dev->id[59] & 0x100)
2177 dev->multi_count = dev->id[59] & 0xff;
2179 if (ata_id_has_lba(id)) {
2180 const char *lba_desc;
2184 dev->flags |= ATA_DFLAG_LBA;
2185 if (ata_id_has_lba48(id)) {
2186 dev->flags |= ATA_DFLAG_LBA48;
2189 if (dev->n_sectors >= (1UL << 28) &&
2190 ata_id_has_flush_ext(id))
2191 dev->flags |= ATA_DFLAG_FLUSH_EXT;
2195 ata_dev_config_ncq(dev, ncq_desc, sizeof(ncq_desc));
2197 /* print device info to dmesg */
2198 if (ata_msg_drv(ap) && print_info) {
2199 ata_dev_printk(dev, KERN_INFO,
2200 "%s: %s, %s, max %s\n",
2201 revbuf, modelbuf, fwrevbuf,
2202 ata_mode_string(xfer_mask));
2203 ata_dev_printk(dev, KERN_INFO,
2204 "%Lu sectors, multi %u: %s %s\n",
2205 (unsigned long long)dev->n_sectors,
2206 dev->multi_count, lba_desc, ncq_desc);
2211 /* Default translation */
2212 dev->cylinders = id[1];
2214 dev->sectors = id[6];
2216 if (ata_id_current_chs_valid(id)) {
2217 /* Current CHS translation is valid. */
2218 dev->cylinders = id[54];
2219 dev->heads = id[55];
2220 dev->sectors = id[56];
2223 /* print device info to dmesg */
2224 if (ata_msg_drv(ap) && print_info) {
2225 ata_dev_printk(dev, KERN_INFO,
2226 "%s: %s, %s, max %s\n",
2227 revbuf, modelbuf, fwrevbuf,
2228 ata_mode_string(xfer_mask));
2229 ata_dev_printk(dev, KERN_INFO,
2230 "%Lu sectors, multi %u, CHS %u/%u/%u\n",
2231 (unsigned long long)dev->n_sectors,
2232 dev->multi_count, dev->cylinders,
2233 dev->heads, dev->sectors);
2240 /* ATAPI-specific feature tests */
2241 else if (dev->class == ATA_DEV_ATAPI) {
2242 const char *cdb_intr_string = "";
2243 const char *atapi_an_string = "";
2246 rc = atapi_cdb_len(id);
2247 if ((rc < 12) || (rc > ATAPI_CDB_LEN)) {
2248 if (ata_msg_warn(ap))
2249 ata_dev_printk(dev, KERN_WARNING,
2250 "unsupported CDB len\n");
2254 dev->cdb_len = (unsigned int) rc;
2256 /* Enable ATAPI AN if both the host and device have
2257 * the support. If PMP is attached, SNTF is required
2258 * to enable ATAPI AN to discern between PHY status
2259 * changed notifications and ATAPI ANs.
2261 if ((ap->flags & ATA_FLAG_AN) && ata_id_has_atapi_AN(id) &&
2262 (!ap->nr_pmp_links ||
2263 sata_scr_read(&ap->link, SCR_NOTIFICATION, &sntf) == 0)) {
2264 unsigned int err_mask;
2266 /* issue SET feature command to turn this on */
2267 err_mask = ata_dev_set_feature(dev,
2268 SETFEATURES_SATA_ENABLE, SATA_AN);
2270 ata_dev_printk(dev, KERN_ERR,
2271 "failed to enable ATAPI AN "
2272 "(err_mask=0x%x)\n", err_mask);
2274 dev->flags |= ATA_DFLAG_AN;
2275 atapi_an_string = ", ATAPI AN";
2279 if (ata_id_cdb_intr(dev->id)) {
2280 dev->flags |= ATA_DFLAG_CDB_INTR;
2281 cdb_intr_string = ", CDB intr";
2284 /* print device info to dmesg */
2285 if (ata_msg_drv(ap) && print_info)
2286 ata_dev_printk(dev, KERN_INFO,
2287 "ATAPI: %s, %s, max %s%s%s\n",
2289 ata_mode_string(xfer_mask),
2290 cdb_intr_string, atapi_an_string);
2293 /* determine max_sectors */
2294 dev->max_sectors = ATA_MAX_SECTORS;
2295 if (dev->flags & ATA_DFLAG_LBA48)
2296 dev->max_sectors = ATA_MAX_SECTORS_LBA48;
2298 if (!(dev->horkage & ATA_HORKAGE_IPM)) {
2299 if (ata_id_has_hipm(dev->id))
2300 dev->flags |= ATA_DFLAG_HIPM;
2301 if (ata_id_has_dipm(dev->id))
2302 dev->flags |= ATA_DFLAG_DIPM;
2305 if (dev->horkage & ATA_HORKAGE_DIAGNOSTIC) {
2306 /* Let the user know. We don't want to disallow opens for
2307 rescue purposes, or in case the vendor is just a blithering
2310 ata_dev_printk(dev, KERN_WARNING,
2311 "Drive reports diagnostics failure. This may indicate a drive\n");
2312 ata_dev_printk(dev, KERN_WARNING,
2313 "fault or invalid emulation. Contact drive vendor for information.\n");
2317 /* limit bridge transfers to udma5, 200 sectors */
2318 if (ata_dev_knobble(dev)) {
2319 if (ata_msg_drv(ap) && print_info)
2320 ata_dev_printk(dev, KERN_INFO,
2321 "applying bridge limits\n");
2322 dev->udma_mask &= ATA_UDMA5;
2323 dev->max_sectors = ATA_MAX_SECTORS;
2326 if ((dev->class == ATA_DEV_ATAPI) &&
2327 (atapi_command_packet_set(id) == TYPE_TAPE)) {
2328 dev->max_sectors = ATA_MAX_SECTORS_TAPE;
2329 dev->horkage |= ATA_HORKAGE_STUCK_ERR;
2332 if (dev->horkage & ATA_HORKAGE_MAX_SEC_128)
2333 dev->max_sectors = min_t(unsigned int, ATA_MAX_SECTORS_128,
2336 if (ata_dev_blacklisted(dev) & ATA_HORKAGE_IPM) {
2337 dev->horkage |= ATA_HORKAGE_IPM;
2339 /* reset link pm_policy for this port to no pm */
2340 ap->pm_policy = MAX_PERFORMANCE;
2343 if (ap->ops->dev_config)
2344 ap->ops->dev_config(dev);
2346 if (ata_msg_probe(ap))
2347 ata_dev_printk(dev, KERN_DEBUG, "%s: EXIT, drv_stat = 0x%x\n",
2348 __FUNCTION__, ata_chk_status(ap));
2352 if (ata_msg_probe(ap))
2353 ata_dev_printk(dev, KERN_DEBUG,
2354 "%s: EXIT, err\n", __FUNCTION__);
2359 * ata_cable_40wire - return 40 wire cable type
2362 * Helper method for drivers which want to hardwire 40 wire cable
2366 int ata_cable_40wire(struct ata_port *ap)
2368 return ATA_CBL_PATA40;
2372 * ata_cable_80wire - return 80 wire cable type
2375 * Helper method for drivers which want to hardwire 80 wire cable
2379 int ata_cable_80wire(struct ata_port *ap)
2381 return ATA_CBL_PATA80;
2385 * ata_cable_unknown - return unknown PATA cable.
2388 * Helper method for drivers which have no PATA cable detection.
2391 int ata_cable_unknown(struct ata_port *ap)
2393 return ATA_CBL_PATA_UNK;
2397 * ata_cable_sata - return SATA cable type
2400 * Helper method for drivers which have SATA cables
2403 int ata_cable_sata(struct ata_port *ap)
2405 return ATA_CBL_SATA;
2409 * ata_bus_probe - Reset and probe ATA bus
2412 * Master ATA bus probing function. Initiates a hardware-dependent
2413 * bus reset, then attempts to identify any devices found on
2417 * PCI/etc. bus probe sem.
2420 * Zero on success, negative errno otherwise.
2423 int ata_bus_probe(struct ata_port *ap)
2425 unsigned int classes[ATA_MAX_DEVICES];
2426 int tries[ATA_MAX_DEVICES];
2428 struct ata_device *dev;
2432 ata_link_for_each_dev(dev, &ap->link)
2433 tries[dev->devno] = ATA_PROBE_MAX_TRIES;
2436 ata_link_for_each_dev(dev, &ap->link) {
2437 /* If we issue an SRST then an ATA drive (not ATAPI)
2438 * may change configuration and be in PIO0 timing. If
2439 * we do a hard reset (or are coming from power on)
2440 * this is true for ATA or ATAPI. Until we've set a
2441 * suitable controller mode we should not touch the
2442 * bus as we may be talking too fast.
2444 dev->pio_mode = XFER_PIO_0;
2446 /* If the controller has a pio mode setup function
2447 * then use it to set the chipset to rights. Don't
2448 * touch the DMA setup as that will be dealt with when
2449 * configuring devices.
2451 if (ap->ops->set_piomode)
2452 ap->ops->set_piomode(ap, dev);
2455 /* reset and determine device classes */
2456 ap->ops->phy_reset(ap);
2458 ata_link_for_each_dev(dev, &ap->link) {
2459 if (!(ap->flags & ATA_FLAG_DISABLED) &&
2460 dev->class != ATA_DEV_UNKNOWN)
2461 classes[dev->devno] = dev->class;
2463 classes[dev->devno] = ATA_DEV_NONE;
2465 dev->class = ATA_DEV_UNKNOWN;
2470 /* read IDENTIFY page and configure devices. We have to do the identify
2471 specific sequence bass-ackwards so that PDIAG- is released by
2474 ata_link_for_each_dev(dev, &ap->link) {
2475 if (tries[dev->devno])
2476 dev->class = classes[dev->devno];
2478 if (!ata_dev_enabled(dev))
2481 rc = ata_dev_read_id(dev, &dev->class, ATA_READID_POSTRESET,
2487 /* Now ask for the cable type as PDIAG- should have been released */
2488 if (ap->ops->cable_detect)
2489 ap->cbl = ap->ops->cable_detect(ap);
2491 /* We may have SATA bridge glue hiding here irrespective of the
2492 reported cable types and sensed types */
2493 ata_link_for_each_dev(dev, &ap->link) {
2494 if (!ata_dev_enabled(dev))
2496 /* SATA drives indicate we have a bridge. We don't know which
2497 end of the link the bridge is which is a problem */
2498 if (ata_id_is_sata(dev->id))
2499 ap->cbl = ATA_CBL_SATA;
2502 /* After the identify sequence we can now set up the devices. We do
2503 this in the normal order so that the user doesn't get confused */
2505 ata_link_for_each_dev(dev, &ap->link) {
2506 if (!ata_dev_enabled(dev))
2509 ap->link.eh_context.i.flags |= ATA_EHI_PRINTINFO;
2510 rc = ata_dev_configure(dev);
2511 ap->link.eh_context.i.flags &= ~ATA_EHI_PRINTINFO;
2516 /* configure transfer mode */
2517 rc = ata_set_mode(&ap->link, &dev);
2521 ata_link_for_each_dev(dev, &ap->link)
2522 if (ata_dev_enabled(dev))
2525 /* no device present, disable port */
2526 ata_port_disable(ap);
2530 tries[dev->devno]--;
2534 /* eeek, something went very wrong, give up */
2535 tries[dev->devno] = 0;
2539 /* give it just one more chance */
2540 tries[dev->devno] = min(tries[dev->devno], 1);
2542 if (tries[dev->devno] == 1) {
2543 /* This is the last chance, better to slow
2544 * down than lose it.
2546 sata_down_spd_limit(&ap->link);
2547 ata_down_xfermask_limit(dev, ATA_DNXFER_PIO);
2551 if (!tries[dev->devno])
2552 ata_dev_disable(dev);
2558 * ata_port_probe - Mark port as enabled
2559 * @ap: Port for which we indicate enablement
2561 * Modify @ap data structure such that the system
2562 * thinks that the entire port is enabled.
2564 * LOCKING: host lock, or some other form of
2568 void ata_port_probe(struct ata_port *ap)
2570 ap->flags &= ~ATA_FLAG_DISABLED;
2574 * sata_print_link_status - Print SATA link status
2575 * @link: SATA link to printk link status about
2577 * This function prints link speed and status of a SATA link.
2582 void sata_print_link_status(struct ata_link *link)
2584 u32 sstatus, scontrol, tmp;
2586 if (sata_scr_read(link, SCR_STATUS, &sstatus))
2588 sata_scr_read(link, SCR_CONTROL, &scontrol);
2590 if (ata_link_online(link)) {
2591 tmp = (sstatus >> 4) & 0xf;
2592 ata_link_printk(link, KERN_INFO,
2593 "SATA link up %s (SStatus %X SControl %X)\n",
2594 sata_spd_string(tmp), sstatus, scontrol);
2596 ata_link_printk(link, KERN_INFO,
2597 "SATA link down (SStatus %X SControl %X)\n",
2603 * ata_dev_pair - return other device on cable
2606 * Obtain the other device on the same cable, or if none is
2607 * present NULL is returned
2610 struct ata_device *ata_dev_pair(struct ata_device *adev)
2612 struct ata_link *link = adev->link;
2613 struct ata_device *pair = &link->device[1 - adev->devno];
2614 if (!ata_dev_enabled(pair))
2620 * ata_port_disable - Disable port.
2621 * @ap: Port to be disabled.
2623 * Modify @ap data structure such that the system
2624 * thinks that the entire port is disabled, and should
2625 * never attempt to probe or communicate with devices
2628 * LOCKING: host lock, or some other form of
2632 void ata_port_disable(struct ata_port *ap)
2634 ap->link.device[0].class = ATA_DEV_NONE;
2635 ap->link.device[1].class = ATA_DEV_NONE;
2636 ap->flags |= ATA_FLAG_DISABLED;
2640 * sata_down_spd_limit - adjust SATA spd limit downward
2641 * @link: Link to adjust SATA spd limit for
2643 * Adjust SATA spd limit of @link downward. Note that this
2644 * function only adjusts the limit. The change must be applied
2645 * using sata_set_spd().
2648 * Inherited from caller.
2651 * 0 on success, negative errno on failure
2653 int sata_down_spd_limit(struct ata_link *link)
2655 u32 sstatus, spd, mask;
2658 if (!sata_scr_valid(link))
2661 /* If SCR can be read, use it to determine the current SPD.
2662 * If not, use cached value in link->sata_spd.
2664 rc = sata_scr_read(link, SCR_STATUS, &sstatus);
2666 spd = (sstatus >> 4) & 0xf;
2668 spd = link->sata_spd;
2670 mask = link->sata_spd_limit;
2674 /* unconditionally mask off the highest bit */
2675 highbit = fls(mask) - 1;
2676 mask &= ~(1 << highbit);
2678 /* Mask off all speeds higher than or equal to the current
2679 * one. Force 1.5Gbps if current SPD is not available.
2682 mask &= (1 << (spd - 1)) - 1;
2686 /* were we already at the bottom? */
2690 link->sata_spd_limit = mask;
2692 ata_link_printk(link, KERN_WARNING, "limiting SATA link speed to %s\n",
2693 sata_spd_string(fls(mask)));
2698 static int __sata_set_spd_needed(struct ata_link *link, u32 *scontrol)
2700 struct ata_link *host_link = &link->ap->link;
2701 u32 limit, target, spd;
2703 limit = link->sata_spd_limit;
2705 /* Don't configure downstream link faster than upstream link.
2706 * It doesn't speed up anything and some PMPs choke on such
2709 if (!ata_is_host_link(link) && host_link->sata_spd)
2710 limit &= (1 << host_link->sata_spd) - 1;
2712 if (limit == UINT_MAX)
2715 target = fls(limit);
2717 spd = (*scontrol >> 4) & 0xf;
2718 *scontrol = (*scontrol & ~0xf0) | ((target & 0xf) << 4);
2720 return spd != target;
2724 * sata_set_spd_needed - is SATA spd configuration needed
2725 * @link: Link in question
2727 * Test whether the spd limit in SControl matches
2728 * @link->sata_spd_limit. This function is used to determine
2729 * whether hardreset is necessary to apply SATA spd
2733 * Inherited from caller.
2736 * 1 if SATA spd configuration is needed, 0 otherwise.
2738 int sata_set_spd_needed(struct ata_link *link)
2742 if (sata_scr_read(link, SCR_CONTROL, &scontrol))
2745 return __sata_set_spd_needed(link, &scontrol);
2749 * sata_set_spd - set SATA spd according to spd limit
2750 * @link: Link to set SATA spd for
2752 * Set SATA spd of @link according to sata_spd_limit.
2755 * Inherited from caller.
2758 * 0 if spd doesn't need to be changed, 1 if spd has been
2759 * changed. Negative errno if SCR registers are inaccessible.
2761 int sata_set_spd(struct ata_link *link)
2766 if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
2769 if (!__sata_set_spd_needed(link, &scontrol))
2772 if ((rc = sata_scr_write(link, SCR_CONTROL, scontrol)))
2779 * This mode timing computation functionality is ported over from
2780 * drivers/ide/ide-timing.h and was originally written by Vojtech Pavlik
2783 * PIO 0-4, MWDMA 0-2 and UDMA 0-6 timings (in nanoseconds).
2784 * These were taken from ATA/ATAPI-6 standard, rev 0a, except
2785 * for UDMA6, which is currently supported only by Maxtor drives.
2787 * For PIO 5/6 MWDMA 3/4 see the CFA specification 3.0.
2790 static const struct ata_timing ata_timing[] = {
2792 { XFER_UDMA_6, 0, 0, 0, 0, 0, 0, 0, 15 },
2793 { XFER_UDMA_5, 0, 0, 0, 0, 0, 0, 0, 20 },
2794 { XFER_UDMA_4, 0, 0, 0, 0, 0, 0, 0, 30 },
2795 { XFER_UDMA_3, 0, 0, 0, 0, 0, 0, 0, 45 },
2797 { XFER_MW_DMA_4, 25, 0, 0, 0, 55, 20, 80, 0 },
2798 { XFER_MW_DMA_3, 25, 0, 0, 0, 65, 25, 100, 0 },
2799 { XFER_UDMA_2, 0, 0, 0, 0, 0, 0, 0, 60 },
2800 { XFER_UDMA_1, 0, 0, 0, 0, 0, 0, 0, 80 },
2801 { XFER_UDMA_0, 0, 0, 0, 0, 0, 0, 0, 120 },
2803 /* { XFER_UDMA_SLOW, 0, 0, 0, 0, 0, 0, 0, 150 }, */
2805 { XFER_MW_DMA_2, 25, 0, 0, 0, 70, 25, 120, 0 },
2806 { XFER_MW_DMA_1, 45, 0, 0, 0, 80, 50, 150, 0 },
2807 { XFER_MW_DMA_0, 60, 0, 0, 0, 215, 215, 480, 0 },
2809 { XFER_SW_DMA_2, 60, 0, 0, 0, 120, 120, 240, 0 },
2810 { XFER_SW_DMA_1, 90, 0, 0, 0, 240, 240, 480, 0 },
2811 { XFER_SW_DMA_0, 120, 0, 0, 0, 480, 480, 960, 0 },
2813 { XFER_PIO_6, 10, 55, 20, 80, 55, 20, 80, 0 },
2814 { XFER_PIO_5, 15, 65, 25, 100, 65, 25, 100, 0 },
2815 { XFER_PIO_4, 25, 70, 25, 120, 70, 25, 120, 0 },
2816 { XFER_PIO_3, 30, 80, 70, 180, 80, 70, 180, 0 },
2818 { XFER_PIO_2, 30, 290, 40, 330, 100, 90, 240, 0 },
2819 { XFER_PIO_1, 50, 290, 93, 383, 125, 100, 383, 0 },
2820 { XFER_PIO_0, 70, 290, 240, 600, 165, 150, 600, 0 },
2822 /* { XFER_PIO_SLOW, 120, 290, 240, 960, 290, 240, 960, 0 }, */
2827 #define ENOUGH(v, unit) (((v)-1)/(unit)+1)
2828 #define EZ(v, unit) ((v)?ENOUGH(v, unit):0)
2830 static void ata_timing_quantize(const struct ata_timing *t, struct ata_timing *q, int T, int UT)
2832 q->setup = EZ(t->setup * 1000, T);
2833 q->act8b = EZ(t->act8b * 1000, T);
2834 q->rec8b = EZ(t->rec8b * 1000, T);
2835 q->cyc8b = EZ(t->cyc8b * 1000, T);
2836 q->active = EZ(t->active * 1000, T);
2837 q->recover = EZ(t->recover * 1000, T);
2838 q->cycle = EZ(t->cycle * 1000, T);
2839 q->udma = EZ(t->udma * 1000, UT);
2842 void ata_timing_merge(const struct ata_timing *a, const struct ata_timing *b,
2843 struct ata_timing *m, unsigned int what)
2845 if (what & ATA_TIMING_SETUP ) m->setup = max(a->setup, b->setup);
2846 if (what & ATA_TIMING_ACT8B ) m->act8b = max(a->act8b, b->act8b);
2847 if (what & ATA_TIMING_REC8B ) m->rec8b = max(a->rec8b, b->rec8b);
2848 if (what & ATA_TIMING_CYC8B ) m->cyc8b = max(a->cyc8b, b->cyc8b);
2849 if (what & ATA_TIMING_ACTIVE ) m->active = max(a->active, b->active);
2850 if (what & ATA_TIMING_RECOVER) m->recover = max(a->recover, b->recover);
2851 if (what & ATA_TIMING_CYCLE ) m->cycle = max(a->cycle, b->cycle);
2852 if (what & ATA_TIMING_UDMA ) m->udma = max(a->udma, b->udma);
2855 const struct ata_timing *ata_timing_find_mode(u8 xfer_mode)
2857 const struct ata_timing *t;
2859 for (t = ata_timing; t->mode != xfer_mode; t++)
2860 if (t->mode == 0xFF)
2865 int ata_timing_compute(struct ata_device *adev, unsigned short speed,
2866 struct ata_timing *t, int T, int UT)
2868 const struct ata_timing *s;
2869 struct ata_timing p;
2875 if (!(s = ata_timing_find_mode(speed)))
2878 memcpy(t, s, sizeof(*s));
2881 * If the drive is an EIDE drive, it can tell us it needs extended
2882 * PIO/MW_DMA cycle timing.
2885 if (adev->id[ATA_ID_FIELD_VALID] & 2) { /* EIDE drive */
2886 memset(&p, 0, sizeof(p));
2887 if (speed >= XFER_PIO_0 && speed <= XFER_SW_DMA_0) {
2888 if (speed <= XFER_PIO_2) p.cycle = p.cyc8b = adev->id[ATA_ID_EIDE_PIO];
2889 else p.cycle = p.cyc8b = adev->id[ATA_ID_EIDE_PIO_IORDY];
2890 } else if (speed >= XFER_MW_DMA_0 && speed <= XFER_MW_DMA_2) {
2891 p.cycle = adev->id[ATA_ID_EIDE_DMA_MIN];
2893 ata_timing_merge(&p, t, t, ATA_TIMING_CYCLE | ATA_TIMING_CYC8B);
2897 * Convert the timing to bus clock counts.
2900 ata_timing_quantize(t, t, T, UT);
2903 * Even in DMA/UDMA modes we still use PIO access for IDENTIFY,
2904 * S.M.A.R.T * and some other commands. We have to ensure that the
2905 * DMA cycle timing is slower/equal than the fastest PIO timing.
2908 if (speed > XFER_PIO_6) {
2909 ata_timing_compute(adev, adev->pio_mode, &p, T, UT);
2910 ata_timing_merge(&p, t, t, ATA_TIMING_ALL);
2914 * Lengthen active & recovery time so that cycle time is correct.
2917 if (t->act8b + t->rec8b < t->cyc8b) {
2918 t->act8b += (t->cyc8b - (t->act8b + t->rec8b)) / 2;
2919 t->rec8b = t->cyc8b - t->act8b;
2922 if (t->active + t->recover < t->cycle) {
2923 t->active += (t->cycle - (t->active + t->recover)) / 2;
2924 t->recover = t->cycle - t->active;
2927 /* In a few cases quantisation may produce enough errors to
2928 leave t->cycle too low for the sum of active and recovery
2929 if so we must correct this */
2930 if (t->active + t->recover > t->cycle)
2931 t->cycle = t->active + t->recover;
2937 * ata_down_xfermask_limit - adjust dev xfer masks downward
2938 * @dev: Device to adjust xfer masks
2939 * @sel: ATA_DNXFER_* selector
2941 * Adjust xfer masks of @dev downward. Note that this function
2942 * does not apply the change. Invoking ata_set_mode() afterwards
2943 * will apply the limit.
2946 * Inherited from caller.
2949 * 0 on success, negative errno on failure
2951 int ata_down_xfermask_limit(struct ata_device *dev, unsigned int sel)
2954 unsigned int orig_mask, xfer_mask;
2955 unsigned int pio_mask, mwdma_mask, udma_mask;
2958 quiet = !!(sel & ATA_DNXFER_QUIET);
2959 sel &= ~ATA_DNXFER_QUIET;
2961 xfer_mask = orig_mask = ata_pack_xfermask(dev->pio_mask,
2964 ata_unpack_xfermask(xfer_mask, &pio_mask, &mwdma_mask, &udma_mask);
2967 case ATA_DNXFER_PIO:
2968 highbit = fls(pio_mask) - 1;
2969 pio_mask &= ~(1 << highbit);
2972 case ATA_DNXFER_DMA:
2974 highbit = fls(udma_mask) - 1;
2975 udma_mask &= ~(1 << highbit);
2978 } else if (mwdma_mask) {
2979 highbit = fls(mwdma_mask) - 1;
2980 mwdma_mask &= ~(1 << highbit);
2986 case ATA_DNXFER_40C:
2987 udma_mask &= ATA_UDMA_MASK_40C;
2990 case ATA_DNXFER_FORCE_PIO0:
2992 case ATA_DNXFER_FORCE_PIO:
3001 xfer_mask &= ata_pack_xfermask(pio_mask, mwdma_mask, udma_mask);
3003 if (!(xfer_mask & ATA_MASK_PIO) || xfer_mask == orig_mask)
3007 if (xfer_mask & (ATA_MASK_MWDMA | ATA_MASK_UDMA))
3008 snprintf(buf, sizeof(buf), "%s:%s",
3009 ata_mode_string(xfer_mask),
3010 ata_mode_string(xfer_mask & ATA_MASK_PIO));
3012 snprintf(buf, sizeof(buf), "%s",
3013 ata_mode_string(xfer_mask));
3015 ata_dev_printk(dev, KERN_WARNING,
3016 "limiting speed to %s\n", buf);
3019 ata_unpack_xfermask(xfer_mask, &dev->pio_mask, &dev->mwdma_mask,
3025 static int ata_dev_set_mode(struct ata_device *dev)
3027 struct ata_eh_context *ehc = &dev->link->eh_context;
3028 unsigned int err_mask;
3031 dev->flags &= ~ATA_DFLAG_PIO;
3032 if (dev->xfer_shift == ATA_SHIFT_PIO)
3033 dev->flags |= ATA_DFLAG_PIO;
3035 err_mask = ata_dev_set_xfermode(dev);
3037 /* Old CFA may refuse this command, which is just fine */
3038 if (dev->xfer_shift == ATA_SHIFT_PIO && ata_id_is_cfa(dev->id))
3039 err_mask &= ~AC_ERR_DEV;
3041 /* Some very old devices and some bad newer ones fail any kind of
3042 SET_XFERMODE request but support PIO0-2 timings and no IORDY */
3043 if (dev->xfer_shift == ATA_SHIFT_PIO && !ata_id_has_iordy(dev->id) &&
3044 dev->pio_mode <= XFER_PIO_2)
3045 err_mask &= ~AC_ERR_DEV;
3047 /* Early MWDMA devices do DMA but don't allow DMA mode setting.
3048 Don't fail an MWDMA0 set IFF the device indicates it is in MWDMA0 */
3049 if (dev->xfer_shift == ATA_SHIFT_MWDMA &&
3050 dev->dma_mode == XFER_MW_DMA_0 &&
3051 (dev->id[63] >> 8) & 1)
3052 err_mask &= ~AC_ERR_DEV;
3055 ata_dev_printk(dev, KERN_ERR, "failed to set xfermode "
3056 "(err_mask=0x%x)\n", err_mask);
3060 ehc->i.flags |= ATA_EHI_POST_SETMODE;
3061 rc = ata_dev_revalidate(dev, ATA_DEV_UNKNOWN, 0);
3062 ehc->i.flags &= ~ATA_EHI_POST_SETMODE;
3066 DPRINTK("xfer_shift=%u, xfer_mode=0x%x\n",
3067 dev->xfer_shift, (int)dev->xfer_mode);
3069 ata_dev_printk(dev, KERN_INFO, "configured for %s\n",
3070 ata_mode_string(ata_xfer_mode2mask(dev->xfer_mode)));
3075 * ata_do_set_mode - Program timings and issue SET FEATURES - XFER
3076 * @link: link on which timings will be programmed
3077 * @r_failed_dev: out paramter for failed device
3079 * Standard implementation of the function used to tune and set
3080 * ATA device disk transfer mode (PIO3, UDMA6, etc.). If
3081 * ata_dev_set_mode() fails, pointer to the failing device is
3082 * returned in @r_failed_dev.
3085 * PCI/etc. bus probe sem.
3088 * 0 on success, negative errno otherwise
3091 int ata_do_set_mode(struct ata_link *link, struct ata_device **r_failed_dev)
3093 struct ata_port *ap = link->ap;
3094 struct ata_device *dev;
3095 int rc = 0, used_dma = 0, found = 0;
3097 /* step 1: calculate xfer_mask */
3098 ata_link_for_each_dev(dev, link) {
3099 unsigned int pio_mask, dma_mask;
3100 unsigned int mode_mask;
3102 if (!ata_dev_enabled(dev))
3105 mode_mask = ATA_DMA_MASK_ATA;
3106 if (dev->class == ATA_DEV_ATAPI)
3107 mode_mask = ATA_DMA_MASK_ATAPI;
3108 else if (ata_id_is_cfa(dev->id))
3109 mode_mask = ATA_DMA_MASK_CFA;
3111 ata_dev_xfermask(dev);
3113 pio_mask = ata_pack_xfermask(dev->pio_mask, 0, 0);
3114 dma_mask = ata_pack_xfermask(0, dev->mwdma_mask, dev->udma_mask);
3116 if (libata_dma_mask & mode_mask)
3117 dma_mask = ata_pack_xfermask(0, dev->mwdma_mask, dev->udma_mask);
3121 dev->pio_mode = ata_xfer_mask2mode(pio_mask);
3122 dev->dma_mode = ata_xfer_mask2mode(dma_mask);
3131 /* step 2: always set host PIO timings */
3132 ata_link_for_each_dev(dev, link) {
3133 if (!ata_dev_enabled(dev))
3136 if (!dev->pio_mode) {
3137 ata_dev_printk(dev, KERN_WARNING, "no PIO support\n");
3142 dev->xfer_mode = dev->pio_mode;
3143 dev->xfer_shift = ATA_SHIFT_PIO;
3144 if (ap->ops->set_piomode)
3145 ap->ops->set_piomode(ap, dev);
3148 /* step 3: set host DMA timings */
3149 ata_link_for_each_dev(dev, link) {
3150 if (!ata_dev_enabled(dev) || !dev->dma_mode)
3153 dev->xfer_mode = dev->dma_mode;
3154 dev->xfer_shift = ata_xfer_mode2shift(dev->dma_mode);
3155 if (ap->ops->set_dmamode)
3156 ap->ops->set_dmamode(ap, dev);
3159 /* step 4: update devices' xfer mode */
3160 ata_link_for_each_dev(dev, link) {
3161 /* don't update suspended devices' xfer mode */
3162 if (!ata_dev_enabled(dev))
3165 rc = ata_dev_set_mode(dev);
3170 /* Record simplex status. If we selected DMA then the other
3171 * host channels are not permitted to do so.
3173 if (used_dma && (ap->host->flags & ATA_HOST_SIMPLEX))
3174 ap->host->simplex_claimed = ap;
3178 *r_failed_dev = dev;
3183 * ata_tf_to_host - issue ATA taskfile to host controller
3184 * @ap: port to which command is being issued
3185 * @tf: ATA taskfile register set
3187 * Issues ATA taskfile register set to ATA host controller,
3188 * with proper synchronization with interrupt handler and
3192 * spin_lock_irqsave(host lock)
3195 static inline void ata_tf_to_host(struct ata_port *ap,
3196 const struct ata_taskfile *tf)
3198 ap->ops->tf_load(ap, tf);
3199 ap->ops->exec_command(ap, tf);
3203 * ata_busy_sleep - sleep until BSY clears, or timeout
3204 * @ap: port containing status register to be polled
3205 * @tmout_pat: impatience timeout
3206 * @tmout: overall timeout
3208 * Sleep until ATA Status register bit BSY clears,
3209 * or a timeout occurs.
3212 * Kernel thread context (may sleep).
3215 * 0 on success, -errno otherwise.
3217 int ata_busy_sleep(struct ata_port *ap,
3218 unsigned long tmout_pat, unsigned long tmout)
3220 unsigned long timer_start, timeout;
3223 status = ata_busy_wait(ap, ATA_BUSY, 300);
3224 timer_start = jiffies;
3225 timeout = timer_start + tmout_pat;
3226 while (status != 0xff && (status & ATA_BUSY) &&
3227 time_before(jiffies, timeout)) {
3229 status = ata_busy_wait(ap, ATA_BUSY, 3);
3232 if (status != 0xff && (status & ATA_BUSY))
3233 ata_port_printk(ap, KERN_WARNING,
3234 "port is slow to respond, please be patient "
3235 "(Status 0x%x)\n", status);
3237 timeout = timer_start + tmout;
3238 while (status != 0xff && (status & ATA_BUSY) &&
3239 time_before(jiffies, timeout)) {
3241 status = ata_chk_status(ap);
3247 if (status & ATA_BUSY) {
3248 ata_port_printk(ap, KERN_ERR, "port failed to respond "
3249 "(%lu secs, Status 0x%x)\n",
3250 tmout / HZ, status);
3258 * ata_wait_after_reset - wait before checking status after reset
3259 * @ap: port containing status register to be polled
3260 * @deadline: deadline jiffies for the operation
3262 * After reset, we need to pause a while before reading status.
3263 * Also, certain combination of controller and device report 0xff
3264 * for some duration (e.g. until SATA PHY is up and running)
3265 * which is interpreted as empty port in ATA world. This
3266 * function also waits for such devices to get out of 0xff
3270 * Kernel thread context (may sleep).
3272 void ata_wait_after_reset(struct ata_port *ap, unsigned long deadline)
3274 unsigned long until = jiffies + ATA_TMOUT_FF_WAIT;
3276 if (time_before(until, deadline))
3279 /* Spec mandates ">= 2ms" before checking status. We wait
3280 * 150ms, because that was the magic delay used for ATAPI
3281 * devices in Hale Landis's ATADRVR, for the period of time
3282 * between when the ATA command register is written, and then
3283 * status is checked. Because waiting for "a while" before
3284 * checking status is fine, post SRST, we perform this magic
3285 * delay here as well.
3287 * Old drivers/ide uses the 2mS rule and then waits for ready.
3291 /* Wait for 0xff to clear. Some SATA devices take a long time
3292 * to clear 0xff after reset. For example, HHD424020F7SV00
3293 * iVDR needs >= 800ms while. Quantum GoVault needs even more
3296 * Note that some PATA controllers (pata_ali) explode if
3297 * status register is read more than once when there's no
3300 if (ap->flags & ATA_FLAG_SATA) {
3302 u8 status = ata_chk_status(ap);
3304 if (status != 0xff || time_after(jiffies, deadline))
3313 * ata_wait_ready - sleep until BSY clears, or timeout
3314 * @ap: port containing status register to be polled
3315 * @deadline: deadline jiffies for the operation
3317 * Sleep until ATA Status register bit BSY clears, or timeout
3321 * Kernel thread context (may sleep).
3324 * 0 on success, -errno otherwise.
3326 int ata_wait_ready(struct ata_port *ap, unsigned long deadline)
3328 unsigned long start = jiffies;
3332 u8 status = ata_chk_status(ap);
3333 unsigned long now = jiffies;
3335 if (!(status & ATA_BUSY))
3337 if (!ata_link_online(&ap->link) && status == 0xff)
3339 if (time_after(now, deadline))
3342 if (!warned && time_after(now, start + 5 * HZ) &&
3343 (deadline - now > 3 * HZ)) {
3344 ata_port_printk(ap, KERN_WARNING,
3345 "port is slow to respond, please be patient "
3346 "(Status 0x%x)\n", status);
3354 static int ata_bus_post_reset(struct ata_port *ap, unsigned int devmask,
3355 unsigned long deadline)
3357 struct ata_ioports *ioaddr = &ap->ioaddr;
3358 unsigned int dev0 = devmask & (1 << 0);
3359 unsigned int dev1 = devmask & (1 << 1);
3362 /* if device 0 was found in ata_devchk, wait for its
3366 rc = ata_wait_ready(ap, deadline);
3374 /* if device 1 was found in ata_devchk, wait for register
3375 * access briefly, then wait for BSY to clear.
3380 ap->ops->dev_select(ap, 1);
3382 /* Wait for register access. Some ATAPI devices fail
3383 * to set nsect/lbal after reset, so don't waste too
3384 * much time on it. We're gonna wait for !BSY anyway.
3386 for (i = 0; i < 2; i++) {
3389 nsect = ioread8(ioaddr->nsect_addr);
3390 lbal = ioread8(ioaddr->lbal_addr);
3391 if ((nsect == 1) && (lbal == 1))
3393 msleep(50); /* give drive a breather */
3396 rc = ata_wait_ready(ap, deadline);
3404 /* is all this really necessary? */
3405 ap->ops->dev_select(ap, 0);
3407 ap->ops->dev_select(ap, 1);
3409 ap->ops->dev_select(ap, 0);
3414 static int ata_bus_softreset(struct ata_port *ap, unsigned int devmask,
3415 unsigned long deadline)
3417 struct ata_ioports *ioaddr = &ap->ioaddr;
3419 DPRINTK("ata%u: bus reset via SRST\n", ap->print_id);
3421 /* software reset. causes dev0 to be selected */
3422 iowrite8(ap->ctl, ioaddr->ctl_addr);
3423 udelay(20); /* FIXME: flush */
3424 iowrite8(ap->ctl | ATA_SRST, ioaddr->ctl_addr);
3425 udelay(20); /* FIXME: flush */
3426 iowrite8(ap->ctl, ioaddr->ctl_addr);
3428 /* wait a while before checking status */
3429 ata_wait_after_reset(ap, deadline);
3431 /* Before we perform post reset processing we want to see if
3432 * the bus shows 0xFF because the odd clown forgets the D7
3433 * pulldown resistor.
3435 if (ata_chk_status(ap) == 0xFF)
3438 return ata_bus_post_reset(ap, devmask, deadline);
3442 * ata_bus_reset - reset host port and associated ATA channel
3443 * @ap: port to reset
3445 * This is typically the first time we actually start issuing
3446 * commands to the ATA channel. We wait for BSY to clear, then
3447 * issue EXECUTE DEVICE DIAGNOSTIC command, polling for its
3448 * result. Determine what devices, if any, are on the channel
3449 * by looking at the device 0/1 error register. Look at the signature
3450 * stored in each device's taskfile registers, to determine if
3451 * the device is ATA or ATAPI.
3454 * PCI/etc. bus probe sem.
3455 * Obtains host lock.
3458 * Sets ATA_FLAG_DISABLED if bus reset fails.
3461 void ata_bus_reset(struct ata_port *ap)
3463 struct ata_device *device = ap->link.device;
3464 struct ata_ioports *ioaddr = &ap->ioaddr;
3465 unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS;
3467 unsigned int dev0, dev1 = 0, devmask = 0;
3470 DPRINTK("ENTER, host %u, port %u\n", ap->print_id, ap->port_no);
3472 /* determine if device 0/1 are present */
3473 if (ap->flags & ATA_FLAG_SATA_RESET)
3476 dev0 = ata_devchk(ap, 0);
3478 dev1 = ata_devchk(ap, 1);
3482 devmask |= (1 << 0);
3484 devmask |= (1 << 1);
3486 /* select device 0 again */
3487 ap->ops->dev_select(ap, 0);
3489 /* issue bus reset */
3490 if (ap->flags & ATA_FLAG_SRST) {
3491 rc = ata_bus_softreset(ap, devmask, jiffies + 40 * HZ);
3492 if (rc && rc != -ENODEV)
3497 * determine by signature whether we have ATA or ATAPI devices
3499 device[0].class = ata_dev_try_classify(&device[0], dev0, &err);
3500 if ((slave_possible) && (err != 0x81))
3501 device[1].class = ata_dev_try_classify(&device[1], dev1, &err);
3503 /* is double-select really necessary? */
3504 if (device[1].class != ATA_DEV_NONE)
3505 ap->ops->dev_select(ap, 1);
3506 if (device[0].class != ATA_DEV_NONE)
3507 ap->ops->dev_select(ap, 0);
3509 /* if no devices were detected, disable this port */
3510 if ((device[0].class == ATA_DEV_NONE) &&
3511 (device[1].class == ATA_DEV_NONE))
3514 if (ap->flags & (ATA_FLAG_SATA_RESET | ATA_FLAG_SRST)) {
3515 /* set up device control for ATA_FLAG_SATA_RESET */
3516 iowrite8(ap->ctl, ioaddr->ctl_addr);
3523 ata_port_printk(ap, KERN_ERR, "disabling port\n");
3524 ata_port_disable(ap);
3530 * sata_link_debounce - debounce SATA phy status
3531 * @link: ATA link to debounce SATA phy status for
3532 * @params: timing parameters { interval, duratinon, timeout } in msec
3533 * @deadline: deadline jiffies for the operation
3535 * Make sure SStatus of @link reaches stable state, determined by
3536 * holding the same value where DET is not 1 for @duration polled
3537 * every @interval, before @timeout. Timeout constraints the
3538 * beginning of the stable state. Because DET gets stuck at 1 on
3539 * some controllers after hot unplugging, this functions waits
3540 * until timeout then returns 0 if DET is stable at 1.
3542 * @timeout is further limited by @deadline. The sooner of the
3546 * Kernel thread context (may sleep)
3549 * 0 on success, -errno on failure.
3551 int sata_link_debounce(struct ata_link *link, const unsigned long *params,
3552 unsigned long deadline)
3554 unsigned long interval_msec = params[0];
3555 unsigned long duration = msecs_to_jiffies(params[1]);
3556 unsigned long last_jiffies, t;
3560 t = jiffies + msecs_to_jiffies(params[2]);
3561 if (time_before(t, deadline))
3564 if ((rc = sata_scr_read(link, SCR_STATUS, &cur)))
3569 last_jiffies = jiffies;
3572 msleep(interval_msec);
3573 if ((rc = sata_scr_read(link, SCR_STATUS, &cur)))
3579 if (cur == 1 && time_before(jiffies, deadline))
3581 if (time_after(jiffies, last_jiffies + duration))
3586 /* unstable, start over */
3588 last_jiffies = jiffies;
3590 /* Check deadline. If debouncing failed, return
3591 * -EPIPE to tell upper layer to lower link speed.
3593 if (time_after(jiffies, deadline))
3599 * sata_link_resume - resume SATA link
3600 * @link: ATA link to resume SATA
3601 * @params: timing parameters { interval, duratinon, timeout } in msec
3602 * @deadline: deadline jiffies for the operation
3604 * Resume SATA phy @link and debounce it.
3607 * Kernel thread context (may sleep)
3610 * 0 on success, -errno on failure.
3612 int sata_link_resume(struct ata_link *link, const unsigned long *params,
3613 unsigned long deadline)
3618 if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
3621 scontrol = (scontrol & 0x0f0) | 0x300;
3623 if ((rc = sata_scr_write(link, SCR_CONTROL, scontrol)))
3626 /* Some PHYs react badly if SStatus is pounded immediately
3627 * after resuming. Delay 200ms before debouncing.
3631 return sata_link_debounce(link, params, deadline);
3635 * ata_std_prereset - prepare for reset
3636 * @link: ATA link to be reset
3637 * @deadline: deadline jiffies for the operation
3639 * @link is about to be reset. Initialize it. Failure from
3640 * prereset makes libata abort whole reset sequence and give up
3641 * that port, so prereset should be best-effort. It does its
3642 * best to prepare for reset sequence but if things go wrong, it
3643 * should just whine, not fail.
3646 * Kernel thread context (may sleep)
3649 * 0 on success, -errno otherwise.
3651 int ata_std_prereset(struct ata_link *link, unsigned long deadline)
3653 struct ata_port *ap = link->ap;
3654 struct ata_eh_context *ehc = &link->eh_context;
3655 const unsigned long *timing = sata_ehc_deb_timing(ehc);
3658 /* handle link resume */
3659 if ((ehc->i.flags & ATA_EHI_RESUME_LINK) &&
3660 (link->flags & ATA_LFLAG_HRST_TO_RESUME))
3661 ehc->i.action |= ATA_EH_HARDRESET;
3663 /* Some PMPs don't work with only SRST, force hardreset if PMP
3666 if (ap->flags & ATA_FLAG_PMP)
3667 ehc->i.action |= ATA_EH_HARDRESET;
3669 /* if we're about to do hardreset, nothing more to do */
3670 if (ehc->i.action & ATA_EH_HARDRESET)
3673 /* if SATA, resume link */
3674 if (ap->flags & ATA_FLAG_SATA) {
3675 rc = sata_link_resume(link, timing, deadline);
3676 /* whine about phy resume failure but proceed */
3677 if (rc && rc != -EOPNOTSUPP)
3678 ata_link_printk(link, KERN_WARNING, "failed to resume "
3679 "link for reset (errno=%d)\n", rc);
3682 /* Wait for !BSY if the controller can wait for the first D2H
3683 * Reg FIS and we don't know that no device is attached.
3685 if (!(link->flags & ATA_LFLAG_SKIP_D2H_BSY) && !ata_link_offline(link)) {
3686 rc = ata_wait_ready(ap, deadline);
3687 if (rc && rc != -ENODEV) {
3688 ata_link_printk(link, KERN_WARNING, "device not ready "
3689 "(errno=%d), forcing hardreset\n", rc);
3690 ehc->i.action |= ATA_EH_HARDRESET;
3698 * ata_std_softreset - reset host port via ATA SRST
3699 * @link: ATA link to reset
3700 * @classes: resulting classes of attached devices
3701 * @deadline: deadline jiffies for the operation
3703 * Reset host port using ATA SRST.
3706 * Kernel thread context (may sleep)
3709 * 0 on success, -errno otherwise.
3711 int ata_std_softreset(struct ata_link *link, unsigned int *classes,
3712 unsigned long deadline)
3714 struct ata_port *ap = link->ap;
3715 unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS;
3716 unsigned int devmask = 0;
3722 if (ata_link_offline(link)) {
3723 classes[0] = ATA_DEV_NONE;
3727 /* determine if device 0/1 are present */
3728 if (ata_devchk(ap, 0))
3729 devmask |= (1 << 0);
3730 if (slave_possible && ata_devchk(ap, 1))
3731 devmask |= (1 << 1);
3733 /* select device 0 again */
3734 ap->ops->dev_select(ap, 0);
3736 /* issue bus reset */
3737 DPRINTK("about to softreset, devmask=%x\n", devmask);
3738 rc = ata_bus_softreset(ap, devmask, deadline);
3739 /* if link is occupied, -ENODEV too is an error */
3740 if (rc && (rc != -ENODEV || sata_scr_valid(link))) {
3741 ata_link_printk(link, KERN_ERR, "SRST failed (errno=%d)\n", rc);
3745 /* determine by signature whether we have ATA or ATAPI devices */
3746 classes[0] = ata_dev_try_classify(&link->device[0],
3747 devmask & (1 << 0), &err);
3748 if (slave_possible && err != 0x81)
3749 classes[1] = ata_dev_try_classify(&link->device[1],
3750 devmask & (1 << 1), &err);
3753 DPRINTK("EXIT, classes[0]=%u [1]=%u\n", classes[0], classes[1]);
3758 * sata_link_hardreset - reset link via SATA phy reset
3759 * @link: link to reset
3760 * @timing: timing parameters { interval, duratinon, timeout } in msec
3761 * @deadline: deadline jiffies for the operation
3763 * SATA phy-reset @link using DET bits of SControl register.
3766 * Kernel thread context (may sleep)
3769 * 0 on success, -errno otherwise.
3771 int sata_link_hardreset(struct ata_link *link, const unsigned long *timing,
3772 unsigned long deadline)
3779 if (sata_set_spd_needed(link)) {
3780 /* SATA spec says nothing about how to reconfigure
3781 * spd. To be on the safe side, turn off phy during
3782 * reconfiguration. This works for at least ICH7 AHCI
3785 if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
3788 scontrol = (scontrol & 0x0f0) | 0x304;
3790 if ((rc = sata_scr_write(link, SCR_CONTROL, scontrol)))
3796 /* issue phy wake/reset */
3797 if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
3800 scontrol = (scontrol & 0x0f0) | 0x301;
3802 if ((rc = sata_scr_write_flush(link, SCR_CONTROL, scontrol)))
3805 /* Couldn't find anything in SATA I/II specs, but AHCI-1.1
3806 * 10.4.2 says at least 1 ms.
3810 /* bring link back */
3811 rc = sata_link_resume(link, timing, deadline);
3813 DPRINTK("EXIT, rc=%d\n", rc);
3818 * sata_std_hardreset - reset host port via SATA phy reset
3819 * @link: link to reset
3820 * @class: resulting class of attached device
3821 * @deadline: deadline jiffies for the operation
3823 * SATA phy-reset host port using DET bits of SControl register,
3824 * wait for !BSY and classify the attached device.
3827 * Kernel thread context (may sleep)
3830 * 0 on success, -errno otherwise.
3832 int sata_std_hardreset(struct ata_link *link, unsigned int *class,
3833 unsigned long deadline)
3835 struct ata_port *ap = link->ap;
3836 const unsigned long *timing = sata_ehc_deb_timing(&link->eh_context);
3842 rc = sata_link_hardreset(link, timing, deadline);
3844 ata_link_printk(link, KERN_ERR,
3845 "COMRESET failed (errno=%d)\n", rc);
3849 /* TODO: phy layer with polling, timeouts, etc. */
3850 if (ata_link_offline(link)) {
3851 *class = ATA_DEV_NONE;
3852 DPRINTK("EXIT, link offline\n");
3856 /* wait a while before checking status */
3857 ata_wait_after_reset(ap, deadline);
3859 /* If PMP is supported, we have to do follow-up SRST. Note
3860 * that some PMPs don't send D2H Reg FIS after hardreset at
3861 * all if the first port is empty. Wait for it just for a
3862 * second and request follow-up SRST.
3864 if (ap->flags & ATA_FLAG_PMP) {
3865 ata_wait_ready(ap, jiffies + HZ);
3869 rc = ata_wait_ready(ap, deadline);
3870 /* link occupied, -ENODEV too is an error */
3872 ata_link_printk(link, KERN_ERR,
3873 "COMRESET failed (errno=%d)\n", rc);
3877 ap->ops->dev_select(ap, 0); /* probably unnecessary */
3879 *class = ata_dev_try_classify(link->device, 1, NULL);
3881 DPRINTK("EXIT, class=%u\n", *class);
3886 * ata_std_postreset - standard postreset callback
3887 * @link: the target ata_link
3888 * @classes: classes of attached devices
3890 * This function is invoked after a successful reset. Note that
3891 * the device might have been reset more than once using
3892 * different reset methods before postreset is invoked.
3895 * Kernel thread context (may sleep)
3897 void ata_std_postreset(struct ata_link *link, unsigned int *classes)
3899 struct ata_port *ap = link->ap;
3904 /* print link status */
3905 sata_print_link_status(link);
3908 if (sata_scr_read(link, SCR_ERROR, &serror) == 0)
3909 sata_scr_write(link, SCR_ERROR, serror);
3910 link->eh_info.serror = 0;
3912 /* is double-select really necessary? */
3913 if (classes[0] != ATA_DEV_NONE)
3914 ap->ops->dev_select(ap, 1);
3915 if (classes[1] != ATA_DEV_NONE)
3916 ap->ops->dev_select(ap, 0);
3918 /* bail out if no device is present */
3919 if (classes[0] == ATA_DEV_NONE && classes[1] == ATA_DEV_NONE) {
3920 DPRINTK("EXIT, no device\n");
3924 /* set up device control */
3925 if (ap->ioaddr.ctl_addr)
3926 iowrite8(ap->ctl, ap->ioaddr.ctl_addr);
3932 * ata_dev_same_device - Determine whether new ID matches configured device
3933 * @dev: device to compare against
3934 * @new_class: class of the new device
3935 * @new_id: IDENTIFY page of the new device
3937 * Compare @new_class and @new_id against @dev and determine
3938 * whether @dev is the device indicated by @new_class and
3945 * 1 if @dev matches @new_class and @new_id, 0 otherwise.
3947 static int ata_dev_same_device(struct ata_device *dev, unsigned int new_class,
3950 const u16 *old_id = dev->id;
3951 unsigned char model[2][ATA_ID_PROD_LEN + 1];
3952 unsigned char serial[2][ATA_ID_SERNO_LEN + 1];
3954 if (dev->class != new_class) {
3955 ata_dev_printk(dev, KERN_INFO, "class mismatch %d != %d\n",
3956 dev->class, new_class);
3960 ata_id_c_string(old_id, model[0], ATA_ID_PROD, sizeof(model[0]));
3961 ata_id_c_string(new_id, model[1], ATA_ID_PROD, sizeof(model[1]));
3962 ata_id_c_string(old_id, serial[0], ATA_ID_SERNO, sizeof(serial[0]));
3963 ata_id_c_string(new_id, serial[1], ATA_ID_SERNO, sizeof(serial[1]));
3965 if (strcmp(model[0], model[1])) {
3966 ata_dev_printk(dev, KERN_INFO, "model number mismatch "
3967 "'%s' != '%s'\n", model[0], model[1]);
3971 if (strcmp(serial[0], serial[1])) {
3972 ata_dev_printk(dev, KERN_INFO, "serial number mismatch "
3973 "'%s' != '%s'\n", serial[0], serial[1]);
3981 * ata_dev_reread_id - Re-read IDENTIFY data
3982 * @dev: target ATA device
3983 * @readid_flags: read ID flags
3985 * Re-read IDENTIFY page and make sure @dev is still attached to
3989 * Kernel thread context (may sleep)
3992 * 0 on success, negative errno otherwise
3994 int ata_dev_reread_id(struct ata_device *dev, unsigned int readid_flags)
3996 unsigned int class = dev->class;
3997 u16 *id = (void *)dev->link->ap->sector_buf;
4001 rc = ata_dev_read_id(dev, &class, readid_flags, id);
4005 /* is the device still there? */
4006 if (!ata_dev_same_device(dev, class, id))
4009 memcpy(dev->id, id, sizeof(id[0]) * ATA_ID_WORDS);
4014 * ata_dev_revalidate - Revalidate ATA device
4015 * @dev: device to revalidate
4016 * @new_class: new class code
4017 * @readid_flags: read ID flags
4019 * Re-read IDENTIFY page, make sure @dev is still attached to the
4020 * port and reconfigure it according to the new IDENTIFY page.
4023 * Kernel thread context (may sleep)
4026 * 0 on success, negative errno otherwise
4028 int ata_dev_revalidate(struct ata_device *dev, unsigned int new_class,
4029 unsigned int readid_flags)
4031 u64 n_sectors = dev->n_sectors;
4034 if (!ata_dev_enabled(dev))
4037 /* fail early if !ATA && !ATAPI to avoid issuing [P]IDENTIFY to PMP */
4038 if (ata_class_enabled(new_class) &&
4039 new_class != ATA_DEV_ATA && new_class != ATA_DEV_ATAPI) {
4040 ata_dev_printk(dev, KERN_INFO, "class mismatch %u != %u\n",
4041 dev->class, new_class);
4047 rc = ata_dev_reread_id(dev, readid_flags);
4051 /* configure device according to the new ID */
4052 rc = ata_dev_configure(dev);
4056 /* verify n_sectors hasn't changed */
4057 if (dev->class == ATA_DEV_ATA && n_sectors &&
4058 dev->n_sectors != n_sectors) {
4059 ata_dev_printk(dev, KERN_INFO, "n_sectors mismatch "
4061 (unsigned long long)n_sectors,
4062 (unsigned long long)dev->n_sectors);
4064 /* restore original n_sectors */
4065 dev->n_sectors = n_sectors;
4074 ata_dev_printk(dev, KERN_ERR, "revalidation failed (errno=%d)\n", rc);
4078 struct ata_blacklist_entry {
4079 const char *model_num;
4080 const char *model_rev;
4081 unsigned long horkage;
4084 static const struct ata_blacklist_entry ata_device_blacklist [] = {
4085 /* Devices with DMA related problems under Linux */
4086 { "WDC AC11000H", NULL, ATA_HORKAGE_NODMA },
4087 { "WDC AC22100H", NULL, ATA_HORKAGE_NODMA },
4088 { "WDC AC32500H", NULL, ATA_HORKAGE_NODMA },
4089 { "WDC AC33100H", NULL, ATA_HORKAGE_NODMA },
4090 { "WDC AC31600H", NULL, ATA_HORKAGE_NODMA },
4091 { "WDC AC32100H", "24.09P07", ATA_HORKAGE_NODMA },
4092 { "WDC AC23200L", "21.10N21", ATA_HORKAGE_NODMA },
4093 { "Compaq CRD-8241B", NULL, ATA_HORKAGE_NODMA },
4094 { "CRD-8400B", NULL, ATA_HORKAGE_NODMA },
4095 { "CRD-8480B", NULL, ATA_HORKAGE_NODMA },
4096 { "CRD-8482B", NULL, ATA_HORKAGE_NODMA },
4097 { "CRD-84", NULL, ATA_HORKAGE_NODMA },
4098 { "SanDisk SDP3B", NULL, ATA_HORKAGE_NODMA },
4099 { "SanDisk SDP3B-64", NULL, ATA_HORKAGE_NODMA },
4100 { "SANYO CD-ROM CRD", NULL, ATA_HORKAGE_NODMA },
4101 { "HITACHI CDR-8", NULL, ATA_HORKAGE_NODMA },
4102 { "HITACHI CDR-8335", NULL, ATA_HORKAGE_NODMA },
4103 { "HITACHI CDR-8435", NULL, ATA_HORKAGE_NODMA },
4104 { "Toshiba CD-ROM XM-6202B", NULL, ATA_HORKAGE_NODMA },
4105 { "TOSHIBA CD-ROM XM-1702BC", NULL, ATA_HORKAGE_NODMA },
4106 { "CD-532E-A", NULL, ATA_HORKAGE_NODMA },
4107 { "E-IDE CD-ROM CR-840",NULL, ATA_HORKAGE_NODMA },
4108 { "CD-ROM Drive/F5A", NULL, ATA_HORKAGE_NODMA },
4109 { "WPI CDD-820", NULL, ATA_HORKAGE_NODMA },
4110 { "SAMSUNG CD-ROM SC-148C", NULL, ATA_HORKAGE_NODMA },
4111 { "SAMSUNG CD-ROM SC", NULL, ATA_HORKAGE_NODMA },
4112 { "ATAPI CD-ROM DRIVE 40X MAXIMUM",NULL,ATA_HORKAGE_NODMA },
4113 { "_NEC DV5800A", NULL, ATA_HORKAGE_NODMA },
4114 { "SAMSUNG CD-ROM SN-124", "N001", ATA_HORKAGE_NODMA },
4115 { "Seagate STT20000A", NULL, ATA_HORKAGE_NODMA },
4116 /* Odd clown on sil3726/4726 PMPs */
4117 { "Config Disk", NULL, ATA_HORKAGE_NODMA |
4118 ATA_HORKAGE_SKIP_PM },
4120 /* Weird ATAPI devices */
4121 { "TORiSAN DVD-ROM DRD-N216", NULL, ATA_HORKAGE_MAX_SEC_128 },
4123 /* Devices we expect to fail diagnostics */
4125 /* Devices where NCQ should be avoided */
4127 { "WDC WD740ADFD-00", NULL, ATA_HORKAGE_NONCQ },
4128 { "WDC WD740ADFD-00NLR1", NULL, ATA_HORKAGE_NONCQ, },
4129 /* http://thread.gmane.org/gmane.linux.ide/14907 */
4130 { "FUJITSU MHT2060BH", NULL, ATA_HORKAGE_NONCQ },
4132 { "Maxtor *", "BANC*", ATA_HORKAGE_NONCQ },
4133 { "Maxtor 7V300F0", "VA111630", ATA_HORKAGE_NONCQ },
4134 { "HITACHI HDS7250SASUN500G*", NULL, ATA_HORKAGE_NONCQ },
4135 { "HITACHI HDS7225SBSUN250G*", NULL, ATA_HORKAGE_NONCQ },
4136 { "ST380817AS", "3.42", ATA_HORKAGE_NONCQ },
4137 { "ST3160023AS", "3.42", ATA_HORKAGE_NONCQ },
4139 /* Blacklist entries taken from Silicon Image 3124/3132
4140 Windows driver .inf file - also several Linux problem reports */
4141 { "HTS541060G9SA00", "MB3OC60D", ATA_HORKAGE_NONCQ, },
4142 { "HTS541080G9SA00", "MB4OC60D", ATA_HORKAGE_NONCQ, },
4143 { "HTS541010G9SA00", "MBZOC60D", ATA_HORKAGE_NONCQ, },
4145 /* devices which puke on READ_NATIVE_MAX */
4146 { "HDS724040KLSA80", "KFAOA20N", ATA_HORKAGE_BROKEN_HPA, },
4147 { "WDC WD3200JD-00KLB0", "WD-WCAMR1130137", ATA_HORKAGE_BROKEN_HPA },
4148 { "WDC WD2500JD-00HBB0", "WD-WMAL71490727", ATA_HORKAGE_BROKEN_HPA },
4149 { "MAXTOR 6L080L4", "A93.0500", ATA_HORKAGE_BROKEN_HPA },
4151 /* Devices which report 1 sector over size HPA */
4152 { "ST340823A", NULL, ATA_HORKAGE_HPA_SIZE, },
4153 { "ST320413A", NULL, ATA_HORKAGE_HPA_SIZE, },
4155 /* Devices which get the IVB wrong */
4156 { "QUANTUM FIREBALLlct10 05", "A03.0900", ATA_HORKAGE_IVB, },
4157 { "TSSTcorp CDDVDW SH-S202J", "SB00", ATA_HORKAGE_IVB, },
4158 { "TSSTcorp CDDVDW SH-S202J", "SB01", ATA_HORKAGE_IVB, },
4159 { "TSSTcorp CDDVDW SH-S202N", "SB00", ATA_HORKAGE_IVB, },
4160 { "TSSTcorp CDDVDW SH-S202N", "SB01", ATA_HORKAGE_IVB, },
4166 static int strn_pattern_cmp(const char *patt, const char *name, int wildchar)
4172 * check for trailing wildcard: *\0
4174 p = strchr(patt, wildchar);
4175 if (p && ((*(p + 1)) == 0))
4186 return strncmp(patt, name, len);
4189 static unsigned long ata_dev_blacklisted(const struct ata_device *dev)
4191 unsigned char model_num[ATA_ID_PROD_LEN + 1];
4192 unsigned char model_rev[ATA_ID_FW_REV_LEN + 1];
4193 const struct ata_blacklist_entry *ad = ata_device_blacklist;
4195 ata_id_c_string(dev->id, model_num, ATA_ID_PROD, sizeof(model_num));
4196 ata_id_c_string(dev->id, model_rev, ATA_ID_FW_REV, sizeof(model_rev));
4198 while (ad->model_num) {
4199 if (!strn_pattern_cmp(ad->model_num, model_num, '*')) {
4200 if (ad->model_rev == NULL)
4202 if (!strn_pattern_cmp(ad->model_rev, model_rev, '*'))
4210 static int ata_dma_blacklisted(const struct ata_device *dev)
4212 /* We don't support polling DMA.
4213 * DMA blacklist those ATAPI devices with CDB-intr (and use PIO)
4214 * if the LLDD handles only interrupts in the HSM_ST_LAST state.
4216 if ((dev->link->ap->flags & ATA_FLAG_PIO_POLLING) &&
4217 (dev->flags & ATA_DFLAG_CDB_INTR))
4219 return (dev->horkage & ATA_HORKAGE_NODMA) ? 1 : 0;
4223 * ata_is_40wire - check drive side detection
4226 * Perform drive side detection decoding, allowing for device vendors
4227 * who can't follow the documentation.
4230 static int ata_is_40wire(struct ata_device *dev)
4232 if (dev->horkage & ATA_HORKAGE_IVB)
4233 return ata_drive_40wire_relaxed(dev->id);
4234 return ata_drive_40wire(dev->id);
4238 * ata_dev_xfermask - Compute supported xfermask of the given device
4239 * @dev: Device to compute xfermask for
4241 * Compute supported xfermask of @dev and store it in
4242 * dev->*_mask. This function is responsible for applying all
4243 * known limits including host controller limits, device
4249 static void ata_dev_xfermask(struct ata_device *dev)
4251 struct ata_link *link = dev->link;
4252 struct ata_port *ap = link->ap;
4253 struct ata_host *host = ap->host;
4254 unsigned long xfer_mask;
4256 /* controller modes available */
4257 xfer_mask = ata_pack_xfermask(ap->pio_mask,
4258 ap->mwdma_mask, ap->udma_mask);
4260 /* drive modes available */
4261 xfer_mask &= ata_pack_xfermask(dev->pio_mask,
4262 dev->mwdma_mask, dev->udma_mask);
4263 xfer_mask &= ata_id_xfermask(dev->id);
4266 * CFA Advanced TrueIDE timings are not allowed on a shared
4269 if (ata_dev_pair(dev)) {
4270 /* No PIO5 or PIO6 */
4271 xfer_mask &= ~(0x03 << (ATA_SHIFT_PIO + 5));
4272 /* No MWDMA3 or MWDMA 4 */
4273 xfer_mask &= ~(0x03 << (ATA_SHIFT_MWDMA + 3));
4276 if (ata_dma_blacklisted(dev)) {
4277 xfer_mask &= ~(ATA_MASK_MWDMA | ATA_MASK_UDMA);
4278 ata_dev_printk(dev, KERN_WARNING,
4279 "device is on DMA blacklist, disabling DMA\n");
4282 if ((host->flags & ATA_HOST_SIMPLEX) &&
4283 host->simplex_claimed && host->simplex_claimed != ap) {
4284 xfer_mask &= ~(ATA_MASK_MWDMA | ATA_MASK_UDMA);
4285 ata_dev_printk(dev, KERN_WARNING, "simplex DMA is claimed by "
4286 "other device, disabling DMA\n");
4289 if (ap->flags & ATA_FLAG_NO_IORDY)
4290 xfer_mask &= ata_pio_mask_no_iordy(dev);
4292 if (ap->ops->mode_filter)
4293 xfer_mask = ap->ops->mode_filter(dev, xfer_mask);
4295 /* Apply cable rule here. Don't apply it early because when
4296 * we handle hot plug the cable type can itself change.
4297 * Check this last so that we know if the transfer rate was
4298 * solely limited by the cable.
4299 * Unknown or 80 wire cables reported host side are checked
4300 * drive side as well. Cases where we know a 40wire cable
4301 * is used safely for 80 are not checked here.
4303 if (xfer_mask & (0xF8 << ATA_SHIFT_UDMA))
4304 /* UDMA/44 or higher would be available */
4305 if ((ap->cbl == ATA_CBL_PATA40) ||
4306 (ata_is_40wire(dev) &&
4307 (ap->cbl == ATA_CBL_PATA_UNK ||
4308 ap->cbl == ATA_CBL_PATA80))) {
4309 ata_dev_printk(dev, KERN_WARNING,
4310 "limited to UDMA/33 due to 40-wire cable\n");
4311 xfer_mask &= ~(0xF8 << ATA_SHIFT_UDMA);
4314 ata_unpack_xfermask(xfer_mask, &dev->pio_mask,
4315 &dev->mwdma_mask, &dev->udma_mask);
4319 * ata_dev_set_xfermode - Issue SET FEATURES - XFER MODE command
4320 * @dev: Device to which command will be sent
4322 * Issue SET FEATURES - XFER MODE command to device @dev
4326 * PCI/etc. bus probe sem.
4329 * 0 on success, AC_ERR_* mask otherwise.
4332 static unsigned int ata_dev_set_xfermode(struct ata_device *dev)
4334 struct ata_taskfile tf;
4335 unsigned int err_mask;
4337 /* set up set-features taskfile */
4338 DPRINTK("set features - xfer mode\n");
4340 /* Some controllers and ATAPI devices show flaky interrupt
4341 * behavior after setting xfer mode. Use polling instead.
4343 ata_tf_init(dev, &tf);
4344 tf.command = ATA_CMD_SET_FEATURES;
4345 tf.feature = SETFEATURES_XFER;
4346 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE | ATA_TFLAG_POLLING;
4347 tf.protocol = ATA_PROT_NODATA;
4348 /* If we are using IORDY we must send the mode setting command */
4349 if (ata_pio_need_iordy(dev))
4350 tf.nsect = dev->xfer_mode;
4351 /* If the device has IORDY and the controller does not - turn it off */
4352 else if (ata_id_has_iordy(dev->id))
4354 else /* In the ancient relic department - skip all of this */
4357 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
4359 DPRINTK("EXIT, err_mask=%x\n", err_mask);
4363 * ata_dev_set_feature - Issue SET FEATURES - SATA FEATURES
4364 * @dev: Device to which command will be sent
4365 * @enable: Whether to enable or disable the feature
4366 * @feature: The sector count represents the feature to set
4368 * Issue SET FEATURES - SATA FEATURES command to device @dev
4369 * on port @ap with sector count
4372 * PCI/etc. bus probe sem.
4375 * 0 on success, AC_ERR_* mask otherwise.
4377 static unsigned int ata_dev_set_feature(struct ata_device *dev, u8 enable,
4380 struct ata_taskfile tf;
4381 unsigned int err_mask;
4383 /* set up set-features taskfile */
4384 DPRINTK("set features - SATA features\n");
4386 ata_tf_init(dev, &tf);
4387 tf.command = ATA_CMD_SET_FEATURES;
4388 tf.feature = enable;
4389 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
4390 tf.protocol = ATA_PROT_NODATA;
4393 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
4395 DPRINTK("EXIT, err_mask=%x\n", err_mask);
4400 * ata_dev_init_params - Issue INIT DEV PARAMS command
4401 * @dev: Device to which command will be sent
4402 * @heads: Number of heads (taskfile parameter)
4403 * @sectors: Number of sectors (taskfile parameter)
4406 * Kernel thread context (may sleep)
4409 * 0 on success, AC_ERR_* mask otherwise.
4411 static unsigned int ata_dev_init_params(struct ata_device *dev,
4412 u16 heads, u16 sectors)
4414 struct ata_taskfile tf;
4415 unsigned int err_mask;
4417 /* Number of sectors per track 1-255. Number of heads 1-16 */
4418 if (sectors < 1 || sectors > 255 || heads < 1 || heads > 16)
4419 return AC_ERR_INVALID;
4421 /* set up init dev params taskfile */
4422 DPRINTK("init dev params \n");
4424 ata_tf_init(dev, &tf);
4425 tf.command = ATA_CMD_INIT_DEV_PARAMS;
4426 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
4427 tf.protocol = ATA_PROT_NODATA;
4429 tf.device |= (heads - 1) & 0x0f; /* max head = num. of heads - 1 */
4431 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
4432 /* A clean abort indicates an original or just out of spec drive
4433 and we should continue as we issue the setup based on the
4434 drive reported working geometry */
4435 if (err_mask == AC_ERR_DEV && (tf.feature & ATA_ABORTED))
4438 DPRINTK("EXIT, err_mask=%x\n", err_mask);
4443 * ata_sg_clean - Unmap DMA memory associated with command
4444 * @qc: Command containing DMA memory to be released
4446 * Unmap all mapped DMA memory associated with this command.
4449 * spin_lock_irqsave(host lock)
4451 void ata_sg_clean(struct ata_queued_cmd *qc)
4453 struct ata_port *ap = qc->ap;
4454 struct scatterlist *sg = qc->__sg;
4455 int dir = qc->dma_dir;
4456 void *pad_buf = NULL;
4458 WARN_ON(!(qc->flags & ATA_QCFLAG_DMAMAP));
4459 WARN_ON(sg == NULL);
4461 if (qc->flags & ATA_QCFLAG_SINGLE)
4462 WARN_ON(qc->n_elem > 1);
4464 VPRINTK("unmapping %u sg elements\n", qc->n_elem);
4466 /* if we padded the buffer out to 32-bit bound, and data
4467 * xfer direction is from-device, we must copy from the
4468 * pad buffer back into the supplied buffer
4470 if (qc->pad_len && !(qc->tf.flags & ATA_TFLAG_WRITE))
4471 pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
4473 if (qc->flags & ATA_QCFLAG_SG) {
4475 dma_unmap_sg(ap->dev, sg, qc->n_elem, dir);
4476 /* restore last sg */
4477 sg_last(sg, qc->orig_n_elem)->length += qc->pad_len;
4479 struct scatterlist *psg = &qc->pad_sgent;
4480 void *addr = kmap_atomic(sg_page(psg), KM_IRQ0);
4481 memcpy(addr + psg->offset, pad_buf, qc->pad_len);
4482 kunmap_atomic(addr, KM_IRQ0);
4486 dma_unmap_single(ap->dev,
4487 sg_dma_address(&sg[0]), sg_dma_len(&sg[0]),
4490 sg->length += qc->pad_len;
4492 memcpy(qc->buf_virt + sg->length - qc->pad_len,
4493 pad_buf, qc->pad_len);
4496 qc->flags &= ~ATA_QCFLAG_DMAMAP;
4501 * ata_fill_sg - Fill PCI IDE PRD table
4502 * @qc: Metadata associated with taskfile to be transferred
4504 * Fill PCI IDE PRD (scatter-gather) table with segments
4505 * associated with the current disk command.
4508 * spin_lock_irqsave(host lock)
4511 static void ata_fill_sg(struct ata_queued_cmd *qc)
4513 struct ata_port *ap = qc->ap;
4514 struct scatterlist *sg;
4517 WARN_ON(qc->__sg == NULL);
4518 WARN_ON(qc->n_elem == 0 && qc->pad_len == 0);
4521 ata_for_each_sg(sg, qc) {
4525 /* determine if physical DMA addr spans 64K boundary.
4526 * Note h/w doesn't support 64-bit, so we unconditionally
4527 * truncate dma_addr_t to u32.
4529 addr = (u32) sg_dma_address(sg);
4530 sg_len = sg_dma_len(sg);
4533 offset = addr & 0xffff;
4535 if ((offset + sg_len) > 0x10000)
4536 len = 0x10000 - offset;
4538 ap->prd[idx].addr = cpu_to_le32(addr);
4539 ap->prd[idx].flags_len = cpu_to_le32(len & 0xffff);
4540 VPRINTK("PRD[%u] = (0x%X, 0x%X)\n", idx, addr, len);
4549 ap->prd[idx - 1].flags_len |= cpu_to_le32(ATA_PRD_EOT);
4553 * ata_fill_sg_dumb - Fill PCI IDE PRD table
4554 * @qc: Metadata associated with taskfile to be transferred
4556 * Fill PCI IDE PRD (scatter-gather) table with segments
4557 * associated with the current disk command. Perform the fill
4558 * so that we avoid writing any length 64K records for
4559 * controllers that don't follow the spec.
4562 * spin_lock_irqsave(host lock)
4565 static void ata_fill_sg_dumb(struct ata_queued_cmd *qc)
4567 struct ata_port *ap = qc->ap;
4568 struct scatterlist *sg;
4571 WARN_ON(qc->__sg == NULL);
4572 WARN_ON(qc->n_elem == 0 && qc->pad_len == 0);
4575 ata_for_each_sg(sg, qc) {
4577 u32 sg_len, len, blen;
4579 /* determine if physical DMA addr spans 64K boundary.
4580 * Note h/w doesn't support 64-bit, so we unconditionally
4581 * truncate dma_addr_t to u32.
4583 addr = (u32) sg_dma_address(sg);
4584 sg_len = sg_dma_len(sg);
4587 offset = addr & 0xffff;
4589 if ((offset + sg_len) > 0x10000)
4590 len = 0x10000 - offset;
4592 blen = len & 0xffff;
4593 ap->prd[idx].addr = cpu_to_le32(addr);
4595 /* Some PATA chipsets like the CS5530 can't
4596 cope with 0x0000 meaning 64K as the spec says */
4597 ap->prd[idx].flags_len = cpu_to_le32(0x8000);
4599 ap->prd[++idx].addr = cpu_to_le32(addr + 0x8000);
4601 ap->prd[idx].flags_len = cpu_to_le32(blen);
4602 VPRINTK("PRD[%u] = (0x%X, 0x%X)\n", idx, addr, len);
4611 ap->prd[idx - 1].flags_len |= cpu_to_le32(ATA_PRD_EOT);
4615 * ata_check_atapi_dma - Check whether ATAPI DMA can be supported
4616 * @qc: Metadata associated with taskfile to check
4618 * Allow low-level driver to filter ATA PACKET commands, returning
4619 * a status indicating whether or not it is OK to use DMA for the
4620 * supplied PACKET command.
4623 * spin_lock_irqsave(host lock)
4625 * RETURNS: 0 when ATAPI DMA can be used
4628 int ata_check_atapi_dma(struct ata_queued_cmd *qc)
4630 struct ata_port *ap = qc->ap;
4632 /* Don't allow DMA if it isn't multiple of 16 bytes. Quite a
4633 * few ATAPI devices choke on such DMA requests.
4635 if (unlikely(qc->nbytes & 15))
4638 if (ap->ops->check_atapi_dma)
4639 return ap->ops->check_atapi_dma(qc);
4645 * atapi_qc_may_overflow - Check whether data transfer may overflow
4646 * @qc: ATA command in question
4648 * ATAPI commands which transfer variable length data to host
4649 * might overflow due to application error or hardare bug. This
4650 * function checks whether overflow should be drained and ignored
4657 * 1 if @qc may overflow; otherwise, 0.
4659 static int atapi_qc_may_overflow(struct ata_queued_cmd *qc)
4661 if (qc->tf.protocol != ATA_PROT_ATAPI &&
4662 qc->tf.protocol != ATA_PROT_ATAPI_DMA)
4665 if (qc->tf.flags & ATA_TFLAG_WRITE)
4668 switch (qc->cdb[0]) {
4674 case GPCMD_READ_CD_MSF:
4682 * ata_std_qc_defer - Check whether a qc needs to be deferred
4683 * @qc: ATA command in question
4685 * Non-NCQ commands cannot run with any other command, NCQ or
4686 * not. As upper layer only knows the queue depth, we are
4687 * responsible for maintaining exclusion. This function checks
4688 * whether a new command @qc can be issued.
4691 * spin_lock_irqsave(host lock)
4694 * ATA_DEFER_* if deferring is needed, 0 otherwise.
4696 int ata_std_qc_defer(struct ata_queued_cmd *qc)
4698 struct ata_link *link = qc->dev->link;
4700 if (qc->tf.protocol == ATA_PROT_NCQ) {
4701 if (!ata_tag_valid(link->active_tag))
4704 if (!ata_tag_valid(link->active_tag) && !link->sactive)
4708 return ATA_DEFER_LINK;
4712 * ata_qc_prep - Prepare taskfile for submission
4713 * @qc: Metadata associated with taskfile to be prepared
4715 * Prepare ATA taskfile for submission.
4718 * spin_lock_irqsave(host lock)
4720 void ata_qc_prep(struct ata_queued_cmd *qc)
4722 if (!(qc->flags & ATA_QCFLAG_DMAMAP))
4729 * ata_dumb_qc_prep - Prepare taskfile for submission
4730 * @qc: Metadata associated with taskfile to be prepared
4732 * Prepare ATA taskfile for submission.
4735 * spin_lock_irqsave(host lock)
4737 void ata_dumb_qc_prep(struct ata_queued_cmd *qc)
4739 if (!(qc->flags & ATA_QCFLAG_DMAMAP))
4742 ata_fill_sg_dumb(qc);
4745 void ata_noop_qc_prep(struct ata_queued_cmd *qc) { }
4748 * ata_sg_init_one - Associate command with memory buffer
4749 * @qc: Command to be associated
4750 * @buf: Memory buffer
4751 * @buflen: Length of memory buffer, in bytes.
4753 * Initialize the data-related elements of queued_cmd @qc
4754 * to point to a single memory buffer, @buf of byte length @buflen.
4757 * spin_lock_irqsave(host lock)
4760 void ata_sg_init_one(struct ata_queued_cmd *qc, void *buf, unsigned int buflen)
4762 qc->flags |= ATA_QCFLAG_SINGLE;
4764 qc->__sg = &qc->sgent;
4766 qc->orig_n_elem = 1;
4768 qc->nbytes = buflen;
4769 qc->cursg = qc->__sg;
4771 sg_init_one(&qc->sgent, buf, buflen);
4775 * ata_sg_init - Associate command with scatter-gather table.
4776 * @qc: Command to be associated
4777 * @sg: Scatter-gather table.
4778 * @n_elem: Number of elements in s/g table.
4780 * Initialize the data-related elements of queued_cmd @qc
4781 * to point to a scatter-gather table @sg, containing @n_elem
4785 * spin_lock_irqsave(host lock)
4788 void ata_sg_init(struct ata_queued_cmd *qc, struct scatterlist *sg,
4789 unsigned int n_elem)
4791 qc->flags |= ATA_QCFLAG_SG;
4793 qc->n_elem = n_elem;
4794 qc->orig_n_elem = n_elem;
4795 qc->cursg = qc->__sg;
4799 * ata_sg_setup_one - DMA-map the memory buffer associated with a command.
4800 * @qc: Command with memory buffer to be mapped.
4802 * DMA-map the memory buffer associated with queued_cmd @qc.
4805 * spin_lock_irqsave(host lock)
4808 * Zero on success, negative on error.
4811 static int ata_sg_setup_one(struct ata_queued_cmd *qc)
4813 struct ata_port *ap = qc->ap;
4814 int dir = qc->dma_dir;
4815 struct scatterlist *sg = qc->__sg;
4816 dma_addr_t dma_address;
4819 /* we must lengthen transfers to end on a 32-bit boundary */
4820 qc->pad_len = sg->length & 3;
4822 void *pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
4823 struct scatterlist *psg = &qc->pad_sgent;
4825 WARN_ON(qc->dev->class != ATA_DEV_ATAPI);
4827 memset(pad_buf, 0, ATA_DMA_PAD_SZ);
4829 if (qc->tf.flags & ATA_TFLAG_WRITE)
4830 memcpy(pad_buf, qc->buf_virt + sg->length - qc->pad_len,
4833 sg_dma_address(psg) = ap->pad_dma + (qc->tag * ATA_DMA_PAD_SZ);
4834 sg_dma_len(psg) = ATA_DMA_PAD_SZ;
4836 sg->length -= qc->pad_len;
4837 if (sg->length == 0)
4840 DPRINTK("padding done, sg->length=%u pad_len=%u\n",
4841 sg->length, qc->pad_len);
4849 dma_address = dma_map_single(ap->dev, qc->buf_virt,
4851 if (dma_mapping_error(dma_address)) {
4853 sg->length += qc->pad_len;
4857 sg_dma_address(sg) = dma_address;
4858 sg_dma_len(sg) = sg->length;
4861 DPRINTK("mapped buffer of %d bytes for %s\n", sg_dma_len(sg),
4862 qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
4868 * ata_sg_setup - DMA-map the scatter-gather table associated with a command.
4869 * @qc: Command with scatter-gather table to be mapped.
4871 * DMA-map the scatter-gather table associated with queued_cmd @qc.
4874 * spin_lock_irqsave(host lock)
4877 * Zero on success, negative on error.
4881 static int ata_sg_setup(struct ata_queued_cmd *qc)
4883 struct ata_port *ap = qc->ap;
4884 struct scatterlist *sg = qc->__sg;
4885 struct scatterlist *lsg = sg_last(qc->__sg, qc->n_elem);
4886 int n_elem, pre_n_elem, dir, trim_sg = 0;
4888 VPRINTK("ENTER, ata%u\n", ap->print_id);
4889 WARN_ON(!(qc->flags & ATA_QCFLAG_SG));
4891 /* we must lengthen transfers to end on a 32-bit boundary */
4892 qc->pad_len = lsg->length & 3;
4894 void *pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
4895 struct scatterlist *psg = &qc->pad_sgent;
4896 unsigned int offset;
4898 WARN_ON(qc->dev->class != ATA_DEV_ATAPI);
4900 memset(pad_buf, 0, ATA_DMA_PAD_SZ);
4903 * psg->page/offset are used to copy to-be-written
4904 * data in this function or read data in ata_sg_clean.
4906 offset = lsg->offset + lsg->length - qc->pad_len;
4907 sg_init_table(psg, 1);
4908 sg_set_page(psg, nth_page(sg_page(lsg), offset >> PAGE_SHIFT),
4909 qc->pad_len, offset_in_page(offset));
4911 if (qc->tf.flags & ATA_TFLAG_WRITE) {
4912 void *addr = kmap_atomic(sg_page(psg), KM_IRQ0);
4913 memcpy(pad_buf, addr + psg->offset, qc->pad_len);
4914 kunmap_atomic(addr, KM_IRQ0);
4917 sg_dma_address(psg) = ap->pad_dma + (qc->tag * ATA_DMA_PAD_SZ);
4918 sg_dma_len(psg) = ATA_DMA_PAD_SZ;
4920 lsg->length -= qc->pad_len;
4921 if (lsg->length == 0)
4924 DPRINTK("padding done, sg[%d].length=%u pad_len=%u\n",
4925 qc->n_elem - 1, lsg->length, qc->pad_len);
4928 pre_n_elem = qc->n_elem;
4929 if (trim_sg && pre_n_elem)
4938 n_elem = dma_map_sg(ap->dev, sg, pre_n_elem, dir);
4940 /* restore last sg */
4941 lsg->length += qc->pad_len;
4945 DPRINTK("%d sg elements mapped\n", n_elem);
4948 qc->n_elem = n_elem;
4954 * swap_buf_le16 - swap halves of 16-bit words in place
4955 * @buf: Buffer to swap
4956 * @buf_words: Number of 16-bit words in buffer.
4958 * Swap halves of 16-bit words if needed to convert from
4959 * little-endian byte order to native cpu byte order, or
4963 * Inherited from caller.
4965 void swap_buf_le16(u16 *buf, unsigned int buf_words)
4970 for (i = 0; i < buf_words; i++)
4971 buf[i] = le16_to_cpu(buf[i]);
4972 #endif /* __BIG_ENDIAN */
4976 * ata_data_xfer - Transfer data by PIO
4977 * @adev: device to target
4979 * @buflen: buffer length
4980 * @write_data: read/write
4982 * Transfer data from/to the device data register by PIO.
4985 * Inherited from caller.
4987 void ata_data_xfer(struct ata_device *adev, unsigned char *buf,
4988 unsigned int buflen, int write_data)
4990 struct ata_port *ap = adev->link->ap;
4991 unsigned int words = buflen >> 1;
4993 /* Transfer multiple of 2 bytes */
4995 iowrite16_rep(ap->ioaddr.data_addr, buf, words);
4997 ioread16_rep(ap->ioaddr.data_addr, buf, words);
4999 /* Transfer trailing 1 byte, if any. */
5000 if (unlikely(buflen & 0x01)) {
5001 u16 align_buf[1] = { 0 };
5002 unsigned char *trailing_buf = buf + buflen - 1;
5005 memcpy(align_buf, trailing_buf, 1);
5006 iowrite16(le16_to_cpu(align_buf[0]), ap->ioaddr.data_addr);
5008 align_buf[0] = cpu_to_le16(ioread16(ap->ioaddr.data_addr));
5009 memcpy(trailing_buf, align_buf, 1);
5015 * ata_data_xfer_noirq - Transfer data by PIO
5016 * @adev: device to target
5018 * @buflen: buffer length
5019 * @write_data: read/write
5021 * Transfer data from/to the device data register by PIO. Do the
5022 * transfer with interrupts disabled.
5025 * Inherited from caller.
5027 void ata_data_xfer_noirq(struct ata_device *adev, unsigned char *buf,
5028 unsigned int buflen, int write_data)
5030 unsigned long flags;
5031 local_irq_save(flags);
5032 ata_data_xfer(adev, buf, buflen, write_data);
5033 local_irq_restore(flags);
5038 * ata_pio_sector - Transfer a sector of data.
5039 * @qc: Command on going
5041 * Transfer qc->sect_size bytes of data from/to the ATA device.
5044 * Inherited from caller.
5047 static void ata_pio_sector(struct ata_queued_cmd *qc)
5049 int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
5050 struct ata_port *ap = qc->ap;
5052 unsigned int offset;
5055 if (qc->curbytes == qc->nbytes - qc->sect_size)
5056 ap->hsm_task_state = HSM_ST_LAST;
5058 page = sg_page(qc->cursg);
5059 offset = qc->cursg->offset + qc->cursg_ofs;
5061 /* get the current page and offset */
5062 page = nth_page(page, (offset >> PAGE_SHIFT));
5063 offset %= PAGE_SIZE;
5065 DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
5067 if (PageHighMem(page)) {
5068 unsigned long flags;
5070 /* FIXME: use a bounce buffer */
5071 local_irq_save(flags);
5072 buf = kmap_atomic(page, KM_IRQ0);
5074 /* do the actual data transfer */
5075 ap->ops->data_xfer(qc->dev, buf + offset, qc->sect_size, do_write);
5077 kunmap_atomic(buf, KM_IRQ0);
5078 local_irq_restore(flags);
5080 buf = page_address(page);
5081 ap->ops->data_xfer(qc->dev, buf + offset, qc->sect_size, do_write);
5084 qc->curbytes += qc->sect_size;
5085 qc->cursg_ofs += qc->sect_size;
5087 if (qc->cursg_ofs == qc->cursg->length) {
5088 qc->cursg = sg_next(qc->cursg);
5094 * ata_pio_sectors - Transfer one or many sectors.
5095 * @qc: Command on going
5097 * Transfer one or many sectors of data from/to the
5098 * ATA device for the DRQ request.
5101 * Inherited from caller.
5104 static void ata_pio_sectors(struct ata_queued_cmd *qc)
5106 if (is_multi_taskfile(&qc->tf)) {
5107 /* READ/WRITE MULTIPLE */
5110 WARN_ON(qc->dev->multi_count == 0);
5112 nsect = min((qc->nbytes - qc->curbytes) / qc->sect_size,
5113 qc->dev->multi_count);
5119 ata_altstatus(qc->ap); /* flush */
5123 * atapi_send_cdb - Write CDB bytes to hardware
5124 * @ap: Port to which ATAPI device is attached.
5125 * @qc: Taskfile currently active
5127 * When device has indicated its readiness to accept
5128 * a CDB, this function is called. Send the CDB.
5134 static void atapi_send_cdb(struct ata_port *ap, struct ata_queued_cmd *qc)
5137 DPRINTK("send cdb\n");
5138 WARN_ON(qc->dev->cdb_len < 12);
5140 ap->ops->data_xfer(qc->dev, qc->cdb, qc->dev->cdb_len, 1);
5141 ata_altstatus(ap); /* flush */
5143 switch (qc->tf.protocol) {
5144 case ATA_PROT_ATAPI:
5145 ap->hsm_task_state = HSM_ST;
5147 case ATA_PROT_ATAPI_NODATA:
5148 ap->hsm_task_state = HSM_ST_LAST;
5150 case ATA_PROT_ATAPI_DMA:
5151 ap->hsm_task_state = HSM_ST_LAST;
5152 /* initiate bmdma */
5153 ap->ops->bmdma_start(qc);
5159 * __atapi_pio_bytes - Transfer data from/to the ATAPI device.
5160 * @qc: Command on going
5161 * @bytes: number of bytes
5163 * Transfer Transfer data from/to the ATAPI device.
5166 * Inherited from caller.
5169 static int __atapi_pio_bytes(struct ata_queued_cmd *qc, unsigned int bytes)
5171 int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
5172 struct ata_port *ap = qc->ap;
5173 struct ata_eh_info *ehi = &qc->dev->link->eh_info;
5174 struct scatterlist *sg;
5177 unsigned int offset, count;
5181 if (unlikely(!sg)) {
5183 * The end of qc->sg is reached and the device expects
5184 * more data to transfer. In order not to overrun qc->sg
5185 * and fulfill length specified in the byte count register,
5186 * - for read case, discard trailing data from the device
5187 * - for write case, padding zero data to the device
5189 u16 pad_buf[1] = { 0 };
5192 if (bytes > qc->curbytes - qc->nbytes + ATAPI_MAX_DRAIN) {
5193 ata_ehi_push_desc(ehi, "too much trailing data "
5194 "buf=%u cur=%u bytes=%u",
5195 qc->nbytes, qc->curbytes, bytes);
5199 /* overflow is exptected for misc ATAPI commands */
5200 if (bytes && !atapi_qc_may_overflow(qc))
5201 ata_dev_printk(qc->dev, KERN_WARNING, "ATAPI %u bytes "
5202 "trailing data (cdb=%02x nbytes=%u)\n",
5203 bytes, qc->cdb[0], qc->nbytes);
5205 for (i = 0; i < (bytes + 1) / 2; i++)
5206 ap->ops->data_xfer(qc->dev, (unsigned char *)pad_buf, 2, do_write);
5208 qc->curbytes += bytes;
5214 offset = sg->offset + qc->cursg_ofs;
5216 /* get the current page and offset */
5217 page = nth_page(page, (offset >> PAGE_SHIFT));
5218 offset %= PAGE_SIZE;
5220 /* don't overrun current sg */
5221 count = min(sg->length - qc->cursg_ofs, bytes);
5223 /* don't cross page boundaries */
5224 count = min(count, (unsigned int)PAGE_SIZE - offset);
5226 DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
5228 if (PageHighMem(page)) {
5229 unsigned long flags;
5231 /* FIXME: use bounce buffer */
5232 local_irq_save(flags);
5233 buf = kmap_atomic(page, KM_IRQ0);
5235 /* do the actual data transfer */
5236 ap->ops->data_xfer(qc->dev, buf + offset, count, do_write);
5238 kunmap_atomic(buf, KM_IRQ0);
5239 local_irq_restore(flags);
5241 buf = page_address(page);
5242 ap->ops->data_xfer(qc->dev, buf + offset, count, do_write);
5246 if ((count & 1) && bytes)
5248 qc->curbytes += count;
5249 qc->cursg_ofs += count;
5251 if (qc->cursg_ofs == sg->length) {
5252 qc->cursg = sg_next(qc->cursg);
5263 * atapi_pio_bytes - Transfer data from/to the ATAPI device.
5264 * @qc: Command on going
5266 * Transfer Transfer data from/to the ATAPI device.
5269 * Inherited from caller.
5272 static void atapi_pio_bytes(struct ata_queued_cmd *qc)
5274 struct ata_port *ap = qc->ap;
5275 struct ata_device *dev = qc->dev;
5276 unsigned int ireason, bc_lo, bc_hi, bytes;
5277 int i_write, do_write = (qc->tf.flags & ATA_TFLAG_WRITE) ? 1 : 0;
5279 /* Abuse qc->result_tf for temp storage of intermediate TF
5280 * here to save some kernel stack usage.
5281 * For normal completion, qc->result_tf is not relevant. For
5282 * error, qc->result_tf is later overwritten by ata_qc_complete().
5283 * So, the correctness of qc->result_tf is not affected.
5285 ap->ops->tf_read(ap, &qc->result_tf);
5286 ireason = qc->result_tf.nsect;
5287 bc_lo = qc->result_tf.lbam;
5288 bc_hi = qc->result_tf.lbah;
5289 bytes = (bc_hi << 8) | bc_lo;
5291 /* shall be cleared to zero, indicating xfer of data */
5292 if (ireason & (1 << 0))
5295 /* make sure transfer direction matches expected */
5296 i_write = ((ireason & (1 << 1)) == 0) ? 1 : 0;
5297 if (do_write != i_write)
5300 VPRINTK("ata%u: xfering %d bytes\n", ap->print_id, bytes);
5302 if (__atapi_pio_bytes(qc, bytes))
5304 ata_altstatus(ap); /* flush */
5309 ata_dev_printk(dev, KERN_INFO, "ATAPI check failed\n");
5310 qc->err_mask |= AC_ERR_HSM;
5311 ap->hsm_task_state = HSM_ST_ERR;
5315 * ata_hsm_ok_in_wq - Check if the qc can be handled in the workqueue.
5316 * @ap: the target ata_port
5320 * 1 if ok in workqueue, 0 otherwise.
5323 static inline int ata_hsm_ok_in_wq(struct ata_port *ap, struct ata_queued_cmd *qc)
5325 if (qc->tf.flags & ATA_TFLAG_POLLING)
5328 if (ap->hsm_task_state == HSM_ST_FIRST) {
5329 if (qc->tf.protocol == ATA_PROT_PIO &&
5330 (qc->tf.flags & ATA_TFLAG_WRITE))
5333 if (ata_is_atapi(qc->tf.protocol) &&
5334 !(qc->dev->flags & ATA_DFLAG_CDB_INTR))
5342 * ata_hsm_qc_complete - finish a qc running on standard HSM
5343 * @qc: Command to complete
5344 * @in_wq: 1 if called from workqueue, 0 otherwise
5346 * Finish @qc which is running on standard HSM.
5349 * If @in_wq is zero, spin_lock_irqsave(host lock).
5350 * Otherwise, none on entry and grabs host lock.
5352 static void ata_hsm_qc_complete(struct ata_queued_cmd *qc, int in_wq)
5354 struct ata_port *ap = qc->ap;
5355 unsigned long flags;
5357 if (ap->ops->error_handler) {
5359 spin_lock_irqsave(ap->lock, flags);
5361 /* EH might have kicked in while host lock is
5364 qc = ata_qc_from_tag(ap, qc->tag);
5366 if (likely(!(qc->err_mask & AC_ERR_HSM))) {
5367 ap->ops->irq_on(ap);
5368 ata_qc_complete(qc);
5370 ata_port_freeze(ap);
5373 spin_unlock_irqrestore(ap->lock, flags);
5375 if (likely(!(qc->err_mask & AC_ERR_HSM)))
5376 ata_qc_complete(qc);
5378 ata_port_freeze(ap);
5382 spin_lock_irqsave(ap->lock, flags);
5383 ap->ops->irq_on(ap);
5384 ata_qc_complete(qc);
5385 spin_unlock_irqrestore(ap->lock, flags);
5387 ata_qc_complete(qc);
5392 * ata_hsm_move - move the HSM to the next state.
5393 * @ap: the target ata_port
5395 * @status: current device status
5396 * @in_wq: 1 if called from workqueue, 0 otherwise
5399 * 1 when poll next status needed, 0 otherwise.
5401 int ata_hsm_move(struct ata_port *ap, struct ata_queued_cmd *qc,
5402 u8 status, int in_wq)
5404 unsigned long flags = 0;
5407 WARN_ON((qc->flags & ATA_QCFLAG_ACTIVE) == 0);
5409 /* Make sure ata_qc_issue_prot() does not throw things
5410 * like DMA polling into the workqueue. Notice that
5411 * in_wq is not equivalent to (qc->tf.flags & ATA_TFLAG_POLLING).
5413 WARN_ON(in_wq != ata_hsm_ok_in_wq(ap, qc));
5416 DPRINTK("ata%u: protocol %d task_state %d (dev_stat 0x%X)\n",
5417 ap->print_id, qc->tf.protocol, ap->hsm_task_state, status);
5419 switch (ap->hsm_task_state) {
5421 /* Send first data block or PACKET CDB */
5423 /* If polling, we will stay in the work queue after
5424 * sending the data. Otherwise, interrupt handler
5425 * takes over after sending the data.
5427 poll_next = (qc->tf.flags & ATA_TFLAG_POLLING);
5429 /* check device status */
5430 if (unlikely((status & ATA_DRQ) == 0)) {
5431 /* handle BSY=0, DRQ=0 as error */
5432 if (likely(status & (ATA_ERR | ATA_DF)))
5433 /* device stops HSM for abort/error */
5434 qc->err_mask |= AC_ERR_DEV;
5436 /* HSM violation. Let EH handle this */
5437 qc->err_mask |= AC_ERR_HSM;
5439 ap->hsm_task_state = HSM_ST_ERR;
5443 /* Device should not ask for data transfer (DRQ=1)
5444 * when it finds something wrong.
5445 * We ignore DRQ here and stop the HSM by
5446 * changing hsm_task_state to HSM_ST_ERR and
5447 * let the EH abort the command or reset the device.
5449 if (unlikely(status & (ATA_ERR | ATA_DF))) {
5450 /* Some ATAPI tape drives forget to clear the ERR bit
5451 * when doing the next command (mostly request sense).
5452 * We ignore ERR here to workaround and proceed sending
5455 if (!(qc->dev->horkage & ATA_HORKAGE_STUCK_ERR)) {
5456 ata_port_printk(ap, KERN_WARNING,
5457 "DRQ=1 with device error, "
5458 "dev_stat 0x%X\n", status);
5459 qc->err_mask |= AC_ERR_HSM;
5460 ap->hsm_task_state = HSM_ST_ERR;
5465 /* Send the CDB (atapi) or the first data block (ata pio out).
5466 * During the state transition, interrupt handler shouldn't
5467 * be invoked before the data transfer is complete and
5468 * hsm_task_state is changed. Hence, the following locking.
5471 spin_lock_irqsave(ap->lock, flags);
5473 if (qc->tf.protocol == ATA_PROT_PIO) {
5474 /* PIO data out protocol.
5475 * send first data block.
5478 /* ata_pio_sectors() might change the state
5479 * to HSM_ST_LAST. so, the state is changed here
5480 * before ata_pio_sectors().
5482 ap->hsm_task_state = HSM_ST;
5483 ata_pio_sectors(qc);
5486 atapi_send_cdb(ap, qc);
5489 spin_unlock_irqrestore(ap->lock, flags);
5491 /* if polling, ata_pio_task() handles the rest.
5492 * otherwise, interrupt handler takes over from here.
5497 /* complete command or read/write the data register */
5498 if (qc->tf.protocol == ATA_PROT_ATAPI) {
5499 /* ATAPI PIO protocol */
5500 if ((status & ATA_DRQ) == 0) {
5501 /* No more data to transfer or device error.
5502 * Device error will be tagged in HSM_ST_LAST.
5504 ap->hsm_task_state = HSM_ST_LAST;
5508 /* Device should not ask for data transfer (DRQ=1)
5509 * when it finds something wrong.
5510 * We ignore DRQ here and stop the HSM by
5511 * changing hsm_task_state to HSM_ST_ERR and
5512 * let the EH abort the command or reset the device.
5514 if (unlikely(status & (ATA_ERR | ATA_DF))) {
5515 ata_port_printk(ap, KERN_WARNING, "DRQ=1 with "
5516 "device error, dev_stat 0x%X\n",
5518 qc->err_mask |= AC_ERR_HSM;
5519 ap->hsm_task_state = HSM_ST_ERR;
5523 atapi_pio_bytes(qc);
5525 if (unlikely(ap->hsm_task_state == HSM_ST_ERR))
5526 /* bad ireason reported by device */
5530 /* ATA PIO protocol */
5531 if (unlikely((status & ATA_DRQ) == 0)) {
5532 /* handle BSY=0, DRQ=0 as error */
5533 if (likely(status & (ATA_ERR | ATA_DF)))
5534 /* device stops HSM for abort/error */
5535 qc->err_mask |= AC_ERR_DEV;
5537 /* HSM violation. Let EH handle this.
5538 * Phantom devices also trigger this
5539 * condition. Mark hint.
5541 qc->err_mask |= AC_ERR_HSM |
5544 ap->hsm_task_state = HSM_ST_ERR;
5548 /* For PIO reads, some devices may ask for
5549 * data transfer (DRQ=1) alone with ERR=1.
5550 * We respect DRQ here and transfer one
5551 * block of junk data before changing the
5552 * hsm_task_state to HSM_ST_ERR.
5554 * For PIO writes, ERR=1 DRQ=1 doesn't make
5555 * sense since the data block has been
5556 * transferred to the device.
5558 if (unlikely(status & (ATA_ERR | ATA_DF))) {
5559 /* data might be corrputed */
5560 qc->err_mask |= AC_ERR_DEV;
5562 if (!(qc->tf.flags & ATA_TFLAG_WRITE)) {
5563 ata_pio_sectors(qc);
5564 status = ata_wait_idle(ap);
5567 if (status & (ATA_BUSY | ATA_DRQ))
5568 qc->err_mask |= AC_ERR_HSM;
5570 /* ata_pio_sectors() might change the
5571 * state to HSM_ST_LAST. so, the state
5572 * is changed after ata_pio_sectors().
5574 ap->hsm_task_state = HSM_ST_ERR;
5578 ata_pio_sectors(qc);
5580 if (ap->hsm_task_state == HSM_ST_LAST &&
5581 (!(qc->tf.flags & ATA_TFLAG_WRITE))) {
5583 status = ata_wait_idle(ap);
5592 if (unlikely(!ata_ok(status))) {
5593 qc->err_mask |= __ac_err_mask(status);
5594 ap->hsm_task_state = HSM_ST_ERR;
5598 /* no more data to transfer */
5599 DPRINTK("ata%u: dev %u command complete, drv_stat 0x%x\n",
5600 ap->print_id, qc->dev->devno, status);
5602 WARN_ON(qc->err_mask);
5604 ap->hsm_task_state = HSM_ST_IDLE;
5606 /* complete taskfile transaction */
5607 ata_hsm_qc_complete(qc, in_wq);
5613 /* make sure qc->err_mask is available to
5614 * know what's wrong and recover
5616 WARN_ON(qc->err_mask == 0);
5618 ap->hsm_task_state = HSM_ST_IDLE;
5620 /* complete taskfile transaction */
5621 ata_hsm_qc_complete(qc, in_wq);
5633 static void ata_pio_task(struct work_struct *work)
5635 struct ata_port *ap =
5636 container_of(work, struct ata_port, port_task.work);
5637 struct ata_queued_cmd *qc = ap->port_task_data;
5642 WARN_ON(ap->hsm_task_state == HSM_ST_IDLE);
5645 * This is purely heuristic. This is a fast path.
5646 * Sometimes when we enter, BSY will be cleared in
5647 * a chk-status or two. If not, the drive is probably seeking
5648 * or something. Snooze for a couple msecs, then
5649 * chk-status again. If still busy, queue delayed work.
5651 status = ata_busy_wait(ap, ATA_BUSY, 5);
5652 if (status & ATA_BUSY) {
5654 status = ata_busy_wait(ap, ATA_BUSY, 10);
5655 if (status & ATA_BUSY) {
5656 ata_port_queue_task(ap, ata_pio_task, qc, ATA_SHORT_PAUSE);
5662 poll_next = ata_hsm_move(ap, qc, status, 1);
5664 /* another command or interrupt handler
5665 * may be running at this point.
5672 * ata_qc_new - Request an available ATA command, for queueing
5673 * @ap: Port associated with device @dev
5674 * @dev: Device from whom we request an available command structure
5680 static struct ata_queued_cmd *ata_qc_new(struct ata_port *ap)
5682 struct ata_queued_cmd *qc = NULL;
5685 /* no command while frozen */
5686 if (unlikely(ap->pflags & ATA_PFLAG_FROZEN))
5689 /* the last tag is reserved for internal command. */
5690 for (i = 0; i < ATA_MAX_QUEUE - 1; i++)
5691 if (!test_and_set_bit(i, &ap->qc_allocated)) {
5692 qc = __ata_qc_from_tag(ap, i);
5703 * ata_qc_new_init - Request an available ATA command, and initialize it
5704 * @dev: Device from whom we request an available command structure
5710 struct ata_queued_cmd *ata_qc_new_init(struct ata_device *dev)
5712 struct ata_port *ap = dev->link->ap;
5713 struct ata_queued_cmd *qc;
5715 qc = ata_qc_new(ap);
5728 * ata_qc_free - free unused ata_queued_cmd
5729 * @qc: Command to complete
5731 * Designed to free unused ata_queued_cmd object
5732 * in case something prevents using it.
5735 * spin_lock_irqsave(host lock)
5737 void ata_qc_free(struct ata_queued_cmd *qc)
5739 struct ata_port *ap = qc->ap;
5742 WARN_ON(qc == NULL); /* ata_qc_from_tag _might_ return NULL */
5746 if (likely(ata_tag_valid(tag))) {
5747 qc->tag = ATA_TAG_POISON;
5748 clear_bit(tag, &ap->qc_allocated);
5752 void __ata_qc_complete(struct ata_queued_cmd *qc)
5754 struct ata_port *ap = qc->ap;
5755 struct ata_link *link = qc->dev->link;
5757 WARN_ON(qc == NULL); /* ata_qc_from_tag _might_ return NULL */
5758 WARN_ON(!(qc->flags & ATA_QCFLAG_ACTIVE));
5760 if (likely(qc->flags & ATA_QCFLAG_DMAMAP))
5763 /* command should be marked inactive atomically with qc completion */
5764 if (qc->tf.protocol == ATA_PROT_NCQ) {
5765 link->sactive &= ~(1 << qc->tag);
5767 ap->nr_active_links--;
5769 link->active_tag = ATA_TAG_POISON;
5770 ap->nr_active_links--;
5773 /* clear exclusive status */
5774 if (unlikely(qc->flags & ATA_QCFLAG_CLEAR_EXCL &&
5775 ap->excl_link == link))
5776 ap->excl_link = NULL;
5778 /* atapi: mark qc as inactive to prevent the interrupt handler
5779 * from completing the command twice later, before the error handler
5780 * is called. (when rc != 0 and atapi request sense is needed)
5782 qc->flags &= ~ATA_QCFLAG_ACTIVE;
5783 ap->qc_active &= ~(1 << qc->tag);
5785 /* call completion callback */
5786 qc->complete_fn(qc);
5789 static void fill_result_tf(struct ata_queued_cmd *qc)
5791 struct ata_port *ap = qc->ap;
5793 qc->result_tf.flags = qc->tf.flags;
5794 ap->ops->tf_read(ap, &qc->result_tf);
5797 static void ata_verify_xfer(struct ata_queued_cmd *qc)
5799 struct ata_device *dev = qc->dev;
5801 if (ata_tag_internal(qc->tag))
5804 if (ata_is_nodata(qc->tf.protocol))
5807 if ((dev->mwdma_mask || dev->udma_mask) && ata_is_pio(qc->tf.protocol))
5810 dev->flags &= ~ATA_DFLAG_DUBIOUS_XFER;
5814 * ata_qc_complete - Complete an active ATA command
5815 * @qc: Command to complete
5816 * @err_mask: ATA Status register contents
5818 * Indicate to the mid and upper layers that an ATA
5819 * command has completed, with either an ok or not-ok status.
5822 * spin_lock_irqsave(host lock)
5824 void ata_qc_complete(struct ata_queued_cmd *qc)
5826 struct ata_port *ap = qc->ap;
5828 /* XXX: New EH and old EH use different mechanisms to
5829 * synchronize EH with regular execution path.
5831 * In new EH, a failed qc is marked with ATA_QCFLAG_FAILED.
5832 * Normal execution path is responsible for not accessing a
5833 * failed qc. libata core enforces the rule by returning NULL
5834 * from ata_qc_from_tag() for failed qcs.
5836 * Old EH depends on ata_qc_complete() nullifying completion
5837 * requests if ATA_QCFLAG_EH_SCHEDULED is set. Old EH does
5838 * not synchronize with interrupt handler. Only PIO task is
5841 if (ap->ops->error_handler) {
5842 struct ata_device *dev = qc->dev;
5843 struct ata_eh_info *ehi = &dev->link->eh_info;
5845 WARN_ON(ap->pflags & ATA_PFLAG_FROZEN);
5847 if (unlikely(qc->err_mask))
5848 qc->flags |= ATA_QCFLAG_FAILED;
5850 if (unlikely(qc->flags & ATA_QCFLAG_FAILED)) {
5851 if (!ata_tag_internal(qc->tag)) {
5852 /* always fill result TF for failed qc */
5854 ata_qc_schedule_eh(qc);
5859 /* read result TF if requested */
5860 if (qc->flags & ATA_QCFLAG_RESULT_TF)
5863 /* Some commands need post-processing after successful
5866 switch (qc->tf.command) {
5867 case ATA_CMD_SET_FEATURES:
5868 if (qc->tf.feature != SETFEATURES_WC_ON &&
5869 qc->tf.feature != SETFEATURES_WC_OFF)
5872 case ATA_CMD_INIT_DEV_PARAMS: /* CHS translation changed */
5873 case ATA_CMD_SET_MULTI: /* multi_count changed */
5874 /* revalidate device */
5875 ehi->dev_action[dev->devno] |= ATA_EH_REVALIDATE;
5876 ata_port_schedule_eh(ap);
5880 dev->flags |= ATA_DFLAG_SLEEPING;
5884 if (unlikely(dev->flags & ATA_DFLAG_DUBIOUS_XFER))
5885 ata_verify_xfer(qc);
5887 __ata_qc_complete(qc);
5889 if (qc->flags & ATA_QCFLAG_EH_SCHEDULED)
5892 /* read result TF if failed or requested */
5893 if (qc->err_mask || qc->flags & ATA_QCFLAG_RESULT_TF)
5896 __ata_qc_complete(qc);
5901 * ata_qc_complete_multiple - Complete multiple qcs successfully
5902 * @ap: port in question
5903 * @qc_active: new qc_active mask
5904 * @finish_qc: LLDD callback invoked before completing a qc
5906 * Complete in-flight commands. This functions is meant to be
5907 * called from low-level driver's interrupt routine to complete
5908 * requests normally. ap->qc_active and @qc_active is compared
5909 * and commands are completed accordingly.
5912 * spin_lock_irqsave(host lock)
5915 * Number of completed commands on success, -errno otherwise.
5917 int ata_qc_complete_multiple(struct ata_port *ap, u32 qc_active,
5918 void (*finish_qc)(struct ata_queued_cmd *))
5924 done_mask = ap->qc_active ^ qc_active;
5926 if (unlikely(done_mask & qc_active)) {
5927 ata_port_printk(ap, KERN_ERR, "illegal qc_active transition "
5928 "(%08x->%08x)\n", ap->qc_active, qc_active);
5932 for (i = 0; i < ATA_MAX_QUEUE; i++) {
5933 struct ata_queued_cmd *qc;
5935 if (!(done_mask & (1 << i)))
5938 if ((qc = ata_qc_from_tag(ap, i))) {
5941 ata_qc_complete(qc);
5950 * ata_qc_issue - issue taskfile to device
5951 * @qc: command to issue to device
5953 * Prepare an ATA command to submission to device.
5954 * This includes mapping the data into a DMA-able
5955 * area, filling in the S/G table, and finally
5956 * writing the taskfile to hardware, starting the command.
5959 * spin_lock_irqsave(host lock)
5961 void ata_qc_issue(struct ata_queued_cmd *qc)
5963 struct ata_port *ap = qc->ap;
5964 struct ata_link *link = qc->dev->link;
5965 u8 prot = qc->tf.protocol;
5967 /* Make sure only one non-NCQ command is outstanding. The
5968 * check is skipped for old EH because it reuses active qc to
5969 * request ATAPI sense.
5971 WARN_ON(ap->ops->error_handler && ata_tag_valid(link->active_tag));
5973 if (prot == ATA_PROT_NCQ) {
5974 WARN_ON(link->sactive & (1 << qc->tag));
5977 ap->nr_active_links++;
5978 link->sactive |= 1 << qc->tag;
5980 WARN_ON(link->sactive);
5982 ap->nr_active_links++;
5983 link->active_tag = qc->tag;
5986 qc->flags |= ATA_QCFLAG_ACTIVE;
5987 ap->qc_active |= 1 << qc->tag;
5989 if (ata_is_dma(prot) || (ata_is_pio(prot) &&
5990 (ap->flags & ATA_FLAG_PIO_DMA))) {
5991 if (qc->flags & ATA_QCFLAG_SG) {
5992 if (ata_sg_setup(qc))
5994 } else if (qc->flags & ATA_QCFLAG_SINGLE) {
5995 if (ata_sg_setup_one(qc))
5999 qc->flags &= ~ATA_QCFLAG_DMAMAP;
6002 /* if device is sleeping, schedule softreset and abort the link */
6003 if (unlikely(qc->dev->flags & ATA_DFLAG_SLEEPING)) {
6004 link->eh_info.action |= ATA_EH_SOFTRESET;
6005 ata_ehi_push_desc(&link->eh_info, "waking up from sleep");
6006 ata_link_abort(link);
6010 ap->ops->qc_prep(qc);
6012 qc->err_mask |= ap->ops->qc_issue(qc);
6013 if (unlikely(qc->err_mask))
6018 qc->flags &= ~ATA_QCFLAG_DMAMAP;
6019 qc->err_mask |= AC_ERR_SYSTEM;
6021 ata_qc_complete(qc);
6025 * ata_qc_issue_prot - issue taskfile to device in proto-dependent manner
6026 * @qc: command to issue to device
6028 * Using various libata functions and hooks, this function
6029 * starts an ATA command. ATA commands are grouped into
6030 * classes called "protocols", and issuing each type of protocol
6031 * is slightly different.
6033 * May be used as the qc_issue() entry in ata_port_operations.
6036 * spin_lock_irqsave(host lock)
6039 * Zero on success, AC_ERR_* mask on failure
6042 unsigned int ata_qc_issue_prot(struct ata_queued_cmd *qc)
6044 struct ata_port *ap = qc->ap;
6046 /* Use polling pio if the LLD doesn't handle
6047 * interrupt driven pio and atapi CDB interrupt.
6049 if (ap->flags & ATA_FLAG_PIO_POLLING) {
6050 switch (qc->tf.protocol) {
6052 case ATA_PROT_NODATA:
6053 case ATA_PROT_ATAPI:
6054 case ATA_PROT_ATAPI_NODATA:
6055 qc->tf.flags |= ATA_TFLAG_POLLING;
6057 case ATA_PROT_ATAPI_DMA:
6058 if (qc->dev->flags & ATA_DFLAG_CDB_INTR)
6059 /* see ata_dma_blacklisted() */
6067 /* select the device */
6068 ata_dev_select(ap, qc->dev->devno, 1, 0);
6070 /* start the command */
6071 switch (qc->tf.protocol) {
6072 case ATA_PROT_NODATA:
6073 if (qc->tf.flags & ATA_TFLAG_POLLING)
6074 ata_qc_set_polling(qc);
6076 ata_tf_to_host(ap, &qc->tf);
6077 ap->hsm_task_state = HSM_ST_LAST;
6079 if (qc->tf.flags & ATA_TFLAG_POLLING)
6080 ata_port_queue_task(ap, ata_pio_task, qc, 0);
6085 WARN_ON(qc->tf.flags & ATA_TFLAG_POLLING);
6087 ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
6088 ap->ops->bmdma_setup(qc); /* set up bmdma */
6089 ap->ops->bmdma_start(qc); /* initiate bmdma */
6090 ap->hsm_task_state = HSM_ST_LAST;
6094 if (qc->tf.flags & ATA_TFLAG_POLLING)
6095 ata_qc_set_polling(qc);
6097 ata_tf_to_host(ap, &qc->tf);
6099 if (qc->tf.flags & ATA_TFLAG_WRITE) {
6100 /* PIO data out protocol */
6101 ap->hsm_task_state = HSM_ST_FIRST;
6102 ata_port_queue_task(ap, ata_pio_task, qc, 0);
6104 /* always send first data block using
6105 * the ata_pio_task() codepath.
6108 /* PIO data in protocol */
6109 ap->hsm_task_state = HSM_ST;
6111 if (qc->tf.flags & ATA_TFLAG_POLLING)
6112 ata_port_queue_task(ap, ata_pio_task, qc, 0);
6114 /* if polling, ata_pio_task() handles the rest.
6115 * otherwise, interrupt handler takes over from here.
6121 case ATA_PROT_ATAPI:
6122 case ATA_PROT_ATAPI_NODATA:
6123 if (qc->tf.flags & ATA_TFLAG_POLLING)
6124 ata_qc_set_polling(qc);
6126 ata_tf_to_host(ap, &qc->tf);
6128 ap->hsm_task_state = HSM_ST_FIRST;
6130 /* send cdb by polling if no cdb interrupt */
6131 if ((!(qc->dev->flags & ATA_DFLAG_CDB_INTR)) ||
6132 (qc->tf.flags & ATA_TFLAG_POLLING))
6133 ata_port_queue_task(ap, ata_pio_task, qc, 0);
6136 case ATA_PROT_ATAPI_DMA:
6137 WARN_ON(qc->tf.flags & ATA_TFLAG_POLLING);
6139 ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
6140 ap->ops->bmdma_setup(qc); /* set up bmdma */
6141 ap->hsm_task_state = HSM_ST_FIRST;
6143 /* send cdb by polling if no cdb interrupt */
6144 if (!(qc->dev->flags & ATA_DFLAG_CDB_INTR))
6145 ata_port_queue_task(ap, ata_pio_task, qc, 0);
6150 return AC_ERR_SYSTEM;
6157 * ata_host_intr - Handle host interrupt for given (port, task)
6158 * @ap: Port on which interrupt arrived (possibly...)
6159 * @qc: Taskfile currently active in engine
6161 * Handle host interrupt for given queued command. Currently,
6162 * only DMA interrupts are handled. All other commands are
6163 * handled via polling with interrupts disabled (nIEN bit).
6166 * spin_lock_irqsave(host lock)
6169 * One if interrupt was handled, zero if not (shared irq).
6172 inline unsigned int ata_host_intr(struct ata_port *ap,
6173 struct ata_queued_cmd *qc)
6175 struct ata_eh_info *ehi = &ap->link.eh_info;
6176 u8 status, host_stat = 0;
6178 VPRINTK("ata%u: protocol %d task_state %d\n",
6179 ap->print_id, qc->tf.protocol, ap->hsm_task_state);
6181 /* Check whether we are expecting interrupt in this state */
6182 switch (ap->hsm_task_state) {
6184 /* Some pre-ATAPI-4 devices assert INTRQ
6185 * at this state when ready to receive CDB.
6188 /* Check the ATA_DFLAG_CDB_INTR flag is enough here.
6189 * The flag was turned on only for atapi devices. No
6190 * need to check ata_is_atapi(qc->tf.protocol) again.
6192 if (!(qc->dev->flags & ATA_DFLAG_CDB_INTR))
6196 if (qc->tf.protocol == ATA_PROT_DMA ||
6197 qc->tf.protocol == ATA_PROT_ATAPI_DMA) {
6198 /* check status of DMA engine */
6199 host_stat = ap->ops->bmdma_status(ap);
6200 VPRINTK("ata%u: host_stat 0x%X\n",
6201 ap->print_id, host_stat);
6203 /* if it's not our irq... */
6204 if (!(host_stat & ATA_DMA_INTR))
6207 /* before we do anything else, clear DMA-Start bit */
6208 ap->ops->bmdma_stop(qc);
6210 if (unlikely(host_stat & ATA_DMA_ERR)) {
6211 /* error when transfering data to/from memory */
6212 qc->err_mask |= AC_ERR_HOST_BUS;
6213 ap->hsm_task_state = HSM_ST_ERR;
6223 /* check altstatus */
6224 status = ata_altstatus(ap);
6225 if (status & ATA_BUSY)
6228 /* check main status, clearing INTRQ */
6229 status = ata_chk_status(ap);
6230 if (unlikely(status & ATA_BUSY))
6233 /* ack bmdma irq events */
6234 ap->ops->irq_clear(ap);
6236 ata_hsm_move(ap, qc, status, 0);
6238 if (unlikely(qc->err_mask) && (qc->tf.protocol == ATA_PROT_DMA ||
6239 qc->tf.protocol == ATA_PROT_ATAPI_DMA))
6240 ata_ehi_push_desc(ehi, "BMDMA stat 0x%x", host_stat);
6242 return 1; /* irq handled */
6245 ap->stats.idle_irq++;
6248 if ((ap->stats.idle_irq % 1000) == 0) {
6250 ap->ops->irq_clear(ap);
6251 ata_port_printk(ap, KERN_WARNING, "irq trap\n");
6255 return 0; /* irq not handled */
6259 * ata_interrupt - Default ATA host interrupt handler
6260 * @irq: irq line (unused)
6261 * @dev_instance: pointer to our ata_host information structure
6263 * Default interrupt handler for PCI IDE devices. Calls
6264 * ata_host_intr() for each port that is not disabled.
6267 * Obtains host lock during operation.
6270 * IRQ_NONE or IRQ_HANDLED.
6273 irqreturn_t ata_interrupt(int irq, void *dev_instance)
6275 struct ata_host *host = dev_instance;
6277 unsigned int handled = 0;
6278 unsigned long flags;
6280 /* TODO: make _irqsave conditional on x86 PCI IDE legacy mode */
6281 spin_lock_irqsave(&host->lock, flags);
6283 for (i = 0; i < host->n_ports; i++) {
6284 struct ata_port *ap;
6286 ap = host->ports[i];
6288 !(ap->flags & ATA_FLAG_DISABLED)) {
6289 struct ata_queued_cmd *qc;
6291 qc = ata_qc_from_tag(ap, ap->link.active_tag);
6292 if (qc && (!(qc->tf.flags & ATA_TFLAG_POLLING)) &&
6293 (qc->flags & ATA_QCFLAG_ACTIVE))
6294 handled |= ata_host_intr(ap, qc);
6298 spin_unlock_irqrestore(&host->lock, flags);
6300 return IRQ_RETVAL(handled);
6304 * sata_scr_valid - test whether SCRs are accessible
6305 * @link: ATA link to test SCR accessibility for
6307 * Test whether SCRs are accessible for @link.
6313 * 1 if SCRs are accessible, 0 otherwise.
6315 int sata_scr_valid(struct ata_link *link)
6317 struct ata_port *ap = link->ap;
6319 return (ap->flags & ATA_FLAG_SATA) && ap->ops->scr_read;
6323 * sata_scr_read - read SCR register of the specified port
6324 * @link: ATA link to read SCR for
6326 * @val: Place to store read value
6328 * Read SCR register @reg of @link into *@val. This function is
6329 * guaranteed to succeed if @link is ap->link, the cable type of
6330 * the port is SATA and the port implements ->scr_read.
6333 * None if @link is ap->link. Kernel thread context otherwise.
6336 * 0 on success, negative errno on failure.
6338 int sata_scr_read(struct ata_link *link, int reg, u32 *val)
6340 if (ata_is_host_link(link)) {
6341 struct ata_port *ap = link->ap;
6343 if (sata_scr_valid(link))
6344 return ap->ops->scr_read(ap, reg, val);
6348 return sata_pmp_scr_read(link, reg, val);
6352 * sata_scr_write - write SCR register of the specified port
6353 * @link: ATA link to write SCR for
6354 * @reg: SCR to write
6355 * @val: value to write
6357 * Write @val to SCR register @reg of @link. This function is
6358 * guaranteed to succeed if @link is ap->link, the cable type of
6359 * the port is SATA and the port implements ->scr_read.
6362 * None if @link is ap->link. Kernel thread context otherwise.
6365 * 0 on success, negative errno on failure.
6367 int sata_scr_write(struct ata_link *link, int reg, u32 val)
6369 if (ata_is_host_link(link)) {
6370 struct ata_port *ap = link->ap;
6372 if (sata_scr_valid(link))
6373 return ap->ops->scr_write(ap, reg, val);
6377 return sata_pmp_scr_write(link, reg, val);
6381 * sata_scr_write_flush - write SCR register of the specified port and flush
6382 * @link: ATA link to write SCR for
6383 * @reg: SCR to write
6384 * @val: value to write
6386 * This function is identical to sata_scr_write() except that this
6387 * function performs flush after writing to the register.
6390 * None if @link is ap->link. Kernel thread context otherwise.
6393 * 0 on success, negative errno on failure.
6395 int sata_scr_write_flush(struct ata_link *link, int reg, u32 val)
6397 if (ata_is_host_link(link)) {
6398 struct ata_port *ap = link->ap;
6401 if (sata_scr_valid(link)) {
6402 rc = ap->ops->scr_write(ap, reg, val);
6404 rc = ap->ops->scr_read(ap, reg, &val);
6410 return sata_pmp_scr_write(link, reg, val);
6414 * ata_link_online - test whether the given link is online
6415 * @link: ATA link to test
6417 * Test whether @link is online. Note that this function returns
6418 * 0 if online status of @link cannot be obtained, so
6419 * ata_link_online(link) != !ata_link_offline(link).
6425 * 1 if the port online status is available and online.
6427 int ata_link_online(struct ata_link *link)
6431 if (sata_scr_read(link, SCR_STATUS, &sstatus) == 0 &&
6432 (sstatus & 0xf) == 0x3)
6438 * ata_link_offline - test whether the given link is offline
6439 * @link: ATA link to test
6441 * Test whether @link is offline. Note that this function
6442 * returns 0 if offline status of @link cannot be obtained, so
6443 * ata_link_online(link) != !ata_link_offline(link).
6449 * 1 if the port offline status is available and offline.
6451 int ata_link_offline(struct ata_link *link)
6455 if (sata_scr_read(link, SCR_STATUS, &sstatus) == 0 &&
6456 (sstatus & 0xf) != 0x3)
6461 int ata_flush_cache(struct ata_device *dev)
6463 unsigned int err_mask;
6466 if (!ata_try_flush_cache(dev))
6469 if (dev->flags & ATA_DFLAG_FLUSH_EXT)
6470 cmd = ATA_CMD_FLUSH_EXT;
6472 cmd = ATA_CMD_FLUSH;
6474 /* This is wrong. On a failed flush we get back the LBA of the lost
6475 sector and we should (assuming it wasn't aborted as unknown) issue
6476 a further flush command to continue the writeback until it
6478 err_mask = ata_do_simple_cmd(dev, cmd);
6480 ata_dev_printk(dev, KERN_ERR, "failed to flush cache\n");
6488 static int ata_host_request_pm(struct ata_host *host, pm_message_t mesg,
6489 unsigned int action, unsigned int ehi_flags,
6492 unsigned long flags;
6495 for (i = 0; i < host->n_ports; i++) {
6496 struct ata_port *ap = host->ports[i];
6497 struct ata_link *link;
6499 /* Previous resume operation might still be in
6500 * progress. Wait for PM_PENDING to clear.
6502 if (ap->pflags & ATA_PFLAG_PM_PENDING) {
6503 ata_port_wait_eh(ap);
6504 WARN_ON(ap->pflags & ATA_PFLAG_PM_PENDING);
6507 /* request PM ops to EH */
6508 spin_lock_irqsave(ap->lock, flags);
6513 ap->pm_result = &rc;
6516 ap->pflags |= ATA_PFLAG_PM_PENDING;
6517 __ata_port_for_each_link(link, ap) {
6518 link->eh_info.action |= action;
6519 link->eh_info.flags |= ehi_flags;
6522 ata_port_schedule_eh(ap);
6524 spin_unlock_irqrestore(ap->lock, flags);
6526 /* wait and check result */
6528 ata_port_wait_eh(ap);
6529 WARN_ON(ap->pflags & ATA_PFLAG_PM_PENDING);
6539 * ata_host_suspend - suspend host
6540 * @host: host to suspend
6543 * Suspend @host. Actual operation is performed by EH. This
6544 * function requests EH to perform PM operations and waits for EH
6548 * Kernel thread context (may sleep).
6551 * 0 on success, -errno on failure.
6553 int ata_host_suspend(struct ata_host *host, pm_message_t mesg)
6558 * disable link pm on all ports before requesting
6561 ata_lpm_enable(host);
6563 rc = ata_host_request_pm(host, mesg, 0, ATA_EHI_QUIET, 1);
6565 host->dev->power.power_state = mesg;
6570 * ata_host_resume - resume host
6571 * @host: host to resume
6573 * Resume @host. Actual operation is performed by EH. This
6574 * function requests EH to perform PM operations and returns.
6575 * Note that all resume operations are performed parallely.
6578 * Kernel thread context (may sleep).
6580 void ata_host_resume(struct ata_host *host)
6582 ata_host_request_pm(host, PMSG_ON, ATA_EH_SOFTRESET,
6583 ATA_EHI_NO_AUTOPSY | ATA_EHI_QUIET, 0);
6584 host->dev->power.power_state = PMSG_ON;
6586 /* reenable link pm */
6587 ata_lpm_disable(host);
6592 * ata_port_start - Set port up for dma.
6593 * @ap: Port to initialize
6595 * Called just after data structures for each port are
6596 * initialized. Allocates space for PRD table.
6598 * May be used as the port_start() entry in ata_port_operations.
6601 * Inherited from caller.
6603 int ata_port_start(struct ata_port *ap)
6605 struct device *dev = ap->dev;
6608 ap->prd = dmam_alloc_coherent(dev, ATA_PRD_TBL_SZ, &ap->prd_dma,
6613 rc = ata_pad_alloc(ap, dev);
6617 DPRINTK("prd alloc, virt %p, dma %llx\n", ap->prd,
6618 (unsigned long long)ap->prd_dma);
6623 * ata_dev_init - Initialize an ata_device structure
6624 * @dev: Device structure to initialize
6626 * Initialize @dev in preparation for probing.
6629 * Inherited from caller.
6631 void ata_dev_init(struct ata_device *dev)
6633 struct ata_link *link = dev->link;
6634 struct ata_port *ap = link->ap;
6635 unsigned long flags;
6637 /* SATA spd limit is bound to the first device */
6638 link->sata_spd_limit = link->hw_sata_spd_limit;
6641 /* High bits of dev->flags are used to record warm plug
6642 * requests which occur asynchronously. Synchronize using
6645 spin_lock_irqsave(ap->lock, flags);
6646 dev->flags &= ~ATA_DFLAG_INIT_MASK;
6648 spin_unlock_irqrestore(ap->lock, flags);
6650 memset((void *)dev + ATA_DEVICE_CLEAR_OFFSET, 0,
6651 sizeof(*dev) - ATA_DEVICE_CLEAR_OFFSET);
6652 dev->pio_mask = UINT_MAX;
6653 dev->mwdma_mask = UINT_MAX;
6654 dev->udma_mask = UINT_MAX;
6658 * ata_link_init - Initialize an ata_link structure
6659 * @ap: ATA port link is attached to
6660 * @link: Link structure to initialize
6661 * @pmp: Port multiplier port number
6666 * Kernel thread context (may sleep)
6668 void ata_link_init(struct ata_port *ap, struct ata_link *link, int pmp)
6672 /* clear everything except for devices */
6673 memset(link, 0, offsetof(struct ata_link, device[0]));
6677 link->active_tag = ATA_TAG_POISON;
6678 link->hw_sata_spd_limit = UINT_MAX;
6680 /* can't use iterator, ap isn't initialized yet */
6681 for (i = 0; i < ATA_MAX_DEVICES; i++) {
6682 struct ata_device *dev = &link->device[i];
6685 dev->devno = dev - link->device;
6691 * sata_link_init_spd - Initialize link->sata_spd_limit
6692 * @link: Link to configure sata_spd_limit for
6694 * Initialize @link->[hw_]sata_spd_limit to the currently
6698 * Kernel thread context (may sleep).
6701 * 0 on success, -errno on failure.
6703 int sata_link_init_spd(struct ata_link *link)
6708 rc = sata_scr_read(link, SCR_CONTROL, &scontrol);
6712 spd = (scontrol >> 4) & 0xf;
6714 link->hw_sata_spd_limit &= (1 << spd) - 1;
6716 link->sata_spd_limit = link->hw_sata_spd_limit;
6722 * ata_port_alloc - allocate and initialize basic ATA port resources
6723 * @host: ATA host this allocated port belongs to
6725 * Allocate and initialize basic ATA port resources.
6728 * Allocate ATA port on success, NULL on failure.
6731 * Inherited from calling layer (may sleep).
6733 struct ata_port *ata_port_alloc(struct ata_host *host)
6735 struct ata_port *ap;
6739 ap = kzalloc(sizeof(*ap), GFP_KERNEL);
6743 ap->pflags |= ATA_PFLAG_INITIALIZING;
6744 ap->lock = &host->lock;
6745 ap->flags = ATA_FLAG_DISABLED;
6747 ap->ctl = ATA_DEVCTL_OBS;
6749 ap->dev = host->dev;
6750 ap->last_ctl = 0xFF;
6752 #if defined(ATA_VERBOSE_DEBUG)
6753 /* turn on all debugging levels */
6754 ap->msg_enable = 0x00FF;
6755 #elif defined(ATA_DEBUG)
6756 ap->msg_enable = ATA_MSG_DRV | ATA_MSG_INFO | ATA_MSG_CTL | ATA_MSG_WARN | ATA_MSG_ERR;
6758 ap->msg_enable = ATA_MSG_DRV | ATA_MSG_ERR | ATA_MSG_WARN;
6761 INIT_DELAYED_WORK(&ap->port_task, NULL);
6762 INIT_DELAYED_WORK(&ap->hotplug_task, ata_scsi_hotplug);
6763 INIT_WORK(&ap->scsi_rescan_task, ata_scsi_dev_rescan);
6764 INIT_LIST_HEAD(&ap->eh_done_q);
6765 init_waitqueue_head(&ap->eh_wait_q);
6766 init_timer_deferrable(&ap->fastdrain_timer);
6767 ap->fastdrain_timer.function = ata_eh_fastdrain_timerfn;
6768 ap->fastdrain_timer.data = (unsigned long)ap;
6770 ap->cbl = ATA_CBL_NONE;
6772 ata_link_init(ap, &ap->link, 0);
6775 ap->stats.unhandled_irq = 1;
6776 ap->stats.idle_irq = 1;
6781 static void ata_host_release(struct device *gendev, void *res)
6783 struct ata_host *host = dev_get_drvdata(gendev);
6786 for (i = 0; i < host->n_ports; i++) {
6787 struct ata_port *ap = host->ports[i];
6793 scsi_host_put(ap->scsi_host);
6795 kfree(ap->pmp_link);
6797 host->ports[i] = NULL;
6800 dev_set_drvdata(gendev, NULL);
6804 * ata_host_alloc - allocate and init basic ATA host resources
6805 * @dev: generic device this host is associated with
6806 * @max_ports: maximum number of ATA ports associated with this host
6808 * Allocate and initialize basic ATA host resources. LLD calls
6809 * this function to allocate a host, initializes it fully and
6810 * attaches it using ata_host_register().
6812 * @max_ports ports are allocated and host->n_ports is
6813 * initialized to @max_ports. The caller is allowed to decrease
6814 * host->n_ports before calling ata_host_register(). The unused
6815 * ports will be automatically freed on registration.
6818 * Allocate ATA host on success, NULL on failure.
6821 * Inherited from calling layer (may sleep).
6823 struct ata_host *ata_host_alloc(struct device *dev, int max_ports)
6825 struct ata_host *host;
6831 if (!devres_open_group(dev, NULL, GFP_KERNEL))
6834 /* alloc a container for our list of ATA ports (buses) */
6835 sz = sizeof(struct ata_host) + (max_ports + 1) * sizeof(void *);
6836 /* alloc a container for our list of ATA ports (buses) */
6837 host = devres_alloc(ata_host_release, sz, GFP_KERNEL);
6841 devres_add(dev, host);
6842 dev_set_drvdata(dev, host);
6844 spin_lock_init(&host->lock);
6846 host->n_ports = max_ports;
6848 /* allocate ports bound to this host */
6849 for (i = 0; i < max_ports; i++) {
6850 struct ata_port *ap;
6852 ap = ata_port_alloc(host);
6857 host->ports[i] = ap;
6860 devres_remove_group(dev, NULL);
6864 devres_release_group(dev, NULL);
6869 * ata_host_alloc_pinfo - alloc host and init with port_info array
6870 * @dev: generic device this host is associated with
6871 * @ppi: array of ATA port_info to initialize host with
6872 * @n_ports: number of ATA ports attached to this host
6874 * Allocate ATA host and initialize with info from @ppi. If NULL
6875 * terminated, @ppi may contain fewer entries than @n_ports. The
6876 * last entry will be used for the remaining ports.
6879 * Allocate ATA host on success, NULL on failure.
6882 * Inherited from calling layer (may sleep).
6884 struct ata_host *ata_host_alloc_pinfo(struct device *dev,
6885 const struct ata_port_info * const * ppi,
6888 const struct ata_port_info *pi;
6889 struct ata_host *host;
6892 host = ata_host_alloc(dev, n_ports);
6896 for (i = 0, j = 0, pi = NULL; i < host->n_ports; i++) {
6897 struct ata_port *ap = host->ports[i];
6902 ap->pio_mask = pi->pio_mask;
6903 ap->mwdma_mask = pi->mwdma_mask;
6904 ap->udma_mask = pi->udma_mask;
6905 ap->flags |= pi->flags;
6906 ap->link.flags |= pi->link_flags;
6907 ap->ops = pi->port_ops;
6909 if (!host->ops && (pi->port_ops != &ata_dummy_port_ops))
6910 host->ops = pi->port_ops;
6911 if (!host->private_data && pi->private_data)
6912 host->private_data = pi->private_data;
6918 static void ata_host_stop(struct device *gendev, void *res)
6920 struct ata_host *host = dev_get_drvdata(gendev);
6923 WARN_ON(!(host->flags & ATA_HOST_STARTED));
6925 for (i = 0; i < host->n_ports; i++) {
6926 struct ata_port *ap = host->ports[i];
6928 if (ap->ops->port_stop)
6929 ap->ops->port_stop(ap);
6932 if (host->ops->host_stop)
6933 host->ops->host_stop(host);
6937 * ata_host_start - start and freeze ports of an ATA host
6938 * @host: ATA host to start ports for
6940 * Start and then freeze ports of @host. Started status is
6941 * recorded in host->flags, so this function can be called
6942 * multiple times. Ports are guaranteed to get started only
6943 * once. If host->ops isn't initialized yet, its set to the
6944 * first non-dummy port ops.
6947 * Inherited from calling layer (may sleep).
6950 * 0 if all ports are started successfully, -errno otherwise.
6952 int ata_host_start(struct ata_host *host)
6955 void *start_dr = NULL;
6958 if (host->flags & ATA_HOST_STARTED)
6961 for (i = 0; i < host->n_ports; i++) {
6962 struct ata_port *ap = host->ports[i];
6964 if (!host->ops && !ata_port_is_dummy(ap))
6965 host->ops = ap->ops;
6967 if (ap->ops->port_stop)
6971 if (host->ops->host_stop)
6975 start_dr = devres_alloc(ata_host_stop, 0, GFP_KERNEL);
6980 for (i = 0; i < host->n_ports; i++) {
6981 struct ata_port *ap = host->ports[i];
6983 if (ap->ops->port_start) {
6984 rc = ap->ops->port_start(ap);
6987 dev_printk(KERN_ERR, host->dev,
6988 "failed to start port %d "
6989 "(errno=%d)\n", i, rc);
6993 ata_eh_freeze_port(ap);
6997 devres_add(host->dev, start_dr);
6998 host->flags |= ATA_HOST_STARTED;
7003 struct ata_port *ap = host->ports[i];
7005 if (ap->ops->port_stop)
7006 ap->ops->port_stop(ap);
7008 devres_free(start_dr);
7013 * ata_sas_host_init - Initialize a host struct
7014 * @host: host to initialize
7015 * @dev: device host is attached to
7016 * @flags: host flags
7020 * PCI/etc. bus probe sem.
7023 /* KILLME - the only user left is ipr */
7024 void ata_host_init(struct ata_host *host, struct device *dev,
7025 unsigned long flags, const struct ata_port_operations *ops)
7027 spin_lock_init(&host->lock);
7029 host->flags = flags;
7034 * ata_host_register - register initialized ATA host
7035 * @host: ATA host to register
7036 * @sht: template for SCSI host
7038 * Register initialized ATA host. @host is allocated using
7039 * ata_host_alloc() and fully initialized by LLD. This function
7040 * starts ports, registers @host with ATA and SCSI layers and
7041 * probe registered devices.
7044 * Inherited from calling layer (may sleep).
7047 * 0 on success, -errno otherwise.
7049 int ata_host_register(struct ata_host *host, struct scsi_host_template *sht)
7053 /* host must have been started */
7054 if (!(host->flags & ATA_HOST_STARTED)) {
7055 dev_printk(KERN_ERR, host->dev,
7056 "BUG: trying to register unstarted host\n");
7061 /* Blow away unused ports. This happens when LLD can't
7062 * determine the exact number of ports to allocate at
7065 for (i = host->n_ports; host->ports[i]; i++)
7066 kfree(host->ports[i]);
7068 /* give ports names and add SCSI hosts */
7069 for (i = 0; i < host->n_ports; i++)
7070 host->ports[i]->print_id = ata_print_id++;
7072 rc = ata_scsi_add_hosts(host, sht);
7076 /* associate with ACPI nodes */
7077 ata_acpi_associate(host);
7079 /* set cable, sata_spd_limit and report */
7080 for (i = 0; i < host->n_ports; i++) {
7081 struct ata_port *ap = host->ports[i];
7082 unsigned long xfer_mask;
7084 /* set SATA cable type if still unset */
7085 if (ap->cbl == ATA_CBL_NONE && (ap->flags & ATA_FLAG_SATA))
7086 ap->cbl = ATA_CBL_SATA;
7088 /* init sata_spd_limit to the current value */
7089 sata_link_init_spd(&ap->link);
7091 /* print per-port info to dmesg */
7092 xfer_mask = ata_pack_xfermask(ap->pio_mask, ap->mwdma_mask,
7095 if (!ata_port_is_dummy(ap)) {
7096 ata_port_printk(ap, KERN_INFO,
7097 "%cATA max %s %s\n",
7098 (ap->flags & ATA_FLAG_SATA) ? 'S' : 'P',
7099 ata_mode_string(xfer_mask),
7100 ap->link.eh_info.desc);
7101 ata_ehi_clear_desc(&ap->link.eh_info);
7103 ata_port_printk(ap, KERN_INFO, "DUMMY\n");
7106 /* perform each probe synchronously */
7107 DPRINTK("probe begin\n");
7108 for (i = 0; i < host->n_ports; i++) {
7109 struct ata_port *ap = host->ports[i];
7113 if (ap->ops->error_handler) {
7114 struct ata_eh_info *ehi = &ap->link.eh_info;
7115 unsigned long flags;
7119 /* kick EH for boot probing */
7120 spin_lock_irqsave(ap->lock, flags);
7123 (1 << ata_link_max_devices(&ap->link)) - 1;
7124 ehi->action |= ATA_EH_SOFTRESET;
7125 ehi->flags |= ATA_EHI_NO_AUTOPSY | ATA_EHI_QUIET;
7127 ap->pflags &= ~ATA_PFLAG_INITIALIZING;
7128 ap->pflags |= ATA_PFLAG_LOADING;
7129 ata_port_schedule_eh(ap);
7131 spin_unlock_irqrestore(ap->lock, flags);
7133 /* wait for EH to finish */
7134 ata_port_wait_eh(ap);
7136 DPRINTK("ata%u: bus probe begin\n", ap->print_id);
7137 rc = ata_bus_probe(ap);
7138 DPRINTK("ata%u: bus probe end\n", ap->print_id);
7141 /* FIXME: do something useful here?
7142 * Current libata behavior will
7143 * tear down everything when
7144 * the module is removed
7145 * or the h/w is unplugged.
7151 /* probes are done, now scan each port's disk(s) */
7152 DPRINTK("host probe begin\n");
7153 for (i = 0; i < host->n_ports; i++) {
7154 struct ata_port *ap = host->ports[i];
7156 ata_scsi_scan_host(ap, 1);
7157 ata_lpm_schedule(ap, ap->pm_policy);
7164 * ata_host_activate - start host, request IRQ and register it
7165 * @host: target ATA host
7166 * @irq: IRQ to request
7167 * @irq_handler: irq_handler used when requesting IRQ
7168 * @irq_flags: irq_flags used when requesting IRQ
7169 * @sht: scsi_host_template to use when registering the host
7171 * After allocating an ATA host and initializing it, most libata
7172 * LLDs perform three steps to activate the host - start host,
7173 * request IRQ and register it. This helper takes necessasry
7174 * arguments and performs the three steps in one go.
7176 * An invalid IRQ skips the IRQ registration and expects the host to
7177 * have set polling mode on the port. In this case, @irq_handler
7181 * Inherited from calling layer (may sleep).
7184 * 0 on success, -errno otherwise.
7186 int ata_host_activate(struct ata_host *host, int irq,
7187 irq_handler_t irq_handler, unsigned long irq_flags,
7188 struct scsi_host_template *sht)
7192 rc = ata_host_start(host);
7196 /* Special case for polling mode */
7198 WARN_ON(irq_handler);
7199 return ata_host_register(host, sht);
7202 rc = devm_request_irq(host->dev, irq, irq_handler, irq_flags,
7203 dev_driver_string(host->dev), host);
7207 for (i = 0; i < host->n_ports; i++)
7208 ata_port_desc(host->ports[i], "irq %d", irq);
7210 rc = ata_host_register(host, sht);
7211 /* if failed, just free the IRQ and leave ports alone */
7213 devm_free_irq(host->dev, irq, host);
7219 * ata_port_detach - Detach ATA port in prepration of device removal
7220 * @ap: ATA port to be detached
7222 * Detach all ATA devices and the associated SCSI devices of @ap;
7223 * then, remove the associated SCSI host. @ap is guaranteed to
7224 * be quiescent on return from this function.
7227 * Kernel thread context (may sleep).
7229 static void ata_port_detach(struct ata_port *ap)
7231 unsigned long flags;
7232 struct ata_link *link;
7233 struct ata_device *dev;
7235 if (!ap->ops->error_handler)
7238 /* tell EH we're leaving & flush EH */
7239 spin_lock_irqsave(ap->lock, flags);
7240 ap->pflags |= ATA_PFLAG_UNLOADING;
7241 spin_unlock_irqrestore(ap->lock, flags);
7243 ata_port_wait_eh(ap);
7245 /* EH is now guaranteed to see UNLOADING - EH context belongs
7246 * to us. Disable all existing devices.
7248 ata_port_for_each_link(link, ap) {
7249 ata_link_for_each_dev(dev, link)
7250 ata_dev_disable(dev);
7253 /* Final freeze & EH. All in-flight commands are aborted. EH
7254 * will be skipped and retrials will be terminated with bad
7257 spin_lock_irqsave(ap->lock, flags);
7258 ata_port_freeze(ap); /* won't be thawed */
7259 spin_unlock_irqrestore(ap->lock, flags);
7261 ata_port_wait_eh(ap);
7262 cancel_rearming_delayed_work(&ap->hotplug_task);
7265 /* remove the associated SCSI host */
7266 scsi_remove_host(ap->scsi_host);
7270 * ata_host_detach - Detach all ports of an ATA host
7271 * @host: Host to detach
7273 * Detach all ports of @host.
7276 * Kernel thread context (may sleep).
7278 void ata_host_detach(struct ata_host *host)
7282 for (i = 0; i < host->n_ports; i++)
7283 ata_port_detach(host->ports[i]);
7285 /* the host is dead now, dissociate ACPI */
7286 ata_acpi_dissociate(host);
7290 * ata_std_ports - initialize ioaddr with standard port offsets.
7291 * @ioaddr: IO address structure to be initialized
7293 * Utility function which initializes data_addr, error_addr,
7294 * feature_addr, nsect_addr, lbal_addr, lbam_addr, lbah_addr,
7295 * device_addr, status_addr, and command_addr to standard offsets
7296 * relative to cmd_addr.
7298 * Does not set ctl_addr, altstatus_addr, bmdma_addr, or scr_addr.
7301 void ata_std_ports(struct ata_ioports *ioaddr)
7303 ioaddr->data_addr = ioaddr->cmd_addr + ATA_REG_DATA;
7304 ioaddr->error_addr = ioaddr->cmd_addr + ATA_REG_ERR;
7305 ioaddr->feature_addr = ioaddr->cmd_addr + ATA_REG_FEATURE;
7306 ioaddr->nsect_addr = ioaddr->cmd_addr + ATA_REG_NSECT;
7307 ioaddr->lbal_addr = ioaddr->cmd_addr + ATA_REG_LBAL;
7308 ioaddr->lbam_addr = ioaddr->cmd_addr + ATA_REG_LBAM;
7309 ioaddr->lbah_addr = ioaddr->cmd_addr + ATA_REG_LBAH;
7310 ioaddr->device_addr = ioaddr->cmd_addr + ATA_REG_DEVICE;
7311 ioaddr->status_addr = ioaddr->cmd_addr + ATA_REG_STATUS;
7312 ioaddr->command_addr = ioaddr->cmd_addr + ATA_REG_CMD;
7319 * ata_pci_remove_one - PCI layer callback for device removal
7320 * @pdev: PCI device that was removed
7322 * PCI layer indicates to libata via this hook that hot-unplug or
7323 * module unload event has occurred. Detach all ports. Resource
7324 * release is handled via devres.
7327 * Inherited from PCI layer (may sleep).
7329 void ata_pci_remove_one(struct pci_dev *pdev)
7331 struct device *dev = &pdev->dev;
7332 struct ata_host *host = dev_get_drvdata(dev);
7334 ata_host_detach(host);
7337 /* move to PCI subsystem */
7338 int pci_test_config_bits(struct pci_dev *pdev, const struct pci_bits *bits)
7340 unsigned long tmp = 0;
7342 switch (bits->width) {
7345 pci_read_config_byte(pdev, bits->reg, &tmp8);
7351 pci_read_config_word(pdev, bits->reg, &tmp16);
7357 pci_read_config_dword(pdev, bits->reg, &tmp32);
7368 return (tmp == bits->val) ? 1 : 0;
7372 void ata_pci_device_do_suspend(struct pci_dev *pdev, pm_message_t mesg)
7374 pci_save_state(pdev);
7375 pci_disable_device(pdev);
7377 if (mesg.event == PM_EVENT_SUSPEND)
7378 pci_set_power_state(pdev, PCI_D3hot);
7381 int ata_pci_device_do_resume(struct pci_dev *pdev)
7385 pci_set_power_state(pdev, PCI_D0);
7386 pci_restore_state(pdev);
7388 rc = pcim_enable_device(pdev);
7390 dev_printk(KERN_ERR, &pdev->dev,
7391 "failed to enable device after resume (%d)\n", rc);
7395 pci_set_master(pdev);
7399 int ata_pci_device_suspend(struct pci_dev *pdev, pm_message_t mesg)
7401 struct ata_host *host = dev_get_drvdata(&pdev->dev);
7404 rc = ata_host_suspend(host, mesg);
7408 ata_pci_device_do_suspend(pdev, mesg);
7413 int ata_pci_device_resume(struct pci_dev *pdev)
7415 struct ata_host *host = dev_get_drvdata(&pdev->dev);
7418 rc = ata_pci_device_do_resume(pdev);
7420 ata_host_resume(host);
7423 #endif /* CONFIG_PM */
7425 #endif /* CONFIG_PCI */
7428 static int __init ata_init(void)
7430 ata_probe_timeout *= HZ;
7431 ata_wq = create_workqueue("ata");
7435 ata_aux_wq = create_singlethread_workqueue("ata_aux");
7437 destroy_workqueue(ata_wq);
7441 printk(KERN_DEBUG "libata version " DRV_VERSION " loaded.\n");
7445 static void __exit ata_exit(void)
7447 destroy_workqueue(ata_wq);
7448 destroy_workqueue(ata_aux_wq);
7451 subsys_initcall(ata_init);
7452 module_exit(ata_exit);
7454 static unsigned long ratelimit_time;
7455 static DEFINE_SPINLOCK(ata_ratelimit_lock);
7457 int ata_ratelimit(void)
7460 unsigned long flags;
7462 spin_lock_irqsave(&ata_ratelimit_lock, flags);
7464 if (time_after(jiffies, ratelimit_time)) {
7466 ratelimit_time = jiffies + (HZ/5);
7470 spin_unlock_irqrestore(&ata_ratelimit_lock, flags);
7476 * ata_wait_register - wait until register value changes
7477 * @reg: IO-mapped register
7478 * @mask: Mask to apply to read register value
7479 * @val: Wait condition
7480 * @interval_msec: polling interval in milliseconds
7481 * @timeout_msec: timeout in milliseconds
7483 * Waiting for some bits of register to change is a common
7484 * operation for ATA controllers. This function reads 32bit LE
7485 * IO-mapped register @reg and tests for the following condition.
7487 * (*@reg & mask) != val
7489 * If the condition is met, it returns; otherwise, the process is
7490 * repeated after @interval_msec until timeout.
7493 * Kernel thread context (may sleep)
7496 * The final register value.
7498 u32 ata_wait_register(void __iomem *reg, u32 mask, u32 val,
7499 unsigned long interval_msec,
7500 unsigned long timeout_msec)
7502 unsigned long timeout;
7505 tmp = ioread32(reg);
7507 /* Calculate timeout _after_ the first read to make sure
7508 * preceding writes reach the controller before starting to
7509 * eat away the timeout.
7511 timeout = jiffies + (timeout_msec * HZ) / 1000;
7513 while ((tmp & mask) == val && time_before(jiffies, timeout)) {
7514 msleep(interval_msec);
7515 tmp = ioread32(reg);
7524 static void ata_dummy_noret(struct ata_port *ap) { }
7525 static int ata_dummy_ret0(struct ata_port *ap) { return 0; }
7526 static void ata_dummy_qc_noret(struct ata_queued_cmd *qc) { }
7528 static u8 ata_dummy_check_status(struct ata_port *ap)
7533 static unsigned int ata_dummy_qc_issue(struct ata_queued_cmd *qc)
7535 return AC_ERR_SYSTEM;
7538 const struct ata_port_operations ata_dummy_port_ops = {
7539 .check_status = ata_dummy_check_status,
7540 .check_altstatus = ata_dummy_check_status,
7541 .dev_select = ata_noop_dev_select,
7542 .qc_prep = ata_noop_qc_prep,
7543 .qc_issue = ata_dummy_qc_issue,
7544 .freeze = ata_dummy_noret,
7545 .thaw = ata_dummy_noret,
7546 .error_handler = ata_dummy_noret,
7547 .post_internal_cmd = ata_dummy_qc_noret,
7548 .irq_clear = ata_dummy_noret,
7549 .port_start = ata_dummy_ret0,
7550 .port_stop = ata_dummy_noret,
7553 const struct ata_port_info ata_dummy_port_info = {
7554 .port_ops = &ata_dummy_port_ops,
7558 * libata is essentially a library of internal helper functions for
7559 * low-level ATA host controller drivers. As such, the API/ABI is
7560 * likely to change as new drivers are added and updated.
7561 * Do not depend on ABI/API stability.
7563 EXPORT_SYMBOL_GPL(sata_deb_timing_normal);
7564 EXPORT_SYMBOL_GPL(sata_deb_timing_hotplug);
7565 EXPORT_SYMBOL_GPL(sata_deb_timing_long);
7566 EXPORT_SYMBOL_GPL(ata_dummy_port_ops);
7567 EXPORT_SYMBOL_GPL(ata_dummy_port_info);
7568 EXPORT_SYMBOL_GPL(ata_std_bios_param);
7569 EXPORT_SYMBOL_GPL(ata_std_ports);
7570 EXPORT_SYMBOL_GPL(ata_host_init);
7571 EXPORT_SYMBOL_GPL(ata_host_alloc);
7572 EXPORT_SYMBOL_GPL(ata_host_alloc_pinfo);
7573 EXPORT_SYMBOL_GPL(ata_host_start);
7574 EXPORT_SYMBOL_GPL(ata_host_register);
7575 EXPORT_SYMBOL_GPL(ata_host_activate);
7576 EXPORT_SYMBOL_GPL(ata_host_detach);
7577 EXPORT_SYMBOL_GPL(ata_sg_init);
7578 EXPORT_SYMBOL_GPL(ata_sg_init_one);
7579 EXPORT_SYMBOL_GPL(ata_hsm_move);
7580 EXPORT_SYMBOL_GPL(ata_qc_complete);
7581 EXPORT_SYMBOL_GPL(ata_qc_complete_multiple);
7582 EXPORT_SYMBOL_GPL(ata_qc_issue_prot);
7583 EXPORT_SYMBOL_GPL(ata_tf_load);
7584 EXPORT_SYMBOL_GPL(ata_tf_read);
7585 EXPORT_SYMBOL_GPL(ata_noop_dev_select);
7586 EXPORT_SYMBOL_GPL(ata_std_dev_select);
7587 EXPORT_SYMBOL_GPL(sata_print_link_status);
7588 EXPORT_SYMBOL_GPL(ata_tf_to_fis);
7589 EXPORT_SYMBOL_GPL(ata_tf_from_fis);
7590 EXPORT_SYMBOL_GPL(ata_pack_xfermask);
7591 EXPORT_SYMBOL_GPL(ata_unpack_xfermask);
7592 EXPORT_SYMBOL_GPL(ata_xfer_mask2mode);
7593 EXPORT_SYMBOL_GPL(ata_xfer_mode2mask);
7594 EXPORT_SYMBOL_GPL(ata_xfer_mode2shift);
7595 EXPORT_SYMBOL_GPL(ata_mode_string);
7596 EXPORT_SYMBOL_GPL(ata_id_xfermask);
7597 EXPORT_SYMBOL_GPL(ata_check_status);
7598 EXPORT_SYMBOL_GPL(ata_altstatus);
7599 EXPORT_SYMBOL_GPL(ata_exec_command);
7600 EXPORT_SYMBOL_GPL(ata_port_start);
7601 EXPORT_SYMBOL_GPL(ata_sff_port_start);
7602 EXPORT_SYMBOL_GPL(ata_interrupt);
7603 EXPORT_SYMBOL_GPL(ata_do_set_mode);
7604 EXPORT_SYMBOL_GPL(ata_data_xfer);
7605 EXPORT_SYMBOL_GPL(ata_data_xfer_noirq);
7606 EXPORT_SYMBOL_GPL(ata_std_qc_defer);
7607 EXPORT_SYMBOL_GPL(ata_qc_prep);
7608 EXPORT_SYMBOL_GPL(ata_dumb_qc_prep);
7609 EXPORT_SYMBOL_GPL(ata_noop_qc_prep);
7610 EXPORT_SYMBOL_GPL(ata_bmdma_setup);
7611 EXPORT_SYMBOL_GPL(ata_bmdma_start);
7612 EXPORT_SYMBOL_GPL(ata_bmdma_irq_clear);
7613 EXPORT_SYMBOL_GPL(ata_bmdma_status);
7614 EXPORT_SYMBOL_GPL(ata_bmdma_stop);
7615 EXPORT_SYMBOL_GPL(ata_bmdma_freeze);
7616 EXPORT_SYMBOL_GPL(ata_bmdma_thaw);
7617 EXPORT_SYMBOL_GPL(ata_bmdma_drive_eh);
7618 EXPORT_SYMBOL_GPL(ata_bmdma_error_handler);
7619 EXPORT_SYMBOL_GPL(ata_bmdma_post_internal_cmd);
7620 EXPORT_SYMBOL_GPL(ata_port_probe);
7621 EXPORT_SYMBOL_GPL(ata_dev_disable);
7622 EXPORT_SYMBOL_GPL(sata_set_spd);
7623 EXPORT_SYMBOL_GPL(sata_link_debounce);
7624 EXPORT_SYMBOL_GPL(sata_link_resume);
7625 EXPORT_SYMBOL_GPL(ata_bus_reset);
7626 EXPORT_SYMBOL_GPL(ata_std_prereset);
7627 EXPORT_SYMBOL_GPL(ata_std_softreset);
7628 EXPORT_SYMBOL_GPL(sata_link_hardreset);
7629 EXPORT_SYMBOL_GPL(sata_std_hardreset);
7630 EXPORT_SYMBOL_GPL(ata_std_postreset);
7631 EXPORT_SYMBOL_GPL(ata_dev_classify);
7632 EXPORT_SYMBOL_GPL(ata_dev_pair);
7633 EXPORT_SYMBOL_GPL(ata_port_disable);
7634 EXPORT_SYMBOL_GPL(ata_ratelimit);
7635 EXPORT_SYMBOL_GPL(ata_wait_register);
7636 EXPORT_SYMBOL_GPL(ata_busy_sleep);
7637 EXPORT_SYMBOL_GPL(ata_wait_after_reset);
7638 EXPORT_SYMBOL_GPL(ata_wait_ready);
7639 EXPORT_SYMBOL_GPL(ata_port_queue_task);
7640 EXPORT_SYMBOL_GPL(ata_scsi_ioctl);
7641 EXPORT_SYMBOL_GPL(ata_scsi_queuecmd);
7642 EXPORT_SYMBOL_GPL(ata_scsi_slave_config);
7643 EXPORT_SYMBOL_GPL(ata_scsi_slave_destroy);
7644 EXPORT_SYMBOL_GPL(ata_scsi_change_queue_depth);
7645 EXPORT_SYMBOL_GPL(ata_host_intr);
7646 EXPORT_SYMBOL_GPL(sata_scr_valid);
7647 EXPORT_SYMBOL_GPL(sata_scr_read);
7648 EXPORT_SYMBOL_GPL(sata_scr_write);
7649 EXPORT_SYMBOL_GPL(sata_scr_write_flush);
7650 EXPORT_SYMBOL_GPL(ata_link_online);
7651 EXPORT_SYMBOL_GPL(ata_link_offline);
7653 EXPORT_SYMBOL_GPL(ata_host_suspend);
7654 EXPORT_SYMBOL_GPL(ata_host_resume);
7655 #endif /* CONFIG_PM */
7656 EXPORT_SYMBOL_GPL(ata_id_string);
7657 EXPORT_SYMBOL_GPL(ata_id_c_string);
7658 EXPORT_SYMBOL_GPL(ata_id_to_dma_mode);
7659 EXPORT_SYMBOL_GPL(ata_scsi_simulate);
7661 EXPORT_SYMBOL_GPL(ata_pio_need_iordy);
7662 EXPORT_SYMBOL_GPL(ata_timing_find_mode);
7663 EXPORT_SYMBOL_GPL(ata_timing_compute);
7664 EXPORT_SYMBOL_GPL(ata_timing_merge);
7667 EXPORT_SYMBOL_GPL(pci_test_config_bits);
7668 EXPORT_SYMBOL_GPL(ata_pci_init_sff_host);
7669 EXPORT_SYMBOL_GPL(ata_pci_init_bmdma);
7670 EXPORT_SYMBOL_GPL(ata_pci_prepare_sff_host);
7671 EXPORT_SYMBOL_GPL(ata_pci_init_one);
7672 EXPORT_SYMBOL_GPL(ata_pci_remove_one);
7674 EXPORT_SYMBOL_GPL(ata_pci_device_do_suspend);
7675 EXPORT_SYMBOL_GPL(ata_pci_device_do_resume);
7676 EXPORT_SYMBOL_GPL(ata_pci_device_suspend);
7677 EXPORT_SYMBOL_GPL(ata_pci_device_resume);
7678 #endif /* CONFIG_PM */
7679 EXPORT_SYMBOL_GPL(ata_pci_default_filter);
7680 EXPORT_SYMBOL_GPL(ata_pci_clear_simplex);
7681 #endif /* CONFIG_PCI */
7683 EXPORT_SYMBOL_GPL(sata_pmp_qc_defer_cmd_switch);
7684 EXPORT_SYMBOL_GPL(sata_pmp_std_prereset);
7685 EXPORT_SYMBOL_GPL(sata_pmp_std_hardreset);
7686 EXPORT_SYMBOL_GPL(sata_pmp_std_postreset);
7687 EXPORT_SYMBOL_GPL(sata_pmp_do_eh);
7689 EXPORT_SYMBOL_GPL(__ata_ehi_push_desc);
7690 EXPORT_SYMBOL_GPL(ata_ehi_push_desc);
7691 EXPORT_SYMBOL_GPL(ata_ehi_clear_desc);
7692 EXPORT_SYMBOL_GPL(ata_port_desc);
7694 EXPORT_SYMBOL_GPL(ata_port_pbar_desc);
7695 #endif /* CONFIG_PCI */
7696 EXPORT_SYMBOL_GPL(ata_port_schedule_eh);
7697 EXPORT_SYMBOL_GPL(ata_link_abort);
7698 EXPORT_SYMBOL_GPL(ata_port_abort);
7699 EXPORT_SYMBOL_GPL(ata_port_freeze);
7700 EXPORT_SYMBOL_GPL(sata_async_notification);
7701 EXPORT_SYMBOL_GPL(ata_eh_freeze_port);
7702 EXPORT_SYMBOL_GPL(ata_eh_thaw_port);
7703 EXPORT_SYMBOL_GPL(ata_eh_qc_complete);
7704 EXPORT_SYMBOL_GPL(ata_eh_qc_retry);
7705 EXPORT_SYMBOL_GPL(ata_do_eh);
7706 EXPORT_SYMBOL_GPL(ata_irq_on);
7707 EXPORT_SYMBOL_GPL(ata_dev_try_classify);
7709 EXPORT_SYMBOL_GPL(ata_cable_40wire);
7710 EXPORT_SYMBOL_GPL(ata_cable_80wire);
7711 EXPORT_SYMBOL_GPL(ata_cable_unknown);
7712 EXPORT_SYMBOL_GPL(ata_cable_sata);