2 * libata-core.c - helper library for ATA
4 * Maintained by: Tejun Heo <tj@kernel.org>
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/driver-api/libata.rst
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/spinlock.h>
50 #include <linux/blkdev.h>
51 #include <linux/delay.h>
52 #include <linux/timer.h>
53 #include <linux/time.h>
54 #include <linux/interrupt.h>
55 #include <linux/completion.h>
56 #include <linux/suspend.h>
57 #include <linux/workqueue.h>
58 #include <linux/scatterlist.h>
60 #include <linux/log2.h>
61 #include <linux/slab.h>
62 #include <linux/glob.h>
63 #include <scsi/scsi.h>
64 #include <scsi/scsi_cmnd.h>
65 #include <scsi/scsi_host.h>
66 #include <linux/libata.h>
67 #include <asm/byteorder.h>
68 #include <asm/unaligned.h>
69 #include <linux/cdrom.h>
70 #include <linux/ratelimit.h>
71 #include <linux/leds.h>
72 #include <linux/pm_runtime.h>
73 #include <linux/platform_device.h>
75 #define CREATE_TRACE_POINTS
76 #include <trace/events/libata.h>
79 #include "libata-transport.h"
81 /* debounce timing parameters in msecs { interval, duration, timeout } */
82 const unsigned long sata_deb_timing_normal[] = { 5, 100, 2000 };
83 const unsigned long sata_deb_timing_hotplug[] = { 25, 500, 2000 };
84 const unsigned long sata_deb_timing_long[] = { 100, 2000, 5000 };
86 const struct ata_port_operations ata_base_port_ops = {
87 .prereset = ata_std_prereset,
88 .postreset = ata_std_postreset,
89 .error_handler = ata_std_error_handler,
90 .sched_eh = ata_std_sched_eh,
91 .end_eh = ata_std_end_eh,
94 const struct ata_port_operations sata_port_ops = {
95 .inherits = &ata_base_port_ops,
97 .qc_defer = ata_std_qc_defer,
98 .hardreset = sata_std_hardreset,
101 static unsigned int ata_dev_init_params(struct ata_device *dev,
102 u16 heads, u16 sectors);
103 static unsigned int ata_dev_set_xfermode(struct ata_device *dev);
104 static void ata_dev_xfermask(struct ata_device *dev);
105 static unsigned long ata_dev_blacklisted(const struct ata_device *dev);
107 atomic_t ata_print_id = ATOMIC_INIT(0);
109 struct ata_force_param {
113 unsigned long xfer_mask;
114 unsigned int horkage_on;
115 unsigned int horkage_off;
119 struct ata_force_ent {
122 struct ata_force_param param;
125 static struct ata_force_ent *ata_force_tbl;
126 static int ata_force_tbl_size;
128 static char ata_force_param_buf[PAGE_SIZE] __initdata;
129 /* param_buf is thrown away after initialization, disallow read */
130 module_param_string(force, ata_force_param_buf, sizeof(ata_force_param_buf), 0);
131 MODULE_PARM_DESC(force, "Force ATA configurations including cable type, link speed and transfer mode (see Documentation/admin-guide/kernel-parameters.rst for details)");
133 static int atapi_enabled = 1;
134 module_param(atapi_enabled, int, 0444);
135 MODULE_PARM_DESC(atapi_enabled, "Enable discovery of ATAPI devices (0=off, 1=on [default])");
137 static int atapi_dmadir = 0;
138 module_param(atapi_dmadir, int, 0444);
139 MODULE_PARM_DESC(atapi_dmadir, "Enable ATAPI DMADIR bridge support (0=off [default], 1=on)");
141 int atapi_passthru16 = 1;
142 module_param(atapi_passthru16, int, 0444);
143 MODULE_PARM_DESC(atapi_passthru16, "Enable ATA_16 passthru for ATAPI devices (0=off, 1=on [default])");
146 module_param_named(fua, libata_fua, int, 0444);
147 MODULE_PARM_DESC(fua, "FUA support (0=off [default], 1=on)");
149 static int ata_ignore_hpa;
150 module_param_named(ignore_hpa, ata_ignore_hpa, int, 0644);
151 MODULE_PARM_DESC(ignore_hpa, "Ignore HPA limit (0=keep BIOS limits, 1=ignore limits, using full disk)");
153 static int libata_dma_mask = ATA_DMA_MASK_ATA|ATA_DMA_MASK_ATAPI|ATA_DMA_MASK_CFA;
154 module_param_named(dma, libata_dma_mask, int, 0444);
155 MODULE_PARM_DESC(dma, "DMA enable/disable (0x1==ATA, 0x2==ATAPI, 0x4==CF)");
157 static int ata_probe_timeout;
158 module_param(ata_probe_timeout, int, 0444);
159 MODULE_PARM_DESC(ata_probe_timeout, "Set ATA probing timeout (seconds)");
161 int libata_noacpi = 0;
162 module_param_named(noacpi, libata_noacpi, int, 0444);
163 MODULE_PARM_DESC(noacpi, "Disable the use of ACPI in probe/suspend/resume (0=off [default], 1=on)");
165 int libata_allow_tpm = 0;
166 module_param_named(allow_tpm, libata_allow_tpm, int, 0444);
167 MODULE_PARM_DESC(allow_tpm, "Permit the use of TPM commands (0=off [default], 1=on)");
170 module_param(atapi_an, int, 0444);
171 MODULE_PARM_DESC(atapi_an, "Enable ATAPI AN media presence notification (0=0ff [default], 1=on)");
173 MODULE_AUTHOR("Jeff Garzik");
174 MODULE_DESCRIPTION("Library module for ATA devices");
175 MODULE_LICENSE("GPL");
176 MODULE_VERSION(DRV_VERSION);
179 static bool ata_sstatus_online(u32 sstatus)
181 return (sstatus & 0xf) == 0x3;
185 * ata_link_next - link iteration helper
186 * @link: the previous link, NULL to start
187 * @ap: ATA port containing links to iterate
188 * @mode: iteration mode, one of ATA_LITER_*
191 * Host lock or EH context.
194 * Pointer to the next link.
196 struct ata_link *ata_link_next(struct ata_link *link, struct ata_port *ap,
197 enum ata_link_iter_mode mode)
199 BUG_ON(mode != ATA_LITER_EDGE &&
200 mode != ATA_LITER_PMP_FIRST && mode != ATA_LITER_HOST_FIRST);
202 /* NULL link indicates start of iteration */
206 case ATA_LITER_PMP_FIRST:
207 if (sata_pmp_attached(ap))
210 case ATA_LITER_HOST_FIRST:
214 /* we just iterated over the host link, what's next? */
215 if (link == &ap->link)
217 case ATA_LITER_HOST_FIRST:
218 if (sata_pmp_attached(ap))
221 case ATA_LITER_PMP_FIRST:
222 if (unlikely(ap->slave_link))
223 return ap->slave_link;
229 /* slave_link excludes PMP */
230 if (unlikely(link == ap->slave_link))
233 /* we were over a PMP link */
234 if (++link < ap->pmp_link + ap->nr_pmp_links)
237 if (mode == ATA_LITER_PMP_FIRST)
244 * ata_dev_next - device iteration helper
245 * @dev: the previous device, NULL to start
246 * @link: ATA link containing devices to iterate
247 * @mode: iteration mode, one of ATA_DITER_*
250 * Host lock or EH context.
253 * Pointer to the next device.
255 struct ata_device *ata_dev_next(struct ata_device *dev, struct ata_link *link,
256 enum ata_dev_iter_mode mode)
258 BUG_ON(mode != ATA_DITER_ENABLED && mode != ATA_DITER_ENABLED_REVERSE &&
259 mode != ATA_DITER_ALL && mode != ATA_DITER_ALL_REVERSE);
261 /* NULL dev indicates start of iteration */
264 case ATA_DITER_ENABLED:
268 case ATA_DITER_ENABLED_REVERSE:
269 case ATA_DITER_ALL_REVERSE:
270 dev = link->device + ata_link_max_devices(link) - 1;
275 /* move to the next one */
277 case ATA_DITER_ENABLED:
279 if (++dev < link->device + ata_link_max_devices(link))
282 case ATA_DITER_ENABLED_REVERSE:
283 case ATA_DITER_ALL_REVERSE:
284 if (--dev >= link->device)
290 if ((mode == ATA_DITER_ENABLED || mode == ATA_DITER_ENABLED_REVERSE) &&
291 !ata_dev_enabled(dev))
297 * ata_dev_phys_link - find physical link for a device
298 * @dev: ATA device to look up physical link for
300 * Look up physical link which @dev is attached to. Note that
301 * this is different from @dev->link only when @dev is on slave
302 * link. For all other cases, it's the same as @dev->link.
308 * Pointer to the found physical link.
310 struct ata_link *ata_dev_phys_link(struct ata_device *dev)
312 struct ata_port *ap = dev->link->ap;
318 return ap->slave_link;
322 * ata_force_cbl - force cable type according to libata.force
323 * @ap: ATA port of interest
325 * Force cable type according to libata.force and whine about it.
326 * The last entry which has matching port number is used, so it
327 * can be specified as part of device force parameters. For
328 * example, both "a:40c,1.00:udma4" and "1.00:40c,udma4" have the
334 void ata_force_cbl(struct ata_port *ap)
338 for (i = ata_force_tbl_size - 1; i >= 0; i--) {
339 const struct ata_force_ent *fe = &ata_force_tbl[i];
341 if (fe->port != -1 && fe->port != ap->print_id)
344 if (fe->param.cbl == ATA_CBL_NONE)
347 ap->cbl = fe->param.cbl;
348 ata_port_notice(ap, "FORCE: cable set to %s\n", fe->param.name);
354 * ata_force_link_limits - force link limits according to libata.force
355 * @link: ATA link of interest
357 * Force link flags and SATA spd limit according to libata.force
358 * and whine about it. When only the port part is specified
359 * (e.g. 1:), the limit applies to all links connected to both
360 * the host link and all fan-out ports connected via PMP. If the
361 * device part is specified as 0 (e.g. 1.00:), it specifies the
362 * first fan-out link not the host link. Device number 15 always
363 * points to the host link whether PMP is attached or not. If the
364 * controller has slave link, device number 16 points to it.
369 static void ata_force_link_limits(struct ata_link *link)
371 bool did_spd = false;
372 int linkno = link->pmp;
375 if (ata_is_host_link(link))
378 for (i = ata_force_tbl_size - 1; i >= 0; i--) {
379 const struct ata_force_ent *fe = &ata_force_tbl[i];
381 if (fe->port != -1 && fe->port != link->ap->print_id)
384 if (fe->device != -1 && fe->device != linkno)
387 /* only honor the first spd limit */
388 if (!did_spd && fe->param.spd_limit) {
389 link->hw_sata_spd_limit = (1 << fe->param.spd_limit) - 1;
390 ata_link_notice(link, "FORCE: PHY spd limit set to %s\n",
395 /* let lflags stack */
396 if (fe->param.lflags) {
397 link->flags |= fe->param.lflags;
398 ata_link_notice(link,
399 "FORCE: link flag 0x%x forced -> 0x%x\n",
400 fe->param.lflags, link->flags);
406 * ata_force_xfermask - force xfermask according to libata.force
407 * @dev: ATA device of interest
409 * Force xfer_mask according to libata.force and whine about it.
410 * For consistency with link selection, device number 15 selects
411 * the first device connected to the host link.
416 static void ata_force_xfermask(struct ata_device *dev)
418 int devno = dev->link->pmp + dev->devno;
419 int alt_devno = devno;
422 /* allow n.15/16 for devices attached to host port */
423 if (ata_is_host_link(dev->link))
426 for (i = ata_force_tbl_size - 1; i >= 0; i--) {
427 const struct ata_force_ent *fe = &ata_force_tbl[i];
428 unsigned long pio_mask, mwdma_mask, udma_mask;
430 if (fe->port != -1 && fe->port != dev->link->ap->print_id)
433 if (fe->device != -1 && fe->device != devno &&
434 fe->device != alt_devno)
437 if (!fe->param.xfer_mask)
440 ata_unpack_xfermask(fe->param.xfer_mask,
441 &pio_mask, &mwdma_mask, &udma_mask);
443 dev->udma_mask = udma_mask;
444 else if (mwdma_mask) {
446 dev->mwdma_mask = mwdma_mask;
450 dev->pio_mask = pio_mask;
453 ata_dev_notice(dev, "FORCE: xfer_mask set to %s\n",
460 * ata_force_horkage - force horkage according to libata.force
461 * @dev: ATA device of interest
463 * Force horkage according to libata.force and whine about it.
464 * For consistency with link selection, device number 15 selects
465 * the first device connected to the host link.
470 static void ata_force_horkage(struct ata_device *dev)
472 int devno = dev->link->pmp + dev->devno;
473 int alt_devno = devno;
476 /* allow n.15/16 for devices attached to host port */
477 if (ata_is_host_link(dev->link))
480 for (i = 0; i < ata_force_tbl_size; i++) {
481 const struct ata_force_ent *fe = &ata_force_tbl[i];
483 if (fe->port != -1 && fe->port != dev->link->ap->print_id)
486 if (fe->device != -1 && fe->device != devno &&
487 fe->device != alt_devno)
490 if (!(~dev->horkage & fe->param.horkage_on) &&
491 !(dev->horkage & fe->param.horkage_off))
494 dev->horkage |= fe->param.horkage_on;
495 dev->horkage &= ~fe->param.horkage_off;
497 ata_dev_notice(dev, "FORCE: horkage modified (%s)\n",
503 * atapi_cmd_type - Determine ATAPI command type from SCSI opcode
504 * @opcode: SCSI opcode
506 * Determine ATAPI command type from @opcode.
512 * ATAPI_{READ|WRITE|READ_CD|PASS_THRU|MISC}
514 int atapi_cmd_type(u8 opcode)
523 case GPCMD_WRITE_AND_VERIFY_10:
527 case GPCMD_READ_CD_MSF:
528 return ATAPI_READ_CD;
532 if (atapi_passthru16)
533 return ATAPI_PASS_THRU;
541 * ata_tf_to_fis - Convert ATA taskfile to SATA FIS structure
542 * @tf: Taskfile to convert
543 * @pmp: Port multiplier port
544 * @is_cmd: This FIS is for command
545 * @fis: Buffer into which data will output
547 * Converts a standard ATA taskfile to a Serial ATA
548 * FIS structure (Register - Host to Device).
551 * Inherited from caller.
553 void ata_tf_to_fis(const struct ata_taskfile *tf, u8 pmp, int is_cmd, u8 *fis)
555 fis[0] = 0x27; /* Register - Host to Device FIS */
556 fis[1] = pmp & 0xf; /* Port multiplier number*/
558 fis[1] |= (1 << 7); /* bit 7 indicates Command FIS */
560 fis[2] = tf->command;
561 fis[3] = tf->feature;
568 fis[8] = tf->hob_lbal;
569 fis[9] = tf->hob_lbam;
570 fis[10] = tf->hob_lbah;
571 fis[11] = tf->hob_feature;
574 fis[13] = tf->hob_nsect;
578 fis[16] = tf->auxiliary & 0xff;
579 fis[17] = (tf->auxiliary >> 8) & 0xff;
580 fis[18] = (tf->auxiliary >> 16) & 0xff;
581 fis[19] = (tf->auxiliary >> 24) & 0xff;
585 * ata_tf_from_fis - Convert SATA FIS to ATA taskfile
586 * @fis: Buffer from which data will be input
587 * @tf: Taskfile to output
589 * Converts a serial ATA FIS structure to a standard ATA taskfile.
592 * Inherited from caller.
595 void ata_tf_from_fis(const u8 *fis, struct ata_taskfile *tf)
597 tf->command = fis[2]; /* status */
598 tf->feature = fis[3]; /* error */
605 tf->hob_lbal = fis[8];
606 tf->hob_lbam = fis[9];
607 tf->hob_lbah = fis[10];
610 tf->hob_nsect = fis[13];
613 static const u8 ata_rw_cmds[] = {
617 ATA_CMD_READ_MULTI_EXT,
618 ATA_CMD_WRITE_MULTI_EXT,
622 ATA_CMD_WRITE_MULTI_FUA_EXT,
626 ATA_CMD_PIO_READ_EXT,
627 ATA_CMD_PIO_WRITE_EXT,
640 ATA_CMD_WRITE_FUA_EXT
644 * ata_rwcmd_protocol - set taskfile r/w commands and protocol
645 * @tf: command to examine and configure
646 * @dev: device tf belongs to
648 * Examine the device configuration and tf->flags to calculate
649 * the proper read/write commands and protocol to use.
654 static int ata_rwcmd_protocol(struct ata_taskfile *tf, struct ata_device *dev)
658 int index, fua, lba48, write;
660 fua = (tf->flags & ATA_TFLAG_FUA) ? 4 : 0;
661 lba48 = (tf->flags & ATA_TFLAG_LBA48) ? 2 : 0;
662 write = (tf->flags & ATA_TFLAG_WRITE) ? 1 : 0;
664 if (dev->flags & ATA_DFLAG_PIO) {
665 tf->protocol = ATA_PROT_PIO;
666 index = dev->multi_count ? 0 : 8;
667 } else if (lba48 && (dev->link->ap->flags & ATA_FLAG_PIO_LBA48)) {
668 /* Unable to use DMA due to host limitation */
669 tf->protocol = ATA_PROT_PIO;
670 index = dev->multi_count ? 0 : 8;
672 tf->protocol = ATA_PROT_DMA;
676 cmd = ata_rw_cmds[index + fua + lba48 + write];
685 * ata_tf_read_block - Read block address from ATA taskfile
686 * @tf: ATA taskfile of interest
687 * @dev: ATA device @tf belongs to
692 * Read block address from @tf. This function can handle all
693 * three address formats - LBA, LBA48 and CHS. tf->protocol and
694 * flags select the address format to use.
697 * Block address read from @tf.
699 u64 ata_tf_read_block(const struct ata_taskfile *tf, struct ata_device *dev)
703 if (tf->flags & ATA_TFLAG_LBA) {
704 if (tf->flags & ATA_TFLAG_LBA48) {
705 block |= (u64)tf->hob_lbah << 40;
706 block |= (u64)tf->hob_lbam << 32;
707 block |= (u64)tf->hob_lbal << 24;
709 block |= (tf->device & 0xf) << 24;
711 block |= tf->lbah << 16;
712 block |= tf->lbam << 8;
717 cyl = tf->lbam | (tf->lbah << 8);
718 head = tf->device & 0xf;
723 "device reported invalid CHS sector 0\n");
727 block = (cyl * dev->heads + head) * dev->sectors + sect - 1;
734 * ata_build_rw_tf - Build ATA taskfile for given read/write request
735 * @tf: Target ATA taskfile
736 * @dev: ATA device @tf belongs to
737 * @block: Block address
738 * @n_block: Number of blocks
739 * @tf_flags: RW/FUA etc...
741 * @class: IO priority class
746 * Build ATA taskfile @tf for read/write request described by
747 * @block, @n_block, @tf_flags and @tag on @dev.
751 * 0 on success, -ERANGE if the request is too large for @dev,
752 * -EINVAL if the request is invalid.
754 int ata_build_rw_tf(struct ata_taskfile *tf, struct ata_device *dev,
755 u64 block, u32 n_block, unsigned int tf_flags,
756 unsigned int tag, int class)
758 tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
759 tf->flags |= tf_flags;
761 if (ata_ncq_enabled(dev) && !ata_tag_internal(tag)) {
763 if (!lba_48_ok(block, n_block))
766 tf->protocol = ATA_PROT_NCQ;
767 tf->flags |= ATA_TFLAG_LBA | ATA_TFLAG_LBA48;
769 if (tf->flags & ATA_TFLAG_WRITE)
770 tf->command = ATA_CMD_FPDMA_WRITE;
772 tf->command = ATA_CMD_FPDMA_READ;
774 tf->nsect = tag << 3;
775 tf->hob_feature = (n_block >> 8) & 0xff;
776 tf->feature = n_block & 0xff;
778 tf->hob_lbah = (block >> 40) & 0xff;
779 tf->hob_lbam = (block >> 32) & 0xff;
780 tf->hob_lbal = (block >> 24) & 0xff;
781 tf->lbah = (block >> 16) & 0xff;
782 tf->lbam = (block >> 8) & 0xff;
783 tf->lbal = block & 0xff;
785 tf->device = ATA_LBA;
786 if (tf->flags & ATA_TFLAG_FUA)
787 tf->device |= 1 << 7;
789 if (dev->flags & ATA_DFLAG_NCQ_PRIO) {
790 if (class == IOPRIO_CLASS_RT)
791 tf->hob_nsect |= ATA_PRIO_HIGH <<
794 } else if (dev->flags & ATA_DFLAG_LBA) {
795 tf->flags |= ATA_TFLAG_LBA;
797 if (lba_28_ok(block, n_block)) {
799 tf->device |= (block >> 24) & 0xf;
800 } else if (lba_48_ok(block, n_block)) {
801 if (!(dev->flags & ATA_DFLAG_LBA48))
805 tf->flags |= ATA_TFLAG_LBA48;
807 tf->hob_nsect = (n_block >> 8) & 0xff;
809 tf->hob_lbah = (block >> 40) & 0xff;
810 tf->hob_lbam = (block >> 32) & 0xff;
811 tf->hob_lbal = (block >> 24) & 0xff;
813 /* request too large even for LBA48 */
816 if (unlikely(ata_rwcmd_protocol(tf, dev) < 0))
819 tf->nsect = n_block & 0xff;
821 tf->lbah = (block >> 16) & 0xff;
822 tf->lbam = (block >> 8) & 0xff;
823 tf->lbal = block & 0xff;
825 tf->device |= ATA_LBA;
828 u32 sect, head, cyl, track;
830 /* The request -may- be too large for CHS addressing. */
831 if (!lba_28_ok(block, n_block))
834 if (unlikely(ata_rwcmd_protocol(tf, dev) < 0))
837 /* Convert LBA to CHS */
838 track = (u32)block / dev->sectors;
839 cyl = track / dev->heads;
840 head = track % dev->heads;
841 sect = (u32)block % dev->sectors + 1;
843 DPRINTK("block %u track %u cyl %u head %u sect %u\n",
844 (u32)block, track, cyl, head, sect);
846 /* Check whether the converted CHS can fit.
850 if ((cyl >> 16) || (head >> 4) || (sect >> 8) || (!sect))
853 tf->nsect = n_block & 0xff; /* Sector count 0 means 256 sectors */
864 * ata_pack_xfermask - Pack pio, mwdma and udma masks into xfer_mask
865 * @pio_mask: pio_mask
866 * @mwdma_mask: mwdma_mask
867 * @udma_mask: udma_mask
869 * Pack @pio_mask, @mwdma_mask and @udma_mask into a single
870 * unsigned int xfer_mask.
878 unsigned long ata_pack_xfermask(unsigned long pio_mask,
879 unsigned long mwdma_mask,
880 unsigned long udma_mask)
882 return ((pio_mask << ATA_SHIFT_PIO) & ATA_MASK_PIO) |
883 ((mwdma_mask << ATA_SHIFT_MWDMA) & ATA_MASK_MWDMA) |
884 ((udma_mask << ATA_SHIFT_UDMA) & ATA_MASK_UDMA);
888 * ata_unpack_xfermask - Unpack xfer_mask into pio, mwdma and udma masks
889 * @xfer_mask: xfer_mask to unpack
890 * @pio_mask: resulting pio_mask
891 * @mwdma_mask: resulting mwdma_mask
892 * @udma_mask: resulting udma_mask
894 * Unpack @xfer_mask into @pio_mask, @mwdma_mask and @udma_mask.
895 * Any NULL destination masks will be ignored.
897 void ata_unpack_xfermask(unsigned long xfer_mask, unsigned long *pio_mask,
898 unsigned long *mwdma_mask, unsigned long *udma_mask)
901 *pio_mask = (xfer_mask & ATA_MASK_PIO) >> ATA_SHIFT_PIO;
903 *mwdma_mask = (xfer_mask & ATA_MASK_MWDMA) >> ATA_SHIFT_MWDMA;
905 *udma_mask = (xfer_mask & ATA_MASK_UDMA) >> ATA_SHIFT_UDMA;
908 static const struct ata_xfer_ent {
912 { ATA_SHIFT_PIO, ATA_NR_PIO_MODES, XFER_PIO_0 },
913 { ATA_SHIFT_MWDMA, ATA_NR_MWDMA_MODES, XFER_MW_DMA_0 },
914 { ATA_SHIFT_UDMA, ATA_NR_UDMA_MODES, XFER_UDMA_0 },
919 * ata_xfer_mask2mode - Find matching XFER_* for the given xfer_mask
920 * @xfer_mask: xfer_mask of interest
922 * Return matching XFER_* value for @xfer_mask. Only the highest
923 * bit of @xfer_mask is considered.
929 * Matching XFER_* value, 0xff if no match found.
931 u8 ata_xfer_mask2mode(unsigned long xfer_mask)
933 int highbit = fls(xfer_mask) - 1;
934 const struct ata_xfer_ent *ent;
936 for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
937 if (highbit >= ent->shift && highbit < ent->shift + ent->bits)
938 return ent->base + highbit - ent->shift;
943 * ata_xfer_mode2mask - Find matching xfer_mask for XFER_*
944 * @xfer_mode: XFER_* of interest
946 * Return matching xfer_mask for @xfer_mode.
952 * Matching xfer_mask, 0 if no match found.
954 unsigned long ata_xfer_mode2mask(u8 xfer_mode)
956 const struct ata_xfer_ent *ent;
958 for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
959 if (xfer_mode >= ent->base && xfer_mode < ent->base + ent->bits)
960 return ((2 << (ent->shift + xfer_mode - ent->base)) - 1)
961 & ~((1 << ent->shift) - 1);
966 * ata_xfer_mode2shift - Find matching xfer_shift for XFER_*
967 * @xfer_mode: XFER_* of interest
969 * Return matching xfer_shift for @xfer_mode.
975 * Matching xfer_shift, -1 if no match found.
977 int ata_xfer_mode2shift(unsigned long xfer_mode)
979 const struct ata_xfer_ent *ent;
981 for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
982 if (xfer_mode >= ent->base && xfer_mode < ent->base + ent->bits)
988 * ata_mode_string - convert xfer_mask to string
989 * @xfer_mask: mask of bits supported; only highest bit counts.
991 * Determine string which represents the highest speed
992 * (highest bit in @modemask).
998 * Constant C string representing highest speed listed in
999 * @mode_mask, or the constant C string "<n/a>".
1001 const char *ata_mode_string(unsigned long xfer_mask)
1003 static const char * const xfer_mode_str[] = {
1027 highbit = fls(xfer_mask) - 1;
1028 if (highbit >= 0 && highbit < ARRAY_SIZE(xfer_mode_str))
1029 return xfer_mode_str[highbit];
1033 const char *sata_spd_string(unsigned int spd)
1035 static const char * const spd_str[] = {
1041 if (spd == 0 || (spd - 1) >= ARRAY_SIZE(spd_str))
1043 return spd_str[spd - 1];
1047 * ata_dev_classify - determine device type based on ATA-spec signature
1048 * @tf: ATA taskfile register set for device to be identified
1050 * Determine from taskfile register contents whether a device is
1051 * ATA or ATAPI, as per "Signature and persistence" section
1052 * of ATA/PI spec (volume 1, sect 5.14).
1058 * Device type, %ATA_DEV_ATA, %ATA_DEV_ATAPI, %ATA_DEV_PMP,
1059 * %ATA_DEV_ZAC, or %ATA_DEV_UNKNOWN the event of failure.
1061 unsigned int ata_dev_classify(const struct ata_taskfile *tf)
1063 /* Apple's open source Darwin code hints that some devices only
1064 * put a proper signature into the LBA mid/high registers,
1065 * So, we only check those. It's sufficient for uniqueness.
1067 * ATA/ATAPI-7 (d1532v1r1: Feb. 19, 2003) specified separate
1068 * signatures for ATA and ATAPI devices attached on SerialATA,
1069 * 0x3c/0xc3 and 0x69/0x96 respectively. However, SerialATA
1070 * spec has never mentioned about using different signatures
1071 * for ATA/ATAPI devices. Then, Serial ATA II: Port
1072 * Multiplier specification began to use 0x69/0x96 to identify
1073 * port multpliers and 0x3c/0xc3 to identify SEMB device.
1074 * ATA/ATAPI-7 dropped descriptions about 0x3c/0xc3 and
1075 * 0x69/0x96 shortly and described them as reserved for
1078 * We follow the current spec and consider that 0x69/0x96
1079 * identifies a port multiplier and 0x3c/0xc3 a SEMB device.
1080 * Unfortunately, WDC WD1600JS-62MHB5 (a hard drive) reports
1081 * SEMB signature. This is worked around in
1082 * ata_dev_read_id().
1084 if ((tf->lbam == 0) && (tf->lbah == 0)) {
1085 DPRINTK("found ATA device by sig\n");
1089 if ((tf->lbam == 0x14) && (tf->lbah == 0xeb)) {
1090 DPRINTK("found ATAPI device by sig\n");
1091 return ATA_DEV_ATAPI;
1094 if ((tf->lbam == 0x69) && (tf->lbah == 0x96)) {
1095 DPRINTK("found PMP device by sig\n");
1099 if ((tf->lbam == 0x3c) && (tf->lbah == 0xc3)) {
1100 DPRINTK("found SEMB device by sig (could be ATA device)\n");
1101 return ATA_DEV_SEMB;
1104 if ((tf->lbam == 0xcd) && (tf->lbah == 0xab)) {
1105 DPRINTK("found ZAC device by sig\n");
1109 DPRINTK("unknown device\n");
1110 return ATA_DEV_UNKNOWN;
1114 * ata_id_string - Convert IDENTIFY DEVICE page into string
1115 * @id: IDENTIFY DEVICE results we will examine
1116 * @s: string into which data is output
1117 * @ofs: offset into identify device page
1118 * @len: length of string to return. must be an even number.
1120 * The strings in the IDENTIFY DEVICE page are broken up into
1121 * 16-bit chunks. Run through the string, and output each
1122 * 8-bit chunk linearly, regardless of platform.
1128 void ata_id_string(const u16 *id, unsigned char *s,
1129 unsigned int ofs, unsigned int len)
1150 * ata_id_c_string - Convert IDENTIFY DEVICE page into C string
1151 * @id: IDENTIFY DEVICE results we will examine
1152 * @s: string into which data is output
1153 * @ofs: offset into identify device page
1154 * @len: length of string to return. must be an odd number.
1156 * This function is identical to ata_id_string except that it
1157 * trims trailing spaces and terminates the resulting string with
1158 * null. @len must be actual maximum length (even number) + 1.
1163 void ata_id_c_string(const u16 *id, unsigned char *s,
1164 unsigned int ofs, unsigned int len)
1168 ata_id_string(id, s, ofs, len - 1);
1170 p = s + strnlen(s, len - 1);
1171 while (p > s && p[-1] == ' ')
1176 static u64 ata_id_n_sectors(const u16 *id)
1178 if (ata_id_has_lba(id)) {
1179 if (ata_id_has_lba48(id))
1180 return ata_id_u64(id, ATA_ID_LBA_CAPACITY_2);
1182 return ata_id_u32(id, ATA_ID_LBA_CAPACITY);
1184 if (ata_id_current_chs_valid(id))
1185 return id[ATA_ID_CUR_CYLS] * id[ATA_ID_CUR_HEADS] *
1186 id[ATA_ID_CUR_SECTORS];
1188 return id[ATA_ID_CYLS] * id[ATA_ID_HEADS] *
1193 u64 ata_tf_to_lba48(const struct ata_taskfile *tf)
1197 sectors |= ((u64)(tf->hob_lbah & 0xff)) << 40;
1198 sectors |= ((u64)(tf->hob_lbam & 0xff)) << 32;
1199 sectors |= ((u64)(tf->hob_lbal & 0xff)) << 24;
1200 sectors |= (tf->lbah & 0xff) << 16;
1201 sectors |= (tf->lbam & 0xff) << 8;
1202 sectors |= (tf->lbal & 0xff);
1207 u64 ata_tf_to_lba(const struct ata_taskfile *tf)
1211 sectors |= (tf->device & 0x0f) << 24;
1212 sectors |= (tf->lbah & 0xff) << 16;
1213 sectors |= (tf->lbam & 0xff) << 8;
1214 sectors |= (tf->lbal & 0xff);
1220 * ata_read_native_max_address - Read native max address
1221 * @dev: target device
1222 * @max_sectors: out parameter for the result native max address
1224 * Perform an LBA48 or LBA28 native size query upon the device in
1228 * 0 on success, -EACCES if command is aborted by the drive.
1229 * -EIO on other errors.
1231 static int ata_read_native_max_address(struct ata_device *dev, u64 *max_sectors)
1233 unsigned int err_mask;
1234 struct ata_taskfile tf;
1235 int lba48 = ata_id_has_lba48(dev->id);
1237 ata_tf_init(dev, &tf);
1239 /* always clear all address registers */
1240 tf.flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_ISADDR;
1243 tf.command = ATA_CMD_READ_NATIVE_MAX_EXT;
1244 tf.flags |= ATA_TFLAG_LBA48;
1246 tf.command = ATA_CMD_READ_NATIVE_MAX;
1248 tf.protocol = ATA_PROT_NODATA;
1249 tf.device |= ATA_LBA;
1251 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
1254 "failed to read native max address (err_mask=0x%x)\n",
1256 if (err_mask == AC_ERR_DEV && (tf.feature & ATA_ABORTED))
1262 *max_sectors = ata_tf_to_lba48(&tf) + 1;
1264 *max_sectors = ata_tf_to_lba(&tf) + 1;
1265 if (dev->horkage & ATA_HORKAGE_HPA_SIZE)
1271 * ata_set_max_sectors - Set max sectors
1272 * @dev: target device
1273 * @new_sectors: new max sectors value to set for the device
1275 * Set max sectors of @dev to @new_sectors.
1278 * 0 on success, -EACCES if command is aborted or denied (due to
1279 * previous non-volatile SET_MAX) by the drive. -EIO on other
1282 static int ata_set_max_sectors(struct ata_device *dev, u64 new_sectors)
1284 unsigned int err_mask;
1285 struct ata_taskfile tf;
1286 int lba48 = ata_id_has_lba48(dev->id);
1290 ata_tf_init(dev, &tf);
1292 tf.flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_ISADDR;
1295 tf.command = ATA_CMD_SET_MAX_EXT;
1296 tf.flags |= ATA_TFLAG_LBA48;
1298 tf.hob_lbal = (new_sectors >> 24) & 0xff;
1299 tf.hob_lbam = (new_sectors >> 32) & 0xff;
1300 tf.hob_lbah = (new_sectors >> 40) & 0xff;
1302 tf.command = ATA_CMD_SET_MAX;
1304 tf.device |= (new_sectors >> 24) & 0xf;
1307 tf.protocol = ATA_PROT_NODATA;
1308 tf.device |= ATA_LBA;
1310 tf.lbal = (new_sectors >> 0) & 0xff;
1311 tf.lbam = (new_sectors >> 8) & 0xff;
1312 tf.lbah = (new_sectors >> 16) & 0xff;
1314 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
1317 "failed to set max address (err_mask=0x%x)\n",
1319 if (err_mask == AC_ERR_DEV &&
1320 (tf.feature & (ATA_ABORTED | ATA_IDNF)))
1329 * ata_hpa_resize - Resize a device with an HPA set
1330 * @dev: Device to resize
1332 * Read the size of an LBA28 or LBA48 disk with HPA features and resize
1333 * it if required to the full size of the media. The caller must check
1334 * the drive has the HPA feature set enabled.
1337 * 0 on success, -errno on failure.
1339 static int ata_hpa_resize(struct ata_device *dev)
1341 struct ata_eh_context *ehc = &dev->link->eh_context;
1342 int print_info = ehc->i.flags & ATA_EHI_PRINTINFO;
1343 bool unlock_hpa = ata_ignore_hpa || dev->flags & ATA_DFLAG_UNLOCK_HPA;
1344 u64 sectors = ata_id_n_sectors(dev->id);
1348 /* do we need to do it? */
1349 if ((dev->class != ATA_DEV_ATA && dev->class != ATA_DEV_ZAC) ||
1350 !ata_id_has_lba(dev->id) || !ata_id_hpa_enabled(dev->id) ||
1351 (dev->horkage & ATA_HORKAGE_BROKEN_HPA))
1354 /* read native max address */
1355 rc = ata_read_native_max_address(dev, &native_sectors);
1357 /* If device aborted the command or HPA isn't going to
1358 * be unlocked, skip HPA resizing.
1360 if (rc == -EACCES || !unlock_hpa) {
1362 "HPA support seems broken, skipping HPA handling\n");
1363 dev->horkage |= ATA_HORKAGE_BROKEN_HPA;
1365 /* we can continue if device aborted the command */
1372 dev->n_native_sectors = native_sectors;
1374 /* nothing to do? */
1375 if (native_sectors <= sectors || !unlock_hpa) {
1376 if (!print_info || native_sectors == sectors)
1379 if (native_sectors > sectors)
1381 "HPA detected: current %llu, native %llu\n",
1382 (unsigned long long)sectors,
1383 (unsigned long long)native_sectors);
1384 else if (native_sectors < sectors)
1386 "native sectors (%llu) is smaller than sectors (%llu)\n",
1387 (unsigned long long)native_sectors,
1388 (unsigned long long)sectors);
1392 /* let's unlock HPA */
1393 rc = ata_set_max_sectors(dev, native_sectors);
1394 if (rc == -EACCES) {
1395 /* if device aborted the command, skip HPA resizing */
1397 "device aborted resize (%llu -> %llu), skipping HPA handling\n",
1398 (unsigned long long)sectors,
1399 (unsigned long long)native_sectors);
1400 dev->horkage |= ATA_HORKAGE_BROKEN_HPA;
1405 /* re-read IDENTIFY data */
1406 rc = ata_dev_reread_id(dev, 0);
1409 "failed to re-read IDENTIFY data after HPA resizing\n");
1414 u64 new_sectors = ata_id_n_sectors(dev->id);
1416 "HPA unlocked: %llu -> %llu, native %llu\n",
1417 (unsigned long long)sectors,
1418 (unsigned long long)new_sectors,
1419 (unsigned long long)native_sectors);
1426 * ata_dump_id - IDENTIFY DEVICE info debugging output
1427 * @id: IDENTIFY DEVICE page to dump
1429 * Dump selected 16-bit words from the given IDENTIFY DEVICE
1436 static inline void ata_dump_id(const u16 *id)
1438 DPRINTK("49==0x%04x "
1448 DPRINTK("80==0x%04x "
1458 DPRINTK("88==0x%04x "
1465 * ata_id_xfermask - Compute xfermask from the given IDENTIFY data
1466 * @id: IDENTIFY data to compute xfer mask from
1468 * Compute the xfermask for this device. This is not as trivial
1469 * as it seems if we must consider early devices correctly.
1471 * FIXME: pre IDE drive timing (do we care ?).
1479 unsigned long ata_id_xfermask(const u16 *id)
1481 unsigned long pio_mask, mwdma_mask, udma_mask;
1483 /* Usual case. Word 53 indicates word 64 is valid */
1484 if (id[ATA_ID_FIELD_VALID] & (1 << 1)) {
1485 pio_mask = id[ATA_ID_PIO_MODES] & 0x03;
1489 /* If word 64 isn't valid then Word 51 high byte holds
1490 * the PIO timing number for the maximum. Turn it into
1493 u8 mode = (id[ATA_ID_OLD_PIO_MODES] >> 8) & 0xFF;
1494 if (mode < 5) /* Valid PIO range */
1495 pio_mask = (2 << mode) - 1;
1499 /* But wait.. there's more. Design your standards by
1500 * committee and you too can get a free iordy field to
1501 * process. However its the speeds not the modes that
1502 * are supported... Note drivers using the timing API
1503 * will get this right anyway
1507 mwdma_mask = id[ATA_ID_MWDMA_MODES] & 0x07;
1509 if (ata_id_is_cfa(id)) {
1511 * Process compact flash extended modes
1513 int pio = (id[ATA_ID_CFA_MODES] >> 0) & 0x7;
1514 int dma = (id[ATA_ID_CFA_MODES] >> 3) & 0x7;
1517 pio_mask |= (1 << 5);
1519 pio_mask |= (1 << 6);
1521 mwdma_mask |= (1 << 3);
1523 mwdma_mask |= (1 << 4);
1527 if (id[ATA_ID_FIELD_VALID] & (1 << 2))
1528 udma_mask = id[ATA_ID_UDMA_MODES] & 0xff;
1530 return ata_pack_xfermask(pio_mask, mwdma_mask, udma_mask);
1533 static void ata_qc_complete_internal(struct ata_queued_cmd *qc)
1535 struct completion *waiting = qc->private_data;
1541 * ata_exec_internal_sg - execute libata internal command
1542 * @dev: Device to which the command is sent
1543 * @tf: Taskfile registers for the command and the result
1544 * @cdb: CDB for packet command
1545 * @dma_dir: Data transfer direction of the command
1546 * @sgl: sg list for the data buffer of the command
1547 * @n_elem: Number of sg entries
1548 * @timeout: Timeout in msecs (0 for default)
1550 * Executes libata internal command with timeout. @tf contains
1551 * command on entry and result on return. Timeout and error
1552 * conditions are reported via return value. No recovery action
1553 * is taken after a command times out. It's caller's duty to
1554 * clean up after timeout.
1557 * None. Should be called with kernel context, might sleep.
1560 * Zero on success, AC_ERR_* mask on failure
1562 unsigned ata_exec_internal_sg(struct ata_device *dev,
1563 struct ata_taskfile *tf, const u8 *cdb,
1564 int dma_dir, struct scatterlist *sgl,
1565 unsigned int n_elem, unsigned long timeout)
1567 struct ata_link *link = dev->link;
1568 struct ata_port *ap = link->ap;
1569 u8 command = tf->command;
1570 int auto_timeout = 0;
1571 struct ata_queued_cmd *qc;
1572 unsigned int preempted_tag;
1573 u32 preempted_sactive;
1574 u64 preempted_qc_active;
1575 int preempted_nr_active_links;
1576 DECLARE_COMPLETION_ONSTACK(wait);
1577 unsigned long flags;
1578 unsigned int err_mask;
1581 spin_lock_irqsave(ap->lock, flags);
1583 /* no internal command while frozen */
1584 if (ap->pflags & ATA_PFLAG_FROZEN) {
1585 spin_unlock_irqrestore(ap->lock, flags);
1586 return AC_ERR_SYSTEM;
1589 /* initialize internal qc */
1590 qc = __ata_qc_from_tag(ap, ATA_TAG_INTERNAL);
1592 qc->tag = ATA_TAG_INTERNAL;
1599 preempted_tag = link->active_tag;
1600 preempted_sactive = link->sactive;
1601 preempted_qc_active = ap->qc_active;
1602 preempted_nr_active_links = ap->nr_active_links;
1603 link->active_tag = ATA_TAG_POISON;
1606 ap->nr_active_links = 0;
1608 /* prepare & issue qc */
1611 memcpy(qc->cdb, cdb, ATAPI_CDB_LEN);
1613 /* some SATA bridges need us to indicate data xfer direction */
1614 if (tf->protocol == ATAPI_PROT_DMA && (dev->flags & ATA_DFLAG_DMADIR) &&
1615 dma_dir == DMA_FROM_DEVICE)
1616 qc->tf.feature |= ATAPI_DMADIR;
1618 qc->flags |= ATA_QCFLAG_RESULT_TF;
1619 qc->dma_dir = dma_dir;
1620 if (dma_dir != DMA_NONE) {
1621 unsigned int i, buflen = 0;
1622 struct scatterlist *sg;
1624 for_each_sg(sgl, sg, n_elem, i)
1625 buflen += sg->length;
1627 ata_sg_init(qc, sgl, n_elem);
1628 qc->nbytes = buflen;
1631 qc->private_data = &wait;
1632 qc->complete_fn = ata_qc_complete_internal;
1636 spin_unlock_irqrestore(ap->lock, flags);
1639 if (ata_probe_timeout)
1640 timeout = ata_probe_timeout * 1000;
1642 timeout = ata_internal_cmd_timeout(dev, command);
1647 if (ap->ops->error_handler)
1650 rc = wait_for_completion_timeout(&wait, msecs_to_jiffies(timeout));
1652 if (ap->ops->error_handler)
1655 ata_sff_flush_pio_task(ap);
1658 spin_lock_irqsave(ap->lock, flags);
1660 /* We're racing with irq here. If we lose, the
1661 * following test prevents us from completing the qc
1662 * twice. If we win, the port is frozen and will be
1663 * cleaned up by ->post_internal_cmd().
1665 if (qc->flags & ATA_QCFLAG_ACTIVE) {
1666 qc->err_mask |= AC_ERR_TIMEOUT;
1668 if (ap->ops->error_handler)
1669 ata_port_freeze(ap);
1671 ata_qc_complete(qc);
1673 if (ata_msg_warn(ap))
1674 ata_dev_warn(dev, "qc timeout (cmd 0x%x)\n",
1678 spin_unlock_irqrestore(ap->lock, flags);
1681 /* do post_internal_cmd */
1682 if (ap->ops->post_internal_cmd)
1683 ap->ops->post_internal_cmd(qc);
1685 /* perform minimal error analysis */
1686 if (qc->flags & ATA_QCFLAG_FAILED) {
1687 if (qc->result_tf.command & (ATA_ERR | ATA_DF))
1688 qc->err_mask |= AC_ERR_DEV;
1691 qc->err_mask |= AC_ERR_OTHER;
1693 if (qc->err_mask & ~AC_ERR_OTHER)
1694 qc->err_mask &= ~AC_ERR_OTHER;
1695 } else if (qc->tf.command == ATA_CMD_REQ_SENSE_DATA) {
1696 qc->result_tf.command |= ATA_SENSE;
1700 spin_lock_irqsave(ap->lock, flags);
1702 *tf = qc->result_tf;
1703 err_mask = qc->err_mask;
1706 link->active_tag = preempted_tag;
1707 link->sactive = preempted_sactive;
1708 ap->qc_active = preempted_qc_active;
1709 ap->nr_active_links = preempted_nr_active_links;
1711 spin_unlock_irqrestore(ap->lock, flags);
1713 if ((err_mask & AC_ERR_TIMEOUT) && auto_timeout)
1714 ata_internal_cmd_timed_out(dev, command);
1720 * ata_exec_internal - execute libata internal command
1721 * @dev: Device to which the command is sent
1722 * @tf: Taskfile registers for the command and the result
1723 * @cdb: CDB for packet command
1724 * @dma_dir: Data transfer direction of the command
1725 * @buf: Data buffer of the command
1726 * @buflen: Length of data buffer
1727 * @timeout: Timeout in msecs (0 for default)
1729 * Wrapper around ata_exec_internal_sg() which takes simple
1730 * buffer instead of sg list.
1733 * None. Should be called with kernel context, might sleep.
1736 * Zero on success, AC_ERR_* mask on failure
1738 unsigned ata_exec_internal(struct ata_device *dev,
1739 struct ata_taskfile *tf, const u8 *cdb,
1740 int dma_dir, void *buf, unsigned int buflen,
1741 unsigned long timeout)
1743 struct scatterlist *psg = NULL, sg;
1744 unsigned int n_elem = 0;
1746 if (dma_dir != DMA_NONE) {
1748 sg_init_one(&sg, buf, buflen);
1753 return ata_exec_internal_sg(dev, tf, cdb, dma_dir, psg, n_elem,
1758 * ata_pio_need_iordy - check if iordy needed
1761 * Check if the current speed of the device requires IORDY. Used
1762 * by various controllers for chip configuration.
1764 unsigned int ata_pio_need_iordy(const struct ata_device *adev)
1766 /* Don't set IORDY if we're preparing for reset. IORDY may
1767 * lead to controller lock up on certain controllers if the
1768 * port is not occupied. See bko#11703 for details.
1770 if (adev->link->ap->pflags & ATA_PFLAG_RESETTING)
1772 /* Controller doesn't support IORDY. Probably a pointless
1773 * check as the caller should know this.
1775 if (adev->link->ap->flags & ATA_FLAG_NO_IORDY)
1777 /* CF spec. r4.1 Table 22 says no iordy on PIO5 and PIO6. */
1778 if (ata_id_is_cfa(adev->id)
1779 && (adev->pio_mode == XFER_PIO_5 || adev->pio_mode == XFER_PIO_6))
1781 /* PIO3 and higher it is mandatory */
1782 if (adev->pio_mode > XFER_PIO_2)
1784 /* We turn it on when possible */
1785 if (ata_id_has_iordy(adev->id))
1791 * ata_pio_mask_no_iordy - Return the non IORDY mask
1794 * Compute the highest mode possible if we are not using iordy. Return
1795 * -1 if no iordy mode is available.
1797 static u32 ata_pio_mask_no_iordy(const struct ata_device *adev)
1799 /* If we have no drive specific rule, then PIO 2 is non IORDY */
1800 if (adev->id[ATA_ID_FIELD_VALID] & 2) { /* EIDE */
1801 u16 pio = adev->id[ATA_ID_EIDE_PIO];
1802 /* Is the speed faster than the drive allows non IORDY ? */
1804 /* This is cycle times not frequency - watch the logic! */
1805 if (pio > 240) /* PIO2 is 240nS per cycle */
1806 return 3 << ATA_SHIFT_PIO;
1807 return 7 << ATA_SHIFT_PIO;
1810 return 3 << ATA_SHIFT_PIO;
1814 * ata_do_dev_read_id - default ID read method
1816 * @tf: proposed taskfile
1819 * Issue the identify taskfile and hand back the buffer containing
1820 * identify data. For some RAID controllers and for pre ATA devices
1821 * this function is wrapped or replaced by the driver
1823 unsigned int ata_do_dev_read_id(struct ata_device *dev,
1824 struct ata_taskfile *tf, u16 *id)
1826 return ata_exec_internal(dev, tf, NULL, DMA_FROM_DEVICE,
1827 id, sizeof(id[0]) * ATA_ID_WORDS, 0);
1831 * ata_dev_read_id - Read ID data from the specified device
1832 * @dev: target device
1833 * @p_class: pointer to class of the target device (may be changed)
1834 * @flags: ATA_READID_* flags
1835 * @id: buffer to read IDENTIFY data into
1837 * Read ID data from the specified device. ATA_CMD_ID_ATA is
1838 * performed on ATA devices and ATA_CMD_ID_ATAPI on ATAPI
1839 * devices. This function also issues ATA_CMD_INIT_DEV_PARAMS
1840 * for pre-ATA4 drives.
1842 * FIXME: ATA_CMD_ID_ATA is optional for early drives and right
1843 * now we abort if we hit that case.
1846 * Kernel thread context (may sleep)
1849 * 0 on success, -errno otherwise.
1851 int ata_dev_read_id(struct ata_device *dev, unsigned int *p_class,
1852 unsigned int flags, u16 *id)
1854 struct ata_port *ap = dev->link->ap;
1855 unsigned int class = *p_class;
1856 struct ata_taskfile tf;
1857 unsigned int err_mask = 0;
1859 bool is_semb = class == ATA_DEV_SEMB;
1860 int may_fallback = 1, tried_spinup = 0;
1863 if (ata_msg_ctl(ap))
1864 ata_dev_dbg(dev, "%s: ENTER\n", __func__);
1867 ata_tf_init(dev, &tf);
1871 class = ATA_DEV_ATA; /* some hard drives report SEMB sig */
1875 tf.command = ATA_CMD_ID_ATA;
1878 tf.command = ATA_CMD_ID_ATAPI;
1882 reason = "unsupported class";
1886 tf.protocol = ATA_PROT_PIO;
1888 /* Some devices choke if TF registers contain garbage. Make
1889 * sure those are properly initialized.
1891 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
1893 /* Device presence detection is unreliable on some
1894 * controllers. Always poll IDENTIFY if available.
1896 tf.flags |= ATA_TFLAG_POLLING;
1898 if (ap->ops->read_id)
1899 err_mask = ap->ops->read_id(dev, &tf, id);
1901 err_mask = ata_do_dev_read_id(dev, &tf, id);
1904 if (err_mask & AC_ERR_NODEV_HINT) {
1905 ata_dev_dbg(dev, "NODEV after polling detection\n");
1911 "IDENTIFY failed on device w/ SEMB sig, disabled\n");
1912 /* SEMB is not supported yet */
1913 *p_class = ATA_DEV_SEMB_UNSUP;
1917 if ((err_mask == AC_ERR_DEV) && (tf.feature & ATA_ABORTED)) {
1918 /* Device or controller might have reported
1919 * the wrong device class. Give a shot at the
1920 * other IDENTIFY if the current one is
1921 * aborted by the device.
1926 if (class == ATA_DEV_ATA)
1927 class = ATA_DEV_ATAPI;
1929 class = ATA_DEV_ATA;
1933 /* Control reaches here iff the device aborted
1934 * both flavors of IDENTIFYs which happens
1935 * sometimes with phantom devices.
1938 "both IDENTIFYs aborted, assuming NODEV\n");
1943 reason = "I/O error";
1947 if (dev->horkage & ATA_HORKAGE_DUMP_ID) {
1948 ata_dev_dbg(dev, "dumping IDENTIFY data, "
1949 "class=%d may_fallback=%d tried_spinup=%d\n",
1950 class, may_fallback, tried_spinup);
1951 print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET,
1952 16, 2, id, ATA_ID_WORDS * sizeof(*id), true);
1955 /* Falling back doesn't make sense if ID data was read
1956 * successfully at least once.
1960 swap_buf_le16(id, ATA_ID_WORDS);
1964 reason = "device reports invalid type";
1966 if (class == ATA_DEV_ATA || class == ATA_DEV_ZAC) {
1967 if (!ata_id_is_ata(id) && !ata_id_is_cfa(id))
1969 if (ap->host->flags & ATA_HOST_IGNORE_ATA &&
1970 ata_id_is_ata(id)) {
1972 "host indicates ignore ATA devices, ignored\n");
1976 if (ata_id_is_ata(id))
1980 if (!tried_spinup && (id[2] == 0x37c8 || id[2] == 0x738c)) {
1983 * Drive powered-up in standby mode, and requires a specific
1984 * SET_FEATURES spin-up subcommand before it will accept
1985 * anything other than the original IDENTIFY command.
1987 err_mask = ata_dev_set_feature(dev, SETFEATURES_SPINUP, 0);
1988 if (err_mask && id[2] != 0x738c) {
1990 reason = "SPINUP failed";
1994 * If the drive initially returned incomplete IDENTIFY info,
1995 * we now must reissue the IDENTIFY command.
1997 if (id[2] == 0x37c8)
2001 if ((flags & ATA_READID_POSTRESET) &&
2002 (class == ATA_DEV_ATA || class == ATA_DEV_ZAC)) {
2004 * The exact sequence expected by certain pre-ATA4 drives is:
2006 * IDENTIFY (optional in early ATA)
2007 * INITIALIZE DEVICE PARAMETERS (later IDE and ATA)
2009 * Some drives were very specific about that exact sequence.
2011 * Note that ATA4 says lba is mandatory so the second check
2012 * should never trigger.
2014 if (ata_id_major_version(id) < 4 || !ata_id_has_lba(id)) {
2015 err_mask = ata_dev_init_params(dev, id[3], id[6]);
2018 reason = "INIT_DEV_PARAMS failed";
2022 /* current CHS translation info (id[53-58]) might be
2023 * changed. reread the identify device info.
2025 flags &= ~ATA_READID_POSTRESET;
2035 if (ata_msg_warn(ap))
2036 ata_dev_warn(dev, "failed to IDENTIFY (%s, err_mask=0x%x)\n",
2042 * ata_read_log_page - read a specific log page
2043 * @dev: target device
2045 * @page: page to read
2046 * @buf: buffer to store read page
2047 * @sectors: number of sectors to read
2049 * Read log page using READ_LOG_EXT command.
2052 * Kernel thread context (may sleep).
2055 * 0 on success, AC_ERR_* mask otherwise.
2057 unsigned int ata_read_log_page(struct ata_device *dev, u8 log,
2058 u8 page, void *buf, unsigned int sectors)
2060 unsigned long ap_flags = dev->link->ap->flags;
2061 struct ata_taskfile tf;
2062 unsigned int err_mask;
2065 DPRINTK("read log page - log 0x%x, page 0x%x\n", log, page);
2068 * Return error without actually issuing the command on controllers
2069 * which e.g. lockup on a read log page.
2071 if (ap_flags & ATA_FLAG_NO_LOG_PAGE)
2075 ata_tf_init(dev, &tf);
2076 if (ata_dma_enabled(dev) && ata_id_has_read_log_dma_ext(dev->id) &&
2077 !(dev->horkage & ATA_HORKAGE_NO_DMA_LOG)) {
2078 tf.command = ATA_CMD_READ_LOG_DMA_EXT;
2079 tf.protocol = ATA_PROT_DMA;
2082 tf.command = ATA_CMD_READ_LOG_EXT;
2083 tf.protocol = ATA_PROT_PIO;
2089 tf.hob_nsect = sectors >> 8;
2090 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_LBA48 | ATA_TFLAG_DEVICE;
2092 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_FROM_DEVICE,
2093 buf, sectors * ATA_SECT_SIZE, 0);
2095 if (err_mask && dma) {
2096 dev->horkage |= ATA_HORKAGE_NO_DMA_LOG;
2097 ata_dev_warn(dev, "READ LOG DMA EXT failed, trying PIO\n");
2101 DPRINTK("EXIT, err_mask=%x\n", err_mask);
2105 static bool ata_log_supported(struct ata_device *dev, u8 log)
2107 struct ata_port *ap = dev->link->ap;
2109 if (ata_read_log_page(dev, ATA_LOG_DIRECTORY, 0, ap->sector_buf, 1))
2111 return get_unaligned_le16(&ap->sector_buf[log * 2]) ? true : false;
2114 static bool ata_identify_page_supported(struct ata_device *dev, u8 page)
2116 struct ata_port *ap = dev->link->ap;
2117 unsigned int err, i;
2119 if (!ata_log_supported(dev, ATA_LOG_IDENTIFY_DEVICE)) {
2120 ata_dev_warn(dev, "ATA Identify Device Log not supported\n");
2125 * Read IDENTIFY DEVICE data log, page 0, to figure out if the page is
2128 err = ata_read_log_page(dev, ATA_LOG_IDENTIFY_DEVICE, 0, ap->sector_buf,
2132 "failed to get Device Identify Log Emask 0x%x\n",
2137 for (i = 0; i < ap->sector_buf[8]; i++) {
2138 if (ap->sector_buf[9 + i] == page)
2145 static int ata_do_link_spd_horkage(struct ata_device *dev)
2147 struct ata_link *plink = ata_dev_phys_link(dev);
2148 u32 target, target_limit;
2150 if (!sata_scr_valid(plink))
2153 if (dev->horkage & ATA_HORKAGE_1_5_GBPS)
2158 target_limit = (1 << target) - 1;
2160 /* if already on stricter limit, no need to push further */
2161 if (plink->sata_spd_limit <= target_limit)
2164 plink->sata_spd_limit = target_limit;
2166 /* Request another EH round by returning -EAGAIN if link is
2167 * going faster than the target speed. Forward progress is
2168 * guaranteed by setting sata_spd_limit to target_limit above.
2170 if (plink->sata_spd > target) {
2171 ata_dev_info(dev, "applying link speed limit horkage to %s\n",
2172 sata_spd_string(target));
2178 static inline u8 ata_dev_knobble(struct ata_device *dev)
2180 struct ata_port *ap = dev->link->ap;
2182 if (ata_dev_blacklisted(dev) & ATA_HORKAGE_BRIDGE_OK)
2185 return ((ap->cbl == ATA_CBL_SATA) && (!ata_id_is_sata(dev->id)));
2188 static void ata_dev_config_ncq_send_recv(struct ata_device *dev)
2190 struct ata_port *ap = dev->link->ap;
2191 unsigned int err_mask;
2193 if (!ata_log_supported(dev, ATA_LOG_NCQ_SEND_RECV)) {
2194 ata_dev_warn(dev, "NCQ Send/Recv Log not supported\n");
2197 err_mask = ata_read_log_page(dev, ATA_LOG_NCQ_SEND_RECV,
2198 0, ap->sector_buf, 1);
2201 "failed to get NCQ Send/Recv Log Emask 0x%x\n",
2204 u8 *cmds = dev->ncq_send_recv_cmds;
2206 dev->flags |= ATA_DFLAG_NCQ_SEND_RECV;
2207 memcpy(cmds, ap->sector_buf, ATA_LOG_NCQ_SEND_RECV_SIZE);
2209 if (dev->horkage & ATA_HORKAGE_NO_NCQ_TRIM) {
2210 ata_dev_dbg(dev, "disabling queued TRIM support\n");
2211 cmds[ATA_LOG_NCQ_SEND_RECV_DSM_OFFSET] &=
2212 ~ATA_LOG_NCQ_SEND_RECV_DSM_TRIM;
2217 static void ata_dev_config_ncq_non_data(struct ata_device *dev)
2219 struct ata_port *ap = dev->link->ap;
2220 unsigned int err_mask;
2222 if (!ata_log_supported(dev, ATA_LOG_NCQ_NON_DATA)) {
2224 "NCQ Send/Recv Log not supported\n");
2227 err_mask = ata_read_log_page(dev, ATA_LOG_NCQ_NON_DATA,
2228 0, ap->sector_buf, 1);
2231 "failed to get NCQ Non-Data Log Emask 0x%x\n",
2234 u8 *cmds = dev->ncq_non_data_cmds;
2236 memcpy(cmds, ap->sector_buf, ATA_LOG_NCQ_NON_DATA_SIZE);
2240 static void ata_dev_config_ncq_prio(struct ata_device *dev)
2242 struct ata_port *ap = dev->link->ap;
2243 unsigned int err_mask;
2245 if (!(dev->flags & ATA_DFLAG_NCQ_PRIO_ENABLE)) {
2246 dev->flags &= ~ATA_DFLAG_NCQ_PRIO;
2250 err_mask = ata_read_log_page(dev,
2251 ATA_LOG_IDENTIFY_DEVICE,
2252 ATA_LOG_SATA_SETTINGS,
2257 "failed to get Identify Device data, Emask 0x%x\n",
2262 if (ap->sector_buf[ATA_LOG_NCQ_PRIO_OFFSET] & BIT(3)) {
2263 dev->flags |= ATA_DFLAG_NCQ_PRIO;
2265 dev->flags &= ~ATA_DFLAG_NCQ_PRIO;
2266 ata_dev_dbg(dev, "SATA page does not support priority\n");
2271 static bool ata_dev_check_adapter(struct ata_device *dev,
2272 unsigned short vendor_id)
2274 struct pci_dev *pcidev = NULL;
2275 struct device *parent_dev = NULL;
2277 for (parent_dev = dev->tdev.parent; parent_dev != NULL;
2278 parent_dev = parent_dev->parent) {
2279 if (dev_is_pci(parent_dev)) {
2280 pcidev = to_pci_dev(parent_dev);
2281 if (pcidev->vendor == vendor_id)
2290 static int ata_dev_config_ncq(struct ata_device *dev,
2291 char *desc, size_t desc_sz)
2293 struct ata_port *ap = dev->link->ap;
2294 int hdepth = 0, ddepth = ata_id_queue_depth(dev->id);
2295 unsigned int err_mask;
2298 if (!ata_id_has_ncq(dev->id)) {
2302 if (dev->horkage & ATA_HORKAGE_NONCQ) {
2303 snprintf(desc, desc_sz, "NCQ (not used)");
2307 if (dev->horkage & ATA_HORKAGE_NO_NCQ_ON_ATI &&
2308 ata_dev_check_adapter(dev, PCI_VENDOR_ID_ATI)) {
2309 snprintf(desc, desc_sz, "NCQ (not used)");
2313 if (ap->flags & ATA_FLAG_NCQ) {
2314 hdepth = min(ap->scsi_host->can_queue, ATA_MAX_QUEUE);
2315 dev->flags |= ATA_DFLAG_NCQ;
2318 if (!(dev->horkage & ATA_HORKAGE_BROKEN_FPDMA_AA) &&
2319 (ap->flags & ATA_FLAG_FPDMA_AA) &&
2320 ata_id_has_fpdma_aa(dev->id)) {
2321 err_mask = ata_dev_set_feature(dev, SETFEATURES_SATA_ENABLE,
2325 "failed to enable AA (error_mask=0x%x)\n",
2327 if (err_mask != AC_ERR_DEV) {
2328 dev->horkage |= ATA_HORKAGE_BROKEN_FPDMA_AA;
2335 if (hdepth >= ddepth)
2336 snprintf(desc, desc_sz, "NCQ (depth %d)%s", ddepth, aa_desc);
2338 snprintf(desc, desc_sz, "NCQ (depth %d/%d)%s", hdepth,
2341 if ((ap->flags & ATA_FLAG_FPDMA_AUX)) {
2342 if (ata_id_has_ncq_send_and_recv(dev->id))
2343 ata_dev_config_ncq_send_recv(dev);
2344 if (ata_id_has_ncq_non_data(dev->id))
2345 ata_dev_config_ncq_non_data(dev);
2346 if (ata_id_has_ncq_prio(dev->id))
2347 ata_dev_config_ncq_prio(dev);
2353 static void ata_dev_config_sense_reporting(struct ata_device *dev)
2355 unsigned int err_mask;
2357 if (!ata_id_has_sense_reporting(dev->id))
2360 if (ata_id_sense_reporting_enabled(dev->id))
2363 err_mask = ata_dev_set_feature(dev, SETFEATURE_SENSE_DATA, 0x1);
2366 "failed to enable Sense Data Reporting, Emask 0x%x\n",
2371 static void ata_dev_config_zac(struct ata_device *dev)
2373 struct ata_port *ap = dev->link->ap;
2374 unsigned int err_mask;
2375 u8 *identify_buf = ap->sector_buf;
2377 dev->zac_zones_optimal_open = U32_MAX;
2378 dev->zac_zones_optimal_nonseq = U32_MAX;
2379 dev->zac_zones_max_open = U32_MAX;
2382 * Always set the 'ZAC' flag for Host-managed devices.
2384 if (dev->class == ATA_DEV_ZAC)
2385 dev->flags |= ATA_DFLAG_ZAC;
2386 else if (ata_id_zoned_cap(dev->id) == 0x01)
2388 * Check for host-aware devices.
2390 dev->flags |= ATA_DFLAG_ZAC;
2392 if (!(dev->flags & ATA_DFLAG_ZAC))
2395 if (!ata_identify_page_supported(dev, ATA_LOG_ZONED_INFORMATION)) {
2397 "ATA Zoned Information Log not supported\n");
2402 * Read IDENTIFY DEVICE data log, page 9 (Zoned-device information)
2404 err_mask = ata_read_log_page(dev, ATA_LOG_IDENTIFY_DEVICE,
2405 ATA_LOG_ZONED_INFORMATION,
2408 u64 zoned_cap, opt_open, opt_nonseq, max_open;
2410 zoned_cap = get_unaligned_le64(&identify_buf[8]);
2411 if ((zoned_cap >> 63))
2412 dev->zac_zoned_cap = (zoned_cap & 1);
2413 opt_open = get_unaligned_le64(&identify_buf[24]);
2414 if ((opt_open >> 63))
2415 dev->zac_zones_optimal_open = (u32)opt_open;
2416 opt_nonseq = get_unaligned_le64(&identify_buf[32]);
2417 if ((opt_nonseq >> 63))
2418 dev->zac_zones_optimal_nonseq = (u32)opt_nonseq;
2419 max_open = get_unaligned_le64(&identify_buf[40]);
2420 if ((max_open >> 63))
2421 dev->zac_zones_max_open = (u32)max_open;
2425 static void ata_dev_config_trusted(struct ata_device *dev)
2427 struct ata_port *ap = dev->link->ap;
2431 if (!ata_id_has_trusted(dev->id))
2434 if (!ata_identify_page_supported(dev, ATA_LOG_SECURITY)) {
2436 "Security Log not supported\n");
2440 err = ata_read_log_page(dev, ATA_LOG_IDENTIFY_DEVICE, ATA_LOG_SECURITY,
2444 "failed to read Security Log, Emask 0x%x\n", err);
2448 trusted_cap = get_unaligned_le64(&ap->sector_buf[40]);
2449 if (!(trusted_cap & (1ULL << 63))) {
2451 "Trusted Computing capability qword not valid!\n");
2455 if (trusted_cap & (1 << 0))
2456 dev->flags |= ATA_DFLAG_TRUSTED;
2460 * ata_dev_configure - Configure the specified ATA/ATAPI device
2461 * @dev: Target device to configure
2463 * Configure @dev according to @dev->id. Generic and low-level
2464 * driver specific fixups are also applied.
2467 * Kernel thread context (may sleep)
2470 * 0 on success, -errno otherwise
2472 int ata_dev_configure(struct ata_device *dev)
2474 struct ata_port *ap = dev->link->ap;
2475 struct ata_eh_context *ehc = &dev->link->eh_context;
2476 int print_info = ehc->i.flags & ATA_EHI_PRINTINFO;
2477 const u16 *id = dev->id;
2478 unsigned long xfer_mask;
2479 unsigned int err_mask;
2480 char revbuf[7]; /* XYZ-99\0 */
2481 char fwrevbuf[ATA_ID_FW_REV_LEN+1];
2482 char modelbuf[ATA_ID_PROD_LEN+1];
2485 if (!ata_dev_enabled(dev) && ata_msg_info(ap)) {
2486 ata_dev_info(dev, "%s: ENTER/EXIT -- nodev\n", __func__);
2490 if (ata_msg_probe(ap))
2491 ata_dev_dbg(dev, "%s: ENTER\n", __func__);
2494 dev->horkage |= ata_dev_blacklisted(dev);
2495 ata_force_horkage(dev);
2497 if (dev->horkage & ATA_HORKAGE_DISABLE) {
2498 ata_dev_info(dev, "unsupported device, disabling\n");
2499 ata_dev_disable(dev);
2503 if ((!atapi_enabled || (ap->flags & ATA_FLAG_NO_ATAPI)) &&
2504 dev->class == ATA_DEV_ATAPI) {
2505 ata_dev_warn(dev, "WARNING: ATAPI is %s, device ignored\n",
2506 atapi_enabled ? "not supported with this driver"
2508 ata_dev_disable(dev);
2512 rc = ata_do_link_spd_horkage(dev);
2516 /* some WD SATA-1 drives have issues with LPM, turn on NOLPM for them */
2517 if ((dev->horkage & ATA_HORKAGE_WD_BROKEN_LPM) &&
2518 (id[ATA_ID_SATA_CAPABILITY] & 0xe) == 0x2)
2519 dev->horkage |= ATA_HORKAGE_NOLPM;
2521 if (ap->flags & ATA_FLAG_NO_LPM)
2522 dev->horkage |= ATA_HORKAGE_NOLPM;
2524 if (dev->horkage & ATA_HORKAGE_NOLPM) {
2525 ata_dev_warn(dev, "LPM support broken, forcing max_power\n");
2526 dev->link->ap->target_lpm_policy = ATA_LPM_MAX_POWER;
2529 /* let ACPI work its magic */
2530 rc = ata_acpi_on_devcfg(dev);
2534 /* massage HPA, do it early as it might change IDENTIFY data */
2535 rc = ata_hpa_resize(dev);
2539 /* print device capabilities */
2540 if (ata_msg_probe(ap))
2542 "%s: cfg 49:%04x 82:%04x 83:%04x 84:%04x "
2543 "85:%04x 86:%04x 87:%04x 88:%04x\n",
2545 id[49], id[82], id[83], id[84],
2546 id[85], id[86], id[87], id[88]);
2548 /* initialize to-be-configured parameters */
2549 dev->flags &= ~ATA_DFLAG_CFG_MASK;
2550 dev->max_sectors = 0;
2556 dev->multi_count = 0;
2559 * common ATA, ATAPI feature tests
2562 /* find max transfer mode; for printk only */
2563 xfer_mask = ata_id_xfermask(id);
2565 if (ata_msg_probe(ap))
2568 /* SCSI only uses 4-char revisions, dump full 8 chars from ATA */
2569 ata_id_c_string(dev->id, fwrevbuf, ATA_ID_FW_REV,
2572 ata_id_c_string(dev->id, modelbuf, ATA_ID_PROD,
2575 /* ATA-specific feature tests */
2576 if (dev->class == ATA_DEV_ATA || dev->class == ATA_DEV_ZAC) {
2577 if (ata_id_is_cfa(id)) {
2578 /* CPRM may make this media unusable */
2579 if (id[ATA_ID_CFA_KEY_MGMT] & 1)
2581 "supports DRM functions and may not be fully accessible\n");
2582 snprintf(revbuf, 7, "CFA");
2584 snprintf(revbuf, 7, "ATA-%d", ata_id_major_version(id));
2585 /* Warn the user if the device has TPM extensions */
2586 if (ata_id_has_tpm(id))
2588 "supports DRM functions and may not be fully accessible\n");
2591 dev->n_sectors = ata_id_n_sectors(id);
2593 /* get current R/W Multiple count setting */
2594 if ((dev->id[47] >> 8) == 0x80 && (dev->id[59] & 0x100)) {
2595 unsigned int max = dev->id[47] & 0xff;
2596 unsigned int cnt = dev->id[59] & 0xff;
2597 /* only recognize/allow powers of two here */
2598 if (is_power_of_2(max) && is_power_of_2(cnt))
2600 dev->multi_count = cnt;
2603 if (ata_id_has_lba(id)) {
2604 const char *lba_desc;
2608 dev->flags |= ATA_DFLAG_LBA;
2609 if (ata_id_has_lba48(id)) {
2610 dev->flags |= ATA_DFLAG_LBA48;
2613 if (dev->n_sectors >= (1UL << 28) &&
2614 ata_id_has_flush_ext(id))
2615 dev->flags |= ATA_DFLAG_FLUSH_EXT;
2619 rc = ata_dev_config_ncq(dev, ncq_desc, sizeof(ncq_desc));
2623 /* print device info to dmesg */
2624 if (ata_msg_drv(ap) && print_info) {
2625 ata_dev_info(dev, "%s: %s, %s, max %s\n",
2626 revbuf, modelbuf, fwrevbuf,
2627 ata_mode_string(xfer_mask));
2629 "%llu sectors, multi %u: %s %s\n",
2630 (unsigned long long)dev->n_sectors,
2631 dev->multi_count, lba_desc, ncq_desc);
2636 /* Default translation */
2637 dev->cylinders = id[1];
2639 dev->sectors = id[6];
2641 if (ata_id_current_chs_valid(id)) {
2642 /* Current CHS translation is valid. */
2643 dev->cylinders = id[54];
2644 dev->heads = id[55];
2645 dev->sectors = id[56];
2648 /* print device info to dmesg */
2649 if (ata_msg_drv(ap) && print_info) {
2650 ata_dev_info(dev, "%s: %s, %s, max %s\n",
2651 revbuf, modelbuf, fwrevbuf,
2652 ata_mode_string(xfer_mask));
2654 "%llu sectors, multi %u, CHS %u/%u/%u\n",
2655 (unsigned long long)dev->n_sectors,
2656 dev->multi_count, dev->cylinders,
2657 dev->heads, dev->sectors);
2661 /* Check and mark DevSlp capability. Get DevSlp timing variables
2662 * from SATA Settings page of Identify Device Data Log.
2664 if (ata_id_has_devslp(dev->id)) {
2665 u8 *sata_setting = ap->sector_buf;
2668 dev->flags |= ATA_DFLAG_DEVSLP;
2669 err_mask = ata_read_log_page(dev,
2670 ATA_LOG_IDENTIFY_DEVICE,
2671 ATA_LOG_SATA_SETTINGS,
2676 "failed to get Identify Device Data, Emask 0x%x\n",
2679 for (i = 0; i < ATA_LOG_DEVSLP_SIZE; i++) {
2680 j = ATA_LOG_DEVSLP_OFFSET + i;
2681 dev->devslp_timing[i] = sata_setting[j];
2684 ata_dev_config_sense_reporting(dev);
2685 ata_dev_config_zac(dev);
2686 ata_dev_config_trusted(dev);
2690 /* ATAPI-specific feature tests */
2691 else if (dev->class == ATA_DEV_ATAPI) {
2692 const char *cdb_intr_string = "";
2693 const char *atapi_an_string = "";
2694 const char *dma_dir_string = "";
2697 rc = atapi_cdb_len(id);
2698 if ((rc < 12) || (rc > ATAPI_CDB_LEN)) {
2699 if (ata_msg_warn(ap))
2700 ata_dev_warn(dev, "unsupported CDB len\n");
2704 dev->cdb_len = (unsigned int) rc;
2706 /* Enable ATAPI AN if both the host and device have
2707 * the support. If PMP is attached, SNTF is required
2708 * to enable ATAPI AN to discern between PHY status
2709 * changed notifications and ATAPI ANs.
2712 (ap->flags & ATA_FLAG_AN) && ata_id_has_atapi_AN(id) &&
2713 (!sata_pmp_attached(ap) ||
2714 sata_scr_read(&ap->link, SCR_NOTIFICATION, &sntf) == 0)) {
2715 /* issue SET feature command to turn this on */
2716 err_mask = ata_dev_set_feature(dev,
2717 SETFEATURES_SATA_ENABLE, SATA_AN);
2720 "failed to enable ATAPI AN (err_mask=0x%x)\n",
2723 dev->flags |= ATA_DFLAG_AN;
2724 atapi_an_string = ", ATAPI AN";
2728 if (ata_id_cdb_intr(dev->id)) {
2729 dev->flags |= ATA_DFLAG_CDB_INTR;
2730 cdb_intr_string = ", CDB intr";
2733 if (atapi_dmadir || (dev->horkage & ATA_HORKAGE_ATAPI_DMADIR) || atapi_id_dmadir(dev->id)) {
2734 dev->flags |= ATA_DFLAG_DMADIR;
2735 dma_dir_string = ", DMADIR";
2738 if (ata_id_has_da(dev->id)) {
2739 dev->flags |= ATA_DFLAG_DA;
2743 /* print device info to dmesg */
2744 if (ata_msg_drv(ap) && print_info)
2746 "ATAPI: %s, %s, max %s%s%s%s\n",
2748 ata_mode_string(xfer_mask),
2749 cdb_intr_string, atapi_an_string,
2753 /* determine max_sectors */
2754 dev->max_sectors = ATA_MAX_SECTORS;
2755 if (dev->flags & ATA_DFLAG_LBA48)
2756 dev->max_sectors = ATA_MAX_SECTORS_LBA48;
2758 /* Limit PATA drive on SATA cable bridge transfers to udma5,
2760 if (ata_dev_knobble(dev)) {
2761 if (ata_msg_drv(ap) && print_info)
2762 ata_dev_info(dev, "applying bridge limits\n");
2763 dev->udma_mask &= ATA_UDMA5;
2764 dev->max_sectors = ATA_MAX_SECTORS;
2767 if ((dev->class == ATA_DEV_ATAPI) &&
2768 (atapi_command_packet_set(id) == TYPE_TAPE)) {
2769 dev->max_sectors = ATA_MAX_SECTORS_TAPE;
2770 dev->horkage |= ATA_HORKAGE_STUCK_ERR;
2773 if (dev->horkage & ATA_HORKAGE_MAX_SEC_128)
2774 dev->max_sectors = min_t(unsigned int, ATA_MAX_SECTORS_128,
2777 if (dev->horkage & ATA_HORKAGE_MAX_SEC_1024)
2778 dev->max_sectors = min_t(unsigned int, ATA_MAX_SECTORS_1024,
2781 if (dev->horkage & ATA_HORKAGE_MAX_SEC_LBA48)
2782 dev->max_sectors = ATA_MAX_SECTORS_LBA48;
2784 if (ap->ops->dev_config)
2785 ap->ops->dev_config(dev);
2787 if (dev->horkage & ATA_HORKAGE_DIAGNOSTIC) {
2788 /* Let the user know. We don't want to disallow opens for
2789 rescue purposes, or in case the vendor is just a blithering
2790 idiot. Do this after the dev_config call as some controllers
2791 with buggy firmware may want to avoid reporting false device
2796 "Drive reports diagnostics failure. This may indicate a drive\n");
2798 "fault or invalid emulation. Contact drive vendor for information.\n");
2802 if ((dev->horkage & ATA_HORKAGE_FIRMWARE_WARN) && print_info) {
2803 ata_dev_warn(dev, "WARNING: device requires firmware update to be fully functional\n");
2804 ata_dev_warn(dev, " contact the vendor or visit http://ata.wiki.kernel.org\n");
2810 if (ata_msg_probe(ap))
2811 ata_dev_dbg(dev, "%s: EXIT, err\n", __func__);
2816 * ata_cable_40wire - return 40 wire cable type
2819 * Helper method for drivers which want to hardwire 40 wire cable
2823 int ata_cable_40wire(struct ata_port *ap)
2825 return ATA_CBL_PATA40;
2829 * ata_cable_80wire - return 80 wire cable type
2832 * Helper method for drivers which want to hardwire 80 wire cable
2836 int ata_cable_80wire(struct ata_port *ap)
2838 return ATA_CBL_PATA80;
2842 * ata_cable_unknown - return unknown PATA cable.
2845 * Helper method for drivers which have no PATA cable detection.
2848 int ata_cable_unknown(struct ata_port *ap)
2850 return ATA_CBL_PATA_UNK;
2854 * ata_cable_ignore - return ignored PATA cable.
2857 * Helper method for drivers which don't use cable type to limit
2860 int ata_cable_ignore(struct ata_port *ap)
2862 return ATA_CBL_PATA_IGN;
2866 * ata_cable_sata - return SATA cable type
2869 * Helper method for drivers which have SATA cables
2872 int ata_cable_sata(struct ata_port *ap)
2874 return ATA_CBL_SATA;
2878 * ata_bus_probe - Reset and probe ATA bus
2881 * Master ATA bus probing function. Initiates a hardware-dependent
2882 * bus reset, then attempts to identify any devices found on
2886 * PCI/etc. bus probe sem.
2889 * Zero on success, negative errno otherwise.
2892 int ata_bus_probe(struct ata_port *ap)
2894 unsigned int classes[ATA_MAX_DEVICES];
2895 int tries[ATA_MAX_DEVICES];
2897 struct ata_device *dev;
2899 ata_for_each_dev(dev, &ap->link, ALL)
2900 tries[dev->devno] = ATA_PROBE_MAX_TRIES;
2903 ata_for_each_dev(dev, &ap->link, ALL) {
2904 /* If we issue an SRST then an ATA drive (not ATAPI)
2905 * may change configuration and be in PIO0 timing. If
2906 * we do a hard reset (or are coming from power on)
2907 * this is true for ATA or ATAPI. Until we've set a
2908 * suitable controller mode we should not touch the
2909 * bus as we may be talking too fast.
2911 dev->pio_mode = XFER_PIO_0;
2912 dev->dma_mode = 0xff;
2914 /* If the controller has a pio mode setup function
2915 * then use it to set the chipset to rights. Don't
2916 * touch the DMA setup as that will be dealt with when
2917 * configuring devices.
2919 if (ap->ops->set_piomode)
2920 ap->ops->set_piomode(ap, dev);
2923 /* reset and determine device classes */
2924 ap->ops->phy_reset(ap);
2926 ata_for_each_dev(dev, &ap->link, ALL) {
2927 if (dev->class != ATA_DEV_UNKNOWN)
2928 classes[dev->devno] = dev->class;
2930 classes[dev->devno] = ATA_DEV_NONE;
2932 dev->class = ATA_DEV_UNKNOWN;
2935 /* read IDENTIFY page and configure devices. We have to do the identify
2936 specific sequence bass-ackwards so that PDIAG- is released by
2939 ata_for_each_dev(dev, &ap->link, ALL_REVERSE) {
2940 if (tries[dev->devno])
2941 dev->class = classes[dev->devno];
2943 if (!ata_dev_enabled(dev))
2946 rc = ata_dev_read_id(dev, &dev->class, ATA_READID_POSTRESET,
2952 /* Now ask for the cable type as PDIAG- should have been released */
2953 if (ap->ops->cable_detect)
2954 ap->cbl = ap->ops->cable_detect(ap);
2956 /* We may have SATA bridge glue hiding here irrespective of
2957 * the reported cable types and sensed types. When SATA
2958 * drives indicate we have a bridge, we don't know which end
2959 * of the link the bridge is which is a problem.
2961 ata_for_each_dev(dev, &ap->link, ENABLED)
2962 if (ata_id_is_sata(dev->id))
2963 ap->cbl = ATA_CBL_SATA;
2965 /* After the identify sequence we can now set up the devices. We do
2966 this in the normal order so that the user doesn't get confused */
2968 ata_for_each_dev(dev, &ap->link, ENABLED) {
2969 ap->link.eh_context.i.flags |= ATA_EHI_PRINTINFO;
2970 rc = ata_dev_configure(dev);
2971 ap->link.eh_context.i.flags &= ~ATA_EHI_PRINTINFO;
2976 /* configure transfer mode */
2977 rc = ata_set_mode(&ap->link, &dev);
2981 ata_for_each_dev(dev, &ap->link, ENABLED)
2987 tries[dev->devno]--;
2991 /* eeek, something went very wrong, give up */
2992 tries[dev->devno] = 0;
2996 /* give it just one more chance */
2997 tries[dev->devno] = min(tries[dev->devno], 1);
3000 if (tries[dev->devno] == 1) {
3001 /* This is the last chance, better to slow
3002 * down than lose it.
3004 sata_down_spd_limit(&ap->link, 0);
3005 ata_down_xfermask_limit(dev, ATA_DNXFER_PIO);
3009 if (!tries[dev->devno])
3010 ata_dev_disable(dev);
3016 * sata_print_link_status - Print SATA link status
3017 * @link: SATA link to printk link status about
3019 * This function prints link speed and status of a SATA link.
3024 static void sata_print_link_status(struct ata_link *link)
3026 u32 sstatus, scontrol, tmp;
3028 if (sata_scr_read(link, SCR_STATUS, &sstatus))
3030 sata_scr_read(link, SCR_CONTROL, &scontrol);
3032 if (ata_phys_link_online(link)) {
3033 tmp = (sstatus >> 4) & 0xf;
3034 ata_link_info(link, "SATA link up %s (SStatus %X SControl %X)\n",
3035 sata_spd_string(tmp), sstatus, scontrol);
3037 ata_link_info(link, "SATA link down (SStatus %X SControl %X)\n",
3043 * ata_dev_pair - return other device on cable
3046 * Obtain the other device on the same cable, or if none is
3047 * present NULL is returned
3050 struct ata_device *ata_dev_pair(struct ata_device *adev)
3052 struct ata_link *link = adev->link;
3053 struct ata_device *pair = &link->device[1 - adev->devno];
3054 if (!ata_dev_enabled(pair))
3060 * sata_down_spd_limit - adjust SATA spd limit downward
3061 * @link: Link to adjust SATA spd limit for
3062 * @spd_limit: Additional limit
3064 * Adjust SATA spd limit of @link downward. Note that this
3065 * function only adjusts the limit. The change must be applied
3066 * using sata_set_spd().
3068 * If @spd_limit is non-zero, the speed is limited to equal to or
3069 * lower than @spd_limit if such speed is supported. If
3070 * @spd_limit is slower than any supported speed, only the lowest
3071 * supported speed is allowed.
3074 * Inherited from caller.
3077 * 0 on success, negative errno on failure
3079 int sata_down_spd_limit(struct ata_link *link, u32 spd_limit)
3081 u32 sstatus, spd, mask;
3084 if (!sata_scr_valid(link))
3087 /* If SCR can be read, use it to determine the current SPD.
3088 * If not, use cached value in link->sata_spd.
3090 rc = sata_scr_read(link, SCR_STATUS, &sstatus);
3091 if (rc == 0 && ata_sstatus_online(sstatus))
3092 spd = (sstatus >> 4) & 0xf;
3094 spd = link->sata_spd;
3096 mask = link->sata_spd_limit;
3100 /* unconditionally mask off the highest bit */
3101 bit = fls(mask) - 1;
3102 mask &= ~(1 << bit);
3105 * Mask off all speeds higher than or equal to the current one. At
3106 * this point, if current SPD is not available and we previously
3107 * recorded the link speed from SStatus, the driver has already
3108 * masked off the highest bit so mask should already be 1 or 0.
3109 * Otherwise, we should not force 1.5Gbps on a link where we have
3110 * not previously recorded speed from SStatus. Just return in this
3114 mask &= (1 << (spd - 1)) - 1;
3118 /* were we already at the bottom? */
3123 if (mask & ((1 << spd_limit) - 1))
3124 mask &= (1 << spd_limit) - 1;
3126 bit = ffs(mask) - 1;
3131 link->sata_spd_limit = mask;
3133 ata_link_warn(link, "limiting SATA link speed to %s\n",
3134 sata_spd_string(fls(mask)));
3139 static int __sata_set_spd_needed(struct ata_link *link, u32 *scontrol)
3141 struct ata_link *host_link = &link->ap->link;
3142 u32 limit, target, spd;
3144 limit = link->sata_spd_limit;
3146 /* Don't configure downstream link faster than upstream link.
3147 * It doesn't speed up anything and some PMPs choke on such
3150 if (!ata_is_host_link(link) && host_link->sata_spd)
3151 limit &= (1 << host_link->sata_spd) - 1;
3153 if (limit == UINT_MAX)
3156 target = fls(limit);
3158 spd = (*scontrol >> 4) & 0xf;
3159 *scontrol = (*scontrol & ~0xf0) | ((target & 0xf) << 4);
3161 return spd != target;
3165 * sata_set_spd_needed - is SATA spd configuration needed
3166 * @link: Link in question
3168 * Test whether the spd limit in SControl matches
3169 * @link->sata_spd_limit. This function is used to determine
3170 * whether hardreset is necessary to apply SATA spd
3174 * Inherited from caller.
3177 * 1 if SATA spd configuration is needed, 0 otherwise.
3179 static int sata_set_spd_needed(struct ata_link *link)
3183 if (sata_scr_read(link, SCR_CONTROL, &scontrol))
3186 return __sata_set_spd_needed(link, &scontrol);
3190 * sata_set_spd - set SATA spd according to spd limit
3191 * @link: Link to set SATA spd for
3193 * Set SATA spd of @link according to sata_spd_limit.
3196 * Inherited from caller.
3199 * 0 if spd doesn't need to be changed, 1 if spd has been
3200 * changed. Negative errno if SCR registers are inaccessible.
3202 int sata_set_spd(struct ata_link *link)
3207 if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
3210 if (!__sata_set_spd_needed(link, &scontrol))
3213 if ((rc = sata_scr_write(link, SCR_CONTROL, scontrol)))
3220 * This mode timing computation functionality is ported over from
3221 * drivers/ide/ide-timing.h and was originally written by Vojtech Pavlik
3224 * PIO 0-4, MWDMA 0-2 and UDMA 0-6 timings (in nanoseconds).
3225 * These were taken from ATA/ATAPI-6 standard, rev 0a, except
3226 * for UDMA6, which is currently supported only by Maxtor drives.
3228 * For PIO 5/6 MWDMA 3/4 see the CFA specification 3.0.
3231 static const struct ata_timing ata_timing[] = {
3232 /* { XFER_PIO_SLOW, 120, 290, 240, 960, 290, 240, 0, 960, 0 }, */
3233 { XFER_PIO_0, 70, 290, 240, 600, 165, 150, 0, 600, 0 },
3234 { XFER_PIO_1, 50, 290, 93, 383, 125, 100, 0, 383, 0 },
3235 { XFER_PIO_2, 30, 290, 40, 330, 100, 90, 0, 240, 0 },
3236 { XFER_PIO_3, 30, 80, 70, 180, 80, 70, 0, 180, 0 },
3237 { XFER_PIO_4, 25, 70, 25, 120, 70, 25, 0, 120, 0 },
3238 { XFER_PIO_5, 15, 65, 25, 100, 65, 25, 0, 100, 0 },
3239 { XFER_PIO_6, 10, 55, 20, 80, 55, 20, 0, 80, 0 },
3241 { XFER_SW_DMA_0, 120, 0, 0, 0, 480, 480, 50, 960, 0 },
3242 { XFER_SW_DMA_1, 90, 0, 0, 0, 240, 240, 30, 480, 0 },
3243 { XFER_SW_DMA_2, 60, 0, 0, 0, 120, 120, 20, 240, 0 },
3245 { XFER_MW_DMA_0, 60, 0, 0, 0, 215, 215, 20, 480, 0 },
3246 { XFER_MW_DMA_1, 45, 0, 0, 0, 80, 50, 5, 150, 0 },
3247 { XFER_MW_DMA_2, 25, 0, 0, 0, 70, 25, 5, 120, 0 },
3248 { XFER_MW_DMA_3, 25, 0, 0, 0, 65, 25, 5, 100, 0 },
3249 { XFER_MW_DMA_4, 25, 0, 0, 0, 55, 20, 5, 80, 0 },
3251 /* { XFER_UDMA_SLOW, 0, 0, 0, 0, 0, 0, 0, 0, 150 }, */
3252 { XFER_UDMA_0, 0, 0, 0, 0, 0, 0, 0, 0, 120 },
3253 { XFER_UDMA_1, 0, 0, 0, 0, 0, 0, 0, 0, 80 },
3254 { XFER_UDMA_2, 0, 0, 0, 0, 0, 0, 0, 0, 60 },
3255 { XFER_UDMA_3, 0, 0, 0, 0, 0, 0, 0, 0, 45 },
3256 { XFER_UDMA_4, 0, 0, 0, 0, 0, 0, 0, 0, 30 },
3257 { XFER_UDMA_5, 0, 0, 0, 0, 0, 0, 0, 0, 20 },
3258 { XFER_UDMA_6, 0, 0, 0, 0, 0, 0, 0, 0, 15 },
3263 #define ENOUGH(v, unit) (((v)-1)/(unit)+1)
3264 #define EZ(v, unit) ((v)?ENOUGH(((v) * 1000), unit):0)
3266 static void ata_timing_quantize(const struct ata_timing *t, struct ata_timing *q, int T, int UT)
3268 q->setup = EZ(t->setup, T);
3269 q->act8b = EZ(t->act8b, T);
3270 q->rec8b = EZ(t->rec8b, T);
3271 q->cyc8b = EZ(t->cyc8b, T);
3272 q->active = EZ(t->active, T);
3273 q->recover = EZ(t->recover, T);
3274 q->dmack_hold = EZ(t->dmack_hold, T);
3275 q->cycle = EZ(t->cycle, T);
3276 q->udma = EZ(t->udma, UT);
3279 void ata_timing_merge(const struct ata_timing *a, const struct ata_timing *b,
3280 struct ata_timing *m, unsigned int what)
3282 if (what & ATA_TIMING_SETUP ) m->setup = max(a->setup, b->setup);
3283 if (what & ATA_TIMING_ACT8B ) m->act8b = max(a->act8b, b->act8b);
3284 if (what & ATA_TIMING_REC8B ) m->rec8b = max(a->rec8b, b->rec8b);
3285 if (what & ATA_TIMING_CYC8B ) m->cyc8b = max(a->cyc8b, b->cyc8b);
3286 if (what & ATA_TIMING_ACTIVE ) m->active = max(a->active, b->active);
3287 if (what & ATA_TIMING_RECOVER) m->recover = max(a->recover, b->recover);
3288 if (what & ATA_TIMING_DMACK_HOLD) m->dmack_hold = max(a->dmack_hold, b->dmack_hold);
3289 if (what & ATA_TIMING_CYCLE ) m->cycle = max(a->cycle, b->cycle);
3290 if (what & ATA_TIMING_UDMA ) m->udma = max(a->udma, b->udma);
3293 const struct ata_timing *ata_timing_find_mode(u8 xfer_mode)
3295 const struct ata_timing *t = ata_timing;
3297 while (xfer_mode > t->mode)
3300 if (xfer_mode == t->mode)
3303 WARN_ONCE(true, "%s: unable to find timing for xfer_mode 0x%x\n",
3304 __func__, xfer_mode);
3309 int ata_timing_compute(struct ata_device *adev, unsigned short speed,
3310 struct ata_timing *t, int T, int UT)
3312 const u16 *id = adev->id;
3313 const struct ata_timing *s;
3314 struct ata_timing p;
3320 if (!(s = ata_timing_find_mode(speed)))
3323 memcpy(t, s, sizeof(*s));
3326 * If the drive is an EIDE drive, it can tell us it needs extended
3327 * PIO/MW_DMA cycle timing.
3330 if (id[ATA_ID_FIELD_VALID] & 2) { /* EIDE drive */
3331 memset(&p, 0, sizeof(p));
3333 if (speed >= XFER_PIO_0 && speed < XFER_SW_DMA_0) {
3334 if (speed <= XFER_PIO_2)
3335 p.cycle = p.cyc8b = id[ATA_ID_EIDE_PIO];
3336 else if ((speed <= XFER_PIO_4) ||
3337 (speed == XFER_PIO_5 && !ata_id_is_cfa(id)))
3338 p.cycle = p.cyc8b = id[ATA_ID_EIDE_PIO_IORDY];
3339 } else if (speed >= XFER_MW_DMA_0 && speed <= XFER_MW_DMA_2)
3340 p.cycle = id[ATA_ID_EIDE_DMA_MIN];
3342 ata_timing_merge(&p, t, t, ATA_TIMING_CYCLE | ATA_TIMING_CYC8B);
3346 * Convert the timing to bus clock counts.
3349 ata_timing_quantize(t, t, T, UT);
3352 * Even in DMA/UDMA modes we still use PIO access for IDENTIFY,
3353 * S.M.A.R.T * and some other commands. We have to ensure that the
3354 * DMA cycle timing is slower/equal than the fastest PIO timing.
3357 if (speed > XFER_PIO_6) {
3358 ata_timing_compute(adev, adev->pio_mode, &p, T, UT);
3359 ata_timing_merge(&p, t, t, ATA_TIMING_ALL);
3363 * Lengthen active & recovery time so that cycle time is correct.
3366 if (t->act8b + t->rec8b < t->cyc8b) {
3367 t->act8b += (t->cyc8b - (t->act8b + t->rec8b)) / 2;
3368 t->rec8b = t->cyc8b - t->act8b;
3371 if (t->active + t->recover < t->cycle) {
3372 t->active += (t->cycle - (t->active + t->recover)) / 2;
3373 t->recover = t->cycle - t->active;
3376 /* In a few cases quantisation may produce enough errors to
3377 leave t->cycle too low for the sum of active and recovery
3378 if so we must correct this */
3379 if (t->active + t->recover > t->cycle)
3380 t->cycle = t->active + t->recover;
3386 * ata_timing_cycle2mode - find xfer mode for the specified cycle duration
3387 * @xfer_shift: ATA_SHIFT_* value for transfer type to examine.
3388 * @cycle: cycle duration in ns
3390 * Return matching xfer mode for @cycle. The returned mode is of
3391 * the transfer type specified by @xfer_shift. If @cycle is too
3392 * slow for @xfer_shift, 0xff is returned. If @cycle is faster
3393 * than the fastest known mode, the fasted mode is returned.
3399 * Matching xfer_mode, 0xff if no match found.
3401 u8 ata_timing_cycle2mode(unsigned int xfer_shift, int cycle)
3403 u8 base_mode = 0xff, last_mode = 0xff;
3404 const struct ata_xfer_ent *ent;
3405 const struct ata_timing *t;
3407 for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
3408 if (ent->shift == xfer_shift)
3409 base_mode = ent->base;
3411 for (t = ata_timing_find_mode(base_mode);
3412 t && ata_xfer_mode2shift(t->mode) == xfer_shift; t++) {
3413 unsigned short this_cycle;
3415 switch (xfer_shift) {
3417 case ATA_SHIFT_MWDMA:
3418 this_cycle = t->cycle;
3420 case ATA_SHIFT_UDMA:
3421 this_cycle = t->udma;
3427 if (cycle > this_cycle)
3430 last_mode = t->mode;
3437 * ata_down_xfermask_limit - adjust dev xfer masks downward
3438 * @dev: Device to adjust xfer masks
3439 * @sel: ATA_DNXFER_* selector
3441 * Adjust xfer masks of @dev downward. Note that this function
3442 * does not apply the change. Invoking ata_set_mode() afterwards
3443 * will apply the limit.
3446 * Inherited from caller.
3449 * 0 on success, negative errno on failure
3451 int ata_down_xfermask_limit(struct ata_device *dev, unsigned int sel)
3454 unsigned long orig_mask, xfer_mask;
3455 unsigned long pio_mask, mwdma_mask, udma_mask;
3458 quiet = !!(sel & ATA_DNXFER_QUIET);
3459 sel &= ~ATA_DNXFER_QUIET;
3461 xfer_mask = orig_mask = ata_pack_xfermask(dev->pio_mask,
3464 ata_unpack_xfermask(xfer_mask, &pio_mask, &mwdma_mask, &udma_mask);
3467 case ATA_DNXFER_PIO:
3468 highbit = fls(pio_mask) - 1;
3469 pio_mask &= ~(1 << highbit);
3472 case ATA_DNXFER_DMA:
3474 highbit = fls(udma_mask) - 1;
3475 udma_mask &= ~(1 << highbit);
3478 } else if (mwdma_mask) {
3479 highbit = fls(mwdma_mask) - 1;
3480 mwdma_mask &= ~(1 << highbit);
3486 case ATA_DNXFER_40C:
3487 udma_mask &= ATA_UDMA_MASK_40C;
3490 case ATA_DNXFER_FORCE_PIO0:
3493 case ATA_DNXFER_FORCE_PIO:
3502 xfer_mask &= ata_pack_xfermask(pio_mask, mwdma_mask, udma_mask);
3504 if (!(xfer_mask & ATA_MASK_PIO) || xfer_mask == orig_mask)
3508 if (xfer_mask & (ATA_MASK_MWDMA | ATA_MASK_UDMA))
3509 snprintf(buf, sizeof(buf), "%s:%s",
3510 ata_mode_string(xfer_mask),
3511 ata_mode_string(xfer_mask & ATA_MASK_PIO));
3513 snprintf(buf, sizeof(buf), "%s",
3514 ata_mode_string(xfer_mask));
3516 ata_dev_warn(dev, "limiting speed to %s\n", buf);
3519 ata_unpack_xfermask(xfer_mask, &dev->pio_mask, &dev->mwdma_mask,
3525 static int ata_dev_set_mode(struct ata_device *dev)
3527 struct ata_port *ap = dev->link->ap;
3528 struct ata_eh_context *ehc = &dev->link->eh_context;
3529 const bool nosetxfer = dev->horkage & ATA_HORKAGE_NOSETXFER;
3530 const char *dev_err_whine = "";
3531 int ign_dev_err = 0;
3532 unsigned int err_mask = 0;
3535 dev->flags &= ~ATA_DFLAG_PIO;
3536 if (dev->xfer_shift == ATA_SHIFT_PIO)
3537 dev->flags |= ATA_DFLAG_PIO;
3539 if (nosetxfer && ap->flags & ATA_FLAG_SATA && ata_id_is_sata(dev->id))
3540 dev_err_whine = " (SET_XFERMODE skipped)";
3544 "NOSETXFER but PATA detected - can't "
3545 "skip SETXFER, might malfunction\n");
3546 err_mask = ata_dev_set_xfermode(dev);
3549 if (err_mask & ~AC_ERR_DEV)
3553 ehc->i.flags |= ATA_EHI_POST_SETMODE;
3554 rc = ata_dev_revalidate(dev, ATA_DEV_UNKNOWN, 0);
3555 ehc->i.flags &= ~ATA_EHI_POST_SETMODE;
3559 if (dev->xfer_shift == ATA_SHIFT_PIO) {
3560 /* Old CFA may refuse this command, which is just fine */
3561 if (ata_id_is_cfa(dev->id))
3563 /* Catch several broken garbage emulations plus some pre
3565 if (ata_id_major_version(dev->id) == 0 &&
3566 dev->pio_mode <= XFER_PIO_2)
3568 /* Some very old devices and some bad newer ones fail
3569 any kind of SET_XFERMODE request but support PIO0-2
3570 timings and no IORDY */
3571 if (!ata_id_has_iordy(dev->id) && dev->pio_mode <= XFER_PIO_2)
3574 /* Early MWDMA devices do DMA but don't allow DMA mode setting.
3575 Don't fail an MWDMA0 set IFF the device indicates it is in MWDMA0 */
3576 if (dev->xfer_shift == ATA_SHIFT_MWDMA &&
3577 dev->dma_mode == XFER_MW_DMA_0 &&
3578 (dev->id[63] >> 8) & 1)
3581 /* if the device is actually configured correctly, ignore dev err */
3582 if (dev->xfer_mode == ata_xfer_mask2mode(ata_id_xfermask(dev->id)))
3585 if (err_mask & AC_ERR_DEV) {
3589 dev_err_whine = " (device error ignored)";
3592 DPRINTK("xfer_shift=%u, xfer_mode=0x%x\n",
3593 dev->xfer_shift, (int)dev->xfer_mode);
3595 if (!(ehc->i.flags & ATA_EHI_QUIET) ||
3596 ehc->i.flags & ATA_EHI_DID_HARDRESET)
3597 ata_dev_info(dev, "configured for %s%s\n",
3598 ata_mode_string(ata_xfer_mode2mask(dev->xfer_mode)),
3604 ata_dev_err(dev, "failed to set xfermode (err_mask=0x%x)\n", err_mask);
3609 * ata_do_set_mode - Program timings and issue SET FEATURES - XFER
3610 * @link: link on which timings will be programmed
3611 * @r_failed_dev: out parameter for failed device
3613 * Standard implementation of the function used to tune and set
3614 * ATA device disk transfer mode (PIO3, UDMA6, etc.). If
3615 * ata_dev_set_mode() fails, pointer to the failing device is
3616 * returned in @r_failed_dev.
3619 * PCI/etc. bus probe sem.
3622 * 0 on success, negative errno otherwise
3625 int ata_do_set_mode(struct ata_link *link, struct ata_device **r_failed_dev)
3627 struct ata_port *ap = link->ap;
3628 struct ata_device *dev;
3629 int rc = 0, used_dma = 0, found = 0;
3631 /* step 1: calculate xfer_mask */
3632 ata_for_each_dev(dev, link, ENABLED) {
3633 unsigned long pio_mask, dma_mask;
3634 unsigned int mode_mask;
3636 mode_mask = ATA_DMA_MASK_ATA;
3637 if (dev->class == ATA_DEV_ATAPI)
3638 mode_mask = ATA_DMA_MASK_ATAPI;
3639 else if (ata_id_is_cfa(dev->id))
3640 mode_mask = ATA_DMA_MASK_CFA;
3642 ata_dev_xfermask(dev);
3643 ata_force_xfermask(dev);
3645 pio_mask = ata_pack_xfermask(dev->pio_mask, 0, 0);
3647 if (libata_dma_mask & mode_mask)
3648 dma_mask = ata_pack_xfermask(0, dev->mwdma_mask,
3653 dev->pio_mode = ata_xfer_mask2mode(pio_mask);
3654 dev->dma_mode = ata_xfer_mask2mode(dma_mask);
3657 if (ata_dma_enabled(dev))
3663 /* step 2: always set host PIO timings */
3664 ata_for_each_dev(dev, link, ENABLED) {
3665 if (dev->pio_mode == 0xff) {
3666 ata_dev_warn(dev, "no PIO support\n");
3671 dev->xfer_mode = dev->pio_mode;
3672 dev->xfer_shift = ATA_SHIFT_PIO;
3673 if (ap->ops->set_piomode)
3674 ap->ops->set_piomode(ap, dev);
3677 /* step 3: set host DMA timings */
3678 ata_for_each_dev(dev, link, ENABLED) {
3679 if (!ata_dma_enabled(dev))
3682 dev->xfer_mode = dev->dma_mode;
3683 dev->xfer_shift = ata_xfer_mode2shift(dev->dma_mode);
3684 if (ap->ops->set_dmamode)
3685 ap->ops->set_dmamode(ap, dev);
3688 /* step 4: update devices' xfer mode */
3689 ata_for_each_dev(dev, link, ENABLED) {
3690 rc = ata_dev_set_mode(dev);
3695 /* Record simplex status. If we selected DMA then the other
3696 * host channels are not permitted to do so.
3698 if (used_dma && (ap->host->flags & ATA_HOST_SIMPLEX))
3699 ap->host->simplex_claimed = ap;
3703 *r_failed_dev = dev;
3708 * ata_wait_ready - wait for link to become ready
3709 * @link: link to be waited on
3710 * @deadline: deadline jiffies for the operation
3711 * @check_ready: callback to check link readiness
3713 * Wait for @link to become ready. @check_ready should return
3714 * positive number if @link is ready, 0 if it isn't, -ENODEV if
3715 * link doesn't seem to be occupied, other errno for other error
3718 * Transient -ENODEV conditions are allowed for
3719 * ATA_TMOUT_FF_WAIT.
3725 * 0 if @link is ready before @deadline; otherwise, -errno.
3727 int ata_wait_ready(struct ata_link *link, unsigned long deadline,
3728 int (*check_ready)(struct ata_link *link))
3730 unsigned long start = jiffies;
3731 unsigned long nodev_deadline;
3734 /* choose which 0xff timeout to use, read comment in libata.h */
3735 if (link->ap->host->flags & ATA_HOST_PARALLEL_SCAN)
3736 nodev_deadline = ata_deadline(start, ATA_TMOUT_FF_WAIT_LONG);
3738 nodev_deadline = ata_deadline(start, ATA_TMOUT_FF_WAIT);
3740 /* Slave readiness can't be tested separately from master. On
3741 * M/S emulation configuration, this function should be called
3742 * only on the master and it will handle both master and slave.
3744 WARN_ON(link == link->ap->slave_link);
3746 if (time_after(nodev_deadline, deadline))
3747 nodev_deadline = deadline;
3750 unsigned long now = jiffies;
3753 ready = tmp = check_ready(link);
3758 * -ENODEV could be transient. Ignore -ENODEV if link
3759 * is online. Also, some SATA devices take a long
3760 * time to clear 0xff after reset. Wait for
3761 * ATA_TMOUT_FF_WAIT[_LONG] on -ENODEV if link isn't
3764 * Note that some PATA controllers (pata_ali) explode
3765 * if status register is read more than once when
3766 * there's no device attached.
3768 if (ready == -ENODEV) {
3769 if (ata_link_online(link))
3771 else if ((link->ap->flags & ATA_FLAG_SATA) &&
3772 !ata_link_offline(link) &&
3773 time_before(now, nodev_deadline))
3779 if (time_after(now, deadline))
3782 if (!warned && time_after(now, start + 5 * HZ) &&
3783 (deadline - now > 3 * HZ)) {
3785 "link is slow to respond, please be patient "
3786 "(ready=%d)\n", tmp);
3790 ata_msleep(link->ap, 50);
3795 * ata_wait_after_reset - wait for link to become ready after reset
3796 * @link: link to be waited on
3797 * @deadline: deadline jiffies for the operation
3798 * @check_ready: callback to check link readiness
3800 * Wait for @link to become ready after reset.
3806 * 0 if @link is ready before @deadline; otherwise, -errno.
3808 int ata_wait_after_reset(struct ata_link *link, unsigned long deadline,
3809 int (*check_ready)(struct ata_link *link))
3811 ata_msleep(link->ap, ATA_WAIT_AFTER_RESET);
3813 return ata_wait_ready(link, deadline, check_ready);
3817 * sata_link_debounce - debounce SATA phy status
3818 * @link: ATA link to debounce SATA phy status for
3819 * @params: timing parameters { interval, duration, timeout } in msec
3820 * @deadline: deadline jiffies for the operation
3822 * Make sure SStatus of @link reaches stable state, determined by
3823 * holding the same value where DET is not 1 for @duration polled
3824 * every @interval, before @timeout. Timeout constraints the
3825 * beginning of the stable state. Because DET gets stuck at 1 on
3826 * some controllers after hot unplugging, this functions waits
3827 * until timeout then returns 0 if DET is stable at 1.
3829 * @timeout is further limited by @deadline. The sooner of the
3833 * Kernel thread context (may sleep)
3836 * 0 on success, -errno on failure.
3838 int sata_link_debounce(struct ata_link *link, const unsigned long *params,
3839 unsigned long deadline)
3841 unsigned long interval = params[0];
3842 unsigned long duration = params[1];
3843 unsigned long last_jiffies, t;
3847 t = ata_deadline(jiffies, params[2]);
3848 if (time_before(t, deadline))
3851 if ((rc = sata_scr_read(link, SCR_STATUS, &cur)))
3856 last_jiffies = jiffies;
3859 ata_msleep(link->ap, interval);
3860 if ((rc = sata_scr_read(link, SCR_STATUS, &cur)))
3866 if (cur == 1 && time_before(jiffies, deadline))
3868 if (time_after(jiffies,
3869 ata_deadline(last_jiffies, duration)))
3874 /* unstable, start over */
3876 last_jiffies = jiffies;
3878 /* Check deadline. If debouncing failed, return
3879 * -EPIPE to tell upper layer to lower link speed.
3881 if (time_after(jiffies, deadline))
3887 * sata_link_resume - resume SATA link
3888 * @link: ATA link to resume SATA
3889 * @params: timing parameters { interval, duration, timeout } in msec
3890 * @deadline: deadline jiffies for the operation
3892 * Resume SATA phy @link and debounce it.
3895 * Kernel thread context (may sleep)
3898 * 0 on success, -errno on failure.
3900 int sata_link_resume(struct ata_link *link, const unsigned long *params,
3901 unsigned long deadline)
3903 int tries = ATA_LINK_RESUME_TRIES;
3904 u32 scontrol, serror;
3907 if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
3911 * Writes to SControl sometimes get ignored under certain
3912 * controllers (ata_piix SIDPR). Make sure DET actually is
3916 scontrol = (scontrol & 0x0f0) | 0x300;
3917 if ((rc = sata_scr_write(link, SCR_CONTROL, scontrol)))
3920 * Some PHYs react badly if SStatus is pounded
3921 * immediately after resuming. Delay 200ms before
3924 if (!(link->flags & ATA_LFLAG_NO_DB_DELAY))
3925 ata_msleep(link->ap, 200);
3927 /* is SControl restored correctly? */
3928 if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
3930 } while ((scontrol & 0xf0f) != 0x300 && --tries);
3932 if ((scontrol & 0xf0f) != 0x300) {
3933 ata_link_warn(link, "failed to resume link (SControl %X)\n",
3938 if (tries < ATA_LINK_RESUME_TRIES)
3939 ata_link_warn(link, "link resume succeeded after %d retries\n",
3940 ATA_LINK_RESUME_TRIES - tries);
3942 if ((rc = sata_link_debounce(link, params, deadline)))
3945 /* clear SError, some PHYs require this even for SRST to work */
3946 if (!(rc = sata_scr_read(link, SCR_ERROR, &serror)))
3947 rc = sata_scr_write(link, SCR_ERROR, serror);
3949 return rc != -EINVAL ? rc : 0;
3953 * sata_link_scr_lpm - manipulate SControl IPM and SPM fields
3954 * @link: ATA link to manipulate SControl for
3955 * @policy: LPM policy to configure
3956 * @spm_wakeup: initiate LPM transition to active state
3958 * Manipulate the IPM field of the SControl register of @link
3959 * according to @policy. If @policy is ATA_LPM_MAX_POWER and
3960 * @spm_wakeup is %true, the SPM field is manipulated to wake up
3961 * the link. This function also clears PHYRDY_CHG before
3968 * 0 on success, -errno otherwise.
3970 int sata_link_scr_lpm(struct ata_link *link, enum ata_lpm_policy policy,
3973 struct ata_eh_context *ehc = &link->eh_context;
3974 bool woken_up = false;
3978 rc = sata_scr_read(link, SCR_CONTROL, &scontrol);
3983 case ATA_LPM_MAX_POWER:
3984 /* disable all LPM transitions */
3985 scontrol |= (0x7 << 8);
3986 /* initiate transition to active state */
3988 scontrol |= (0x4 << 12);
3992 case ATA_LPM_MED_POWER:
3993 /* allow LPM to PARTIAL */
3994 scontrol &= ~(0x1 << 8);
3995 scontrol |= (0x6 << 8);
3997 case ATA_LPM_MED_POWER_WITH_DIPM:
3998 case ATA_LPM_MIN_POWER_WITH_PARTIAL:
3999 case ATA_LPM_MIN_POWER:
4000 if (ata_link_nr_enabled(link) > 0)
4001 /* no restrictions on LPM transitions */
4002 scontrol &= ~(0x7 << 8);
4004 /* empty port, power off */
4006 scontrol |= (0x1 << 2);
4013 rc = sata_scr_write(link, SCR_CONTROL, scontrol);
4017 /* give the link time to transit out of LPM state */
4021 /* clear PHYRDY_CHG from SError */
4022 ehc->i.serror &= ~SERR_PHYRDY_CHG;
4023 return sata_scr_write(link, SCR_ERROR, SERR_PHYRDY_CHG);
4027 * ata_std_prereset - prepare for reset
4028 * @link: ATA link to be reset
4029 * @deadline: deadline jiffies for the operation
4031 * @link is about to be reset. Initialize it. Failure from
4032 * prereset makes libata abort whole reset sequence and give up
4033 * that port, so prereset should be best-effort. It does its
4034 * best to prepare for reset sequence but if things go wrong, it
4035 * should just whine, not fail.
4038 * Kernel thread context (may sleep)
4041 * 0 on success, -errno otherwise.
4043 int ata_std_prereset(struct ata_link *link, unsigned long deadline)
4045 struct ata_port *ap = link->ap;
4046 struct ata_eh_context *ehc = &link->eh_context;
4047 const unsigned long *timing = sata_ehc_deb_timing(ehc);
4050 /* if we're about to do hardreset, nothing more to do */
4051 if (ehc->i.action & ATA_EH_HARDRESET)
4054 /* if SATA, resume link */
4055 if (ap->flags & ATA_FLAG_SATA) {
4056 rc = sata_link_resume(link, timing, deadline);
4057 /* whine about phy resume failure but proceed */
4058 if (rc && rc != -EOPNOTSUPP)
4060 "failed to resume link for reset (errno=%d)\n",
4064 /* no point in trying softreset on offline link */
4065 if (ata_phys_link_offline(link))
4066 ehc->i.action &= ~ATA_EH_SOFTRESET;
4072 * sata_link_hardreset - reset link via SATA phy reset
4073 * @link: link to reset
4074 * @timing: timing parameters { interval, duration, timeout } in msec
4075 * @deadline: deadline jiffies for the operation
4076 * @online: optional out parameter indicating link onlineness
4077 * @check_ready: optional callback to check link readiness
4079 * SATA phy-reset @link using DET bits of SControl register.
4080 * After hardreset, link readiness is waited upon using
4081 * ata_wait_ready() if @check_ready is specified. LLDs are
4082 * allowed to not specify @check_ready and wait itself after this
4083 * function returns. Device classification is LLD's
4086 * *@online is set to one iff reset succeeded and @link is online
4090 * Kernel thread context (may sleep)
4093 * 0 on success, -errno otherwise.
4095 int sata_link_hardreset(struct ata_link *link, const unsigned long *timing,
4096 unsigned long deadline,
4097 bool *online, int (*check_ready)(struct ata_link *))
4107 if (sata_set_spd_needed(link)) {
4108 /* SATA spec says nothing about how to reconfigure
4109 * spd. To be on the safe side, turn off phy during
4110 * reconfiguration. This works for at least ICH7 AHCI
4113 if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
4116 scontrol = (scontrol & 0x0f0) | 0x304;
4118 if ((rc = sata_scr_write(link, SCR_CONTROL, scontrol)))
4124 /* issue phy wake/reset */
4125 if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
4128 scontrol = (scontrol & 0x0f0) | 0x301;
4130 if ((rc = sata_scr_write_flush(link, SCR_CONTROL, scontrol)))
4133 /* Couldn't find anything in SATA I/II specs, but AHCI-1.1
4134 * 10.4.2 says at least 1 ms.
4136 ata_msleep(link->ap, 1);
4138 /* bring link back */
4139 rc = sata_link_resume(link, timing, deadline);
4142 /* if link is offline nothing more to do */
4143 if (ata_phys_link_offline(link))
4146 /* Link is online. From this point, -ENODEV too is an error. */
4150 if (sata_pmp_supported(link->ap) && ata_is_host_link(link)) {
4151 /* If PMP is supported, we have to do follow-up SRST.
4152 * Some PMPs don't send D2H Reg FIS after hardreset if
4153 * the first port is empty. Wait only for
4154 * ATA_TMOUT_PMP_SRST_WAIT.
4157 unsigned long pmp_deadline;
4159 pmp_deadline = ata_deadline(jiffies,
4160 ATA_TMOUT_PMP_SRST_WAIT);
4161 if (time_after(pmp_deadline, deadline))
4162 pmp_deadline = deadline;
4163 ata_wait_ready(link, pmp_deadline, check_ready);
4171 rc = ata_wait_ready(link, deadline, check_ready);
4173 if (rc && rc != -EAGAIN) {
4174 /* online is set iff link is online && reset succeeded */
4177 ata_link_err(link, "COMRESET failed (errno=%d)\n", rc);
4179 DPRINTK("EXIT, rc=%d\n", rc);
4184 * sata_std_hardreset - COMRESET w/o waiting or classification
4185 * @link: link to reset
4186 * @class: resulting class of attached device
4187 * @deadline: deadline jiffies for the operation
4189 * Standard SATA COMRESET w/o waiting or classification.
4192 * Kernel thread context (may sleep)
4195 * 0 if link offline, -EAGAIN if link online, -errno on errors.
4197 int sata_std_hardreset(struct ata_link *link, unsigned int *class,
4198 unsigned long deadline)
4200 const unsigned long *timing = sata_ehc_deb_timing(&link->eh_context);
4205 rc = sata_link_hardreset(link, timing, deadline, &online, NULL);
4206 return online ? -EAGAIN : rc;
4210 * ata_std_postreset - standard postreset callback
4211 * @link: the target ata_link
4212 * @classes: classes of attached devices
4214 * This function is invoked after a successful reset. Note that
4215 * the device might have been reset more than once using
4216 * different reset methods before postreset is invoked.
4219 * Kernel thread context (may sleep)
4221 void ata_std_postreset(struct ata_link *link, unsigned int *classes)
4227 /* reset complete, clear SError */
4228 if (!sata_scr_read(link, SCR_ERROR, &serror))
4229 sata_scr_write(link, SCR_ERROR, serror);
4231 /* print link status */
4232 sata_print_link_status(link);
4238 * ata_dev_same_device - Determine whether new ID matches configured device
4239 * @dev: device to compare against
4240 * @new_class: class of the new device
4241 * @new_id: IDENTIFY page of the new device
4243 * Compare @new_class and @new_id against @dev and determine
4244 * whether @dev is the device indicated by @new_class and
4251 * 1 if @dev matches @new_class and @new_id, 0 otherwise.
4253 static int ata_dev_same_device(struct ata_device *dev, unsigned int new_class,
4256 const u16 *old_id = dev->id;
4257 unsigned char model[2][ATA_ID_PROD_LEN + 1];
4258 unsigned char serial[2][ATA_ID_SERNO_LEN + 1];
4260 if (dev->class != new_class) {
4261 ata_dev_info(dev, "class mismatch %d != %d\n",
4262 dev->class, new_class);
4266 ata_id_c_string(old_id, model[0], ATA_ID_PROD, sizeof(model[0]));
4267 ata_id_c_string(new_id, model[1], ATA_ID_PROD, sizeof(model[1]));
4268 ata_id_c_string(old_id, serial[0], ATA_ID_SERNO, sizeof(serial[0]));
4269 ata_id_c_string(new_id, serial[1], ATA_ID_SERNO, sizeof(serial[1]));
4271 if (strcmp(model[0], model[1])) {
4272 ata_dev_info(dev, "model number mismatch '%s' != '%s'\n",
4273 model[0], model[1]);
4277 if (strcmp(serial[0], serial[1])) {
4278 ata_dev_info(dev, "serial number mismatch '%s' != '%s'\n",
4279 serial[0], serial[1]);
4287 * ata_dev_reread_id - Re-read IDENTIFY data
4288 * @dev: target ATA device
4289 * @readid_flags: read ID flags
4291 * Re-read IDENTIFY page and make sure @dev is still attached to
4295 * Kernel thread context (may sleep)
4298 * 0 on success, negative errno otherwise
4300 int ata_dev_reread_id(struct ata_device *dev, unsigned int readid_flags)
4302 unsigned int class = dev->class;
4303 u16 *id = (void *)dev->link->ap->sector_buf;
4307 rc = ata_dev_read_id(dev, &class, readid_flags, id);
4311 /* is the device still there? */
4312 if (!ata_dev_same_device(dev, class, id))
4315 memcpy(dev->id, id, sizeof(id[0]) * ATA_ID_WORDS);
4320 * ata_dev_revalidate - Revalidate ATA device
4321 * @dev: device to revalidate
4322 * @new_class: new class code
4323 * @readid_flags: read ID flags
4325 * Re-read IDENTIFY page, make sure @dev is still attached to the
4326 * port and reconfigure it according to the new IDENTIFY page.
4329 * Kernel thread context (may sleep)
4332 * 0 on success, negative errno otherwise
4334 int ata_dev_revalidate(struct ata_device *dev, unsigned int new_class,
4335 unsigned int readid_flags)
4337 u64 n_sectors = dev->n_sectors;
4338 u64 n_native_sectors = dev->n_native_sectors;
4341 if (!ata_dev_enabled(dev))
4344 /* fail early if !ATA && !ATAPI to avoid issuing [P]IDENTIFY to PMP */
4345 if (ata_class_enabled(new_class) &&
4346 new_class != ATA_DEV_ATA &&
4347 new_class != ATA_DEV_ATAPI &&
4348 new_class != ATA_DEV_ZAC &&
4349 new_class != ATA_DEV_SEMB) {
4350 ata_dev_info(dev, "class mismatch %u != %u\n",
4351 dev->class, new_class);
4357 rc = ata_dev_reread_id(dev, readid_flags);
4361 /* configure device according to the new ID */
4362 rc = ata_dev_configure(dev);
4366 /* verify n_sectors hasn't changed */
4367 if (dev->class != ATA_DEV_ATA || !n_sectors ||
4368 dev->n_sectors == n_sectors)
4371 /* n_sectors has changed */
4372 ata_dev_warn(dev, "n_sectors mismatch %llu != %llu\n",
4373 (unsigned long long)n_sectors,
4374 (unsigned long long)dev->n_sectors);
4377 * Something could have caused HPA to be unlocked
4378 * involuntarily. If n_native_sectors hasn't changed and the
4379 * new size matches it, keep the device.
4381 if (dev->n_native_sectors == n_native_sectors &&
4382 dev->n_sectors > n_sectors && dev->n_sectors == n_native_sectors) {
4384 "new n_sectors matches native, probably "
4385 "late HPA unlock, n_sectors updated\n");
4386 /* use the larger n_sectors */
4391 * Some BIOSes boot w/o HPA but resume w/ HPA locked. Try
4392 * unlocking HPA in those cases.
4394 * https://bugzilla.kernel.org/show_bug.cgi?id=15396
4396 if (dev->n_native_sectors == n_native_sectors &&
4397 dev->n_sectors < n_sectors && n_sectors == n_native_sectors &&
4398 !(dev->horkage & ATA_HORKAGE_BROKEN_HPA)) {
4400 "old n_sectors matches native, probably "
4401 "late HPA lock, will try to unlock HPA\n");
4402 /* try unlocking HPA */
4403 dev->flags |= ATA_DFLAG_UNLOCK_HPA;
4408 /* restore original n_[native_]sectors and fail */
4409 dev->n_native_sectors = n_native_sectors;
4410 dev->n_sectors = n_sectors;
4412 ata_dev_err(dev, "revalidation failed (errno=%d)\n", rc);
4416 struct ata_blacklist_entry {
4417 const char *model_num;
4418 const char *model_rev;
4419 unsigned long horkage;
4422 static const struct ata_blacklist_entry ata_device_blacklist [] = {
4423 /* Devices with DMA related problems under Linux */
4424 { "WDC AC11000H", NULL, ATA_HORKAGE_NODMA },
4425 { "WDC AC22100H", NULL, ATA_HORKAGE_NODMA },
4426 { "WDC AC32500H", NULL, ATA_HORKAGE_NODMA },
4427 { "WDC AC33100H", NULL, ATA_HORKAGE_NODMA },
4428 { "WDC AC31600H", NULL, ATA_HORKAGE_NODMA },
4429 { "WDC AC32100H", "24.09P07", ATA_HORKAGE_NODMA },
4430 { "WDC AC23200L", "21.10N21", ATA_HORKAGE_NODMA },
4431 { "Compaq CRD-8241B", NULL, ATA_HORKAGE_NODMA },
4432 { "CRD-8400B", NULL, ATA_HORKAGE_NODMA },
4433 { "CRD-848[02]B", NULL, ATA_HORKAGE_NODMA },
4434 { "CRD-84", NULL, ATA_HORKAGE_NODMA },
4435 { "SanDisk SDP3B", NULL, ATA_HORKAGE_NODMA },
4436 { "SanDisk SDP3B-64", NULL, ATA_HORKAGE_NODMA },
4437 { "SANYO CD-ROM CRD", NULL, ATA_HORKAGE_NODMA },
4438 { "HITACHI CDR-8", NULL, ATA_HORKAGE_NODMA },
4439 { "HITACHI CDR-8[34]35",NULL, ATA_HORKAGE_NODMA },
4440 { "Toshiba CD-ROM XM-6202B", NULL, ATA_HORKAGE_NODMA },
4441 { "TOSHIBA CD-ROM XM-1702BC", NULL, ATA_HORKAGE_NODMA },
4442 { "CD-532E-A", NULL, ATA_HORKAGE_NODMA },
4443 { "E-IDE CD-ROM CR-840",NULL, ATA_HORKAGE_NODMA },
4444 { "CD-ROM Drive/F5A", NULL, ATA_HORKAGE_NODMA },
4445 { "WPI CDD-820", NULL, ATA_HORKAGE_NODMA },
4446 { "SAMSUNG CD-ROM SC-148C", NULL, ATA_HORKAGE_NODMA },
4447 { "SAMSUNG CD-ROM SC", NULL, ATA_HORKAGE_NODMA },
4448 { "ATAPI CD-ROM DRIVE 40X MAXIMUM",NULL,ATA_HORKAGE_NODMA },
4449 { "_NEC DV5800A", NULL, ATA_HORKAGE_NODMA },
4450 { "SAMSUNG CD-ROM SN-124", "N001", ATA_HORKAGE_NODMA },
4451 { "Seagate STT20000A", NULL, ATA_HORKAGE_NODMA },
4452 { " 2GB ATA Flash Disk", "ADMA428M", ATA_HORKAGE_NODMA },
4453 { "VRFDFC22048UCHC-TE*", NULL, ATA_HORKAGE_NODMA },
4454 /* Odd clown on sil3726/4726 PMPs */
4455 { "Config Disk", NULL, ATA_HORKAGE_DISABLE },
4456 /* Similar story with ASMedia 1092 */
4457 { "ASMT109x- Config", NULL, ATA_HORKAGE_DISABLE },
4459 /* Weird ATAPI devices */
4460 { "TORiSAN DVD-ROM DRD-N216", NULL, ATA_HORKAGE_MAX_SEC_128 },
4461 { "QUANTUM DAT DAT72-000", NULL, ATA_HORKAGE_ATAPI_MOD16_DMA },
4462 { "Slimtype DVD A DS8A8SH", NULL, ATA_HORKAGE_MAX_SEC_LBA48 },
4463 { "Slimtype DVD A DS8A9SH", NULL, ATA_HORKAGE_MAX_SEC_LBA48 },
4466 * Causes silent data corruption with higher max sects.
4467 * http://lkml.kernel.org/g/x49wpy40ysk.fsf@segfault.boston.devel.redhat.com
4469 { "ST380013AS", "3.20", ATA_HORKAGE_MAX_SEC_1024 },
4472 * These devices time out with higher max sects.
4473 * https://bugzilla.kernel.org/show_bug.cgi?id=121671
4475 { "LITEON CX1-JB*-HP", NULL, ATA_HORKAGE_MAX_SEC_1024 },
4476 { "LITEON EP1-*", NULL, ATA_HORKAGE_MAX_SEC_1024 },
4478 /* Devices we expect to fail diagnostics */
4480 /* Devices where NCQ should be avoided */
4482 { "WDC WD740ADFD-00", NULL, ATA_HORKAGE_NONCQ },
4483 { "WDC WD740ADFD-00NLR1", NULL, ATA_HORKAGE_NONCQ, },
4484 /* http://thread.gmane.org/gmane.linux.ide/14907 */
4485 { "FUJITSU MHT2060BH", NULL, ATA_HORKAGE_NONCQ },
4487 { "Maxtor *", "BANC*", ATA_HORKAGE_NONCQ },
4488 { "Maxtor 7V300F0", "VA111630", ATA_HORKAGE_NONCQ },
4489 { "ST380817AS", "3.42", ATA_HORKAGE_NONCQ },
4490 { "ST3160023AS", "3.42", ATA_HORKAGE_NONCQ },
4491 { "OCZ CORE_SSD", "02.10104", ATA_HORKAGE_NONCQ },
4493 /* Seagate NCQ + FLUSH CACHE firmware bug */
4494 { "ST31500341AS", "SD1[5-9]", ATA_HORKAGE_NONCQ |
4495 ATA_HORKAGE_FIRMWARE_WARN },
4497 { "ST31000333AS", "SD1[5-9]", ATA_HORKAGE_NONCQ |
4498 ATA_HORKAGE_FIRMWARE_WARN },
4500 { "ST3640[36]23AS", "SD1[5-9]", ATA_HORKAGE_NONCQ |
4501 ATA_HORKAGE_FIRMWARE_WARN },
4503 { "ST3320[68]13AS", "SD1[5-9]", ATA_HORKAGE_NONCQ |
4504 ATA_HORKAGE_FIRMWARE_WARN },
4506 /* drives which fail FPDMA_AA activation (some may freeze afterwards)
4507 the ST disks also have LPM issues */
4508 { "ST1000LM024 HN-M101MBB", "2AR10001", ATA_HORKAGE_BROKEN_FPDMA_AA |
4509 ATA_HORKAGE_NOLPM, },
4510 { "ST1000LM024 HN-M101MBB", "2BA30001", ATA_HORKAGE_BROKEN_FPDMA_AA |
4511 ATA_HORKAGE_NOLPM, },
4512 { "VB0250EAVER", "HPG7", ATA_HORKAGE_BROKEN_FPDMA_AA },
4514 /* Blacklist entries taken from Silicon Image 3124/3132
4515 Windows driver .inf file - also several Linux problem reports */
4516 { "HTS541060G9SA00", "MB3OC60D", ATA_HORKAGE_NONCQ, },
4517 { "HTS541080G9SA00", "MB4OC60D", ATA_HORKAGE_NONCQ, },
4518 { "HTS541010G9SA00", "MBZOC60D", ATA_HORKAGE_NONCQ, },
4520 /* https://bugzilla.kernel.org/show_bug.cgi?id=15573 */
4521 { "C300-CTFDDAC128MAG", "0001", ATA_HORKAGE_NONCQ, },
4523 /* Sandisk SD7/8/9s lock up hard on large trims */
4524 { "SanDisk SD[789]*", NULL, ATA_HORKAGE_MAX_TRIM_128M, },
4526 /* devices which puke on READ_NATIVE_MAX */
4527 { "HDS724040KLSA80", "KFAOA20N", ATA_HORKAGE_BROKEN_HPA, },
4528 { "WDC WD3200JD-00KLB0", "WD-WCAMR1130137", ATA_HORKAGE_BROKEN_HPA },
4529 { "WDC WD2500JD-00HBB0", "WD-WMAL71490727", ATA_HORKAGE_BROKEN_HPA },
4530 { "MAXTOR 6L080L4", "A93.0500", ATA_HORKAGE_BROKEN_HPA },
4532 /* this one allows HPA unlocking but fails IOs on the area */
4533 { "OCZ-VERTEX", "1.30", ATA_HORKAGE_BROKEN_HPA },
4535 /* Devices which report 1 sector over size HPA */
4536 { "ST340823A", NULL, ATA_HORKAGE_HPA_SIZE, },
4537 { "ST320413A", NULL, ATA_HORKAGE_HPA_SIZE, },
4538 { "ST310211A", NULL, ATA_HORKAGE_HPA_SIZE, },
4540 /* Devices which get the IVB wrong */
4541 { "QUANTUM FIREBALLlct10 05", "A03.0900", ATA_HORKAGE_IVB, },
4542 /* Maybe we should just blacklist TSSTcorp... */
4543 { "TSSTcorp CDDVDW SH-S202[HJN]", "SB0[01]", ATA_HORKAGE_IVB, },
4545 /* Devices that do not need bridging limits applied */
4546 { "MTRON MSP-SATA*", NULL, ATA_HORKAGE_BRIDGE_OK, },
4547 { "BUFFALO HD-QSU2/R5", NULL, ATA_HORKAGE_BRIDGE_OK, },
4549 /* Devices which aren't very happy with higher link speeds */
4550 { "WD My Book", NULL, ATA_HORKAGE_1_5_GBPS, },
4551 { "Seagate FreeAgent GoFlex", NULL, ATA_HORKAGE_1_5_GBPS, },
4554 * Devices which choke on SETXFER. Applies only if both the
4555 * device and controller are SATA.
4557 { "PIONEER DVD-RW DVRTD08", NULL, ATA_HORKAGE_NOSETXFER },
4558 { "PIONEER DVD-RW DVRTD08A", NULL, ATA_HORKAGE_NOSETXFER },
4559 { "PIONEER DVD-RW DVR-215", NULL, ATA_HORKAGE_NOSETXFER },
4560 { "PIONEER DVD-RW DVR-212D", NULL, ATA_HORKAGE_NOSETXFER },
4561 { "PIONEER DVD-RW DVR-216D", NULL, ATA_HORKAGE_NOSETXFER },
4563 /* Crucial BX100 SSD 500GB has broken LPM support */
4564 { "CT500BX100SSD1", NULL, ATA_HORKAGE_NOLPM },
4566 /* 512GB MX100 with MU01 firmware has both queued TRIM and LPM issues */
4567 { "Crucial_CT512MX100*", "MU01", ATA_HORKAGE_NO_NCQ_TRIM |
4568 ATA_HORKAGE_ZERO_AFTER_TRIM |
4569 ATA_HORKAGE_NOLPM, },
4570 /* 512GB MX100 with newer firmware has only LPM issues */
4571 { "Crucial_CT512MX100*", NULL, ATA_HORKAGE_ZERO_AFTER_TRIM |
4572 ATA_HORKAGE_NOLPM, },
4574 /* 480GB+ M500 SSDs have both queued TRIM and LPM issues */
4575 { "Crucial_CT480M500*", NULL, ATA_HORKAGE_NO_NCQ_TRIM |
4576 ATA_HORKAGE_ZERO_AFTER_TRIM |
4577 ATA_HORKAGE_NOLPM, },
4578 { "Crucial_CT960M500*", NULL, ATA_HORKAGE_NO_NCQ_TRIM |
4579 ATA_HORKAGE_ZERO_AFTER_TRIM |
4580 ATA_HORKAGE_NOLPM, },
4582 /* These specific Samsung models/firmware-revs do not handle LPM well */
4583 { "SAMSUNG MZMPC128HBFU-000MV", "CXM14M1Q", ATA_HORKAGE_NOLPM, },
4584 { "SAMSUNG SSD PM830 mSATA *", "CXM13D1Q", ATA_HORKAGE_NOLPM, },
4585 { "SAMSUNG MZ7TD256HAFV-000L9", NULL, ATA_HORKAGE_NOLPM, },
4586 { "SAMSUNG MZ7TE512HMHP-000L1", "EXT06L0Q", ATA_HORKAGE_NOLPM, },
4588 /* devices that don't properly handle queued TRIM commands */
4589 { "Micron_M500IT_*", "MU01", ATA_HORKAGE_NO_NCQ_TRIM |
4590 ATA_HORKAGE_ZERO_AFTER_TRIM, },
4591 { "Micron_M500_*", NULL, ATA_HORKAGE_NO_NCQ_TRIM |
4592 ATA_HORKAGE_ZERO_AFTER_TRIM, },
4593 { "Crucial_CT*M500*", NULL, ATA_HORKAGE_NO_NCQ_TRIM |
4594 ATA_HORKAGE_ZERO_AFTER_TRIM, },
4595 { "Micron_M5[15]0_*", "MU01", ATA_HORKAGE_NO_NCQ_TRIM |
4596 ATA_HORKAGE_ZERO_AFTER_TRIM, },
4597 { "Crucial_CT*M550*", "MU01", ATA_HORKAGE_NO_NCQ_TRIM |
4598 ATA_HORKAGE_ZERO_AFTER_TRIM, },
4599 { "Crucial_CT*MX100*", "MU01", ATA_HORKAGE_NO_NCQ_TRIM |
4600 ATA_HORKAGE_ZERO_AFTER_TRIM, },
4601 { "Samsung SSD 840 EVO*", NULL, ATA_HORKAGE_NO_NCQ_TRIM |
4602 ATA_HORKAGE_NO_DMA_LOG |
4603 ATA_HORKAGE_ZERO_AFTER_TRIM, },
4604 { "Samsung SSD 840*", NULL, ATA_HORKAGE_NO_NCQ_TRIM |
4605 ATA_HORKAGE_ZERO_AFTER_TRIM, },
4606 { "Samsung SSD 850*", NULL, ATA_HORKAGE_NO_NCQ_TRIM |
4607 ATA_HORKAGE_ZERO_AFTER_TRIM, },
4608 { "Samsung SSD 860*", NULL, ATA_HORKAGE_NO_NCQ_TRIM |
4609 ATA_HORKAGE_ZERO_AFTER_TRIM |
4610 ATA_HORKAGE_NO_NCQ_ON_ATI, },
4611 { "Samsung SSD 870*", NULL, ATA_HORKAGE_NO_NCQ_TRIM |
4612 ATA_HORKAGE_ZERO_AFTER_TRIM |
4613 ATA_HORKAGE_NO_NCQ_ON_ATI, },
4614 { "FCCT*M500*", NULL, ATA_HORKAGE_NO_NCQ_TRIM |
4615 ATA_HORKAGE_ZERO_AFTER_TRIM, },
4617 /* devices that don't properly handle TRIM commands */
4618 { "SuperSSpeed S238*", NULL, ATA_HORKAGE_NOTRIM, },
4619 { "M88V29*", NULL, ATA_HORKAGE_NOTRIM, },
4622 * As defined, the DRAT (Deterministic Read After Trim) and RZAT
4623 * (Return Zero After Trim) flags in the ATA Command Set are
4624 * unreliable in the sense that they only define what happens if
4625 * the device successfully executed the DSM TRIM command. TRIM
4626 * is only advisory, however, and the device is free to silently
4627 * ignore all or parts of the request.
4629 * Whitelist drives that are known to reliably return zeroes
4634 * The intel 510 drive has buggy DRAT/RZAT. Explicitly exclude
4635 * that model before whitelisting all other intel SSDs.
4637 { "INTEL*SSDSC2MH*", NULL, 0, },
4639 { "Micron*", NULL, ATA_HORKAGE_ZERO_AFTER_TRIM, },
4640 { "Crucial*", NULL, ATA_HORKAGE_ZERO_AFTER_TRIM, },
4641 { "INTEL*SSD*", NULL, ATA_HORKAGE_ZERO_AFTER_TRIM, },
4642 { "SSD*INTEL*", NULL, ATA_HORKAGE_ZERO_AFTER_TRIM, },
4643 { "Samsung*SSD*", NULL, ATA_HORKAGE_ZERO_AFTER_TRIM, },
4644 { "SAMSUNG*SSD*", NULL, ATA_HORKAGE_ZERO_AFTER_TRIM, },
4645 { "SAMSUNG*MZ7KM*", NULL, ATA_HORKAGE_ZERO_AFTER_TRIM, },
4646 { "ST[1248][0248]0[FH]*", NULL, ATA_HORKAGE_ZERO_AFTER_TRIM, },
4649 * Some WD SATA-I drives spin up and down erratically when the link
4650 * is put into the slumber mode. We don't have full list of the
4651 * affected devices. Disable LPM if the device matches one of the
4652 * known prefixes and is SATA-1. As a side effect LPM partial is
4655 * https://bugzilla.kernel.org/show_bug.cgi?id=57211
4657 { "WDC WD800JD-*", NULL, ATA_HORKAGE_WD_BROKEN_LPM },
4658 { "WDC WD1200JD-*", NULL, ATA_HORKAGE_WD_BROKEN_LPM },
4659 { "WDC WD1600JD-*", NULL, ATA_HORKAGE_WD_BROKEN_LPM },
4660 { "WDC WD2000JD-*", NULL, ATA_HORKAGE_WD_BROKEN_LPM },
4661 { "WDC WD2500JD-*", NULL, ATA_HORKAGE_WD_BROKEN_LPM },
4662 { "WDC WD3000JD-*", NULL, ATA_HORKAGE_WD_BROKEN_LPM },
4663 { "WDC WD3200JD-*", NULL, ATA_HORKAGE_WD_BROKEN_LPM },
4669 static unsigned long ata_dev_blacklisted(const struct ata_device *dev)
4671 unsigned char model_num[ATA_ID_PROD_LEN + 1];
4672 unsigned char model_rev[ATA_ID_FW_REV_LEN + 1];
4673 const struct ata_blacklist_entry *ad = ata_device_blacklist;
4675 ata_id_c_string(dev->id, model_num, ATA_ID_PROD, sizeof(model_num));
4676 ata_id_c_string(dev->id, model_rev, ATA_ID_FW_REV, sizeof(model_rev));
4678 while (ad->model_num) {
4679 if (glob_match(ad->model_num, model_num)) {
4680 if (ad->model_rev == NULL)
4682 if (glob_match(ad->model_rev, model_rev))
4690 static int ata_dma_blacklisted(const struct ata_device *dev)
4692 /* We don't support polling DMA.
4693 * DMA blacklist those ATAPI devices with CDB-intr (and use PIO)
4694 * if the LLDD handles only interrupts in the HSM_ST_LAST state.
4696 if ((dev->link->ap->flags & ATA_FLAG_PIO_POLLING) &&
4697 (dev->flags & ATA_DFLAG_CDB_INTR))
4699 return (dev->horkage & ATA_HORKAGE_NODMA) ? 1 : 0;
4703 * ata_is_40wire - check drive side detection
4706 * Perform drive side detection decoding, allowing for device vendors
4707 * who can't follow the documentation.
4710 static int ata_is_40wire(struct ata_device *dev)
4712 if (dev->horkage & ATA_HORKAGE_IVB)
4713 return ata_drive_40wire_relaxed(dev->id);
4714 return ata_drive_40wire(dev->id);
4718 * cable_is_40wire - 40/80/SATA decider
4719 * @ap: port to consider
4721 * This function encapsulates the policy for speed management
4722 * in one place. At the moment we don't cache the result but
4723 * there is a good case for setting ap->cbl to the result when
4724 * we are called with unknown cables (and figuring out if it
4725 * impacts hotplug at all).
4727 * Return 1 if the cable appears to be 40 wire.
4730 static int cable_is_40wire(struct ata_port *ap)
4732 struct ata_link *link;
4733 struct ata_device *dev;
4735 /* If the controller thinks we are 40 wire, we are. */
4736 if (ap->cbl == ATA_CBL_PATA40)
4739 /* If the controller thinks we are 80 wire, we are. */
4740 if (ap->cbl == ATA_CBL_PATA80 || ap->cbl == ATA_CBL_SATA)
4743 /* If the system is known to be 40 wire short cable (eg
4744 * laptop), then we allow 80 wire modes even if the drive
4747 if (ap->cbl == ATA_CBL_PATA40_SHORT)
4750 /* If the controller doesn't know, we scan.
4752 * Note: We look for all 40 wire detects at this point. Any
4753 * 80 wire detect is taken to be 80 wire cable because
4754 * - in many setups only the one drive (slave if present) will
4755 * give a valid detect
4756 * - if you have a non detect capable drive you don't want it
4757 * to colour the choice
4759 ata_for_each_link(link, ap, EDGE) {
4760 ata_for_each_dev(dev, link, ENABLED) {
4761 if (!ata_is_40wire(dev))
4769 * ata_dev_xfermask - Compute supported xfermask of the given device
4770 * @dev: Device to compute xfermask for
4772 * Compute supported xfermask of @dev and store it in
4773 * dev->*_mask. This function is responsible for applying all
4774 * known limits including host controller limits, device
4780 static void ata_dev_xfermask(struct ata_device *dev)
4782 struct ata_link *link = dev->link;
4783 struct ata_port *ap = link->ap;
4784 struct ata_host *host = ap->host;
4785 unsigned long xfer_mask;
4787 /* controller modes available */
4788 xfer_mask = ata_pack_xfermask(ap->pio_mask,
4789 ap->mwdma_mask, ap->udma_mask);
4791 /* drive modes available */
4792 xfer_mask &= ata_pack_xfermask(dev->pio_mask,
4793 dev->mwdma_mask, dev->udma_mask);
4794 xfer_mask &= ata_id_xfermask(dev->id);
4797 * CFA Advanced TrueIDE timings are not allowed on a shared
4800 if (ata_dev_pair(dev)) {
4801 /* No PIO5 or PIO6 */
4802 xfer_mask &= ~(0x03 << (ATA_SHIFT_PIO + 5));
4803 /* No MWDMA3 or MWDMA 4 */
4804 xfer_mask &= ~(0x03 << (ATA_SHIFT_MWDMA + 3));
4807 if (ata_dma_blacklisted(dev)) {
4808 xfer_mask &= ~(ATA_MASK_MWDMA | ATA_MASK_UDMA);
4810 "device is on DMA blacklist, disabling DMA\n");
4813 if ((host->flags & ATA_HOST_SIMPLEX) &&
4814 host->simplex_claimed && host->simplex_claimed != ap) {
4815 xfer_mask &= ~(ATA_MASK_MWDMA | ATA_MASK_UDMA);
4817 "simplex DMA is claimed by other device, disabling DMA\n");
4820 if (ap->flags & ATA_FLAG_NO_IORDY)
4821 xfer_mask &= ata_pio_mask_no_iordy(dev);
4823 if (ap->ops->mode_filter)
4824 xfer_mask = ap->ops->mode_filter(dev, xfer_mask);
4826 /* Apply cable rule here. Don't apply it early because when
4827 * we handle hot plug the cable type can itself change.
4828 * Check this last so that we know if the transfer rate was
4829 * solely limited by the cable.
4830 * Unknown or 80 wire cables reported host side are checked
4831 * drive side as well. Cases where we know a 40wire cable
4832 * is used safely for 80 are not checked here.
4834 if (xfer_mask & (0xF8 << ATA_SHIFT_UDMA))
4835 /* UDMA/44 or higher would be available */
4836 if (cable_is_40wire(ap)) {
4838 "limited to UDMA/33 due to 40-wire cable\n");
4839 xfer_mask &= ~(0xF8 << ATA_SHIFT_UDMA);
4842 ata_unpack_xfermask(xfer_mask, &dev->pio_mask,
4843 &dev->mwdma_mask, &dev->udma_mask);
4847 * ata_dev_set_xfermode - Issue SET FEATURES - XFER MODE command
4848 * @dev: Device to which command will be sent
4850 * Issue SET FEATURES - XFER MODE command to device @dev
4854 * PCI/etc. bus probe sem.
4857 * 0 on success, AC_ERR_* mask otherwise.
4860 static unsigned int ata_dev_set_xfermode(struct ata_device *dev)
4862 struct ata_taskfile tf;
4863 unsigned int err_mask;
4865 /* set up set-features taskfile */
4866 DPRINTK("set features - xfer mode\n");
4868 /* Some controllers and ATAPI devices show flaky interrupt
4869 * behavior after setting xfer mode. Use polling instead.
4871 ata_tf_init(dev, &tf);
4872 tf.command = ATA_CMD_SET_FEATURES;
4873 tf.feature = SETFEATURES_XFER;
4874 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE | ATA_TFLAG_POLLING;
4875 tf.protocol = ATA_PROT_NODATA;
4876 /* If we are using IORDY we must send the mode setting command */
4877 if (ata_pio_need_iordy(dev))
4878 tf.nsect = dev->xfer_mode;
4879 /* If the device has IORDY and the controller does not - turn it off */
4880 else if (ata_id_has_iordy(dev->id))
4882 else /* In the ancient relic department - skip all of this */
4885 /* On some disks, this command causes spin-up, so we need longer timeout */
4886 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 15000);
4888 DPRINTK("EXIT, err_mask=%x\n", err_mask);
4893 * ata_dev_set_feature - Issue SET FEATURES - SATA FEATURES
4894 * @dev: Device to which command will be sent
4895 * @enable: Whether to enable or disable the feature
4896 * @feature: The sector count represents the feature to set
4898 * Issue SET FEATURES - SATA FEATURES command to device @dev
4899 * on port @ap with sector count
4902 * PCI/etc. bus probe sem.
4905 * 0 on success, AC_ERR_* mask otherwise.
4907 unsigned int ata_dev_set_feature(struct ata_device *dev, u8 enable, u8 feature)
4909 struct ata_taskfile tf;
4910 unsigned int err_mask;
4911 unsigned long timeout = 0;
4913 /* set up set-features taskfile */
4914 DPRINTK("set features - SATA features\n");
4916 ata_tf_init(dev, &tf);
4917 tf.command = ATA_CMD_SET_FEATURES;
4918 tf.feature = enable;
4919 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
4920 tf.protocol = ATA_PROT_NODATA;
4923 if (enable == SETFEATURES_SPINUP)
4924 timeout = ata_probe_timeout ?
4925 ata_probe_timeout * 1000 : SETFEATURES_SPINUP_TIMEOUT;
4926 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, timeout);
4928 DPRINTK("EXIT, err_mask=%x\n", err_mask);
4931 EXPORT_SYMBOL_GPL(ata_dev_set_feature);
4934 * ata_dev_init_params - Issue INIT DEV PARAMS command
4935 * @dev: Device to which command will be sent
4936 * @heads: Number of heads (taskfile parameter)
4937 * @sectors: Number of sectors (taskfile parameter)
4940 * Kernel thread context (may sleep)
4943 * 0 on success, AC_ERR_* mask otherwise.
4945 static unsigned int ata_dev_init_params(struct ata_device *dev,
4946 u16 heads, u16 sectors)
4948 struct ata_taskfile tf;
4949 unsigned int err_mask;
4951 /* Number of sectors per track 1-255. Number of heads 1-16 */
4952 if (sectors < 1 || sectors > 255 || heads < 1 || heads > 16)
4953 return AC_ERR_INVALID;
4955 /* set up init dev params taskfile */
4956 DPRINTK("init dev params \n");
4958 ata_tf_init(dev, &tf);
4959 tf.command = ATA_CMD_INIT_DEV_PARAMS;
4960 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
4961 tf.protocol = ATA_PROT_NODATA;
4963 tf.device |= (heads - 1) & 0x0f; /* max head = num. of heads - 1 */
4965 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
4966 /* A clean abort indicates an original or just out of spec drive
4967 and we should continue as we issue the setup based on the
4968 drive reported working geometry */
4969 if (err_mask == AC_ERR_DEV && (tf.feature & ATA_ABORTED))
4972 DPRINTK("EXIT, err_mask=%x\n", err_mask);
4977 * atapi_check_dma - Check whether ATAPI DMA can be supported
4978 * @qc: Metadata associated with taskfile to check
4980 * Allow low-level driver to filter ATA PACKET commands, returning
4981 * a status indicating whether or not it is OK to use DMA for the
4982 * supplied PACKET command.
4985 * spin_lock_irqsave(host lock)
4987 * RETURNS: 0 when ATAPI DMA can be used
4990 int atapi_check_dma(struct ata_queued_cmd *qc)
4992 struct ata_port *ap = qc->ap;
4994 /* Don't allow DMA if it isn't multiple of 16 bytes. Quite a
4995 * few ATAPI devices choke on such DMA requests.
4997 if (!(qc->dev->horkage & ATA_HORKAGE_ATAPI_MOD16_DMA) &&
4998 unlikely(qc->nbytes & 15))
5001 if (ap->ops->check_atapi_dma)
5002 return ap->ops->check_atapi_dma(qc);
5008 * ata_std_qc_defer - Check whether a qc needs to be deferred
5009 * @qc: ATA command in question
5011 * Non-NCQ commands cannot run with any other command, NCQ or
5012 * not. As upper layer only knows the queue depth, we are
5013 * responsible for maintaining exclusion. This function checks
5014 * whether a new command @qc can be issued.
5017 * spin_lock_irqsave(host lock)
5020 * ATA_DEFER_* if deferring is needed, 0 otherwise.
5022 int ata_std_qc_defer(struct ata_queued_cmd *qc)
5024 struct ata_link *link = qc->dev->link;
5026 if (ata_is_ncq(qc->tf.protocol)) {
5027 if (!ata_tag_valid(link->active_tag))
5030 if (!ata_tag_valid(link->active_tag) && !link->sactive)
5034 return ATA_DEFER_LINK;
5037 enum ata_completion_errors ata_noop_qc_prep(struct ata_queued_cmd *qc)
5043 * ata_sg_init - Associate command with scatter-gather table.
5044 * @qc: Command to be associated
5045 * @sg: Scatter-gather table.
5046 * @n_elem: Number of elements in s/g table.
5048 * Initialize the data-related elements of queued_cmd @qc
5049 * to point to a scatter-gather table @sg, containing @n_elem
5053 * spin_lock_irqsave(host lock)
5055 void ata_sg_init(struct ata_queued_cmd *qc, struct scatterlist *sg,
5056 unsigned int n_elem)
5059 qc->n_elem = n_elem;
5063 #ifdef CONFIG_HAS_DMA
5066 * ata_sg_clean - Unmap DMA memory associated with command
5067 * @qc: Command containing DMA memory to be released
5069 * Unmap all mapped DMA memory associated with this command.
5072 * spin_lock_irqsave(host lock)
5074 static void ata_sg_clean(struct ata_queued_cmd *qc)
5076 struct ata_port *ap = qc->ap;
5077 struct scatterlist *sg = qc->sg;
5078 int dir = qc->dma_dir;
5080 WARN_ON_ONCE(sg == NULL);
5082 VPRINTK("unmapping %u sg elements\n", qc->n_elem);
5085 dma_unmap_sg(ap->dev, sg, qc->orig_n_elem, dir);
5087 qc->flags &= ~ATA_QCFLAG_DMAMAP;
5092 * ata_sg_setup - DMA-map the scatter-gather table associated with a command.
5093 * @qc: Command with scatter-gather table to be mapped.
5095 * DMA-map the scatter-gather table associated with queued_cmd @qc.
5098 * spin_lock_irqsave(host lock)
5101 * Zero on success, negative on error.
5104 static int ata_sg_setup(struct ata_queued_cmd *qc)
5106 struct ata_port *ap = qc->ap;
5107 unsigned int n_elem;
5109 VPRINTK("ENTER, ata%u\n", ap->print_id);
5111 n_elem = dma_map_sg(ap->dev, qc->sg, qc->n_elem, qc->dma_dir);
5115 VPRINTK("%d sg elements mapped\n", n_elem);
5116 qc->orig_n_elem = qc->n_elem;
5117 qc->n_elem = n_elem;
5118 qc->flags |= ATA_QCFLAG_DMAMAP;
5123 #else /* !CONFIG_HAS_DMA */
5125 static inline void ata_sg_clean(struct ata_queued_cmd *qc) {}
5126 static inline int ata_sg_setup(struct ata_queued_cmd *qc) { return -1; }
5128 #endif /* !CONFIG_HAS_DMA */
5131 * swap_buf_le16 - swap halves of 16-bit words in place
5132 * @buf: Buffer to swap
5133 * @buf_words: Number of 16-bit words in buffer.
5135 * Swap halves of 16-bit words if needed to convert from
5136 * little-endian byte order to native cpu byte order, or
5140 * Inherited from caller.
5142 void swap_buf_le16(u16 *buf, unsigned int buf_words)
5147 for (i = 0; i < buf_words; i++)
5148 buf[i] = le16_to_cpu(buf[i]);
5149 #endif /* __BIG_ENDIAN */
5153 * ata_qc_new_init - Request an available ATA command, and initialize it
5154 * @dev: Device from whom we request an available command structure
5161 struct ata_queued_cmd *ata_qc_new_init(struct ata_device *dev, int tag)
5163 struct ata_port *ap = dev->link->ap;
5164 struct ata_queued_cmd *qc;
5166 /* no command while frozen */
5167 if (unlikely(ap->pflags & ATA_PFLAG_FROZEN))
5171 if (ap->flags & ATA_FLAG_SAS_HOST) {
5172 tag = ata_sas_allocate_tag(ap);
5177 qc = __ata_qc_from_tag(ap, tag);
5178 qc->tag = qc->hw_tag = tag;
5189 * ata_qc_free - free unused ata_queued_cmd
5190 * @qc: Command to complete
5192 * Designed to free unused ata_queued_cmd object
5193 * in case something prevents using it.
5196 * spin_lock_irqsave(host lock)
5198 void ata_qc_free(struct ata_queued_cmd *qc)
5200 struct ata_port *ap;
5203 WARN_ON_ONCE(qc == NULL); /* ata_qc_from_tag _might_ return NULL */
5208 if (ata_tag_valid(tag)) {
5209 qc->tag = ATA_TAG_POISON;
5210 if (ap->flags & ATA_FLAG_SAS_HOST)
5211 ata_sas_free_tag(tag, ap);
5215 void __ata_qc_complete(struct ata_queued_cmd *qc)
5217 struct ata_port *ap;
5218 struct ata_link *link;
5220 WARN_ON_ONCE(qc == NULL); /* ata_qc_from_tag _might_ return NULL */
5221 WARN_ON_ONCE(!(qc->flags & ATA_QCFLAG_ACTIVE));
5223 link = qc->dev->link;
5225 if (likely(qc->flags & ATA_QCFLAG_DMAMAP))
5228 /* command should be marked inactive atomically with qc completion */
5229 if (ata_is_ncq(qc->tf.protocol)) {
5230 link->sactive &= ~(1 << qc->hw_tag);
5232 ap->nr_active_links--;
5234 link->active_tag = ATA_TAG_POISON;
5235 ap->nr_active_links--;
5238 /* clear exclusive status */
5239 if (unlikely(qc->flags & ATA_QCFLAG_CLEAR_EXCL &&
5240 ap->excl_link == link))
5241 ap->excl_link = NULL;
5243 /* atapi: mark qc as inactive to prevent the interrupt handler
5244 * from completing the command twice later, before the error handler
5245 * is called. (when rc != 0 and atapi request sense is needed)
5247 qc->flags &= ~ATA_QCFLAG_ACTIVE;
5248 ap->qc_active &= ~(1ULL << qc->tag);
5250 /* call completion callback */
5251 qc->complete_fn(qc);
5254 static void fill_result_tf(struct ata_queued_cmd *qc)
5256 struct ata_port *ap = qc->ap;
5258 qc->result_tf.flags = qc->tf.flags;
5259 ap->ops->qc_fill_rtf(qc);
5262 static void ata_verify_xfer(struct ata_queued_cmd *qc)
5264 struct ata_device *dev = qc->dev;
5266 if (!ata_is_data(qc->tf.protocol))
5269 if ((dev->mwdma_mask || dev->udma_mask) && ata_is_pio(qc->tf.protocol))
5272 dev->flags &= ~ATA_DFLAG_DUBIOUS_XFER;
5276 * ata_qc_complete - Complete an active ATA command
5277 * @qc: Command to complete
5279 * Indicate to the mid and upper layers that an ATA command has
5280 * completed, with either an ok or not-ok status.
5282 * Refrain from calling this function multiple times when
5283 * successfully completing multiple NCQ commands.
5284 * ata_qc_complete_multiple() should be used instead, which will
5285 * properly update IRQ expect state.
5288 * spin_lock_irqsave(host lock)
5290 void ata_qc_complete(struct ata_queued_cmd *qc)
5292 struct ata_port *ap = qc->ap;
5294 /* Trigger the LED (if available) */
5295 ledtrig_disk_activity(!!(qc->tf.flags & ATA_TFLAG_WRITE));
5297 /* XXX: New EH and old EH use different mechanisms to
5298 * synchronize EH with regular execution path.
5300 * In new EH, a failed qc is marked with ATA_QCFLAG_FAILED.
5301 * Normal execution path is responsible for not accessing a
5302 * failed qc. libata core enforces the rule by returning NULL
5303 * from ata_qc_from_tag() for failed qcs.
5305 * Old EH depends on ata_qc_complete() nullifying completion
5306 * requests if ATA_QCFLAG_EH_SCHEDULED is set. Old EH does
5307 * not synchronize with interrupt handler. Only PIO task is
5310 if (ap->ops->error_handler) {
5311 struct ata_device *dev = qc->dev;
5312 struct ata_eh_info *ehi = &dev->link->eh_info;
5314 if (unlikely(qc->err_mask))
5315 qc->flags |= ATA_QCFLAG_FAILED;
5318 * Finish internal commands without any further processing
5319 * and always with the result TF filled.
5321 if (unlikely(ata_tag_internal(qc->tag))) {
5323 trace_ata_qc_complete_internal(qc);
5324 __ata_qc_complete(qc);
5329 * Non-internal qc has failed. Fill the result TF and
5332 if (unlikely(qc->flags & ATA_QCFLAG_FAILED)) {
5334 trace_ata_qc_complete_failed(qc);
5335 ata_qc_schedule_eh(qc);
5339 WARN_ON_ONCE(ap->pflags & ATA_PFLAG_FROZEN);
5341 /* read result TF if requested */
5342 if (qc->flags & ATA_QCFLAG_RESULT_TF)
5345 trace_ata_qc_complete_done(qc);
5346 /* Some commands need post-processing after successful
5349 switch (qc->tf.command) {
5350 case ATA_CMD_SET_FEATURES:
5351 if (qc->tf.feature != SETFEATURES_WC_ON &&
5352 qc->tf.feature != SETFEATURES_WC_OFF &&
5353 qc->tf.feature != SETFEATURES_RA_ON &&
5354 qc->tf.feature != SETFEATURES_RA_OFF)
5357 case ATA_CMD_INIT_DEV_PARAMS: /* CHS translation changed */
5358 case ATA_CMD_SET_MULTI: /* multi_count changed */
5359 /* revalidate device */
5360 ehi->dev_action[dev->devno] |= ATA_EH_REVALIDATE;
5361 ata_port_schedule_eh(ap);
5365 dev->flags |= ATA_DFLAG_SLEEPING;
5369 if (unlikely(dev->flags & ATA_DFLAG_DUBIOUS_XFER))
5370 ata_verify_xfer(qc);
5372 __ata_qc_complete(qc);
5374 if (qc->flags & ATA_QCFLAG_EH_SCHEDULED)
5377 /* read result TF if failed or requested */
5378 if (qc->err_mask || qc->flags & ATA_QCFLAG_RESULT_TF)
5381 __ata_qc_complete(qc);
5386 * ata_qc_get_active - get bitmask of active qcs
5387 * @ap: port in question
5390 * spin_lock_irqsave(host lock)
5393 * Bitmask of active qcs
5395 u64 ata_qc_get_active(struct ata_port *ap)
5397 u64 qc_active = ap->qc_active;
5399 /* ATA_TAG_INTERNAL is sent to hw as tag 0 */
5400 if (qc_active & (1ULL << ATA_TAG_INTERNAL)) {
5401 qc_active |= (1 << 0);
5402 qc_active &= ~(1ULL << ATA_TAG_INTERNAL);
5407 EXPORT_SYMBOL_GPL(ata_qc_get_active);
5410 * ata_qc_complete_multiple - Complete multiple qcs successfully
5411 * @ap: port in question
5412 * @qc_active: new qc_active mask
5414 * Complete in-flight commands. This functions is meant to be
5415 * called from low-level driver's interrupt routine to complete
5416 * requests normally. ap->qc_active and @qc_active is compared
5417 * and commands are completed accordingly.
5419 * Always use this function when completing multiple NCQ commands
5420 * from IRQ handlers instead of calling ata_qc_complete()
5421 * multiple times to keep IRQ expect status properly in sync.
5424 * spin_lock_irqsave(host lock)
5427 * Number of completed commands on success, -errno otherwise.
5429 int ata_qc_complete_multiple(struct ata_port *ap, u64 qc_active)
5431 u64 done_mask, ap_qc_active = ap->qc_active;
5435 * If the internal tag is set on ap->qc_active, then we care about
5436 * bit0 on the passed in qc_active mask. Move that bit up to match
5439 if (ap_qc_active & (1ULL << ATA_TAG_INTERNAL)) {
5440 qc_active |= (qc_active & 0x01) << ATA_TAG_INTERNAL;
5441 qc_active ^= qc_active & 0x01;
5444 done_mask = ap_qc_active ^ qc_active;
5446 if (unlikely(done_mask & qc_active)) {
5447 ata_port_err(ap, "illegal qc_active transition (%08llx->%08llx)\n",
5448 ap->qc_active, qc_active);
5453 struct ata_queued_cmd *qc;
5454 unsigned int tag = __ffs64(done_mask);
5456 qc = ata_qc_from_tag(ap, tag);
5458 ata_qc_complete(qc);
5461 done_mask &= ~(1ULL << tag);
5468 * ata_qc_issue - issue taskfile to device
5469 * @qc: command to issue to device
5471 * Prepare an ATA command to submission to device.
5472 * This includes mapping the data into a DMA-able
5473 * area, filling in the S/G table, and finally
5474 * writing the taskfile to hardware, starting the command.
5477 * spin_lock_irqsave(host lock)
5479 void ata_qc_issue(struct ata_queued_cmd *qc)
5481 struct ata_port *ap = qc->ap;
5482 struct ata_link *link = qc->dev->link;
5483 u8 prot = qc->tf.protocol;
5485 /* Make sure only one non-NCQ command is outstanding. The
5486 * check is skipped for old EH because it reuses active qc to
5487 * request ATAPI sense.
5489 WARN_ON_ONCE(ap->ops->error_handler && ata_tag_valid(link->active_tag));
5491 if (ata_is_ncq(prot)) {
5492 WARN_ON_ONCE(link->sactive & (1 << qc->hw_tag));
5495 ap->nr_active_links++;
5496 link->sactive |= 1 << qc->hw_tag;
5498 WARN_ON_ONCE(link->sactive);
5500 ap->nr_active_links++;
5501 link->active_tag = qc->tag;
5504 qc->flags |= ATA_QCFLAG_ACTIVE;
5505 ap->qc_active |= 1ULL << qc->tag;
5508 * We guarantee to LLDs that they will have at least one
5509 * non-zero sg if the command is a data command.
5511 if (ata_is_data(prot) && (!qc->sg || !qc->n_elem || !qc->nbytes))
5514 if (ata_is_dma(prot) || (ata_is_pio(prot) &&
5515 (ap->flags & ATA_FLAG_PIO_DMA)))
5516 if (ata_sg_setup(qc))
5519 /* if device is sleeping, schedule reset and abort the link */
5520 if (unlikely(qc->dev->flags & ATA_DFLAG_SLEEPING)) {
5521 link->eh_info.action |= ATA_EH_RESET;
5522 ata_ehi_push_desc(&link->eh_info, "waking up from sleep");
5523 ata_link_abort(link);
5527 qc->err_mask |= ap->ops->qc_prep(qc);
5528 if (unlikely(qc->err_mask))
5530 trace_ata_qc_issue(qc);
5531 qc->err_mask |= ap->ops->qc_issue(qc);
5532 if (unlikely(qc->err_mask))
5537 qc->err_mask |= AC_ERR_SYSTEM;
5539 ata_qc_complete(qc);
5543 * sata_scr_valid - test whether SCRs are accessible
5544 * @link: ATA link to test SCR accessibility for
5546 * Test whether SCRs are accessible for @link.
5552 * 1 if SCRs are accessible, 0 otherwise.
5554 int sata_scr_valid(struct ata_link *link)
5556 struct ata_port *ap = link->ap;
5558 return (ap->flags & ATA_FLAG_SATA) && ap->ops->scr_read;
5562 * sata_scr_read - read SCR register of the specified port
5563 * @link: ATA link to read SCR for
5565 * @val: Place to store read value
5567 * Read SCR register @reg of @link into *@val. This function is
5568 * guaranteed to succeed if @link is ap->link, the cable type of
5569 * the port is SATA and the port implements ->scr_read.
5572 * None if @link is ap->link. Kernel thread context otherwise.
5575 * 0 on success, negative errno on failure.
5577 int sata_scr_read(struct ata_link *link, int reg, u32 *val)
5579 if (ata_is_host_link(link)) {
5580 if (sata_scr_valid(link))
5581 return link->ap->ops->scr_read(link, reg, val);
5585 return sata_pmp_scr_read(link, reg, val);
5589 * sata_scr_write - write SCR register of the specified port
5590 * @link: ATA link to write SCR for
5591 * @reg: SCR to write
5592 * @val: value to write
5594 * Write @val to SCR register @reg of @link. This function is
5595 * guaranteed to succeed if @link is ap->link, the cable type of
5596 * the port is SATA and the port implements ->scr_read.
5599 * None if @link is ap->link. Kernel thread context otherwise.
5602 * 0 on success, negative errno on failure.
5604 int sata_scr_write(struct ata_link *link, int reg, u32 val)
5606 if (ata_is_host_link(link)) {
5607 if (sata_scr_valid(link))
5608 return link->ap->ops->scr_write(link, reg, val);
5612 return sata_pmp_scr_write(link, reg, val);
5616 * sata_scr_write_flush - write SCR register of the specified port and flush
5617 * @link: ATA link to write SCR for
5618 * @reg: SCR to write
5619 * @val: value to write
5621 * This function is identical to sata_scr_write() except that this
5622 * function performs flush after writing to the register.
5625 * None if @link is ap->link. Kernel thread context otherwise.
5628 * 0 on success, negative errno on failure.
5630 int sata_scr_write_flush(struct ata_link *link, int reg, u32 val)
5632 if (ata_is_host_link(link)) {
5635 if (sata_scr_valid(link)) {
5636 rc = link->ap->ops->scr_write(link, reg, val);
5638 rc = link->ap->ops->scr_read(link, reg, &val);
5644 return sata_pmp_scr_write(link, reg, val);
5648 * ata_phys_link_online - test whether the given link is online
5649 * @link: ATA link to test
5651 * Test whether @link is online. Note that this function returns
5652 * 0 if online status of @link cannot be obtained, so
5653 * ata_link_online(link) != !ata_link_offline(link).
5659 * True if the port online status is available and online.
5661 bool ata_phys_link_online(struct ata_link *link)
5665 if (sata_scr_read(link, SCR_STATUS, &sstatus) == 0 &&
5666 ata_sstatus_online(sstatus))
5672 * ata_phys_link_offline - test whether the given link is offline
5673 * @link: ATA link to test
5675 * Test whether @link is offline. Note that this function
5676 * returns 0 if offline status of @link cannot be obtained, so
5677 * ata_link_online(link) != !ata_link_offline(link).
5683 * True if the port offline status is available and offline.
5685 bool ata_phys_link_offline(struct ata_link *link)
5689 if (sata_scr_read(link, SCR_STATUS, &sstatus) == 0 &&
5690 !ata_sstatus_online(sstatus))
5696 * ata_link_online - test whether the given link is online
5697 * @link: ATA link to test
5699 * Test whether @link is online. This is identical to
5700 * ata_phys_link_online() when there's no slave link. When
5701 * there's a slave link, this function should only be called on
5702 * the master link and will return true if any of M/S links is
5709 * True if the port online status is available and online.
5711 bool ata_link_online(struct ata_link *link)
5713 struct ata_link *slave = link->ap->slave_link;
5715 WARN_ON(link == slave); /* shouldn't be called on slave link */
5717 return ata_phys_link_online(link) ||
5718 (slave && ata_phys_link_online(slave));
5722 * ata_link_offline - test whether the given link is offline
5723 * @link: ATA link to test
5725 * Test whether @link is offline. This is identical to
5726 * ata_phys_link_offline() when there's no slave link. When
5727 * there's a slave link, this function should only be called on
5728 * the master link and will return true if both M/S links are
5735 * True if the port offline status is available and offline.
5737 bool ata_link_offline(struct ata_link *link)
5739 struct ata_link *slave = link->ap->slave_link;
5741 WARN_ON(link == slave); /* shouldn't be called on slave link */
5743 return ata_phys_link_offline(link) &&
5744 (!slave || ata_phys_link_offline(slave));
5748 static void ata_port_request_pm(struct ata_port *ap, pm_message_t mesg,
5749 unsigned int action, unsigned int ehi_flags,
5752 struct ata_link *link;
5753 unsigned long flags;
5755 /* Previous resume operation might still be in
5756 * progress. Wait for PM_PENDING to clear.
5758 if (ap->pflags & ATA_PFLAG_PM_PENDING) {
5759 ata_port_wait_eh(ap);
5760 WARN_ON(ap->pflags & ATA_PFLAG_PM_PENDING);
5763 /* request PM ops to EH */
5764 spin_lock_irqsave(ap->lock, flags);
5767 ap->pflags |= ATA_PFLAG_PM_PENDING;
5768 ata_for_each_link(link, ap, HOST_FIRST) {
5769 link->eh_info.action |= action;
5770 link->eh_info.flags |= ehi_flags;
5773 ata_port_schedule_eh(ap);
5775 spin_unlock_irqrestore(ap->lock, flags);
5778 ata_port_wait_eh(ap);
5779 WARN_ON(ap->pflags & ATA_PFLAG_PM_PENDING);
5784 * On some hardware, device fails to respond after spun down for suspend. As
5785 * the device won't be used before being resumed, we don't need to touch the
5786 * device. Ask EH to skip the usual stuff and proceed directly to suspend.
5788 * http://thread.gmane.org/gmane.linux.ide/46764
5790 static const unsigned int ata_port_suspend_ehi = ATA_EHI_QUIET
5791 | ATA_EHI_NO_AUTOPSY
5792 | ATA_EHI_NO_RECOVERY;
5794 static void ata_port_suspend(struct ata_port *ap, pm_message_t mesg)
5796 ata_port_request_pm(ap, mesg, 0, ata_port_suspend_ehi, false);
5799 static void ata_port_suspend_async(struct ata_port *ap, pm_message_t mesg)
5801 ata_port_request_pm(ap, mesg, 0, ata_port_suspend_ehi, true);
5804 static int ata_port_pm_suspend(struct device *dev)
5806 struct ata_port *ap = to_ata_port(dev);
5808 if (pm_runtime_suspended(dev))
5811 ata_port_suspend(ap, PMSG_SUSPEND);
5815 static int ata_port_pm_freeze(struct device *dev)
5817 struct ata_port *ap = to_ata_port(dev);
5819 if (pm_runtime_suspended(dev))
5822 ata_port_suspend(ap, PMSG_FREEZE);
5826 static int ata_port_pm_poweroff(struct device *dev)
5828 ata_port_suspend(to_ata_port(dev), PMSG_HIBERNATE);
5832 static const unsigned int ata_port_resume_ehi = ATA_EHI_NO_AUTOPSY
5835 static void ata_port_resume(struct ata_port *ap, pm_message_t mesg)
5837 ata_port_request_pm(ap, mesg, ATA_EH_RESET, ata_port_resume_ehi, false);
5840 static void ata_port_resume_async(struct ata_port *ap, pm_message_t mesg)
5842 ata_port_request_pm(ap, mesg, ATA_EH_RESET, ata_port_resume_ehi, true);
5845 static int ata_port_pm_resume(struct device *dev)
5847 ata_port_resume_async(to_ata_port(dev), PMSG_RESUME);
5848 pm_runtime_disable(dev);
5849 pm_runtime_set_active(dev);
5850 pm_runtime_enable(dev);
5855 * For ODDs, the upper layer will poll for media change every few seconds,
5856 * which will make it enter and leave suspend state every few seconds. And
5857 * as each suspend will cause a hard/soft reset, the gain of runtime suspend
5858 * is very little and the ODD may malfunction after constantly being reset.
5859 * So the idle callback here will not proceed to suspend if a non-ZPODD capable
5860 * ODD is attached to the port.
5862 static int ata_port_runtime_idle(struct device *dev)
5864 struct ata_port *ap = to_ata_port(dev);
5865 struct ata_link *link;
5866 struct ata_device *adev;
5868 ata_for_each_link(link, ap, HOST_FIRST) {
5869 ata_for_each_dev(adev, link, ENABLED)
5870 if (adev->class == ATA_DEV_ATAPI &&
5871 !zpodd_dev_enabled(adev))
5878 static int ata_port_runtime_suspend(struct device *dev)
5880 ata_port_suspend(to_ata_port(dev), PMSG_AUTO_SUSPEND);
5884 static int ata_port_runtime_resume(struct device *dev)
5886 ata_port_resume(to_ata_port(dev), PMSG_AUTO_RESUME);
5890 static const struct dev_pm_ops ata_port_pm_ops = {
5891 .suspend = ata_port_pm_suspend,
5892 .resume = ata_port_pm_resume,
5893 .freeze = ata_port_pm_freeze,
5894 .thaw = ata_port_pm_resume,
5895 .poweroff = ata_port_pm_poweroff,
5896 .restore = ata_port_pm_resume,
5898 .runtime_suspend = ata_port_runtime_suspend,
5899 .runtime_resume = ata_port_runtime_resume,
5900 .runtime_idle = ata_port_runtime_idle,
5903 /* sas ports don't participate in pm runtime management of ata_ports,
5904 * and need to resume ata devices at the domain level, not the per-port
5905 * level. sas suspend/resume is async to allow parallel port recovery
5906 * since sas has multiple ata_port instances per Scsi_Host.
5908 void ata_sas_port_suspend(struct ata_port *ap)
5910 ata_port_suspend_async(ap, PMSG_SUSPEND);
5912 EXPORT_SYMBOL_GPL(ata_sas_port_suspend);
5914 void ata_sas_port_resume(struct ata_port *ap)
5916 ata_port_resume_async(ap, PMSG_RESUME);
5918 EXPORT_SYMBOL_GPL(ata_sas_port_resume);
5921 * ata_host_suspend - suspend host
5922 * @host: host to suspend
5925 * Suspend @host. Actual operation is performed by port suspend.
5927 int ata_host_suspend(struct ata_host *host, pm_message_t mesg)
5929 host->dev->power.power_state = mesg;
5934 * ata_host_resume - resume host
5935 * @host: host to resume
5937 * Resume @host. Actual operation is performed by port resume.
5939 void ata_host_resume(struct ata_host *host)
5941 host->dev->power.power_state = PMSG_ON;
5945 const struct device_type ata_port_type = {
5948 .pm = &ata_port_pm_ops,
5953 * ata_dev_init - Initialize an ata_device structure
5954 * @dev: Device structure to initialize
5956 * Initialize @dev in preparation for probing.
5959 * Inherited from caller.
5961 void ata_dev_init(struct ata_device *dev)
5963 struct ata_link *link = ata_dev_phys_link(dev);
5964 struct ata_port *ap = link->ap;
5965 unsigned long flags;
5967 /* SATA spd limit is bound to the attached device, reset together */
5968 link->sata_spd_limit = link->hw_sata_spd_limit;
5971 /* High bits of dev->flags are used to record warm plug
5972 * requests which occur asynchronously. Synchronize using
5975 spin_lock_irqsave(ap->lock, flags);
5976 dev->flags &= ~ATA_DFLAG_INIT_MASK;
5978 spin_unlock_irqrestore(ap->lock, flags);
5980 memset((void *)dev + ATA_DEVICE_CLEAR_BEGIN, 0,
5981 ATA_DEVICE_CLEAR_END - ATA_DEVICE_CLEAR_BEGIN);
5982 dev->pio_mask = UINT_MAX;
5983 dev->mwdma_mask = UINT_MAX;
5984 dev->udma_mask = UINT_MAX;
5988 * ata_link_init - Initialize an ata_link structure
5989 * @ap: ATA port link is attached to
5990 * @link: Link structure to initialize
5991 * @pmp: Port multiplier port number
5996 * Kernel thread context (may sleep)
5998 void ata_link_init(struct ata_port *ap, struct ata_link *link, int pmp)
6002 /* clear everything except for devices */
6003 memset((void *)link + ATA_LINK_CLEAR_BEGIN, 0,
6004 ATA_LINK_CLEAR_END - ATA_LINK_CLEAR_BEGIN);
6008 link->active_tag = ATA_TAG_POISON;
6009 link->hw_sata_spd_limit = UINT_MAX;
6011 /* can't use iterator, ap isn't initialized yet */
6012 for (i = 0; i < ATA_MAX_DEVICES; i++) {
6013 struct ata_device *dev = &link->device[i];
6016 dev->devno = dev - link->device;
6017 #ifdef CONFIG_ATA_ACPI
6018 dev->gtf_filter = ata_acpi_gtf_filter;
6025 * sata_link_init_spd - Initialize link->sata_spd_limit
6026 * @link: Link to configure sata_spd_limit for
6028 * Initialize @link->[hw_]sata_spd_limit to the currently
6032 * Kernel thread context (may sleep).
6035 * 0 on success, -errno on failure.
6037 int sata_link_init_spd(struct ata_link *link)
6042 rc = sata_scr_read(link, SCR_CONTROL, &link->saved_scontrol);
6046 spd = (link->saved_scontrol >> 4) & 0xf;
6048 link->hw_sata_spd_limit &= (1 << spd) - 1;
6050 ata_force_link_limits(link);
6052 link->sata_spd_limit = link->hw_sata_spd_limit;
6058 * ata_port_alloc - allocate and initialize basic ATA port resources
6059 * @host: ATA host this allocated port belongs to
6061 * Allocate and initialize basic ATA port resources.
6064 * Allocate ATA port on success, NULL on failure.
6067 * Inherited from calling layer (may sleep).
6069 struct ata_port *ata_port_alloc(struct ata_host *host)
6071 struct ata_port *ap;
6075 ap = kzalloc(sizeof(*ap), GFP_KERNEL);
6079 ap->pflags |= ATA_PFLAG_INITIALIZING | ATA_PFLAG_FROZEN;
6080 ap->lock = &host->lock;
6082 ap->local_port_no = -1;
6084 ap->dev = host->dev;
6086 #if defined(ATA_VERBOSE_DEBUG)
6087 /* turn on all debugging levels */
6088 ap->msg_enable = 0x00FF;
6089 #elif defined(ATA_DEBUG)
6090 ap->msg_enable = ATA_MSG_DRV | ATA_MSG_INFO | ATA_MSG_CTL | ATA_MSG_WARN | ATA_MSG_ERR;
6092 ap->msg_enable = ATA_MSG_DRV | ATA_MSG_ERR | ATA_MSG_WARN;
6095 mutex_init(&ap->scsi_scan_mutex);
6096 INIT_DELAYED_WORK(&ap->hotplug_task, ata_scsi_hotplug);
6097 INIT_WORK(&ap->scsi_rescan_task, ata_scsi_dev_rescan);
6098 INIT_LIST_HEAD(&ap->eh_done_q);
6099 init_waitqueue_head(&ap->eh_wait_q);
6100 init_completion(&ap->park_req_pending);
6101 timer_setup(&ap->fastdrain_timer, ata_eh_fastdrain_timerfn,
6104 ap->cbl = ATA_CBL_NONE;
6106 ata_link_init(ap, &ap->link, 0);
6109 ap->stats.unhandled_irq = 1;
6110 ap->stats.idle_irq = 1;
6112 ata_sff_port_init(ap);
6117 static void ata_devres_release(struct device *gendev, void *res)
6119 struct ata_host *host = dev_get_drvdata(gendev);
6122 for (i = 0; i < host->n_ports; i++) {
6123 struct ata_port *ap = host->ports[i];
6129 scsi_host_put(ap->scsi_host);
6133 dev_set_drvdata(gendev, NULL);
6137 static void ata_host_release(struct kref *kref)
6139 struct ata_host *host = container_of(kref, struct ata_host, kref);
6142 for (i = 0; i < host->n_ports; i++) {
6143 struct ata_port *ap = host->ports[i];
6145 kfree(ap->pmp_link);
6146 kfree(ap->slave_link);
6148 host->ports[i] = NULL;
6153 void ata_host_get(struct ata_host *host)
6155 kref_get(&host->kref);
6158 void ata_host_put(struct ata_host *host)
6160 kref_put(&host->kref, ata_host_release);
6164 * ata_host_alloc - allocate and init basic ATA host resources
6165 * @dev: generic device this host is associated with
6166 * @max_ports: maximum number of ATA ports associated with this host
6168 * Allocate and initialize basic ATA host resources. LLD calls
6169 * this function to allocate a host, initializes it fully and
6170 * attaches it using ata_host_register().
6172 * @max_ports ports are allocated and host->n_ports is
6173 * initialized to @max_ports. The caller is allowed to decrease
6174 * host->n_ports before calling ata_host_register(). The unused
6175 * ports will be automatically freed on registration.
6178 * Allocate ATA host on success, NULL on failure.
6181 * Inherited from calling layer (may sleep).
6183 struct ata_host *ata_host_alloc(struct device *dev, int max_ports)
6185 struct ata_host *host;
6192 /* alloc a container for our list of ATA ports (buses) */
6193 sz = sizeof(struct ata_host) + (max_ports + 1) * sizeof(void *);
6194 host = kzalloc(sz, GFP_KERNEL);
6198 if (!devres_open_group(dev, NULL, GFP_KERNEL))
6201 dr = devres_alloc(ata_devres_release, 0, GFP_KERNEL);
6205 devres_add(dev, dr);
6206 dev_set_drvdata(dev, host);
6208 spin_lock_init(&host->lock);
6209 mutex_init(&host->eh_mutex);
6211 host->n_ports = max_ports;
6212 kref_init(&host->kref);
6214 /* allocate ports bound to this host */
6215 for (i = 0; i < max_ports; i++) {
6216 struct ata_port *ap;
6218 ap = ata_port_alloc(host);
6223 host->ports[i] = ap;
6226 devres_remove_group(dev, NULL);
6230 devres_release_group(dev, NULL);
6237 * ata_host_alloc_pinfo - alloc host and init with port_info array
6238 * @dev: generic device this host is associated with
6239 * @ppi: array of ATA port_info to initialize host with
6240 * @n_ports: number of ATA ports attached to this host
6242 * Allocate ATA host and initialize with info from @ppi. If NULL
6243 * terminated, @ppi may contain fewer entries than @n_ports. The
6244 * last entry will be used for the remaining ports.
6247 * Allocate ATA host on success, NULL on failure.
6250 * Inherited from calling layer (may sleep).
6252 struct ata_host *ata_host_alloc_pinfo(struct device *dev,
6253 const struct ata_port_info * const * ppi,
6256 const struct ata_port_info *pi;
6257 struct ata_host *host;
6260 host = ata_host_alloc(dev, n_ports);
6264 for (i = 0, j = 0, pi = NULL; i < host->n_ports; i++) {
6265 struct ata_port *ap = host->ports[i];
6270 ap->pio_mask = pi->pio_mask;
6271 ap->mwdma_mask = pi->mwdma_mask;
6272 ap->udma_mask = pi->udma_mask;
6273 ap->flags |= pi->flags;
6274 ap->link.flags |= pi->link_flags;
6275 ap->ops = pi->port_ops;
6277 if (!host->ops && (pi->port_ops != &ata_dummy_port_ops))
6278 host->ops = pi->port_ops;
6285 * ata_slave_link_init - initialize slave link
6286 * @ap: port to initialize slave link for
6288 * Create and initialize slave link for @ap. This enables slave
6289 * link handling on the port.
6291 * In libata, a port contains links and a link contains devices.
6292 * There is single host link but if a PMP is attached to it,
6293 * there can be multiple fan-out links. On SATA, there's usually
6294 * a single device connected to a link but PATA and SATA
6295 * controllers emulating TF based interface can have two - master
6298 * However, there are a few controllers which don't fit into this
6299 * abstraction too well - SATA controllers which emulate TF
6300 * interface with both master and slave devices but also have
6301 * separate SCR register sets for each device. These controllers
6302 * need separate links for physical link handling
6303 * (e.g. onlineness, link speed) but should be treated like a
6304 * traditional M/S controller for everything else (e.g. command
6305 * issue, softreset).
6307 * slave_link is libata's way of handling this class of
6308 * controllers without impacting core layer too much. For
6309 * anything other than physical link handling, the default host
6310 * link is used for both master and slave. For physical link
6311 * handling, separate @ap->slave_link is used. All dirty details
6312 * are implemented inside libata core layer. From LLD's POV, the
6313 * only difference is that prereset, hardreset and postreset are
6314 * called once more for the slave link, so the reset sequence
6315 * looks like the following.
6317 * prereset(M) -> prereset(S) -> hardreset(M) -> hardreset(S) ->
6318 * softreset(M) -> postreset(M) -> postreset(S)
6320 * Note that softreset is called only for the master. Softreset
6321 * resets both M/S by definition, so SRST on master should handle
6322 * both (the standard method will work just fine).
6325 * Should be called before host is registered.
6328 * 0 on success, -errno on failure.
6330 int ata_slave_link_init(struct ata_port *ap)
6332 struct ata_link *link;
6334 WARN_ON(ap->slave_link);
6335 WARN_ON(ap->flags & ATA_FLAG_PMP);
6337 link = kzalloc(sizeof(*link), GFP_KERNEL);
6341 ata_link_init(ap, link, 1);
6342 ap->slave_link = link;
6346 static void ata_host_stop(struct device *gendev, void *res)
6348 struct ata_host *host = dev_get_drvdata(gendev);
6351 WARN_ON(!(host->flags & ATA_HOST_STARTED));
6353 for (i = 0; i < host->n_ports; i++) {
6354 struct ata_port *ap = host->ports[i];
6356 if (ap->ops->port_stop)
6357 ap->ops->port_stop(ap);
6360 if (host->ops->host_stop)
6361 host->ops->host_stop(host);
6365 * ata_finalize_port_ops - finalize ata_port_operations
6366 * @ops: ata_port_operations to finalize
6368 * An ata_port_operations can inherit from another ops and that
6369 * ops can again inherit from another. This can go on as many
6370 * times as necessary as long as there is no loop in the
6371 * inheritance chain.
6373 * Ops tables are finalized when the host is started. NULL or
6374 * unspecified entries are inherited from the closet ancestor
6375 * which has the method and the entry is populated with it.
6376 * After finalization, the ops table directly points to all the
6377 * methods and ->inherits is no longer necessary and cleared.
6379 * Using ATA_OP_NULL, inheriting ops can force a method to NULL.
6384 static void ata_finalize_port_ops(struct ata_port_operations *ops)
6386 static DEFINE_SPINLOCK(lock);
6387 const struct ata_port_operations *cur;
6388 void **begin = (void **)ops;
6389 void **end = (void **)&ops->inherits;
6392 if (!ops || !ops->inherits)
6397 for (cur = ops->inherits; cur; cur = cur->inherits) {
6398 void **inherit = (void **)cur;
6400 for (pp = begin; pp < end; pp++, inherit++)
6405 for (pp = begin; pp < end; pp++)
6409 ops->inherits = NULL;
6415 * ata_host_start - start and freeze ports of an ATA host
6416 * @host: ATA host to start ports for
6418 * Start and then freeze ports of @host. Started status is
6419 * recorded in host->flags, so this function can be called
6420 * multiple times. Ports are guaranteed to get started only
6421 * once. If host->ops isn't initialized yet, its set to the
6422 * first non-dummy port ops.
6425 * Inherited from calling layer (may sleep).
6428 * 0 if all ports are started successfully, -errno otherwise.
6430 int ata_host_start(struct ata_host *host)
6433 void *start_dr = NULL;
6436 if (host->flags & ATA_HOST_STARTED)
6439 ata_finalize_port_ops(host->ops);
6441 for (i = 0; i < host->n_ports; i++) {
6442 struct ata_port *ap = host->ports[i];
6444 ata_finalize_port_ops(ap->ops);
6446 if (!host->ops && !ata_port_is_dummy(ap))
6447 host->ops = ap->ops;
6449 if (ap->ops->port_stop)
6453 if (host->ops && host->ops->host_stop)
6457 start_dr = devres_alloc(ata_host_stop, 0, GFP_KERNEL);
6462 for (i = 0; i < host->n_ports; i++) {
6463 struct ata_port *ap = host->ports[i];
6465 if (ap->ops->port_start) {
6466 rc = ap->ops->port_start(ap);
6470 "failed to start port %d (errno=%d)\n",
6475 ata_eh_freeze_port(ap);
6479 devres_add(host->dev, start_dr);
6480 host->flags |= ATA_HOST_STARTED;
6485 struct ata_port *ap = host->ports[i];
6487 if (ap->ops->port_stop)
6488 ap->ops->port_stop(ap);
6490 devres_free(start_dr);
6495 * ata_sas_host_init - Initialize a host struct for sas (ipr, libsas)
6496 * @host: host to initialize
6497 * @dev: device host is attached to
6501 void ata_host_init(struct ata_host *host, struct device *dev,
6502 struct ata_port_operations *ops)
6504 spin_lock_init(&host->lock);
6505 mutex_init(&host->eh_mutex);
6506 host->n_tags = ATA_MAX_QUEUE;
6509 kref_init(&host->kref);
6512 void __ata_port_probe(struct ata_port *ap)
6514 struct ata_eh_info *ehi = &ap->link.eh_info;
6515 unsigned long flags;
6517 /* kick EH for boot probing */
6518 spin_lock_irqsave(ap->lock, flags);
6520 ehi->probe_mask |= ATA_ALL_DEVICES;
6521 ehi->action |= ATA_EH_RESET;
6522 ehi->flags |= ATA_EHI_NO_AUTOPSY | ATA_EHI_QUIET;
6524 ap->pflags &= ~ATA_PFLAG_INITIALIZING;
6525 ap->pflags |= ATA_PFLAG_LOADING;
6526 ata_port_schedule_eh(ap);
6528 spin_unlock_irqrestore(ap->lock, flags);
6531 int ata_port_probe(struct ata_port *ap)
6535 if (ap->ops->error_handler) {
6536 __ata_port_probe(ap);
6537 ata_port_wait_eh(ap);
6539 DPRINTK("ata%u: bus probe begin\n", ap->print_id);
6540 rc = ata_bus_probe(ap);
6541 DPRINTK("ata%u: bus probe end\n", ap->print_id);
6547 static void async_port_probe(void *data, async_cookie_t cookie)
6549 struct ata_port *ap = data;
6552 * If we're not allowed to scan this host in parallel,
6553 * we need to wait until all previous scans have completed
6554 * before going further.
6555 * Jeff Garzik says this is only within a controller, so we
6556 * don't need to wait for port 0, only for later ports.
6558 if (!(ap->host->flags & ATA_HOST_PARALLEL_SCAN) && ap->port_no != 0)
6559 async_synchronize_cookie(cookie);
6561 (void)ata_port_probe(ap);
6563 /* in order to keep device order, we need to synchronize at this point */
6564 async_synchronize_cookie(cookie);
6566 ata_scsi_scan_host(ap, 1);
6570 * ata_host_register - register initialized ATA host
6571 * @host: ATA host to register
6572 * @sht: template for SCSI host
6574 * Register initialized ATA host. @host is allocated using
6575 * ata_host_alloc() and fully initialized by LLD. This function
6576 * starts ports, registers @host with ATA and SCSI layers and
6577 * probe registered devices.
6580 * Inherited from calling layer (may sleep).
6583 * 0 on success, -errno otherwise.
6585 int ata_host_register(struct ata_host *host, struct scsi_host_template *sht)
6589 host->n_tags = clamp(sht->can_queue, 1, ATA_MAX_QUEUE);
6591 /* host must have been started */
6592 if (!(host->flags & ATA_HOST_STARTED)) {
6593 dev_err(host->dev, "BUG: trying to register unstarted host\n");
6598 /* Blow away unused ports. This happens when LLD can't
6599 * determine the exact number of ports to allocate at
6602 for (i = host->n_ports; host->ports[i]; i++)
6603 kfree(host->ports[i]);
6605 /* give ports names and add SCSI hosts */
6606 for (i = 0; i < host->n_ports; i++) {
6607 host->ports[i]->print_id = atomic_inc_return(&ata_print_id);
6608 host->ports[i]->local_port_no = i + 1;
6611 /* Create associated sysfs transport objects */
6612 for (i = 0; i < host->n_ports; i++) {
6613 rc = ata_tport_add(host->dev,host->ports[i]);
6619 rc = ata_scsi_add_hosts(host, sht);
6623 /* set cable, sata_spd_limit and report */
6624 for (i = 0; i < host->n_ports; i++) {
6625 struct ata_port *ap = host->ports[i];
6626 unsigned long xfer_mask;
6628 /* set SATA cable type if still unset */
6629 if (ap->cbl == ATA_CBL_NONE && (ap->flags & ATA_FLAG_SATA))
6630 ap->cbl = ATA_CBL_SATA;
6632 /* init sata_spd_limit to the current value */
6633 sata_link_init_spd(&ap->link);
6635 sata_link_init_spd(ap->slave_link);
6637 /* print per-port info to dmesg */
6638 xfer_mask = ata_pack_xfermask(ap->pio_mask, ap->mwdma_mask,
6641 if (!ata_port_is_dummy(ap)) {
6642 ata_port_info(ap, "%cATA max %s %s\n",
6643 (ap->flags & ATA_FLAG_SATA) ? 'S' : 'P',
6644 ata_mode_string(xfer_mask),
6645 ap->link.eh_info.desc);
6646 ata_ehi_clear_desc(&ap->link.eh_info);
6648 ata_port_info(ap, "DUMMY\n");
6651 /* perform each probe asynchronously */
6652 for (i = 0; i < host->n_ports; i++) {
6653 struct ata_port *ap = host->ports[i];
6654 ap->cookie = async_schedule(async_port_probe, ap);
6661 ata_tport_delete(host->ports[i]);
6668 * ata_host_activate - start host, request IRQ and register it
6669 * @host: target ATA host
6670 * @irq: IRQ to request
6671 * @irq_handler: irq_handler used when requesting IRQ
6672 * @irq_flags: irq_flags used when requesting IRQ
6673 * @sht: scsi_host_template to use when registering the host
6675 * After allocating an ATA host and initializing it, most libata
6676 * LLDs perform three steps to activate the host - start host,
6677 * request IRQ and register it. This helper takes necessary
6678 * arguments and performs the three steps in one go.
6680 * An invalid IRQ skips the IRQ registration and expects the host to
6681 * have set polling mode on the port. In this case, @irq_handler
6685 * Inherited from calling layer (may sleep).
6688 * 0 on success, -errno otherwise.
6690 int ata_host_activate(struct ata_host *host, int irq,
6691 irq_handler_t irq_handler, unsigned long irq_flags,
6692 struct scsi_host_template *sht)
6697 rc = ata_host_start(host);
6701 /* Special case for polling mode */
6703 WARN_ON(irq_handler);
6704 return ata_host_register(host, sht);
6707 irq_desc = devm_kasprintf(host->dev, GFP_KERNEL, "%s[%s]",
6708 dev_driver_string(host->dev),
6709 dev_name(host->dev));
6713 rc = devm_request_irq(host->dev, irq, irq_handler, irq_flags,
6718 for (i = 0; i < host->n_ports; i++)
6719 ata_port_desc(host->ports[i], "irq %d", irq);
6721 rc = ata_host_register(host, sht);
6722 /* if failed, just free the IRQ and leave ports alone */
6724 devm_free_irq(host->dev, irq, host);
6730 * ata_port_detach - Detach ATA port in preparation of device removal
6731 * @ap: ATA port to be detached
6733 * Detach all ATA devices and the associated SCSI devices of @ap;
6734 * then, remove the associated SCSI host. @ap is guaranteed to
6735 * be quiescent on return from this function.
6738 * Kernel thread context (may sleep).
6740 static void ata_port_detach(struct ata_port *ap)
6742 unsigned long flags;
6743 struct ata_link *link;
6744 struct ata_device *dev;
6746 if (!ap->ops->error_handler)
6749 /* tell EH we're leaving & flush EH */
6750 spin_lock_irqsave(ap->lock, flags);
6751 ap->pflags |= ATA_PFLAG_UNLOADING;
6752 ata_port_schedule_eh(ap);
6753 spin_unlock_irqrestore(ap->lock, flags);
6755 /* wait till EH commits suicide */
6756 ata_port_wait_eh(ap);
6758 /* it better be dead now */
6759 WARN_ON(!(ap->pflags & ATA_PFLAG_UNLOADED));
6761 cancel_delayed_work_sync(&ap->hotplug_task);
6764 /* clean up zpodd on port removal */
6765 ata_for_each_link(link, ap, HOST_FIRST) {
6766 ata_for_each_dev(dev, link, ALL) {
6767 if (zpodd_dev_enabled(dev))
6773 for (i = 0; i < SATA_PMP_MAX_PORTS; i++)
6774 ata_tlink_delete(&ap->pmp_link[i]);
6776 /* remove the associated SCSI host */
6777 scsi_remove_host(ap->scsi_host);
6778 ata_tport_delete(ap);
6782 * ata_host_detach - Detach all ports of an ATA host
6783 * @host: Host to detach
6785 * Detach all ports of @host.
6788 * Kernel thread context (may sleep).
6790 void ata_host_detach(struct ata_host *host)
6794 for (i = 0; i < host->n_ports; i++) {
6795 /* Ensure ata_port probe has completed */
6796 async_synchronize_cookie(host->ports[i]->cookie + 1);
6797 ata_port_detach(host->ports[i]);
6800 /* the host is dead now, dissociate ACPI */
6801 ata_acpi_dissociate(host);
6807 * ata_pci_remove_one - PCI layer callback for device removal
6808 * @pdev: PCI device that was removed
6810 * PCI layer indicates to libata via this hook that hot-unplug or
6811 * module unload event has occurred. Detach all ports. Resource
6812 * release is handled via devres.
6815 * Inherited from PCI layer (may sleep).
6817 void ata_pci_remove_one(struct pci_dev *pdev)
6819 struct ata_host *host = pci_get_drvdata(pdev);
6821 ata_host_detach(host);
6824 void ata_pci_shutdown_one(struct pci_dev *pdev)
6826 struct ata_host *host = pci_get_drvdata(pdev);
6829 for (i = 0; i < host->n_ports; i++) {
6830 struct ata_port *ap = host->ports[i];
6832 ap->pflags |= ATA_PFLAG_FROZEN;
6834 /* Disable port interrupts */
6835 if (ap->ops->freeze)
6836 ap->ops->freeze(ap);
6838 /* Stop the port DMA engines */
6839 if (ap->ops->port_stop)
6840 ap->ops->port_stop(ap);
6844 /* move to PCI subsystem */
6845 int pci_test_config_bits(struct pci_dev *pdev, const struct pci_bits *bits)
6847 unsigned long tmp = 0;
6849 switch (bits->width) {
6852 pci_read_config_byte(pdev, bits->reg, &tmp8);
6858 pci_read_config_word(pdev, bits->reg, &tmp16);
6864 pci_read_config_dword(pdev, bits->reg, &tmp32);
6875 return (tmp == bits->val) ? 1 : 0;
6879 void ata_pci_device_do_suspend(struct pci_dev *pdev, pm_message_t mesg)
6881 pci_save_state(pdev);
6882 pci_disable_device(pdev);
6884 if (mesg.event & PM_EVENT_SLEEP)
6885 pci_set_power_state(pdev, PCI_D3hot);
6888 int ata_pci_device_do_resume(struct pci_dev *pdev)
6892 pci_set_power_state(pdev, PCI_D0);
6893 pci_restore_state(pdev);
6895 rc = pcim_enable_device(pdev);
6898 "failed to enable device after resume (%d)\n", rc);
6902 pci_set_master(pdev);
6906 int ata_pci_device_suspend(struct pci_dev *pdev, pm_message_t mesg)
6908 struct ata_host *host = pci_get_drvdata(pdev);
6911 rc = ata_host_suspend(host, mesg);
6915 ata_pci_device_do_suspend(pdev, mesg);
6920 int ata_pci_device_resume(struct pci_dev *pdev)
6922 struct ata_host *host = pci_get_drvdata(pdev);
6925 rc = ata_pci_device_do_resume(pdev);
6927 ata_host_resume(host);
6930 #endif /* CONFIG_PM */
6932 #endif /* CONFIG_PCI */
6935 * ata_platform_remove_one - Platform layer callback for device removal
6936 * @pdev: Platform device that was removed
6938 * Platform layer indicates to libata via this hook that hot-unplug or
6939 * module unload event has occurred. Detach all ports. Resource
6940 * release is handled via devres.
6943 * Inherited from platform layer (may sleep).
6945 int ata_platform_remove_one(struct platform_device *pdev)
6947 struct ata_host *host = platform_get_drvdata(pdev);
6949 ata_host_detach(host);
6954 static int __init ata_parse_force_one(char **cur,
6955 struct ata_force_ent *force_ent,
6956 const char **reason)
6958 static const struct ata_force_param force_tbl[] __initconst = {
6959 { "40c", .cbl = ATA_CBL_PATA40 },
6960 { "80c", .cbl = ATA_CBL_PATA80 },
6961 { "short40c", .cbl = ATA_CBL_PATA40_SHORT },
6962 { "unk", .cbl = ATA_CBL_PATA_UNK },
6963 { "ign", .cbl = ATA_CBL_PATA_IGN },
6964 { "sata", .cbl = ATA_CBL_SATA },
6965 { "1.5Gbps", .spd_limit = 1 },
6966 { "3.0Gbps", .spd_limit = 2 },
6967 { "noncq", .horkage_on = ATA_HORKAGE_NONCQ },
6968 { "ncq", .horkage_off = ATA_HORKAGE_NONCQ },
6969 { "noncqtrim", .horkage_on = ATA_HORKAGE_NO_NCQ_TRIM },
6970 { "ncqtrim", .horkage_off = ATA_HORKAGE_NO_NCQ_TRIM },
6971 { "noncqati", .horkage_on = ATA_HORKAGE_NO_NCQ_ON_ATI },
6972 { "ncqati", .horkage_off = ATA_HORKAGE_NO_NCQ_ON_ATI },
6973 { "dump_id", .horkage_on = ATA_HORKAGE_DUMP_ID },
6974 { "pio0", .xfer_mask = 1 << (ATA_SHIFT_PIO + 0) },
6975 { "pio1", .xfer_mask = 1 << (ATA_SHIFT_PIO + 1) },
6976 { "pio2", .xfer_mask = 1 << (ATA_SHIFT_PIO + 2) },
6977 { "pio3", .xfer_mask = 1 << (ATA_SHIFT_PIO + 3) },
6978 { "pio4", .xfer_mask = 1 << (ATA_SHIFT_PIO + 4) },
6979 { "pio5", .xfer_mask = 1 << (ATA_SHIFT_PIO + 5) },
6980 { "pio6", .xfer_mask = 1 << (ATA_SHIFT_PIO + 6) },
6981 { "mwdma0", .xfer_mask = 1 << (ATA_SHIFT_MWDMA + 0) },
6982 { "mwdma1", .xfer_mask = 1 << (ATA_SHIFT_MWDMA + 1) },
6983 { "mwdma2", .xfer_mask = 1 << (ATA_SHIFT_MWDMA + 2) },
6984 { "mwdma3", .xfer_mask = 1 << (ATA_SHIFT_MWDMA + 3) },
6985 { "mwdma4", .xfer_mask = 1 << (ATA_SHIFT_MWDMA + 4) },
6986 { "udma0", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 0) },
6987 { "udma16", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 0) },
6988 { "udma/16", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 0) },
6989 { "udma1", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 1) },
6990 { "udma25", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 1) },
6991 { "udma/25", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 1) },
6992 { "udma2", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 2) },
6993 { "udma33", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 2) },
6994 { "udma/33", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 2) },
6995 { "udma3", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 3) },
6996 { "udma44", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 3) },
6997 { "udma/44", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 3) },
6998 { "udma4", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 4) },
6999 { "udma66", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 4) },
7000 { "udma/66", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 4) },
7001 { "udma5", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 5) },
7002 { "udma100", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 5) },
7003 { "udma/100", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 5) },
7004 { "udma6", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 6) },
7005 { "udma133", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 6) },
7006 { "udma/133", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 6) },
7007 { "udma7", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 7) },
7008 { "nohrst", .lflags = ATA_LFLAG_NO_HRST },
7009 { "nosrst", .lflags = ATA_LFLAG_NO_SRST },
7010 { "norst", .lflags = ATA_LFLAG_NO_HRST | ATA_LFLAG_NO_SRST },
7011 { "rstonce", .lflags = ATA_LFLAG_RST_ONCE },
7012 { "atapi_dmadir", .horkage_on = ATA_HORKAGE_ATAPI_DMADIR },
7013 { "disable", .horkage_on = ATA_HORKAGE_DISABLE },
7015 char *start = *cur, *p = *cur;
7016 char *id, *val, *endp;
7017 const struct ata_force_param *match_fp = NULL;
7018 int nr_matches = 0, i;
7020 /* find where this param ends and update *cur */
7021 while (*p != '\0' && *p != ',')
7032 p = strchr(start, ':');
7034 val = strstrip(start);
7039 id = strstrip(start);
7040 val = strstrip(p + 1);
7043 p = strchr(id, '.');
7046 force_ent->device = simple_strtoul(p, &endp, 10);
7047 if (p == endp || *endp != '\0') {
7048 *reason = "invalid device";
7053 force_ent->port = simple_strtoul(id, &endp, 10);
7054 if (id == endp || *endp != '\0') {
7055 *reason = "invalid port/link";
7060 /* parse val, allow shortcuts so that both 1.5 and 1.5Gbps work */
7061 for (i = 0; i < ARRAY_SIZE(force_tbl); i++) {
7062 const struct ata_force_param *fp = &force_tbl[i];
7064 if (strncasecmp(val, fp->name, strlen(val)))
7070 if (strcasecmp(val, fp->name) == 0) {
7077 *reason = "unknown value";
7080 if (nr_matches > 1) {
7081 *reason = "ambiguous value";
7085 force_ent->param = *match_fp;
7090 static void __init ata_parse_force_param(void)
7092 int idx = 0, size = 1;
7093 int last_port = -1, last_device = -1;
7094 char *p, *cur, *next;
7096 /* calculate maximum number of params and allocate force_tbl */
7097 for (p = ata_force_param_buf; *p; p++)
7101 ata_force_tbl = kcalloc(size, sizeof(ata_force_tbl[0]), GFP_KERNEL);
7102 if (!ata_force_tbl) {
7103 printk(KERN_WARNING "ata: failed to extend force table, "
7104 "libata.force ignored\n");
7108 /* parse and populate the table */
7109 for (cur = ata_force_param_buf; *cur != '\0'; cur = next) {
7110 const char *reason = "";
7111 struct ata_force_ent te = { .port = -1, .device = -1 };
7114 if (ata_parse_force_one(&next, &te, &reason)) {
7115 printk(KERN_WARNING "ata: failed to parse force "
7116 "parameter \"%s\" (%s)\n",
7121 if (te.port == -1) {
7122 te.port = last_port;
7123 te.device = last_device;
7126 ata_force_tbl[idx++] = te;
7128 last_port = te.port;
7129 last_device = te.device;
7132 ata_force_tbl_size = idx;
7135 static int __init ata_init(void)
7139 ata_parse_force_param();
7141 rc = ata_sff_init();
7143 kfree(ata_force_tbl);
7147 libata_transport_init();
7148 ata_scsi_transport_template = ata_attach_transport();
7149 if (!ata_scsi_transport_template) {
7155 printk(KERN_DEBUG "libata version " DRV_VERSION " loaded.\n");
7162 static void __exit ata_exit(void)
7164 ata_release_transport(ata_scsi_transport_template);
7165 libata_transport_exit();
7167 kfree(ata_force_tbl);
7170 subsys_initcall(ata_init);
7171 module_exit(ata_exit);
7173 static DEFINE_RATELIMIT_STATE(ratelimit, HZ / 5, 1);
7175 int ata_ratelimit(void)
7177 return __ratelimit(&ratelimit);
7181 * ata_msleep - ATA EH owner aware msleep
7182 * @ap: ATA port to attribute the sleep to
7183 * @msecs: duration to sleep in milliseconds
7185 * Sleeps @msecs. If the current task is owner of @ap's EH, the
7186 * ownership is released before going to sleep and reacquired
7187 * after the sleep is complete. IOW, other ports sharing the
7188 * @ap->host will be allowed to own the EH while this task is
7194 void ata_msleep(struct ata_port *ap, unsigned int msecs)
7196 bool owns_eh = ap && ap->host->eh_owner == current;
7202 unsigned long usecs = msecs * USEC_PER_MSEC;
7203 usleep_range(usecs, usecs + 50);
7213 * ata_wait_register - wait until register value changes
7214 * @ap: ATA port to wait register for, can be NULL
7215 * @reg: IO-mapped register
7216 * @mask: Mask to apply to read register value
7217 * @val: Wait condition
7218 * @interval: polling interval in milliseconds
7219 * @timeout: timeout in milliseconds
7221 * Waiting for some bits of register to change is a common
7222 * operation for ATA controllers. This function reads 32bit LE
7223 * IO-mapped register @reg and tests for the following condition.
7225 * (*@reg & mask) != val
7227 * If the condition is met, it returns; otherwise, the process is
7228 * repeated after @interval_msec until timeout.
7231 * Kernel thread context (may sleep)
7234 * The final register value.
7236 u32 ata_wait_register(struct ata_port *ap, void __iomem *reg, u32 mask, u32 val,
7237 unsigned long interval, unsigned long timeout)
7239 unsigned long deadline;
7242 tmp = ioread32(reg);
7244 /* Calculate timeout _after_ the first read to make sure
7245 * preceding writes reach the controller before starting to
7246 * eat away the timeout.
7248 deadline = ata_deadline(jiffies, timeout);
7250 while ((tmp & mask) == val && time_before(jiffies, deadline)) {
7251 ata_msleep(ap, interval);
7252 tmp = ioread32(reg);
7259 * sata_lpm_ignore_phy_events - test if PHY event should be ignored
7260 * @link: Link receiving the event
7262 * Test whether the received PHY event has to be ignored or not.
7268 * True if the event has to be ignored.
7270 bool sata_lpm_ignore_phy_events(struct ata_link *link)
7272 unsigned long lpm_timeout = link->last_lpm_change +
7273 msecs_to_jiffies(ATA_TMOUT_SPURIOUS_PHY);
7275 /* if LPM is enabled, PHYRDY doesn't mean anything */
7276 if (link->lpm_policy > ATA_LPM_MAX_POWER)
7279 /* ignore the first PHY event after the LPM policy changed
7280 * as it is might be spurious
7282 if ((link->flags & ATA_LFLAG_CHANGED) &&
7283 time_before(jiffies, lpm_timeout))
7288 EXPORT_SYMBOL_GPL(sata_lpm_ignore_phy_events);
7293 static unsigned int ata_dummy_qc_issue(struct ata_queued_cmd *qc)
7295 return AC_ERR_SYSTEM;
7298 static void ata_dummy_error_handler(struct ata_port *ap)
7303 struct ata_port_operations ata_dummy_port_ops = {
7304 .qc_prep = ata_noop_qc_prep,
7305 .qc_issue = ata_dummy_qc_issue,
7306 .error_handler = ata_dummy_error_handler,
7307 .sched_eh = ata_std_sched_eh,
7308 .end_eh = ata_std_end_eh,
7311 const struct ata_port_info ata_dummy_port_info = {
7312 .port_ops = &ata_dummy_port_ops,
7316 * Utility print functions
7318 void ata_port_printk(const struct ata_port *ap, const char *level,
7319 const char *fmt, ...)
7321 struct va_format vaf;
7324 va_start(args, fmt);
7329 printk("%sata%u: %pV", level, ap->print_id, &vaf);
7333 EXPORT_SYMBOL(ata_port_printk);
7335 void ata_link_printk(const struct ata_link *link, const char *level,
7336 const char *fmt, ...)
7338 struct va_format vaf;
7341 va_start(args, fmt);
7346 if (sata_pmp_attached(link->ap) || link->ap->slave_link)
7347 printk("%sata%u.%02u: %pV",
7348 level, link->ap->print_id, link->pmp, &vaf);
7350 printk("%sata%u: %pV",
7351 level, link->ap->print_id, &vaf);
7355 EXPORT_SYMBOL(ata_link_printk);
7357 void ata_dev_printk(const struct ata_device *dev, const char *level,
7358 const char *fmt, ...)
7360 struct va_format vaf;
7363 va_start(args, fmt);
7368 printk("%sata%u.%02u: %pV",
7369 level, dev->link->ap->print_id, dev->link->pmp + dev->devno,
7374 EXPORT_SYMBOL(ata_dev_printk);
7376 void ata_print_version(const struct device *dev, const char *version)
7378 dev_printk(KERN_DEBUG, dev, "version %s\n", version);
7380 EXPORT_SYMBOL(ata_print_version);
7383 * libata is essentially a library of internal helper functions for
7384 * low-level ATA host controller drivers. As such, the API/ABI is
7385 * likely to change as new drivers are added and updated.
7386 * Do not depend on ABI/API stability.
7388 EXPORT_SYMBOL_GPL(sata_deb_timing_normal);
7389 EXPORT_SYMBOL_GPL(sata_deb_timing_hotplug);
7390 EXPORT_SYMBOL_GPL(sata_deb_timing_long);
7391 EXPORT_SYMBOL_GPL(ata_base_port_ops);
7392 EXPORT_SYMBOL_GPL(sata_port_ops);
7393 EXPORT_SYMBOL_GPL(ata_dummy_port_ops);
7394 EXPORT_SYMBOL_GPL(ata_dummy_port_info);
7395 EXPORT_SYMBOL_GPL(ata_link_next);
7396 EXPORT_SYMBOL_GPL(ata_dev_next);
7397 EXPORT_SYMBOL_GPL(ata_std_bios_param);
7398 EXPORT_SYMBOL_GPL(ata_scsi_unlock_native_capacity);
7399 EXPORT_SYMBOL_GPL(ata_host_init);
7400 EXPORT_SYMBOL_GPL(ata_host_alloc);
7401 EXPORT_SYMBOL_GPL(ata_host_alloc_pinfo);
7402 EXPORT_SYMBOL_GPL(ata_slave_link_init);
7403 EXPORT_SYMBOL_GPL(ata_host_start);
7404 EXPORT_SYMBOL_GPL(ata_host_register);
7405 EXPORT_SYMBOL_GPL(ata_host_activate);
7406 EXPORT_SYMBOL_GPL(ata_host_detach);
7407 EXPORT_SYMBOL_GPL(ata_sg_init);
7408 EXPORT_SYMBOL_GPL(ata_qc_complete);
7409 EXPORT_SYMBOL_GPL(ata_qc_complete_multiple);
7410 EXPORT_SYMBOL_GPL(atapi_cmd_type);
7411 EXPORT_SYMBOL_GPL(ata_tf_to_fis);
7412 EXPORT_SYMBOL_GPL(ata_tf_from_fis);
7413 EXPORT_SYMBOL_GPL(ata_pack_xfermask);
7414 EXPORT_SYMBOL_GPL(ata_unpack_xfermask);
7415 EXPORT_SYMBOL_GPL(ata_xfer_mask2mode);
7416 EXPORT_SYMBOL_GPL(ata_xfer_mode2mask);
7417 EXPORT_SYMBOL_GPL(ata_xfer_mode2shift);
7418 EXPORT_SYMBOL_GPL(ata_mode_string);
7419 EXPORT_SYMBOL_GPL(ata_id_xfermask);
7420 EXPORT_SYMBOL_GPL(ata_do_set_mode);
7421 EXPORT_SYMBOL_GPL(ata_std_qc_defer);
7422 EXPORT_SYMBOL_GPL(ata_noop_qc_prep);
7423 EXPORT_SYMBOL_GPL(ata_dev_disable);
7424 EXPORT_SYMBOL_GPL(sata_set_spd);
7425 EXPORT_SYMBOL_GPL(ata_wait_after_reset);
7426 EXPORT_SYMBOL_GPL(sata_link_debounce);
7427 EXPORT_SYMBOL_GPL(sata_link_resume);
7428 EXPORT_SYMBOL_GPL(sata_link_scr_lpm);
7429 EXPORT_SYMBOL_GPL(ata_std_prereset);
7430 EXPORT_SYMBOL_GPL(sata_link_hardreset);
7431 EXPORT_SYMBOL_GPL(sata_std_hardreset);
7432 EXPORT_SYMBOL_GPL(ata_std_postreset);
7433 EXPORT_SYMBOL_GPL(ata_dev_classify);
7434 EXPORT_SYMBOL_GPL(ata_dev_pair);
7435 EXPORT_SYMBOL_GPL(ata_ratelimit);
7436 EXPORT_SYMBOL_GPL(ata_msleep);
7437 EXPORT_SYMBOL_GPL(ata_wait_register);
7438 EXPORT_SYMBOL_GPL(ata_scsi_queuecmd);
7439 EXPORT_SYMBOL_GPL(ata_scsi_slave_config);
7440 EXPORT_SYMBOL_GPL(ata_scsi_slave_destroy);
7441 EXPORT_SYMBOL_GPL(ata_scsi_change_queue_depth);
7442 EXPORT_SYMBOL_GPL(__ata_change_queue_depth);
7443 EXPORT_SYMBOL_GPL(sata_scr_valid);
7444 EXPORT_SYMBOL_GPL(sata_scr_read);
7445 EXPORT_SYMBOL_GPL(sata_scr_write);
7446 EXPORT_SYMBOL_GPL(sata_scr_write_flush);
7447 EXPORT_SYMBOL_GPL(ata_link_online);
7448 EXPORT_SYMBOL_GPL(ata_link_offline);
7450 EXPORT_SYMBOL_GPL(ata_host_suspend);
7451 EXPORT_SYMBOL_GPL(ata_host_resume);
7452 #endif /* CONFIG_PM */
7453 EXPORT_SYMBOL_GPL(ata_id_string);
7454 EXPORT_SYMBOL_GPL(ata_id_c_string);
7455 EXPORT_SYMBOL_GPL(ata_do_dev_read_id);
7456 EXPORT_SYMBOL_GPL(ata_scsi_simulate);
7458 EXPORT_SYMBOL_GPL(ata_pio_need_iordy);
7459 EXPORT_SYMBOL_GPL(ata_timing_find_mode);
7460 EXPORT_SYMBOL_GPL(ata_timing_compute);
7461 EXPORT_SYMBOL_GPL(ata_timing_merge);
7462 EXPORT_SYMBOL_GPL(ata_timing_cycle2mode);
7465 EXPORT_SYMBOL_GPL(pci_test_config_bits);
7466 EXPORT_SYMBOL_GPL(ata_pci_shutdown_one);
7467 EXPORT_SYMBOL_GPL(ata_pci_remove_one);
7469 EXPORT_SYMBOL_GPL(ata_pci_device_do_suspend);
7470 EXPORT_SYMBOL_GPL(ata_pci_device_do_resume);
7471 EXPORT_SYMBOL_GPL(ata_pci_device_suspend);
7472 EXPORT_SYMBOL_GPL(ata_pci_device_resume);
7473 #endif /* CONFIG_PM */
7474 #endif /* CONFIG_PCI */
7476 EXPORT_SYMBOL_GPL(ata_platform_remove_one);
7478 EXPORT_SYMBOL_GPL(__ata_ehi_push_desc);
7479 EXPORT_SYMBOL_GPL(ata_ehi_push_desc);
7480 EXPORT_SYMBOL_GPL(ata_ehi_clear_desc);
7481 EXPORT_SYMBOL_GPL(ata_port_desc);
7483 EXPORT_SYMBOL_GPL(ata_port_pbar_desc);
7484 #endif /* CONFIG_PCI */
7485 EXPORT_SYMBOL_GPL(ata_port_schedule_eh);
7486 EXPORT_SYMBOL_GPL(ata_link_abort);
7487 EXPORT_SYMBOL_GPL(ata_port_abort);
7488 EXPORT_SYMBOL_GPL(ata_port_freeze);
7489 EXPORT_SYMBOL_GPL(sata_async_notification);
7490 EXPORT_SYMBOL_GPL(ata_eh_freeze_port);
7491 EXPORT_SYMBOL_GPL(ata_eh_thaw_port);
7492 EXPORT_SYMBOL_GPL(ata_eh_qc_complete);
7493 EXPORT_SYMBOL_GPL(ata_eh_qc_retry);
7494 EXPORT_SYMBOL_GPL(ata_eh_analyze_ncq_error);
7495 EXPORT_SYMBOL_GPL(ata_do_eh);
7496 EXPORT_SYMBOL_GPL(ata_std_error_handler);
7498 EXPORT_SYMBOL_GPL(ata_cable_40wire);
7499 EXPORT_SYMBOL_GPL(ata_cable_80wire);
7500 EXPORT_SYMBOL_GPL(ata_cable_unknown);
7501 EXPORT_SYMBOL_GPL(ata_cable_ignore);
7502 EXPORT_SYMBOL_GPL(ata_cable_sata);
7503 EXPORT_SYMBOL_GPL(ata_host_get);
7504 EXPORT_SYMBOL_GPL(ata_host_put);