1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * libata-core.c - helper library for ATA
5 * Copyright 2003-2004 Red Hat, Inc. All rights reserved.
6 * Copyright 2003-2004 Jeff Garzik
8 * libata documentation is available via 'make {ps|pdf}docs',
9 * as Documentation/driver-api/libata.rst
11 * Hardware documentation available from http://www.t13.org/ and
12 * http://www.sata-io.org/
14 * Standards documents from:
15 * http://www.t13.org (ATA standards, PCI DMA IDE spec)
16 * http://www.t10.org (SCSI MMC - for ATAPI MMC)
17 * http://www.sata-io.org (SATA)
18 * http://www.compactflash.org (CF)
19 * http://www.qic.org (QIC157 - Tape and DSC)
20 * http://www.ce-ata.org (CE-ATA: not supported)
22 * libata is essentially a library of internal helper functions for
23 * low-level ATA host controller drivers. As such, the API/ABI is
24 * likely to change as new drivers are added and updated.
25 * Do not depend on ABI/API stability.
28 #include <linux/kernel.h>
29 #include <linux/module.h>
30 #include <linux/pci.h>
31 #include <linux/init.h>
32 #include <linux/list.h>
34 #include <linux/spinlock.h>
35 #include <linux/blkdev.h>
36 #include <linux/delay.h>
37 #include <linux/timer.h>
38 #include <linux/time.h>
39 #include <linux/interrupt.h>
40 #include <linux/completion.h>
41 #include <linux/suspend.h>
42 #include <linux/workqueue.h>
43 #include <linux/scatterlist.h>
45 #include <linux/log2.h>
46 #include <linux/slab.h>
47 #include <linux/glob.h>
48 #include <scsi/scsi.h>
49 #include <scsi/scsi_cmnd.h>
50 #include <scsi/scsi_host.h>
51 #include <linux/libata.h>
52 #include <asm/byteorder.h>
53 #include <asm/unaligned.h>
54 #include <linux/cdrom.h>
55 #include <linux/ratelimit.h>
56 #include <linux/leds.h>
57 #include <linux/pm_runtime.h>
58 #include <linux/platform_device.h>
59 #include <asm/setup.h>
61 #define CREATE_TRACE_POINTS
62 #include <trace/events/libata.h>
65 #include "libata-transport.h"
67 const struct ata_port_operations ata_base_port_ops = {
68 .prereset = ata_std_prereset,
69 .postreset = ata_std_postreset,
70 .error_handler = ata_std_error_handler,
71 .sched_eh = ata_std_sched_eh,
72 .end_eh = ata_std_end_eh,
75 const struct ata_port_operations sata_port_ops = {
76 .inherits = &ata_base_port_ops,
78 .qc_defer = ata_std_qc_defer,
79 .hardreset = sata_std_hardreset,
81 EXPORT_SYMBOL_GPL(sata_port_ops);
83 static unsigned int ata_dev_init_params(struct ata_device *dev,
84 u16 heads, u16 sectors);
85 static unsigned int ata_dev_set_xfermode(struct ata_device *dev);
86 static void ata_dev_xfermask(struct ata_device *dev);
87 static unsigned long ata_dev_blacklisted(const struct ata_device *dev);
89 atomic_t ata_print_id = ATOMIC_INIT(0);
91 #ifdef CONFIG_ATA_FORCE
92 struct ata_force_param {
96 unsigned long xfer_mask;
97 unsigned int horkage_on;
98 unsigned int horkage_off;
103 struct ata_force_ent {
106 struct ata_force_param param;
109 static struct ata_force_ent *ata_force_tbl;
110 static int ata_force_tbl_size;
112 static char ata_force_param_buf[COMMAND_LINE_SIZE] __initdata;
113 /* param_buf is thrown away after initialization, disallow read */
114 module_param_string(force, ata_force_param_buf, sizeof(ata_force_param_buf), 0);
115 MODULE_PARM_DESC(force, "Force ATA configurations including cable type, link speed and transfer mode (see Documentation/admin-guide/kernel-parameters.rst for details)");
118 static int atapi_enabled = 1;
119 module_param(atapi_enabled, int, 0444);
120 MODULE_PARM_DESC(atapi_enabled, "Enable discovery of ATAPI devices (0=off, 1=on [default])");
122 static int atapi_dmadir = 0;
123 module_param(atapi_dmadir, int, 0444);
124 MODULE_PARM_DESC(atapi_dmadir, "Enable ATAPI DMADIR bridge support (0=off [default], 1=on)");
126 int atapi_passthru16 = 1;
127 module_param(atapi_passthru16, int, 0444);
128 MODULE_PARM_DESC(atapi_passthru16, "Enable ATA_16 passthru for ATAPI devices (0=off, 1=on [default])");
131 module_param_named(fua, libata_fua, int, 0444);
132 MODULE_PARM_DESC(fua, "FUA support (0=off [default], 1=on)");
134 static int ata_ignore_hpa;
135 module_param_named(ignore_hpa, ata_ignore_hpa, int, 0644);
136 MODULE_PARM_DESC(ignore_hpa, "Ignore HPA limit (0=keep BIOS limits, 1=ignore limits, using full disk)");
138 static int libata_dma_mask = ATA_DMA_MASK_ATA|ATA_DMA_MASK_ATAPI|ATA_DMA_MASK_CFA;
139 module_param_named(dma, libata_dma_mask, int, 0444);
140 MODULE_PARM_DESC(dma, "DMA enable/disable (0x1==ATA, 0x2==ATAPI, 0x4==CF)");
142 static int ata_probe_timeout;
143 module_param(ata_probe_timeout, int, 0444);
144 MODULE_PARM_DESC(ata_probe_timeout, "Set ATA probing timeout (seconds)");
146 int libata_noacpi = 0;
147 module_param_named(noacpi, libata_noacpi, int, 0444);
148 MODULE_PARM_DESC(noacpi, "Disable the use of ACPI in probe/suspend/resume (0=off [default], 1=on)");
150 int libata_allow_tpm = 0;
151 module_param_named(allow_tpm, libata_allow_tpm, int, 0444);
152 MODULE_PARM_DESC(allow_tpm, "Permit the use of TPM commands (0=off [default], 1=on)");
155 module_param(atapi_an, int, 0444);
156 MODULE_PARM_DESC(atapi_an, "Enable ATAPI AN media presence notification (0=0ff [default], 1=on)");
158 MODULE_AUTHOR("Jeff Garzik");
159 MODULE_DESCRIPTION("Library module for ATA devices");
160 MODULE_LICENSE("GPL");
161 MODULE_VERSION(DRV_VERSION);
163 static inline bool ata_dev_print_info(struct ata_device *dev)
165 struct ata_eh_context *ehc = &dev->link->eh_context;
167 return ehc->i.flags & ATA_EHI_PRINTINFO;
170 static bool ata_sstatus_online(u32 sstatus)
172 return (sstatus & 0xf) == 0x3;
176 * ata_link_next - link iteration helper
177 * @link: the previous link, NULL to start
178 * @ap: ATA port containing links to iterate
179 * @mode: iteration mode, one of ATA_LITER_*
182 * Host lock or EH context.
185 * Pointer to the next link.
187 struct ata_link *ata_link_next(struct ata_link *link, struct ata_port *ap,
188 enum ata_link_iter_mode mode)
190 BUG_ON(mode != ATA_LITER_EDGE &&
191 mode != ATA_LITER_PMP_FIRST && mode != ATA_LITER_HOST_FIRST);
193 /* NULL link indicates start of iteration */
197 case ATA_LITER_PMP_FIRST:
198 if (sata_pmp_attached(ap))
201 case ATA_LITER_HOST_FIRST:
205 /* we just iterated over the host link, what's next? */
206 if (link == &ap->link)
208 case ATA_LITER_HOST_FIRST:
209 if (sata_pmp_attached(ap))
212 case ATA_LITER_PMP_FIRST:
213 if (unlikely(ap->slave_link))
214 return ap->slave_link;
220 /* slave_link excludes PMP */
221 if (unlikely(link == ap->slave_link))
224 /* we were over a PMP link */
225 if (++link < ap->pmp_link + ap->nr_pmp_links)
228 if (mode == ATA_LITER_PMP_FIRST)
233 EXPORT_SYMBOL_GPL(ata_link_next);
236 * ata_dev_next - device iteration helper
237 * @dev: the previous device, NULL to start
238 * @link: ATA link containing devices to iterate
239 * @mode: iteration mode, one of ATA_DITER_*
242 * Host lock or EH context.
245 * Pointer to the next device.
247 struct ata_device *ata_dev_next(struct ata_device *dev, struct ata_link *link,
248 enum ata_dev_iter_mode mode)
250 BUG_ON(mode != ATA_DITER_ENABLED && mode != ATA_DITER_ENABLED_REVERSE &&
251 mode != ATA_DITER_ALL && mode != ATA_DITER_ALL_REVERSE);
253 /* NULL dev indicates start of iteration */
256 case ATA_DITER_ENABLED:
260 case ATA_DITER_ENABLED_REVERSE:
261 case ATA_DITER_ALL_REVERSE:
262 dev = link->device + ata_link_max_devices(link) - 1;
267 /* move to the next one */
269 case ATA_DITER_ENABLED:
271 if (++dev < link->device + ata_link_max_devices(link))
274 case ATA_DITER_ENABLED_REVERSE:
275 case ATA_DITER_ALL_REVERSE:
276 if (--dev >= link->device)
282 if ((mode == ATA_DITER_ENABLED || mode == ATA_DITER_ENABLED_REVERSE) &&
283 !ata_dev_enabled(dev))
287 EXPORT_SYMBOL_GPL(ata_dev_next);
290 * ata_dev_phys_link - find physical link for a device
291 * @dev: ATA device to look up physical link for
293 * Look up physical link which @dev is attached to. Note that
294 * this is different from @dev->link only when @dev is on slave
295 * link. For all other cases, it's the same as @dev->link.
301 * Pointer to the found physical link.
303 struct ata_link *ata_dev_phys_link(struct ata_device *dev)
305 struct ata_port *ap = dev->link->ap;
311 return ap->slave_link;
314 #ifdef CONFIG_ATA_FORCE
316 * ata_force_cbl - force cable type according to libata.force
317 * @ap: ATA port of interest
319 * Force cable type according to libata.force and whine about it.
320 * The last entry which has matching port number is used, so it
321 * can be specified as part of device force parameters. For
322 * example, both "a:40c,1.00:udma4" and "1.00:40c,udma4" have the
328 void ata_force_cbl(struct ata_port *ap)
332 for (i = ata_force_tbl_size - 1; i >= 0; i--) {
333 const struct ata_force_ent *fe = &ata_force_tbl[i];
335 if (fe->port != -1 && fe->port != ap->print_id)
338 if (fe->param.cbl == ATA_CBL_NONE)
341 ap->cbl = fe->param.cbl;
342 ata_port_notice(ap, "FORCE: cable set to %s\n", fe->param.name);
348 * ata_force_link_limits - force link limits according to libata.force
349 * @link: ATA link of interest
351 * Force link flags and SATA spd limit according to libata.force
352 * and whine about it. When only the port part is specified
353 * (e.g. 1:), the limit applies to all links connected to both
354 * the host link and all fan-out ports connected via PMP. If the
355 * device part is specified as 0 (e.g. 1.00:), it specifies the
356 * first fan-out link not the host link. Device number 15 always
357 * points to the host link whether PMP is attached or not. If the
358 * controller has slave link, device number 16 points to it.
363 static void ata_force_link_limits(struct ata_link *link)
365 bool did_spd = false;
366 int linkno = link->pmp;
369 if (ata_is_host_link(link))
372 for (i = ata_force_tbl_size - 1; i >= 0; i--) {
373 const struct ata_force_ent *fe = &ata_force_tbl[i];
375 if (fe->port != -1 && fe->port != link->ap->print_id)
378 if (fe->device != -1 && fe->device != linkno)
381 /* only honor the first spd limit */
382 if (!did_spd && fe->param.spd_limit) {
383 link->hw_sata_spd_limit = (1 << fe->param.spd_limit) - 1;
384 ata_link_notice(link, "FORCE: PHY spd limit set to %s\n",
389 /* let lflags stack */
390 if (fe->param.lflags_on) {
391 link->flags |= fe->param.lflags_on;
392 ata_link_notice(link,
393 "FORCE: link flag 0x%x forced -> 0x%x\n",
394 fe->param.lflags_on, link->flags);
396 if (fe->param.lflags_off) {
397 link->flags &= ~fe->param.lflags_off;
398 ata_link_notice(link,
399 "FORCE: link flag 0x%x cleared -> 0x%x\n",
400 fe->param.lflags_off, 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",
502 static inline void ata_force_link_limits(struct ata_link *link) { }
503 static inline void ata_force_xfermask(struct ata_device *dev) { }
504 static inline void ata_force_horkage(struct ata_device *dev) { }
508 * atapi_cmd_type - Determine ATAPI command type from SCSI opcode
509 * @opcode: SCSI opcode
511 * Determine ATAPI command type from @opcode.
517 * ATAPI_{READ|WRITE|READ_CD|PASS_THRU|MISC}
519 int atapi_cmd_type(u8 opcode)
528 case GPCMD_WRITE_AND_VERIFY_10:
532 case GPCMD_READ_CD_MSF:
533 return ATAPI_READ_CD;
537 if (atapi_passthru16)
538 return ATAPI_PASS_THRU;
544 EXPORT_SYMBOL_GPL(atapi_cmd_type);
546 static const u8 ata_rw_cmds[] = {
550 ATA_CMD_READ_MULTI_EXT,
551 ATA_CMD_WRITE_MULTI_EXT,
555 ATA_CMD_WRITE_MULTI_FUA_EXT,
559 ATA_CMD_PIO_READ_EXT,
560 ATA_CMD_PIO_WRITE_EXT,
573 ATA_CMD_WRITE_FUA_EXT
577 * ata_rwcmd_protocol - set taskfile r/w commands and protocol
578 * @tf: command to examine and configure
579 * @dev: device tf belongs to
581 * Examine the device configuration and tf->flags to calculate
582 * the proper read/write commands and protocol to use.
587 static int ata_rwcmd_protocol(struct ata_taskfile *tf, struct ata_device *dev)
591 int index, fua, lba48, write;
593 fua = (tf->flags & ATA_TFLAG_FUA) ? 4 : 0;
594 lba48 = (tf->flags & ATA_TFLAG_LBA48) ? 2 : 0;
595 write = (tf->flags & ATA_TFLAG_WRITE) ? 1 : 0;
597 if (dev->flags & ATA_DFLAG_PIO) {
598 tf->protocol = ATA_PROT_PIO;
599 index = dev->multi_count ? 0 : 8;
600 } else if (lba48 && (dev->link->ap->flags & ATA_FLAG_PIO_LBA48)) {
601 /* Unable to use DMA due to host limitation */
602 tf->protocol = ATA_PROT_PIO;
603 index = dev->multi_count ? 0 : 8;
605 tf->protocol = ATA_PROT_DMA;
609 cmd = ata_rw_cmds[index + fua + lba48 + write];
618 * ata_tf_read_block - Read block address from ATA taskfile
619 * @tf: ATA taskfile of interest
620 * @dev: ATA device @tf belongs to
625 * Read block address from @tf. This function can handle all
626 * three address formats - LBA, LBA48 and CHS. tf->protocol and
627 * flags select the address format to use.
630 * Block address read from @tf.
632 u64 ata_tf_read_block(const struct ata_taskfile *tf, struct ata_device *dev)
636 if (tf->flags & ATA_TFLAG_LBA) {
637 if (tf->flags & ATA_TFLAG_LBA48) {
638 block |= (u64)tf->hob_lbah << 40;
639 block |= (u64)tf->hob_lbam << 32;
640 block |= (u64)tf->hob_lbal << 24;
642 block |= (tf->device & 0xf) << 24;
644 block |= tf->lbah << 16;
645 block |= tf->lbam << 8;
650 cyl = tf->lbam | (tf->lbah << 8);
651 head = tf->device & 0xf;
656 "device reported invalid CHS sector 0\n");
660 block = (cyl * dev->heads + head) * dev->sectors + sect - 1;
667 * ata_build_rw_tf - Build ATA taskfile for given read/write request
668 * @tf: Target ATA taskfile
669 * @dev: ATA device @tf belongs to
670 * @block: Block address
671 * @n_block: Number of blocks
672 * @tf_flags: RW/FUA etc...
674 * @class: IO priority class
679 * Build ATA taskfile @tf for read/write request described by
680 * @block, @n_block, @tf_flags and @tag on @dev.
684 * 0 on success, -ERANGE if the request is too large for @dev,
685 * -EINVAL if the request is invalid.
687 int ata_build_rw_tf(struct ata_taskfile *tf, struct ata_device *dev,
688 u64 block, u32 n_block, unsigned int tf_flags,
689 unsigned int tag, int class)
691 tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
692 tf->flags |= tf_flags;
694 if (ata_ncq_enabled(dev) && !ata_tag_internal(tag)) {
696 if (!lba_48_ok(block, n_block))
699 tf->protocol = ATA_PROT_NCQ;
700 tf->flags |= ATA_TFLAG_LBA | ATA_TFLAG_LBA48;
702 if (tf->flags & ATA_TFLAG_WRITE)
703 tf->command = ATA_CMD_FPDMA_WRITE;
705 tf->command = ATA_CMD_FPDMA_READ;
707 tf->nsect = tag << 3;
708 tf->hob_feature = (n_block >> 8) & 0xff;
709 tf->feature = n_block & 0xff;
711 tf->hob_lbah = (block >> 40) & 0xff;
712 tf->hob_lbam = (block >> 32) & 0xff;
713 tf->hob_lbal = (block >> 24) & 0xff;
714 tf->lbah = (block >> 16) & 0xff;
715 tf->lbam = (block >> 8) & 0xff;
716 tf->lbal = block & 0xff;
718 tf->device = ATA_LBA;
719 if (tf->flags & ATA_TFLAG_FUA)
720 tf->device |= 1 << 7;
722 if (dev->flags & ATA_DFLAG_NCQ_PRIO_ENABLE &&
723 class == IOPRIO_CLASS_RT)
724 tf->hob_nsect |= ATA_PRIO_HIGH << ATA_SHIFT_PRIO;
725 } else if (dev->flags & ATA_DFLAG_LBA) {
726 tf->flags |= ATA_TFLAG_LBA;
728 if (lba_28_ok(block, n_block)) {
730 tf->device |= (block >> 24) & 0xf;
731 } else if (lba_48_ok(block, n_block)) {
732 if (!(dev->flags & ATA_DFLAG_LBA48))
736 tf->flags |= ATA_TFLAG_LBA48;
738 tf->hob_nsect = (n_block >> 8) & 0xff;
740 tf->hob_lbah = (block >> 40) & 0xff;
741 tf->hob_lbam = (block >> 32) & 0xff;
742 tf->hob_lbal = (block >> 24) & 0xff;
744 /* request too large even for LBA48 */
747 if (unlikely(ata_rwcmd_protocol(tf, dev) < 0))
750 tf->nsect = n_block & 0xff;
752 tf->lbah = (block >> 16) & 0xff;
753 tf->lbam = (block >> 8) & 0xff;
754 tf->lbal = block & 0xff;
756 tf->device |= ATA_LBA;
759 u32 sect, head, cyl, track;
761 /* The request -may- be too large for CHS addressing. */
762 if (!lba_28_ok(block, n_block))
765 if (unlikely(ata_rwcmd_protocol(tf, dev) < 0))
768 /* Convert LBA to CHS */
769 track = (u32)block / dev->sectors;
770 cyl = track / dev->heads;
771 head = track % dev->heads;
772 sect = (u32)block % dev->sectors + 1;
774 /* Check whether the converted CHS can fit.
778 if ((cyl >> 16) || (head >> 4) || (sect >> 8) || (!sect))
781 tf->nsect = n_block & 0xff; /* Sector count 0 means 256 sectors */
792 * ata_pack_xfermask - Pack pio, mwdma and udma masks into xfer_mask
793 * @pio_mask: pio_mask
794 * @mwdma_mask: mwdma_mask
795 * @udma_mask: udma_mask
797 * Pack @pio_mask, @mwdma_mask and @udma_mask into a single
798 * unsigned int xfer_mask.
806 unsigned long ata_pack_xfermask(unsigned long pio_mask,
807 unsigned long mwdma_mask,
808 unsigned long udma_mask)
810 return ((pio_mask << ATA_SHIFT_PIO) & ATA_MASK_PIO) |
811 ((mwdma_mask << ATA_SHIFT_MWDMA) & ATA_MASK_MWDMA) |
812 ((udma_mask << ATA_SHIFT_UDMA) & ATA_MASK_UDMA);
814 EXPORT_SYMBOL_GPL(ata_pack_xfermask);
817 * ata_unpack_xfermask - Unpack xfer_mask into pio, mwdma and udma masks
818 * @xfer_mask: xfer_mask to unpack
819 * @pio_mask: resulting pio_mask
820 * @mwdma_mask: resulting mwdma_mask
821 * @udma_mask: resulting udma_mask
823 * Unpack @xfer_mask into @pio_mask, @mwdma_mask and @udma_mask.
824 * Any NULL destination masks will be ignored.
826 void ata_unpack_xfermask(unsigned long xfer_mask, unsigned long *pio_mask,
827 unsigned long *mwdma_mask, unsigned long *udma_mask)
830 *pio_mask = (xfer_mask & ATA_MASK_PIO) >> ATA_SHIFT_PIO;
832 *mwdma_mask = (xfer_mask & ATA_MASK_MWDMA) >> ATA_SHIFT_MWDMA;
834 *udma_mask = (xfer_mask & ATA_MASK_UDMA) >> ATA_SHIFT_UDMA;
837 static const struct ata_xfer_ent {
841 { ATA_SHIFT_PIO, ATA_NR_PIO_MODES, XFER_PIO_0 },
842 { ATA_SHIFT_MWDMA, ATA_NR_MWDMA_MODES, XFER_MW_DMA_0 },
843 { ATA_SHIFT_UDMA, ATA_NR_UDMA_MODES, XFER_UDMA_0 },
848 * ata_xfer_mask2mode - Find matching XFER_* for the given xfer_mask
849 * @xfer_mask: xfer_mask of interest
851 * Return matching XFER_* value for @xfer_mask. Only the highest
852 * bit of @xfer_mask is considered.
858 * Matching XFER_* value, 0xff if no match found.
860 u8 ata_xfer_mask2mode(unsigned long xfer_mask)
862 int highbit = fls(xfer_mask) - 1;
863 const struct ata_xfer_ent *ent;
865 for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
866 if (highbit >= ent->shift && highbit < ent->shift + ent->bits)
867 return ent->base + highbit - ent->shift;
870 EXPORT_SYMBOL_GPL(ata_xfer_mask2mode);
873 * ata_xfer_mode2mask - Find matching xfer_mask for XFER_*
874 * @xfer_mode: XFER_* of interest
876 * Return matching xfer_mask for @xfer_mode.
882 * Matching xfer_mask, 0 if no match found.
884 unsigned long ata_xfer_mode2mask(u8 xfer_mode)
886 const struct ata_xfer_ent *ent;
888 for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
889 if (xfer_mode >= ent->base && xfer_mode < ent->base + ent->bits)
890 return ((2 << (ent->shift + xfer_mode - ent->base)) - 1)
891 & ~((1 << ent->shift) - 1);
894 EXPORT_SYMBOL_GPL(ata_xfer_mode2mask);
897 * ata_xfer_mode2shift - Find matching xfer_shift for XFER_*
898 * @xfer_mode: XFER_* of interest
900 * Return matching xfer_shift for @xfer_mode.
906 * Matching xfer_shift, -1 if no match found.
908 int ata_xfer_mode2shift(u8 xfer_mode)
910 const struct ata_xfer_ent *ent;
912 for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
913 if (xfer_mode >= ent->base && xfer_mode < ent->base + ent->bits)
917 EXPORT_SYMBOL_GPL(ata_xfer_mode2shift);
920 * ata_mode_string - convert xfer_mask to string
921 * @xfer_mask: mask of bits supported; only highest bit counts.
923 * Determine string which represents the highest speed
924 * (highest bit in @modemask).
930 * Constant C string representing highest speed listed in
931 * @mode_mask, or the constant C string "<n/a>".
933 const char *ata_mode_string(unsigned long xfer_mask)
935 static const char * const xfer_mode_str[] = {
959 highbit = fls(xfer_mask) - 1;
960 if (highbit >= 0 && highbit < ARRAY_SIZE(xfer_mode_str))
961 return xfer_mode_str[highbit];
964 EXPORT_SYMBOL_GPL(ata_mode_string);
966 const char *sata_spd_string(unsigned int spd)
968 static const char * const spd_str[] = {
974 if (spd == 0 || (spd - 1) >= ARRAY_SIZE(spd_str))
976 return spd_str[spd - 1];
980 * ata_dev_classify - determine device type based on ATA-spec signature
981 * @tf: ATA taskfile register set for device to be identified
983 * Determine from taskfile register contents whether a device is
984 * ATA or ATAPI, as per "Signature and persistence" section
985 * of ATA/PI spec (volume 1, sect 5.14).
991 * Device type, %ATA_DEV_ATA, %ATA_DEV_ATAPI, %ATA_DEV_PMP,
992 * %ATA_DEV_ZAC, or %ATA_DEV_UNKNOWN the event of failure.
994 unsigned int ata_dev_classify(const struct ata_taskfile *tf)
996 /* Apple's open source Darwin code hints that some devices only
997 * put a proper signature into the LBA mid/high registers,
998 * So, we only check those. It's sufficient for uniqueness.
1000 * ATA/ATAPI-7 (d1532v1r1: Feb. 19, 2003) specified separate
1001 * signatures for ATA and ATAPI devices attached on SerialATA,
1002 * 0x3c/0xc3 and 0x69/0x96 respectively. However, SerialATA
1003 * spec has never mentioned about using different signatures
1004 * for ATA/ATAPI devices. Then, Serial ATA II: Port
1005 * Multiplier specification began to use 0x69/0x96 to identify
1006 * port multpliers and 0x3c/0xc3 to identify SEMB device.
1007 * ATA/ATAPI-7 dropped descriptions about 0x3c/0xc3 and
1008 * 0x69/0x96 shortly and described them as reserved for
1011 * We follow the current spec and consider that 0x69/0x96
1012 * identifies a port multiplier and 0x3c/0xc3 a SEMB device.
1013 * Unfortunately, WDC WD1600JS-62MHB5 (a hard drive) reports
1014 * SEMB signature. This is worked around in
1015 * ata_dev_read_id().
1017 if (tf->lbam == 0 && tf->lbah == 0)
1020 if (tf->lbam == 0x14 && tf->lbah == 0xeb)
1021 return ATA_DEV_ATAPI;
1023 if (tf->lbam == 0x69 && tf->lbah == 0x96)
1026 if (tf->lbam == 0x3c && tf->lbah == 0xc3)
1027 return ATA_DEV_SEMB;
1029 if (tf->lbam == 0xcd && tf->lbah == 0xab)
1032 return ATA_DEV_UNKNOWN;
1034 EXPORT_SYMBOL_GPL(ata_dev_classify);
1037 * ata_id_string - Convert IDENTIFY DEVICE page into string
1038 * @id: IDENTIFY DEVICE results we will examine
1039 * @s: string into which data is output
1040 * @ofs: offset into identify device page
1041 * @len: length of string to return. must be an even number.
1043 * The strings in the IDENTIFY DEVICE page are broken up into
1044 * 16-bit chunks. Run through the string, and output each
1045 * 8-bit chunk linearly, regardless of platform.
1051 void ata_id_string(const u16 *id, unsigned char *s,
1052 unsigned int ofs, unsigned int len)
1071 EXPORT_SYMBOL_GPL(ata_id_string);
1074 * ata_id_c_string - Convert IDENTIFY DEVICE page into C string
1075 * @id: IDENTIFY DEVICE results we will examine
1076 * @s: string into which data is output
1077 * @ofs: offset into identify device page
1078 * @len: length of string to return. must be an odd number.
1080 * This function is identical to ata_id_string except that it
1081 * trims trailing spaces and terminates the resulting string with
1082 * null. @len must be actual maximum length (even number) + 1.
1087 void ata_id_c_string(const u16 *id, unsigned char *s,
1088 unsigned int ofs, unsigned int len)
1092 ata_id_string(id, s, ofs, len - 1);
1094 p = s + strnlen(s, len - 1);
1095 while (p > s && p[-1] == ' ')
1099 EXPORT_SYMBOL_GPL(ata_id_c_string);
1101 static u64 ata_id_n_sectors(const u16 *id)
1103 if (ata_id_has_lba(id)) {
1104 if (ata_id_has_lba48(id))
1105 return ata_id_u64(id, ATA_ID_LBA_CAPACITY_2);
1107 return ata_id_u32(id, ATA_ID_LBA_CAPACITY);
1109 if (ata_id_current_chs_valid(id))
1110 return id[ATA_ID_CUR_CYLS] * id[ATA_ID_CUR_HEADS] *
1111 id[ATA_ID_CUR_SECTORS];
1113 return id[ATA_ID_CYLS] * id[ATA_ID_HEADS] *
1118 u64 ata_tf_to_lba48(const struct ata_taskfile *tf)
1122 sectors |= ((u64)(tf->hob_lbah & 0xff)) << 40;
1123 sectors |= ((u64)(tf->hob_lbam & 0xff)) << 32;
1124 sectors |= ((u64)(tf->hob_lbal & 0xff)) << 24;
1125 sectors |= (tf->lbah & 0xff) << 16;
1126 sectors |= (tf->lbam & 0xff) << 8;
1127 sectors |= (tf->lbal & 0xff);
1132 u64 ata_tf_to_lba(const struct ata_taskfile *tf)
1136 sectors |= (tf->device & 0x0f) << 24;
1137 sectors |= (tf->lbah & 0xff) << 16;
1138 sectors |= (tf->lbam & 0xff) << 8;
1139 sectors |= (tf->lbal & 0xff);
1145 * ata_read_native_max_address - Read native max address
1146 * @dev: target device
1147 * @max_sectors: out parameter for the result native max address
1149 * Perform an LBA48 or LBA28 native size query upon the device in
1153 * 0 on success, -EACCES if command is aborted by the drive.
1154 * -EIO on other errors.
1156 static int ata_read_native_max_address(struct ata_device *dev, u64 *max_sectors)
1158 unsigned int err_mask;
1159 struct ata_taskfile tf;
1160 int lba48 = ata_id_has_lba48(dev->id);
1162 ata_tf_init(dev, &tf);
1164 /* always clear all address registers */
1165 tf.flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_ISADDR;
1168 tf.command = ATA_CMD_READ_NATIVE_MAX_EXT;
1169 tf.flags |= ATA_TFLAG_LBA48;
1171 tf.command = ATA_CMD_READ_NATIVE_MAX;
1173 tf.protocol = ATA_PROT_NODATA;
1174 tf.device |= ATA_LBA;
1176 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
1179 "failed to read native max address (err_mask=0x%x)\n",
1181 if (err_mask == AC_ERR_DEV && (tf.error & ATA_ABORTED))
1187 *max_sectors = ata_tf_to_lba48(&tf) + 1;
1189 *max_sectors = ata_tf_to_lba(&tf) + 1;
1190 if (dev->horkage & ATA_HORKAGE_HPA_SIZE)
1196 * ata_set_max_sectors - Set max sectors
1197 * @dev: target device
1198 * @new_sectors: new max sectors value to set for the device
1200 * Set max sectors of @dev to @new_sectors.
1203 * 0 on success, -EACCES if command is aborted or denied (due to
1204 * previous non-volatile SET_MAX) by the drive. -EIO on other
1207 static int ata_set_max_sectors(struct ata_device *dev, u64 new_sectors)
1209 unsigned int err_mask;
1210 struct ata_taskfile tf;
1211 int lba48 = ata_id_has_lba48(dev->id);
1215 ata_tf_init(dev, &tf);
1217 tf.flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_ISADDR;
1220 tf.command = ATA_CMD_SET_MAX_EXT;
1221 tf.flags |= ATA_TFLAG_LBA48;
1223 tf.hob_lbal = (new_sectors >> 24) & 0xff;
1224 tf.hob_lbam = (new_sectors >> 32) & 0xff;
1225 tf.hob_lbah = (new_sectors >> 40) & 0xff;
1227 tf.command = ATA_CMD_SET_MAX;
1229 tf.device |= (new_sectors >> 24) & 0xf;
1232 tf.protocol = ATA_PROT_NODATA;
1233 tf.device |= ATA_LBA;
1235 tf.lbal = (new_sectors >> 0) & 0xff;
1236 tf.lbam = (new_sectors >> 8) & 0xff;
1237 tf.lbah = (new_sectors >> 16) & 0xff;
1239 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
1242 "failed to set max address (err_mask=0x%x)\n",
1244 if (err_mask == AC_ERR_DEV &&
1245 (tf.error & (ATA_ABORTED | ATA_IDNF)))
1254 * ata_hpa_resize - Resize a device with an HPA set
1255 * @dev: Device to resize
1257 * Read the size of an LBA28 or LBA48 disk with HPA features and resize
1258 * it if required to the full size of the media. The caller must check
1259 * the drive has the HPA feature set enabled.
1262 * 0 on success, -errno on failure.
1264 static int ata_hpa_resize(struct ata_device *dev)
1266 bool print_info = ata_dev_print_info(dev);
1267 bool unlock_hpa = ata_ignore_hpa || dev->flags & ATA_DFLAG_UNLOCK_HPA;
1268 u64 sectors = ata_id_n_sectors(dev->id);
1272 /* do we need to do it? */
1273 if ((dev->class != ATA_DEV_ATA && dev->class != ATA_DEV_ZAC) ||
1274 !ata_id_has_lba(dev->id) || !ata_id_hpa_enabled(dev->id) ||
1275 (dev->horkage & ATA_HORKAGE_BROKEN_HPA))
1278 /* read native max address */
1279 rc = ata_read_native_max_address(dev, &native_sectors);
1281 /* If device aborted the command or HPA isn't going to
1282 * be unlocked, skip HPA resizing.
1284 if (rc == -EACCES || !unlock_hpa) {
1286 "HPA support seems broken, skipping HPA handling\n");
1287 dev->horkage |= ATA_HORKAGE_BROKEN_HPA;
1289 /* we can continue if device aborted the command */
1296 dev->n_native_sectors = native_sectors;
1298 /* nothing to do? */
1299 if (native_sectors <= sectors || !unlock_hpa) {
1300 if (!print_info || native_sectors == sectors)
1303 if (native_sectors > sectors)
1305 "HPA detected: current %llu, native %llu\n",
1306 (unsigned long long)sectors,
1307 (unsigned long long)native_sectors);
1308 else if (native_sectors < sectors)
1310 "native sectors (%llu) is smaller than sectors (%llu)\n",
1311 (unsigned long long)native_sectors,
1312 (unsigned long long)sectors);
1316 /* let's unlock HPA */
1317 rc = ata_set_max_sectors(dev, native_sectors);
1318 if (rc == -EACCES) {
1319 /* if device aborted the command, skip HPA resizing */
1321 "device aborted resize (%llu -> %llu), skipping HPA handling\n",
1322 (unsigned long long)sectors,
1323 (unsigned long long)native_sectors);
1324 dev->horkage |= ATA_HORKAGE_BROKEN_HPA;
1329 /* re-read IDENTIFY data */
1330 rc = ata_dev_reread_id(dev, 0);
1333 "failed to re-read IDENTIFY data after HPA resizing\n");
1338 u64 new_sectors = ata_id_n_sectors(dev->id);
1340 "HPA unlocked: %llu -> %llu, native %llu\n",
1341 (unsigned long long)sectors,
1342 (unsigned long long)new_sectors,
1343 (unsigned long long)native_sectors);
1350 * ata_dump_id - IDENTIFY DEVICE info debugging output
1351 * @dev: device from which the information is fetched
1352 * @id: IDENTIFY DEVICE page to dump
1354 * Dump selected 16-bit words from the given IDENTIFY DEVICE
1361 static inline void ata_dump_id(struct ata_device *dev, const u16 *id)
1364 "49==0x%04x 53==0x%04x 63==0x%04x 64==0x%04x 75==0x%04x\n"
1365 "80==0x%04x 81==0x%04x 82==0x%04x 83==0x%04x 84==0x%04x\n"
1366 "88==0x%04x 93==0x%04x\n",
1367 id[49], id[53], id[63], id[64], id[75], id[80],
1368 id[81], id[82], id[83], id[84], id[88], id[93]);
1372 * ata_id_xfermask - Compute xfermask from the given IDENTIFY data
1373 * @id: IDENTIFY data to compute xfer mask from
1375 * Compute the xfermask for this device. This is not as trivial
1376 * as it seems if we must consider early devices correctly.
1378 * FIXME: pre IDE drive timing (do we care ?).
1386 unsigned long ata_id_xfermask(const u16 *id)
1388 unsigned long pio_mask, mwdma_mask, udma_mask;
1390 /* Usual case. Word 53 indicates word 64 is valid */
1391 if (id[ATA_ID_FIELD_VALID] & (1 << 1)) {
1392 pio_mask = id[ATA_ID_PIO_MODES] & 0x03;
1396 /* If word 64 isn't valid then Word 51 high byte holds
1397 * the PIO timing number for the maximum. Turn it into
1400 u8 mode = (id[ATA_ID_OLD_PIO_MODES] >> 8) & 0xFF;
1401 if (mode < 5) /* Valid PIO range */
1402 pio_mask = (2 << mode) - 1;
1406 /* But wait.. there's more. Design your standards by
1407 * committee and you too can get a free iordy field to
1408 * process. However it is the speeds not the modes that
1409 * are supported... Note drivers using the timing API
1410 * will get this right anyway
1414 mwdma_mask = id[ATA_ID_MWDMA_MODES] & 0x07;
1416 if (ata_id_is_cfa(id)) {
1418 * Process compact flash extended modes
1420 int pio = (id[ATA_ID_CFA_MODES] >> 0) & 0x7;
1421 int dma = (id[ATA_ID_CFA_MODES] >> 3) & 0x7;
1424 pio_mask |= (1 << 5);
1426 pio_mask |= (1 << 6);
1428 mwdma_mask |= (1 << 3);
1430 mwdma_mask |= (1 << 4);
1434 if (id[ATA_ID_FIELD_VALID] & (1 << 2))
1435 udma_mask = id[ATA_ID_UDMA_MODES] & 0xff;
1437 return ata_pack_xfermask(pio_mask, mwdma_mask, udma_mask);
1439 EXPORT_SYMBOL_GPL(ata_id_xfermask);
1441 static void ata_qc_complete_internal(struct ata_queued_cmd *qc)
1443 struct completion *waiting = qc->private_data;
1449 * ata_exec_internal_sg - execute libata internal command
1450 * @dev: Device to which the command is sent
1451 * @tf: Taskfile registers for the command and the result
1452 * @cdb: CDB for packet command
1453 * @dma_dir: Data transfer direction of the command
1454 * @sgl: sg list for the data buffer of the command
1455 * @n_elem: Number of sg entries
1456 * @timeout: Timeout in msecs (0 for default)
1458 * Executes libata internal command with timeout. @tf contains
1459 * command on entry and result on return. Timeout and error
1460 * conditions are reported via return value. No recovery action
1461 * is taken after a command times out. It's caller's duty to
1462 * clean up after timeout.
1465 * None. Should be called with kernel context, might sleep.
1468 * Zero on success, AC_ERR_* mask on failure
1470 unsigned ata_exec_internal_sg(struct ata_device *dev,
1471 struct ata_taskfile *tf, const u8 *cdb,
1472 int dma_dir, struct scatterlist *sgl,
1473 unsigned int n_elem, unsigned long timeout)
1475 struct ata_link *link = dev->link;
1476 struct ata_port *ap = link->ap;
1477 u8 command = tf->command;
1478 int auto_timeout = 0;
1479 struct ata_queued_cmd *qc;
1480 unsigned int preempted_tag;
1481 u32 preempted_sactive;
1482 u64 preempted_qc_active;
1483 int preempted_nr_active_links;
1484 DECLARE_COMPLETION_ONSTACK(wait);
1485 unsigned long flags;
1486 unsigned int err_mask;
1489 spin_lock_irqsave(ap->lock, flags);
1491 /* no internal command while frozen */
1492 if (ap->pflags & ATA_PFLAG_FROZEN) {
1493 spin_unlock_irqrestore(ap->lock, flags);
1494 return AC_ERR_SYSTEM;
1497 /* initialize internal qc */
1498 qc = __ata_qc_from_tag(ap, ATA_TAG_INTERNAL);
1500 qc->tag = ATA_TAG_INTERNAL;
1507 preempted_tag = link->active_tag;
1508 preempted_sactive = link->sactive;
1509 preempted_qc_active = ap->qc_active;
1510 preempted_nr_active_links = ap->nr_active_links;
1511 link->active_tag = ATA_TAG_POISON;
1514 ap->nr_active_links = 0;
1516 /* prepare & issue qc */
1519 memcpy(qc->cdb, cdb, ATAPI_CDB_LEN);
1521 /* some SATA bridges need us to indicate data xfer direction */
1522 if (tf->protocol == ATAPI_PROT_DMA && (dev->flags & ATA_DFLAG_DMADIR) &&
1523 dma_dir == DMA_FROM_DEVICE)
1524 qc->tf.feature |= ATAPI_DMADIR;
1526 qc->flags |= ATA_QCFLAG_RESULT_TF;
1527 qc->dma_dir = dma_dir;
1528 if (dma_dir != DMA_NONE) {
1529 unsigned int i, buflen = 0;
1530 struct scatterlist *sg;
1532 for_each_sg(sgl, sg, n_elem, i)
1533 buflen += sg->length;
1535 ata_sg_init(qc, sgl, n_elem);
1536 qc->nbytes = buflen;
1539 qc->private_data = &wait;
1540 qc->complete_fn = ata_qc_complete_internal;
1544 spin_unlock_irqrestore(ap->lock, flags);
1547 if (ata_probe_timeout)
1548 timeout = ata_probe_timeout * 1000;
1550 timeout = ata_internal_cmd_timeout(dev, command);
1555 if (ap->ops->error_handler)
1558 rc = wait_for_completion_timeout(&wait, msecs_to_jiffies(timeout));
1560 if (ap->ops->error_handler)
1563 ata_sff_flush_pio_task(ap);
1566 spin_lock_irqsave(ap->lock, flags);
1568 /* We're racing with irq here. If we lose, the
1569 * following test prevents us from completing the qc
1570 * twice. If we win, the port is frozen and will be
1571 * cleaned up by ->post_internal_cmd().
1573 if (qc->flags & ATA_QCFLAG_ACTIVE) {
1574 qc->err_mask |= AC_ERR_TIMEOUT;
1576 if (ap->ops->error_handler)
1577 ata_port_freeze(ap);
1579 ata_qc_complete(qc);
1581 ata_dev_warn(dev, "qc timeout (cmd 0x%x)\n",
1585 spin_unlock_irqrestore(ap->lock, flags);
1588 /* do post_internal_cmd */
1589 if (ap->ops->post_internal_cmd)
1590 ap->ops->post_internal_cmd(qc);
1592 /* perform minimal error analysis */
1593 if (qc->flags & ATA_QCFLAG_FAILED) {
1594 if (qc->result_tf.status & (ATA_ERR | ATA_DF))
1595 qc->err_mask |= AC_ERR_DEV;
1598 qc->err_mask |= AC_ERR_OTHER;
1600 if (qc->err_mask & ~AC_ERR_OTHER)
1601 qc->err_mask &= ~AC_ERR_OTHER;
1602 } else if (qc->tf.command == ATA_CMD_REQ_SENSE_DATA) {
1603 qc->result_tf.status |= ATA_SENSE;
1607 spin_lock_irqsave(ap->lock, flags);
1609 *tf = qc->result_tf;
1610 err_mask = qc->err_mask;
1613 link->active_tag = preempted_tag;
1614 link->sactive = preempted_sactive;
1615 ap->qc_active = preempted_qc_active;
1616 ap->nr_active_links = preempted_nr_active_links;
1618 spin_unlock_irqrestore(ap->lock, flags);
1620 if ((err_mask & AC_ERR_TIMEOUT) && auto_timeout)
1621 ata_internal_cmd_timed_out(dev, command);
1627 * ata_exec_internal - execute libata internal command
1628 * @dev: Device to which the command is sent
1629 * @tf: Taskfile registers for the command and the result
1630 * @cdb: CDB for packet command
1631 * @dma_dir: Data transfer direction of the command
1632 * @buf: Data buffer of the command
1633 * @buflen: Length of data buffer
1634 * @timeout: Timeout in msecs (0 for default)
1636 * Wrapper around ata_exec_internal_sg() which takes simple
1637 * buffer instead of sg list.
1640 * None. Should be called with kernel context, might sleep.
1643 * Zero on success, AC_ERR_* mask on failure
1645 unsigned ata_exec_internal(struct ata_device *dev,
1646 struct ata_taskfile *tf, const u8 *cdb,
1647 int dma_dir, void *buf, unsigned int buflen,
1648 unsigned long timeout)
1650 struct scatterlist *psg = NULL, sg;
1651 unsigned int n_elem = 0;
1653 if (dma_dir != DMA_NONE) {
1655 sg_init_one(&sg, buf, buflen);
1660 return ata_exec_internal_sg(dev, tf, cdb, dma_dir, psg, n_elem,
1665 * ata_pio_need_iordy - check if iordy needed
1668 * Check if the current speed of the device requires IORDY. Used
1669 * by various controllers for chip configuration.
1671 unsigned int ata_pio_need_iordy(const struct ata_device *adev)
1673 /* Don't set IORDY if we're preparing for reset. IORDY may
1674 * lead to controller lock up on certain controllers if the
1675 * port is not occupied. See bko#11703 for details.
1677 if (adev->link->ap->pflags & ATA_PFLAG_RESETTING)
1679 /* Controller doesn't support IORDY. Probably a pointless
1680 * check as the caller should know this.
1682 if (adev->link->ap->flags & ATA_FLAG_NO_IORDY)
1684 /* CF spec. r4.1 Table 22 says no iordy on PIO5 and PIO6. */
1685 if (ata_id_is_cfa(adev->id)
1686 && (adev->pio_mode == XFER_PIO_5 || adev->pio_mode == XFER_PIO_6))
1688 /* PIO3 and higher it is mandatory */
1689 if (adev->pio_mode > XFER_PIO_2)
1691 /* We turn it on when possible */
1692 if (ata_id_has_iordy(adev->id))
1696 EXPORT_SYMBOL_GPL(ata_pio_need_iordy);
1699 * ata_pio_mask_no_iordy - Return the non IORDY mask
1702 * Compute the highest mode possible if we are not using iordy. Return
1703 * -1 if no iordy mode is available.
1705 static u32 ata_pio_mask_no_iordy(const struct ata_device *adev)
1707 /* If we have no drive specific rule, then PIO 2 is non IORDY */
1708 if (adev->id[ATA_ID_FIELD_VALID] & 2) { /* EIDE */
1709 u16 pio = adev->id[ATA_ID_EIDE_PIO];
1710 /* Is the speed faster than the drive allows non IORDY ? */
1712 /* This is cycle times not frequency - watch the logic! */
1713 if (pio > 240) /* PIO2 is 240nS per cycle */
1714 return 3 << ATA_SHIFT_PIO;
1715 return 7 << ATA_SHIFT_PIO;
1718 return 3 << ATA_SHIFT_PIO;
1722 * ata_do_dev_read_id - default ID read method
1724 * @tf: proposed taskfile
1727 * Issue the identify taskfile and hand back the buffer containing
1728 * identify data. For some RAID controllers and for pre ATA devices
1729 * this function is wrapped or replaced by the driver
1731 unsigned int ata_do_dev_read_id(struct ata_device *dev,
1732 struct ata_taskfile *tf, __le16 *id)
1734 return ata_exec_internal(dev, tf, NULL, DMA_FROM_DEVICE,
1735 id, sizeof(id[0]) * ATA_ID_WORDS, 0);
1737 EXPORT_SYMBOL_GPL(ata_do_dev_read_id);
1740 * ata_dev_read_id - Read ID data from the specified device
1741 * @dev: target device
1742 * @p_class: pointer to class of the target device (may be changed)
1743 * @flags: ATA_READID_* flags
1744 * @id: buffer to read IDENTIFY data into
1746 * Read ID data from the specified device. ATA_CMD_ID_ATA is
1747 * performed on ATA devices and ATA_CMD_ID_ATAPI on ATAPI
1748 * devices. This function also issues ATA_CMD_INIT_DEV_PARAMS
1749 * for pre-ATA4 drives.
1751 * FIXME: ATA_CMD_ID_ATA is optional for early drives and right
1752 * now we abort if we hit that case.
1755 * Kernel thread context (may sleep)
1758 * 0 on success, -errno otherwise.
1760 int ata_dev_read_id(struct ata_device *dev, unsigned int *p_class,
1761 unsigned int flags, u16 *id)
1763 struct ata_port *ap = dev->link->ap;
1764 unsigned int class = *p_class;
1765 struct ata_taskfile tf;
1766 unsigned int err_mask = 0;
1768 bool is_semb = class == ATA_DEV_SEMB;
1769 int may_fallback = 1, tried_spinup = 0;
1773 ata_tf_init(dev, &tf);
1777 class = ATA_DEV_ATA; /* some hard drives report SEMB sig */
1781 tf.command = ATA_CMD_ID_ATA;
1784 tf.command = ATA_CMD_ID_ATAPI;
1788 reason = "unsupported class";
1792 tf.protocol = ATA_PROT_PIO;
1794 /* Some devices choke if TF registers contain garbage. Make
1795 * sure those are properly initialized.
1797 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
1799 /* Device presence detection is unreliable on some
1800 * controllers. Always poll IDENTIFY if available.
1802 tf.flags |= ATA_TFLAG_POLLING;
1804 if (ap->ops->read_id)
1805 err_mask = ap->ops->read_id(dev, &tf, (__le16 *)id);
1807 err_mask = ata_do_dev_read_id(dev, &tf, (__le16 *)id);
1810 if (err_mask & AC_ERR_NODEV_HINT) {
1811 ata_dev_dbg(dev, "NODEV after polling detection\n");
1817 "IDENTIFY failed on device w/ SEMB sig, disabled\n");
1818 /* SEMB is not supported yet */
1819 *p_class = ATA_DEV_SEMB_UNSUP;
1823 if ((err_mask == AC_ERR_DEV) && (tf.error & ATA_ABORTED)) {
1824 /* Device or controller might have reported
1825 * the wrong device class. Give a shot at the
1826 * other IDENTIFY if the current one is
1827 * aborted by the device.
1832 if (class == ATA_DEV_ATA)
1833 class = ATA_DEV_ATAPI;
1835 class = ATA_DEV_ATA;
1839 /* Control reaches here iff the device aborted
1840 * both flavors of IDENTIFYs which happens
1841 * sometimes with phantom devices.
1844 "both IDENTIFYs aborted, assuming NODEV\n");
1849 reason = "I/O error";
1853 if (dev->horkage & ATA_HORKAGE_DUMP_ID) {
1854 ata_dev_info(dev, "dumping IDENTIFY data, "
1855 "class=%d may_fallback=%d tried_spinup=%d\n",
1856 class, may_fallback, tried_spinup);
1857 print_hex_dump(KERN_INFO, "", DUMP_PREFIX_OFFSET,
1858 16, 2, id, ATA_ID_WORDS * sizeof(*id), true);
1861 /* Falling back doesn't make sense if ID data was read
1862 * successfully at least once.
1866 swap_buf_le16(id, ATA_ID_WORDS);
1870 reason = "device reports invalid type";
1872 if (class == ATA_DEV_ATA || class == ATA_DEV_ZAC) {
1873 if (!ata_id_is_ata(id) && !ata_id_is_cfa(id))
1875 if (ap->host->flags & ATA_HOST_IGNORE_ATA &&
1876 ata_id_is_ata(id)) {
1878 "host indicates ignore ATA devices, ignored\n");
1882 if (ata_id_is_ata(id))
1886 if (!tried_spinup && (id[2] == 0x37c8 || id[2] == 0x738c)) {
1889 * Drive powered-up in standby mode, and requires a specific
1890 * SET_FEATURES spin-up subcommand before it will accept
1891 * anything other than the original IDENTIFY command.
1893 err_mask = ata_dev_set_feature(dev, SETFEATURES_SPINUP, 0);
1894 if (err_mask && id[2] != 0x738c) {
1896 reason = "SPINUP failed";
1900 * If the drive initially returned incomplete IDENTIFY info,
1901 * we now must reissue the IDENTIFY command.
1903 if (id[2] == 0x37c8)
1907 if ((flags & ATA_READID_POSTRESET) &&
1908 (class == ATA_DEV_ATA || class == ATA_DEV_ZAC)) {
1910 * The exact sequence expected by certain pre-ATA4 drives is:
1912 * IDENTIFY (optional in early ATA)
1913 * INITIALIZE DEVICE PARAMETERS (later IDE and ATA)
1915 * Some drives were very specific about that exact sequence.
1917 * Note that ATA4 says lba is mandatory so the second check
1918 * should never trigger.
1920 if (ata_id_major_version(id) < 4 || !ata_id_has_lba(id)) {
1921 err_mask = ata_dev_init_params(dev, id[3], id[6]);
1924 reason = "INIT_DEV_PARAMS failed";
1928 /* current CHS translation info (id[53-58]) might be
1929 * changed. reread the identify device info.
1931 flags &= ~ATA_READID_POSTRESET;
1941 ata_dev_warn(dev, "failed to IDENTIFY (%s, err_mask=0x%x)\n",
1947 * ata_read_log_page - read a specific log page
1948 * @dev: target device
1950 * @page: page to read
1951 * @buf: buffer to store read page
1952 * @sectors: number of sectors to read
1954 * Read log page using READ_LOG_EXT command.
1957 * Kernel thread context (may sleep).
1960 * 0 on success, AC_ERR_* mask otherwise.
1962 unsigned int ata_read_log_page(struct ata_device *dev, u8 log,
1963 u8 page, void *buf, unsigned int sectors)
1965 unsigned long ap_flags = dev->link->ap->flags;
1966 struct ata_taskfile tf;
1967 unsigned int err_mask;
1970 ata_dev_dbg(dev, "read log page - log 0x%x, page 0x%x\n", log, page);
1973 * Return error without actually issuing the command on controllers
1974 * which e.g. lockup on a read log page.
1976 if (ap_flags & ATA_FLAG_NO_LOG_PAGE)
1980 ata_tf_init(dev, &tf);
1981 if (ata_dma_enabled(dev) && ata_id_has_read_log_dma_ext(dev->id) &&
1982 !(dev->horkage & ATA_HORKAGE_NO_DMA_LOG)) {
1983 tf.command = ATA_CMD_READ_LOG_DMA_EXT;
1984 tf.protocol = ATA_PROT_DMA;
1987 tf.command = ATA_CMD_READ_LOG_EXT;
1988 tf.protocol = ATA_PROT_PIO;
1994 tf.hob_nsect = sectors >> 8;
1995 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_LBA48 | ATA_TFLAG_DEVICE;
1997 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_FROM_DEVICE,
1998 buf, sectors * ATA_SECT_SIZE, 0);
2002 dev->horkage |= ATA_HORKAGE_NO_DMA_LOG;
2006 "Read log 0x%02x page 0x%02x failed, Emask 0x%x\n",
2007 (unsigned int)log, (unsigned int)page, err_mask);
2013 static bool ata_log_supported(struct ata_device *dev, u8 log)
2015 struct ata_port *ap = dev->link->ap;
2017 if (dev->horkage & ATA_HORKAGE_NO_LOG_DIR)
2020 if (ata_read_log_page(dev, ATA_LOG_DIRECTORY, 0, ap->sector_buf, 1))
2022 return get_unaligned_le16(&ap->sector_buf[log * 2]) ? true : false;
2025 static bool ata_identify_page_supported(struct ata_device *dev, u8 page)
2027 struct ata_port *ap = dev->link->ap;
2028 unsigned int err, i;
2030 if (dev->horkage & ATA_HORKAGE_NO_ID_DEV_LOG)
2033 if (!ata_log_supported(dev, ATA_LOG_IDENTIFY_DEVICE)) {
2035 * IDENTIFY DEVICE data log is defined as mandatory starting
2036 * with ACS-3 (ATA version 10). Warn about the missing log
2037 * for drives which implement this ATA level or above.
2039 if (ata_id_major_version(dev->id) >= 10)
2041 "ATA Identify Device Log not supported\n");
2042 dev->horkage |= ATA_HORKAGE_NO_ID_DEV_LOG;
2047 * Read IDENTIFY DEVICE data log, page 0, to figure out if the page is
2050 err = ata_read_log_page(dev, ATA_LOG_IDENTIFY_DEVICE, 0, ap->sector_buf,
2055 for (i = 0; i < ap->sector_buf[8]; i++) {
2056 if (ap->sector_buf[9 + i] == page)
2063 static int ata_do_link_spd_horkage(struct ata_device *dev)
2065 struct ata_link *plink = ata_dev_phys_link(dev);
2066 u32 target, target_limit;
2068 if (!sata_scr_valid(plink))
2071 if (dev->horkage & ATA_HORKAGE_1_5_GBPS)
2076 target_limit = (1 << target) - 1;
2078 /* if already on stricter limit, no need to push further */
2079 if (plink->sata_spd_limit <= target_limit)
2082 plink->sata_spd_limit = target_limit;
2084 /* Request another EH round by returning -EAGAIN if link is
2085 * going faster than the target speed. Forward progress is
2086 * guaranteed by setting sata_spd_limit to target_limit above.
2088 if (plink->sata_spd > target) {
2089 ata_dev_info(dev, "applying link speed limit horkage to %s\n",
2090 sata_spd_string(target));
2096 static inline u8 ata_dev_knobble(struct ata_device *dev)
2098 struct ata_port *ap = dev->link->ap;
2100 if (ata_dev_blacklisted(dev) & ATA_HORKAGE_BRIDGE_OK)
2103 return ((ap->cbl == ATA_CBL_SATA) && (!ata_id_is_sata(dev->id)));
2106 static void ata_dev_config_ncq_send_recv(struct ata_device *dev)
2108 struct ata_port *ap = dev->link->ap;
2109 unsigned int err_mask;
2111 if (!ata_log_supported(dev, ATA_LOG_NCQ_SEND_RECV)) {
2112 ata_dev_warn(dev, "NCQ Send/Recv Log not supported\n");
2115 err_mask = ata_read_log_page(dev, ATA_LOG_NCQ_SEND_RECV,
2116 0, ap->sector_buf, 1);
2118 u8 *cmds = dev->ncq_send_recv_cmds;
2120 dev->flags |= ATA_DFLAG_NCQ_SEND_RECV;
2121 memcpy(cmds, ap->sector_buf, ATA_LOG_NCQ_SEND_RECV_SIZE);
2123 if (dev->horkage & ATA_HORKAGE_NO_NCQ_TRIM) {
2124 ata_dev_dbg(dev, "disabling queued TRIM support\n");
2125 cmds[ATA_LOG_NCQ_SEND_RECV_DSM_OFFSET] &=
2126 ~ATA_LOG_NCQ_SEND_RECV_DSM_TRIM;
2131 static void ata_dev_config_ncq_non_data(struct ata_device *dev)
2133 struct ata_port *ap = dev->link->ap;
2134 unsigned int err_mask;
2136 if (!ata_log_supported(dev, ATA_LOG_NCQ_NON_DATA)) {
2138 "NCQ Send/Recv Log not supported\n");
2141 err_mask = ata_read_log_page(dev, ATA_LOG_NCQ_NON_DATA,
2142 0, ap->sector_buf, 1);
2144 u8 *cmds = dev->ncq_non_data_cmds;
2146 memcpy(cmds, ap->sector_buf, ATA_LOG_NCQ_NON_DATA_SIZE);
2150 static void ata_dev_config_ncq_prio(struct ata_device *dev)
2152 struct ata_port *ap = dev->link->ap;
2153 unsigned int err_mask;
2155 if (!ata_identify_page_supported(dev, ATA_LOG_SATA_SETTINGS))
2158 err_mask = ata_read_log_page(dev,
2159 ATA_LOG_IDENTIFY_DEVICE,
2160 ATA_LOG_SATA_SETTINGS,
2166 if (!(ap->sector_buf[ATA_LOG_NCQ_PRIO_OFFSET] & BIT(3)))
2169 dev->flags |= ATA_DFLAG_NCQ_PRIO;
2174 dev->flags &= ~ATA_DFLAG_NCQ_PRIO_ENABLE;
2175 dev->flags &= ~ATA_DFLAG_NCQ_PRIO;
2178 static bool ata_dev_check_adapter(struct ata_device *dev,
2179 unsigned short vendor_id)
2181 struct pci_dev *pcidev = NULL;
2182 struct device *parent_dev = NULL;
2184 for (parent_dev = dev->tdev.parent; parent_dev != NULL;
2185 parent_dev = parent_dev->parent) {
2186 if (dev_is_pci(parent_dev)) {
2187 pcidev = to_pci_dev(parent_dev);
2188 if (pcidev->vendor == vendor_id)
2197 static int ata_dev_config_ncq(struct ata_device *dev,
2198 char *desc, size_t desc_sz)
2200 struct ata_port *ap = dev->link->ap;
2201 int hdepth = 0, ddepth = ata_id_queue_depth(dev->id);
2202 unsigned int err_mask;
2205 if (!ata_id_has_ncq(dev->id)) {
2209 if (!IS_ENABLED(CONFIG_SATA_HOST))
2211 if (dev->horkage & ATA_HORKAGE_NONCQ) {
2212 snprintf(desc, desc_sz, "NCQ (not used)");
2216 if (dev->horkage & ATA_HORKAGE_NO_NCQ_ON_ATI &&
2217 ata_dev_check_adapter(dev, PCI_VENDOR_ID_ATI)) {
2218 snprintf(desc, desc_sz, "NCQ (not used)");
2222 if (ap->flags & ATA_FLAG_NCQ) {
2223 hdepth = min(ap->scsi_host->can_queue, ATA_MAX_QUEUE);
2224 dev->flags |= ATA_DFLAG_NCQ;
2227 if (!(dev->horkage & ATA_HORKAGE_BROKEN_FPDMA_AA) &&
2228 (ap->flags & ATA_FLAG_FPDMA_AA) &&
2229 ata_id_has_fpdma_aa(dev->id)) {
2230 err_mask = ata_dev_set_feature(dev, SETFEATURES_SATA_ENABLE,
2234 "failed to enable AA (error_mask=0x%x)\n",
2236 if (err_mask != AC_ERR_DEV) {
2237 dev->horkage |= ATA_HORKAGE_BROKEN_FPDMA_AA;
2244 if (hdepth >= ddepth)
2245 snprintf(desc, desc_sz, "NCQ (depth %d)%s", ddepth, aa_desc);
2247 snprintf(desc, desc_sz, "NCQ (depth %d/%d)%s", hdepth,
2250 if ((ap->flags & ATA_FLAG_FPDMA_AUX)) {
2251 if (ata_id_has_ncq_send_and_recv(dev->id))
2252 ata_dev_config_ncq_send_recv(dev);
2253 if (ata_id_has_ncq_non_data(dev->id))
2254 ata_dev_config_ncq_non_data(dev);
2255 if (ata_id_has_ncq_prio(dev->id))
2256 ata_dev_config_ncq_prio(dev);
2262 static void ata_dev_config_sense_reporting(struct ata_device *dev)
2264 unsigned int err_mask;
2266 if (!ata_id_has_sense_reporting(dev->id))
2269 if (ata_id_sense_reporting_enabled(dev->id))
2272 err_mask = ata_dev_set_feature(dev, SETFEATURE_SENSE_DATA, 0x1);
2275 "failed to enable Sense Data Reporting, Emask 0x%x\n",
2280 static void ata_dev_config_zac(struct ata_device *dev)
2282 struct ata_port *ap = dev->link->ap;
2283 unsigned int err_mask;
2284 u8 *identify_buf = ap->sector_buf;
2286 dev->zac_zones_optimal_open = U32_MAX;
2287 dev->zac_zones_optimal_nonseq = U32_MAX;
2288 dev->zac_zones_max_open = U32_MAX;
2291 * Always set the 'ZAC' flag for Host-managed devices.
2293 if (dev->class == ATA_DEV_ZAC)
2294 dev->flags |= ATA_DFLAG_ZAC;
2295 else if (ata_id_zoned_cap(dev->id) == 0x01)
2297 * Check for host-aware devices.
2299 dev->flags |= ATA_DFLAG_ZAC;
2301 if (!(dev->flags & ATA_DFLAG_ZAC))
2304 if (!ata_identify_page_supported(dev, ATA_LOG_ZONED_INFORMATION)) {
2306 "ATA Zoned Information Log not supported\n");
2311 * Read IDENTIFY DEVICE data log, page 9 (Zoned-device information)
2313 err_mask = ata_read_log_page(dev, ATA_LOG_IDENTIFY_DEVICE,
2314 ATA_LOG_ZONED_INFORMATION,
2317 u64 zoned_cap, opt_open, opt_nonseq, max_open;
2319 zoned_cap = get_unaligned_le64(&identify_buf[8]);
2320 if ((zoned_cap >> 63))
2321 dev->zac_zoned_cap = (zoned_cap & 1);
2322 opt_open = get_unaligned_le64(&identify_buf[24]);
2323 if ((opt_open >> 63))
2324 dev->zac_zones_optimal_open = (u32)opt_open;
2325 opt_nonseq = get_unaligned_le64(&identify_buf[32]);
2326 if ((opt_nonseq >> 63))
2327 dev->zac_zones_optimal_nonseq = (u32)opt_nonseq;
2328 max_open = get_unaligned_le64(&identify_buf[40]);
2329 if ((max_open >> 63))
2330 dev->zac_zones_max_open = (u32)max_open;
2334 static void ata_dev_config_trusted(struct ata_device *dev)
2336 struct ata_port *ap = dev->link->ap;
2340 if (!ata_id_has_trusted(dev->id))
2343 if (!ata_identify_page_supported(dev, ATA_LOG_SECURITY)) {
2345 "Security Log not supported\n");
2349 err = ata_read_log_page(dev, ATA_LOG_IDENTIFY_DEVICE, ATA_LOG_SECURITY,
2354 trusted_cap = get_unaligned_le64(&ap->sector_buf[40]);
2355 if (!(trusted_cap & (1ULL << 63))) {
2357 "Trusted Computing capability qword not valid!\n");
2361 if (trusted_cap & (1 << 0))
2362 dev->flags |= ATA_DFLAG_TRUSTED;
2365 static int ata_dev_config_lba(struct ata_device *dev)
2367 const u16 *id = dev->id;
2368 const char *lba_desc;
2372 dev->flags |= ATA_DFLAG_LBA;
2374 if (ata_id_has_lba48(id)) {
2376 dev->flags |= ATA_DFLAG_LBA48;
2377 if (dev->n_sectors >= (1UL << 28) &&
2378 ata_id_has_flush_ext(id))
2379 dev->flags |= ATA_DFLAG_FLUSH_EXT;
2385 ret = ata_dev_config_ncq(dev, ncq_desc, sizeof(ncq_desc));
2387 /* print device info to dmesg */
2388 if (ata_dev_print_info(dev))
2390 "%llu sectors, multi %u: %s %s\n",
2391 (unsigned long long)dev->n_sectors,
2392 dev->multi_count, lba_desc, ncq_desc);
2397 static void ata_dev_config_chs(struct ata_device *dev)
2399 const u16 *id = dev->id;
2401 if (ata_id_current_chs_valid(id)) {
2402 /* Current CHS translation is valid. */
2403 dev->cylinders = id[54];
2404 dev->heads = id[55];
2405 dev->sectors = id[56];
2407 /* Default translation */
2408 dev->cylinders = id[1];
2410 dev->sectors = id[6];
2413 /* print device info to dmesg */
2414 if (ata_dev_print_info(dev))
2416 "%llu sectors, multi %u, CHS %u/%u/%u\n",
2417 (unsigned long long)dev->n_sectors,
2418 dev->multi_count, dev->cylinders,
2419 dev->heads, dev->sectors);
2422 static void ata_dev_config_devslp(struct ata_device *dev)
2424 u8 *sata_setting = dev->link->ap->sector_buf;
2425 unsigned int err_mask;
2429 * Check device sleep capability. Get DevSlp timing variables
2430 * from SATA Settings page of Identify Device Data Log.
2432 if (!ata_id_has_devslp(dev->id) ||
2433 !ata_identify_page_supported(dev, ATA_LOG_SATA_SETTINGS))
2436 err_mask = ata_read_log_page(dev,
2437 ATA_LOG_IDENTIFY_DEVICE,
2438 ATA_LOG_SATA_SETTINGS,
2443 dev->flags |= ATA_DFLAG_DEVSLP;
2444 for (i = 0; i < ATA_LOG_DEVSLP_SIZE; i++) {
2445 j = ATA_LOG_DEVSLP_OFFSET + i;
2446 dev->devslp_timing[i] = sata_setting[j];
2450 static void ata_dev_config_cpr(struct ata_device *dev)
2452 unsigned int err_mask;
2455 struct ata_cpr_log *cpr_log = NULL;
2456 u8 *desc, *buf = NULL;
2458 if (ata_id_major_version(dev->id) < 11 ||
2459 !ata_log_supported(dev, ATA_LOG_CONCURRENT_POSITIONING_RANGES))
2463 * Read the concurrent positioning ranges log (0x47). We can have at
2464 * most 255 32B range descriptors plus a 64B header.
2466 buf_len = (64 + 255 * 32 + 511) & ~511;
2467 buf = kzalloc(buf_len, GFP_KERNEL);
2471 err_mask = ata_read_log_page(dev, ATA_LOG_CONCURRENT_POSITIONING_RANGES,
2472 0, buf, buf_len >> 9);
2480 cpr_log = kzalloc(struct_size(cpr_log, cpr, nr_cpr), GFP_KERNEL);
2484 cpr_log->nr_cpr = nr_cpr;
2486 for (i = 0; i < nr_cpr; i++, desc += 32) {
2487 cpr_log->cpr[i].num = desc[0];
2488 cpr_log->cpr[i].num_storage_elements = desc[1];
2489 cpr_log->cpr[i].start_lba = get_unaligned_le64(&desc[8]);
2490 cpr_log->cpr[i].num_lbas = get_unaligned_le64(&desc[16]);
2494 swap(dev->cpr_log, cpr_log);
2499 static void ata_dev_print_features(struct ata_device *dev)
2501 if (!(dev->flags & ATA_DFLAG_FEATURES_MASK))
2505 "Features:%s%s%s%s%s%s\n",
2506 dev->flags & ATA_DFLAG_TRUSTED ? " Trust" : "",
2507 dev->flags & ATA_DFLAG_DA ? " Dev-Attention" : "",
2508 dev->flags & ATA_DFLAG_DEVSLP ? " Dev-Sleep" : "",
2509 dev->flags & ATA_DFLAG_NCQ_SEND_RECV ? " NCQ-sndrcv" : "",
2510 dev->flags & ATA_DFLAG_NCQ_PRIO ? " NCQ-prio" : "",
2511 dev->cpr_log ? " CPR" : "");
2515 * ata_dev_configure - Configure the specified ATA/ATAPI device
2516 * @dev: Target device to configure
2518 * Configure @dev according to @dev->id. Generic and low-level
2519 * driver specific fixups are also applied.
2522 * Kernel thread context (may sleep)
2525 * 0 on success, -errno otherwise
2527 int ata_dev_configure(struct ata_device *dev)
2529 struct ata_port *ap = dev->link->ap;
2530 bool print_info = ata_dev_print_info(dev);
2531 const u16 *id = dev->id;
2532 unsigned long xfer_mask;
2533 unsigned int err_mask;
2534 char revbuf[7]; /* XYZ-99\0 */
2535 char fwrevbuf[ATA_ID_FW_REV_LEN+1];
2536 char modelbuf[ATA_ID_PROD_LEN+1];
2539 if (!ata_dev_enabled(dev)) {
2540 ata_dev_dbg(dev, "no device\n");
2545 dev->horkage |= ata_dev_blacklisted(dev);
2546 ata_force_horkage(dev);
2548 if (dev->horkage & ATA_HORKAGE_DISABLE) {
2549 ata_dev_info(dev, "unsupported device, disabling\n");
2550 ata_dev_disable(dev);
2554 if ((!atapi_enabled || (ap->flags & ATA_FLAG_NO_ATAPI)) &&
2555 dev->class == ATA_DEV_ATAPI) {
2556 ata_dev_warn(dev, "WARNING: ATAPI is %s, device ignored\n",
2557 atapi_enabled ? "not supported with this driver"
2559 ata_dev_disable(dev);
2563 rc = ata_do_link_spd_horkage(dev);
2567 /* some WD SATA-1 drives have issues with LPM, turn on NOLPM for them */
2568 if ((dev->horkage & ATA_HORKAGE_WD_BROKEN_LPM) &&
2569 (id[ATA_ID_SATA_CAPABILITY] & 0xe) == 0x2)
2570 dev->horkage |= ATA_HORKAGE_NOLPM;
2572 if (ap->flags & ATA_FLAG_NO_LPM)
2573 dev->horkage |= ATA_HORKAGE_NOLPM;
2575 if (dev->horkage & ATA_HORKAGE_NOLPM) {
2576 ata_dev_warn(dev, "LPM support broken, forcing max_power\n");
2577 dev->link->ap->target_lpm_policy = ATA_LPM_MAX_POWER;
2580 /* let ACPI work its magic */
2581 rc = ata_acpi_on_devcfg(dev);
2585 /* massage HPA, do it early as it might change IDENTIFY data */
2586 rc = ata_hpa_resize(dev);
2590 /* print device capabilities */
2592 "%s: cfg 49:%04x 82:%04x 83:%04x 84:%04x "
2593 "85:%04x 86:%04x 87:%04x 88:%04x\n",
2595 id[49], id[82], id[83], id[84],
2596 id[85], id[86], id[87], id[88]);
2598 /* initialize to-be-configured parameters */
2599 dev->flags &= ~ATA_DFLAG_CFG_MASK;
2600 dev->max_sectors = 0;
2606 dev->multi_count = 0;
2609 * common ATA, ATAPI feature tests
2612 /* find max transfer mode; for printk only */
2613 xfer_mask = ata_id_xfermask(id);
2615 ata_dump_id(dev, id);
2617 /* SCSI only uses 4-char revisions, dump full 8 chars from ATA */
2618 ata_id_c_string(dev->id, fwrevbuf, ATA_ID_FW_REV,
2621 ata_id_c_string(dev->id, modelbuf, ATA_ID_PROD,
2624 /* ATA-specific feature tests */
2625 if (dev->class == ATA_DEV_ATA || dev->class == ATA_DEV_ZAC) {
2626 if (ata_id_is_cfa(id)) {
2627 /* CPRM may make this media unusable */
2628 if (id[ATA_ID_CFA_KEY_MGMT] & 1)
2630 "supports DRM functions and may not be fully accessible\n");
2631 snprintf(revbuf, 7, "CFA");
2633 snprintf(revbuf, 7, "ATA-%d", ata_id_major_version(id));
2634 /* Warn the user if the device has TPM extensions */
2635 if (ata_id_has_tpm(id))
2637 "supports DRM functions and may not be fully accessible\n");
2640 dev->n_sectors = ata_id_n_sectors(id);
2642 /* get current R/W Multiple count setting */
2643 if ((dev->id[47] >> 8) == 0x80 && (dev->id[59] & 0x100)) {
2644 unsigned int max = dev->id[47] & 0xff;
2645 unsigned int cnt = dev->id[59] & 0xff;
2646 /* only recognize/allow powers of two here */
2647 if (is_power_of_2(max) && is_power_of_2(cnt))
2649 dev->multi_count = cnt;
2652 /* print device info to dmesg */
2654 ata_dev_info(dev, "%s: %s, %s, max %s\n",
2655 revbuf, modelbuf, fwrevbuf,
2656 ata_mode_string(xfer_mask));
2658 if (ata_id_has_lba(id)) {
2659 rc = ata_dev_config_lba(dev);
2663 ata_dev_config_chs(dev);
2666 ata_dev_config_devslp(dev);
2667 ata_dev_config_sense_reporting(dev);
2668 ata_dev_config_zac(dev);
2669 ata_dev_config_trusted(dev);
2670 ata_dev_config_cpr(dev);
2674 ata_dev_print_features(dev);
2677 /* ATAPI-specific feature tests */
2678 else if (dev->class == ATA_DEV_ATAPI) {
2679 const char *cdb_intr_string = "";
2680 const char *atapi_an_string = "";
2681 const char *dma_dir_string = "";
2684 rc = atapi_cdb_len(id);
2685 if ((rc < 12) || (rc > ATAPI_CDB_LEN)) {
2686 ata_dev_warn(dev, "unsupported CDB len %d\n", rc);
2690 dev->cdb_len = (unsigned int) rc;
2692 /* Enable ATAPI AN if both the host and device have
2693 * the support. If PMP is attached, SNTF is required
2694 * to enable ATAPI AN to discern between PHY status
2695 * changed notifications and ATAPI ANs.
2698 (ap->flags & ATA_FLAG_AN) && ata_id_has_atapi_AN(id) &&
2699 (!sata_pmp_attached(ap) ||
2700 sata_scr_read(&ap->link, SCR_NOTIFICATION, &sntf) == 0)) {
2701 /* issue SET feature command to turn this on */
2702 err_mask = ata_dev_set_feature(dev,
2703 SETFEATURES_SATA_ENABLE, SATA_AN);
2706 "failed to enable ATAPI AN (err_mask=0x%x)\n",
2709 dev->flags |= ATA_DFLAG_AN;
2710 atapi_an_string = ", ATAPI AN";
2714 if (ata_id_cdb_intr(dev->id)) {
2715 dev->flags |= ATA_DFLAG_CDB_INTR;
2716 cdb_intr_string = ", CDB intr";
2719 if (atapi_dmadir || (dev->horkage & ATA_HORKAGE_ATAPI_DMADIR) || atapi_id_dmadir(dev->id)) {
2720 dev->flags |= ATA_DFLAG_DMADIR;
2721 dma_dir_string = ", DMADIR";
2724 if (ata_id_has_da(dev->id)) {
2725 dev->flags |= ATA_DFLAG_DA;
2729 /* print device info to dmesg */
2732 "ATAPI: %s, %s, max %s%s%s%s\n",
2734 ata_mode_string(xfer_mask),
2735 cdb_intr_string, atapi_an_string,
2739 /* determine max_sectors */
2740 dev->max_sectors = ATA_MAX_SECTORS;
2741 if (dev->flags & ATA_DFLAG_LBA48)
2742 dev->max_sectors = ATA_MAX_SECTORS_LBA48;
2744 /* Limit PATA drive on SATA cable bridge transfers to udma5,
2746 if (ata_dev_knobble(dev)) {
2748 ata_dev_info(dev, "applying bridge limits\n");
2749 dev->udma_mask &= ATA_UDMA5;
2750 dev->max_sectors = ATA_MAX_SECTORS;
2753 if ((dev->class == ATA_DEV_ATAPI) &&
2754 (atapi_command_packet_set(id) == TYPE_TAPE)) {
2755 dev->max_sectors = ATA_MAX_SECTORS_TAPE;
2756 dev->horkage |= ATA_HORKAGE_STUCK_ERR;
2759 if (dev->horkage & ATA_HORKAGE_MAX_SEC_128)
2760 dev->max_sectors = min_t(unsigned int, ATA_MAX_SECTORS_128,
2763 if (dev->horkage & ATA_HORKAGE_MAX_SEC_1024)
2764 dev->max_sectors = min_t(unsigned int, ATA_MAX_SECTORS_1024,
2767 if (dev->horkage & ATA_HORKAGE_MAX_SEC_LBA48)
2768 dev->max_sectors = ATA_MAX_SECTORS_LBA48;
2770 if (ap->ops->dev_config)
2771 ap->ops->dev_config(dev);
2773 if (dev->horkage & ATA_HORKAGE_DIAGNOSTIC) {
2774 /* Let the user know. We don't want to disallow opens for
2775 rescue purposes, or in case the vendor is just a blithering
2776 idiot. Do this after the dev_config call as some controllers
2777 with buggy firmware may want to avoid reporting false device
2782 "Drive reports diagnostics failure. This may indicate a drive\n");
2784 "fault or invalid emulation. Contact drive vendor for information.\n");
2788 if ((dev->horkage & ATA_HORKAGE_FIRMWARE_WARN) && print_info) {
2789 ata_dev_warn(dev, "WARNING: device requires firmware update to be fully functional\n");
2790 ata_dev_warn(dev, " contact the vendor or visit http://ata.wiki.kernel.org\n");
2800 * ata_cable_40wire - return 40 wire cable type
2803 * Helper method for drivers which want to hardwire 40 wire cable
2807 int ata_cable_40wire(struct ata_port *ap)
2809 return ATA_CBL_PATA40;
2811 EXPORT_SYMBOL_GPL(ata_cable_40wire);
2814 * ata_cable_80wire - return 80 wire cable type
2817 * Helper method for drivers which want to hardwire 80 wire cable
2821 int ata_cable_80wire(struct ata_port *ap)
2823 return ATA_CBL_PATA80;
2825 EXPORT_SYMBOL_GPL(ata_cable_80wire);
2828 * ata_cable_unknown - return unknown PATA cable.
2831 * Helper method for drivers which have no PATA cable detection.
2834 int ata_cable_unknown(struct ata_port *ap)
2836 return ATA_CBL_PATA_UNK;
2838 EXPORT_SYMBOL_GPL(ata_cable_unknown);
2841 * ata_cable_ignore - return ignored PATA cable.
2844 * Helper method for drivers which don't use cable type to limit
2847 int ata_cable_ignore(struct ata_port *ap)
2849 return ATA_CBL_PATA_IGN;
2851 EXPORT_SYMBOL_GPL(ata_cable_ignore);
2854 * ata_cable_sata - return SATA cable type
2857 * Helper method for drivers which have SATA cables
2860 int ata_cable_sata(struct ata_port *ap)
2862 return ATA_CBL_SATA;
2864 EXPORT_SYMBOL_GPL(ata_cable_sata);
2867 * ata_bus_probe - Reset and probe ATA bus
2870 * Master ATA bus probing function. Initiates a hardware-dependent
2871 * bus reset, then attempts to identify any devices found on
2875 * PCI/etc. bus probe sem.
2878 * Zero on success, negative errno otherwise.
2881 int ata_bus_probe(struct ata_port *ap)
2883 unsigned int classes[ATA_MAX_DEVICES];
2884 int tries[ATA_MAX_DEVICES];
2886 struct ata_device *dev;
2888 ata_for_each_dev(dev, &ap->link, ALL)
2889 tries[dev->devno] = ATA_PROBE_MAX_TRIES;
2892 ata_for_each_dev(dev, &ap->link, ALL) {
2893 /* If we issue an SRST then an ATA drive (not ATAPI)
2894 * may change configuration and be in PIO0 timing. If
2895 * we do a hard reset (or are coming from power on)
2896 * this is true for ATA or ATAPI. Until we've set a
2897 * suitable controller mode we should not touch the
2898 * bus as we may be talking too fast.
2900 dev->pio_mode = XFER_PIO_0;
2901 dev->dma_mode = 0xff;
2903 /* If the controller has a pio mode setup function
2904 * then use it to set the chipset to rights. Don't
2905 * touch the DMA setup as that will be dealt with when
2906 * configuring devices.
2908 if (ap->ops->set_piomode)
2909 ap->ops->set_piomode(ap, dev);
2912 /* reset and determine device classes */
2913 ap->ops->phy_reset(ap);
2915 ata_for_each_dev(dev, &ap->link, ALL) {
2916 if (dev->class != ATA_DEV_UNKNOWN)
2917 classes[dev->devno] = dev->class;
2919 classes[dev->devno] = ATA_DEV_NONE;
2921 dev->class = ATA_DEV_UNKNOWN;
2924 /* read IDENTIFY page and configure devices. We have to do the identify
2925 specific sequence bass-ackwards so that PDIAG- is released by
2928 ata_for_each_dev(dev, &ap->link, ALL_REVERSE) {
2929 if (tries[dev->devno])
2930 dev->class = classes[dev->devno];
2932 if (!ata_dev_enabled(dev))
2935 rc = ata_dev_read_id(dev, &dev->class, ATA_READID_POSTRESET,
2941 /* Now ask for the cable type as PDIAG- should have been released */
2942 if (ap->ops->cable_detect)
2943 ap->cbl = ap->ops->cable_detect(ap);
2945 /* We may have SATA bridge glue hiding here irrespective of
2946 * the reported cable types and sensed types. When SATA
2947 * drives indicate we have a bridge, we don't know which end
2948 * of the link the bridge is which is a problem.
2950 ata_for_each_dev(dev, &ap->link, ENABLED)
2951 if (ata_id_is_sata(dev->id))
2952 ap->cbl = ATA_CBL_SATA;
2954 /* After the identify sequence we can now set up the devices. We do
2955 this in the normal order so that the user doesn't get confused */
2957 ata_for_each_dev(dev, &ap->link, ENABLED) {
2958 ap->link.eh_context.i.flags |= ATA_EHI_PRINTINFO;
2959 rc = ata_dev_configure(dev);
2960 ap->link.eh_context.i.flags &= ~ATA_EHI_PRINTINFO;
2965 /* configure transfer mode */
2966 rc = ata_set_mode(&ap->link, &dev);
2970 ata_for_each_dev(dev, &ap->link, ENABLED)
2976 tries[dev->devno]--;
2980 /* eeek, something went very wrong, give up */
2981 tries[dev->devno] = 0;
2985 /* give it just one more chance */
2986 tries[dev->devno] = min(tries[dev->devno], 1);
2989 if (tries[dev->devno] == 1) {
2990 /* This is the last chance, better to slow
2991 * down than lose it.
2993 sata_down_spd_limit(&ap->link, 0);
2994 ata_down_xfermask_limit(dev, ATA_DNXFER_PIO);
2998 if (!tries[dev->devno])
2999 ata_dev_disable(dev);
3005 * sata_print_link_status - Print SATA link status
3006 * @link: SATA link to printk link status about
3008 * This function prints link speed and status of a SATA link.
3013 static void sata_print_link_status(struct ata_link *link)
3015 u32 sstatus, scontrol, tmp;
3017 if (sata_scr_read(link, SCR_STATUS, &sstatus))
3019 sata_scr_read(link, SCR_CONTROL, &scontrol);
3021 if (ata_phys_link_online(link)) {
3022 tmp = (sstatus >> 4) & 0xf;
3023 ata_link_info(link, "SATA link up %s (SStatus %X SControl %X)\n",
3024 sata_spd_string(tmp), sstatus, scontrol);
3026 ata_link_info(link, "SATA link down (SStatus %X SControl %X)\n",
3032 * ata_dev_pair - return other device on cable
3035 * Obtain the other device on the same cable, or if none is
3036 * present NULL is returned
3039 struct ata_device *ata_dev_pair(struct ata_device *adev)
3041 struct ata_link *link = adev->link;
3042 struct ata_device *pair = &link->device[1 - adev->devno];
3043 if (!ata_dev_enabled(pair))
3047 EXPORT_SYMBOL_GPL(ata_dev_pair);
3050 * sata_down_spd_limit - adjust SATA spd limit downward
3051 * @link: Link to adjust SATA spd limit for
3052 * @spd_limit: Additional limit
3054 * Adjust SATA spd limit of @link downward. Note that this
3055 * function only adjusts the limit. The change must be applied
3056 * using sata_set_spd().
3058 * If @spd_limit is non-zero, the speed is limited to equal to or
3059 * lower than @spd_limit if such speed is supported. If
3060 * @spd_limit is slower than any supported speed, only the lowest
3061 * supported speed is allowed.
3064 * Inherited from caller.
3067 * 0 on success, negative errno on failure
3069 int sata_down_spd_limit(struct ata_link *link, u32 spd_limit)
3071 u32 sstatus, spd, mask;
3074 if (!sata_scr_valid(link))
3077 /* If SCR can be read, use it to determine the current SPD.
3078 * If not, use cached value in link->sata_spd.
3080 rc = sata_scr_read(link, SCR_STATUS, &sstatus);
3081 if (rc == 0 && ata_sstatus_online(sstatus))
3082 spd = (sstatus >> 4) & 0xf;
3084 spd = link->sata_spd;
3086 mask = link->sata_spd_limit;
3090 /* unconditionally mask off the highest bit */
3091 bit = fls(mask) - 1;
3092 mask &= ~(1 << bit);
3095 * Mask off all speeds higher than or equal to the current one. At
3096 * this point, if current SPD is not available and we previously
3097 * recorded the link speed from SStatus, the driver has already
3098 * masked off the highest bit so mask should already be 1 or 0.
3099 * Otherwise, we should not force 1.5Gbps on a link where we have
3100 * not previously recorded speed from SStatus. Just return in this
3104 mask &= (1 << (spd - 1)) - 1;
3108 /* were we already at the bottom? */
3113 if (mask & ((1 << spd_limit) - 1))
3114 mask &= (1 << spd_limit) - 1;
3116 bit = ffs(mask) - 1;
3121 link->sata_spd_limit = mask;
3123 ata_link_warn(link, "limiting SATA link speed to %s\n",
3124 sata_spd_string(fls(mask)));
3129 #ifdef CONFIG_ATA_ACPI
3131 * ata_timing_cycle2mode - find xfer mode for the specified cycle duration
3132 * @xfer_shift: ATA_SHIFT_* value for transfer type to examine.
3133 * @cycle: cycle duration in ns
3135 * Return matching xfer mode for @cycle. The returned mode is of
3136 * the transfer type specified by @xfer_shift. If @cycle is too
3137 * slow for @xfer_shift, 0xff is returned. If @cycle is faster
3138 * than the fastest known mode, the fasted mode is returned.
3144 * Matching xfer_mode, 0xff if no match found.
3146 u8 ata_timing_cycle2mode(unsigned int xfer_shift, int cycle)
3148 u8 base_mode = 0xff, last_mode = 0xff;
3149 const struct ata_xfer_ent *ent;
3150 const struct ata_timing *t;
3152 for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
3153 if (ent->shift == xfer_shift)
3154 base_mode = ent->base;
3156 for (t = ata_timing_find_mode(base_mode);
3157 t && ata_xfer_mode2shift(t->mode) == xfer_shift; t++) {
3158 unsigned short this_cycle;
3160 switch (xfer_shift) {
3162 case ATA_SHIFT_MWDMA:
3163 this_cycle = t->cycle;
3165 case ATA_SHIFT_UDMA:
3166 this_cycle = t->udma;
3172 if (cycle > this_cycle)
3175 last_mode = t->mode;
3183 * ata_down_xfermask_limit - adjust dev xfer masks downward
3184 * @dev: Device to adjust xfer masks
3185 * @sel: ATA_DNXFER_* selector
3187 * Adjust xfer masks of @dev downward. Note that this function
3188 * does not apply the change. Invoking ata_set_mode() afterwards
3189 * will apply the limit.
3192 * Inherited from caller.
3195 * 0 on success, negative errno on failure
3197 int ata_down_xfermask_limit(struct ata_device *dev, unsigned int sel)
3200 unsigned long orig_mask, xfer_mask;
3201 unsigned long pio_mask, mwdma_mask, udma_mask;
3204 quiet = !!(sel & ATA_DNXFER_QUIET);
3205 sel &= ~ATA_DNXFER_QUIET;
3207 xfer_mask = orig_mask = ata_pack_xfermask(dev->pio_mask,
3210 ata_unpack_xfermask(xfer_mask, &pio_mask, &mwdma_mask, &udma_mask);
3213 case ATA_DNXFER_PIO:
3214 highbit = fls(pio_mask) - 1;
3215 pio_mask &= ~(1 << highbit);
3218 case ATA_DNXFER_DMA:
3220 highbit = fls(udma_mask) - 1;
3221 udma_mask &= ~(1 << highbit);
3224 } else if (mwdma_mask) {
3225 highbit = fls(mwdma_mask) - 1;
3226 mwdma_mask &= ~(1 << highbit);
3232 case ATA_DNXFER_40C:
3233 udma_mask &= ATA_UDMA_MASK_40C;
3236 case ATA_DNXFER_FORCE_PIO0:
3239 case ATA_DNXFER_FORCE_PIO:
3248 xfer_mask &= ata_pack_xfermask(pio_mask, mwdma_mask, udma_mask);
3250 if (!(xfer_mask & ATA_MASK_PIO) || xfer_mask == orig_mask)
3254 if (xfer_mask & (ATA_MASK_MWDMA | ATA_MASK_UDMA))
3255 snprintf(buf, sizeof(buf), "%s:%s",
3256 ata_mode_string(xfer_mask),
3257 ata_mode_string(xfer_mask & ATA_MASK_PIO));
3259 snprintf(buf, sizeof(buf), "%s",
3260 ata_mode_string(xfer_mask));
3262 ata_dev_warn(dev, "limiting speed to %s\n", buf);
3265 ata_unpack_xfermask(xfer_mask, &dev->pio_mask, &dev->mwdma_mask,
3271 static int ata_dev_set_mode(struct ata_device *dev)
3273 struct ata_port *ap = dev->link->ap;
3274 struct ata_eh_context *ehc = &dev->link->eh_context;
3275 const bool nosetxfer = dev->horkage & ATA_HORKAGE_NOSETXFER;
3276 const char *dev_err_whine = "";
3277 int ign_dev_err = 0;
3278 unsigned int err_mask = 0;
3281 dev->flags &= ~ATA_DFLAG_PIO;
3282 if (dev->xfer_shift == ATA_SHIFT_PIO)
3283 dev->flags |= ATA_DFLAG_PIO;
3285 if (nosetxfer && ap->flags & ATA_FLAG_SATA && ata_id_is_sata(dev->id))
3286 dev_err_whine = " (SET_XFERMODE skipped)";
3290 "NOSETXFER but PATA detected - can't "
3291 "skip SETXFER, might malfunction\n");
3292 err_mask = ata_dev_set_xfermode(dev);
3295 if (err_mask & ~AC_ERR_DEV)
3299 ehc->i.flags |= ATA_EHI_POST_SETMODE;
3300 rc = ata_dev_revalidate(dev, ATA_DEV_UNKNOWN, 0);
3301 ehc->i.flags &= ~ATA_EHI_POST_SETMODE;
3305 if (dev->xfer_shift == ATA_SHIFT_PIO) {
3306 /* Old CFA may refuse this command, which is just fine */
3307 if (ata_id_is_cfa(dev->id))
3309 /* Catch several broken garbage emulations plus some pre
3311 if (ata_id_major_version(dev->id) == 0 &&
3312 dev->pio_mode <= XFER_PIO_2)
3314 /* Some very old devices and some bad newer ones fail
3315 any kind of SET_XFERMODE request but support PIO0-2
3316 timings and no IORDY */
3317 if (!ata_id_has_iordy(dev->id) && dev->pio_mode <= XFER_PIO_2)
3320 /* Early MWDMA devices do DMA but don't allow DMA mode setting.
3321 Don't fail an MWDMA0 set IFF the device indicates it is in MWDMA0 */
3322 if (dev->xfer_shift == ATA_SHIFT_MWDMA &&
3323 dev->dma_mode == XFER_MW_DMA_0 &&
3324 (dev->id[63] >> 8) & 1)
3327 /* if the device is actually configured correctly, ignore dev err */
3328 if (dev->xfer_mode == ata_xfer_mask2mode(ata_id_xfermask(dev->id)))
3331 if (err_mask & AC_ERR_DEV) {
3335 dev_err_whine = " (device error ignored)";
3338 ata_dev_dbg(dev, "xfer_shift=%u, xfer_mode=0x%x\n",
3339 dev->xfer_shift, (int)dev->xfer_mode);
3341 if (!(ehc->i.flags & ATA_EHI_QUIET) ||
3342 ehc->i.flags & ATA_EHI_DID_HARDRESET)
3343 ata_dev_info(dev, "configured for %s%s\n",
3344 ata_mode_string(ata_xfer_mode2mask(dev->xfer_mode)),
3350 ata_dev_err(dev, "failed to set xfermode (err_mask=0x%x)\n", err_mask);
3355 * ata_do_set_mode - Program timings and issue SET FEATURES - XFER
3356 * @link: link on which timings will be programmed
3357 * @r_failed_dev: out parameter for failed device
3359 * Standard implementation of the function used to tune and set
3360 * ATA device disk transfer mode (PIO3, UDMA6, etc.). If
3361 * ata_dev_set_mode() fails, pointer to the failing device is
3362 * returned in @r_failed_dev.
3365 * PCI/etc. bus probe sem.
3368 * 0 on success, negative errno otherwise
3371 int ata_do_set_mode(struct ata_link *link, struct ata_device **r_failed_dev)
3373 struct ata_port *ap = link->ap;
3374 struct ata_device *dev;
3375 int rc = 0, used_dma = 0, found = 0;
3377 /* step 1: calculate xfer_mask */
3378 ata_for_each_dev(dev, link, ENABLED) {
3379 unsigned long pio_mask, dma_mask;
3380 unsigned int mode_mask;
3382 mode_mask = ATA_DMA_MASK_ATA;
3383 if (dev->class == ATA_DEV_ATAPI)
3384 mode_mask = ATA_DMA_MASK_ATAPI;
3385 else if (ata_id_is_cfa(dev->id))
3386 mode_mask = ATA_DMA_MASK_CFA;
3388 ata_dev_xfermask(dev);
3389 ata_force_xfermask(dev);
3391 pio_mask = ata_pack_xfermask(dev->pio_mask, 0, 0);
3393 if (libata_dma_mask & mode_mask)
3394 dma_mask = ata_pack_xfermask(0, dev->mwdma_mask,
3399 dev->pio_mode = ata_xfer_mask2mode(pio_mask);
3400 dev->dma_mode = ata_xfer_mask2mode(dma_mask);
3403 if (ata_dma_enabled(dev))
3409 /* step 2: always set host PIO timings */
3410 ata_for_each_dev(dev, link, ENABLED) {
3411 if (dev->pio_mode == 0xff) {
3412 ata_dev_warn(dev, "no PIO support\n");
3417 dev->xfer_mode = dev->pio_mode;
3418 dev->xfer_shift = ATA_SHIFT_PIO;
3419 if (ap->ops->set_piomode)
3420 ap->ops->set_piomode(ap, dev);
3423 /* step 3: set host DMA timings */
3424 ata_for_each_dev(dev, link, ENABLED) {
3425 if (!ata_dma_enabled(dev))
3428 dev->xfer_mode = dev->dma_mode;
3429 dev->xfer_shift = ata_xfer_mode2shift(dev->dma_mode);
3430 if (ap->ops->set_dmamode)
3431 ap->ops->set_dmamode(ap, dev);
3434 /* step 4: update devices' xfer mode */
3435 ata_for_each_dev(dev, link, ENABLED) {
3436 rc = ata_dev_set_mode(dev);
3441 /* Record simplex status. If we selected DMA then the other
3442 * host channels are not permitted to do so.
3444 if (used_dma && (ap->host->flags & ATA_HOST_SIMPLEX))
3445 ap->host->simplex_claimed = ap;
3449 *r_failed_dev = dev;
3452 EXPORT_SYMBOL_GPL(ata_do_set_mode);
3455 * ata_wait_ready - wait for link to become ready
3456 * @link: link to be waited on
3457 * @deadline: deadline jiffies for the operation
3458 * @check_ready: callback to check link readiness
3460 * Wait for @link to become ready. @check_ready should return
3461 * positive number if @link is ready, 0 if it isn't, -ENODEV if
3462 * link doesn't seem to be occupied, other errno for other error
3465 * Transient -ENODEV conditions are allowed for
3466 * ATA_TMOUT_FF_WAIT.
3472 * 0 if @link is ready before @deadline; otherwise, -errno.
3474 int ata_wait_ready(struct ata_link *link, unsigned long deadline,
3475 int (*check_ready)(struct ata_link *link))
3477 unsigned long start = jiffies;
3478 unsigned long nodev_deadline;
3481 /* choose which 0xff timeout to use, read comment in libata.h */
3482 if (link->ap->host->flags & ATA_HOST_PARALLEL_SCAN)
3483 nodev_deadline = ata_deadline(start, ATA_TMOUT_FF_WAIT_LONG);
3485 nodev_deadline = ata_deadline(start, ATA_TMOUT_FF_WAIT);
3487 /* Slave readiness can't be tested separately from master. On
3488 * M/S emulation configuration, this function should be called
3489 * only on the master and it will handle both master and slave.
3491 WARN_ON(link == link->ap->slave_link);
3493 if (time_after(nodev_deadline, deadline))
3494 nodev_deadline = deadline;
3497 unsigned long now = jiffies;
3500 ready = tmp = check_ready(link);
3505 * -ENODEV could be transient. Ignore -ENODEV if link
3506 * is online. Also, some SATA devices take a long
3507 * time to clear 0xff after reset. Wait for
3508 * ATA_TMOUT_FF_WAIT[_LONG] on -ENODEV if link isn't
3511 * Note that some PATA controllers (pata_ali) explode
3512 * if status register is read more than once when
3513 * there's no device attached.
3515 if (ready == -ENODEV) {
3516 if (ata_link_online(link))
3518 else if ((link->ap->flags & ATA_FLAG_SATA) &&
3519 !ata_link_offline(link) &&
3520 time_before(now, nodev_deadline))
3526 if (time_after(now, deadline))
3529 if (!warned && time_after(now, start + 5 * HZ) &&
3530 (deadline - now > 3 * HZ)) {
3532 "link is slow to respond, please be patient "
3533 "(ready=%d)\n", tmp);
3537 ata_msleep(link->ap, 50);
3542 * ata_wait_after_reset - wait for link to become ready after reset
3543 * @link: link to be waited on
3544 * @deadline: deadline jiffies for the operation
3545 * @check_ready: callback to check link readiness
3547 * Wait for @link to become ready after reset.
3553 * 0 if @link is ready before @deadline; otherwise, -errno.
3555 int ata_wait_after_reset(struct ata_link *link, unsigned long deadline,
3556 int (*check_ready)(struct ata_link *link))
3558 ata_msleep(link->ap, ATA_WAIT_AFTER_RESET);
3560 return ata_wait_ready(link, deadline, check_ready);
3562 EXPORT_SYMBOL_GPL(ata_wait_after_reset);
3565 * ata_std_prereset - prepare for reset
3566 * @link: ATA link to be reset
3567 * @deadline: deadline jiffies for the operation
3569 * @link is about to be reset. Initialize it. Failure from
3570 * prereset makes libata abort whole reset sequence and give up
3571 * that port, so prereset should be best-effort. It does its
3572 * best to prepare for reset sequence but if things go wrong, it
3573 * should just whine, not fail.
3576 * Kernel thread context (may sleep)
3581 int ata_std_prereset(struct ata_link *link, unsigned long deadline)
3583 struct ata_port *ap = link->ap;
3584 struct ata_eh_context *ehc = &link->eh_context;
3585 const unsigned long *timing = sata_ehc_deb_timing(ehc);
3588 /* if we're about to do hardreset, nothing more to do */
3589 if (ehc->i.action & ATA_EH_HARDRESET)
3592 /* if SATA, resume link */
3593 if (ap->flags & ATA_FLAG_SATA) {
3594 rc = sata_link_resume(link, timing, deadline);
3595 /* whine about phy resume failure but proceed */
3596 if (rc && rc != -EOPNOTSUPP)
3598 "failed to resume link for reset (errno=%d)\n",
3602 /* no point in trying softreset on offline link */
3603 if (ata_phys_link_offline(link))
3604 ehc->i.action &= ~ATA_EH_SOFTRESET;
3608 EXPORT_SYMBOL_GPL(ata_std_prereset);
3611 * sata_std_hardreset - COMRESET w/o waiting or classification
3612 * @link: link to reset
3613 * @class: resulting class of attached device
3614 * @deadline: deadline jiffies for the operation
3616 * Standard SATA COMRESET w/o waiting or classification.
3619 * Kernel thread context (may sleep)
3622 * 0 if link offline, -EAGAIN if link online, -errno on errors.
3624 int sata_std_hardreset(struct ata_link *link, unsigned int *class,
3625 unsigned long deadline)
3627 const unsigned long *timing = sata_ehc_deb_timing(&link->eh_context);
3632 rc = sata_link_hardreset(link, timing, deadline, &online, NULL);
3633 return online ? -EAGAIN : rc;
3635 EXPORT_SYMBOL_GPL(sata_std_hardreset);
3638 * ata_std_postreset - standard postreset callback
3639 * @link: the target ata_link
3640 * @classes: classes of attached devices
3642 * This function is invoked after a successful reset. Note that
3643 * the device might have been reset more than once using
3644 * different reset methods before postreset is invoked.
3647 * Kernel thread context (may sleep)
3649 void ata_std_postreset(struct ata_link *link, unsigned int *classes)
3653 /* reset complete, clear SError */
3654 if (!sata_scr_read(link, SCR_ERROR, &serror))
3655 sata_scr_write(link, SCR_ERROR, serror);
3657 /* print link status */
3658 sata_print_link_status(link);
3660 EXPORT_SYMBOL_GPL(ata_std_postreset);
3663 * ata_dev_same_device - Determine whether new ID matches configured device
3664 * @dev: device to compare against
3665 * @new_class: class of the new device
3666 * @new_id: IDENTIFY page of the new device
3668 * Compare @new_class and @new_id against @dev and determine
3669 * whether @dev is the device indicated by @new_class and
3676 * 1 if @dev matches @new_class and @new_id, 0 otherwise.
3678 static int ata_dev_same_device(struct ata_device *dev, unsigned int new_class,
3681 const u16 *old_id = dev->id;
3682 unsigned char model[2][ATA_ID_PROD_LEN + 1];
3683 unsigned char serial[2][ATA_ID_SERNO_LEN + 1];
3685 if (dev->class != new_class) {
3686 ata_dev_info(dev, "class mismatch %d != %d\n",
3687 dev->class, new_class);
3691 ata_id_c_string(old_id, model[0], ATA_ID_PROD, sizeof(model[0]));
3692 ata_id_c_string(new_id, model[1], ATA_ID_PROD, sizeof(model[1]));
3693 ata_id_c_string(old_id, serial[0], ATA_ID_SERNO, sizeof(serial[0]));
3694 ata_id_c_string(new_id, serial[1], ATA_ID_SERNO, sizeof(serial[1]));
3696 if (strcmp(model[0], model[1])) {
3697 ata_dev_info(dev, "model number mismatch '%s' != '%s'\n",
3698 model[0], model[1]);
3702 if (strcmp(serial[0], serial[1])) {
3703 ata_dev_info(dev, "serial number mismatch '%s' != '%s'\n",
3704 serial[0], serial[1]);
3712 * ata_dev_reread_id - Re-read IDENTIFY data
3713 * @dev: target ATA device
3714 * @readid_flags: read ID flags
3716 * Re-read IDENTIFY page and make sure @dev is still attached to
3720 * Kernel thread context (may sleep)
3723 * 0 on success, negative errno otherwise
3725 int ata_dev_reread_id(struct ata_device *dev, unsigned int readid_flags)
3727 unsigned int class = dev->class;
3728 u16 *id = (void *)dev->link->ap->sector_buf;
3732 rc = ata_dev_read_id(dev, &class, readid_flags, id);
3736 /* is the device still there? */
3737 if (!ata_dev_same_device(dev, class, id))
3740 memcpy(dev->id, id, sizeof(id[0]) * ATA_ID_WORDS);
3745 * ata_dev_revalidate - Revalidate ATA device
3746 * @dev: device to revalidate
3747 * @new_class: new class code
3748 * @readid_flags: read ID flags
3750 * Re-read IDENTIFY page, make sure @dev is still attached to the
3751 * port and reconfigure it according to the new IDENTIFY page.
3754 * Kernel thread context (may sleep)
3757 * 0 on success, negative errno otherwise
3759 int ata_dev_revalidate(struct ata_device *dev, unsigned int new_class,
3760 unsigned int readid_flags)
3762 u64 n_sectors = dev->n_sectors;
3763 u64 n_native_sectors = dev->n_native_sectors;
3766 if (!ata_dev_enabled(dev))
3769 /* fail early if !ATA && !ATAPI to avoid issuing [P]IDENTIFY to PMP */
3770 if (ata_class_enabled(new_class) &&
3771 new_class != ATA_DEV_ATA &&
3772 new_class != ATA_DEV_ATAPI &&
3773 new_class != ATA_DEV_ZAC &&
3774 new_class != ATA_DEV_SEMB) {
3775 ata_dev_info(dev, "class mismatch %u != %u\n",
3776 dev->class, new_class);
3782 rc = ata_dev_reread_id(dev, readid_flags);
3786 /* configure device according to the new ID */
3787 rc = ata_dev_configure(dev);
3791 /* verify n_sectors hasn't changed */
3792 if (dev->class != ATA_DEV_ATA || !n_sectors ||
3793 dev->n_sectors == n_sectors)
3796 /* n_sectors has changed */
3797 ata_dev_warn(dev, "n_sectors mismatch %llu != %llu\n",
3798 (unsigned long long)n_sectors,
3799 (unsigned long long)dev->n_sectors);
3802 * Something could have caused HPA to be unlocked
3803 * involuntarily. If n_native_sectors hasn't changed and the
3804 * new size matches it, keep the device.
3806 if (dev->n_native_sectors == n_native_sectors &&
3807 dev->n_sectors > n_sectors && dev->n_sectors == n_native_sectors) {
3809 "new n_sectors matches native, probably "
3810 "late HPA unlock, n_sectors updated\n");
3811 /* use the larger n_sectors */
3816 * Some BIOSes boot w/o HPA but resume w/ HPA locked. Try
3817 * unlocking HPA in those cases.
3819 * https://bugzilla.kernel.org/show_bug.cgi?id=15396
3821 if (dev->n_native_sectors == n_native_sectors &&
3822 dev->n_sectors < n_sectors && n_sectors == n_native_sectors &&
3823 !(dev->horkage & ATA_HORKAGE_BROKEN_HPA)) {
3825 "old n_sectors matches native, probably "
3826 "late HPA lock, will try to unlock HPA\n");
3827 /* try unlocking HPA */
3828 dev->flags |= ATA_DFLAG_UNLOCK_HPA;
3833 /* restore original n_[native_]sectors and fail */
3834 dev->n_native_sectors = n_native_sectors;
3835 dev->n_sectors = n_sectors;
3837 ata_dev_err(dev, "revalidation failed (errno=%d)\n", rc);
3841 struct ata_blacklist_entry {
3842 const char *model_num;
3843 const char *model_rev;
3844 unsigned long horkage;
3847 static const struct ata_blacklist_entry ata_device_blacklist [] = {
3848 /* Devices with DMA related problems under Linux */
3849 { "WDC AC11000H", NULL, ATA_HORKAGE_NODMA },
3850 { "WDC AC22100H", NULL, ATA_HORKAGE_NODMA },
3851 { "WDC AC32500H", NULL, ATA_HORKAGE_NODMA },
3852 { "WDC AC33100H", NULL, ATA_HORKAGE_NODMA },
3853 { "WDC AC31600H", NULL, ATA_HORKAGE_NODMA },
3854 { "WDC AC32100H", "24.09P07", ATA_HORKAGE_NODMA },
3855 { "WDC AC23200L", "21.10N21", ATA_HORKAGE_NODMA },
3856 { "Compaq CRD-8241B", NULL, ATA_HORKAGE_NODMA },
3857 { "CRD-8400B", NULL, ATA_HORKAGE_NODMA },
3858 { "CRD-848[02]B", NULL, ATA_HORKAGE_NODMA },
3859 { "CRD-84", NULL, ATA_HORKAGE_NODMA },
3860 { "SanDisk SDP3B", NULL, ATA_HORKAGE_NODMA },
3861 { "SanDisk SDP3B-64", NULL, ATA_HORKAGE_NODMA },
3862 { "SANYO CD-ROM CRD", NULL, ATA_HORKAGE_NODMA },
3863 { "HITACHI CDR-8", NULL, ATA_HORKAGE_NODMA },
3864 { "HITACHI CDR-8[34]35",NULL, ATA_HORKAGE_NODMA },
3865 { "Toshiba CD-ROM XM-6202B", NULL, ATA_HORKAGE_NODMA },
3866 { "TOSHIBA CD-ROM XM-1702BC", NULL, ATA_HORKAGE_NODMA },
3867 { "CD-532E-A", NULL, ATA_HORKAGE_NODMA },
3868 { "E-IDE CD-ROM CR-840",NULL, ATA_HORKAGE_NODMA },
3869 { "CD-ROM Drive/F5A", NULL, ATA_HORKAGE_NODMA },
3870 { "WPI CDD-820", NULL, ATA_HORKAGE_NODMA },
3871 { "SAMSUNG CD-ROM SC-148C", NULL, ATA_HORKAGE_NODMA },
3872 { "SAMSUNG CD-ROM SC", NULL, ATA_HORKAGE_NODMA },
3873 { "ATAPI CD-ROM DRIVE 40X MAXIMUM",NULL,ATA_HORKAGE_NODMA },
3874 { "_NEC DV5800A", NULL, ATA_HORKAGE_NODMA },
3875 { "SAMSUNG CD-ROM SN-124", "N001", ATA_HORKAGE_NODMA },
3876 { "Seagate STT20000A", NULL, ATA_HORKAGE_NODMA },
3877 { " 2GB ATA Flash Disk", "ADMA428M", ATA_HORKAGE_NODMA },
3878 { "VRFDFC22048UCHC-TE*", NULL, ATA_HORKAGE_NODMA },
3879 /* Odd clown on sil3726/4726 PMPs */
3880 { "Config Disk", NULL, ATA_HORKAGE_DISABLE },
3881 /* Similar story with ASMedia 1092 */
3882 { "ASMT109x- Config", NULL, ATA_HORKAGE_DISABLE },
3884 /* Weird ATAPI devices */
3885 { "TORiSAN DVD-ROM DRD-N216", NULL, ATA_HORKAGE_MAX_SEC_128 },
3886 { "QUANTUM DAT DAT72-000", NULL, ATA_HORKAGE_ATAPI_MOD16_DMA },
3887 { "Slimtype DVD A DS8A8SH", NULL, ATA_HORKAGE_MAX_SEC_LBA48 },
3888 { "Slimtype DVD A DS8A9SH", NULL, ATA_HORKAGE_MAX_SEC_LBA48 },
3891 * Causes silent data corruption with higher max sects.
3892 * http://lkml.kernel.org/g/x49wpy40ysk.fsf@segfault.boston.devel.redhat.com
3894 { "ST380013AS", "3.20", ATA_HORKAGE_MAX_SEC_1024 },
3897 * These devices time out with higher max sects.
3898 * https://bugzilla.kernel.org/show_bug.cgi?id=121671
3900 { "LITEON CX1-JB*-HP", NULL, ATA_HORKAGE_MAX_SEC_1024 },
3901 { "LITEON EP1-*", NULL, ATA_HORKAGE_MAX_SEC_1024 },
3903 /* Devices we expect to fail diagnostics */
3905 /* Devices where NCQ should be avoided */
3907 { "WDC WD740ADFD-00", NULL, ATA_HORKAGE_NONCQ },
3908 { "WDC WD740ADFD-00NLR1", NULL, ATA_HORKAGE_NONCQ },
3909 /* http://thread.gmane.org/gmane.linux.ide/14907 */
3910 { "FUJITSU MHT2060BH", NULL, ATA_HORKAGE_NONCQ },
3912 { "Maxtor *", "BANC*", ATA_HORKAGE_NONCQ },
3913 { "Maxtor 7V300F0", "VA111630", ATA_HORKAGE_NONCQ },
3914 { "ST380817AS", "3.42", ATA_HORKAGE_NONCQ },
3915 { "ST3160023AS", "3.42", ATA_HORKAGE_NONCQ },
3916 { "OCZ CORE_SSD", "02.10104", ATA_HORKAGE_NONCQ },
3918 /* Seagate NCQ + FLUSH CACHE firmware bug */
3919 { "ST31500341AS", "SD1[5-9]", ATA_HORKAGE_NONCQ |
3920 ATA_HORKAGE_FIRMWARE_WARN },
3922 { "ST31000333AS", "SD1[5-9]", ATA_HORKAGE_NONCQ |
3923 ATA_HORKAGE_FIRMWARE_WARN },
3925 { "ST3640[36]23AS", "SD1[5-9]", ATA_HORKAGE_NONCQ |
3926 ATA_HORKAGE_FIRMWARE_WARN },
3928 { "ST3320[68]13AS", "SD1[5-9]", ATA_HORKAGE_NONCQ |
3929 ATA_HORKAGE_FIRMWARE_WARN },
3931 /* drives which fail FPDMA_AA activation (some may freeze afterwards)
3932 the ST disks also have LPM issues */
3933 { "ST1000LM024 HN-M101MBB", NULL, ATA_HORKAGE_BROKEN_FPDMA_AA |
3934 ATA_HORKAGE_NOLPM },
3935 { "VB0250EAVER", "HPG7", ATA_HORKAGE_BROKEN_FPDMA_AA },
3937 /* Blacklist entries taken from Silicon Image 3124/3132
3938 Windows driver .inf file - also several Linux problem reports */
3939 { "HTS541060G9SA00", "MB3OC60D", ATA_HORKAGE_NONCQ },
3940 { "HTS541080G9SA00", "MB4OC60D", ATA_HORKAGE_NONCQ },
3941 { "HTS541010G9SA00", "MBZOC60D", ATA_HORKAGE_NONCQ },
3943 /* https://bugzilla.kernel.org/show_bug.cgi?id=15573 */
3944 { "C300-CTFDDAC128MAG", "0001", ATA_HORKAGE_NONCQ },
3946 /* Sandisk SD7/8/9s lock up hard on large trims */
3947 { "SanDisk SD[789]*", NULL, ATA_HORKAGE_MAX_TRIM_128M },
3949 /* devices which puke on READ_NATIVE_MAX */
3950 { "HDS724040KLSA80", "KFAOA20N", ATA_HORKAGE_BROKEN_HPA },
3951 { "WDC WD3200JD-00KLB0", "WD-WCAMR1130137", ATA_HORKAGE_BROKEN_HPA },
3952 { "WDC WD2500JD-00HBB0", "WD-WMAL71490727", ATA_HORKAGE_BROKEN_HPA },
3953 { "MAXTOR 6L080L4", "A93.0500", ATA_HORKAGE_BROKEN_HPA },
3955 /* this one allows HPA unlocking but fails IOs on the area */
3956 { "OCZ-VERTEX", "1.30", ATA_HORKAGE_BROKEN_HPA },
3958 /* Devices which report 1 sector over size HPA */
3959 { "ST340823A", NULL, ATA_HORKAGE_HPA_SIZE },
3960 { "ST320413A", NULL, ATA_HORKAGE_HPA_SIZE },
3961 { "ST310211A", NULL, ATA_HORKAGE_HPA_SIZE },
3963 /* Devices which get the IVB wrong */
3964 { "QUANTUM FIREBALLlct10 05", "A03.0900", ATA_HORKAGE_IVB },
3965 /* Maybe we should just blacklist TSSTcorp... */
3966 { "TSSTcorp CDDVDW SH-S202[HJN]", "SB0[01]", ATA_HORKAGE_IVB },
3968 /* Devices that do not need bridging limits applied */
3969 { "MTRON MSP-SATA*", NULL, ATA_HORKAGE_BRIDGE_OK },
3970 { "BUFFALO HD-QSU2/R5", NULL, ATA_HORKAGE_BRIDGE_OK },
3972 /* Devices which aren't very happy with higher link speeds */
3973 { "WD My Book", NULL, ATA_HORKAGE_1_5_GBPS },
3974 { "Seagate FreeAgent GoFlex", NULL, ATA_HORKAGE_1_5_GBPS },
3977 * Devices which choke on SETXFER. Applies only if both the
3978 * device and controller are SATA.
3980 { "PIONEER DVD-RW DVRTD08", NULL, ATA_HORKAGE_NOSETXFER },
3981 { "PIONEER DVD-RW DVRTD08A", NULL, ATA_HORKAGE_NOSETXFER },
3982 { "PIONEER DVD-RW DVR-215", NULL, ATA_HORKAGE_NOSETXFER },
3983 { "PIONEER DVD-RW DVR-212D", NULL, ATA_HORKAGE_NOSETXFER },
3984 { "PIONEER DVD-RW DVR-216D", NULL, ATA_HORKAGE_NOSETXFER },
3986 /* Crucial BX100 SSD 500GB has broken LPM support */
3987 { "CT500BX100SSD1", NULL, ATA_HORKAGE_NOLPM },
3989 /* 512GB MX100 with MU01 firmware has both queued TRIM and LPM issues */
3990 { "Crucial_CT512MX100*", "MU01", ATA_HORKAGE_NO_NCQ_TRIM |
3991 ATA_HORKAGE_ZERO_AFTER_TRIM |
3992 ATA_HORKAGE_NOLPM },
3993 /* 512GB MX100 with newer firmware has only LPM issues */
3994 { "Crucial_CT512MX100*", NULL, ATA_HORKAGE_ZERO_AFTER_TRIM |
3995 ATA_HORKAGE_NOLPM },
3997 /* 480GB+ M500 SSDs have both queued TRIM and LPM issues */
3998 { "Crucial_CT480M500*", NULL, ATA_HORKAGE_NO_NCQ_TRIM |
3999 ATA_HORKAGE_ZERO_AFTER_TRIM |
4000 ATA_HORKAGE_NOLPM },
4001 { "Crucial_CT960M500*", NULL, ATA_HORKAGE_NO_NCQ_TRIM |
4002 ATA_HORKAGE_ZERO_AFTER_TRIM |
4003 ATA_HORKAGE_NOLPM },
4005 /* These specific Samsung models/firmware-revs do not handle LPM well */
4006 { "SAMSUNG MZMPC128HBFU-000MV", "CXM14M1Q", ATA_HORKAGE_NOLPM },
4007 { "SAMSUNG SSD PM830 mSATA *", "CXM13D1Q", ATA_HORKAGE_NOLPM },
4008 { "SAMSUNG MZ7TD256HAFV-000L9", NULL, ATA_HORKAGE_NOLPM },
4009 { "SAMSUNG MZ7TE512HMHP-000L1", "EXT06L0Q", ATA_HORKAGE_NOLPM },
4011 /* devices that don't properly handle queued TRIM commands */
4012 { "Micron_M500IT_*", "MU01", ATA_HORKAGE_NO_NCQ_TRIM |
4013 ATA_HORKAGE_ZERO_AFTER_TRIM },
4014 { "Micron_M500_*", NULL, ATA_HORKAGE_NO_NCQ_TRIM |
4015 ATA_HORKAGE_ZERO_AFTER_TRIM },
4016 { "Crucial_CT*M500*", NULL, ATA_HORKAGE_NO_NCQ_TRIM |
4017 ATA_HORKAGE_ZERO_AFTER_TRIM },
4018 { "Micron_M5[15]0_*", "MU01", ATA_HORKAGE_NO_NCQ_TRIM |
4019 ATA_HORKAGE_ZERO_AFTER_TRIM },
4020 { "Crucial_CT*M550*", "MU01", ATA_HORKAGE_NO_NCQ_TRIM |
4021 ATA_HORKAGE_ZERO_AFTER_TRIM },
4022 { "Crucial_CT*MX100*", "MU01", ATA_HORKAGE_NO_NCQ_TRIM |
4023 ATA_HORKAGE_ZERO_AFTER_TRIM },
4024 { "Samsung SSD 840 EVO*", NULL, ATA_HORKAGE_NO_NCQ_TRIM |
4025 ATA_HORKAGE_NO_DMA_LOG |
4026 ATA_HORKAGE_ZERO_AFTER_TRIM },
4027 { "Samsung SSD 840*", NULL, ATA_HORKAGE_NO_NCQ_TRIM |
4028 ATA_HORKAGE_ZERO_AFTER_TRIM },
4029 { "Samsung SSD 850*", NULL, ATA_HORKAGE_NO_NCQ_TRIM |
4030 ATA_HORKAGE_ZERO_AFTER_TRIM },
4031 { "Samsung SSD 860*", NULL, ATA_HORKAGE_NO_NCQ_TRIM |
4032 ATA_HORKAGE_ZERO_AFTER_TRIM |
4033 ATA_HORKAGE_NO_NCQ_ON_ATI },
4034 { "Samsung SSD 870*", NULL, ATA_HORKAGE_NO_NCQ_TRIM |
4035 ATA_HORKAGE_ZERO_AFTER_TRIM |
4036 ATA_HORKAGE_NO_NCQ_ON_ATI },
4037 { "FCCT*M500*", NULL, ATA_HORKAGE_NO_NCQ_TRIM |
4038 ATA_HORKAGE_ZERO_AFTER_TRIM },
4040 /* devices that don't properly handle TRIM commands */
4041 { "SuperSSpeed S238*", NULL, ATA_HORKAGE_NOTRIM },
4042 { "M88V29*", NULL, ATA_HORKAGE_NOTRIM },
4045 * As defined, the DRAT (Deterministic Read After Trim) and RZAT
4046 * (Return Zero After Trim) flags in the ATA Command Set are
4047 * unreliable in the sense that they only define what happens if
4048 * the device successfully executed the DSM TRIM command. TRIM
4049 * is only advisory, however, and the device is free to silently
4050 * ignore all or parts of the request.
4052 * Whitelist drives that are known to reliably return zeroes
4057 * The intel 510 drive has buggy DRAT/RZAT. Explicitly exclude
4058 * that model before whitelisting all other intel SSDs.
4060 { "INTEL*SSDSC2MH*", NULL, 0 },
4062 { "Micron*", NULL, ATA_HORKAGE_ZERO_AFTER_TRIM },
4063 { "Crucial*", NULL, ATA_HORKAGE_ZERO_AFTER_TRIM },
4064 { "INTEL*SSD*", NULL, ATA_HORKAGE_ZERO_AFTER_TRIM },
4065 { "SSD*INTEL*", NULL, ATA_HORKAGE_ZERO_AFTER_TRIM },
4066 { "Samsung*SSD*", NULL, ATA_HORKAGE_ZERO_AFTER_TRIM },
4067 { "SAMSUNG*SSD*", NULL, ATA_HORKAGE_ZERO_AFTER_TRIM },
4068 { "SAMSUNG*MZ7KM*", NULL, ATA_HORKAGE_ZERO_AFTER_TRIM },
4069 { "ST[1248][0248]0[FH]*", NULL, ATA_HORKAGE_ZERO_AFTER_TRIM },
4072 * Some WD SATA-I drives spin up and down erratically when the link
4073 * is put into the slumber mode. We don't have full list of the
4074 * affected devices. Disable LPM if the device matches one of the
4075 * known prefixes and is SATA-1. As a side effect LPM partial is
4078 * https://bugzilla.kernel.org/show_bug.cgi?id=57211
4080 { "WDC WD800JD-*", NULL, ATA_HORKAGE_WD_BROKEN_LPM },
4081 { "WDC WD1200JD-*", NULL, ATA_HORKAGE_WD_BROKEN_LPM },
4082 { "WDC WD1600JD-*", NULL, ATA_HORKAGE_WD_BROKEN_LPM },
4083 { "WDC WD2000JD-*", NULL, ATA_HORKAGE_WD_BROKEN_LPM },
4084 { "WDC WD2500JD-*", NULL, ATA_HORKAGE_WD_BROKEN_LPM },
4085 { "WDC WD3000JD-*", NULL, ATA_HORKAGE_WD_BROKEN_LPM },
4086 { "WDC WD3200JD-*", NULL, ATA_HORKAGE_WD_BROKEN_LPM },
4089 * This sata dom device goes on a walkabout when the ATA_LOG_DIRECTORY
4090 * log page is accessed. Ensure we never ask for this log page with
4093 { "SATADOM-ML 3ME", NULL, ATA_HORKAGE_NO_LOG_DIR },
4099 static unsigned long ata_dev_blacklisted(const struct ata_device *dev)
4101 unsigned char model_num[ATA_ID_PROD_LEN + 1];
4102 unsigned char model_rev[ATA_ID_FW_REV_LEN + 1];
4103 const struct ata_blacklist_entry *ad = ata_device_blacklist;
4105 ata_id_c_string(dev->id, model_num, ATA_ID_PROD, sizeof(model_num));
4106 ata_id_c_string(dev->id, model_rev, ATA_ID_FW_REV, sizeof(model_rev));
4108 while (ad->model_num) {
4109 if (glob_match(ad->model_num, model_num)) {
4110 if (ad->model_rev == NULL)
4112 if (glob_match(ad->model_rev, model_rev))
4120 static int ata_dma_blacklisted(const struct ata_device *dev)
4122 /* We don't support polling DMA.
4123 * DMA blacklist those ATAPI devices with CDB-intr (and use PIO)
4124 * if the LLDD handles only interrupts in the HSM_ST_LAST state.
4126 if ((dev->link->ap->flags & ATA_FLAG_PIO_POLLING) &&
4127 (dev->flags & ATA_DFLAG_CDB_INTR))
4129 return (dev->horkage & ATA_HORKAGE_NODMA) ? 1 : 0;
4133 * ata_is_40wire - check drive side detection
4136 * Perform drive side detection decoding, allowing for device vendors
4137 * who can't follow the documentation.
4140 static int ata_is_40wire(struct ata_device *dev)
4142 if (dev->horkage & ATA_HORKAGE_IVB)
4143 return ata_drive_40wire_relaxed(dev->id);
4144 return ata_drive_40wire(dev->id);
4148 * cable_is_40wire - 40/80/SATA decider
4149 * @ap: port to consider
4151 * This function encapsulates the policy for speed management
4152 * in one place. At the moment we don't cache the result but
4153 * there is a good case for setting ap->cbl to the result when
4154 * we are called with unknown cables (and figuring out if it
4155 * impacts hotplug at all).
4157 * Return 1 if the cable appears to be 40 wire.
4160 static int cable_is_40wire(struct ata_port *ap)
4162 struct ata_link *link;
4163 struct ata_device *dev;
4165 /* If the controller thinks we are 40 wire, we are. */
4166 if (ap->cbl == ATA_CBL_PATA40)
4169 /* If the controller thinks we are 80 wire, we are. */
4170 if (ap->cbl == ATA_CBL_PATA80 || ap->cbl == ATA_CBL_SATA)
4173 /* If the system is known to be 40 wire short cable (eg
4174 * laptop), then we allow 80 wire modes even if the drive
4177 if (ap->cbl == ATA_CBL_PATA40_SHORT)
4180 /* If the controller doesn't know, we scan.
4182 * Note: We look for all 40 wire detects at this point. Any
4183 * 80 wire detect is taken to be 80 wire cable because
4184 * - in many setups only the one drive (slave if present) will
4185 * give a valid detect
4186 * - if you have a non detect capable drive you don't want it
4187 * to colour the choice
4189 ata_for_each_link(link, ap, EDGE) {
4190 ata_for_each_dev(dev, link, ENABLED) {
4191 if (!ata_is_40wire(dev))
4199 * ata_dev_xfermask - Compute supported xfermask of the given device
4200 * @dev: Device to compute xfermask for
4202 * Compute supported xfermask of @dev and store it in
4203 * dev->*_mask. This function is responsible for applying all
4204 * known limits including host controller limits, device
4210 static void ata_dev_xfermask(struct ata_device *dev)
4212 struct ata_link *link = dev->link;
4213 struct ata_port *ap = link->ap;
4214 struct ata_host *host = ap->host;
4215 unsigned long xfer_mask;
4217 /* controller modes available */
4218 xfer_mask = ata_pack_xfermask(ap->pio_mask,
4219 ap->mwdma_mask, ap->udma_mask);
4221 /* drive modes available */
4222 xfer_mask &= ata_pack_xfermask(dev->pio_mask,
4223 dev->mwdma_mask, dev->udma_mask);
4224 xfer_mask &= ata_id_xfermask(dev->id);
4227 * CFA Advanced TrueIDE timings are not allowed on a shared
4230 if (ata_dev_pair(dev)) {
4231 /* No PIO5 or PIO6 */
4232 xfer_mask &= ~(0x03 << (ATA_SHIFT_PIO + 5));
4233 /* No MWDMA3 or MWDMA 4 */
4234 xfer_mask &= ~(0x03 << (ATA_SHIFT_MWDMA + 3));
4237 if (ata_dma_blacklisted(dev)) {
4238 xfer_mask &= ~(ATA_MASK_MWDMA | ATA_MASK_UDMA);
4240 "device is on DMA blacklist, disabling DMA\n");
4243 if ((host->flags & ATA_HOST_SIMPLEX) &&
4244 host->simplex_claimed && host->simplex_claimed != ap) {
4245 xfer_mask &= ~(ATA_MASK_MWDMA | ATA_MASK_UDMA);
4247 "simplex DMA is claimed by other device, disabling DMA\n");
4250 if (ap->flags & ATA_FLAG_NO_IORDY)
4251 xfer_mask &= ata_pio_mask_no_iordy(dev);
4253 if (ap->ops->mode_filter)
4254 xfer_mask = ap->ops->mode_filter(dev, xfer_mask);
4256 /* Apply cable rule here. Don't apply it early because when
4257 * we handle hot plug the cable type can itself change.
4258 * Check this last so that we know if the transfer rate was
4259 * solely limited by the cable.
4260 * Unknown or 80 wire cables reported host side are checked
4261 * drive side as well. Cases where we know a 40wire cable
4262 * is used safely for 80 are not checked here.
4264 if (xfer_mask & (0xF8 << ATA_SHIFT_UDMA))
4265 /* UDMA/44 or higher would be available */
4266 if (cable_is_40wire(ap)) {
4268 "limited to UDMA/33 due to 40-wire cable\n");
4269 xfer_mask &= ~(0xF8 << ATA_SHIFT_UDMA);
4272 ata_unpack_xfermask(xfer_mask, &dev->pio_mask,
4273 &dev->mwdma_mask, &dev->udma_mask);
4277 * ata_dev_set_xfermode - Issue SET FEATURES - XFER MODE command
4278 * @dev: Device to which command will be sent
4280 * Issue SET FEATURES - XFER MODE command to device @dev
4284 * PCI/etc. bus probe sem.
4287 * 0 on success, AC_ERR_* mask otherwise.
4290 static unsigned int ata_dev_set_xfermode(struct ata_device *dev)
4292 struct ata_taskfile tf;
4293 unsigned int err_mask;
4295 /* set up set-features taskfile */
4296 ata_dev_dbg(dev, "set features - xfer mode\n");
4298 /* Some controllers and ATAPI devices show flaky interrupt
4299 * behavior after setting xfer mode. Use polling instead.
4301 ata_tf_init(dev, &tf);
4302 tf.command = ATA_CMD_SET_FEATURES;
4303 tf.feature = SETFEATURES_XFER;
4304 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE | ATA_TFLAG_POLLING;
4305 tf.protocol = ATA_PROT_NODATA;
4306 /* If we are using IORDY we must send the mode setting command */
4307 if (ata_pio_need_iordy(dev))
4308 tf.nsect = dev->xfer_mode;
4309 /* If the device has IORDY and the controller does not - turn it off */
4310 else if (ata_id_has_iordy(dev->id))
4312 else /* In the ancient relic department - skip all of this */
4315 /* On some disks, this command causes spin-up, so we need longer timeout */
4316 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 15000);
4322 * ata_dev_set_feature - Issue SET FEATURES - SATA FEATURES
4323 * @dev: Device to which command will be sent
4324 * @enable: Whether to enable or disable the feature
4325 * @feature: The sector count represents the feature to set
4327 * Issue SET FEATURES - SATA FEATURES command to device @dev
4328 * on port @ap with sector count
4331 * PCI/etc. bus probe sem.
4334 * 0 on success, AC_ERR_* mask otherwise.
4336 unsigned int ata_dev_set_feature(struct ata_device *dev, u8 enable, u8 feature)
4338 struct ata_taskfile tf;
4339 unsigned int err_mask;
4340 unsigned long timeout = 0;
4342 /* set up set-features taskfile */
4343 ata_dev_dbg(dev, "set features - SATA features\n");
4345 ata_tf_init(dev, &tf);
4346 tf.command = ATA_CMD_SET_FEATURES;
4347 tf.feature = enable;
4348 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
4349 tf.protocol = ATA_PROT_NODATA;
4352 if (enable == SETFEATURES_SPINUP)
4353 timeout = ata_probe_timeout ?
4354 ata_probe_timeout * 1000 : SETFEATURES_SPINUP_TIMEOUT;
4355 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, timeout);
4359 EXPORT_SYMBOL_GPL(ata_dev_set_feature);
4362 * ata_dev_init_params - Issue INIT DEV PARAMS command
4363 * @dev: Device to which command will be sent
4364 * @heads: Number of heads (taskfile parameter)
4365 * @sectors: Number of sectors (taskfile parameter)
4368 * Kernel thread context (may sleep)
4371 * 0 on success, AC_ERR_* mask otherwise.
4373 static unsigned int ata_dev_init_params(struct ata_device *dev,
4374 u16 heads, u16 sectors)
4376 struct ata_taskfile tf;
4377 unsigned int err_mask;
4379 /* Number of sectors per track 1-255. Number of heads 1-16 */
4380 if (sectors < 1 || sectors > 255 || heads < 1 || heads > 16)
4381 return AC_ERR_INVALID;
4383 /* set up init dev params taskfile */
4384 ata_dev_dbg(dev, "init dev params \n");
4386 ata_tf_init(dev, &tf);
4387 tf.command = ATA_CMD_INIT_DEV_PARAMS;
4388 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
4389 tf.protocol = ATA_PROT_NODATA;
4391 tf.device |= (heads - 1) & 0x0f; /* max head = num. of heads - 1 */
4393 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
4394 /* A clean abort indicates an original or just out of spec drive
4395 and we should continue as we issue the setup based on the
4396 drive reported working geometry */
4397 if (err_mask == AC_ERR_DEV && (tf.error & ATA_ABORTED))
4404 * atapi_check_dma - Check whether ATAPI DMA can be supported
4405 * @qc: Metadata associated with taskfile to check
4407 * Allow low-level driver to filter ATA PACKET commands, returning
4408 * a status indicating whether or not it is OK to use DMA for the
4409 * supplied PACKET command.
4412 * spin_lock_irqsave(host lock)
4414 * RETURNS: 0 when ATAPI DMA can be used
4417 int atapi_check_dma(struct ata_queued_cmd *qc)
4419 struct ata_port *ap = qc->ap;
4421 /* Don't allow DMA if it isn't multiple of 16 bytes. Quite a
4422 * few ATAPI devices choke on such DMA requests.
4424 if (!(qc->dev->horkage & ATA_HORKAGE_ATAPI_MOD16_DMA) &&
4425 unlikely(qc->nbytes & 15))
4428 if (ap->ops->check_atapi_dma)
4429 return ap->ops->check_atapi_dma(qc);
4435 * ata_std_qc_defer - Check whether a qc needs to be deferred
4436 * @qc: ATA command in question
4438 * Non-NCQ commands cannot run with any other command, NCQ or
4439 * not. As upper layer only knows the queue depth, we are
4440 * responsible for maintaining exclusion. This function checks
4441 * whether a new command @qc can be issued.
4444 * spin_lock_irqsave(host lock)
4447 * ATA_DEFER_* if deferring is needed, 0 otherwise.
4449 int ata_std_qc_defer(struct ata_queued_cmd *qc)
4451 struct ata_link *link = qc->dev->link;
4453 if (ata_is_ncq(qc->tf.protocol)) {
4454 if (!ata_tag_valid(link->active_tag))
4457 if (!ata_tag_valid(link->active_tag) && !link->sactive)
4461 return ATA_DEFER_LINK;
4463 EXPORT_SYMBOL_GPL(ata_std_qc_defer);
4465 enum ata_completion_errors ata_noop_qc_prep(struct ata_queued_cmd *qc)
4469 EXPORT_SYMBOL_GPL(ata_noop_qc_prep);
4472 * ata_sg_init - Associate command with scatter-gather table.
4473 * @qc: Command to be associated
4474 * @sg: Scatter-gather table.
4475 * @n_elem: Number of elements in s/g table.
4477 * Initialize the data-related elements of queued_cmd @qc
4478 * to point to a scatter-gather table @sg, containing @n_elem
4482 * spin_lock_irqsave(host lock)
4484 void ata_sg_init(struct ata_queued_cmd *qc, struct scatterlist *sg,
4485 unsigned int n_elem)
4488 qc->n_elem = n_elem;
4492 #ifdef CONFIG_HAS_DMA
4495 * ata_sg_clean - Unmap DMA memory associated with command
4496 * @qc: Command containing DMA memory to be released
4498 * Unmap all mapped DMA memory associated with this command.
4501 * spin_lock_irqsave(host lock)
4503 static void ata_sg_clean(struct ata_queued_cmd *qc)
4505 struct ata_port *ap = qc->ap;
4506 struct scatterlist *sg = qc->sg;
4507 int dir = qc->dma_dir;
4509 WARN_ON_ONCE(sg == NULL);
4512 dma_unmap_sg(ap->dev, sg, qc->orig_n_elem, dir);
4514 qc->flags &= ~ATA_QCFLAG_DMAMAP;
4519 * ata_sg_setup - DMA-map the scatter-gather table associated with a command.
4520 * @qc: Command with scatter-gather table to be mapped.
4522 * DMA-map the scatter-gather table associated with queued_cmd @qc.
4525 * spin_lock_irqsave(host lock)
4528 * Zero on success, negative on error.
4531 static int ata_sg_setup(struct ata_queued_cmd *qc)
4533 struct ata_port *ap = qc->ap;
4534 unsigned int n_elem;
4536 n_elem = dma_map_sg(ap->dev, qc->sg, qc->n_elem, qc->dma_dir);
4540 qc->orig_n_elem = qc->n_elem;
4541 qc->n_elem = n_elem;
4542 qc->flags |= ATA_QCFLAG_DMAMAP;
4547 #else /* !CONFIG_HAS_DMA */
4549 static inline void ata_sg_clean(struct ata_queued_cmd *qc) {}
4550 static inline int ata_sg_setup(struct ata_queued_cmd *qc) { return -1; }
4552 #endif /* !CONFIG_HAS_DMA */
4555 * swap_buf_le16 - swap halves of 16-bit words in place
4556 * @buf: Buffer to swap
4557 * @buf_words: Number of 16-bit words in buffer.
4559 * Swap halves of 16-bit words if needed to convert from
4560 * little-endian byte order to native cpu byte order, or
4564 * Inherited from caller.
4566 void swap_buf_le16(u16 *buf, unsigned int buf_words)
4571 for (i = 0; i < buf_words; i++)
4572 buf[i] = le16_to_cpu(buf[i]);
4573 #endif /* __BIG_ENDIAN */
4577 * ata_qc_free - free unused ata_queued_cmd
4578 * @qc: Command to complete
4580 * Designed to free unused ata_queued_cmd object
4581 * in case something prevents using it.
4584 * spin_lock_irqsave(host lock)
4586 void ata_qc_free(struct ata_queued_cmd *qc)
4589 if (ata_tag_valid(qc->tag))
4590 qc->tag = ATA_TAG_POISON;
4593 void __ata_qc_complete(struct ata_queued_cmd *qc)
4595 struct ata_port *ap;
4596 struct ata_link *link;
4598 WARN_ON_ONCE(qc == NULL); /* ata_qc_from_tag _might_ return NULL */
4599 WARN_ON_ONCE(!(qc->flags & ATA_QCFLAG_ACTIVE));
4601 link = qc->dev->link;
4603 if (likely(qc->flags & ATA_QCFLAG_DMAMAP))
4606 /* command should be marked inactive atomically with qc completion */
4607 if (ata_is_ncq(qc->tf.protocol)) {
4608 link->sactive &= ~(1 << qc->hw_tag);
4610 ap->nr_active_links--;
4612 link->active_tag = ATA_TAG_POISON;
4613 ap->nr_active_links--;
4616 /* clear exclusive status */
4617 if (unlikely(qc->flags & ATA_QCFLAG_CLEAR_EXCL &&
4618 ap->excl_link == link))
4619 ap->excl_link = NULL;
4621 /* atapi: mark qc as inactive to prevent the interrupt handler
4622 * from completing the command twice later, before the error handler
4623 * is called. (when rc != 0 and atapi request sense is needed)
4625 qc->flags &= ~ATA_QCFLAG_ACTIVE;
4626 ap->qc_active &= ~(1ULL << qc->tag);
4628 /* call completion callback */
4629 qc->complete_fn(qc);
4632 static void fill_result_tf(struct ata_queued_cmd *qc)
4634 struct ata_port *ap = qc->ap;
4636 qc->result_tf.flags = qc->tf.flags;
4637 ap->ops->qc_fill_rtf(qc);
4640 static void ata_verify_xfer(struct ata_queued_cmd *qc)
4642 struct ata_device *dev = qc->dev;
4644 if (!ata_is_data(qc->tf.protocol))
4647 if ((dev->mwdma_mask || dev->udma_mask) && ata_is_pio(qc->tf.protocol))
4650 dev->flags &= ~ATA_DFLAG_DUBIOUS_XFER;
4654 * ata_qc_complete - Complete an active ATA command
4655 * @qc: Command to complete
4657 * Indicate to the mid and upper layers that an ATA command has
4658 * completed, with either an ok or not-ok status.
4660 * Refrain from calling this function multiple times when
4661 * successfully completing multiple NCQ commands.
4662 * ata_qc_complete_multiple() should be used instead, which will
4663 * properly update IRQ expect state.
4666 * spin_lock_irqsave(host lock)
4668 void ata_qc_complete(struct ata_queued_cmd *qc)
4670 struct ata_port *ap = qc->ap;
4672 /* Trigger the LED (if available) */
4673 ledtrig_disk_activity(!!(qc->tf.flags & ATA_TFLAG_WRITE));
4675 /* XXX: New EH and old EH use different mechanisms to
4676 * synchronize EH with regular execution path.
4678 * In new EH, a failed qc is marked with ATA_QCFLAG_FAILED.
4679 * Normal execution path is responsible for not accessing a
4680 * failed qc. libata core enforces the rule by returning NULL
4681 * from ata_qc_from_tag() for failed qcs.
4683 * Old EH depends on ata_qc_complete() nullifying completion
4684 * requests if ATA_QCFLAG_EH_SCHEDULED is set. Old EH does
4685 * not synchronize with interrupt handler. Only PIO task is
4688 if (ap->ops->error_handler) {
4689 struct ata_device *dev = qc->dev;
4690 struct ata_eh_info *ehi = &dev->link->eh_info;
4692 if (unlikely(qc->err_mask))
4693 qc->flags |= ATA_QCFLAG_FAILED;
4696 * Finish internal commands without any further processing
4697 * and always with the result TF filled.
4699 if (unlikely(ata_tag_internal(qc->tag))) {
4701 trace_ata_qc_complete_internal(qc);
4702 __ata_qc_complete(qc);
4707 * Non-internal qc has failed. Fill the result TF and
4710 if (unlikely(qc->flags & ATA_QCFLAG_FAILED)) {
4712 trace_ata_qc_complete_failed(qc);
4713 ata_qc_schedule_eh(qc);
4717 WARN_ON_ONCE(ap->pflags & ATA_PFLAG_FROZEN);
4719 /* read result TF if requested */
4720 if (qc->flags & ATA_QCFLAG_RESULT_TF)
4723 trace_ata_qc_complete_done(qc);
4724 /* Some commands need post-processing after successful
4727 switch (qc->tf.command) {
4728 case ATA_CMD_SET_FEATURES:
4729 if (qc->tf.feature != SETFEATURES_WC_ON &&
4730 qc->tf.feature != SETFEATURES_WC_OFF &&
4731 qc->tf.feature != SETFEATURES_RA_ON &&
4732 qc->tf.feature != SETFEATURES_RA_OFF)
4735 case ATA_CMD_INIT_DEV_PARAMS: /* CHS translation changed */
4736 case ATA_CMD_SET_MULTI: /* multi_count changed */
4737 /* revalidate device */
4738 ehi->dev_action[dev->devno] |= ATA_EH_REVALIDATE;
4739 ata_port_schedule_eh(ap);
4743 dev->flags |= ATA_DFLAG_SLEEPING;
4747 if (unlikely(dev->flags & ATA_DFLAG_DUBIOUS_XFER))
4748 ata_verify_xfer(qc);
4750 __ata_qc_complete(qc);
4752 if (qc->flags & ATA_QCFLAG_EH_SCHEDULED)
4755 /* read result TF if failed or requested */
4756 if (qc->err_mask || qc->flags & ATA_QCFLAG_RESULT_TF)
4759 __ata_qc_complete(qc);
4762 EXPORT_SYMBOL_GPL(ata_qc_complete);
4765 * ata_qc_get_active - get bitmask of active qcs
4766 * @ap: port in question
4769 * spin_lock_irqsave(host lock)
4772 * Bitmask of active qcs
4774 u64 ata_qc_get_active(struct ata_port *ap)
4776 u64 qc_active = ap->qc_active;
4778 /* ATA_TAG_INTERNAL is sent to hw as tag 0 */
4779 if (qc_active & (1ULL << ATA_TAG_INTERNAL)) {
4780 qc_active |= (1 << 0);
4781 qc_active &= ~(1ULL << ATA_TAG_INTERNAL);
4786 EXPORT_SYMBOL_GPL(ata_qc_get_active);
4789 * ata_qc_issue - issue taskfile to device
4790 * @qc: command to issue to device
4792 * Prepare an ATA command to submission to device.
4793 * This includes mapping the data into a DMA-able
4794 * area, filling in the S/G table, and finally
4795 * writing the taskfile to hardware, starting the command.
4798 * spin_lock_irqsave(host lock)
4800 void ata_qc_issue(struct ata_queued_cmd *qc)
4802 struct ata_port *ap = qc->ap;
4803 struct ata_link *link = qc->dev->link;
4804 u8 prot = qc->tf.protocol;
4806 /* Make sure only one non-NCQ command is outstanding. The
4807 * check is skipped for old EH because it reuses active qc to
4808 * request ATAPI sense.
4810 WARN_ON_ONCE(ap->ops->error_handler && ata_tag_valid(link->active_tag));
4812 if (ata_is_ncq(prot)) {
4813 WARN_ON_ONCE(link->sactive & (1 << qc->hw_tag));
4816 ap->nr_active_links++;
4817 link->sactive |= 1 << qc->hw_tag;
4819 WARN_ON_ONCE(link->sactive);
4821 ap->nr_active_links++;
4822 link->active_tag = qc->tag;
4825 qc->flags |= ATA_QCFLAG_ACTIVE;
4826 ap->qc_active |= 1ULL << qc->tag;
4829 * We guarantee to LLDs that they will have at least one
4830 * non-zero sg if the command is a data command.
4832 if (ata_is_data(prot) && (!qc->sg || !qc->n_elem || !qc->nbytes))
4835 if (ata_is_dma(prot) || (ata_is_pio(prot) &&
4836 (ap->flags & ATA_FLAG_PIO_DMA)))
4837 if (ata_sg_setup(qc))
4840 /* if device is sleeping, schedule reset and abort the link */
4841 if (unlikely(qc->dev->flags & ATA_DFLAG_SLEEPING)) {
4842 link->eh_info.action |= ATA_EH_RESET;
4843 ata_ehi_push_desc(&link->eh_info, "waking up from sleep");
4844 ata_link_abort(link);
4848 trace_ata_qc_prep(qc);
4849 qc->err_mask |= ap->ops->qc_prep(qc);
4850 if (unlikely(qc->err_mask))
4852 trace_ata_qc_issue(qc);
4853 qc->err_mask |= ap->ops->qc_issue(qc);
4854 if (unlikely(qc->err_mask))
4859 qc->err_mask |= AC_ERR_SYSTEM;
4861 ata_qc_complete(qc);
4865 * ata_phys_link_online - test whether the given link is online
4866 * @link: ATA link to test
4868 * Test whether @link is online. Note that this function returns
4869 * 0 if online status of @link cannot be obtained, so
4870 * ata_link_online(link) != !ata_link_offline(link).
4876 * True if the port online status is available and online.
4878 bool ata_phys_link_online(struct ata_link *link)
4882 if (sata_scr_read(link, SCR_STATUS, &sstatus) == 0 &&
4883 ata_sstatus_online(sstatus))
4889 * ata_phys_link_offline - test whether the given link is offline
4890 * @link: ATA link to test
4892 * Test whether @link is offline. Note that this function
4893 * returns 0 if offline status of @link cannot be obtained, so
4894 * ata_link_online(link) != !ata_link_offline(link).
4900 * True if the port offline status is available and offline.
4902 bool ata_phys_link_offline(struct ata_link *link)
4906 if (sata_scr_read(link, SCR_STATUS, &sstatus) == 0 &&
4907 !ata_sstatus_online(sstatus))
4913 * ata_link_online - test whether the given link is online
4914 * @link: ATA link to test
4916 * Test whether @link is online. This is identical to
4917 * ata_phys_link_online() when there's no slave link. When
4918 * there's a slave link, this function should only be called on
4919 * the master link and will return true if any of M/S links is
4926 * True if the port online status is available and online.
4928 bool ata_link_online(struct ata_link *link)
4930 struct ata_link *slave = link->ap->slave_link;
4932 WARN_ON(link == slave); /* shouldn't be called on slave link */
4934 return ata_phys_link_online(link) ||
4935 (slave && ata_phys_link_online(slave));
4937 EXPORT_SYMBOL_GPL(ata_link_online);
4940 * ata_link_offline - test whether the given link is offline
4941 * @link: ATA link to test
4943 * Test whether @link is offline. This is identical to
4944 * ata_phys_link_offline() when there's no slave link. When
4945 * there's a slave link, this function should only be called on
4946 * the master link and will return true if both M/S links are
4953 * True if the port offline status is available and offline.
4955 bool ata_link_offline(struct ata_link *link)
4957 struct ata_link *slave = link->ap->slave_link;
4959 WARN_ON(link == slave); /* shouldn't be called on slave link */
4961 return ata_phys_link_offline(link) &&
4962 (!slave || ata_phys_link_offline(slave));
4964 EXPORT_SYMBOL_GPL(ata_link_offline);
4967 static void ata_port_request_pm(struct ata_port *ap, pm_message_t mesg,
4968 unsigned int action, unsigned int ehi_flags,
4971 struct ata_link *link;
4972 unsigned long flags;
4974 /* Previous resume operation might still be in
4975 * progress. Wait for PM_PENDING to clear.
4977 if (ap->pflags & ATA_PFLAG_PM_PENDING) {
4978 ata_port_wait_eh(ap);
4979 WARN_ON(ap->pflags & ATA_PFLAG_PM_PENDING);
4982 /* request PM ops to EH */
4983 spin_lock_irqsave(ap->lock, flags);
4986 ap->pflags |= ATA_PFLAG_PM_PENDING;
4987 ata_for_each_link(link, ap, HOST_FIRST) {
4988 link->eh_info.action |= action;
4989 link->eh_info.flags |= ehi_flags;
4992 ata_port_schedule_eh(ap);
4994 spin_unlock_irqrestore(ap->lock, flags);
4997 ata_port_wait_eh(ap);
4998 WARN_ON(ap->pflags & ATA_PFLAG_PM_PENDING);
5003 * On some hardware, device fails to respond after spun down for suspend. As
5004 * the device won't be used before being resumed, we don't need to touch the
5005 * device. Ask EH to skip the usual stuff and proceed directly to suspend.
5007 * http://thread.gmane.org/gmane.linux.ide/46764
5009 static const unsigned int ata_port_suspend_ehi = ATA_EHI_QUIET
5010 | ATA_EHI_NO_AUTOPSY
5011 | ATA_EHI_NO_RECOVERY;
5013 static void ata_port_suspend(struct ata_port *ap, pm_message_t mesg)
5015 ata_port_request_pm(ap, mesg, 0, ata_port_suspend_ehi, false);
5018 static void ata_port_suspend_async(struct ata_port *ap, pm_message_t mesg)
5020 ata_port_request_pm(ap, mesg, 0, ata_port_suspend_ehi, true);
5023 static int ata_port_pm_suspend(struct device *dev)
5025 struct ata_port *ap = to_ata_port(dev);
5027 if (pm_runtime_suspended(dev))
5030 ata_port_suspend(ap, PMSG_SUSPEND);
5034 static int ata_port_pm_freeze(struct device *dev)
5036 struct ata_port *ap = to_ata_port(dev);
5038 if (pm_runtime_suspended(dev))
5041 ata_port_suspend(ap, PMSG_FREEZE);
5045 static int ata_port_pm_poweroff(struct device *dev)
5047 ata_port_suspend(to_ata_port(dev), PMSG_HIBERNATE);
5051 static const unsigned int ata_port_resume_ehi = ATA_EHI_NO_AUTOPSY
5054 static void ata_port_resume(struct ata_port *ap, pm_message_t mesg)
5056 ata_port_request_pm(ap, mesg, ATA_EH_RESET, ata_port_resume_ehi, false);
5059 static void ata_port_resume_async(struct ata_port *ap, pm_message_t mesg)
5061 ata_port_request_pm(ap, mesg, ATA_EH_RESET, ata_port_resume_ehi, true);
5064 static int ata_port_pm_resume(struct device *dev)
5066 ata_port_resume_async(to_ata_port(dev), PMSG_RESUME);
5067 pm_runtime_disable(dev);
5068 pm_runtime_set_active(dev);
5069 pm_runtime_enable(dev);
5074 * For ODDs, the upper layer will poll for media change every few seconds,
5075 * which will make it enter and leave suspend state every few seconds. And
5076 * as each suspend will cause a hard/soft reset, the gain of runtime suspend
5077 * is very little and the ODD may malfunction after constantly being reset.
5078 * So the idle callback here will not proceed to suspend if a non-ZPODD capable
5079 * ODD is attached to the port.
5081 static int ata_port_runtime_idle(struct device *dev)
5083 struct ata_port *ap = to_ata_port(dev);
5084 struct ata_link *link;
5085 struct ata_device *adev;
5087 ata_for_each_link(link, ap, HOST_FIRST) {
5088 ata_for_each_dev(adev, link, ENABLED)
5089 if (adev->class == ATA_DEV_ATAPI &&
5090 !zpodd_dev_enabled(adev))
5097 static int ata_port_runtime_suspend(struct device *dev)
5099 ata_port_suspend(to_ata_port(dev), PMSG_AUTO_SUSPEND);
5103 static int ata_port_runtime_resume(struct device *dev)
5105 ata_port_resume(to_ata_port(dev), PMSG_AUTO_RESUME);
5109 static const struct dev_pm_ops ata_port_pm_ops = {
5110 .suspend = ata_port_pm_suspend,
5111 .resume = ata_port_pm_resume,
5112 .freeze = ata_port_pm_freeze,
5113 .thaw = ata_port_pm_resume,
5114 .poweroff = ata_port_pm_poweroff,
5115 .restore = ata_port_pm_resume,
5117 .runtime_suspend = ata_port_runtime_suspend,
5118 .runtime_resume = ata_port_runtime_resume,
5119 .runtime_idle = ata_port_runtime_idle,
5122 /* sas ports don't participate in pm runtime management of ata_ports,
5123 * and need to resume ata devices at the domain level, not the per-port
5124 * level. sas suspend/resume is async to allow parallel port recovery
5125 * since sas has multiple ata_port instances per Scsi_Host.
5127 void ata_sas_port_suspend(struct ata_port *ap)
5129 ata_port_suspend_async(ap, PMSG_SUSPEND);
5131 EXPORT_SYMBOL_GPL(ata_sas_port_suspend);
5133 void ata_sas_port_resume(struct ata_port *ap)
5135 ata_port_resume_async(ap, PMSG_RESUME);
5137 EXPORT_SYMBOL_GPL(ata_sas_port_resume);
5140 * ata_host_suspend - suspend host
5141 * @host: host to suspend
5144 * Suspend @host. Actual operation is performed by port suspend.
5146 void ata_host_suspend(struct ata_host *host, pm_message_t mesg)
5148 host->dev->power.power_state = mesg;
5150 EXPORT_SYMBOL_GPL(ata_host_suspend);
5153 * ata_host_resume - resume host
5154 * @host: host to resume
5156 * Resume @host. Actual operation is performed by port resume.
5158 void ata_host_resume(struct ata_host *host)
5160 host->dev->power.power_state = PMSG_ON;
5162 EXPORT_SYMBOL_GPL(ata_host_resume);
5165 const struct device_type ata_port_type = {
5168 .pm = &ata_port_pm_ops,
5173 * ata_dev_init - Initialize an ata_device structure
5174 * @dev: Device structure to initialize
5176 * Initialize @dev in preparation for probing.
5179 * Inherited from caller.
5181 void ata_dev_init(struct ata_device *dev)
5183 struct ata_link *link = ata_dev_phys_link(dev);
5184 struct ata_port *ap = link->ap;
5185 unsigned long flags;
5187 /* SATA spd limit is bound to the attached device, reset together */
5188 link->sata_spd_limit = link->hw_sata_spd_limit;
5191 /* High bits of dev->flags are used to record warm plug
5192 * requests which occur asynchronously. Synchronize using
5195 spin_lock_irqsave(ap->lock, flags);
5196 dev->flags &= ~ATA_DFLAG_INIT_MASK;
5198 spin_unlock_irqrestore(ap->lock, flags);
5200 memset((void *)dev + ATA_DEVICE_CLEAR_BEGIN, 0,
5201 ATA_DEVICE_CLEAR_END - ATA_DEVICE_CLEAR_BEGIN);
5202 dev->pio_mask = UINT_MAX;
5203 dev->mwdma_mask = UINT_MAX;
5204 dev->udma_mask = UINT_MAX;
5208 * ata_link_init - Initialize an ata_link structure
5209 * @ap: ATA port link is attached to
5210 * @link: Link structure to initialize
5211 * @pmp: Port multiplier port number
5216 * Kernel thread context (may sleep)
5218 void ata_link_init(struct ata_port *ap, struct ata_link *link, int pmp)
5222 /* clear everything except for devices */
5223 memset((void *)link + ATA_LINK_CLEAR_BEGIN, 0,
5224 ATA_LINK_CLEAR_END - ATA_LINK_CLEAR_BEGIN);
5228 link->active_tag = ATA_TAG_POISON;
5229 link->hw_sata_spd_limit = UINT_MAX;
5231 /* can't use iterator, ap isn't initialized yet */
5232 for (i = 0; i < ATA_MAX_DEVICES; i++) {
5233 struct ata_device *dev = &link->device[i];
5236 dev->devno = dev - link->device;
5237 #ifdef CONFIG_ATA_ACPI
5238 dev->gtf_filter = ata_acpi_gtf_filter;
5245 * sata_link_init_spd - Initialize link->sata_spd_limit
5246 * @link: Link to configure sata_spd_limit for
5248 * Initialize ``link->[hw_]sata_spd_limit`` to the currently
5252 * Kernel thread context (may sleep).
5255 * 0 on success, -errno on failure.
5257 int sata_link_init_spd(struct ata_link *link)
5262 rc = sata_scr_read(link, SCR_CONTROL, &link->saved_scontrol);
5266 spd = (link->saved_scontrol >> 4) & 0xf;
5268 link->hw_sata_spd_limit &= (1 << spd) - 1;
5270 ata_force_link_limits(link);
5272 link->sata_spd_limit = link->hw_sata_spd_limit;
5278 * ata_port_alloc - allocate and initialize basic ATA port resources
5279 * @host: ATA host this allocated port belongs to
5281 * Allocate and initialize basic ATA port resources.
5284 * Allocate ATA port on success, NULL on failure.
5287 * Inherited from calling layer (may sleep).
5289 struct ata_port *ata_port_alloc(struct ata_host *host)
5291 struct ata_port *ap;
5293 ap = kzalloc(sizeof(*ap), GFP_KERNEL);
5297 ap->pflags |= ATA_PFLAG_INITIALIZING | ATA_PFLAG_FROZEN;
5298 ap->lock = &host->lock;
5300 ap->local_port_no = -1;
5302 ap->dev = host->dev;
5304 mutex_init(&ap->scsi_scan_mutex);
5305 INIT_DELAYED_WORK(&ap->hotplug_task, ata_scsi_hotplug);
5306 INIT_WORK(&ap->scsi_rescan_task, ata_scsi_dev_rescan);
5307 INIT_LIST_HEAD(&ap->eh_done_q);
5308 init_waitqueue_head(&ap->eh_wait_q);
5309 init_completion(&ap->park_req_pending);
5310 timer_setup(&ap->fastdrain_timer, ata_eh_fastdrain_timerfn,
5313 ap->cbl = ATA_CBL_NONE;
5315 ata_link_init(ap, &ap->link, 0);
5318 ap->stats.unhandled_irq = 1;
5319 ap->stats.idle_irq = 1;
5321 ata_sff_port_init(ap);
5326 static void ata_devres_release(struct device *gendev, void *res)
5328 struct ata_host *host = dev_get_drvdata(gendev);
5331 for (i = 0; i < host->n_ports; i++) {
5332 struct ata_port *ap = host->ports[i];
5338 scsi_host_put(ap->scsi_host);
5342 dev_set_drvdata(gendev, NULL);
5346 static void ata_host_release(struct kref *kref)
5348 struct ata_host *host = container_of(kref, struct ata_host, kref);
5351 for (i = 0; i < host->n_ports; i++) {
5352 struct ata_port *ap = host->ports[i];
5354 kfree(ap->pmp_link);
5355 kfree(ap->slave_link);
5357 host->ports[i] = NULL;
5362 void ata_host_get(struct ata_host *host)
5364 kref_get(&host->kref);
5367 void ata_host_put(struct ata_host *host)
5369 kref_put(&host->kref, ata_host_release);
5371 EXPORT_SYMBOL_GPL(ata_host_put);
5374 * ata_host_alloc - allocate and init basic ATA host resources
5375 * @dev: generic device this host is associated with
5376 * @max_ports: maximum number of ATA ports associated with this host
5378 * Allocate and initialize basic ATA host resources. LLD calls
5379 * this function to allocate a host, initializes it fully and
5380 * attaches it using ata_host_register().
5382 * @max_ports ports are allocated and host->n_ports is
5383 * initialized to @max_ports. The caller is allowed to decrease
5384 * host->n_ports before calling ata_host_register(). The unused
5385 * ports will be automatically freed on registration.
5388 * Allocate ATA host on success, NULL on failure.
5391 * Inherited from calling layer (may sleep).
5393 struct ata_host *ata_host_alloc(struct device *dev, int max_ports)
5395 struct ata_host *host;
5400 /* alloc a container for our list of ATA ports (buses) */
5401 sz = sizeof(struct ata_host) + (max_ports + 1) * sizeof(void *);
5402 host = kzalloc(sz, GFP_KERNEL);
5406 if (!devres_open_group(dev, NULL, GFP_KERNEL))
5409 dr = devres_alloc(ata_devres_release, 0, GFP_KERNEL);
5413 devres_add(dev, dr);
5414 dev_set_drvdata(dev, host);
5416 spin_lock_init(&host->lock);
5417 mutex_init(&host->eh_mutex);
5419 host->n_ports = max_ports;
5420 kref_init(&host->kref);
5422 /* allocate ports bound to this host */
5423 for (i = 0; i < max_ports; i++) {
5424 struct ata_port *ap;
5426 ap = ata_port_alloc(host);
5431 host->ports[i] = ap;
5434 devres_remove_group(dev, NULL);
5438 devres_release_group(dev, NULL);
5443 EXPORT_SYMBOL_GPL(ata_host_alloc);
5446 * ata_host_alloc_pinfo - alloc host and init with port_info array
5447 * @dev: generic device this host is associated with
5448 * @ppi: array of ATA port_info to initialize host with
5449 * @n_ports: number of ATA ports attached to this host
5451 * Allocate ATA host and initialize with info from @ppi. If NULL
5452 * terminated, @ppi may contain fewer entries than @n_ports. The
5453 * last entry will be used for the remaining ports.
5456 * Allocate ATA host on success, NULL on failure.
5459 * Inherited from calling layer (may sleep).
5461 struct ata_host *ata_host_alloc_pinfo(struct device *dev,
5462 const struct ata_port_info * const * ppi,
5465 const struct ata_port_info *pi;
5466 struct ata_host *host;
5469 host = ata_host_alloc(dev, n_ports);
5473 for (i = 0, j = 0, pi = NULL; i < host->n_ports; i++) {
5474 struct ata_port *ap = host->ports[i];
5479 ap->pio_mask = pi->pio_mask;
5480 ap->mwdma_mask = pi->mwdma_mask;
5481 ap->udma_mask = pi->udma_mask;
5482 ap->flags |= pi->flags;
5483 ap->link.flags |= pi->link_flags;
5484 ap->ops = pi->port_ops;
5486 if (!host->ops && (pi->port_ops != &ata_dummy_port_ops))
5487 host->ops = pi->port_ops;
5492 EXPORT_SYMBOL_GPL(ata_host_alloc_pinfo);
5494 static void ata_host_stop(struct device *gendev, void *res)
5496 struct ata_host *host = dev_get_drvdata(gendev);
5499 WARN_ON(!(host->flags & ATA_HOST_STARTED));
5501 for (i = 0; i < host->n_ports; i++) {
5502 struct ata_port *ap = host->ports[i];
5504 if (ap->ops->port_stop)
5505 ap->ops->port_stop(ap);
5508 if (host->ops->host_stop)
5509 host->ops->host_stop(host);
5513 * ata_finalize_port_ops - finalize ata_port_operations
5514 * @ops: ata_port_operations to finalize
5516 * An ata_port_operations can inherit from another ops and that
5517 * ops can again inherit from another. This can go on as many
5518 * times as necessary as long as there is no loop in the
5519 * inheritance chain.
5521 * Ops tables are finalized when the host is started. NULL or
5522 * unspecified entries are inherited from the closet ancestor
5523 * which has the method and the entry is populated with it.
5524 * After finalization, the ops table directly points to all the
5525 * methods and ->inherits is no longer necessary and cleared.
5527 * Using ATA_OP_NULL, inheriting ops can force a method to NULL.
5532 static void ata_finalize_port_ops(struct ata_port_operations *ops)
5534 static DEFINE_SPINLOCK(lock);
5535 const struct ata_port_operations *cur;
5536 void **begin = (void **)ops;
5537 void **end = (void **)&ops->inherits;
5540 if (!ops || !ops->inherits)
5545 for (cur = ops->inherits; cur; cur = cur->inherits) {
5546 void **inherit = (void **)cur;
5548 for (pp = begin; pp < end; pp++, inherit++)
5553 for (pp = begin; pp < end; pp++)
5557 ops->inherits = NULL;
5563 * ata_host_start - start and freeze ports of an ATA host
5564 * @host: ATA host to start ports for
5566 * Start and then freeze ports of @host. Started status is
5567 * recorded in host->flags, so this function can be called
5568 * multiple times. Ports are guaranteed to get started only
5569 * once. If host->ops is not initialized yet, it is set to the
5570 * first non-dummy port ops.
5573 * Inherited from calling layer (may sleep).
5576 * 0 if all ports are started successfully, -errno otherwise.
5578 int ata_host_start(struct ata_host *host)
5581 void *start_dr = NULL;
5584 if (host->flags & ATA_HOST_STARTED)
5587 ata_finalize_port_ops(host->ops);
5589 for (i = 0; i < host->n_ports; i++) {
5590 struct ata_port *ap = host->ports[i];
5592 ata_finalize_port_ops(ap->ops);
5594 if (!host->ops && !ata_port_is_dummy(ap))
5595 host->ops = ap->ops;
5597 if (ap->ops->port_stop)
5601 if (host->ops && host->ops->host_stop)
5605 start_dr = devres_alloc(ata_host_stop, 0, GFP_KERNEL);
5610 for (i = 0; i < host->n_ports; i++) {
5611 struct ata_port *ap = host->ports[i];
5613 if (ap->ops->port_start) {
5614 rc = ap->ops->port_start(ap);
5618 "failed to start port %d (errno=%d)\n",
5623 ata_eh_freeze_port(ap);
5627 devres_add(host->dev, start_dr);
5628 host->flags |= ATA_HOST_STARTED;
5633 struct ata_port *ap = host->ports[i];
5635 if (ap->ops->port_stop)
5636 ap->ops->port_stop(ap);
5638 devres_free(start_dr);
5641 EXPORT_SYMBOL_GPL(ata_host_start);
5644 * ata_host_init - Initialize a host struct for sas (ipr, libsas)
5645 * @host: host to initialize
5646 * @dev: device host is attached to
5650 void ata_host_init(struct ata_host *host, struct device *dev,
5651 struct ata_port_operations *ops)
5653 spin_lock_init(&host->lock);
5654 mutex_init(&host->eh_mutex);
5655 host->n_tags = ATA_MAX_QUEUE;
5658 kref_init(&host->kref);
5660 EXPORT_SYMBOL_GPL(ata_host_init);
5662 void __ata_port_probe(struct ata_port *ap)
5664 struct ata_eh_info *ehi = &ap->link.eh_info;
5665 unsigned long flags;
5667 /* kick EH for boot probing */
5668 spin_lock_irqsave(ap->lock, flags);
5670 ehi->probe_mask |= ATA_ALL_DEVICES;
5671 ehi->action |= ATA_EH_RESET;
5672 ehi->flags |= ATA_EHI_NO_AUTOPSY | ATA_EHI_QUIET;
5674 ap->pflags &= ~ATA_PFLAG_INITIALIZING;
5675 ap->pflags |= ATA_PFLAG_LOADING;
5676 ata_port_schedule_eh(ap);
5678 spin_unlock_irqrestore(ap->lock, flags);
5681 int ata_port_probe(struct ata_port *ap)
5685 if (ap->ops->error_handler) {
5686 __ata_port_probe(ap);
5687 ata_port_wait_eh(ap);
5689 rc = ata_bus_probe(ap);
5695 static void async_port_probe(void *data, async_cookie_t cookie)
5697 struct ata_port *ap = data;
5700 * If we're not allowed to scan this host in parallel,
5701 * we need to wait until all previous scans have completed
5702 * before going further.
5703 * Jeff Garzik says this is only within a controller, so we
5704 * don't need to wait for port 0, only for later ports.
5706 if (!(ap->host->flags & ATA_HOST_PARALLEL_SCAN) && ap->port_no != 0)
5707 async_synchronize_cookie(cookie);
5709 (void)ata_port_probe(ap);
5711 /* in order to keep device order, we need to synchronize at this point */
5712 async_synchronize_cookie(cookie);
5714 ata_scsi_scan_host(ap, 1);
5718 * ata_host_register - register initialized ATA host
5719 * @host: ATA host to register
5720 * @sht: template for SCSI host
5722 * Register initialized ATA host. @host is allocated using
5723 * ata_host_alloc() and fully initialized by LLD. This function
5724 * starts ports, registers @host with ATA and SCSI layers and
5725 * probe registered devices.
5728 * Inherited from calling layer (may sleep).
5731 * 0 on success, -errno otherwise.
5733 int ata_host_register(struct ata_host *host, struct scsi_host_template *sht)
5737 host->n_tags = clamp(sht->can_queue, 1, ATA_MAX_QUEUE);
5739 /* host must have been started */
5740 if (!(host->flags & ATA_HOST_STARTED)) {
5741 dev_err(host->dev, "BUG: trying to register unstarted host\n");
5746 /* Blow away unused ports. This happens when LLD can't
5747 * determine the exact number of ports to allocate at
5750 for (i = host->n_ports; host->ports[i]; i++)
5751 kfree(host->ports[i]);
5753 /* give ports names and add SCSI hosts */
5754 for (i = 0; i < host->n_ports; i++) {
5755 host->ports[i]->print_id = atomic_inc_return(&ata_print_id);
5756 host->ports[i]->local_port_no = i + 1;
5759 /* Create associated sysfs transport objects */
5760 for (i = 0; i < host->n_ports; i++) {
5761 rc = ata_tport_add(host->dev,host->ports[i]);
5767 rc = ata_scsi_add_hosts(host, sht);
5771 /* set cable, sata_spd_limit and report */
5772 for (i = 0; i < host->n_ports; i++) {
5773 struct ata_port *ap = host->ports[i];
5774 unsigned long xfer_mask;
5776 /* set SATA cable type if still unset */
5777 if (ap->cbl == ATA_CBL_NONE && (ap->flags & ATA_FLAG_SATA))
5778 ap->cbl = ATA_CBL_SATA;
5780 /* init sata_spd_limit to the current value */
5781 sata_link_init_spd(&ap->link);
5783 sata_link_init_spd(ap->slave_link);
5785 /* print per-port info to dmesg */
5786 xfer_mask = ata_pack_xfermask(ap->pio_mask, ap->mwdma_mask,
5789 if (!ata_port_is_dummy(ap)) {
5790 ata_port_info(ap, "%cATA max %s %s\n",
5791 (ap->flags & ATA_FLAG_SATA) ? 'S' : 'P',
5792 ata_mode_string(xfer_mask),
5793 ap->link.eh_info.desc);
5794 ata_ehi_clear_desc(&ap->link.eh_info);
5796 ata_port_info(ap, "DUMMY\n");
5799 /* perform each probe asynchronously */
5800 for (i = 0; i < host->n_ports; i++) {
5801 struct ata_port *ap = host->ports[i];
5802 ap->cookie = async_schedule(async_port_probe, ap);
5809 ata_tport_delete(host->ports[i]);
5814 EXPORT_SYMBOL_GPL(ata_host_register);
5817 * ata_host_activate - start host, request IRQ and register it
5818 * @host: target ATA host
5819 * @irq: IRQ to request
5820 * @irq_handler: irq_handler used when requesting IRQ
5821 * @irq_flags: irq_flags used when requesting IRQ
5822 * @sht: scsi_host_template to use when registering the host
5824 * After allocating an ATA host and initializing it, most libata
5825 * LLDs perform three steps to activate the host - start host,
5826 * request IRQ and register it. This helper takes necessary
5827 * arguments and performs the three steps in one go.
5829 * An invalid IRQ skips the IRQ registration and expects the host to
5830 * have set polling mode on the port. In this case, @irq_handler
5834 * Inherited from calling layer (may sleep).
5837 * 0 on success, -errno otherwise.
5839 int ata_host_activate(struct ata_host *host, int irq,
5840 irq_handler_t irq_handler, unsigned long irq_flags,
5841 struct scsi_host_template *sht)
5846 rc = ata_host_start(host);
5850 /* Special case for polling mode */
5852 WARN_ON(irq_handler);
5853 return ata_host_register(host, sht);
5856 irq_desc = devm_kasprintf(host->dev, GFP_KERNEL, "%s[%s]",
5857 dev_driver_string(host->dev),
5858 dev_name(host->dev));
5862 rc = devm_request_irq(host->dev, irq, irq_handler, irq_flags,
5867 for (i = 0; i < host->n_ports; i++)
5868 ata_port_desc(host->ports[i], "irq %d", irq);
5870 rc = ata_host_register(host, sht);
5871 /* if failed, just free the IRQ and leave ports alone */
5873 devm_free_irq(host->dev, irq, host);
5877 EXPORT_SYMBOL_GPL(ata_host_activate);
5880 * ata_port_detach - Detach ATA port in preparation of device removal
5881 * @ap: ATA port to be detached
5883 * Detach all ATA devices and the associated SCSI devices of @ap;
5884 * then, remove the associated SCSI host. @ap is guaranteed to
5885 * be quiescent on return from this function.
5888 * Kernel thread context (may sleep).
5890 static void ata_port_detach(struct ata_port *ap)
5892 unsigned long flags;
5893 struct ata_link *link;
5894 struct ata_device *dev;
5896 if (!ap->ops->error_handler)
5899 /* tell EH we're leaving & flush EH */
5900 spin_lock_irqsave(ap->lock, flags);
5901 ap->pflags |= ATA_PFLAG_UNLOADING;
5902 ata_port_schedule_eh(ap);
5903 spin_unlock_irqrestore(ap->lock, flags);
5905 /* wait till EH commits suicide */
5906 ata_port_wait_eh(ap);
5908 /* it better be dead now */
5909 WARN_ON(!(ap->pflags & ATA_PFLAG_UNLOADED));
5911 cancel_delayed_work_sync(&ap->hotplug_task);
5914 /* clean up zpodd on port removal */
5915 ata_for_each_link(link, ap, HOST_FIRST) {
5916 ata_for_each_dev(dev, link, ALL) {
5917 if (zpodd_dev_enabled(dev))
5923 for (i = 0; i < SATA_PMP_MAX_PORTS; i++)
5924 ata_tlink_delete(&ap->pmp_link[i]);
5926 /* remove the associated SCSI host */
5927 scsi_remove_host(ap->scsi_host);
5928 ata_tport_delete(ap);
5932 * ata_host_detach - Detach all ports of an ATA host
5933 * @host: Host to detach
5935 * Detach all ports of @host.
5938 * Kernel thread context (may sleep).
5940 void ata_host_detach(struct ata_host *host)
5944 for (i = 0; i < host->n_ports; i++) {
5945 /* Ensure ata_port probe has completed */
5946 async_synchronize_cookie(host->ports[i]->cookie + 1);
5947 ata_port_detach(host->ports[i]);
5950 /* the host is dead now, dissociate ACPI */
5951 ata_acpi_dissociate(host);
5953 EXPORT_SYMBOL_GPL(ata_host_detach);
5958 * ata_pci_remove_one - PCI layer callback for device removal
5959 * @pdev: PCI device that was removed
5961 * PCI layer indicates to libata via this hook that hot-unplug or
5962 * module unload event has occurred. Detach all ports. Resource
5963 * release is handled via devres.
5966 * Inherited from PCI layer (may sleep).
5968 void ata_pci_remove_one(struct pci_dev *pdev)
5970 struct ata_host *host = pci_get_drvdata(pdev);
5972 ata_host_detach(host);
5974 EXPORT_SYMBOL_GPL(ata_pci_remove_one);
5976 void ata_pci_shutdown_one(struct pci_dev *pdev)
5978 struct ata_host *host = pci_get_drvdata(pdev);
5981 for (i = 0; i < host->n_ports; i++) {
5982 struct ata_port *ap = host->ports[i];
5984 ap->pflags |= ATA_PFLAG_FROZEN;
5986 /* Disable port interrupts */
5987 if (ap->ops->freeze)
5988 ap->ops->freeze(ap);
5990 /* Stop the port DMA engines */
5991 if (ap->ops->port_stop)
5992 ap->ops->port_stop(ap);
5995 EXPORT_SYMBOL_GPL(ata_pci_shutdown_one);
5997 /* move to PCI subsystem */
5998 int pci_test_config_bits(struct pci_dev *pdev, const struct pci_bits *bits)
6000 unsigned long tmp = 0;
6002 switch (bits->width) {
6005 pci_read_config_byte(pdev, bits->reg, &tmp8);
6011 pci_read_config_word(pdev, bits->reg, &tmp16);
6017 pci_read_config_dword(pdev, bits->reg, &tmp32);
6028 return (tmp == bits->val) ? 1 : 0;
6030 EXPORT_SYMBOL_GPL(pci_test_config_bits);
6033 void ata_pci_device_do_suspend(struct pci_dev *pdev, pm_message_t mesg)
6035 pci_save_state(pdev);
6036 pci_disable_device(pdev);
6038 if (mesg.event & PM_EVENT_SLEEP)
6039 pci_set_power_state(pdev, PCI_D3hot);
6041 EXPORT_SYMBOL_GPL(ata_pci_device_do_suspend);
6043 int ata_pci_device_do_resume(struct pci_dev *pdev)
6047 pci_set_power_state(pdev, PCI_D0);
6048 pci_restore_state(pdev);
6050 rc = pcim_enable_device(pdev);
6053 "failed to enable device after resume (%d)\n", rc);
6057 pci_set_master(pdev);
6060 EXPORT_SYMBOL_GPL(ata_pci_device_do_resume);
6062 int ata_pci_device_suspend(struct pci_dev *pdev, pm_message_t mesg)
6064 struct ata_host *host = pci_get_drvdata(pdev);
6066 ata_host_suspend(host, mesg);
6068 ata_pci_device_do_suspend(pdev, mesg);
6072 EXPORT_SYMBOL_GPL(ata_pci_device_suspend);
6074 int ata_pci_device_resume(struct pci_dev *pdev)
6076 struct ata_host *host = pci_get_drvdata(pdev);
6079 rc = ata_pci_device_do_resume(pdev);
6081 ata_host_resume(host);
6084 EXPORT_SYMBOL_GPL(ata_pci_device_resume);
6085 #endif /* CONFIG_PM */
6086 #endif /* CONFIG_PCI */
6089 * ata_platform_remove_one - Platform layer callback for device removal
6090 * @pdev: Platform device that was removed
6092 * Platform layer indicates to libata via this hook that hot-unplug or
6093 * module unload event has occurred. Detach all ports. Resource
6094 * release is handled via devres.
6097 * Inherited from platform layer (may sleep).
6099 int ata_platform_remove_one(struct platform_device *pdev)
6101 struct ata_host *host = platform_get_drvdata(pdev);
6103 ata_host_detach(host);
6107 EXPORT_SYMBOL_GPL(ata_platform_remove_one);
6109 #ifdef CONFIG_ATA_FORCE
6111 #define force_cbl(name, flag) \
6112 { #name, .cbl = (flag) }
6114 #define force_spd_limit(spd, val) \
6115 { #spd, .spd_limit = (val) }
6117 #define force_xfer(mode, shift) \
6118 { #mode, .xfer_mask = (1UL << (shift)) }
6120 #define force_lflag_on(name, flags) \
6121 { #name, .lflags_on = (flags) }
6123 #define force_lflag_onoff(name, flags) \
6124 { "no" #name, .lflags_on = (flags) }, \
6125 { #name, .lflags_off = (flags) }
6127 #define force_horkage_on(name, flag) \
6128 { #name, .horkage_on = (flag) }
6130 #define force_horkage_onoff(name, flag) \
6131 { "no" #name, .horkage_on = (flag) }, \
6132 { #name, .horkage_off = (flag) }
6134 static const struct ata_force_param force_tbl[] __initconst = {
6135 force_cbl(40c, ATA_CBL_PATA40),
6136 force_cbl(80c, ATA_CBL_PATA80),
6137 force_cbl(short40c, ATA_CBL_PATA40_SHORT),
6138 force_cbl(unk, ATA_CBL_PATA_UNK),
6139 force_cbl(ign, ATA_CBL_PATA_IGN),
6140 force_cbl(sata, ATA_CBL_SATA),
6142 force_spd_limit(1.5Gbps, 1),
6143 force_spd_limit(3.0Gbps, 2),
6145 force_xfer(pio0, ATA_SHIFT_PIO + 0),
6146 force_xfer(pio1, ATA_SHIFT_PIO + 1),
6147 force_xfer(pio2, ATA_SHIFT_PIO + 2),
6148 force_xfer(pio3, ATA_SHIFT_PIO + 3),
6149 force_xfer(pio4, ATA_SHIFT_PIO + 4),
6150 force_xfer(pio5, ATA_SHIFT_PIO + 5),
6151 force_xfer(pio6, ATA_SHIFT_PIO + 6),
6152 force_xfer(mwdma0, ATA_SHIFT_MWDMA + 0),
6153 force_xfer(mwdma1, ATA_SHIFT_MWDMA + 1),
6154 force_xfer(mwdma2, ATA_SHIFT_MWDMA + 2),
6155 force_xfer(mwdma3, ATA_SHIFT_MWDMA + 3),
6156 force_xfer(mwdma4, ATA_SHIFT_MWDMA + 4),
6157 force_xfer(udma0, ATA_SHIFT_UDMA + 0),
6158 force_xfer(udma16, ATA_SHIFT_UDMA + 0),
6159 force_xfer(udma/16, ATA_SHIFT_UDMA + 0),
6160 force_xfer(udma1, ATA_SHIFT_UDMA + 1),
6161 force_xfer(udma25, ATA_SHIFT_UDMA + 1),
6162 force_xfer(udma/25, ATA_SHIFT_UDMA + 1),
6163 force_xfer(udma2, ATA_SHIFT_UDMA + 2),
6164 force_xfer(udma33, ATA_SHIFT_UDMA + 2),
6165 force_xfer(udma/33, ATA_SHIFT_UDMA + 2),
6166 force_xfer(udma3, ATA_SHIFT_UDMA + 3),
6167 force_xfer(udma44, ATA_SHIFT_UDMA + 3),
6168 force_xfer(udma/44, ATA_SHIFT_UDMA + 3),
6169 force_xfer(udma4, ATA_SHIFT_UDMA + 4),
6170 force_xfer(udma66, ATA_SHIFT_UDMA + 4),
6171 force_xfer(udma/66, ATA_SHIFT_UDMA + 4),
6172 force_xfer(udma5, ATA_SHIFT_UDMA + 5),
6173 force_xfer(udma100, ATA_SHIFT_UDMA + 5),
6174 force_xfer(udma/100, ATA_SHIFT_UDMA + 5),
6175 force_xfer(udma6, ATA_SHIFT_UDMA + 6),
6176 force_xfer(udma133, ATA_SHIFT_UDMA + 6),
6177 force_xfer(udma/133, ATA_SHIFT_UDMA + 6),
6178 force_xfer(udma7, ATA_SHIFT_UDMA + 7),
6180 force_lflag_on(nohrst, ATA_LFLAG_NO_HRST),
6181 force_lflag_on(nosrst, ATA_LFLAG_NO_SRST),
6182 force_lflag_on(norst, ATA_LFLAG_NO_HRST | ATA_LFLAG_NO_SRST),
6183 force_lflag_on(rstonce, ATA_LFLAG_RST_ONCE),
6184 force_lflag_onoff(dbdelay, ATA_LFLAG_NO_DEBOUNCE_DELAY),
6186 force_horkage_onoff(ncq, ATA_HORKAGE_NONCQ),
6187 force_horkage_onoff(ncqtrim, ATA_HORKAGE_NO_NCQ_TRIM),
6188 force_horkage_onoff(ncqati, ATA_HORKAGE_NO_NCQ_ON_ATI),
6190 force_horkage_onoff(trim, ATA_HORKAGE_NOTRIM),
6191 force_horkage_on(trim_zero, ATA_HORKAGE_ZERO_AFTER_TRIM),
6192 force_horkage_on(max_trim_128m, ATA_HORKAGE_MAX_TRIM_128M),
6194 force_horkage_onoff(dma, ATA_HORKAGE_NODMA),
6195 force_horkage_on(atapi_dmadir, ATA_HORKAGE_ATAPI_DMADIR),
6196 force_horkage_on(atapi_mod16_dma, ATA_HORKAGE_ATAPI_MOD16_DMA),
6198 force_horkage_onoff(dmalog, ATA_HORKAGE_NO_DMA_LOG),
6199 force_horkage_onoff(iddevlog, ATA_HORKAGE_NO_ID_DEV_LOG),
6200 force_horkage_onoff(logdir, ATA_HORKAGE_NO_LOG_DIR),
6202 force_horkage_on(max_sec_128, ATA_HORKAGE_MAX_SEC_128),
6203 force_horkage_on(max_sec_1024, ATA_HORKAGE_MAX_SEC_1024),
6204 force_horkage_on(max_sec_lba48, ATA_HORKAGE_MAX_SEC_LBA48),
6206 force_horkage_onoff(lpm, ATA_HORKAGE_NOLPM),
6207 force_horkage_onoff(setxfer, ATA_HORKAGE_NOSETXFER),
6208 force_horkage_on(dump_id, ATA_HORKAGE_DUMP_ID),
6210 force_horkage_on(disable, ATA_HORKAGE_DISABLE),
6213 static int __init ata_parse_force_one(char **cur,
6214 struct ata_force_ent *force_ent,
6215 const char **reason)
6217 char *start = *cur, *p = *cur;
6218 char *id, *val, *endp;
6219 const struct ata_force_param *match_fp = NULL;
6220 int nr_matches = 0, i;
6222 /* find where this param ends and update *cur */
6223 while (*p != '\0' && *p != ',')
6234 p = strchr(start, ':');
6236 val = strstrip(start);
6241 id = strstrip(start);
6242 val = strstrip(p + 1);
6245 p = strchr(id, '.');
6248 force_ent->device = simple_strtoul(p, &endp, 10);
6249 if (p == endp || *endp != '\0') {
6250 *reason = "invalid device";
6255 force_ent->port = simple_strtoul(id, &endp, 10);
6256 if (id == endp || *endp != '\0') {
6257 *reason = "invalid port/link";
6262 /* parse val, allow shortcuts so that both 1.5 and 1.5Gbps work */
6263 for (i = 0; i < ARRAY_SIZE(force_tbl); i++) {
6264 const struct ata_force_param *fp = &force_tbl[i];
6266 if (strncasecmp(val, fp->name, strlen(val)))
6272 if (strcasecmp(val, fp->name) == 0) {
6279 *reason = "unknown value";
6282 if (nr_matches > 1) {
6283 *reason = "ambiguous value";
6287 force_ent->param = *match_fp;
6292 static void __init ata_parse_force_param(void)
6294 int idx = 0, size = 1;
6295 int last_port = -1, last_device = -1;
6296 char *p, *cur, *next;
6298 /* Calculate maximum number of params and allocate ata_force_tbl */
6299 for (p = ata_force_param_buf; *p; p++)
6303 ata_force_tbl = kcalloc(size, sizeof(ata_force_tbl[0]), GFP_KERNEL);
6304 if (!ata_force_tbl) {
6305 printk(KERN_WARNING "ata: failed to extend force table, "
6306 "libata.force ignored\n");
6310 /* parse and populate the table */
6311 for (cur = ata_force_param_buf; *cur != '\0'; cur = next) {
6312 const char *reason = "";
6313 struct ata_force_ent te = { .port = -1, .device = -1 };
6316 if (ata_parse_force_one(&next, &te, &reason)) {
6317 printk(KERN_WARNING "ata: failed to parse force "
6318 "parameter \"%s\" (%s)\n",
6323 if (te.port == -1) {
6324 te.port = last_port;
6325 te.device = last_device;
6328 ata_force_tbl[idx++] = te;
6330 last_port = te.port;
6331 last_device = te.device;
6334 ata_force_tbl_size = idx;
6337 static void ata_free_force_param(void)
6339 kfree(ata_force_tbl);
6342 static inline void ata_parse_force_param(void) { }
6343 static inline void ata_free_force_param(void) { }
6346 static int __init ata_init(void)
6350 ata_parse_force_param();
6352 rc = ata_sff_init();
6354 ata_free_force_param();
6358 libata_transport_init();
6359 ata_scsi_transport_template = ata_attach_transport();
6360 if (!ata_scsi_transport_template) {
6366 printk(KERN_DEBUG "libata version " DRV_VERSION " loaded.\n");
6373 static void __exit ata_exit(void)
6375 ata_release_transport(ata_scsi_transport_template);
6376 libata_transport_exit();
6378 ata_free_force_param();
6381 subsys_initcall(ata_init);
6382 module_exit(ata_exit);
6384 static DEFINE_RATELIMIT_STATE(ratelimit, HZ / 5, 1);
6386 int ata_ratelimit(void)
6388 return __ratelimit(&ratelimit);
6390 EXPORT_SYMBOL_GPL(ata_ratelimit);
6393 * ata_msleep - ATA EH owner aware msleep
6394 * @ap: ATA port to attribute the sleep to
6395 * @msecs: duration to sleep in milliseconds
6397 * Sleeps @msecs. If the current task is owner of @ap's EH, the
6398 * ownership is released before going to sleep and reacquired
6399 * after the sleep is complete. IOW, other ports sharing the
6400 * @ap->host will be allowed to own the EH while this task is
6406 void ata_msleep(struct ata_port *ap, unsigned int msecs)
6408 bool owns_eh = ap && ap->host->eh_owner == current;
6414 unsigned long usecs = msecs * USEC_PER_MSEC;
6415 usleep_range(usecs, usecs + 50);
6423 EXPORT_SYMBOL_GPL(ata_msleep);
6426 * ata_wait_register - wait until register value changes
6427 * @ap: ATA port to wait register for, can be NULL
6428 * @reg: IO-mapped register
6429 * @mask: Mask to apply to read register value
6430 * @val: Wait condition
6431 * @interval: polling interval in milliseconds
6432 * @timeout: timeout in milliseconds
6434 * Waiting for some bits of register to change is a common
6435 * operation for ATA controllers. This function reads 32bit LE
6436 * IO-mapped register @reg and tests for the following condition.
6438 * (*@reg & mask) != val
6440 * If the condition is met, it returns; otherwise, the process is
6441 * repeated after @interval_msec until timeout.
6444 * Kernel thread context (may sleep)
6447 * The final register value.
6449 u32 ata_wait_register(struct ata_port *ap, void __iomem *reg, u32 mask, u32 val,
6450 unsigned long interval, unsigned long timeout)
6452 unsigned long deadline;
6455 tmp = ioread32(reg);
6457 /* Calculate timeout _after_ the first read to make sure
6458 * preceding writes reach the controller before starting to
6459 * eat away the timeout.
6461 deadline = ata_deadline(jiffies, timeout);
6463 while ((tmp & mask) == val && time_before(jiffies, deadline)) {
6464 ata_msleep(ap, interval);
6465 tmp = ioread32(reg);
6470 EXPORT_SYMBOL_GPL(ata_wait_register);
6475 static unsigned int ata_dummy_qc_issue(struct ata_queued_cmd *qc)
6477 return AC_ERR_SYSTEM;
6480 static void ata_dummy_error_handler(struct ata_port *ap)
6485 struct ata_port_operations ata_dummy_port_ops = {
6486 .qc_prep = ata_noop_qc_prep,
6487 .qc_issue = ata_dummy_qc_issue,
6488 .error_handler = ata_dummy_error_handler,
6489 .sched_eh = ata_std_sched_eh,
6490 .end_eh = ata_std_end_eh,
6492 EXPORT_SYMBOL_GPL(ata_dummy_port_ops);
6494 const struct ata_port_info ata_dummy_port_info = {
6495 .port_ops = &ata_dummy_port_ops,
6497 EXPORT_SYMBOL_GPL(ata_dummy_port_info);
6499 void ata_print_version(const struct device *dev, const char *version)
6501 dev_printk(KERN_DEBUG, dev, "version %s\n", version);
6503 EXPORT_SYMBOL(ata_print_version);
6505 EXPORT_TRACEPOINT_SYMBOL_GPL(ata_tf_load);
6506 EXPORT_TRACEPOINT_SYMBOL_GPL(ata_exec_command);
6507 EXPORT_TRACEPOINT_SYMBOL_GPL(ata_bmdma_setup);
6508 EXPORT_TRACEPOINT_SYMBOL_GPL(ata_bmdma_start);
6509 EXPORT_TRACEPOINT_SYMBOL_GPL(ata_bmdma_status);