Thomas Gleixner | c82ee6d | 2019-05-19 15:51:48 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
Jeff Garzik | af36d7f | 2005-08-28 20:18:39 -0400 | [diff] [blame] | 3 | * libata-core.c - helper library for ATA |
| 4 | * |
Jeff Garzik | af36d7f | 2005-08-28 20:18:39 -0400 | [diff] [blame] | 5 | * Copyright 2003-2004 Red Hat, Inc. All rights reserved. |
| 6 | * Copyright 2003-2004 Jeff Garzik |
| 7 | * |
Jeff Garzik | af36d7f | 2005-08-28 20:18:39 -0400 | [diff] [blame] | 8 | * libata documentation is available via 'make {ps|pdf}docs', |
Mauro Carvalho Chehab | 19285f3 | 2017-05-14 11:52:56 -0300 | [diff] [blame] | 9 | * as Documentation/driver-api/libata.rst |
Jeff Garzik | af36d7f | 2005-08-28 20:18:39 -0400 | [diff] [blame] | 10 | * |
| 11 | * Hardware documentation available from http://www.t13.org/ and |
| 12 | * http://www.sata-io.org/ |
| 13 | * |
Alan Cox | 92c52c5 | 2007-11-19 14:30:16 +0000 | [diff] [blame] | 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) |
Bartlomiej Zolnierkiewicz | a52fbcf | 2020-03-26 16:58:04 +0100 | [diff] [blame] | 21 | * |
| 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. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | */ |
| 27 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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> |
| 33 | #include <linux/mm.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | #include <linux/spinlock.h> |
| 35 | #include <linux/blkdev.h> |
| 36 | #include <linux/delay.h> |
| 37 | #include <linux/timer.h> |
Anil Veliyankara Madam | 848c392 | 2016-01-07 21:18:52 -0800 | [diff] [blame] | 38 | #include <linux/time.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | #include <linux/interrupt.h> |
| 40 | #include <linux/completion.h> |
| 41 | #include <linux/suspend.h> |
| 42 | #include <linux/workqueue.h> |
David Hardeman | 378f058 | 2005-09-17 17:55:31 +1000 | [diff] [blame] | 43 | #include <linux/scatterlist.h> |
Jeff Garzik | 2dcb407 | 2007-10-19 06:42:56 -0400 | [diff] [blame] | 44 | #include <linux/io.h> |
Mark Lord | e18086d | 2009-03-19 13:32:21 -0400 | [diff] [blame] | 45 | #include <linux/log2.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 46 | #include <linux/slab.h> |
George Spelvin | 428ac5f | 2014-08-06 16:09:27 -0700 | [diff] [blame] | 47 | #include <linux/glob.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | #include <scsi/scsi.h> |
Jeff Garzik | 193515d | 2005-11-07 00:59:37 -0500 | [diff] [blame] | 49 | #include <scsi/scsi_cmnd.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | #include <scsi/scsi_host.h> |
| 51 | #include <linux/libata.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | #include <asm/byteorder.h> |
Hannes Reinecke | fe5af0c | 2016-04-25 12:45:48 +0200 | [diff] [blame] | 53 | #include <asm/unaligned.h> |
Tejun Heo | 140b5e59 | 2007-12-12 12:21:52 +0900 | [diff] [blame] | 54 | #include <linux/cdrom.h> |
Akinobu Mita | 9990b6f | 2010-04-12 21:11:41 +0900 | [diff] [blame] | 55 | #include <linux/ratelimit.h> |
Stephan Linz | eb25cb9 | 2016-06-10 07:59:56 +0200 | [diff] [blame] | 56 | #include <linux/leds.h> |
Lin Ming | 9ee4f39 | 2011-12-05 09:20:28 +0800 | [diff] [blame] | 57 | #include <linux/pm_runtime.h> |
Brian Norris | b7db04d | 2012-11-02 12:29:32 -0700 | [diff] [blame] | 58 | #include <linux/platform_device.h> |
Bartlomiej Zolnierkiewicz | bbf5a09 | 2020-03-26 16:58:01 +0100 | [diff] [blame] | 59 | #include <asm/setup.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | |
Hannes Reinecke | 255c03d | 2015-03-27 16:46:38 +0100 | [diff] [blame] | 61 | #define CREATE_TRACE_POINTS |
| 62 | #include <trace/events/libata.h> |
| 63 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | #include "libata.h" |
Gwendal Grignou | d902747 | 2010-05-25 12:31:38 -0700 | [diff] [blame] | 65 | #include "libata-transport.h" |
Jeff Garzik | fda0efc | 2007-01-31 07:43:15 -0500 | [diff] [blame] | 66 | |
Tejun Heo | 029cfd6 | 2008-03-25 12:22:49 +0900 | [diff] [blame] | 67 | const struct ata_port_operations ata_base_port_ops = { |
Tejun Heo | 0aa1113 | 2008-04-07 22:47:18 +0900 | [diff] [blame] | 68 | .prereset = ata_std_prereset, |
Tejun Heo | 203c75b | 2008-04-07 22:47:18 +0900 | [diff] [blame] | 69 | .postreset = ata_std_postreset, |
Tejun Heo | a1efdab | 2008-03-25 12:22:50 +0900 | [diff] [blame] | 70 | .error_handler = ata_std_error_handler, |
Dan Williams | e4a9c37 | 2012-06-21 23:25:27 -0700 | [diff] [blame] | 71 | .sched_eh = ata_std_sched_eh, |
| 72 | .end_eh = ata_std_end_eh, |
Tejun Heo | 029cfd6 | 2008-03-25 12:22:49 +0900 | [diff] [blame] | 73 | }; |
| 74 | |
| 75 | const struct ata_port_operations sata_port_ops = { |
| 76 | .inherits = &ata_base_port_ops, |
| 77 | |
| 78 | .qc_defer = ata_std_qc_defer, |
Tejun Heo | 57c9efd | 2008-04-07 22:47:19 +0900 | [diff] [blame] | 79 | .hardreset = sata_std_hardreset, |
Tejun Heo | 029cfd6 | 2008-03-25 12:22:49 +0900 | [diff] [blame] | 80 | }; |
Bartlomiej Zolnierkiewicz | a52fbcf | 2020-03-26 16:58:04 +0100 | [diff] [blame] | 81 | EXPORT_SYMBOL_GPL(sata_port_ops); |
Tejun Heo | 029cfd6 | 2008-03-25 12:22:49 +0900 | [diff] [blame] | 82 | |
Tejun Heo | 3373efd | 2006-05-15 20:57:53 +0900 | [diff] [blame] | 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); |
Tejun Heo | 75683fe | 2007-07-05 13:31:27 +0900 | [diff] [blame] | 87 | static unsigned long ata_dev_blacklisted(const struct ata_device *dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | |
Tero Roponen | a78f57a | 2012-04-22 11:38:00 +0300 | [diff] [blame] | 89 | atomic_t ata_print_id = ATOMIC_INIT(0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | |
Bartlomiej Zolnierkiewicz | bf89b0b | 2020-03-26 16:58:22 +0100 | [diff] [blame] | 91 | #ifdef CONFIG_ATA_FORCE |
Tejun Heo | 3326732 | 2008-02-13 09:15:09 +0900 | [diff] [blame] | 92 | struct ata_force_param { |
| 93 | const char *name; |
Bartlomiej Zolnierkiewicz | 8ba5a45 | 2020-03-26 16:58:02 +0100 | [diff] [blame] | 94 | u8 cbl; |
| 95 | u8 spd_limit; |
Tejun Heo | 3326732 | 2008-02-13 09:15:09 +0900 | [diff] [blame] | 96 | unsigned long xfer_mask; |
| 97 | unsigned int horkage_on; |
| 98 | unsigned int horkage_off; |
Bartlomiej Zolnierkiewicz | 8ba5a45 | 2020-03-26 16:58:02 +0100 | [diff] [blame] | 99 | u16 lflags; |
Tejun Heo | 3326732 | 2008-02-13 09:15:09 +0900 | [diff] [blame] | 100 | }; |
| 101 | |
| 102 | struct ata_force_ent { |
| 103 | int port; |
| 104 | int device; |
| 105 | struct ata_force_param param; |
| 106 | }; |
| 107 | |
| 108 | static struct ata_force_ent *ata_force_tbl; |
| 109 | static int ata_force_tbl_size; |
| 110 | |
Bartlomiej Zolnierkiewicz | bbf5a09 | 2020-03-26 16:58:01 +0100 | [diff] [blame] | 111 | static char ata_force_param_buf[COMMAND_LINE_SIZE] __initdata; |
Tejun Heo | 7afb422 | 2008-03-09 20:21:53 +0900 | [diff] [blame] | 112 | /* param_buf is thrown away after initialization, disallow read */ |
| 113 | module_param_string(force, ata_force_param_buf, sizeof(ata_force_param_buf), 0); |
Mauro Carvalho Chehab | 8c27ceff3 | 2016-10-18 10:12:27 -0200 | [diff] [blame] | 114 | MODULE_PARM_DESC(force, "Force ATA configurations including cable type, link speed and transfer mode (see Documentation/admin-guide/kernel-parameters.rst for details)"); |
Bartlomiej Zolnierkiewicz | bf89b0b | 2020-03-26 16:58:22 +0100 | [diff] [blame] | 115 | #endif |
Tejun Heo | 3326732 | 2008-02-13 09:15:09 +0900 | [diff] [blame] | 116 | |
Tejun Heo | 2486fa5 | 2008-07-31 07:52:40 +0900 | [diff] [blame] | 117 | static int atapi_enabled = 1; |
Jeff Garzik | 1623c81 | 2005-08-30 03:37:42 -0400 | [diff] [blame] | 118 | module_param(atapi_enabled, int, 0444); |
Evgeni Golov | ad5d8ea | 2009-06-22 09:46:20 +0200 | [diff] [blame] | 119 | MODULE_PARM_DESC(atapi_enabled, "Enable discovery of ATAPI devices (0=off, 1=on [default])"); |
Jeff Garzik | 1623c81 | 2005-08-30 03:37:42 -0400 | [diff] [blame] | 120 | |
Adrian Bunk | c5c61bd | 2008-02-25 02:07:25 +0200 | [diff] [blame] | 121 | static int atapi_dmadir = 0; |
Albert Lee | 95de719 | 2006-04-04 10:57:18 +0800 | [diff] [blame] | 122 | module_param(atapi_dmadir, int, 0444); |
Evgeni Golov | ad5d8ea | 2009-06-22 09:46:20 +0200 | [diff] [blame] | 123 | MODULE_PARM_DESC(atapi_dmadir, "Enable ATAPI DMADIR bridge support (0=off [default], 1=on)"); |
Albert Lee | 95de719 | 2006-04-04 10:57:18 +0800 | [diff] [blame] | 124 | |
Mark Lord | baf4fdf | 2007-08-08 01:08:45 +0900 | [diff] [blame] | 125 | int atapi_passthru16 = 1; |
| 126 | module_param(atapi_passthru16, int, 0444); |
Evgeni Golov | ad5d8ea | 2009-06-22 09:46:20 +0200 | [diff] [blame] | 127 | MODULE_PARM_DESC(atapi_passthru16, "Enable ATA_16 passthru for ATAPI devices (0=off, 1=on [default])"); |
Mark Lord | baf4fdf | 2007-08-08 01:08:45 +0900 | [diff] [blame] | 128 | |
Jeff Garzik | c3c013a | 2006-02-27 22:31:19 -0500 | [diff] [blame] | 129 | int libata_fua = 0; |
| 130 | module_param_named(fua, libata_fua, int, 0444); |
Evgeni Golov | ad5d8ea | 2009-06-22 09:46:20 +0200 | [diff] [blame] | 131 | MODULE_PARM_DESC(fua, "FUA support (0=off [default], 1=on)"); |
Jeff Garzik | c3c013a | 2006-02-27 22:31:19 -0500 | [diff] [blame] | 132 | |
Jeff Garzik | 2dcb407 | 2007-10-19 06:42:56 -0400 | [diff] [blame] | 133 | static int ata_ignore_hpa; |
Alan Cox | 1e99973 | 2007-04-11 00:23:13 +0100 | [diff] [blame] | 134 | module_param_named(ignore_hpa, ata_ignore_hpa, int, 0644); |
| 135 | MODULE_PARM_DESC(ignore_hpa, "Ignore HPA limit (0=keep BIOS limits, 1=ignore limits, using full disk)"); |
| 136 | |
Alan Cox | b3a7060 | 2007-10-02 12:38:26 -0400 | [diff] [blame] | 137 | static int libata_dma_mask = ATA_DMA_MASK_ATA|ATA_DMA_MASK_ATAPI|ATA_DMA_MASK_CFA; |
| 138 | module_param_named(dma, libata_dma_mask, int, 0444); |
| 139 | MODULE_PARM_DESC(dma, "DMA enable/disable (0x1==ATA, 0x2==ATAPI, 0x4==CF)"); |
| 140 | |
Tejun Heo | 87fbc5a | 2008-05-20 02:17:54 +0900 | [diff] [blame] | 141 | static int ata_probe_timeout; |
Andrew Morton | a8601e5 | 2006-06-25 01:36:52 -0700 | [diff] [blame] | 142 | module_param(ata_probe_timeout, int, 0444); |
| 143 | MODULE_PARM_DESC(ata_probe_timeout, "Set ATA probing timeout (seconds)"); |
| 144 | |
Jeff Garzik | 6ebe9d8 | 2007-10-05 16:28:36 -0400 | [diff] [blame] | 145 | int libata_noacpi = 0; |
Jeff Garzik | d7d0dad | 2007-03-28 01:57:37 -0400 | [diff] [blame] | 146 | module_param_named(noacpi, libata_noacpi, int, 0444); |
Evgeni Golov | ad5d8ea | 2009-06-22 09:46:20 +0200 | [diff] [blame] | 147 | MODULE_PARM_DESC(noacpi, "Disable the use of ACPI in probe/suspend/resume (0=off [default], 1=on)"); |
Kristen Carlson Accardi | 11ef697 | 2006-09-28 11:29:01 -0700 | [diff] [blame] | 148 | |
Alan Cox | ae8d4ee | 2007-11-04 22:05:49 -0500 | [diff] [blame] | 149 | int libata_allow_tpm = 0; |
| 150 | module_param_named(allow_tpm, libata_allow_tpm, int, 0444); |
Evgeni Golov | ad5d8ea | 2009-06-22 09:46:20 +0200 | [diff] [blame] | 151 | MODULE_PARM_DESC(allow_tpm, "Permit the use of TPM commands (0=off [default], 1=on)"); |
Alan Cox | ae8d4ee | 2007-11-04 22:05:49 -0500 | [diff] [blame] | 152 | |
Tejun Heo | e7ecd43 | 2010-05-19 15:38:58 +0200 | [diff] [blame] | 153 | static int atapi_an; |
| 154 | module_param(atapi_an, int, 0444); |
| 155 | MODULE_PARM_DESC(atapi_an, "Enable ATAPI AN media presence notification (0=0ff [default], 1=on)"); |
| 156 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | MODULE_AUTHOR("Jeff Garzik"); |
| 158 | MODULE_DESCRIPTION("Library module for ATA devices"); |
| 159 | MODULE_LICENSE("GPL"); |
| 160 | MODULE_VERSION(DRV_VERSION); |
| 161 | |
Damien Le Moal | 891fd7c | 2021-08-16 10:44:50 +0900 | [diff] [blame] | 162 | static inline bool ata_dev_print_info(struct ata_device *dev) |
| 163 | { |
| 164 | struct ata_eh_context *ehc = &dev->link->eh_context; |
| 165 | |
| 166 | return ehc->i.flags & ATA_EHI_PRINTINFO; |
| 167 | } |
Edward Falk | 0baab86 | 2005-06-02 18:17:13 -0400 | [diff] [blame] | 168 | |
Tejun Heo | 9913ff8a | 2009-01-29 20:31:31 +0900 | [diff] [blame] | 169 | static bool ata_sstatus_online(u32 sstatus) |
| 170 | { |
| 171 | return (sstatus & 0xf) == 0x3; |
| 172 | } |
| 173 | |
Tejun Heo | 1eca436 | 2008-11-03 20:03:17 +0900 | [diff] [blame] | 174 | /** |
| 175 | * ata_link_next - link iteration helper |
| 176 | * @link: the previous link, NULL to start |
| 177 | * @ap: ATA port containing links to iterate |
| 178 | * @mode: iteration mode, one of ATA_LITER_* |
Tejun Heo | aadffb6 | 2008-07-31 17:02:41 +0900 | [diff] [blame] | 179 | * |
Tejun Heo | 1eca436 | 2008-11-03 20:03:17 +0900 | [diff] [blame] | 180 | * LOCKING: |
| 181 | * Host lock or EH context. |
| 182 | * |
| 183 | * RETURNS: |
| 184 | * Pointer to the next link. |
Tejun Heo | aadffb6 | 2008-07-31 17:02:41 +0900 | [diff] [blame] | 185 | */ |
Tejun Heo | 1eca436 | 2008-11-03 20:03:17 +0900 | [diff] [blame] | 186 | struct ata_link *ata_link_next(struct ata_link *link, struct ata_port *ap, |
| 187 | enum ata_link_iter_mode mode) |
Tejun Heo | aadffb6 | 2008-07-31 17:02:41 +0900 | [diff] [blame] | 188 | { |
Tejun Heo | 1eca436 | 2008-11-03 20:03:17 +0900 | [diff] [blame] | 189 | BUG_ON(mode != ATA_LITER_EDGE && |
| 190 | mode != ATA_LITER_PMP_FIRST && mode != ATA_LITER_HOST_FIRST); |
Tejun Heo | aadffb6 | 2008-07-31 17:02:41 +0900 | [diff] [blame] | 191 | |
Tejun Heo | 1eca436 | 2008-11-03 20:03:17 +0900 | [diff] [blame] | 192 | /* NULL link indicates start of iteration */ |
| 193 | if (!link) |
| 194 | switch (mode) { |
| 195 | case ATA_LITER_EDGE: |
| 196 | case ATA_LITER_PMP_FIRST: |
| 197 | if (sata_pmp_attached(ap)) |
| 198 | return ap->pmp_link; |
Gustavo A. R. Silva | df561f66 | 2020-08-23 17:36:59 -0500 | [diff] [blame] | 199 | fallthrough; |
Tejun Heo | 1eca436 | 2008-11-03 20:03:17 +0900 | [diff] [blame] | 200 | case ATA_LITER_HOST_FIRST: |
| 201 | return &ap->link; |
| 202 | } |
| 203 | |
| 204 | /* we just iterated over the host link, what's next? */ |
| 205 | if (link == &ap->link) |
| 206 | switch (mode) { |
| 207 | case ATA_LITER_HOST_FIRST: |
| 208 | if (sata_pmp_attached(ap)) |
| 209 | return ap->pmp_link; |
Gustavo A. R. Silva | df561f66 | 2020-08-23 17:36:59 -0500 | [diff] [blame] | 210 | fallthrough; |
Tejun Heo | 1eca436 | 2008-11-03 20:03:17 +0900 | [diff] [blame] | 211 | case ATA_LITER_PMP_FIRST: |
| 212 | if (unlikely(ap->slave_link)) |
Tejun Heo | b1c7291 | 2008-07-31 17:02:43 +0900 | [diff] [blame] | 213 | return ap->slave_link; |
Gustavo A. R. Silva | df561f66 | 2020-08-23 17:36:59 -0500 | [diff] [blame] | 214 | fallthrough; |
Tejun Heo | 1eca436 | 2008-11-03 20:03:17 +0900 | [diff] [blame] | 215 | case ATA_LITER_EDGE: |
Tejun Heo | aadffb6 | 2008-07-31 17:02:41 +0900 | [diff] [blame] | 216 | return NULL; |
Tejun Heo | b1c7291 | 2008-07-31 17:02:43 +0900 | [diff] [blame] | 217 | } |
Tejun Heo | aadffb6 | 2008-07-31 17:02:41 +0900 | [diff] [blame] | 218 | |
Tejun Heo | b1c7291 | 2008-07-31 17:02:43 +0900 | [diff] [blame] | 219 | /* slave_link excludes PMP */ |
| 220 | if (unlikely(link == ap->slave_link)) |
| 221 | return NULL; |
| 222 | |
Tejun Heo | 1eca436 | 2008-11-03 20:03:17 +0900 | [diff] [blame] | 223 | /* we were over a PMP link */ |
Tejun Heo | aadffb6 | 2008-07-31 17:02:41 +0900 | [diff] [blame] | 224 | if (++link < ap->pmp_link + ap->nr_pmp_links) |
| 225 | return link; |
Tejun Heo | 1eca436 | 2008-11-03 20:03:17 +0900 | [diff] [blame] | 226 | |
| 227 | if (mode == ATA_LITER_PMP_FIRST) |
| 228 | return &ap->link; |
| 229 | |
Tejun Heo | aadffb6 | 2008-07-31 17:02:41 +0900 | [diff] [blame] | 230 | return NULL; |
| 231 | } |
Bartlomiej Zolnierkiewicz | a52fbcf | 2020-03-26 16:58:04 +0100 | [diff] [blame] | 232 | EXPORT_SYMBOL_GPL(ata_link_next); |
Tejun Heo | aadffb6 | 2008-07-31 17:02:41 +0900 | [diff] [blame] | 233 | |
Edward Falk | 0baab86 | 2005-06-02 18:17:13 -0400 | [diff] [blame] | 234 | /** |
Tejun Heo | 1eca436 | 2008-11-03 20:03:17 +0900 | [diff] [blame] | 235 | * ata_dev_next - device iteration helper |
| 236 | * @dev: the previous device, NULL to start |
| 237 | * @link: ATA link containing devices to iterate |
| 238 | * @mode: iteration mode, one of ATA_DITER_* |
| 239 | * |
| 240 | * LOCKING: |
| 241 | * Host lock or EH context. |
| 242 | * |
| 243 | * RETURNS: |
| 244 | * Pointer to the next device. |
| 245 | */ |
| 246 | struct ata_device *ata_dev_next(struct ata_device *dev, struct ata_link *link, |
| 247 | enum ata_dev_iter_mode mode) |
| 248 | { |
| 249 | BUG_ON(mode != ATA_DITER_ENABLED && mode != ATA_DITER_ENABLED_REVERSE && |
| 250 | mode != ATA_DITER_ALL && mode != ATA_DITER_ALL_REVERSE); |
| 251 | |
| 252 | /* NULL dev indicates start of iteration */ |
| 253 | if (!dev) |
| 254 | switch (mode) { |
| 255 | case ATA_DITER_ENABLED: |
| 256 | case ATA_DITER_ALL: |
| 257 | dev = link->device; |
| 258 | goto check; |
| 259 | case ATA_DITER_ENABLED_REVERSE: |
| 260 | case ATA_DITER_ALL_REVERSE: |
| 261 | dev = link->device + ata_link_max_devices(link) - 1; |
| 262 | goto check; |
| 263 | } |
| 264 | |
| 265 | next: |
| 266 | /* move to the next one */ |
| 267 | switch (mode) { |
| 268 | case ATA_DITER_ENABLED: |
| 269 | case ATA_DITER_ALL: |
| 270 | if (++dev < link->device + ata_link_max_devices(link)) |
| 271 | goto check; |
| 272 | return NULL; |
| 273 | case ATA_DITER_ENABLED_REVERSE: |
| 274 | case ATA_DITER_ALL_REVERSE: |
| 275 | if (--dev >= link->device) |
| 276 | goto check; |
| 277 | return NULL; |
| 278 | } |
| 279 | |
| 280 | check: |
| 281 | if ((mode == ATA_DITER_ENABLED || mode == ATA_DITER_ENABLED_REVERSE) && |
| 282 | !ata_dev_enabled(dev)) |
| 283 | goto next; |
| 284 | return dev; |
| 285 | } |
Bartlomiej Zolnierkiewicz | a52fbcf | 2020-03-26 16:58:04 +0100 | [diff] [blame] | 286 | EXPORT_SYMBOL_GPL(ata_dev_next); |
Tejun Heo | 1eca436 | 2008-11-03 20:03:17 +0900 | [diff] [blame] | 287 | |
| 288 | /** |
Tejun Heo | b1c7291 | 2008-07-31 17:02:43 +0900 | [diff] [blame] | 289 | * ata_dev_phys_link - find physical link for a device |
| 290 | * @dev: ATA device to look up physical link for |
| 291 | * |
| 292 | * Look up physical link which @dev is attached to. Note that |
| 293 | * this is different from @dev->link only when @dev is on slave |
| 294 | * link. For all other cases, it's the same as @dev->link. |
| 295 | * |
| 296 | * LOCKING: |
| 297 | * Don't care. |
| 298 | * |
| 299 | * RETURNS: |
| 300 | * Pointer to the found physical link. |
| 301 | */ |
| 302 | struct ata_link *ata_dev_phys_link(struct ata_device *dev) |
| 303 | { |
| 304 | struct ata_port *ap = dev->link->ap; |
| 305 | |
| 306 | if (!ap->slave_link) |
| 307 | return dev->link; |
| 308 | if (!dev->devno) |
| 309 | return &ap->link; |
| 310 | return ap->slave_link; |
| 311 | } |
| 312 | |
Bartlomiej Zolnierkiewicz | bf89b0b | 2020-03-26 16:58:22 +0100 | [diff] [blame] | 313 | #ifdef CONFIG_ATA_FORCE |
Tejun Heo | b1c7291 | 2008-07-31 17:02:43 +0900 | [diff] [blame] | 314 | /** |
Tejun Heo | 3326732 | 2008-02-13 09:15:09 +0900 | [diff] [blame] | 315 | * ata_force_cbl - force cable type according to libata.force |
Randy Dunlap | 4cdfa1b | 2008-02-22 12:21:37 -0800 | [diff] [blame] | 316 | * @ap: ATA port of interest |
Tejun Heo | 3326732 | 2008-02-13 09:15:09 +0900 | [diff] [blame] | 317 | * |
| 318 | * Force cable type according to libata.force and whine about it. |
| 319 | * The last entry which has matching port number is used, so it |
| 320 | * can be specified as part of device force parameters. For |
| 321 | * example, both "a:40c,1.00:udma4" and "1.00:40c,udma4" have the |
| 322 | * same effect. |
| 323 | * |
| 324 | * LOCKING: |
| 325 | * EH context. |
| 326 | */ |
| 327 | void ata_force_cbl(struct ata_port *ap) |
| 328 | { |
| 329 | int i; |
| 330 | |
| 331 | for (i = ata_force_tbl_size - 1; i >= 0; i--) { |
| 332 | const struct ata_force_ent *fe = &ata_force_tbl[i]; |
| 333 | |
| 334 | if (fe->port != -1 && fe->port != ap->print_id) |
| 335 | continue; |
| 336 | |
| 337 | if (fe->param.cbl == ATA_CBL_NONE) |
| 338 | continue; |
| 339 | |
| 340 | ap->cbl = fe->param.cbl; |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 341 | ata_port_notice(ap, "FORCE: cable set to %s\n", fe->param.name); |
Tejun Heo | 3326732 | 2008-02-13 09:15:09 +0900 | [diff] [blame] | 342 | return; |
| 343 | } |
| 344 | } |
| 345 | |
| 346 | /** |
Tejun Heo | 05944bd | 2008-08-13 20:19:09 +0900 | [diff] [blame] | 347 | * ata_force_link_limits - force link limits according to libata.force |
Tejun Heo | 3326732 | 2008-02-13 09:15:09 +0900 | [diff] [blame] | 348 | * @link: ATA link of interest |
| 349 | * |
Tejun Heo | 05944bd | 2008-08-13 20:19:09 +0900 | [diff] [blame] | 350 | * Force link flags and SATA spd limit according to libata.force |
| 351 | * and whine about it. When only the port part is specified |
| 352 | * (e.g. 1:), the limit applies to all links connected to both |
| 353 | * the host link and all fan-out ports connected via PMP. If the |
| 354 | * device part is specified as 0 (e.g. 1.00:), it specifies the |
| 355 | * first fan-out link not the host link. Device number 15 always |
Tejun Heo | b1c7291 | 2008-07-31 17:02:43 +0900 | [diff] [blame] | 356 | * points to the host link whether PMP is attached or not. If the |
| 357 | * controller has slave link, device number 16 points to it. |
Tejun Heo | 3326732 | 2008-02-13 09:15:09 +0900 | [diff] [blame] | 358 | * |
| 359 | * LOCKING: |
| 360 | * EH context. |
| 361 | */ |
Tejun Heo | 05944bd | 2008-08-13 20:19:09 +0900 | [diff] [blame] | 362 | static void ata_force_link_limits(struct ata_link *link) |
Tejun Heo | 3326732 | 2008-02-13 09:15:09 +0900 | [diff] [blame] | 363 | { |
Tejun Heo | 05944bd | 2008-08-13 20:19:09 +0900 | [diff] [blame] | 364 | bool did_spd = false; |
Tejun Heo | b1c7291 | 2008-07-31 17:02:43 +0900 | [diff] [blame] | 365 | int linkno = link->pmp; |
| 366 | int i; |
Tejun Heo | 3326732 | 2008-02-13 09:15:09 +0900 | [diff] [blame] | 367 | |
| 368 | if (ata_is_host_link(link)) |
Tejun Heo | b1c7291 | 2008-07-31 17:02:43 +0900 | [diff] [blame] | 369 | linkno += 15; |
Tejun Heo | 3326732 | 2008-02-13 09:15:09 +0900 | [diff] [blame] | 370 | |
| 371 | for (i = ata_force_tbl_size - 1; i >= 0; i--) { |
| 372 | const struct ata_force_ent *fe = &ata_force_tbl[i]; |
| 373 | |
| 374 | if (fe->port != -1 && fe->port != link->ap->print_id) |
| 375 | continue; |
| 376 | |
| 377 | if (fe->device != -1 && fe->device != linkno) |
| 378 | continue; |
| 379 | |
Tejun Heo | 05944bd | 2008-08-13 20:19:09 +0900 | [diff] [blame] | 380 | /* only honor the first spd limit */ |
| 381 | if (!did_spd && fe->param.spd_limit) { |
| 382 | link->hw_sata_spd_limit = (1 << fe->param.spd_limit) - 1; |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 383 | ata_link_notice(link, "FORCE: PHY spd limit set to %s\n", |
Tejun Heo | 05944bd | 2008-08-13 20:19:09 +0900 | [diff] [blame] | 384 | fe->param.name); |
| 385 | did_spd = true; |
| 386 | } |
Tejun Heo | 3326732 | 2008-02-13 09:15:09 +0900 | [diff] [blame] | 387 | |
Tejun Heo | 05944bd | 2008-08-13 20:19:09 +0900 | [diff] [blame] | 388 | /* let lflags stack */ |
| 389 | if (fe->param.lflags) { |
| 390 | link->flags |= fe->param.lflags; |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 391 | ata_link_notice(link, |
Tejun Heo | 05944bd | 2008-08-13 20:19:09 +0900 | [diff] [blame] | 392 | "FORCE: link flag 0x%x forced -> 0x%x\n", |
| 393 | fe->param.lflags, link->flags); |
| 394 | } |
Tejun Heo | 3326732 | 2008-02-13 09:15:09 +0900 | [diff] [blame] | 395 | } |
| 396 | } |
| 397 | |
| 398 | /** |
| 399 | * ata_force_xfermask - force xfermask according to libata.force |
| 400 | * @dev: ATA device of interest |
| 401 | * |
| 402 | * Force xfer_mask according to libata.force and whine about it. |
| 403 | * For consistency with link selection, device number 15 selects |
| 404 | * the first device connected to the host link. |
| 405 | * |
| 406 | * LOCKING: |
| 407 | * EH context. |
| 408 | */ |
| 409 | static void ata_force_xfermask(struct ata_device *dev) |
| 410 | { |
| 411 | int devno = dev->link->pmp + dev->devno; |
| 412 | int alt_devno = devno; |
| 413 | int i; |
| 414 | |
Tejun Heo | b1c7291 | 2008-07-31 17:02:43 +0900 | [diff] [blame] | 415 | /* allow n.15/16 for devices attached to host port */ |
| 416 | if (ata_is_host_link(dev->link)) |
| 417 | alt_devno += 15; |
Tejun Heo | 3326732 | 2008-02-13 09:15:09 +0900 | [diff] [blame] | 418 | |
| 419 | for (i = ata_force_tbl_size - 1; i >= 0; i--) { |
| 420 | const struct ata_force_ent *fe = &ata_force_tbl[i]; |
| 421 | unsigned long pio_mask, mwdma_mask, udma_mask; |
| 422 | |
| 423 | if (fe->port != -1 && fe->port != dev->link->ap->print_id) |
| 424 | continue; |
| 425 | |
| 426 | if (fe->device != -1 && fe->device != devno && |
| 427 | fe->device != alt_devno) |
| 428 | continue; |
| 429 | |
| 430 | if (!fe->param.xfer_mask) |
| 431 | continue; |
| 432 | |
| 433 | ata_unpack_xfermask(fe->param.xfer_mask, |
| 434 | &pio_mask, &mwdma_mask, &udma_mask); |
| 435 | if (udma_mask) |
| 436 | dev->udma_mask = udma_mask; |
| 437 | else if (mwdma_mask) { |
| 438 | dev->udma_mask = 0; |
| 439 | dev->mwdma_mask = mwdma_mask; |
| 440 | } else { |
| 441 | dev->udma_mask = 0; |
| 442 | dev->mwdma_mask = 0; |
| 443 | dev->pio_mask = pio_mask; |
| 444 | } |
| 445 | |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 446 | ata_dev_notice(dev, "FORCE: xfer_mask set to %s\n", |
| 447 | fe->param.name); |
Tejun Heo | 3326732 | 2008-02-13 09:15:09 +0900 | [diff] [blame] | 448 | return; |
| 449 | } |
| 450 | } |
| 451 | |
| 452 | /** |
| 453 | * ata_force_horkage - force horkage according to libata.force |
| 454 | * @dev: ATA device of interest |
| 455 | * |
| 456 | * Force horkage according to libata.force and whine about it. |
| 457 | * For consistency with link selection, device number 15 selects |
| 458 | * the first device connected to the host link. |
| 459 | * |
| 460 | * LOCKING: |
| 461 | * EH context. |
| 462 | */ |
| 463 | static void ata_force_horkage(struct ata_device *dev) |
| 464 | { |
| 465 | int devno = dev->link->pmp + dev->devno; |
| 466 | int alt_devno = devno; |
| 467 | int i; |
| 468 | |
Tejun Heo | b1c7291 | 2008-07-31 17:02:43 +0900 | [diff] [blame] | 469 | /* allow n.15/16 for devices attached to host port */ |
| 470 | if (ata_is_host_link(dev->link)) |
| 471 | alt_devno += 15; |
Tejun Heo | 3326732 | 2008-02-13 09:15:09 +0900 | [diff] [blame] | 472 | |
| 473 | for (i = 0; i < ata_force_tbl_size; i++) { |
| 474 | const struct ata_force_ent *fe = &ata_force_tbl[i]; |
| 475 | |
| 476 | if (fe->port != -1 && fe->port != dev->link->ap->print_id) |
| 477 | continue; |
| 478 | |
| 479 | if (fe->device != -1 && fe->device != devno && |
| 480 | fe->device != alt_devno) |
| 481 | continue; |
| 482 | |
| 483 | if (!(~dev->horkage & fe->param.horkage_on) && |
| 484 | !(dev->horkage & fe->param.horkage_off)) |
| 485 | continue; |
| 486 | |
| 487 | dev->horkage |= fe->param.horkage_on; |
| 488 | dev->horkage &= ~fe->param.horkage_off; |
| 489 | |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 490 | ata_dev_notice(dev, "FORCE: horkage modified (%s)\n", |
| 491 | fe->param.name); |
Tejun Heo | 3326732 | 2008-02-13 09:15:09 +0900 | [diff] [blame] | 492 | } |
| 493 | } |
Bartlomiej Zolnierkiewicz | bf89b0b | 2020-03-26 16:58:22 +0100 | [diff] [blame] | 494 | #else |
| 495 | static inline void ata_force_link_limits(struct ata_link *link) { } |
| 496 | static inline void ata_force_xfermask(struct ata_device *dev) { } |
| 497 | static inline void ata_force_horkage(struct ata_device *dev) { } |
| 498 | #endif |
Tejun Heo | 3326732 | 2008-02-13 09:15:09 +0900 | [diff] [blame] | 499 | |
| 500 | /** |
Tejun Heo | 436d34b | 2008-04-02 17:28:46 +0900 | [diff] [blame] | 501 | * atapi_cmd_type - Determine ATAPI command type from SCSI opcode |
| 502 | * @opcode: SCSI opcode |
| 503 | * |
| 504 | * Determine ATAPI command type from @opcode. |
| 505 | * |
| 506 | * LOCKING: |
| 507 | * None. |
| 508 | * |
| 509 | * RETURNS: |
| 510 | * ATAPI_{READ|WRITE|READ_CD|PASS_THRU|MISC} |
| 511 | */ |
| 512 | int atapi_cmd_type(u8 opcode) |
| 513 | { |
| 514 | switch (opcode) { |
| 515 | case GPCMD_READ_10: |
| 516 | case GPCMD_READ_12: |
| 517 | return ATAPI_READ; |
| 518 | |
| 519 | case GPCMD_WRITE_10: |
| 520 | case GPCMD_WRITE_12: |
| 521 | case GPCMD_WRITE_AND_VERIFY_10: |
| 522 | return ATAPI_WRITE; |
| 523 | |
| 524 | case GPCMD_READ_CD: |
| 525 | case GPCMD_READ_CD_MSF: |
| 526 | return ATAPI_READ_CD; |
| 527 | |
Tejun Heo | e52dcc4 | 2008-04-02 17:35:19 +0900 | [diff] [blame] | 528 | case ATA_16: |
| 529 | case ATA_12: |
| 530 | if (atapi_passthru16) |
| 531 | return ATAPI_PASS_THRU; |
Gustavo A. R. Silva | df561f66 | 2020-08-23 17:36:59 -0500 | [diff] [blame] | 532 | fallthrough; |
Tejun Heo | 436d34b | 2008-04-02 17:28:46 +0900 | [diff] [blame] | 533 | default: |
| 534 | return ATAPI_MISC; |
| 535 | } |
| 536 | } |
Bartlomiej Zolnierkiewicz | a52fbcf | 2020-03-26 16:58:04 +0100 | [diff] [blame] | 537 | EXPORT_SYMBOL_GPL(atapi_cmd_type); |
Tejun Heo | 436d34b | 2008-04-02 17:28:46 +0900 | [diff] [blame] | 538 | |
Albert Lee | 8cbd6df | 2005-10-12 15:06:27 +0800 | [diff] [blame] | 539 | static const u8 ata_rw_cmds[] = { |
| 540 | /* pio multi */ |
| 541 | ATA_CMD_READ_MULTI, |
| 542 | ATA_CMD_WRITE_MULTI, |
| 543 | ATA_CMD_READ_MULTI_EXT, |
| 544 | ATA_CMD_WRITE_MULTI_EXT, |
Tejun Heo | 9a3dccc | 2006-01-06 09:56:18 +0100 | [diff] [blame] | 545 | 0, |
| 546 | 0, |
| 547 | 0, |
| 548 | ATA_CMD_WRITE_MULTI_FUA_EXT, |
Albert Lee | 8cbd6df | 2005-10-12 15:06:27 +0800 | [diff] [blame] | 549 | /* pio */ |
| 550 | ATA_CMD_PIO_READ, |
| 551 | ATA_CMD_PIO_WRITE, |
| 552 | ATA_CMD_PIO_READ_EXT, |
| 553 | ATA_CMD_PIO_WRITE_EXT, |
Tejun Heo | 9a3dccc | 2006-01-06 09:56:18 +0100 | [diff] [blame] | 554 | 0, |
| 555 | 0, |
| 556 | 0, |
| 557 | 0, |
Albert Lee | 8cbd6df | 2005-10-12 15:06:27 +0800 | [diff] [blame] | 558 | /* dma */ |
| 559 | ATA_CMD_READ, |
| 560 | ATA_CMD_WRITE, |
| 561 | ATA_CMD_READ_EXT, |
Tejun Heo | 9a3dccc | 2006-01-06 09:56:18 +0100 | [diff] [blame] | 562 | ATA_CMD_WRITE_EXT, |
| 563 | 0, |
| 564 | 0, |
| 565 | 0, |
| 566 | ATA_CMD_WRITE_FUA_EXT |
Albert Lee | 8cbd6df | 2005-10-12 15:06:27 +0800 | [diff] [blame] | 567 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 568 | |
| 569 | /** |
Albert Lee | 8cbd6df | 2005-10-12 15:06:27 +0800 | [diff] [blame] | 570 | * ata_rwcmd_protocol - set taskfile r/w commands and protocol |
Tejun Heo | bd056d7 | 2006-11-14 22:47:10 +0900 | [diff] [blame] | 571 | * @tf: command to examine and configure |
| 572 | * @dev: device tf belongs to |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 573 | * |
Jeff Garzik | 2e9edbf | 2006-03-24 09:56:57 -0500 | [diff] [blame] | 574 | * Examine the device configuration and tf->flags to calculate |
Albert Lee | 8cbd6df | 2005-10-12 15:06:27 +0800 | [diff] [blame] | 575 | * the proper read/write commands and protocol to use. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 576 | * |
| 577 | * LOCKING: |
| 578 | * caller. |
| 579 | */ |
Tejun Heo | bd056d7 | 2006-11-14 22:47:10 +0900 | [diff] [blame] | 580 | static int ata_rwcmd_protocol(struct ata_taskfile *tf, struct ata_device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 581 | { |
Tejun Heo | 9a3dccc | 2006-01-06 09:56:18 +0100 | [diff] [blame] | 582 | u8 cmd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 583 | |
Tejun Heo | 9a3dccc | 2006-01-06 09:56:18 +0100 | [diff] [blame] | 584 | int index, fua, lba48, write; |
Jeff Garzik | 2e9edbf | 2006-03-24 09:56:57 -0500 | [diff] [blame] | 585 | |
Tejun Heo | 9a3dccc | 2006-01-06 09:56:18 +0100 | [diff] [blame] | 586 | fua = (tf->flags & ATA_TFLAG_FUA) ? 4 : 0; |
Albert Lee | 8cbd6df | 2005-10-12 15:06:27 +0800 | [diff] [blame] | 587 | lba48 = (tf->flags & ATA_TFLAG_LBA48) ? 2 : 0; |
| 588 | write = (tf->flags & ATA_TFLAG_WRITE) ? 1 : 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 589 | |
Albert Lee | 8cbd6df | 2005-10-12 15:06:27 +0800 | [diff] [blame] | 590 | if (dev->flags & ATA_DFLAG_PIO) { |
| 591 | tf->protocol = ATA_PROT_PIO; |
Tejun Heo | 9a3dccc | 2006-01-06 09:56:18 +0100 | [diff] [blame] | 592 | index = dev->multi_count ? 0 : 8; |
Tejun Heo | 9af5c9c | 2007-08-06 18:36:22 +0900 | [diff] [blame] | 593 | } else if (lba48 && (dev->link->ap->flags & ATA_FLAG_PIO_LBA48)) { |
Alan Cox | 8d238e0 | 2006-01-17 20:50:31 +0000 | [diff] [blame] | 594 | /* Unable to use DMA due to host limitation */ |
| 595 | tf->protocol = ATA_PROT_PIO; |
Albert Lee | 0565c26 | 2006-02-13 18:55:25 +0800 | [diff] [blame] | 596 | index = dev->multi_count ? 0 : 8; |
Albert Lee | 8cbd6df | 2005-10-12 15:06:27 +0800 | [diff] [blame] | 597 | } else { |
| 598 | tf->protocol = ATA_PROT_DMA; |
Tejun Heo | 9a3dccc | 2006-01-06 09:56:18 +0100 | [diff] [blame] | 599 | index = 16; |
Albert Lee | 8cbd6df | 2005-10-12 15:06:27 +0800 | [diff] [blame] | 600 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 601 | |
Tejun Heo | 9a3dccc | 2006-01-06 09:56:18 +0100 | [diff] [blame] | 602 | cmd = ata_rw_cmds[index + fua + lba48 + write]; |
| 603 | if (cmd) { |
| 604 | tf->command = cmd; |
| 605 | return 0; |
| 606 | } |
| 607 | return -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 608 | } |
| 609 | |
Tejun Heo | cb95d56 | 2006-03-06 04:31:56 +0900 | [diff] [blame] | 610 | /** |
Tejun Heo | 35b649f | 2006-11-14 22:37:35 +0900 | [diff] [blame] | 611 | * ata_tf_read_block - Read block address from ATA taskfile |
| 612 | * @tf: ATA taskfile of interest |
| 613 | * @dev: ATA device @tf belongs to |
| 614 | * |
| 615 | * LOCKING: |
| 616 | * None. |
| 617 | * |
| 618 | * Read block address from @tf. This function can handle all |
| 619 | * three address formats - LBA, LBA48 and CHS. tf->protocol and |
| 620 | * flags select the address format to use. |
| 621 | * |
| 622 | * RETURNS: |
| 623 | * Block address read from @tf. |
| 624 | */ |
Hannes Reinecke | cffd1ee | 2016-04-04 11:43:57 +0200 | [diff] [blame] | 625 | u64 ata_tf_read_block(const struct ata_taskfile *tf, struct ata_device *dev) |
Tejun Heo | 35b649f | 2006-11-14 22:37:35 +0900 | [diff] [blame] | 626 | { |
| 627 | u64 block = 0; |
| 628 | |
Tejun Heo | fe16d4f | 2015-08-03 11:41:33 -0400 | [diff] [blame] | 629 | if (tf->flags & ATA_TFLAG_LBA) { |
Tejun Heo | 35b649f | 2006-11-14 22:37:35 +0900 | [diff] [blame] | 630 | if (tf->flags & ATA_TFLAG_LBA48) { |
| 631 | block |= (u64)tf->hob_lbah << 40; |
| 632 | block |= (u64)tf->hob_lbam << 32; |
Roland Dreier | 44901a9 | 2008-11-04 10:34:48 -0800 | [diff] [blame] | 633 | block |= (u64)tf->hob_lbal << 24; |
Tejun Heo | 35b649f | 2006-11-14 22:37:35 +0900 | [diff] [blame] | 634 | } else |
| 635 | block |= (tf->device & 0xf) << 24; |
| 636 | |
| 637 | block |= tf->lbah << 16; |
| 638 | block |= tf->lbam << 8; |
| 639 | block |= tf->lbal; |
| 640 | } else { |
| 641 | u32 cyl, head, sect; |
| 642 | |
| 643 | cyl = tf->lbam | (tf->lbah << 8); |
| 644 | head = tf->device & 0xf; |
| 645 | sect = tf->lbal; |
| 646 | |
Tejun Heo | ac8672e | 2009-08-16 21:21:21 +0900 | [diff] [blame] | 647 | if (!sect) { |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 648 | ata_dev_warn(dev, |
| 649 | "device reported invalid CHS sector 0\n"); |
Hannes Reinecke | cffd1ee | 2016-04-04 11:43:57 +0200 | [diff] [blame] | 650 | return U64_MAX; |
Tejun Heo | ac8672e | 2009-08-16 21:21:21 +0900 | [diff] [blame] | 651 | } |
| 652 | |
| 653 | block = (cyl * dev->heads + head) * dev->sectors + sect - 1; |
Tejun Heo | 35b649f | 2006-11-14 22:37:35 +0900 | [diff] [blame] | 654 | } |
| 655 | |
| 656 | return block; |
| 657 | } |
| 658 | |
| 659 | /** |
Tejun Heo | bd056d7 | 2006-11-14 22:47:10 +0900 | [diff] [blame] | 660 | * ata_build_rw_tf - Build ATA taskfile for given read/write request |
| 661 | * @tf: Target ATA taskfile |
| 662 | * @dev: ATA device @tf belongs to |
| 663 | * @block: Block address |
| 664 | * @n_block: Number of blocks |
| 665 | * @tf_flags: RW/FUA etc... |
| 666 | * @tag: tag |
Adam Manzanares | 8e06178 | 2016-10-17 11:27:29 -0700 | [diff] [blame] | 667 | * @class: IO priority class |
Tejun Heo | bd056d7 | 2006-11-14 22:47:10 +0900 | [diff] [blame] | 668 | * |
| 669 | * LOCKING: |
| 670 | * None. |
| 671 | * |
| 672 | * Build ATA taskfile @tf for read/write request described by |
| 673 | * @block, @n_block, @tf_flags and @tag on @dev. |
| 674 | * |
| 675 | * RETURNS: |
| 676 | * |
| 677 | * 0 on success, -ERANGE if the request is too large for @dev, |
| 678 | * -EINVAL if the request is invalid. |
| 679 | */ |
| 680 | int ata_build_rw_tf(struct ata_taskfile *tf, struct ata_device *dev, |
| 681 | u64 block, u32 n_block, unsigned int tf_flags, |
Adam Manzanares | 8e06178 | 2016-10-17 11:27:29 -0700 | [diff] [blame] | 682 | unsigned int tag, int class) |
Tejun Heo | bd056d7 | 2006-11-14 22:47:10 +0900 | [diff] [blame] | 683 | { |
| 684 | tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE; |
| 685 | tf->flags |= tf_flags; |
| 686 | |
Jens Axboe | 2e2cc67 | 2018-05-11 12:51:06 -0600 | [diff] [blame] | 687 | if (ata_ncq_enabled(dev) && !ata_tag_internal(tag)) { |
Tejun Heo | bd056d7 | 2006-11-14 22:47:10 +0900 | [diff] [blame] | 688 | /* yay, NCQ */ |
| 689 | if (!lba_48_ok(block, n_block)) |
| 690 | return -ERANGE; |
| 691 | |
| 692 | tf->protocol = ATA_PROT_NCQ; |
| 693 | tf->flags |= ATA_TFLAG_LBA | ATA_TFLAG_LBA48; |
| 694 | |
| 695 | if (tf->flags & ATA_TFLAG_WRITE) |
| 696 | tf->command = ATA_CMD_FPDMA_WRITE; |
| 697 | else |
| 698 | tf->command = ATA_CMD_FPDMA_READ; |
| 699 | |
| 700 | tf->nsect = tag << 3; |
| 701 | tf->hob_feature = (n_block >> 8) & 0xff; |
| 702 | tf->feature = n_block & 0xff; |
| 703 | |
| 704 | tf->hob_lbah = (block >> 40) & 0xff; |
| 705 | tf->hob_lbam = (block >> 32) & 0xff; |
| 706 | tf->hob_lbal = (block >> 24) & 0xff; |
| 707 | tf->lbah = (block >> 16) & 0xff; |
| 708 | tf->lbam = (block >> 8) & 0xff; |
| 709 | tf->lbal = block & 0xff; |
| 710 | |
Sergei Shtylyov | 9ca7cfa | 2012-08-21 22:18:50 +0400 | [diff] [blame] | 711 | tf->device = ATA_LBA; |
Tejun Heo | bd056d7 | 2006-11-14 22:47:10 +0900 | [diff] [blame] | 712 | if (tf->flags & ATA_TFLAG_FUA) |
| 713 | tf->device |= 1 << 7; |
Adam Manzanares | 8e06178 | 2016-10-17 11:27:29 -0700 | [diff] [blame] | 714 | |
Damien Le Moal | 2360fa1 | 2021-08-16 10:44:51 +0900 | [diff] [blame] | 715 | if (dev->flags & ATA_DFLAG_NCQ_PRIO_ENABLE && |
| 716 | class == IOPRIO_CLASS_RT) |
| 717 | tf->hob_nsect |= ATA_PRIO_HIGH << ATA_SHIFT_PRIO; |
Tejun Heo | bd056d7 | 2006-11-14 22:47:10 +0900 | [diff] [blame] | 718 | } else if (dev->flags & ATA_DFLAG_LBA) { |
| 719 | tf->flags |= ATA_TFLAG_LBA; |
| 720 | |
| 721 | if (lba_28_ok(block, n_block)) { |
| 722 | /* use LBA28 */ |
| 723 | tf->device |= (block >> 24) & 0xf; |
| 724 | } else if (lba_48_ok(block, n_block)) { |
| 725 | if (!(dev->flags & ATA_DFLAG_LBA48)) |
| 726 | return -ERANGE; |
| 727 | |
| 728 | /* use LBA48 */ |
| 729 | tf->flags |= ATA_TFLAG_LBA48; |
| 730 | |
| 731 | tf->hob_nsect = (n_block >> 8) & 0xff; |
| 732 | |
| 733 | tf->hob_lbah = (block >> 40) & 0xff; |
| 734 | tf->hob_lbam = (block >> 32) & 0xff; |
| 735 | tf->hob_lbal = (block >> 24) & 0xff; |
| 736 | } else |
| 737 | /* request too large even for LBA48 */ |
| 738 | return -ERANGE; |
| 739 | |
| 740 | if (unlikely(ata_rwcmd_protocol(tf, dev) < 0)) |
| 741 | return -EINVAL; |
| 742 | |
| 743 | tf->nsect = n_block & 0xff; |
| 744 | |
| 745 | tf->lbah = (block >> 16) & 0xff; |
| 746 | tf->lbam = (block >> 8) & 0xff; |
| 747 | tf->lbal = block & 0xff; |
| 748 | |
| 749 | tf->device |= ATA_LBA; |
| 750 | } else { |
| 751 | /* CHS */ |
| 752 | u32 sect, head, cyl, track; |
| 753 | |
| 754 | /* The request -may- be too large for CHS addressing. */ |
| 755 | if (!lba_28_ok(block, n_block)) |
| 756 | return -ERANGE; |
| 757 | |
| 758 | if (unlikely(ata_rwcmd_protocol(tf, dev) < 0)) |
| 759 | return -EINVAL; |
| 760 | |
| 761 | /* Convert LBA to CHS */ |
| 762 | track = (u32)block / dev->sectors; |
| 763 | cyl = track / dev->heads; |
| 764 | head = track % dev->heads; |
| 765 | sect = (u32)block % dev->sectors + 1; |
| 766 | |
Tejun Heo | bd056d7 | 2006-11-14 22:47:10 +0900 | [diff] [blame] | 767 | /* Check whether the converted CHS can fit. |
| 768 | Cylinder: 0-65535 |
| 769 | Head: 0-15 |
| 770 | Sector: 1-255*/ |
| 771 | if ((cyl >> 16) || (head >> 4) || (sect >> 8) || (!sect)) |
| 772 | return -ERANGE; |
| 773 | |
| 774 | tf->nsect = n_block & 0xff; /* Sector count 0 means 256 sectors */ |
| 775 | tf->lbal = sect; |
| 776 | tf->lbam = cyl; |
| 777 | tf->lbah = cyl >> 8; |
| 778 | tf->device |= head; |
| 779 | } |
| 780 | |
| 781 | return 0; |
| 782 | } |
| 783 | |
| 784 | /** |
Tejun Heo | cb95d56 | 2006-03-06 04:31:56 +0900 | [diff] [blame] | 785 | * ata_pack_xfermask - Pack pio, mwdma and udma masks into xfer_mask |
| 786 | * @pio_mask: pio_mask |
| 787 | * @mwdma_mask: mwdma_mask |
| 788 | * @udma_mask: udma_mask |
| 789 | * |
| 790 | * Pack @pio_mask, @mwdma_mask and @udma_mask into a single |
| 791 | * unsigned int xfer_mask. |
| 792 | * |
| 793 | * LOCKING: |
| 794 | * None. |
| 795 | * |
| 796 | * RETURNS: |
| 797 | * Packed xfer_mask. |
| 798 | */ |
Tejun Heo | 7dc951a | 2007-11-27 19:43:42 +0900 | [diff] [blame] | 799 | unsigned long ata_pack_xfermask(unsigned long pio_mask, |
| 800 | unsigned long mwdma_mask, |
| 801 | unsigned long udma_mask) |
Tejun Heo | cb95d56 | 2006-03-06 04:31:56 +0900 | [diff] [blame] | 802 | { |
| 803 | return ((pio_mask << ATA_SHIFT_PIO) & ATA_MASK_PIO) | |
| 804 | ((mwdma_mask << ATA_SHIFT_MWDMA) & ATA_MASK_MWDMA) | |
| 805 | ((udma_mask << ATA_SHIFT_UDMA) & ATA_MASK_UDMA); |
| 806 | } |
Bartlomiej Zolnierkiewicz | a52fbcf | 2020-03-26 16:58:04 +0100 | [diff] [blame] | 807 | EXPORT_SYMBOL_GPL(ata_pack_xfermask); |
Tejun Heo | cb95d56 | 2006-03-06 04:31:56 +0900 | [diff] [blame] | 808 | |
Tejun Heo | c0489e4 | 2006-03-24 14:07:49 +0900 | [diff] [blame] | 809 | /** |
| 810 | * ata_unpack_xfermask - Unpack xfer_mask into pio, mwdma and udma masks |
| 811 | * @xfer_mask: xfer_mask to unpack |
| 812 | * @pio_mask: resulting pio_mask |
| 813 | * @mwdma_mask: resulting mwdma_mask |
| 814 | * @udma_mask: resulting udma_mask |
| 815 | * |
| 816 | * Unpack @xfer_mask into @pio_mask, @mwdma_mask and @udma_mask. |
Masanari Iida | c9b5560 | 2016-04-13 23:36:27 +0900 | [diff] [blame] | 817 | * Any NULL destination masks will be ignored. |
Tejun Heo | c0489e4 | 2006-03-24 14:07:49 +0900 | [diff] [blame] | 818 | */ |
Tejun Heo | 7dc951a | 2007-11-27 19:43:42 +0900 | [diff] [blame] | 819 | void ata_unpack_xfermask(unsigned long xfer_mask, unsigned long *pio_mask, |
| 820 | unsigned long *mwdma_mask, unsigned long *udma_mask) |
Tejun Heo | c0489e4 | 2006-03-24 14:07:49 +0900 | [diff] [blame] | 821 | { |
| 822 | if (pio_mask) |
| 823 | *pio_mask = (xfer_mask & ATA_MASK_PIO) >> ATA_SHIFT_PIO; |
| 824 | if (mwdma_mask) |
| 825 | *mwdma_mask = (xfer_mask & ATA_MASK_MWDMA) >> ATA_SHIFT_MWDMA; |
| 826 | if (udma_mask) |
| 827 | *udma_mask = (xfer_mask & ATA_MASK_UDMA) >> ATA_SHIFT_UDMA; |
| 828 | } |
| 829 | |
Tejun Heo | cb95d56 | 2006-03-06 04:31:56 +0900 | [diff] [blame] | 830 | static const struct ata_xfer_ent { |
Tejun Heo | be9a50c | 2006-03-31 22:48:52 +0900 | [diff] [blame] | 831 | int shift, bits; |
Tejun Heo | cb95d56 | 2006-03-06 04:31:56 +0900 | [diff] [blame] | 832 | u8 base; |
| 833 | } ata_xfer_tbl[] = { |
Tejun Heo | 70cd071 | 2007-11-27 19:43:40 +0900 | [diff] [blame] | 834 | { ATA_SHIFT_PIO, ATA_NR_PIO_MODES, XFER_PIO_0 }, |
| 835 | { ATA_SHIFT_MWDMA, ATA_NR_MWDMA_MODES, XFER_MW_DMA_0 }, |
| 836 | { ATA_SHIFT_UDMA, ATA_NR_UDMA_MODES, XFER_UDMA_0 }, |
Tejun Heo | cb95d56 | 2006-03-06 04:31:56 +0900 | [diff] [blame] | 837 | { -1, }, |
| 838 | }; |
| 839 | |
| 840 | /** |
| 841 | * ata_xfer_mask2mode - Find matching XFER_* for the given xfer_mask |
| 842 | * @xfer_mask: xfer_mask of interest |
| 843 | * |
| 844 | * Return matching XFER_* value for @xfer_mask. Only the highest |
| 845 | * bit of @xfer_mask is considered. |
| 846 | * |
| 847 | * LOCKING: |
| 848 | * None. |
| 849 | * |
| 850 | * RETURNS: |
Tejun Heo | 70cd071 | 2007-11-27 19:43:40 +0900 | [diff] [blame] | 851 | * Matching XFER_* value, 0xff if no match found. |
Tejun Heo | cb95d56 | 2006-03-06 04:31:56 +0900 | [diff] [blame] | 852 | */ |
Tejun Heo | 7dc951a | 2007-11-27 19:43:42 +0900 | [diff] [blame] | 853 | u8 ata_xfer_mask2mode(unsigned long xfer_mask) |
Tejun Heo | cb95d56 | 2006-03-06 04:31:56 +0900 | [diff] [blame] | 854 | { |
| 855 | int highbit = fls(xfer_mask) - 1; |
| 856 | const struct ata_xfer_ent *ent; |
| 857 | |
| 858 | for (ent = ata_xfer_tbl; ent->shift >= 0; ent++) |
| 859 | if (highbit >= ent->shift && highbit < ent->shift + ent->bits) |
| 860 | return ent->base + highbit - ent->shift; |
Tejun Heo | 70cd071 | 2007-11-27 19:43:40 +0900 | [diff] [blame] | 861 | return 0xff; |
Tejun Heo | cb95d56 | 2006-03-06 04:31:56 +0900 | [diff] [blame] | 862 | } |
Bartlomiej Zolnierkiewicz | a52fbcf | 2020-03-26 16:58:04 +0100 | [diff] [blame] | 863 | EXPORT_SYMBOL_GPL(ata_xfer_mask2mode); |
Tejun Heo | cb95d56 | 2006-03-06 04:31:56 +0900 | [diff] [blame] | 864 | |
| 865 | /** |
| 866 | * ata_xfer_mode2mask - Find matching xfer_mask for XFER_* |
| 867 | * @xfer_mode: XFER_* of interest |
| 868 | * |
| 869 | * Return matching xfer_mask for @xfer_mode. |
| 870 | * |
| 871 | * LOCKING: |
| 872 | * None. |
| 873 | * |
| 874 | * RETURNS: |
| 875 | * Matching xfer_mask, 0 if no match found. |
| 876 | */ |
Tejun Heo | 7dc951a | 2007-11-27 19:43:42 +0900 | [diff] [blame] | 877 | unsigned long ata_xfer_mode2mask(u8 xfer_mode) |
Tejun Heo | cb95d56 | 2006-03-06 04:31:56 +0900 | [diff] [blame] | 878 | { |
| 879 | const struct ata_xfer_ent *ent; |
| 880 | |
| 881 | for (ent = ata_xfer_tbl; ent->shift >= 0; ent++) |
| 882 | if (xfer_mode >= ent->base && xfer_mode < ent->base + ent->bits) |
Tejun Heo | 70cd071 | 2007-11-27 19:43:40 +0900 | [diff] [blame] | 883 | return ((2 << (ent->shift + xfer_mode - ent->base)) - 1) |
| 884 | & ~((1 << ent->shift) - 1); |
Tejun Heo | cb95d56 | 2006-03-06 04:31:56 +0900 | [diff] [blame] | 885 | return 0; |
| 886 | } |
Bartlomiej Zolnierkiewicz | a52fbcf | 2020-03-26 16:58:04 +0100 | [diff] [blame] | 887 | EXPORT_SYMBOL_GPL(ata_xfer_mode2mask); |
Tejun Heo | cb95d56 | 2006-03-06 04:31:56 +0900 | [diff] [blame] | 888 | |
| 889 | /** |
| 890 | * ata_xfer_mode2shift - Find matching xfer_shift for XFER_* |
| 891 | * @xfer_mode: XFER_* of interest |
| 892 | * |
| 893 | * Return matching xfer_shift for @xfer_mode. |
| 894 | * |
| 895 | * LOCKING: |
| 896 | * None. |
| 897 | * |
| 898 | * RETURNS: |
| 899 | * Matching xfer_shift, -1 if no match found. |
| 900 | */ |
Tejun Heo | 7dc951a | 2007-11-27 19:43:42 +0900 | [diff] [blame] | 901 | int ata_xfer_mode2shift(unsigned long xfer_mode) |
Tejun Heo | cb95d56 | 2006-03-06 04:31:56 +0900 | [diff] [blame] | 902 | { |
| 903 | const struct ata_xfer_ent *ent; |
| 904 | |
| 905 | for (ent = ata_xfer_tbl; ent->shift >= 0; ent++) |
| 906 | if (xfer_mode >= ent->base && xfer_mode < ent->base + ent->bits) |
| 907 | return ent->shift; |
| 908 | return -1; |
| 909 | } |
Bartlomiej Zolnierkiewicz | a52fbcf | 2020-03-26 16:58:04 +0100 | [diff] [blame] | 910 | EXPORT_SYMBOL_GPL(ata_xfer_mode2shift); |
Tejun Heo | cb95d56 | 2006-03-06 04:31:56 +0900 | [diff] [blame] | 911 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 912 | /** |
Tejun Heo | 1da7b0d | 2006-03-06 04:31:56 +0900 | [diff] [blame] | 913 | * ata_mode_string - convert xfer_mask to string |
| 914 | * @xfer_mask: mask of bits supported; only highest bit counts. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 915 | * |
| 916 | * Determine string which represents the highest speed |
Tejun Heo | 1da7b0d | 2006-03-06 04:31:56 +0900 | [diff] [blame] | 917 | * (highest bit in @modemask). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 918 | * |
| 919 | * LOCKING: |
| 920 | * None. |
| 921 | * |
| 922 | * RETURNS: |
| 923 | * Constant C string representing highest speed listed in |
Tejun Heo | 1da7b0d | 2006-03-06 04:31:56 +0900 | [diff] [blame] | 924 | * @mode_mask, or the constant C string "<n/a>". |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 925 | */ |
Tejun Heo | 7dc951a | 2007-11-27 19:43:42 +0900 | [diff] [blame] | 926 | const char *ata_mode_string(unsigned long xfer_mask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 927 | { |
Tejun Heo | 75f554b | 2006-03-06 04:31:57 +0900 | [diff] [blame] | 928 | static const char * const xfer_mode_str[] = { |
| 929 | "PIO0", |
| 930 | "PIO1", |
| 931 | "PIO2", |
| 932 | "PIO3", |
| 933 | "PIO4", |
Alan Cox | b352e57 | 2006-08-10 18:52:12 +0100 | [diff] [blame] | 934 | "PIO5", |
| 935 | "PIO6", |
Tejun Heo | 75f554b | 2006-03-06 04:31:57 +0900 | [diff] [blame] | 936 | "MWDMA0", |
| 937 | "MWDMA1", |
| 938 | "MWDMA2", |
Alan Cox | b352e57 | 2006-08-10 18:52:12 +0100 | [diff] [blame] | 939 | "MWDMA3", |
| 940 | "MWDMA4", |
Tejun Heo | 75f554b | 2006-03-06 04:31:57 +0900 | [diff] [blame] | 941 | "UDMA/16", |
| 942 | "UDMA/25", |
| 943 | "UDMA/33", |
| 944 | "UDMA/44", |
| 945 | "UDMA/66", |
| 946 | "UDMA/100", |
| 947 | "UDMA/133", |
| 948 | "UDMA7", |
| 949 | }; |
Tejun Heo | 1da7b0d | 2006-03-06 04:31:56 +0900 | [diff] [blame] | 950 | int highbit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 951 | |
Tejun Heo | 1da7b0d | 2006-03-06 04:31:56 +0900 | [diff] [blame] | 952 | highbit = fls(xfer_mask) - 1; |
| 953 | if (highbit >= 0 && highbit < ARRAY_SIZE(xfer_mode_str)) |
| 954 | return xfer_mode_str[highbit]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 955 | return "<n/a>"; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 956 | } |
Bartlomiej Zolnierkiewicz | a52fbcf | 2020-03-26 16:58:04 +0100 | [diff] [blame] | 957 | EXPORT_SYMBOL_GPL(ata_mode_string); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 958 | |
Gwendal Grignou | d902747 | 2010-05-25 12:31:38 -0700 | [diff] [blame] | 959 | const char *sata_spd_string(unsigned int spd) |
Tejun Heo | 4c360c8 | 2006-04-01 01:38:17 +0900 | [diff] [blame] | 960 | { |
| 961 | static const char * const spd_str[] = { |
| 962 | "1.5 Gbps", |
| 963 | "3.0 Gbps", |
Shane Huang | 8522ee2 | 2008-12-30 11:00:37 +0800 | [diff] [blame] | 964 | "6.0 Gbps", |
Tejun Heo | 4c360c8 | 2006-04-01 01:38:17 +0900 | [diff] [blame] | 965 | }; |
| 966 | |
| 967 | if (spd == 0 || (spd - 1) >= ARRAY_SIZE(spd_str)) |
| 968 | return "<unknown>"; |
| 969 | return spd_str[spd - 1]; |
| 970 | } |
| 971 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 972 | /** |
| 973 | * ata_dev_classify - determine device type based on ATA-spec signature |
| 974 | * @tf: ATA taskfile register set for device to be identified |
| 975 | * |
| 976 | * Determine from taskfile register contents whether a device is |
| 977 | * ATA or ATAPI, as per "Signature and persistence" section |
| 978 | * of ATA/PI spec (volume 1, sect 5.14). |
| 979 | * |
| 980 | * LOCKING: |
| 981 | * None. |
| 982 | * |
| 983 | * RETURNS: |
Hannes Reinecke | 9162c65 | 2014-11-05 13:08:21 +0100 | [diff] [blame] | 984 | * Device type, %ATA_DEV_ATA, %ATA_DEV_ATAPI, %ATA_DEV_PMP, |
| 985 | * %ATA_DEV_ZAC, or %ATA_DEV_UNKNOWN the event of failure. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 986 | */ |
Jeff Garzik | 057ace5 | 2005-10-22 14:27:05 -0400 | [diff] [blame] | 987 | unsigned int ata_dev_classify(const struct ata_taskfile *tf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 988 | { |
| 989 | /* Apple's open source Darwin code hints that some devices only |
| 990 | * put a proper signature into the LBA mid/high registers, |
| 991 | * So, we only check those. It's sufficient for uniqueness. |
Tejun Heo | 633273a | 2007-09-23 13:19:54 +0900 | [diff] [blame] | 992 | * |
| 993 | * ATA/ATAPI-7 (d1532v1r1: Feb. 19, 2003) specified separate |
| 994 | * signatures for ATA and ATAPI devices attached on SerialATA, |
| 995 | * 0x3c/0xc3 and 0x69/0x96 respectively. However, SerialATA |
| 996 | * spec has never mentioned about using different signatures |
| 997 | * for ATA/ATAPI devices. Then, Serial ATA II: Port |
| 998 | * Multiplier specification began to use 0x69/0x96 to identify |
| 999 | * port multpliers and 0x3c/0xc3 to identify SEMB device. |
| 1000 | * ATA/ATAPI-7 dropped descriptions about 0x3c/0xc3 and |
| 1001 | * 0x69/0x96 shortly and described them as reserved for |
| 1002 | * SerialATA. |
| 1003 | * |
| 1004 | * We follow the current spec and consider that 0x69/0x96 |
| 1005 | * identifies a port multiplier and 0x3c/0xc3 a SEMB device. |
Tejun Heo | 79b42ba | 2009-04-15 06:21:10 +0900 | [diff] [blame] | 1006 | * Unfortunately, WDC WD1600JS-62MHB5 (a hard drive) reports |
| 1007 | * SEMB signature. This is worked around in |
| 1008 | * ata_dev_read_id(). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1009 | */ |
Hannes Reinecke | 6c952a0 | 2021-12-21 08:20:26 +0100 | [diff] [blame] | 1010 | if (tf->lbam == 0 && tf->lbah == 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1011 | return ATA_DEV_ATA; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1012 | |
Hannes Reinecke | 6c952a0 | 2021-12-21 08:20:26 +0100 | [diff] [blame] | 1013 | if (tf->lbam == 0x14 && tf->lbah == 0xeb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1014 | return ATA_DEV_ATAPI; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1015 | |
Hannes Reinecke | 6c952a0 | 2021-12-21 08:20:26 +0100 | [diff] [blame] | 1016 | if (tf->lbam == 0x69 && tf->lbah == 0x96) |
Tejun Heo | 633273a | 2007-09-23 13:19:54 +0900 | [diff] [blame] | 1017 | return ATA_DEV_PMP; |
Tejun Heo | 633273a | 2007-09-23 13:19:54 +0900 | [diff] [blame] | 1018 | |
Hannes Reinecke | 6c952a0 | 2021-12-21 08:20:26 +0100 | [diff] [blame] | 1019 | if (tf->lbam == 0x3c && tf->lbah == 0xc3) |
Tejun Heo | 79b42ba | 2009-04-15 06:21:10 +0900 | [diff] [blame] | 1020 | return ATA_DEV_SEMB; |
Tejun Heo | 633273a | 2007-09-23 13:19:54 +0900 | [diff] [blame] | 1021 | |
Hannes Reinecke | 6c952a0 | 2021-12-21 08:20:26 +0100 | [diff] [blame] | 1022 | if (tf->lbam == 0xcd && tf->lbah == 0xab) |
Hannes Reinecke | 9162c65 | 2014-11-05 13:08:21 +0100 | [diff] [blame] | 1023 | return ATA_DEV_ZAC; |
Hannes Reinecke | 9162c65 | 2014-11-05 13:08:21 +0100 | [diff] [blame] | 1024 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1025 | return ATA_DEV_UNKNOWN; |
| 1026 | } |
Bartlomiej Zolnierkiewicz | a52fbcf | 2020-03-26 16:58:04 +0100 | [diff] [blame] | 1027 | EXPORT_SYMBOL_GPL(ata_dev_classify); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1028 | |
| 1029 | /** |
Tejun Heo | 6a62a04 | 2006-02-13 10:02:46 +0900 | [diff] [blame] | 1030 | * ata_id_string - Convert IDENTIFY DEVICE page into string |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1031 | * @id: IDENTIFY DEVICE results we will examine |
| 1032 | * @s: string into which data is output |
| 1033 | * @ofs: offset into identify device page |
| 1034 | * @len: length of string to return. must be an even number. |
| 1035 | * |
| 1036 | * The strings in the IDENTIFY DEVICE page are broken up into |
| 1037 | * 16-bit chunks. Run through the string, and output each |
| 1038 | * 8-bit chunk linearly, regardless of platform. |
| 1039 | * |
| 1040 | * LOCKING: |
| 1041 | * caller. |
| 1042 | */ |
| 1043 | |
Tejun Heo | 6a62a04 | 2006-02-13 10:02:46 +0900 | [diff] [blame] | 1044 | void ata_id_string(const u16 *id, unsigned char *s, |
| 1045 | unsigned int ofs, unsigned int len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1046 | { |
| 1047 | unsigned int c; |
| 1048 | |
Alan Cox | 963e497 | 2008-07-24 17:16:06 +0100 | [diff] [blame] | 1049 | BUG_ON(len & 1); |
| 1050 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1051 | while (len > 0) { |
| 1052 | c = id[ofs] >> 8; |
| 1053 | *s = c; |
| 1054 | s++; |
| 1055 | |
| 1056 | c = id[ofs] & 0xff; |
| 1057 | *s = c; |
| 1058 | s++; |
| 1059 | |
| 1060 | ofs++; |
| 1061 | len -= 2; |
| 1062 | } |
| 1063 | } |
Bartlomiej Zolnierkiewicz | a52fbcf | 2020-03-26 16:58:04 +0100 | [diff] [blame] | 1064 | EXPORT_SYMBOL_GPL(ata_id_string); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1065 | |
Tejun Heo | 0e949ff | 2006-02-12 22:47:04 +0900 | [diff] [blame] | 1066 | /** |
Tejun Heo | 6a62a04 | 2006-02-13 10:02:46 +0900 | [diff] [blame] | 1067 | * ata_id_c_string - Convert IDENTIFY DEVICE page into C string |
Tejun Heo | 0e949ff | 2006-02-12 22:47:04 +0900 | [diff] [blame] | 1068 | * @id: IDENTIFY DEVICE results we will examine |
| 1069 | * @s: string into which data is output |
| 1070 | * @ofs: offset into identify device page |
| 1071 | * @len: length of string to return. must be an odd number. |
| 1072 | * |
Tejun Heo | 6a62a04 | 2006-02-13 10:02:46 +0900 | [diff] [blame] | 1073 | * This function is identical to ata_id_string except that it |
Tejun Heo | 0e949ff | 2006-02-12 22:47:04 +0900 | [diff] [blame] | 1074 | * trims trailing spaces and terminates the resulting string with |
| 1075 | * null. @len must be actual maximum length (even number) + 1. |
| 1076 | * |
| 1077 | * LOCKING: |
| 1078 | * caller. |
| 1079 | */ |
Tejun Heo | 6a62a04 | 2006-02-13 10:02:46 +0900 | [diff] [blame] | 1080 | void ata_id_c_string(const u16 *id, unsigned char *s, |
| 1081 | unsigned int ofs, unsigned int len) |
Tejun Heo | 0e949ff | 2006-02-12 22:47:04 +0900 | [diff] [blame] | 1082 | { |
| 1083 | unsigned char *p; |
| 1084 | |
Tejun Heo | 6a62a04 | 2006-02-13 10:02:46 +0900 | [diff] [blame] | 1085 | ata_id_string(id, s, ofs, len - 1); |
Tejun Heo | 0e949ff | 2006-02-12 22:47:04 +0900 | [diff] [blame] | 1086 | |
| 1087 | p = s + strnlen(s, len - 1); |
| 1088 | while (p > s && p[-1] == ' ') |
| 1089 | p--; |
| 1090 | *p = '\0'; |
| 1091 | } |
Bartlomiej Zolnierkiewicz | a52fbcf | 2020-03-26 16:58:04 +0100 | [diff] [blame] | 1092 | EXPORT_SYMBOL_GPL(ata_id_c_string); |
Edward Falk | 0baab86 | 2005-06-02 18:17:13 -0400 | [diff] [blame] | 1093 | |
Tejun Heo | db6f875 | 2007-09-03 12:31:58 +0900 | [diff] [blame] | 1094 | static u64 ata_id_n_sectors(const u16 *id) |
| 1095 | { |
| 1096 | if (ata_id_has_lba(id)) { |
| 1097 | if (ata_id_has_lba48(id)) |
Robert Hancock | 968e594 | 2009-02-16 20:15:08 -0600 | [diff] [blame] | 1098 | return ata_id_u64(id, ATA_ID_LBA_CAPACITY_2); |
Tejun Heo | db6f875 | 2007-09-03 12:31:58 +0900 | [diff] [blame] | 1099 | else |
Robert Hancock | 968e594 | 2009-02-16 20:15:08 -0600 | [diff] [blame] | 1100 | return ata_id_u32(id, ATA_ID_LBA_CAPACITY); |
Tejun Heo | db6f875 | 2007-09-03 12:31:58 +0900 | [diff] [blame] | 1101 | } else { |
| 1102 | if (ata_id_current_chs_valid(id)) |
Robert Hancock | 968e594 | 2009-02-16 20:15:08 -0600 | [diff] [blame] | 1103 | return id[ATA_ID_CUR_CYLS] * id[ATA_ID_CUR_HEADS] * |
| 1104 | id[ATA_ID_CUR_SECTORS]; |
Tejun Heo | db6f875 | 2007-09-03 12:31:58 +0900 | [diff] [blame] | 1105 | else |
Robert Hancock | 968e594 | 2009-02-16 20:15:08 -0600 | [diff] [blame] | 1106 | return id[ATA_ID_CYLS] * id[ATA_ID_HEADS] * |
| 1107 | id[ATA_ID_SECTORS]; |
Tejun Heo | db6f875 | 2007-09-03 12:31:58 +0900 | [diff] [blame] | 1108 | } |
| 1109 | } |
| 1110 | |
Tejun Heo | a5987e0 | 2008-03-27 19:14:23 +0900 | [diff] [blame] | 1111 | u64 ata_tf_to_lba48(const struct ata_taskfile *tf) |
Alan Cox | 1e99973 | 2007-04-11 00:23:13 +0100 | [diff] [blame] | 1112 | { |
| 1113 | u64 sectors = 0; |
| 1114 | |
| 1115 | sectors |= ((u64)(tf->hob_lbah & 0xff)) << 40; |
| 1116 | sectors |= ((u64)(tf->hob_lbam & 0xff)) << 32; |
Roland Dreier | ba14a9c | 2008-10-28 16:52:20 -0700 | [diff] [blame] | 1117 | sectors |= ((u64)(tf->hob_lbal & 0xff)) << 24; |
Alan Cox | 1e99973 | 2007-04-11 00:23:13 +0100 | [diff] [blame] | 1118 | sectors |= (tf->lbah & 0xff) << 16; |
| 1119 | sectors |= (tf->lbam & 0xff) << 8; |
| 1120 | sectors |= (tf->lbal & 0xff); |
| 1121 | |
Tejun Heo | a5987e0 | 2008-03-27 19:14:23 +0900 | [diff] [blame] | 1122 | return sectors; |
Alan Cox | 1e99973 | 2007-04-11 00:23:13 +0100 | [diff] [blame] | 1123 | } |
| 1124 | |
Tejun Heo | a5987e0 | 2008-03-27 19:14:23 +0900 | [diff] [blame] | 1125 | u64 ata_tf_to_lba(const struct ata_taskfile *tf) |
Alan Cox | 1e99973 | 2007-04-11 00:23:13 +0100 | [diff] [blame] | 1126 | { |
| 1127 | u64 sectors = 0; |
| 1128 | |
| 1129 | sectors |= (tf->device & 0x0f) << 24; |
| 1130 | sectors |= (tf->lbah & 0xff) << 16; |
| 1131 | sectors |= (tf->lbam & 0xff) << 8; |
| 1132 | sectors |= (tf->lbal & 0xff); |
| 1133 | |
Tejun Heo | a5987e0 | 2008-03-27 19:14:23 +0900 | [diff] [blame] | 1134 | return sectors; |
Alan Cox | 1e99973 | 2007-04-11 00:23:13 +0100 | [diff] [blame] | 1135 | } |
| 1136 | |
| 1137 | /** |
Tejun Heo | c728a91 | 2007-09-03 12:32:30 +0900 | [diff] [blame] | 1138 | * ata_read_native_max_address - Read native max address |
| 1139 | * @dev: target device |
| 1140 | * @max_sectors: out parameter for the result native max address |
Alan Cox | 1e99973 | 2007-04-11 00:23:13 +0100 | [diff] [blame] | 1141 | * |
Tejun Heo | c728a91 | 2007-09-03 12:32:30 +0900 | [diff] [blame] | 1142 | * Perform an LBA48 or LBA28 native size query upon the device in |
| 1143 | * question. |
| 1144 | * |
| 1145 | * RETURNS: |
| 1146 | * 0 on success, -EACCES if command is aborted by the drive. |
| 1147 | * -EIO on other errors. |
Alan Cox | 1e99973 | 2007-04-11 00:23:13 +0100 | [diff] [blame] | 1148 | */ |
Tejun Heo | c728a91 | 2007-09-03 12:32:30 +0900 | [diff] [blame] | 1149 | static int ata_read_native_max_address(struct ata_device *dev, u64 *max_sectors) |
Alan Cox | 1e99973 | 2007-04-11 00:23:13 +0100 | [diff] [blame] | 1150 | { |
Tejun Heo | c728a91 | 2007-09-03 12:32:30 +0900 | [diff] [blame] | 1151 | unsigned int err_mask; |
Alan Cox | 1e99973 | 2007-04-11 00:23:13 +0100 | [diff] [blame] | 1152 | struct ata_taskfile tf; |
Tejun Heo | c728a91 | 2007-09-03 12:32:30 +0900 | [diff] [blame] | 1153 | int lba48 = ata_id_has_lba48(dev->id); |
Alan Cox | 1e99973 | 2007-04-11 00:23:13 +0100 | [diff] [blame] | 1154 | |
| 1155 | ata_tf_init(dev, &tf); |
| 1156 | |
Tejun Heo | c728a91 | 2007-09-03 12:32:30 +0900 | [diff] [blame] | 1157 | /* always clear all address registers */ |
Alan Cox | 1e99973 | 2007-04-11 00:23:13 +0100 | [diff] [blame] | 1158 | tf.flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_ISADDR; |
Tejun Heo | c728a91 | 2007-09-03 12:32:30 +0900 | [diff] [blame] | 1159 | |
| 1160 | if (lba48) { |
| 1161 | tf.command = ATA_CMD_READ_NATIVE_MAX_EXT; |
| 1162 | tf.flags |= ATA_TFLAG_LBA48; |
| 1163 | } else |
| 1164 | tf.command = ATA_CMD_READ_NATIVE_MAX; |
| 1165 | |
Hannes Reinecke | bd18bc0 | 2016-07-14 09:05:46 +0900 | [diff] [blame] | 1166 | tf.protocol = ATA_PROT_NODATA; |
Tejun Heo | c728a91 | 2007-09-03 12:32:30 +0900 | [diff] [blame] | 1167 | tf.device |= ATA_LBA; |
Alan Cox | 1e99973 | 2007-04-11 00:23:13 +0100 | [diff] [blame] | 1168 | |
Tejun Heo | 2b78910 | 2007-10-09 15:05:44 +0900 | [diff] [blame] | 1169 | err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0); |
Tejun Heo | c728a91 | 2007-09-03 12:32:30 +0900 | [diff] [blame] | 1170 | if (err_mask) { |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 1171 | ata_dev_warn(dev, |
| 1172 | "failed to read native max address (err_mask=0x%x)\n", |
| 1173 | err_mask); |
Tejun Heo | c728a91 | 2007-09-03 12:32:30 +0900 | [diff] [blame] | 1174 | if (err_mask == AC_ERR_DEV && (tf.feature & ATA_ABORTED)) |
| 1175 | return -EACCES; |
| 1176 | return -EIO; |
| 1177 | } |
Alan Cox | 1e99973 | 2007-04-11 00:23:13 +0100 | [diff] [blame] | 1178 | |
Tejun Heo | c728a91 | 2007-09-03 12:32:30 +0900 | [diff] [blame] | 1179 | if (lba48) |
Tejun Heo | a5987e0 | 2008-03-27 19:14:23 +0900 | [diff] [blame] | 1180 | *max_sectors = ata_tf_to_lba48(&tf) + 1; |
Tejun Heo | c728a91 | 2007-09-03 12:32:30 +0900 | [diff] [blame] | 1181 | else |
Tejun Heo | a5987e0 | 2008-03-27 19:14:23 +0900 | [diff] [blame] | 1182 | *max_sectors = ata_tf_to_lba(&tf) + 1; |
Jeff Garzik | 2dcb407 | 2007-10-19 06:42:56 -0400 | [diff] [blame] | 1183 | if (dev->horkage & ATA_HORKAGE_HPA_SIZE) |
Alan Cox | 93328e11 | 2007-09-29 04:06:48 -0400 | [diff] [blame] | 1184 | (*max_sectors)--; |
Tejun Heo | c728a91 | 2007-09-03 12:32:30 +0900 | [diff] [blame] | 1185 | return 0; |
Alan Cox | 1e99973 | 2007-04-11 00:23:13 +0100 | [diff] [blame] | 1186 | } |
| 1187 | |
| 1188 | /** |
Tejun Heo | c728a91 | 2007-09-03 12:32:30 +0900 | [diff] [blame] | 1189 | * ata_set_max_sectors - Set max sectors |
| 1190 | * @dev: target device |
Randy Dunlap | 6b38d1d | 2007-05-01 17:35:55 -0700 | [diff] [blame] | 1191 | * @new_sectors: new max sectors value to set for the device |
Alan Cox | 1e99973 | 2007-04-11 00:23:13 +0100 | [diff] [blame] | 1192 | * |
Tejun Heo | c728a91 | 2007-09-03 12:32:30 +0900 | [diff] [blame] | 1193 | * Set max sectors of @dev to @new_sectors. |
| 1194 | * |
| 1195 | * RETURNS: |
| 1196 | * 0 on success, -EACCES if command is aborted or denied (due to |
| 1197 | * previous non-volatile SET_MAX) by the drive. -EIO on other |
| 1198 | * errors. |
Alan Cox | 1e99973 | 2007-04-11 00:23:13 +0100 | [diff] [blame] | 1199 | */ |
Tejun Heo | 05027ad | 2007-09-03 12:32:57 +0900 | [diff] [blame] | 1200 | static int ata_set_max_sectors(struct ata_device *dev, u64 new_sectors) |
Alan Cox | 1e99973 | 2007-04-11 00:23:13 +0100 | [diff] [blame] | 1201 | { |
Tejun Heo | c728a91 | 2007-09-03 12:32:30 +0900 | [diff] [blame] | 1202 | unsigned int err_mask; |
Alan Cox | 1e99973 | 2007-04-11 00:23:13 +0100 | [diff] [blame] | 1203 | struct ata_taskfile tf; |
Tejun Heo | c728a91 | 2007-09-03 12:32:30 +0900 | [diff] [blame] | 1204 | int lba48 = ata_id_has_lba48(dev->id); |
Alan Cox | 1e99973 | 2007-04-11 00:23:13 +0100 | [diff] [blame] | 1205 | |
| 1206 | new_sectors--; |
| 1207 | |
| 1208 | ata_tf_init(dev, &tf); |
| 1209 | |
Alan Cox | 1e99973 | 2007-04-11 00:23:13 +0100 | [diff] [blame] | 1210 | tf.flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_ISADDR; |
Tejun Heo | c728a91 | 2007-09-03 12:32:30 +0900 | [diff] [blame] | 1211 | |
| 1212 | if (lba48) { |
| 1213 | tf.command = ATA_CMD_SET_MAX_EXT; |
| 1214 | tf.flags |= ATA_TFLAG_LBA48; |
| 1215 | |
| 1216 | tf.hob_lbal = (new_sectors >> 24) & 0xff; |
| 1217 | tf.hob_lbam = (new_sectors >> 32) & 0xff; |
| 1218 | tf.hob_lbah = (new_sectors >> 40) & 0xff; |
Tejun Heo | 1e582ba | 2007-09-21 20:07:14 +0900 | [diff] [blame] | 1219 | } else { |
Tejun Heo | c728a91 | 2007-09-03 12:32:30 +0900 | [diff] [blame] | 1220 | tf.command = ATA_CMD_SET_MAX; |
| 1221 | |
Tejun Heo | 1e582ba | 2007-09-21 20:07:14 +0900 | [diff] [blame] | 1222 | tf.device |= (new_sectors >> 24) & 0xf; |
| 1223 | } |
| 1224 | |
Hannes Reinecke | bd18bc0 | 2016-07-14 09:05:46 +0900 | [diff] [blame] | 1225 | tf.protocol = ATA_PROT_NODATA; |
Tejun Heo | c728a91 | 2007-09-03 12:32:30 +0900 | [diff] [blame] | 1226 | tf.device |= ATA_LBA; |
Alan Cox | 1e99973 | 2007-04-11 00:23:13 +0100 | [diff] [blame] | 1227 | |
| 1228 | tf.lbal = (new_sectors >> 0) & 0xff; |
| 1229 | tf.lbam = (new_sectors >> 8) & 0xff; |
| 1230 | tf.lbah = (new_sectors >> 16) & 0xff; |
Alan Cox | 1e99973 | 2007-04-11 00:23:13 +0100 | [diff] [blame] | 1231 | |
Tejun Heo | 2b78910 | 2007-10-09 15:05:44 +0900 | [diff] [blame] | 1232 | err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0); |
Tejun Heo | c728a91 | 2007-09-03 12:32:30 +0900 | [diff] [blame] | 1233 | if (err_mask) { |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 1234 | ata_dev_warn(dev, |
| 1235 | "failed to set max address (err_mask=0x%x)\n", |
| 1236 | err_mask); |
Tejun Heo | c728a91 | 2007-09-03 12:32:30 +0900 | [diff] [blame] | 1237 | if (err_mask == AC_ERR_DEV && |
| 1238 | (tf.feature & (ATA_ABORTED | ATA_IDNF))) |
| 1239 | return -EACCES; |
| 1240 | return -EIO; |
| 1241 | } |
Alan Cox | 1e99973 | 2007-04-11 00:23:13 +0100 | [diff] [blame] | 1242 | |
Tejun Heo | c728a91 | 2007-09-03 12:32:30 +0900 | [diff] [blame] | 1243 | return 0; |
Alan Cox | 1e99973 | 2007-04-11 00:23:13 +0100 | [diff] [blame] | 1244 | } |
| 1245 | |
| 1246 | /** |
| 1247 | * ata_hpa_resize - Resize a device with an HPA set |
| 1248 | * @dev: Device to resize |
| 1249 | * |
| 1250 | * Read the size of an LBA28 or LBA48 disk with HPA features and resize |
| 1251 | * it if required to the full size of the media. The caller must check |
| 1252 | * the drive has the HPA feature set enabled. |
Tejun Heo | 05027ad | 2007-09-03 12:32:57 +0900 | [diff] [blame] | 1253 | * |
| 1254 | * RETURNS: |
| 1255 | * 0 on success, -errno on failure. |
Alan Cox | 1e99973 | 2007-04-11 00:23:13 +0100 | [diff] [blame] | 1256 | */ |
Tejun Heo | 05027ad | 2007-09-03 12:32:57 +0900 | [diff] [blame] | 1257 | static int ata_hpa_resize(struct ata_device *dev) |
Alan Cox | 1e99973 | 2007-04-11 00:23:13 +0100 | [diff] [blame] | 1258 | { |
Damien Le Moal | 891fd7c | 2021-08-16 10:44:50 +0900 | [diff] [blame] | 1259 | bool print_info = ata_dev_print_info(dev); |
Tejun Heo | 445d211 | 2010-04-05 10:33:13 +0900 | [diff] [blame] | 1260 | bool unlock_hpa = ata_ignore_hpa || dev->flags & ATA_DFLAG_UNLOCK_HPA; |
Tejun Heo | 05027ad | 2007-09-03 12:32:57 +0900 | [diff] [blame] | 1261 | u64 sectors = ata_id_n_sectors(dev->id); |
| 1262 | u64 native_sectors; |
Tejun Heo | c728a91 | 2007-09-03 12:32:30 +0900 | [diff] [blame] | 1263 | int rc; |
Jeff Garzik | a617c09 | 2007-05-21 20:14:23 -0400 | [diff] [blame] | 1264 | |
Tejun Heo | 05027ad | 2007-09-03 12:32:57 +0900 | [diff] [blame] | 1265 | /* do we need to do it? */ |
Hannes Reinecke | 9162c65 | 2014-11-05 13:08:21 +0100 | [diff] [blame] | 1266 | if ((dev->class != ATA_DEV_ATA && dev->class != ATA_DEV_ZAC) || |
Tejun Heo | 05027ad | 2007-09-03 12:32:57 +0900 | [diff] [blame] | 1267 | !ata_id_has_lba(dev->id) || !ata_id_hpa_enabled(dev->id) || |
| 1268 | (dev->horkage & ATA_HORKAGE_BROKEN_HPA)) |
Tejun Heo | c728a91 | 2007-09-03 12:32:30 +0900 | [diff] [blame] | 1269 | return 0; |
Alan Cox | 1e99973 | 2007-04-11 00:23:13 +0100 | [diff] [blame] | 1270 | |
Tejun Heo | 05027ad | 2007-09-03 12:32:57 +0900 | [diff] [blame] | 1271 | /* read native max address */ |
| 1272 | rc = ata_read_native_max_address(dev, &native_sectors); |
| 1273 | if (rc) { |
Tejun Heo | dda7aba | 2008-03-23 21:05:15 +0900 | [diff] [blame] | 1274 | /* If device aborted the command or HPA isn't going to |
| 1275 | * be unlocked, skip HPA resizing. |
Tejun Heo | 05027ad | 2007-09-03 12:32:57 +0900 | [diff] [blame] | 1276 | */ |
Tejun Heo | 445d211 | 2010-04-05 10:33:13 +0900 | [diff] [blame] | 1277 | if (rc == -EACCES || !unlock_hpa) { |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 1278 | ata_dev_warn(dev, |
| 1279 | "HPA support seems broken, skipping HPA handling\n"); |
Tejun Heo | 05027ad | 2007-09-03 12:32:57 +0900 | [diff] [blame] | 1280 | dev->horkage |= ATA_HORKAGE_BROKEN_HPA; |
Alan Cox | 1e99973 | 2007-04-11 00:23:13 +0100 | [diff] [blame] | 1281 | |
Tejun Heo | 05027ad | 2007-09-03 12:32:57 +0900 | [diff] [blame] | 1282 | /* we can continue if device aborted the command */ |
| 1283 | if (rc == -EACCES) |
| 1284 | rc = 0; |
Alan Cox | 1e99973 | 2007-04-11 00:23:13 +0100 | [diff] [blame] | 1285 | } |
Tejun Heo | 37301a5 | 2007-06-25 20:45:54 +0900 | [diff] [blame] | 1286 | |
Tejun Heo | 05027ad | 2007-09-03 12:32:57 +0900 | [diff] [blame] | 1287 | return rc; |
| 1288 | } |
Tejun Heo | 5920dad | 2009-07-15 17:11:41 +0900 | [diff] [blame] | 1289 | dev->n_native_sectors = native_sectors; |
Tejun Heo | 05027ad | 2007-09-03 12:32:57 +0900 | [diff] [blame] | 1290 | |
| 1291 | /* nothing to do? */ |
Tejun Heo | 445d211 | 2010-04-05 10:33:13 +0900 | [diff] [blame] | 1292 | if (native_sectors <= sectors || !unlock_hpa) { |
Tejun Heo | 05027ad | 2007-09-03 12:32:57 +0900 | [diff] [blame] | 1293 | if (!print_info || native_sectors == sectors) |
| 1294 | return 0; |
| 1295 | |
| 1296 | if (native_sectors > sectors) |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 1297 | ata_dev_info(dev, |
Tejun Heo | 05027ad | 2007-09-03 12:32:57 +0900 | [diff] [blame] | 1298 | "HPA detected: current %llu, native %llu\n", |
| 1299 | (unsigned long long)sectors, |
| 1300 | (unsigned long long)native_sectors); |
| 1301 | else if (native_sectors < sectors) |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 1302 | ata_dev_warn(dev, |
| 1303 | "native sectors (%llu) is smaller than sectors (%llu)\n", |
Tejun Heo | 05027ad | 2007-09-03 12:32:57 +0900 | [diff] [blame] | 1304 | (unsigned long long)native_sectors, |
| 1305 | (unsigned long long)sectors); |
| 1306 | return 0; |
| 1307 | } |
| 1308 | |
| 1309 | /* let's unlock HPA */ |
| 1310 | rc = ata_set_max_sectors(dev, native_sectors); |
| 1311 | if (rc == -EACCES) { |
| 1312 | /* if device aborted the command, skip HPA resizing */ |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 1313 | ata_dev_warn(dev, |
| 1314 | "device aborted resize (%llu -> %llu), skipping HPA handling\n", |
| 1315 | (unsigned long long)sectors, |
| 1316 | (unsigned long long)native_sectors); |
Tejun Heo | 05027ad | 2007-09-03 12:32:57 +0900 | [diff] [blame] | 1317 | dev->horkage |= ATA_HORKAGE_BROKEN_HPA; |
| 1318 | return 0; |
| 1319 | } else if (rc) |
| 1320 | return rc; |
| 1321 | |
| 1322 | /* re-read IDENTIFY data */ |
| 1323 | rc = ata_dev_reread_id(dev, 0); |
| 1324 | if (rc) { |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 1325 | ata_dev_err(dev, |
| 1326 | "failed to re-read IDENTIFY data after HPA resizing\n"); |
Tejun Heo | 05027ad | 2007-09-03 12:32:57 +0900 | [diff] [blame] | 1327 | return rc; |
| 1328 | } |
| 1329 | |
| 1330 | if (print_info) { |
| 1331 | u64 new_sectors = ata_id_n_sectors(dev->id); |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 1332 | ata_dev_info(dev, |
Tejun Heo | 05027ad | 2007-09-03 12:32:57 +0900 | [diff] [blame] | 1333 | "HPA unlocked: %llu -> %llu, native %llu\n", |
| 1334 | (unsigned long long)sectors, |
| 1335 | (unsigned long long)new_sectors, |
| 1336 | (unsigned long long)native_sectors); |
| 1337 | } |
| 1338 | |
| 1339 | return 0; |
Alan Cox | 1e99973 | 2007-04-11 00:23:13 +0100 | [diff] [blame] | 1340 | } |
| 1341 | |
Edward Falk | 0baab86 | 2005-06-02 18:17:13 -0400 | [diff] [blame] | 1342 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1343 | * ata_dump_id - IDENTIFY DEVICE info debugging output |
Hannes Reinecke | 6044f3c | 2021-12-21 08:20:27 +0100 | [diff] [blame] | 1344 | * @dev: device from which the information is fetched |
Tejun Heo | 0bd3300 | 2006-02-12 22:47:05 +0900 | [diff] [blame] | 1345 | * @id: IDENTIFY DEVICE page to dump |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1346 | * |
Tejun Heo | 0bd3300 | 2006-02-12 22:47:05 +0900 | [diff] [blame] | 1347 | * Dump selected 16-bit words from the given IDENTIFY DEVICE |
| 1348 | * page. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1349 | * |
| 1350 | * LOCKING: |
| 1351 | * caller. |
| 1352 | */ |
| 1353 | |
Hannes Reinecke | 6044f3c | 2021-12-21 08:20:27 +0100 | [diff] [blame] | 1354 | static inline void ata_dump_id(struct ata_device *dev, const u16 *id) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1355 | { |
Hannes Reinecke | 6044f3c | 2021-12-21 08:20:27 +0100 | [diff] [blame] | 1356 | ata_dev_dbg(dev, |
| 1357 | "49==0x%04x 53==0x%04x 63==0x%04x 64==0x%04x 75==0x%04x\n" |
| 1358 | "80==0x%04x 81==0x%04x 82==0x%04x 83==0x%04x 84==0x%04x\n" |
| 1359 | "88==0x%04x 93==0x%04x\n", |
| 1360 | id[49], id[53], id[63], id[64], id[75], id[80], |
| 1361 | id[81], id[82], id[83], id[84], id[88], id[93]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1362 | } |
| 1363 | |
Tejun Heo | cb95d56 | 2006-03-06 04:31:56 +0900 | [diff] [blame] | 1364 | /** |
| 1365 | * ata_id_xfermask - Compute xfermask from the given IDENTIFY data |
| 1366 | * @id: IDENTIFY data to compute xfer mask from |
| 1367 | * |
| 1368 | * Compute the xfermask for this device. This is not as trivial |
| 1369 | * as it seems if we must consider early devices correctly. |
| 1370 | * |
| 1371 | * FIXME: pre IDE drive timing (do we care ?). |
| 1372 | * |
| 1373 | * LOCKING: |
| 1374 | * None. |
| 1375 | * |
| 1376 | * RETURNS: |
| 1377 | * Computed xfermask |
| 1378 | */ |
Tejun Heo | 7dc951a | 2007-11-27 19:43:42 +0900 | [diff] [blame] | 1379 | unsigned long ata_id_xfermask(const u16 *id) |
Tejun Heo | cb95d56 | 2006-03-06 04:31:56 +0900 | [diff] [blame] | 1380 | { |
Tejun Heo | 7dc951a | 2007-11-27 19:43:42 +0900 | [diff] [blame] | 1381 | unsigned long pio_mask, mwdma_mask, udma_mask; |
Tejun Heo | cb95d56 | 2006-03-06 04:31:56 +0900 | [diff] [blame] | 1382 | |
| 1383 | /* Usual case. Word 53 indicates word 64 is valid */ |
| 1384 | if (id[ATA_ID_FIELD_VALID] & (1 << 1)) { |
| 1385 | pio_mask = id[ATA_ID_PIO_MODES] & 0x03; |
| 1386 | pio_mask <<= 3; |
| 1387 | pio_mask |= 0x7; |
| 1388 | } else { |
| 1389 | /* If word 64 isn't valid then Word 51 high byte holds |
| 1390 | * the PIO timing number for the maximum. Turn it into |
| 1391 | * a mask. |
| 1392 | */ |
Lennert Buytenhek | 7a0f1c8 | 2007-01-29 13:28:47 +0100 | [diff] [blame] | 1393 | u8 mode = (id[ATA_ID_OLD_PIO_MODES] >> 8) & 0xFF; |
Alan Cox | 46767aeb | 2006-09-29 18:26:47 +0100 | [diff] [blame] | 1394 | if (mode < 5) /* Valid PIO range */ |
Jeff Garzik | 2dcb407 | 2007-10-19 06:42:56 -0400 | [diff] [blame] | 1395 | pio_mask = (2 << mode) - 1; |
Alan Cox | 46767aeb | 2006-09-29 18:26:47 +0100 | [diff] [blame] | 1396 | else |
| 1397 | pio_mask = 1; |
Tejun Heo | cb95d56 | 2006-03-06 04:31:56 +0900 | [diff] [blame] | 1398 | |
| 1399 | /* But wait.. there's more. Design your standards by |
| 1400 | * committee and you too can get a free iordy field to |
| 1401 | * process. However its the speeds not the modes that |
| 1402 | * are supported... Note drivers using the timing API |
| 1403 | * will get this right anyway |
| 1404 | */ |
| 1405 | } |
| 1406 | |
| 1407 | mwdma_mask = id[ATA_ID_MWDMA_MODES] & 0x07; |
Tejun Heo | fb21f0d | 2006-03-12 12:34:35 +0900 | [diff] [blame] | 1408 | |
Alan Cox | b352e57 | 2006-08-10 18:52:12 +0100 | [diff] [blame] | 1409 | if (ata_id_is_cfa(id)) { |
| 1410 | /* |
| 1411 | * Process compact flash extended modes |
| 1412 | */ |
Sergei Shtylyov | 62afe5d | 2009-04-13 20:50:00 +0400 | [diff] [blame] | 1413 | int pio = (id[ATA_ID_CFA_MODES] >> 0) & 0x7; |
| 1414 | int dma = (id[ATA_ID_CFA_MODES] >> 3) & 0x7; |
Alan Cox | b352e57 | 2006-08-10 18:52:12 +0100 | [diff] [blame] | 1415 | |
| 1416 | if (pio) |
| 1417 | pio_mask |= (1 << 5); |
| 1418 | if (pio > 1) |
| 1419 | pio_mask |= (1 << 6); |
| 1420 | if (dma) |
| 1421 | mwdma_mask |= (1 << 3); |
| 1422 | if (dma > 1) |
| 1423 | mwdma_mask |= (1 << 4); |
| 1424 | } |
| 1425 | |
Tejun Heo | fb21f0d | 2006-03-12 12:34:35 +0900 | [diff] [blame] | 1426 | udma_mask = 0; |
| 1427 | if (id[ATA_ID_FIELD_VALID] & (1 << 2)) |
| 1428 | udma_mask = id[ATA_ID_UDMA_MODES] & 0xff; |
Tejun Heo | cb95d56 | 2006-03-06 04:31:56 +0900 | [diff] [blame] | 1429 | |
| 1430 | return ata_pack_xfermask(pio_mask, mwdma_mask, udma_mask); |
| 1431 | } |
Bartlomiej Zolnierkiewicz | a52fbcf | 2020-03-26 16:58:04 +0100 | [diff] [blame] | 1432 | EXPORT_SYMBOL_GPL(ata_id_xfermask); |
Tejun Heo | cb95d56 | 2006-03-06 04:31:56 +0900 | [diff] [blame] | 1433 | |
Adrian Bunk | 7102d23 | 2007-01-04 00:09:36 +0100 | [diff] [blame] | 1434 | static void ata_qc_complete_internal(struct ata_queued_cmd *qc) |
Tejun Heo | a2a7a66 | 2005-12-13 14:48:31 +0900 | [diff] [blame] | 1435 | { |
Tejun Heo | 77853bf | 2006-01-23 13:09:36 +0900 | [diff] [blame] | 1436 | struct completion *waiting = qc->private_data; |
Tejun Heo | a2a7a66 | 2005-12-13 14:48:31 +0900 | [diff] [blame] | 1437 | |
Tejun Heo | a2a7a66 | 2005-12-13 14:48:31 +0900 | [diff] [blame] | 1438 | complete(waiting); |
Tejun Heo | a2a7a66 | 2005-12-13 14:48:31 +0900 | [diff] [blame] | 1439 | } |
| 1440 | |
| 1441 | /** |
Tejun Heo | 2432697 | 2006-11-14 22:47:09 +0900 | [diff] [blame] | 1442 | * ata_exec_internal_sg - execute libata internal command |
Tejun Heo | a2a7a66 | 2005-12-13 14:48:31 +0900 | [diff] [blame] | 1443 | * @dev: Device to which the command is sent |
| 1444 | * @tf: Taskfile registers for the command and the result |
Tejun Heo | d69cf37 | 2006-04-02 18:51:53 +0900 | [diff] [blame] | 1445 | * @cdb: CDB for packet command |
Masanari Iida | e227867 | 2014-02-18 22:54:36 +0900 | [diff] [blame] | 1446 | * @dma_dir: Data transfer direction of the command |
Randy Dunlap | 5c1ad8b | 2007-10-18 14:12:26 -0700 | [diff] [blame] | 1447 | * @sgl: sg list for the data buffer of the command |
Tejun Heo | 2432697 | 2006-11-14 22:47:09 +0900 | [diff] [blame] | 1448 | * @n_elem: Number of sg entries |
Tejun Heo | 2b78910 | 2007-10-09 15:05:44 +0900 | [diff] [blame] | 1449 | * @timeout: Timeout in msecs (0 for default) |
Tejun Heo | a2a7a66 | 2005-12-13 14:48:31 +0900 | [diff] [blame] | 1450 | * |
| 1451 | * Executes libata internal command with timeout. @tf contains |
| 1452 | * command on entry and result on return. Timeout and error |
| 1453 | * conditions are reported via return value. No recovery action |
| 1454 | * is taken after a command times out. It's caller's duty to |
| 1455 | * clean up after timeout. |
| 1456 | * |
| 1457 | * LOCKING: |
| 1458 | * None. Should be called with kernel context, might sleep. |
Tejun Heo | 551e888 | 2006-06-12 14:09:49 +0900 | [diff] [blame] | 1459 | * |
| 1460 | * RETURNS: |
| 1461 | * Zero on success, AC_ERR_* mask on failure |
Tejun Heo | a2a7a66 | 2005-12-13 14:48:31 +0900 | [diff] [blame] | 1462 | */ |
Tejun Heo | 2432697 | 2006-11-14 22:47:09 +0900 | [diff] [blame] | 1463 | unsigned ata_exec_internal_sg(struct ata_device *dev, |
| 1464 | struct ata_taskfile *tf, const u8 *cdb, |
Jens Axboe | 8726021 | 2007-10-16 11:14:12 +0200 | [diff] [blame] | 1465 | int dma_dir, struct scatterlist *sgl, |
Tejun Heo | 2b78910 | 2007-10-09 15:05:44 +0900 | [diff] [blame] | 1466 | unsigned int n_elem, unsigned long timeout) |
Tejun Heo | a2a7a66 | 2005-12-13 14:48:31 +0900 | [diff] [blame] | 1467 | { |
Tejun Heo | 9af5c9c | 2007-08-06 18:36:22 +0900 | [diff] [blame] | 1468 | struct ata_link *link = dev->link; |
| 1469 | struct ata_port *ap = link->ap; |
Tejun Heo | a2a7a66 | 2005-12-13 14:48:31 +0900 | [diff] [blame] | 1470 | u8 command = tf->command; |
Tejun Heo | 87fbc5a | 2008-05-20 02:17:54 +0900 | [diff] [blame] | 1471 | int auto_timeout = 0; |
Tejun Heo | a2a7a66 | 2005-12-13 14:48:31 +0900 | [diff] [blame] | 1472 | struct ata_queued_cmd *qc; |
Jens Axboe | 28361c4 | 2018-05-11 12:51:09 -0600 | [diff] [blame] | 1473 | unsigned int preempted_tag; |
Jens Axboe | e3ed8939 | 2018-05-11 12:51:05 -0600 | [diff] [blame] | 1474 | u32 preempted_sactive; |
| 1475 | u64 preempted_qc_active; |
Tejun Heo | da917d6 | 2007-09-23 13:14:12 +0900 | [diff] [blame] | 1476 | int preempted_nr_active_links; |
Ingo Molnar | 60be6b9 | 2006-07-03 00:25:26 -0700 | [diff] [blame] | 1477 | DECLARE_COMPLETION_ONSTACK(wait); |
Tejun Heo | a2a7a66 | 2005-12-13 14:48:31 +0900 | [diff] [blame] | 1478 | unsigned long flags; |
Tejun Heo | 77853bf | 2006-01-23 13:09:36 +0900 | [diff] [blame] | 1479 | unsigned int err_mask; |
Tejun Heo | d95a717 | 2006-05-15 20:58:14 +0900 | [diff] [blame] | 1480 | int rc; |
Tejun Heo | a2a7a66 | 2005-12-13 14:48:31 +0900 | [diff] [blame] | 1481 | |
Jeff Garzik | ba6a130 | 2006-06-22 23:46:10 -0400 | [diff] [blame] | 1482 | spin_lock_irqsave(ap->lock, flags); |
Tejun Heo | a2a7a66 | 2005-12-13 14:48:31 +0900 | [diff] [blame] | 1483 | |
Tejun Heo | e318049 | 2006-05-15 20:58:09 +0900 | [diff] [blame] | 1484 | /* no internal command while frozen */ |
Tejun Heo | b51e9e5 | 2006-06-29 01:29:30 +0900 | [diff] [blame] | 1485 | if (ap->pflags & ATA_PFLAG_FROZEN) { |
Jeff Garzik | ba6a130 | 2006-06-22 23:46:10 -0400 | [diff] [blame] | 1486 | spin_unlock_irqrestore(ap->lock, flags); |
Tejun Heo | e318049 | 2006-05-15 20:58:09 +0900 | [diff] [blame] | 1487 | return AC_ERR_SYSTEM; |
| 1488 | } |
| 1489 | |
Tejun Heo | 2ab7db1 | 2006-05-15 20:58:02 +0900 | [diff] [blame] | 1490 | /* initialize internal qc */ |
Jens Axboe | 28361c4 | 2018-05-11 12:51:09 -0600 | [diff] [blame] | 1491 | qc = __ata_qc_from_tag(ap, ATA_TAG_INTERNAL); |
Tejun Heo | a2a7a66 | 2005-12-13 14:48:31 +0900 | [diff] [blame] | 1492 | |
Jens Axboe | 28361c4 | 2018-05-11 12:51:09 -0600 | [diff] [blame] | 1493 | qc->tag = ATA_TAG_INTERNAL; |
| 1494 | qc->hw_tag = 0; |
Tejun Heo | 2ab7db1 | 2006-05-15 20:58:02 +0900 | [diff] [blame] | 1495 | qc->scsicmd = NULL; |
| 1496 | qc->ap = ap; |
| 1497 | qc->dev = dev; |
| 1498 | ata_qc_reinit(qc); |
| 1499 | |
Tejun Heo | 9af5c9c | 2007-08-06 18:36:22 +0900 | [diff] [blame] | 1500 | preempted_tag = link->active_tag; |
| 1501 | preempted_sactive = link->sactive; |
Tejun Heo | dedaf2b | 2006-05-15 21:03:43 +0900 | [diff] [blame] | 1502 | preempted_qc_active = ap->qc_active; |
Tejun Heo | da917d6 | 2007-09-23 13:14:12 +0900 | [diff] [blame] | 1503 | preempted_nr_active_links = ap->nr_active_links; |
Tejun Heo | 9af5c9c | 2007-08-06 18:36:22 +0900 | [diff] [blame] | 1504 | link->active_tag = ATA_TAG_POISON; |
| 1505 | link->sactive = 0; |
Tejun Heo | dedaf2b | 2006-05-15 21:03:43 +0900 | [diff] [blame] | 1506 | ap->qc_active = 0; |
Tejun Heo | da917d6 | 2007-09-23 13:14:12 +0900 | [diff] [blame] | 1507 | ap->nr_active_links = 0; |
Tejun Heo | 2ab7db1 | 2006-05-15 20:58:02 +0900 | [diff] [blame] | 1508 | |
| 1509 | /* prepare & issue qc */ |
Tejun Heo | a2a7a66 | 2005-12-13 14:48:31 +0900 | [diff] [blame] | 1510 | qc->tf = *tf; |
Tejun Heo | d69cf37 | 2006-04-02 18:51:53 +0900 | [diff] [blame] | 1511 | if (cdb) |
| 1512 | memcpy(qc->cdb, cdb, ATAPI_CDB_LEN); |
Vincent Pelletier | e771451 | 2013-05-18 18:44:04 +0200 | [diff] [blame] | 1513 | |
| 1514 | /* some SATA bridges need us to indicate data xfer direction */ |
| 1515 | if (tf->protocol == ATAPI_PROT_DMA && (dev->flags & ATA_DFLAG_DMADIR) && |
| 1516 | dma_dir == DMA_FROM_DEVICE) |
| 1517 | qc->tf.feature |= ATAPI_DMADIR; |
| 1518 | |
Tejun Heo | e61e067 | 2006-05-15 20:57:40 +0900 | [diff] [blame] | 1519 | qc->flags |= ATA_QCFLAG_RESULT_TF; |
Tejun Heo | a2a7a66 | 2005-12-13 14:48:31 +0900 | [diff] [blame] | 1520 | qc->dma_dir = dma_dir; |
| 1521 | if (dma_dir != DMA_NONE) { |
Tejun Heo | 2432697 | 2006-11-14 22:47:09 +0900 | [diff] [blame] | 1522 | unsigned int i, buflen = 0; |
Jens Axboe | 8726021 | 2007-10-16 11:14:12 +0200 | [diff] [blame] | 1523 | struct scatterlist *sg; |
Tejun Heo | 2432697 | 2006-11-14 22:47:09 +0900 | [diff] [blame] | 1524 | |
Jens Axboe | 8726021 | 2007-10-16 11:14:12 +0200 | [diff] [blame] | 1525 | for_each_sg(sgl, sg, n_elem, i) |
| 1526 | buflen += sg->length; |
Tejun Heo | 2432697 | 2006-11-14 22:47:09 +0900 | [diff] [blame] | 1527 | |
Jens Axboe | 8726021 | 2007-10-16 11:14:12 +0200 | [diff] [blame] | 1528 | ata_sg_init(qc, sgl, n_elem); |
Brian King | 49c8042 | 2007-01-30 11:32:26 -0600 | [diff] [blame] | 1529 | qc->nbytes = buflen; |
Tejun Heo | a2a7a66 | 2005-12-13 14:48:31 +0900 | [diff] [blame] | 1530 | } |
| 1531 | |
Tejun Heo | 77853bf | 2006-01-23 13:09:36 +0900 | [diff] [blame] | 1532 | qc->private_data = &wait; |
Tejun Heo | a2a7a66 | 2005-12-13 14:48:31 +0900 | [diff] [blame] | 1533 | qc->complete_fn = ata_qc_complete_internal; |
| 1534 | |
Tejun Heo | 8e0e694 | 2006-03-31 20:41:11 +0900 | [diff] [blame] | 1535 | ata_qc_issue(qc); |
Tejun Heo | a2a7a66 | 2005-12-13 14:48:31 +0900 | [diff] [blame] | 1536 | |
Jeff Garzik | ba6a130 | 2006-06-22 23:46:10 -0400 | [diff] [blame] | 1537 | spin_unlock_irqrestore(ap->lock, flags); |
Tejun Heo | a2a7a66 | 2005-12-13 14:48:31 +0900 | [diff] [blame] | 1538 | |
Tejun Heo | 87fbc5a | 2008-05-20 02:17:54 +0900 | [diff] [blame] | 1539 | if (!timeout) { |
| 1540 | if (ata_probe_timeout) |
| 1541 | timeout = ata_probe_timeout * 1000; |
| 1542 | else { |
| 1543 | timeout = ata_internal_cmd_timeout(dev, command); |
| 1544 | auto_timeout = 1; |
| 1545 | } |
| 1546 | } |
Tejun Heo | 2b78910 | 2007-10-09 15:05:44 +0900 | [diff] [blame] | 1547 | |
Tejun Heo | c0c362b | 2010-09-06 17:57:14 +0200 | [diff] [blame] | 1548 | if (ap->ops->error_handler) |
| 1549 | ata_eh_release(ap); |
| 1550 | |
Tejun Heo | 2b78910 | 2007-10-09 15:05:44 +0900 | [diff] [blame] | 1551 | rc = wait_for_completion_timeout(&wait, msecs_to_jiffies(timeout)); |
Albert Lee | 41ade50 | 2006-03-14 11:19:04 +0800 | [diff] [blame] | 1552 | |
Tejun Heo | c0c362b | 2010-09-06 17:57:14 +0200 | [diff] [blame] | 1553 | if (ap->ops->error_handler) |
| 1554 | ata_eh_acquire(ap); |
| 1555 | |
Tejun Heo | c429137 | 2010-05-10 21:41:38 +0200 | [diff] [blame] | 1556 | ata_sff_flush_pio_task(ap); |
Tejun Heo | d95a717 | 2006-05-15 20:58:14 +0900 | [diff] [blame] | 1557 | |
| 1558 | if (!rc) { |
Jeff Garzik | ba6a130 | 2006-06-22 23:46:10 -0400 | [diff] [blame] | 1559 | spin_lock_irqsave(ap->lock, flags); |
Tejun Heo | a2a7a66 | 2005-12-13 14:48:31 +0900 | [diff] [blame] | 1560 | |
| 1561 | /* We're racing with irq here. If we lose, the |
| 1562 | * following test prevents us from completing the qc |
Tejun Heo | d95a717 | 2006-05-15 20:58:14 +0900 | [diff] [blame] | 1563 | * twice. If we win, the port is frozen and will be |
| 1564 | * cleaned up by ->post_internal_cmd(). |
Tejun Heo | a2a7a66 | 2005-12-13 14:48:31 +0900 | [diff] [blame] | 1565 | */ |
Tejun Heo | 77853bf | 2006-01-23 13:09:36 +0900 | [diff] [blame] | 1566 | if (qc->flags & ATA_QCFLAG_ACTIVE) { |
Tejun Heo | d95a717 | 2006-05-15 20:58:14 +0900 | [diff] [blame] | 1567 | qc->err_mask |= AC_ERR_TIMEOUT; |
| 1568 | |
| 1569 | if (ap->ops->error_handler) |
| 1570 | ata_port_freeze(ap); |
| 1571 | else |
| 1572 | ata_qc_complete(qc); |
Tejun Heo | f15a1da | 2006-05-15 20:57:56 +0900 | [diff] [blame] | 1573 | |
Hannes Reinecke | 16d4246 | 2021-12-21 08:21:04 +0100 | [diff] [blame] | 1574 | ata_dev_warn(dev, "qc timeout (cmd 0x%x)\n", |
| 1575 | command); |
Tejun Heo | a2a7a66 | 2005-12-13 14:48:31 +0900 | [diff] [blame] | 1576 | } |
| 1577 | |
Jeff Garzik | ba6a130 | 2006-06-22 23:46:10 -0400 | [diff] [blame] | 1578 | spin_unlock_irqrestore(ap->lock, flags); |
Tejun Heo | a2a7a66 | 2005-12-13 14:48:31 +0900 | [diff] [blame] | 1579 | } |
| 1580 | |
Tejun Heo | d95a717 | 2006-05-15 20:58:14 +0900 | [diff] [blame] | 1581 | /* do post_internal_cmd */ |
| 1582 | if (ap->ops->post_internal_cmd) |
| 1583 | ap->ops->post_internal_cmd(qc); |
| 1584 | |
Tejun Heo | a51d644 | 2007-03-20 15:24:11 +0900 | [diff] [blame] | 1585 | /* perform minimal error analysis */ |
| 1586 | if (qc->flags & ATA_QCFLAG_FAILED) { |
| 1587 | if (qc->result_tf.command & (ATA_ERR | ATA_DF)) |
| 1588 | qc->err_mask |= AC_ERR_DEV; |
| 1589 | |
| 1590 | if (!qc->err_mask) |
| 1591 | qc->err_mask |= AC_ERR_OTHER; |
| 1592 | |
| 1593 | if (qc->err_mask & ~AC_ERR_OTHER) |
| 1594 | qc->err_mask &= ~AC_ERR_OTHER; |
Damien Le Moal | 2dae995 | 2016-12-19 10:17:40 +0900 | [diff] [blame] | 1595 | } else if (qc->tf.command == ATA_CMD_REQ_SENSE_DATA) { |
| 1596 | qc->result_tf.command |= ATA_SENSE; |
Tejun Heo | d95a717 | 2006-05-15 20:58:14 +0900 | [diff] [blame] | 1597 | } |
| 1598 | |
Tejun Heo | 1586930 | 2006-05-15 20:57:33 +0900 | [diff] [blame] | 1599 | /* finish up */ |
Jeff Garzik | ba6a130 | 2006-06-22 23:46:10 -0400 | [diff] [blame] | 1600 | spin_lock_irqsave(ap->lock, flags); |
Tejun Heo | 1586930 | 2006-05-15 20:57:33 +0900 | [diff] [blame] | 1601 | |
Tejun Heo | e61e067 | 2006-05-15 20:57:40 +0900 | [diff] [blame] | 1602 | *tf = qc->result_tf; |
Tejun Heo | 77853bf | 2006-01-23 13:09:36 +0900 | [diff] [blame] | 1603 | err_mask = qc->err_mask; |
| 1604 | |
| 1605 | ata_qc_free(qc); |
Tejun Heo | 9af5c9c | 2007-08-06 18:36:22 +0900 | [diff] [blame] | 1606 | link->active_tag = preempted_tag; |
| 1607 | link->sactive = preempted_sactive; |
Tejun Heo | dedaf2b | 2006-05-15 21:03:43 +0900 | [diff] [blame] | 1608 | ap->qc_active = preempted_qc_active; |
Tejun Heo | da917d6 | 2007-09-23 13:14:12 +0900 | [diff] [blame] | 1609 | ap->nr_active_links = preempted_nr_active_links; |
Tejun Heo | 77853bf | 2006-01-23 13:09:36 +0900 | [diff] [blame] | 1610 | |
Jeff Garzik | ba6a130 | 2006-06-22 23:46:10 -0400 | [diff] [blame] | 1611 | spin_unlock_irqrestore(ap->lock, flags); |
Tejun Heo | 1586930 | 2006-05-15 20:57:33 +0900 | [diff] [blame] | 1612 | |
Tejun Heo | 87fbc5a | 2008-05-20 02:17:54 +0900 | [diff] [blame] | 1613 | if ((err_mask & AC_ERR_TIMEOUT) && auto_timeout) |
| 1614 | ata_internal_cmd_timed_out(dev, command); |
| 1615 | |
Tejun Heo | 77853bf | 2006-01-23 13:09:36 +0900 | [diff] [blame] | 1616 | return err_mask; |
Tejun Heo | a2a7a66 | 2005-12-13 14:48:31 +0900 | [diff] [blame] | 1617 | } |
| 1618 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1619 | /** |
Tejun Heo | 33480a0 | 2006-12-12 02:15:31 +0900 | [diff] [blame] | 1620 | * ata_exec_internal - execute libata internal command |
Tejun Heo | 2432697 | 2006-11-14 22:47:09 +0900 | [diff] [blame] | 1621 | * @dev: Device to which the command is sent |
| 1622 | * @tf: Taskfile registers for the command and the result |
| 1623 | * @cdb: CDB for packet command |
Masanari Iida | e227867 | 2014-02-18 22:54:36 +0900 | [diff] [blame] | 1624 | * @dma_dir: Data transfer direction of the command |
Tejun Heo | 2432697 | 2006-11-14 22:47:09 +0900 | [diff] [blame] | 1625 | * @buf: Data buffer of the command |
| 1626 | * @buflen: Length of data buffer |
Tejun Heo | 2b78910 | 2007-10-09 15:05:44 +0900 | [diff] [blame] | 1627 | * @timeout: Timeout in msecs (0 for default) |
Tejun Heo | 2432697 | 2006-11-14 22:47:09 +0900 | [diff] [blame] | 1628 | * |
| 1629 | * Wrapper around ata_exec_internal_sg() which takes simple |
| 1630 | * buffer instead of sg list. |
| 1631 | * |
| 1632 | * LOCKING: |
| 1633 | * None. Should be called with kernel context, might sleep. |
| 1634 | * |
| 1635 | * RETURNS: |
| 1636 | * Zero on success, AC_ERR_* mask on failure |
| 1637 | */ |
| 1638 | unsigned ata_exec_internal(struct ata_device *dev, |
| 1639 | struct ata_taskfile *tf, const u8 *cdb, |
Tejun Heo | 2b78910 | 2007-10-09 15:05:44 +0900 | [diff] [blame] | 1640 | int dma_dir, void *buf, unsigned int buflen, |
| 1641 | unsigned long timeout) |
Tejun Heo | 2432697 | 2006-11-14 22:47:09 +0900 | [diff] [blame] | 1642 | { |
Tejun Heo | 33480a0 | 2006-12-12 02:15:31 +0900 | [diff] [blame] | 1643 | struct scatterlist *psg = NULL, sg; |
| 1644 | unsigned int n_elem = 0; |
Tejun Heo | 2432697 | 2006-11-14 22:47:09 +0900 | [diff] [blame] | 1645 | |
Tejun Heo | 33480a0 | 2006-12-12 02:15:31 +0900 | [diff] [blame] | 1646 | if (dma_dir != DMA_NONE) { |
| 1647 | WARN_ON(!buf); |
| 1648 | sg_init_one(&sg, buf, buflen); |
| 1649 | psg = &sg; |
| 1650 | n_elem++; |
| 1651 | } |
Tejun Heo | 2432697 | 2006-11-14 22:47:09 +0900 | [diff] [blame] | 1652 | |
Tejun Heo | 2b78910 | 2007-10-09 15:05:44 +0900 | [diff] [blame] | 1653 | return ata_exec_internal_sg(dev, tf, cdb, dma_dir, psg, n_elem, |
| 1654 | timeout); |
Tejun Heo | 2432697 | 2006-11-14 22:47:09 +0900 | [diff] [blame] | 1655 | } |
| 1656 | |
| 1657 | /** |
Alan Cox | 1bc4ccf | 2006-01-09 17:18:14 +0000 | [diff] [blame] | 1658 | * ata_pio_need_iordy - check if iordy needed |
| 1659 | * @adev: ATA device |
| 1660 | * |
| 1661 | * Check if the current speed of the device requires IORDY. Used |
| 1662 | * by various controllers for chip configuration. |
| 1663 | */ |
Alan Cox | 1bc4ccf | 2006-01-09 17:18:14 +0000 | [diff] [blame] | 1664 | unsigned int ata_pio_need_iordy(const struct ata_device *adev) |
| 1665 | { |
Tejun Heo | 0d9e665 | 2009-06-11 11:04:45 +0900 | [diff] [blame] | 1666 | /* Don't set IORDY if we're preparing for reset. IORDY may |
| 1667 | * lead to controller lock up on certain controllers if the |
| 1668 | * port is not occupied. See bko#11703 for details. |
| 1669 | */ |
| 1670 | if (adev->link->ap->pflags & ATA_PFLAG_RESETTING) |
| 1671 | return 0; |
| 1672 | /* Controller doesn't support IORDY. Probably a pointless |
| 1673 | * check as the caller should know this. |
| 1674 | */ |
Tejun Heo | 9af5c9c | 2007-08-06 18:36:22 +0900 | [diff] [blame] | 1675 | if (adev->link->ap->flags & ATA_FLAG_NO_IORDY) |
Alan Cox | 1bc4ccf | 2006-01-09 17:18:14 +0000 | [diff] [blame] | 1676 | return 0; |
David Daney | 5c18c4d | 2008-12-10 15:39:12 -0800 | [diff] [blame] | 1677 | /* CF spec. r4.1 Table 22 says no iordy on PIO5 and PIO6. */ |
| 1678 | if (ata_id_is_cfa(adev->id) |
| 1679 | && (adev->pio_mode == XFER_PIO_5 || adev->pio_mode == XFER_PIO_6)) |
| 1680 | return 0; |
Alan Cox | 432729f | 2007-03-08 23:22:59 +0000 | [diff] [blame] | 1681 | /* PIO3 and higher it is mandatory */ |
| 1682 | if (adev->pio_mode > XFER_PIO_2) |
Alan Cox | 1bc4ccf | 2006-01-09 17:18:14 +0000 | [diff] [blame] | 1683 | return 1; |
Alan Cox | 432729f | 2007-03-08 23:22:59 +0000 | [diff] [blame] | 1684 | /* We turn it on when possible */ |
| 1685 | if (ata_id_has_iordy(adev->id)) |
| 1686 | return 1; |
| 1687 | return 0; |
| 1688 | } |
Bartlomiej Zolnierkiewicz | a52fbcf | 2020-03-26 16:58:04 +0100 | [diff] [blame] | 1689 | EXPORT_SYMBOL_GPL(ata_pio_need_iordy); |
Jeff Garzik | 2e9edbf | 2006-03-24 09:56:57 -0500 | [diff] [blame] | 1690 | |
Alan Cox | 432729f | 2007-03-08 23:22:59 +0000 | [diff] [blame] | 1691 | /** |
| 1692 | * ata_pio_mask_no_iordy - Return the non IORDY mask |
| 1693 | * @adev: ATA device |
| 1694 | * |
| 1695 | * Compute the highest mode possible if we are not using iordy. Return |
| 1696 | * -1 if no iordy mode is available. |
| 1697 | */ |
Alan Cox | 432729f | 2007-03-08 23:22:59 +0000 | [diff] [blame] | 1698 | static u32 ata_pio_mask_no_iordy(const struct ata_device *adev) |
| 1699 | { |
Alan Cox | 1bc4ccf | 2006-01-09 17:18:14 +0000 | [diff] [blame] | 1700 | /* If we have no drive specific rule, then PIO 2 is non IORDY */ |
Alan Cox | 1bc4ccf | 2006-01-09 17:18:14 +0000 | [diff] [blame] | 1701 | if (adev->id[ATA_ID_FIELD_VALID] & 2) { /* EIDE */ |
Alan Cox | 432729f | 2007-03-08 23:22:59 +0000 | [diff] [blame] | 1702 | u16 pio = adev->id[ATA_ID_EIDE_PIO]; |
Alan Cox | 1bc4ccf | 2006-01-09 17:18:14 +0000 | [diff] [blame] | 1703 | /* Is the speed faster than the drive allows non IORDY ? */ |
| 1704 | if (pio) { |
| 1705 | /* This is cycle times not frequency - watch the logic! */ |
| 1706 | if (pio > 240) /* PIO2 is 240nS per cycle */ |
Alan Cox | 432729f | 2007-03-08 23:22:59 +0000 | [diff] [blame] | 1707 | return 3 << ATA_SHIFT_PIO; |
| 1708 | return 7 << ATA_SHIFT_PIO; |
Alan Cox | 1bc4ccf | 2006-01-09 17:18:14 +0000 | [diff] [blame] | 1709 | } |
| 1710 | } |
Alan Cox | 432729f | 2007-03-08 23:22:59 +0000 | [diff] [blame] | 1711 | return 3 << ATA_SHIFT_PIO; |
Alan Cox | 1bc4ccf | 2006-01-09 17:18:14 +0000 | [diff] [blame] | 1712 | } |
| 1713 | |
| 1714 | /** |
Alan Cox | 963e497 | 2008-07-24 17:16:06 +0100 | [diff] [blame] | 1715 | * ata_do_dev_read_id - default ID read method |
| 1716 | * @dev: device |
| 1717 | * @tf: proposed taskfile |
| 1718 | * @id: data buffer |
| 1719 | * |
| 1720 | * Issue the identify taskfile and hand back the buffer containing |
| 1721 | * identify data. For some RAID controllers and for pre ATA devices |
| 1722 | * this function is wrapped or replaced by the driver |
| 1723 | */ |
| 1724 | unsigned int ata_do_dev_read_id(struct ata_device *dev, |
| 1725 | struct ata_taskfile *tf, u16 *id) |
| 1726 | { |
| 1727 | return ata_exec_internal(dev, tf, NULL, DMA_FROM_DEVICE, |
| 1728 | id, sizeof(id[0]) * ATA_ID_WORDS, 0); |
| 1729 | } |
Bartlomiej Zolnierkiewicz | a52fbcf | 2020-03-26 16:58:04 +0100 | [diff] [blame] | 1730 | EXPORT_SYMBOL_GPL(ata_do_dev_read_id); |
Alan Cox | 963e497 | 2008-07-24 17:16:06 +0100 | [diff] [blame] | 1731 | |
| 1732 | /** |
Tejun Heo | 49016ac | 2006-02-21 02:12:11 +0900 | [diff] [blame] | 1733 | * ata_dev_read_id - Read ID data from the specified device |
Tejun Heo | 49016ac | 2006-02-21 02:12:11 +0900 | [diff] [blame] | 1734 | * @dev: target device |
| 1735 | * @p_class: pointer to class of the target device (may be changed) |
Tejun Heo | bff0464 | 2006-11-10 18:08:10 +0900 | [diff] [blame] | 1736 | * @flags: ATA_READID_* flags |
Tejun Heo | fe635c7 | 2006-05-15 20:57:35 +0900 | [diff] [blame] | 1737 | * @id: buffer to read IDENTIFY data into |
Tejun Heo | 49016ac | 2006-02-21 02:12:11 +0900 | [diff] [blame] | 1738 | * |
| 1739 | * Read ID data from the specified device. ATA_CMD_ID_ATA is |
| 1740 | * performed on ATA devices and ATA_CMD_ID_ATAPI on ATAPI |
Tejun Heo | aec5c3c | 2006-03-25 01:33:34 +0900 | [diff] [blame] | 1741 | * devices. This function also issues ATA_CMD_INIT_DEV_PARAMS |
| 1742 | * for pre-ATA4 drives. |
Tejun Heo | 49016ac | 2006-02-21 02:12:11 +0900 | [diff] [blame] | 1743 | * |
Alan Cox | 50a9901 | 2007-08-08 14:27:00 +0100 | [diff] [blame] | 1744 | * FIXME: ATA_CMD_ID_ATA is optional for early drives and right |
Jeff Garzik | 2dcb407 | 2007-10-19 06:42:56 -0400 | [diff] [blame] | 1745 | * now we abort if we hit that case. |
Alan Cox | 50a9901 | 2007-08-08 14:27:00 +0100 | [diff] [blame] | 1746 | * |
Tejun Heo | 49016ac | 2006-02-21 02:12:11 +0900 | [diff] [blame] | 1747 | * LOCKING: |
| 1748 | * Kernel thread context (may sleep) |
| 1749 | * |
| 1750 | * RETURNS: |
| 1751 | * 0 on success, -errno otherwise. |
| 1752 | */ |
Tejun Heo | a9beec9 | 2006-05-31 18:27:44 +0900 | [diff] [blame] | 1753 | int ata_dev_read_id(struct ata_device *dev, unsigned int *p_class, |
Tejun Heo | bff0464 | 2006-11-10 18:08:10 +0900 | [diff] [blame] | 1754 | unsigned int flags, u16 *id) |
Tejun Heo | 49016ac | 2006-02-21 02:12:11 +0900 | [diff] [blame] | 1755 | { |
Tejun Heo | 9af5c9c | 2007-08-06 18:36:22 +0900 | [diff] [blame] | 1756 | struct ata_port *ap = dev->link->ap; |
Tejun Heo | 49016ac | 2006-02-21 02:12:11 +0900 | [diff] [blame] | 1757 | unsigned int class = *p_class; |
Tejun Heo | 49016ac | 2006-02-21 02:12:11 +0900 | [diff] [blame] | 1758 | struct ata_taskfile tf; |
| 1759 | unsigned int err_mask = 0; |
| 1760 | const char *reason; |
Tejun Heo | 79b42ba | 2009-04-15 06:21:10 +0900 | [diff] [blame] | 1761 | bool is_semb = class == ATA_DEV_SEMB; |
Tejun Heo | 54936f8 | 2007-05-11 14:35:29 +0200 | [diff] [blame] | 1762 | int may_fallback = 1, tried_spinup = 0; |
Tejun Heo | 49016ac | 2006-02-21 02:12:11 +0900 | [diff] [blame] | 1763 | int rc; |
| 1764 | |
Alan Cox | 963e497 | 2008-07-24 17:16:06 +0100 | [diff] [blame] | 1765 | retry: |
Tejun Heo | 3373efd | 2006-05-15 20:57:53 +0900 | [diff] [blame] | 1766 | ata_tf_init(dev, &tf); |
Tejun Heo | 49016ac | 2006-02-21 02:12:11 +0900 | [diff] [blame] | 1767 | |
| 1768 | switch (class) { |
Tejun Heo | 79b42ba | 2009-04-15 06:21:10 +0900 | [diff] [blame] | 1769 | case ATA_DEV_SEMB: |
| 1770 | class = ATA_DEV_ATA; /* some hard drives report SEMB sig */ |
Gustavo A. R. Silva | df561f66 | 2020-08-23 17:36:59 -0500 | [diff] [blame] | 1771 | fallthrough; |
Tejun Heo | 49016ac | 2006-02-21 02:12:11 +0900 | [diff] [blame] | 1772 | case ATA_DEV_ATA: |
Hannes Reinecke | 9162c65 | 2014-11-05 13:08:21 +0100 | [diff] [blame] | 1773 | case ATA_DEV_ZAC: |
Tejun Heo | 49016ac | 2006-02-21 02:12:11 +0900 | [diff] [blame] | 1774 | tf.command = ATA_CMD_ID_ATA; |
| 1775 | break; |
| 1776 | case ATA_DEV_ATAPI: |
| 1777 | tf.command = ATA_CMD_ID_ATAPI; |
| 1778 | break; |
| 1779 | default: |
| 1780 | rc = -ENODEV; |
| 1781 | reason = "unsupported class"; |
| 1782 | goto err_out; |
| 1783 | } |
| 1784 | |
| 1785 | tf.protocol = ATA_PROT_PIO; |
Tejun Heo | 81afe89 | 2007-02-07 12:37:41 -0800 | [diff] [blame] | 1786 | |
| 1787 | /* Some devices choke if TF registers contain garbage. Make |
| 1788 | * sure those are properly initialized. |
| 1789 | */ |
| 1790 | tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE; |
| 1791 | |
| 1792 | /* Device presence detection is unreliable on some |
| 1793 | * controllers. Always poll IDENTIFY if available. |
| 1794 | */ |
| 1795 | tf.flags |= ATA_TFLAG_POLLING; |
Tejun Heo | 49016ac | 2006-02-21 02:12:11 +0900 | [diff] [blame] | 1796 | |
Alan Cox | 963e497 | 2008-07-24 17:16:06 +0100 | [diff] [blame] | 1797 | if (ap->ops->read_id) |
| 1798 | err_mask = ap->ops->read_id(dev, &tf, id); |
| 1799 | else |
| 1800 | err_mask = ata_do_dev_read_id(dev, &tf, id); |
| 1801 | |
Tejun Heo | 49016ac | 2006-02-21 02:12:11 +0900 | [diff] [blame] | 1802 | if (err_mask) { |
Tejun Heo | 800b399 | 2006-12-03 21:34:13 +0900 | [diff] [blame] | 1803 | if (err_mask & AC_ERR_NODEV_HINT) { |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 1804 | ata_dev_dbg(dev, "NODEV after polling detection\n"); |
Tejun Heo | 55a8e2c | 2006-11-10 18:08:10 +0900 | [diff] [blame] | 1805 | return -ENOENT; |
| 1806 | } |
| 1807 | |
Tejun Heo | 79b42ba | 2009-04-15 06:21:10 +0900 | [diff] [blame] | 1808 | if (is_semb) { |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 1809 | ata_dev_info(dev, |
| 1810 | "IDENTIFY failed on device w/ SEMB sig, disabled\n"); |
Tejun Heo | 79b42ba | 2009-04-15 06:21:10 +0900 | [diff] [blame] | 1811 | /* SEMB is not supported yet */ |
| 1812 | *p_class = ATA_DEV_SEMB_UNSUP; |
| 1813 | return 0; |
| 1814 | } |
| 1815 | |
Tejun Heo | 1ffc151 | 2008-03-23 15:16:53 +0900 | [diff] [blame] | 1816 | if ((err_mask == AC_ERR_DEV) && (tf.feature & ATA_ABORTED)) { |
| 1817 | /* Device or controller might have reported |
| 1818 | * the wrong device class. Give a shot at the |
| 1819 | * other IDENTIFY if the current one is |
| 1820 | * aborted by the device. |
| 1821 | */ |
| 1822 | if (may_fallback) { |
| 1823 | may_fallback = 0; |
Tejun Heo | 54936f8 | 2007-05-11 14:35:29 +0200 | [diff] [blame] | 1824 | |
Tejun Heo | 1ffc151 | 2008-03-23 15:16:53 +0900 | [diff] [blame] | 1825 | if (class == ATA_DEV_ATA) |
| 1826 | class = ATA_DEV_ATAPI; |
| 1827 | else |
| 1828 | class = ATA_DEV_ATA; |
| 1829 | goto retry; |
| 1830 | } |
| 1831 | |
| 1832 | /* Control reaches here iff the device aborted |
| 1833 | * both flavors of IDENTIFYs which happens |
| 1834 | * sometimes with phantom devices. |
| 1835 | */ |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 1836 | ata_dev_dbg(dev, |
| 1837 | "both IDENTIFYs aborted, assuming NODEV\n"); |
Tejun Heo | 1ffc151 | 2008-03-23 15:16:53 +0900 | [diff] [blame] | 1838 | return -ENOENT; |
Tejun Heo | 54936f8 | 2007-05-11 14:35:29 +0200 | [diff] [blame] | 1839 | } |
| 1840 | |
Tejun Heo | 49016ac | 2006-02-21 02:12:11 +0900 | [diff] [blame] | 1841 | rc = -EIO; |
| 1842 | reason = "I/O error"; |
Tejun Heo | 49016ac | 2006-02-21 02:12:11 +0900 | [diff] [blame] | 1843 | goto err_out; |
| 1844 | } |
| 1845 | |
Tejun Heo | 43c9c59 | 2010-05-23 12:59:11 +0200 | [diff] [blame] | 1846 | if (dev->horkage & ATA_HORKAGE_DUMP_ID) { |
Hannes Reinecke | 4baa574 | 2021-12-21 08:20:28 +0100 | [diff] [blame] | 1847 | ata_dev_info(dev, "dumping IDENTIFY data, " |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 1848 | "class=%d may_fallback=%d tried_spinup=%d\n", |
| 1849 | class, may_fallback, tried_spinup); |
Hannes Reinecke | 4baa574 | 2021-12-21 08:20:28 +0100 | [diff] [blame] | 1850 | print_hex_dump(KERN_INFO, "", DUMP_PREFIX_OFFSET, |
Tejun Heo | 43c9c59 | 2010-05-23 12:59:11 +0200 | [diff] [blame] | 1851 | 16, 2, id, ATA_ID_WORDS * sizeof(*id), true); |
| 1852 | } |
| 1853 | |
Tejun Heo | 54936f8 | 2007-05-11 14:35:29 +0200 | [diff] [blame] | 1854 | /* Falling back doesn't make sense if ID data was read |
| 1855 | * successfully at least once. |
| 1856 | */ |
| 1857 | may_fallback = 0; |
| 1858 | |
Tejun Heo | 49016ac | 2006-02-21 02:12:11 +0900 | [diff] [blame] | 1859 | swap_buf_le16(id, ATA_ID_WORDS); |
| 1860 | |
Tejun Heo | 49016ac | 2006-02-21 02:12:11 +0900 | [diff] [blame] | 1861 | /* sanity check */ |
Tejun Heo | a4f5749 | 2006-09-12 20:35:49 -0700 | [diff] [blame] | 1862 | rc = -EINVAL; |
Alan Cox | 6070068 | 2007-06-07 16:13:55 +0100 | [diff] [blame] | 1863 | reason = "device reports invalid type"; |
Tejun Heo | a4f5749 | 2006-09-12 20:35:49 -0700 | [diff] [blame] | 1864 | |
Hannes Reinecke | 9162c65 | 2014-11-05 13:08:21 +0100 | [diff] [blame] | 1865 | if (class == ATA_DEV_ATA || class == ATA_DEV_ZAC) { |
Tejun Heo | a4f5749 | 2006-09-12 20:35:49 -0700 | [diff] [blame] | 1866 | if (!ata_id_is_ata(id) && !ata_id_is_cfa(id)) |
| 1867 | goto err_out; |
Andy Whitcroft | db63a4c | 2012-05-04 22:15:10 +0100 | [diff] [blame] | 1868 | if (ap->host->flags & ATA_HOST_IGNORE_ATA && |
| 1869 | ata_id_is_ata(id)) { |
| 1870 | ata_dev_dbg(dev, |
| 1871 | "host indicates ignore ATA devices, ignored\n"); |
| 1872 | return -ENOENT; |
| 1873 | } |
Tejun Heo | a4f5749 | 2006-09-12 20:35:49 -0700 | [diff] [blame] | 1874 | } else { |
| 1875 | if (ata_id_is_ata(id)) |
| 1876 | goto err_out; |
Tejun Heo | 49016ac | 2006-02-21 02:12:11 +0900 | [diff] [blame] | 1877 | } |
| 1878 | |
Mark Lord | 169439c | 2007-04-17 18:26:07 -0400 | [diff] [blame] | 1879 | if (!tried_spinup && (id[2] == 0x37c8 || id[2] == 0x738c)) { |
| 1880 | tried_spinup = 1; |
| 1881 | /* |
| 1882 | * Drive powered-up in standby mode, and requires a specific |
| 1883 | * SET_FEATURES spin-up subcommand before it will accept |
| 1884 | * anything other than the original IDENTIFY command. |
| 1885 | */ |
Jeff Garzik | 218f3d3 | 2007-10-25 00:33:27 -0400 | [diff] [blame] | 1886 | err_mask = ata_dev_set_feature(dev, SETFEATURES_SPINUP, 0); |
Ryan Power | fb0582f9 | 2007-08-10 13:59:35 -0700 | [diff] [blame] | 1887 | if (err_mask && id[2] != 0x738c) { |
Mark Lord | 169439c | 2007-04-17 18:26:07 -0400 | [diff] [blame] | 1888 | rc = -EIO; |
| 1889 | reason = "SPINUP failed"; |
| 1890 | goto err_out; |
| 1891 | } |
| 1892 | /* |
| 1893 | * If the drive initially returned incomplete IDENTIFY info, |
| 1894 | * we now must reissue the IDENTIFY command. |
| 1895 | */ |
| 1896 | if (id[2] == 0x37c8) |
| 1897 | goto retry; |
| 1898 | } |
| 1899 | |
Hannes Reinecke | 9162c65 | 2014-11-05 13:08:21 +0100 | [diff] [blame] | 1900 | if ((flags & ATA_READID_POSTRESET) && |
| 1901 | (class == ATA_DEV_ATA || class == ATA_DEV_ZAC)) { |
Tejun Heo | 49016ac | 2006-02-21 02:12:11 +0900 | [diff] [blame] | 1902 | /* |
| 1903 | * The exact sequence expected by certain pre-ATA4 drives is: |
| 1904 | * SRST RESET |
Alan Cox | 50a9901 | 2007-08-08 14:27:00 +0100 | [diff] [blame] | 1905 | * IDENTIFY (optional in early ATA) |
| 1906 | * INITIALIZE DEVICE PARAMETERS (later IDE and ATA) |
Tejun Heo | 49016ac | 2006-02-21 02:12:11 +0900 | [diff] [blame] | 1907 | * anything else.. |
| 1908 | * Some drives were very specific about that exact sequence. |
Alan Cox | 50a9901 | 2007-08-08 14:27:00 +0100 | [diff] [blame] | 1909 | * |
| 1910 | * Note that ATA4 says lba is mandatory so the second check |
Adam Buchbinder | c9404c9 | 2009-12-18 15:40:42 -0500 | [diff] [blame] | 1911 | * should never trigger. |
Tejun Heo | 49016ac | 2006-02-21 02:12:11 +0900 | [diff] [blame] | 1912 | */ |
| 1913 | if (ata_id_major_version(id) < 4 || !ata_id_has_lba(id)) { |
Tejun Heo | 3373efd | 2006-05-15 20:57:53 +0900 | [diff] [blame] | 1914 | err_mask = ata_dev_init_params(dev, id[3], id[6]); |
Tejun Heo | 49016ac | 2006-02-21 02:12:11 +0900 | [diff] [blame] | 1915 | if (err_mask) { |
| 1916 | rc = -EIO; |
| 1917 | reason = "INIT_DEV_PARAMS failed"; |
| 1918 | goto err_out; |
| 1919 | } |
| 1920 | |
| 1921 | /* current CHS translation info (id[53-58]) might be |
| 1922 | * changed. reread the identify device info. |
| 1923 | */ |
Tejun Heo | bff0464 | 2006-11-10 18:08:10 +0900 | [diff] [blame] | 1924 | flags &= ~ATA_READID_POSTRESET; |
Tejun Heo | 49016ac | 2006-02-21 02:12:11 +0900 | [diff] [blame] | 1925 | goto retry; |
| 1926 | } |
| 1927 | } |
| 1928 | |
| 1929 | *p_class = class; |
Tejun Heo | fe635c7 | 2006-05-15 20:57:35 +0900 | [diff] [blame] | 1930 | |
Tejun Heo | 49016ac | 2006-02-21 02:12:11 +0900 | [diff] [blame] | 1931 | return 0; |
| 1932 | |
| 1933 | err_out: |
Hannes Reinecke | 16d4246 | 2021-12-21 08:21:04 +0100 | [diff] [blame] | 1934 | ata_dev_warn(dev, "failed to IDENTIFY (%s, err_mask=0x%x)\n", |
| 1935 | reason, err_mask); |
Tejun Heo | 49016ac | 2006-02-21 02:12:11 +0900 | [diff] [blame] | 1936 | return rc; |
| 1937 | } |
| 1938 | |
Christoph Hellwig | f01f62c | 2017-06-04 14:42:20 +0200 | [diff] [blame] | 1939 | /** |
| 1940 | * ata_read_log_page - read a specific log page |
| 1941 | * @dev: target device |
| 1942 | * @log: log to read |
| 1943 | * @page: page to read |
| 1944 | * @buf: buffer to store read page |
| 1945 | * @sectors: number of sectors to read |
| 1946 | * |
| 1947 | * Read log page using READ_LOG_EXT command. |
| 1948 | * |
| 1949 | * LOCKING: |
| 1950 | * Kernel thread context (may sleep). |
| 1951 | * |
| 1952 | * RETURNS: |
| 1953 | * 0 on success, AC_ERR_* mask otherwise. |
| 1954 | */ |
| 1955 | unsigned int ata_read_log_page(struct ata_device *dev, u8 log, |
| 1956 | u8 page, void *buf, unsigned int sectors) |
| 1957 | { |
| 1958 | unsigned long ap_flags = dev->link->ap->flags; |
| 1959 | struct ata_taskfile tf; |
| 1960 | unsigned int err_mask; |
| 1961 | bool dma = false; |
| 1962 | |
Hannes Reinecke | 4633778 | 2021-12-21 08:20:37 +0100 | [diff] [blame] | 1963 | ata_dev_dbg(dev, "read log page - log 0x%x, page 0x%x\n", log, page); |
Christoph Hellwig | f01f62c | 2017-06-04 14:42:20 +0200 | [diff] [blame] | 1964 | |
| 1965 | /* |
| 1966 | * Return error without actually issuing the command on controllers |
| 1967 | * which e.g. lockup on a read log page. |
| 1968 | */ |
| 1969 | if (ap_flags & ATA_FLAG_NO_LOG_PAGE) |
| 1970 | return AC_ERR_DEV; |
| 1971 | |
| 1972 | retry: |
| 1973 | ata_tf_init(dev, &tf); |
Reimar Döffinger | f971a85 | 2021-10-12 08:27:44 +0200 | [diff] [blame] | 1974 | if (ata_dma_enabled(dev) && ata_id_has_read_log_dma_ext(dev->id) && |
Damien Le Moal | 7cfdfdc | 2017-07-10 14:45:20 +0900 | [diff] [blame] | 1975 | !(dev->horkage & ATA_HORKAGE_NO_DMA_LOG)) { |
Christoph Hellwig | f01f62c | 2017-06-04 14:42:20 +0200 | [diff] [blame] | 1976 | tf.command = ATA_CMD_READ_LOG_DMA_EXT; |
| 1977 | tf.protocol = ATA_PROT_DMA; |
| 1978 | dma = true; |
| 1979 | } else { |
| 1980 | tf.command = ATA_CMD_READ_LOG_EXT; |
| 1981 | tf.protocol = ATA_PROT_PIO; |
| 1982 | dma = false; |
| 1983 | } |
| 1984 | tf.lbal = log; |
| 1985 | tf.lbam = page; |
| 1986 | tf.nsect = sectors; |
| 1987 | tf.hob_nsect = sectors >> 8; |
| 1988 | tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_LBA48 | ATA_TFLAG_DEVICE; |
| 1989 | |
| 1990 | err_mask = ata_exec_internal(dev, &tf, NULL, DMA_FROM_DEVICE, |
| 1991 | buf, sectors * ATA_SECT_SIZE, 0); |
| 1992 | |
Damien Le Moal | fc5c8aa | 2021-08-16 10:44:52 +0900 | [diff] [blame] | 1993 | if (err_mask) { |
| 1994 | if (dma) { |
| 1995 | dev->horkage |= ATA_HORKAGE_NO_DMA_LOG; |
| 1996 | goto retry; |
| 1997 | } |
Damien Le Moal | 23ef63d | 2021-11-15 12:37:46 +0900 | [diff] [blame] | 1998 | ata_dev_err(dev, |
| 1999 | "Read log 0x%02x page 0x%02x failed, Emask 0x%x\n", |
| 2000 | (unsigned int)log, (unsigned int)page, err_mask); |
Christoph Hellwig | f01f62c | 2017-06-04 14:42:20 +0200 | [diff] [blame] | 2001 | } |
| 2002 | |
Christoph Hellwig | f01f62c | 2017-06-04 14:42:20 +0200 | [diff] [blame] | 2003 | return err_mask; |
| 2004 | } |
| 2005 | |
Christoph Hellwig | efe205a | 2017-06-04 14:42:21 +0200 | [diff] [blame] | 2006 | static bool ata_log_supported(struct ata_device *dev, u8 log) |
| 2007 | { |
| 2008 | struct ata_port *ap = dev->link->ap; |
| 2009 | |
| 2010 | if (ata_read_log_page(dev, ATA_LOG_DIRECTORY, 0, ap->sector_buf, 1)) |
| 2011 | return false; |
| 2012 | return get_unaligned_le16(&ap->sector_buf[log * 2]) ? true : false; |
| 2013 | } |
| 2014 | |
Christoph Hellwig | a0fd245 | 2017-06-04 14:42:23 +0200 | [diff] [blame] | 2015 | static bool ata_identify_page_supported(struct ata_device *dev, u8 page) |
| 2016 | { |
| 2017 | struct ata_port *ap = dev->link->ap; |
| 2018 | unsigned int err, i; |
| 2019 | |
Damien Le Moal | 636f6e2 | 2021-11-09 08:45:25 +0900 | [diff] [blame] | 2020 | if (dev->horkage & ATA_HORKAGE_NO_ID_DEV_LOG) |
| 2021 | return false; |
| 2022 | |
Christoph Hellwig | a0fd245 | 2017-06-04 14:42:23 +0200 | [diff] [blame] | 2023 | if (!ata_log_supported(dev, ATA_LOG_IDENTIFY_DEVICE)) { |
Damien Le Moal | 636f6e2 | 2021-11-09 08:45:25 +0900 | [diff] [blame] | 2024 | /* |
| 2025 | * IDENTIFY DEVICE data log is defined as mandatory starting |
| 2026 | * with ACS-3 (ATA version 10). Warn about the missing log |
| 2027 | * for drives which implement this ATA level or above. |
| 2028 | */ |
| 2029 | if (ata_id_major_version(dev->id) >= 10) |
| 2030 | ata_dev_warn(dev, |
| 2031 | "ATA Identify Device Log not supported\n"); |
| 2032 | dev->horkage |= ATA_HORKAGE_NO_ID_DEV_LOG; |
Christoph Hellwig | a0fd245 | 2017-06-04 14:42:23 +0200 | [diff] [blame] | 2033 | return false; |
| 2034 | } |
| 2035 | |
| 2036 | /* |
| 2037 | * Read IDENTIFY DEVICE data log, page 0, to figure out if the page is |
| 2038 | * supported. |
| 2039 | */ |
| 2040 | err = ata_read_log_page(dev, ATA_LOG_IDENTIFY_DEVICE, 0, ap->sector_buf, |
| 2041 | 1); |
Damien Le Moal | fc5c8aa | 2021-08-16 10:44:52 +0900 | [diff] [blame] | 2042 | if (err) |
Christoph Hellwig | a0fd245 | 2017-06-04 14:42:23 +0200 | [diff] [blame] | 2043 | return false; |
Christoph Hellwig | a0fd245 | 2017-06-04 14:42:23 +0200 | [diff] [blame] | 2044 | |
| 2045 | for (i = 0; i < ap->sector_buf[8]; i++) { |
| 2046 | if (ap->sector_buf[9 + i] == page) |
| 2047 | return true; |
| 2048 | } |
| 2049 | |
| 2050 | return false; |
| 2051 | } |
| 2052 | |
Tejun Heo | 9062712 | 2009-01-29 20:31:36 +0900 | [diff] [blame] | 2053 | static int ata_do_link_spd_horkage(struct ata_device *dev) |
| 2054 | { |
| 2055 | struct ata_link *plink = ata_dev_phys_link(dev); |
| 2056 | u32 target, target_limit; |
| 2057 | |
| 2058 | if (!sata_scr_valid(plink)) |
| 2059 | return 0; |
| 2060 | |
| 2061 | if (dev->horkage & ATA_HORKAGE_1_5_GBPS) |
| 2062 | target = 1; |
| 2063 | else |
| 2064 | return 0; |
| 2065 | |
| 2066 | target_limit = (1 << target) - 1; |
| 2067 | |
| 2068 | /* if already on stricter limit, no need to push further */ |
| 2069 | if (plink->sata_spd_limit <= target_limit) |
| 2070 | return 0; |
| 2071 | |
| 2072 | plink->sata_spd_limit = target_limit; |
| 2073 | |
| 2074 | /* Request another EH round by returning -EAGAIN if link is |
| 2075 | * going faster than the target speed. Forward progress is |
| 2076 | * guaranteed by setting sata_spd_limit to target_limit above. |
| 2077 | */ |
| 2078 | if (plink->sata_spd > target) { |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 2079 | ata_dev_info(dev, "applying link speed limit horkage to %s\n", |
| 2080 | sata_spd_string(target)); |
Tejun Heo | 9062712 | 2009-01-29 20:31:36 +0900 | [diff] [blame] | 2081 | return -EAGAIN; |
| 2082 | } |
| 2083 | return 0; |
| 2084 | } |
| 2085 | |
Tejun Heo | 3373efd | 2006-05-15 20:57:53 +0900 | [diff] [blame] | 2086 | static inline u8 ata_dev_knobble(struct ata_device *dev) |
Tejun Heo | 4b2f3ed | 2006-03-01 16:09:36 +0900 | [diff] [blame] | 2087 | { |
Tejun Heo | 9af5c9c | 2007-08-06 18:36:22 +0900 | [diff] [blame] | 2088 | struct ata_port *ap = dev->link->ap; |
Jens Axboe | 9ce8e30 | 2008-08-27 15:23:18 +0200 | [diff] [blame] | 2089 | |
| 2090 | if (ata_dev_blacklisted(dev) & ATA_HORKAGE_BRIDGE_OK) |
| 2091 | return 0; |
| 2092 | |
Tejun Heo | 9af5c9c | 2007-08-06 18:36:22 +0900 | [diff] [blame] | 2093 | return ((ap->cbl == ATA_CBL_SATA) && (!ata_id_is_sata(dev->id))); |
Tejun Heo | 4b2f3ed | 2006-03-01 16:09:36 +0900 | [diff] [blame] | 2094 | } |
| 2095 | |
Hannes Reinecke | 5a23355 | 2016-04-25 12:45:46 +0200 | [diff] [blame] | 2096 | static void ata_dev_config_ncq_send_recv(struct ata_device *dev) |
| 2097 | { |
| 2098 | struct ata_port *ap = dev->link->ap; |
| 2099 | unsigned int err_mask; |
| 2100 | |
Christoph Hellwig | efe205a | 2017-06-04 14:42:21 +0200 | [diff] [blame] | 2101 | if (!ata_log_supported(dev, ATA_LOG_NCQ_SEND_RECV)) { |
| 2102 | ata_dev_warn(dev, "NCQ Send/Recv Log not supported\n"); |
Hannes Reinecke | fe5af0c | 2016-04-25 12:45:48 +0200 | [diff] [blame] | 2103 | return; |
| 2104 | } |
Hannes Reinecke | 5a23355 | 2016-04-25 12:45:46 +0200 | [diff] [blame] | 2105 | err_mask = ata_read_log_page(dev, ATA_LOG_NCQ_SEND_RECV, |
| 2106 | 0, ap->sector_buf, 1); |
Damien Le Moal | fc5c8aa | 2021-08-16 10:44:52 +0900 | [diff] [blame] | 2107 | if (!err_mask) { |
Hannes Reinecke | 5a23355 | 2016-04-25 12:45:46 +0200 | [diff] [blame] | 2108 | u8 *cmds = dev->ncq_send_recv_cmds; |
| 2109 | |
| 2110 | dev->flags |= ATA_DFLAG_NCQ_SEND_RECV; |
| 2111 | memcpy(cmds, ap->sector_buf, ATA_LOG_NCQ_SEND_RECV_SIZE); |
| 2112 | |
| 2113 | if (dev->horkage & ATA_HORKAGE_NO_NCQ_TRIM) { |
| 2114 | ata_dev_dbg(dev, "disabling queued TRIM support\n"); |
| 2115 | cmds[ATA_LOG_NCQ_SEND_RECV_DSM_OFFSET] &= |
| 2116 | ~ATA_LOG_NCQ_SEND_RECV_DSM_TRIM; |
| 2117 | } |
| 2118 | } |
| 2119 | } |
| 2120 | |
Hannes Reinecke | 284b3b7 | 2016-04-25 12:45:54 +0200 | [diff] [blame] | 2121 | static void ata_dev_config_ncq_non_data(struct ata_device *dev) |
| 2122 | { |
| 2123 | struct ata_port *ap = dev->link->ap; |
| 2124 | unsigned int err_mask; |
Hannes Reinecke | 284b3b7 | 2016-04-25 12:45:54 +0200 | [diff] [blame] | 2125 | |
Christoph Hellwig | efe205a | 2017-06-04 14:42:21 +0200 | [diff] [blame] | 2126 | if (!ata_log_supported(dev, ATA_LOG_NCQ_NON_DATA)) { |
Hannes Reinecke | 284b3b7 | 2016-04-25 12:45:54 +0200 | [diff] [blame] | 2127 | ata_dev_warn(dev, |
| 2128 | "NCQ Send/Recv Log not supported\n"); |
| 2129 | return; |
| 2130 | } |
| 2131 | err_mask = ata_read_log_page(dev, ATA_LOG_NCQ_NON_DATA, |
| 2132 | 0, ap->sector_buf, 1); |
Damien Le Moal | fc5c8aa | 2021-08-16 10:44:52 +0900 | [diff] [blame] | 2133 | if (!err_mask) { |
Hannes Reinecke | 284b3b7 | 2016-04-25 12:45:54 +0200 | [diff] [blame] | 2134 | u8 *cmds = dev->ncq_non_data_cmds; |
| 2135 | |
| 2136 | memcpy(cmds, ap->sector_buf, ATA_LOG_NCQ_NON_DATA_SIZE); |
| 2137 | } |
| 2138 | } |
| 2139 | |
Adam Manzanares | 8e06178 | 2016-10-17 11:27:29 -0700 | [diff] [blame] | 2140 | static void ata_dev_config_ncq_prio(struct ata_device *dev) |
| 2141 | { |
| 2142 | struct ata_port *ap = dev->link->ap; |
| 2143 | unsigned int err_mask; |
| 2144 | |
Damien Le Moal | 06f6c4c | 2021-11-15 12:47:26 +0900 | [diff] [blame] | 2145 | if (!ata_identify_page_supported(dev, ATA_LOG_SATA_SETTINGS)) |
| 2146 | return; |
| 2147 | |
Adam Manzanares | 8e06178 | 2016-10-17 11:27:29 -0700 | [diff] [blame] | 2148 | err_mask = ata_read_log_page(dev, |
Christoph Hellwig | 1d51d5f | 2017-06-04 14:42:22 +0200 | [diff] [blame] | 2149 | ATA_LOG_IDENTIFY_DEVICE, |
Adam Manzanares | 8e06178 | 2016-10-17 11:27:29 -0700 | [diff] [blame] | 2150 | ATA_LOG_SATA_SETTINGS, |
| 2151 | ap->sector_buf, |
| 2152 | 1); |
Damien Le Moal | fc5c8aa | 2021-08-16 10:44:52 +0900 | [diff] [blame] | 2153 | if (err_mask) |
Damien Le Moal | 2360fa1 | 2021-08-16 10:44:51 +0900 | [diff] [blame] | 2154 | goto not_supported; |
Adam Manzanares | 8e06178 | 2016-10-17 11:27:29 -0700 | [diff] [blame] | 2155 | |
Damien Le Moal | 2360fa1 | 2021-08-16 10:44:51 +0900 | [diff] [blame] | 2156 | if (!(ap->sector_buf[ATA_LOG_NCQ_PRIO_OFFSET] & BIT(3))) |
| 2157 | goto not_supported; |
Adam Manzanares | 8e06178 | 2016-10-17 11:27:29 -0700 | [diff] [blame] | 2158 | |
Damien Le Moal | 2360fa1 | 2021-08-16 10:44:51 +0900 | [diff] [blame] | 2159 | dev->flags |= ATA_DFLAG_NCQ_PRIO; |
| 2160 | |
| 2161 | return; |
| 2162 | |
| 2163 | not_supported: |
| 2164 | dev->flags &= ~ATA_DFLAG_NCQ_PRIO_ENABLE; |
| 2165 | dev->flags &= ~ATA_DFLAG_NCQ_PRIO; |
Adam Manzanares | 8e06178 | 2016-10-17 11:27:29 -0700 | [diff] [blame] | 2166 | } |
| 2167 | |
Kate Hsuan | 7a8526a | 2021-09-03 17:44:11 +0800 | [diff] [blame] | 2168 | static bool ata_dev_check_adapter(struct ata_device *dev, |
| 2169 | unsigned short vendor_id) |
| 2170 | { |
| 2171 | struct pci_dev *pcidev = NULL; |
| 2172 | struct device *parent_dev = NULL; |
| 2173 | |
| 2174 | for (parent_dev = dev->tdev.parent; parent_dev != NULL; |
| 2175 | parent_dev = parent_dev->parent) { |
| 2176 | if (dev_is_pci(parent_dev)) { |
| 2177 | pcidev = to_pci_dev(parent_dev); |
| 2178 | if (pcidev->vendor == vendor_id) |
| 2179 | return true; |
| 2180 | break; |
| 2181 | } |
| 2182 | } |
| 2183 | |
| 2184 | return false; |
| 2185 | } |
| 2186 | |
Shaohua Li | 388539f | 2009-07-27 09:24:35 +0800 | [diff] [blame] | 2187 | static int ata_dev_config_ncq(struct ata_device *dev, |
Tejun Heo | a6e6ce8 | 2006-05-15 21:03:48 +0900 | [diff] [blame] | 2188 | char *desc, size_t desc_sz) |
| 2189 | { |
Tejun Heo | 9af5c9c | 2007-08-06 18:36:22 +0900 | [diff] [blame] | 2190 | struct ata_port *ap = dev->link->ap; |
Tejun Heo | a6e6ce8 | 2006-05-15 21:03:48 +0900 | [diff] [blame] | 2191 | int hdepth = 0, ddepth = ata_id_queue_depth(dev->id); |
Shaohua Li | 388539f | 2009-07-27 09:24:35 +0800 | [diff] [blame] | 2192 | unsigned int err_mask; |
| 2193 | char *aa_desc = ""; |
Tejun Heo | a6e6ce8 | 2006-05-15 21:03:48 +0900 | [diff] [blame] | 2194 | |
| 2195 | if (!ata_id_has_ncq(dev->id)) { |
| 2196 | desc[0] = '\0'; |
Shaohua Li | 388539f | 2009-07-27 09:24:35 +0800 | [diff] [blame] | 2197 | return 0; |
Tejun Heo | a6e6ce8 | 2006-05-15 21:03:48 +0900 | [diff] [blame] | 2198 | } |
Bartlomiej Zolnierkiewicz | cba97ea | 2020-03-26 16:58:09 +0100 | [diff] [blame] | 2199 | if (!IS_ENABLED(CONFIG_SATA_HOST)) |
| 2200 | return 0; |
Tejun Heo | 75683fe | 2007-07-05 13:31:27 +0900 | [diff] [blame] | 2201 | if (dev->horkage & ATA_HORKAGE_NONCQ) { |
Alan Cox | 6919a0a | 2006-10-27 19:08:46 -0700 | [diff] [blame] | 2202 | snprintf(desc, desc_sz, "NCQ (not used)"); |
Shaohua Li | 388539f | 2009-07-27 09:24:35 +0800 | [diff] [blame] | 2203 | return 0; |
Alan Cox | 6919a0a | 2006-10-27 19:08:46 -0700 | [diff] [blame] | 2204 | } |
Kate Hsuan | 7a8526a | 2021-09-03 17:44:11 +0800 | [diff] [blame] | 2205 | |
| 2206 | if (dev->horkage & ATA_HORKAGE_NO_NCQ_ON_ATI && |
| 2207 | ata_dev_check_adapter(dev, PCI_VENDOR_ID_ATI)) { |
| 2208 | snprintf(desc, desc_sz, "NCQ (not used)"); |
| 2209 | return 0; |
| 2210 | } |
| 2211 | |
Tejun Heo | a6e6ce8 | 2006-05-15 21:03:48 +0900 | [diff] [blame] | 2212 | if (ap->flags & ATA_FLAG_NCQ) { |
Jens Axboe | 69278f7 | 2018-05-11 12:51:10 -0600 | [diff] [blame] | 2213 | hdepth = min(ap->scsi_host->can_queue, ATA_MAX_QUEUE); |
Tejun Heo | a6e6ce8 | 2006-05-15 21:03:48 +0900 | [diff] [blame] | 2214 | dev->flags |= ATA_DFLAG_NCQ; |
| 2215 | } |
| 2216 | |
Shaohua Li | 388539f | 2009-07-27 09:24:35 +0800 | [diff] [blame] | 2217 | if (!(dev->horkage & ATA_HORKAGE_BROKEN_FPDMA_AA) && |
| 2218 | (ap->flags & ATA_FLAG_FPDMA_AA) && |
| 2219 | ata_id_has_fpdma_aa(dev->id)) { |
| 2220 | err_mask = ata_dev_set_feature(dev, SETFEATURES_SATA_ENABLE, |
| 2221 | SATA_FPDMA_AA); |
| 2222 | if (err_mask) { |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 2223 | ata_dev_err(dev, |
| 2224 | "failed to enable AA (error_mask=0x%x)\n", |
| 2225 | err_mask); |
Shaohua Li | 388539f | 2009-07-27 09:24:35 +0800 | [diff] [blame] | 2226 | if (err_mask != AC_ERR_DEV) { |
| 2227 | dev->horkage |= ATA_HORKAGE_BROKEN_FPDMA_AA; |
| 2228 | return -EIO; |
| 2229 | } |
| 2230 | } else |
| 2231 | aa_desc = ", AA"; |
| 2232 | } |
| 2233 | |
Tejun Heo | a6e6ce8 | 2006-05-15 21:03:48 +0900 | [diff] [blame] | 2234 | if (hdepth >= ddepth) |
Shaohua Li | 388539f | 2009-07-27 09:24:35 +0800 | [diff] [blame] | 2235 | snprintf(desc, desc_sz, "NCQ (depth %d)%s", ddepth, aa_desc); |
Tejun Heo | a6e6ce8 | 2006-05-15 21:03:48 +0900 | [diff] [blame] | 2236 | else |
Shaohua Li | 388539f | 2009-07-27 09:24:35 +0800 | [diff] [blame] | 2237 | snprintf(desc, desc_sz, "NCQ (depth %d/%d)%s", hdepth, |
| 2238 | ddepth, aa_desc); |
Marc Carino | ed36911 | 2013-08-24 23:22:50 -0700 | [diff] [blame] | 2239 | |
Hannes Reinecke | 284b3b7 | 2016-04-25 12:45:54 +0200 | [diff] [blame] | 2240 | if ((ap->flags & ATA_FLAG_FPDMA_AUX)) { |
| 2241 | if (ata_id_has_ncq_send_and_recv(dev->id)) |
| 2242 | ata_dev_config_ncq_send_recv(dev); |
| 2243 | if (ata_id_has_ncq_non_data(dev->id)) |
| 2244 | ata_dev_config_ncq_non_data(dev); |
Adam Manzanares | 8e06178 | 2016-10-17 11:27:29 -0700 | [diff] [blame] | 2245 | if (ata_id_has_ncq_prio(dev->id)) |
| 2246 | ata_dev_config_ncq_prio(dev); |
Marc Carino | ed36911 | 2013-08-24 23:22:50 -0700 | [diff] [blame] | 2247 | } |
| 2248 | |
Shaohua Li | 388539f | 2009-07-27 09:24:35 +0800 | [diff] [blame] | 2249 | return 0; |
Tejun Heo | a6e6ce8 | 2006-05-15 21:03:48 +0900 | [diff] [blame] | 2250 | } |
| 2251 | |
Hannes Reinecke | e87fd28 | 2016-04-04 11:43:55 +0200 | [diff] [blame] | 2252 | static void ata_dev_config_sense_reporting(struct ata_device *dev) |
| 2253 | { |
| 2254 | unsigned int err_mask; |
| 2255 | |
| 2256 | if (!ata_id_has_sense_reporting(dev->id)) |
| 2257 | return; |
| 2258 | |
| 2259 | if (ata_id_sense_reporting_enabled(dev->id)) |
| 2260 | return; |
| 2261 | |
| 2262 | err_mask = ata_dev_set_feature(dev, SETFEATURE_SENSE_DATA, 0x1); |
| 2263 | if (err_mask) { |
| 2264 | ata_dev_dbg(dev, |
| 2265 | "failed to enable Sense Data Reporting, Emask 0x%x\n", |
| 2266 | err_mask); |
| 2267 | } |
| 2268 | } |
| 2269 | |
Hannes Reinecke | 6d1003a | 2016-04-25 12:45:56 +0200 | [diff] [blame] | 2270 | static void ata_dev_config_zac(struct ata_device *dev) |
| 2271 | { |
| 2272 | struct ata_port *ap = dev->link->ap; |
| 2273 | unsigned int err_mask; |
| 2274 | u8 *identify_buf = ap->sector_buf; |
Hannes Reinecke | 6d1003a | 2016-04-25 12:45:56 +0200 | [diff] [blame] | 2275 | |
| 2276 | dev->zac_zones_optimal_open = U32_MAX; |
| 2277 | dev->zac_zones_optimal_nonseq = U32_MAX; |
| 2278 | dev->zac_zones_max_open = U32_MAX; |
| 2279 | |
| 2280 | /* |
| 2281 | * Always set the 'ZAC' flag for Host-managed devices. |
| 2282 | */ |
| 2283 | if (dev->class == ATA_DEV_ZAC) |
| 2284 | dev->flags |= ATA_DFLAG_ZAC; |
| 2285 | else if (ata_id_zoned_cap(dev->id) == 0x01) |
| 2286 | /* |
| 2287 | * Check for host-aware devices. |
| 2288 | */ |
| 2289 | dev->flags |= ATA_DFLAG_ZAC; |
| 2290 | |
| 2291 | if (!(dev->flags & ATA_DFLAG_ZAC)) |
| 2292 | return; |
| 2293 | |
Christoph Hellwig | a0fd245 | 2017-06-04 14:42:23 +0200 | [diff] [blame] | 2294 | if (!ata_identify_page_supported(dev, ATA_LOG_ZONED_INFORMATION)) { |
Hannes Reinecke | 6d1003a | 2016-04-25 12:45:56 +0200 | [diff] [blame] | 2295 | ata_dev_warn(dev, |
| 2296 | "ATA Zoned Information Log not supported\n"); |
| 2297 | return; |
| 2298 | } |
| 2299 | |
| 2300 | /* |
| 2301 | * Read IDENTIFY DEVICE data log, page 9 (Zoned-device information) |
| 2302 | */ |
Christoph Hellwig | 1d51d5f | 2017-06-04 14:42:22 +0200 | [diff] [blame] | 2303 | err_mask = ata_read_log_page(dev, ATA_LOG_IDENTIFY_DEVICE, |
Hannes Reinecke | 6d1003a | 2016-04-25 12:45:56 +0200 | [diff] [blame] | 2304 | ATA_LOG_ZONED_INFORMATION, |
| 2305 | identify_buf, 1); |
| 2306 | if (!err_mask) { |
| 2307 | u64 zoned_cap, opt_open, opt_nonseq, max_open; |
| 2308 | |
| 2309 | zoned_cap = get_unaligned_le64(&identify_buf[8]); |
| 2310 | if ((zoned_cap >> 63)) |
| 2311 | dev->zac_zoned_cap = (zoned_cap & 1); |
| 2312 | opt_open = get_unaligned_le64(&identify_buf[24]); |
| 2313 | if ((opt_open >> 63)) |
| 2314 | dev->zac_zones_optimal_open = (u32)opt_open; |
| 2315 | opt_nonseq = get_unaligned_le64(&identify_buf[32]); |
| 2316 | if ((opt_nonseq >> 63)) |
| 2317 | dev->zac_zones_optimal_nonseq = (u32)opt_nonseq; |
| 2318 | max_open = get_unaligned_le64(&identify_buf[40]); |
| 2319 | if ((max_open >> 63)) |
| 2320 | dev->zac_zones_max_open = (u32)max_open; |
| 2321 | } |
| 2322 | } |
| 2323 | |
Christoph Hellwig | 818831c | 2017-06-04 14:42:24 +0200 | [diff] [blame] | 2324 | static void ata_dev_config_trusted(struct ata_device *dev) |
| 2325 | { |
| 2326 | struct ata_port *ap = dev->link->ap; |
| 2327 | u64 trusted_cap; |
| 2328 | unsigned int err; |
| 2329 | |
Christoph Hellwig | e8f11db | 2017-08-29 14:42:06 +0200 | [diff] [blame] | 2330 | if (!ata_id_has_trusted(dev->id)) |
| 2331 | return; |
| 2332 | |
Christoph Hellwig | 818831c | 2017-06-04 14:42:24 +0200 | [diff] [blame] | 2333 | if (!ata_identify_page_supported(dev, ATA_LOG_SECURITY)) { |
| 2334 | ata_dev_warn(dev, |
| 2335 | "Security Log not supported\n"); |
| 2336 | return; |
| 2337 | } |
| 2338 | |
| 2339 | err = ata_read_log_page(dev, ATA_LOG_IDENTIFY_DEVICE, ATA_LOG_SECURITY, |
| 2340 | ap->sector_buf, 1); |
Damien Le Moal | fc5c8aa | 2021-08-16 10:44:52 +0900 | [diff] [blame] | 2341 | if (err) |
Christoph Hellwig | 818831c | 2017-06-04 14:42:24 +0200 | [diff] [blame] | 2342 | return; |
Christoph Hellwig | 818831c | 2017-06-04 14:42:24 +0200 | [diff] [blame] | 2343 | |
| 2344 | trusted_cap = get_unaligned_le64(&ap->sector_buf[40]); |
| 2345 | if (!(trusted_cap & (1ULL << 63))) { |
| 2346 | ata_dev_dbg(dev, |
| 2347 | "Trusted Computing capability qword not valid!\n"); |
| 2348 | return; |
| 2349 | } |
| 2350 | |
| 2351 | if (trusted_cap & (1 << 0)) |
| 2352 | dev->flags |= ATA_DFLAG_TRUSTED; |
| 2353 | } |
| 2354 | |
Damien Le Moal | 891fd7c | 2021-08-16 10:44:50 +0900 | [diff] [blame] | 2355 | static int ata_dev_config_lba(struct ata_device *dev) |
| 2356 | { |
| 2357 | struct ata_port *ap = dev->link->ap; |
| 2358 | const u16 *id = dev->id; |
| 2359 | const char *lba_desc; |
| 2360 | char ncq_desc[24]; |
| 2361 | int ret; |
| 2362 | |
| 2363 | dev->flags |= ATA_DFLAG_LBA; |
| 2364 | |
| 2365 | if (ata_id_has_lba48(id)) { |
| 2366 | lba_desc = "LBA48"; |
| 2367 | dev->flags |= ATA_DFLAG_LBA48; |
| 2368 | if (dev->n_sectors >= (1UL << 28) && |
| 2369 | ata_id_has_flush_ext(id)) |
| 2370 | dev->flags |= ATA_DFLAG_FLUSH_EXT; |
| 2371 | } else { |
| 2372 | lba_desc = "LBA"; |
| 2373 | } |
| 2374 | |
| 2375 | /* config NCQ */ |
| 2376 | ret = ata_dev_config_ncq(dev, ncq_desc, sizeof(ncq_desc)); |
| 2377 | |
| 2378 | /* print device info to dmesg */ |
| 2379 | if (ata_msg_drv(ap) && ata_dev_print_info(dev)) |
| 2380 | ata_dev_info(dev, |
| 2381 | "%llu sectors, multi %u: %s %s\n", |
| 2382 | (unsigned long long)dev->n_sectors, |
| 2383 | dev->multi_count, lba_desc, ncq_desc); |
| 2384 | |
| 2385 | return ret; |
| 2386 | } |
| 2387 | |
| 2388 | static void ata_dev_config_chs(struct ata_device *dev) |
| 2389 | { |
| 2390 | struct ata_port *ap = dev->link->ap; |
| 2391 | const u16 *id = dev->id; |
| 2392 | |
| 2393 | if (ata_id_current_chs_valid(id)) { |
| 2394 | /* Current CHS translation is valid. */ |
| 2395 | dev->cylinders = id[54]; |
| 2396 | dev->heads = id[55]; |
| 2397 | dev->sectors = id[56]; |
| 2398 | } else { |
| 2399 | /* Default translation */ |
| 2400 | dev->cylinders = id[1]; |
| 2401 | dev->heads = id[3]; |
| 2402 | dev->sectors = id[6]; |
| 2403 | } |
| 2404 | |
| 2405 | /* print device info to dmesg */ |
| 2406 | if (ata_msg_drv(ap) && ata_dev_print_info(dev)) |
| 2407 | ata_dev_info(dev, |
| 2408 | "%llu sectors, multi %u, CHS %u/%u/%u\n", |
| 2409 | (unsigned long long)dev->n_sectors, |
| 2410 | dev->multi_count, dev->cylinders, |
| 2411 | dev->heads, dev->sectors); |
| 2412 | } |
| 2413 | |
Damien Le Moal | d8d8778 | 2021-08-16 10:44:49 +0900 | [diff] [blame] | 2414 | static void ata_dev_config_devslp(struct ata_device *dev) |
| 2415 | { |
| 2416 | u8 *sata_setting = dev->link->ap->sector_buf; |
| 2417 | unsigned int err_mask; |
| 2418 | int i, j; |
| 2419 | |
| 2420 | /* |
| 2421 | * Check device sleep capability. Get DevSlp timing variables |
| 2422 | * from SATA Settings page of Identify Device Data Log. |
| 2423 | */ |
Damien Le Moal | 06f6c4c | 2021-11-15 12:47:26 +0900 | [diff] [blame] | 2424 | if (!ata_id_has_devslp(dev->id) || |
| 2425 | !ata_identify_page_supported(dev, ATA_LOG_SATA_SETTINGS)) |
Damien Le Moal | d8d8778 | 2021-08-16 10:44:49 +0900 | [diff] [blame] | 2426 | return; |
| 2427 | |
| 2428 | err_mask = ata_read_log_page(dev, |
| 2429 | ATA_LOG_IDENTIFY_DEVICE, |
| 2430 | ATA_LOG_SATA_SETTINGS, |
| 2431 | sata_setting, 1); |
Damien Le Moal | fc5c8aa | 2021-08-16 10:44:52 +0900 | [diff] [blame] | 2432 | if (err_mask) |
Damien Le Moal | d8d8778 | 2021-08-16 10:44:49 +0900 | [diff] [blame] | 2433 | return; |
Damien Le Moal | d8d8778 | 2021-08-16 10:44:49 +0900 | [diff] [blame] | 2434 | |
| 2435 | dev->flags |= ATA_DFLAG_DEVSLP; |
| 2436 | for (i = 0; i < ATA_LOG_DEVSLP_SIZE; i++) { |
| 2437 | j = ATA_LOG_DEVSLP_OFFSET + i; |
| 2438 | dev->devslp_timing[i] = sata_setting[j]; |
| 2439 | } |
| 2440 | } |
| 2441 | |
Damien Le Moal | fe22e1c | 2021-10-27 11:22:21 +0900 | [diff] [blame] | 2442 | static void ata_dev_config_cpr(struct ata_device *dev) |
| 2443 | { |
| 2444 | unsigned int err_mask; |
| 2445 | size_t buf_len; |
| 2446 | int i, nr_cpr = 0; |
| 2447 | struct ata_cpr_log *cpr_log = NULL; |
| 2448 | u8 *desc, *buf = NULL; |
| 2449 | |
| 2450 | if (!ata_identify_page_supported(dev, |
| 2451 | ATA_LOG_CONCURRENT_POSITIONING_RANGES)) |
| 2452 | goto out; |
| 2453 | |
| 2454 | /* |
| 2455 | * Read IDENTIFY DEVICE data log, page 0x47 |
| 2456 | * (concurrent positioning ranges). We can have at most 255 32B range |
| 2457 | * descriptors plus a 64B header. |
| 2458 | */ |
| 2459 | buf_len = (64 + 255 * 32 + 511) & ~511; |
| 2460 | buf = kzalloc(buf_len, GFP_KERNEL); |
| 2461 | if (!buf) |
| 2462 | goto out; |
| 2463 | |
| 2464 | err_mask = ata_read_log_page(dev, ATA_LOG_IDENTIFY_DEVICE, |
| 2465 | ATA_LOG_CONCURRENT_POSITIONING_RANGES, |
| 2466 | buf, buf_len >> 9); |
| 2467 | if (err_mask) |
| 2468 | goto out; |
| 2469 | |
| 2470 | nr_cpr = buf[0]; |
| 2471 | if (!nr_cpr) |
| 2472 | goto out; |
| 2473 | |
| 2474 | cpr_log = kzalloc(struct_size(cpr_log, cpr, nr_cpr), GFP_KERNEL); |
| 2475 | if (!cpr_log) |
| 2476 | goto out; |
| 2477 | |
| 2478 | cpr_log->nr_cpr = nr_cpr; |
| 2479 | desc = &buf[64]; |
| 2480 | for (i = 0; i < nr_cpr; i++, desc += 32) { |
| 2481 | cpr_log->cpr[i].num = desc[0]; |
| 2482 | cpr_log->cpr[i].num_storage_elements = desc[1]; |
| 2483 | cpr_log->cpr[i].start_lba = get_unaligned_le64(&desc[8]); |
| 2484 | cpr_log->cpr[i].num_lbas = get_unaligned_le64(&desc[16]); |
| 2485 | } |
| 2486 | |
| 2487 | out: |
| 2488 | swap(dev->cpr_log, cpr_log); |
| 2489 | kfree(cpr_log); |
| 2490 | kfree(buf); |
| 2491 | } |
| 2492 | |
Damien Le Moal | d633b8a | 2021-08-16 10:44:53 +0900 | [diff] [blame] | 2493 | static void ata_dev_print_features(struct ata_device *dev) |
| 2494 | { |
| 2495 | if (!(dev->flags & ATA_DFLAG_FEATURES_MASK)) |
| 2496 | return; |
| 2497 | |
| 2498 | ata_dev_info(dev, |
Damien Le Moal | fe22e1c | 2021-10-27 11:22:21 +0900 | [diff] [blame] | 2499 | "Features:%s%s%s%s%s%s\n", |
Damien Le Moal | d633b8a | 2021-08-16 10:44:53 +0900 | [diff] [blame] | 2500 | dev->flags & ATA_DFLAG_TRUSTED ? " Trust" : "", |
| 2501 | dev->flags & ATA_DFLAG_DA ? " Dev-Attention" : "", |
| 2502 | dev->flags & ATA_DFLAG_DEVSLP ? " Dev-Sleep" : "", |
| 2503 | dev->flags & ATA_DFLAG_NCQ_SEND_RECV ? " NCQ-sndrcv" : "", |
Damien Le Moal | fe22e1c | 2021-10-27 11:22:21 +0900 | [diff] [blame] | 2504 | dev->flags & ATA_DFLAG_NCQ_PRIO ? " NCQ-prio" : "", |
| 2505 | dev->cpr_log ? " CPR" : ""); |
Damien Le Moal | d633b8a | 2021-08-16 10:44:53 +0900 | [diff] [blame] | 2506 | } |
| 2507 | |
Tejun Heo | 49016ac | 2006-02-21 02:12:11 +0900 | [diff] [blame] | 2508 | /** |
Tejun Heo | ffeae41 | 2006-03-01 16:09:35 +0900 | [diff] [blame] | 2509 | * ata_dev_configure - Configure the specified ATA/ATAPI device |
Tejun Heo | ffeae41 | 2006-03-01 16:09:35 +0900 | [diff] [blame] | 2510 | * @dev: Target device to configure |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2511 | * |
Tejun Heo | ffeae41 | 2006-03-01 16:09:35 +0900 | [diff] [blame] | 2512 | * Configure @dev according to @dev->id. Generic and low-level |
| 2513 | * driver specific fixups are also applied. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2514 | * |
| 2515 | * LOCKING: |
Tejun Heo | ffeae41 | 2006-03-01 16:09:35 +0900 | [diff] [blame] | 2516 | * Kernel thread context (may sleep) |
| 2517 | * |
| 2518 | * RETURNS: |
| 2519 | * 0 on success, -errno otherwise |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2520 | */ |
Tejun Heo | efdaedc | 2006-11-01 18:38:52 +0900 | [diff] [blame] | 2521 | int ata_dev_configure(struct ata_device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2522 | { |
Tejun Heo | 9af5c9c | 2007-08-06 18:36:22 +0900 | [diff] [blame] | 2523 | struct ata_port *ap = dev->link->ap; |
Damien Le Moal | 891fd7c | 2021-08-16 10:44:50 +0900 | [diff] [blame] | 2524 | bool print_info = ata_dev_print_info(dev); |
Tejun Heo | 1148c3a | 2006-03-13 19:48:04 +0900 | [diff] [blame] | 2525 | const u16 *id = dev->id; |
Tejun Heo | 7dc951a | 2007-11-27 19:43:42 +0900 | [diff] [blame] | 2526 | unsigned long xfer_mask; |
Shane Huang | 65fe1f0 | 2012-09-07 22:40:01 +0800 | [diff] [blame] | 2527 | unsigned int err_mask; |
Alan Cox | b352e57 | 2006-08-10 18:52:12 +0100 | [diff] [blame] | 2528 | char revbuf[7]; /* XYZ-99\0 */ |
Eric D. Mudama | 3f64f56 | 2007-01-30 23:00:40 -0700 | [diff] [blame] | 2529 | char fwrevbuf[ATA_ID_FW_REV_LEN+1]; |
| 2530 | char modelbuf[ATA_ID_PROD_LEN+1]; |
Brian King | e6d902a | 2006-06-28 08:30:31 -0500 | [diff] [blame] | 2531 | int rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2532 | |
Hannes Reinecke | 96c810f | 2021-12-21 08:21:06 +0100 | [diff] [blame^] | 2533 | if (!ata_dev_enabled(dev)) { |
| 2534 | ata_dev_dbg(dev, "no device\n"); |
Tejun Heo | ffeae41 | 2006-03-01 16:09:35 +0900 | [diff] [blame] | 2535 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2536 | } |
| 2537 | |
Tejun Heo | 75683fe | 2007-07-05 13:31:27 +0900 | [diff] [blame] | 2538 | /* set horkage */ |
| 2539 | dev->horkage |= ata_dev_blacklisted(dev); |
Tejun Heo | 3326732 | 2008-02-13 09:15:09 +0900 | [diff] [blame] | 2540 | ata_force_horkage(dev); |
Tejun Heo | 75683fe | 2007-07-05 13:31:27 +0900 | [diff] [blame] | 2541 | |
Tejun Heo | 50af2fa | 2008-05-19 01:15:14 +0900 | [diff] [blame] | 2542 | if (dev->horkage & ATA_HORKAGE_DISABLE) { |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 2543 | ata_dev_info(dev, "unsupported device, disabling\n"); |
Tejun Heo | 50af2fa | 2008-05-19 01:15:14 +0900 | [diff] [blame] | 2544 | ata_dev_disable(dev); |
| 2545 | return 0; |
| 2546 | } |
| 2547 | |
Tejun Heo | 2486fa5 | 2008-07-31 07:52:40 +0900 | [diff] [blame] | 2548 | if ((!atapi_enabled || (ap->flags & ATA_FLAG_NO_ATAPI)) && |
| 2549 | dev->class == ATA_DEV_ATAPI) { |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 2550 | ata_dev_warn(dev, "WARNING: ATAPI is %s, device ignored\n", |
| 2551 | atapi_enabled ? "not supported with this driver" |
| 2552 | : "disabled"); |
Tejun Heo | 2486fa5 | 2008-07-31 07:52:40 +0900 | [diff] [blame] | 2553 | ata_dev_disable(dev); |
| 2554 | return 0; |
| 2555 | } |
| 2556 | |
Tejun Heo | 9062712 | 2009-01-29 20:31:36 +0900 | [diff] [blame] | 2557 | rc = ata_do_link_spd_horkage(dev); |
| 2558 | if (rc) |
| 2559 | return rc; |
| 2560 | |
Tejun Heo | ecd75ad | 2014-01-16 09:47:17 -0500 | [diff] [blame] | 2561 | /* some WD SATA-1 drives have issues with LPM, turn on NOLPM for them */ |
| 2562 | if ((dev->horkage & ATA_HORKAGE_WD_BROKEN_LPM) && |
| 2563 | (id[ATA_ID_SATA_CAPABILITY] & 0xe) == 0x2) |
| 2564 | dev->horkage |= ATA_HORKAGE_NOLPM; |
| 2565 | |
Hans de Goede | 240630e | 2018-07-01 12:15:46 +0200 | [diff] [blame] | 2566 | if (ap->flags & ATA_FLAG_NO_LPM) |
| 2567 | dev->horkage |= ATA_HORKAGE_NOLPM; |
| 2568 | |
Tejun Heo | ecd75ad | 2014-01-16 09:47:17 -0500 | [diff] [blame] | 2569 | if (dev->horkage & ATA_HORKAGE_NOLPM) { |
| 2570 | ata_dev_warn(dev, "LPM support broken, forcing max_power\n"); |
| 2571 | dev->link->ap->target_lpm_policy = ATA_LPM_MAX_POWER; |
| 2572 | } |
| 2573 | |
Tejun Heo | 6746544 | 2007-05-15 03:28:16 +0900 | [diff] [blame] | 2574 | /* let ACPI work its magic */ |
| 2575 | rc = ata_acpi_on_devcfg(dev); |
| 2576 | if (rc) |
| 2577 | return rc; |
Kristen Carlson Accardi | 08573a8 | 2006-11-10 16:14:47 -0800 | [diff] [blame] | 2578 | |
Tejun Heo | 05027ad | 2007-09-03 12:32:57 +0900 | [diff] [blame] | 2579 | /* massage HPA, do it early as it might change IDENTIFY data */ |
| 2580 | rc = ata_hpa_resize(dev); |
| 2581 | if (rc) |
| 2582 | return rc; |
| 2583 | |
Tejun Heo | c39f5eb | 2006-03-13 19:51:19 +0900 | [diff] [blame] | 2584 | /* print device capabilities */ |
Hannes Reinecke | 17a1e1b | 2021-12-21 08:21:05 +0100 | [diff] [blame] | 2585 | ata_dev_dbg(dev, |
| 2586 | "%s: cfg 49:%04x 82:%04x 83:%04x 84:%04x " |
| 2587 | "85:%04x 86:%04x 87:%04x 88:%04x\n", |
| 2588 | __func__, |
| 2589 | id[49], id[82], id[83], id[84], |
| 2590 | id[85], id[86], id[87], id[88]); |
Tejun Heo | c39f5eb | 2006-03-13 19:51:19 +0900 | [diff] [blame] | 2591 | |
Tejun Heo | 208a993 | 2006-03-05 17:55:58 +0900 | [diff] [blame] | 2592 | /* initialize to-be-configured parameters */ |
Tejun Heo | ea1dd4e | 2006-04-02 18:51:53 +0900 | [diff] [blame] | 2593 | dev->flags &= ~ATA_DFLAG_CFG_MASK; |
Tejun Heo | 208a993 | 2006-03-05 17:55:58 +0900 | [diff] [blame] | 2594 | dev->max_sectors = 0; |
| 2595 | dev->cdb_len = 0; |
| 2596 | dev->n_sectors = 0; |
| 2597 | dev->cylinders = 0; |
| 2598 | dev->heads = 0; |
| 2599 | dev->sectors = 0; |
Mark Lord | e18086d | 2009-03-19 13:32:21 -0400 | [diff] [blame] | 2600 | dev->multi_count = 0; |
Tejun Heo | 208a993 | 2006-03-05 17:55:58 +0900 | [diff] [blame] | 2601 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2602 | /* |
| 2603 | * common ATA, ATAPI feature tests |
| 2604 | */ |
| 2605 | |
Tejun Heo | ff8854b | 2006-03-06 04:31:56 +0900 | [diff] [blame] | 2606 | /* find max transfer mode; for printk only */ |
Tejun Heo | 1148c3a | 2006-03-13 19:48:04 +0900 | [diff] [blame] | 2607 | xfer_mask = ata_id_xfermask(id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2608 | |
Hannes Reinecke | 6044f3c | 2021-12-21 08:20:27 +0100 | [diff] [blame] | 2609 | ata_dump_id(dev, id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2610 | |
Albert Lee | ef143d5 | 2007-06-05 13:01:33 +0800 | [diff] [blame] | 2611 | /* SCSI only uses 4-char revisions, dump full 8 chars from ATA */ |
| 2612 | ata_id_c_string(dev->id, fwrevbuf, ATA_ID_FW_REV, |
| 2613 | sizeof(fwrevbuf)); |
| 2614 | |
| 2615 | ata_id_c_string(dev->id, modelbuf, ATA_ID_PROD, |
| 2616 | sizeof(modelbuf)); |
| 2617 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2618 | /* ATA-specific feature tests */ |
Hannes Reinecke | 9162c65 | 2014-11-05 13:08:21 +0100 | [diff] [blame] | 2619 | if (dev->class == ATA_DEV_ATA || dev->class == ATA_DEV_ZAC) { |
Alan Cox | b352e57 | 2006-08-10 18:52:12 +0100 | [diff] [blame] | 2620 | if (ata_id_is_cfa(id)) { |
Sergei Shtylyov | 62afe5d | 2009-04-13 20:50:00 +0400 | [diff] [blame] | 2621 | /* CPRM may make this media unusable */ |
| 2622 | if (id[ATA_ID_CFA_KEY_MGMT] & 1) |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 2623 | ata_dev_warn(dev, |
| 2624 | "supports DRM functions and may not be fully accessible\n"); |
Alan Cox | b352e57 | 2006-08-10 18:52:12 +0100 | [diff] [blame] | 2625 | snprintf(revbuf, 7, "CFA"); |
Alan Cox | ae8d4ee | 2007-11-04 22:05:49 -0500 | [diff] [blame] | 2626 | } else { |
Jeff Garzik | 2dcb407 | 2007-10-19 06:42:56 -0400 | [diff] [blame] | 2627 | snprintf(revbuf, 7, "ATA-%d", ata_id_major_version(id)); |
Alan Cox | ae8d4ee | 2007-11-04 22:05:49 -0500 | [diff] [blame] | 2628 | /* Warn the user if the device has TPM extensions */ |
| 2629 | if (ata_id_has_tpm(id)) |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 2630 | ata_dev_warn(dev, |
| 2631 | "supports DRM functions and may not be fully accessible\n"); |
Alan Cox | ae8d4ee | 2007-11-04 22:05:49 -0500 | [diff] [blame] | 2632 | } |
Alan Cox | b352e57 | 2006-08-10 18:52:12 +0100 | [diff] [blame] | 2633 | |
Tejun Heo | 1148c3a | 2006-03-13 19:48:04 +0900 | [diff] [blame] | 2634 | dev->n_sectors = ata_id_n_sectors(id); |
Tejun Heo | 2940740 | 2006-02-12 22:47:04 +0900 | [diff] [blame] | 2635 | |
Mark Lord | e18086d | 2009-03-19 13:32:21 -0400 | [diff] [blame] | 2636 | /* get current R/W Multiple count setting */ |
| 2637 | if ((dev->id[47] >> 8) == 0x80 && (dev->id[59] & 0x100)) { |
| 2638 | unsigned int max = dev->id[47] & 0xff; |
| 2639 | unsigned int cnt = dev->id[59] & 0xff; |
| 2640 | /* only recognize/allow powers of two here */ |
| 2641 | if (is_power_of_2(max) && is_power_of_2(cnt)) |
| 2642 | if (cnt <= max) |
| 2643 | dev->multi_count = cnt; |
| 2644 | } |
Eric D. Mudama | 3f64f56 | 2007-01-30 23:00:40 -0700 | [diff] [blame] | 2645 | |
Damien Le Moal | 891fd7c | 2021-08-16 10:44:50 +0900 | [diff] [blame] | 2646 | /* print device info to dmesg */ |
| 2647 | if (ata_msg_drv(ap) && print_info) |
| 2648 | ata_dev_info(dev, "%s: %s, %s, max %s\n", |
| 2649 | revbuf, modelbuf, fwrevbuf, |
| 2650 | ata_mode_string(xfer_mask)); |
| 2651 | |
Tejun Heo | 1148c3a | 2006-03-13 19:48:04 +0900 | [diff] [blame] | 2652 | if (ata_id_has_lba(id)) { |
Damien Le Moal | 891fd7c | 2021-08-16 10:44:50 +0900 | [diff] [blame] | 2653 | rc = ata_dev_config_lba(dev); |
Shaohua Li | 388539f | 2009-07-27 09:24:35 +0800 | [diff] [blame] | 2654 | if (rc) |
| 2655 | return rc; |
Tejun Heo | ffeae41 | 2006-03-01 16:09:35 +0900 | [diff] [blame] | 2656 | } else { |
Damien Le Moal | 891fd7c | 2021-08-16 10:44:50 +0900 | [diff] [blame] | 2657 | ata_dev_config_chs(dev); |
Albert Lee | 07f6f7d | 2005-11-01 19:33:20 +0800 | [diff] [blame] | 2658 | } |
| 2659 | |
Damien Le Moal | d8d8778 | 2021-08-16 10:44:49 +0900 | [diff] [blame] | 2660 | ata_dev_config_devslp(dev); |
Hannes Reinecke | e87fd28 | 2016-04-04 11:43:55 +0200 | [diff] [blame] | 2661 | ata_dev_config_sense_reporting(dev); |
Hannes Reinecke | 6d1003a | 2016-04-25 12:45:56 +0200 | [diff] [blame] | 2662 | ata_dev_config_zac(dev); |
Christoph Hellwig | 818831c | 2017-06-04 14:42:24 +0200 | [diff] [blame] | 2663 | ata_dev_config_trusted(dev); |
Damien Le Moal | fe22e1c | 2021-10-27 11:22:21 +0900 | [diff] [blame] | 2664 | ata_dev_config_cpr(dev); |
Minwoo Im | b1ffbf8 | 2017-06-24 03:41:10 +0900 | [diff] [blame] | 2665 | dev->cdb_len = 32; |
Damien Le Moal | d633b8a | 2021-08-16 10:44:53 +0900 | [diff] [blame] | 2666 | |
| 2667 | if (ata_msg_drv(ap) && print_info) |
| 2668 | ata_dev_print_features(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2669 | } |
| 2670 | |
| 2671 | /* ATAPI-specific feature tests */ |
Jeff Garzik | 2c13b7c | 2005-11-14 14:14:16 -0500 | [diff] [blame] | 2672 | else if (dev->class == ATA_DEV_ATAPI) { |
Tejun Heo | 854c73a | 2007-09-23 13:14:11 +0900 | [diff] [blame] | 2673 | const char *cdb_intr_string = ""; |
| 2674 | const char *atapi_an_string = ""; |
Tejun Heo | 9116300 | 2008-02-21 13:25:50 +0900 | [diff] [blame] | 2675 | const char *dma_dir_string = ""; |
Tejun Heo | 7d77b24 | 2007-09-23 13:14:13 +0900 | [diff] [blame] | 2676 | u32 sntf; |
Albert Lee | 08a556d | 2006-03-31 13:29:04 +0800 | [diff] [blame] | 2677 | |
Tejun Heo | 1148c3a | 2006-03-13 19:48:04 +0900 | [diff] [blame] | 2678 | rc = atapi_cdb_len(id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2679 | if ((rc < 12) || (rc > ATAPI_CDB_LEN)) { |
Hannes Reinecke | 16d4246 | 2021-12-21 08:21:04 +0100 | [diff] [blame] | 2680 | ata_dev_warn(dev, "unsupported CDB len %d\n", rc); |
Tejun Heo | ffeae41 | 2006-03-01 16:09:35 +0900 | [diff] [blame] | 2681 | rc = -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2682 | goto err_out_nosup; |
| 2683 | } |
Tejun Heo | 6e7846e | 2006-02-12 23:32:58 +0900 | [diff] [blame] | 2684 | dev->cdb_len = (unsigned int) rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2685 | |
Tejun Heo | 7d77b24 | 2007-09-23 13:14:13 +0900 | [diff] [blame] | 2686 | /* Enable ATAPI AN if both the host and device have |
| 2687 | * the support. If PMP is attached, SNTF is required |
| 2688 | * to enable ATAPI AN to discern between PHY status |
| 2689 | * changed notifications and ATAPI ANs. |
Kristen Carlson Accardi | 9f45cbd | 2007-08-15 03:57:11 -0400 | [diff] [blame] | 2690 | */ |
Tejun Heo | e7ecd43 | 2010-05-19 15:38:58 +0200 | [diff] [blame] | 2691 | if (atapi_an && |
| 2692 | (ap->flags & ATA_FLAG_AN) && ata_id_has_atapi_AN(id) && |
Tejun Heo | 071f44b | 2008-04-07 22:47:22 +0900 | [diff] [blame] | 2693 | (!sata_pmp_attached(ap) || |
Tejun Heo | 7d77b24 | 2007-09-23 13:14:13 +0900 | [diff] [blame] | 2694 | sata_scr_read(&ap->link, SCR_NOTIFICATION, &sntf) == 0)) { |
Kristen Carlson Accardi | 9f45cbd | 2007-08-15 03:57:11 -0400 | [diff] [blame] | 2695 | /* issue SET feature command to turn this on */ |
Jeff Garzik | 218f3d3 | 2007-10-25 00:33:27 -0400 | [diff] [blame] | 2696 | err_mask = ata_dev_set_feature(dev, |
| 2697 | SETFEATURES_SATA_ENABLE, SATA_AN); |
Tejun Heo | 854c73a | 2007-09-23 13:14:11 +0900 | [diff] [blame] | 2698 | if (err_mask) |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 2699 | ata_dev_err(dev, |
| 2700 | "failed to enable ATAPI AN (err_mask=0x%x)\n", |
| 2701 | err_mask); |
Tejun Heo | 854c73a | 2007-09-23 13:14:11 +0900 | [diff] [blame] | 2702 | else { |
Kristen Carlson Accardi | 9f45cbd | 2007-08-15 03:57:11 -0400 | [diff] [blame] | 2703 | dev->flags |= ATA_DFLAG_AN; |
Tejun Heo | 854c73a | 2007-09-23 13:14:11 +0900 | [diff] [blame] | 2704 | atapi_an_string = ", ATAPI AN"; |
| 2705 | } |
Kristen Carlson Accardi | 9f45cbd | 2007-08-15 03:57:11 -0400 | [diff] [blame] | 2706 | } |
| 2707 | |
Albert Lee | 08a556d | 2006-03-31 13:29:04 +0800 | [diff] [blame] | 2708 | if (ata_id_cdb_intr(dev->id)) { |
Albert Lee | 312f7da | 2005-09-27 17:38:03 +0800 | [diff] [blame] | 2709 | dev->flags |= ATA_DFLAG_CDB_INTR; |
Albert Lee | 08a556d | 2006-03-31 13:29:04 +0800 | [diff] [blame] | 2710 | cdb_intr_string = ", CDB intr"; |
| 2711 | } |
Albert Lee | 312f7da | 2005-09-27 17:38:03 +0800 | [diff] [blame] | 2712 | |
Vincent Pelletier | 966fbe1 | 2013-05-21 22:30:58 +0200 | [diff] [blame] | 2713 | if (atapi_dmadir || (dev->horkage & ATA_HORKAGE_ATAPI_DMADIR) || atapi_id_dmadir(dev->id)) { |
Tejun Heo | 9116300 | 2008-02-21 13:25:50 +0900 | [diff] [blame] | 2714 | dev->flags |= ATA_DFLAG_DMADIR; |
| 2715 | dma_dir_string = ", DMADIR"; |
| 2716 | } |
| 2717 | |
Aaron Lu | afe7595 | 2013-01-15 17:20:58 +0800 | [diff] [blame] | 2718 | if (ata_id_has_da(dev->id)) { |
Lin Ming | b1354cb | 2012-06-25 16:13:08 +0800 | [diff] [blame] | 2719 | dev->flags |= ATA_DFLAG_DA; |
Aaron Lu | afe7595 | 2013-01-15 17:20:58 +0800 | [diff] [blame] | 2720 | zpodd_init(dev); |
| 2721 | } |
Lin Ming | b1354cb | 2012-06-25 16:13:08 +0800 | [diff] [blame] | 2722 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2723 | /* print device info to dmesg */ |
Borislav Petkov | 5afc814 | 2006-06-27 14:51:25 +0200 | [diff] [blame] | 2724 | if (ata_msg_drv(ap) && print_info) |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 2725 | ata_dev_info(dev, |
| 2726 | "ATAPI: %s, %s, max %s%s%s%s\n", |
| 2727 | modelbuf, fwrevbuf, |
| 2728 | ata_mode_string(xfer_mask), |
| 2729 | cdb_intr_string, atapi_an_string, |
| 2730 | dma_dir_string); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2731 | } |
| 2732 | |
Tejun Heo | 914ed35 | 2006-11-01 18:39:55 +0900 | [diff] [blame] | 2733 | /* determine max_sectors */ |
| 2734 | dev->max_sectors = ATA_MAX_SECTORS; |
| 2735 | if (dev->flags & ATA_DFLAG_LBA48) |
| 2736 | dev->max_sectors = ATA_MAX_SECTORS_LBA48; |
| 2737 | |
Alan Cox | c5038fc | 2007-10-25 14:21:16 +0100 | [diff] [blame] | 2738 | /* Limit PATA drive on SATA cable bridge transfers to udma5, |
| 2739 | 200 sectors */ |
Tejun Heo | 3373efd | 2006-05-15 20:57:53 +0900 | [diff] [blame] | 2740 | if (ata_dev_knobble(dev)) { |
Borislav Petkov | 5afc814 | 2006-06-27 14:51:25 +0200 | [diff] [blame] | 2741 | if (ata_msg_drv(ap) && print_info) |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 2742 | ata_dev_info(dev, "applying bridge limits\n"); |
Tejun Heo | 5a52913 | 2006-03-24 14:07:50 +0900 | [diff] [blame] | 2743 | dev->udma_mask &= ATA_UDMA5; |
Tejun Heo | 4b2f3ed | 2006-03-01 16:09:36 +0900 | [diff] [blame] | 2744 | dev->max_sectors = ATA_MAX_SECTORS; |
| 2745 | } |
| 2746 | |
Tony Battersby | f8d8e57 | 2007-10-30 11:44:35 -0400 | [diff] [blame] | 2747 | if ((dev->class == ATA_DEV_ATAPI) && |
Albert Lee | f442cd8 | 2007-11-15 10:35:47 +0900 | [diff] [blame] | 2748 | (atapi_command_packet_set(id) == TYPE_TAPE)) { |
Tony Battersby | f8d8e57 | 2007-10-30 11:44:35 -0400 | [diff] [blame] | 2749 | dev->max_sectors = ATA_MAX_SECTORS_TAPE; |
Albert Lee | f442cd8 | 2007-11-15 10:35:47 +0900 | [diff] [blame] | 2750 | dev->horkage |= ATA_HORKAGE_STUCK_ERR; |
| 2751 | } |
Tony Battersby | f8d8e57 | 2007-10-30 11:44:35 -0400 | [diff] [blame] | 2752 | |
Tejun Heo | 75683fe | 2007-07-05 13:31:27 +0900 | [diff] [blame] | 2753 | if (dev->horkage & ATA_HORKAGE_MAX_SEC_128) |
Tejun Heo | 03ec52d | 2007-04-12 13:38:11 +0900 | [diff] [blame] | 2754 | dev->max_sectors = min_t(unsigned int, ATA_MAX_SECTORS_128, |
| 2755 | dev->max_sectors); |
Albert Lee | 18d6e9d | 2007-04-02 11:34:15 +0800 | [diff] [blame] | 2756 | |
David Milburn | af34d63 | 2015-07-13 11:48:23 -0500 | [diff] [blame] | 2757 | if (dev->horkage & ATA_HORKAGE_MAX_SEC_1024) |
| 2758 | dev->max_sectors = min_t(unsigned int, ATA_MAX_SECTORS_1024, |
| 2759 | dev->max_sectors); |
| 2760 | |
Shan Hai | a32450e | 2013-03-18 10:30:44 +0800 | [diff] [blame] | 2761 | if (dev->horkage & ATA_HORKAGE_MAX_SEC_LBA48) |
| 2762 | dev->max_sectors = ATA_MAX_SECTORS_LBA48; |
| 2763 | |
Tejun Heo | 4b2f3ed | 2006-03-01 16:09:36 +0900 | [diff] [blame] | 2764 | if (ap->ops->dev_config) |
Alan | cd0d3bb | 2007-03-02 00:56:15 +0000 | [diff] [blame] | 2765 | ap->ops->dev_config(dev); |
Tejun Heo | 4b2f3ed | 2006-03-01 16:09:36 +0900 | [diff] [blame] | 2766 | |
Alan Cox | c5038fc | 2007-10-25 14:21:16 +0100 | [diff] [blame] | 2767 | if (dev->horkage & ATA_HORKAGE_DIAGNOSTIC) { |
| 2768 | /* Let the user know. We don't want to disallow opens for |
| 2769 | rescue purposes, or in case the vendor is just a blithering |
| 2770 | idiot. Do this after the dev_config call as some controllers |
| 2771 | with buggy firmware may want to avoid reporting false device |
| 2772 | bugs */ |
| 2773 | |
| 2774 | if (print_info) { |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 2775 | ata_dev_warn(dev, |
Alan Cox | c5038fc | 2007-10-25 14:21:16 +0100 | [diff] [blame] | 2776 | "Drive reports diagnostics failure. This may indicate a drive\n"); |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 2777 | ata_dev_warn(dev, |
Alan Cox | c5038fc | 2007-10-25 14:21:16 +0100 | [diff] [blame] | 2778 | "fault or invalid emulation. Contact drive vendor for information.\n"); |
| 2779 | } |
| 2780 | } |
| 2781 | |
Tejun Heo | ac70a96 | 2008-11-27 13:36:48 +0900 | [diff] [blame] | 2782 | if ((dev->horkage & ATA_HORKAGE_FIRMWARE_WARN) && print_info) { |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 2783 | ata_dev_warn(dev, "WARNING: device requires firmware update to be fully functional\n"); |
| 2784 | ata_dev_warn(dev, " contact the vendor or visit http://ata.wiki.kernel.org\n"); |
Tejun Heo | ac70a96 | 2008-11-27 13:36:48 +0900 | [diff] [blame] | 2785 | } |
| 2786 | |
Tejun Heo | ffeae41 | 2006-03-01 16:09:35 +0900 | [diff] [blame] | 2787 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2788 | |
| 2789 | err_out_nosup: |
Tejun Heo | ffeae41 | 2006-03-01 16:09:35 +0900 | [diff] [blame] | 2790 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2791 | } |
| 2792 | |
| 2793 | /** |
Alan Cox | 2e41e8e | 2007-03-08 23:19:19 +0000 | [diff] [blame] | 2794 | * ata_cable_40wire - return 40 wire cable type |
Alan Cox | be0d18d | 2007-03-06 02:37:56 -0800 | [diff] [blame] | 2795 | * @ap: port |
| 2796 | * |
Alan Cox | 2e41e8e | 2007-03-08 23:19:19 +0000 | [diff] [blame] | 2797 | * Helper method for drivers which want to hardwire 40 wire cable |
Alan Cox | be0d18d | 2007-03-06 02:37:56 -0800 | [diff] [blame] | 2798 | * detection. |
| 2799 | */ |
| 2800 | |
| 2801 | int ata_cable_40wire(struct ata_port *ap) |
| 2802 | { |
| 2803 | return ATA_CBL_PATA40; |
| 2804 | } |
Bartlomiej Zolnierkiewicz | a52fbcf | 2020-03-26 16:58:04 +0100 | [diff] [blame] | 2805 | EXPORT_SYMBOL_GPL(ata_cable_40wire); |
Alan Cox | be0d18d | 2007-03-06 02:37:56 -0800 | [diff] [blame] | 2806 | |
| 2807 | /** |
Alan Cox | 2e41e8e | 2007-03-08 23:19:19 +0000 | [diff] [blame] | 2808 | * ata_cable_80wire - return 80 wire cable type |
Alan Cox | be0d18d | 2007-03-06 02:37:56 -0800 | [diff] [blame] | 2809 | * @ap: port |
| 2810 | * |
Alan Cox | 2e41e8e | 2007-03-08 23:19:19 +0000 | [diff] [blame] | 2811 | * Helper method for drivers which want to hardwire 80 wire cable |
Alan Cox | be0d18d | 2007-03-06 02:37:56 -0800 | [diff] [blame] | 2812 | * detection. |
| 2813 | */ |
| 2814 | |
| 2815 | int ata_cable_80wire(struct ata_port *ap) |
| 2816 | { |
| 2817 | return ATA_CBL_PATA80; |
| 2818 | } |
Bartlomiej Zolnierkiewicz | a52fbcf | 2020-03-26 16:58:04 +0100 | [diff] [blame] | 2819 | EXPORT_SYMBOL_GPL(ata_cable_80wire); |
Alan Cox | be0d18d | 2007-03-06 02:37:56 -0800 | [diff] [blame] | 2820 | |
| 2821 | /** |
| 2822 | * ata_cable_unknown - return unknown PATA cable. |
| 2823 | * @ap: port |
| 2824 | * |
| 2825 | * Helper method for drivers which have no PATA cable detection. |
| 2826 | */ |
| 2827 | |
| 2828 | int ata_cable_unknown(struct ata_port *ap) |
| 2829 | { |
| 2830 | return ATA_CBL_PATA_UNK; |
| 2831 | } |
Bartlomiej Zolnierkiewicz | a52fbcf | 2020-03-26 16:58:04 +0100 | [diff] [blame] | 2832 | EXPORT_SYMBOL_GPL(ata_cable_unknown); |
Alan Cox | be0d18d | 2007-03-06 02:37:56 -0800 | [diff] [blame] | 2833 | |
| 2834 | /** |
Tejun Heo | c88f90c | 2007-11-27 19:43:48 +0900 | [diff] [blame] | 2835 | * ata_cable_ignore - return ignored PATA cable. |
| 2836 | * @ap: port |
| 2837 | * |
| 2838 | * Helper method for drivers which don't use cable type to limit |
| 2839 | * transfer mode. |
| 2840 | */ |
| 2841 | int ata_cable_ignore(struct ata_port *ap) |
| 2842 | { |
| 2843 | return ATA_CBL_PATA_IGN; |
| 2844 | } |
Bartlomiej Zolnierkiewicz | a52fbcf | 2020-03-26 16:58:04 +0100 | [diff] [blame] | 2845 | EXPORT_SYMBOL_GPL(ata_cable_ignore); |
Tejun Heo | c88f90c | 2007-11-27 19:43:48 +0900 | [diff] [blame] | 2846 | |
| 2847 | /** |
Alan Cox | be0d18d | 2007-03-06 02:37:56 -0800 | [diff] [blame] | 2848 | * ata_cable_sata - return SATA cable type |
| 2849 | * @ap: port |
| 2850 | * |
| 2851 | * Helper method for drivers which have SATA cables |
| 2852 | */ |
| 2853 | |
| 2854 | int ata_cable_sata(struct ata_port *ap) |
| 2855 | { |
| 2856 | return ATA_CBL_SATA; |
| 2857 | } |
Bartlomiej Zolnierkiewicz | a52fbcf | 2020-03-26 16:58:04 +0100 | [diff] [blame] | 2858 | EXPORT_SYMBOL_GPL(ata_cable_sata); |
Alan Cox | be0d18d | 2007-03-06 02:37:56 -0800 | [diff] [blame] | 2859 | |
| 2860 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2861 | * ata_bus_probe - Reset and probe ATA bus |
| 2862 | * @ap: Bus to probe |
| 2863 | * |
Jeff Garzik | 0cba632 | 2005-05-30 19:49:12 -0400 | [diff] [blame] | 2864 | * Master ATA bus probing function. Initiates a hardware-dependent |
| 2865 | * bus reset, then attempts to identify any devices found on |
| 2866 | * the bus. |
| 2867 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2868 | * LOCKING: |
Jeff Garzik | 0cba632 | 2005-05-30 19:49:12 -0400 | [diff] [blame] | 2869 | * PCI/etc. bus probe sem. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2870 | * |
| 2871 | * RETURNS: |
Tejun Heo | 96072e6 | 2006-04-01 01:38:17 +0900 | [diff] [blame] | 2872 | * Zero on success, negative errno otherwise. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2873 | */ |
| 2874 | |
Brian King | 8028916 | 2006-08-07 14:27:31 -0500 | [diff] [blame] | 2875 | int ata_bus_probe(struct ata_port *ap) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2876 | { |
Tejun Heo | 28ca5c5 | 2006-03-01 16:09:36 +0900 | [diff] [blame] | 2877 | unsigned int classes[ATA_MAX_DEVICES]; |
Tejun Heo | 14d2bac | 2006-04-02 17:54:46 +0900 | [diff] [blame] | 2878 | int tries[ATA_MAX_DEVICES]; |
Tejun Heo | f58229f | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 2879 | int rc; |
Tejun Heo | e82cbdb | 2006-04-01 01:38:18 +0900 | [diff] [blame] | 2880 | struct ata_device *dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2881 | |
Tejun Heo | 1eca436 | 2008-11-03 20:03:17 +0900 | [diff] [blame] | 2882 | ata_for_each_dev(dev, &ap->link, ALL) |
Tejun Heo | f58229f | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 2883 | tries[dev->devno] = ATA_PROBE_MAX_TRIES; |
Tejun Heo | 14d2bac | 2006-04-02 17:54:46 +0900 | [diff] [blame] | 2884 | |
| 2885 | retry: |
Tejun Heo | 1eca436 | 2008-11-03 20:03:17 +0900 | [diff] [blame] | 2886 | ata_for_each_dev(dev, &ap->link, ALL) { |
Tejun Heo | cdeab11 | 2007-10-29 16:41:09 +0900 | [diff] [blame] | 2887 | /* If we issue an SRST then an ATA drive (not ATAPI) |
| 2888 | * may change configuration and be in PIO0 timing. If |
| 2889 | * we do a hard reset (or are coming from power on) |
| 2890 | * this is true for ATA or ATAPI. Until we've set a |
| 2891 | * suitable controller mode we should not touch the |
| 2892 | * bus as we may be talking too fast. |
| 2893 | */ |
| 2894 | dev->pio_mode = XFER_PIO_0; |
Aaron Lu | 5416912 | 2012-12-03 11:35:02 +0800 | [diff] [blame] | 2895 | dev->dma_mode = 0xff; |
Tejun Heo | cdeab11 | 2007-10-29 16:41:09 +0900 | [diff] [blame] | 2896 | |
| 2897 | /* If the controller has a pio mode setup function |
| 2898 | * then use it to set the chipset to rights. Don't |
| 2899 | * touch the DMA setup as that will be dealt with when |
| 2900 | * configuring devices. |
| 2901 | */ |
| 2902 | if (ap->ops->set_piomode) |
| 2903 | ap->ops->set_piomode(ap, dev); |
| 2904 | } |
| 2905 | |
Tejun Heo | 2044470 | 2006-03-13 01:57:01 +0900 | [diff] [blame] | 2906 | /* reset and determine device classes */ |
Tejun Heo | 52783c5 | 2006-05-31 18:28:22 +0900 | [diff] [blame] | 2907 | ap->ops->phy_reset(ap); |
Tejun Heo | 2061a47 | 2006-03-12 00:57:39 +0900 | [diff] [blame] | 2908 | |
Tejun Heo | 1eca436 | 2008-11-03 20:03:17 +0900 | [diff] [blame] | 2909 | ata_for_each_dev(dev, &ap->link, ALL) { |
Tejun Heo | 3e4ec34 | 2010-05-10 21:41:30 +0200 | [diff] [blame] | 2910 | if (dev->class != ATA_DEV_UNKNOWN) |
Tejun Heo | 52783c5 | 2006-05-31 18:28:22 +0900 | [diff] [blame] | 2911 | classes[dev->devno] = dev->class; |
| 2912 | else |
| 2913 | classes[dev->devno] = ATA_DEV_NONE; |
Tejun Heo | 2044470 | 2006-03-13 01:57:01 +0900 | [diff] [blame] | 2914 | |
Tejun Heo | 52783c5 | 2006-05-31 18:28:22 +0900 | [diff] [blame] | 2915 | dev->class = ATA_DEV_UNKNOWN; |
Tejun Heo | 28ca5c5 | 2006-03-01 16:09:36 +0900 | [diff] [blame] | 2916 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2917 | |
Jeff Garzik | f31f0cc | 2007-03-02 17:47:28 -0500 | [diff] [blame] | 2918 | /* read IDENTIFY page and configure devices. We have to do the identify |
| 2919 | specific sequence bass-ackwards so that PDIAG- is released by |
| 2920 | the slave device */ |
| 2921 | |
Tejun Heo | 1eca436 | 2008-11-03 20:03:17 +0900 | [diff] [blame] | 2922 | ata_for_each_dev(dev, &ap->link, ALL_REVERSE) { |
Tejun Heo | f58229f | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 2923 | if (tries[dev->devno]) |
| 2924 | dev->class = classes[dev->devno]; |
Tejun Heo | 14d2bac | 2006-04-02 17:54:46 +0900 | [diff] [blame] | 2925 | |
Tejun Heo | e1211e3 | 2006-04-01 01:38:18 +0900 | [diff] [blame] | 2926 | if (!ata_dev_enabled(dev)) |
Tejun Heo | ffeae41 | 2006-03-01 16:09:35 +0900 | [diff] [blame] | 2927 | continue; |
| 2928 | |
Tejun Heo | bff0464 | 2006-11-10 18:08:10 +0900 | [diff] [blame] | 2929 | rc = ata_dev_read_id(dev, &dev->class, ATA_READID_POSTRESET, |
| 2930 | dev->id); |
Tejun Heo | 14d2bac | 2006-04-02 17:54:46 +0900 | [diff] [blame] | 2931 | if (rc) |
| 2932 | goto fail; |
Jeff Garzik | f31f0cc | 2007-03-02 17:47:28 -0500 | [diff] [blame] | 2933 | } |
| 2934 | |
Alan Cox | be0d18d | 2007-03-06 02:37:56 -0800 | [diff] [blame] | 2935 | /* Now ask for the cable type as PDIAG- should have been released */ |
| 2936 | if (ap->ops->cable_detect) |
| 2937 | ap->cbl = ap->ops->cable_detect(ap); |
| 2938 | |
Tejun Heo | 1eca436 | 2008-11-03 20:03:17 +0900 | [diff] [blame] | 2939 | /* We may have SATA bridge glue hiding here irrespective of |
| 2940 | * the reported cable types and sensed types. When SATA |
| 2941 | * drives indicate we have a bridge, we don't know which end |
| 2942 | * of the link the bridge is which is a problem. |
| 2943 | */ |
| 2944 | ata_for_each_dev(dev, &ap->link, ENABLED) |
Alan Cox | 614fe29 | 2007-08-22 23:22:45 +0100 | [diff] [blame] | 2945 | if (ata_id_is_sata(dev->id)) |
| 2946 | ap->cbl = ATA_CBL_SATA; |
Alan Cox | 614fe29 | 2007-08-22 23:22:45 +0100 | [diff] [blame] | 2947 | |
Jeff Garzik | f31f0cc | 2007-03-02 17:47:28 -0500 | [diff] [blame] | 2948 | /* After the identify sequence we can now set up the devices. We do |
| 2949 | this in the normal order so that the user doesn't get confused */ |
| 2950 | |
Tejun Heo | 1eca436 | 2008-11-03 20:03:17 +0900 | [diff] [blame] | 2951 | ata_for_each_dev(dev, &ap->link, ENABLED) { |
Tejun Heo | 9af5c9c | 2007-08-06 18:36:22 +0900 | [diff] [blame] | 2952 | ap->link.eh_context.i.flags |= ATA_EHI_PRINTINFO; |
Tejun Heo | efdaedc | 2006-11-01 18:38:52 +0900 | [diff] [blame] | 2953 | rc = ata_dev_configure(dev); |
Tejun Heo | 9af5c9c | 2007-08-06 18:36:22 +0900 | [diff] [blame] | 2954 | ap->link.eh_context.i.flags &= ~ATA_EHI_PRINTINFO; |
Tejun Heo | 14d2bac | 2006-04-02 17:54:46 +0900 | [diff] [blame] | 2955 | if (rc) |
| 2956 | goto fail; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2957 | } |
| 2958 | |
Tejun Heo | e82cbdb | 2006-04-01 01:38:18 +0900 | [diff] [blame] | 2959 | /* configure transfer mode */ |
Tejun Heo | 0260731 | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 2960 | rc = ata_set_mode(&ap->link, &dev); |
Tejun Heo | 4ae72a1 | 2007-02-02 16:22:30 +0900 | [diff] [blame] | 2961 | if (rc) |
Tejun Heo | 51713d3 | 2006-04-11 22:26:29 +0900 | [diff] [blame] | 2962 | goto fail; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2963 | |
Tejun Heo | 1eca436 | 2008-11-03 20:03:17 +0900 | [diff] [blame] | 2964 | ata_for_each_dev(dev, &ap->link, ENABLED) |
| 2965 | return 0; |
Alan Cox | e35a9e0 | 2006-03-27 18:46:37 +0100 | [diff] [blame] | 2966 | |
Tejun Heo | 96072e6 | 2006-04-01 01:38:17 +0900 | [diff] [blame] | 2967 | return -ENODEV; |
Tejun Heo | 14d2bac | 2006-04-02 17:54:46 +0900 | [diff] [blame] | 2968 | |
| 2969 | fail: |
Tejun Heo | 4ae72a1 | 2007-02-02 16:22:30 +0900 | [diff] [blame] | 2970 | tries[dev->devno]--; |
| 2971 | |
Tejun Heo | 14d2bac | 2006-04-02 17:54:46 +0900 | [diff] [blame] | 2972 | switch (rc) { |
| 2973 | case -EINVAL: |
Tejun Heo | 4ae72a1 | 2007-02-02 16:22:30 +0900 | [diff] [blame] | 2974 | /* eeek, something went very wrong, give up */ |
Tejun Heo | 14d2bac | 2006-04-02 17:54:46 +0900 | [diff] [blame] | 2975 | tries[dev->devno] = 0; |
| 2976 | break; |
Tejun Heo | 4ae72a1 | 2007-02-02 16:22:30 +0900 | [diff] [blame] | 2977 | |
| 2978 | case -ENODEV: |
| 2979 | /* give it just one more chance */ |
| 2980 | tries[dev->devno] = min(tries[dev->devno], 1); |
Gustavo A. R. Silva | df561f66 | 2020-08-23 17:36:59 -0500 | [diff] [blame] | 2981 | fallthrough; |
Tejun Heo | 14d2bac | 2006-04-02 17:54:46 +0900 | [diff] [blame] | 2982 | case -EIO: |
Tejun Heo | 4ae72a1 | 2007-02-02 16:22:30 +0900 | [diff] [blame] | 2983 | if (tries[dev->devno] == 1) { |
| 2984 | /* This is the last chance, better to slow |
| 2985 | * down than lose it. |
| 2986 | */ |
Tejun Heo | a07d499 | 2009-01-29 20:31:33 +0900 | [diff] [blame] | 2987 | sata_down_spd_limit(&ap->link, 0); |
Tejun Heo | 4ae72a1 | 2007-02-02 16:22:30 +0900 | [diff] [blame] | 2988 | ata_down_xfermask_limit(dev, ATA_DNXFER_PIO); |
| 2989 | } |
Tejun Heo | 14d2bac | 2006-04-02 17:54:46 +0900 | [diff] [blame] | 2990 | } |
| 2991 | |
Tejun Heo | 4ae72a1 | 2007-02-02 16:22:30 +0900 | [diff] [blame] | 2992 | if (!tries[dev->devno]) |
Tejun Heo | 3373efd | 2006-05-15 20:57:53 +0900 | [diff] [blame] | 2993 | ata_dev_disable(dev); |
Tejun Heo | ec57375 | 2006-04-11 22:26:29 +0900 | [diff] [blame] | 2994 | |
Tejun Heo | 14d2bac | 2006-04-02 17:54:46 +0900 | [diff] [blame] | 2995 | goto retry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2996 | } |
| 2997 | |
| 2998 | /** |
Tejun Heo | 3be680b | 2005-12-19 22:35:02 +0900 | [diff] [blame] | 2999 | * sata_print_link_status - Print SATA link status |
Tejun Heo | 936fd73 | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 3000 | * @link: SATA link to printk link status about |
Tejun Heo | 3be680b | 2005-12-19 22:35:02 +0900 | [diff] [blame] | 3001 | * |
| 3002 | * This function prints link speed and status of a SATA link. |
| 3003 | * |
| 3004 | * LOCKING: |
| 3005 | * None. |
| 3006 | */ |
Adrian Bunk | 6bdb4fc | 2008-04-21 11:51:11 +0300 | [diff] [blame] | 3007 | static void sata_print_link_status(struct ata_link *link) |
Tejun Heo | 3be680b | 2005-12-19 22:35:02 +0900 | [diff] [blame] | 3008 | { |
Tejun Heo | 6d5f973 | 2006-04-03 00:09:41 +0900 | [diff] [blame] | 3009 | u32 sstatus, scontrol, tmp; |
Tejun Heo | 3be680b | 2005-12-19 22:35:02 +0900 | [diff] [blame] | 3010 | |
Tejun Heo | 936fd73 | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 3011 | if (sata_scr_read(link, SCR_STATUS, &sstatus)) |
Tejun Heo | 3be680b | 2005-12-19 22:35:02 +0900 | [diff] [blame] | 3012 | return; |
Tejun Heo | 936fd73 | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 3013 | sata_scr_read(link, SCR_CONTROL, &scontrol); |
Tejun Heo | 3be680b | 2005-12-19 22:35:02 +0900 | [diff] [blame] | 3014 | |
Tejun Heo | b1c7291 | 2008-07-31 17:02:43 +0900 | [diff] [blame] | 3015 | if (ata_phys_link_online(link)) { |
Tejun Heo | 3be680b | 2005-12-19 22:35:02 +0900 | [diff] [blame] | 3016 | tmp = (sstatus >> 4) & 0xf; |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 3017 | ata_link_info(link, "SATA link up %s (SStatus %X SControl %X)\n", |
| 3018 | sata_spd_string(tmp), sstatus, scontrol); |
Tejun Heo | 3be680b | 2005-12-19 22:35:02 +0900 | [diff] [blame] | 3019 | } else { |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 3020 | ata_link_info(link, "SATA link down (SStatus %X SControl %X)\n", |
| 3021 | sstatus, scontrol); |
Tejun Heo | 3be680b | 2005-12-19 22:35:02 +0900 | [diff] [blame] | 3022 | } |
| 3023 | } |
| 3024 | |
| 3025 | /** |
Alan Cox | ebdfca6e | 2006-03-23 15:38:34 +0000 | [diff] [blame] | 3026 | * ata_dev_pair - return other device on cable |
Alan Cox | ebdfca6e | 2006-03-23 15:38:34 +0000 | [diff] [blame] | 3027 | * @adev: device |
| 3028 | * |
| 3029 | * Obtain the other device on the same cable, or if none is |
| 3030 | * present NULL is returned |
| 3031 | */ |
Jeff Garzik | 2e9edbf | 2006-03-24 09:56:57 -0500 | [diff] [blame] | 3032 | |
Tejun Heo | 3373efd | 2006-05-15 20:57:53 +0900 | [diff] [blame] | 3033 | struct ata_device *ata_dev_pair(struct ata_device *adev) |
Alan Cox | ebdfca6e | 2006-03-23 15:38:34 +0000 | [diff] [blame] | 3034 | { |
Tejun Heo | 9af5c9c | 2007-08-06 18:36:22 +0900 | [diff] [blame] | 3035 | struct ata_link *link = adev->link; |
| 3036 | struct ata_device *pair = &link->device[1 - adev->devno]; |
Tejun Heo | e1211e3 | 2006-04-01 01:38:18 +0900 | [diff] [blame] | 3037 | if (!ata_dev_enabled(pair)) |
Alan Cox | ebdfca6e | 2006-03-23 15:38:34 +0000 | [diff] [blame] | 3038 | return NULL; |
| 3039 | return pair; |
| 3040 | } |
Bartlomiej Zolnierkiewicz | a52fbcf | 2020-03-26 16:58:04 +0100 | [diff] [blame] | 3041 | EXPORT_SYMBOL_GPL(ata_dev_pair); |
Alan Cox | ebdfca6e | 2006-03-23 15:38:34 +0000 | [diff] [blame] | 3042 | |
| 3043 | /** |
Tejun Heo | 3c567b7 | 2006-05-15 20:57:23 +0900 | [diff] [blame] | 3044 | * sata_down_spd_limit - adjust SATA spd limit downward |
Tejun Heo | 936fd73 | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 3045 | * @link: Link to adjust SATA spd limit for |
Tejun Heo | a07d499 | 2009-01-29 20:31:33 +0900 | [diff] [blame] | 3046 | * @spd_limit: Additional limit |
Tejun Heo | 1c3fae4 | 2006-04-02 20:53:28 +0900 | [diff] [blame] | 3047 | * |
Tejun Heo | 936fd73 | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 3048 | * Adjust SATA spd limit of @link downward. Note that this |
Tejun Heo | 1c3fae4 | 2006-04-02 20:53:28 +0900 | [diff] [blame] | 3049 | * function only adjusts the limit. The change must be applied |
Tejun Heo | 3c567b7 | 2006-05-15 20:57:23 +0900 | [diff] [blame] | 3050 | * using sata_set_spd(). |
Tejun Heo | 1c3fae4 | 2006-04-02 20:53:28 +0900 | [diff] [blame] | 3051 | * |
Tejun Heo | a07d499 | 2009-01-29 20:31:33 +0900 | [diff] [blame] | 3052 | * If @spd_limit is non-zero, the speed is limited to equal to or |
| 3053 | * lower than @spd_limit if such speed is supported. If |
| 3054 | * @spd_limit is slower than any supported speed, only the lowest |
| 3055 | * supported speed is allowed. |
| 3056 | * |
Tejun Heo | 1c3fae4 | 2006-04-02 20:53:28 +0900 | [diff] [blame] | 3057 | * LOCKING: |
| 3058 | * Inherited from caller. |
| 3059 | * |
| 3060 | * RETURNS: |
| 3061 | * 0 on success, negative errno on failure |
| 3062 | */ |
Tejun Heo | a07d499 | 2009-01-29 20:31:33 +0900 | [diff] [blame] | 3063 | int sata_down_spd_limit(struct ata_link *link, u32 spd_limit) |
Tejun Heo | 1c3fae4 | 2006-04-02 20:53:28 +0900 | [diff] [blame] | 3064 | { |
Tejun Heo | 81952c5 | 2006-05-15 20:57:47 +0900 | [diff] [blame] | 3065 | u32 sstatus, spd, mask; |
Tejun Heo | a07d499 | 2009-01-29 20:31:33 +0900 | [diff] [blame] | 3066 | int rc, bit; |
Tejun Heo | 1c3fae4 | 2006-04-02 20:53:28 +0900 | [diff] [blame] | 3067 | |
Tejun Heo | 936fd73 | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 3068 | if (!sata_scr_valid(link)) |
Tejun Heo | 008a789 | 2007-07-16 14:29:40 +0900 | [diff] [blame] | 3069 | return -EOPNOTSUPP; |
| 3070 | |
| 3071 | /* If SCR can be read, use it to determine the current SPD. |
Tejun Heo | 936fd73 | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 3072 | * If not, use cached value in link->sata_spd. |
Tejun Heo | 008a789 | 2007-07-16 14:29:40 +0900 | [diff] [blame] | 3073 | */ |
Tejun Heo | 936fd73 | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 3074 | rc = sata_scr_read(link, SCR_STATUS, &sstatus); |
Tejun Heo | 9913ff8a | 2009-01-29 20:31:31 +0900 | [diff] [blame] | 3075 | if (rc == 0 && ata_sstatus_online(sstatus)) |
Tejun Heo | 008a789 | 2007-07-16 14:29:40 +0900 | [diff] [blame] | 3076 | spd = (sstatus >> 4) & 0xf; |
| 3077 | else |
Tejun Heo | 936fd73 | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 3078 | spd = link->sata_spd; |
Tejun Heo | 1c3fae4 | 2006-04-02 20:53:28 +0900 | [diff] [blame] | 3079 | |
Tejun Heo | 936fd73 | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 3080 | mask = link->sata_spd_limit; |
Tejun Heo | 1c3fae4 | 2006-04-02 20:53:28 +0900 | [diff] [blame] | 3081 | if (mask <= 1) |
| 3082 | return -EINVAL; |
Tejun Heo | 008a789 | 2007-07-16 14:29:40 +0900 | [diff] [blame] | 3083 | |
| 3084 | /* unconditionally mask off the highest bit */ |
Tejun Heo | a07d499 | 2009-01-29 20:31:33 +0900 | [diff] [blame] | 3085 | bit = fls(mask) - 1; |
| 3086 | mask &= ~(1 << bit); |
Tejun Heo | 1c3fae4 | 2006-04-02 20:53:28 +0900 | [diff] [blame] | 3087 | |
David Milburn | 2dc0b46 | 2017-11-14 16:17:25 -0600 | [diff] [blame] | 3088 | /* |
| 3089 | * Mask off all speeds higher than or equal to the current one. At |
| 3090 | * this point, if current SPD is not available and we previously |
| 3091 | * recorded the link speed from SStatus, the driver has already |
| 3092 | * masked off the highest bit so mask should already be 1 or 0. |
| 3093 | * Otherwise, we should not force 1.5Gbps on a link where we have |
| 3094 | * not previously recorded speed from SStatus. Just return in this |
| 3095 | * case. |
Tejun Heo | 008a789 | 2007-07-16 14:29:40 +0900 | [diff] [blame] | 3096 | */ |
| 3097 | if (spd > 1) |
| 3098 | mask &= (1 << (spd - 1)) - 1; |
| 3099 | else |
David Milburn | 2dc0b46 | 2017-11-14 16:17:25 -0600 | [diff] [blame] | 3100 | return -EINVAL; |
Tejun Heo | 008a789 | 2007-07-16 14:29:40 +0900 | [diff] [blame] | 3101 | |
| 3102 | /* were we already at the bottom? */ |
Tejun Heo | 1c3fae4 | 2006-04-02 20:53:28 +0900 | [diff] [blame] | 3103 | if (!mask) |
| 3104 | return -EINVAL; |
| 3105 | |
Tejun Heo | a07d499 | 2009-01-29 20:31:33 +0900 | [diff] [blame] | 3106 | if (spd_limit) { |
| 3107 | if (mask & ((1 << spd_limit) - 1)) |
| 3108 | mask &= (1 << spd_limit) - 1; |
| 3109 | else { |
| 3110 | bit = ffs(mask) - 1; |
| 3111 | mask = 1 << bit; |
| 3112 | } |
| 3113 | } |
| 3114 | |
Tejun Heo | 936fd73 | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 3115 | link->sata_spd_limit = mask; |
Tejun Heo | 1c3fae4 | 2006-04-02 20:53:28 +0900 | [diff] [blame] | 3116 | |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 3117 | ata_link_warn(link, "limiting SATA link speed to %s\n", |
| 3118 | sata_spd_string(fls(mask))); |
Tejun Heo | 1c3fae4 | 2006-04-02 20:53:28 +0900 | [diff] [blame] | 3119 | |
| 3120 | return 0; |
| 3121 | } |
| 3122 | |
Bartlomiej Zolnierkiewicz | a9b2c12 | 2020-03-26 16:58:07 +0100 | [diff] [blame] | 3123 | #ifdef CONFIG_ATA_ACPI |
Tejun Heo | cf176e1 | 2006-04-02 17:54:46 +0900 | [diff] [blame] | 3124 | /** |
Tejun Heo | a0f79b9 | 2007-12-18 16:33:05 +0900 | [diff] [blame] | 3125 | * ata_timing_cycle2mode - find xfer mode for the specified cycle duration |
| 3126 | * @xfer_shift: ATA_SHIFT_* value for transfer type to examine. |
| 3127 | * @cycle: cycle duration in ns |
| 3128 | * |
| 3129 | * Return matching xfer mode for @cycle. The returned mode is of |
| 3130 | * the transfer type specified by @xfer_shift. If @cycle is too |
| 3131 | * slow for @xfer_shift, 0xff is returned. If @cycle is faster |
| 3132 | * than the fastest known mode, the fasted mode is returned. |
| 3133 | * |
| 3134 | * LOCKING: |
| 3135 | * None. |
| 3136 | * |
| 3137 | * RETURNS: |
| 3138 | * Matching xfer_mode, 0xff if no match found. |
| 3139 | */ |
| 3140 | u8 ata_timing_cycle2mode(unsigned int xfer_shift, int cycle) |
| 3141 | { |
| 3142 | u8 base_mode = 0xff, last_mode = 0xff; |
| 3143 | const struct ata_xfer_ent *ent; |
| 3144 | const struct ata_timing *t; |
| 3145 | |
| 3146 | for (ent = ata_xfer_tbl; ent->shift >= 0; ent++) |
| 3147 | if (ent->shift == xfer_shift) |
| 3148 | base_mode = ent->base; |
| 3149 | |
| 3150 | for (t = ata_timing_find_mode(base_mode); |
| 3151 | t && ata_xfer_mode2shift(t->mode) == xfer_shift; t++) { |
| 3152 | unsigned short this_cycle; |
| 3153 | |
| 3154 | switch (xfer_shift) { |
| 3155 | case ATA_SHIFT_PIO: |
| 3156 | case ATA_SHIFT_MWDMA: |
| 3157 | this_cycle = t->cycle; |
| 3158 | break; |
| 3159 | case ATA_SHIFT_UDMA: |
| 3160 | this_cycle = t->udma; |
| 3161 | break; |
| 3162 | default: |
| 3163 | return 0xff; |
| 3164 | } |
| 3165 | |
| 3166 | if (cycle > this_cycle) |
| 3167 | break; |
| 3168 | |
| 3169 | last_mode = t->mode; |
| 3170 | } |
| 3171 | |
| 3172 | return last_mode; |
| 3173 | } |
Bartlomiej Zolnierkiewicz | a9b2c12 | 2020-03-26 16:58:07 +0100 | [diff] [blame] | 3174 | #endif |
Tejun Heo | a0f79b9 | 2007-12-18 16:33:05 +0900 | [diff] [blame] | 3175 | |
| 3176 | /** |
Tejun Heo | cf176e1 | 2006-04-02 17:54:46 +0900 | [diff] [blame] | 3177 | * ata_down_xfermask_limit - adjust dev xfer masks downward |
Tejun Heo | cf176e1 | 2006-04-02 17:54:46 +0900 | [diff] [blame] | 3178 | * @dev: Device to adjust xfer masks |
Tejun Heo | 458337d | 2007-02-02 16:22:30 +0900 | [diff] [blame] | 3179 | * @sel: ATA_DNXFER_* selector |
Tejun Heo | cf176e1 | 2006-04-02 17:54:46 +0900 | [diff] [blame] | 3180 | * |
| 3181 | * Adjust xfer masks of @dev downward. Note that this function |
| 3182 | * does not apply the change. Invoking ata_set_mode() afterwards |
| 3183 | * will apply the limit. |
| 3184 | * |
| 3185 | * LOCKING: |
| 3186 | * Inherited from caller. |
| 3187 | * |
| 3188 | * RETURNS: |
| 3189 | * 0 on success, negative errno on failure |
| 3190 | */ |
Tejun Heo | 458337d | 2007-02-02 16:22:30 +0900 | [diff] [blame] | 3191 | int ata_down_xfermask_limit(struct ata_device *dev, unsigned int sel) |
Tejun Heo | cf176e1 | 2006-04-02 17:54:46 +0900 | [diff] [blame] | 3192 | { |
Tejun Heo | 458337d | 2007-02-02 16:22:30 +0900 | [diff] [blame] | 3193 | char buf[32]; |
Tejun Heo | 7dc951a | 2007-11-27 19:43:42 +0900 | [diff] [blame] | 3194 | unsigned long orig_mask, xfer_mask; |
| 3195 | unsigned long pio_mask, mwdma_mask, udma_mask; |
Tejun Heo | 458337d | 2007-02-02 16:22:30 +0900 | [diff] [blame] | 3196 | int quiet, highbit; |
Tejun Heo | cf176e1 | 2006-04-02 17:54:46 +0900 | [diff] [blame] | 3197 | |
Tejun Heo | 458337d | 2007-02-02 16:22:30 +0900 | [diff] [blame] | 3198 | quiet = !!(sel & ATA_DNXFER_QUIET); |
| 3199 | sel &= ~ATA_DNXFER_QUIET; |
Tejun Heo | cf176e1 | 2006-04-02 17:54:46 +0900 | [diff] [blame] | 3200 | |
Tejun Heo | 458337d | 2007-02-02 16:22:30 +0900 | [diff] [blame] | 3201 | xfer_mask = orig_mask = ata_pack_xfermask(dev->pio_mask, |
| 3202 | dev->mwdma_mask, |
| 3203 | dev->udma_mask); |
| 3204 | ata_unpack_xfermask(xfer_mask, &pio_mask, &mwdma_mask, &udma_mask); |
Tejun Heo | cf176e1 | 2006-04-02 17:54:46 +0900 | [diff] [blame] | 3205 | |
Tejun Heo | 458337d | 2007-02-02 16:22:30 +0900 | [diff] [blame] | 3206 | switch (sel) { |
| 3207 | case ATA_DNXFER_PIO: |
| 3208 | highbit = fls(pio_mask) - 1; |
| 3209 | pio_mask &= ~(1 << highbit); |
| 3210 | break; |
| 3211 | |
| 3212 | case ATA_DNXFER_DMA: |
| 3213 | if (udma_mask) { |
| 3214 | highbit = fls(udma_mask) - 1; |
| 3215 | udma_mask &= ~(1 << highbit); |
| 3216 | if (!udma_mask) |
| 3217 | return -ENOENT; |
| 3218 | } else if (mwdma_mask) { |
| 3219 | highbit = fls(mwdma_mask) - 1; |
| 3220 | mwdma_mask &= ~(1 << highbit); |
| 3221 | if (!mwdma_mask) |
| 3222 | return -ENOENT; |
| 3223 | } |
| 3224 | break; |
| 3225 | |
| 3226 | case ATA_DNXFER_40C: |
| 3227 | udma_mask &= ATA_UDMA_MASK_40C; |
| 3228 | break; |
| 3229 | |
| 3230 | case ATA_DNXFER_FORCE_PIO0: |
| 3231 | pio_mask &= 1; |
Gustavo A. R. Silva | df561f66 | 2020-08-23 17:36:59 -0500 | [diff] [blame] | 3232 | fallthrough; |
Tejun Heo | 458337d | 2007-02-02 16:22:30 +0900 | [diff] [blame] | 3233 | case ATA_DNXFER_FORCE_PIO: |
| 3234 | mwdma_mask = 0; |
| 3235 | udma_mask = 0; |
| 3236 | break; |
| 3237 | |
Tejun Heo | 458337d | 2007-02-02 16:22:30 +0900 | [diff] [blame] | 3238 | default: |
| 3239 | BUG(); |
| 3240 | } |
| 3241 | |
| 3242 | xfer_mask &= ata_pack_xfermask(pio_mask, mwdma_mask, udma_mask); |
| 3243 | |
| 3244 | if (!(xfer_mask & ATA_MASK_PIO) || xfer_mask == orig_mask) |
| 3245 | return -ENOENT; |
| 3246 | |
| 3247 | if (!quiet) { |
| 3248 | if (xfer_mask & (ATA_MASK_MWDMA | ATA_MASK_UDMA)) |
| 3249 | snprintf(buf, sizeof(buf), "%s:%s", |
| 3250 | ata_mode_string(xfer_mask), |
| 3251 | ata_mode_string(xfer_mask & ATA_MASK_PIO)); |
| 3252 | else |
| 3253 | snprintf(buf, sizeof(buf), "%s", |
| 3254 | ata_mode_string(xfer_mask)); |
| 3255 | |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 3256 | ata_dev_warn(dev, "limiting speed to %s\n", buf); |
Tejun Heo | 458337d | 2007-02-02 16:22:30 +0900 | [diff] [blame] | 3257 | } |
Tejun Heo | cf176e1 | 2006-04-02 17:54:46 +0900 | [diff] [blame] | 3258 | |
| 3259 | ata_unpack_xfermask(xfer_mask, &dev->pio_mask, &dev->mwdma_mask, |
| 3260 | &dev->udma_mask); |
| 3261 | |
Tejun Heo | cf176e1 | 2006-04-02 17:54:46 +0900 | [diff] [blame] | 3262 | return 0; |
Tejun Heo | cf176e1 | 2006-04-02 17:54:46 +0900 | [diff] [blame] | 3263 | } |
| 3264 | |
Tejun Heo | 3373efd | 2006-05-15 20:57:53 +0900 | [diff] [blame] | 3265 | static int ata_dev_set_mode(struct ata_device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3266 | { |
Tejun Heo | d0cb43b | 2009-07-09 09:27:50 +0900 | [diff] [blame] | 3267 | struct ata_port *ap = dev->link->ap; |
Tejun Heo | 9af5c9c | 2007-08-06 18:36:22 +0900 | [diff] [blame] | 3268 | struct ata_eh_context *ehc = &dev->link->eh_context; |
Tejun Heo | d0cb43b | 2009-07-09 09:27:50 +0900 | [diff] [blame] | 3269 | const bool nosetxfer = dev->horkage & ATA_HORKAGE_NOSETXFER; |
Tejun Heo | 4055dee | 2008-02-07 10:34:08 +0900 | [diff] [blame] | 3270 | const char *dev_err_whine = ""; |
| 3271 | int ign_dev_err = 0; |
Tejun Heo | d0cb43b | 2009-07-09 09:27:50 +0900 | [diff] [blame] | 3272 | unsigned int err_mask = 0; |
Tejun Heo | 83206a2 | 2006-03-24 15:25:31 +0900 | [diff] [blame] | 3273 | int rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3274 | |
Tejun Heo | e838460 | 2006-04-02 18:51:53 +0900 | [diff] [blame] | 3275 | dev->flags &= ~ATA_DFLAG_PIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3276 | if (dev->xfer_shift == ATA_SHIFT_PIO) |
| 3277 | dev->flags |= ATA_DFLAG_PIO; |
| 3278 | |
Tejun Heo | d0cb43b | 2009-07-09 09:27:50 +0900 | [diff] [blame] | 3279 | if (nosetxfer && ap->flags & ATA_FLAG_SATA && ata_id_is_sata(dev->id)) |
| 3280 | dev_err_whine = " (SET_XFERMODE skipped)"; |
| 3281 | else { |
| 3282 | if (nosetxfer) |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 3283 | ata_dev_warn(dev, |
| 3284 | "NOSETXFER but PATA detected - can't " |
| 3285 | "skip SETXFER, might malfunction\n"); |
Tejun Heo | d0cb43b | 2009-07-09 09:27:50 +0900 | [diff] [blame] | 3286 | err_mask = ata_dev_set_xfermode(dev); |
| 3287 | } |
Jeff Garzik | 2dcb407 | 2007-10-19 06:42:56 -0400 | [diff] [blame] | 3288 | |
Tejun Heo | 4055dee | 2008-02-07 10:34:08 +0900 | [diff] [blame] | 3289 | if (err_mask & ~AC_ERR_DEV) |
| 3290 | goto fail; |
Jeff Garzik | 2dcb407 | 2007-10-19 06:42:56 -0400 | [diff] [blame] | 3291 | |
Tejun Heo | 4055dee | 2008-02-07 10:34:08 +0900 | [diff] [blame] | 3292 | /* revalidate */ |
Tejun Heo | baa1e78 | 2006-11-01 18:39:27 +0900 | [diff] [blame] | 3293 | ehc->i.flags |= ATA_EHI_POST_SETMODE; |
Tejun Heo | 422c9da | 2007-09-23 13:14:12 +0900 | [diff] [blame] | 3294 | rc = ata_dev_revalidate(dev, ATA_DEV_UNKNOWN, 0); |
Tejun Heo | baa1e78 | 2006-11-01 18:39:27 +0900 | [diff] [blame] | 3295 | ehc->i.flags &= ~ATA_EHI_POST_SETMODE; |
Tejun Heo | 5eb45c0 | 2006-04-02 18:51:52 +0900 | [diff] [blame] | 3296 | if (rc) |
Tejun Heo | 83206a2 | 2006-03-24 15:25:31 +0900 | [diff] [blame] | 3297 | return rc; |
Tejun Heo | 48a8a14 | 2006-03-05 17:55:58 +0900 | [diff] [blame] | 3298 | |
Alan Cox | b93fda1 | 2008-04-08 16:36:44 +0100 | [diff] [blame] | 3299 | if (dev->xfer_shift == ATA_SHIFT_PIO) { |
| 3300 | /* Old CFA may refuse this command, which is just fine */ |
| 3301 | if (ata_id_is_cfa(dev->id)) |
| 3302 | ign_dev_err = 1; |
| 3303 | /* Catch several broken garbage emulations plus some pre |
| 3304 | ATA devices */ |
| 3305 | if (ata_id_major_version(dev->id) == 0 && |
| 3306 | dev->pio_mode <= XFER_PIO_2) |
| 3307 | ign_dev_err = 1; |
| 3308 | /* Some very old devices and some bad newer ones fail |
| 3309 | any kind of SET_XFERMODE request but support PIO0-2 |
| 3310 | timings and no IORDY */ |
| 3311 | if (!ata_id_has_iordy(dev->id) && dev->pio_mode <= XFER_PIO_2) |
| 3312 | ign_dev_err = 1; |
| 3313 | } |
Tejun Heo | 4055dee | 2008-02-07 10:34:08 +0900 | [diff] [blame] | 3314 | /* Early MWDMA devices do DMA but don't allow DMA mode setting. |
| 3315 | Don't fail an MWDMA0 set IFF the device indicates it is in MWDMA0 */ |
| 3316 | if (dev->xfer_shift == ATA_SHIFT_MWDMA && |
| 3317 | dev->dma_mode == XFER_MW_DMA_0 && |
| 3318 | (dev->id[63] >> 8) & 1) |
| 3319 | ign_dev_err = 1; |
| 3320 | |
| 3321 | /* if the device is actually configured correctly, ignore dev err */ |
| 3322 | if (dev->xfer_mode == ata_xfer_mask2mode(ata_id_xfermask(dev->id))) |
| 3323 | ign_dev_err = 1; |
| 3324 | |
| 3325 | if (err_mask & AC_ERR_DEV) { |
| 3326 | if (!ign_dev_err) |
| 3327 | goto fail; |
| 3328 | else |
| 3329 | dev_err_whine = " (device error ignored)"; |
| 3330 | } |
| 3331 | |
Hannes Reinecke | 4633778 | 2021-12-21 08:20:37 +0100 | [diff] [blame] | 3332 | ata_dev_dbg(dev, "xfer_shift=%u, xfer_mode=0x%x\n", |
| 3333 | dev->xfer_shift, (int)dev->xfer_mode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3334 | |
Damien Le Moal | 07b9b6d | 2018-05-09 09:28:10 +0900 | [diff] [blame] | 3335 | if (!(ehc->i.flags & ATA_EHI_QUIET) || |
| 3336 | ehc->i.flags & ATA_EHI_DID_HARDRESET) |
| 3337 | ata_dev_info(dev, "configured for %s%s\n", |
| 3338 | ata_mode_string(ata_xfer_mode2mask(dev->xfer_mode)), |
| 3339 | dev_err_whine); |
Tejun Heo | 4055dee | 2008-02-07 10:34:08 +0900 | [diff] [blame] | 3340 | |
Tejun Heo | 83206a2 | 2006-03-24 15:25:31 +0900 | [diff] [blame] | 3341 | return 0; |
Tejun Heo | 4055dee | 2008-02-07 10:34:08 +0900 | [diff] [blame] | 3342 | |
| 3343 | fail: |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 3344 | ata_dev_err(dev, "failed to set xfermode (err_mask=0x%x)\n", err_mask); |
Tejun Heo | 4055dee | 2008-02-07 10:34:08 +0900 | [diff] [blame] | 3345 | return -EIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3346 | } |
| 3347 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3348 | /** |
Alan | 0435182 | 2007-03-06 02:37:52 -0800 | [diff] [blame] | 3349 | * ata_do_set_mode - Program timings and issue SET FEATURES - XFER |
Tejun Heo | 0260731 | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 3350 | * @link: link on which timings will be programmed |
Joe Perches | 1967b7f | 2008-02-03 17:08:11 +0200 | [diff] [blame] | 3351 | * @r_failed_dev: out parameter for failed device |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3352 | * |
Alan | 0435182 | 2007-03-06 02:37:52 -0800 | [diff] [blame] | 3353 | * Standard implementation of the function used to tune and set |
| 3354 | * ATA device disk transfer mode (PIO3, UDMA6, etc.). If |
| 3355 | * ata_dev_set_mode() fails, pointer to the failing device is |
Tejun Heo | e82cbdb | 2006-04-01 01:38:18 +0900 | [diff] [blame] | 3356 | * returned in @r_failed_dev. |
Jeff Garzik | 780a87f | 2005-05-30 15:41:05 -0400 | [diff] [blame] | 3357 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3358 | * LOCKING: |
Jeff Garzik | 0cba632 | 2005-05-30 19:49:12 -0400 | [diff] [blame] | 3359 | * PCI/etc. bus probe sem. |
Tejun Heo | e82cbdb | 2006-04-01 01:38:18 +0900 | [diff] [blame] | 3360 | * |
| 3361 | * RETURNS: |
| 3362 | * 0 on success, negative errno otherwise |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3363 | */ |
Alan | 0435182 | 2007-03-06 02:37:52 -0800 | [diff] [blame] | 3364 | |
Tejun Heo | 0260731 | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 3365 | int ata_do_set_mode(struct ata_link *link, struct ata_device **r_failed_dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3366 | { |
Tejun Heo | 0260731 | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 3367 | struct ata_port *ap = link->ap; |
Tejun Heo | e8e0619 | 2006-04-01 01:38:18 +0900 | [diff] [blame] | 3368 | struct ata_device *dev; |
Tejun Heo | f58229f | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 3369 | int rc = 0, used_dma = 0, found = 0; |
Tejun Heo | 3adcebb | 2006-05-15 20:57:37 +0900 | [diff] [blame] | 3370 | |
Tejun Heo | a6d5a51 | 2006-03-06 04:31:57 +0900 | [diff] [blame] | 3371 | /* step 1: calculate xfer_mask */ |
Tejun Heo | 1eca436 | 2008-11-03 20:03:17 +0900 | [diff] [blame] | 3372 | ata_for_each_dev(dev, link, ENABLED) { |
Tejun Heo | 7dc951a | 2007-11-27 19:43:42 +0900 | [diff] [blame] | 3373 | unsigned long pio_mask, dma_mask; |
Alan Cox | b3a7060 | 2007-10-02 12:38:26 -0400 | [diff] [blame] | 3374 | unsigned int mode_mask; |
Tejun Heo | a6d5a51 | 2006-03-06 04:31:57 +0900 | [diff] [blame] | 3375 | |
Alan Cox | b3a7060 | 2007-10-02 12:38:26 -0400 | [diff] [blame] | 3376 | mode_mask = ATA_DMA_MASK_ATA; |
| 3377 | if (dev->class == ATA_DEV_ATAPI) |
| 3378 | mode_mask = ATA_DMA_MASK_ATAPI; |
| 3379 | else if (ata_id_is_cfa(dev->id)) |
| 3380 | mode_mask = ATA_DMA_MASK_CFA; |
| 3381 | |
Tejun Heo | 3373efd | 2006-05-15 20:57:53 +0900 | [diff] [blame] | 3382 | ata_dev_xfermask(dev); |
Tejun Heo | 3326732 | 2008-02-13 09:15:09 +0900 | [diff] [blame] | 3383 | ata_force_xfermask(dev); |
Tejun Heo | a6d5a51 | 2006-03-06 04:31:57 +0900 | [diff] [blame] | 3384 | |
Tejun Heo | acf356b | 2006-03-24 14:07:50 +0900 | [diff] [blame] | 3385 | pio_mask = ata_pack_xfermask(dev->pio_mask, 0, 0); |
Alan Cox | b3a7060 | 2007-10-02 12:38:26 -0400 | [diff] [blame] | 3386 | |
| 3387 | if (libata_dma_mask & mode_mask) |
Sergei Shtylyov | 80a9c43 | 2011-12-22 22:41:32 +0300 | [diff] [blame] | 3388 | dma_mask = ata_pack_xfermask(0, dev->mwdma_mask, |
| 3389 | dev->udma_mask); |
Alan Cox | b3a7060 | 2007-10-02 12:38:26 -0400 | [diff] [blame] | 3390 | else |
| 3391 | dma_mask = 0; |
| 3392 | |
Tejun Heo | acf356b | 2006-03-24 14:07:50 +0900 | [diff] [blame] | 3393 | dev->pio_mode = ata_xfer_mask2mode(pio_mask); |
| 3394 | dev->dma_mode = ata_xfer_mask2mode(dma_mask); |
Alan Cox | 5444a6f | 2006-03-27 18:58:20 +0100 | [diff] [blame] | 3395 | |
Tejun Heo | 4f65977 | 2006-04-01 01:38:18 +0900 | [diff] [blame] | 3396 | found = 1; |
Alan Cox | b15b3eb | 2008-08-01 09:18:34 +0100 | [diff] [blame] | 3397 | if (ata_dma_enabled(dev)) |
Alan Cox | 5444a6f | 2006-03-27 18:58:20 +0100 | [diff] [blame] | 3398 | used_dma = 1; |
Tejun Heo | a6d5a51 | 2006-03-06 04:31:57 +0900 | [diff] [blame] | 3399 | } |
Tejun Heo | 4f65977 | 2006-04-01 01:38:18 +0900 | [diff] [blame] | 3400 | if (!found) |
Tejun Heo | e82cbdb | 2006-04-01 01:38:18 +0900 | [diff] [blame] | 3401 | goto out; |
Tejun Heo | a6d5a51 | 2006-03-06 04:31:57 +0900 | [diff] [blame] | 3402 | |
| 3403 | /* step 2: always set host PIO timings */ |
Tejun Heo | 1eca436 | 2008-11-03 20:03:17 +0900 | [diff] [blame] | 3404 | ata_for_each_dev(dev, link, ENABLED) { |
Tejun Heo | 70cd071 | 2007-11-27 19:43:40 +0900 | [diff] [blame] | 3405 | if (dev->pio_mode == 0xff) { |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 3406 | ata_dev_warn(dev, "no PIO support\n"); |
Tejun Heo | e8e0619 | 2006-04-01 01:38:18 +0900 | [diff] [blame] | 3407 | rc = -EINVAL; |
Tejun Heo | e82cbdb | 2006-04-01 01:38:18 +0900 | [diff] [blame] | 3408 | goto out; |
Tejun Heo | e8e0619 | 2006-04-01 01:38:18 +0900 | [diff] [blame] | 3409 | } |
| 3410 | |
| 3411 | dev->xfer_mode = dev->pio_mode; |
| 3412 | dev->xfer_shift = ATA_SHIFT_PIO; |
| 3413 | if (ap->ops->set_piomode) |
| 3414 | ap->ops->set_piomode(ap, dev); |
| 3415 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3416 | |
Tejun Heo | a6d5a51 | 2006-03-06 04:31:57 +0900 | [diff] [blame] | 3417 | /* step 3: set host DMA timings */ |
Tejun Heo | 1eca436 | 2008-11-03 20:03:17 +0900 | [diff] [blame] | 3418 | ata_for_each_dev(dev, link, ENABLED) { |
| 3419 | if (!ata_dma_enabled(dev)) |
Tejun Heo | e8e0619 | 2006-04-01 01:38:18 +0900 | [diff] [blame] | 3420 | continue; |
| 3421 | |
| 3422 | dev->xfer_mode = dev->dma_mode; |
| 3423 | dev->xfer_shift = ata_xfer_mode2shift(dev->dma_mode); |
| 3424 | if (ap->ops->set_dmamode) |
| 3425 | ap->ops->set_dmamode(ap, dev); |
| 3426 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3427 | |
| 3428 | /* step 4: update devices' xfer mode */ |
Tejun Heo | 1eca436 | 2008-11-03 20:03:17 +0900 | [diff] [blame] | 3429 | ata_for_each_dev(dev, link, ENABLED) { |
Tejun Heo | 3373efd | 2006-05-15 20:57:53 +0900 | [diff] [blame] | 3430 | rc = ata_dev_set_mode(dev); |
Tejun Heo | 5bbc53f | 2006-04-01 01:38:17 +0900 | [diff] [blame] | 3431 | if (rc) |
Tejun Heo | e82cbdb | 2006-04-01 01:38:18 +0900 | [diff] [blame] | 3432 | goto out; |
Tejun Heo | 83206a2 | 2006-03-24 15:25:31 +0900 | [diff] [blame] | 3433 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3434 | |
Tejun Heo | e8e0619 | 2006-04-01 01:38:18 +0900 | [diff] [blame] | 3435 | /* Record simplex status. If we selected DMA then the other |
| 3436 | * host channels are not permitted to do so. |
Alan Cox | 5444a6f | 2006-03-27 18:58:20 +0100 | [diff] [blame] | 3437 | */ |
Jeff Garzik | cca3974 | 2006-08-24 03:19:22 -0400 | [diff] [blame] | 3438 | if (used_dma && (ap->host->flags & ATA_HOST_SIMPLEX)) |
Alan | 032af1c | 2007-03-01 17:36:46 +0000 | [diff] [blame] | 3439 | ap->host->simplex_claimed = ap; |
Alan Cox | 5444a6f | 2006-03-27 18:58:20 +0100 | [diff] [blame] | 3440 | |
Tejun Heo | e82cbdb | 2006-04-01 01:38:18 +0900 | [diff] [blame] | 3441 | out: |
| 3442 | if (rc) |
| 3443 | *r_failed_dev = dev; |
| 3444 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3445 | } |
Bartlomiej Zolnierkiewicz | a52fbcf | 2020-03-26 16:58:04 +0100 | [diff] [blame] | 3446 | EXPORT_SYMBOL_GPL(ata_do_set_mode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3447 | |
| 3448 | /** |
Tejun Heo | aa2731a | 2008-04-07 22:47:19 +0900 | [diff] [blame] | 3449 | * ata_wait_ready - wait for link to become ready |
| 3450 | * @link: link to be waited on |
| 3451 | * @deadline: deadline jiffies for the operation |
| 3452 | * @check_ready: callback to check link readiness |
| 3453 | * |
| 3454 | * Wait for @link to become ready. @check_ready should return |
| 3455 | * positive number if @link is ready, 0 if it isn't, -ENODEV if |
| 3456 | * link doesn't seem to be occupied, other errno for other error |
| 3457 | * conditions. |
| 3458 | * |
| 3459 | * Transient -ENODEV conditions are allowed for |
| 3460 | * ATA_TMOUT_FF_WAIT. |
| 3461 | * |
| 3462 | * LOCKING: |
| 3463 | * EH context. |
| 3464 | * |
| 3465 | * RETURNS: |
Masanari Iida | c9b5560 | 2016-04-13 23:36:27 +0900 | [diff] [blame] | 3466 | * 0 if @link is ready before @deadline; otherwise, -errno. |
Tejun Heo | aa2731a | 2008-04-07 22:47:19 +0900 | [diff] [blame] | 3467 | */ |
| 3468 | int ata_wait_ready(struct ata_link *link, unsigned long deadline, |
| 3469 | int (*check_ready)(struct ata_link *link)) |
| 3470 | { |
| 3471 | unsigned long start = jiffies; |
Tejun Heo | b48d58f | 2010-04-09 19:46:38 +0900 | [diff] [blame] | 3472 | unsigned long nodev_deadline; |
Tejun Heo | aa2731a | 2008-04-07 22:47:19 +0900 | [diff] [blame] | 3473 | int warned = 0; |
| 3474 | |
Tejun Heo | b48d58f | 2010-04-09 19:46:38 +0900 | [diff] [blame] | 3475 | /* choose which 0xff timeout to use, read comment in libata.h */ |
| 3476 | if (link->ap->host->flags & ATA_HOST_PARALLEL_SCAN) |
| 3477 | nodev_deadline = ata_deadline(start, ATA_TMOUT_FF_WAIT_LONG); |
| 3478 | else |
| 3479 | nodev_deadline = ata_deadline(start, ATA_TMOUT_FF_WAIT); |
| 3480 | |
Tejun Heo | b1c7291 | 2008-07-31 17:02:43 +0900 | [diff] [blame] | 3481 | /* Slave readiness can't be tested separately from master. On |
| 3482 | * M/S emulation configuration, this function should be called |
| 3483 | * only on the master and it will handle both master and slave. |
| 3484 | */ |
| 3485 | WARN_ON(link == link->ap->slave_link); |
| 3486 | |
Tejun Heo | aa2731a | 2008-04-07 22:47:19 +0900 | [diff] [blame] | 3487 | if (time_after(nodev_deadline, deadline)) |
| 3488 | nodev_deadline = deadline; |
| 3489 | |
| 3490 | while (1) { |
| 3491 | unsigned long now = jiffies; |
| 3492 | int ready, tmp; |
| 3493 | |
| 3494 | ready = tmp = check_ready(link); |
| 3495 | if (ready > 0) |
| 3496 | return 0; |
| 3497 | |
Tejun Heo | b48d58f | 2010-04-09 19:46:38 +0900 | [diff] [blame] | 3498 | /* |
| 3499 | * -ENODEV could be transient. Ignore -ENODEV if link |
Tejun Heo | aa2731a | 2008-04-07 22:47:19 +0900 | [diff] [blame] | 3500 | * is online. Also, some SATA devices take a long |
Tejun Heo | b48d58f | 2010-04-09 19:46:38 +0900 | [diff] [blame] | 3501 | * time to clear 0xff after reset. Wait for |
| 3502 | * ATA_TMOUT_FF_WAIT[_LONG] on -ENODEV if link isn't |
| 3503 | * offline. |
Tejun Heo | aa2731a | 2008-04-07 22:47:19 +0900 | [diff] [blame] | 3504 | * |
| 3505 | * Note that some PATA controllers (pata_ali) explode |
| 3506 | * if status register is read more than once when |
| 3507 | * there's no device attached. |
| 3508 | */ |
| 3509 | if (ready == -ENODEV) { |
| 3510 | if (ata_link_online(link)) |
| 3511 | ready = 0; |
| 3512 | else if ((link->ap->flags & ATA_FLAG_SATA) && |
| 3513 | !ata_link_offline(link) && |
| 3514 | time_before(now, nodev_deadline)) |
| 3515 | ready = 0; |
| 3516 | } |
| 3517 | |
| 3518 | if (ready) |
| 3519 | return ready; |
| 3520 | if (time_after(now, deadline)) |
| 3521 | return -EBUSY; |
| 3522 | |
| 3523 | if (!warned && time_after(now, start + 5 * HZ) && |
| 3524 | (deadline - now > 3 * HZ)) { |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 3525 | ata_link_warn(link, |
Tejun Heo | aa2731a | 2008-04-07 22:47:19 +0900 | [diff] [blame] | 3526 | "link is slow to respond, please be patient " |
| 3527 | "(ready=%d)\n", tmp); |
| 3528 | warned = 1; |
| 3529 | } |
| 3530 | |
Tejun Heo | 97750ce | 2010-09-06 17:56:29 +0200 | [diff] [blame] | 3531 | ata_msleep(link->ap, 50); |
Tejun Heo | aa2731a | 2008-04-07 22:47:19 +0900 | [diff] [blame] | 3532 | } |
| 3533 | } |
| 3534 | |
| 3535 | /** |
| 3536 | * ata_wait_after_reset - wait for link to become ready after reset |
| 3537 | * @link: link to be waited on |
| 3538 | * @deadline: deadline jiffies for the operation |
| 3539 | * @check_ready: callback to check link readiness |
| 3540 | * |
| 3541 | * Wait for @link to become ready after reset. |
| 3542 | * |
| 3543 | * LOCKING: |
| 3544 | * EH context. |
| 3545 | * |
| 3546 | * RETURNS: |
Masanari Iida | c9b5560 | 2016-04-13 23:36:27 +0900 | [diff] [blame] | 3547 | * 0 if @link is ready before @deadline; otherwise, -errno. |
Tejun Heo | aa2731a | 2008-04-07 22:47:19 +0900 | [diff] [blame] | 3548 | */ |
Harvey Harrison | 2b4221b | 2008-04-24 18:37:34 -0700 | [diff] [blame] | 3549 | int ata_wait_after_reset(struct ata_link *link, unsigned long deadline, |
Tejun Heo | aa2731a | 2008-04-07 22:47:19 +0900 | [diff] [blame] | 3550 | int (*check_ready)(struct ata_link *link)) |
| 3551 | { |
Tejun Heo | 97750ce | 2010-09-06 17:56:29 +0200 | [diff] [blame] | 3552 | ata_msleep(link->ap, ATA_WAIT_AFTER_RESET); |
Tejun Heo | aa2731a | 2008-04-07 22:47:19 +0900 | [diff] [blame] | 3553 | |
| 3554 | return ata_wait_ready(link, deadline, check_ready); |
| 3555 | } |
Bartlomiej Zolnierkiewicz | a52fbcf | 2020-03-26 16:58:04 +0100 | [diff] [blame] | 3556 | EXPORT_SYMBOL_GPL(ata_wait_after_reset); |
Tejun Heo | aa2731a | 2008-04-07 22:47:19 +0900 | [diff] [blame] | 3557 | |
| 3558 | /** |
Tejun Heo | 0aa1113 | 2008-04-07 22:47:18 +0900 | [diff] [blame] | 3559 | * ata_std_prereset - prepare for reset |
Tejun Heo | cc0680a | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 3560 | * @link: ATA link to be reset |
Tejun Heo | d4b2bab | 2007-02-02 16:50:52 +0900 | [diff] [blame] | 3561 | * @deadline: deadline jiffies for the operation |
Tejun Heo | f5914a4 | 2006-05-31 18:27:48 +0900 | [diff] [blame] | 3562 | * |
Tejun Heo | cc0680a | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 3563 | * @link is about to be reset. Initialize it. Failure from |
Tejun Heo | b8cffc6 | 2007-02-02 16:50:52 +0900 | [diff] [blame] | 3564 | * prereset makes libata abort whole reset sequence and give up |
| 3565 | * that port, so prereset should be best-effort. It does its |
| 3566 | * best to prepare for reset sequence but if things go wrong, it |
| 3567 | * should just whine, not fail. |
Tejun Heo | f5914a4 | 2006-05-31 18:27:48 +0900 | [diff] [blame] | 3568 | * |
| 3569 | * LOCKING: |
| 3570 | * Kernel thread context (may sleep) |
| 3571 | * |
| 3572 | * RETURNS: |
| 3573 | * 0 on success, -errno otherwise. |
| 3574 | */ |
Tejun Heo | 0aa1113 | 2008-04-07 22:47:18 +0900 | [diff] [blame] | 3575 | int ata_std_prereset(struct ata_link *link, unsigned long deadline) |
Tejun Heo | f5914a4 | 2006-05-31 18:27:48 +0900 | [diff] [blame] | 3576 | { |
Tejun Heo | cc0680a | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 3577 | struct ata_port *ap = link->ap; |
Tejun Heo | 936fd73 | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 3578 | struct ata_eh_context *ehc = &link->eh_context; |
Tejun Heo | e9c8391 | 2006-07-03 16:07:26 +0900 | [diff] [blame] | 3579 | const unsigned long *timing = sata_ehc_deb_timing(ehc); |
Tejun Heo | f5914a4 | 2006-05-31 18:27:48 +0900 | [diff] [blame] | 3580 | int rc; |
| 3581 | |
Tejun Heo | f5914a4 | 2006-05-31 18:27:48 +0900 | [diff] [blame] | 3582 | /* if we're about to do hardreset, nothing more to do */ |
| 3583 | if (ehc->i.action & ATA_EH_HARDRESET) |
| 3584 | return 0; |
| 3585 | |
Tejun Heo | 936fd73 | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 3586 | /* if SATA, resume link */ |
Tejun Heo | a16abc0 | 2007-05-21 18:33:47 +0200 | [diff] [blame] | 3587 | if (ap->flags & ATA_FLAG_SATA) { |
Tejun Heo | 936fd73 | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 3588 | rc = sata_link_resume(link, timing, deadline); |
Tejun Heo | b8cffc6 | 2007-02-02 16:50:52 +0900 | [diff] [blame] | 3589 | /* whine about phy resume failure but proceed */ |
| 3590 | if (rc && rc != -EOPNOTSUPP) |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 3591 | ata_link_warn(link, |
| 3592 | "failed to resume link for reset (errno=%d)\n", |
| 3593 | rc); |
Tejun Heo | f5914a4 | 2006-05-31 18:27:48 +0900 | [diff] [blame] | 3594 | } |
| 3595 | |
Tejun Heo | 45db2f6 | 2008-04-08 01:46:56 +0900 | [diff] [blame] | 3596 | /* no point in trying softreset on offline link */ |
Tejun Heo | b1c7291 | 2008-07-31 17:02:43 +0900 | [diff] [blame] | 3597 | if (ata_phys_link_offline(link)) |
Tejun Heo | 45db2f6 | 2008-04-08 01:46:56 +0900 | [diff] [blame] | 3598 | ehc->i.action &= ~ATA_EH_SOFTRESET; |
| 3599 | |
Tejun Heo | f5914a4 | 2006-05-31 18:27:48 +0900 | [diff] [blame] | 3600 | return 0; |
| 3601 | } |
Bartlomiej Zolnierkiewicz | a52fbcf | 2020-03-26 16:58:04 +0100 | [diff] [blame] | 3602 | EXPORT_SYMBOL_GPL(ata_std_prereset); |
Tejun Heo | f5914a4 | 2006-05-31 18:27:48 +0900 | [diff] [blame] | 3603 | |
Tejun Heo | c2bd580 | 2006-01-24 17:05:22 +0900 | [diff] [blame] | 3604 | /** |
Tejun Heo | 57c9efd | 2008-04-07 22:47:19 +0900 | [diff] [blame] | 3605 | * sata_std_hardreset - COMRESET w/o waiting or classification |
| 3606 | * @link: link to reset |
| 3607 | * @class: resulting class of attached device |
| 3608 | * @deadline: deadline jiffies for the operation |
| 3609 | * |
| 3610 | * Standard SATA COMRESET w/o waiting or classification. |
| 3611 | * |
| 3612 | * LOCKING: |
| 3613 | * Kernel thread context (may sleep) |
| 3614 | * |
| 3615 | * RETURNS: |
| 3616 | * 0 if link offline, -EAGAIN if link online, -errno on errors. |
| 3617 | */ |
| 3618 | int sata_std_hardreset(struct ata_link *link, unsigned int *class, |
| 3619 | unsigned long deadline) |
| 3620 | { |
| 3621 | const unsigned long *timing = sata_ehc_deb_timing(&link->eh_context); |
| 3622 | bool online; |
| 3623 | int rc; |
| 3624 | |
| 3625 | /* do hardreset */ |
| 3626 | rc = sata_link_hardreset(link, timing, deadline, &online, NULL); |
Tejun Heo | 57c9efd | 2008-04-07 22:47:19 +0900 | [diff] [blame] | 3627 | return online ? -EAGAIN : rc; |
| 3628 | } |
Bartlomiej Zolnierkiewicz | a52fbcf | 2020-03-26 16:58:04 +0100 | [diff] [blame] | 3629 | EXPORT_SYMBOL_GPL(sata_std_hardreset); |
Tejun Heo | 57c9efd | 2008-04-07 22:47:19 +0900 | [diff] [blame] | 3630 | |
| 3631 | /** |
Tejun Heo | 203c75b | 2008-04-07 22:47:18 +0900 | [diff] [blame] | 3632 | * ata_std_postreset - standard postreset callback |
Tejun Heo | cc0680a | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 3633 | * @link: the target ata_link |
Tejun Heo | c2bd580 | 2006-01-24 17:05:22 +0900 | [diff] [blame] | 3634 | * @classes: classes of attached devices |
| 3635 | * |
| 3636 | * This function is invoked after a successful reset. Note that |
| 3637 | * the device might have been reset more than once using |
| 3638 | * different reset methods before postreset is invoked. |
Tejun Heo | c2bd580 | 2006-01-24 17:05:22 +0900 | [diff] [blame] | 3639 | * |
Tejun Heo | c2bd580 | 2006-01-24 17:05:22 +0900 | [diff] [blame] | 3640 | * LOCKING: |
| 3641 | * Kernel thread context (may sleep) |
| 3642 | */ |
Tejun Heo | 203c75b | 2008-04-07 22:47:18 +0900 | [diff] [blame] | 3643 | void ata_std_postreset(struct ata_link *link, unsigned int *classes) |
Tejun Heo | c2bd580 | 2006-01-24 17:05:22 +0900 | [diff] [blame] | 3644 | { |
Tejun Heo | f046519 | 2008-05-19 01:15:08 +0900 | [diff] [blame] | 3645 | u32 serror; |
| 3646 | |
Tejun Heo | f046519 | 2008-05-19 01:15:08 +0900 | [diff] [blame] | 3647 | /* reset complete, clear SError */ |
| 3648 | if (!sata_scr_read(link, SCR_ERROR, &serror)) |
| 3649 | sata_scr_write(link, SCR_ERROR, serror); |
| 3650 | |
Tejun Heo | c2bd580 | 2006-01-24 17:05:22 +0900 | [diff] [blame] | 3651 | /* print link status */ |
Tejun Heo | 936fd73 | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 3652 | sata_print_link_status(link); |
Tejun Heo | c2bd580 | 2006-01-24 17:05:22 +0900 | [diff] [blame] | 3653 | } |
Bartlomiej Zolnierkiewicz | a52fbcf | 2020-03-26 16:58:04 +0100 | [diff] [blame] | 3654 | EXPORT_SYMBOL_GPL(ata_std_postreset); |
Tejun Heo | c2bd580 | 2006-01-24 17:05:22 +0900 | [diff] [blame] | 3655 | |
Tejun Heo | a62c0fc | 2006-01-24 17:05:22 +0900 | [diff] [blame] | 3656 | /** |
Tejun Heo | 623a312 | 2006-03-05 17:55:58 +0900 | [diff] [blame] | 3657 | * ata_dev_same_device - Determine whether new ID matches configured device |
Tejun Heo | 623a312 | 2006-03-05 17:55:58 +0900 | [diff] [blame] | 3658 | * @dev: device to compare against |
| 3659 | * @new_class: class of the new device |
| 3660 | * @new_id: IDENTIFY page of the new device |
| 3661 | * |
| 3662 | * Compare @new_class and @new_id against @dev and determine |
| 3663 | * whether @dev is the device indicated by @new_class and |
| 3664 | * @new_id. |
| 3665 | * |
| 3666 | * LOCKING: |
| 3667 | * None. |
| 3668 | * |
| 3669 | * RETURNS: |
| 3670 | * 1 if @dev matches @new_class and @new_id, 0 otherwise. |
| 3671 | */ |
Tejun Heo | 3373efd | 2006-05-15 20:57:53 +0900 | [diff] [blame] | 3672 | static int ata_dev_same_device(struct ata_device *dev, unsigned int new_class, |
| 3673 | const u16 *new_id) |
Tejun Heo | 623a312 | 2006-03-05 17:55:58 +0900 | [diff] [blame] | 3674 | { |
| 3675 | const u16 *old_id = dev->id; |
Tejun Heo | a0cf733 | 2007-01-02 20:18:49 +0900 | [diff] [blame] | 3676 | unsigned char model[2][ATA_ID_PROD_LEN + 1]; |
| 3677 | unsigned char serial[2][ATA_ID_SERNO_LEN + 1]; |
Tejun Heo | 623a312 | 2006-03-05 17:55:58 +0900 | [diff] [blame] | 3678 | |
| 3679 | if (dev->class != new_class) { |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 3680 | ata_dev_info(dev, "class mismatch %d != %d\n", |
| 3681 | dev->class, new_class); |
Tejun Heo | 623a312 | 2006-03-05 17:55:58 +0900 | [diff] [blame] | 3682 | return 0; |
| 3683 | } |
| 3684 | |
Tejun Heo | a0cf733 | 2007-01-02 20:18:49 +0900 | [diff] [blame] | 3685 | ata_id_c_string(old_id, model[0], ATA_ID_PROD, sizeof(model[0])); |
| 3686 | ata_id_c_string(new_id, model[1], ATA_ID_PROD, sizeof(model[1])); |
| 3687 | ata_id_c_string(old_id, serial[0], ATA_ID_SERNO, sizeof(serial[0])); |
| 3688 | ata_id_c_string(new_id, serial[1], ATA_ID_SERNO, sizeof(serial[1])); |
Tejun Heo | 623a312 | 2006-03-05 17:55:58 +0900 | [diff] [blame] | 3689 | |
| 3690 | if (strcmp(model[0], model[1])) { |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 3691 | ata_dev_info(dev, "model number mismatch '%s' != '%s'\n", |
| 3692 | model[0], model[1]); |
Tejun Heo | 623a312 | 2006-03-05 17:55:58 +0900 | [diff] [blame] | 3693 | return 0; |
| 3694 | } |
| 3695 | |
| 3696 | if (strcmp(serial[0], serial[1])) { |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 3697 | ata_dev_info(dev, "serial number mismatch '%s' != '%s'\n", |
| 3698 | serial[0], serial[1]); |
Tejun Heo | 623a312 | 2006-03-05 17:55:58 +0900 | [diff] [blame] | 3699 | return 0; |
| 3700 | } |
| 3701 | |
Tejun Heo | 623a312 | 2006-03-05 17:55:58 +0900 | [diff] [blame] | 3702 | return 1; |
| 3703 | } |
| 3704 | |
| 3705 | /** |
Tejun Heo | fe30911 | 2007-05-15 03:28:15 +0900 | [diff] [blame] | 3706 | * ata_dev_reread_id - Re-read IDENTIFY data |
Henrik Kretzschmar | 3fae450 | 2007-06-19 10:10:50 +0200 | [diff] [blame] | 3707 | * @dev: target ATA device |
Tejun Heo | bff0464 | 2006-11-10 18:08:10 +0900 | [diff] [blame] | 3708 | * @readid_flags: read ID flags |
Tejun Heo | 623a312 | 2006-03-05 17:55:58 +0900 | [diff] [blame] | 3709 | * |
| 3710 | * Re-read IDENTIFY page and make sure @dev is still attached to |
| 3711 | * the port. |
| 3712 | * |
| 3713 | * LOCKING: |
| 3714 | * Kernel thread context (may sleep) |
| 3715 | * |
| 3716 | * RETURNS: |
| 3717 | * 0 on success, negative errno otherwise |
| 3718 | */ |
Tejun Heo | fe30911 | 2007-05-15 03:28:15 +0900 | [diff] [blame] | 3719 | int ata_dev_reread_id(struct ata_device *dev, unsigned int readid_flags) |
Tejun Heo | 623a312 | 2006-03-05 17:55:58 +0900 | [diff] [blame] | 3720 | { |
Tejun Heo | 5eb45c0 | 2006-04-02 18:51:52 +0900 | [diff] [blame] | 3721 | unsigned int class = dev->class; |
Tejun Heo | 9af5c9c | 2007-08-06 18:36:22 +0900 | [diff] [blame] | 3722 | u16 *id = (void *)dev->link->ap->sector_buf; |
Tejun Heo | 623a312 | 2006-03-05 17:55:58 +0900 | [diff] [blame] | 3723 | int rc; |
| 3724 | |
Tejun Heo | fe635c7 | 2006-05-15 20:57:35 +0900 | [diff] [blame] | 3725 | /* read ID data */ |
Tejun Heo | bff0464 | 2006-11-10 18:08:10 +0900 | [diff] [blame] | 3726 | rc = ata_dev_read_id(dev, &class, readid_flags, id); |
Tejun Heo | 623a312 | 2006-03-05 17:55:58 +0900 | [diff] [blame] | 3727 | if (rc) |
Tejun Heo | fe30911 | 2007-05-15 03:28:15 +0900 | [diff] [blame] | 3728 | return rc; |
Tejun Heo | 623a312 | 2006-03-05 17:55:58 +0900 | [diff] [blame] | 3729 | |
| 3730 | /* is the device still there? */ |
Tejun Heo | fe30911 | 2007-05-15 03:28:15 +0900 | [diff] [blame] | 3731 | if (!ata_dev_same_device(dev, class, id)) |
| 3732 | return -ENODEV; |
Tejun Heo | 623a312 | 2006-03-05 17:55:58 +0900 | [diff] [blame] | 3733 | |
Tejun Heo | fe635c7 | 2006-05-15 20:57:35 +0900 | [diff] [blame] | 3734 | memcpy(dev->id, id, sizeof(id[0]) * ATA_ID_WORDS); |
Tejun Heo | fe30911 | 2007-05-15 03:28:15 +0900 | [diff] [blame] | 3735 | return 0; |
| 3736 | } |
| 3737 | |
| 3738 | /** |
| 3739 | * ata_dev_revalidate - Revalidate ATA device |
| 3740 | * @dev: device to revalidate |
Tejun Heo | 422c9da | 2007-09-23 13:14:12 +0900 | [diff] [blame] | 3741 | * @new_class: new class code |
Tejun Heo | fe30911 | 2007-05-15 03:28:15 +0900 | [diff] [blame] | 3742 | * @readid_flags: read ID flags |
| 3743 | * |
| 3744 | * Re-read IDENTIFY page, make sure @dev is still attached to the |
| 3745 | * port and reconfigure it according to the new IDENTIFY page. |
| 3746 | * |
| 3747 | * LOCKING: |
| 3748 | * Kernel thread context (may sleep) |
| 3749 | * |
| 3750 | * RETURNS: |
| 3751 | * 0 on success, negative errno otherwise |
| 3752 | */ |
Tejun Heo | 422c9da | 2007-09-23 13:14:12 +0900 | [diff] [blame] | 3753 | int ata_dev_revalidate(struct ata_device *dev, unsigned int new_class, |
| 3754 | unsigned int readid_flags) |
Tejun Heo | fe30911 | 2007-05-15 03:28:15 +0900 | [diff] [blame] | 3755 | { |
Tejun Heo | 6ddcd3b | 2007-05-15 03:28:15 +0900 | [diff] [blame] | 3756 | u64 n_sectors = dev->n_sectors; |
Tejun Heo | 5920dad | 2009-07-15 17:11:41 +0900 | [diff] [blame] | 3757 | u64 n_native_sectors = dev->n_native_sectors; |
Tejun Heo | fe30911 | 2007-05-15 03:28:15 +0900 | [diff] [blame] | 3758 | int rc; |
| 3759 | |
| 3760 | if (!ata_dev_enabled(dev)) |
| 3761 | return -ENODEV; |
| 3762 | |
Tejun Heo | 422c9da | 2007-09-23 13:14:12 +0900 | [diff] [blame] | 3763 | /* fail early if !ATA && !ATAPI to avoid issuing [P]IDENTIFY to PMP */ |
| 3764 | if (ata_class_enabled(new_class) && |
Borislav Petkov | f0d0613 | 2009-04-26 16:33:34 +0200 | [diff] [blame] | 3765 | new_class != ATA_DEV_ATA && |
| 3766 | new_class != ATA_DEV_ATAPI && |
Hannes Reinecke | 9162c65 | 2014-11-05 13:08:21 +0100 | [diff] [blame] | 3767 | new_class != ATA_DEV_ZAC && |
Borislav Petkov | f0d0613 | 2009-04-26 16:33:34 +0200 | [diff] [blame] | 3768 | new_class != ATA_DEV_SEMB) { |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 3769 | ata_dev_info(dev, "class mismatch %u != %u\n", |
| 3770 | dev->class, new_class); |
Tejun Heo | 422c9da | 2007-09-23 13:14:12 +0900 | [diff] [blame] | 3771 | rc = -ENODEV; |
| 3772 | goto fail; |
| 3773 | } |
| 3774 | |
Tejun Heo | fe30911 | 2007-05-15 03:28:15 +0900 | [diff] [blame] | 3775 | /* re-read ID */ |
| 3776 | rc = ata_dev_reread_id(dev, readid_flags); |
| 3777 | if (rc) |
| 3778 | goto fail; |
Tejun Heo | 623a312 | 2006-03-05 17:55:58 +0900 | [diff] [blame] | 3779 | |
| 3780 | /* configure device according to the new ID */ |
Tejun Heo | efdaedc | 2006-11-01 18:38:52 +0900 | [diff] [blame] | 3781 | rc = ata_dev_configure(dev); |
Tejun Heo | 6ddcd3b | 2007-05-15 03:28:15 +0900 | [diff] [blame] | 3782 | if (rc) |
| 3783 | goto fail; |
| 3784 | |
| 3785 | /* verify n_sectors hasn't changed */ |
Tejun Heo | 445d211 | 2010-04-05 10:33:13 +0900 | [diff] [blame] | 3786 | if (dev->class != ATA_DEV_ATA || !n_sectors || |
| 3787 | dev->n_sectors == n_sectors) |
| 3788 | return 0; |
| 3789 | |
| 3790 | /* n_sectors has changed */ |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 3791 | ata_dev_warn(dev, "n_sectors mismatch %llu != %llu\n", |
| 3792 | (unsigned long long)n_sectors, |
| 3793 | (unsigned long long)dev->n_sectors); |
Tejun Heo | 445d211 | 2010-04-05 10:33:13 +0900 | [diff] [blame] | 3794 | |
| 3795 | /* |
| 3796 | * Something could have caused HPA to be unlocked |
| 3797 | * involuntarily. If n_native_sectors hasn't changed and the |
| 3798 | * new size matches it, keep the device. |
| 3799 | */ |
| 3800 | if (dev->n_native_sectors == n_native_sectors && |
| 3801 | dev->n_sectors > n_sectors && dev->n_sectors == n_native_sectors) { |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 3802 | ata_dev_warn(dev, |
| 3803 | "new n_sectors matches native, probably " |
| 3804 | "late HPA unlock, n_sectors updated\n"); |
Tejun Heo | 68939ce | 2010-05-15 20:09:33 +0200 | [diff] [blame] | 3805 | /* use the larger n_sectors */ |
Tejun Heo | 445d211 | 2010-04-05 10:33:13 +0900 | [diff] [blame] | 3806 | return 0; |
Tejun Heo | 6ddcd3b | 2007-05-15 03:28:15 +0900 | [diff] [blame] | 3807 | } |
| 3808 | |
Tejun Heo | 445d211 | 2010-04-05 10:33:13 +0900 | [diff] [blame] | 3809 | /* |
| 3810 | * Some BIOSes boot w/o HPA but resume w/ HPA locked. Try |
| 3811 | * unlocking HPA in those cases. |
| 3812 | * |
| 3813 | * https://bugzilla.kernel.org/show_bug.cgi?id=15396 |
| 3814 | */ |
| 3815 | if (dev->n_native_sectors == n_native_sectors && |
| 3816 | dev->n_sectors < n_sectors && n_sectors == n_native_sectors && |
| 3817 | !(dev->horkage & ATA_HORKAGE_BROKEN_HPA)) { |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 3818 | ata_dev_warn(dev, |
| 3819 | "old n_sectors matches native, probably " |
| 3820 | "late HPA lock, will try to unlock HPA\n"); |
Tejun Heo | 445d211 | 2010-04-05 10:33:13 +0900 | [diff] [blame] | 3821 | /* try unlocking HPA */ |
| 3822 | dev->flags |= ATA_DFLAG_UNLOCK_HPA; |
| 3823 | rc = -EIO; |
| 3824 | } else |
| 3825 | rc = -ENODEV; |
Tejun Heo | 623a312 | 2006-03-05 17:55:58 +0900 | [diff] [blame] | 3826 | |
Tejun Heo | 445d211 | 2010-04-05 10:33:13 +0900 | [diff] [blame] | 3827 | /* restore original n_[native_]sectors and fail */ |
| 3828 | dev->n_native_sectors = n_native_sectors; |
| 3829 | dev->n_sectors = n_sectors; |
Tejun Heo | 623a312 | 2006-03-05 17:55:58 +0900 | [diff] [blame] | 3830 | fail: |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 3831 | ata_dev_err(dev, "revalidation failed (errno=%d)\n", rc); |
Tejun Heo | 623a312 | 2006-03-05 17:55:58 +0900 | [diff] [blame] | 3832 | return rc; |
| 3833 | } |
| 3834 | |
Alan Cox | 6919a0a | 2006-10-27 19:08:46 -0700 | [diff] [blame] | 3835 | struct ata_blacklist_entry { |
| 3836 | const char *model_num; |
| 3837 | const char *model_rev; |
| 3838 | unsigned long horkage; |
| 3839 | }; |
| 3840 | |
| 3841 | static const struct ata_blacklist_entry ata_device_blacklist [] = { |
| 3842 | /* Devices with DMA related problems under Linux */ |
| 3843 | { "WDC AC11000H", NULL, ATA_HORKAGE_NODMA }, |
| 3844 | { "WDC AC22100H", NULL, ATA_HORKAGE_NODMA }, |
| 3845 | { "WDC AC32500H", NULL, ATA_HORKAGE_NODMA }, |
| 3846 | { "WDC AC33100H", NULL, ATA_HORKAGE_NODMA }, |
| 3847 | { "WDC AC31600H", NULL, ATA_HORKAGE_NODMA }, |
| 3848 | { "WDC AC32100H", "24.09P07", ATA_HORKAGE_NODMA }, |
| 3849 | { "WDC AC23200L", "21.10N21", ATA_HORKAGE_NODMA }, |
| 3850 | { "Compaq CRD-8241B", NULL, ATA_HORKAGE_NODMA }, |
| 3851 | { "CRD-8400B", NULL, ATA_HORKAGE_NODMA }, |
Mark Lord | 7da4c93 | 2010-07-01 18:18:12 -0400 | [diff] [blame] | 3852 | { "CRD-848[02]B", NULL, ATA_HORKAGE_NODMA }, |
Alan Cox | 6919a0a | 2006-10-27 19:08:46 -0700 | [diff] [blame] | 3853 | { "CRD-84", NULL, ATA_HORKAGE_NODMA }, |
| 3854 | { "SanDisk SDP3B", NULL, ATA_HORKAGE_NODMA }, |
| 3855 | { "SanDisk SDP3B-64", NULL, ATA_HORKAGE_NODMA }, |
| 3856 | { "SANYO CD-ROM CRD", NULL, ATA_HORKAGE_NODMA }, |
| 3857 | { "HITACHI CDR-8", NULL, ATA_HORKAGE_NODMA }, |
Mark Lord | 7da4c93 | 2010-07-01 18:18:12 -0400 | [diff] [blame] | 3858 | { "HITACHI CDR-8[34]35",NULL, ATA_HORKAGE_NODMA }, |
Alan Cox | 6919a0a | 2006-10-27 19:08:46 -0700 | [diff] [blame] | 3859 | { "Toshiba CD-ROM XM-6202B", NULL, ATA_HORKAGE_NODMA }, |
| 3860 | { "TOSHIBA CD-ROM XM-1702BC", NULL, ATA_HORKAGE_NODMA }, |
| 3861 | { "CD-532E-A", NULL, ATA_HORKAGE_NODMA }, |
| 3862 | { "E-IDE CD-ROM CR-840",NULL, ATA_HORKAGE_NODMA }, |
| 3863 | { "CD-ROM Drive/F5A", NULL, ATA_HORKAGE_NODMA }, |
| 3864 | { "WPI CDD-820", NULL, ATA_HORKAGE_NODMA }, |
| 3865 | { "SAMSUNG CD-ROM SC-148C", NULL, ATA_HORKAGE_NODMA }, |
| 3866 | { "SAMSUNG CD-ROM SC", NULL, ATA_HORKAGE_NODMA }, |
Alan Cox | 6919a0a | 2006-10-27 19:08:46 -0700 | [diff] [blame] | 3867 | { "ATAPI CD-ROM DRIVE 40X MAXIMUM",NULL,ATA_HORKAGE_NODMA }, |
| 3868 | { "_NEC DV5800A", NULL, ATA_HORKAGE_NODMA }, |
Jeff Garzik | 2dcb407 | 2007-10-19 06:42:56 -0400 | [diff] [blame] | 3869 | { "SAMSUNG CD-ROM SN-124", "N001", ATA_HORKAGE_NODMA }, |
Dave Jones | 39f1988 | 2007-05-21 14:31:03 -0400 | [diff] [blame] | 3870 | { "Seagate STT20000A", NULL, ATA_HORKAGE_NODMA }, |
Prarit Bhargava | d17d794 | 2012-08-23 15:11:52 -0400 | [diff] [blame] | 3871 | { " 2GB ATA Flash Disk", "ADMA428M", ATA_HORKAGE_NODMA }, |
Shikha Jain | b00622f | 2016-01-11 14:55:45 -0800 | [diff] [blame] | 3872 | { "VRFDFC22048UCHC-TE*", NULL, ATA_HORKAGE_NODMA }, |
Tejun Heo | 3af9a77 | 2007-09-23 13:19:54 +0900 | [diff] [blame] | 3873 | /* Odd clown on sil3726/4726 PMPs */ |
Tejun Heo | 50af2fa | 2008-05-19 01:15:14 +0900 | [diff] [blame] | 3874 | { "Config Disk", NULL, ATA_HORKAGE_DISABLE }, |
Hannes Reinecke | a66307d | 2021-12-08 07:58:53 +0100 | [diff] [blame] | 3875 | /* Similar story with ASMedia 1092 */ |
| 3876 | { "ASMT109x- Config", NULL, ATA_HORKAGE_DISABLE }, |
Alan Cox | 6919a0a | 2006-10-27 19:08:46 -0700 | [diff] [blame] | 3877 | |
Albert Lee | 18d6e9d | 2007-04-02 11:34:15 +0800 | [diff] [blame] | 3878 | /* Weird ATAPI devices */ |
Tejun Heo | 40a1d53 | 2007-06-27 02:49:38 +0900 | [diff] [blame] | 3879 | { "TORiSAN DVD-ROM DRD-N216", NULL, ATA_HORKAGE_MAX_SEC_128 }, |
Tejun Heo | 6a87e42 | 2008-11-03 19:01:09 +0900 | [diff] [blame] | 3880 | { "QUANTUM DAT DAT72-000", NULL, ATA_HORKAGE_ATAPI_MOD16_DMA }, |
Shan Hai | a32450e | 2013-03-18 10:30:44 +0800 | [diff] [blame] | 3881 | { "Slimtype DVD A DS8A8SH", NULL, ATA_HORKAGE_MAX_SEC_LBA48 }, |
Shan Hai | 0523f03 | 2013-10-28 16:08:01 +0800 | [diff] [blame] | 3882 | { "Slimtype DVD A DS8A9SH", NULL, ATA_HORKAGE_MAX_SEC_LBA48 }, |
Albert Lee | 18d6e9d | 2007-04-02 11:34:15 +0800 | [diff] [blame] | 3883 | |
David Milburn | af34d63 | 2015-07-13 11:48:23 -0500 | [diff] [blame] | 3884 | /* |
| 3885 | * Causes silent data corruption with higher max sects. |
| 3886 | * http://lkml.kernel.org/g/x49wpy40ysk.fsf@segfault.boston.devel.redhat.com |
| 3887 | */ |
| 3888 | { "ST380013AS", "3.20", ATA_HORKAGE_MAX_SEC_1024 }, |
| 3889 | |
Tejun Heo | 1488a1e | 2016-07-18 18:40:00 -0400 | [diff] [blame] | 3890 | /* |
Tejun Heo | e0edc8c | 2017-01-06 11:48:50 -0500 | [diff] [blame] | 3891 | * These devices time out with higher max sects. |
Tejun Heo | 1488a1e | 2016-07-18 18:40:00 -0400 | [diff] [blame] | 3892 | * https://bugzilla.kernel.org/show_bug.cgi?id=121671 |
| 3893 | */ |
Tejun Heo | e0edc8c | 2017-01-06 11:48:50 -0500 | [diff] [blame] | 3894 | { "LITEON CX1-JB*-HP", NULL, ATA_HORKAGE_MAX_SEC_1024 }, |
Xinyu Lin | db5ff90 | 2017-12-17 20:13:39 +0800 | [diff] [blame] | 3895 | { "LITEON EP1-*", NULL, ATA_HORKAGE_MAX_SEC_1024 }, |
Tejun Heo | 1488a1e | 2016-07-18 18:40:00 -0400 | [diff] [blame] | 3896 | |
Alan Cox | 6919a0a | 2006-10-27 19:08:46 -0700 | [diff] [blame] | 3897 | /* Devices we expect to fail diagnostics */ |
| 3898 | |
| 3899 | /* Devices where NCQ should be avoided */ |
| 3900 | /* NCQ is slow */ |
Jeff Garzik | 2dcb407 | 2007-10-19 06:42:56 -0400 | [diff] [blame] | 3901 | { "WDC WD740ADFD-00", NULL, ATA_HORKAGE_NONCQ }, |
Tejun Heo | 459ad68 | 2007-12-07 12:46:23 +0900 | [diff] [blame] | 3902 | { "WDC WD740ADFD-00NLR1", NULL, ATA_HORKAGE_NONCQ, }, |
Tejun Heo | 09125ea | 2007-02-28 15:21:23 +0900 | [diff] [blame] | 3903 | /* http://thread.gmane.org/gmane.linux.ide/14907 */ |
| 3904 | { "FUJITSU MHT2060BH", NULL, ATA_HORKAGE_NONCQ }, |
Paul Rolland | 7acfaf3 | 2007-03-26 21:43:44 -0800 | [diff] [blame] | 3905 | /* NCQ is broken */ |
Jeff Garzik | 539cc7c | 2007-09-20 16:31:47 -0400 | [diff] [blame] | 3906 | { "Maxtor *", "BANC*", ATA_HORKAGE_NONCQ }, |
Alan Cox | 0e3dbc0 | 2007-09-20 15:22:47 +0100 | [diff] [blame] | 3907 | { "Maxtor 7V300F0", "VA111630", ATA_HORKAGE_NONCQ }, |
Paolo Ornati | da6f0ec | 2007-10-04 11:06:56 +0900 | [diff] [blame] | 3908 | { "ST380817AS", "3.42", ATA_HORKAGE_NONCQ }, |
Tejun Heo | e41bd3e | 2007-12-09 19:45:39 +0900 | [diff] [blame] | 3909 | { "ST3160023AS", "3.42", ATA_HORKAGE_NONCQ }, |
Lubomir Bulej | 5ccfca9 | 2008-12-22 11:35:22 +0100 | [diff] [blame] | 3910 | { "OCZ CORE_SSD", "02.10104", ATA_HORKAGE_NONCQ }, |
Jeff Garzik | 539cc7c | 2007-09-20 16:31:47 -0400 | [diff] [blame] | 3911 | |
Tejun Heo | ac70a96 | 2008-11-27 13:36:48 +0900 | [diff] [blame] | 3912 | /* Seagate NCQ + FLUSH CACHE firmware bug */ |
Mark Lord | 4d1f908 | 2010-07-05 18:53:37 -0400 | [diff] [blame] | 3913 | { "ST31500341AS", "SD1[5-9]", ATA_HORKAGE_NONCQ | |
Tejun Heo | ac70a96 | 2008-11-27 13:36:48 +0900 | [diff] [blame] | 3914 | ATA_HORKAGE_FIRMWARE_WARN }, |
Tejun Heo | d10d491 | 2008-12-11 13:42:42 +0900 | [diff] [blame] | 3915 | |
Mark Lord | 4d1f908 | 2010-07-05 18:53:37 -0400 | [diff] [blame] | 3916 | { "ST31000333AS", "SD1[5-9]", ATA_HORKAGE_NONCQ | |
Tejun Heo | d10d491 | 2008-12-11 13:42:42 +0900 | [diff] [blame] | 3917 | ATA_HORKAGE_FIRMWARE_WARN }, |
| 3918 | |
Mark Lord | 4d1f908 | 2010-07-05 18:53:37 -0400 | [diff] [blame] | 3919 | { "ST3640[36]23AS", "SD1[5-9]", ATA_HORKAGE_NONCQ | |
Tejun Heo | d10d491 | 2008-12-11 13:42:42 +0900 | [diff] [blame] | 3920 | ATA_HORKAGE_FIRMWARE_WARN }, |
| 3921 | |
Mark Lord | 4d1f908 | 2010-07-05 18:53:37 -0400 | [diff] [blame] | 3922 | { "ST3320[68]13AS", "SD1[5-9]", ATA_HORKAGE_NONCQ | |
Tejun Heo | ac70a96 | 2008-11-27 13:36:48 +0900 | [diff] [blame] | 3923 | ATA_HORKAGE_FIRMWARE_WARN }, |
| 3924 | |
Hans de Goede | 31f6264 | 2019-06-11 16:32:59 +0200 | [diff] [blame] | 3925 | /* drives which fail FPDMA_AA activation (some may freeze afterwards) |
| 3926 | the ST disks also have LPM issues */ |
Hans de Goede | 8756a25 | 2019-07-01 18:58:13 +0200 | [diff] [blame] | 3927 | { "ST1000LM024 HN-M101MBB", NULL, ATA_HORKAGE_BROKEN_FPDMA_AA | |
Hans de Goede | 31f6264 | 2019-06-11 16:32:59 +0200 | [diff] [blame] | 3928 | ATA_HORKAGE_NOLPM, }, |
Aleksei Mamlin | 08c85d2 | 2015-07-01 13:48:30 +0300 | [diff] [blame] | 3929 | { "VB0250EAVER", "HPG7", ATA_HORKAGE_BROKEN_FPDMA_AA }, |
Michele Baldessari | 8780994 | 2013-11-25 19:00:14 +0000 | [diff] [blame] | 3930 | |
Robert Hancock | 36e337d | 2007-04-02 22:05:29 -0600 | [diff] [blame] | 3931 | /* Blacklist entries taken from Silicon Image 3124/3132 |
| 3932 | Windows driver .inf file - also several Linux problem reports */ |
| 3933 | { "HTS541060G9SA00", "MB3OC60D", ATA_HORKAGE_NONCQ, }, |
| 3934 | { "HTS541080G9SA00", "MB4OC60D", ATA_HORKAGE_NONCQ, }, |
| 3935 | { "HTS541010G9SA00", "MBZOC60D", ATA_HORKAGE_NONCQ, }, |
Alan Cox | 6919a0a | 2006-10-27 19:08:46 -0700 | [diff] [blame] | 3936 | |
Tejun Heo | 68b0ddb | 2010-04-05 10:51:26 +0900 | [diff] [blame] | 3937 | /* https://bugzilla.kernel.org/show_bug.cgi?id=15573 */ |
| 3938 | { "C300-CTFDDAC128MAG", "0001", ATA_HORKAGE_NONCQ, }, |
| 3939 | |
Tejun Heo | 3b54556 | 2020-09-02 12:32:45 -0400 | [diff] [blame] | 3940 | /* Sandisk SD7/8/9s lock up hard on large trims */ |
| 3941 | { "SanDisk SD[789]*", NULL, ATA_HORKAGE_MAX_TRIM_128M, }, |
Tejun Heo | 322579d | 2018-05-08 14:21:56 -0700 | [diff] [blame] | 3942 | |
Tejun Heo | 16c55b0 | 2007-08-29 11:58:33 +0900 | [diff] [blame] | 3943 | /* devices which puke on READ_NATIVE_MAX */ |
| 3944 | { "HDS724040KLSA80", "KFAOA20N", ATA_HORKAGE_BROKEN_HPA, }, |
| 3945 | { "WDC WD3200JD-00KLB0", "WD-WCAMR1130137", ATA_HORKAGE_BROKEN_HPA }, |
| 3946 | { "WDC WD2500JD-00HBB0", "WD-WMAL71490727", ATA_HORKAGE_BROKEN_HPA }, |
| 3947 | { "MAXTOR 6L080L4", "A93.0500", ATA_HORKAGE_BROKEN_HPA }, |
Alan Cox | 6919a0a | 2006-10-27 19:08:46 -0700 | [diff] [blame] | 3948 | |
Tejun Heo | 7831387b | 2009-08-07 01:59:15 +0900 | [diff] [blame] | 3949 | /* this one allows HPA unlocking but fails IOs on the area */ |
| 3950 | { "OCZ-VERTEX", "1.30", ATA_HORKAGE_BROKEN_HPA }, |
| 3951 | |
Alan Cox | 93328e11 | 2007-09-29 04:06:48 -0400 | [diff] [blame] | 3952 | /* Devices which report 1 sector over size HPA */ |
| 3953 | { "ST340823A", NULL, ATA_HORKAGE_HPA_SIZE, }, |
| 3954 | { "ST320413A", NULL, ATA_HORKAGE_HPA_SIZE, }, |
Mikko Rapeli | b152fcd | 2008-02-19 01:41:25 +0100 | [diff] [blame] | 3955 | { "ST310211A", NULL, ATA_HORKAGE_HPA_SIZE, }, |
Alan Cox | 93328e11 | 2007-09-29 04:06:48 -0400 | [diff] [blame] | 3956 | |
Alan Cox | 6bbfd53 | 2007-11-05 22:58:58 +0000 | [diff] [blame] | 3957 | /* Devices which get the IVB wrong */ |
| 3958 | { "QUANTUM FIREBALLlct10 05", "A03.0900", ATA_HORKAGE_IVB, }, |
Alan Cox | a79067e | 2008-04-29 14:08:36 +0100 | [diff] [blame] | 3959 | /* Maybe we should just blacklist TSSTcorp... */ |
Mark Lord | 7da4c93 | 2010-07-01 18:18:12 -0400 | [diff] [blame] | 3960 | { "TSSTcorp CDDVDW SH-S202[HJN]", "SB0[01]", ATA_HORKAGE_IVB, }, |
Alan Cox | 6bbfd53 | 2007-11-05 22:58:58 +0000 | [diff] [blame] | 3961 | |
Jens Axboe | 9ce8e30 | 2008-08-27 15:23:18 +0200 | [diff] [blame] | 3962 | /* Devices that do not need bridging limits applied */ |
| 3963 | { "MTRON MSP-SATA*", NULL, ATA_HORKAGE_BRIDGE_OK, }, |
Jeff Garzik | 04d0f1b8 | 2012-08-17 13:36:59 -0400 | [diff] [blame] | 3964 | { "BUFFALO HD-QSU2/R5", NULL, ATA_HORKAGE_BRIDGE_OK, }, |
Jens Axboe | 9ce8e30 | 2008-08-27 15:23:18 +0200 | [diff] [blame] | 3965 | |
Tejun Heo | 9062712 | 2009-01-29 20:31:36 +0900 | [diff] [blame] | 3966 | /* Devices which aren't very happy with higher link speeds */ |
| 3967 | { "WD My Book", NULL, ATA_HORKAGE_1_5_GBPS, }, |
Daniel J Blueman | c531077 | 2012-07-23 12:22:37 +0800 | [diff] [blame] | 3968 | { "Seagate FreeAgent GoFlex", NULL, ATA_HORKAGE_1_5_GBPS, }, |
Tejun Heo | 9062712 | 2009-01-29 20:31:36 +0900 | [diff] [blame] | 3969 | |
Tejun Heo | d0cb43b | 2009-07-09 09:27:50 +0900 | [diff] [blame] | 3970 | /* |
| 3971 | * Devices which choke on SETXFER. Applies only if both the |
| 3972 | * device and controller are SATA. |
| 3973 | */ |
Tejun Heo | cd69187 | 2011-06-22 12:13:10 +0200 | [diff] [blame] | 3974 | { "PIONEER DVD-RW DVRTD08", NULL, ATA_HORKAGE_NOSETXFER }, |
Vladimir LAVALLADE | 3a25179 | 2012-01-08 13:50:13 +0100 | [diff] [blame] | 3975 | { "PIONEER DVD-RW DVRTD08A", NULL, ATA_HORKAGE_NOSETXFER }, |
| 3976 | { "PIONEER DVD-RW DVR-215", NULL, ATA_HORKAGE_NOSETXFER }, |
Tejun Heo | cd69187 | 2011-06-22 12:13:10 +0200 | [diff] [blame] | 3977 | { "PIONEER DVD-RW DVR-212D", NULL, ATA_HORKAGE_NOSETXFER }, |
| 3978 | { "PIONEER DVD-RW DVR-216D", NULL, ATA_HORKAGE_NOSETXFER }, |
Tejun Heo | d0cb43b | 2009-07-09 09:27:50 +0900 | [diff] [blame] | 3979 | |
Kai-Heng Feng | b17e5729 | 2018-02-18 22:17:09 +0800 | [diff] [blame] | 3980 | /* Crucial BX100 SSD 500GB has broken LPM support */ |
Hans de Goede | 3bf7b5d | 2018-03-19 16:33:59 +0100 | [diff] [blame] | 3981 | { "CT500BX100SSD1", NULL, ATA_HORKAGE_NOLPM }, |
Kai-Heng Feng | b17e5729 | 2018-02-18 22:17:09 +0800 | [diff] [blame] | 3982 | |
Hans de Goede | d418ff5 | 2018-03-19 16:34:00 +0100 | [diff] [blame] | 3983 | /* 512GB MX100 with MU01 firmware has both queued TRIM and LPM issues */ |
| 3984 | { "Crucial_CT512MX100*", "MU01", ATA_HORKAGE_NO_NCQ_TRIM | |
Hans de Goede | 9c7be59f | 2018-02-16 10:48:20 +0100 | [diff] [blame] | 3985 | ATA_HORKAGE_ZERO_AFTER_TRIM | |
| 3986 | ATA_HORKAGE_NOLPM, }, |
Hans de Goede | d418ff5 | 2018-03-19 16:34:00 +0100 | [diff] [blame] | 3987 | /* 512GB MX100 with newer firmware has only LPM issues */ |
| 3988 | { "Crucial_CT512MX100*", NULL, ATA_HORKAGE_ZERO_AFTER_TRIM | |
| 3989 | ATA_HORKAGE_NOLPM, }, |
Hans de Goede | 9c7be59f | 2018-02-16 10:48:20 +0100 | [diff] [blame] | 3990 | |
Hans de Goede | 62ac3f7 | 2018-03-19 16:33:58 +0100 | [diff] [blame] | 3991 | /* 480GB+ M500 SSDs have both queued TRIM and LPM issues */ |
| 3992 | { "Crucial_CT480M500*", NULL, ATA_HORKAGE_NO_NCQ_TRIM | |
| 3993 | ATA_HORKAGE_ZERO_AFTER_TRIM | |
| 3994 | ATA_HORKAGE_NOLPM, }, |
| 3995 | { "Crucial_CT960M500*", NULL, ATA_HORKAGE_NO_NCQ_TRIM | |
| 3996 | ATA_HORKAGE_ZERO_AFTER_TRIM | |
| 3997 | ATA_HORKAGE_NOLPM, }, |
| 3998 | |
François Cami | 76936e9 | 2018-05-13 20:11:15 +0200 | [diff] [blame] | 3999 | /* These specific Samsung models/firmware-revs do not handle LPM well */ |
Hans de Goede | b5b4d3a | 2018-04-24 11:19:07 +0200 | [diff] [blame] | 4000 | { "SAMSUNG MZMPC128HBFU-000MV", "CXM14M1Q", ATA_HORKAGE_NOLPM, }, |
François Cami | 76936e9 | 2018-05-13 20:11:15 +0200 | [diff] [blame] | 4001 | { "SAMSUNG SSD PM830 mSATA *", "CXM13D1Q", ATA_HORKAGE_NOLPM, }, |
Diego Viola | 410b5c7 | 2018-11-12 17:22:52 -0200 | [diff] [blame] | 4002 | { "SAMSUNG MZ7TD256HAFV-000L9", NULL, ATA_HORKAGE_NOLPM, }, |
Hans de Goede | dd95749 | 2019-02-03 10:02:07 +0100 | [diff] [blame] | 4003 | { "SAMSUNG MZ7TE512HMHP-000L1", "EXT06L0Q", ATA_HORKAGE_NOLPM, }, |
Hans de Goede | b5b4d3a | 2018-04-24 11:19:07 +0200 | [diff] [blame] | 4004 | |
Marc Carino | f78dea0 | 2013-12-16 18:15:53 -0800 | [diff] [blame] | 4005 | /* devices that don't properly handle queued TRIM commands */ |
Sudip Mukherjee | 136d769 | 2018-05-19 22:29:36 +0100 | [diff] [blame] | 4006 | { "Micron_M500IT_*", "MU01", ATA_HORKAGE_NO_NCQ_TRIM | |
| 4007 | ATA_HORKAGE_ZERO_AFTER_TRIM, }, |
Martin K. Petersen | 243918b | 2015-06-18 14:50:18 -0400 | [diff] [blame] | 4008 | { "Micron_M500_*", NULL, ATA_HORKAGE_NO_NCQ_TRIM | |
Martin K. Petersen | e61f7d1 | 2015-01-08 10:34:27 -0500 | [diff] [blame] | 4009 | ATA_HORKAGE_ZERO_AFTER_TRIM, }, |
Martin K. Petersen | ff7f53f | 2015-03-27 15:17:20 -0400 | [diff] [blame] | 4010 | { "Crucial_CT*M500*", NULL, ATA_HORKAGE_NO_NCQ_TRIM | |
| 4011 | ATA_HORKAGE_ZERO_AFTER_TRIM, }, |
Martin K. Petersen | 9051bd3 | 2015-07-15 21:03:23 -0400 | [diff] [blame] | 4012 | { "Micron_M5[15]0_*", "MU01", ATA_HORKAGE_NO_NCQ_TRIM | |
Martin K. Petersen | ff7f53f | 2015-03-27 15:17:20 -0400 | [diff] [blame] | 4013 | ATA_HORKAGE_ZERO_AFTER_TRIM, }, |
| 4014 | { "Crucial_CT*M550*", "MU01", ATA_HORKAGE_NO_NCQ_TRIM | |
| 4015 | ATA_HORKAGE_ZERO_AFTER_TRIM, }, |
| 4016 | { "Crucial_CT*MX100*", "MU01", ATA_HORKAGE_NO_NCQ_TRIM | |
| 4017 | ATA_HORKAGE_ZERO_AFTER_TRIM, }, |
Ju Hyung Park | ca6bfcb | 2018-03-11 02:28:35 +0900 | [diff] [blame] | 4018 | { "Samsung SSD 840*", NULL, ATA_HORKAGE_NO_NCQ_TRIM | |
| 4019 | ATA_HORKAGE_ZERO_AFTER_TRIM, }, |
| 4020 | { "Samsung SSD 850*", NULL, ATA_HORKAGE_NO_NCQ_TRIM | |
Martin K. Petersen | 6fc4d97 | 2015-03-27 15:17:21 -0400 | [diff] [blame] | 4021 | ATA_HORKAGE_ZERO_AFTER_TRIM, }, |
Hans de Goede | 8a6430a | 2021-08-23 11:52:20 +0200 | [diff] [blame] | 4022 | { "Samsung SSD 860*", NULL, ATA_HORKAGE_NO_NCQ_TRIM | |
Kate Hsuan | 7a8526a | 2021-09-03 17:44:11 +0800 | [diff] [blame] | 4023 | ATA_HORKAGE_ZERO_AFTER_TRIM | |
| 4024 | ATA_HORKAGE_NO_NCQ_ON_ATI, }, |
Hans de Goede | 8a6430a | 2021-08-23 11:52:20 +0200 | [diff] [blame] | 4025 | { "Samsung SSD 870*", NULL, ATA_HORKAGE_NO_NCQ_TRIM | |
Kate Hsuan | 7a8526a | 2021-09-03 17:44:11 +0800 | [diff] [blame] | 4026 | ATA_HORKAGE_ZERO_AFTER_TRIM | |
| 4027 | ATA_HORKAGE_NO_NCQ_ON_ATI, }, |
Guillermo A. Amaral | 7a7184b | 2015-08-25 23:29:13 -0700 | [diff] [blame] | 4028 | { "FCCT*M500*", NULL, ATA_HORKAGE_NO_NCQ_TRIM | |
| 4029 | ATA_HORKAGE_ZERO_AFTER_TRIM, }, |
Martin K. Petersen | e61f7d1 | 2015-01-08 10:34:27 -0500 | [diff] [blame] | 4030 | |
Arne Fitzenreiter | cda57b1 | 2015-07-15 13:54:37 +0200 | [diff] [blame] | 4031 | /* devices that don't properly handle TRIM commands */ |
| 4032 | { "SuperSSpeed S238*", NULL, ATA_HORKAGE_NOTRIM, }, |
| 4033 | |
Martin K. Petersen | e61f7d1 | 2015-01-08 10:34:27 -0500 | [diff] [blame] | 4034 | /* |
| 4035 | * As defined, the DRAT (Deterministic Read After Trim) and RZAT |
| 4036 | * (Return Zero After Trim) flags in the ATA Command Set are |
| 4037 | * unreliable in the sense that they only define what happens if |
| 4038 | * the device successfully executed the DSM TRIM command. TRIM |
| 4039 | * is only advisory, however, and the device is free to silently |
| 4040 | * ignore all or parts of the request. |
| 4041 | * |
| 4042 | * Whitelist drives that are known to reliably return zeroes |
| 4043 | * after TRIM. |
| 4044 | */ |
| 4045 | |
| 4046 | /* |
| 4047 | * The intel 510 drive has buggy DRAT/RZAT. Explicitly exclude |
| 4048 | * that model before whitelisting all other intel SSDs. |
| 4049 | */ |
| 4050 | { "INTEL*SSDSC2MH*", NULL, 0, }, |
| 4051 | |
Martin K. Petersen | ff7f53f | 2015-03-27 15:17:20 -0400 | [diff] [blame] | 4052 | { "Micron*", NULL, ATA_HORKAGE_ZERO_AFTER_TRIM, }, |
| 4053 | { "Crucial*", NULL, ATA_HORKAGE_ZERO_AFTER_TRIM, }, |
Martin K. Petersen | e61f7d1 | 2015-01-08 10:34:27 -0500 | [diff] [blame] | 4054 | { "INTEL*SSD*", NULL, ATA_HORKAGE_ZERO_AFTER_TRIM, }, |
| 4055 | { "SSD*INTEL*", NULL, ATA_HORKAGE_ZERO_AFTER_TRIM, }, |
| 4056 | { "Samsung*SSD*", NULL, ATA_HORKAGE_ZERO_AFTER_TRIM, }, |
| 4057 | { "SAMSUNG*SSD*", NULL, ATA_HORKAGE_ZERO_AFTER_TRIM, }, |
Juha-Matti Tilli | fd6f32f | 2018-12-02 12:47:08 +0200 | [diff] [blame] | 4058 | { "SAMSUNG*MZ7KM*", NULL, ATA_HORKAGE_ZERO_AFTER_TRIM, }, |
Martin K. Petersen | e61f7d1 | 2015-01-08 10:34:27 -0500 | [diff] [blame] | 4059 | { "ST[1248][0248]0[FH]*", NULL, ATA_HORKAGE_ZERO_AFTER_TRIM, }, |
Marc Carino | f78dea0 | 2013-12-16 18:15:53 -0800 | [diff] [blame] | 4060 | |
Tejun Heo | ecd75ad | 2014-01-16 09:47:17 -0500 | [diff] [blame] | 4061 | /* |
| 4062 | * Some WD SATA-I drives spin up and down erratically when the link |
| 4063 | * is put into the slumber mode. We don't have full list of the |
| 4064 | * affected devices. Disable LPM if the device matches one of the |
| 4065 | * known prefixes and is SATA-1. As a side effect LPM partial is |
| 4066 | * lost too. |
| 4067 | * |
| 4068 | * https://bugzilla.kernel.org/show_bug.cgi?id=57211 |
| 4069 | */ |
| 4070 | { "WDC WD800JD-*", NULL, ATA_HORKAGE_WD_BROKEN_LPM }, |
| 4071 | { "WDC WD1200JD-*", NULL, ATA_HORKAGE_WD_BROKEN_LPM }, |
| 4072 | { "WDC WD1600JD-*", NULL, ATA_HORKAGE_WD_BROKEN_LPM }, |
| 4073 | { "WDC WD2000JD-*", NULL, ATA_HORKAGE_WD_BROKEN_LPM }, |
| 4074 | { "WDC WD2500JD-*", NULL, ATA_HORKAGE_WD_BROKEN_LPM }, |
| 4075 | { "WDC WD3000JD-*", NULL, ATA_HORKAGE_WD_BROKEN_LPM }, |
| 4076 | { "WDC WD3200JD-*", NULL, ATA_HORKAGE_WD_BROKEN_LPM }, |
| 4077 | |
Alan Cox | 6919a0a | 2006-10-27 19:08:46 -0700 | [diff] [blame] | 4078 | /* End Marker */ |
| 4079 | { } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4080 | }; |
Jeff Garzik | 2e9edbf | 2006-03-24 09:56:57 -0500 | [diff] [blame] | 4081 | |
Tejun Heo | 75683fe | 2007-07-05 13:31:27 +0900 | [diff] [blame] | 4082 | static unsigned long ata_dev_blacklisted(const struct ata_device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4083 | { |
Tejun Heo | 8bfa79f | 2007-01-02 20:19:40 +0900 | [diff] [blame] | 4084 | unsigned char model_num[ATA_ID_PROD_LEN + 1]; |
| 4085 | unsigned char model_rev[ATA_ID_FW_REV_LEN + 1]; |
Alan Cox | 6919a0a | 2006-10-27 19:08:46 -0700 | [diff] [blame] | 4086 | const struct ata_blacklist_entry *ad = ata_device_blacklist; |
Albert Lee | 3a77827 | 2006-06-22 13:00:25 +0800 | [diff] [blame] | 4087 | |
Tejun Heo | 8bfa79f | 2007-01-02 20:19:40 +0900 | [diff] [blame] | 4088 | ata_id_c_string(dev->id, model_num, ATA_ID_PROD, sizeof(model_num)); |
| 4089 | ata_id_c_string(dev->id, model_rev, ATA_ID_FW_REV, sizeof(model_rev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4090 | |
Alan Cox | 6919a0a | 2006-10-27 19:08:46 -0700 | [diff] [blame] | 4091 | while (ad->model_num) { |
George Spelvin | 1c40279 | 2014-10-07 07:26:38 -0400 | [diff] [blame] | 4092 | if (glob_match(ad->model_num, model_num)) { |
Alan Cox | 6919a0a | 2006-10-27 19:08:46 -0700 | [diff] [blame] | 4093 | if (ad->model_rev == NULL) |
| 4094 | return ad->horkage; |
George Spelvin | 1c40279 | 2014-10-07 07:26:38 -0400 | [diff] [blame] | 4095 | if (glob_match(ad->model_rev, model_rev)) |
Alan Cox | 6919a0a | 2006-10-27 19:08:46 -0700 | [diff] [blame] | 4096 | return ad->horkage; |
Alan Cox | f4b15fe | 2006-03-22 15:54:04 +0000 | [diff] [blame] | 4097 | } |
Alan Cox | 6919a0a | 2006-10-27 19:08:46 -0700 | [diff] [blame] | 4098 | ad++; |
Alan Cox | f4b15fe | 2006-03-22 15:54:04 +0000 | [diff] [blame] | 4099 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4100 | return 0; |
| 4101 | } |
| 4102 | |
Alan Cox | 6919a0a | 2006-10-27 19:08:46 -0700 | [diff] [blame] | 4103 | static int ata_dma_blacklisted(const struct ata_device *dev) |
| 4104 | { |
| 4105 | /* We don't support polling DMA. |
| 4106 | * DMA blacklist those ATAPI devices with CDB-intr (and use PIO) |
| 4107 | * if the LLDD handles only interrupts in the HSM_ST_LAST state. |
| 4108 | */ |
Tejun Heo | 9af5c9c | 2007-08-06 18:36:22 +0900 | [diff] [blame] | 4109 | if ((dev->link->ap->flags & ATA_FLAG_PIO_POLLING) && |
Alan Cox | 6919a0a | 2006-10-27 19:08:46 -0700 | [diff] [blame] | 4110 | (dev->flags & ATA_DFLAG_CDB_INTR)) |
| 4111 | return 1; |
Tejun Heo | 75683fe | 2007-07-05 13:31:27 +0900 | [diff] [blame] | 4112 | return (dev->horkage & ATA_HORKAGE_NODMA) ? 1 : 0; |
Alan Cox | 6919a0a | 2006-10-27 19:08:46 -0700 | [diff] [blame] | 4113 | } |
| 4114 | |
Tejun Heo | a6d5a51 | 2006-03-06 04:31:57 +0900 | [diff] [blame] | 4115 | /** |
Alan Cox | 6bbfd53 | 2007-11-05 22:58:58 +0000 | [diff] [blame] | 4116 | * ata_is_40wire - check drive side detection |
| 4117 | * @dev: device |
| 4118 | * |
| 4119 | * Perform drive side detection decoding, allowing for device vendors |
| 4120 | * who can't follow the documentation. |
| 4121 | */ |
| 4122 | |
| 4123 | static int ata_is_40wire(struct ata_device *dev) |
| 4124 | { |
| 4125 | if (dev->horkage & ATA_HORKAGE_IVB) |
| 4126 | return ata_drive_40wire_relaxed(dev->id); |
| 4127 | return ata_drive_40wire(dev->id); |
| 4128 | } |
| 4129 | |
| 4130 | /** |
Alan Cox | 15a5551 | 2008-03-28 14:33:46 -0700 | [diff] [blame] | 4131 | * cable_is_40wire - 40/80/SATA decider |
| 4132 | * @ap: port to consider |
| 4133 | * |
| 4134 | * This function encapsulates the policy for speed management |
| 4135 | * in one place. At the moment we don't cache the result but |
| 4136 | * there is a good case for setting ap->cbl to the result when |
| 4137 | * we are called with unknown cables (and figuring out if it |
| 4138 | * impacts hotplug at all). |
| 4139 | * |
| 4140 | * Return 1 if the cable appears to be 40 wire. |
| 4141 | */ |
| 4142 | |
| 4143 | static int cable_is_40wire(struct ata_port *ap) |
| 4144 | { |
| 4145 | struct ata_link *link; |
| 4146 | struct ata_device *dev; |
| 4147 | |
Tejun Heo | 4a9c7b3 | 2008-10-27 19:59:23 +0900 | [diff] [blame] | 4148 | /* If the controller thinks we are 40 wire, we are. */ |
Alan Cox | 15a5551 | 2008-03-28 14:33:46 -0700 | [diff] [blame] | 4149 | if (ap->cbl == ATA_CBL_PATA40) |
| 4150 | return 1; |
Tejun Heo | 4a9c7b3 | 2008-10-27 19:59:23 +0900 | [diff] [blame] | 4151 | |
| 4152 | /* If the controller thinks we are 80 wire, we are. */ |
Alan Cox | 15a5551 | 2008-03-28 14:33:46 -0700 | [diff] [blame] | 4153 | if (ap->cbl == ATA_CBL_PATA80 || ap->cbl == ATA_CBL_SATA) |
| 4154 | return 0; |
Tejun Heo | 4a9c7b3 | 2008-10-27 19:59:23 +0900 | [diff] [blame] | 4155 | |
| 4156 | /* If the system is known to be 40 wire short cable (eg |
| 4157 | * laptop), then we allow 80 wire modes even if the drive |
| 4158 | * isn't sure. |
| 4159 | */ |
Alan Cox | f792068 | 2008-04-08 16:37:50 +0100 | [diff] [blame] | 4160 | if (ap->cbl == ATA_CBL_PATA40_SHORT) |
| 4161 | return 0; |
Alan Cox | 15a5551 | 2008-03-28 14:33:46 -0700 | [diff] [blame] | 4162 | |
Tejun Heo | 4a9c7b3 | 2008-10-27 19:59:23 +0900 | [diff] [blame] | 4163 | /* If the controller doesn't know, we scan. |
| 4164 | * |
| 4165 | * Note: We look for all 40 wire detects at this point. Any |
| 4166 | * 80 wire detect is taken to be 80 wire cable because |
| 4167 | * - in many setups only the one drive (slave if present) will |
| 4168 | * give a valid detect |
| 4169 | * - if you have a non detect capable drive you don't want it |
| 4170 | * to colour the choice |
| 4171 | */ |
Tejun Heo | 1eca436 | 2008-11-03 20:03:17 +0900 | [diff] [blame] | 4172 | ata_for_each_link(link, ap, EDGE) { |
| 4173 | ata_for_each_dev(dev, link, ENABLED) { |
| 4174 | if (!ata_is_40wire(dev)) |
Alan Cox | 15a5551 | 2008-03-28 14:33:46 -0700 | [diff] [blame] | 4175 | return 0; |
| 4176 | } |
| 4177 | } |
| 4178 | return 1; |
| 4179 | } |
| 4180 | |
| 4181 | /** |
Tejun Heo | a6d5a51 | 2006-03-06 04:31:57 +0900 | [diff] [blame] | 4182 | * ata_dev_xfermask - Compute supported xfermask of the given device |
Tejun Heo | a6d5a51 | 2006-03-06 04:31:57 +0900 | [diff] [blame] | 4183 | * @dev: Device to compute xfermask for |
| 4184 | * |
Tejun Heo | acf356b | 2006-03-24 14:07:50 +0900 | [diff] [blame] | 4185 | * Compute supported xfermask of @dev and store it in |
| 4186 | * dev->*_mask. This function is responsible for applying all |
| 4187 | * known limits including host controller limits, device |
| 4188 | * blacklist, etc... |
Tejun Heo | a6d5a51 | 2006-03-06 04:31:57 +0900 | [diff] [blame] | 4189 | * |
| 4190 | * LOCKING: |
| 4191 | * None. |
Tejun Heo | a6d5a51 | 2006-03-06 04:31:57 +0900 | [diff] [blame] | 4192 | */ |
Tejun Heo | 3373efd | 2006-05-15 20:57:53 +0900 | [diff] [blame] | 4193 | static void ata_dev_xfermask(struct ata_device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4194 | { |
Tejun Heo | 9af5c9c | 2007-08-06 18:36:22 +0900 | [diff] [blame] | 4195 | struct ata_link *link = dev->link; |
| 4196 | struct ata_port *ap = link->ap; |
Jeff Garzik | cca3974 | 2006-08-24 03:19:22 -0400 | [diff] [blame] | 4197 | struct ata_host *host = ap->host; |
Tejun Heo | a6d5a51 | 2006-03-06 04:31:57 +0900 | [diff] [blame] | 4198 | unsigned long xfer_mask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4199 | |
Tejun Heo | 37deecb | 2006-08-10 16:59:07 +0900 | [diff] [blame] | 4200 | /* controller modes available */ |
Tejun Heo | 565083e | 2006-04-02 17:54:47 +0900 | [diff] [blame] | 4201 | xfer_mask = ata_pack_xfermask(ap->pio_mask, |
| 4202 | ap->mwdma_mask, ap->udma_mask); |
| 4203 | |
Robert Hancock | 8343f88 | 2007-03-06 02:37:51 -0800 | [diff] [blame] | 4204 | /* drive modes available */ |
Tejun Heo | 37deecb | 2006-08-10 16:59:07 +0900 | [diff] [blame] | 4205 | xfer_mask &= ata_pack_xfermask(dev->pio_mask, |
| 4206 | dev->mwdma_mask, dev->udma_mask); |
| 4207 | xfer_mask &= ata_id_xfermask(dev->id); |
Tejun Heo | 565083e | 2006-04-02 17:54:47 +0900 | [diff] [blame] | 4208 | |
Alan Cox | b352e57 | 2006-08-10 18:52:12 +0100 | [diff] [blame] | 4209 | /* |
| 4210 | * CFA Advanced TrueIDE timings are not allowed on a shared |
| 4211 | * cable |
| 4212 | */ |
| 4213 | if (ata_dev_pair(dev)) { |
| 4214 | /* No PIO5 or PIO6 */ |
| 4215 | xfer_mask &= ~(0x03 << (ATA_SHIFT_PIO + 5)); |
| 4216 | /* No MWDMA3 or MWDMA 4 */ |
| 4217 | xfer_mask &= ~(0x03 << (ATA_SHIFT_MWDMA + 3)); |
| 4218 | } |
| 4219 | |
Tejun Heo | 37deecb | 2006-08-10 16:59:07 +0900 | [diff] [blame] | 4220 | if (ata_dma_blacklisted(dev)) { |
| 4221 | xfer_mask &= ~(ATA_MASK_MWDMA | ATA_MASK_UDMA); |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 4222 | ata_dev_warn(dev, |
| 4223 | "device is on DMA blacklist, disabling DMA\n"); |
Tejun Heo | 37deecb | 2006-08-10 16:59:07 +0900 | [diff] [blame] | 4224 | } |
Tejun Heo | a6d5a51 | 2006-03-06 04:31:57 +0900 | [diff] [blame] | 4225 | |
Petr Vandrovec | 14d66ab | 2007-03-08 10:12:12 +0100 | [diff] [blame] | 4226 | if ((host->flags & ATA_HOST_SIMPLEX) && |
Jeff Garzik | 2dcb407 | 2007-10-19 06:42:56 -0400 | [diff] [blame] | 4227 | host->simplex_claimed && host->simplex_claimed != ap) { |
Tejun Heo | 37deecb | 2006-08-10 16:59:07 +0900 | [diff] [blame] | 4228 | xfer_mask &= ~(ATA_MASK_MWDMA | ATA_MASK_UDMA); |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 4229 | ata_dev_warn(dev, |
| 4230 | "simplex DMA is claimed by other device, disabling DMA\n"); |
Alan Cox | 5444a6f | 2006-03-27 18:58:20 +0100 | [diff] [blame] | 4231 | } |
Tejun Heo | 565083e | 2006-04-02 17:54:47 +0900 | [diff] [blame] | 4232 | |
Jeff Garzik | e424675 | 2007-03-09 09:56:46 -0500 | [diff] [blame] | 4233 | if (ap->flags & ATA_FLAG_NO_IORDY) |
| 4234 | xfer_mask &= ata_pio_mask_no_iordy(dev); |
| 4235 | |
Alan Cox | 5444a6f | 2006-03-27 18:58:20 +0100 | [diff] [blame] | 4236 | if (ap->ops->mode_filter) |
Alan Cox | a76b62ca | 2007-03-09 09:34:07 -0500 | [diff] [blame] | 4237 | xfer_mask = ap->ops->mode_filter(dev, xfer_mask); |
Alan Cox | 5444a6f | 2006-03-27 18:58:20 +0100 | [diff] [blame] | 4238 | |
Robert Hancock | 8343f88 | 2007-03-06 02:37:51 -0800 | [diff] [blame] | 4239 | /* Apply cable rule here. Don't apply it early because when |
| 4240 | * we handle hot plug the cable type can itself change. |
| 4241 | * Check this last so that we know if the transfer rate was |
| 4242 | * solely limited by the cable. |
| 4243 | * Unknown or 80 wire cables reported host side are checked |
| 4244 | * drive side as well. Cases where we know a 40wire cable |
| 4245 | * is used safely for 80 are not checked here. |
| 4246 | */ |
| 4247 | if (xfer_mask & (0xF8 << ATA_SHIFT_UDMA)) |
| 4248 | /* UDMA/44 or higher would be available */ |
Alan Cox | 15a5551 | 2008-03-28 14:33:46 -0700 | [diff] [blame] | 4249 | if (cable_is_40wire(ap)) { |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 4250 | ata_dev_warn(dev, |
| 4251 | "limited to UDMA/33 due to 40-wire cable\n"); |
Robert Hancock | 8343f88 | 2007-03-06 02:37:51 -0800 | [diff] [blame] | 4252 | xfer_mask &= ~(0xF8 << ATA_SHIFT_UDMA); |
| 4253 | } |
| 4254 | |
Tejun Heo | 565083e | 2006-04-02 17:54:47 +0900 | [diff] [blame] | 4255 | ata_unpack_xfermask(xfer_mask, &dev->pio_mask, |
| 4256 | &dev->mwdma_mask, &dev->udma_mask); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4257 | } |
| 4258 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4259 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4260 | * ata_dev_set_xfermode - Issue SET FEATURES - XFER MODE command |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4261 | * @dev: Device to which command will be sent |
| 4262 | * |
Jeff Garzik | 780a87f | 2005-05-30 15:41:05 -0400 | [diff] [blame] | 4263 | * Issue SET FEATURES - XFER MODE command to device @dev |
| 4264 | * on port @ap. |
| 4265 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4266 | * LOCKING: |
Jeff Garzik | 0cba632 | 2005-05-30 19:49:12 -0400 | [diff] [blame] | 4267 | * PCI/etc. bus probe sem. |
Tejun Heo | 83206a2 | 2006-03-24 15:25:31 +0900 | [diff] [blame] | 4268 | * |
| 4269 | * RETURNS: |
| 4270 | * 0 on success, AC_ERR_* mask otherwise. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4271 | */ |
| 4272 | |
Tejun Heo | 3373efd | 2006-05-15 20:57:53 +0900 | [diff] [blame] | 4273 | static unsigned int ata_dev_set_xfermode(struct ata_device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4274 | { |
Tejun Heo | a012370 | 2005-12-13 14:49:31 +0900 | [diff] [blame] | 4275 | struct ata_taskfile tf; |
Tejun Heo | 83206a2 | 2006-03-24 15:25:31 +0900 | [diff] [blame] | 4276 | unsigned int err_mask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4277 | |
| 4278 | /* set up set-features taskfile */ |
Hannes Reinecke | 4633778 | 2021-12-21 08:20:37 +0100 | [diff] [blame] | 4279 | ata_dev_dbg(dev, "set features - xfer mode\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4280 | |
Tejun Heo | 464cf17 | 2007-05-27 15:10:40 +0200 | [diff] [blame] | 4281 | /* Some controllers and ATAPI devices show flaky interrupt |
| 4282 | * behavior after setting xfer mode. Use polling instead. |
| 4283 | */ |
Tejun Heo | 3373efd | 2006-05-15 20:57:53 +0900 | [diff] [blame] | 4284 | ata_tf_init(dev, &tf); |
Tejun Heo | a012370 | 2005-12-13 14:49:31 +0900 | [diff] [blame] | 4285 | tf.command = ATA_CMD_SET_FEATURES; |
| 4286 | tf.feature = SETFEATURES_XFER; |
Tejun Heo | 464cf17 | 2007-05-27 15:10:40 +0200 | [diff] [blame] | 4287 | tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE | ATA_TFLAG_POLLING; |
Tejun Heo | a012370 | 2005-12-13 14:49:31 +0900 | [diff] [blame] | 4288 | tf.protocol = ATA_PROT_NODATA; |
Alan Cox | b9f8ab2 | 2007-11-19 14:33:11 +0000 | [diff] [blame] | 4289 | /* If we are using IORDY we must send the mode setting command */ |
Jeff Garzik | 11b7bec | 2007-11-23 21:12:14 -0500 | [diff] [blame] | 4290 | if (ata_pio_need_iordy(dev)) |
| 4291 | tf.nsect = dev->xfer_mode; |
Alan Cox | b9f8ab2 | 2007-11-19 14:33:11 +0000 | [diff] [blame] | 4292 | /* If the device has IORDY and the controller does not - turn it off */ |
| 4293 | else if (ata_id_has_iordy(dev->id)) |
Jeff Garzik | 11b7bec | 2007-11-23 21:12:14 -0500 | [diff] [blame] | 4294 | tf.nsect = 0x01; |
Alan Cox | b9f8ab2 | 2007-11-19 14:33:11 +0000 | [diff] [blame] | 4295 | else /* In the ancient relic department - skip all of this */ |
| 4296 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4297 | |
Mikulas Patocka | d531be2 | 2015-07-08 13:06:12 -0400 | [diff] [blame] | 4298 | /* On some disks, this command causes spin-up, so we need longer timeout */ |
| 4299 | err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 15000); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4300 | |
Tejun Heo | 83206a2 | 2006-03-24 15:25:31 +0900 | [diff] [blame] | 4301 | return err_mask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4302 | } |
Tejun Heo | 1152b26 | 2010-09-01 17:50:05 +0200 | [diff] [blame] | 4303 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4304 | /** |
Jeff Garzik | 218f3d3 | 2007-10-25 00:33:27 -0400 | [diff] [blame] | 4305 | * ata_dev_set_feature - Issue SET FEATURES - SATA FEATURES |
Kristen Carlson Accardi | 9f45cbd | 2007-08-15 03:57:11 -0400 | [diff] [blame] | 4306 | * @dev: Device to which command will be sent |
| 4307 | * @enable: Whether to enable or disable the feature |
Jeff Garzik | 218f3d3 | 2007-10-25 00:33:27 -0400 | [diff] [blame] | 4308 | * @feature: The sector count represents the feature to set |
Kristen Carlson Accardi | 9f45cbd | 2007-08-15 03:57:11 -0400 | [diff] [blame] | 4309 | * |
| 4310 | * Issue SET FEATURES - SATA FEATURES command to device @dev |
Jeff Garzik | 218f3d3 | 2007-10-25 00:33:27 -0400 | [diff] [blame] | 4311 | * on port @ap with sector count |
Kristen Carlson Accardi | 9f45cbd | 2007-08-15 03:57:11 -0400 | [diff] [blame] | 4312 | * |
| 4313 | * LOCKING: |
| 4314 | * PCI/etc. bus probe sem. |
| 4315 | * |
| 4316 | * RETURNS: |
| 4317 | * 0 on success, AC_ERR_* mask otherwise. |
| 4318 | */ |
Tejun Heo | 1152b26 | 2010-09-01 17:50:05 +0200 | [diff] [blame] | 4319 | unsigned int ata_dev_set_feature(struct ata_device *dev, u8 enable, u8 feature) |
Kristen Carlson Accardi | 9f45cbd | 2007-08-15 03:57:11 -0400 | [diff] [blame] | 4320 | { |
| 4321 | struct ata_taskfile tf; |
| 4322 | unsigned int err_mask; |
Damien Le Moal | 974e0a4 | 2016-04-04 12:17:09 -0400 | [diff] [blame] | 4323 | unsigned long timeout = 0; |
Kristen Carlson Accardi | 9f45cbd | 2007-08-15 03:57:11 -0400 | [diff] [blame] | 4324 | |
| 4325 | /* set up set-features taskfile */ |
Hannes Reinecke | 4633778 | 2021-12-21 08:20:37 +0100 | [diff] [blame] | 4326 | ata_dev_dbg(dev, "set features - SATA features\n"); |
Kristen Carlson Accardi | 9f45cbd | 2007-08-15 03:57:11 -0400 | [diff] [blame] | 4327 | |
| 4328 | ata_tf_init(dev, &tf); |
| 4329 | tf.command = ATA_CMD_SET_FEATURES; |
| 4330 | tf.feature = enable; |
| 4331 | tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE; |
| 4332 | tf.protocol = ATA_PROT_NODATA; |
Jeff Garzik | 218f3d3 | 2007-10-25 00:33:27 -0400 | [diff] [blame] | 4333 | tf.nsect = feature; |
Kristen Carlson Accardi | 9f45cbd | 2007-08-15 03:57:11 -0400 | [diff] [blame] | 4334 | |
Damien Le Moal | 974e0a4 | 2016-04-04 12:17:09 -0400 | [diff] [blame] | 4335 | if (enable == SETFEATURES_SPINUP) |
| 4336 | timeout = ata_probe_timeout ? |
| 4337 | ata_probe_timeout * 1000 : SETFEATURES_SPINUP_TIMEOUT; |
| 4338 | err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, timeout); |
Kristen Carlson Accardi | 9f45cbd | 2007-08-15 03:57:11 -0400 | [diff] [blame] | 4339 | |
Kristen Carlson Accardi | 9f45cbd | 2007-08-15 03:57:11 -0400 | [diff] [blame] | 4340 | return err_mask; |
| 4341 | } |
Jeff Garzik | 633de4c | 2012-09-13 01:12:29 -0400 | [diff] [blame] | 4342 | EXPORT_SYMBOL_GPL(ata_dev_set_feature); |
Kristen Carlson Accardi | 9f45cbd | 2007-08-15 03:57:11 -0400 | [diff] [blame] | 4343 | |
| 4344 | /** |
Albert Lee | 8bf62ece | 2005-05-12 15:29:42 -0400 | [diff] [blame] | 4345 | * ata_dev_init_params - Issue INIT DEV PARAMS command |
Albert Lee | 8bf62ece | 2005-05-12 15:29:42 -0400 | [diff] [blame] | 4346 | * @dev: Device to which command will be sent |
Randy Dunlap | e2a7f77 | 2006-05-18 10:50:18 -0700 | [diff] [blame] | 4347 | * @heads: Number of heads (taskfile parameter) |
| 4348 | * @sectors: Number of sectors (taskfile parameter) |
Albert Lee | 8bf62ece | 2005-05-12 15:29:42 -0400 | [diff] [blame] | 4349 | * |
| 4350 | * LOCKING: |
Tejun Heo | 6aff8f1 | 2006-02-15 18:24:09 +0900 | [diff] [blame] | 4351 | * Kernel thread context (may sleep) |
| 4352 | * |
| 4353 | * RETURNS: |
| 4354 | * 0 on success, AC_ERR_* mask otherwise. |
Albert Lee | 8bf62ece | 2005-05-12 15:29:42 -0400 | [diff] [blame] | 4355 | */ |
Tejun Heo | 3373efd | 2006-05-15 20:57:53 +0900 | [diff] [blame] | 4356 | static unsigned int ata_dev_init_params(struct ata_device *dev, |
| 4357 | u16 heads, u16 sectors) |
Albert Lee | 8bf62ece | 2005-05-12 15:29:42 -0400 | [diff] [blame] | 4358 | { |
Tejun Heo | a012370 | 2005-12-13 14:49:31 +0900 | [diff] [blame] | 4359 | struct ata_taskfile tf; |
Tejun Heo | 6aff8f1 | 2006-02-15 18:24:09 +0900 | [diff] [blame] | 4360 | unsigned int err_mask; |
Albert Lee | 8bf62ece | 2005-05-12 15:29:42 -0400 | [diff] [blame] | 4361 | |
| 4362 | /* Number of sectors per track 1-255. Number of heads 1-16 */ |
| 4363 | if (sectors < 1 || sectors > 255 || heads < 1 || heads > 16) |
Albert Lee | 00b6f5e | 2006-03-27 16:39:18 +0800 | [diff] [blame] | 4364 | return AC_ERR_INVALID; |
Albert Lee | 8bf62ece | 2005-05-12 15:29:42 -0400 | [diff] [blame] | 4365 | |
| 4366 | /* set up init dev params taskfile */ |
Hannes Reinecke | 4633778 | 2021-12-21 08:20:37 +0100 | [diff] [blame] | 4367 | ata_dev_dbg(dev, "init dev params \n"); |
Albert Lee | 8bf62ece | 2005-05-12 15:29:42 -0400 | [diff] [blame] | 4368 | |
Tejun Heo | 3373efd | 2006-05-15 20:57:53 +0900 | [diff] [blame] | 4369 | ata_tf_init(dev, &tf); |
Tejun Heo | a012370 | 2005-12-13 14:49:31 +0900 | [diff] [blame] | 4370 | tf.command = ATA_CMD_INIT_DEV_PARAMS; |
| 4371 | tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE; |
| 4372 | tf.protocol = ATA_PROT_NODATA; |
| 4373 | tf.nsect = sectors; |
| 4374 | tf.device |= (heads - 1) & 0x0f; /* max head = num. of heads - 1 */ |
Albert Lee | 8bf62ece | 2005-05-12 15:29:42 -0400 | [diff] [blame] | 4375 | |
Tejun Heo | 2b78910 | 2007-10-09 15:05:44 +0900 | [diff] [blame] | 4376 | err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0); |
Alan Cox | 18b2466 | 2007-08-08 14:28:49 +0100 | [diff] [blame] | 4377 | /* A clean abort indicates an original or just out of spec drive |
| 4378 | and we should continue as we issue the setup based on the |
| 4379 | drive reported working geometry */ |
| 4380 | if (err_mask == AC_ERR_DEV && (tf.feature & ATA_ABORTED)) |
| 4381 | err_mask = 0; |
Albert Lee | 8bf62ece | 2005-05-12 15:29:42 -0400 | [diff] [blame] | 4382 | |
Tejun Heo | 6aff8f1 | 2006-02-15 18:24:09 +0900 | [diff] [blame] | 4383 | return err_mask; |
Albert Lee | 8bf62ece | 2005-05-12 15:29:42 -0400 | [diff] [blame] | 4384 | } |
| 4385 | |
| 4386 | /** |
Tejun Heo | 5895ef9 | 2008-06-17 12:36:26 +0900 | [diff] [blame] | 4387 | * atapi_check_dma - Check whether ATAPI DMA can be supported |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4388 | * @qc: Metadata associated with taskfile to check |
| 4389 | * |
Jeff Garzik | 780a87f | 2005-05-30 15:41:05 -0400 | [diff] [blame] | 4390 | * Allow low-level driver to filter ATA PACKET commands, returning |
| 4391 | * a status indicating whether or not it is OK to use DMA for the |
| 4392 | * supplied PACKET command. |
| 4393 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4394 | * LOCKING: |
Jeff Garzik | cca3974 | 2006-08-24 03:19:22 -0400 | [diff] [blame] | 4395 | * spin_lock_irqsave(host lock) |
Jeff Garzik | 0cba632 | 2005-05-30 19:49:12 -0400 | [diff] [blame] | 4396 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4397 | * RETURNS: 0 when ATAPI DMA can be used |
| 4398 | * nonzero otherwise |
| 4399 | */ |
Tejun Heo | 5895ef9 | 2008-06-17 12:36:26 +0900 | [diff] [blame] | 4400 | int atapi_check_dma(struct ata_queued_cmd *qc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4401 | { |
| 4402 | struct ata_port *ap = qc->ap; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4403 | |
Tejun Heo | b9a4197 | 2007-06-27 02:48:43 +0900 | [diff] [blame] | 4404 | /* Don't allow DMA if it isn't multiple of 16 bytes. Quite a |
| 4405 | * few ATAPI devices choke on such DMA requests. |
| 4406 | */ |
Tejun Heo | 6a87e42 | 2008-11-03 19:01:09 +0900 | [diff] [blame] | 4407 | if (!(qc->dev->horkage & ATA_HORKAGE_ATAPI_MOD16_DMA) && |
| 4408 | unlikely(qc->nbytes & 15)) |
Tejun Heo | b9a4197 | 2007-06-27 02:48:43 +0900 | [diff] [blame] | 4409 | return 1; |
Albert Lee | 6f23a31 | 2007-04-02 11:39:25 +0800 | [diff] [blame] | 4410 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4411 | if (ap->ops->check_atapi_dma) |
Tejun Heo | b9a4197 | 2007-06-27 02:48:43 +0900 | [diff] [blame] | 4412 | return ap->ops->check_atapi_dma(qc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4413 | |
Tejun Heo | b9a4197 | 2007-06-27 02:48:43 +0900 | [diff] [blame] | 4414 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4415 | } |
Tejun Heo | b9a4197 | 2007-06-27 02:48:43 +0900 | [diff] [blame] | 4416 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4417 | /** |
Tejun Heo | 31cc23b | 2007-09-23 13:14:12 +0900 | [diff] [blame] | 4418 | * ata_std_qc_defer - Check whether a qc needs to be deferred |
| 4419 | * @qc: ATA command in question |
| 4420 | * |
| 4421 | * Non-NCQ commands cannot run with any other command, NCQ or |
| 4422 | * not. As upper layer only knows the queue depth, we are |
| 4423 | * responsible for maintaining exclusion. This function checks |
| 4424 | * whether a new command @qc can be issued. |
| 4425 | * |
| 4426 | * LOCKING: |
| 4427 | * spin_lock_irqsave(host lock) |
| 4428 | * |
| 4429 | * RETURNS: |
| 4430 | * ATA_DEFER_* if deferring is needed, 0 otherwise. |
| 4431 | */ |
| 4432 | int ata_std_qc_defer(struct ata_queued_cmd *qc) |
| 4433 | { |
| 4434 | struct ata_link *link = qc->dev->link; |
| 4435 | |
Hannes Reinecke | 179b310 | 2016-07-14 09:05:43 +0900 | [diff] [blame] | 4436 | if (ata_is_ncq(qc->tf.protocol)) { |
Tejun Heo | 31cc23b | 2007-09-23 13:14:12 +0900 | [diff] [blame] | 4437 | if (!ata_tag_valid(link->active_tag)) |
| 4438 | return 0; |
| 4439 | } else { |
| 4440 | if (!ata_tag_valid(link->active_tag) && !link->sactive) |
| 4441 | return 0; |
| 4442 | } |
| 4443 | |
| 4444 | return ATA_DEFER_LINK; |
| 4445 | } |
Bartlomiej Zolnierkiewicz | a52fbcf | 2020-03-26 16:58:04 +0100 | [diff] [blame] | 4446 | EXPORT_SYMBOL_GPL(ata_std_qc_defer); |
Tejun Heo | 31cc23b | 2007-09-23 13:14:12 +0900 | [diff] [blame] | 4447 | |
Jiri Slaby | 95364f3 | 2019-10-31 10:59:45 +0100 | [diff] [blame] | 4448 | enum ata_completion_errors ata_noop_qc_prep(struct ata_queued_cmd *qc) |
| 4449 | { |
| 4450 | return AC_ERR_OK; |
| 4451 | } |
Bartlomiej Zolnierkiewicz | a52fbcf | 2020-03-26 16:58:04 +0100 | [diff] [blame] | 4452 | EXPORT_SYMBOL_GPL(ata_noop_qc_prep); |
Brian King | e46834c | 2006-03-17 17:04:03 -0600 | [diff] [blame] | 4453 | |
Jeff Garzik | 0cba632 | 2005-05-30 19:49:12 -0400 | [diff] [blame] | 4454 | /** |
Jeff Garzik | 0cba632 | 2005-05-30 19:49:12 -0400 | [diff] [blame] | 4455 | * ata_sg_init - Associate command with scatter-gather table. |
| 4456 | * @qc: Command to be associated |
| 4457 | * @sg: Scatter-gather table. |
| 4458 | * @n_elem: Number of elements in s/g table. |
| 4459 | * |
| 4460 | * Initialize the data-related elements of queued_cmd @qc |
| 4461 | * to point to a scatter-gather table @sg, containing @n_elem |
| 4462 | * elements. |
| 4463 | * |
| 4464 | * LOCKING: |
Jeff Garzik | cca3974 | 2006-08-24 03:19:22 -0400 | [diff] [blame] | 4465 | * spin_lock_irqsave(host lock) |
Jeff Garzik | 0cba632 | 2005-05-30 19:49:12 -0400 | [diff] [blame] | 4466 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4467 | void ata_sg_init(struct ata_queued_cmd *qc, struct scatterlist *sg, |
| 4468 | unsigned int n_elem) |
| 4469 | { |
Tejun Heo | ff2aeb1 | 2007-12-05 16:43:11 +0900 | [diff] [blame] | 4470 | qc->sg = sg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4471 | qc->n_elem = n_elem; |
Tejun Heo | ff2aeb1 | 2007-12-05 16:43:11 +0900 | [diff] [blame] | 4472 | qc->cursg = qc->sg; |
| 4473 | } |
| 4474 | |
Geert Uytterhoeven | 2874d5e | 2017-01-03 19:09:49 +0100 | [diff] [blame] | 4475 | #ifdef CONFIG_HAS_DMA |
| 4476 | |
| 4477 | /** |
| 4478 | * ata_sg_clean - Unmap DMA memory associated with command |
| 4479 | * @qc: Command containing DMA memory to be released |
| 4480 | * |
| 4481 | * Unmap all mapped DMA memory associated with this command. |
| 4482 | * |
| 4483 | * LOCKING: |
| 4484 | * spin_lock_irqsave(host lock) |
| 4485 | */ |
Jason Yan | af27e01 | 2017-03-10 10:05:40 +0800 | [diff] [blame] | 4486 | static void ata_sg_clean(struct ata_queued_cmd *qc) |
Geert Uytterhoeven | 2874d5e | 2017-01-03 19:09:49 +0100 | [diff] [blame] | 4487 | { |
| 4488 | struct ata_port *ap = qc->ap; |
| 4489 | struct scatterlist *sg = qc->sg; |
| 4490 | int dir = qc->dma_dir; |
| 4491 | |
| 4492 | WARN_ON_ONCE(sg == NULL); |
| 4493 | |
Geert Uytterhoeven | 2874d5e | 2017-01-03 19:09:49 +0100 | [diff] [blame] | 4494 | if (qc->n_elem) |
| 4495 | dma_unmap_sg(ap->dev, sg, qc->orig_n_elem, dir); |
| 4496 | |
| 4497 | qc->flags &= ~ATA_QCFLAG_DMAMAP; |
| 4498 | qc->sg = NULL; |
| 4499 | } |
| 4500 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4501 | /** |
Jeff Garzik | 0cba632 | 2005-05-30 19:49:12 -0400 | [diff] [blame] | 4502 | * ata_sg_setup - DMA-map the scatter-gather table associated with a command. |
| 4503 | * @qc: Command with scatter-gather table to be mapped. |
| 4504 | * |
| 4505 | * DMA-map the scatter-gather table associated with queued_cmd @qc. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4506 | * |
| 4507 | * LOCKING: |
Jeff Garzik | cca3974 | 2006-08-24 03:19:22 -0400 | [diff] [blame] | 4508 | * spin_lock_irqsave(host lock) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4509 | * |
| 4510 | * RETURNS: |
Jeff Garzik | 0cba632 | 2005-05-30 19:49:12 -0400 | [diff] [blame] | 4511 | * Zero on success, negative on error. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4512 | * |
| 4513 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4514 | static int ata_sg_setup(struct ata_queued_cmd *qc) |
| 4515 | { |
| 4516 | struct ata_port *ap = qc->ap; |
James Bottomley | dde2020 | 2008-02-19 11:36:56 +0100 | [diff] [blame] | 4517 | unsigned int n_elem; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4518 | |
James Bottomley | dde2020 | 2008-02-19 11:36:56 +0100 | [diff] [blame] | 4519 | n_elem = dma_map_sg(ap->dev, qc->sg, qc->n_elem, qc->dma_dir); |
| 4520 | if (n_elem < 1) |
| 4521 | return -1; |
Jeff Garzik | cedc9a4 | 2005-10-05 07:13:30 -0400 | [diff] [blame] | 4522 | |
FUJITA Tomonori | 5825627 | 2009-02-27 17:35:43 +0900 | [diff] [blame] | 4523 | qc->orig_n_elem = qc->n_elem; |
James Bottomley | dde2020 | 2008-02-19 11:36:56 +0100 | [diff] [blame] | 4524 | qc->n_elem = n_elem; |
Tejun Heo | f92a263 | 2007-12-05 16:43:10 +0900 | [diff] [blame] | 4525 | qc->flags |= ATA_QCFLAG_DMAMAP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4526 | |
| 4527 | return 0; |
| 4528 | } |
| 4529 | |
Geert Uytterhoeven | 2874d5e | 2017-01-03 19:09:49 +0100 | [diff] [blame] | 4530 | #else /* !CONFIG_HAS_DMA */ |
| 4531 | |
| 4532 | static inline void ata_sg_clean(struct ata_queued_cmd *qc) {} |
| 4533 | static inline int ata_sg_setup(struct ata_queued_cmd *qc) { return -1; } |
| 4534 | |
| 4535 | #endif /* !CONFIG_HAS_DMA */ |
| 4536 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4537 | /** |
Randy Dunlap | c893a3a | 2006-01-28 13:15:32 -0500 | [diff] [blame] | 4538 | * swap_buf_le16 - swap halves of 16-bit words in place |
Edward Falk | 0baab86 | 2005-06-02 18:17:13 -0400 | [diff] [blame] | 4539 | * @buf: Buffer to swap |
| 4540 | * @buf_words: Number of 16-bit words in buffer. |
| 4541 | * |
| 4542 | * Swap halves of 16-bit words if needed to convert from |
| 4543 | * little-endian byte order to native cpu byte order, or |
| 4544 | * vice-versa. |
| 4545 | * |
| 4546 | * LOCKING: |
Randy Dunlap | 6f0ef4f | 2005-10-25 01:44:30 -0400 | [diff] [blame] | 4547 | * Inherited from caller. |
Edward Falk | 0baab86 | 2005-06-02 18:17:13 -0400 | [diff] [blame] | 4548 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4549 | void swap_buf_le16(u16 *buf, unsigned int buf_words) |
| 4550 | { |
| 4551 | #ifdef __BIG_ENDIAN |
| 4552 | unsigned int i; |
| 4553 | |
| 4554 | for (i = 0; i < buf_words; i++) |
| 4555 | buf[i] = le16_to_cpu(buf[i]); |
| 4556 | #endif /* __BIG_ENDIAN */ |
| 4557 | } |
| 4558 | |
Albert Lee | 6ae4cfb | 2005-08-12 14:15:34 +0800 | [diff] [blame] | 4559 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4560 | * ata_qc_new_init - Request an available ATA command, and initialize it |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4561 | * @dev: Device from whom we request an available command structure |
Randy Dunlap | 38755e8 | 2015-08-09 18:37:36 -0700 | [diff] [blame] | 4562 | * @tag: tag |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4563 | * |
| 4564 | * LOCKING: |
Jeff Garzik | 0cba632 | 2005-05-30 19:49:12 -0400 | [diff] [blame] | 4565 | * None. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4566 | */ |
| 4567 | |
Shaohua Li | 98bd4be | 2015-01-23 19:52:07 -0800 | [diff] [blame] | 4568 | struct ata_queued_cmd *ata_qc_new_init(struct ata_device *dev, int tag) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4569 | { |
Tejun Heo | 9af5c9c | 2007-08-06 18:36:22 +0900 | [diff] [blame] | 4570 | struct ata_port *ap = dev->link->ap; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4571 | struct ata_queued_cmd *qc; |
| 4572 | |
Shaohua Li | 98bd4be | 2015-01-23 19:52:07 -0800 | [diff] [blame] | 4573 | /* no command while frozen */ |
| 4574 | if (unlikely(ap->pflags & ATA_PFLAG_FROZEN)) |
| 4575 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4576 | |
Shaohua Li | 98bd4be | 2015-01-23 19:52:07 -0800 | [diff] [blame] | 4577 | /* libsas case */ |
Shaohua Li | 5067c04 | 2015-03-12 10:32:18 -0700 | [diff] [blame] | 4578 | if (ap->flags & ATA_FLAG_SAS_HOST) { |
Shaohua Li | 98bd4be | 2015-01-23 19:52:07 -0800 | [diff] [blame] | 4579 | tag = ata_sas_allocate_tag(ap); |
| 4580 | if (tag < 0) |
| 4581 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4582 | } |
| 4583 | |
Shaohua Li | 98bd4be | 2015-01-23 19:52:07 -0800 | [diff] [blame] | 4584 | qc = __ata_qc_from_tag(ap, tag); |
Jens Axboe | 5ac4079 | 2018-05-11 12:51:03 -0600 | [diff] [blame] | 4585 | qc->tag = qc->hw_tag = tag; |
Shaohua Li | 98bd4be | 2015-01-23 19:52:07 -0800 | [diff] [blame] | 4586 | qc->scsicmd = NULL; |
| 4587 | qc->ap = ap; |
| 4588 | qc->dev = dev; |
| 4589 | |
| 4590 | ata_qc_reinit(qc); |
| 4591 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4592 | return qc; |
| 4593 | } |
| 4594 | |
Tejun Heo | 8a8bc22 | 2008-11-10 14:48:21 +0900 | [diff] [blame] | 4595 | /** |
| 4596 | * ata_qc_free - free unused ata_queued_cmd |
| 4597 | * @qc: Command to complete |
| 4598 | * |
| 4599 | * Designed to free unused ata_queued_cmd object |
| 4600 | * in case something prevents using it. |
| 4601 | * |
| 4602 | * LOCKING: |
| 4603 | * spin_lock_irqsave(host lock) |
| 4604 | */ |
| 4605 | void ata_qc_free(struct ata_queued_cmd *qc) |
| 4606 | { |
Julia Lawall | a110401 | 2009-10-17 08:41:47 +0200 | [diff] [blame] | 4607 | struct ata_port *ap; |
Tejun Heo | 8a8bc22 | 2008-11-10 14:48:21 +0900 | [diff] [blame] | 4608 | unsigned int tag; |
| 4609 | |
Tejun Heo | efcb3cf | 2009-01-09 19:19:14 +0900 | [diff] [blame] | 4610 | WARN_ON_ONCE(qc == NULL); /* ata_qc_from_tag _might_ return NULL */ |
Julia Lawall | a110401 | 2009-10-17 08:41:47 +0200 | [diff] [blame] | 4611 | ap = qc->ap; |
Tejun Heo | 8a8bc22 | 2008-11-10 14:48:21 +0900 | [diff] [blame] | 4612 | |
| 4613 | qc->flags = 0; |
| 4614 | tag = qc->tag; |
Jens Axboe | 28361c4 | 2018-05-11 12:51:09 -0600 | [diff] [blame] | 4615 | if (ata_tag_valid(tag)) { |
Tejun Heo | 8a8bc22 | 2008-11-10 14:48:21 +0900 | [diff] [blame] | 4616 | qc->tag = ATA_TAG_POISON; |
Shaohua Li | 5067c04 | 2015-03-12 10:32:18 -0700 | [diff] [blame] | 4617 | if (ap->flags & ATA_FLAG_SAS_HOST) |
Shaohua Li | 98bd4be | 2015-01-23 19:52:07 -0800 | [diff] [blame] | 4618 | ata_sas_free_tag(tag, ap); |
Tejun Heo | 8a8bc22 | 2008-11-10 14:48:21 +0900 | [diff] [blame] | 4619 | } |
| 4620 | } |
| 4621 | |
Tejun Heo | 7601442 | 2006-02-11 15:13:49 +0900 | [diff] [blame] | 4622 | void __ata_qc_complete(struct ata_queued_cmd *qc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4623 | { |
Julia Lawall | a110401 | 2009-10-17 08:41:47 +0200 | [diff] [blame] | 4624 | struct ata_port *ap; |
| 4625 | struct ata_link *link; |
Tejun Heo | dedaf2b | 2006-05-15 21:03:43 +0900 | [diff] [blame] | 4626 | |
Tejun Heo | efcb3cf | 2009-01-09 19:19:14 +0900 | [diff] [blame] | 4627 | WARN_ON_ONCE(qc == NULL); /* ata_qc_from_tag _might_ return NULL */ |
| 4628 | WARN_ON_ONCE(!(qc->flags & ATA_QCFLAG_ACTIVE)); |
Julia Lawall | a110401 | 2009-10-17 08:41:47 +0200 | [diff] [blame] | 4629 | ap = qc->ap; |
| 4630 | link = qc->dev->link; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4631 | |
| 4632 | if (likely(qc->flags & ATA_QCFLAG_DMAMAP)) |
| 4633 | ata_sg_clean(qc); |
| 4634 | |
Tejun Heo | 7401abf | 2006-05-15 20:57:32 +0900 | [diff] [blame] | 4635 | /* command should be marked inactive atomically with qc completion */ |
Hannes Reinecke | 179b310 | 2016-07-14 09:05:43 +0900 | [diff] [blame] | 4636 | if (ata_is_ncq(qc->tf.protocol)) { |
Jens Axboe | 4e5b626 | 2018-05-11 12:51:04 -0600 | [diff] [blame] | 4637 | link->sactive &= ~(1 << qc->hw_tag); |
Tejun Heo | da917d6 | 2007-09-23 13:14:12 +0900 | [diff] [blame] | 4638 | if (!link->sactive) |
| 4639 | ap->nr_active_links--; |
| 4640 | } else { |
Tejun Heo | 9af5c9c | 2007-08-06 18:36:22 +0900 | [diff] [blame] | 4641 | link->active_tag = ATA_TAG_POISON; |
Tejun Heo | da917d6 | 2007-09-23 13:14:12 +0900 | [diff] [blame] | 4642 | ap->nr_active_links--; |
| 4643 | } |
| 4644 | |
| 4645 | /* clear exclusive status */ |
| 4646 | if (unlikely(qc->flags & ATA_QCFLAG_CLEAR_EXCL && |
| 4647 | ap->excl_link == link)) |
| 4648 | ap->excl_link = NULL; |
Tejun Heo | 7401abf | 2006-05-15 20:57:32 +0900 | [diff] [blame] | 4649 | |
Albert Lee | 3f3791d | 2005-08-16 14:25:38 +0800 | [diff] [blame] | 4650 | /* atapi: mark qc as inactive to prevent the interrupt handler |
| 4651 | * from completing the command twice later, before the error handler |
| 4652 | * is called. (when rc != 0 and atapi request sense is needed) |
| 4653 | */ |
| 4654 | qc->flags &= ~ATA_QCFLAG_ACTIVE; |
Jens Axboe | e3ed8939 | 2018-05-11 12:51:05 -0600 | [diff] [blame] | 4655 | ap->qc_active &= ~(1ULL << qc->tag); |
Albert Lee | 3f3791d | 2005-08-16 14:25:38 +0800 | [diff] [blame] | 4656 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4657 | /* call completion callback */ |
Tejun Heo | 77853bf | 2006-01-23 13:09:36 +0900 | [diff] [blame] | 4658 | qc->complete_fn(qc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4659 | } |
| 4660 | |
Tejun Heo | 39599a5 | 2006-11-14 22:37:35 +0900 | [diff] [blame] | 4661 | static void fill_result_tf(struct ata_queued_cmd *qc) |
| 4662 | { |
| 4663 | struct ata_port *ap = qc->ap; |
| 4664 | |
Tejun Heo | 39599a5 | 2006-11-14 22:37:35 +0900 | [diff] [blame] | 4665 | qc->result_tf.flags = qc->tf.flags; |
Tejun Heo | 22183bf | 2008-04-07 22:47:20 +0900 | [diff] [blame] | 4666 | ap->ops->qc_fill_rtf(qc); |
Tejun Heo | 39599a5 | 2006-11-14 22:37:35 +0900 | [diff] [blame] | 4667 | } |
| 4668 | |
Tejun Heo | 00115e0 | 2007-11-27 19:28:58 +0900 | [diff] [blame] | 4669 | static void ata_verify_xfer(struct ata_queued_cmd *qc) |
| 4670 | { |
| 4671 | struct ata_device *dev = qc->dev; |
| 4672 | |
Christoph Hellwig | eb0effd | 2016-07-16 22:16:41 +0900 | [diff] [blame] | 4673 | if (!ata_is_data(qc->tf.protocol)) |
Tejun Heo | 00115e0 | 2007-11-27 19:28:58 +0900 | [diff] [blame] | 4674 | return; |
| 4675 | |
| 4676 | if ((dev->mwdma_mask || dev->udma_mask) && ata_is_pio(qc->tf.protocol)) |
| 4677 | return; |
| 4678 | |
| 4679 | dev->flags &= ~ATA_DFLAG_DUBIOUS_XFER; |
| 4680 | } |
| 4681 | |
Tejun Heo | f686bcb | 2006-05-15 20:58:05 +0900 | [diff] [blame] | 4682 | /** |
| 4683 | * ata_qc_complete - Complete an active ATA command |
| 4684 | * @qc: Command to complete |
Tejun Heo | f686bcb | 2006-05-15 20:58:05 +0900 | [diff] [blame] | 4685 | * |
Tejun Heo | 1aadf5c | 2010-06-25 15:03:34 +0200 | [diff] [blame] | 4686 | * Indicate to the mid and upper layers that an ATA command has |
| 4687 | * completed, with either an ok or not-ok status. |
| 4688 | * |
| 4689 | * Refrain from calling this function multiple times when |
| 4690 | * successfully completing multiple NCQ commands. |
| 4691 | * ata_qc_complete_multiple() should be used instead, which will |
| 4692 | * properly update IRQ expect state. |
Tejun Heo | f686bcb | 2006-05-15 20:58:05 +0900 | [diff] [blame] | 4693 | * |
| 4694 | * LOCKING: |
Jeff Garzik | cca3974 | 2006-08-24 03:19:22 -0400 | [diff] [blame] | 4695 | * spin_lock_irqsave(host lock) |
Tejun Heo | f686bcb | 2006-05-15 20:58:05 +0900 | [diff] [blame] | 4696 | */ |
| 4697 | void ata_qc_complete(struct ata_queued_cmd *qc) |
| 4698 | { |
| 4699 | struct ata_port *ap = qc->ap; |
| 4700 | |
Stephan Linz | eb25cb9 | 2016-06-10 07:59:56 +0200 | [diff] [blame] | 4701 | /* Trigger the LED (if available) */ |
Linus Walleij | d1ed7c5 | 2018-02-24 23:45:56 +0100 | [diff] [blame] | 4702 | ledtrig_disk_activity(!!(qc->tf.flags & ATA_TFLAG_WRITE)); |
Stephan Linz | eb25cb9 | 2016-06-10 07:59:56 +0200 | [diff] [blame] | 4703 | |
Tejun Heo | f686bcb | 2006-05-15 20:58:05 +0900 | [diff] [blame] | 4704 | /* XXX: New EH and old EH use different mechanisms to |
| 4705 | * synchronize EH with regular execution path. |
| 4706 | * |
| 4707 | * In new EH, a failed qc is marked with ATA_QCFLAG_FAILED. |
| 4708 | * Normal execution path is responsible for not accessing a |
| 4709 | * failed qc. libata core enforces the rule by returning NULL |
| 4710 | * from ata_qc_from_tag() for failed qcs. |
| 4711 | * |
| 4712 | * Old EH depends on ata_qc_complete() nullifying completion |
| 4713 | * requests if ATA_QCFLAG_EH_SCHEDULED is set. Old EH does |
| 4714 | * not synchronize with interrupt handler. Only PIO task is |
| 4715 | * taken care of. |
| 4716 | */ |
| 4717 | if (ap->ops->error_handler) { |
Tejun Heo | 4dbfa39 | 2007-10-25 18:22:44 +0900 | [diff] [blame] | 4718 | struct ata_device *dev = qc->dev; |
| 4719 | struct ata_eh_info *ehi = &dev->link->eh_info; |
| 4720 | |
Tejun Heo | f686bcb | 2006-05-15 20:58:05 +0900 | [diff] [blame] | 4721 | if (unlikely(qc->err_mask)) |
| 4722 | qc->flags |= ATA_QCFLAG_FAILED; |
| 4723 | |
Tejun Heo | f08dc1a | 2010-12-09 15:59:32 +0100 | [diff] [blame] | 4724 | /* |
| 4725 | * Finish internal commands without any further processing |
| 4726 | * and always with the result TF filled. |
| 4727 | */ |
| 4728 | if (unlikely(ata_tag_internal(qc->tag))) { |
Tejun Heo | f4b31db9 | 2009-10-16 13:00:51 +0900 | [diff] [blame] | 4729 | fill_result_tf(qc); |
Hannes Reinecke | 255c03d | 2015-03-27 16:46:38 +0100 | [diff] [blame] | 4730 | trace_ata_qc_complete_internal(qc); |
Tejun Heo | f08dc1a | 2010-12-09 15:59:32 +0100 | [diff] [blame] | 4731 | __ata_qc_complete(qc); |
| 4732 | return; |
| 4733 | } |
Tejun Heo | f4b31db9 | 2009-10-16 13:00:51 +0900 | [diff] [blame] | 4734 | |
Tejun Heo | f08dc1a | 2010-12-09 15:59:32 +0100 | [diff] [blame] | 4735 | /* |
| 4736 | * Non-internal qc has failed. Fill the result TF and |
| 4737 | * summon EH. |
| 4738 | */ |
| 4739 | if (unlikely(qc->flags & ATA_QCFLAG_FAILED)) { |
| 4740 | fill_result_tf(qc); |
Hannes Reinecke | 255c03d | 2015-03-27 16:46:38 +0100 | [diff] [blame] | 4741 | trace_ata_qc_complete_failed(qc); |
Tejun Heo | f08dc1a | 2010-12-09 15:59:32 +0100 | [diff] [blame] | 4742 | ata_qc_schedule_eh(qc); |
Tejun Heo | f4b31db9 | 2009-10-16 13:00:51 +0900 | [diff] [blame] | 4743 | return; |
Tejun Heo | f686bcb | 2006-05-15 20:58:05 +0900 | [diff] [blame] | 4744 | } |
| 4745 | |
Tejun Heo | 4dc738e | 2009-09-17 18:45:27 +0900 | [diff] [blame] | 4746 | WARN_ON_ONCE(ap->pflags & ATA_PFLAG_FROZEN); |
| 4747 | |
Tejun Heo | f686bcb | 2006-05-15 20:58:05 +0900 | [diff] [blame] | 4748 | /* read result TF if requested */ |
| 4749 | if (qc->flags & ATA_QCFLAG_RESULT_TF) |
Tejun Heo | 39599a5 | 2006-11-14 22:37:35 +0900 | [diff] [blame] | 4750 | fill_result_tf(qc); |
Tejun Heo | f686bcb | 2006-05-15 20:58:05 +0900 | [diff] [blame] | 4751 | |
Hannes Reinecke | 255c03d | 2015-03-27 16:46:38 +0100 | [diff] [blame] | 4752 | trace_ata_qc_complete_done(qc); |
Tejun Heo | 4dbfa39 | 2007-10-25 18:22:44 +0900 | [diff] [blame] | 4753 | /* Some commands need post-processing after successful |
| 4754 | * completion. |
| 4755 | */ |
| 4756 | switch (qc->tf.command) { |
| 4757 | case ATA_CMD_SET_FEATURES: |
| 4758 | if (qc->tf.feature != SETFEATURES_WC_ON && |
Tom Yan | 0c12735 | 2016-07-12 21:37:02 +0800 | [diff] [blame] | 4759 | qc->tf.feature != SETFEATURES_WC_OFF && |
| 4760 | qc->tf.feature != SETFEATURES_RA_ON && |
| 4761 | qc->tf.feature != SETFEATURES_RA_OFF) |
Tejun Heo | 4dbfa39 | 2007-10-25 18:22:44 +0900 | [diff] [blame] | 4762 | break; |
Gustavo A. R. Silva | df561f66 | 2020-08-23 17:36:59 -0500 | [diff] [blame] | 4763 | fallthrough; |
Tejun Heo | 4dbfa39 | 2007-10-25 18:22:44 +0900 | [diff] [blame] | 4764 | case ATA_CMD_INIT_DEV_PARAMS: /* CHS translation changed */ |
| 4765 | case ATA_CMD_SET_MULTI: /* multi_count changed */ |
| 4766 | /* revalidate device */ |
| 4767 | ehi->dev_action[dev->devno] |= ATA_EH_REVALIDATE; |
| 4768 | ata_port_schedule_eh(ap); |
| 4769 | break; |
Tejun Heo | 054a5fb | 2007-10-25 18:30:36 +0900 | [diff] [blame] | 4770 | |
| 4771 | case ATA_CMD_SLEEP: |
| 4772 | dev->flags |= ATA_DFLAG_SLEEPING; |
| 4773 | break; |
Tejun Heo | 4dbfa39 | 2007-10-25 18:22:44 +0900 | [diff] [blame] | 4774 | } |
| 4775 | |
Tejun Heo | 00115e0 | 2007-11-27 19:28:58 +0900 | [diff] [blame] | 4776 | if (unlikely(dev->flags & ATA_DFLAG_DUBIOUS_XFER)) |
| 4777 | ata_verify_xfer(qc); |
| 4778 | |
Tejun Heo | f686bcb | 2006-05-15 20:58:05 +0900 | [diff] [blame] | 4779 | __ata_qc_complete(qc); |
| 4780 | } else { |
| 4781 | if (qc->flags & ATA_QCFLAG_EH_SCHEDULED) |
| 4782 | return; |
| 4783 | |
| 4784 | /* read result TF if failed or requested */ |
| 4785 | if (qc->err_mask || qc->flags & ATA_QCFLAG_RESULT_TF) |
Tejun Heo | 39599a5 | 2006-11-14 22:37:35 +0900 | [diff] [blame] | 4786 | fill_result_tf(qc); |
Tejun Heo | f686bcb | 2006-05-15 20:58:05 +0900 | [diff] [blame] | 4787 | |
| 4788 | __ata_qc_complete(qc); |
| 4789 | } |
| 4790 | } |
Bartlomiej Zolnierkiewicz | a52fbcf | 2020-03-26 16:58:04 +0100 | [diff] [blame] | 4791 | EXPORT_SYMBOL_GPL(ata_qc_complete); |
Tejun Heo | f686bcb | 2006-05-15 20:58:05 +0900 | [diff] [blame] | 4792 | |
Tejun Heo | dedaf2b | 2006-05-15 21:03:43 +0900 | [diff] [blame] | 4793 | /** |
Sascha Hauer | 8385d75 | 2019-12-13 09:04:08 +0100 | [diff] [blame] | 4794 | * ata_qc_get_active - get bitmask of active qcs |
| 4795 | * @ap: port in question |
| 4796 | * |
| 4797 | * LOCKING: |
| 4798 | * spin_lock_irqsave(host lock) |
| 4799 | * |
| 4800 | * RETURNS: |
| 4801 | * Bitmask of active qcs |
| 4802 | */ |
| 4803 | u64 ata_qc_get_active(struct ata_port *ap) |
| 4804 | { |
| 4805 | u64 qc_active = ap->qc_active; |
| 4806 | |
| 4807 | /* ATA_TAG_INTERNAL is sent to hw as tag 0 */ |
| 4808 | if (qc_active & (1ULL << ATA_TAG_INTERNAL)) { |
| 4809 | qc_active |= (1 << 0); |
| 4810 | qc_active &= ~(1ULL << ATA_TAG_INTERNAL); |
| 4811 | } |
| 4812 | |
| 4813 | return qc_active; |
| 4814 | } |
| 4815 | EXPORT_SYMBOL_GPL(ata_qc_get_active); |
| 4816 | |
| 4817 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4818 | * ata_qc_issue - issue taskfile to device |
| 4819 | * @qc: command to issue to device |
| 4820 | * |
| 4821 | * Prepare an ATA command to submission to device. |
| 4822 | * This includes mapping the data into a DMA-able |
| 4823 | * area, filling in the S/G table, and finally |
| 4824 | * writing the taskfile to hardware, starting the command. |
| 4825 | * |
| 4826 | * LOCKING: |
Jeff Garzik | cca3974 | 2006-08-24 03:19:22 -0400 | [diff] [blame] | 4827 | * spin_lock_irqsave(host lock) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4828 | */ |
Tejun Heo | 8e0e694 | 2006-03-31 20:41:11 +0900 | [diff] [blame] | 4829 | void ata_qc_issue(struct ata_queued_cmd *qc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4830 | { |
| 4831 | struct ata_port *ap = qc->ap; |
Tejun Heo | 9af5c9c | 2007-08-06 18:36:22 +0900 | [diff] [blame] | 4832 | struct ata_link *link = qc->dev->link; |
Tejun Heo | 405e66b | 2007-11-27 19:28:53 +0900 | [diff] [blame] | 4833 | u8 prot = qc->tf.protocol; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4834 | |
Tejun Heo | dedaf2b | 2006-05-15 21:03:43 +0900 | [diff] [blame] | 4835 | /* Make sure only one non-NCQ command is outstanding. The |
| 4836 | * check is skipped for old EH because it reuses active qc to |
| 4837 | * request ATAPI sense. |
| 4838 | */ |
Tejun Heo | efcb3cf | 2009-01-09 19:19:14 +0900 | [diff] [blame] | 4839 | WARN_ON_ONCE(ap->ops->error_handler && ata_tag_valid(link->active_tag)); |
Tejun Heo | dedaf2b | 2006-05-15 21:03:43 +0900 | [diff] [blame] | 4840 | |
Tejun Heo | 1973a02 | 2007-12-05 10:36:13 +0900 | [diff] [blame] | 4841 | if (ata_is_ncq(prot)) { |
Jens Axboe | 4e5b626 | 2018-05-11 12:51:04 -0600 | [diff] [blame] | 4842 | WARN_ON_ONCE(link->sactive & (1 << qc->hw_tag)); |
Tejun Heo | da917d6 | 2007-09-23 13:14:12 +0900 | [diff] [blame] | 4843 | |
| 4844 | if (!link->sactive) |
| 4845 | ap->nr_active_links++; |
Jens Axboe | 4e5b626 | 2018-05-11 12:51:04 -0600 | [diff] [blame] | 4846 | link->sactive |= 1 << qc->hw_tag; |
Tejun Heo | dedaf2b | 2006-05-15 21:03:43 +0900 | [diff] [blame] | 4847 | } else { |
Tejun Heo | efcb3cf | 2009-01-09 19:19:14 +0900 | [diff] [blame] | 4848 | WARN_ON_ONCE(link->sactive); |
Tejun Heo | da917d6 | 2007-09-23 13:14:12 +0900 | [diff] [blame] | 4849 | |
| 4850 | ap->nr_active_links++; |
Tejun Heo | 9af5c9c | 2007-08-06 18:36:22 +0900 | [diff] [blame] | 4851 | link->active_tag = qc->tag; |
Tejun Heo | dedaf2b | 2006-05-15 21:03:43 +0900 | [diff] [blame] | 4852 | } |
| 4853 | |
Tejun Heo | e4a70e7 | 2006-03-31 20:36:47 +0900 | [diff] [blame] | 4854 | qc->flags |= ATA_QCFLAG_ACTIVE; |
Jens Axboe | e3ed8939 | 2018-05-11 12:51:05 -0600 | [diff] [blame] | 4855 | ap->qc_active |= 1ULL << qc->tag; |
Tejun Heo | e4a70e7 | 2006-03-31 20:36:47 +0900 | [diff] [blame] | 4856 | |
Tejun Heo | 60f5d6e | 2010-08-23 11:27:27 +0200 | [diff] [blame] | 4857 | /* |
| 4858 | * We guarantee to LLDs that they will have at least one |
Tejun Heo | f92a263 | 2007-12-05 16:43:10 +0900 | [diff] [blame] | 4859 | * non-zero sg if the command is a data command. |
| 4860 | */ |
Eric Biggers | 9173e5e | 2018-02-03 20:33:27 -0800 | [diff] [blame] | 4861 | if (ata_is_data(prot) && (!qc->sg || !qc->n_elem || !qc->nbytes)) |
Tejun Heo | 60f5d6e | 2010-08-23 11:27:27 +0200 | [diff] [blame] | 4862 | goto sys_err; |
Tejun Heo | f92a263 | 2007-12-05 16:43:10 +0900 | [diff] [blame] | 4863 | |
Tejun Heo | 405e66b | 2007-11-27 19:28:53 +0900 | [diff] [blame] | 4864 | if (ata_is_dma(prot) || (ata_is_pio(prot) && |
Tejun Heo | f92a263 | 2007-12-05 16:43:10 +0900 | [diff] [blame] | 4865 | (ap->flags & ATA_FLAG_PIO_DMA))) |
Tejun Heo | 001102d | 2007-12-05 16:43:09 +0900 | [diff] [blame] | 4866 | if (ata_sg_setup(qc)) |
Tejun Heo | 60f5d6e | 2010-08-23 11:27:27 +0200 | [diff] [blame] | 4867 | goto sys_err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4868 | |
Tejun Heo | cf48062 | 2008-01-24 00:05:14 +0900 | [diff] [blame] | 4869 | /* if device is sleeping, schedule reset and abort the link */ |
Tejun Heo | 054a5fb | 2007-10-25 18:30:36 +0900 | [diff] [blame] | 4870 | if (unlikely(qc->dev->flags & ATA_DFLAG_SLEEPING)) { |
Tejun Heo | cf48062 | 2008-01-24 00:05:14 +0900 | [diff] [blame] | 4871 | link->eh_info.action |= ATA_EH_RESET; |
Tejun Heo | 054a5fb | 2007-10-25 18:30:36 +0900 | [diff] [blame] | 4872 | ata_ehi_push_desc(&link->eh_info, "waking up from sleep"); |
| 4873 | ata_link_abort(link); |
| 4874 | return; |
| 4875 | } |
| 4876 | |
Hannes Reinecke | fc914fa | 2021-12-21 08:20:30 +0100 | [diff] [blame] | 4877 | trace_ata_qc_prep(qc); |
Jiri Slaby | 95364f3 | 2019-10-31 10:59:45 +0100 | [diff] [blame] | 4878 | qc->err_mask |= ap->ops->qc_prep(qc); |
| 4879 | if (unlikely(qc->err_mask)) |
| 4880 | goto err; |
Hannes Reinecke | 255c03d | 2015-03-27 16:46:38 +0100 | [diff] [blame] | 4881 | trace_ata_qc_issue(qc); |
Tejun Heo | 8e0e694 | 2006-03-31 20:41:11 +0900 | [diff] [blame] | 4882 | qc->err_mask |= ap->ops->qc_issue(qc); |
| 4883 | if (unlikely(qc->err_mask)) |
| 4884 | goto err; |
| 4885 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4886 | |
Tejun Heo | 60f5d6e | 2010-08-23 11:27:27 +0200 | [diff] [blame] | 4887 | sys_err: |
Tejun Heo | 8e0e694 | 2006-03-31 20:41:11 +0900 | [diff] [blame] | 4888 | qc->err_mask |= AC_ERR_SYSTEM; |
| 4889 | err: |
| 4890 | ata_qc_complete(qc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4891 | } |
| 4892 | |
| 4893 | /** |
Tejun Heo | b1c7291 | 2008-07-31 17:02:43 +0900 | [diff] [blame] | 4894 | * ata_phys_link_online - test whether the given link is online |
Tejun Heo | 936fd73 | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 4895 | * @link: ATA link to test |
Tejun Heo | 34bf217 | 2006-05-15 20:57:46 +0900 | [diff] [blame] | 4896 | * |
Tejun Heo | 936fd73 | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 4897 | * Test whether @link is online. Note that this function returns |
| 4898 | * 0 if online status of @link cannot be obtained, so |
| 4899 | * ata_link_online(link) != !ata_link_offline(link). |
Tejun Heo | 34bf217 | 2006-05-15 20:57:46 +0900 | [diff] [blame] | 4900 | * |
| 4901 | * LOCKING: |
| 4902 | * None. |
| 4903 | * |
| 4904 | * RETURNS: |
Tejun Heo | b5b3fa3 | 2008-07-31 17:02:42 +0900 | [diff] [blame] | 4905 | * True if the port online status is available and online. |
Tejun Heo | 34bf217 | 2006-05-15 20:57:46 +0900 | [diff] [blame] | 4906 | */ |
Tejun Heo | b1c7291 | 2008-07-31 17:02:43 +0900 | [diff] [blame] | 4907 | bool ata_phys_link_online(struct ata_link *link) |
Tejun Heo | 34bf217 | 2006-05-15 20:57:46 +0900 | [diff] [blame] | 4908 | { |
| 4909 | u32 sstatus; |
| 4910 | |
Tejun Heo | 936fd73 | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 4911 | if (sata_scr_read(link, SCR_STATUS, &sstatus) == 0 && |
Tejun Heo | 9913ff8a | 2009-01-29 20:31:31 +0900 | [diff] [blame] | 4912 | ata_sstatus_online(sstatus)) |
Tejun Heo | b5b3fa3 | 2008-07-31 17:02:42 +0900 | [diff] [blame] | 4913 | return true; |
| 4914 | return false; |
Tejun Heo | 34bf217 | 2006-05-15 20:57:46 +0900 | [diff] [blame] | 4915 | } |
| 4916 | |
| 4917 | /** |
Tejun Heo | b1c7291 | 2008-07-31 17:02:43 +0900 | [diff] [blame] | 4918 | * ata_phys_link_offline - test whether the given link is offline |
Tejun Heo | 936fd73 | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 4919 | * @link: ATA link to test |
Tejun Heo | 34bf217 | 2006-05-15 20:57:46 +0900 | [diff] [blame] | 4920 | * |
Tejun Heo | 936fd73 | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 4921 | * Test whether @link is offline. Note that this function |
| 4922 | * returns 0 if offline status of @link cannot be obtained, so |
| 4923 | * ata_link_online(link) != !ata_link_offline(link). |
Tejun Heo | 34bf217 | 2006-05-15 20:57:46 +0900 | [diff] [blame] | 4924 | * |
| 4925 | * LOCKING: |
| 4926 | * None. |
| 4927 | * |
| 4928 | * RETURNS: |
Tejun Heo | b5b3fa3 | 2008-07-31 17:02:42 +0900 | [diff] [blame] | 4929 | * True if the port offline status is available and offline. |
Tejun Heo | 34bf217 | 2006-05-15 20:57:46 +0900 | [diff] [blame] | 4930 | */ |
Tejun Heo | b1c7291 | 2008-07-31 17:02:43 +0900 | [diff] [blame] | 4931 | bool ata_phys_link_offline(struct ata_link *link) |
Tejun Heo | 34bf217 | 2006-05-15 20:57:46 +0900 | [diff] [blame] | 4932 | { |
| 4933 | u32 sstatus; |
| 4934 | |
Tejun Heo | 936fd73 | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 4935 | if (sata_scr_read(link, SCR_STATUS, &sstatus) == 0 && |
Tejun Heo | 9913ff8a | 2009-01-29 20:31:31 +0900 | [diff] [blame] | 4936 | !ata_sstatus_online(sstatus)) |
Tejun Heo | b5b3fa3 | 2008-07-31 17:02:42 +0900 | [diff] [blame] | 4937 | return true; |
| 4938 | return false; |
Tejun Heo | 34bf217 | 2006-05-15 20:57:46 +0900 | [diff] [blame] | 4939 | } |
Edward Falk | 0baab86 | 2005-06-02 18:17:13 -0400 | [diff] [blame] | 4940 | |
Tejun Heo | b1c7291 | 2008-07-31 17:02:43 +0900 | [diff] [blame] | 4941 | /** |
| 4942 | * ata_link_online - test whether the given link is online |
| 4943 | * @link: ATA link to test |
| 4944 | * |
| 4945 | * Test whether @link is online. This is identical to |
| 4946 | * ata_phys_link_online() when there's no slave link. When |
| 4947 | * there's a slave link, this function should only be called on |
| 4948 | * the master link and will return true if any of M/S links is |
| 4949 | * online. |
| 4950 | * |
| 4951 | * LOCKING: |
| 4952 | * None. |
| 4953 | * |
| 4954 | * RETURNS: |
| 4955 | * True if the port online status is available and online. |
| 4956 | */ |
| 4957 | bool ata_link_online(struct ata_link *link) |
| 4958 | { |
| 4959 | struct ata_link *slave = link->ap->slave_link; |
| 4960 | |
| 4961 | WARN_ON(link == slave); /* shouldn't be called on slave link */ |
| 4962 | |
| 4963 | return ata_phys_link_online(link) || |
| 4964 | (slave && ata_phys_link_online(slave)); |
| 4965 | } |
Bartlomiej Zolnierkiewicz | a52fbcf | 2020-03-26 16:58:04 +0100 | [diff] [blame] | 4966 | EXPORT_SYMBOL_GPL(ata_link_online); |
Tejun Heo | b1c7291 | 2008-07-31 17:02:43 +0900 | [diff] [blame] | 4967 | |
| 4968 | /** |
| 4969 | * ata_link_offline - test whether the given link is offline |
| 4970 | * @link: ATA link to test |
| 4971 | * |
| 4972 | * Test whether @link is offline. This is identical to |
| 4973 | * ata_phys_link_offline() when there's no slave link. When |
| 4974 | * there's a slave link, this function should only be called on |
| 4975 | * the master link and will return true if both M/S links are |
| 4976 | * offline. |
| 4977 | * |
| 4978 | * LOCKING: |
| 4979 | * None. |
| 4980 | * |
| 4981 | * RETURNS: |
| 4982 | * True if the port offline status is available and offline. |
| 4983 | */ |
| 4984 | bool ata_link_offline(struct ata_link *link) |
| 4985 | { |
| 4986 | struct ata_link *slave = link->ap->slave_link; |
| 4987 | |
| 4988 | WARN_ON(link == slave); /* shouldn't be called on slave link */ |
| 4989 | |
| 4990 | return ata_phys_link_offline(link) && |
| 4991 | (!slave || ata_phys_link_offline(slave)); |
| 4992 | } |
Bartlomiej Zolnierkiewicz | a52fbcf | 2020-03-26 16:58:04 +0100 | [diff] [blame] | 4993 | EXPORT_SYMBOL_GPL(ata_link_offline); |
Tejun Heo | b1c7291 | 2008-07-31 17:02:43 +0900 | [diff] [blame] | 4994 | |
Tejun Heo | 6ffa01d | 2007-03-02 17:32:47 +0900 | [diff] [blame] | 4995 | #ifdef CONFIG_PM |
Dan Williams | bc6e7c4 | 2014-03-14 13:52:48 -0700 | [diff] [blame] | 4996 | static void ata_port_request_pm(struct ata_port *ap, pm_message_t mesg, |
| 4997 | unsigned int action, unsigned int ehi_flags, |
| 4998 | bool async) |
Tejun Heo | 500530f | 2006-07-03 16:07:27 +0900 | [diff] [blame] | 4999 | { |
Lin Ming | 5ef4108 | 2011-12-05 09:20:27 +0800 | [diff] [blame] | 5000 | struct ata_link *link; |
Tejun Heo | 500530f | 2006-07-03 16:07:27 +0900 | [diff] [blame] | 5001 | unsigned long flags; |
Tejun Heo | 500530f | 2006-07-03 16:07:27 +0900 | [diff] [blame] | 5002 | |
Lin Ming | 5ef4108 | 2011-12-05 09:20:27 +0800 | [diff] [blame] | 5003 | /* Previous resume operation might still be in |
| 5004 | * progress. Wait for PM_PENDING to clear. |
| 5005 | */ |
| 5006 | if (ap->pflags & ATA_PFLAG_PM_PENDING) { |
| 5007 | ata_port_wait_eh(ap); |
| 5008 | WARN_ON(ap->pflags & ATA_PFLAG_PM_PENDING); |
Tejun Heo | 500530f | 2006-07-03 16:07:27 +0900 | [diff] [blame] | 5009 | } |
| 5010 | |
Lin Ming | 5ef4108 | 2011-12-05 09:20:27 +0800 | [diff] [blame] | 5011 | /* request PM ops to EH */ |
| 5012 | spin_lock_irqsave(ap->lock, flags); |
| 5013 | |
| 5014 | ap->pm_mesg = mesg; |
Lin Ming | 5ef4108 | 2011-12-05 09:20:27 +0800 | [diff] [blame] | 5015 | ap->pflags |= ATA_PFLAG_PM_PENDING; |
| 5016 | ata_for_each_link(link, ap, HOST_FIRST) { |
| 5017 | link->eh_info.action |= action; |
| 5018 | link->eh_info.flags |= ehi_flags; |
| 5019 | } |
| 5020 | |
| 5021 | ata_port_schedule_eh(ap); |
| 5022 | |
| 5023 | spin_unlock_irqrestore(ap->lock, flags); |
| 5024 | |
Dan Williams | 2fcbdcb | 2012-06-21 23:41:46 -0700 | [diff] [blame] | 5025 | if (!async) { |
Lin Ming | 5ef4108 | 2011-12-05 09:20:27 +0800 | [diff] [blame] | 5026 | ata_port_wait_eh(ap); |
| 5027 | WARN_ON(ap->pflags & ATA_PFLAG_PM_PENDING); |
| 5028 | } |
Tejun Heo | 500530f | 2006-07-03 16:07:27 +0900 | [diff] [blame] | 5029 | } |
| 5030 | |
Dan Williams | bc6e7c4 | 2014-03-14 13:52:48 -0700 | [diff] [blame] | 5031 | /* |
| 5032 | * On some hardware, device fails to respond after spun down for suspend. As |
| 5033 | * the device won't be used before being resumed, we don't need to touch the |
| 5034 | * device. Ask EH to skip the usual stuff and proceed directly to suspend. |
| 5035 | * |
| 5036 | * http://thread.gmane.org/gmane.linux.ide/46764 |
| 5037 | */ |
| 5038 | static const unsigned int ata_port_suspend_ehi = ATA_EHI_QUIET |
| 5039 | | ATA_EHI_NO_AUTOPSY |
| 5040 | | ATA_EHI_NO_RECOVERY; |
| 5041 | |
| 5042 | static void ata_port_suspend(struct ata_port *ap, pm_message_t mesg) |
Lin Ming | 5ef4108 | 2011-12-05 09:20:27 +0800 | [diff] [blame] | 5043 | { |
Dan Williams | bc6e7c4 | 2014-03-14 13:52:48 -0700 | [diff] [blame] | 5044 | ata_port_request_pm(ap, mesg, 0, ata_port_suspend_ehi, false); |
Lin Ming | 5ef4108 | 2011-12-05 09:20:27 +0800 | [diff] [blame] | 5045 | } |
| 5046 | |
Dan Williams | bc6e7c4 | 2014-03-14 13:52:48 -0700 | [diff] [blame] | 5047 | static void ata_port_suspend_async(struct ata_port *ap, pm_message_t mesg) |
| 5048 | { |
| 5049 | ata_port_request_pm(ap, mesg, 0, ata_port_suspend_ehi, true); |
| 5050 | } |
| 5051 | |
| 5052 | static int ata_port_pm_suspend(struct device *dev) |
Dan Williams | 2fcbdcb | 2012-06-21 23:41:46 -0700 | [diff] [blame] | 5053 | { |
| 5054 | struct ata_port *ap = to_ata_port(dev); |
| 5055 | |
Lin Ming | 5ef4108 | 2011-12-05 09:20:27 +0800 | [diff] [blame] | 5056 | if (pm_runtime_suspended(dev)) |
| 5057 | return 0; |
| 5058 | |
Dan Williams | bc6e7c4 | 2014-03-14 13:52:48 -0700 | [diff] [blame] | 5059 | ata_port_suspend(ap, PMSG_SUSPEND); |
| 5060 | return 0; |
Lin Ming | 33574d6 | 2011-12-22 14:50:49 +0800 | [diff] [blame] | 5061 | } |
| 5062 | |
Dan Williams | bc6e7c4 | 2014-03-14 13:52:48 -0700 | [diff] [blame] | 5063 | static int ata_port_pm_freeze(struct device *dev) |
Dan Williams | 2fcbdcb | 2012-06-21 23:41:46 -0700 | [diff] [blame] | 5064 | { |
| 5065 | struct ata_port *ap = to_ata_port(dev); |
| 5066 | |
Dan Williams | bc6e7c4 | 2014-03-14 13:52:48 -0700 | [diff] [blame] | 5067 | if (pm_runtime_suspended(dev)) |
| 5068 | return 0; |
| 5069 | |
| 5070 | ata_port_suspend(ap, PMSG_FREEZE); |
| 5071 | return 0; |
Dan Williams | 2fcbdcb | 2012-06-21 23:41:46 -0700 | [diff] [blame] | 5072 | } |
| 5073 | |
Dan Williams | bc6e7c4 | 2014-03-14 13:52:48 -0700 | [diff] [blame] | 5074 | static int ata_port_pm_poweroff(struct device *dev) |
Lin Ming | e90b1e5 | 2011-12-22 14:50:48 +0800 | [diff] [blame] | 5075 | { |
Dan Williams | bc6e7c4 | 2014-03-14 13:52:48 -0700 | [diff] [blame] | 5076 | ata_port_suspend(to_ata_port(dev), PMSG_HIBERNATE); |
| 5077 | return 0; |
| 5078 | } |
Lin Ming | e90b1e5 | 2011-12-22 14:50:48 +0800 | [diff] [blame] | 5079 | |
Dan Williams | bc6e7c4 | 2014-03-14 13:52:48 -0700 | [diff] [blame] | 5080 | static const unsigned int ata_port_resume_ehi = ATA_EHI_NO_AUTOPSY |
| 5081 | | ATA_EHI_QUIET; |
Lin Ming | e90b1e5 | 2011-12-22 14:50:48 +0800 | [diff] [blame] | 5082 | |
Dan Williams | bc6e7c4 | 2014-03-14 13:52:48 -0700 | [diff] [blame] | 5083 | static void ata_port_resume(struct ata_port *ap, pm_message_t mesg) |
| 5084 | { |
| 5085 | ata_port_request_pm(ap, mesg, ATA_EH_RESET, ata_port_resume_ehi, false); |
| 5086 | } |
| 5087 | |
| 5088 | static void ata_port_resume_async(struct ata_port *ap, pm_message_t mesg) |
| 5089 | { |
| 5090 | ata_port_request_pm(ap, mesg, ATA_EH_RESET, ata_port_resume_ehi, true); |
| 5091 | } |
| 5092 | |
| 5093 | static int ata_port_pm_resume(struct device *dev) |
| 5094 | { |
Todd Brandt | 200421a | 2014-03-14 13:52:54 -0700 | [diff] [blame] | 5095 | ata_port_resume_async(to_ata_port(dev), PMSG_RESUME); |
Dan Williams | bc6e7c4 | 2014-03-14 13:52:48 -0700 | [diff] [blame] | 5096 | pm_runtime_disable(dev); |
| 5097 | pm_runtime_set_active(dev); |
| 5098 | pm_runtime_enable(dev); |
| 5099 | return 0; |
Lin Ming | e90b1e5 | 2011-12-22 14:50:48 +0800 | [diff] [blame] | 5100 | } |
| 5101 | |
Aaron Lu | 7e15e9b | 2013-01-15 17:21:04 +0800 | [diff] [blame] | 5102 | /* |
| 5103 | * For ODDs, the upper layer will poll for media change every few seconds, |
| 5104 | * which will make it enter and leave suspend state every few seconds. And |
| 5105 | * as each suspend will cause a hard/soft reset, the gain of runtime suspend |
| 5106 | * is very little and the ODD may malfunction after constantly being reset. |
| 5107 | * So the idle callback here will not proceed to suspend if a non-ZPODD capable |
| 5108 | * ODD is attached to the port. |
| 5109 | */ |
Lin Ming | 9ee4f39 | 2011-12-05 09:20:28 +0800 | [diff] [blame] | 5110 | static int ata_port_runtime_idle(struct device *dev) |
| 5111 | { |
Aaron Lu | 7e15e9b | 2013-01-15 17:21:04 +0800 | [diff] [blame] | 5112 | struct ata_port *ap = to_ata_port(dev); |
| 5113 | struct ata_link *link; |
| 5114 | struct ata_device *adev; |
| 5115 | |
| 5116 | ata_for_each_link(link, ap, HOST_FIRST) { |
| 5117 | ata_for_each_dev(adev, link, ENABLED) |
| 5118 | if (adev->class == ATA_DEV_ATAPI && |
| 5119 | !zpodd_dev_enabled(adev)) |
| 5120 | return -EBUSY; |
| 5121 | } |
| 5122 | |
Rafael J. Wysocki | 45f0a85 | 2013-06-03 21:49:52 +0200 | [diff] [blame] | 5123 | return 0; |
Lin Ming | 9ee4f39 | 2011-12-05 09:20:28 +0800 | [diff] [blame] | 5124 | } |
| 5125 | |
Aaron Lu | a7ff60d | 2013-01-25 14:29:35 +0800 | [diff] [blame] | 5126 | static int ata_port_runtime_suspend(struct device *dev) |
| 5127 | { |
Dan Williams | bc6e7c4 | 2014-03-14 13:52:48 -0700 | [diff] [blame] | 5128 | ata_port_suspend(to_ata_port(dev), PMSG_AUTO_SUSPEND); |
| 5129 | return 0; |
Aaron Lu | a7ff60d | 2013-01-25 14:29:35 +0800 | [diff] [blame] | 5130 | } |
| 5131 | |
| 5132 | static int ata_port_runtime_resume(struct device *dev) |
| 5133 | { |
Dan Williams | bc6e7c4 | 2014-03-14 13:52:48 -0700 | [diff] [blame] | 5134 | ata_port_resume(to_ata_port(dev), PMSG_AUTO_RESUME); |
| 5135 | return 0; |
Aaron Lu | a7ff60d | 2013-01-25 14:29:35 +0800 | [diff] [blame] | 5136 | } |
| 5137 | |
Lin Ming | 5ef4108 | 2011-12-05 09:20:27 +0800 | [diff] [blame] | 5138 | static const struct dev_pm_ops ata_port_pm_ops = { |
Dan Williams | bc6e7c4 | 2014-03-14 13:52:48 -0700 | [diff] [blame] | 5139 | .suspend = ata_port_pm_suspend, |
| 5140 | .resume = ata_port_pm_resume, |
| 5141 | .freeze = ata_port_pm_freeze, |
| 5142 | .thaw = ata_port_pm_resume, |
| 5143 | .poweroff = ata_port_pm_poweroff, |
| 5144 | .restore = ata_port_pm_resume, |
Lin Ming | 9ee4f39 | 2011-12-05 09:20:28 +0800 | [diff] [blame] | 5145 | |
Aaron Lu | a7ff60d | 2013-01-25 14:29:35 +0800 | [diff] [blame] | 5146 | .runtime_suspend = ata_port_runtime_suspend, |
| 5147 | .runtime_resume = ata_port_runtime_resume, |
Lin Ming | 9ee4f39 | 2011-12-05 09:20:28 +0800 | [diff] [blame] | 5148 | .runtime_idle = ata_port_runtime_idle, |
Lin Ming | 5ef4108 | 2011-12-05 09:20:27 +0800 | [diff] [blame] | 5149 | }; |
| 5150 | |
Dan Williams | 2fcbdcb | 2012-06-21 23:41:46 -0700 | [diff] [blame] | 5151 | /* sas ports don't participate in pm runtime management of ata_ports, |
| 5152 | * and need to resume ata devices at the domain level, not the per-port |
| 5153 | * level. sas suspend/resume is async to allow parallel port recovery |
| 5154 | * since sas has multiple ata_port instances per Scsi_Host. |
| 5155 | */ |
Dan Williams | bc6e7c4 | 2014-03-14 13:52:48 -0700 | [diff] [blame] | 5156 | void ata_sas_port_suspend(struct ata_port *ap) |
Dan Williams | 2fcbdcb | 2012-06-21 23:41:46 -0700 | [diff] [blame] | 5157 | { |
Dan Williams | bc6e7c4 | 2014-03-14 13:52:48 -0700 | [diff] [blame] | 5158 | ata_port_suspend_async(ap, PMSG_SUSPEND); |
Dan Williams | 2fcbdcb | 2012-06-21 23:41:46 -0700 | [diff] [blame] | 5159 | } |
Dan Williams | bc6e7c4 | 2014-03-14 13:52:48 -0700 | [diff] [blame] | 5160 | EXPORT_SYMBOL_GPL(ata_sas_port_suspend); |
Dan Williams | 2fcbdcb | 2012-06-21 23:41:46 -0700 | [diff] [blame] | 5161 | |
Dan Williams | bc6e7c4 | 2014-03-14 13:52:48 -0700 | [diff] [blame] | 5162 | void ata_sas_port_resume(struct ata_port *ap) |
Dan Williams | 2fcbdcb | 2012-06-21 23:41:46 -0700 | [diff] [blame] | 5163 | { |
Dan Williams | bc6e7c4 | 2014-03-14 13:52:48 -0700 | [diff] [blame] | 5164 | ata_port_resume_async(ap, PMSG_RESUME); |
Dan Williams | 2fcbdcb | 2012-06-21 23:41:46 -0700 | [diff] [blame] | 5165 | } |
Dan Williams | bc6e7c4 | 2014-03-14 13:52:48 -0700 | [diff] [blame] | 5166 | EXPORT_SYMBOL_GPL(ata_sas_port_resume); |
Dan Williams | 2fcbdcb | 2012-06-21 23:41:46 -0700 | [diff] [blame] | 5167 | |
Tejun Heo | 500530f | 2006-07-03 16:07:27 +0900 | [diff] [blame] | 5168 | /** |
Jeff Garzik | cca3974 | 2006-08-24 03:19:22 -0400 | [diff] [blame] | 5169 | * ata_host_suspend - suspend host |
| 5170 | * @host: host to suspend |
Tejun Heo | 500530f | 2006-07-03 16:07:27 +0900 | [diff] [blame] | 5171 | * @mesg: PM message |
| 5172 | * |
Lin Ming | 5ef4108 | 2011-12-05 09:20:27 +0800 | [diff] [blame] | 5173 | * Suspend @host. Actual operation is performed by port suspend. |
Tejun Heo | 500530f | 2006-07-03 16:07:27 +0900 | [diff] [blame] | 5174 | */ |
Jeff Garzik | cca3974 | 2006-08-24 03:19:22 -0400 | [diff] [blame] | 5175 | int ata_host_suspend(struct ata_host *host, pm_message_t mesg) |
Tejun Heo | 500530f | 2006-07-03 16:07:27 +0900 | [diff] [blame] | 5176 | { |
Lin Ming | 5ef4108 | 2011-12-05 09:20:27 +0800 | [diff] [blame] | 5177 | host->dev->power.power_state = mesg; |
| 5178 | return 0; |
Tejun Heo | 500530f | 2006-07-03 16:07:27 +0900 | [diff] [blame] | 5179 | } |
Bartlomiej Zolnierkiewicz | a52fbcf | 2020-03-26 16:58:04 +0100 | [diff] [blame] | 5180 | EXPORT_SYMBOL_GPL(ata_host_suspend); |
Tejun Heo | 500530f | 2006-07-03 16:07:27 +0900 | [diff] [blame] | 5181 | |
| 5182 | /** |
Jeff Garzik | cca3974 | 2006-08-24 03:19:22 -0400 | [diff] [blame] | 5183 | * ata_host_resume - resume host |
| 5184 | * @host: host to resume |
Tejun Heo | 500530f | 2006-07-03 16:07:27 +0900 | [diff] [blame] | 5185 | * |
Lin Ming | 5ef4108 | 2011-12-05 09:20:27 +0800 | [diff] [blame] | 5186 | * Resume @host. Actual operation is performed by port resume. |
Tejun Heo | 500530f | 2006-07-03 16:07:27 +0900 | [diff] [blame] | 5187 | */ |
Jeff Garzik | cca3974 | 2006-08-24 03:19:22 -0400 | [diff] [blame] | 5188 | void ata_host_resume(struct ata_host *host) |
Tejun Heo | 500530f | 2006-07-03 16:07:27 +0900 | [diff] [blame] | 5189 | { |
Jeff Garzik | 72ad6ec | 2008-02-25 17:31:10 -0500 | [diff] [blame] | 5190 | host->dev->power.power_state = PMSG_ON; |
Tejun Heo | 500530f | 2006-07-03 16:07:27 +0900 | [diff] [blame] | 5191 | } |
Bartlomiej Zolnierkiewicz | a52fbcf | 2020-03-26 16:58:04 +0100 | [diff] [blame] | 5192 | EXPORT_SYMBOL_GPL(ata_host_resume); |
Tejun Heo | 6ffa01d | 2007-03-02 17:32:47 +0900 | [diff] [blame] | 5193 | #endif |
Tejun Heo | 500530f | 2006-07-03 16:07:27 +0900 | [diff] [blame] | 5194 | |
Bhumika Goyal | 8df82c1 | 2017-09-30 22:10:40 +0530 | [diff] [blame] | 5195 | const struct device_type ata_port_type = { |
Lin Ming | 5ef4108 | 2011-12-05 09:20:27 +0800 | [diff] [blame] | 5196 | .name = "ata_port", |
| 5197 | #ifdef CONFIG_PM |
| 5198 | .pm = &ata_port_pm_ops, |
| 5199 | #endif |
| 5200 | }; |
| 5201 | |
Randy Dunlap | c893a3a | 2006-01-28 13:15:32 -0500 | [diff] [blame] | 5202 | /** |
Tejun Heo | 3ef3b43 | 2006-05-31 18:27:30 +0900 | [diff] [blame] | 5203 | * ata_dev_init - Initialize an ata_device structure |
| 5204 | * @dev: Device structure to initialize |
| 5205 | * |
| 5206 | * Initialize @dev in preparation for probing. |
| 5207 | * |
| 5208 | * LOCKING: |
| 5209 | * Inherited from caller. |
| 5210 | */ |
| 5211 | void ata_dev_init(struct ata_device *dev) |
| 5212 | { |
Tejun Heo | b1c7291 | 2008-07-31 17:02:43 +0900 | [diff] [blame] | 5213 | struct ata_link *link = ata_dev_phys_link(dev); |
Tejun Heo | 9af5c9c | 2007-08-06 18:36:22 +0900 | [diff] [blame] | 5214 | struct ata_port *ap = link->ap; |
Tejun Heo | 72fa4b7 | 2006-05-31 18:27:32 +0900 | [diff] [blame] | 5215 | unsigned long flags; |
Tejun Heo | 3ef3b43 | 2006-05-31 18:27:30 +0900 | [diff] [blame] | 5216 | |
Tejun Heo | b1c7291 | 2008-07-31 17:02:43 +0900 | [diff] [blame] | 5217 | /* SATA spd limit is bound to the attached device, reset together */ |
Tejun Heo | 9af5c9c | 2007-08-06 18:36:22 +0900 | [diff] [blame] | 5218 | link->sata_spd_limit = link->hw_sata_spd_limit; |
| 5219 | link->sata_spd = 0; |
Tejun Heo | 5a04bf4 | 2006-05-31 18:27:38 +0900 | [diff] [blame] | 5220 | |
Tejun Heo | 72fa4b7 | 2006-05-31 18:27:32 +0900 | [diff] [blame] | 5221 | /* High bits of dev->flags are used to record warm plug |
| 5222 | * requests which occur asynchronously. Synchronize using |
Jeff Garzik | cca3974 | 2006-08-24 03:19:22 -0400 | [diff] [blame] | 5223 | * host lock. |
Tejun Heo | 72fa4b7 | 2006-05-31 18:27:32 +0900 | [diff] [blame] | 5224 | */ |
Jeff Garzik | ba6a130 | 2006-06-22 23:46:10 -0400 | [diff] [blame] | 5225 | spin_lock_irqsave(ap->lock, flags); |
Tejun Heo | 72fa4b7 | 2006-05-31 18:27:32 +0900 | [diff] [blame] | 5226 | dev->flags &= ~ATA_DFLAG_INIT_MASK; |
Tejun Heo | 3dcc323 | 2007-09-03 12:20:11 +0900 | [diff] [blame] | 5227 | dev->horkage = 0; |
Jeff Garzik | ba6a130 | 2006-06-22 23:46:10 -0400 | [diff] [blame] | 5228 | spin_unlock_irqrestore(ap->lock, flags); |
Tejun Heo | 72fa4b7 | 2006-05-31 18:27:32 +0900 | [diff] [blame] | 5229 | |
Tejun Heo | 99cf610 | 2009-01-29 20:31:32 +0900 | [diff] [blame] | 5230 | memset((void *)dev + ATA_DEVICE_CLEAR_BEGIN, 0, |
| 5231 | ATA_DEVICE_CLEAR_END - ATA_DEVICE_CLEAR_BEGIN); |
Tejun Heo | 3ef3b43 | 2006-05-31 18:27:30 +0900 | [diff] [blame] | 5232 | dev->pio_mask = UINT_MAX; |
| 5233 | dev->mwdma_mask = UINT_MAX; |
| 5234 | dev->udma_mask = UINT_MAX; |
| 5235 | } |
| 5236 | |
| 5237 | /** |
Tejun Heo | 4fb37a2 | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 5238 | * ata_link_init - Initialize an ata_link structure |
| 5239 | * @ap: ATA port link is attached to |
| 5240 | * @link: Link structure to initialize |
Tejun Heo | 8989805 | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 5241 | * @pmp: Port multiplier port number |
Tejun Heo | 4fb37a2 | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 5242 | * |
| 5243 | * Initialize @link. |
| 5244 | * |
| 5245 | * LOCKING: |
| 5246 | * Kernel thread context (may sleep) |
| 5247 | */ |
Tejun Heo | fb7fd61 | 2007-09-23 13:14:12 +0900 | [diff] [blame] | 5248 | void ata_link_init(struct ata_port *ap, struct ata_link *link, int pmp) |
Tejun Heo | 4fb37a2 | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 5249 | { |
| 5250 | int i; |
| 5251 | |
| 5252 | /* clear everything except for devices */ |
Gwendal Grignou | d902747 | 2010-05-25 12:31:38 -0700 | [diff] [blame] | 5253 | memset((void *)link + ATA_LINK_CLEAR_BEGIN, 0, |
| 5254 | ATA_LINK_CLEAR_END - ATA_LINK_CLEAR_BEGIN); |
Tejun Heo | 4fb37a2 | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 5255 | |
| 5256 | link->ap = ap; |
Tejun Heo | 8989805 | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 5257 | link->pmp = pmp; |
Tejun Heo | 4fb37a2 | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 5258 | link->active_tag = ATA_TAG_POISON; |
| 5259 | link->hw_sata_spd_limit = UINT_MAX; |
| 5260 | |
| 5261 | /* can't use iterator, ap isn't initialized yet */ |
| 5262 | for (i = 0; i < ATA_MAX_DEVICES; i++) { |
| 5263 | struct ata_device *dev = &link->device[i]; |
| 5264 | |
| 5265 | dev->link = link; |
| 5266 | dev->devno = dev - link->device; |
Tejun Heo | 110f66d | 2009-09-16 04:17:28 +0900 | [diff] [blame] | 5267 | #ifdef CONFIG_ATA_ACPI |
| 5268 | dev->gtf_filter = ata_acpi_gtf_filter; |
| 5269 | #endif |
Tejun Heo | 4fb37a2 | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 5270 | ata_dev_init(dev); |
| 5271 | } |
| 5272 | } |
| 5273 | |
| 5274 | /** |
| 5275 | * sata_link_init_spd - Initialize link->sata_spd_limit |
| 5276 | * @link: Link to configure sata_spd_limit for |
| 5277 | * |
Mauro Carvalho Chehab | a31a699 | 2020-04-14 18:48:55 +0200 | [diff] [blame] | 5278 | * Initialize ``link->[hw_]sata_spd_limit`` to the currently |
Tejun Heo | 4fb37a2 | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 5279 | * configured value. |
| 5280 | * |
| 5281 | * LOCKING: |
| 5282 | * Kernel thread context (may sleep). |
| 5283 | * |
| 5284 | * RETURNS: |
| 5285 | * 0 on success, -errno on failure. |
| 5286 | */ |
Tejun Heo | fb7fd61 | 2007-09-23 13:14:12 +0900 | [diff] [blame] | 5287 | int sata_link_init_spd(struct ata_link *link) |
Tejun Heo | 4fb37a2 | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 5288 | { |
Tejun Heo | 3326732 | 2008-02-13 09:15:09 +0900 | [diff] [blame] | 5289 | u8 spd; |
Tejun Heo | 4fb37a2 | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 5290 | int rc; |
| 5291 | |
Tejun Heo | d127ea7 | 2008-07-31 16:09:34 +0900 | [diff] [blame] | 5292 | rc = sata_scr_read(link, SCR_CONTROL, &link->saved_scontrol); |
Tejun Heo | 4fb37a2 | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 5293 | if (rc) |
| 5294 | return rc; |
| 5295 | |
Tejun Heo | d127ea7 | 2008-07-31 16:09:34 +0900 | [diff] [blame] | 5296 | spd = (link->saved_scontrol >> 4) & 0xf; |
Tejun Heo | 4fb37a2 | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 5297 | if (spd) |
| 5298 | link->hw_sata_spd_limit &= (1 << spd) - 1; |
| 5299 | |
Tejun Heo | 05944bd | 2008-08-13 20:19:09 +0900 | [diff] [blame] | 5300 | ata_force_link_limits(link); |
Tejun Heo | 3326732 | 2008-02-13 09:15:09 +0900 | [diff] [blame] | 5301 | |
Tejun Heo | 4fb37a2 | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 5302 | link->sata_spd_limit = link->hw_sata_spd_limit; |
| 5303 | |
| 5304 | return 0; |
| 5305 | } |
| 5306 | |
| 5307 | /** |
Tejun Heo | f318719 | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 5308 | * ata_port_alloc - allocate and initialize basic ATA port resources |
| 5309 | * @host: ATA host this allocated port belongs to |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5310 | * |
Tejun Heo | f318719 | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 5311 | * Allocate and initialize basic ATA port resources. |
| 5312 | * |
| 5313 | * RETURNS: |
| 5314 | * Allocate ATA port on success, NULL on failure. |
Jeff Garzik | 0cba632 | 2005-05-30 19:49:12 -0400 | [diff] [blame] | 5315 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5316 | * LOCKING: |
Tejun Heo | f318719 | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 5317 | * Inherited from calling layer (may sleep). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5318 | */ |
Tejun Heo | f318719 | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 5319 | struct ata_port *ata_port_alloc(struct ata_host *host) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5320 | { |
Tejun Heo | f318719 | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 5321 | struct ata_port *ap; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5322 | |
Tejun Heo | f318719 | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 5323 | ap = kzalloc(sizeof(*ap), GFP_KERNEL); |
| 5324 | if (!ap) |
| 5325 | return NULL; |
Jeff Garzik | 4fca377 | 2011-02-15 01:13:24 -0500 | [diff] [blame] | 5326 | |
Maxime Bizon | 7b3a24c5 | 2011-03-16 14:58:32 +0100 | [diff] [blame] | 5327 | ap->pflags |= ATA_PFLAG_INITIALIZING | ATA_PFLAG_FROZEN; |
Jeff Garzik | cca3974 | 2006-08-24 03:19:22 -0400 | [diff] [blame] | 5328 | ap->lock = &host->lock; |
Tejun Heo | f318719 | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 5329 | ap->print_id = -1; |
David Milburn | e628dc9 | 2013-05-14 13:48:40 -0500 | [diff] [blame] | 5330 | ap->local_port_no = -1; |
Jeff Garzik | cca3974 | 2006-08-24 03:19:22 -0400 | [diff] [blame] | 5331 | ap->host = host; |
Tejun Heo | f318719 | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 5332 | ap->dev = host->dev; |
Borislav Petkov | bd5d825 | 2006-06-11 23:17:01 -0400 | [diff] [blame] | 5333 | |
| 5334 | #if defined(ATA_VERBOSE_DEBUG) |
| 5335 | /* turn on all debugging levels */ |
Hannes Reinecke | 96c810f | 2021-12-21 08:21:06 +0100 | [diff] [blame^] | 5336 | ap->msg_enable = 0x0001; |
Borislav Petkov | bd5d825 | 2006-06-11 23:17:01 -0400 | [diff] [blame] | 5337 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5338 | |
Tejun Heo | ad72cf9 | 2010-07-02 10:03:52 +0200 | [diff] [blame] | 5339 | mutex_init(&ap->scsi_scan_mutex); |
David Howells | 65f27f3 | 2006-11-22 14:55:48 +0000 | [diff] [blame] | 5340 | INIT_DELAYED_WORK(&ap->hotplug_task, ata_scsi_hotplug); |
| 5341 | INIT_WORK(&ap->scsi_rescan_task, ata_scsi_dev_rescan); |
Tejun Heo | a72ec4c | 2006-01-23 13:09:37 +0900 | [diff] [blame] | 5342 | INIT_LIST_HEAD(&ap->eh_done_q); |
Tejun Heo | c6cf9e9 | 2006-05-31 18:27:27 +0900 | [diff] [blame] | 5343 | init_waitqueue_head(&ap->eh_wait_q); |
Elias Oltmanns | 45fabbb | 2008-09-21 11:54:08 +0200 | [diff] [blame] | 5344 | init_completion(&ap->park_req_pending); |
Kees Cook | b93ab33 | 2017-10-16 14:56:42 -0700 | [diff] [blame] | 5345 | timer_setup(&ap->fastdrain_timer, ata_eh_fastdrain_timerfn, |
| 5346 | TIMER_DEFERRABLE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5347 | |
Tejun Heo | 838df62 | 2006-05-15 20:57:44 +0900 | [diff] [blame] | 5348 | ap->cbl = ATA_CBL_NONE; |
Tejun Heo | 838df62 | 2006-05-15 20:57:44 +0900 | [diff] [blame] | 5349 | |
Tejun Heo | 8989805 | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 5350 | ata_link_init(ap, &ap->link, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5351 | |
| 5352 | #ifdef ATA_IRQ_TRAP |
| 5353 | ap->stats.unhandled_irq = 1; |
| 5354 | ap->stats.idle_irq = 1; |
| 5355 | #endif |
Tejun Heo | 270390e | 2010-05-10 21:41:35 +0200 | [diff] [blame] | 5356 | ata_sff_port_init(ap); |
| 5357 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5358 | return ap; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5359 | } |
| 5360 | |
Taras Kondratiuk | 2623c7a | 2018-03-09 08:34:41 +0000 | [diff] [blame] | 5361 | static void ata_devres_release(struct device *gendev, void *res) |
Tejun Heo | f0d36ef | 2007-01-20 16:00:28 +0900 | [diff] [blame] | 5362 | { |
| 5363 | struct ata_host *host = dev_get_drvdata(gendev); |
| 5364 | int i; |
| 5365 | |
| 5366 | for (i = 0; i < host->n_ports; i++) { |
| 5367 | struct ata_port *ap = host->ports[i]; |
| 5368 | |
Tejun Heo | ecef725 | 2007-04-17 23:44:06 +0900 | [diff] [blame] | 5369 | if (!ap) |
| 5370 | continue; |
| 5371 | |
Tejun Heo | 4911487 | 2007-04-17 23:44:06 +0900 | [diff] [blame] | 5372 | if (ap->scsi_host) |
Tejun Heo | 1aa506e4 | 2007-03-09 19:36:12 +0900 | [diff] [blame] | 5373 | scsi_host_put(ap->scsi_host); |
| 5374 | |
Taras Kondratiuk | 2623c7a | 2018-03-09 08:34:41 +0000 | [diff] [blame] | 5375 | } |
| 5376 | |
| 5377 | dev_set_drvdata(gendev, NULL); |
| 5378 | ata_host_put(host); |
| 5379 | } |
| 5380 | |
| 5381 | static void ata_host_release(struct kref *kref) |
| 5382 | { |
| 5383 | struct ata_host *host = container_of(kref, struct ata_host, kref); |
| 5384 | int i; |
| 5385 | |
| 5386 | for (i = 0; i < host->n_ports; i++) { |
| 5387 | struct ata_port *ap = host->ports[i]; |
| 5388 | |
Tejun Heo | 633273a | 2007-09-23 13:19:54 +0900 | [diff] [blame] | 5389 | kfree(ap->pmp_link); |
Tejun Heo | b1c7291 | 2008-07-31 17:02:43 +0900 | [diff] [blame] | 5390 | kfree(ap->slave_link); |
Tejun Heo | 4911487 | 2007-04-17 23:44:06 +0900 | [diff] [blame] | 5391 | kfree(ap); |
Tejun Heo | 1aa506e4 | 2007-03-09 19:36:12 +0900 | [diff] [blame] | 5392 | host->ports[i] = NULL; |
| 5393 | } |
Taras Kondratiuk | 2623c7a | 2018-03-09 08:34:41 +0000 | [diff] [blame] | 5394 | kfree(host); |
| 5395 | } |
Tejun Heo | 1aa506e4 | 2007-03-09 19:36:12 +0900 | [diff] [blame] | 5396 | |
Taras Kondratiuk | 2623c7a | 2018-03-09 08:34:41 +0000 | [diff] [blame] | 5397 | void ata_host_get(struct ata_host *host) |
| 5398 | { |
| 5399 | kref_get(&host->kref); |
| 5400 | } |
| 5401 | |
| 5402 | void ata_host_put(struct ata_host *host) |
| 5403 | { |
| 5404 | kref_put(&host->kref, ata_host_release); |
Tejun Heo | f0d36ef | 2007-01-20 16:00:28 +0900 | [diff] [blame] | 5405 | } |
Bartlomiej Zolnierkiewicz | a52fbcf | 2020-03-26 16:58:04 +0100 | [diff] [blame] | 5406 | EXPORT_SYMBOL_GPL(ata_host_put); |
Tejun Heo | f0d36ef | 2007-01-20 16:00:28 +0900 | [diff] [blame] | 5407 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5408 | /** |
Tejun Heo | f318719 | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 5409 | * ata_host_alloc - allocate and init basic ATA host resources |
| 5410 | * @dev: generic device this host is associated with |
| 5411 | * @max_ports: maximum number of ATA ports associated with this host |
| 5412 | * |
| 5413 | * Allocate and initialize basic ATA host resources. LLD calls |
| 5414 | * this function to allocate a host, initializes it fully and |
| 5415 | * attaches it using ata_host_register(). |
| 5416 | * |
| 5417 | * @max_ports ports are allocated and host->n_ports is |
| 5418 | * initialized to @max_ports. The caller is allowed to decrease |
| 5419 | * host->n_ports before calling ata_host_register(). The unused |
| 5420 | * ports will be automatically freed on registration. |
| 5421 | * |
| 5422 | * RETURNS: |
| 5423 | * Allocate ATA host on success, NULL on failure. |
| 5424 | * |
| 5425 | * LOCKING: |
| 5426 | * Inherited from calling layer (may sleep). |
| 5427 | */ |
| 5428 | struct ata_host *ata_host_alloc(struct device *dev, int max_ports) |
| 5429 | { |
| 5430 | struct ata_host *host; |
| 5431 | size_t sz; |
| 5432 | int i; |
Taras Kondratiuk | 2623c7a | 2018-03-09 08:34:41 +0000 | [diff] [blame] | 5433 | void *dr; |
Tejun Heo | f318719 | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 5434 | |
Tejun Heo | f318719 | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 5435 | /* alloc a container for our list of ATA ports (buses) */ |
| 5436 | sz = sizeof(struct ata_host) + (max_ports + 1) * sizeof(void *); |
Taras Kondratiuk | 2623c7a | 2018-03-09 08:34:41 +0000 | [diff] [blame] | 5437 | host = kzalloc(sz, GFP_KERNEL); |
Tejun Heo | f318719 | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 5438 | if (!host) |
Taras Kondratiuk | 2623c7a | 2018-03-09 08:34:41 +0000 | [diff] [blame] | 5439 | return NULL; |
| 5440 | |
Tejun Heo | f318719 | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 5441 | if (!devres_open_group(dev, NULL, GFP_KERNEL)) |
Colin Ian King | dafd6c4 | 2018-03-27 14:26:01 +0100 | [diff] [blame] | 5442 | goto err_free; |
Tejun Heo | f318719 | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 5443 | |
Taras Kondratiuk | 2623c7a | 2018-03-09 08:34:41 +0000 | [diff] [blame] | 5444 | dr = devres_alloc(ata_devres_release, 0, GFP_KERNEL); |
| 5445 | if (!dr) |
Tejun Heo | f318719 | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 5446 | goto err_out; |
| 5447 | |
Taras Kondratiuk | 2623c7a | 2018-03-09 08:34:41 +0000 | [diff] [blame] | 5448 | devres_add(dev, dr); |
Tejun Heo | f318719 | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 5449 | dev_set_drvdata(dev, host); |
| 5450 | |
| 5451 | spin_lock_init(&host->lock); |
Tejun Heo | c0c362b | 2010-09-06 17:57:14 +0200 | [diff] [blame] | 5452 | mutex_init(&host->eh_mutex); |
Tejun Heo | f318719 | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 5453 | host->dev = dev; |
| 5454 | host->n_ports = max_ports; |
Taras Kondratiuk | 2623c7a | 2018-03-09 08:34:41 +0000 | [diff] [blame] | 5455 | kref_init(&host->kref); |
Tejun Heo | f318719 | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 5456 | |
| 5457 | /* allocate ports bound to this host */ |
| 5458 | for (i = 0; i < max_ports; i++) { |
| 5459 | struct ata_port *ap; |
| 5460 | |
| 5461 | ap = ata_port_alloc(host); |
| 5462 | if (!ap) |
| 5463 | goto err_out; |
| 5464 | |
| 5465 | ap->port_no = i; |
| 5466 | host->ports[i] = ap; |
| 5467 | } |
| 5468 | |
| 5469 | devres_remove_group(dev, NULL); |
| 5470 | return host; |
| 5471 | |
| 5472 | err_out: |
| 5473 | devres_release_group(dev, NULL); |
Colin Ian King | dafd6c4 | 2018-03-27 14:26:01 +0100 | [diff] [blame] | 5474 | err_free: |
| 5475 | kfree(host); |
Tejun Heo | f318719 | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 5476 | return NULL; |
| 5477 | } |
Bartlomiej Zolnierkiewicz | a52fbcf | 2020-03-26 16:58:04 +0100 | [diff] [blame] | 5478 | EXPORT_SYMBOL_GPL(ata_host_alloc); |
Tejun Heo | f318719 | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 5479 | |
| 5480 | /** |
Tejun Heo | f5cda25 | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 5481 | * ata_host_alloc_pinfo - alloc host and init with port_info array |
| 5482 | * @dev: generic device this host is associated with |
| 5483 | * @ppi: array of ATA port_info to initialize host with |
| 5484 | * @n_ports: number of ATA ports attached to this host |
| 5485 | * |
| 5486 | * Allocate ATA host and initialize with info from @ppi. If NULL |
| 5487 | * terminated, @ppi may contain fewer entries than @n_ports. The |
| 5488 | * last entry will be used for the remaining ports. |
| 5489 | * |
| 5490 | * RETURNS: |
| 5491 | * Allocate ATA host on success, NULL on failure. |
| 5492 | * |
| 5493 | * LOCKING: |
| 5494 | * Inherited from calling layer (may sleep). |
| 5495 | */ |
| 5496 | struct ata_host *ata_host_alloc_pinfo(struct device *dev, |
| 5497 | const struct ata_port_info * const * ppi, |
| 5498 | int n_ports) |
| 5499 | { |
| 5500 | const struct ata_port_info *pi; |
| 5501 | struct ata_host *host; |
| 5502 | int i, j; |
| 5503 | |
| 5504 | host = ata_host_alloc(dev, n_ports); |
| 5505 | if (!host) |
| 5506 | return NULL; |
| 5507 | |
| 5508 | for (i = 0, j = 0, pi = NULL; i < host->n_ports; i++) { |
| 5509 | struct ata_port *ap = host->ports[i]; |
| 5510 | |
| 5511 | if (ppi[j]) |
| 5512 | pi = ppi[j++]; |
| 5513 | |
| 5514 | ap->pio_mask = pi->pio_mask; |
| 5515 | ap->mwdma_mask = pi->mwdma_mask; |
| 5516 | ap->udma_mask = pi->udma_mask; |
| 5517 | ap->flags |= pi->flags; |
Tejun Heo | 0c88758 | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 5518 | ap->link.flags |= pi->link_flags; |
Tejun Heo | f5cda25 | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 5519 | ap->ops = pi->port_ops; |
| 5520 | |
| 5521 | if (!host->ops && (pi->port_ops != &ata_dummy_port_ops)) |
| 5522 | host->ops = pi->port_ops; |
Tejun Heo | f5cda25 | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 5523 | } |
| 5524 | |
| 5525 | return host; |
| 5526 | } |
Bartlomiej Zolnierkiewicz | a52fbcf | 2020-03-26 16:58:04 +0100 | [diff] [blame] | 5527 | EXPORT_SYMBOL_GPL(ata_host_alloc_pinfo); |
Tejun Heo | f5cda25 | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 5528 | |
Tejun Heo | 32ebbc0 | 2007-11-08 13:09:00 +0900 | [diff] [blame] | 5529 | static void ata_host_stop(struct device *gendev, void *res) |
| 5530 | { |
| 5531 | struct ata_host *host = dev_get_drvdata(gendev); |
| 5532 | int i; |
| 5533 | |
| 5534 | WARN_ON(!(host->flags & ATA_HOST_STARTED)); |
| 5535 | |
| 5536 | for (i = 0; i < host->n_ports; i++) { |
| 5537 | struct ata_port *ap = host->ports[i]; |
| 5538 | |
| 5539 | if (ap->ops->port_stop) |
| 5540 | ap->ops->port_stop(ap); |
| 5541 | } |
| 5542 | |
| 5543 | if (host->ops->host_stop) |
| 5544 | host->ops->host_stop(host); |
| 5545 | } |
| 5546 | |
Tejun Heo | f5cda25 | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 5547 | /** |
Tejun Heo | 029cfd6 | 2008-03-25 12:22:49 +0900 | [diff] [blame] | 5548 | * ata_finalize_port_ops - finalize ata_port_operations |
| 5549 | * @ops: ata_port_operations to finalize |
| 5550 | * |
| 5551 | * An ata_port_operations can inherit from another ops and that |
| 5552 | * ops can again inherit from another. This can go on as many |
| 5553 | * times as necessary as long as there is no loop in the |
| 5554 | * inheritance chain. |
| 5555 | * |
| 5556 | * Ops tables are finalized when the host is started. NULL or |
| 5557 | * unspecified entries are inherited from the closet ancestor |
| 5558 | * which has the method and the entry is populated with it. |
| 5559 | * After finalization, the ops table directly points to all the |
| 5560 | * methods and ->inherits is no longer necessary and cleared. |
| 5561 | * |
| 5562 | * Using ATA_OP_NULL, inheriting ops can force a method to NULL. |
| 5563 | * |
| 5564 | * LOCKING: |
| 5565 | * None. |
| 5566 | */ |
| 5567 | static void ata_finalize_port_ops(struct ata_port_operations *ops) |
| 5568 | { |
Pradeep Singh Rautela | 2da6765 | 2008-05-29 23:28:14 +0530 | [diff] [blame] | 5569 | static DEFINE_SPINLOCK(lock); |
Tejun Heo | 029cfd6 | 2008-03-25 12:22:49 +0900 | [diff] [blame] | 5570 | const struct ata_port_operations *cur; |
| 5571 | void **begin = (void **)ops; |
| 5572 | void **end = (void **)&ops->inherits; |
| 5573 | void **pp; |
| 5574 | |
| 5575 | if (!ops || !ops->inherits) |
| 5576 | return; |
| 5577 | |
| 5578 | spin_lock(&lock); |
| 5579 | |
| 5580 | for (cur = ops->inherits; cur; cur = cur->inherits) { |
| 5581 | void **inherit = (void **)cur; |
| 5582 | |
| 5583 | for (pp = begin; pp < end; pp++, inherit++) |
| 5584 | if (!*pp) |
| 5585 | *pp = *inherit; |
| 5586 | } |
| 5587 | |
| 5588 | for (pp = begin; pp < end; pp++) |
| 5589 | if (IS_ERR(*pp)) |
| 5590 | *pp = NULL; |
| 5591 | |
| 5592 | ops->inherits = NULL; |
| 5593 | |
| 5594 | spin_unlock(&lock); |
| 5595 | } |
| 5596 | |
| 5597 | /** |
Tejun Heo | ecef725 | 2007-04-17 23:44:06 +0900 | [diff] [blame] | 5598 | * ata_host_start - start and freeze ports of an ATA host |
| 5599 | * @host: ATA host to start ports for |
| 5600 | * |
| 5601 | * Start and then freeze ports of @host. Started status is |
| 5602 | * recorded in host->flags, so this function can be called |
| 5603 | * multiple times. Ports are guaranteed to get started only |
Tejun Heo | f318719 | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 5604 | * once. If host->ops isn't initialized yet, its set to the |
| 5605 | * first non-dummy port ops. |
Tejun Heo | ecef725 | 2007-04-17 23:44:06 +0900 | [diff] [blame] | 5606 | * |
| 5607 | * LOCKING: |
| 5608 | * Inherited from calling layer (may sleep). |
| 5609 | * |
| 5610 | * RETURNS: |
| 5611 | * 0 if all ports are started successfully, -errno otherwise. |
| 5612 | */ |
| 5613 | int ata_host_start(struct ata_host *host) |
| 5614 | { |
Tejun Heo | 32ebbc0 | 2007-11-08 13:09:00 +0900 | [diff] [blame] | 5615 | int have_stop = 0; |
| 5616 | void *start_dr = NULL; |
Tejun Heo | ecef725 | 2007-04-17 23:44:06 +0900 | [diff] [blame] | 5617 | int i, rc; |
| 5618 | |
| 5619 | if (host->flags & ATA_HOST_STARTED) |
| 5620 | return 0; |
| 5621 | |
Tejun Heo | 029cfd6 | 2008-03-25 12:22:49 +0900 | [diff] [blame] | 5622 | ata_finalize_port_ops(host->ops); |
| 5623 | |
Tejun Heo | ecef725 | 2007-04-17 23:44:06 +0900 | [diff] [blame] | 5624 | for (i = 0; i < host->n_ports; i++) { |
| 5625 | struct ata_port *ap = host->ports[i]; |
| 5626 | |
Tejun Heo | 029cfd6 | 2008-03-25 12:22:49 +0900 | [diff] [blame] | 5627 | ata_finalize_port_ops(ap->ops); |
| 5628 | |
Tejun Heo | f318719 | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 5629 | if (!host->ops && !ata_port_is_dummy(ap)) |
| 5630 | host->ops = ap->ops; |
| 5631 | |
Tejun Heo | 32ebbc0 | 2007-11-08 13:09:00 +0900 | [diff] [blame] | 5632 | if (ap->ops->port_stop) |
| 5633 | have_stop = 1; |
| 5634 | } |
| 5635 | |
Damien Le Moal | 355a803 | 2021-08-16 10:44:47 +0900 | [diff] [blame] | 5636 | if (host->ops && host->ops->host_stop) |
Tejun Heo | 32ebbc0 | 2007-11-08 13:09:00 +0900 | [diff] [blame] | 5637 | have_stop = 1; |
| 5638 | |
| 5639 | if (have_stop) { |
| 5640 | start_dr = devres_alloc(ata_host_stop, 0, GFP_KERNEL); |
| 5641 | if (!start_dr) |
| 5642 | return -ENOMEM; |
| 5643 | } |
| 5644 | |
| 5645 | for (i = 0; i < host->n_ports; i++) { |
| 5646 | struct ata_port *ap = host->ports[i]; |
| 5647 | |
Tejun Heo | ecef725 | 2007-04-17 23:44:06 +0900 | [diff] [blame] | 5648 | if (ap->ops->port_start) { |
| 5649 | rc = ap->ops->port_start(ap); |
| 5650 | if (rc) { |
Alan Cox | 0f9fe9b | 2007-11-30 15:23:16 +0000 | [diff] [blame] | 5651 | if (rc != -ENODEV) |
Joe Perches | a44fec1 | 2011-04-15 15:51:58 -0700 | [diff] [blame] | 5652 | dev_err(host->dev, |
| 5653 | "failed to start port %d (errno=%d)\n", |
| 5654 | i, rc); |
Tejun Heo | ecef725 | 2007-04-17 23:44:06 +0900 | [diff] [blame] | 5655 | goto err_out; |
| 5656 | } |
| 5657 | } |
Tejun Heo | ecef725 | 2007-04-17 23:44:06 +0900 | [diff] [blame] | 5658 | ata_eh_freeze_port(ap); |
| 5659 | } |
| 5660 | |
Tejun Heo | 32ebbc0 | 2007-11-08 13:09:00 +0900 | [diff] [blame] | 5661 | if (start_dr) |
| 5662 | devres_add(host->dev, start_dr); |
Tejun Heo | ecef725 | 2007-04-17 23:44:06 +0900 | [diff] [blame] | 5663 | host->flags |= ATA_HOST_STARTED; |
| 5664 | return 0; |
| 5665 | |
| 5666 | err_out: |
| 5667 | while (--i >= 0) { |
| 5668 | struct ata_port *ap = host->ports[i]; |
| 5669 | |
| 5670 | if (ap->ops->port_stop) |
| 5671 | ap->ops->port_stop(ap); |
| 5672 | } |
Tejun Heo | 32ebbc0 | 2007-11-08 13:09:00 +0900 | [diff] [blame] | 5673 | devres_free(start_dr); |
Tejun Heo | ecef725 | 2007-04-17 23:44:06 +0900 | [diff] [blame] | 5674 | return rc; |
| 5675 | } |
Bartlomiej Zolnierkiewicz | a52fbcf | 2020-03-26 16:58:04 +0100 | [diff] [blame] | 5676 | EXPORT_SYMBOL_GPL(ata_host_start); |
Tejun Heo | ecef725 | 2007-04-17 23:44:06 +0900 | [diff] [blame] | 5677 | |
| 5678 | /** |
Mauro Carvalho Chehab | 94bd571 | 2020-10-23 18:32:55 +0200 | [diff] [blame] | 5679 | * ata_host_init - Initialize a host struct for sas (ipr, libsas) |
Jeff Garzik | cca3974 | 2006-08-24 03:19:22 -0400 | [diff] [blame] | 5680 | * @host: host to initialize |
| 5681 | * @dev: device host is attached to |
Jeff Garzik | cca3974 | 2006-08-24 03:19:22 -0400 | [diff] [blame] | 5682 | * @ops: port_ops |
Brian King | b03732f | 2006-08-07 14:27:10 -0500 | [diff] [blame] | 5683 | * |
Brian King | b03732f | 2006-08-07 14:27:10 -0500 | [diff] [blame] | 5684 | */ |
Jeff Garzik | cca3974 | 2006-08-24 03:19:22 -0400 | [diff] [blame] | 5685 | void ata_host_init(struct ata_host *host, struct device *dev, |
Dan Williams | 8d8e7d1 | 2012-07-09 21:06:08 -0700 | [diff] [blame] | 5686 | struct ata_port_operations *ops) |
Brian King | b03732f | 2006-08-07 14:27:10 -0500 | [diff] [blame] | 5687 | { |
Jeff Garzik | cca3974 | 2006-08-24 03:19:22 -0400 | [diff] [blame] | 5688 | spin_lock_init(&host->lock); |
Tejun Heo | c0c362b | 2010-09-06 17:57:14 +0200 | [diff] [blame] | 5689 | mutex_init(&host->eh_mutex); |
Jens Axboe | 69278f7 | 2018-05-11 12:51:10 -0600 | [diff] [blame] | 5690 | host->n_tags = ATA_MAX_QUEUE; |
Jeff Garzik | cca3974 | 2006-08-24 03:19:22 -0400 | [diff] [blame] | 5691 | host->dev = dev; |
Jeff Garzik | cca3974 | 2006-08-24 03:19:22 -0400 | [diff] [blame] | 5692 | host->ops = ops; |
Jason Yan | 2fa4a32 | 2018-05-10 11:05:16 +0800 | [diff] [blame] | 5693 | kref_init(&host->kref); |
Brian King | b03732f | 2006-08-07 14:27:10 -0500 | [diff] [blame] | 5694 | } |
Bartlomiej Zolnierkiewicz | a52fbcf | 2020-03-26 16:58:04 +0100 | [diff] [blame] | 5695 | EXPORT_SYMBOL_GPL(ata_host_init); |
Brian King | b03732f | 2006-08-07 14:27:10 -0500 | [diff] [blame] | 5696 | |
Dan Williams | 9508a66 | 2012-01-18 20:47:01 -0800 | [diff] [blame] | 5697 | void __ata_port_probe(struct ata_port *ap) |
| 5698 | { |
| 5699 | struct ata_eh_info *ehi = &ap->link.eh_info; |
| 5700 | unsigned long flags; |
| 5701 | |
| 5702 | /* kick EH for boot probing */ |
| 5703 | spin_lock_irqsave(ap->lock, flags); |
| 5704 | |
| 5705 | ehi->probe_mask |= ATA_ALL_DEVICES; |
| 5706 | ehi->action |= ATA_EH_RESET; |
| 5707 | ehi->flags |= ATA_EHI_NO_AUTOPSY | ATA_EHI_QUIET; |
| 5708 | |
| 5709 | ap->pflags &= ~ATA_PFLAG_INITIALIZING; |
| 5710 | ap->pflags |= ATA_PFLAG_LOADING; |
| 5711 | ata_port_schedule_eh(ap); |
| 5712 | |
| 5713 | spin_unlock_irqrestore(ap->lock, flags); |
| 5714 | } |
| 5715 | |
James Bottomley | 238c9cf | 2011-01-23 08:28:33 -0600 | [diff] [blame] | 5716 | int ata_port_probe(struct ata_port *ap) |
Arjan van de Ven | 7931805 | 2009-01-04 05:32:28 -0800 | [diff] [blame] | 5717 | { |
James Bottomley | 238c9cf | 2011-01-23 08:28:33 -0600 | [diff] [blame] | 5718 | int rc = 0; |
Arjan van de Ven | 886ad09 | 2009-01-09 15:54:07 -0800 | [diff] [blame] | 5719 | |
Arjan van de Ven | 7931805 | 2009-01-04 05:32:28 -0800 | [diff] [blame] | 5720 | if (ap->ops->error_handler) { |
Dan Williams | 9508a66 | 2012-01-18 20:47:01 -0800 | [diff] [blame] | 5721 | __ata_port_probe(ap); |
Arjan van de Ven | 7931805 | 2009-01-04 05:32:28 -0800 | [diff] [blame] | 5722 | ata_port_wait_eh(ap); |
| 5723 | } else { |
Arjan van de Ven | 7931805 | 2009-01-04 05:32:28 -0800 | [diff] [blame] | 5724 | rc = ata_bus_probe(ap); |
Arjan van de Ven | 7931805 | 2009-01-04 05:32:28 -0800 | [diff] [blame] | 5725 | } |
James Bottomley | 238c9cf | 2011-01-23 08:28:33 -0600 | [diff] [blame] | 5726 | return rc; |
| 5727 | } |
| 5728 | |
| 5729 | |
| 5730 | static void async_port_probe(void *data, async_cookie_t cookie) |
| 5731 | { |
| 5732 | struct ata_port *ap = data; |
Jeff Garzik | 4fca377 | 2011-02-15 01:13:24 -0500 | [diff] [blame] | 5733 | |
James Bottomley | 238c9cf | 2011-01-23 08:28:33 -0600 | [diff] [blame] | 5734 | /* |
| 5735 | * If we're not allowed to scan this host in parallel, |
| 5736 | * we need to wait until all previous scans have completed |
| 5737 | * before going further. |
| 5738 | * Jeff Garzik says this is only within a controller, so we |
| 5739 | * don't need to wait for port 0, only for later ports. |
| 5740 | */ |
| 5741 | if (!(ap->host->flags & ATA_HOST_PARALLEL_SCAN) && ap->port_no != 0) |
| 5742 | async_synchronize_cookie(cookie); |
| 5743 | |
| 5744 | (void)ata_port_probe(ap); |
Arjan van de Ven | f29d3b2 | 2009-01-05 15:07:07 -0800 | [diff] [blame] | 5745 | |
| 5746 | /* in order to keep device order, we need to synchronize at this point */ |
| 5747 | async_synchronize_cookie(cookie); |
| 5748 | |
| 5749 | ata_scsi_scan_host(ap, 1); |
Arjan van de Ven | 7931805 | 2009-01-04 05:32:28 -0800 | [diff] [blame] | 5750 | } |
James Bottomley | 238c9cf | 2011-01-23 08:28:33 -0600 | [diff] [blame] | 5751 | |
Brian King | b03732f | 2006-08-07 14:27:10 -0500 | [diff] [blame] | 5752 | /** |
Tejun Heo | f318719 | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 5753 | * ata_host_register - register initialized ATA host |
| 5754 | * @host: ATA host to register |
| 5755 | * @sht: template for SCSI host |
Jeff Garzik | 0cba632 | 2005-05-30 19:49:12 -0400 | [diff] [blame] | 5756 | * |
Tejun Heo | f318719 | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 5757 | * Register initialized ATA host. @host is allocated using |
| 5758 | * ata_host_alloc() and fully initialized by LLD. This function |
| 5759 | * starts ports, registers @host with ATA and SCSI layers and |
| 5760 | * probe registered devices. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5761 | * |
| 5762 | * LOCKING: |
Tejun Heo | f318719 | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 5763 | * Inherited from calling layer (may sleep). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5764 | * |
| 5765 | * RETURNS: |
Tejun Heo | f318719 | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 5766 | * 0 on success, -errno otherwise. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5767 | */ |
Tejun Heo | f318719 | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 5768 | int ata_host_register(struct ata_host *host, struct scsi_host_template *sht) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5769 | { |
Tejun Heo | f318719 | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 5770 | int i, rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5771 | |
Jens Axboe | 69278f7 | 2018-05-11 12:51:10 -0600 | [diff] [blame] | 5772 | host->n_tags = clamp(sht->can_queue, 1, ATA_MAX_QUEUE); |
Kevin Hao | 1871ee1 | 2014-07-12 12:08:24 +0800 | [diff] [blame] | 5773 | |
Tejun Heo | f318719 | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 5774 | /* host must have been started */ |
| 5775 | if (!(host->flags & ATA_HOST_STARTED)) { |
Joe Perches | a44fec1 | 2011-04-15 15:51:58 -0700 | [diff] [blame] | 5776 | dev_err(host->dev, "BUG: trying to register unstarted host\n"); |
Tejun Heo | f318719 | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 5777 | WARN_ON(1); |
| 5778 | return -EINVAL; |
Alan Cox | 02f076a | 2006-09-26 17:35:32 +0100 | [diff] [blame] | 5779 | } |
Tejun Heo | f0d36ef | 2007-01-20 16:00:28 +0900 | [diff] [blame] | 5780 | |
Tejun Heo | f318719 | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 5781 | /* Blow away unused ports. This happens when LLD can't |
| 5782 | * determine the exact number of ports to allocate at |
| 5783 | * allocation time. |
| 5784 | */ |
| 5785 | for (i = host->n_ports; host->ports[i]; i++) |
| 5786 | kfree(host->ports[i]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5787 | |
Tejun Heo | f318719 | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 5788 | /* give ports names and add SCSI hosts */ |
David Milburn | e628dc9 | 2013-05-14 13:48:40 -0500 | [diff] [blame] | 5789 | for (i = 0; i < host->n_ports; i++) { |
Dan Williams | 85d6725 | 2012-03-10 23:28:46 -0800 | [diff] [blame] | 5790 | host->ports[i]->print_id = atomic_inc_return(&ata_print_id); |
David Milburn | e628dc9 | 2013-05-14 13:48:40 -0500 | [diff] [blame] | 5791 | host->ports[i]->local_port_no = i + 1; |
| 5792 | } |
Jeff Garzik | 4fca377 | 2011-02-15 01:13:24 -0500 | [diff] [blame] | 5793 | |
Gwendal Grignou | d902747 | 2010-05-25 12:31:38 -0700 | [diff] [blame] | 5794 | /* Create associated sysfs transport objects */ |
| 5795 | for (i = 0; i < host->n_ports; i++) { |
| 5796 | rc = ata_tport_add(host->dev,host->ports[i]); |
| 5797 | if (rc) { |
| 5798 | goto err_tadd; |
| 5799 | } |
| 5800 | } |
| 5801 | |
Tejun Heo | f318719 | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 5802 | rc = ata_scsi_add_hosts(host, sht); |
Tejun Heo | ecef725 | 2007-04-17 23:44:06 +0900 | [diff] [blame] | 5803 | if (rc) |
Gwendal Grignou | d902747 | 2010-05-25 12:31:38 -0700 | [diff] [blame] | 5804 | goto err_tadd; |
Tejun Heo | ecef725 | 2007-04-17 23:44:06 +0900 | [diff] [blame] | 5805 | |
Tejun Heo | f318719 | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 5806 | /* set cable, sata_spd_limit and report */ |
Jeff Garzik | cca3974 | 2006-08-24 03:19:22 -0400 | [diff] [blame] | 5807 | for (i = 0; i < host->n_ports; i++) { |
| 5808 | struct ata_port *ap = host->ports[i]; |
Tejun Heo | f318719 | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 5809 | unsigned long xfer_mask; |
| 5810 | |
| 5811 | /* set SATA cable type if still unset */ |
| 5812 | if (ap->cbl == ATA_CBL_NONE && (ap->flags & ATA_FLAG_SATA)) |
| 5813 | ap->cbl = ATA_CBL_SATA; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5814 | |
Tejun Heo | 5a04bf4 | 2006-05-31 18:27:38 +0900 | [diff] [blame] | 5815 | /* init sata_spd_limit to the current value */ |
Tejun Heo | 4fb37a2 | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 5816 | sata_link_init_spd(&ap->link); |
Tejun Heo | b1c7291 | 2008-07-31 17:02:43 +0900 | [diff] [blame] | 5817 | if (ap->slave_link) |
| 5818 | sata_link_init_spd(ap->slave_link); |
Tejun Heo | 5a04bf4 | 2006-05-31 18:27:38 +0900 | [diff] [blame] | 5819 | |
Tejun Heo | cbcdd87 | 2007-08-18 13:14:55 +0900 | [diff] [blame] | 5820 | /* print per-port info to dmesg */ |
Tejun Heo | f318719 | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 5821 | xfer_mask = ata_pack_xfermask(ap->pio_mask, ap->mwdma_mask, |
| 5822 | ap->udma_mask); |
| 5823 | |
Tejun Heo | abf6e8e | 2007-10-09 14:57:25 +0900 | [diff] [blame] | 5824 | if (!ata_port_is_dummy(ap)) { |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 5825 | ata_port_info(ap, "%cATA max %s %s\n", |
| 5826 | (ap->flags & ATA_FLAG_SATA) ? 'S' : 'P', |
| 5827 | ata_mode_string(xfer_mask), |
| 5828 | ap->link.eh_info.desc); |
Tejun Heo | abf6e8e | 2007-10-09 14:57:25 +0900 | [diff] [blame] | 5829 | ata_ehi_clear_desc(&ap->link.eh_info); |
| 5830 | } else |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 5831 | ata_port_info(ap, "DUMMY\n"); |
Tejun Heo | f318719 | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 5832 | } |
| 5833 | |
Vegard Nossum | f600535 | 2009-04-08 18:19:39 +0200 | [diff] [blame] | 5834 | /* perform each probe asynchronously */ |
Tejun Heo | f318719 | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 5835 | for (i = 0; i < host->n_ports; i++) { |
| 5836 | struct ata_port *ap = host->ports[i]; |
Kai-Heng Feng | b529211 | 2020-06-03 15:48:19 +0800 | [diff] [blame] | 5837 | ap->cookie = async_schedule(async_port_probe, ap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5838 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5839 | |
Tejun Heo | f318719 | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 5840 | return 0; |
Gwendal Grignou | d902747 | 2010-05-25 12:31:38 -0700 | [diff] [blame] | 5841 | |
| 5842 | err_tadd: |
| 5843 | while (--i >= 0) { |
| 5844 | ata_tport_delete(host->ports[i]); |
| 5845 | } |
| 5846 | return rc; |
| 5847 | |
Tejun Heo | f318719 | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 5848 | } |
Bartlomiej Zolnierkiewicz | a52fbcf | 2020-03-26 16:58:04 +0100 | [diff] [blame] | 5849 | EXPORT_SYMBOL_GPL(ata_host_register); |
Tejun Heo | f318719 | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 5850 | |
| 5851 | /** |
Tejun Heo | f5cda25 | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 5852 | * ata_host_activate - start host, request IRQ and register it |
| 5853 | * @host: target ATA host |
| 5854 | * @irq: IRQ to request |
| 5855 | * @irq_handler: irq_handler used when requesting IRQ |
| 5856 | * @irq_flags: irq_flags used when requesting IRQ |
| 5857 | * @sht: scsi_host_template to use when registering the host |
| 5858 | * |
| 5859 | * After allocating an ATA host and initializing it, most libata |
| 5860 | * LLDs perform three steps to activate the host - start host, |
Masanari Iida | c9b5560 | 2016-04-13 23:36:27 +0900 | [diff] [blame] | 5861 | * request IRQ and register it. This helper takes necessary |
Tejun Heo | f5cda25 | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 5862 | * arguments and performs the three steps in one go. |
| 5863 | * |
Paul Mundt | 3d46b2e | 2007-11-08 11:14:56 +0900 | [diff] [blame] | 5864 | * An invalid IRQ skips the IRQ registration and expects the host to |
| 5865 | * have set polling mode on the port. In this case, @irq_handler |
| 5866 | * should be NULL. |
| 5867 | * |
Tejun Heo | f5cda25 | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 5868 | * LOCKING: |
| 5869 | * Inherited from calling layer (may sleep). |
| 5870 | * |
| 5871 | * RETURNS: |
| 5872 | * 0 on success, -errno otherwise. |
| 5873 | */ |
| 5874 | int ata_host_activate(struct ata_host *host, int irq, |
| 5875 | irq_handler_t irq_handler, unsigned long irq_flags, |
| 5876 | struct scsi_host_template *sht) |
| 5877 | { |
Tejun Heo | cbcdd87 | 2007-08-18 13:14:55 +0900 | [diff] [blame] | 5878 | int i, rc; |
Heiner Kallweit | 7e22c00 | 2015-12-06 21:56:33 +0100 | [diff] [blame] | 5879 | char *irq_desc; |
Tejun Heo | f5cda25 | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 5880 | |
| 5881 | rc = ata_host_start(host); |
| 5882 | if (rc) |
| 5883 | return rc; |
| 5884 | |
Paul Mundt | 3d46b2e | 2007-11-08 11:14:56 +0900 | [diff] [blame] | 5885 | /* Special case for polling mode */ |
| 5886 | if (!irq) { |
| 5887 | WARN_ON(irq_handler); |
| 5888 | return ata_host_register(host, sht); |
| 5889 | } |
| 5890 | |
Heiner Kallweit | 7e22c00 | 2015-12-06 21:56:33 +0100 | [diff] [blame] | 5891 | irq_desc = devm_kasprintf(host->dev, GFP_KERNEL, "%s[%s]", |
| 5892 | dev_driver_string(host->dev), |
| 5893 | dev_name(host->dev)); |
| 5894 | if (!irq_desc) |
| 5895 | return -ENOMEM; |
| 5896 | |
Tejun Heo | f5cda25 | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 5897 | rc = devm_request_irq(host->dev, irq, irq_handler, irq_flags, |
Heiner Kallweit | 7e22c00 | 2015-12-06 21:56:33 +0100 | [diff] [blame] | 5898 | irq_desc, host); |
Tejun Heo | f5cda25 | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 5899 | if (rc) |
| 5900 | return rc; |
| 5901 | |
Tejun Heo | cbcdd87 | 2007-08-18 13:14:55 +0900 | [diff] [blame] | 5902 | for (i = 0; i < host->n_ports; i++) |
| 5903 | ata_port_desc(host->ports[i], "irq %d", irq); |
Tejun Heo | 4031826 | 2007-07-03 01:38:47 +0900 | [diff] [blame] | 5904 | |
Tejun Heo | f5cda25 | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 5905 | rc = ata_host_register(host, sht); |
| 5906 | /* if failed, just free the IRQ and leave ports alone */ |
| 5907 | if (rc) |
| 5908 | devm_free_irq(host->dev, irq, host); |
| 5909 | |
| 5910 | return rc; |
| 5911 | } |
Bartlomiej Zolnierkiewicz | a52fbcf | 2020-03-26 16:58:04 +0100 | [diff] [blame] | 5912 | EXPORT_SYMBOL_GPL(ata_host_activate); |
Tejun Heo | f5cda25 | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 5913 | |
| 5914 | /** |
Masanari Iida | c9b5560 | 2016-04-13 23:36:27 +0900 | [diff] [blame] | 5915 | * ata_port_detach - Detach ATA port in preparation of device removal |
Tejun Heo | 720ba12 | 2006-05-31 18:28:13 +0900 | [diff] [blame] | 5916 | * @ap: ATA port to be detached |
| 5917 | * |
| 5918 | * Detach all ATA devices and the associated SCSI devices of @ap; |
| 5919 | * then, remove the associated SCSI host. @ap is guaranteed to |
| 5920 | * be quiescent on return from this function. |
| 5921 | * |
| 5922 | * LOCKING: |
| 5923 | * Kernel thread context (may sleep). |
| 5924 | */ |
Adrian Bunk | 741b776 | 2007-10-24 18:23:06 +0200 | [diff] [blame] | 5925 | static void ata_port_detach(struct ata_port *ap) |
Tejun Heo | 720ba12 | 2006-05-31 18:28:13 +0900 | [diff] [blame] | 5926 | { |
| 5927 | unsigned long flags; |
Levente Kurusa | a6f9bf4 | 2014-05-06 15:57:48 +0200 | [diff] [blame] | 5928 | struct ata_link *link; |
| 5929 | struct ata_device *dev; |
Tejun Heo | 720ba12 | 2006-05-31 18:28:13 +0900 | [diff] [blame] | 5930 | |
| 5931 | if (!ap->ops->error_handler) |
Tejun Heo | c3cf30a | 2006-08-05 03:59:11 +0900 | [diff] [blame] | 5932 | goto skip_eh; |
Tejun Heo | 720ba12 | 2006-05-31 18:28:13 +0900 | [diff] [blame] | 5933 | |
| 5934 | /* tell EH we're leaving & flush EH */ |
Jeff Garzik | ba6a130 | 2006-06-22 23:46:10 -0400 | [diff] [blame] | 5935 | spin_lock_irqsave(ap->lock, flags); |
Tejun Heo | b51e9e5 | 2006-06-29 01:29:30 +0900 | [diff] [blame] | 5936 | ap->pflags |= ATA_PFLAG_UNLOADING; |
Tejun Heo | ece180d | 2008-11-03 20:04:37 +0900 | [diff] [blame] | 5937 | ata_port_schedule_eh(ap); |
Jeff Garzik | ba6a130 | 2006-06-22 23:46:10 -0400 | [diff] [blame] | 5938 | spin_unlock_irqrestore(ap->lock, flags); |
Tejun Heo | 720ba12 | 2006-05-31 18:28:13 +0900 | [diff] [blame] | 5939 | |
Tejun Heo | ece180d | 2008-11-03 20:04:37 +0900 | [diff] [blame] | 5940 | /* wait till EH commits suicide */ |
Tejun Heo | 720ba12 | 2006-05-31 18:28:13 +0900 | [diff] [blame] | 5941 | ata_port_wait_eh(ap); |
| 5942 | |
Tejun Heo | ece180d | 2008-11-03 20:04:37 +0900 | [diff] [blame] | 5943 | /* it better be dead now */ |
| 5944 | WARN_ON(!(ap->pflags & ATA_PFLAG_UNLOADED)); |
Tejun Heo | 720ba12 | 2006-05-31 18:28:13 +0900 | [diff] [blame] | 5945 | |
Tejun Heo | afe2c51 | 2010-12-14 16:21:17 +0100 | [diff] [blame] | 5946 | cancel_delayed_work_sync(&ap->hotplug_task); |
Tejun Heo | 720ba12 | 2006-05-31 18:28:13 +0900 | [diff] [blame] | 5947 | |
Tejun Heo | c3cf30a | 2006-08-05 03:59:11 +0900 | [diff] [blame] | 5948 | skip_eh: |
Levente Kurusa | a6f9bf4 | 2014-05-06 15:57:48 +0200 | [diff] [blame] | 5949 | /* clean up zpodd on port removal */ |
| 5950 | ata_for_each_link(link, ap, HOST_FIRST) { |
| 5951 | ata_for_each_dev(dev, link, ALL) { |
| 5952 | if (zpodd_dev_enabled(dev)) |
| 5953 | zpodd_exit(dev); |
| 5954 | } |
| 5955 | } |
Gwendal Grignou | d902747 | 2010-05-25 12:31:38 -0700 | [diff] [blame] | 5956 | if (ap->pmp_link) { |
| 5957 | int i; |
| 5958 | for (i = 0; i < SATA_PMP_MAX_PORTS; i++) |
| 5959 | ata_tlink_delete(&ap->pmp_link[i]); |
| 5960 | } |
Tejun Heo | 720ba12 | 2006-05-31 18:28:13 +0900 | [diff] [blame] | 5961 | /* remove the associated SCSI host */ |
Jeff Garzik | cca3974 | 2006-08-24 03:19:22 -0400 | [diff] [blame] | 5962 | scsi_remove_host(ap->scsi_host); |
Rafael J. Wysocki | c570076 | 2013-11-25 13:19:01 +0100 | [diff] [blame] | 5963 | ata_tport_delete(ap); |
Tejun Heo | 720ba12 | 2006-05-31 18:28:13 +0900 | [diff] [blame] | 5964 | } |
| 5965 | |
| 5966 | /** |
Tejun Heo | 0529c159 | 2007-01-20 16:00:26 +0900 | [diff] [blame] | 5967 | * ata_host_detach - Detach all ports of an ATA host |
| 5968 | * @host: Host to detach |
| 5969 | * |
| 5970 | * Detach all ports of @host. |
| 5971 | * |
| 5972 | * LOCKING: |
| 5973 | * Kernel thread context (may sleep). |
| 5974 | */ |
| 5975 | void ata_host_detach(struct ata_host *host) |
| 5976 | { |
| 5977 | int i; |
| 5978 | |
Kai-Heng Feng | b529211 | 2020-06-03 15:48:19 +0800 | [diff] [blame] | 5979 | for (i = 0; i < host->n_ports; i++) { |
| 5980 | /* Ensure ata_port probe has completed */ |
| 5981 | async_synchronize_cookie(host->ports[i]->cookie + 1); |
Tejun Heo | 0529c159 | 2007-01-20 16:00:26 +0900 | [diff] [blame] | 5982 | ata_port_detach(host->ports[i]); |
Kai-Heng Feng | b529211 | 2020-06-03 15:48:19 +0800 | [diff] [blame] | 5983 | } |
Tejun Heo | 562f0c2 | 2007-12-15 15:05:01 +0900 | [diff] [blame] | 5984 | |
| 5985 | /* the host is dead now, dissociate ACPI */ |
| 5986 | ata_acpi_dissociate(host); |
Tejun Heo | 0529c159 | 2007-01-20 16:00:26 +0900 | [diff] [blame] | 5987 | } |
Bartlomiej Zolnierkiewicz | a52fbcf | 2020-03-26 16:58:04 +0100 | [diff] [blame] | 5988 | EXPORT_SYMBOL_GPL(ata_host_detach); |
Tejun Heo | 0529c159 | 2007-01-20 16:00:26 +0900 | [diff] [blame] | 5989 | |
Jeff Garzik | 374b187 | 2005-08-30 05:42:52 -0400 | [diff] [blame] | 5990 | #ifdef CONFIG_PCI |
| 5991 | |
Edward Falk | 0baab86 | 2005-06-02 18:17:13 -0400 | [diff] [blame] | 5992 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5993 | * ata_pci_remove_one - PCI layer callback for device removal |
| 5994 | * @pdev: PCI device that was removed |
| 5995 | * |
Tejun Heo | b878ca5 | 2007-01-20 16:00:28 +0900 | [diff] [blame] | 5996 | * PCI layer indicates to libata via this hook that hot-unplug or |
| 5997 | * module unload event has occurred. Detach all ports. Resource |
| 5998 | * release is handled via devres. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5999 | * |
| 6000 | * LOCKING: |
| 6001 | * Inherited from PCI layer (may sleep). |
| 6002 | */ |
Tejun Heo | f0d36ef | 2007-01-20 16:00:28 +0900 | [diff] [blame] | 6003 | void ata_pci_remove_one(struct pci_dev *pdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6004 | { |
Brian Norris | 04a3f5b | 2012-12-03 10:34:41 -0800 | [diff] [blame] | 6005 | struct ata_host *host = pci_get_drvdata(pdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6006 | |
Tejun Heo | b878ca5 | 2007-01-20 16:00:28 +0900 | [diff] [blame] | 6007 | ata_host_detach(host); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6008 | } |
Bartlomiej Zolnierkiewicz | a52fbcf | 2020-03-26 16:58:04 +0100 | [diff] [blame] | 6009 | EXPORT_SYMBOL_GPL(ata_pci_remove_one); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6010 | |
Prabhakar Kushwaha | 10a663a | 2020-01-25 03:37:29 +0000 | [diff] [blame] | 6011 | void ata_pci_shutdown_one(struct pci_dev *pdev) |
| 6012 | { |
| 6013 | struct ata_host *host = pci_get_drvdata(pdev); |
| 6014 | int i; |
| 6015 | |
| 6016 | for (i = 0; i < host->n_ports; i++) { |
| 6017 | struct ata_port *ap = host->ports[i]; |
| 6018 | |
| 6019 | ap->pflags |= ATA_PFLAG_FROZEN; |
| 6020 | |
| 6021 | /* Disable port interrupts */ |
| 6022 | if (ap->ops->freeze) |
| 6023 | ap->ops->freeze(ap); |
| 6024 | |
| 6025 | /* Stop the port DMA engines */ |
| 6026 | if (ap->ops->port_stop) |
| 6027 | ap->ops->port_stop(ap); |
| 6028 | } |
| 6029 | } |
Bartlomiej Zolnierkiewicz | a52fbcf | 2020-03-26 16:58:04 +0100 | [diff] [blame] | 6030 | EXPORT_SYMBOL_GPL(ata_pci_shutdown_one); |
Prabhakar Kushwaha | 10a663a | 2020-01-25 03:37:29 +0000 | [diff] [blame] | 6031 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6032 | /* move to PCI subsystem */ |
Jeff Garzik | 057ace5 | 2005-10-22 14:27:05 -0400 | [diff] [blame] | 6033 | int pci_test_config_bits(struct pci_dev *pdev, const struct pci_bits *bits) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6034 | { |
| 6035 | unsigned long tmp = 0; |
| 6036 | |
| 6037 | switch (bits->width) { |
| 6038 | case 1: { |
| 6039 | u8 tmp8 = 0; |
| 6040 | pci_read_config_byte(pdev, bits->reg, &tmp8); |
| 6041 | tmp = tmp8; |
| 6042 | break; |
| 6043 | } |
| 6044 | case 2: { |
| 6045 | u16 tmp16 = 0; |
| 6046 | pci_read_config_word(pdev, bits->reg, &tmp16); |
| 6047 | tmp = tmp16; |
| 6048 | break; |
| 6049 | } |
| 6050 | case 4: { |
| 6051 | u32 tmp32 = 0; |
| 6052 | pci_read_config_dword(pdev, bits->reg, &tmp32); |
| 6053 | tmp = tmp32; |
| 6054 | break; |
| 6055 | } |
| 6056 | |
| 6057 | default: |
| 6058 | return -EINVAL; |
| 6059 | } |
| 6060 | |
| 6061 | tmp &= bits->mask; |
| 6062 | |
| 6063 | return (tmp == bits->val) ? 1 : 0; |
| 6064 | } |
Bartlomiej Zolnierkiewicz | a52fbcf | 2020-03-26 16:58:04 +0100 | [diff] [blame] | 6065 | EXPORT_SYMBOL_GPL(pci_test_config_bits); |
Jens Axboe | 9b84754 | 2006-01-06 09:28:07 +0100 | [diff] [blame] | 6066 | |
Tejun Heo | 6ffa01d | 2007-03-02 17:32:47 +0900 | [diff] [blame] | 6067 | #ifdef CONFIG_PM |
Tejun Heo | 3c5100c | 2006-07-26 16:58:33 +0900 | [diff] [blame] | 6068 | void ata_pci_device_do_suspend(struct pci_dev *pdev, pm_message_t mesg) |
Jens Axboe | 9b84754 | 2006-01-06 09:28:07 +0100 | [diff] [blame] | 6069 | { |
| 6070 | pci_save_state(pdev); |
Tejun Heo | 4c90d97 | 2007-02-20 18:14:48 +0900 | [diff] [blame] | 6071 | pci_disable_device(pdev); |
Tejun Heo | 500530f | 2006-07-03 16:07:27 +0900 | [diff] [blame] | 6072 | |
Rafael J. Wysocki | 3a2d5b7 | 2008-02-23 19:13:25 +0100 | [diff] [blame] | 6073 | if (mesg.event & PM_EVENT_SLEEP) |
Tejun Heo | 500530f | 2006-07-03 16:07:27 +0900 | [diff] [blame] | 6074 | pci_set_power_state(pdev, PCI_D3hot); |
Jens Axboe | 9b84754 | 2006-01-06 09:28:07 +0100 | [diff] [blame] | 6075 | } |
Bartlomiej Zolnierkiewicz | a52fbcf | 2020-03-26 16:58:04 +0100 | [diff] [blame] | 6076 | EXPORT_SYMBOL_GPL(ata_pci_device_do_suspend); |
Jens Axboe | 9b84754 | 2006-01-06 09:28:07 +0100 | [diff] [blame] | 6077 | |
Tejun Heo | 553c4aa | 2006-12-26 19:39:50 +0900 | [diff] [blame] | 6078 | int ata_pci_device_do_resume(struct pci_dev *pdev) |
Jens Axboe | 9b84754 | 2006-01-06 09:28:07 +0100 | [diff] [blame] | 6079 | { |
Tejun Heo | 553c4aa | 2006-12-26 19:39:50 +0900 | [diff] [blame] | 6080 | int rc; |
| 6081 | |
Jens Axboe | 9b84754 | 2006-01-06 09:28:07 +0100 | [diff] [blame] | 6082 | pci_set_power_state(pdev, PCI_D0); |
| 6083 | pci_restore_state(pdev); |
Tejun Heo | 553c4aa | 2006-12-26 19:39:50 +0900 | [diff] [blame] | 6084 | |
Tejun Heo | b878ca5 | 2007-01-20 16:00:28 +0900 | [diff] [blame] | 6085 | rc = pcim_enable_device(pdev); |
Tejun Heo | 553c4aa | 2006-12-26 19:39:50 +0900 | [diff] [blame] | 6086 | if (rc) { |
Joe Perches | a44fec1 | 2011-04-15 15:51:58 -0700 | [diff] [blame] | 6087 | dev_err(&pdev->dev, |
| 6088 | "failed to enable device after resume (%d)\n", rc); |
Tejun Heo | 553c4aa | 2006-12-26 19:39:50 +0900 | [diff] [blame] | 6089 | return rc; |
| 6090 | } |
| 6091 | |
Jens Axboe | 9b84754 | 2006-01-06 09:28:07 +0100 | [diff] [blame] | 6092 | pci_set_master(pdev); |
Tejun Heo | 553c4aa | 2006-12-26 19:39:50 +0900 | [diff] [blame] | 6093 | return 0; |
Tejun Heo | 500530f | 2006-07-03 16:07:27 +0900 | [diff] [blame] | 6094 | } |
Bartlomiej Zolnierkiewicz | a52fbcf | 2020-03-26 16:58:04 +0100 | [diff] [blame] | 6095 | EXPORT_SYMBOL_GPL(ata_pci_device_do_resume); |
Tejun Heo | 500530f | 2006-07-03 16:07:27 +0900 | [diff] [blame] | 6096 | |
Tejun Heo | 3c5100c | 2006-07-26 16:58:33 +0900 | [diff] [blame] | 6097 | int ata_pci_device_suspend(struct pci_dev *pdev, pm_message_t mesg) |
Tejun Heo | 500530f | 2006-07-03 16:07:27 +0900 | [diff] [blame] | 6098 | { |
Brian Norris | 04a3f5b | 2012-12-03 10:34:41 -0800 | [diff] [blame] | 6099 | struct ata_host *host = pci_get_drvdata(pdev); |
Tejun Heo | 500530f | 2006-07-03 16:07:27 +0900 | [diff] [blame] | 6100 | int rc = 0; |
| 6101 | |
Jeff Garzik | cca3974 | 2006-08-24 03:19:22 -0400 | [diff] [blame] | 6102 | rc = ata_host_suspend(host, mesg); |
Tejun Heo | 500530f | 2006-07-03 16:07:27 +0900 | [diff] [blame] | 6103 | if (rc) |
| 6104 | return rc; |
| 6105 | |
Tejun Heo | 3c5100c | 2006-07-26 16:58:33 +0900 | [diff] [blame] | 6106 | ata_pci_device_do_suspend(pdev, mesg); |
Tejun Heo | 500530f | 2006-07-03 16:07:27 +0900 | [diff] [blame] | 6107 | |
| 6108 | return 0; |
| 6109 | } |
Bartlomiej Zolnierkiewicz | a52fbcf | 2020-03-26 16:58:04 +0100 | [diff] [blame] | 6110 | EXPORT_SYMBOL_GPL(ata_pci_device_suspend); |
Tejun Heo | 500530f | 2006-07-03 16:07:27 +0900 | [diff] [blame] | 6111 | |
| 6112 | int ata_pci_device_resume(struct pci_dev *pdev) |
| 6113 | { |
Brian Norris | 04a3f5b | 2012-12-03 10:34:41 -0800 | [diff] [blame] | 6114 | struct ata_host *host = pci_get_drvdata(pdev); |
Tejun Heo | 553c4aa | 2006-12-26 19:39:50 +0900 | [diff] [blame] | 6115 | int rc; |
Tejun Heo | 500530f | 2006-07-03 16:07:27 +0900 | [diff] [blame] | 6116 | |
Tejun Heo | 553c4aa | 2006-12-26 19:39:50 +0900 | [diff] [blame] | 6117 | rc = ata_pci_device_do_resume(pdev); |
| 6118 | if (rc == 0) |
| 6119 | ata_host_resume(host); |
| 6120 | return rc; |
Jens Axboe | 9b84754 | 2006-01-06 09:28:07 +0100 | [diff] [blame] | 6121 | } |
Bartlomiej Zolnierkiewicz | a52fbcf | 2020-03-26 16:58:04 +0100 | [diff] [blame] | 6122 | EXPORT_SYMBOL_GPL(ata_pci_device_resume); |
Tejun Heo | 6ffa01d | 2007-03-02 17:32:47 +0900 | [diff] [blame] | 6123 | #endif /* CONFIG_PM */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6124 | #endif /* CONFIG_PCI */ |
| 6125 | |
Brian Norris | b7db04d | 2012-11-02 12:29:32 -0700 | [diff] [blame] | 6126 | /** |
| 6127 | * ata_platform_remove_one - Platform layer callback for device removal |
| 6128 | * @pdev: Platform device that was removed |
| 6129 | * |
| 6130 | * Platform layer indicates to libata via this hook that hot-unplug or |
| 6131 | * module unload event has occurred. Detach all ports. Resource |
| 6132 | * release is handled via devres. |
| 6133 | * |
| 6134 | * LOCKING: |
| 6135 | * Inherited from platform layer (may sleep). |
| 6136 | */ |
| 6137 | int ata_platform_remove_one(struct platform_device *pdev) |
| 6138 | { |
| 6139 | struct ata_host *host = platform_get_drvdata(pdev); |
| 6140 | |
| 6141 | ata_host_detach(host); |
| 6142 | |
| 6143 | return 0; |
| 6144 | } |
Bartlomiej Zolnierkiewicz | a52fbcf | 2020-03-26 16:58:04 +0100 | [diff] [blame] | 6145 | EXPORT_SYMBOL_GPL(ata_platform_remove_one); |
Brian Norris | b7db04d | 2012-11-02 12:29:32 -0700 | [diff] [blame] | 6146 | |
Bartlomiej Zolnierkiewicz | bf89b0b | 2020-03-26 16:58:22 +0100 | [diff] [blame] | 6147 | #ifdef CONFIG_ATA_FORCE |
Tejun Heo | 3326732 | 2008-02-13 09:15:09 +0900 | [diff] [blame] | 6148 | static int __init ata_parse_force_one(char **cur, |
| 6149 | struct ata_force_ent *force_ent, |
| 6150 | const char **reason) |
| 6151 | { |
Rasmus Villemoes | 0f5f264 | 2015-06-09 15:33:19 +0200 | [diff] [blame] | 6152 | static const struct ata_force_param force_tbl[] __initconst = { |
Tejun Heo | 3326732 | 2008-02-13 09:15:09 +0900 | [diff] [blame] | 6153 | { "40c", .cbl = ATA_CBL_PATA40 }, |
| 6154 | { "80c", .cbl = ATA_CBL_PATA80 }, |
| 6155 | { "short40c", .cbl = ATA_CBL_PATA40_SHORT }, |
| 6156 | { "unk", .cbl = ATA_CBL_PATA_UNK }, |
| 6157 | { "ign", .cbl = ATA_CBL_PATA_IGN }, |
| 6158 | { "sata", .cbl = ATA_CBL_SATA }, |
| 6159 | { "1.5Gbps", .spd_limit = 1 }, |
| 6160 | { "3.0Gbps", .spd_limit = 2 }, |
| 6161 | { "noncq", .horkage_on = ATA_HORKAGE_NONCQ }, |
| 6162 | { "ncq", .horkage_off = ATA_HORKAGE_NONCQ }, |
Martin K. Petersen | d7b16e4 | 2015-05-04 21:54:18 -0400 | [diff] [blame] | 6163 | { "noncqtrim", .horkage_on = ATA_HORKAGE_NO_NCQ_TRIM }, |
| 6164 | { "ncqtrim", .horkage_off = ATA_HORKAGE_NO_NCQ_TRIM }, |
Kate Hsuan | 7a8526a | 2021-09-03 17:44:11 +0800 | [diff] [blame] | 6165 | { "noncqati", .horkage_on = ATA_HORKAGE_NO_NCQ_ON_ATI }, |
| 6166 | { "ncqati", .horkage_off = ATA_HORKAGE_NO_NCQ_ON_ATI }, |
Tejun Heo | 43c9c59 | 2010-05-23 12:59:11 +0200 | [diff] [blame] | 6167 | { "dump_id", .horkage_on = ATA_HORKAGE_DUMP_ID }, |
Tejun Heo | 3326732 | 2008-02-13 09:15:09 +0900 | [diff] [blame] | 6168 | { "pio0", .xfer_mask = 1 << (ATA_SHIFT_PIO + 0) }, |
| 6169 | { "pio1", .xfer_mask = 1 << (ATA_SHIFT_PIO + 1) }, |
| 6170 | { "pio2", .xfer_mask = 1 << (ATA_SHIFT_PIO + 2) }, |
| 6171 | { "pio3", .xfer_mask = 1 << (ATA_SHIFT_PIO + 3) }, |
| 6172 | { "pio4", .xfer_mask = 1 << (ATA_SHIFT_PIO + 4) }, |
| 6173 | { "pio5", .xfer_mask = 1 << (ATA_SHIFT_PIO + 5) }, |
| 6174 | { "pio6", .xfer_mask = 1 << (ATA_SHIFT_PIO + 6) }, |
| 6175 | { "mwdma0", .xfer_mask = 1 << (ATA_SHIFT_MWDMA + 0) }, |
| 6176 | { "mwdma1", .xfer_mask = 1 << (ATA_SHIFT_MWDMA + 1) }, |
| 6177 | { "mwdma2", .xfer_mask = 1 << (ATA_SHIFT_MWDMA + 2) }, |
| 6178 | { "mwdma3", .xfer_mask = 1 << (ATA_SHIFT_MWDMA + 3) }, |
| 6179 | { "mwdma4", .xfer_mask = 1 << (ATA_SHIFT_MWDMA + 4) }, |
| 6180 | { "udma0", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 0) }, |
| 6181 | { "udma16", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 0) }, |
| 6182 | { "udma/16", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 0) }, |
| 6183 | { "udma1", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 1) }, |
| 6184 | { "udma25", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 1) }, |
| 6185 | { "udma/25", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 1) }, |
| 6186 | { "udma2", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 2) }, |
| 6187 | { "udma33", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 2) }, |
| 6188 | { "udma/33", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 2) }, |
| 6189 | { "udma3", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 3) }, |
| 6190 | { "udma44", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 3) }, |
| 6191 | { "udma/44", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 3) }, |
| 6192 | { "udma4", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 4) }, |
| 6193 | { "udma66", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 4) }, |
| 6194 | { "udma/66", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 4) }, |
| 6195 | { "udma5", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 5) }, |
| 6196 | { "udma100", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 5) }, |
| 6197 | { "udma/100", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 5) }, |
| 6198 | { "udma6", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 6) }, |
| 6199 | { "udma133", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 6) }, |
| 6200 | { "udma/133", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 6) }, |
| 6201 | { "udma7", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 7) }, |
Tejun Heo | 05944bd | 2008-08-13 20:19:09 +0900 | [diff] [blame] | 6202 | { "nohrst", .lflags = ATA_LFLAG_NO_HRST }, |
| 6203 | { "nosrst", .lflags = ATA_LFLAG_NO_SRST }, |
| 6204 | { "norst", .lflags = ATA_LFLAG_NO_HRST | ATA_LFLAG_NO_SRST }, |
Dan Williams | ca6d43b | 2012-06-21 23:41:41 -0700 | [diff] [blame] | 6205 | { "rstonce", .lflags = ATA_LFLAG_RST_ONCE }, |
Vincent Pelletier | 966fbe1 | 2013-05-21 22:30:58 +0200 | [diff] [blame] | 6206 | { "atapi_dmadir", .horkage_on = ATA_HORKAGE_ATAPI_DMADIR }, |
Robin H. Johnson | b8bd6dc | 2013-12-16 09:31:19 -0800 | [diff] [blame] | 6207 | { "disable", .horkage_on = ATA_HORKAGE_DISABLE }, |
Tejun Heo | 3326732 | 2008-02-13 09:15:09 +0900 | [diff] [blame] | 6208 | }; |
| 6209 | char *start = *cur, *p = *cur; |
| 6210 | char *id, *val, *endp; |
| 6211 | const struct ata_force_param *match_fp = NULL; |
| 6212 | int nr_matches = 0, i; |
| 6213 | |
| 6214 | /* find where this param ends and update *cur */ |
| 6215 | while (*p != '\0' && *p != ',') |
| 6216 | p++; |
| 6217 | |
| 6218 | if (*p == '\0') |
| 6219 | *cur = p; |
| 6220 | else |
| 6221 | *cur = p + 1; |
| 6222 | |
| 6223 | *p = '\0'; |
| 6224 | |
| 6225 | /* parse */ |
| 6226 | p = strchr(start, ':'); |
| 6227 | if (!p) { |
| 6228 | val = strstrip(start); |
| 6229 | goto parse_val; |
| 6230 | } |
| 6231 | *p = '\0'; |
| 6232 | |
| 6233 | id = strstrip(start); |
| 6234 | val = strstrip(p + 1); |
| 6235 | |
| 6236 | /* parse id */ |
| 6237 | p = strchr(id, '.'); |
| 6238 | if (p) { |
| 6239 | *p++ = '\0'; |
| 6240 | force_ent->device = simple_strtoul(p, &endp, 10); |
| 6241 | if (p == endp || *endp != '\0') { |
| 6242 | *reason = "invalid device"; |
| 6243 | return -EINVAL; |
| 6244 | } |
| 6245 | } |
| 6246 | |
| 6247 | force_ent->port = simple_strtoul(id, &endp, 10); |
Tejun Heo | f7cf69a | 2017-05-31 14:26:26 -0400 | [diff] [blame] | 6248 | if (id == endp || *endp != '\0') { |
Tejun Heo | 3326732 | 2008-02-13 09:15:09 +0900 | [diff] [blame] | 6249 | *reason = "invalid port/link"; |
| 6250 | return -EINVAL; |
| 6251 | } |
| 6252 | |
| 6253 | parse_val: |
| 6254 | /* parse val, allow shortcuts so that both 1.5 and 1.5Gbps work */ |
| 6255 | for (i = 0; i < ARRAY_SIZE(force_tbl); i++) { |
| 6256 | const struct ata_force_param *fp = &force_tbl[i]; |
| 6257 | |
| 6258 | if (strncasecmp(val, fp->name, strlen(val))) |
| 6259 | continue; |
| 6260 | |
| 6261 | nr_matches++; |
| 6262 | match_fp = fp; |
| 6263 | |
| 6264 | if (strcasecmp(val, fp->name) == 0) { |
| 6265 | nr_matches = 1; |
| 6266 | break; |
| 6267 | } |
| 6268 | } |
| 6269 | |
| 6270 | if (!nr_matches) { |
| 6271 | *reason = "unknown value"; |
| 6272 | return -EINVAL; |
| 6273 | } |
| 6274 | if (nr_matches > 1) { |
Arvind Yadav | 9de5535 | 2017-10-28 23:51:47 +0530 | [diff] [blame] | 6275 | *reason = "ambiguous value"; |
Tejun Heo | 3326732 | 2008-02-13 09:15:09 +0900 | [diff] [blame] | 6276 | return -EINVAL; |
| 6277 | } |
| 6278 | |
| 6279 | force_ent->param = *match_fp; |
| 6280 | |
| 6281 | return 0; |
| 6282 | } |
| 6283 | |
| 6284 | static void __init ata_parse_force_param(void) |
| 6285 | { |
| 6286 | int idx = 0, size = 1; |
| 6287 | int last_port = -1, last_device = -1; |
| 6288 | char *p, *cur, *next; |
| 6289 | |
| 6290 | /* calculate maximum number of params and allocate force_tbl */ |
| 6291 | for (p = ata_force_param_buf; *p; p++) |
| 6292 | if (*p == ',') |
| 6293 | size++; |
| 6294 | |
Kees Cook | 6396bb2 | 2018-06-12 14:03:40 -0700 | [diff] [blame] | 6295 | ata_force_tbl = kcalloc(size, sizeof(ata_force_tbl[0]), GFP_KERNEL); |
Tejun Heo | 3326732 | 2008-02-13 09:15:09 +0900 | [diff] [blame] | 6296 | if (!ata_force_tbl) { |
| 6297 | printk(KERN_WARNING "ata: failed to extend force table, " |
| 6298 | "libata.force ignored\n"); |
| 6299 | return; |
| 6300 | } |
| 6301 | |
| 6302 | /* parse and populate the table */ |
| 6303 | for (cur = ata_force_param_buf; *cur != '\0'; cur = next) { |
| 6304 | const char *reason = ""; |
| 6305 | struct ata_force_ent te = { .port = -1, .device = -1 }; |
| 6306 | |
| 6307 | next = cur; |
| 6308 | if (ata_parse_force_one(&next, &te, &reason)) { |
| 6309 | printk(KERN_WARNING "ata: failed to parse force " |
| 6310 | "parameter \"%s\" (%s)\n", |
| 6311 | cur, reason); |
| 6312 | continue; |
| 6313 | } |
| 6314 | |
| 6315 | if (te.port == -1) { |
| 6316 | te.port = last_port; |
| 6317 | te.device = last_device; |
| 6318 | } |
| 6319 | |
| 6320 | ata_force_tbl[idx++] = te; |
| 6321 | |
| 6322 | last_port = te.port; |
| 6323 | last_device = te.device; |
| 6324 | } |
| 6325 | |
| 6326 | ata_force_tbl_size = idx; |
| 6327 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6328 | |
Bartlomiej Zolnierkiewicz | bf89b0b | 2020-03-26 16:58:22 +0100 | [diff] [blame] | 6329 | static void ata_free_force_param(void) |
| 6330 | { |
| 6331 | kfree(ata_force_tbl); |
| 6332 | } |
| 6333 | #else |
| 6334 | static inline void ata_parse_force_param(void) { } |
| 6335 | static inline void ata_free_force_param(void) { } |
| 6336 | #endif |
| 6337 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6338 | static int __init ata_init(void) |
| 6339 | { |
Gwendal Grignou | d902747 | 2010-05-25 12:31:38 -0700 | [diff] [blame] | 6340 | int rc; |
Tejun Heo | 270390e | 2010-05-10 21:41:35 +0200 | [diff] [blame] | 6341 | |
Tejun Heo | 3326732 | 2008-02-13 09:15:09 +0900 | [diff] [blame] | 6342 | ata_parse_force_param(); |
| 6343 | |
Tejun Heo | 270390e | 2010-05-10 21:41:35 +0200 | [diff] [blame] | 6344 | rc = ata_sff_init(); |
Tejun Heo | ad72cf9 | 2010-07-02 10:03:52 +0200 | [diff] [blame] | 6345 | if (rc) { |
Bartlomiej Zolnierkiewicz | bf89b0b | 2020-03-26 16:58:22 +0100 | [diff] [blame] | 6346 | ata_free_force_param(); |
Tejun Heo | ad72cf9 | 2010-07-02 10:03:52 +0200 | [diff] [blame] | 6347 | return rc; |
| 6348 | } |
Tejun Heo | 453b07a | 2006-05-31 18:27:42 +0900 | [diff] [blame] | 6349 | |
Gwendal Grignou | d902747 | 2010-05-25 12:31:38 -0700 | [diff] [blame] | 6350 | libata_transport_init(); |
| 6351 | ata_scsi_transport_template = ata_attach_transport(); |
| 6352 | if (!ata_scsi_transport_template) { |
| 6353 | ata_sff_exit(); |
| 6354 | rc = -ENOMEM; |
| 6355 | goto err_out; |
Jeff Garzik | 4fca377 | 2011-02-15 01:13:24 -0500 | [diff] [blame] | 6356 | } |
Gwendal Grignou | d902747 | 2010-05-25 12:31:38 -0700 | [diff] [blame] | 6357 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6358 | printk(KERN_DEBUG "libata version " DRV_VERSION " loaded.\n"); |
| 6359 | return 0; |
Gwendal Grignou | d902747 | 2010-05-25 12:31:38 -0700 | [diff] [blame] | 6360 | |
| 6361 | err_out: |
| 6362 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6363 | } |
| 6364 | |
| 6365 | static void __exit ata_exit(void) |
| 6366 | { |
Gwendal Grignou | d902747 | 2010-05-25 12:31:38 -0700 | [diff] [blame] | 6367 | ata_release_transport(ata_scsi_transport_template); |
| 6368 | libata_transport_exit(); |
Tejun Heo | 270390e | 2010-05-10 21:41:35 +0200 | [diff] [blame] | 6369 | ata_sff_exit(); |
Bartlomiej Zolnierkiewicz | bf89b0b | 2020-03-26 16:58:22 +0100 | [diff] [blame] | 6370 | ata_free_force_param(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6371 | } |
| 6372 | |
Brian King | a462508 | 2006-11-13 16:32:36 -0600 | [diff] [blame] | 6373 | subsys_initcall(ata_init); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6374 | module_exit(ata_exit); |
| 6375 | |
Akinobu Mita | 9990b6f | 2010-04-12 21:11:41 +0900 | [diff] [blame] | 6376 | static DEFINE_RATELIMIT_STATE(ratelimit, HZ / 5, 1); |
Jeff Garzik | 67846b3 | 2005-10-05 02:58:32 -0400 | [diff] [blame] | 6377 | |
| 6378 | int ata_ratelimit(void) |
| 6379 | { |
Akinobu Mita | 9990b6f | 2010-04-12 21:11:41 +0900 | [diff] [blame] | 6380 | return __ratelimit(&ratelimit); |
Jeff Garzik | 67846b3 | 2005-10-05 02:58:32 -0400 | [diff] [blame] | 6381 | } |
Bartlomiej Zolnierkiewicz | a52fbcf | 2020-03-26 16:58:04 +0100 | [diff] [blame] | 6382 | EXPORT_SYMBOL_GPL(ata_ratelimit); |
Jeff Garzik | 67846b3 | 2005-10-05 02:58:32 -0400 | [diff] [blame] | 6383 | |
Tejun Heo | c0c362b | 2010-09-06 17:57:14 +0200 | [diff] [blame] | 6384 | /** |
| 6385 | * ata_msleep - ATA EH owner aware msleep |
| 6386 | * @ap: ATA port to attribute the sleep to |
| 6387 | * @msecs: duration to sleep in milliseconds |
| 6388 | * |
| 6389 | * Sleeps @msecs. If the current task is owner of @ap's EH, the |
| 6390 | * ownership is released before going to sleep and reacquired |
| 6391 | * after the sleep is complete. IOW, other ports sharing the |
| 6392 | * @ap->host will be allowed to own the EH while this task is |
| 6393 | * sleeping. |
| 6394 | * |
| 6395 | * LOCKING: |
| 6396 | * Might sleep. |
| 6397 | */ |
Tejun Heo | 97750ce | 2010-09-06 17:56:29 +0200 | [diff] [blame] | 6398 | void ata_msleep(struct ata_port *ap, unsigned int msecs) |
| 6399 | { |
Tejun Heo | c0c362b | 2010-09-06 17:57:14 +0200 | [diff] [blame] | 6400 | bool owns_eh = ap && ap->host->eh_owner == current; |
| 6401 | |
| 6402 | if (owns_eh) |
| 6403 | ata_eh_release(ap); |
| 6404 | |
Anil Veliyankara Madam | 848c392 | 2016-01-07 21:18:52 -0800 | [diff] [blame] | 6405 | if (msecs < 20) { |
| 6406 | unsigned long usecs = msecs * USEC_PER_MSEC; |
| 6407 | usleep_range(usecs, usecs + 50); |
| 6408 | } else { |
| 6409 | msleep(msecs); |
| 6410 | } |
Tejun Heo | c0c362b | 2010-09-06 17:57:14 +0200 | [diff] [blame] | 6411 | |
| 6412 | if (owns_eh) |
| 6413 | ata_eh_acquire(ap); |
Tejun Heo | 97750ce | 2010-09-06 17:56:29 +0200 | [diff] [blame] | 6414 | } |
Bartlomiej Zolnierkiewicz | a52fbcf | 2020-03-26 16:58:04 +0100 | [diff] [blame] | 6415 | EXPORT_SYMBOL_GPL(ata_msleep); |
Tejun Heo | 97750ce | 2010-09-06 17:56:29 +0200 | [diff] [blame] | 6416 | |
Tejun Heo | c22daff | 2006-04-11 22:22:29 +0900 | [diff] [blame] | 6417 | /** |
| 6418 | * ata_wait_register - wait until register value changes |
Tejun Heo | 97750ce | 2010-09-06 17:56:29 +0200 | [diff] [blame] | 6419 | * @ap: ATA port to wait register for, can be NULL |
Tejun Heo | c22daff | 2006-04-11 22:22:29 +0900 | [diff] [blame] | 6420 | * @reg: IO-mapped register |
| 6421 | * @mask: Mask to apply to read register value |
| 6422 | * @val: Wait condition |
Tejun Heo | 341c2c9 | 2008-05-20 02:17:51 +0900 | [diff] [blame] | 6423 | * @interval: polling interval in milliseconds |
| 6424 | * @timeout: timeout in milliseconds |
Tejun Heo | c22daff | 2006-04-11 22:22:29 +0900 | [diff] [blame] | 6425 | * |
| 6426 | * Waiting for some bits of register to change is a common |
| 6427 | * operation for ATA controllers. This function reads 32bit LE |
| 6428 | * IO-mapped register @reg and tests for the following condition. |
| 6429 | * |
| 6430 | * (*@reg & mask) != val |
| 6431 | * |
| 6432 | * If the condition is met, it returns; otherwise, the process is |
| 6433 | * repeated after @interval_msec until timeout. |
| 6434 | * |
| 6435 | * LOCKING: |
| 6436 | * Kernel thread context (may sleep) |
| 6437 | * |
| 6438 | * RETURNS: |
| 6439 | * The final register value. |
| 6440 | */ |
Tejun Heo | 97750ce | 2010-09-06 17:56:29 +0200 | [diff] [blame] | 6441 | u32 ata_wait_register(struct ata_port *ap, void __iomem *reg, u32 mask, u32 val, |
Tejun Heo | 341c2c9 | 2008-05-20 02:17:51 +0900 | [diff] [blame] | 6442 | unsigned long interval, unsigned long timeout) |
Tejun Heo | c22daff | 2006-04-11 22:22:29 +0900 | [diff] [blame] | 6443 | { |
Tejun Heo | 341c2c9 | 2008-05-20 02:17:51 +0900 | [diff] [blame] | 6444 | unsigned long deadline; |
Tejun Heo | c22daff | 2006-04-11 22:22:29 +0900 | [diff] [blame] | 6445 | u32 tmp; |
| 6446 | |
| 6447 | tmp = ioread32(reg); |
| 6448 | |
| 6449 | /* Calculate timeout _after_ the first read to make sure |
| 6450 | * preceding writes reach the controller before starting to |
| 6451 | * eat away the timeout. |
| 6452 | */ |
Tejun Heo | 341c2c9 | 2008-05-20 02:17:51 +0900 | [diff] [blame] | 6453 | deadline = ata_deadline(jiffies, timeout); |
Tejun Heo | c22daff | 2006-04-11 22:22:29 +0900 | [diff] [blame] | 6454 | |
Tejun Heo | 341c2c9 | 2008-05-20 02:17:51 +0900 | [diff] [blame] | 6455 | while ((tmp & mask) == val && time_before(jiffies, deadline)) { |
Tejun Heo | 97750ce | 2010-09-06 17:56:29 +0200 | [diff] [blame] | 6456 | ata_msleep(ap, interval); |
Tejun Heo | c22daff | 2006-04-11 22:22:29 +0900 | [diff] [blame] | 6457 | tmp = ioread32(reg); |
| 6458 | } |
| 6459 | |
| 6460 | return tmp; |
| 6461 | } |
Bartlomiej Zolnierkiewicz | a52fbcf | 2020-03-26 16:58:04 +0100 | [diff] [blame] | 6462 | EXPORT_SYMBOL_GPL(ata_wait_register); |
Tejun Heo | c22daff | 2006-04-11 22:22:29 +0900 | [diff] [blame] | 6463 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6464 | /* |
Tejun Heo | dd5b06c | 2006-08-10 16:59:12 +0900 | [diff] [blame] | 6465 | * Dummy port_ops |
| 6466 | */ |
Tejun Heo | dd5b06c | 2006-08-10 16:59:12 +0900 | [diff] [blame] | 6467 | static unsigned int ata_dummy_qc_issue(struct ata_queued_cmd *qc) |
| 6468 | { |
| 6469 | return AC_ERR_SYSTEM; |
| 6470 | } |
| 6471 | |
Tejun Heo | 182d7bb | 2008-04-07 22:47:21 +0900 | [diff] [blame] | 6472 | static void ata_dummy_error_handler(struct ata_port *ap) |
| 6473 | { |
| 6474 | /* truly dummy */ |
| 6475 | } |
| 6476 | |
Tejun Heo | 029cfd6 | 2008-03-25 12:22:49 +0900 | [diff] [blame] | 6477 | struct ata_port_operations ata_dummy_port_ops = { |
Tejun Heo | dd5b06c | 2006-08-10 16:59:12 +0900 | [diff] [blame] | 6478 | .qc_prep = ata_noop_qc_prep, |
| 6479 | .qc_issue = ata_dummy_qc_issue, |
Tejun Heo | 182d7bb | 2008-04-07 22:47:21 +0900 | [diff] [blame] | 6480 | .error_handler = ata_dummy_error_handler, |
Dan Williams | e4a9c37 | 2012-06-21 23:25:27 -0700 | [diff] [blame] | 6481 | .sched_eh = ata_std_sched_eh, |
| 6482 | .end_eh = ata_std_end_eh, |
Tejun Heo | dd5b06c | 2006-08-10 16:59:12 +0900 | [diff] [blame] | 6483 | }; |
Bartlomiej Zolnierkiewicz | a52fbcf | 2020-03-26 16:58:04 +0100 | [diff] [blame] | 6484 | EXPORT_SYMBOL_GPL(ata_dummy_port_ops); |
Tejun Heo | dd5b06c | 2006-08-10 16:59:12 +0900 | [diff] [blame] | 6485 | |
Tejun Heo | 21b0ad4 | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 6486 | const struct ata_port_info ata_dummy_port_info = { |
| 6487 | .port_ops = &ata_dummy_port_ops, |
| 6488 | }; |
Bartlomiej Zolnierkiewicz | a52fbcf | 2020-03-26 16:58:04 +0100 | [diff] [blame] | 6489 | EXPORT_SYMBOL_GPL(ata_dummy_port_info); |
Tejun Heo | 21b0ad4 | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 6490 | |
Joe Perches | 06296a1 | 2011-04-15 15:52:00 -0700 | [diff] [blame] | 6491 | void ata_print_version(const struct device *dev, const char *version) |
| 6492 | { |
| 6493 | dev_printk(KERN_DEBUG, dev, "version %s\n", version); |
| 6494 | } |
| 6495 | EXPORT_SYMBOL(ata_print_version); |
Hannes Reinecke | c206a38 | 2021-12-21 08:20:31 +0100 | [diff] [blame] | 6496 | |
| 6497 | EXPORT_TRACEPOINT_SYMBOL_GPL(ata_tf_load); |
| 6498 | EXPORT_TRACEPOINT_SYMBOL_GPL(ata_exec_command); |
| 6499 | EXPORT_TRACEPOINT_SYMBOL_GPL(ata_bmdma_setup); |
| 6500 | EXPORT_TRACEPOINT_SYMBOL_GPL(ata_bmdma_start); |
| 6501 | EXPORT_TRACEPOINT_SYMBOL_GPL(ata_bmdma_status); |