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 | |
| 767 | DPRINTK("block %u track %u cyl %u head %u sect %u\n", |
| 768 | (u32)block, track, cyl, head, sect); |
| 769 | |
| 770 | /* Check whether the converted CHS can fit. |
| 771 | Cylinder: 0-65535 |
| 772 | Head: 0-15 |
| 773 | Sector: 1-255*/ |
| 774 | if ((cyl >> 16) || (head >> 4) || (sect >> 8) || (!sect)) |
| 775 | return -ERANGE; |
| 776 | |
| 777 | tf->nsect = n_block & 0xff; /* Sector count 0 means 256 sectors */ |
| 778 | tf->lbal = sect; |
| 779 | tf->lbam = cyl; |
| 780 | tf->lbah = cyl >> 8; |
| 781 | tf->device |= head; |
| 782 | } |
| 783 | |
| 784 | return 0; |
| 785 | } |
| 786 | |
| 787 | /** |
Tejun Heo | cb95d56 | 2006-03-06 04:31:56 +0900 | [diff] [blame] | 788 | * ata_pack_xfermask - Pack pio, mwdma and udma masks into xfer_mask |
| 789 | * @pio_mask: pio_mask |
| 790 | * @mwdma_mask: mwdma_mask |
| 791 | * @udma_mask: udma_mask |
| 792 | * |
| 793 | * Pack @pio_mask, @mwdma_mask and @udma_mask into a single |
| 794 | * unsigned int xfer_mask. |
| 795 | * |
| 796 | * LOCKING: |
| 797 | * None. |
| 798 | * |
| 799 | * RETURNS: |
| 800 | * Packed xfer_mask. |
| 801 | */ |
Tejun Heo | 7dc951a | 2007-11-27 19:43:42 +0900 | [diff] [blame] | 802 | unsigned long ata_pack_xfermask(unsigned long pio_mask, |
| 803 | unsigned long mwdma_mask, |
| 804 | unsigned long udma_mask) |
Tejun Heo | cb95d56 | 2006-03-06 04:31:56 +0900 | [diff] [blame] | 805 | { |
| 806 | return ((pio_mask << ATA_SHIFT_PIO) & ATA_MASK_PIO) | |
| 807 | ((mwdma_mask << ATA_SHIFT_MWDMA) & ATA_MASK_MWDMA) | |
| 808 | ((udma_mask << ATA_SHIFT_UDMA) & ATA_MASK_UDMA); |
| 809 | } |
Bartlomiej Zolnierkiewicz | a52fbcf | 2020-03-26 16:58:04 +0100 | [diff] [blame] | 810 | EXPORT_SYMBOL_GPL(ata_pack_xfermask); |
Tejun Heo | cb95d56 | 2006-03-06 04:31:56 +0900 | [diff] [blame] | 811 | |
Tejun Heo | c0489e4 | 2006-03-24 14:07:49 +0900 | [diff] [blame] | 812 | /** |
| 813 | * ata_unpack_xfermask - Unpack xfer_mask into pio, mwdma and udma masks |
| 814 | * @xfer_mask: xfer_mask to unpack |
| 815 | * @pio_mask: resulting pio_mask |
| 816 | * @mwdma_mask: resulting mwdma_mask |
| 817 | * @udma_mask: resulting udma_mask |
| 818 | * |
| 819 | * Unpack @xfer_mask into @pio_mask, @mwdma_mask and @udma_mask. |
Masanari Iida | c9b5560 | 2016-04-13 23:36:27 +0900 | [diff] [blame] | 820 | * Any NULL destination masks will be ignored. |
Tejun Heo | c0489e4 | 2006-03-24 14:07:49 +0900 | [diff] [blame] | 821 | */ |
Tejun Heo | 7dc951a | 2007-11-27 19:43:42 +0900 | [diff] [blame] | 822 | void ata_unpack_xfermask(unsigned long xfer_mask, unsigned long *pio_mask, |
| 823 | unsigned long *mwdma_mask, unsigned long *udma_mask) |
Tejun Heo | c0489e4 | 2006-03-24 14:07:49 +0900 | [diff] [blame] | 824 | { |
| 825 | if (pio_mask) |
| 826 | *pio_mask = (xfer_mask & ATA_MASK_PIO) >> ATA_SHIFT_PIO; |
| 827 | if (mwdma_mask) |
| 828 | *mwdma_mask = (xfer_mask & ATA_MASK_MWDMA) >> ATA_SHIFT_MWDMA; |
| 829 | if (udma_mask) |
| 830 | *udma_mask = (xfer_mask & ATA_MASK_UDMA) >> ATA_SHIFT_UDMA; |
| 831 | } |
| 832 | |
Tejun Heo | cb95d56 | 2006-03-06 04:31:56 +0900 | [diff] [blame] | 833 | static const struct ata_xfer_ent { |
Tejun Heo | be9a50c | 2006-03-31 22:48:52 +0900 | [diff] [blame] | 834 | int shift, bits; |
Tejun Heo | cb95d56 | 2006-03-06 04:31:56 +0900 | [diff] [blame] | 835 | u8 base; |
| 836 | } ata_xfer_tbl[] = { |
Tejun Heo | 70cd071 | 2007-11-27 19:43:40 +0900 | [diff] [blame] | 837 | { ATA_SHIFT_PIO, ATA_NR_PIO_MODES, XFER_PIO_0 }, |
| 838 | { ATA_SHIFT_MWDMA, ATA_NR_MWDMA_MODES, XFER_MW_DMA_0 }, |
| 839 | { ATA_SHIFT_UDMA, ATA_NR_UDMA_MODES, XFER_UDMA_0 }, |
Tejun Heo | cb95d56 | 2006-03-06 04:31:56 +0900 | [diff] [blame] | 840 | { -1, }, |
| 841 | }; |
| 842 | |
| 843 | /** |
| 844 | * ata_xfer_mask2mode - Find matching XFER_* for the given xfer_mask |
| 845 | * @xfer_mask: xfer_mask of interest |
| 846 | * |
| 847 | * Return matching XFER_* value for @xfer_mask. Only the highest |
| 848 | * bit of @xfer_mask is considered. |
| 849 | * |
| 850 | * LOCKING: |
| 851 | * None. |
| 852 | * |
| 853 | * RETURNS: |
Tejun Heo | 70cd071 | 2007-11-27 19:43:40 +0900 | [diff] [blame] | 854 | * Matching XFER_* value, 0xff if no match found. |
Tejun Heo | cb95d56 | 2006-03-06 04:31:56 +0900 | [diff] [blame] | 855 | */ |
Tejun Heo | 7dc951a | 2007-11-27 19:43:42 +0900 | [diff] [blame] | 856 | u8 ata_xfer_mask2mode(unsigned long xfer_mask) |
Tejun Heo | cb95d56 | 2006-03-06 04:31:56 +0900 | [diff] [blame] | 857 | { |
| 858 | int highbit = fls(xfer_mask) - 1; |
| 859 | const struct ata_xfer_ent *ent; |
| 860 | |
| 861 | for (ent = ata_xfer_tbl; ent->shift >= 0; ent++) |
| 862 | if (highbit >= ent->shift && highbit < ent->shift + ent->bits) |
| 863 | return ent->base + highbit - ent->shift; |
Tejun Heo | 70cd071 | 2007-11-27 19:43:40 +0900 | [diff] [blame] | 864 | return 0xff; |
Tejun Heo | cb95d56 | 2006-03-06 04:31:56 +0900 | [diff] [blame] | 865 | } |
Bartlomiej Zolnierkiewicz | a52fbcf | 2020-03-26 16:58:04 +0100 | [diff] [blame] | 866 | EXPORT_SYMBOL_GPL(ata_xfer_mask2mode); |
Tejun Heo | cb95d56 | 2006-03-06 04:31:56 +0900 | [diff] [blame] | 867 | |
| 868 | /** |
| 869 | * ata_xfer_mode2mask - Find matching xfer_mask for XFER_* |
| 870 | * @xfer_mode: XFER_* of interest |
| 871 | * |
| 872 | * Return matching xfer_mask for @xfer_mode. |
| 873 | * |
| 874 | * LOCKING: |
| 875 | * None. |
| 876 | * |
| 877 | * RETURNS: |
| 878 | * Matching xfer_mask, 0 if no match found. |
| 879 | */ |
Tejun Heo | 7dc951a | 2007-11-27 19:43:42 +0900 | [diff] [blame] | 880 | unsigned long ata_xfer_mode2mask(u8 xfer_mode) |
Tejun Heo | cb95d56 | 2006-03-06 04:31:56 +0900 | [diff] [blame] | 881 | { |
| 882 | const struct ata_xfer_ent *ent; |
| 883 | |
| 884 | for (ent = ata_xfer_tbl; ent->shift >= 0; ent++) |
| 885 | if (xfer_mode >= ent->base && xfer_mode < ent->base + ent->bits) |
Tejun Heo | 70cd071 | 2007-11-27 19:43:40 +0900 | [diff] [blame] | 886 | return ((2 << (ent->shift + xfer_mode - ent->base)) - 1) |
| 887 | & ~((1 << ent->shift) - 1); |
Tejun Heo | cb95d56 | 2006-03-06 04:31:56 +0900 | [diff] [blame] | 888 | return 0; |
| 889 | } |
Bartlomiej Zolnierkiewicz | a52fbcf | 2020-03-26 16:58:04 +0100 | [diff] [blame] | 890 | EXPORT_SYMBOL_GPL(ata_xfer_mode2mask); |
Tejun Heo | cb95d56 | 2006-03-06 04:31:56 +0900 | [diff] [blame] | 891 | |
| 892 | /** |
| 893 | * ata_xfer_mode2shift - Find matching xfer_shift for XFER_* |
| 894 | * @xfer_mode: XFER_* of interest |
| 895 | * |
| 896 | * Return matching xfer_shift for @xfer_mode. |
| 897 | * |
| 898 | * LOCKING: |
| 899 | * None. |
| 900 | * |
| 901 | * RETURNS: |
| 902 | * Matching xfer_shift, -1 if no match found. |
| 903 | */ |
Tejun Heo | 7dc951a | 2007-11-27 19:43:42 +0900 | [diff] [blame] | 904 | int ata_xfer_mode2shift(unsigned long xfer_mode) |
Tejun Heo | cb95d56 | 2006-03-06 04:31:56 +0900 | [diff] [blame] | 905 | { |
| 906 | const struct ata_xfer_ent *ent; |
| 907 | |
| 908 | for (ent = ata_xfer_tbl; ent->shift >= 0; ent++) |
| 909 | if (xfer_mode >= ent->base && xfer_mode < ent->base + ent->bits) |
| 910 | return ent->shift; |
| 911 | return -1; |
| 912 | } |
Bartlomiej Zolnierkiewicz | a52fbcf | 2020-03-26 16:58:04 +0100 | [diff] [blame] | 913 | EXPORT_SYMBOL_GPL(ata_xfer_mode2shift); |
Tejun Heo | cb95d56 | 2006-03-06 04:31:56 +0900 | [diff] [blame] | 914 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 915 | /** |
Tejun Heo | 1da7b0d | 2006-03-06 04:31:56 +0900 | [diff] [blame] | 916 | * ata_mode_string - convert xfer_mask to string |
| 917 | * @xfer_mask: mask of bits supported; only highest bit counts. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 918 | * |
| 919 | * Determine string which represents the highest speed |
Tejun Heo | 1da7b0d | 2006-03-06 04:31:56 +0900 | [diff] [blame] | 920 | * (highest bit in @modemask). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 921 | * |
| 922 | * LOCKING: |
| 923 | * None. |
| 924 | * |
| 925 | * RETURNS: |
| 926 | * Constant C string representing highest speed listed in |
Tejun Heo | 1da7b0d | 2006-03-06 04:31:56 +0900 | [diff] [blame] | 927 | * @mode_mask, or the constant C string "<n/a>". |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 928 | */ |
Tejun Heo | 7dc951a | 2007-11-27 19:43:42 +0900 | [diff] [blame] | 929 | const char *ata_mode_string(unsigned long xfer_mask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 930 | { |
Tejun Heo | 75f554b | 2006-03-06 04:31:57 +0900 | [diff] [blame] | 931 | static const char * const xfer_mode_str[] = { |
| 932 | "PIO0", |
| 933 | "PIO1", |
| 934 | "PIO2", |
| 935 | "PIO3", |
| 936 | "PIO4", |
Alan Cox | b352e57 | 2006-08-10 18:52:12 +0100 | [diff] [blame] | 937 | "PIO5", |
| 938 | "PIO6", |
Tejun Heo | 75f554b | 2006-03-06 04:31:57 +0900 | [diff] [blame] | 939 | "MWDMA0", |
| 940 | "MWDMA1", |
| 941 | "MWDMA2", |
Alan Cox | b352e57 | 2006-08-10 18:52:12 +0100 | [diff] [blame] | 942 | "MWDMA3", |
| 943 | "MWDMA4", |
Tejun Heo | 75f554b | 2006-03-06 04:31:57 +0900 | [diff] [blame] | 944 | "UDMA/16", |
| 945 | "UDMA/25", |
| 946 | "UDMA/33", |
| 947 | "UDMA/44", |
| 948 | "UDMA/66", |
| 949 | "UDMA/100", |
| 950 | "UDMA/133", |
| 951 | "UDMA7", |
| 952 | }; |
Tejun Heo | 1da7b0d | 2006-03-06 04:31:56 +0900 | [diff] [blame] | 953 | int highbit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 954 | |
Tejun Heo | 1da7b0d | 2006-03-06 04:31:56 +0900 | [diff] [blame] | 955 | highbit = fls(xfer_mask) - 1; |
| 956 | if (highbit >= 0 && highbit < ARRAY_SIZE(xfer_mode_str)) |
| 957 | return xfer_mode_str[highbit]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 958 | return "<n/a>"; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 959 | } |
Bartlomiej Zolnierkiewicz | a52fbcf | 2020-03-26 16:58:04 +0100 | [diff] [blame] | 960 | EXPORT_SYMBOL_GPL(ata_mode_string); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 961 | |
Gwendal Grignou | d902747 | 2010-05-25 12:31:38 -0700 | [diff] [blame] | 962 | const char *sata_spd_string(unsigned int spd) |
Tejun Heo | 4c360c8 | 2006-04-01 01:38:17 +0900 | [diff] [blame] | 963 | { |
| 964 | static const char * const spd_str[] = { |
| 965 | "1.5 Gbps", |
| 966 | "3.0 Gbps", |
Shane Huang | 8522ee2 | 2008-12-30 11:00:37 +0800 | [diff] [blame] | 967 | "6.0 Gbps", |
Tejun Heo | 4c360c8 | 2006-04-01 01:38:17 +0900 | [diff] [blame] | 968 | }; |
| 969 | |
| 970 | if (spd == 0 || (spd - 1) >= ARRAY_SIZE(spd_str)) |
| 971 | return "<unknown>"; |
| 972 | return spd_str[spd - 1]; |
| 973 | } |
| 974 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 975 | /** |
| 976 | * ata_dev_classify - determine device type based on ATA-spec signature |
| 977 | * @tf: ATA taskfile register set for device to be identified |
| 978 | * |
| 979 | * Determine from taskfile register contents whether a device is |
| 980 | * ATA or ATAPI, as per "Signature and persistence" section |
| 981 | * of ATA/PI spec (volume 1, sect 5.14). |
| 982 | * |
| 983 | * LOCKING: |
| 984 | * None. |
| 985 | * |
| 986 | * RETURNS: |
Hannes Reinecke | 9162c65 | 2014-11-05 13:08:21 +0100 | [diff] [blame] | 987 | * Device type, %ATA_DEV_ATA, %ATA_DEV_ATAPI, %ATA_DEV_PMP, |
| 988 | * %ATA_DEV_ZAC, or %ATA_DEV_UNKNOWN the event of failure. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 989 | */ |
Jeff Garzik | 057ace5 | 2005-10-22 14:27:05 -0400 | [diff] [blame] | 990 | unsigned int ata_dev_classify(const struct ata_taskfile *tf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 991 | { |
| 992 | /* Apple's open source Darwin code hints that some devices only |
| 993 | * put a proper signature into the LBA mid/high registers, |
| 994 | * So, we only check those. It's sufficient for uniqueness. |
Tejun Heo | 633273a | 2007-09-23 13:19:54 +0900 | [diff] [blame] | 995 | * |
| 996 | * ATA/ATAPI-7 (d1532v1r1: Feb. 19, 2003) specified separate |
| 997 | * signatures for ATA and ATAPI devices attached on SerialATA, |
| 998 | * 0x3c/0xc3 and 0x69/0x96 respectively. However, SerialATA |
| 999 | * spec has never mentioned about using different signatures |
| 1000 | * for ATA/ATAPI devices. Then, Serial ATA II: Port |
| 1001 | * Multiplier specification began to use 0x69/0x96 to identify |
| 1002 | * port multpliers and 0x3c/0xc3 to identify SEMB device. |
| 1003 | * ATA/ATAPI-7 dropped descriptions about 0x3c/0xc3 and |
| 1004 | * 0x69/0x96 shortly and described them as reserved for |
| 1005 | * SerialATA. |
| 1006 | * |
| 1007 | * We follow the current spec and consider that 0x69/0x96 |
| 1008 | * identifies a port multiplier and 0x3c/0xc3 a SEMB device. |
Tejun Heo | 79b42ba | 2009-04-15 06:21:10 +0900 | [diff] [blame] | 1009 | * Unfortunately, WDC WD1600JS-62MHB5 (a hard drive) reports |
| 1010 | * SEMB signature. This is worked around in |
| 1011 | * ata_dev_read_id(). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1012 | */ |
Tejun Heo | 633273a | 2007-09-23 13:19:54 +0900 | [diff] [blame] | 1013 | if ((tf->lbam == 0) && (tf->lbah == 0)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1014 | DPRINTK("found ATA device by sig\n"); |
| 1015 | return ATA_DEV_ATA; |
| 1016 | } |
| 1017 | |
Tejun Heo | 633273a | 2007-09-23 13:19:54 +0900 | [diff] [blame] | 1018 | if ((tf->lbam == 0x14) && (tf->lbah == 0xeb)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1019 | DPRINTK("found ATAPI device by sig\n"); |
| 1020 | return ATA_DEV_ATAPI; |
| 1021 | } |
| 1022 | |
Tejun Heo | 633273a | 2007-09-23 13:19:54 +0900 | [diff] [blame] | 1023 | if ((tf->lbam == 0x69) && (tf->lbah == 0x96)) { |
| 1024 | DPRINTK("found PMP device by sig\n"); |
| 1025 | return ATA_DEV_PMP; |
| 1026 | } |
| 1027 | |
| 1028 | if ((tf->lbam == 0x3c) && (tf->lbah == 0xc3)) { |
Tejun Heo | 79b42ba | 2009-04-15 06:21:10 +0900 | [diff] [blame] | 1029 | DPRINTK("found SEMB device by sig (could be ATA device)\n"); |
| 1030 | return ATA_DEV_SEMB; |
Tejun Heo | 633273a | 2007-09-23 13:19:54 +0900 | [diff] [blame] | 1031 | } |
| 1032 | |
Hannes Reinecke | 9162c65 | 2014-11-05 13:08:21 +0100 | [diff] [blame] | 1033 | if ((tf->lbam == 0xcd) && (tf->lbah == 0xab)) { |
| 1034 | DPRINTK("found ZAC device by sig\n"); |
| 1035 | return ATA_DEV_ZAC; |
| 1036 | } |
| 1037 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1038 | DPRINTK("unknown device\n"); |
| 1039 | return ATA_DEV_UNKNOWN; |
| 1040 | } |
Bartlomiej Zolnierkiewicz | a52fbcf | 2020-03-26 16:58:04 +0100 | [diff] [blame] | 1041 | EXPORT_SYMBOL_GPL(ata_dev_classify); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1042 | |
| 1043 | /** |
Tejun Heo | 6a62a04 | 2006-02-13 10:02:46 +0900 | [diff] [blame] | 1044 | * ata_id_string - Convert IDENTIFY DEVICE page into string |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1045 | * @id: IDENTIFY DEVICE results we will examine |
| 1046 | * @s: string into which data is output |
| 1047 | * @ofs: offset into identify device page |
| 1048 | * @len: length of string to return. must be an even number. |
| 1049 | * |
| 1050 | * The strings in the IDENTIFY DEVICE page are broken up into |
| 1051 | * 16-bit chunks. Run through the string, and output each |
| 1052 | * 8-bit chunk linearly, regardless of platform. |
| 1053 | * |
| 1054 | * LOCKING: |
| 1055 | * caller. |
| 1056 | */ |
| 1057 | |
Tejun Heo | 6a62a04 | 2006-02-13 10:02:46 +0900 | [diff] [blame] | 1058 | void ata_id_string(const u16 *id, unsigned char *s, |
| 1059 | unsigned int ofs, unsigned int len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1060 | { |
| 1061 | unsigned int c; |
| 1062 | |
Alan Cox | 963e497 | 2008-07-24 17:16:06 +0100 | [diff] [blame] | 1063 | BUG_ON(len & 1); |
| 1064 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1065 | while (len > 0) { |
| 1066 | c = id[ofs] >> 8; |
| 1067 | *s = c; |
| 1068 | s++; |
| 1069 | |
| 1070 | c = id[ofs] & 0xff; |
| 1071 | *s = c; |
| 1072 | s++; |
| 1073 | |
| 1074 | ofs++; |
| 1075 | len -= 2; |
| 1076 | } |
| 1077 | } |
Bartlomiej Zolnierkiewicz | a52fbcf | 2020-03-26 16:58:04 +0100 | [diff] [blame] | 1078 | EXPORT_SYMBOL_GPL(ata_id_string); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1079 | |
Tejun Heo | 0e949ff | 2006-02-12 22:47:04 +0900 | [diff] [blame] | 1080 | /** |
Tejun Heo | 6a62a04 | 2006-02-13 10:02:46 +0900 | [diff] [blame] | 1081 | * ata_id_c_string - Convert IDENTIFY DEVICE page into C string |
Tejun Heo | 0e949ff | 2006-02-12 22:47:04 +0900 | [diff] [blame] | 1082 | * @id: IDENTIFY DEVICE results we will examine |
| 1083 | * @s: string into which data is output |
| 1084 | * @ofs: offset into identify device page |
| 1085 | * @len: length of string to return. must be an odd number. |
| 1086 | * |
Tejun Heo | 6a62a04 | 2006-02-13 10:02:46 +0900 | [diff] [blame] | 1087 | * This function is identical to ata_id_string except that it |
Tejun Heo | 0e949ff | 2006-02-12 22:47:04 +0900 | [diff] [blame] | 1088 | * trims trailing spaces and terminates the resulting string with |
| 1089 | * null. @len must be actual maximum length (even number) + 1. |
| 1090 | * |
| 1091 | * LOCKING: |
| 1092 | * caller. |
| 1093 | */ |
Tejun Heo | 6a62a04 | 2006-02-13 10:02:46 +0900 | [diff] [blame] | 1094 | void ata_id_c_string(const u16 *id, unsigned char *s, |
| 1095 | unsigned int ofs, unsigned int len) |
Tejun Heo | 0e949ff | 2006-02-12 22:47:04 +0900 | [diff] [blame] | 1096 | { |
| 1097 | unsigned char *p; |
| 1098 | |
Tejun Heo | 6a62a04 | 2006-02-13 10:02:46 +0900 | [diff] [blame] | 1099 | ata_id_string(id, s, ofs, len - 1); |
Tejun Heo | 0e949ff | 2006-02-12 22:47:04 +0900 | [diff] [blame] | 1100 | |
| 1101 | p = s + strnlen(s, len - 1); |
| 1102 | while (p > s && p[-1] == ' ') |
| 1103 | p--; |
| 1104 | *p = '\0'; |
| 1105 | } |
Bartlomiej Zolnierkiewicz | a52fbcf | 2020-03-26 16:58:04 +0100 | [diff] [blame] | 1106 | EXPORT_SYMBOL_GPL(ata_id_c_string); |
Edward Falk | 0baab86 | 2005-06-02 18:17:13 -0400 | [diff] [blame] | 1107 | |
Tejun Heo | db6f875 | 2007-09-03 12:31:58 +0900 | [diff] [blame] | 1108 | static u64 ata_id_n_sectors(const u16 *id) |
| 1109 | { |
| 1110 | if (ata_id_has_lba(id)) { |
| 1111 | if (ata_id_has_lba48(id)) |
Robert Hancock | 968e594 | 2009-02-16 20:15:08 -0600 | [diff] [blame] | 1112 | return ata_id_u64(id, ATA_ID_LBA_CAPACITY_2); |
Tejun Heo | db6f875 | 2007-09-03 12:31:58 +0900 | [diff] [blame] | 1113 | else |
Robert Hancock | 968e594 | 2009-02-16 20:15:08 -0600 | [diff] [blame] | 1114 | return ata_id_u32(id, ATA_ID_LBA_CAPACITY); |
Tejun Heo | db6f875 | 2007-09-03 12:31:58 +0900 | [diff] [blame] | 1115 | } else { |
| 1116 | if (ata_id_current_chs_valid(id)) |
Robert Hancock | 968e594 | 2009-02-16 20:15:08 -0600 | [diff] [blame] | 1117 | return id[ATA_ID_CUR_CYLS] * id[ATA_ID_CUR_HEADS] * |
| 1118 | id[ATA_ID_CUR_SECTORS]; |
Tejun Heo | db6f875 | 2007-09-03 12:31:58 +0900 | [diff] [blame] | 1119 | else |
Robert Hancock | 968e594 | 2009-02-16 20:15:08 -0600 | [diff] [blame] | 1120 | return id[ATA_ID_CYLS] * id[ATA_ID_HEADS] * |
| 1121 | id[ATA_ID_SECTORS]; |
Tejun Heo | db6f875 | 2007-09-03 12:31:58 +0900 | [diff] [blame] | 1122 | } |
| 1123 | } |
| 1124 | |
Tejun Heo | a5987e0 | 2008-03-27 19:14:23 +0900 | [diff] [blame] | 1125 | u64 ata_tf_to_lba48(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 |= ((u64)(tf->hob_lbah & 0xff)) << 40; |
| 1130 | sectors |= ((u64)(tf->hob_lbam & 0xff)) << 32; |
Roland Dreier | ba14a9c | 2008-10-28 16:52:20 -0700 | [diff] [blame] | 1131 | sectors |= ((u64)(tf->hob_lbal & 0xff)) << 24; |
Alan Cox | 1e99973 | 2007-04-11 00:23:13 +0100 | [diff] [blame] | 1132 | sectors |= (tf->lbah & 0xff) << 16; |
| 1133 | sectors |= (tf->lbam & 0xff) << 8; |
| 1134 | sectors |= (tf->lbal & 0xff); |
| 1135 | |
Tejun Heo | a5987e0 | 2008-03-27 19:14:23 +0900 | [diff] [blame] | 1136 | return sectors; |
Alan Cox | 1e99973 | 2007-04-11 00:23:13 +0100 | [diff] [blame] | 1137 | } |
| 1138 | |
Tejun Heo | a5987e0 | 2008-03-27 19:14:23 +0900 | [diff] [blame] | 1139 | u64 ata_tf_to_lba(const struct ata_taskfile *tf) |
Alan Cox | 1e99973 | 2007-04-11 00:23:13 +0100 | [diff] [blame] | 1140 | { |
| 1141 | u64 sectors = 0; |
| 1142 | |
| 1143 | sectors |= (tf->device & 0x0f) << 24; |
| 1144 | sectors |= (tf->lbah & 0xff) << 16; |
| 1145 | sectors |= (tf->lbam & 0xff) << 8; |
| 1146 | sectors |= (tf->lbal & 0xff); |
| 1147 | |
Tejun Heo | a5987e0 | 2008-03-27 19:14:23 +0900 | [diff] [blame] | 1148 | return sectors; |
Alan Cox | 1e99973 | 2007-04-11 00:23:13 +0100 | [diff] [blame] | 1149 | } |
| 1150 | |
| 1151 | /** |
Tejun Heo | c728a91 | 2007-09-03 12:32:30 +0900 | [diff] [blame] | 1152 | * ata_read_native_max_address - Read native max address |
| 1153 | * @dev: target device |
| 1154 | * @max_sectors: out parameter for the result native max address |
Alan Cox | 1e99973 | 2007-04-11 00:23:13 +0100 | [diff] [blame] | 1155 | * |
Tejun Heo | c728a91 | 2007-09-03 12:32:30 +0900 | [diff] [blame] | 1156 | * Perform an LBA48 or LBA28 native size query upon the device in |
| 1157 | * question. |
| 1158 | * |
| 1159 | * RETURNS: |
| 1160 | * 0 on success, -EACCES if command is aborted by the drive. |
| 1161 | * -EIO on other errors. |
Alan Cox | 1e99973 | 2007-04-11 00:23:13 +0100 | [diff] [blame] | 1162 | */ |
Tejun Heo | c728a91 | 2007-09-03 12:32:30 +0900 | [diff] [blame] | 1163 | 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] | 1164 | { |
Tejun Heo | c728a91 | 2007-09-03 12:32:30 +0900 | [diff] [blame] | 1165 | unsigned int err_mask; |
Alan Cox | 1e99973 | 2007-04-11 00:23:13 +0100 | [diff] [blame] | 1166 | struct ata_taskfile tf; |
Tejun Heo | c728a91 | 2007-09-03 12:32:30 +0900 | [diff] [blame] | 1167 | int lba48 = ata_id_has_lba48(dev->id); |
Alan Cox | 1e99973 | 2007-04-11 00:23:13 +0100 | [diff] [blame] | 1168 | |
| 1169 | ata_tf_init(dev, &tf); |
| 1170 | |
Tejun Heo | c728a91 | 2007-09-03 12:32:30 +0900 | [diff] [blame] | 1171 | /* always clear all address registers */ |
Alan Cox | 1e99973 | 2007-04-11 00:23:13 +0100 | [diff] [blame] | 1172 | tf.flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_ISADDR; |
Tejun Heo | c728a91 | 2007-09-03 12:32:30 +0900 | [diff] [blame] | 1173 | |
| 1174 | if (lba48) { |
| 1175 | tf.command = ATA_CMD_READ_NATIVE_MAX_EXT; |
| 1176 | tf.flags |= ATA_TFLAG_LBA48; |
| 1177 | } else |
| 1178 | tf.command = ATA_CMD_READ_NATIVE_MAX; |
| 1179 | |
Hannes Reinecke | bd18bc0 | 2016-07-14 09:05:46 +0900 | [diff] [blame] | 1180 | tf.protocol = ATA_PROT_NODATA; |
Tejun Heo | c728a91 | 2007-09-03 12:32:30 +0900 | [diff] [blame] | 1181 | tf.device |= ATA_LBA; |
Alan Cox | 1e99973 | 2007-04-11 00:23:13 +0100 | [diff] [blame] | 1182 | |
Tejun Heo | 2b78910 | 2007-10-09 15:05:44 +0900 | [diff] [blame] | 1183 | 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] | 1184 | if (err_mask) { |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 1185 | ata_dev_warn(dev, |
| 1186 | "failed to read native max address (err_mask=0x%x)\n", |
| 1187 | err_mask); |
Tejun Heo | c728a91 | 2007-09-03 12:32:30 +0900 | [diff] [blame] | 1188 | if (err_mask == AC_ERR_DEV && (tf.feature & ATA_ABORTED)) |
| 1189 | return -EACCES; |
| 1190 | return -EIO; |
| 1191 | } |
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 | if (lba48) |
Tejun Heo | a5987e0 | 2008-03-27 19:14:23 +0900 | [diff] [blame] | 1194 | *max_sectors = ata_tf_to_lba48(&tf) + 1; |
Tejun Heo | c728a91 | 2007-09-03 12:32:30 +0900 | [diff] [blame] | 1195 | else |
Tejun Heo | a5987e0 | 2008-03-27 19:14:23 +0900 | [diff] [blame] | 1196 | *max_sectors = ata_tf_to_lba(&tf) + 1; |
Jeff Garzik | 2dcb407 | 2007-10-19 06:42:56 -0400 | [diff] [blame] | 1197 | if (dev->horkage & ATA_HORKAGE_HPA_SIZE) |
Alan Cox | 93328e11 | 2007-09-29 04:06:48 -0400 | [diff] [blame] | 1198 | (*max_sectors)--; |
Tejun Heo | c728a91 | 2007-09-03 12:32:30 +0900 | [diff] [blame] | 1199 | return 0; |
Alan Cox | 1e99973 | 2007-04-11 00:23:13 +0100 | [diff] [blame] | 1200 | } |
| 1201 | |
| 1202 | /** |
Tejun Heo | c728a91 | 2007-09-03 12:32:30 +0900 | [diff] [blame] | 1203 | * ata_set_max_sectors - Set max sectors |
| 1204 | * @dev: target device |
Randy Dunlap | 6b38d1d | 2007-05-01 17:35:55 -0700 | [diff] [blame] | 1205 | * @new_sectors: new max sectors value to set for the device |
Alan Cox | 1e99973 | 2007-04-11 00:23:13 +0100 | [diff] [blame] | 1206 | * |
Tejun Heo | c728a91 | 2007-09-03 12:32:30 +0900 | [diff] [blame] | 1207 | * Set max sectors of @dev to @new_sectors. |
| 1208 | * |
| 1209 | * RETURNS: |
| 1210 | * 0 on success, -EACCES if command is aborted or denied (due to |
| 1211 | * previous non-volatile SET_MAX) by the drive. -EIO on other |
| 1212 | * errors. |
Alan Cox | 1e99973 | 2007-04-11 00:23:13 +0100 | [diff] [blame] | 1213 | */ |
Tejun Heo | 05027ad | 2007-09-03 12:32:57 +0900 | [diff] [blame] | 1214 | 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] | 1215 | { |
Tejun Heo | c728a91 | 2007-09-03 12:32:30 +0900 | [diff] [blame] | 1216 | unsigned int err_mask; |
Alan Cox | 1e99973 | 2007-04-11 00:23:13 +0100 | [diff] [blame] | 1217 | struct ata_taskfile tf; |
Tejun Heo | c728a91 | 2007-09-03 12:32:30 +0900 | [diff] [blame] | 1218 | int lba48 = ata_id_has_lba48(dev->id); |
Alan Cox | 1e99973 | 2007-04-11 00:23:13 +0100 | [diff] [blame] | 1219 | |
| 1220 | new_sectors--; |
| 1221 | |
| 1222 | ata_tf_init(dev, &tf); |
| 1223 | |
Alan Cox | 1e99973 | 2007-04-11 00:23:13 +0100 | [diff] [blame] | 1224 | tf.flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_ISADDR; |
Tejun Heo | c728a91 | 2007-09-03 12:32:30 +0900 | [diff] [blame] | 1225 | |
| 1226 | if (lba48) { |
| 1227 | tf.command = ATA_CMD_SET_MAX_EXT; |
| 1228 | tf.flags |= ATA_TFLAG_LBA48; |
| 1229 | |
| 1230 | tf.hob_lbal = (new_sectors >> 24) & 0xff; |
| 1231 | tf.hob_lbam = (new_sectors >> 32) & 0xff; |
| 1232 | tf.hob_lbah = (new_sectors >> 40) & 0xff; |
Tejun Heo | 1e582ba | 2007-09-21 20:07:14 +0900 | [diff] [blame] | 1233 | } else { |
Tejun Heo | c728a91 | 2007-09-03 12:32:30 +0900 | [diff] [blame] | 1234 | tf.command = ATA_CMD_SET_MAX; |
| 1235 | |
Tejun Heo | 1e582ba | 2007-09-21 20:07:14 +0900 | [diff] [blame] | 1236 | tf.device |= (new_sectors >> 24) & 0xf; |
| 1237 | } |
| 1238 | |
Hannes Reinecke | bd18bc0 | 2016-07-14 09:05:46 +0900 | [diff] [blame] | 1239 | tf.protocol = ATA_PROT_NODATA; |
Tejun Heo | c728a91 | 2007-09-03 12:32:30 +0900 | [diff] [blame] | 1240 | tf.device |= ATA_LBA; |
Alan Cox | 1e99973 | 2007-04-11 00:23:13 +0100 | [diff] [blame] | 1241 | |
| 1242 | tf.lbal = (new_sectors >> 0) & 0xff; |
| 1243 | tf.lbam = (new_sectors >> 8) & 0xff; |
| 1244 | tf.lbah = (new_sectors >> 16) & 0xff; |
Alan Cox | 1e99973 | 2007-04-11 00:23:13 +0100 | [diff] [blame] | 1245 | |
Tejun Heo | 2b78910 | 2007-10-09 15:05:44 +0900 | [diff] [blame] | 1246 | 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] | 1247 | if (err_mask) { |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 1248 | ata_dev_warn(dev, |
| 1249 | "failed to set max address (err_mask=0x%x)\n", |
| 1250 | err_mask); |
Tejun Heo | c728a91 | 2007-09-03 12:32:30 +0900 | [diff] [blame] | 1251 | if (err_mask == AC_ERR_DEV && |
| 1252 | (tf.feature & (ATA_ABORTED | ATA_IDNF))) |
| 1253 | return -EACCES; |
| 1254 | return -EIO; |
| 1255 | } |
Alan Cox | 1e99973 | 2007-04-11 00:23:13 +0100 | [diff] [blame] | 1256 | |
Tejun Heo | c728a91 | 2007-09-03 12:32:30 +0900 | [diff] [blame] | 1257 | return 0; |
Alan Cox | 1e99973 | 2007-04-11 00:23:13 +0100 | [diff] [blame] | 1258 | } |
| 1259 | |
| 1260 | /** |
| 1261 | * ata_hpa_resize - Resize a device with an HPA set |
| 1262 | * @dev: Device to resize |
| 1263 | * |
| 1264 | * Read the size of an LBA28 or LBA48 disk with HPA features and resize |
| 1265 | * it if required to the full size of the media. The caller must check |
| 1266 | * the drive has the HPA feature set enabled. |
Tejun Heo | 05027ad | 2007-09-03 12:32:57 +0900 | [diff] [blame] | 1267 | * |
| 1268 | * RETURNS: |
| 1269 | * 0 on success, -errno on failure. |
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 | static int ata_hpa_resize(struct ata_device *dev) |
Alan Cox | 1e99973 | 2007-04-11 00:23:13 +0100 | [diff] [blame] | 1272 | { |
Damien Le Moal | 891fd7c | 2021-08-16 10:44:50 +0900 | [diff] [blame] | 1273 | bool print_info = ata_dev_print_info(dev); |
Tejun Heo | 445d211 | 2010-04-05 10:33:13 +0900 | [diff] [blame] | 1274 | bool unlock_hpa = ata_ignore_hpa || dev->flags & ATA_DFLAG_UNLOCK_HPA; |
Tejun Heo | 05027ad | 2007-09-03 12:32:57 +0900 | [diff] [blame] | 1275 | u64 sectors = ata_id_n_sectors(dev->id); |
| 1276 | u64 native_sectors; |
Tejun Heo | c728a91 | 2007-09-03 12:32:30 +0900 | [diff] [blame] | 1277 | int rc; |
Jeff Garzik | a617c09 | 2007-05-21 20:14:23 -0400 | [diff] [blame] | 1278 | |
Tejun Heo | 05027ad | 2007-09-03 12:32:57 +0900 | [diff] [blame] | 1279 | /* do we need to do it? */ |
Hannes Reinecke | 9162c65 | 2014-11-05 13:08:21 +0100 | [diff] [blame] | 1280 | if ((dev->class != ATA_DEV_ATA && dev->class != ATA_DEV_ZAC) || |
Tejun Heo | 05027ad | 2007-09-03 12:32:57 +0900 | [diff] [blame] | 1281 | !ata_id_has_lba(dev->id) || !ata_id_hpa_enabled(dev->id) || |
| 1282 | (dev->horkage & ATA_HORKAGE_BROKEN_HPA)) |
Tejun Heo | c728a91 | 2007-09-03 12:32:30 +0900 | [diff] [blame] | 1283 | return 0; |
Alan Cox | 1e99973 | 2007-04-11 00:23:13 +0100 | [diff] [blame] | 1284 | |
Tejun Heo | 05027ad | 2007-09-03 12:32:57 +0900 | [diff] [blame] | 1285 | /* read native max address */ |
| 1286 | rc = ata_read_native_max_address(dev, &native_sectors); |
| 1287 | if (rc) { |
Tejun Heo | dda7aba | 2008-03-23 21:05:15 +0900 | [diff] [blame] | 1288 | /* If device aborted the command or HPA isn't going to |
| 1289 | * be unlocked, skip HPA resizing. |
Tejun Heo | 05027ad | 2007-09-03 12:32:57 +0900 | [diff] [blame] | 1290 | */ |
Tejun Heo | 445d211 | 2010-04-05 10:33:13 +0900 | [diff] [blame] | 1291 | if (rc == -EACCES || !unlock_hpa) { |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 1292 | ata_dev_warn(dev, |
| 1293 | "HPA support seems broken, skipping HPA handling\n"); |
Tejun Heo | 05027ad | 2007-09-03 12:32:57 +0900 | [diff] [blame] | 1294 | dev->horkage |= ATA_HORKAGE_BROKEN_HPA; |
Alan Cox | 1e99973 | 2007-04-11 00:23:13 +0100 | [diff] [blame] | 1295 | |
Tejun Heo | 05027ad | 2007-09-03 12:32:57 +0900 | [diff] [blame] | 1296 | /* we can continue if device aborted the command */ |
| 1297 | if (rc == -EACCES) |
| 1298 | rc = 0; |
Alan Cox | 1e99973 | 2007-04-11 00:23:13 +0100 | [diff] [blame] | 1299 | } |
Tejun Heo | 37301a5 | 2007-06-25 20:45:54 +0900 | [diff] [blame] | 1300 | |
Tejun Heo | 05027ad | 2007-09-03 12:32:57 +0900 | [diff] [blame] | 1301 | return rc; |
| 1302 | } |
Tejun Heo | 5920dad | 2009-07-15 17:11:41 +0900 | [diff] [blame] | 1303 | dev->n_native_sectors = native_sectors; |
Tejun Heo | 05027ad | 2007-09-03 12:32:57 +0900 | [diff] [blame] | 1304 | |
| 1305 | /* nothing to do? */ |
Tejun Heo | 445d211 | 2010-04-05 10:33:13 +0900 | [diff] [blame] | 1306 | if (native_sectors <= sectors || !unlock_hpa) { |
Tejun Heo | 05027ad | 2007-09-03 12:32:57 +0900 | [diff] [blame] | 1307 | if (!print_info || native_sectors == sectors) |
| 1308 | return 0; |
| 1309 | |
| 1310 | if (native_sectors > sectors) |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 1311 | ata_dev_info(dev, |
Tejun Heo | 05027ad | 2007-09-03 12:32:57 +0900 | [diff] [blame] | 1312 | "HPA detected: current %llu, native %llu\n", |
| 1313 | (unsigned long long)sectors, |
| 1314 | (unsigned long long)native_sectors); |
| 1315 | else if (native_sectors < sectors) |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 1316 | ata_dev_warn(dev, |
| 1317 | "native sectors (%llu) is smaller than sectors (%llu)\n", |
Tejun Heo | 05027ad | 2007-09-03 12:32:57 +0900 | [diff] [blame] | 1318 | (unsigned long long)native_sectors, |
| 1319 | (unsigned long long)sectors); |
| 1320 | return 0; |
| 1321 | } |
| 1322 | |
| 1323 | /* let's unlock HPA */ |
| 1324 | rc = ata_set_max_sectors(dev, native_sectors); |
| 1325 | if (rc == -EACCES) { |
| 1326 | /* if device aborted the command, skip HPA resizing */ |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 1327 | ata_dev_warn(dev, |
| 1328 | "device aborted resize (%llu -> %llu), skipping HPA handling\n", |
| 1329 | (unsigned long long)sectors, |
| 1330 | (unsigned long long)native_sectors); |
Tejun Heo | 05027ad | 2007-09-03 12:32:57 +0900 | [diff] [blame] | 1331 | dev->horkage |= ATA_HORKAGE_BROKEN_HPA; |
| 1332 | return 0; |
| 1333 | } else if (rc) |
| 1334 | return rc; |
| 1335 | |
| 1336 | /* re-read IDENTIFY data */ |
| 1337 | rc = ata_dev_reread_id(dev, 0); |
| 1338 | if (rc) { |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 1339 | ata_dev_err(dev, |
| 1340 | "failed to re-read IDENTIFY data after HPA resizing\n"); |
Tejun Heo | 05027ad | 2007-09-03 12:32:57 +0900 | [diff] [blame] | 1341 | return rc; |
| 1342 | } |
| 1343 | |
| 1344 | if (print_info) { |
| 1345 | u64 new_sectors = ata_id_n_sectors(dev->id); |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 1346 | ata_dev_info(dev, |
Tejun Heo | 05027ad | 2007-09-03 12:32:57 +0900 | [diff] [blame] | 1347 | "HPA unlocked: %llu -> %llu, native %llu\n", |
| 1348 | (unsigned long long)sectors, |
| 1349 | (unsigned long long)new_sectors, |
| 1350 | (unsigned long long)native_sectors); |
| 1351 | } |
| 1352 | |
| 1353 | return 0; |
Alan Cox | 1e99973 | 2007-04-11 00:23:13 +0100 | [diff] [blame] | 1354 | } |
| 1355 | |
Edward Falk | 0baab86 | 2005-06-02 18:17:13 -0400 | [diff] [blame] | 1356 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1357 | * ata_dump_id - IDENTIFY DEVICE info debugging output |
Tejun Heo | 0bd3300 | 2006-02-12 22:47:05 +0900 | [diff] [blame] | 1358 | * @id: IDENTIFY DEVICE page to dump |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1359 | * |
Tejun Heo | 0bd3300 | 2006-02-12 22:47:05 +0900 | [diff] [blame] | 1360 | * Dump selected 16-bit words from the given IDENTIFY DEVICE |
| 1361 | * page. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1362 | * |
| 1363 | * LOCKING: |
| 1364 | * caller. |
| 1365 | */ |
| 1366 | |
Tejun Heo | 0bd3300 | 2006-02-12 22:47:05 +0900 | [diff] [blame] | 1367 | static inline void ata_dump_id(const u16 *id) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1368 | { |
| 1369 | DPRINTK("49==0x%04x " |
| 1370 | "53==0x%04x " |
| 1371 | "63==0x%04x " |
| 1372 | "64==0x%04x " |
| 1373 | "75==0x%04x \n", |
Tejun Heo | 0bd3300 | 2006-02-12 22:47:05 +0900 | [diff] [blame] | 1374 | id[49], |
| 1375 | id[53], |
| 1376 | id[63], |
| 1377 | id[64], |
| 1378 | id[75]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1379 | DPRINTK("80==0x%04x " |
| 1380 | "81==0x%04x " |
| 1381 | "82==0x%04x " |
| 1382 | "83==0x%04x " |
| 1383 | "84==0x%04x \n", |
Tejun Heo | 0bd3300 | 2006-02-12 22:47:05 +0900 | [diff] [blame] | 1384 | id[80], |
| 1385 | id[81], |
| 1386 | id[82], |
| 1387 | id[83], |
| 1388 | id[84]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1389 | DPRINTK("88==0x%04x " |
| 1390 | "93==0x%04x\n", |
Tejun Heo | 0bd3300 | 2006-02-12 22:47:05 +0900 | [diff] [blame] | 1391 | id[88], |
| 1392 | id[93]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1393 | } |
| 1394 | |
Tejun Heo | cb95d56 | 2006-03-06 04:31:56 +0900 | [diff] [blame] | 1395 | /** |
| 1396 | * ata_id_xfermask - Compute xfermask from the given IDENTIFY data |
| 1397 | * @id: IDENTIFY data to compute xfer mask from |
| 1398 | * |
| 1399 | * Compute the xfermask for this device. This is not as trivial |
| 1400 | * as it seems if we must consider early devices correctly. |
| 1401 | * |
| 1402 | * FIXME: pre IDE drive timing (do we care ?). |
| 1403 | * |
| 1404 | * LOCKING: |
| 1405 | * None. |
| 1406 | * |
| 1407 | * RETURNS: |
| 1408 | * Computed xfermask |
| 1409 | */ |
Tejun Heo | 7dc951a | 2007-11-27 19:43:42 +0900 | [diff] [blame] | 1410 | unsigned long ata_id_xfermask(const u16 *id) |
Tejun Heo | cb95d56 | 2006-03-06 04:31:56 +0900 | [diff] [blame] | 1411 | { |
Tejun Heo | 7dc951a | 2007-11-27 19:43:42 +0900 | [diff] [blame] | 1412 | unsigned long pio_mask, mwdma_mask, udma_mask; |
Tejun Heo | cb95d56 | 2006-03-06 04:31:56 +0900 | [diff] [blame] | 1413 | |
| 1414 | /* Usual case. Word 53 indicates word 64 is valid */ |
| 1415 | if (id[ATA_ID_FIELD_VALID] & (1 << 1)) { |
| 1416 | pio_mask = id[ATA_ID_PIO_MODES] & 0x03; |
| 1417 | pio_mask <<= 3; |
| 1418 | pio_mask |= 0x7; |
| 1419 | } else { |
| 1420 | /* If word 64 isn't valid then Word 51 high byte holds |
| 1421 | * the PIO timing number for the maximum. Turn it into |
| 1422 | * a mask. |
| 1423 | */ |
Lennert Buytenhek | 7a0f1c8 | 2007-01-29 13:28:47 +0100 | [diff] [blame] | 1424 | u8 mode = (id[ATA_ID_OLD_PIO_MODES] >> 8) & 0xFF; |
Alan Cox | 46767aeb | 2006-09-29 18:26:47 +0100 | [diff] [blame] | 1425 | if (mode < 5) /* Valid PIO range */ |
Jeff Garzik | 2dcb407 | 2007-10-19 06:42:56 -0400 | [diff] [blame] | 1426 | pio_mask = (2 << mode) - 1; |
Alan Cox | 46767aeb | 2006-09-29 18:26:47 +0100 | [diff] [blame] | 1427 | else |
| 1428 | pio_mask = 1; |
Tejun Heo | cb95d56 | 2006-03-06 04:31:56 +0900 | [diff] [blame] | 1429 | |
| 1430 | /* But wait.. there's more. Design your standards by |
| 1431 | * committee and you too can get a free iordy field to |
| 1432 | * process. However its the speeds not the modes that |
| 1433 | * are supported... Note drivers using the timing API |
| 1434 | * will get this right anyway |
| 1435 | */ |
| 1436 | } |
| 1437 | |
| 1438 | mwdma_mask = id[ATA_ID_MWDMA_MODES] & 0x07; |
Tejun Heo | fb21f0d | 2006-03-12 12:34:35 +0900 | [diff] [blame] | 1439 | |
Alan Cox | b352e57 | 2006-08-10 18:52:12 +0100 | [diff] [blame] | 1440 | if (ata_id_is_cfa(id)) { |
| 1441 | /* |
| 1442 | * Process compact flash extended modes |
| 1443 | */ |
Sergei Shtylyov | 62afe5d | 2009-04-13 20:50:00 +0400 | [diff] [blame] | 1444 | int pio = (id[ATA_ID_CFA_MODES] >> 0) & 0x7; |
| 1445 | int dma = (id[ATA_ID_CFA_MODES] >> 3) & 0x7; |
Alan Cox | b352e57 | 2006-08-10 18:52:12 +0100 | [diff] [blame] | 1446 | |
| 1447 | if (pio) |
| 1448 | pio_mask |= (1 << 5); |
| 1449 | if (pio > 1) |
| 1450 | pio_mask |= (1 << 6); |
| 1451 | if (dma) |
| 1452 | mwdma_mask |= (1 << 3); |
| 1453 | if (dma > 1) |
| 1454 | mwdma_mask |= (1 << 4); |
| 1455 | } |
| 1456 | |
Tejun Heo | fb21f0d | 2006-03-12 12:34:35 +0900 | [diff] [blame] | 1457 | udma_mask = 0; |
| 1458 | if (id[ATA_ID_FIELD_VALID] & (1 << 2)) |
| 1459 | udma_mask = id[ATA_ID_UDMA_MODES] & 0xff; |
Tejun Heo | cb95d56 | 2006-03-06 04:31:56 +0900 | [diff] [blame] | 1460 | |
| 1461 | return ata_pack_xfermask(pio_mask, mwdma_mask, udma_mask); |
| 1462 | } |
Bartlomiej Zolnierkiewicz | a52fbcf | 2020-03-26 16:58:04 +0100 | [diff] [blame] | 1463 | EXPORT_SYMBOL_GPL(ata_id_xfermask); |
Tejun Heo | cb95d56 | 2006-03-06 04:31:56 +0900 | [diff] [blame] | 1464 | |
Adrian Bunk | 7102d23 | 2007-01-04 00:09:36 +0100 | [diff] [blame] | 1465 | static void ata_qc_complete_internal(struct ata_queued_cmd *qc) |
Tejun Heo | a2a7a66 | 2005-12-13 14:48:31 +0900 | [diff] [blame] | 1466 | { |
Tejun Heo | 77853bf | 2006-01-23 13:09:36 +0900 | [diff] [blame] | 1467 | struct completion *waiting = qc->private_data; |
Tejun Heo | a2a7a66 | 2005-12-13 14:48:31 +0900 | [diff] [blame] | 1468 | |
Tejun Heo | a2a7a66 | 2005-12-13 14:48:31 +0900 | [diff] [blame] | 1469 | complete(waiting); |
Tejun Heo | a2a7a66 | 2005-12-13 14:48:31 +0900 | [diff] [blame] | 1470 | } |
| 1471 | |
| 1472 | /** |
Tejun Heo | 2432697 | 2006-11-14 22:47:09 +0900 | [diff] [blame] | 1473 | * ata_exec_internal_sg - execute libata internal command |
Tejun Heo | a2a7a66 | 2005-12-13 14:48:31 +0900 | [diff] [blame] | 1474 | * @dev: Device to which the command is sent |
| 1475 | * @tf: Taskfile registers for the command and the result |
Tejun Heo | d69cf37 | 2006-04-02 18:51:53 +0900 | [diff] [blame] | 1476 | * @cdb: CDB for packet command |
Masanari Iida | e227867 | 2014-02-18 22:54:36 +0900 | [diff] [blame] | 1477 | * @dma_dir: Data transfer direction of the command |
Randy Dunlap | 5c1ad8b | 2007-10-18 14:12:26 -0700 | [diff] [blame] | 1478 | * @sgl: sg list for the data buffer of the command |
Tejun Heo | 2432697 | 2006-11-14 22:47:09 +0900 | [diff] [blame] | 1479 | * @n_elem: Number of sg entries |
Tejun Heo | 2b78910 | 2007-10-09 15:05:44 +0900 | [diff] [blame] | 1480 | * @timeout: Timeout in msecs (0 for default) |
Tejun Heo | a2a7a66 | 2005-12-13 14:48:31 +0900 | [diff] [blame] | 1481 | * |
| 1482 | * Executes libata internal command with timeout. @tf contains |
| 1483 | * command on entry and result on return. Timeout and error |
| 1484 | * conditions are reported via return value. No recovery action |
| 1485 | * is taken after a command times out. It's caller's duty to |
| 1486 | * clean up after timeout. |
| 1487 | * |
| 1488 | * LOCKING: |
| 1489 | * None. Should be called with kernel context, might sleep. |
Tejun Heo | 551e888 | 2006-06-12 14:09:49 +0900 | [diff] [blame] | 1490 | * |
| 1491 | * RETURNS: |
| 1492 | * Zero on success, AC_ERR_* mask on failure |
Tejun Heo | a2a7a66 | 2005-12-13 14:48:31 +0900 | [diff] [blame] | 1493 | */ |
Tejun Heo | 2432697 | 2006-11-14 22:47:09 +0900 | [diff] [blame] | 1494 | unsigned ata_exec_internal_sg(struct ata_device *dev, |
| 1495 | struct ata_taskfile *tf, const u8 *cdb, |
Jens Axboe | 8726021 | 2007-10-16 11:14:12 +0200 | [diff] [blame] | 1496 | int dma_dir, struct scatterlist *sgl, |
Tejun Heo | 2b78910 | 2007-10-09 15:05:44 +0900 | [diff] [blame] | 1497 | unsigned int n_elem, unsigned long timeout) |
Tejun Heo | a2a7a66 | 2005-12-13 14:48:31 +0900 | [diff] [blame] | 1498 | { |
Tejun Heo | 9af5c9c | 2007-08-06 18:36:22 +0900 | [diff] [blame] | 1499 | struct ata_link *link = dev->link; |
| 1500 | struct ata_port *ap = link->ap; |
Tejun Heo | a2a7a66 | 2005-12-13 14:48:31 +0900 | [diff] [blame] | 1501 | u8 command = tf->command; |
Tejun Heo | 87fbc5a | 2008-05-20 02:17:54 +0900 | [diff] [blame] | 1502 | int auto_timeout = 0; |
Tejun Heo | a2a7a66 | 2005-12-13 14:48:31 +0900 | [diff] [blame] | 1503 | struct ata_queued_cmd *qc; |
Jens Axboe | 28361c4 | 2018-05-11 12:51:09 -0600 | [diff] [blame] | 1504 | unsigned int preempted_tag; |
Jens Axboe | e3ed8939 | 2018-05-11 12:51:05 -0600 | [diff] [blame] | 1505 | u32 preempted_sactive; |
| 1506 | u64 preempted_qc_active; |
Tejun Heo | da917d6 | 2007-09-23 13:14:12 +0900 | [diff] [blame] | 1507 | int preempted_nr_active_links; |
Ingo Molnar | 60be6b9 | 2006-07-03 00:25:26 -0700 | [diff] [blame] | 1508 | DECLARE_COMPLETION_ONSTACK(wait); |
Tejun Heo | a2a7a66 | 2005-12-13 14:48:31 +0900 | [diff] [blame] | 1509 | unsigned long flags; |
Tejun Heo | 77853bf | 2006-01-23 13:09:36 +0900 | [diff] [blame] | 1510 | unsigned int err_mask; |
Tejun Heo | d95a717 | 2006-05-15 20:58:14 +0900 | [diff] [blame] | 1511 | int rc; |
Tejun Heo | a2a7a66 | 2005-12-13 14:48:31 +0900 | [diff] [blame] | 1512 | |
Jeff Garzik | ba6a130 | 2006-06-22 23:46:10 -0400 | [diff] [blame] | 1513 | spin_lock_irqsave(ap->lock, flags); |
Tejun Heo | a2a7a66 | 2005-12-13 14:48:31 +0900 | [diff] [blame] | 1514 | |
Tejun Heo | e318049 | 2006-05-15 20:58:09 +0900 | [diff] [blame] | 1515 | /* no internal command while frozen */ |
Tejun Heo | b51e9e5 | 2006-06-29 01:29:30 +0900 | [diff] [blame] | 1516 | if (ap->pflags & ATA_PFLAG_FROZEN) { |
Jeff Garzik | ba6a130 | 2006-06-22 23:46:10 -0400 | [diff] [blame] | 1517 | spin_unlock_irqrestore(ap->lock, flags); |
Tejun Heo | e318049 | 2006-05-15 20:58:09 +0900 | [diff] [blame] | 1518 | return AC_ERR_SYSTEM; |
| 1519 | } |
| 1520 | |
Tejun Heo | 2ab7db1 | 2006-05-15 20:58:02 +0900 | [diff] [blame] | 1521 | /* initialize internal qc */ |
Jens Axboe | 28361c4 | 2018-05-11 12:51:09 -0600 | [diff] [blame] | 1522 | qc = __ata_qc_from_tag(ap, ATA_TAG_INTERNAL); |
Tejun Heo | a2a7a66 | 2005-12-13 14:48:31 +0900 | [diff] [blame] | 1523 | |
Jens Axboe | 28361c4 | 2018-05-11 12:51:09 -0600 | [diff] [blame] | 1524 | qc->tag = ATA_TAG_INTERNAL; |
| 1525 | qc->hw_tag = 0; |
Tejun Heo | 2ab7db1 | 2006-05-15 20:58:02 +0900 | [diff] [blame] | 1526 | qc->scsicmd = NULL; |
| 1527 | qc->ap = ap; |
| 1528 | qc->dev = dev; |
| 1529 | ata_qc_reinit(qc); |
| 1530 | |
Tejun Heo | 9af5c9c | 2007-08-06 18:36:22 +0900 | [diff] [blame] | 1531 | preempted_tag = link->active_tag; |
| 1532 | preempted_sactive = link->sactive; |
Tejun Heo | dedaf2b | 2006-05-15 21:03:43 +0900 | [diff] [blame] | 1533 | preempted_qc_active = ap->qc_active; |
Tejun Heo | da917d6 | 2007-09-23 13:14:12 +0900 | [diff] [blame] | 1534 | preempted_nr_active_links = ap->nr_active_links; |
Tejun Heo | 9af5c9c | 2007-08-06 18:36:22 +0900 | [diff] [blame] | 1535 | link->active_tag = ATA_TAG_POISON; |
| 1536 | link->sactive = 0; |
Tejun Heo | dedaf2b | 2006-05-15 21:03:43 +0900 | [diff] [blame] | 1537 | ap->qc_active = 0; |
Tejun Heo | da917d6 | 2007-09-23 13:14:12 +0900 | [diff] [blame] | 1538 | ap->nr_active_links = 0; |
Tejun Heo | 2ab7db1 | 2006-05-15 20:58:02 +0900 | [diff] [blame] | 1539 | |
| 1540 | /* prepare & issue qc */ |
Tejun Heo | a2a7a66 | 2005-12-13 14:48:31 +0900 | [diff] [blame] | 1541 | qc->tf = *tf; |
Tejun Heo | d69cf37 | 2006-04-02 18:51:53 +0900 | [diff] [blame] | 1542 | if (cdb) |
| 1543 | memcpy(qc->cdb, cdb, ATAPI_CDB_LEN); |
Vincent Pelletier | e771451 | 2013-05-18 18:44:04 +0200 | [diff] [blame] | 1544 | |
| 1545 | /* some SATA bridges need us to indicate data xfer direction */ |
| 1546 | if (tf->protocol == ATAPI_PROT_DMA && (dev->flags & ATA_DFLAG_DMADIR) && |
| 1547 | dma_dir == DMA_FROM_DEVICE) |
| 1548 | qc->tf.feature |= ATAPI_DMADIR; |
| 1549 | |
Tejun Heo | e61e067 | 2006-05-15 20:57:40 +0900 | [diff] [blame] | 1550 | qc->flags |= ATA_QCFLAG_RESULT_TF; |
Tejun Heo | a2a7a66 | 2005-12-13 14:48:31 +0900 | [diff] [blame] | 1551 | qc->dma_dir = dma_dir; |
| 1552 | if (dma_dir != DMA_NONE) { |
Tejun Heo | 2432697 | 2006-11-14 22:47:09 +0900 | [diff] [blame] | 1553 | unsigned int i, buflen = 0; |
Jens Axboe | 8726021 | 2007-10-16 11:14:12 +0200 | [diff] [blame] | 1554 | struct scatterlist *sg; |
Tejun Heo | 2432697 | 2006-11-14 22:47:09 +0900 | [diff] [blame] | 1555 | |
Jens Axboe | 8726021 | 2007-10-16 11:14:12 +0200 | [diff] [blame] | 1556 | for_each_sg(sgl, sg, n_elem, i) |
| 1557 | buflen += sg->length; |
Tejun Heo | 2432697 | 2006-11-14 22:47:09 +0900 | [diff] [blame] | 1558 | |
Jens Axboe | 8726021 | 2007-10-16 11:14:12 +0200 | [diff] [blame] | 1559 | ata_sg_init(qc, sgl, n_elem); |
Brian King | 49c8042 | 2007-01-30 11:32:26 -0600 | [diff] [blame] | 1560 | qc->nbytes = buflen; |
Tejun Heo | a2a7a66 | 2005-12-13 14:48:31 +0900 | [diff] [blame] | 1561 | } |
| 1562 | |
Tejun Heo | 77853bf | 2006-01-23 13:09:36 +0900 | [diff] [blame] | 1563 | qc->private_data = &wait; |
Tejun Heo | a2a7a66 | 2005-12-13 14:48:31 +0900 | [diff] [blame] | 1564 | qc->complete_fn = ata_qc_complete_internal; |
| 1565 | |
Tejun Heo | 8e0e694 | 2006-03-31 20:41:11 +0900 | [diff] [blame] | 1566 | ata_qc_issue(qc); |
Tejun Heo | a2a7a66 | 2005-12-13 14:48:31 +0900 | [diff] [blame] | 1567 | |
Jeff Garzik | ba6a130 | 2006-06-22 23:46:10 -0400 | [diff] [blame] | 1568 | spin_unlock_irqrestore(ap->lock, flags); |
Tejun Heo | a2a7a66 | 2005-12-13 14:48:31 +0900 | [diff] [blame] | 1569 | |
Tejun Heo | 87fbc5a | 2008-05-20 02:17:54 +0900 | [diff] [blame] | 1570 | if (!timeout) { |
| 1571 | if (ata_probe_timeout) |
| 1572 | timeout = ata_probe_timeout * 1000; |
| 1573 | else { |
| 1574 | timeout = ata_internal_cmd_timeout(dev, command); |
| 1575 | auto_timeout = 1; |
| 1576 | } |
| 1577 | } |
Tejun Heo | 2b78910 | 2007-10-09 15:05:44 +0900 | [diff] [blame] | 1578 | |
Tejun Heo | c0c362b | 2010-09-06 17:57:14 +0200 | [diff] [blame] | 1579 | if (ap->ops->error_handler) |
| 1580 | ata_eh_release(ap); |
| 1581 | |
Tejun Heo | 2b78910 | 2007-10-09 15:05:44 +0900 | [diff] [blame] | 1582 | rc = wait_for_completion_timeout(&wait, msecs_to_jiffies(timeout)); |
Albert Lee | 41ade50 | 2006-03-14 11:19:04 +0800 | [diff] [blame] | 1583 | |
Tejun Heo | c0c362b | 2010-09-06 17:57:14 +0200 | [diff] [blame] | 1584 | if (ap->ops->error_handler) |
| 1585 | ata_eh_acquire(ap); |
| 1586 | |
Tejun Heo | c429137 | 2010-05-10 21:41:38 +0200 | [diff] [blame] | 1587 | ata_sff_flush_pio_task(ap); |
Tejun Heo | d95a717 | 2006-05-15 20:58:14 +0900 | [diff] [blame] | 1588 | |
| 1589 | if (!rc) { |
Jeff Garzik | ba6a130 | 2006-06-22 23:46:10 -0400 | [diff] [blame] | 1590 | spin_lock_irqsave(ap->lock, flags); |
Tejun Heo | a2a7a66 | 2005-12-13 14:48:31 +0900 | [diff] [blame] | 1591 | |
| 1592 | /* We're racing with irq here. If we lose, the |
| 1593 | * following test prevents us from completing the qc |
Tejun Heo | d95a717 | 2006-05-15 20:58:14 +0900 | [diff] [blame] | 1594 | * twice. If we win, the port is frozen and will be |
| 1595 | * cleaned up by ->post_internal_cmd(). |
Tejun Heo | a2a7a66 | 2005-12-13 14:48:31 +0900 | [diff] [blame] | 1596 | */ |
Tejun Heo | 77853bf | 2006-01-23 13:09:36 +0900 | [diff] [blame] | 1597 | if (qc->flags & ATA_QCFLAG_ACTIVE) { |
Tejun Heo | d95a717 | 2006-05-15 20:58:14 +0900 | [diff] [blame] | 1598 | qc->err_mask |= AC_ERR_TIMEOUT; |
| 1599 | |
| 1600 | if (ap->ops->error_handler) |
| 1601 | ata_port_freeze(ap); |
| 1602 | else |
| 1603 | ata_qc_complete(qc); |
Tejun Heo | f15a1da | 2006-05-15 20:57:56 +0900 | [diff] [blame] | 1604 | |
Borislav Petkov | 0dd4b21 | 2006-06-23 02:29:08 -0400 | [diff] [blame] | 1605 | if (ata_msg_warn(ap)) |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 1606 | ata_dev_warn(dev, "qc timeout (cmd 0x%x)\n", |
| 1607 | command); |
Tejun Heo | a2a7a66 | 2005-12-13 14:48:31 +0900 | [diff] [blame] | 1608 | } |
| 1609 | |
Jeff Garzik | ba6a130 | 2006-06-22 23:46:10 -0400 | [diff] [blame] | 1610 | spin_unlock_irqrestore(ap->lock, flags); |
Tejun Heo | a2a7a66 | 2005-12-13 14:48:31 +0900 | [diff] [blame] | 1611 | } |
| 1612 | |
Tejun Heo | d95a717 | 2006-05-15 20:58:14 +0900 | [diff] [blame] | 1613 | /* do post_internal_cmd */ |
| 1614 | if (ap->ops->post_internal_cmd) |
| 1615 | ap->ops->post_internal_cmd(qc); |
| 1616 | |
Tejun Heo | a51d644 | 2007-03-20 15:24:11 +0900 | [diff] [blame] | 1617 | /* perform minimal error analysis */ |
| 1618 | if (qc->flags & ATA_QCFLAG_FAILED) { |
| 1619 | if (qc->result_tf.command & (ATA_ERR | ATA_DF)) |
| 1620 | qc->err_mask |= AC_ERR_DEV; |
| 1621 | |
| 1622 | if (!qc->err_mask) |
| 1623 | qc->err_mask |= AC_ERR_OTHER; |
| 1624 | |
| 1625 | if (qc->err_mask & ~AC_ERR_OTHER) |
| 1626 | qc->err_mask &= ~AC_ERR_OTHER; |
Damien Le Moal | 2dae995 | 2016-12-19 10:17:40 +0900 | [diff] [blame] | 1627 | } else if (qc->tf.command == ATA_CMD_REQ_SENSE_DATA) { |
| 1628 | qc->result_tf.command |= ATA_SENSE; |
Tejun Heo | d95a717 | 2006-05-15 20:58:14 +0900 | [diff] [blame] | 1629 | } |
| 1630 | |
Tejun Heo | 1586930 | 2006-05-15 20:57:33 +0900 | [diff] [blame] | 1631 | /* finish up */ |
Jeff Garzik | ba6a130 | 2006-06-22 23:46:10 -0400 | [diff] [blame] | 1632 | spin_lock_irqsave(ap->lock, flags); |
Tejun Heo | 1586930 | 2006-05-15 20:57:33 +0900 | [diff] [blame] | 1633 | |
Tejun Heo | e61e067 | 2006-05-15 20:57:40 +0900 | [diff] [blame] | 1634 | *tf = qc->result_tf; |
Tejun Heo | 77853bf | 2006-01-23 13:09:36 +0900 | [diff] [blame] | 1635 | err_mask = qc->err_mask; |
| 1636 | |
| 1637 | ata_qc_free(qc); |
Tejun Heo | 9af5c9c | 2007-08-06 18:36:22 +0900 | [diff] [blame] | 1638 | link->active_tag = preempted_tag; |
| 1639 | link->sactive = preempted_sactive; |
Tejun Heo | dedaf2b | 2006-05-15 21:03:43 +0900 | [diff] [blame] | 1640 | ap->qc_active = preempted_qc_active; |
Tejun Heo | da917d6 | 2007-09-23 13:14:12 +0900 | [diff] [blame] | 1641 | ap->nr_active_links = preempted_nr_active_links; |
Tejun Heo | 77853bf | 2006-01-23 13:09:36 +0900 | [diff] [blame] | 1642 | |
Jeff Garzik | ba6a130 | 2006-06-22 23:46:10 -0400 | [diff] [blame] | 1643 | spin_unlock_irqrestore(ap->lock, flags); |
Tejun Heo | 1586930 | 2006-05-15 20:57:33 +0900 | [diff] [blame] | 1644 | |
Tejun Heo | 87fbc5a | 2008-05-20 02:17:54 +0900 | [diff] [blame] | 1645 | if ((err_mask & AC_ERR_TIMEOUT) && auto_timeout) |
| 1646 | ata_internal_cmd_timed_out(dev, command); |
| 1647 | |
Tejun Heo | 77853bf | 2006-01-23 13:09:36 +0900 | [diff] [blame] | 1648 | return err_mask; |
Tejun Heo | a2a7a66 | 2005-12-13 14:48:31 +0900 | [diff] [blame] | 1649 | } |
| 1650 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1651 | /** |
Tejun Heo | 33480a0 | 2006-12-12 02:15:31 +0900 | [diff] [blame] | 1652 | * ata_exec_internal - execute libata internal command |
Tejun Heo | 2432697 | 2006-11-14 22:47:09 +0900 | [diff] [blame] | 1653 | * @dev: Device to which the command is sent |
| 1654 | * @tf: Taskfile registers for the command and the result |
| 1655 | * @cdb: CDB for packet command |
Masanari Iida | e227867 | 2014-02-18 22:54:36 +0900 | [diff] [blame] | 1656 | * @dma_dir: Data transfer direction of the command |
Tejun Heo | 2432697 | 2006-11-14 22:47:09 +0900 | [diff] [blame] | 1657 | * @buf: Data buffer of the command |
| 1658 | * @buflen: Length of data buffer |
Tejun Heo | 2b78910 | 2007-10-09 15:05:44 +0900 | [diff] [blame] | 1659 | * @timeout: Timeout in msecs (0 for default) |
Tejun Heo | 2432697 | 2006-11-14 22:47:09 +0900 | [diff] [blame] | 1660 | * |
| 1661 | * Wrapper around ata_exec_internal_sg() which takes simple |
| 1662 | * buffer instead of sg list. |
| 1663 | * |
| 1664 | * LOCKING: |
| 1665 | * None. Should be called with kernel context, might sleep. |
| 1666 | * |
| 1667 | * RETURNS: |
| 1668 | * Zero on success, AC_ERR_* mask on failure |
| 1669 | */ |
| 1670 | unsigned ata_exec_internal(struct ata_device *dev, |
| 1671 | struct ata_taskfile *tf, const u8 *cdb, |
Tejun Heo | 2b78910 | 2007-10-09 15:05:44 +0900 | [diff] [blame] | 1672 | int dma_dir, void *buf, unsigned int buflen, |
| 1673 | unsigned long timeout) |
Tejun Heo | 2432697 | 2006-11-14 22:47:09 +0900 | [diff] [blame] | 1674 | { |
Tejun Heo | 33480a0 | 2006-12-12 02:15:31 +0900 | [diff] [blame] | 1675 | struct scatterlist *psg = NULL, sg; |
| 1676 | unsigned int n_elem = 0; |
Tejun Heo | 2432697 | 2006-11-14 22:47:09 +0900 | [diff] [blame] | 1677 | |
Tejun Heo | 33480a0 | 2006-12-12 02:15:31 +0900 | [diff] [blame] | 1678 | if (dma_dir != DMA_NONE) { |
| 1679 | WARN_ON(!buf); |
| 1680 | sg_init_one(&sg, buf, buflen); |
| 1681 | psg = &sg; |
| 1682 | n_elem++; |
| 1683 | } |
Tejun Heo | 2432697 | 2006-11-14 22:47:09 +0900 | [diff] [blame] | 1684 | |
Tejun Heo | 2b78910 | 2007-10-09 15:05:44 +0900 | [diff] [blame] | 1685 | return ata_exec_internal_sg(dev, tf, cdb, dma_dir, psg, n_elem, |
| 1686 | timeout); |
Tejun Heo | 2432697 | 2006-11-14 22:47:09 +0900 | [diff] [blame] | 1687 | } |
| 1688 | |
| 1689 | /** |
Alan Cox | 1bc4ccf | 2006-01-09 17:18:14 +0000 | [diff] [blame] | 1690 | * ata_pio_need_iordy - check if iordy needed |
| 1691 | * @adev: ATA device |
| 1692 | * |
| 1693 | * Check if the current speed of the device requires IORDY. Used |
| 1694 | * by various controllers for chip configuration. |
| 1695 | */ |
Alan Cox | 1bc4ccf | 2006-01-09 17:18:14 +0000 | [diff] [blame] | 1696 | unsigned int ata_pio_need_iordy(const struct ata_device *adev) |
| 1697 | { |
Tejun Heo | 0d9e665 | 2009-06-11 11:04:45 +0900 | [diff] [blame] | 1698 | /* Don't set IORDY if we're preparing for reset. IORDY may |
| 1699 | * lead to controller lock up on certain controllers if the |
| 1700 | * port is not occupied. See bko#11703 for details. |
| 1701 | */ |
| 1702 | if (adev->link->ap->pflags & ATA_PFLAG_RESETTING) |
| 1703 | return 0; |
| 1704 | /* Controller doesn't support IORDY. Probably a pointless |
| 1705 | * check as the caller should know this. |
| 1706 | */ |
Tejun Heo | 9af5c9c | 2007-08-06 18:36:22 +0900 | [diff] [blame] | 1707 | if (adev->link->ap->flags & ATA_FLAG_NO_IORDY) |
Alan Cox | 1bc4ccf | 2006-01-09 17:18:14 +0000 | [diff] [blame] | 1708 | return 0; |
David Daney | 5c18c4d | 2008-12-10 15:39:12 -0800 | [diff] [blame] | 1709 | /* CF spec. r4.1 Table 22 says no iordy on PIO5 and PIO6. */ |
| 1710 | if (ata_id_is_cfa(adev->id) |
| 1711 | && (adev->pio_mode == XFER_PIO_5 || adev->pio_mode == XFER_PIO_6)) |
| 1712 | return 0; |
Alan Cox | 432729f | 2007-03-08 23:22:59 +0000 | [diff] [blame] | 1713 | /* PIO3 and higher it is mandatory */ |
| 1714 | if (adev->pio_mode > XFER_PIO_2) |
Alan Cox | 1bc4ccf | 2006-01-09 17:18:14 +0000 | [diff] [blame] | 1715 | return 1; |
Alan Cox | 432729f | 2007-03-08 23:22:59 +0000 | [diff] [blame] | 1716 | /* We turn it on when possible */ |
| 1717 | if (ata_id_has_iordy(adev->id)) |
| 1718 | return 1; |
| 1719 | return 0; |
| 1720 | } |
Bartlomiej Zolnierkiewicz | a52fbcf | 2020-03-26 16:58:04 +0100 | [diff] [blame] | 1721 | EXPORT_SYMBOL_GPL(ata_pio_need_iordy); |
Jeff Garzik | 2e9edbf | 2006-03-24 09:56:57 -0500 | [diff] [blame] | 1722 | |
Alan Cox | 432729f | 2007-03-08 23:22:59 +0000 | [diff] [blame] | 1723 | /** |
| 1724 | * ata_pio_mask_no_iordy - Return the non IORDY mask |
| 1725 | * @adev: ATA device |
| 1726 | * |
| 1727 | * Compute the highest mode possible if we are not using iordy. Return |
| 1728 | * -1 if no iordy mode is available. |
| 1729 | */ |
Alan Cox | 432729f | 2007-03-08 23:22:59 +0000 | [diff] [blame] | 1730 | static u32 ata_pio_mask_no_iordy(const struct ata_device *adev) |
| 1731 | { |
Alan Cox | 1bc4ccf | 2006-01-09 17:18:14 +0000 | [diff] [blame] | 1732 | /* 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] | 1733 | if (adev->id[ATA_ID_FIELD_VALID] & 2) { /* EIDE */ |
Alan Cox | 432729f | 2007-03-08 23:22:59 +0000 | [diff] [blame] | 1734 | u16 pio = adev->id[ATA_ID_EIDE_PIO]; |
Alan Cox | 1bc4ccf | 2006-01-09 17:18:14 +0000 | [diff] [blame] | 1735 | /* Is the speed faster than the drive allows non IORDY ? */ |
| 1736 | if (pio) { |
| 1737 | /* This is cycle times not frequency - watch the logic! */ |
| 1738 | if (pio > 240) /* PIO2 is 240nS per cycle */ |
Alan Cox | 432729f | 2007-03-08 23:22:59 +0000 | [diff] [blame] | 1739 | return 3 << ATA_SHIFT_PIO; |
| 1740 | return 7 << ATA_SHIFT_PIO; |
Alan Cox | 1bc4ccf | 2006-01-09 17:18:14 +0000 | [diff] [blame] | 1741 | } |
| 1742 | } |
Alan Cox | 432729f | 2007-03-08 23:22:59 +0000 | [diff] [blame] | 1743 | return 3 << ATA_SHIFT_PIO; |
Alan Cox | 1bc4ccf | 2006-01-09 17:18:14 +0000 | [diff] [blame] | 1744 | } |
| 1745 | |
| 1746 | /** |
Alan Cox | 963e497 | 2008-07-24 17:16:06 +0100 | [diff] [blame] | 1747 | * ata_do_dev_read_id - default ID read method |
| 1748 | * @dev: device |
| 1749 | * @tf: proposed taskfile |
| 1750 | * @id: data buffer |
| 1751 | * |
| 1752 | * Issue the identify taskfile and hand back the buffer containing |
| 1753 | * identify data. For some RAID controllers and for pre ATA devices |
| 1754 | * this function is wrapped or replaced by the driver |
| 1755 | */ |
| 1756 | unsigned int ata_do_dev_read_id(struct ata_device *dev, |
| 1757 | struct ata_taskfile *tf, u16 *id) |
| 1758 | { |
| 1759 | return ata_exec_internal(dev, tf, NULL, DMA_FROM_DEVICE, |
| 1760 | id, sizeof(id[0]) * ATA_ID_WORDS, 0); |
| 1761 | } |
Bartlomiej Zolnierkiewicz | a52fbcf | 2020-03-26 16:58:04 +0100 | [diff] [blame] | 1762 | EXPORT_SYMBOL_GPL(ata_do_dev_read_id); |
Alan Cox | 963e497 | 2008-07-24 17:16:06 +0100 | [diff] [blame] | 1763 | |
| 1764 | /** |
Tejun Heo | 49016ac | 2006-02-21 02:12:11 +0900 | [diff] [blame] | 1765 | * ata_dev_read_id - Read ID data from the specified device |
Tejun Heo | 49016ac | 2006-02-21 02:12:11 +0900 | [diff] [blame] | 1766 | * @dev: target device |
| 1767 | * @p_class: pointer to class of the target device (may be changed) |
Tejun Heo | bff0464 | 2006-11-10 18:08:10 +0900 | [diff] [blame] | 1768 | * @flags: ATA_READID_* flags |
Tejun Heo | fe635c7 | 2006-05-15 20:57:35 +0900 | [diff] [blame] | 1769 | * @id: buffer to read IDENTIFY data into |
Tejun Heo | 49016ac | 2006-02-21 02:12:11 +0900 | [diff] [blame] | 1770 | * |
| 1771 | * Read ID data from the specified device. ATA_CMD_ID_ATA is |
| 1772 | * performed on ATA devices and ATA_CMD_ID_ATAPI on ATAPI |
Tejun Heo | aec5c3c | 2006-03-25 01:33:34 +0900 | [diff] [blame] | 1773 | * devices. This function also issues ATA_CMD_INIT_DEV_PARAMS |
| 1774 | * for pre-ATA4 drives. |
Tejun Heo | 49016ac | 2006-02-21 02:12:11 +0900 | [diff] [blame] | 1775 | * |
Alan Cox | 50a9901 | 2007-08-08 14:27:00 +0100 | [diff] [blame] | 1776 | * FIXME: ATA_CMD_ID_ATA is optional for early drives and right |
Jeff Garzik | 2dcb407 | 2007-10-19 06:42:56 -0400 | [diff] [blame] | 1777 | * now we abort if we hit that case. |
Alan Cox | 50a9901 | 2007-08-08 14:27:00 +0100 | [diff] [blame] | 1778 | * |
Tejun Heo | 49016ac | 2006-02-21 02:12:11 +0900 | [diff] [blame] | 1779 | * LOCKING: |
| 1780 | * Kernel thread context (may sleep) |
| 1781 | * |
| 1782 | * RETURNS: |
| 1783 | * 0 on success, -errno otherwise. |
| 1784 | */ |
Tejun Heo | a9beec9 | 2006-05-31 18:27:44 +0900 | [diff] [blame] | 1785 | 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] | 1786 | unsigned int flags, u16 *id) |
Tejun Heo | 49016ac | 2006-02-21 02:12:11 +0900 | [diff] [blame] | 1787 | { |
Tejun Heo | 9af5c9c | 2007-08-06 18:36:22 +0900 | [diff] [blame] | 1788 | struct ata_port *ap = dev->link->ap; |
Tejun Heo | 49016ac | 2006-02-21 02:12:11 +0900 | [diff] [blame] | 1789 | unsigned int class = *p_class; |
Tejun Heo | 49016ac | 2006-02-21 02:12:11 +0900 | [diff] [blame] | 1790 | struct ata_taskfile tf; |
| 1791 | unsigned int err_mask = 0; |
| 1792 | const char *reason; |
Tejun Heo | 79b42ba | 2009-04-15 06:21:10 +0900 | [diff] [blame] | 1793 | bool is_semb = class == ATA_DEV_SEMB; |
Tejun Heo | 54936f8 | 2007-05-11 14:35:29 +0200 | [diff] [blame] | 1794 | int may_fallback = 1, tried_spinup = 0; |
Tejun Heo | 49016ac | 2006-02-21 02:12:11 +0900 | [diff] [blame] | 1795 | int rc; |
| 1796 | |
Borislav Petkov | 0dd4b21 | 2006-06-23 02:29:08 -0400 | [diff] [blame] | 1797 | if (ata_msg_ctl(ap)) |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 1798 | ata_dev_dbg(dev, "%s: ENTER\n", __func__); |
Tejun Heo | 49016ac | 2006-02-21 02:12:11 +0900 | [diff] [blame] | 1799 | |
Alan Cox | 963e497 | 2008-07-24 17:16:06 +0100 | [diff] [blame] | 1800 | retry: |
Tejun Heo | 3373efd | 2006-05-15 20:57:53 +0900 | [diff] [blame] | 1801 | ata_tf_init(dev, &tf); |
Tejun Heo | 49016ac | 2006-02-21 02:12:11 +0900 | [diff] [blame] | 1802 | |
| 1803 | switch (class) { |
Tejun Heo | 79b42ba | 2009-04-15 06:21:10 +0900 | [diff] [blame] | 1804 | case ATA_DEV_SEMB: |
| 1805 | class = ATA_DEV_ATA; /* some hard drives report SEMB sig */ |
Gustavo A. R. Silva | df561f66 | 2020-08-23 17:36:59 -0500 | [diff] [blame] | 1806 | fallthrough; |
Tejun Heo | 49016ac | 2006-02-21 02:12:11 +0900 | [diff] [blame] | 1807 | case ATA_DEV_ATA: |
Hannes Reinecke | 9162c65 | 2014-11-05 13:08:21 +0100 | [diff] [blame] | 1808 | case ATA_DEV_ZAC: |
Tejun Heo | 49016ac | 2006-02-21 02:12:11 +0900 | [diff] [blame] | 1809 | tf.command = ATA_CMD_ID_ATA; |
| 1810 | break; |
| 1811 | case ATA_DEV_ATAPI: |
| 1812 | tf.command = ATA_CMD_ID_ATAPI; |
| 1813 | break; |
| 1814 | default: |
| 1815 | rc = -ENODEV; |
| 1816 | reason = "unsupported class"; |
| 1817 | goto err_out; |
| 1818 | } |
| 1819 | |
| 1820 | tf.protocol = ATA_PROT_PIO; |
Tejun Heo | 81afe89 | 2007-02-07 12:37:41 -0800 | [diff] [blame] | 1821 | |
| 1822 | /* Some devices choke if TF registers contain garbage. Make |
| 1823 | * sure those are properly initialized. |
| 1824 | */ |
| 1825 | tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE; |
| 1826 | |
| 1827 | /* Device presence detection is unreliable on some |
| 1828 | * controllers. Always poll IDENTIFY if available. |
| 1829 | */ |
| 1830 | tf.flags |= ATA_TFLAG_POLLING; |
Tejun Heo | 49016ac | 2006-02-21 02:12:11 +0900 | [diff] [blame] | 1831 | |
Alan Cox | 963e497 | 2008-07-24 17:16:06 +0100 | [diff] [blame] | 1832 | if (ap->ops->read_id) |
| 1833 | err_mask = ap->ops->read_id(dev, &tf, id); |
| 1834 | else |
| 1835 | err_mask = ata_do_dev_read_id(dev, &tf, id); |
| 1836 | |
Tejun Heo | 49016ac | 2006-02-21 02:12:11 +0900 | [diff] [blame] | 1837 | if (err_mask) { |
Tejun Heo | 800b399 | 2006-12-03 21:34:13 +0900 | [diff] [blame] | 1838 | if (err_mask & AC_ERR_NODEV_HINT) { |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 1839 | ata_dev_dbg(dev, "NODEV after polling detection\n"); |
Tejun Heo | 55a8e2c | 2006-11-10 18:08:10 +0900 | [diff] [blame] | 1840 | return -ENOENT; |
| 1841 | } |
| 1842 | |
Tejun Heo | 79b42ba | 2009-04-15 06:21:10 +0900 | [diff] [blame] | 1843 | if (is_semb) { |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 1844 | ata_dev_info(dev, |
| 1845 | "IDENTIFY failed on device w/ SEMB sig, disabled\n"); |
Tejun Heo | 79b42ba | 2009-04-15 06:21:10 +0900 | [diff] [blame] | 1846 | /* SEMB is not supported yet */ |
| 1847 | *p_class = ATA_DEV_SEMB_UNSUP; |
| 1848 | return 0; |
| 1849 | } |
| 1850 | |
Tejun Heo | 1ffc151 | 2008-03-23 15:16:53 +0900 | [diff] [blame] | 1851 | if ((err_mask == AC_ERR_DEV) && (tf.feature & ATA_ABORTED)) { |
| 1852 | /* Device or controller might have reported |
| 1853 | * the wrong device class. Give a shot at the |
| 1854 | * other IDENTIFY if the current one is |
| 1855 | * aborted by the device. |
| 1856 | */ |
| 1857 | if (may_fallback) { |
| 1858 | may_fallback = 0; |
Tejun Heo | 54936f8 | 2007-05-11 14:35:29 +0200 | [diff] [blame] | 1859 | |
Tejun Heo | 1ffc151 | 2008-03-23 15:16:53 +0900 | [diff] [blame] | 1860 | if (class == ATA_DEV_ATA) |
| 1861 | class = ATA_DEV_ATAPI; |
| 1862 | else |
| 1863 | class = ATA_DEV_ATA; |
| 1864 | goto retry; |
| 1865 | } |
| 1866 | |
| 1867 | /* Control reaches here iff the device aborted |
| 1868 | * both flavors of IDENTIFYs which happens |
| 1869 | * sometimes with phantom devices. |
| 1870 | */ |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 1871 | ata_dev_dbg(dev, |
| 1872 | "both IDENTIFYs aborted, assuming NODEV\n"); |
Tejun Heo | 1ffc151 | 2008-03-23 15:16:53 +0900 | [diff] [blame] | 1873 | return -ENOENT; |
Tejun Heo | 54936f8 | 2007-05-11 14:35:29 +0200 | [diff] [blame] | 1874 | } |
| 1875 | |
Tejun Heo | 49016ac | 2006-02-21 02:12:11 +0900 | [diff] [blame] | 1876 | rc = -EIO; |
| 1877 | reason = "I/O error"; |
Tejun Heo | 49016ac | 2006-02-21 02:12:11 +0900 | [diff] [blame] | 1878 | goto err_out; |
| 1879 | } |
| 1880 | |
Tejun Heo | 43c9c59 | 2010-05-23 12:59:11 +0200 | [diff] [blame] | 1881 | if (dev->horkage & ATA_HORKAGE_DUMP_ID) { |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 1882 | ata_dev_dbg(dev, "dumping IDENTIFY data, " |
| 1883 | "class=%d may_fallback=%d tried_spinup=%d\n", |
| 1884 | class, may_fallback, tried_spinup); |
Tejun Heo | 43c9c59 | 2010-05-23 12:59:11 +0200 | [diff] [blame] | 1885 | print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, |
| 1886 | 16, 2, id, ATA_ID_WORDS * sizeof(*id), true); |
| 1887 | } |
| 1888 | |
Tejun Heo | 54936f8 | 2007-05-11 14:35:29 +0200 | [diff] [blame] | 1889 | /* Falling back doesn't make sense if ID data was read |
| 1890 | * successfully at least once. |
| 1891 | */ |
| 1892 | may_fallback = 0; |
| 1893 | |
Tejun Heo | 49016ac | 2006-02-21 02:12:11 +0900 | [diff] [blame] | 1894 | swap_buf_le16(id, ATA_ID_WORDS); |
| 1895 | |
Tejun Heo | 49016ac | 2006-02-21 02:12:11 +0900 | [diff] [blame] | 1896 | /* sanity check */ |
Tejun Heo | a4f5749 | 2006-09-12 20:35:49 -0700 | [diff] [blame] | 1897 | rc = -EINVAL; |
Alan Cox | 6070068 | 2007-06-07 16:13:55 +0100 | [diff] [blame] | 1898 | reason = "device reports invalid type"; |
Tejun Heo | a4f5749 | 2006-09-12 20:35:49 -0700 | [diff] [blame] | 1899 | |
Hannes Reinecke | 9162c65 | 2014-11-05 13:08:21 +0100 | [diff] [blame] | 1900 | if (class == ATA_DEV_ATA || class == ATA_DEV_ZAC) { |
Tejun Heo | a4f5749 | 2006-09-12 20:35:49 -0700 | [diff] [blame] | 1901 | if (!ata_id_is_ata(id) && !ata_id_is_cfa(id)) |
| 1902 | goto err_out; |
Andy Whitcroft | db63a4c | 2012-05-04 22:15:10 +0100 | [diff] [blame] | 1903 | if (ap->host->flags & ATA_HOST_IGNORE_ATA && |
| 1904 | ata_id_is_ata(id)) { |
| 1905 | ata_dev_dbg(dev, |
| 1906 | "host indicates ignore ATA devices, ignored\n"); |
| 1907 | return -ENOENT; |
| 1908 | } |
Tejun Heo | a4f5749 | 2006-09-12 20:35:49 -0700 | [diff] [blame] | 1909 | } else { |
| 1910 | if (ata_id_is_ata(id)) |
| 1911 | goto err_out; |
Tejun Heo | 49016ac | 2006-02-21 02:12:11 +0900 | [diff] [blame] | 1912 | } |
| 1913 | |
Mark Lord | 169439c | 2007-04-17 18:26:07 -0400 | [diff] [blame] | 1914 | if (!tried_spinup && (id[2] == 0x37c8 || id[2] == 0x738c)) { |
| 1915 | tried_spinup = 1; |
| 1916 | /* |
| 1917 | * Drive powered-up in standby mode, and requires a specific |
| 1918 | * SET_FEATURES spin-up subcommand before it will accept |
| 1919 | * anything other than the original IDENTIFY command. |
| 1920 | */ |
Jeff Garzik | 218f3d3 | 2007-10-25 00:33:27 -0400 | [diff] [blame] | 1921 | err_mask = ata_dev_set_feature(dev, SETFEATURES_SPINUP, 0); |
Ryan Power | fb0582f9 | 2007-08-10 13:59:35 -0700 | [diff] [blame] | 1922 | if (err_mask && id[2] != 0x738c) { |
Mark Lord | 169439c | 2007-04-17 18:26:07 -0400 | [diff] [blame] | 1923 | rc = -EIO; |
| 1924 | reason = "SPINUP failed"; |
| 1925 | goto err_out; |
| 1926 | } |
| 1927 | /* |
| 1928 | * If the drive initially returned incomplete IDENTIFY info, |
| 1929 | * we now must reissue the IDENTIFY command. |
| 1930 | */ |
| 1931 | if (id[2] == 0x37c8) |
| 1932 | goto retry; |
| 1933 | } |
| 1934 | |
Hannes Reinecke | 9162c65 | 2014-11-05 13:08:21 +0100 | [diff] [blame] | 1935 | if ((flags & ATA_READID_POSTRESET) && |
| 1936 | (class == ATA_DEV_ATA || class == ATA_DEV_ZAC)) { |
Tejun Heo | 49016ac | 2006-02-21 02:12:11 +0900 | [diff] [blame] | 1937 | /* |
| 1938 | * The exact sequence expected by certain pre-ATA4 drives is: |
| 1939 | * SRST RESET |
Alan Cox | 50a9901 | 2007-08-08 14:27:00 +0100 | [diff] [blame] | 1940 | * IDENTIFY (optional in early ATA) |
| 1941 | * INITIALIZE DEVICE PARAMETERS (later IDE and ATA) |
Tejun Heo | 49016ac | 2006-02-21 02:12:11 +0900 | [diff] [blame] | 1942 | * anything else.. |
| 1943 | * Some drives were very specific about that exact sequence. |
Alan Cox | 50a9901 | 2007-08-08 14:27:00 +0100 | [diff] [blame] | 1944 | * |
| 1945 | * Note that ATA4 says lba is mandatory so the second check |
Adam Buchbinder | c9404c9 | 2009-12-18 15:40:42 -0500 | [diff] [blame] | 1946 | * should never trigger. |
Tejun Heo | 49016ac | 2006-02-21 02:12:11 +0900 | [diff] [blame] | 1947 | */ |
| 1948 | if (ata_id_major_version(id) < 4 || !ata_id_has_lba(id)) { |
Tejun Heo | 3373efd | 2006-05-15 20:57:53 +0900 | [diff] [blame] | 1949 | err_mask = ata_dev_init_params(dev, id[3], id[6]); |
Tejun Heo | 49016ac | 2006-02-21 02:12:11 +0900 | [diff] [blame] | 1950 | if (err_mask) { |
| 1951 | rc = -EIO; |
| 1952 | reason = "INIT_DEV_PARAMS failed"; |
| 1953 | goto err_out; |
| 1954 | } |
| 1955 | |
| 1956 | /* current CHS translation info (id[53-58]) might be |
| 1957 | * changed. reread the identify device info. |
| 1958 | */ |
Tejun Heo | bff0464 | 2006-11-10 18:08:10 +0900 | [diff] [blame] | 1959 | flags &= ~ATA_READID_POSTRESET; |
Tejun Heo | 49016ac | 2006-02-21 02:12:11 +0900 | [diff] [blame] | 1960 | goto retry; |
| 1961 | } |
| 1962 | } |
| 1963 | |
| 1964 | *p_class = class; |
Tejun Heo | fe635c7 | 2006-05-15 20:57:35 +0900 | [diff] [blame] | 1965 | |
Tejun Heo | 49016ac | 2006-02-21 02:12:11 +0900 | [diff] [blame] | 1966 | return 0; |
| 1967 | |
| 1968 | err_out: |
Tejun Heo | 8857455 | 2006-06-25 20:00:35 +0900 | [diff] [blame] | 1969 | if (ata_msg_warn(ap)) |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 1970 | ata_dev_warn(dev, "failed to IDENTIFY (%s, err_mask=0x%x)\n", |
| 1971 | reason, err_mask); |
Tejun Heo | 49016ac | 2006-02-21 02:12:11 +0900 | [diff] [blame] | 1972 | return rc; |
| 1973 | } |
| 1974 | |
Christoph Hellwig | f01f62c | 2017-06-04 14:42:20 +0200 | [diff] [blame] | 1975 | /** |
| 1976 | * ata_read_log_page - read a specific log page |
| 1977 | * @dev: target device |
| 1978 | * @log: log to read |
| 1979 | * @page: page to read |
| 1980 | * @buf: buffer to store read page |
| 1981 | * @sectors: number of sectors to read |
| 1982 | * |
| 1983 | * Read log page using READ_LOG_EXT command. |
| 1984 | * |
| 1985 | * LOCKING: |
| 1986 | * Kernel thread context (may sleep). |
| 1987 | * |
| 1988 | * RETURNS: |
| 1989 | * 0 on success, AC_ERR_* mask otherwise. |
| 1990 | */ |
| 1991 | unsigned int ata_read_log_page(struct ata_device *dev, u8 log, |
| 1992 | u8 page, void *buf, unsigned int sectors) |
| 1993 | { |
| 1994 | unsigned long ap_flags = dev->link->ap->flags; |
| 1995 | struct ata_taskfile tf; |
| 1996 | unsigned int err_mask; |
| 1997 | bool dma = false; |
| 1998 | |
| 1999 | DPRINTK("read log page - log 0x%x, page 0x%x\n", log, page); |
| 2000 | |
| 2001 | /* |
| 2002 | * Return error without actually issuing the command on controllers |
| 2003 | * which e.g. lockup on a read log page. |
| 2004 | */ |
| 2005 | if (ap_flags & ATA_FLAG_NO_LOG_PAGE) |
| 2006 | return AC_ERR_DEV; |
| 2007 | |
| 2008 | retry: |
| 2009 | ata_tf_init(dev, &tf); |
| 2010 | if (dev->dma_mode && ata_id_has_read_log_dma_ext(dev->id) && |
Damien Le Moal | 7cfdfdc | 2017-07-10 14:45:20 +0900 | [diff] [blame] | 2011 | !(dev->horkage & ATA_HORKAGE_NO_DMA_LOG)) { |
Christoph Hellwig | f01f62c | 2017-06-04 14:42:20 +0200 | [diff] [blame] | 2012 | tf.command = ATA_CMD_READ_LOG_DMA_EXT; |
| 2013 | tf.protocol = ATA_PROT_DMA; |
| 2014 | dma = true; |
| 2015 | } else { |
| 2016 | tf.command = ATA_CMD_READ_LOG_EXT; |
| 2017 | tf.protocol = ATA_PROT_PIO; |
| 2018 | dma = false; |
| 2019 | } |
| 2020 | tf.lbal = log; |
| 2021 | tf.lbam = page; |
| 2022 | tf.nsect = sectors; |
| 2023 | tf.hob_nsect = sectors >> 8; |
| 2024 | tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_LBA48 | ATA_TFLAG_DEVICE; |
| 2025 | |
| 2026 | err_mask = ata_exec_internal(dev, &tf, NULL, DMA_FROM_DEVICE, |
| 2027 | buf, sectors * ATA_SECT_SIZE, 0); |
| 2028 | |
Damien Le Moal | fc5c8aa | 2021-08-16 10:44:52 +0900 | [diff] [blame^] | 2029 | if (err_mask) { |
| 2030 | if (dma) { |
| 2031 | dev->horkage |= ATA_HORKAGE_NO_DMA_LOG; |
| 2032 | goto retry; |
| 2033 | } |
| 2034 | ata_dev_err(dev, "Read log page 0x%02x failed, Emask 0x%x\n", |
| 2035 | (unsigned int)page, err_mask); |
Christoph Hellwig | f01f62c | 2017-06-04 14:42:20 +0200 | [diff] [blame] | 2036 | } |
| 2037 | |
Christoph Hellwig | f01f62c | 2017-06-04 14:42:20 +0200 | [diff] [blame] | 2038 | return err_mask; |
| 2039 | } |
| 2040 | |
Christoph Hellwig | efe205a | 2017-06-04 14:42:21 +0200 | [diff] [blame] | 2041 | static bool ata_log_supported(struct ata_device *dev, u8 log) |
| 2042 | { |
| 2043 | struct ata_port *ap = dev->link->ap; |
| 2044 | |
| 2045 | if (ata_read_log_page(dev, ATA_LOG_DIRECTORY, 0, ap->sector_buf, 1)) |
| 2046 | return false; |
| 2047 | return get_unaligned_le16(&ap->sector_buf[log * 2]) ? true : false; |
| 2048 | } |
| 2049 | |
Christoph Hellwig | a0fd245 | 2017-06-04 14:42:23 +0200 | [diff] [blame] | 2050 | static bool ata_identify_page_supported(struct ata_device *dev, u8 page) |
| 2051 | { |
| 2052 | struct ata_port *ap = dev->link->ap; |
| 2053 | unsigned int err, i; |
| 2054 | |
| 2055 | if (!ata_log_supported(dev, ATA_LOG_IDENTIFY_DEVICE)) { |
| 2056 | ata_dev_warn(dev, "ATA Identify Device Log not supported\n"); |
| 2057 | return false; |
| 2058 | } |
| 2059 | |
| 2060 | /* |
| 2061 | * Read IDENTIFY DEVICE data log, page 0, to figure out if the page is |
| 2062 | * supported. |
| 2063 | */ |
| 2064 | err = ata_read_log_page(dev, ATA_LOG_IDENTIFY_DEVICE, 0, ap->sector_buf, |
| 2065 | 1); |
Damien Le Moal | fc5c8aa | 2021-08-16 10:44:52 +0900 | [diff] [blame^] | 2066 | if (err) |
Christoph Hellwig | a0fd245 | 2017-06-04 14:42:23 +0200 | [diff] [blame] | 2067 | return false; |
Christoph Hellwig | a0fd245 | 2017-06-04 14:42:23 +0200 | [diff] [blame] | 2068 | |
| 2069 | for (i = 0; i < ap->sector_buf[8]; i++) { |
| 2070 | if (ap->sector_buf[9 + i] == page) |
| 2071 | return true; |
| 2072 | } |
| 2073 | |
| 2074 | return false; |
| 2075 | } |
| 2076 | |
Tejun Heo | 9062712 | 2009-01-29 20:31:36 +0900 | [diff] [blame] | 2077 | static int ata_do_link_spd_horkage(struct ata_device *dev) |
| 2078 | { |
| 2079 | struct ata_link *plink = ata_dev_phys_link(dev); |
| 2080 | u32 target, target_limit; |
| 2081 | |
| 2082 | if (!sata_scr_valid(plink)) |
| 2083 | return 0; |
| 2084 | |
| 2085 | if (dev->horkage & ATA_HORKAGE_1_5_GBPS) |
| 2086 | target = 1; |
| 2087 | else |
| 2088 | return 0; |
| 2089 | |
| 2090 | target_limit = (1 << target) - 1; |
| 2091 | |
| 2092 | /* if already on stricter limit, no need to push further */ |
| 2093 | if (plink->sata_spd_limit <= target_limit) |
| 2094 | return 0; |
| 2095 | |
| 2096 | plink->sata_spd_limit = target_limit; |
| 2097 | |
| 2098 | /* Request another EH round by returning -EAGAIN if link is |
| 2099 | * going faster than the target speed. Forward progress is |
| 2100 | * guaranteed by setting sata_spd_limit to target_limit above. |
| 2101 | */ |
| 2102 | if (plink->sata_spd > target) { |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 2103 | ata_dev_info(dev, "applying link speed limit horkage to %s\n", |
| 2104 | sata_spd_string(target)); |
Tejun Heo | 9062712 | 2009-01-29 20:31:36 +0900 | [diff] [blame] | 2105 | return -EAGAIN; |
| 2106 | } |
| 2107 | return 0; |
| 2108 | } |
| 2109 | |
Tejun Heo | 3373efd | 2006-05-15 20:57:53 +0900 | [diff] [blame] | 2110 | static inline u8 ata_dev_knobble(struct ata_device *dev) |
Tejun Heo | 4b2f3ed | 2006-03-01 16:09:36 +0900 | [diff] [blame] | 2111 | { |
Tejun Heo | 9af5c9c | 2007-08-06 18:36:22 +0900 | [diff] [blame] | 2112 | struct ata_port *ap = dev->link->ap; |
Jens Axboe | 9ce8e30 | 2008-08-27 15:23:18 +0200 | [diff] [blame] | 2113 | |
| 2114 | if (ata_dev_blacklisted(dev) & ATA_HORKAGE_BRIDGE_OK) |
| 2115 | return 0; |
| 2116 | |
Tejun Heo | 9af5c9c | 2007-08-06 18:36:22 +0900 | [diff] [blame] | 2117 | return ((ap->cbl == ATA_CBL_SATA) && (!ata_id_is_sata(dev->id))); |
Tejun Heo | 4b2f3ed | 2006-03-01 16:09:36 +0900 | [diff] [blame] | 2118 | } |
| 2119 | |
Hannes Reinecke | 5a23355 | 2016-04-25 12:45:46 +0200 | [diff] [blame] | 2120 | static void ata_dev_config_ncq_send_recv(struct ata_device *dev) |
| 2121 | { |
| 2122 | struct ata_port *ap = dev->link->ap; |
| 2123 | unsigned int err_mask; |
| 2124 | |
Christoph Hellwig | efe205a | 2017-06-04 14:42:21 +0200 | [diff] [blame] | 2125 | if (!ata_log_supported(dev, ATA_LOG_NCQ_SEND_RECV)) { |
| 2126 | ata_dev_warn(dev, "NCQ Send/Recv Log not supported\n"); |
Hannes Reinecke | fe5af0c | 2016-04-25 12:45:48 +0200 | [diff] [blame] | 2127 | return; |
| 2128 | } |
Hannes Reinecke | 5a23355 | 2016-04-25 12:45:46 +0200 | [diff] [blame] | 2129 | err_mask = ata_read_log_page(dev, ATA_LOG_NCQ_SEND_RECV, |
| 2130 | 0, ap->sector_buf, 1); |
Damien Le Moal | fc5c8aa | 2021-08-16 10:44:52 +0900 | [diff] [blame^] | 2131 | if (!err_mask) { |
Hannes Reinecke | 5a23355 | 2016-04-25 12:45:46 +0200 | [diff] [blame] | 2132 | u8 *cmds = dev->ncq_send_recv_cmds; |
| 2133 | |
| 2134 | dev->flags |= ATA_DFLAG_NCQ_SEND_RECV; |
| 2135 | memcpy(cmds, ap->sector_buf, ATA_LOG_NCQ_SEND_RECV_SIZE); |
| 2136 | |
| 2137 | if (dev->horkage & ATA_HORKAGE_NO_NCQ_TRIM) { |
| 2138 | ata_dev_dbg(dev, "disabling queued TRIM support\n"); |
| 2139 | cmds[ATA_LOG_NCQ_SEND_RECV_DSM_OFFSET] &= |
| 2140 | ~ATA_LOG_NCQ_SEND_RECV_DSM_TRIM; |
| 2141 | } |
| 2142 | } |
| 2143 | } |
| 2144 | |
Hannes Reinecke | 284b3b7 | 2016-04-25 12:45:54 +0200 | [diff] [blame] | 2145 | static void ata_dev_config_ncq_non_data(struct ata_device *dev) |
| 2146 | { |
| 2147 | struct ata_port *ap = dev->link->ap; |
| 2148 | unsigned int err_mask; |
Hannes Reinecke | 284b3b7 | 2016-04-25 12:45:54 +0200 | [diff] [blame] | 2149 | |
Christoph Hellwig | efe205a | 2017-06-04 14:42:21 +0200 | [diff] [blame] | 2150 | if (!ata_log_supported(dev, ATA_LOG_NCQ_NON_DATA)) { |
Hannes Reinecke | 284b3b7 | 2016-04-25 12:45:54 +0200 | [diff] [blame] | 2151 | ata_dev_warn(dev, |
| 2152 | "NCQ Send/Recv Log not supported\n"); |
| 2153 | return; |
| 2154 | } |
| 2155 | err_mask = ata_read_log_page(dev, ATA_LOG_NCQ_NON_DATA, |
| 2156 | 0, ap->sector_buf, 1); |
Damien Le Moal | fc5c8aa | 2021-08-16 10:44:52 +0900 | [diff] [blame^] | 2157 | if (!err_mask) { |
Hannes Reinecke | 284b3b7 | 2016-04-25 12:45:54 +0200 | [diff] [blame] | 2158 | u8 *cmds = dev->ncq_non_data_cmds; |
| 2159 | |
| 2160 | memcpy(cmds, ap->sector_buf, ATA_LOG_NCQ_NON_DATA_SIZE); |
| 2161 | } |
| 2162 | } |
| 2163 | |
Adam Manzanares | 8e06178 | 2016-10-17 11:27:29 -0700 | [diff] [blame] | 2164 | static void ata_dev_config_ncq_prio(struct ata_device *dev) |
| 2165 | { |
| 2166 | struct ata_port *ap = dev->link->ap; |
| 2167 | unsigned int err_mask; |
| 2168 | |
| 2169 | err_mask = ata_read_log_page(dev, |
Christoph Hellwig | 1d51d5f | 2017-06-04 14:42:22 +0200 | [diff] [blame] | 2170 | ATA_LOG_IDENTIFY_DEVICE, |
Adam Manzanares | 8e06178 | 2016-10-17 11:27:29 -0700 | [diff] [blame] | 2171 | ATA_LOG_SATA_SETTINGS, |
| 2172 | ap->sector_buf, |
| 2173 | 1); |
Damien Le Moal | fc5c8aa | 2021-08-16 10:44:52 +0900 | [diff] [blame^] | 2174 | if (err_mask) |
Damien Le Moal | 2360fa1 | 2021-08-16 10:44:51 +0900 | [diff] [blame] | 2175 | goto not_supported; |
Adam Manzanares | 8e06178 | 2016-10-17 11:27:29 -0700 | [diff] [blame] | 2176 | |
Damien Le Moal | 2360fa1 | 2021-08-16 10:44:51 +0900 | [diff] [blame] | 2177 | if (!(ap->sector_buf[ATA_LOG_NCQ_PRIO_OFFSET] & BIT(3))) |
| 2178 | goto not_supported; |
Adam Manzanares | 8e06178 | 2016-10-17 11:27:29 -0700 | [diff] [blame] | 2179 | |
Damien Le Moal | 2360fa1 | 2021-08-16 10:44:51 +0900 | [diff] [blame] | 2180 | dev->flags |= ATA_DFLAG_NCQ_PRIO; |
| 2181 | |
| 2182 | return; |
| 2183 | |
| 2184 | not_supported: |
| 2185 | dev->flags &= ~ATA_DFLAG_NCQ_PRIO_ENABLE; |
| 2186 | dev->flags &= ~ATA_DFLAG_NCQ_PRIO; |
Adam Manzanares | 8e06178 | 2016-10-17 11:27:29 -0700 | [diff] [blame] | 2187 | } |
| 2188 | |
Shaohua Li | 388539f | 2009-07-27 09:24:35 +0800 | [diff] [blame] | 2189 | static int ata_dev_config_ncq(struct ata_device *dev, |
Tejun Heo | a6e6ce8 | 2006-05-15 21:03:48 +0900 | [diff] [blame] | 2190 | char *desc, size_t desc_sz) |
| 2191 | { |
Tejun Heo | 9af5c9c | 2007-08-06 18:36:22 +0900 | [diff] [blame] | 2192 | struct ata_port *ap = dev->link->ap; |
Tejun Heo | a6e6ce8 | 2006-05-15 21:03:48 +0900 | [diff] [blame] | 2193 | int hdepth = 0, ddepth = ata_id_queue_depth(dev->id); |
Shaohua Li | 388539f | 2009-07-27 09:24:35 +0800 | [diff] [blame] | 2194 | unsigned int err_mask; |
| 2195 | char *aa_desc = ""; |
Tejun Heo | a6e6ce8 | 2006-05-15 21:03:48 +0900 | [diff] [blame] | 2196 | |
| 2197 | if (!ata_id_has_ncq(dev->id)) { |
| 2198 | desc[0] = '\0'; |
Shaohua Li | 388539f | 2009-07-27 09:24:35 +0800 | [diff] [blame] | 2199 | return 0; |
Tejun Heo | a6e6ce8 | 2006-05-15 21:03:48 +0900 | [diff] [blame] | 2200 | } |
Bartlomiej Zolnierkiewicz | cba97ea | 2020-03-26 16:58:09 +0100 | [diff] [blame] | 2201 | if (!IS_ENABLED(CONFIG_SATA_HOST)) |
| 2202 | return 0; |
Tejun Heo | 75683fe | 2007-07-05 13:31:27 +0900 | [diff] [blame] | 2203 | if (dev->horkage & ATA_HORKAGE_NONCQ) { |
Alan Cox | 6919a0a | 2006-10-27 19:08:46 -0700 | [diff] [blame] | 2204 | snprintf(desc, desc_sz, "NCQ (not used)"); |
Shaohua Li | 388539f | 2009-07-27 09:24:35 +0800 | [diff] [blame] | 2205 | return 0; |
Alan Cox | 6919a0a | 2006-10-27 19:08:46 -0700 | [diff] [blame] | 2206 | } |
Tejun Heo | a6e6ce8 | 2006-05-15 21:03:48 +0900 | [diff] [blame] | 2207 | if (ap->flags & ATA_FLAG_NCQ) { |
Jens Axboe | 69278f7 | 2018-05-11 12:51:10 -0600 | [diff] [blame] | 2208 | hdepth = min(ap->scsi_host->can_queue, ATA_MAX_QUEUE); |
Tejun Heo | a6e6ce8 | 2006-05-15 21:03:48 +0900 | [diff] [blame] | 2209 | dev->flags |= ATA_DFLAG_NCQ; |
| 2210 | } |
| 2211 | |
Shaohua Li | 388539f | 2009-07-27 09:24:35 +0800 | [diff] [blame] | 2212 | if (!(dev->horkage & ATA_HORKAGE_BROKEN_FPDMA_AA) && |
| 2213 | (ap->flags & ATA_FLAG_FPDMA_AA) && |
| 2214 | ata_id_has_fpdma_aa(dev->id)) { |
| 2215 | err_mask = ata_dev_set_feature(dev, SETFEATURES_SATA_ENABLE, |
| 2216 | SATA_FPDMA_AA); |
| 2217 | if (err_mask) { |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 2218 | ata_dev_err(dev, |
| 2219 | "failed to enable AA (error_mask=0x%x)\n", |
| 2220 | err_mask); |
Shaohua Li | 388539f | 2009-07-27 09:24:35 +0800 | [diff] [blame] | 2221 | if (err_mask != AC_ERR_DEV) { |
| 2222 | dev->horkage |= ATA_HORKAGE_BROKEN_FPDMA_AA; |
| 2223 | return -EIO; |
| 2224 | } |
| 2225 | } else |
| 2226 | aa_desc = ", AA"; |
| 2227 | } |
| 2228 | |
Tejun Heo | a6e6ce8 | 2006-05-15 21:03:48 +0900 | [diff] [blame] | 2229 | if (hdepth >= ddepth) |
Shaohua Li | 388539f | 2009-07-27 09:24:35 +0800 | [diff] [blame] | 2230 | snprintf(desc, desc_sz, "NCQ (depth %d)%s", ddepth, aa_desc); |
Tejun Heo | a6e6ce8 | 2006-05-15 21:03:48 +0900 | [diff] [blame] | 2231 | else |
Shaohua Li | 388539f | 2009-07-27 09:24:35 +0800 | [diff] [blame] | 2232 | snprintf(desc, desc_sz, "NCQ (depth %d/%d)%s", hdepth, |
| 2233 | ddepth, aa_desc); |
Marc Carino | ed36911 | 2013-08-24 23:22:50 -0700 | [diff] [blame] | 2234 | |
Hannes Reinecke | 284b3b7 | 2016-04-25 12:45:54 +0200 | [diff] [blame] | 2235 | if ((ap->flags & ATA_FLAG_FPDMA_AUX)) { |
| 2236 | if (ata_id_has_ncq_send_and_recv(dev->id)) |
| 2237 | ata_dev_config_ncq_send_recv(dev); |
| 2238 | if (ata_id_has_ncq_non_data(dev->id)) |
| 2239 | ata_dev_config_ncq_non_data(dev); |
Adam Manzanares | 8e06178 | 2016-10-17 11:27:29 -0700 | [diff] [blame] | 2240 | if (ata_id_has_ncq_prio(dev->id)) |
| 2241 | ata_dev_config_ncq_prio(dev); |
Marc Carino | ed36911 | 2013-08-24 23:22:50 -0700 | [diff] [blame] | 2242 | } |
| 2243 | |
Shaohua Li | 388539f | 2009-07-27 09:24:35 +0800 | [diff] [blame] | 2244 | return 0; |
Tejun Heo | a6e6ce8 | 2006-05-15 21:03:48 +0900 | [diff] [blame] | 2245 | } |
| 2246 | |
Hannes Reinecke | e87fd28 | 2016-04-04 11:43:55 +0200 | [diff] [blame] | 2247 | static void ata_dev_config_sense_reporting(struct ata_device *dev) |
| 2248 | { |
| 2249 | unsigned int err_mask; |
| 2250 | |
| 2251 | if (!ata_id_has_sense_reporting(dev->id)) |
| 2252 | return; |
| 2253 | |
| 2254 | if (ata_id_sense_reporting_enabled(dev->id)) |
| 2255 | return; |
| 2256 | |
| 2257 | err_mask = ata_dev_set_feature(dev, SETFEATURE_SENSE_DATA, 0x1); |
| 2258 | if (err_mask) { |
| 2259 | ata_dev_dbg(dev, |
| 2260 | "failed to enable Sense Data Reporting, Emask 0x%x\n", |
| 2261 | err_mask); |
| 2262 | } |
| 2263 | } |
| 2264 | |
Hannes Reinecke | 6d1003a | 2016-04-25 12:45:56 +0200 | [diff] [blame] | 2265 | static void ata_dev_config_zac(struct ata_device *dev) |
| 2266 | { |
| 2267 | struct ata_port *ap = dev->link->ap; |
| 2268 | unsigned int err_mask; |
| 2269 | u8 *identify_buf = ap->sector_buf; |
Hannes Reinecke | 6d1003a | 2016-04-25 12:45:56 +0200 | [diff] [blame] | 2270 | |
| 2271 | dev->zac_zones_optimal_open = U32_MAX; |
| 2272 | dev->zac_zones_optimal_nonseq = U32_MAX; |
| 2273 | dev->zac_zones_max_open = U32_MAX; |
| 2274 | |
| 2275 | /* |
| 2276 | * Always set the 'ZAC' flag for Host-managed devices. |
| 2277 | */ |
| 2278 | if (dev->class == ATA_DEV_ZAC) |
| 2279 | dev->flags |= ATA_DFLAG_ZAC; |
| 2280 | else if (ata_id_zoned_cap(dev->id) == 0x01) |
| 2281 | /* |
| 2282 | * Check for host-aware devices. |
| 2283 | */ |
| 2284 | dev->flags |= ATA_DFLAG_ZAC; |
| 2285 | |
| 2286 | if (!(dev->flags & ATA_DFLAG_ZAC)) |
| 2287 | return; |
| 2288 | |
Christoph Hellwig | a0fd245 | 2017-06-04 14:42:23 +0200 | [diff] [blame] | 2289 | if (!ata_identify_page_supported(dev, ATA_LOG_ZONED_INFORMATION)) { |
Hannes Reinecke | 6d1003a | 2016-04-25 12:45:56 +0200 | [diff] [blame] | 2290 | ata_dev_warn(dev, |
| 2291 | "ATA Zoned Information Log not supported\n"); |
| 2292 | return; |
| 2293 | } |
| 2294 | |
| 2295 | /* |
| 2296 | * Read IDENTIFY DEVICE data log, page 9 (Zoned-device information) |
| 2297 | */ |
Christoph Hellwig | 1d51d5f | 2017-06-04 14:42:22 +0200 | [diff] [blame] | 2298 | err_mask = ata_read_log_page(dev, ATA_LOG_IDENTIFY_DEVICE, |
Hannes Reinecke | 6d1003a | 2016-04-25 12:45:56 +0200 | [diff] [blame] | 2299 | ATA_LOG_ZONED_INFORMATION, |
| 2300 | identify_buf, 1); |
| 2301 | if (!err_mask) { |
| 2302 | u64 zoned_cap, opt_open, opt_nonseq, max_open; |
| 2303 | |
| 2304 | zoned_cap = get_unaligned_le64(&identify_buf[8]); |
| 2305 | if ((zoned_cap >> 63)) |
| 2306 | dev->zac_zoned_cap = (zoned_cap & 1); |
| 2307 | opt_open = get_unaligned_le64(&identify_buf[24]); |
| 2308 | if ((opt_open >> 63)) |
| 2309 | dev->zac_zones_optimal_open = (u32)opt_open; |
| 2310 | opt_nonseq = get_unaligned_le64(&identify_buf[32]); |
| 2311 | if ((opt_nonseq >> 63)) |
| 2312 | dev->zac_zones_optimal_nonseq = (u32)opt_nonseq; |
| 2313 | max_open = get_unaligned_le64(&identify_buf[40]); |
| 2314 | if ((max_open >> 63)) |
| 2315 | dev->zac_zones_max_open = (u32)max_open; |
| 2316 | } |
| 2317 | } |
| 2318 | |
Christoph Hellwig | 818831c | 2017-06-04 14:42:24 +0200 | [diff] [blame] | 2319 | static void ata_dev_config_trusted(struct ata_device *dev) |
| 2320 | { |
| 2321 | struct ata_port *ap = dev->link->ap; |
| 2322 | u64 trusted_cap; |
| 2323 | unsigned int err; |
| 2324 | |
Christoph Hellwig | e8f11db | 2017-08-29 14:42:06 +0200 | [diff] [blame] | 2325 | if (!ata_id_has_trusted(dev->id)) |
| 2326 | return; |
| 2327 | |
Christoph Hellwig | 818831c | 2017-06-04 14:42:24 +0200 | [diff] [blame] | 2328 | if (!ata_identify_page_supported(dev, ATA_LOG_SECURITY)) { |
| 2329 | ata_dev_warn(dev, |
| 2330 | "Security Log not supported\n"); |
| 2331 | return; |
| 2332 | } |
| 2333 | |
| 2334 | err = ata_read_log_page(dev, ATA_LOG_IDENTIFY_DEVICE, ATA_LOG_SECURITY, |
| 2335 | ap->sector_buf, 1); |
Damien Le Moal | fc5c8aa | 2021-08-16 10:44:52 +0900 | [diff] [blame^] | 2336 | if (err) |
Christoph Hellwig | 818831c | 2017-06-04 14:42:24 +0200 | [diff] [blame] | 2337 | return; |
Christoph Hellwig | 818831c | 2017-06-04 14:42:24 +0200 | [diff] [blame] | 2338 | |
| 2339 | trusted_cap = get_unaligned_le64(&ap->sector_buf[40]); |
| 2340 | if (!(trusted_cap & (1ULL << 63))) { |
| 2341 | ata_dev_dbg(dev, |
| 2342 | "Trusted Computing capability qword not valid!\n"); |
| 2343 | return; |
| 2344 | } |
| 2345 | |
| 2346 | if (trusted_cap & (1 << 0)) |
| 2347 | dev->flags |= ATA_DFLAG_TRUSTED; |
| 2348 | } |
| 2349 | |
Damien Le Moal | 891fd7c | 2021-08-16 10:44:50 +0900 | [diff] [blame] | 2350 | static int ata_dev_config_lba(struct ata_device *dev) |
| 2351 | { |
| 2352 | struct ata_port *ap = dev->link->ap; |
| 2353 | const u16 *id = dev->id; |
| 2354 | const char *lba_desc; |
| 2355 | char ncq_desc[24]; |
| 2356 | int ret; |
| 2357 | |
| 2358 | dev->flags |= ATA_DFLAG_LBA; |
| 2359 | |
| 2360 | if (ata_id_has_lba48(id)) { |
| 2361 | lba_desc = "LBA48"; |
| 2362 | dev->flags |= ATA_DFLAG_LBA48; |
| 2363 | if (dev->n_sectors >= (1UL << 28) && |
| 2364 | ata_id_has_flush_ext(id)) |
| 2365 | dev->flags |= ATA_DFLAG_FLUSH_EXT; |
| 2366 | } else { |
| 2367 | lba_desc = "LBA"; |
| 2368 | } |
| 2369 | |
| 2370 | /* config NCQ */ |
| 2371 | ret = ata_dev_config_ncq(dev, ncq_desc, sizeof(ncq_desc)); |
| 2372 | |
| 2373 | /* print device info to dmesg */ |
| 2374 | if (ata_msg_drv(ap) && ata_dev_print_info(dev)) |
| 2375 | ata_dev_info(dev, |
| 2376 | "%llu sectors, multi %u: %s %s\n", |
| 2377 | (unsigned long long)dev->n_sectors, |
| 2378 | dev->multi_count, lba_desc, ncq_desc); |
| 2379 | |
| 2380 | return ret; |
| 2381 | } |
| 2382 | |
| 2383 | static void ata_dev_config_chs(struct ata_device *dev) |
| 2384 | { |
| 2385 | struct ata_port *ap = dev->link->ap; |
| 2386 | const u16 *id = dev->id; |
| 2387 | |
| 2388 | if (ata_id_current_chs_valid(id)) { |
| 2389 | /* Current CHS translation is valid. */ |
| 2390 | dev->cylinders = id[54]; |
| 2391 | dev->heads = id[55]; |
| 2392 | dev->sectors = id[56]; |
| 2393 | } else { |
| 2394 | /* Default translation */ |
| 2395 | dev->cylinders = id[1]; |
| 2396 | dev->heads = id[3]; |
| 2397 | dev->sectors = id[6]; |
| 2398 | } |
| 2399 | |
| 2400 | /* print device info to dmesg */ |
| 2401 | if (ata_msg_drv(ap) && ata_dev_print_info(dev)) |
| 2402 | ata_dev_info(dev, |
| 2403 | "%llu sectors, multi %u, CHS %u/%u/%u\n", |
| 2404 | (unsigned long long)dev->n_sectors, |
| 2405 | dev->multi_count, dev->cylinders, |
| 2406 | dev->heads, dev->sectors); |
| 2407 | } |
| 2408 | |
Damien Le Moal | d8d8778 | 2021-08-16 10:44:49 +0900 | [diff] [blame] | 2409 | static void ata_dev_config_devslp(struct ata_device *dev) |
| 2410 | { |
| 2411 | u8 *sata_setting = dev->link->ap->sector_buf; |
| 2412 | unsigned int err_mask; |
| 2413 | int i, j; |
| 2414 | |
| 2415 | /* |
| 2416 | * Check device sleep capability. Get DevSlp timing variables |
| 2417 | * from SATA Settings page of Identify Device Data Log. |
| 2418 | */ |
| 2419 | if (!ata_id_has_devslp(dev->id)) |
| 2420 | return; |
| 2421 | |
| 2422 | err_mask = ata_read_log_page(dev, |
| 2423 | ATA_LOG_IDENTIFY_DEVICE, |
| 2424 | ATA_LOG_SATA_SETTINGS, |
| 2425 | sata_setting, 1); |
Damien Le Moal | fc5c8aa | 2021-08-16 10:44:52 +0900 | [diff] [blame^] | 2426 | if (err_mask) |
Damien Le Moal | d8d8778 | 2021-08-16 10:44:49 +0900 | [diff] [blame] | 2427 | return; |
Damien Le Moal | d8d8778 | 2021-08-16 10:44:49 +0900 | [diff] [blame] | 2428 | |
| 2429 | dev->flags |= ATA_DFLAG_DEVSLP; |
| 2430 | for (i = 0; i < ATA_LOG_DEVSLP_SIZE; i++) { |
| 2431 | j = ATA_LOG_DEVSLP_OFFSET + i; |
| 2432 | dev->devslp_timing[i] = sata_setting[j]; |
| 2433 | } |
| 2434 | } |
| 2435 | |
Tejun Heo | 49016ac | 2006-02-21 02:12:11 +0900 | [diff] [blame] | 2436 | /** |
Tejun Heo | ffeae41 | 2006-03-01 16:09:35 +0900 | [diff] [blame] | 2437 | * ata_dev_configure - Configure the specified ATA/ATAPI device |
Tejun Heo | ffeae41 | 2006-03-01 16:09:35 +0900 | [diff] [blame] | 2438 | * @dev: Target device to configure |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2439 | * |
Tejun Heo | ffeae41 | 2006-03-01 16:09:35 +0900 | [diff] [blame] | 2440 | * Configure @dev according to @dev->id. Generic and low-level |
| 2441 | * driver specific fixups are also applied. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2442 | * |
| 2443 | * LOCKING: |
Tejun Heo | ffeae41 | 2006-03-01 16:09:35 +0900 | [diff] [blame] | 2444 | * Kernel thread context (may sleep) |
| 2445 | * |
| 2446 | * RETURNS: |
| 2447 | * 0 on success, -errno otherwise |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2448 | */ |
Tejun Heo | efdaedc | 2006-11-01 18:38:52 +0900 | [diff] [blame] | 2449 | int ata_dev_configure(struct ata_device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2450 | { |
Tejun Heo | 9af5c9c | 2007-08-06 18:36:22 +0900 | [diff] [blame] | 2451 | struct ata_port *ap = dev->link->ap; |
Damien Le Moal | 891fd7c | 2021-08-16 10:44:50 +0900 | [diff] [blame] | 2452 | bool print_info = ata_dev_print_info(dev); |
Tejun Heo | 1148c3a | 2006-03-13 19:48:04 +0900 | [diff] [blame] | 2453 | const u16 *id = dev->id; |
Tejun Heo | 7dc951a | 2007-11-27 19:43:42 +0900 | [diff] [blame] | 2454 | unsigned long xfer_mask; |
Shane Huang | 65fe1f0 | 2012-09-07 22:40:01 +0800 | [diff] [blame] | 2455 | unsigned int err_mask; |
Alan Cox | b352e57 | 2006-08-10 18:52:12 +0100 | [diff] [blame] | 2456 | char revbuf[7]; /* XYZ-99\0 */ |
Eric D. Mudama | 3f64f56 | 2007-01-30 23:00:40 -0700 | [diff] [blame] | 2457 | char fwrevbuf[ATA_ID_FW_REV_LEN+1]; |
| 2458 | char modelbuf[ATA_ID_PROD_LEN+1]; |
Brian King | e6d902a | 2006-06-28 08:30:31 -0500 | [diff] [blame] | 2459 | int rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2460 | |
Borislav Petkov | 0dd4b21 | 2006-06-23 02:29:08 -0400 | [diff] [blame] | 2461 | if (!ata_dev_enabled(dev) && ata_msg_info(ap)) { |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 2462 | ata_dev_info(dev, "%s: ENTER/EXIT -- nodev\n", __func__); |
Tejun Heo | ffeae41 | 2006-03-01 16:09:35 +0900 | [diff] [blame] | 2463 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2464 | } |
| 2465 | |
Borislav Petkov | 0dd4b21 | 2006-06-23 02:29:08 -0400 | [diff] [blame] | 2466 | if (ata_msg_probe(ap)) |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 2467 | ata_dev_dbg(dev, "%s: ENTER\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2468 | |
Tejun Heo | 75683fe | 2007-07-05 13:31:27 +0900 | [diff] [blame] | 2469 | /* set horkage */ |
| 2470 | dev->horkage |= ata_dev_blacklisted(dev); |
Tejun Heo | 3326732 | 2008-02-13 09:15:09 +0900 | [diff] [blame] | 2471 | ata_force_horkage(dev); |
Tejun Heo | 75683fe | 2007-07-05 13:31:27 +0900 | [diff] [blame] | 2472 | |
Tejun Heo | 50af2fa | 2008-05-19 01:15:14 +0900 | [diff] [blame] | 2473 | if (dev->horkage & ATA_HORKAGE_DISABLE) { |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 2474 | ata_dev_info(dev, "unsupported device, disabling\n"); |
Tejun Heo | 50af2fa | 2008-05-19 01:15:14 +0900 | [diff] [blame] | 2475 | ata_dev_disable(dev); |
| 2476 | return 0; |
| 2477 | } |
| 2478 | |
Tejun Heo | 2486fa5 | 2008-07-31 07:52:40 +0900 | [diff] [blame] | 2479 | if ((!atapi_enabled || (ap->flags & ATA_FLAG_NO_ATAPI)) && |
| 2480 | dev->class == ATA_DEV_ATAPI) { |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 2481 | ata_dev_warn(dev, "WARNING: ATAPI is %s, device ignored\n", |
| 2482 | atapi_enabled ? "not supported with this driver" |
| 2483 | : "disabled"); |
Tejun Heo | 2486fa5 | 2008-07-31 07:52:40 +0900 | [diff] [blame] | 2484 | ata_dev_disable(dev); |
| 2485 | return 0; |
| 2486 | } |
| 2487 | |
Tejun Heo | 9062712 | 2009-01-29 20:31:36 +0900 | [diff] [blame] | 2488 | rc = ata_do_link_spd_horkage(dev); |
| 2489 | if (rc) |
| 2490 | return rc; |
| 2491 | |
Tejun Heo | ecd75ad | 2014-01-16 09:47:17 -0500 | [diff] [blame] | 2492 | /* some WD SATA-1 drives have issues with LPM, turn on NOLPM for them */ |
| 2493 | if ((dev->horkage & ATA_HORKAGE_WD_BROKEN_LPM) && |
| 2494 | (id[ATA_ID_SATA_CAPABILITY] & 0xe) == 0x2) |
| 2495 | dev->horkage |= ATA_HORKAGE_NOLPM; |
| 2496 | |
Hans de Goede | 240630e | 2018-07-01 12:15:46 +0200 | [diff] [blame] | 2497 | if (ap->flags & ATA_FLAG_NO_LPM) |
| 2498 | dev->horkage |= ATA_HORKAGE_NOLPM; |
| 2499 | |
Tejun Heo | ecd75ad | 2014-01-16 09:47:17 -0500 | [diff] [blame] | 2500 | if (dev->horkage & ATA_HORKAGE_NOLPM) { |
| 2501 | ata_dev_warn(dev, "LPM support broken, forcing max_power\n"); |
| 2502 | dev->link->ap->target_lpm_policy = ATA_LPM_MAX_POWER; |
| 2503 | } |
| 2504 | |
Tejun Heo | 6746544 | 2007-05-15 03:28:16 +0900 | [diff] [blame] | 2505 | /* let ACPI work its magic */ |
| 2506 | rc = ata_acpi_on_devcfg(dev); |
| 2507 | if (rc) |
| 2508 | return rc; |
Kristen Carlson Accardi | 08573a8 | 2006-11-10 16:14:47 -0800 | [diff] [blame] | 2509 | |
Tejun Heo | 05027ad | 2007-09-03 12:32:57 +0900 | [diff] [blame] | 2510 | /* massage HPA, do it early as it might change IDENTIFY data */ |
| 2511 | rc = ata_hpa_resize(dev); |
| 2512 | if (rc) |
| 2513 | return rc; |
| 2514 | |
Tejun Heo | c39f5eb | 2006-03-13 19:51:19 +0900 | [diff] [blame] | 2515 | /* print device capabilities */ |
Borislav Petkov | 0dd4b21 | 2006-06-23 02:29:08 -0400 | [diff] [blame] | 2516 | if (ata_msg_probe(ap)) |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 2517 | ata_dev_dbg(dev, |
| 2518 | "%s: cfg 49:%04x 82:%04x 83:%04x 84:%04x " |
| 2519 | "85:%04x 86:%04x 87:%04x 88:%04x\n", |
| 2520 | __func__, |
| 2521 | id[49], id[82], id[83], id[84], |
| 2522 | id[85], id[86], id[87], id[88]); |
Tejun Heo | c39f5eb | 2006-03-13 19:51:19 +0900 | [diff] [blame] | 2523 | |
Tejun Heo | 208a993 | 2006-03-05 17:55:58 +0900 | [diff] [blame] | 2524 | /* initialize to-be-configured parameters */ |
Tejun Heo | ea1dd4e | 2006-04-02 18:51:53 +0900 | [diff] [blame] | 2525 | dev->flags &= ~ATA_DFLAG_CFG_MASK; |
Tejun Heo | 208a993 | 2006-03-05 17:55:58 +0900 | [diff] [blame] | 2526 | dev->max_sectors = 0; |
| 2527 | dev->cdb_len = 0; |
| 2528 | dev->n_sectors = 0; |
| 2529 | dev->cylinders = 0; |
| 2530 | dev->heads = 0; |
| 2531 | dev->sectors = 0; |
Mark Lord | e18086d | 2009-03-19 13:32:21 -0400 | [diff] [blame] | 2532 | dev->multi_count = 0; |
Tejun Heo | 208a993 | 2006-03-05 17:55:58 +0900 | [diff] [blame] | 2533 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2534 | /* |
| 2535 | * common ATA, ATAPI feature tests |
| 2536 | */ |
| 2537 | |
Tejun Heo | ff8854b | 2006-03-06 04:31:56 +0900 | [diff] [blame] | 2538 | /* find max transfer mode; for printk only */ |
Tejun Heo | 1148c3a | 2006-03-13 19:48:04 +0900 | [diff] [blame] | 2539 | xfer_mask = ata_id_xfermask(id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2540 | |
Borislav Petkov | 0dd4b21 | 2006-06-23 02:29:08 -0400 | [diff] [blame] | 2541 | if (ata_msg_probe(ap)) |
| 2542 | ata_dump_id(id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2543 | |
Albert Lee | ef143d5 | 2007-06-05 13:01:33 +0800 | [diff] [blame] | 2544 | /* SCSI only uses 4-char revisions, dump full 8 chars from ATA */ |
| 2545 | ata_id_c_string(dev->id, fwrevbuf, ATA_ID_FW_REV, |
| 2546 | sizeof(fwrevbuf)); |
| 2547 | |
| 2548 | ata_id_c_string(dev->id, modelbuf, ATA_ID_PROD, |
| 2549 | sizeof(modelbuf)); |
| 2550 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2551 | /* ATA-specific feature tests */ |
Hannes Reinecke | 9162c65 | 2014-11-05 13:08:21 +0100 | [diff] [blame] | 2552 | if (dev->class == ATA_DEV_ATA || dev->class == ATA_DEV_ZAC) { |
Alan Cox | b352e57 | 2006-08-10 18:52:12 +0100 | [diff] [blame] | 2553 | if (ata_id_is_cfa(id)) { |
Sergei Shtylyov | 62afe5d | 2009-04-13 20:50:00 +0400 | [diff] [blame] | 2554 | /* CPRM may make this media unusable */ |
| 2555 | if (id[ATA_ID_CFA_KEY_MGMT] & 1) |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 2556 | ata_dev_warn(dev, |
| 2557 | "supports DRM functions and may not be fully accessible\n"); |
Alan Cox | b352e57 | 2006-08-10 18:52:12 +0100 | [diff] [blame] | 2558 | snprintf(revbuf, 7, "CFA"); |
Alan Cox | ae8d4ee | 2007-11-04 22:05:49 -0500 | [diff] [blame] | 2559 | } else { |
Jeff Garzik | 2dcb407 | 2007-10-19 06:42:56 -0400 | [diff] [blame] | 2560 | snprintf(revbuf, 7, "ATA-%d", ata_id_major_version(id)); |
Alan Cox | ae8d4ee | 2007-11-04 22:05:49 -0500 | [diff] [blame] | 2561 | /* Warn the user if the device has TPM extensions */ |
| 2562 | if (ata_id_has_tpm(id)) |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 2563 | ata_dev_warn(dev, |
| 2564 | "supports DRM functions and may not be fully accessible\n"); |
Alan Cox | ae8d4ee | 2007-11-04 22:05:49 -0500 | [diff] [blame] | 2565 | } |
Alan Cox | b352e57 | 2006-08-10 18:52:12 +0100 | [diff] [blame] | 2566 | |
Tejun Heo | 1148c3a | 2006-03-13 19:48:04 +0900 | [diff] [blame] | 2567 | dev->n_sectors = ata_id_n_sectors(id); |
Tejun Heo | 2940740 | 2006-02-12 22:47:04 +0900 | [diff] [blame] | 2568 | |
Mark Lord | e18086d | 2009-03-19 13:32:21 -0400 | [diff] [blame] | 2569 | /* get current R/W Multiple count setting */ |
| 2570 | if ((dev->id[47] >> 8) == 0x80 && (dev->id[59] & 0x100)) { |
| 2571 | unsigned int max = dev->id[47] & 0xff; |
| 2572 | unsigned int cnt = dev->id[59] & 0xff; |
| 2573 | /* only recognize/allow powers of two here */ |
| 2574 | if (is_power_of_2(max) && is_power_of_2(cnt)) |
| 2575 | if (cnt <= max) |
| 2576 | dev->multi_count = cnt; |
| 2577 | } |
Eric D. Mudama | 3f64f56 | 2007-01-30 23:00:40 -0700 | [diff] [blame] | 2578 | |
Damien Le Moal | 891fd7c | 2021-08-16 10:44:50 +0900 | [diff] [blame] | 2579 | /* print device info to dmesg */ |
| 2580 | if (ata_msg_drv(ap) && print_info) |
| 2581 | ata_dev_info(dev, "%s: %s, %s, max %s\n", |
| 2582 | revbuf, modelbuf, fwrevbuf, |
| 2583 | ata_mode_string(xfer_mask)); |
| 2584 | |
Tejun Heo | 1148c3a | 2006-03-13 19:48:04 +0900 | [diff] [blame] | 2585 | if (ata_id_has_lba(id)) { |
Damien Le Moal | 891fd7c | 2021-08-16 10:44:50 +0900 | [diff] [blame] | 2586 | rc = ata_dev_config_lba(dev); |
Shaohua Li | 388539f | 2009-07-27 09:24:35 +0800 | [diff] [blame] | 2587 | if (rc) |
| 2588 | return rc; |
Tejun Heo | ffeae41 | 2006-03-01 16:09:35 +0900 | [diff] [blame] | 2589 | } else { |
Damien Le Moal | 891fd7c | 2021-08-16 10:44:50 +0900 | [diff] [blame] | 2590 | ata_dev_config_chs(dev); |
Albert Lee | 07f6f7d | 2005-11-01 19:33:20 +0800 | [diff] [blame] | 2591 | } |
| 2592 | |
Damien Le Moal | d8d8778 | 2021-08-16 10:44:49 +0900 | [diff] [blame] | 2593 | ata_dev_config_devslp(dev); |
Hannes Reinecke | e87fd28 | 2016-04-04 11:43:55 +0200 | [diff] [blame] | 2594 | ata_dev_config_sense_reporting(dev); |
Hannes Reinecke | 6d1003a | 2016-04-25 12:45:56 +0200 | [diff] [blame] | 2595 | ata_dev_config_zac(dev); |
Christoph Hellwig | 818831c | 2017-06-04 14:42:24 +0200 | [diff] [blame] | 2596 | ata_dev_config_trusted(dev); |
Minwoo Im | b1ffbf8 | 2017-06-24 03:41:10 +0900 | [diff] [blame] | 2597 | dev->cdb_len = 32; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2598 | } |
| 2599 | |
| 2600 | /* ATAPI-specific feature tests */ |
Jeff Garzik | 2c13b7c | 2005-11-14 14:14:16 -0500 | [diff] [blame] | 2601 | else if (dev->class == ATA_DEV_ATAPI) { |
Tejun Heo | 854c73a | 2007-09-23 13:14:11 +0900 | [diff] [blame] | 2602 | const char *cdb_intr_string = ""; |
| 2603 | const char *atapi_an_string = ""; |
Tejun Heo | 9116300 | 2008-02-21 13:25:50 +0900 | [diff] [blame] | 2604 | const char *dma_dir_string = ""; |
Tejun Heo | 7d77b24 | 2007-09-23 13:14:13 +0900 | [diff] [blame] | 2605 | u32 sntf; |
Albert Lee | 08a556d | 2006-03-31 13:29:04 +0800 | [diff] [blame] | 2606 | |
Tejun Heo | 1148c3a | 2006-03-13 19:48:04 +0900 | [diff] [blame] | 2607 | rc = atapi_cdb_len(id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2608 | if ((rc < 12) || (rc > ATAPI_CDB_LEN)) { |
Borislav Petkov | 0dd4b21 | 2006-06-23 02:29:08 -0400 | [diff] [blame] | 2609 | if (ata_msg_warn(ap)) |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 2610 | ata_dev_warn(dev, "unsupported CDB len\n"); |
Tejun Heo | ffeae41 | 2006-03-01 16:09:35 +0900 | [diff] [blame] | 2611 | rc = -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2612 | goto err_out_nosup; |
| 2613 | } |
Tejun Heo | 6e7846e | 2006-02-12 23:32:58 +0900 | [diff] [blame] | 2614 | dev->cdb_len = (unsigned int) rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2615 | |
Tejun Heo | 7d77b24 | 2007-09-23 13:14:13 +0900 | [diff] [blame] | 2616 | /* Enable ATAPI AN if both the host and device have |
| 2617 | * the support. If PMP is attached, SNTF is required |
| 2618 | * to enable ATAPI AN to discern between PHY status |
| 2619 | * changed notifications and ATAPI ANs. |
Kristen Carlson Accardi | 9f45cbd | 2007-08-15 03:57:11 -0400 | [diff] [blame] | 2620 | */ |
Tejun Heo | e7ecd43 | 2010-05-19 15:38:58 +0200 | [diff] [blame] | 2621 | if (atapi_an && |
| 2622 | (ap->flags & ATA_FLAG_AN) && ata_id_has_atapi_AN(id) && |
Tejun Heo | 071f44b | 2008-04-07 22:47:22 +0900 | [diff] [blame] | 2623 | (!sata_pmp_attached(ap) || |
Tejun Heo | 7d77b24 | 2007-09-23 13:14:13 +0900 | [diff] [blame] | 2624 | sata_scr_read(&ap->link, SCR_NOTIFICATION, &sntf) == 0)) { |
Kristen Carlson Accardi | 9f45cbd | 2007-08-15 03:57:11 -0400 | [diff] [blame] | 2625 | /* issue SET feature command to turn this on */ |
Jeff Garzik | 218f3d3 | 2007-10-25 00:33:27 -0400 | [diff] [blame] | 2626 | err_mask = ata_dev_set_feature(dev, |
| 2627 | SETFEATURES_SATA_ENABLE, SATA_AN); |
Tejun Heo | 854c73a | 2007-09-23 13:14:11 +0900 | [diff] [blame] | 2628 | if (err_mask) |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 2629 | ata_dev_err(dev, |
| 2630 | "failed to enable ATAPI AN (err_mask=0x%x)\n", |
| 2631 | err_mask); |
Tejun Heo | 854c73a | 2007-09-23 13:14:11 +0900 | [diff] [blame] | 2632 | else { |
Kristen Carlson Accardi | 9f45cbd | 2007-08-15 03:57:11 -0400 | [diff] [blame] | 2633 | dev->flags |= ATA_DFLAG_AN; |
Tejun Heo | 854c73a | 2007-09-23 13:14:11 +0900 | [diff] [blame] | 2634 | atapi_an_string = ", ATAPI AN"; |
| 2635 | } |
Kristen Carlson Accardi | 9f45cbd | 2007-08-15 03:57:11 -0400 | [diff] [blame] | 2636 | } |
| 2637 | |
Albert Lee | 08a556d | 2006-03-31 13:29:04 +0800 | [diff] [blame] | 2638 | if (ata_id_cdb_intr(dev->id)) { |
Albert Lee | 312f7da | 2005-09-27 17:38:03 +0800 | [diff] [blame] | 2639 | dev->flags |= ATA_DFLAG_CDB_INTR; |
Albert Lee | 08a556d | 2006-03-31 13:29:04 +0800 | [diff] [blame] | 2640 | cdb_intr_string = ", CDB intr"; |
| 2641 | } |
Albert Lee | 312f7da | 2005-09-27 17:38:03 +0800 | [diff] [blame] | 2642 | |
Vincent Pelletier | 966fbe1 | 2013-05-21 22:30:58 +0200 | [diff] [blame] | 2643 | 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] | 2644 | dev->flags |= ATA_DFLAG_DMADIR; |
| 2645 | dma_dir_string = ", DMADIR"; |
| 2646 | } |
| 2647 | |
Aaron Lu | afe7595 | 2013-01-15 17:20:58 +0800 | [diff] [blame] | 2648 | if (ata_id_has_da(dev->id)) { |
Lin Ming | b1354cb | 2012-06-25 16:13:08 +0800 | [diff] [blame] | 2649 | dev->flags |= ATA_DFLAG_DA; |
Aaron Lu | afe7595 | 2013-01-15 17:20:58 +0800 | [diff] [blame] | 2650 | zpodd_init(dev); |
| 2651 | } |
Lin Ming | b1354cb | 2012-06-25 16:13:08 +0800 | [diff] [blame] | 2652 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2653 | /* print device info to dmesg */ |
Borislav Petkov | 5afc814 | 2006-06-27 14:51:25 +0200 | [diff] [blame] | 2654 | if (ata_msg_drv(ap) && print_info) |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 2655 | ata_dev_info(dev, |
| 2656 | "ATAPI: %s, %s, max %s%s%s%s\n", |
| 2657 | modelbuf, fwrevbuf, |
| 2658 | ata_mode_string(xfer_mask), |
| 2659 | cdb_intr_string, atapi_an_string, |
| 2660 | dma_dir_string); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2661 | } |
| 2662 | |
Tejun Heo | 914ed35 | 2006-11-01 18:39:55 +0900 | [diff] [blame] | 2663 | /* determine max_sectors */ |
| 2664 | dev->max_sectors = ATA_MAX_SECTORS; |
| 2665 | if (dev->flags & ATA_DFLAG_LBA48) |
| 2666 | dev->max_sectors = ATA_MAX_SECTORS_LBA48; |
| 2667 | |
Alan Cox | c5038fc | 2007-10-25 14:21:16 +0100 | [diff] [blame] | 2668 | /* Limit PATA drive on SATA cable bridge transfers to udma5, |
| 2669 | 200 sectors */ |
Tejun Heo | 3373efd | 2006-05-15 20:57:53 +0900 | [diff] [blame] | 2670 | if (ata_dev_knobble(dev)) { |
Borislav Petkov | 5afc814 | 2006-06-27 14:51:25 +0200 | [diff] [blame] | 2671 | if (ata_msg_drv(ap) && print_info) |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 2672 | ata_dev_info(dev, "applying bridge limits\n"); |
Tejun Heo | 5a52913 | 2006-03-24 14:07:50 +0900 | [diff] [blame] | 2673 | dev->udma_mask &= ATA_UDMA5; |
Tejun Heo | 4b2f3ed | 2006-03-01 16:09:36 +0900 | [diff] [blame] | 2674 | dev->max_sectors = ATA_MAX_SECTORS; |
| 2675 | } |
| 2676 | |
Tony Battersby | f8d8e57 | 2007-10-30 11:44:35 -0400 | [diff] [blame] | 2677 | if ((dev->class == ATA_DEV_ATAPI) && |
Albert Lee | f442cd8 | 2007-11-15 10:35:47 +0900 | [diff] [blame] | 2678 | (atapi_command_packet_set(id) == TYPE_TAPE)) { |
Tony Battersby | f8d8e57 | 2007-10-30 11:44:35 -0400 | [diff] [blame] | 2679 | dev->max_sectors = ATA_MAX_SECTORS_TAPE; |
Albert Lee | f442cd8 | 2007-11-15 10:35:47 +0900 | [diff] [blame] | 2680 | dev->horkage |= ATA_HORKAGE_STUCK_ERR; |
| 2681 | } |
Tony Battersby | f8d8e57 | 2007-10-30 11:44:35 -0400 | [diff] [blame] | 2682 | |
Tejun Heo | 75683fe | 2007-07-05 13:31:27 +0900 | [diff] [blame] | 2683 | if (dev->horkage & ATA_HORKAGE_MAX_SEC_128) |
Tejun Heo | 03ec52d | 2007-04-12 13:38:11 +0900 | [diff] [blame] | 2684 | dev->max_sectors = min_t(unsigned int, ATA_MAX_SECTORS_128, |
| 2685 | dev->max_sectors); |
Albert Lee | 18d6e9d | 2007-04-02 11:34:15 +0800 | [diff] [blame] | 2686 | |
David Milburn | af34d63 | 2015-07-13 11:48:23 -0500 | [diff] [blame] | 2687 | if (dev->horkage & ATA_HORKAGE_MAX_SEC_1024) |
| 2688 | dev->max_sectors = min_t(unsigned int, ATA_MAX_SECTORS_1024, |
| 2689 | dev->max_sectors); |
| 2690 | |
Shan Hai | a32450e | 2013-03-18 10:30:44 +0800 | [diff] [blame] | 2691 | if (dev->horkage & ATA_HORKAGE_MAX_SEC_LBA48) |
| 2692 | dev->max_sectors = ATA_MAX_SECTORS_LBA48; |
| 2693 | |
Tejun Heo | 4b2f3ed | 2006-03-01 16:09:36 +0900 | [diff] [blame] | 2694 | if (ap->ops->dev_config) |
Alan | cd0d3bb | 2007-03-02 00:56:15 +0000 | [diff] [blame] | 2695 | ap->ops->dev_config(dev); |
Tejun Heo | 4b2f3ed | 2006-03-01 16:09:36 +0900 | [diff] [blame] | 2696 | |
Alan Cox | c5038fc | 2007-10-25 14:21:16 +0100 | [diff] [blame] | 2697 | if (dev->horkage & ATA_HORKAGE_DIAGNOSTIC) { |
| 2698 | /* Let the user know. We don't want to disallow opens for |
| 2699 | rescue purposes, or in case the vendor is just a blithering |
| 2700 | idiot. Do this after the dev_config call as some controllers |
| 2701 | with buggy firmware may want to avoid reporting false device |
| 2702 | bugs */ |
| 2703 | |
| 2704 | if (print_info) { |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 2705 | ata_dev_warn(dev, |
Alan Cox | c5038fc | 2007-10-25 14:21:16 +0100 | [diff] [blame] | 2706 | "Drive reports diagnostics failure. This may indicate a drive\n"); |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 2707 | ata_dev_warn(dev, |
Alan Cox | c5038fc | 2007-10-25 14:21:16 +0100 | [diff] [blame] | 2708 | "fault or invalid emulation. Contact drive vendor for information.\n"); |
| 2709 | } |
| 2710 | } |
| 2711 | |
Tejun Heo | ac70a96 | 2008-11-27 13:36:48 +0900 | [diff] [blame] | 2712 | if ((dev->horkage & ATA_HORKAGE_FIRMWARE_WARN) && print_info) { |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 2713 | ata_dev_warn(dev, "WARNING: device requires firmware update to be fully functional\n"); |
| 2714 | 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] | 2715 | } |
| 2716 | |
Tejun Heo | ffeae41 | 2006-03-01 16:09:35 +0900 | [diff] [blame] | 2717 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2718 | |
| 2719 | err_out_nosup: |
Borislav Petkov | 0dd4b21 | 2006-06-23 02:29:08 -0400 | [diff] [blame] | 2720 | if (ata_msg_probe(ap)) |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 2721 | ata_dev_dbg(dev, "%s: EXIT, err\n", __func__); |
Tejun Heo | ffeae41 | 2006-03-01 16:09:35 +0900 | [diff] [blame] | 2722 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2723 | } |
| 2724 | |
| 2725 | /** |
Alan Cox | 2e41e8e | 2007-03-08 23:19:19 +0000 | [diff] [blame] | 2726 | * ata_cable_40wire - return 40 wire cable type |
Alan Cox | be0d18d | 2007-03-06 02:37:56 -0800 | [diff] [blame] | 2727 | * @ap: port |
| 2728 | * |
Alan Cox | 2e41e8e | 2007-03-08 23:19:19 +0000 | [diff] [blame] | 2729 | * Helper method for drivers which want to hardwire 40 wire cable |
Alan Cox | be0d18d | 2007-03-06 02:37:56 -0800 | [diff] [blame] | 2730 | * detection. |
| 2731 | */ |
| 2732 | |
| 2733 | int ata_cable_40wire(struct ata_port *ap) |
| 2734 | { |
| 2735 | return ATA_CBL_PATA40; |
| 2736 | } |
Bartlomiej Zolnierkiewicz | a52fbcf | 2020-03-26 16:58:04 +0100 | [diff] [blame] | 2737 | EXPORT_SYMBOL_GPL(ata_cable_40wire); |
Alan Cox | be0d18d | 2007-03-06 02:37:56 -0800 | [diff] [blame] | 2738 | |
| 2739 | /** |
Alan Cox | 2e41e8e | 2007-03-08 23:19:19 +0000 | [diff] [blame] | 2740 | * ata_cable_80wire - return 80 wire cable type |
Alan Cox | be0d18d | 2007-03-06 02:37:56 -0800 | [diff] [blame] | 2741 | * @ap: port |
| 2742 | * |
Alan Cox | 2e41e8e | 2007-03-08 23:19:19 +0000 | [diff] [blame] | 2743 | * Helper method for drivers which want to hardwire 80 wire cable |
Alan Cox | be0d18d | 2007-03-06 02:37:56 -0800 | [diff] [blame] | 2744 | * detection. |
| 2745 | */ |
| 2746 | |
| 2747 | int ata_cable_80wire(struct ata_port *ap) |
| 2748 | { |
| 2749 | return ATA_CBL_PATA80; |
| 2750 | } |
Bartlomiej Zolnierkiewicz | a52fbcf | 2020-03-26 16:58:04 +0100 | [diff] [blame] | 2751 | EXPORT_SYMBOL_GPL(ata_cable_80wire); |
Alan Cox | be0d18d | 2007-03-06 02:37:56 -0800 | [diff] [blame] | 2752 | |
| 2753 | /** |
| 2754 | * ata_cable_unknown - return unknown PATA cable. |
| 2755 | * @ap: port |
| 2756 | * |
| 2757 | * Helper method for drivers which have no PATA cable detection. |
| 2758 | */ |
| 2759 | |
| 2760 | int ata_cable_unknown(struct ata_port *ap) |
| 2761 | { |
| 2762 | return ATA_CBL_PATA_UNK; |
| 2763 | } |
Bartlomiej Zolnierkiewicz | a52fbcf | 2020-03-26 16:58:04 +0100 | [diff] [blame] | 2764 | EXPORT_SYMBOL_GPL(ata_cable_unknown); |
Alan Cox | be0d18d | 2007-03-06 02:37:56 -0800 | [diff] [blame] | 2765 | |
| 2766 | /** |
Tejun Heo | c88f90c | 2007-11-27 19:43:48 +0900 | [diff] [blame] | 2767 | * ata_cable_ignore - return ignored PATA cable. |
| 2768 | * @ap: port |
| 2769 | * |
| 2770 | * Helper method for drivers which don't use cable type to limit |
| 2771 | * transfer mode. |
| 2772 | */ |
| 2773 | int ata_cable_ignore(struct ata_port *ap) |
| 2774 | { |
| 2775 | return ATA_CBL_PATA_IGN; |
| 2776 | } |
Bartlomiej Zolnierkiewicz | a52fbcf | 2020-03-26 16:58:04 +0100 | [diff] [blame] | 2777 | EXPORT_SYMBOL_GPL(ata_cable_ignore); |
Tejun Heo | c88f90c | 2007-11-27 19:43:48 +0900 | [diff] [blame] | 2778 | |
| 2779 | /** |
Alan Cox | be0d18d | 2007-03-06 02:37:56 -0800 | [diff] [blame] | 2780 | * ata_cable_sata - return SATA cable type |
| 2781 | * @ap: port |
| 2782 | * |
| 2783 | * Helper method for drivers which have SATA cables |
| 2784 | */ |
| 2785 | |
| 2786 | int ata_cable_sata(struct ata_port *ap) |
| 2787 | { |
| 2788 | return ATA_CBL_SATA; |
| 2789 | } |
Bartlomiej Zolnierkiewicz | a52fbcf | 2020-03-26 16:58:04 +0100 | [diff] [blame] | 2790 | EXPORT_SYMBOL_GPL(ata_cable_sata); |
Alan Cox | be0d18d | 2007-03-06 02:37:56 -0800 | [diff] [blame] | 2791 | |
| 2792 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2793 | * ata_bus_probe - Reset and probe ATA bus |
| 2794 | * @ap: Bus to probe |
| 2795 | * |
Jeff Garzik | 0cba632 | 2005-05-30 19:49:12 -0400 | [diff] [blame] | 2796 | * Master ATA bus probing function. Initiates a hardware-dependent |
| 2797 | * bus reset, then attempts to identify any devices found on |
| 2798 | * the bus. |
| 2799 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2800 | * LOCKING: |
Jeff Garzik | 0cba632 | 2005-05-30 19:49:12 -0400 | [diff] [blame] | 2801 | * PCI/etc. bus probe sem. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2802 | * |
| 2803 | * RETURNS: |
Tejun Heo | 96072e6 | 2006-04-01 01:38:17 +0900 | [diff] [blame] | 2804 | * Zero on success, negative errno otherwise. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2805 | */ |
| 2806 | |
Brian King | 8028916 | 2006-08-07 14:27:31 -0500 | [diff] [blame] | 2807 | int ata_bus_probe(struct ata_port *ap) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2808 | { |
Tejun Heo | 28ca5c5 | 2006-03-01 16:09:36 +0900 | [diff] [blame] | 2809 | unsigned int classes[ATA_MAX_DEVICES]; |
Tejun Heo | 14d2bac | 2006-04-02 17:54:46 +0900 | [diff] [blame] | 2810 | int tries[ATA_MAX_DEVICES]; |
Tejun Heo | f58229f | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 2811 | int rc; |
Tejun Heo | e82cbdb | 2006-04-01 01:38:18 +0900 | [diff] [blame] | 2812 | struct ata_device *dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2813 | |
Tejun Heo | 1eca436 | 2008-11-03 20:03:17 +0900 | [diff] [blame] | 2814 | ata_for_each_dev(dev, &ap->link, ALL) |
Tejun Heo | f58229f | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 2815 | tries[dev->devno] = ATA_PROBE_MAX_TRIES; |
Tejun Heo | 14d2bac | 2006-04-02 17:54:46 +0900 | [diff] [blame] | 2816 | |
| 2817 | retry: |
Tejun Heo | 1eca436 | 2008-11-03 20:03:17 +0900 | [diff] [blame] | 2818 | ata_for_each_dev(dev, &ap->link, ALL) { |
Tejun Heo | cdeab11 | 2007-10-29 16:41:09 +0900 | [diff] [blame] | 2819 | /* If we issue an SRST then an ATA drive (not ATAPI) |
| 2820 | * may change configuration and be in PIO0 timing. If |
| 2821 | * we do a hard reset (or are coming from power on) |
| 2822 | * this is true for ATA or ATAPI. Until we've set a |
| 2823 | * suitable controller mode we should not touch the |
| 2824 | * bus as we may be talking too fast. |
| 2825 | */ |
| 2826 | dev->pio_mode = XFER_PIO_0; |
Aaron Lu | 5416912 | 2012-12-03 11:35:02 +0800 | [diff] [blame] | 2827 | dev->dma_mode = 0xff; |
Tejun Heo | cdeab11 | 2007-10-29 16:41:09 +0900 | [diff] [blame] | 2828 | |
| 2829 | /* If the controller has a pio mode setup function |
| 2830 | * then use it to set the chipset to rights. Don't |
| 2831 | * touch the DMA setup as that will be dealt with when |
| 2832 | * configuring devices. |
| 2833 | */ |
| 2834 | if (ap->ops->set_piomode) |
| 2835 | ap->ops->set_piomode(ap, dev); |
| 2836 | } |
| 2837 | |
Tejun Heo | 2044470 | 2006-03-13 01:57:01 +0900 | [diff] [blame] | 2838 | /* reset and determine device classes */ |
Tejun Heo | 52783c5 | 2006-05-31 18:28:22 +0900 | [diff] [blame] | 2839 | ap->ops->phy_reset(ap); |
Tejun Heo | 2061a47 | 2006-03-12 00:57:39 +0900 | [diff] [blame] | 2840 | |
Tejun Heo | 1eca436 | 2008-11-03 20:03:17 +0900 | [diff] [blame] | 2841 | ata_for_each_dev(dev, &ap->link, ALL) { |
Tejun Heo | 3e4ec34 | 2010-05-10 21:41:30 +0200 | [diff] [blame] | 2842 | if (dev->class != ATA_DEV_UNKNOWN) |
Tejun Heo | 52783c5 | 2006-05-31 18:28:22 +0900 | [diff] [blame] | 2843 | classes[dev->devno] = dev->class; |
| 2844 | else |
| 2845 | classes[dev->devno] = ATA_DEV_NONE; |
Tejun Heo | 2044470 | 2006-03-13 01:57:01 +0900 | [diff] [blame] | 2846 | |
Tejun Heo | 52783c5 | 2006-05-31 18:28:22 +0900 | [diff] [blame] | 2847 | dev->class = ATA_DEV_UNKNOWN; |
Tejun Heo | 28ca5c5 | 2006-03-01 16:09:36 +0900 | [diff] [blame] | 2848 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2849 | |
Jeff Garzik | f31f0cc | 2007-03-02 17:47:28 -0500 | [diff] [blame] | 2850 | /* read IDENTIFY page and configure devices. We have to do the identify |
| 2851 | specific sequence bass-ackwards so that PDIAG- is released by |
| 2852 | the slave device */ |
| 2853 | |
Tejun Heo | 1eca436 | 2008-11-03 20:03:17 +0900 | [diff] [blame] | 2854 | ata_for_each_dev(dev, &ap->link, ALL_REVERSE) { |
Tejun Heo | f58229f | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 2855 | if (tries[dev->devno]) |
| 2856 | dev->class = classes[dev->devno]; |
Tejun Heo | 14d2bac | 2006-04-02 17:54:46 +0900 | [diff] [blame] | 2857 | |
Tejun Heo | e1211e3 | 2006-04-01 01:38:18 +0900 | [diff] [blame] | 2858 | if (!ata_dev_enabled(dev)) |
Tejun Heo | ffeae41 | 2006-03-01 16:09:35 +0900 | [diff] [blame] | 2859 | continue; |
| 2860 | |
Tejun Heo | bff0464 | 2006-11-10 18:08:10 +0900 | [diff] [blame] | 2861 | rc = ata_dev_read_id(dev, &dev->class, ATA_READID_POSTRESET, |
| 2862 | dev->id); |
Tejun Heo | 14d2bac | 2006-04-02 17:54:46 +0900 | [diff] [blame] | 2863 | if (rc) |
| 2864 | goto fail; |
Jeff Garzik | f31f0cc | 2007-03-02 17:47:28 -0500 | [diff] [blame] | 2865 | } |
| 2866 | |
Alan Cox | be0d18d | 2007-03-06 02:37:56 -0800 | [diff] [blame] | 2867 | /* Now ask for the cable type as PDIAG- should have been released */ |
| 2868 | if (ap->ops->cable_detect) |
| 2869 | ap->cbl = ap->ops->cable_detect(ap); |
| 2870 | |
Tejun Heo | 1eca436 | 2008-11-03 20:03:17 +0900 | [diff] [blame] | 2871 | /* We may have SATA bridge glue hiding here irrespective of |
| 2872 | * the reported cable types and sensed types. When SATA |
| 2873 | * drives indicate we have a bridge, we don't know which end |
| 2874 | * of the link the bridge is which is a problem. |
| 2875 | */ |
| 2876 | ata_for_each_dev(dev, &ap->link, ENABLED) |
Alan Cox | 614fe29 | 2007-08-22 23:22:45 +0100 | [diff] [blame] | 2877 | if (ata_id_is_sata(dev->id)) |
| 2878 | ap->cbl = ATA_CBL_SATA; |
Alan Cox | 614fe29 | 2007-08-22 23:22:45 +0100 | [diff] [blame] | 2879 | |
Jeff Garzik | f31f0cc | 2007-03-02 17:47:28 -0500 | [diff] [blame] | 2880 | /* After the identify sequence we can now set up the devices. We do |
| 2881 | this in the normal order so that the user doesn't get confused */ |
| 2882 | |
Tejun Heo | 1eca436 | 2008-11-03 20:03:17 +0900 | [diff] [blame] | 2883 | ata_for_each_dev(dev, &ap->link, ENABLED) { |
Tejun Heo | 9af5c9c | 2007-08-06 18:36:22 +0900 | [diff] [blame] | 2884 | ap->link.eh_context.i.flags |= ATA_EHI_PRINTINFO; |
Tejun Heo | efdaedc | 2006-11-01 18:38:52 +0900 | [diff] [blame] | 2885 | rc = ata_dev_configure(dev); |
Tejun Heo | 9af5c9c | 2007-08-06 18:36:22 +0900 | [diff] [blame] | 2886 | ap->link.eh_context.i.flags &= ~ATA_EHI_PRINTINFO; |
Tejun Heo | 14d2bac | 2006-04-02 17:54:46 +0900 | [diff] [blame] | 2887 | if (rc) |
| 2888 | goto fail; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2889 | } |
| 2890 | |
Tejun Heo | e82cbdb | 2006-04-01 01:38:18 +0900 | [diff] [blame] | 2891 | /* configure transfer mode */ |
Tejun Heo | 0260731 | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 2892 | rc = ata_set_mode(&ap->link, &dev); |
Tejun Heo | 4ae72a1 | 2007-02-02 16:22:30 +0900 | [diff] [blame] | 2893 | if (rc) |
Tejun Heo | 51713d3 | 2006-04-11 22:26:29 +0900 | [diff] [blame] | 2894 | goto fail; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2895 | |
Tejun Heo | 1eca436 | 2008-11-03 20:03:17 +0900 | [diff] [blame] | 2896 | ata_for_each_dev(dev, &ap->link, ENABLED) |
| 2897 | return 0; |
Alan Cox | e35a9e0 | 2006-03-27 18:46:37 +0100 | [diff] [blame] | 2898 | |
Tejun Heo | 96072e6 | 2006-04-01 01:38:17 +0900 | [diff] [blame] | 2899 | return -ENODEV; |
Tejun Heo | 14d2bac | 2006-04-02 17:54:46 +0900 | [diff] [blame] | 2900 | |
| 2901 | fail: |
Tejun Heo | 4ae72a1 | 2007-02-02 16:22:30 +0900 | [diff] [blame] | 2902 | tries[dev->devno]--; |
| 2903 | |
Tejun Heo | 14d2bac | 2006-04-02 17:54:46 +0900 | [diff] [blame] | 2904 | switch (rc) { |
| 2905 | case -EINVAL: |
Tejun Heo | 4ae72a1 | 2007-02-02 16:22:30 +0900 | [diff] [blame] | 2906 | /* eeek, something went very wrong, give up */ |
Tejun Heo | 14d2bac | 2006-04-02 17:54:46 +0900 | [diff] [blame] | 2907 | tries[dev->devno] = 0; |
| 2908 | break; |
Tejun Heo | 4ae72a1 | 2007-02-02 16:22:30 +0900 | [diff] [blame] | 2909 | |
| 2910 | case -ENODEV: |
| 2911 | /* give it just one more chance */ |
| 2912 | tries[dev->devno] = min(tries[dev->devno], 1); |
Gustavo A. R. Silva | df561f66 | 2020-08-23 17:36:59 -0500 | [diff] [blame] | 2913 | fallthrough; |
Tejun Heo | 14d2bac | 2006-04-02 17:54:46 +0900 | [diff] [blame] | 2914 | case -EIO: |
Tejun Heo | 4ae72a1 | 2007-02-02 16:22:30 +0900 | [diff] [blame] | 2915 | if (tries[dev->devno] == 1) { |
| 2916 | /* This is the last chance, better to slow |
| 2917 | * down than lose it. |
| 2918 | */ |
Tejun Heo | a07d499 | 2009-01-29 20:31:33 +0900 | [diff] [blame] | 2919 | sata_down_spd_limit(&ap->link, 0); |
Tejun Heo | 4ae72a1 | 2007-02-02 16:22:30 +0900 | [diff] [blame] | 2920 | ata_down_xfermask_limit(dev, ATA_DNXFER_PIO); |
| 2921 | } |
Tejun Heo | 14d2bac | 2006-04-02 17:54:46 +0900 | [diff] [blame] | 2922 | } |
| 2923 | |
Tejun Heo | 4ae72a1 | 2007-02-02 16:22:30 +0900 | [diff] [blame] | 2924 | if (!tries[dev->devno]) |
Tejun Heo | 3373efd | 2006-05-15 20:57:53 +0900 | [diff] [blame] | 2925 | ata_dev_disable(dev); |
Tejun Heo | ec57375 | 2006-04-11 22:26:29 +0900 | [diff] [blame] | 2926 | |
Tejun Heo | 14d2bac | 2006-04-02 17:54:46 +0900 | [diff] [blame] | 2927 | goto retry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2928 | } |
| 2929 | |
| 2930 | /** |
Tejun Heo | 3be680b | 2005-12-19 22:35:02 +0900 | [diff] [blame] | 2931 | * sata_print_link_status - Print SATA link status |
Tejun Heo | 936fd73 | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 2932 | * @link: SATA link to printk link status about |
Tejun Heo | 3be680b | 2005-12-19 22:35:02 +0900 | [diff] [blame] | 2933 | * |
| 2934 | * This function prints link speed and status of a SATA link. |
| 2935 | * |
| 2936 | * LOCKING: |
| 2937 | * None. |
| 2938 | */ |
Adrian Bunk | 6bdb4fc | 2008-04-21 11:51:11 +0300 | [diff] [blame] | 2939 | static void sata_print_link_status(struct ata_link *link) |
Tejun Heo | 3be680b | 2005-12-19 22:35:02 +0900 | [diff] [blame] | 2940 | { |
Tejun Heo | 6d5f973 | 2006-04-03 00:09:41 +0900 | [diff] [blame] | 2941 | u32 sstatus, scontrol, tmp; |
Tejun Heo | 3be680b | 2005-12-19 22:35:02 +0900 | [diff] [blame] | 2942 | |
Tejun Heo | 936fd73 | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 2943 | if (sata_scr_read(link, SCR_STATUS, &sstatus)) |
Tejun Heo | 3be680b | 2005-12-19 22:35:02 +0900 | [diff] [blame] | 2944 | return; |
Tejun Heo | 936fd73 | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 2945 | sata_scr_read(link, SCR_CONTROL, &scontrol); |
Tejun Heo | 3be680b | 2005-12-19 22:35:02 +0900 | [diff] [blame] | 2946 | |
Tejun Heo | b1c7291 | 2008-07-31 17:02:43 +0900 | [diff] [blame] | 2947 | if (ata_phys_link_online(link)) { |
Tejun Heo | 3be680b | 2005-12-19 22:35:02 +0900 | [diff] [blame] | 2948 | tmp = (sstatus >> 4) & 0xf; |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 2949 | ata_link_info(link, "SATA link up %s (SStatus %X SControl %X)\n", |
| 2950 | sata_spd_string(tmp), sstatus, scontrol); |
Tejun Heo | 3be680b | 2005-12-19 22:35:02 +0900 | [diff] [blame] | 2951 | } else { |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 2952 | ata_link_info(link, "SATA link down (SStatus %X SControl %X)\n", |
| 2953 | sstatus, scontrol); |
Tejun Heo | 3be680b | 2005-12-19 22:35:02 +0900 | [diff] [blame] | 2954 | } |
| 2955 | } |
| 2956 | |
| 2957 | /** |
Alan Cox | ebdfca6e | 2006-03-23 15:38:34 +0000 | [diff] [blame] | 2958 | * ata_dev_pair - return other device on cable |
Alan Cox | ebdfca6e | 2006-03-23 15:38:34 +0000 | [diff] [blame] | 2959 | * @adev: device |
| 2960 | * |
| 2961 | * Obtain the other device on the same cable, or if none is |
| 2962 | * present NULL is returned |
| 2963 | */ |
Jeff Garzik | 2e9edbf | 2006-03-24 09:56:57 -0500 | [diff] [blame] | 2964 | |
Tejun Heo | 3373efd | 2006-05-15 20:57:53 +0900 | [diff] [blame] | 2965 | struct ata_device *ata_dev_pair(struct ata_device *adev) |
Alan Cox | ebdfca6e | 2006-03-23 15:38:34 +0000 | [diff] [blame] | 2966 | { |
Tejun Heo | 9af5c9c | 2007-08-06 18:36:22 +0900 | [diff] [blame] | 2967 | struct ata_link *link = adev->link; |
| 2968 | struct ata_device *pair = &link->device[1 - adev->devno]; |
Tejun Heo | e1211e3 | 2006-04-01 01:38:18 +0900 | [diff] [blame] | 2969 | if (!ata_dev_enabled(pair)) |
Alan Cox | ebdfca6e | 2006-03-23 15:38:34 +0000 | [diff] [blame] | 2970 | return NULL; |
| 2971 | return pair; |
| 2972 | } |
Bartlomiej Zolnierkiewicz | a52fbcf | 2020-03-26 16:58:04 +0100 | [diff] [blame] | 2973 | EXPORT_SYMBOL_GPL(ata_dev_pair); |
Alan Cox | ebdfca6e | 2006-03-23 15:38:34 +0000 | [diff] [blame] | 2974 | |
| 2975 | /** |
Tejun Heo | 3c567b7 | 2006-05-15 20:57:23 +0900 | [diff] [blame] | 2976 | * sata_down_spd_limit - adjust SATA spd limit downward |
Tejun Heo | 936fd73 | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 2977 | * @link: Link to adjust SATA spd limit for |
Tejun Heo | a07d499 | 2009-01-29 20:31:33 +0900 | [diff] [blame] | 2978 | * @spd_limit: Additional limit |
Tejun Heo | 1c3fae4 | 2006-04-02 20:53:28 +0900 | [diff] [blame] | 2979 | * |
Tejun Heo | 936fd73 | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 2980 | * Adjust SATA spd limit of @link downward. Note that this |
Tejun Heo | 1c3fae4 | 2006-04-02 20:53:28 +0900 | [diff] [blame] | 2981 | * function only adjusts the limit. The change must be applied |
Tejun Heo | 3c567b7 | 2006-05-15 20:57:23 +0900 | [diff] [blame] | 2982 | * using sata_set_spd(). |
Tejun Heo | 1c3fae4 | 2006-04-02 20:53:28 +0900 | [diff] [blame] | 2983 | * |
Tejun Heo | a07d499 | 2009-01-29 20:31:33 +0900 | [diff] [blame] | 2984 | * If @spd_limit is non-zero, the speed is limited to equal to or |
| 2985 | * lower than @spd_limit if such speed is supported. If |
| 2986 | * @spd_limit is slower than any supported speed, only the lowest |
| 2987 | * supported speed is allowed. |
| 2988 | * |
Tejun Heo | 1c3fae4 | 2006-04-02 20:53:28 +0900 | [diff] [blame] | 2989 | * LOCKING: |
| 2990 | * Inherited from caller. |
| 2991 | * |
| 2992 | * RETURNS: |
| 2993 | * 0 on success, negative errno on failure |
| 2994 | */ |
Tejun Heo | a07d499 | 2009-01-29 20:31:33 +0900 | [diff] [blame] | 2995 | int sata_down_spd_limit(struct ata_link *link, u32 spd_limit) |
Tejun Heo | 1c3fae4 | 2006-04-02 20:53:28 +0900 | [diff] [blame] | 2996 | { |
Tejun Heo | 81952c5 | 2006-05-15 20:57:47 +0900 | [diff] [blame] | 2997 | u32 sstatus, spd, mask; |
Tejun Heo | a07d499 | 2009-01-29 20:31:33 +0900 | [diff] [blame] | 2998 | int rc, bit; |
Tejun Heo | 1c3fae4 | 2006-04-02 20:53:28 +0900 | [diff] [blame] | 2999 | |
Tejun Heo | 936fd73 | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 3000 | if (!sata_scr_valid(link)) |
Tejun Heo | 008a789 | 2007-07-16 14:29:40 +0900 | [diff] [blame] | 3001 | return -EOPNOTSUPP; |
| 3002 | |
| 3003 | /* If SCR can be read, use it to determine the current SPD. |
Tejun Heo | 936fd73 | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 3004 | * If not, use cached value in link->sata_spd. |
Tejun Heo | 008a789 | 2007-07-16 14:29:40 +0900 | [diff] [blame] | 3005 | */ |
Tejun Heo | 936fd73 | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 3006 | rc = sata_scr_read(link, SCR_STATUS, &sstatus); |
Tejun Heo | 9913ff8a | 2009-01-29 20:31:31 +0900 | [diff] [blame] | 3007 | if (rc == 0 && ata_sstatus_online(sstatus)) |
Tejun Heo | 008a789 | 2007-07-16 14:29:40 +0900 | [diff] [blame] | 3008 | spd = (sstatus >> 4) & 0xf; |
| 3009 | else |
Tejun Heo | 936fd73 | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 3010 | spd = link->sata_spd; |
Tejun Heo | 1c3fae4 | 2006-04-02 20:53:28 +0900 | [diff] [blame] | 3011 | |
Tejun Heo | 936fd73 | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 3012 | mask = link->sata_spd_limit; |
Tejun Heo | 1c3fae4 | 2006-04-02 20:53:28 +0900 | [diff] [blame] | 3013 | if (mask <= 1) |
| 3014 | return -EINVAL; |
Tejun Heo | 008a789 | 2007-07-16 14:29:40 +0900 | [diff] [blame] | 3015 | |
| 3016 | /* unconditionally mask off the highest bit */ |
Tejun Heo | a07d499 | 2009-01-29 20:31:33 +0900 | [diff] [blame] | 3017 | bit = fls(mask) - 1; |
| 3018 | mask &= ~(1 << bit); |
Tejun Heo | 1c3fae4 | 2006-04-02 20:53:28 +0900 | [diff] [blame] | 3019 | |
David Milburn | 2dc0b46 | 2017-11-14 16:17:25 -0600 | [diff] [blame] | 3020 | /* |
| 3021 | * Mask off all speeds higher than or equal to the current one. At |
| 3022 | * this point, if current SPD is not available and we previously |
| 3023 | * recorded the link speed from SStatus, the driver has already |
| 3024 | * masked off the highest bit so mask should already be 1 or 0. |
| 3025 | * Otherwise, we should not force 1.5Gbps on a link where we have |
| 3026 | * not previously recorded speed from SStatus. Just return in this |
| 3027 | * case. |
Tejun Heo | 008a789 | 2007-07-16 14:29:40 +0900 | [diff] [blame] | 3028 | */ |
| 3029 | if (spd > 1) |
| 3030 | mask &= (1 << (spd - 1)) - 1; |
| 3031 | else |
David Milburn | 2dc0b46 | 2017-11-14 16:17:25 -0600 | [diff] [blame] | 3032 | return -EINVAL; |
Tejun Heo | 008a789 | 2007-07-16 14:29:40 +0900 | [diff] [blame] | 3033 | |
| 3034 | /* were we already at the bottom? */ |
Tejun Heo | 1c3fae4 | 2006-04-02 20:53:28 +0900 | [diff] [blame] | 3035 | if (!mask) |
| 3036 | return -EINVAL; |
| 3037 | |
Tejun Heo | a07d499 | 2009-01-29 20:31:33 +0900 | [diff] [blame] | 3038 | if (spd_limit) { |
| 3039 | if (mask & ((1 << spd_limit) - 1)) |
| 3040 | mask &= (1 << spd_limit) - 1; |
| 3041 | else { |
| 3042 | bit = ffs(mask) - 1; |
| 3043 | mask = 1 << bit; |
| 3044 | } |
| 3045 | } |
| 3046 | |
Tejun Heo | 936fd73 | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 3047 | link->sata_spd_limit = mask; |
Tejun Heo | 1c3fae4 | 2006-04-02 20:53:28 +0900 | [diff] [blame] | 3048 | |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 3049 | ata_link_warn(link, "limiting SATA link speed to %s\n", |
| 3050 | sata_spd_string(fls(mask))); |
Tejun Heo | 1c3fae4 | 2006-04-02 20:53:28 +0900 | [diff] [blame] | 3051 | |
| 3052 | return 0; |
| 3053 | } |
| 3054 | |
Bartlomiej Zolnierkiewicz | a9b2c12 | 2020-03-26 16:58:07 +0100 | [diff] [blame] | 3055 | #ifdef CONFIG_ATA_ACPI |
Tejun Heo | cf176e1 | 2006-04-02 17:54:46 +0900 | [diff] [blame] | 3056 | /** |
Tejun Heo | a0f79b9 | 2007-12-18 16:33:05 +0900 | [diff] [blame] | 3057 | * ata_timing_cycle2mode - find xfer mode for the specified cycle duration |
| 3058 | * @xfer_shift: ATA_SHIFT_* value for transfer type to examine. |
| 3059 | * @cycle: cycle duration in ns |
| 3060 | * |
| 3061 | * Return matching xfer mode for @cycle. The returned mode is of |
| 3062 | * the transfer type specified by @xfer_shift. If @cycle is too |
| 3063 | * slow for @xfer_shift, 0xff is returned. If @cycle is faster |
| 3064 | * than the fastest known mode, the fasted mode is returned. |
| 3065 | * |
| 3066 | * LOCKING: |
| 3067 | * None. |
| 3068 | * |
| 3069 | * RETURNS: |
| 3070 | * Matching xfer_mode, 0xff if no match found. |
| 3071 | */ |
| 3072 | u8 ata_timing_cycle2mode(unsigned int xfer_shift, int cycle) |
| 3073 | { |
| 3074 | u8 base_mode = 0xff, last_mode = 0xff; |
| 3075 | const struct ata_xfer_ent *ent; |
| 3076 | const struct ata_timing *t; |
| 3077 | |
| 3078 | for (ent = ata_xfer_tbl; ent->shift >= 0; ent++) |
| 3079 | if (ent->shift == xfer_shift) |
| 3080 | base_mode = ent->base; |
| 3081 | |
| 3082 | for (t = ata_timing_find_mode(base_mode); |
| 3083 | t && ata_xfer_mode2shift(t->mode) == xfer_shift; t++) { |
| 3084 | unsigned short this_cycle; |
| 3085 | |
| 3086 | switch (xfer_shift) { |
| 3087 | case ATA_SHIFT_PIO: |
| 3088 | case ATA_SHIFT_MWDMA: |
| 3089 | this_cycle = t->cycle; |
| 3090 | break; |
| 3091 | case ATA_SHIFT_UDMA: |
| 3092 | this_cycle = t->udma; |
| 3093 | break; |
| 3094 | default: |
| 3095 | return 0xff; |
| 3096 | } |
| 3097 | |
| 3098 | if (cycle > this_cycle) |
| 3099 | break; |
| 3100 | |
| 3101 | last_mode = t->mode; |
| 3102 | } |
| 3103 | |
| 3104 | return last_mode; |
| 3105 | } |
Bartlomiej Zolnierkiewicz | a9b2c12 | 2020-03-26 16:58:07 +0100 | [diff] [blame] | 3106 | #endif |
Tejun Heo | a0f79b9 | 2007-12-18 16:33:05 +0900 | [diff] [blame] | 3107 | |
| 3108 | /** |
Tejun Heo | cf176e1 | 2006-04-02 17:54:46 +0900 | [diff] [blame] | 3109 | * ata_down_xfermask_limit - adjust dev xfer masks downward |
Tejun Heo | cf176e1 | 2006-04-02 17:54:46 +0900 | [diff] [blame] | 3110 | * @dev: Device to adjust xfer masks |
Tejun Heo | 458337d | 2007-02-02 16:22:30 +0900 | [diff] [blame] | 3111 | * @sel: ATA_DNXFER_* selector |
Tejun Heo | cf176e1 | 2006-04-02 17:54:46 +0900 | [diff] [blame] | 3112 | * |
| 3113 | * Adjust xfer masks of @dev downward. Note that this function |
| 3114 | * does not apply the change. Invoking ata_set_mode() afterwards |
| 3115 | * will apply the limit. |
| 3116 | * |
| 3117 | * LOCKING: |
| 3118 | * Inherited from caller. |
| 3119 | * |
| 3120 | * RETURNS: |
| 3121 | * 0 on success, negative errno on failure |
| 3122 | */ |
Tejun Heo | 458337d | 2007-02-02 16:22:30 +0900 | [diff] [blame] | 3123 | int ata_down_xfermask_limit(struct ata_device *dev, unsigned int sel) |
Tejun Heo | cf176e1 | 2006-04-02 17:54:46 +0900 | [diff] [blame] | 3124 | { |
Tejun Heo | 458337d | 2007-02-02 16:22:30 +0900 | [diff] [blame] | 3125 | char buf[32]; |
Tejun Heo | 7dc951a | 2007-11-27 19:43:42 +0900 | [diff] [blame] | 3126 | unsigned long orig_mask, xfer_mask; |
| 3127 | unsigned long pio_mask, mwdma_mask, udma_mask; |
Tejun Heo | 458337d | 2007-02-02 16:22:30 +0900 | [diff] [blame] | 3128 | int quiet, highbit; |
Tejun Heo | cf176e1 | 2006-04-02 17:54:46 +0900 | [diff] [blame] | 3129 | |
Tejun Heo | 458337d | 2007-02-02 16:22:30 +0900 | [diff] [blame] | 3130 | quiet = !!(sel & ATA_DNXFER_QUIET); |
| 3131 | sel &= ~ATA_DNXFER_QUIET; |
Tejun Heo | cf176e1 | 2006-04-02 17:54:46 +0900 | [diff] [blame] | 3132 | |
Tejun Heo | 458337d | 2007-02-02 16:22:30 +0900 | [diff] [blame] | 3133 | xfer_mask = orig_mask = ata_pack_xfermask(dev->pio_mask, |
| 3134 | dev->mwdma_mask, |
| 3135 | dev->udma_mask); |
| 3136 | ata_unpack_xfermask(xfer_mask, &pio_mask, &mwdma_mask, &udma_mask); |
Tejun Heo | cf176e1 | 2006-04-02 17:54:46 +0900 | [diff] [blame] | 3137 | |
Tejun Heo | 458337d | 2007-02-02 16:22:30 +0900 | [diff] [blame] | 3138 | switch (sel) { |
| 3139 | case ATA_DNXFER_PIO: |
| 3140 | highbit = fls(pio_mask) - 1; |
| 3141 | pio_mask &= ~(1 << highbit); |
| 3142 | break; |
| 3143 | |
| 3144 | case ATA_DNXFER_DMA: |
| 3145 | if (udma_mask) { |
| 3146 | highbit = fls(udma_mask) - 1; |
| 3147 | udma_mask &= ~(1 << highbit); |
| 3148 | if (!udma_mask) |
| 3149 | return -ENOENT; |
| 3150 | } else if (mwdma_mask) { |
| 3151 | highbit = fls(mwdma_mask) - 1; |
| 3152 | mwdma_mask &= ~(1 << highbit); |
| 3153 | if (!mwdma_mask) |
| 3154 | return -ENOENT; |
| 3155 | } |
| 3156 | break; |
| 3157 | |
| 3158 | case ATA_DNXFER_40C: |
| 3159 | udma_mask &= ATA_UDMA_MASK_40C; |
| 3160 | break; |
| 3161 | |
| 3162 | case ATA_DNXFER_FORCE_PIO0: |
| 3163 | pio_mask &= 1; |
Gustavo A. R. Silva | df561f66 | 2020-08-23 17:36:59 -0500 | [diff] [blame] | 3164 | fallthrough; |
Tejun Heo | 458337d | 2007-02-02 16:22:30 +0900 | [diff] [blame] | 3165 | case ATA_DNXFER_FORCE_PIO: |
| 3166 | mwdma_mask = 0; |
| 3167 | udma_mask = 0; |
| 3168 | break; |
| 3169 | |
Tejun Heo | 458337d | 2007-02-02 16:22:30 +0900 | [diff] [blame] | 3170 | default: |
| 3171 | BUG(); |
| 3172 | } |
| 3173 | |
| 3174 | xfer_mask &= ata_pack_xfermask(pio_mask, mwdma_mask, udma_mask); |
| 3175 | |
| 3176 | if (!(xfer_mask & ATA_MASK_PIO) || xfer_mask == orig_mask) |
| 3177 | return -ENOENT; |
| 3178 | |
| 3179 | if (!quiet) { |
| 3180 | if (xfer_mask & (ATA_MASK_MWDMA | ATA_MASK_UDMA)) |
| 3181 | snprintf(buf, sizeof(buf), "%s:%s", |
| 3182 | ata_mode_string(xfer_mask), |
| 3183 | ata_mode_string(xfer_mask & ATA_MASK_PIO)); |
| 3184 | else |
| 3185 | snprintf(buf, sizeof(buf), "%s", |
| 3186 | ata_mode_string(xfer_mask)); |
| 3187 | |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 3188 | ata_dev_warn(dev, "limiting speed to %s\n", buf); |
Tejun Heo | 458337d | 2007-02-02 16:22:30 +0900 | [diff] [blame] | 3189 | } |
Tejun Heo | cf176e1 | 2006-04-02 17:54:46 +0900 | [diff] [blame] | 3190 | |
| 3191 | ata_unpack_xfermask(xfer_mask, &dev->pio_mask, &dev->mwdma_mask, |
| 3192 | &dev->udma_mask); |
| 3193 | |
Tejun Heo | cf176e1 | 2006-04-02 17:54:46 +0900 | [diff] [blame] | 3194 | return 0; |
Tejun Heo | cf176e1 | 2006-04-02 17:54:46 +0900 | [diff] [blame] | 3195 | } |
| 3196 | |
Tejun Heo | 3373efd | 2006-05-15 20:57:53 +0900 | [diff] [blame] | 3197 | static int ata_dev_set_mode(struct ata_device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3198 | { |
Tejun Heo | d0cb43b | 2009-07-09 09:27:50 +0900 | [diff] [blame] | 3199 | struct ata_port *ap = dev->link->ap; |
Tejun Heo | 9af5c9c | 2007-08-06 18:36:22 +0900 | [diff] [blame] | 3200 | struct ata_eh_context *ehc = &dev->link->eh_context; |
Tejun Heo | d0cb43b | 2009-07-09 09:27:50 +0900 | [diff] [blame] | 3201 | const bool nosetxfer = dev->horkage & ATA_HORKAGE_NOSETXFER; |
Tejun Heo | 4055dee | 2008-02-07 10:34:08 +0900 | [diff] [blame] | 3202 | const char *dev_err_whine = ""; |
| 3203 | int ign_dev_err = 0; |
Tejun Heo | d0cb43b | 2009-07-09 09:27:50 +0900 | [diff] [blame] | 3204 | unsigned int err_mask = 0; |
Tejun Heo | 83206a2 | 2006-03-24 15:25:31 +0900 | [diff] [blame] | 3205 | int rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3206 | |
Tejun Heo | e838460 | 2006-04-02 18:51:53 +0900 | [diff] [blame] | 3207 | dev->flags &= ~ATA_DFLAG_PIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3208 | if (dev->xfer_shift == ATA_SHIFT_PIO) |
| 3209 | dev->flags |= ATA_DFLAG_PIO; |
| 3210 | |
Tejun Heo | d0cb43b | 2009-07-09 09:27:50 +0900 | [diff] [blame] | 3211 | if (nosetxfer && ap->flags & ATA_FLAG_SATA && ata_id_is_sata(dev->id)) |
| 3212 | dev_err_whine = " (SET_XFERMODE skipped)"; |
| 3213 | else { |
| 3214 | if (nosetxfer) |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 3215 | ata_dev_warn(dev, |
| 3216 | "NOSETXFER but PATA detected - can't " |
| 3217 | "skip SETXFER, might malfunction\n"); |
Tejun Heo | d0cb43b | 2009-07-09 09:27:50 +0900 | [diff] [blame] | 3218 | err_mask = ata_dev_set_xfermode(dev); |
| 3219 | } |
Jeff Garzik | 2dcb407 | 2007-10-19 06:42:56 -0400 | [diff] [blame] | 3220 | |
Tejun Heo | 4055dee | 2008-02-07 10:34:08 +0900 | [diff] [blame] | 3221 | if (err_mask & ~AC_ERR_DEV) |
| 3222 | goto fail; |
Jeff Garzik | 2dcb407 | 2007-10-19 06:42:56 -0400 | [diff] [blame] | 3223 | |
Tejun Heo | 4055dee | 2008-02-07 10:34:08 +0900 | [diff] [blame] | 3224 | /* revalidate */ |
Tejun Heo | baa1e78 | 2006-11-01 18:39:27 +0900 | [diff] [blame] | 3225 | ehc->i.flags |= ATA_EHI_POST_SETMODE; |
Tejun Heo | 422c9da | 2007-09-23 13:14:12 +0900 | [diff] [blame] | 3226 | rc = ata_dev_revalidate(dev, ATA_DEV_UNKNOWN, 0); |
Tejun Heo | baa1e78 | 2006-11-01 18:39:27 +0900 | [diff] [blame] | 3227 | ehc->i.flags &= ~ATA_EHI_POST_SETMODE; |
Tejun Heo | 5eb45c0 | 2006-04-02 18:51:52 +0900 | [diff] [blame] | 3228 | if (rc) |
Tejun Heo | 83206a2 | 2006-03-24 15:25:31 +0900 | [diff] [blame] | 3229 | return rc; |
Tejun Heo | 48a8a14 | 2006-03-05 17:55:58 +0900 | [diff] [blame] | 3230 | |
Alan Cox | b93fda1 | 2008-04-08 16:36:44 +0100 | [diff] [blame] | 3231 | if (dev->xfer_shift == ATA_SHIFT_PIO) { |
| 3232 | /* Old CFA may refuse this command, which is just fine */ |
| 3233 | if (ata_id_is_cfa(dev->id)) |
| 3234 | ign_dev_err = 1; |
| 3235 | /* Catch several broken garbage emulations plus some pre |
| 3236 | ATA devices */ |
| 3237 | if (ata_id_major_version(dev->id) == 0 && |
| 3238 | dev->pio_mode <= XFER_PIO_2) |
| 3239 | ign_dev_err = 1; |
| 3240 | /* Some very old devices and some bad newer ones fail |
| 3241 | any kind of SET_XFERMODE request but support PIO0-2 |
| 3242 | timings and no IORDY */ |
| 3243 | if (!ata_id_has_iordy(dev->id) && dev->pio_mode <= XFER_PIO_2) |
| 3244 | ign_dev_err = 1; |
| 3245 | } |
Tejun Heo | 4055dee | 2008-02-07 10:34:08 +0900 | [diff] [blame] | 3246 | /* Early MWDMA devices do DMA but don't allow DMA mode setting. |
| 3247 | Don't fail an MWDMA0 set IFF the device indicates it is in MWDMA0 */ |
| 3248 | if (dev->xfer_shift == ATA_SHIFT_MWDMA && |
| 3249 | dev->dma_mode == XFER_MW_DMA_0 && |
| 3250 | (dev->id[63] >> 8) & 1) |
| 3251 | ign_dev_err = 1; |
| 3252 | |
| 3253 | /* if the device is actually configured correctly, ignore dev err */ |
| 3254 | if (dev->xfer_mode == ata_xfer_mask2mode(ata_id_xfermask(dev->id))) |
| 3255 | ign_dev_err = 1; |
| 3256 | |
| 3257 | if (err_mask & AC_ERR_DEV) { |
| 3258 | if (!ign_dev_err) |
| 3259 | goto fail; |
| 3260 | else |
| 3261 | dev_err_whine = " (device error ignored)"; |
| 3262 | } |
| 3263 | |
Tejun Heo | 23e71c3 | 2006-03-06 04:31:57 +0900 | [diff] [blame] | 3264 | DPRINTK("xfer_shift=%u, xfer_mode=0x%x\n", |
| 3265 | dev->xfer_shift, (int)dev->xfer_mode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3266 | |
Damien Le Moal | 07b9b6d | 2018-05-09 09:28:10 +0900 | [diff] [blame] | 3267 | if (!(ehc->i.flags & ATA_EHI_QUIET) || |
| 3268 | ehc->i.flags & ATA_EHI_DID_HARDRESET) |
| 3269 | ata_dev_info(dev, "configured for %s%s\n", |
| 3270 | ata_mode_string(ata_xfer_mode2mask(dev->xfer_mode)), |
| 3271 | dev_err_whine); |
Tejun Heo | 4055dee | 2008-02-07 10:34:08 +0900 | [diff] [blame] | 3272 | |
Tejun Heo | 83206a2 | 2006-03-24 15:25:31 +0900 | [diff] [blame] | 3273 | return 0; |
Tejun Heo | 4055dee | 2008-02-07 10:34:08 +0900 | [diff] [blame] | 3274 | |
| 3275 | fail: |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 3276 | 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] | 3277 | return -EIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3278 | } |
| 3279 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3280 | /** |
Alan | 0435182 | 2007-03-06 02:37:52 -0800 | [diff] [blame] | 3281 | * ata_do_set_mode - Program timings and issue SET FEATURES - XFER |
Tejun Heo | 0260731 | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 3282 | * @link: link on which timings will be programmed |
Joe Perches | 1967b7f | 2008-02-03 17:08:11 +0200 | [diff] [blame] | 3283 | * @r_failed_dev: out parameter for failed device |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3284 | * |
Alan | 0435182 | 2007-03-06 02:37:52 -0800 | [diff] [blame] | 3285 | * Standard implementation of the function used to tune and set |
| 3286 | * ATA device disk transfer mode (PIO3, UDMA6, etc.). If |
| 3287 | * ata_dev_set_mode() fails, pointer to the failing device is |
Tejun Heo | e82cbdb | 2006-04-01 01:38:18 +0900 | [diff] [blame] | 3288 | * returned in @r_failed_dev. |
Jeff Garzik | 780a87f | 2005-05-30 15:41:05 -0400 | [diff] [blame] | 3289 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3290 | * LOCKING: |
Jeff Garzik | 0cba632 | 2005-05-30 19:49:12 -0400 | [diff] [blame] | 3291 | * PCI/etc. bus probe sem. |
Tejun Heo | e82cbdb | 2006-04-01 01:38:18 +0900 | [diff] [blame] | 3292 | * |
| 3293 | * RETURNS: |
| 3294 | * 0 on success, negative errno otherwise |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3295 | */ |
Alan | 0435182 | 2007-03-06 02:37:52 -0800 | [diff] [blame] | 3296 | |
Tejun Heo | 0260731 | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 3297 | 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] | 3298 | { |
Tejun Heo | 0260731 | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 3299 | struct ata_port *ap = link->ap; |
Tejun Heo | e8e0619 | 2006-04-01 01:38:18 +0900 | [diff] [blame] | 3300 | struct ata_device *dev; |
Tejun Heo | f58229f | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 3301 | int rc = 0, used_dma = 0, found = 0; |
Tejun Heo | 3adcebb | 2006-05-15 20:57:37 +0900 | [diff] [blame] | 3302 | |
Tejun Heo | a6d5a51 | 2006-03-06 04:31:57 +0900 | [diff] [blame] | 3303 | /* step 1: calculate xfer_mask */ |
Tejun Heo | 1eca436 | 2008-11-03 20:03:17 +0900 | [diff] [blame] | 3304 | ata_for_each_dev(dev, link, ENABLED) { |
Tejun Heo | 7dc951a | 2007-11-27 19:43:42 +0900 | [diff] [blame] | 3305 | unsigned long pio_mask, dma_mask; |
Alan Cox | b3a7060 | 2007-10-02 12:38:26 -0400 | [diff] [blame] | 3306 | unsigned int mode_mask; |
Tejun Heo | a6d5a51 | 2006-03-06 04:31:57 +0900 | [diff] [blame] | 3307 | |
Alan Cox | b3a7060 | 2007-10-02 12:38:26 -0400 | [diff] [blame] | 3308 | mode_mask = ATA_DMA_MASK_ATA; |
| 3309 | if (dev->class == ATA_DEV_ATAPI) |
| 3310 | mode_mask = ATA_DMA_MASK_ATAPI; |
| 3311 | else if (ata_id_is_cfa(dev->id)) |
| 3312 | mode_mask = ATA_DMA_MASK_CFA; |
| 3313 | |
Tejun Heo | 3373efd | 2006-05-15 20:57:53 +0900 | [diff] [blame] | 3314 | ata_dev_xfermask(dev); |
Tejun Heo | 3326732 | 2008-02-13 09:15:09 +0900 | [diff] [blame] | 3315 | ata_force_xfermask(dev); |
Tejun Heo | a6d5a51 | 2006-03-06 04:31:57 +0900 | [diff] [blame] | 3316 | |
Tejun Heo | acf356b | 2006-03-24 14:07:50 +0900 | [diff] [blame] | 3317 | pio_mask = ata_pack_xfermask(dev->pio_mask, 0, 0); |
Alan Cox | b3a7060 | 2007-10-02 12:38:26 -0400 | [diff] [blame] | 3318 | |
| 3319 | if (libata_dma_mask & mode_mask) |
Sergei Shtylyov | 80a9c43 | 2011-12-22 22:41:32 +0300 | [diff] [blame] | 3320 | dma_mask = ata_pack_xfermask(0, dev->mwdma_mask, |
| 3321 | dev->udma_mask); |
Alan Cox | b3a7060 | 2007-10-02 12:38:26 -0400 | [diff] [blame] | 3322 | else |
| 3323 | dma_mask = 0; |
| 3324 | |
Tejun Heo | acf356b | 2006-03-24 14:07:50 +0900 | [diff] [blame] | 3325 | dev->pio_mode = ata_xfer_mask2mode(pio_mask); |
| 3326 | dev->dma_mode = ata_xfer_mask2mode(dma_mask); |
Alan Cox | 5444a6f | 2006-03-27 18:58:20 +0100 | [diff] [blame] | 3327 | |
Tejun Heo | 4f65977 | 2006-04-01 01:38:18 +0900 | [diff] [blame] | 3328 | found = 1; |
Alan Cox | b15b3eb | 2008-08-01 09:18:34 +0100 | [diff] [blame] | 3329 | if (ata_dma_enabled(dev)) |
Alan Cox | 5444a6f | 2006-03-27 18:58:20 +0100 | [diff] [blame] | 3330 | used_dma = 1; |
Tejun Heo | a6d5a51 | 2006-03-06 04:31:57 +0900 | [diff] [blame] | 3331 | } |
Tejun Heo | 4f65977 | 2006-04-01 01:38:18 +0900 | [diff] [blame] | 3332 | if (!found) |
Tejun Heo | e82cbdb | 2006-04-01 01:38:18 +0900 | [diff] [blame] | 3333 | goto out; |
Tejun Heo | a6d5a51 | 2006-03-06 04:31:57 +0900 | [diff] [blame] | 3334 | |
| 3335 | /* step 2: always set host PIO timings */ |
Tejun Heo | 1eca436 | 2008-11-03 20:03:17 +0900 | [diff] [blame] | 3336 | ata_for_each_dev(dev, link, ENABLED) { |
Tejun Heo | 70cd071 | 2007-11-27 19:43:40 +0900 | [diff] [blame] | 3337 | if (dev->pio_mode == 0xff) { |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 3338 | ata_dev_warn(dev, "no PIO support\n"); |
Tejun Heo | e8e0619 | 2006-04-01 01:38:18 +0900 | [diff] [blame] | 3339 | rc = -EINVAL; |
Tejun Heo | e82cbdb | 2006-04-01 01:38:18 +0900 | [diff] [blame] | 3340 | goto out; |
Tejun Heo | e8e0619 | 2006-04-01 01:38:18 +0900 | [diff] [blame] | 3341 | } |
| 3342 | |
| 3343 | dev->xfer_mode = dev->pio_mode; |
| 3344 | dev->xfer_shift = ATA_SHIFT_PIO; |
| 3345 | if (ap->ops->set_piomode) |
| 3346 | ap->ops->set_piomode(ap, dev); |
| 3347 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3348 | |
Tejun Heo | a6d5a51 | 2006-03-06 04:31:57 +0900 | [diff] [blame] | 3349 | /* step 3: set host DMA timings */ |
Tejun Heo | 1eca436 | 2008-11-03 20:03:17 +0900 | [diff] [blame] | 3350 | ata_for_each_dev(dev, link, ENABLED) { |
| 3351 | if (!ata_dma_enabled(dev)) |
Tejun Heo | e8e0619 | 2006-04-01 01:38:18 +0900 | [diff] [blame] | 3352 | continue; |
| 3353 | |
| 3354 | dev->xfer_mode = dev->dma_mode; |
| 3355 | dev->xfer_shift = ata_xfer_mode2shift(dev->dma_mode); |
| 3356 | if (ap->ops->set_dmamode) |
| 3357 | ap->ops->set_dmamode(ap, dev); |
| 3358 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3359 | |
| 3360 | /* step 4: update devices' xfer mode */ |
Tejun Heo | 1eca436 | 2008-11-03 20:03:17 +0900 | [diff] [blame] | 3361 | ata_for_each_dev(dev, link, ENABLED) { |
Tejun Heo | 3373efd | 2006-05-15 20:57:53 +0900 | [diff] [blame] | 3362 | rc = ata_dev_set_mode(dev); |
Tejun Heo | 5bbc53f | 2006-04-01 01:38:17 +0900 | [diff] [blame] | 3363 | if (rc) |
Tejun Heo | e82cbdb | 2006-04-01 01:38:18 +0900 | [diff] [blame] | 3364 | goto out; |
Tejun Heo | 83206a2 | 2006-03-24 15:25:31 +0900 | [diff] [blame] | 3365 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3366 | |
Tejun Heo | e8e0619 | 2006-04-01 01:38:18 +0900 | [diff] [blame] | 3367 | /* Record simplex status. If we selected DMA then the other |
| 3368 | * host channels are not permitted to do so. |
Alan Cox | 5444a6f | 2006-03-27 18:58:20 +0100 | [diff] [blame] | 3369 | */ |
Jeff Garzik | cca3974 | 2006-08-24 03:19:22 -0400 | [diff] [blame] | 3370 | if (used_dma && (ap->host->flags & ATA_HOST_SIMPLEX)) |
Alan | 032af1c | 2007-03-01 17:36:46 +0000 | [diff] [blame] | 3371 | ap->host->simplex_claimed = ap; |
Alan Cox | 5444a6f | 2006-03-27 18:58:20 +0100 | [diff] [blame] | 3372 | |
Tejun Heo | e82cbdb | 2006-04-01 01:38:18 +0900 | [diff] [blame] | 3373 | out: |
| 3374 | if (rc) |
| 3375 | *r_failed_dev = dev; |
| 3376 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3377 | } |
Bartlomiej Zolnierkiewicz | a52fbcf | 2020-03-26 16:58:04 +0100 | [diff] [blame] | 3378 | EXPORT_SYMBOL_GPL(ata_do_set_mode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3379 | |
| 3380 | /** |
Tejun Heo | aa2731a | 2008-04-07 22:47:19 +0900 | [diff] [blame] | 3381 | * ata_wait_ready - wait for link to become ready |
| 3382 | * @link: link to be waited on |
| 3383 | * @deadline: deadline jiffies for the operation |
| 3384 | * @check_ready: callback to check link readiness |
| 3385 | * |
| 3386 | * Wait for @link to become ready. @check_ready should return |
| 3387 | * positive number if @link is ready, 0 if it isn't, -ENODEV if |
| 3388 | * link doesn't seem to be occupied, other errno for other error |
| 3389 | * conditions. |
| 3390 | * |
| 3391 | * Transient -ENODEV conditions are allowed for |
| 3392 | * ATA_TMOUT_FF_WAIT. |
| 3393 | * |
| 3394 | * LOCKING: |
| 3395 | * EH context. |
| 3396 | * |
| 3397 | * RETURNS: |
Masanari Iida | c9b5560 | 2016-04-13 23:36:27 +0900 | [diff] [blame] | 3398 | * 0 if @link is ready before @deadline; otherwise, -errno. |
Tejun Heo | aa2731a | 2008-04-07 22:47:19 +0900 | [diff] [blame] | 3399 | */ |
| 3400 | int ata_wait_ready(struct ata_link *link, unsigned long deadline, |
| 3401 | int (*check_ready)(struct ata_link *link)) |
| 3402 | { |
| 3403 | unsigned long start = jiffies; |
Tejun Heo | b48d58f | 2010-04-09 19:46:38 +0900 | [diff] [blame] | 3404 | unsigned long nodev_deadline; |
Tejun Heo | aa2731a | 2008-04-07 22:47:19 +0900 | [diff] [blame] | 3405 | int warned = 0; |
| 3406 | |
Tejun Heo | b48d58f | 2010-04-09 19:46:38 +0900 | [diff] [blame] | 3407 | /* choose which 0xff timeout to use, read comment in libata.h */ |
| 3408 | if (link->ap->host->flags & ATA_HOST_PARALLEL_SCAN) |
| 3409 | nodev_deadline = ata_deadline(start, ATA_TMOUT_FF_WAIT_LONG); |
| 3410 | else |
| 3411 | nodev_deadline = ata_deadline(start, ATA_TMOUT_FF_WAIT); |
| 3412 | |
Tejun Heo | b1c7291 | 2008-07-31 17:02:43 +0900 | [diff] [blame] | 3413 | /* Slave readiness can't be tested separately from master. On |
| 3414 | * M/S emulation configuration, this function should be called |
| 3415 | * only on the master and it will handle both master and slave. |
| 3416 | */ |
| 3417 | WARN_ON(link == link->ap->slave_link); |
| 3418 | |
Tejun Heo | aa2731a | 2008-04-07 22:47:19 +0900 | [diff] [blame] | 3419 | if (time_after(nodev_deadline, deadline)) |
| 3420 | nodev_deadline = deadline; |
| 3421 | |
| 3422 | while (1) { |
| 3423 | unsigned long now = jiffies; |
| 3424 | int ready, tmp; |
| 3425 | |
| 3426 | ready = tmp = check_ready(link); |
| 3427 | if (ready > 0) |
| 3428 | return 0; |
| 3429 | |
Tejun Heo | b48d58f | 2010-04-09 19:46:38 +0900 | [diff] [blame] | 3430 | /* |
| 3431 | * -ENODEV could be transient. Ignore -ENODEV if link |
Tejun Heo | aa2731a | 2008-04-07 22:47:19 +0900 | [diff] [blame] | 3432 | * is online. Also, some SATA devices take a long |
Tejun Heo | b48d58f | 2010-04-09 19:46:38 +0900 | [diff] [blame] | 3433 | * time to clear 0xff after reset. Wait for |
| 3434 | * ATA_TMOUT_FF_WAIT[_LONG] on -ENODEV if link isn't |
| 3435 | * offline. |
Tejun Heo | aa2731a | 2008-04-07 22:47:19 +0900 | [diff] [blame] | 3436 | * |
| 3437 | * Note that some PATA controllers (pata_ali) explode |
| 3438 | * if status register is read more than once when |
| 3439 | * there's no device attached. |
| 3440 | */ |
| 3441 | if (ready == -ENODEV) { |
| 3442 | if (ata_link_online(link)) |
| 3443 | ready = 0; |
| 3444 | else if ((link->ap->flags & ATA_FLAG_SATA) && |
| 3445 | !ata_link_offline(link) && |
| 3446 | time_before(now, nodev_deadline)) |
| 3447 | ready = 0; |
| 3448 | } |
| 3449 | |
| 3450 | if (ready) |
| 3451 | return ready; |
| 3452 | if (time_after(now, deadline)) |
| 3453 | return -EBUSY; |
| 3454 | |
| 3455 | if (!warned && time_after(now, start + 5 * HZ) && |
| 3456 | (deadline - now > 3 * HZ)) { |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 3457 | ata_link_warn(link, |
Tejun Heo | aa2731a | 2008-04-07 22:47:19 +0900 | [diff] [blame] | 3458 | "link is slow to respond, please be patient " |
| 3459 | "(ready=%d)\n", tmp); |
| 3460 | warned = 1; |
| 3461 | } |
| 3462 | |
Tejun Heo | 97750ce | 2010-09-06 17:56:29 +0200 | [diff] [blame] | 3463 | ata_msleep(link->ap, 50); |
Tejun Heo | aa2731a | 2008-04-07 22:47:19 +0900 | [diff] [blame] | 3464 | } |
| 3465 | } |
| 3466 | |
| 3467 | /** |
| 3468 | * ata_wait_after_reset - wait for link to become ready after reset |
| 3469 | * @link: link to be waited on |
| 3470 | * @deadline: deadline jiffies for the operation |
| 3471 | * @check_ready: callback to check link readiness |
| 3472 | * |
| 3473 | * Wait for @link to become ready after reset. |
| 3474 | * |
| 3475 | * LOCKING: |
| 3476 | * EH context. |
| 3477 | * |
| 3478 | * RETURNS: |
Masanari Iida | c9b5560 | 2016-04-13 23:36:27 +0900 | [diff] [blame] | 3479 | * 0 if @link is ready before @deadline; otherwise, -errno. |
Tejun Heo | aa2731a | 2008-04-07 22:47:19 +0900 | [diff] [blame] | 3480 | */ |
Harvey Harrison | 2b4221b | 2008-04-24 18:37:34 -0700 | [diff] [blame] | 3481 | int ata_wait_after_reset(struct ata_link *link, unsigned long deadline, |
Tejun Heo | aa2731a | 2008-04-07 22:47:19 +0900 | [diff] [blame] | 3482 | int (*check_ready)(struct ata_link *link)) |
| 3483 | { |
Tejun Heo | 97750ce | 2010-09-06 17:56:29 +0200 | [diff] [blame] | 3484 | ata_msleep(link->ap, ATA_WAIT_AFTER_RESET); |
Tejun Heo | aa2731a | 2008-04-07 22:47:19 +0900 | [diff] [blame] | 3485 | |
| 3486 | return ata_wait_ready(link, deadline, check_ready); |
| 3487 | } |
Bartlomiej Zolnierkiewicz | a52fbcf | 2020-03-26 16:58:04 +0100 | [diff] [blame] | 3488 | EXPORT_SYMBOL_GPL(ata_wait_after_reset); |
Tejun Heo | aa2731a | 2008-04-07 22:47:19 +0900 | [diff] [blame] | 3489 | |
| 3490 | /** |
Tejun Heo | 0aa1113 | 2008-04-07 22:47:18 +0900 | [diff] [blame] | 3491 | * ata_std_prereset - prepare for reset |
Tejun Heo | cc0680a | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 3492 | * @link: ATA link to be reset |
Tejun Heo | d4b2bab | 2007-02-02 16:50:52 +0900 | [diff] [blame] | 3493 | * @deadline: deadline jiffies for the operation |
Tejun Heo | f5914a4 | 2006-05-31 18:27:48 +0900 | [diff] [blame] | 3494 | * |
Tejun Heo | cc0680a | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 3495 | * @link is about to be reset. Initialize it. Failure from |
Tejun Heo | b8cffc6 | 2007-02-02 16:50:52 +0900 | [diff] [blame] | 3496 | * prereset makes libata abort whole reset sequence and give up |
| 3497 | * that port, so prereset should be best-effort. It does its |
| 3498 | * best to prepare for reset sequence but if things go wrong, it |
| 3499 | * should just whine, not fail. |
Tejun Heo | f5914a4 | 2006-05-31 18:27:48 +0900 | [diff] [blame] | 3500 | * |
| 3501 | * LOCKING: |
| 3502 | * Kernel thread context (may sleep) |
| 3503 | * |
| 3504 | * RETURNS: |
| 3505 | * 0 on success, -errno otherwise. |
| 3506 | */ |
Tejun Heo | 0aa1113 | 2008-04-07 22:47:18 +0900 | [diff] [blame] | 3507 | int ata_std_prereset(struct ata_link *link, unsigned long deadline) |
Tejun Heo | f5914a4 | 2006-05-31 18:27:48 +0900 | [diff] [blame] | 3508 | { |
Tejun Heo | cc0680a | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 3509 | struct ata_port *ap = link->ap; |
Tejun Heo | 936fd73 | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 3510 | struct ata_eh_context *ehc = &link->eh_context; |
Tejun Heo | e9c8391 | 2006-07-03 16:07:26 +0900 | [diff] [blame] | 3511 | const unsigned long *timing = sata_ehc_deb_timing(ehc); |
Tejun Heo | f5914a4 | 2006-05-31 18:27:48 +0900 | [diff] [blame] | 3512 | int rc; |
| 3513 | |
Tejun Heo | f5914a4 | 2006-05-31 18:27:48 +0900 | [diff] [blame] | 3514 | /* if we're about to do hardreset, nothing more to do */ |
| 3515 | if (ehc->i.action & ATA_EH_HARDRESET) |
| 3516 | return 0; |
| 3517 | |
Tejun Heo | 936fd73 | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 3518 | /* if SATA, resume link */ |
Tejun Heo | a16abc0 | 2007-05-21 18:33:47 +0200 | [diff] [blame] | 3519 | if (ap->flags & ATA_FLAG_SATA) { |
Tejun Heo | 936fd73 | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 3520 | rc = sata_link_resume(link, timing, deadline); |
Tejun Heo | b8cffc6 | 2007-02-02 16:50:52 +0900 | [diff] [blame] | 3521 | /* whine about phy resume failure but proceed */ |
| 3522 | if (rc && rc != -EOPNOTSUPP) |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 3523 | ata_link_warn(link, |
| 3524 | "failed to resume link for reset (errno=%d)\n", |
| 3525 | rc); |
Tejun Heo | f5914a4 | 2006-05-31 18:27:48 +0900 | [diff] [blame] | 3526 | } |
| 3527 | |
Tejun Heo | 45db2f6 | 2008-04-08 01:46:56 +0900 | [diff] [blame] | 3528 | /* no point in trying softreset on offline link */ |
Tejun Heo | b1c7291 | 2008-07-31 17:02:43 +0900 | [diff] [blame] | 3529 | if (ata_phys_link_offline(link)) |
Tejun Heo | 45db2f6 | 2008-04-08 01:46:56 +0900 | [diff] [blame] | 3530 | ehc->i.action &= ~ATA_EH_SOFTRESET; |
| 3531 | |
Tejun Heo | f5914a4 | 2006-05-31 18:27:48 +0900 | [diff] [blame] | 3532 | return 0; |
| 3533 | } |
Bartlomiej Zolnierkiewicz | a52fbcf | 2020-03-26 16:58:04 +0100 | [diff] [blame] | 3534 | EXPORT_SYMBOL_GPL(ata_std_prereset); |
Tejun Heo | f5914a4 | 2006-05-31 18:27:48 +0900 | [diff] [blame] | 3535 | |
Tejun Heo | c2bd580 | 2006-01-24 17:05:22 +0900 | [diff] [blame] | 3536 | /** |
Tejun Heo | 57c9efd | 2008-04-07 22:47:19 +0900 | [diff] [blame] | 3537 | * sata_std_hardreset - COMRESET w/o waiting or classification |
| 3538 | * @link: link to reset |
| 3539 | * @class: resulting class of attached device |
| 3540 | * @deadline: deadline jiffies for the operation |
| 3541 | * |
| 3542 | * Standard SATA COMRESET w/o waiting or classification. |
| 3543 | * |
| 3544 | * LOCKING: |
| 3545 | * Kernel thread context (may sleep) |
| 3546 | * |
| 3547 | * RETURNS: |
| 3548 | * 0 if link offline, -EAGAIN if link online, -errno on errors. |
| 3549 | */ |
| 3550 | int sata_std_hardreset(struct ata_link *link, unsigned int *class, |
| 3551 | unsigned long deadline) |
| 3552 | { |
| 3553 | const unsigned long *timing = sata_ehc_deb_timing(&link->eh_context); |
| 3554 | bool online; |
| 3555 | int rc; |
| 3556 | |
| 3557 | /* do hardreset */ |
| 3558 | rc = sata_link_hardreset(link, timing, deadline, &online, NULL); |
Tejun Heo | 57c9efd | 2008-04-07 22:47:19 +0900 | [diff] [blame] | 3559 | return online ? -EAGAIN : rc; |
| 3560 | } |
Bartlomiej Zolnierkiewicz | a52fbcf | 2020-03-26 16:58:04 +0100 | [diff] [blame] | 3561 | EXPORT_SYMBOL_GPL(sata_std_hardreset); |
Tejun Heo | 57c9efd | 2008-04-07 22:47:19 +0900 | [diff] [blame] | 3562 | |
| 3563 | /** |
Tejun Heo | 203c75b | 2008-04-07 22:47:18 +0900 | [diff] [blame] | 3564 | * ata_std_postreset - standard postreset callback |
Tejun Heo | cc0680a | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 3565 | * @link: the target ata_link |
Tejun Heo | c2bd580 | 2006-01-24 17:05:22 +0900 | [diff] [blame] | 3566 | * @classes: classes of attached devices |
| 3567 | * |
| 3568 | * This function is invoked after a successful reset. Note that |
| 3569 | * the device might have been reset more than once using |
| 3570 | * different reset methods before postreset is invoked. |
Tejun Heo | c2bd580 | 2006-01-24 17:05:22 +0900 | [diff] [blame] | 3571 | * |
Tejun Heo | c2bd580 | 2006-01-24 17:05:22 +0900 | [diff] [blame] | 3572 | * LOCKING: |
| 3573 | * Kernel thread context (may sleep) |
| 3574 | */ |
Tejun Heo | 203c75b | 2008-04-07 22:47:18 +0900 | [diff] [blame] | 3575 | void ata_std_postreset(struct ata_link *link, unsigned int *classes) |
Tejun Heo | c2bd580 | 2006-01-24 17:05:22 +0900 | [diff] [blame] | 3576 | { |
Tejun Heo | f046519 | 2008-05-19 01:15:08 +0900 | [diff] [blame] | 3577 | u32 serror; |
| 3578 | |
Tejun Heo | c2bd580 | 2006-01-24 17:05:22 +0900 | [diff] [blame] | 3579 | DPRINTK("ENTER\n"); |
| 3580 | |
Tejun Heo | f046519 | 2008-05-19 01:15:08 +0900 | [diff] [blame] | 3581 | /* reset complete, clear SError */ |
| 3582 | if (!sata_scr_read(link, SCR_ERROR, &serror)) |
| 3583 | sata_scr_write(link, SCR_ERROR, serror); |
| 3584 | |
Tejun Heo | c2bd580 | 2006-01-24 17:05:22 +0900 | [diff] [blame] | 3585 | /* print link status */ |
Tejun Heo | 936fd73 | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 3586 | sata_print_link_status(link); |
Tejun Heo | c2bd580 | 2006-01-24 17:05:22 +0900 | [diff] [blame] | 3587 | |
Tejun Heo | c2bd580 | 2006-01-24 17:05:22 +0900 | [diff] [blame] | 3588 | DPRINTK("EXIT\n"); |
| 3589 | } |
Bartlomiej Zolnierkiewicz | a52fbcf | 2020-03-26 16:58:04 +0100 | [diff] [blame] | 3590 | EXPORT_SYMBOL_GPL(ata_std_postreset); |
Tejun Heo | c2bd580 | 2006-01-24 17:05:22 +0900 | [diff] [blame] | 3591 | |
Tejun Heo | a62c0fc | 2006-01-24 17:05:22 +0900 | [diff] [blame] | 3592 | /** |
Tejun Heo | 623a312 | 2006-03-05 17:55:58 +0900 | [diff] [blame] | 3593 | * ata_dev_same_device - Determine whether new ID matches configured device |
Tejun Heo | 623a312 | 2006-03-05 17:55:58 +0900 | [diff] [blame] | 3594 | * @dev: device to compare against |
| 3595 | * @new_class: class of the new device |
| 3596 | * @new_id: IDENTIFY page of the new device |
| 3597 | * |
| 3598 | * Compare @new_class and @new_id against @dev and determine |
| 3599 | * whether @dev is the device indicated by @new_class and |
| 3600 | * @new_id. |
| 3601 | * |
| 3602 | * LOCKING: |
| 3603 | * None. |
| 3604 | * |
| 3605 | * RETURNS: |
| 3606 | * 1 if @dev matches @new_class and @new_id, 0 otherwise. |
| 3607 | */ |
Tejun Heo | 3373efd | 2006-05-15 20:57:53 +0900 | [diff] [blame] | 3608 | static int ata_dev_same_device(struct ata_device *dev, unsigned int new_class, |
| 3609 | const u16 *new_id) |
Tejun Heo | 623a312 | 2006-03-05 17:55:58 +0900 | [diff] [blame] | 3610 | { |
| 3611 | const u16 *old_id = dev->id; |
Tejun Heo | a0cf733 | 2007-01-02 20:18:49 +0900 | [diff] [blame] | 3612 | unsigned char model[2][ATA_ID_PROD_LEN + 1]; |
| 3613 | unsigned char serial[2][ATA_ID_SERNO_LEN + 1]; |
Tejun Heo | 623a312 | 2006-03-05 17:55:58 +0900 | [diff] [blame] | 3614 | |
| 3615 | if (dev->class != new_class) { |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 3616 | ata_dev_info(dev, "class mismatch %d != %d\n", |
| 3617 | dev->class, new_class); |
Tejun Heo | 623a312 | 2006-03-05 17:55:58 +0900 | [diff] [blame] | 3618 | return 0; |
| 3619 | } |
| 3620 | |
Tejun Heo | a0cf733 | 2007-01-02 20:18:49 +0900 | [diff] [blame] | 3621 | ata_id_c_string(old_id, model[0], ATA_ID_PROD, sizeof(model[0])); |
| 3622 | ata_id_c_string(new_id, model[1], ATA_ID_PROD, sizeof(model[1])); |
| 3623 | ata_id_c_string(old_id, serial[0], ATA_ID_SERNO, sizeof(serial[0])); |
| 3624 | 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] | 3625 | |
| 3626 | if (strcmp(model[0], model[1])) { |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 3627 | ata_dev_info(dev, "model number mismatch '%s' != '%s'\n", |
| 3628 | model[0], model[1]); |
Tejun Heo | 623a312 | 2006-03-05 17:55:58 +0900 | [diff] [blame] | 3629 | return 0; |
| 3630 | } |
| 3631 | |
| 3632 | if (strcmp(serial[0], serial[1])) { |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 3633 | ata_dev_info(dev, "serial number mismatch '%s' != '%s'\n", |
| 3634 | serial[0], serial[1]); |
Tejun Heo | 623a312 | 2006-03-05 17:55:58 +0900 | [diff] [blame] | 3635 | return 0; |
| 3636 | } |
| 3637 | |
Tejun Heo | 623a312 | 2006-03-05 17:55:58 +0900 | [diff] [blame] | 3638 | return 1; |
| 3639 | } |
| 3640 | |
| 3641 | /** |
Tejun Heo | fe30911 | 2007-05-15 03:28:15 +0900 | [diff] [blame] | 3642 | * ata_dev_reread_id - Re-read IDENTIFY data |
Henrik Kretzschmar | 3fae450 | 2007-06-19 10:10:50 +0200 | [diff] [blame] | 3643 | * @dev: target ATA device |
Tejun Heo | bff0464 | 2006-11-10 18:08:10 +0900 | [diff] [blame] | 3644 | * @readid_flags: read ID flags |
Tejun Heo | 623a312 | 2006-03-05 17:55:58 +0900 | [diff] [blame] | 3645 | * |
| 3646 | * Re-read IDENTIFY page and make sure @dev is still attached to |
| 3647 | * the port. |
| 3648 | * |
| 3649 | * LOCKING: |
| 3650 | * Kernel thread context (may sleep) |
| 3651 | * |
| 3652 | * RETURNS: |
| 3653 | * 0 on success, negative errno otherwise |
| 3654 | */ |
Tejun Heo | fe30911 | 2007-05-15 03:28:15 +0900 | [diff] [blame] | 3655 | 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] | 3656 | { |
Tejun Heo | 5eb45c0 | 2006-04-02 18:51:52 +0900 | [diff] [blame] | 3657 | unsigned int class = dev->class; |
Tejun Heo | 9af5c9c | 2007-08-06 18:36:22 +0900 | [diff] [blame] | 3658 | u16 *id = (void *)dev->link->ap->sector_buf; |
Tejun Heo | 623a312 | 2006-03-05 17:55:58 +0900 | [diff] [blame] | 3659 | int rc; |
| 3660 | |
Tejun Heo | fe635c7 | 2006-05-15 20:57:35 +0900 | [diff] [blame] | 3661 | /* read ID data */ |
Tejun Heo | bff0464 | 2006-11-10 18:08:10 +0900 | [diff] [blame] | 3662 | rc = ata_dev_read_id(dev, &class, readid_flags, id); |
Tejun Heo | 623a312 | 2006-03-05 17:55:58 +0900 | [diff] [blame] | 3663 | if (rc) |
Tejun Heo | fe30911 | 2007-05-15 03:28:15 +0900 | [diff] [blame] | 3664 | return rc; |
Tejun Heo | 623a312 | 2006-03-05 17:55:58 +0900 | [diff] [blame] | 3665 | |
| 3666 | /* is the device still there? */ |
Tejun Heo | fe30911 | 2007-05-15 03:28:15 +0900 | [diff] [blame] | 3667 | if (!ata_dev_same_device(dev, class, id)) |
| 3668 | return -ENODEV; |
Tejun Heo | 623a312 | 2006-03-05 17:55:58 +0900 | [diff] [blame] | 3669 | |
Tejun Heo | fe635c7 | 2006-05-15 20:57:35 +0900 | [diff] [blame] | 3670 | memcpy(dev->id, id, sizeof(id[0]) * ATA_ID_WORDS); |
Tejun Heo | fe30911 | 2007-05-15 03:28:15 +0900 | [diff] [blame] | 3671 | return 0; |
| 3672 | } |
| 3673 | |
| 3674 | /** |
| 3675 | * ata_dev_revalidate - Revalidate ATA device |
| 3676 | * @dev: device to revalidate |
Tejun Heo | 422c9da | 2007-09-23 13:14:12 +0900 | [diff] [blame] | 3677 | * @new_class: new class code |
Tejun Heo | fe30911 | 2007-05-15 03:28:15 +0900 | [diff] [blame] | 3678 | * @readid_flags: read ID flags |
| 3679 | * |
| 3680 | * Re-read IDENTIFY page, make sure @dev is still attached to the |
| 3681 | * port and reconfigure it according to the new IDENTIFY page. |
| 3682 | * |
| 3683 | * LOCKING: |
| 3684 | * Kernel thread context (may sleep) |
| 3685 | * |
| 3686 | * RETURNS: |
| 3687 | * 0 on success, negative errno otherwise |
| 3688 | */ |
Tejun Heo | 422c9da | 2007-09-23 13:14:12 +0900 | [diff] [blame] | 3689 | int ata_dev_revalidate(struct ata_device *dev, unsigned int new_class, |
| 3690 | unsigned int readid_flags) |
Tejun Heo | fe30911 | 2007-05-15 03:28:15 +0900 | [diff] [blame] | 3691 | { |
Tejun Heo | 6ddcd3b | 2007-05-15 03:28:15 +0900 | [diff] [blame] | 3692 | u64 n_sectors = dev->n_sectors; |
Tejun Heo | 5920dad | 2009-07-15 17:11:41 +0900 | [diff] [blame] | 3693 | u64 n_native_sectors = dev->n_native_sectors; |
Tejun Heo | fe30911 | 2007-05-15 03:28:15 +0900 | [diff] [blame] | 3694 | int rc; |
| 3695 | |
| 3696 | if (!ata_dev_enabled(dev)) |
| 3697 | return -ENODEV; |
| 3698 | |
Tejun Heo | 422c9da | 2007-09-23 13:14:12 +0900 | [diff] [blame] | 3699 | /* fail early if !ATA && !ATAPI to avoid issuing [P]IDENTIFY to PMP */ |
| 3700 | if (ata_class_enabled(new_class) && |
Borislav Petkov | f0d0613 | 2009-04-26 16:33:34 +0200 | [diff] [blame] | 3701 | new_class != ATA_DEV_ATA && |
| 3702 | new_class != ATA_DEV_ATAPI && |
Hannes Reinecke | 9162c65 | 2014-11-05 13:08:21 +0100 | [diff] [blame] | 3703 | new_class != ATA_DEV_ZAC && |
Borislav Petkov | f0d0613 | 2009-04-26 16:33:34 +0200 | [diff] [blame] | 3704 | new_class != ATA_DEV_SEMB) { |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 3705 | ata_dev_info(dev, "class mismatch %u != %u\n", |
| 3706 | dev->class, new_class); |
Tejun Heo | 422c9da | 2007-09-23 13:14:12 +0900 | [diff] [blame] | 3707 | rc = -ENODEV; |
| 3708 | goto fail; |
| 3709 | } |
| 3710 | |
Tejun Heo | fe30911 | 2007-05-15 03:28:15 +0900 | [diff] [blame] | 3711 | /* re-read ID */ |
| 3712 | rc = ata_dev_reread_id(dev, readid_flags); |
| 3713 | if (rc) |
| 3714 | goto fail; |
Tejun Heo | 623a312 | 2006-03-05 17:55:58 +0900 | [diff] [blame] | 3715 | |
| 3716 | /* configure device according to the new ID */ |
Tejun Heo | efdaedc | 2006-11-01 18:38:52 +0900 | [diff] [blame] | 3717 | rc = ata_dev_configure(dev); |
Tejun Heo | 6ddcd3b | 2007-05-15 03:28:15 +0900 | [diff] [blame] | 3718 | if (rc) |
| 3719 | goto fail; |
| 3720 | |
| 3721 | /* verify n_sectors hasn't changed */ |
Tejun Heo | 445d211 | 2010-04-05 10:33:13 +0900 | [diff] [blame] | 3722 | if (dev->class != ATA_DEV_ATA || !n_sectors || |
| 3723 | dev->n_sectors == n_sectors) |
| 3724 | return 0; |
| 3725 | |
| 3726 | /* n_sectors has changed */ |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 3727 | ata_dev_warn(dev, "n_sectors mismatch %llu != %llu\n", |
| 3728 | (unsigned long long)n_sectors, |
| 3729 | (unsigned long long)dev->n_sectors); |
Tejun Heo | 445d211 | 2010-04-05 10:33:13 +0900 | [diff] [blame] | 3730 | |
| 3731 | /* |
| 3732 | * Something could have caused HPA to be unlocked |
| 3733 | * involuntarily. If n_native_sectors hasn't changed and the |
| 3734 | * new size matches it, keep the device. |
| 3735 | */ |
| 3736 | if (dev->n_native_sectors == n_native_sectors && |
| 3737 | dev->n_sectors > n_sectors && dev->n_sectors == n_native_sectors) { |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 3738 | ata_dev_warn(dev, |
| 3739 | "new n_sectors matches native, probably " |
| 3740 | "late HPA unlock, n_sectors updated\n"); |
Tejun Heo | 68939ce | 2010-05-15 20:09:33 +0200 | [diff] [blame] | 3741 | /* use the larger n_sectors */ |
Tejun Heo | 445d211 | 2010-04-05 10:33:13 +0900 | [diff] [blame] | 3742 | return 0; |
Tejun Heo | 6ddcd3b | 2007-05-15 03:28:15 +0900 | [diff] [blame] | 3743 | } |
| 3744 | |
Tejun Heo | 445d211 | 2010-04-05 10:33:13 +0900 | [diff] [blame] | 3745 | /* |
| 3746 | * Some BIOSes boot w/o HPA but resume w/ HPA locked. Try |
| 3747 | * unlocking HPA in those cases. |
| 3748 | * |
| 3749 | * https://bugzilla.kernel.org/show_bug.cgi?id=15396 |
| 3750 | */ |
| 3751 | if (dev->n_native_sectors == n_native_sectors && |
| 3752 | dev->n_sectors < n_sectors && n_sectors == n_native_sectors && |
| 3753 | !(dev->horkage & ATA_HORKAGE_BROKEN_HPA)) { |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 3754 | ata_dev_warn(dev, |
| 3755 | "old n_sectors matches native, probably " |
| 3756 | "late HPA lock, will try to unlock HPA\n"); |
Tejun Heo | 445d211 | 2010-04-05 10:33:13 +0900 | [diff] [blame] | 3757 | /* try unlocking HPA */ |
| 3758 | dev->flags |= ATA_DFLAG_UNLOCK_HPA; |
| 3759 | rc = -EIO; |
| 3760 | } else |
| 3761 | rc = -ENODEV; |
Tejun Heo | 623a312 | 2006-03-05 17:55:58 +0900 | [diff] [blame] | 3762 | |
Tejun Heo | 445d211 | 2010-04-05 10:33:13 +0900 | [diff] [blame] | 3763 | /* restore original n_[native_]sectors and fail */ |
| 3764 | dev->n_native_sectors = n_native_sectors; |
| 3765 | dev->n_sectors = n_sectors; |
Tejun Heo | 623a312 | 2006-03-05 17:55:58 +0900 | [diff] [blame] | 3766 | fail: |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 3767 | ata_dev_err(dev, "revalidation failed (errno=%d)\n", rc); |
Tejun Heo | 623a312 | 2006-03-05 17:55:58 +0900 | [diff] [blame] | 3768 | return rc; |
| 3769 | } |
| 3770 | |
Alan Cox | 6919a0a | 2006-10-27 19:08:46 -0700 | [diff] [blame] | 3771 | struct ata_blacklist_entry { |
| 3772 | const char *model_num; |
| 3773 | const char *model_rev; |
| 3774 | unsigned long horkage; |
| 3775 | }; |
| 3776 | |
| 3777 | static const struct ata_blacklist_entry ata_device_blacklist [] = { |
| 3778 | /* Devices with DMA related problems under Linux */ |
| 3779 | { "WDC AC11000H", NULL, ATA_HORKAGE_NODMA }, |
| 3780 | { "WDC AC22100H", NULL, ATA_HORKAGE_NODMA }, |
| 3781 | { "WDC AC32500H", NULL, ATA_HORKAGE_NODMA }, |
| 3782 | { "WDC AC33100H", NULL, ATA_HORKAGE_NODMA }, |
| 3783 | { "WDC AC31600H", NULL, ATA_HORKAGE_NODMA }, |
| 3784 | { "WDC AC32100H", "24.09P07", ATA_HORKAGE_NODMA }, |
| 3785 | { "WDC AC23200L", "21.10N21", ATA_HORKAGE_NODMA }, |
| 3786 | { "Compaq CRD-8241B", NULL, ATA_HORKAGE_NODMA }, |
| 3787 | { "CRD-8400B", NULL, ATA_HORKAGE_NODMA }, |
Mark Lord | 7da4c93 | 2010-07-01 18:18:12 -0400 | [diff] [blame] | 3788 | { "CRD-848[02]B", NULL, ATA_HORKAGE_NODMA }, |
Alan Cox | 6919a0a | 2006-10-27 19:08:46 -0700 | [diff] [blame] | 3789 | { "CRD-84", NULL, ATA_HORKAGE_NODMA }, |
| 3790 | { "SanDisk SDP3B", NULL, ATA_HORKAGE_NODMA }, |
| 3791 | { "SanDisk SDP3B-64", NULL, ATA_HORKAGE_NODMA }, |
| 3792 | { "SANYO CD-ROM CRD", NULL, ATA_HORKAGE_NODMA }, |
| 3793 | { "HITACHI CDR-8", NULL, ATA_HORKAGE_NODMA }, |
Mark Lord | 7da4c93 | 2010-07-01 18:18:12 -0400 | [diff] [blame] | 3794 | { "HITACHI CDR-8[34]35",NULL, ATA_HORKAGE_NODMA }, |
Alan Cox | 6919a0a | 2006-10-27 19:08:46 -0700 | [diff] [blame] | 3795 | { "Toshiba CD-ROM XM-6202B", NULL, ATA_HORKAGE_NODMA }, |
| 3796 | { "TOSHIBA CD-ROM XM-1702BC", NULL, ATA_HORKAGE_NODMA }, |
| 3797 | { "CD-532E-A", NULL, ATA_HORKAGE_NODMA }, |
| 3798 | { "E-IDE CD-ROM CR-840",NULL, ATA_HORKAGE_NODMA }, |
| 3799 | { "CD-ROM Drive/F5A", NULL, ATA_HORKAGE_NODMA }, |
| 3800 | { "WPI CDD-820", NULL, ATA_HORKAGE_NODMA }, |
| 3801 | { "SAMSUNG CD-ROM SC-148C", NULL, ATA_HORKAGE_NODMA }, |
| 3802 | { "SAMSUNG CD-ROM SC", NULL, ATA_HORKAGE_NODMA }, |
Alan Cox | 6919a0a | 2006-10-27 19:08:46 -0700 | [diff] [blame] | 3803 | { "ATAPI CD-ROM DRIVE 40X MAXIMUM",NULL,ATA_HORKAGE_NODMA }, |
| 3804 | { "_NEC DV5800A", NULL, ATA_HORKAGE_NODMA }, |
Jeff Garzik | 2dcb407 | 2007-10-19 06:42:56 -0400 | [diff] [blame] | 3805 | { "SAMSUNG CD-ROM SN-124", "N001", ATA_HORKAGE_NODMA }, |
Dave Jones | 39f1988 | 2007-05-21 14:31:03 -0400 | [diff] [blame] | 3806 | { "Seagate STT20000A", NULL, ATA_HORKAGE_NODMA }, |
Prarit Bhargava | d17d794 | 2012-08-23 15:11:52 -0400 | [diff] [blame] | 3807 | { " 2GB ATA Flash Disk", "ADMA428M", ATA_HORKAGE_NODMA }, |
Shikha Jain | b00622f | 2016-01-11 14:55:45 -0800 | [diff] [blame] | 3808 | { "VRFDFC22048UCHC-TE*", NULL, ATA_HORKAGE_NODMA }, |
Tejun Heo | 3af9a77 | 2007-09-23 13:19:54 +0900 | [diff] [blame] | 3809 | /* Odd clown on sil3726/4726 PMPs */ |
Tejun Heo | 50af2fa | 2008-05-19 01:15:14 +0900 | [diff] [blame] | 3810 | { "Config Disk", NULL, ATA_HORKAGE_DISABLE }, |
Alan Cox | 6919a0a | 2006-10-27 19:08:46 -0700 | [diff] [blame] | 3811 | |
Albert Lee | 18d6e9d | 2007-04-02 11:34:15 +0800 | [diff] [blame] | 3812 | /* Weird ATAPI devices */ |
Tejun Heo | 40a1d53 | 2007-06-27 02:49:38 +0900 | [diff] [blame] | 3813 | { "TORiSAN DVD-ROM DRD-N216", NULL, ATA_HORKAGE_MAX_SEC_128 }, |
Tejun Heo | 6a87e42 | 2008-11-03 19:01:09 +0900 | [diff] [blame] | 3814 | { "QUANTUM DAT DAT72-000", NULL, ATA_HORKAGE_ATAPI_MOD16_DMA }, |
Shan Hai | a32450e | 2013-03-18 10:30:44 +0800 | [diff] [blame] | 3815 | { "Slimtype DVD A DS8A8SH", NULL, ATA_HORKAGE_MAX_SEC_LBA48 }, |
Shan Hai | 0523f03 | 2013-10-28 16:08:01 +0800 | [diff] [blame] | 3816 | { "Slimtype DVD A DS8A9SH", NULL, ATA_HORKAGE_MAX_SEC_LBA48 }, |
Albert Lee | 18d6e9d | 2007-04-02 11:34:15 +0800 | [diff] [blame] | 3817 | |
David Milburn | af34d63 | 2015-07-13 11:48:23 -0500 | [diff] [blame] | 3818 | /* |
| 3819 | * Causes silent data corruption with higher max sects. |
| 3820 | * http://lkml.kernel.org/g/x49wpy40ysk.fsf@segfault.boston.devel.redhat.com |
| 3821 | */ |
| 3822 | { "ST380013AS", "3.20", ATA_HORKAGE_MAX_SEC_1024 }, |
| 3823 | |
Tejun Heo | 1488a1e | 2016-07-18 18:40:00 -0400 | [diff] [blame] | 3824 | /* |
Tejun Heo | e0edc8c | 2017-01-06 11:48:50 -0500 | [diff] [blame] | 3825 | * These devices time out with higher max sects. |
Tejun Heo | 1488a1e | 2016-07-18 18:40:00 -0400 | [diff] [blame] | 3826 | * https://bugzilla.kernel.org/show_bug.cgi?id=121671 |
| 3827 | */ |
Tejun Heo | e0edc8c | 2017-01-06 11:48:50 -0500 | [diff] [blame] | 3828 | { "LITEON CX1-JB*-HP", NULL, ATA_HORKAGE_MAX_SEC_1024 }, |
Xinyu Lin | db5ff90 | 2017-12-17 20:13:39 +0800 | [diff] [blame] | 3829 | { "LITEON EP1-*", NULL, ATA_HORKAGE_MAX_SEC_1024 }, |
Tejun Heo | 1488a1e | 2016-07-18 18:40:00 -0400 | [diff] [blame] | 3830 | |
Alan Cox | 6919a0a | 2006-10-27 19:08:46 -0700 | [diff] [blame] | 3831 | /* Devices we expect to fail diagnostics */ |
| 3832 | |
| 3833 | /* Devices where NCQ should be avoided */ |
| 3834 | /* NCQ is slow */ |
Jeff Garzik | 2dcb407 | 2007-10-19 06:42:56 -0400 | [diff] [blame] | 3835 | { "WDC WD740ADFD-00", NULL, ATA_HORKAGE_NONCQ }, |
Tejun Heo | 459ad68 | 2007-12-07 12:46:23 +0900 | [diff] [blame] | 3836 | { "WDC WD740ADFD-00NLR1", NULL, ATA_HORKAGE_NONCQ, }, |
Tejun Heo | 09125ea | 2007-02-28 15:21:23 +0900 | [diff] [blame] | 3837 | /* http://thread.gmane.org/gmane.linux.ide/14907 */ |
| 3838 | { "FUJITSU MHT2060BH", NULL, ATA_HORKAGE_NONCQ }, |
Paul Rolland | 7acfaf3 | 2007-03-26 21:43:44 -0800 | [diff] [blame] | 3839 | /* NCQ is broken */ |
Jeff Garzik | 539cc7c | 2007-09-20 16:31:47 -0400 | [diff] [blame] | 3840 | { "Maxtor *", "BANC*", ATA_HORKAGE_NONCQ }, |
Alan Cox | 0e3dbc0 | 2007-09-20 15:22:47 +0100 | [diff] [blame] | 3841 | { "Maxtor 7V300F0", "VA111630", ATA_HORKAGE_NONCQ }, |
Paolo Ornati | da6f0ec | 2007-10-04 11:06:56 +0900 | [diff] [blame] | 3842 | { "ST380817AS", "3.42", ATA_HORKAGE_NONCQ }, |
Tejun Heo | e41bd3e | 2007-12-09 19:45:39 +0900 | [diff] [blame] | 3843 | { "ST3160023AS", "3.42", ATA_HORKAGE_NONCQ }, |
Lubomir Bulej | 5ccfca9 | 2008-12-22 11:35:22 +0100 | [diff] [blame] | 3844 | { "OCZ CORE_SSD", "02.10104", ATA_HORKAGE_NONCQ }, |
Jeff Garzik | 539cc7c | 2007-09-20 16:31:47 -0400 | [diff] [blame] | 3845 | |
Tejun Heo | ac70a96 | 2008-11-27 13:36:48 +0900 | [diff] [blame] | 3846 | /* Seagate NCQ + FLUSH CACHE firmware bug */ |
Mark Lord | 4d1f908 | 2010-07-05 18:53:37 -0400 | [diff] [blame] | 3847 | { "ST31500341AS", "SD1[5-9]", ATA_HORKAGE_NONCQ | |
Tejun Heo | ac70a96 | 2008-11-27 13:36:48 +0900 | [diff] [blame] | 3848 | ATA_HORKAGE_FIRMWARE_WARN }, |
Tejun Heo | d10d491 | 2008-12-11 13:42:42 +0900 | [diff] [blame] | 3849 | |
Mark Lord | 4d1f908 | 2010-07-05 18:53:37 -0400 | [diff] [blame] | 3850 | { "ST31000333AS", "SD1[5-9]", ATA_HORKAGE_NONCQ | |
Tejun Heo | d10d491 | 2008-12-11 13:42:42 +0900 | [diff] [blame] | 3851 | ATA_HORKAGE_FIRMWARE_WARN }, |
| 3852 | |
Mark Lord | 4d1f908 | 2010-07-05 18:53:37 -0400 | [diff] [blame] | 3853 | { "ST3640[36]23AS", "SD1[5-9]", ATA_HORKAGE_NONCQ | |
Tejun Heo | d10d491 | 2008-12-11 13:42:42 +0900 | [diff] [blame] | 3854 | ATA_HORKAGE_FIRMWARE_WARN }, |
| 3855 | |
Mark Lord | 4d1f908 | 2010-07-05 18:53:37 -0400 | [diff] [blame] | 3856 | { "ST3320[68]13AS", "SD1[5-9]", ATA_HORKAGE_NONCQ | |
Tejun Heo | ac70a96 | 2008-11-27 13:36:48 +0900 | [diff] [blame] | 3857 | ATA_HORKAGE_FIRMWARE_WARN }, |
| 3858 | |
Hans de Goede | 31f6264 | 2019-06-11 16:32:59 +0200 | [diff] [blame] | 3859 | /* drives which fail FPDMA_AA activation (some may freeze afterwards) |
| 3860 | the ST disks also have LPM issues */ |
Hans de Goede | 8756a25 | 2019-07-01 18:58:13 +0200 | [diff] [blame] | 3861 | { "ST1000LM024 HN-M101MBB", NULL, ATA_HORKAGE_BROKEN_FPDMA_AA | |
Hans de Goede | 31f6264 | 2019-06-11 16:32:59 +0200 | [diff] [blame] | 3862 | ATA_HORKAGE_NOLPM, }, |
Aleksei Mamlin | 08c85d2 | 2015-07-01 13:48:30 +0300 | [diff] [blame] | 3863 | { "VB0250EAVER", "HPG7", ATA_HORKAGE_BROKEN_FPDMA_AA }, |
Michele Baldessari | 8780994 | 2013-11-25 19:00:14 +0000 | [diff] [blame] | 3864 | |
Robert Hancock | 36e337d | 2007-04-02 22:05:29 -0600 | [diff] [blame] | 3865 | /* Blacklist entries taken from Silicon Image 3124/3132 |
| 3866 | Windows driver .inf file - also several Linux problem reports */ |
| 3867 | { "HTS541060G9SA00", "MB3OC60D", ATA_HORKAGE_NONCQ, }, |
| 3868 | { "HTS541080G9SA00", "MB4OC60D", ATA_HORKAGE_NONCQ, }, |
| 3869 | { "HTS541010G9SA00", "MBZOC60D", ATA_HORKAGE_NONCQ, }, |
Alan Cox | 6919a0a | 2006-10-27 19:08:46 -0700 | [diff] [blame] | 3870 | |
Tejun Heo | 68b0ddb | 2010-04-05 10:51:26 +0900 | [diff] [blame] | 3871 | /* https://bugzilla.kernel.org/show_bug.cgi?id=15573 */ |
| 3872 | { "C300-CTFDDAC128MAG", "0001", ATA_HORKAGE_NONCQ, }, |
| 3873 | |
Tejun Heo | 3b54556 | 2020-09-02 12:32:45 -0400 | [diff] [blame] | 3874 | /* Sandisk SD7/8/9s lock up hard on large trims */ |
| 3875 | { "SanDisk SD[789]*", NULL, ATA_HORKAGE_MAX_TRIM_128M, }, |
Tejun Heo | 322579d | 2018-05-08 14:21:56 -0700 | [diff] [blame] | 3876 | |
Tejun Heo | 16c55b0 | 2007-08-29 11:58:33 +0900 | [diff] [blame] | 3877 | /* devices which puke on READ_NATIVE_MAX */ |
| 3878 | { "HDS724040KLSA80", "KFAOA20N", ATA_HORKAGE_BROKEN_HPA, }, |
| 3879 | { "WDC WD3200JD-00KLB0", "WD-WCAMR1130137", ATA_HORKAGE_BROKEN_HPA }, |
| 3880 | { "WDC WD2500JD-00HBB0", "WD-WMAL71490727", ATA_HORKAGE_BROKEN_HPA }, |
| 3881 | { "MAXTOR 6L080L4", "A93.0500", ATA_HORKAGE_BROKEN_HPA }, |
Alan Cox | 6919a0a | 2006-10-27 19:08:46 -0700 | [diff] [blame] | 3882 | |
Tejun Heo | 7831387b | 2009-08-07 01:59:15 +0900 | [diff] [blame] | 3883 | /* this one allows HPA unlocking but fails IOs on the area */ |
| 3884 | { "OCZ-VERTEX", "1.30", ATA_HORKAGE_BROKEN_HPA }, |
| 3885 | |
Alan Cox | 93328e11 | 2007-09-29 04:06:48 -0400 | [diff] [blame] | 3886 | /* Devices which report 1 sector over size HPA */ |
| 3887 | { "ST340823A", NULL, ATA_HORKAGE_HPA_SIZE, }, |
| 3888 | { "ST320413A", NULL, ATA_HORKAGE_HPA_SIZE, }, |
Mikko Rapeli | b152fcd | 2008-02-19 01:41:25 +0100 | [diff] [blame] | 3889 | { "ST310211A", NULL, ATA_HORKAGE_HPA_SIZE, }, |
Alan Cox | 93328e11 | 2007-09-29 04:06:48 -0400 | [diff] [blame] | 3890 | |
Alan Cox | 6bbfd53 | 2007-11-05 22:58:58 +0000 | [diff] [blame] | 3891 | /* Devices which get the IVB wrong */ |
| 3892 | { "QUANTUM FIREBALLlct10 05", "A03.0900", ATA_HORKAGE_IVB, }, |
Alan Cox | a79067e | 2008-04-29 14:08:36 +0100 | [diff] [blame] | 3893 | /* Maybe we should just blacklist TSSTcorp... */ |
Mark Lord | 7da4c93 | 2010-07-01 18:18:12 -0400 | [diff] [blame] | 3894 | { "TSSTcorp CDDVDW SH-S202[HJN]", "SB0[01]", ATA_HORKAGE_IVB, }, |
Alan Cox | 6bbfd53 | 2007-11-05 22:58:58 +0000 | [diff] [blame] | 3895 | |
Jens Axboe | 9ce8e30 | 2008-08-27 15:23:18 +0200 | [diff] [blame] | 3896 | /* Devices that do not need bridging limits applied */ |
| 3897 | { "MTRON MSP-SATA*", NULL, ATA_HORKAGE_BRIDGE_OK, }, |
Jeff Garzik | 04d0f1b8 | 2012-08-17 13:36:59 -0400 | [diff] [blame] | 3898 | { "BUFFALO HD-QSU2/R5", NULL, ATA_HORKAGE_BRIDGE_OK, }, |
Jens Axboe | 9ce8e30 | 2008-08-27 15:23:18 +0200 | [diff] [blame] | 3899 | |
Tejun Heo | 9062712 | 2009-01-29 20:31:36 +0900 | [diff] [blame] | 3900 | /* Devices which aren't very happy with higher link speeds */ |
| 3901 | { "WD My Book", NULL, ATA_HORKAGE_1_5_GBPS, }, |
Daniel J Blueman | c531077 | 2012-07-23 12:22:37 +0800 | [diff] [blame] | 3902 | { "Seagate FreeAgent GoFlex", NULL, ATA_HORKAGE_1_5_GBPS, }, |
Tejun Heo | 9062712 | 2009-01-29 20:31:36 +0900 | [diff] [blame] | 3903 | |
Tejun Heo | d0cb43b | 2009-07-09 09:27:50 +0900 | [diff] [blame] | 3904 | /* |
| 3905 | * Devices which choke on SETXFER. Applies only if both the |
| 3906 | * device and controller are SATA. |
| 3907 | */ |
Tejun Heo | cd69187 | 2011-06-22 12:13:10 +0200 | [diff] [blame] | 3908 | { "PIONEER DVD-RW DVRTD08", NULL, ATA_HORKAGE_NOSETXFER }, |
Vladimir LAVALLADE | 3a25179 | 2012-01-08 13:50:13 +0100 | [diff] [blame] | 3909 | { "PIONEER DVD-RW DVRTD08A", NULL, ATA_HORKAGE_NOSETXFER }, |
| 3910 | { "PIONEER DVD-RW DVR-215", NULL, ATA_HORKAGE_NOSETXFER }, |
Tejun Heo | cd69187 | 2011-06-22 12:13:10 +0200 | [diff] [blame] | 3911 | { "PIONEER DVD-RW DVR-212D", NULL, ATA_HORKAGE_NOSETXFER }, |
| 3912 | { "PIONEER DVD-RW DVR-216D", NULL, ATA_HORKAGE_NOSETXFER }, |
Tejun Heo | d0cb43b | 2009-07-09 09:27:50 +0900 | [diff] [blame] | 3913 | |
Kai-Heng Feng | b17e5729 | 2018-02-18 22:17:09 +0800 | [diff] [blame] | 3914 | /* Crucial BX100 SSD 500GB has broken LPM support */ |
Hans de Goede | 3bf7b5d | 2018-03-19 16:33:59 +0100 | [diff] [blame] | 3915 | { "CT500BX100SSD1", NULL, ATA_HORKAGE_NOLPM }, |
Kai-Heng Feng | b17e5729 | 2018-02-18 22:17:09 +0800 | [diff] [blame] | 3916 | |
Hans de Goede | d418ff5 | 2018-03-19 16:34:00 +0100 | [diff] [blame] | 3917 | /* 512GB MX100 with MU01 firmware has both queued TRIM and LPM issues */ |
| 3918 | { "Crucial_CT512MX100*", "MU01", ATA_HORKAGE_NO_NCQ_TRIM | |
Hans de Goede | 9c7be59f | 2018-02-16 10:48:20 +0100 | [diff] [blame] | 3919 | ATA_HORKAGE_ZERO_AFTER_TRIM | |
| 3920 | ATA_HORKAGE_NOLPM, }, |
Hans de Goede | d418ff5 | 2018-03-19 16:34:00 +0100 | [diff] [blame] | 3921 | /* 512GB MX100 with newer firmware has only LPM issues */ |
| 3922 | { "Crucial_CT512MX100*", NULL, ATA_HORKAGE_ZERO_AFTER_TRIM | |
| 3923 | ATA_HORKAGE_NOLPM, }, |
Hans de Goede | 9c7be59f | 2018-02-16 10:48:20 +0100 | [diff] [blame] | 3924 | |
Hans de Goede | 62ac3f7 | 2018-03-19 16:33:58 +0100 | [diff] [blame] | 3925 | /* 480GB+ M500 SSDs have both queued TRIM and LPM issues */ |
| 3926 | { "Crucial_CT480M500*", NULL, ATA_HORKAGE_NO_NCQ_TRIM | |
| 3927 | ATA_HORKAGE_ZERO_AFTER_TRIM | |
| 3928 | ATA_HORKAGE_NOLPM, }, |
| 3929 | { "Crucial_CT960M500*", NULL, ATA_HORKAGE_NO_NCQ_TRIM | |
| 3930 | ATA_HORKAGE_ZERO_AFTER_TRIM | |
| 3931 | ATA_HORKAGE_NOLPM, }, |
| 3932 | |
François Cami | 76936e9 | 2018-05-13 20:11:15 +0200 | [diff] [blame] | 3933 | /* These specific Samsung models/firmware-revs do not handle LPM well */ |
Hans de Goede | b5b4d3a | 2018-04-24 11:19:07 +0200 | [diff] [blame] | 3934 | { "SAMSUNG MZMPC128HBFU-000MV", "CXM14M1Q", ATA_HORKAGE_NOLPM, }, |
François Cami | 76936e9 | 2018-05-13 20:11:15 +0200 | [diff] [blame] | 3935 | { "SAMSUNG SSD PM830 mSATA *", "CXM13D1Q", ATA_HORKAGE_NOLPM, }, |
Diego Viola | 410b5c7 | 2018-11-12 17:22:52 -0200 | [diff] [blame] | 3936 | { "SAMSUNG MZ7TD256HAFV-000L9", NULL, ATA_HORKAGE_NOLPM, }, |
Hans de Goede | dd95749 | 2019-02-03 10:02:07 +0100 | [diff] [blame] | 3937 | { "SAMSUNG MZ7TE512HMHP-000L1", "EXT06L0Q", ATA_HORKAGE_NOLPM, }, |
Hans de Goede | b5b4d3a | 2018-04-24 11:19:07 +0200 | [diff] [blame] | 3938 | |
Marc Carino | f78dea0 | 2013-12-16 18:15:53 -0800 | [diff] [blame] | 3939 | /* devices that don't properly handle queued TRIM commands */ |
Sudip Mukherjee | 136d769 | 2018-05-19 22:29:36 +0100 | [diff] [blame] | 3940 | { "Micron_M500IT_*", "MU01", ATA_HORKAGE_NO_NCQ_TRIM | |
| 3941 | ATA_HORKAGE_ZERO_AFTER_TRIM, }, |
Martin K. Petersen | 243918b | 2015-06-18 14:50:18 -0400 | [diff] [blame] | 3942 | { "Micron_M500_*", NULL, ATA_HORKAGE_NO_NCQ_TRIM | |
Martin K. Petersen | e61f7d1 | 2015-01-08 10:34:27 -0500 | [diff] [blame] | 3943 | ATA_HORKAGE_ZERO_AFTER_TRIM, }, |
Martin K. Petersen | ff7f53f | 2015-03-27 15:17:20 -0400 | [diff] [blame] | 3944 | { "Crucial_CT*M500*", NULL, ATA_HORKAGE_NO_NCQ_TRIM | |
| 3945 | ATA_HORKAGE_ZERO_AFTER_TRIM, }, |
Martin K. Petersen | 9051bd3 | 2015-07-15 21:03:23 -0400 | [diff] [blame] | 3946 | { "Micron_M5[15]0_*", "MU01", ATA_HORKAGE_NO_NCQ_TRIM | |
Martin K. Petersen | ff7f53f | 2015-03-27 15:17:20 -0400 | [diff] [blame] | 3947 | ATA_HORKAGE_ZERO_AFTER_TRIM, }, |
| 3948 | { "Crucial_CT*M550*", "MU01", ATA_HORKAGE_NO_NCQ_TRIM | |
| 3949 | ATA_HORKAGE_ZERO_AFTER_TRIM, }, |
| 3950 | { "Crucial_CT*MX100*", "MU01", ATA_HORKAGE_NO_NCQ_TRIM | |
| 3951 | ATA_HORKAGE_ZERO_AFTER_TRIM, }, |
Ju Hyung Park | ca6bfcb | 2018-03-11 02:28:35 +0900 | [diff] [blame] | 3952 | { "Samsung SSD 840*", NULL, ATA_HORKAGE_NO_NCQ_TRIM | |
| 3953 | ATA_HORKAGE_ZERO_AFTER_TRIM, }, |
| 3954 | { "Samsung SSD 850*", NULL, ATA_HORKAGE_NO_NCQ_TRIM | |
Martin K. Petersen | 6fc4d97 | 2015-03-27 15:17:21 -0400 | [diff] [blame] | 3955 | ATA_HORKAGE_ZERO_AFTER_TRIM, }, |
Guillermo A. Amaral | 7a7184b | 2015-08-25 23:29:13 -0700 | [diff] [blame] | 3956 | { "FCCT*M500*", NULL, ATA_HORKAGE_NO_NCQ_TRIM | |
| 3957 | ATA_HORKAGE_ZERO_AFTER_TRIM, }, |
Martin K. Petersen | e61f7d1 | 2015-01-08 10:34:27 -0500 | [diff] [blame] | 3958 | |
Arne Fitzenreiter | cda57b1 | 2015-07-15 13:54:37 +0200 | [diff] [blame] | 3959 | /* devices that don't properly handle TRIM commands */ |
| 3960 | { "SuperSSpeed S238*", NULL, ATA_HORKAGE_NOTRIM, }, |
| 3961 | |
Martin K. Petersen | e61f7d1 | 2015-01-08 10:34:27 -0500 | [diff] [blame] | 3962 | /* |
| 3963 | * As defined, the DRAT (Deterministic Read After Trim) and RZAT |
| 3964 | * (Return Zero After Trim) flags in the ATA Command Set are |
| 3965 | * unreliable in the sense that they only define what happens if |
| 3966 | * the device successfully executed the DSM TRIM command. TRIM |
| 3967 | * is only advisory, however, and the device is free to silently |
| 3968 | * ignore all or parts of the request. |
| 3969 | * |
| 3970 | * Whitelist drives that are known to reliably return zeroes |
| 3971 | * after TRIM. |
| 3972 | */ |
| 3973 | |
| 3974 | /* |
| 3975 | * The intel 510 drive has buggy DRAT/RZAT. Explicitly exclude |
| 3976 | * that model before whitelisting all other intel SSDs. |
| 3977 | */ |
| 3978 | { "INTEL*SSDSC2MH*", NULL, 0, }, |
| 3979 | |
Martin K. Petersen | ff7f53f | 2015-03-27 15:17:20 -0400 | [diff] [blame] | 3980 | { "Micron*", NULL, ATA_HORKAGE_ZERO_AFTER_TRIM, }, |
| 3981 | { "Crucial*", NULL, ATA_HORKAGE_ZERO_AFTER_TRIM, }, |
Martin K. Petersen | e61f7d1 | 2015-01-08 10:34:27 -0500 | [diff] [blame] | 3982 | { "INTEL*SSD*", NULL, ATA_HORKAGE_ZERO_AFTER_TRIM, }, |
| 3983 | { "SSD*INTEL*", NULL, ATA_HORKAGE_ZERO_AFTER_TRIM, }, |
| 3984 | { "Samsung*SSD*", NULL, ATA_HORKAGE_ZERO_AFTER_TRIM, }, |
| 3985 | { "SAMSUNG*SSD*", NULL, ATA_HORKAGE_ZERO_AFTER_TRIM, }, |
Juha-Matti Tilli | fd6f32f | 2018-12-02 12:47:08 +0200 | [diff] [blame] | 3986 | { "SAMSUNG*MZ7KM*", NULL, ATA_HORKAGE_ZERO_AFTER_TRIM, }, |
Martin K. Petersen | e61f7d1 | 2015-01-08 10:34:27 -0500 | [diff] [blame] | 3987 | { "ST[1248][0248]0[FH]*", NULL, ATA_HORKAGE_ZERO_AFTER_TRIM, }, |
Marc Carino | f78dea0 | 2013-12-16 18:15:53 -0800 | [diff] [blame] | 3988 | |
Tejun Heo | ecd75ad | 2014-01-16 09:47:17 -0500 | [diff] [blame] | 3989 | /* |
| 3990 | * Some WD SATA-I drives spin up and down erratically when the link |
| 3991 | * is put into the slumber mode. We don't have full list of the |
| 3992 | * affected devices. Disable LPM if the device matches one of the |
| 3993 | * known prefixes and is SATA-1. As a side effect LPM partial is |
| 3994 | * lost too. |
| 3995 | * |
| 3996 | * https://bugzilla.kernel.org/show_bug.cgi?id=57211 |
| 3997 | */ |
| 3998 | { "WDC WD800JD-*", NULL, ATA_HORKAGE_WD_BROKEN_LPM }, |
| 3999 | { "WDC WD1200JD-*", NULL, ATA_HORKAGE_WD_BROKEN_LPM }, |
| 4000 | { "WDC WD1600JD-*", NULL, ATA_HORKAGE_WD_BROKEN_LPM }, |
| 4001 | { "WDC WD2000JD-*", NULL, ATA_HORKAGE_WD_BROKEN_LPM }, |
| 4002 | { "WDC WD2500JD-*", NULL, ATA_HORKAGE_WD_BROKEN_LPM }, |
| 4003 | { "WDC WD3000JD-*", NULL, ATA_HORKAGE_WD_BROKEN_LPM }, |
| 4004 | { "WDC WD3200JD-*", NULL, ATA_HORKAGE_WD_BROKEN_LPM }, |
| 4005 | |
Alan Cox | 6919a0a | 2006-10-27 19:08:46 -0700 | [diff] [blame] | 4006 | /* End Marker */ |
| 4007 | { } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4008 | }; |
Jeff Garzik | 2e9edbf | 2006-03-24 09:56:57 -0500 | [diff] [blame] | 4009 | |
Tejun Heo | 75683fe | 2007-07-05 13:31:27 +0900 | [diff] [blame] | 4010 | static unsigned long ata_dev_blacklisted(const struct ata_device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4011 | { |
Tejun Heo | 8bfa79f | 2007-01-02 20:19:40 +0900 | [diff] [blame] | 4012 | unsigned char model_num[ATA_ID_PROD_LEN + 1]; |
| 4013 | unsigned char model_rev[ATA_ID_FW_REV_LEN + 1]; |
Alan Cox | 6919a0a | 2006-10-27 19:08:46 -0700 | [diff] [blame] | 4014 | const struct ata_blacklist_entry *ad = ata_device_blacklist; |
Albert Lee | 3a77827 | 2006-06-22 13:00:25 +0800 | [diff] [blame] | 4015 | |
Tejun Heo | 8bfa79f | 2007-01-02 20:19:40 +0900 | [diff] [blame] | 4016 | ata_id_c_string(dev->id, model_num, ATA_ID_PROD, sizeof(model_num)); |
| 4017 | 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] | 4018 | |
Alan Cox | 6919a0a | 2006-10-27 19:08:46 -0700 | [diff] [blame] | 4019 | while (ad->model_num) { |
George Spelvin | 1c40279 | 2014-10-07 07:26:38 -0400 | [diff] [blame] | 4020 | if (glob_match(ad->model_num, model_num)) { |
Alan Cox | 6919a0a | 2006-10-27 19:08:46 -0700 | [diff] [blame] | 4021 | if (ad->model_rev == NULL) |
| 4022 | return ad->horkage; |
George Spelvin | 1c40279 | 2014-10-07 07:26:38 -0400 | [diff] [blame] | 4023 | if (glob_match(ad->model_rev, model_rev)) |
Alan Cox | 6919a0a | 2006-10-27 19:08:46 -0700 | [diff] [blame] | 4024 | return ad->horkage; |
Alan Cox | f4b15fe | 2006-03-22 15:54:04 +0000 | [diff] [blame] | 4025 | } |
Alan Cox | 6919a0a | 2006-10-27 19:08:46 -0700 | [diff] [blame] | 4026 | ad++; |
Alan Cox | f4b15fe | 2006-03-22 15:54:04 +0000 | [diff] [blame] | 4027 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4028 | return 0; |
| 4029 | } |
| 4030 | |
Alan Cox | 6919a0a | 2006-10-27 19:08:46 -0700 | [diff] [blame] | 4031 | static int ata_dma_blacklisted(const struct ata_device *dev) |
| 4032 | { |
| 4033 | /* We don't support polling DMA. |
| 4034 | * DMA blacklist those ATAPI devices with CDB-intr (and use PIO) |
| 4035 | * if the LLDD handles only interrupts in the HSM_ST_LAST state. |
| 4036 | */ |
Tejun Heo | 9af5c9c | 2007-08-06 18:36:22 +0900 | [diff] [blame] | 4037 | if ((dev->link->ap->flags & ATA_FLAG_PIO_POLLING) && |
Alan Cox | 6919a0a | 2006-10-27 19:08:46 -0700 | [diff] [blame] | 4038 | (dev->flags & ATA_DFLAG_CDB_INTR)) |
| 4039 | return 1; |
Tejun Heo | 75683fe | 2007-07-05 13:31:27 +0900 | [diff] [blame] | 4040 | return (dev->horkage & ATA_HORKAGE_NODMA) ? 1 : 0; |
Alan Cox | 6919a0a | 2006-10-27 19:08:46 -0700 | [diff] [blame] | 4041 | } |
| 4042 | |
Tejun Heo | a6d5a51 | 2006-03-06 04:31:57 +0900 | [diff] [blame] | 4043 | /** |
Alan Cox | 6bbfd53 | 2007-11-05 22:58:58 +0000 | [diff] [blame] | 4044 | * ata_is_40wire - check drive side detection |
| 4045 | * @dev: device |
| 4046 | * |
| 4047 | * Perform drive side detection decoding, allowing for device vendors |
| 4048 | * who can't follow the documentation. |
| 4049 | */ |
| 4050 | |
| 4051 | static int ata_is_40wire(struct ata_device *dev) |
| 4052 | { |
| 4053 | if (dev->horkage & ATA_HORKAGE_IVB) |
| 4054 | return ata_drive_40wire_relaxed(dev->id); |
| 4055 | return ata_drive_40wire(dev->id); |
| 4056 | } |
| 4057 | |
| 4058 | /** |
Alan Cox | 15a5551 | 2008-03-28 14:33:46 -0700 | [diff] [blame] | 4059 | * cable_is_40wire - 40/80/SATA decider |
| 4060 | * @ap: port to consider |
| 4061 | * |
| 4062 | * This function encapsulates the policy for speed management |
| 4063 | * in one place. At the moment we don't cache the result but |
| 4064 | * there is a good case for setting ap->cbl to the result when |
| 4065 | * we are called with unknown cables (and figuring out if it |
| 4066 | * impacts hotplug at all). |
| 4067 | * |
| 4068 | * Return 1 if the cable appears to be 40 wire. |
| 4069 | */ |
| 4070 | |
| 4071 | static int cable_is_40wire(struct ata_port *ap) |
| 4072 | { |
| 4073 | struct ata_link *link; |
| 4074 | struct ata_device *dev; |
| 4075 | |
Tejun Heo | 4a9c7b3 | 2008-10-27 19:59:23 +0900 | [diff] [blame] | 4076 | /* If the controller thinks we are 40 wire, we are. */ |
Alan Cox | 15a5551 | 2008-03-28 14:33:46 -0700 | [diff] [blame] | 4077 | if (ap->cbl == ATA_CBL_PATA40) |
| 4078 | return 1; |
Tejun Heo | 4a9c7b3 | 2008-10-27 19:59:23 +0900 | [diff] [blame] | 4079 | |
| 4080 | /* If the controller thinks we are 80 wire, we are. */ |
Alan Cox | 15a5551 | 2008-03-28 14:33:46 -0700 | [diff] [blame] | 4081 | if (ap->cbl == ATA_CBL_PATA80 || ap->cbl == ATA_CBL_SATA) |
| 4082 | return 0; |
Tejun Heo | 4a9c7b3 | 2008-10-27 19:59:23 +0900 | [diff] [blame] | 4083 | |
| 4084 | /* If the system is known to be 40 wire short cable (eg |
| 4085 | * laptop), then we allow 80 wire modes even if the drive |
| 4086 | * isn't sure. |
| 4087 | */ |
Alan Cox | f792068 | 2008-04-08 16:37:50 +0100 | [diff] [blame] | 4088 | if (ap->cbl == ATA_CBL_PATA40_SHORT) |
| 4089 | return 0; |
Alan Cox | 15a5551 | 2008-03-28 14:33:46 -0700 | [diff] [blame] | 4090 | |
Tejun Heo | 4a9c7b3 | 2008-10-27 19:59:23 +0900 | [diff] [blame] | 4091 | /* If the controller doesn't know, we scan. |
| 4092 | * |
| 4093 | * Note: We look for all 40 wire detects at this point. Any |
| 4094 | * 80 wire detect is taken to be 80 wire cable because |
| 4095 | * - in many setups only the one drive (slave if present) will |
| 4096 | * give a valid detect |
| 4097 | * - if you have a non detect capable drive you don't want it |
| 4098 | * to colour the choice |
| 4099 | */ |
Tejun Heo | 1eca436 | 2008-11-03 20:03:17 +0900 | [diff] [blame] | 4100 | ata_for_each_link(link, ap, EDGE) { |
| 4101 | ata_for_each_dev(dev, link, ENABLED) { |
| 4102 | if (!ata_is_40wire(dev)) |
Alan Cox | 15a5551 | 2008-03-28 14:33:46 -0700 | [diff] [blame] | 4103 | return 0; |
| 4104 | } |
| 4105 | } |
| 4106 | return 1; |
| 4107 | } |
| 4108 | |
| 4109 | /** |
Tejun Heo | a6d5a51 | 2006-03-06 04:31:57 +0900 | [diff] [blame] | 4110 | * ata_dev_xfermask - Compute supported xfermask of the given device |
Tejun Heo | a6d5a51 | 2006-03-06 04:31:57 +0900 | [diff] [blame] | 4111 | * @dev: Device to compute xfermask for |
| 4112 | * |
Tejun Heo | acf356b | 2006-03-24 14:07:50 +0900 | [diff] [blame] | 4113 | * Compute supported xfermask of @dev and store it in |
| 4114 | * dev->*_mask. This function is responsible for applying all |
| 4115 | * known limits including host controller limits, device |
| 4116 | * blacklist, etc... |
Tejun Heo | a6d5a51 | 2006-03-06 04:31:57 +0900 | [diff] [blame] | 4117 | * |
| 4118 | * LOCKING: |
| 4119 | * None. |
Tejun Heo | a6d5a51 | 2006-03-06 04:31:57 +0900 | [diff] [blame] | 4120 | */ |
Tejun Heo | 3373efd | 2006-05-15 20:57:53 +0900 | [diff] [blame] | 4121 | static void ata_dev_xfermask(struct ata_device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4122 | { |
Tejun Heo | 9af5c9c | 2007-08-06 18:36:22 +0900 | [diff] [blame] | 4123 | struct ata_link *link = dev->link; |
| 4124 | struct ata_port *ap = link->ap; |
Jeff Garzik | cca3974 | 2006-08-24 03:19:22 -0400 | [diff] [blame] | 4125 | struct ata_host *host = ap->host; |
Tejun Heo | a6d5a51 | 2006-03-06 04:31:57 +0900 | [diff] [blame] | 4126 | unsigned long xfer_mask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4127 | |
Tejun Heo | 37deecb | 2006-08-10 16:59:07 +0900 | [diff] [blame] | 4128 | /* controller modes available */ |
Tejun Heo | 565083e | 2006-04-02 17:54:47 +0900 | [diff] [blame] | 4129 | xfer_mask = ata_pack_xfermask(ap->pio_mask, |
| 4130 | ap->mwdma_mask, ap->udma_mask); |
| 4131 | |
Robert Hancock | 8343f88 | 2007-03-06 02:37:51 -0800 | [diff] [blame] | 4132 | /* drive modes available */ |
Tejun Heo | 37deecb | 2006-08-10 16:59:07 +0900 | [diff] [blame] | 4133 | xfer_mask &= ata_pack_xfermask(dev->pio_mask, |
| 4134 | dev->mwdma_mask, dev->udma_mask); |
| 4135 | xfer_mask &= ata_id_xfermask(dev->id); |
Tejun Heo | 565083e | 2006-04-02 17:54:47 +0900 | [diff] [blame] | 4136 | |
Alan Cox | b352e57 | 2006-08-10 18:52:12 +0100 | [diff] [blame] | 4137 | /* |
| 4138 | * CFA Advanced TrueIDE timings are not allowed on a shared |
| 4139 | * cable |
| 4140 | */ |
| 4141 | if (ata_dev_pair(dev)) { |
| 4142 | /* No PIO5 or PIO6 */ |
| 4143 | xfer_mask &= ~(0x03 << (ATA_SHIFT_PIO + 5)); |
| 4144 | /* No MWDMA3 or MWDMA 4 */ |
| 4145 | xfer_mask &= ~(0x03 << (ATA_SHIFT_MWDMA + 3)); |
| 4146 | } |
| 4147 | |
Tejun Heo | 37deecb | 2006-08-10 16:59:07 +0900 | [diff] [blame] | 4148 | if (ata_dma_blacklisted(dev)) { |
| 4149 | xfer_mask &= ~(ATA_MASK_MWDMA | ATA_MASK_UDMA); |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 4150 | ata_dev_warn(dev, |
| 4151 | "device is on DMA blacklist, disabling DMA\n"); |
Tejun Heo | 37deecb | 2006-08-10 16:59:07 +0900 | [diff] [blame] | 4152 | } |
Tejun Heo | a6d5a51 | 2006-03-06 04:31:57 +0900 | [diff] [blame] | 4153 | |
Petr Vandrovec | 14d66ab | 2007-03-08 10:12:12 +0100 | [diff] [blame] | 4154 | if ((host->flags & ATA_HOST_SIMPLEX) && |
Jeff Garzik | 2dcb407 | 2007-10-19 06:42:56 -0400 | [diff] [blame] | 4155 | host->simplex_claimed && host->simplex_claimed != ap) { |
Tejun Heo | 37deecb | 2006-08-10 16:59:07 +0900 | [diff] [blame] | 4156 | xfer_mask &= ~(ATA_MASK_MWDMA | ATA_MASK_UDMA); |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 4157 | ata_dev_warn(dev, |
| 4158 | "simplex DMA is claimed by other device, disabling DMA\n"); |
Alan Cox | 5444a6f | 2006-03-27 18:58:20 +0100 | [diff] [blame] | 4159 | } |
Tejun Heo | 565083e | 2006-04-02 17:54:47 +0900 | [diff] [blame] | 4160 | |
Jeff Garzik | e424675 | 2007-03-09 09:56:46 -0500 | [diff] [blame] | 4161 | if (ap->flags & ATA_FLAG_NO_IORDY) |
| 4162 | xfer_mask &= ata_pio_mask_no_iordy(dev); |
| 4163 | |
Alan Cox | 5444a6f | 2006-03-27 18:58:20 +0100 | [diff] [blame] | 4164 | if (ap->ops->mode_filter) |
Alan Cox | a76b62ca | 2007-03-09 09:34:07 -0500 | [diff] [blame] | 4165 | xfer_mask = ap->ops->mode_filter(dev, xfer_mask); |
Alan Cox | 5444a6f | 2006-03-27 18:58:20 +0100 | [diff] [blame] | 4166 | |
Robert Hancock | 8343f88 | 2007-03-06 02:37:51 -0800 | [diff] [blame] | 4167 | /* Apply cable rule here. Don't apply it early because when |
| 4168 | * we handle hot plug the cable type can itself change. |
| 4169 | * Check this last so that we know if the transfer rate was |
| 4170 | * solely limited by the cable. |
| 4171 | * Unknown or 80 wire cables reported host side are checked |
| 4172 | * drive side as well. Cases where we know a 40wire cable |
| 4173 | * is used safely for 80 are not checked here. |
| 4174 | */ |
| 4175 | if (xfer_mask & (0xF8 << ATA_SHIFT_UDMA)) |
| 4176 | /* UDMA/44 or higher would be available */ |
Alan Cox | 15a5551 | 2008-03-28 14:33:46 -0700 | [diff] [blame] | 4177 | if (cable_is_40wire(ap)) { |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 4178 | ata_dev_warn(dev, |
| 4179 | "limited to UDMA/33 due to 40-wire cable\n"); |
Robert Hancock | 8343f88 | 2007-03-06 02:37:51 -0800 | [diff] [blame] | 4180 | xfer_mask &= ~(0xF8 << ATA_SHIFT_UDMA); |
| 4181 | } |
| 4182 | |
Tejun Heo | 565083e | 2006-04-02 17:54:47 +0900 | [diff] [blame] | 4183 | ata_unpack_xfermask(xfer_mask, &dev->pio_mask, |
| 4184 | &dev->mwdma_mask, &dev->udma_mask); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4185 | } |
| 4186 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4187 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4188 | * ata_dev_set_xfermode - Issue SET FEATURES - XFER MODE command |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4189 | * @dev: Device to which command will be sent |
| 4190 | * |
Jeff Garzik | 780a87f | 2005-05-30 15:41:05 -0400 | [diff] [blame] | 4191 | * Issue SET FEATURES - XFER MODE command to device @dev |
| 4192 | * on port @ap. |
| 4193 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4194 | * LOCKING: |
Jeff Garzik | 0cba632 | 2005-05-30 19:49:12 -0400 | [diff] [blame] | 4195 | * PCI/etc. bus probe sem. |
Tejun Heo | 83206a2 | 2006-03-24 15:25:31 +0900 | [diff] [blame] | 4196 | * |
| 4197 | * RETURNS: |
| 4198 | * 0 on success, AC_ERR_* mask otherwise. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4199 | */ |
| 4200 | |
Tejun Heo | 3373efd | 2006-05-15 20:57:53 +0900 | [diff] [blame] | 4201 | static unsigned int ata_dev_set_xfermode(struct ata_device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4202 | { |
Tejun Heo | a012370 | 2005-12-13 14:49:31 +0900 | [diff] [blame] | 4203 | struct ata_taskfile tf; |
Tejun Heo | 83206a2 | 2006-03-24 15:25:31 +0900 | [diff] [blame] | 4204 | unsigned int err_mask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4205 | |
| 4206 | /* set up set-features taskfile */ |
| 4207 | DPRINTK("set features - xfer mode\n"); |
| 4208 | |
Tejun Heo | 464cf17 | 2007-05-27 15:10:40 +0200 | [diff] [blame] | 4209 | /* Some controllers and ATAPI devices show flaky interrupt |
| 4210 | * behavior after setting xfer mode. Use polling instead. |
| 4211 | */ |
Tejun Heo | 3373efd | 2006-05-15 20:57:53 +0900 | [diff] [blame] | 4212 | ata_tf_init(dev, &tf); |
Tejun Heo | a012370 | 2005-12-13 14:49:31 +0900 | [diff] [blame] | 4213 | tf.command = ATA_CMD_SET_FEATURES; |
| 4214 | tf.feature = SETFEATURES_XFER; |
Tejun Heo | 464cf17 | 2007-05-27 15:10:40 +0200 | [diff] [blame] | 4215 | tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE | ATA_TFLAG_POLLING; |
Tejun Heo | a012370 | 2005-12-13 14:49:31 +0900 | [diff] [blame] | 4216 | tf.protocol = ATA_PROT_NODATA; |
Alan Cox | b9f8ab2 | 2007-11-19 14:33:11 +0000 | [diff] [blame] | 4217 | /* If we are using IORDY we must send the mode setting command */ |
Jeff Garzik | 11b7bec | 2007-11-23 21:12:14 -0500 | [diff] [blame] | 4218 | if (ata_pio_need_iordy(dev)) |
| 4219 | tf.nsect = dev->xfer_mode; |
Alan Cox | b9f8ab2 | 2007-11-19 14:33:11 +0000 | [diff] [blame] | 4220 | /* If the device has IORDY and the controller does not - turn it off */ |
| 4221 | else if (ata_id_has_iordy(dev->id)) |
Jeff Garzik | 11b7bec | 2007-11-23 21:12:14 -0500 | [diff] [blame] | 4222 | tf.nsect = 0x01; |
Alan Cox | b9f8ab2 | 2007-11-19 14:33:11 +0000 | [diff] [blame] | 4223 | else /* In the ancient relic department - skip all of this */ |
| 4224 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4225 | |
Mikulas Patocka | d531be2 | 2015-07-08 13:06:12 -0400 | [diff] [blame] | 4226 | /* On some disks, this command causes spin-up, so we need longer timeout */ |
| 4227 | 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] | 4228 | |
Tejun Heo | 83206a2 | 2006-03-24 15:25:31 +0900 | [diff] [blame] | 4229 | DPRINTK("EXIT, err_mask=%x\n", err_mask); |
| 4230 | return err_mask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4231 | } |
Tejun Heo | 1152b26 | 2010-09-01 17:50:05 +0200 | [diff] [blame] | 4232 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4233 | /** |
Jeff Garzik | 218f3d3 | 2007-10-25 00:33:27 -0400 | [diff] [blame] | 4234 | * ata_dev_set_feature - Issue SET FEATURES - SATA FEATURES |
Kristen Carlson Accardi | 9f45cbd | 2007-08-15 03:57:11 -0400 | [diff] [blame] | 4235 | * @dev: Device to which command will be sent |
| 4236 | * @enable: Whether to enable or disable the feature |
Jeff Garzik | 218f3d3 | 2007-10-25 00:33:27 -0400 | [diff] [blame] | 4237 | * @feature: The sector count represents the feature to set |
Kristen Carlson Accardi | 9f45cbd | 2007-08-15 03:57:11 -0400 | [diff] [blame] | 4238 | * |
| 4239 | * Issue SET FEATURES - SATA FEATURES command to device @dev |
Jeff Garzik | 218f3d3 | 2007-10-25 00:33:27 -0400 | [diff] [blame] | 4240 | * on port @ap with sector count |
Kristen Carlson Accardi | 9f45cbd | 2007-08-15 03:57:11 -0400 | [diff] [blame] | 4241 | * |
| 4242 | * LOCKING: |
| 4243 | * PCI/etc. bus probe sem. |
| 4244 | * |
| 4245 | * RETURNS: |
| 4246 | * 0 on success, AC_ERR_* mask otherwise. |
| 4247 | */ |
Tejun Heo | 1152b26 | 2010-09-01 17:50:05 +0200 | [diff] [blame] | 4248 | 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] | 4249 | { |
| 4250 | struct ata_taskfile tf; |
| 4251 | unsigned int err_mask; |
Damien Le Moal | 974e0a4 | 2016-04-04 12:17:09 -0400 | [diff] [blame] | 4252 | unsigned long timeout = 0; |
Kristen Carlson Accardi | 9f45cbd | 2007-08-15 03:57:11 -0400 | [diff] [blame] | 4253 | |
| 4254 | /* set up set-features taskfile */ |
| 4255 | DPRINTK("set features - SATA features\n"); |
| 4256 | |
| 4257 | ata_tf_init(dev, &tf); |
| 4258 | tf.command = ATA_CMD_SET_FEATURES; |
| 4259 | tf.feature = enable; |
| 4260 | tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE; |
| 4261 | tf.protocol = ATA_PROT_NODATA; |
Jeff Garzik | 218f3d3 | 2007-10-25 00:33:27 -0400 | [diff] [blame] | 4262 | tf.nsect = feature; |
Kristen Carlson Accardi | 9f45cbd | 2007-08-15 03:57:11 -0400 | [diff] [blame] | 4263 | |
Damien Le Moal | 974e0a4 | 2016-04-04 12:17:09 -0400 | [diff] [blame] | 4264 | if (enable == SETFEATURES_SPINUP) |
| 4265 | timeout = ata_probe_timeout ? |
| 4266 | ata_probe_timeout * 1000 : SETFEATURES_SPINUP_TIMEOUT; |
| 4267 | 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] | 4268 | |
| 4269 | DPRINTK("EXIT, err_mask=%x\n", err_mask); |
| 4270 | return err_mask; |
| 4271 | } |
Jeff Garzik | 633de4c | 2012-09-13 01:12:29 -0400 | [diff] [blame] | 4272 | EXPORT_SYMBOL_GPL(ata_dev_set_feature); |
Kristen Carlson Accardi | 9f45cbd | 2007-08-15 03:57:11 -0400 | [diff] [blame] | 4273 | |
| 4274 | /** |
Albert Lee | 8bf62ece | 2005-05-12 15:29:42 -0400 | [diff] [blame] | 4275 | * ata_dev_init_params - Issue INIT DEV PARAMS command |
Albert Lee | 8bf62ece | 2005-05-12 15:29:42 -0400 | [diff] [blame] | 4276 | * @dev: Device to which command will be sent |
Randy Dunlap | e2a7f77 | 2006-05-18 10:50:18 -0700 | [diff] [blame] | 4277 | * @heads: Number of heads (taskfile parameter) |
| 4278 | * @sectors: Number of sectors (taskfile parameter) |
Albert Lee | 8bf62ece | 2005-05-12 15:29:42 -0400 | [diff] [blame] | 4279 | * |
| 4280 | * LOCKING: |
Tejun Heo | 6aff8f1 | 2006-02-15 18:24:09 +0900 | [diff] [blame] | 4281 | * Kernel thread context (may sleep) |
| 4282 | * |
| 4283 | * RETURNS: |
| 4284 | * 0 on success, AC_ERR_* mask otherwise. |
Albert Lee | 8bf62ece | 2005-05-12 15:29:42 -0400 | [diff] [blame] | 4285 | */ |
Tejun Heo | 3373efd | 2006-05-15 20:57:53 +0900 | [diff] [blame] | 4286 | static unsigned int ata_dev_init_params(struct ata_device *dev, |
| 4287 | u16 heads, u16 sectors) |
Albert Lee | 8bf62ece | 2005-05-12 15:29:42 -0400 | [diff] [blame] | 4288 | { |
Tejun Heo | a012370 | 2005-12-13 14:49:31 +0900 | [diff] [blame] | 4289 | struct ata_taskfile tf; |
Tejun Heo | 6aff8f1 | 2006-02-15 18:24:09 +0900 | [diff] [blame] | 4290 | unsigned int err_mask; |
Albert Lee | 8bf62ece | 2005-05-12 15:29:42 -0400 | [diff] [blame] | 4291 | |
| 4292 | /* Number of sectors per track 1-255. Number of heads 1-16 */ |
| 4293 | if (sectors < 1 || sectors > 255 || heads < 1 || heads > 16) |
Albert Lee | 00b6f5e | 2006-03-27 16:39:18 +0800 | [diff] [blame] | 4294 | return AC_ERR_INVALID; |
Albert Lee | 8bf62ece | 2005-05-12 15:29:42 -0400 | [diff] [blame] | 4295 | |
| 4296 | /* set up init dev params taskfile */ |
| 4297 | DPRINTK("init dev params \n"); |
| 4298 | |
Tejun Heo | 3373efd | 2006-05-15 20:57:53 +0900 | [diff] [blame] | 4299 | ata_tf_init(dev, &tf); |
Tejun Heo | a012370 | 2005-12-13 14:49:31 +0900 | [diff] [blame] | 4300 | tf.command = ATA_CMD_INIT_DEV_PARAMS; |
| 4301 | tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE; |
| 4302 | tf.protocol = ATA_PROT_NODATA; |
| 4303 | tf.nsect = sectors; |
| 4304 | tf.device |= (heads - 1) & 0x0f; /* max head = num. of heads - 1 */ |
Albert Lee | 8bf62ece | 2005-05-12 15:29:42 -0400 | [diff] [blame] | 4305 | |
Tejun Heo | 2b78910 | 2007-10-09 15:05:44 +0900 | [diff] [blame] | 4306 | 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] | 4307 | /* A clean abort indicates an original or just out of spec drive |
| 4308 | and we should continue as we issue the setup based on the |
| 4309 | drive reported working geometry */ |
| 4310 | if (err_mask == AC_ERR_DEV && (tf.feature & ATA_ABORTED)) |
| 4311 | err_mask = 0; |
Albert Lee | 8bf62ece | 2005-05-12 15:29:42 -0400 | [diff] [blame] | 4312 | |
Tejun Heo | 6aff8f1 | 2006-02-15 18:24:09 +0900 | [diff] [blame] | 4313 | DPRINTK("EXIT, err_mask=%x\n", err_mask); |
| 4314 | return err_mask; |
Albert Lee | 8bf62ece | 2005-05-12 15:29:42 -0400 | [diff] [blame] | 4315 | } |
| 4316 | |
| 4317 | /** |
Tejun Heo | 5895ef9 | 2008-06-17 12:36:26 +0900 | [diff] [blame] | 4318 | * atapi_check_dma - Check whether ATAPI DMA can be supported |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4319 | * @qc: Metadata associated with taskfile to check |
| 4320 | * |
Jeff Garzik | 780a87f | 2005-05-30 15:41:05 -0400 | [diff] [blame] | 4321 | * Allow low-level driver to filter ATA PACKET commands, returning |
| 4322 | * a status indicating whether or not it is OK to use DMA for the |
| 4323 | * supplied PACKET command. |
| 4324 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4325 | * LOCKING: |
Jeff Garzik | cca3974 | 2006-08-24 03:19:22 -0400 | [diff] [blame] | 4326 | * spin_lock_irqsave(host lock) |
Jeff Garzik | 0cba632 | 2005-05-30 19:49:12 -0400 | [diff] [blame] | 4327 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4328 | * RETURNS: 0 when ATAPI DMA can be used |
| 4329 | * nonzero otherwise |
| 4330 | */ |
Tejun Heo | 5895ef9 | 2008-06-17 12:36:26 +0900 | [diff] [blame] | 4331 | int atapi_check_dma(struct ata_queued_cmd *qc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4332 | { |
| 4333 | struct ata_port *ap = qc->ap; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4334 | |
Tejun Heo | b9a4197 | 2007-06-27 02:48:43 +0900 | [diff] [blame] | 4335 | /* Don't allow DMA if it isn't multiple of 16 bytes. Quite a |
| 4336 | * few ATAPI devices choke on such DMA requests. |
| 4337 | */ |
Tejun Heo | 6a87e42 | 2008-11-03 19:01:09 +0900 | [diff] [blame] | 4338 | if (!(qc->dev->horkage & ATA_HORKAGE_ATAPI_MOD16_DMA) && |
| 4339 | unlikely(qc->nbytes & 15)) |
Tejun Heo | b9a4197 | 2007-06-27 02:48:43 +0900 | [diff] [blame] | 4340 | return 1; |
Albert Lee | 6f23a31 | 2007-04-02 11:39:25 +0800 | [diff] [blame] | 4341 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4342 | if (ap->ops->check_atapi_dma) |
Tejun Heo | b9a4197 | 2007-06-27 02:48:43 +0900 | [diff] [blame] | 4343 | return ap->ops->check_atapi_dma(qc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4344 | |
Tejun Heo | b9a4197 | 2007-06-27 02:48:43 +0900 | [diff] [blame] | 4345 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4346 | } |
Tejun Heo | b9a4197 | 2007-06-27 02:48:43 +0900 | [diff] [blame] | 4347 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4348 | /** |
Tejun Heo | 31cc23b | 2007-09-23 13:14:12 +0900 | [diff] [blame] | 4349 | * ata_std_qc_defer - Check whether a qc needs to be deferred |
| 4350 | * @qc: ATA command in question |
| 4351 | * |
| 4352 | * Non-NCQ commands cannot run with any other command, NCQ or |
| 4353 | * not. As upper layer only knows the queue depth, we are |
| 4354 | * responsible for maintaining exclusion. This function checks |
| 4355 | * whether a new command @qc can be issued. |
| 4356 | * |
| 4357 | * LOCKING: |
| 4358 | * spin_lock_irqsave(host lock) |
| 4359 | * |
| 4360 | * RETURNS: |
| 4361 | * ATA_DEFER_* if deferring is needed, 0 otherwise. |
| 4362 | */ |
| 4363 | int ata_std_qc_defer(struct ata_queued_cmd *qc) |
| 4364 | { |
| 4365 | struct ata_link *link = qc->dev->link; |
| 4366 | |
Hannes Reinecke | 179b310 | 2016-07-14 09:05:43 +0900 | [diff] [blame] | 4367 | if (ata_is_ncq(qc->tf.protocol)) { |
Tejun Heo | 31cc23b | 2007-09-23 13:14:12 +0900 | [diff] [blame] | 4368 | if (!ata_tag_valid(link->active_tag)) |
| 4369 | return 0; |
| 4370 | } else { |
| 4371 | if (!ata_tag_valid(link->active_tag) && !link->sactive) |
| 4372 | return 0; |
| 4373 | } |
| 4374 | |
| 4375 | return ATA_DEFER_LINK; |
| 4376 | } |
Bartlomiej Zolnierkiewicz | a52fbcf | 2020-03-26 16:58:04 +0100 | [diff] [blame] | 4377 | EXPORT_SYMBOL_GPL(ata_std_qc_defer); |
Tejun Heo | 31cc23b | 2007-09-23 13:14:12 +0900 | [diff] [blame] | 4378 | |
Jiri Slaby | 95364f3 | 2019-10-31 10:59:45 +0100 | [diff] [blame] | 4379 | enum ata_completion_errors ata_noop_qc_prep(struct ata_queued_cmd *qc) |
| 4380 | { |
| 4381 | return AC_ERR_OK; |
| 4382 | } |
Bartlomiej Zolnierkiewicz | a52fbcf | 2020-03-26 16:58:04 +0100 | [diff] [blame] | 4383 | EXPORT_SYMBOL_GPL(ata_noop_qc_prep); |
Brian King | e46834c | 2006-03-17 17:04:03 -0600 | [diff] [blame] | 4384 | |
Jeff Garzik | 0cba632 | 2005-05-30 19:49:12 -0400 | [diff] [blame] | 4385 | /** |
Jeff Garzik | 0cba632 | 2005-05-30 19:49:12 -0400 | [diff] [blame] | 4386 | * ata_sg_init - Associate command with scatter-gather table. |
| 4387 | * @qc: Command to be associated |
| 4388 | * @sg: Scatter-gather table. |
| 4389 | * @n_elem: Number of elements in s/g table. |
| 4390 | * |
| 4391 | * Initialize the data-related elements of queued_cmd @qc |
| 4392 | * to point to a scatter-gather table @sg, containing @n_elem |
| 4393 | * elements. |
| 4394 | * |
| 4395 | * LOCKING: |
Jeff Garzik | cca3974 | 2006-08-24 03:19:22 -0400 | [diff] [blame] | 4396 | * spin_lock_irqsave(host lock) |
Jeff Garzik | 0cba632 | 2005-05-30 19:49:12 -0400 | [diff] [blame] | 4397 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4398 | void ata_sg_init(struct ata_queued_cmd *qc, struct scatterlist *sg, |
| 4399 | unsigned int n_elem) |
| 4400 | { |
Tejun Heo | ff2aeb1 | 2007-12-05 16:43:11 +0900 | [diff] [blame] | 4401 | qc->sg = sg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4402 | qc->n_elem = n_elem; |
Tejun Heo | ff2aeb1 | 2007-12-05 16:43:11 +0900 | [diff] [blame] | 4403 | qc->cursg = qc->sg; |
| 4404 | } |
| 4405 | |
Geert Uytterhoeven | 2874d5e | 2017-01-03 19:09:49 +0100 | [diff] [blame] | 4406 | #ifdef CONFIG_HAS_DMA |
| 4407 | |
| 4408 | /** |
| 4409 | * ata_sg_clean - Unmap DMA memory associated with command |
| 4410 | * @qc: Command containing DMA memory to be released |
| 4411 | * |
| 4412 | * Unmap all mapped DMA memory associated with this command. |
| 4413 | * |
| 4414 | * LOCKING: |
| 4415 | * spin_lock_irqsave(host lock) |
| 4416 | */ |
Jason Yan | af27e01 | 2017-03-10 10:05:40 +0800 | [diff] [blame] | 4417 | static void ata_sg_clean(struct ata_queued_cmd *qc) |
Geert Uytterhoeven | 2874d5e | 2017-01-03 19:09:49 +0100 | [diff] [blame] | 4418 | { |
| 4419 | struct ata_port *ap = qc->ap; |
| 4420 | struct scatterlist *sg = qc->sg; |
| 4421 | int dir = qc->dma_dir; |
| 4422 | |
| 4423 | WARN_ON_ONCE(sg == NULL); |
| 4424 | |
| 4425 | VPRINTK("unmapping %u sg elements\n", qc->n_elem); |
| 4426 | |
| 4427 | if (qc->n_elem) |
| 4428 | dma_unmap_sg(ap->dev, sg, qc->orig_n_elem, dir); |
| 4429 | |
| 4430 | qc->flags &= ~ATA_QCFLAG_DMAMAP; |
| 4431 | qc->sg = NULL; |
| 4432 | } |
| 4433 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4434 | /** |
Jeff Garzik | 0cba632 | 2005-05-30 19:49:12 -0400 | [diff] [blame] | 4435 | * ata_sg_setup - DMA-map the scatter-gather table associated with a command. |
| 4436 | * @qc: Command with scatter-gather table to be mapped. |
| 4437 | * |
| 4438 | * DMA-map the scatter-gather table associated with queued_cmd @qc. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4439 | * |
| 4440 | * LOCKING: |
Jeff Garzik | cca3974 | 2006-08-24 03:19:22 -0400 | [diff] [blame] | 4441 | * spin_lock_irqsave(host lock) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4442 | * |
| 4443 | * RETURNS: |
Jeff Garzik | 0cba632 | 2005-05-30 19:49:12 -0400 | [diff] [blame] | 4444 | * Zero on success, negative on error. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4445 | * |
| 4446 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4447 | static int ata_sg_setup(struct ata_queued_cmd *qc) |
| 4448 | { |
| 4449 | struct ata_port *ap = qc->ap; |
James Bottomley | dde2020 | 2008-02-19 11:36:56 +0100 | [diff] [blame] | 4450 | unsigned int n_elem; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4451 | |
Tejun Heo | 44877b4 | 2007-02-21 01:06:51 +0900 | [diff] [blame] | 4452 | VPRINTK("ENTER, ata%u\n", ap->print_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4453 | |
James Bottomley | dde2020 | 2008-02-19 11:36:56 +0100 | [diff] [blame] | 4454 | n_elem = dma_map_sg(ap->dev, qc->sg, qc->n_elem, qc->dma_dir); |
| 4455 | if (n_elem < 1) |
| 4456 | return -1; |
Jeff Garzik | cedc9a4 | 2005-10-05 07:13:30 -0400 | [diff] [blame] | 4457 | |
Paul Menzel | bb37667 | 2018-07-08 09:18:21 +0200 | [diff] [blame] | 4458 | VPRINTK("%d sg elements mapped\n", n_elem); |
FUJITA Tomonori | 5825627 | 2009-02-27 17:35:43 +0900 | [diff] [blame] | 4459 | qc->orig_n_elem = qc->n_elem; |
James Bottomley | dde2020 | 2008-02-19 11:36:56 +0100 | [diff] [blame] | 4460 | qc->n_elem = n_elem; |
Tejun Heo | f92a263 | 2007-12-05 16:43:10 +0900 | [diff] [blame] | 4461 | qc->flags |= ATA_QCFLAG_DMAMAP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4462 | |
| 4463 | return 0; |
| 4464 | } |
| 4465 | |
Geert Uytterhoeven | 2874d5e | 2017-01-03 19:09:49 +0100 | [diff] [blame] | 4466 | #else /* !CONFIG_HAS_DMA */ |
| 4467 | |
| 4468 | static inline void ata_sg_clean(struct ata_queued_cmd *qc) {} |
| 4469 | static inline int ata_sg_setup(struct ata_queued_cmd *qc) { return -1; } |
| 4470 | |
| 4471 | #endif /* !CONFIG_HAS_DMA */ |
| 4472 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4473 | /** |
Randy Dunlap | c893a3a | 2006-01-28 13:15:32 -0500 | [diff] [blame] | 4474 | * swap_buf_le16 - swap halves of 16-bit words in place |
Edward Falk | 0baab86 | 2005-06-02 18:17:13 -0400 | [diff] [blame] | 4475 | * @buf: Buffer to swap |
| 4476 | * @buf_words: Number of 16-bit words in buffer. |
| 4477 | * |
| 4478 | * Swap halves of 16-bit words if needed to convert from |
| 4479 | * little-endian byte order to native cpu byte order, or |
| 4480 | * vice-versa. |
| 4481 | * |
| 4482 | * LOCKING: |
Randy Dunlap | 6f0ef4f | 2005-10-25 01:44:30 -0400 | [diff] [blame] | 4483 | * Inherited from caller. |
Edward Falk | 0baab86 | 2005-06-02 18:17:13 -0400 | [diff] [blame] | 4484 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4485 | void swap_buf_le16(u16 *buf, unsigned int buf_words) |
| 4486 | { |
| 4487 | #ifdef __BIG_ENDIAN |
| 4488 | unsigned int i; |
| 4489 | |
| 4490 | for (i = 0; i < buf_words; i++) |
| 4491 | buf[i] = le16_to_cpu(buf[i]); |
| 4492 | #endif /* __BIG_ENDIAN */ |
| 4493 | } |
| 4494 | |
Albert Lee | 6ae4cfb | 2005-08-12 14:15:34 +0800 | [diff] [blame] | 4495 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4496 | * ata_qc_new_init - Request an available ATA command, and initialize it |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4497 | * @dev: Device from whom we request an available command structure |
Randy Dunlap | 38755e8 | 2015-08-09 18:37:36 -0700 | [diff] [blame] | 4498 | * @tag: tag |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4499 | * |
| 4500 | * LOCKING: |
Jeff Garzik | 0cba632 | 2005-05-30 19:49:12 -0400 | [diff] [blame] | 4501 | * None. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4502 | */ |
| 4503 | |
Shaohua Li | 98bd4be | 2015-01-23 19:52:07 -0800 | [diff] [blame] | 4504 | 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] | 4505 | { |
Tejun Heo | 9af5c9c | 2007-08-06 18:36:22 +0900 | [diff] [blame] | 4506 | struct ata_port *ap = dev->link->ap; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4507 | struct ata_queued_cmd *qc; |
| 4508 | |
Shaohua Li | 98bd4be | 2015-01-23 19:52:07 -0800 | [diff] [blame] | 4509 | /* no command while frozen */ |
| 4510 | if (unlikely(ap->pflags & ATA_PFLAG_FROZEN)) |
| 4511 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4512 | |
Shaohua Li | 98bd4be | 2015-01-23 19:52:07 -0800 | [diff] [blame] | 4513 | /* libsas case */ |
Shaohua Li | 5067c04 | 2015-03-12 10:32:18 -0700 | [diff] [blame] | 4514 | if (ap->flags & ATA_FLAG_SAS_HOST) { |
Shaohua Li | 98bd4be | 2015-01-23 19:52:07 -0800 | [diff] [blame] | 4515 | tag = ata_sas_allocate_tag(ap); |
| 4516 | if (tag < 0) |
| 4517 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4518 | } |
| 4519 | |
Shaohua Li | 98bd4be | 2015-01-23 19:52:07 -0800 | [diff] [blame] | 4520 | qc = __ata_qc_from_tag(ap, tag); |
Jens Axboe | 5ac4079 | 2018-05-11 12:51:03 -0600 | [diff] [blame] | 4521 | qc->tag = qc->hw_tag = tag; |
Shaohua Li | 98bd4be | 2015-01-23 19:52:07 -0800 | [diff] [blame] | 4522 | qc->scsicmd = NULL; |
| 4523 | qc->ap = ap; |
| 4524 | qc->dev = dev; |
| 4525 | |
| 4526 | ata_qc_reinit(qc); |
| 4527 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4528 | return qc; |
| 4529 | } |
| 4530 | |
Tejun Heo | 8a8bc22 | 2008-11-10 14:48:21 +0900 | [diff] [blame] | 4531 | /** |
| 4532 | * ata_qc_free - free unused ata_queued_cmd |
| 4533 | * @qc: Command to complete |
| 4534 | * |
| 4535 | * Designed to free unused ata_queued_cmd object |
| 4536 | * in case something prevents using it. |
| 4537 | * |
| 4538 | * LOCKING: |
| 4539 | * spin_lock_irqsave(host lock) |
| 4540 | */ |
| 4541 | void ata_qc_free(struct ata_queued_cmd *qc) |
| 4542 | { |
Julia Lawall | a110401 | 2009-10-17 08:41:47 +0200 | [diff] [blame] | 4543 | struct ata_port *ap; |
Tejun Heo | 8a8bc22 | 2008-11-10 14:48:21 +0900 | [diff] [blame] | 4544 | unsigned int tag; |
| 4545 | |
Tejun Heo | efcb3cf | 2009-01-09 19:19:14 +0900 | [diff] [blame] | 4546 | WARN_ON_ONCE(qc == NULL); /* ata_qc_from_tag _might_ return NULL */ |
Julia Lawall | a110401 | 2009-10-17 08:41:47 +0200 | [diff] [blame] | 4547 | ap = qc->ap; |
Tejun Heo | 8a8bc22 | 2008-11-10 14:48:21 +0900 | [diff] [blame] | 4548 | |
| 4549 | qc->flags = 0; |
| 4550 | tag = qc->tag; |
Jens Axboe | 28361c4 | 2018-05-11 12:51:09 -0600 | [diff] [blame] | 4551 | if (ata_tag_valid(tag)) { |
Tejun Heo | 8a8bc22 | 2008-11-10 14:48:21 +0900 | [diff] [blame] | 4552 | qc->tag = ATA_TAG_POISON; |
Shaohua Li | 5067c04 | 2015-03-12 10:32:18 -0700 | [diff] [blame] | 4553 | if (ap->flags & ATA_FLAG_SAS_HOST) |
Shaohua Li | 98bd4be | 2015-01-23 19:52:07 -0800 | [diff] [blame] | 4554 | ata_sas_free_tag(tag, ap); |
Tejun Heo | 8a8bc22 | 2008-11-10 14:48:21 +0900 | [diff] [blame] | 4555 | } |
| 4556 | } |
| 4557 | |
Tejun Heo | 7601442 | 2006-02-11 15:13:49 +0900 | [diff] [blame] | 4558 | void __ata_qc_complete(struct ata_queued_cmd *qc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4559 | { |
Julia Lawall | a110401 | 2009-10-17 08:41:47 +0200 | [diff] [blame] | 4560 | struct ata_port *ap; |
| 4561 | struct ata_link *link; |
Tejun Heo | dedaf2b | 2006-05-15 21:03:43 +0900 | [diff] [blame] | 4562 | |
Tejun Heo | efcb3cf | 2009-01-09 19:19:14 +0900 | [diff] [blame] | 4563 | WARN_ON_ONCE(qc == NULL); /* ata_qc_from_tag _might_ return NULL */ |
| 4564 | WARN_ON_ONCE(!(qc->flags & ATA_QCFLAG_ACTIVE)); |
Julia Lawall | a110401 | 2009-10-17 08:41:47 +0200 | [diff] [blame] | 4565 | ap = qc->ap; |
| 4566 | link = qc->dev->link; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4567 | |
| 4568 | if (likely(qc->flags & ATA_QCFLAG_DMAMAP)) |
| 4569 | ata_sg_clean(qc); |
| 4570 | |
Tejun Heo | 7401abf | 2006-05-15 20:57:32 +0900 | [diff] [blame] | 4571 | /* command should be marked inactive atomically with qc completion */ |
Hannes Reinecke | 179b310 | 2016-07-14 09:05:43 +0900 | [diff] [blame] | 4572 | if (ata_is_ncq(qc->tf.protocol)) { |
Jens Axboe | 4e5b626 | 2018-05-11 12:51:04 -0600 | [diff] [blame] | 4573 | link->sactive &= ~(1 << qc->hw_tag); |
Tejun Heo | da917d6 | 2007-09-23 13:14:12 +0900 | [diff] [blame] | 4574 | if (!link->sactive) |
| 4575 | ap->nr_active_links--; |
| 4576 | } else { |
Tejun Heo | 9af5c9c | 2007-08-06 18:36:22 +0900 | [diff] [blame] | 4577 | link->active_tag = ATA_TAG_POISON; |
Tejun Heo | da917d6 | 2007-09-23 13:14:12 +0900 | [diff] [blame] | 4578 | ap->nr_active_links--; |
| 4579 | } |
| 4580 | |
| 4581 | /* clear exclusive status */ |
| 4582 | if (unlikely(qc->flags & ATA_QCFLAG_CLEAR_EXCL && |
| 4583 | ap->excl_link == link)) |
| 4584 | ap->excl_link = NULL; |
Tejun Heo | 7401abf | 2006-05-15 20:57:32 +0900 | [diff] [blame] | 4585 | |
Albert Lee | 3f3791d | 2005-08-16 14:25:38 +0800 | [diff] [blame] | 4586 | /* atapi: mark qc as inactive to prevent the interrupt handler |
| 4587 | * from completing the command twice later, before the error handler |
| 4588 | * is called. (when rc != 0 and atapi request sense is needed) |
| 4589 | */ |
| 4590 | qc->flags &= ~ATA_QCFLAG_ACTIVE; |
Jens Axboe | e3ed8939 | 2018-05-11 12:51:05 -0600 | [diff] [blame] | 4591 | ap->qc_active &= ~(1ULL << qc->tag); |
Albert Lee | 3f3791d | 2005-08-16 14:25:38 +0800 | [diff] [blame] | 4592 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4593 | /* call completion callback */ |
Tejun Heo | 77853bf | 2006-01-23 13:09:36 +0900 | [diff] [blame] | 4594 | qc->complete_fn(qc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4595 | } |
| 4596 | |
Tejun Heo | 39599a5 | 2006-11-14 22:37:35 +0900 | [diff] [blame] | 4597 | static void fill_result_tf(struct ata_queued_cmd *qc) |
| 4598 | { |
| 4599 | struct ata_port *ap = qc->ap; |
| 4600 | |
Tejun Heo | 39599a5 | 2006-11-14 22:37:35 +0900 | [diff] [blame] | 4601 | qc->result_tf.flags = qc->tf.flags; |
Tejun Heo | 22183bf | 2008-04-07 22:47:20 +0900 | [diff] [blame] | 4602 | ap->ops->qc_fill_rtf(qc); |
Tejun Heo | 39599a5 | 2006-11-14 22:37:35 +0900 | [diff] [blame] | 4603 | } |
| 4604 | |
Tejun Heo | 00115e0 | 2007-11-27 19:28:58 +0900 | [diff] [blame] | 4605 | static void ata_verify_xfer(struct ata_queued_cmd *qc) |
| 4606 | { |
| 4607 | struct ata_device *dev = qc->dev; |
| 4608 | |
Christoph Hellwig | eb0effd | 2016-07-16 22:16:41 +0900 | [diff] [blame] | 4609 | if (!ata_is_data(qc->tf.protocol)) |
Tejun Heo | 00115e0 | 2007-11-27 19:28:58 +0900 | [diff] [blame] | 4610 | return; |
| 4611 | |
| 4612 | if ((dev->mwdma_mask || dev->udma_mask) && ata_is_pio(qc->tf.protocol)) |
| 4613 | return; |
| 4614 | |
| 4615 | dev->flags &= ~ATA_DFLAG_DUBIOUS_XFER; |
| 4616 | } |
| 4617 | |
Tejun Heo | f686bcb | 2006-05-15 20:58:05 +0900 | [diff] [blame] | 4618 | /** |
| 4619 | * ata_qc_complete - Complete an active ATA command |
| 4620 | * @qc: Command to complete |
Tejun Heo | f686bcb | 2006-05-15 20:58:05 +0900 | [diff] [blame] | 4621 | * |
Tejun Heo | 1aadf5c | 2010-06-25 15:03:34 +0200 | [diff] [blame] | 4622 | * Indicate to the mid and upper layers that an ATA command has |
| 4623 | * completed, with either an ok or not-ok status. |
| 4624 | * |
| 4625 | * Refrain from calling this function multiple times when |
| 4626 | * successfully completing multiple NCQ commands. |
| 4627 | * ata_qc_complete_multiple() should be used instead, which will |
| 4628 | * properly update IRQ expect state. |
Tejun Heo | f686bcb | 2006-05-15 20:58:05 +0900 | [diff] [blame] | 4629 | * |
| 4630 | * LOCKING: |
Jeff Garzik | cca3974 | 2006-08-24 03:19:22 -0400 | [diff] [blame] | 4631 | * spin_lock_irqsave(host lock) |
Tejun Heo | f686bcb | 2006-05-15 20:58:05 +0900 | [diff] [blame] | 4632 | */ |
| 4633 | void ata_qc_complete(struct ata_queued_cmd *qc) |
| 4634 | { |
| 4635 | struct ata_port *ap = qc->ap; |
| 4636 | |
Stephan Linz | eb25cb9 | 2016-06-10 07:59:56 +0200 | [diff] [blame] | 4637 | /* Trigger the LED (if available) */ |
Linus Walleij | d1ed7c5 | 2018-02-24 23:45:56 +0100 | [diff] [blame] | 4638 | ledtrig_disk_activity(!!(qc->tf.flags & ATA_TFLAG_WRITE)); |
Stephan Linz | eb25cb9 | 2016-06-10 07:59:56 +0200 | [diff] [blame] | 4639 | |
Tejun Heo | f686bcb | 2006-05-15 20:58:05 +0900 | [diff] [blame] | 4640 | /* XXX: New EH and old EH use different mechanisms to |
| 4641 | * synchronize EH with regular execution path. |
| 4642 | * |
| 4643 | * In new EH, a failed qc is marked with ATA_QCFLAG_FAILED. |
| 4644 | * Normal execution path is responsible for not accessing a |
| 4645 | * failed qc. libata core enforces the rule by returning NULL |
| 4646 | * from ata_qc_from_tag() for failed qcs. |
| 4647 | * |
| 4648 | * Old EH depends on ata_qc_complete() nullifying completion |
| 4649 | * requests if ATA_QCFLAG_EH_SCHEDULED is set. Old EH does |
| 4650 | * not synchronize with interrupt handler. Only PIO task is |
| 4651 | * taken care of. |
| 4652 | */ |
| 4653 | if (ap->ops->error_handler) { |
Tejun Heo | 4dbfa39 | 2007-10-25 18:22:44 +0900 | [diff] [blame] | 4654 | struct ata_device *dev = qc->dev; |
| 4655 | struct ata_eh_info *ehi = &dev->link->eh_info; |
| 4656 | |
Tejun Heo | f686bcb | 2006-05-15 20:58:05 +0900 | [diff] [blame] | 4657 | if (unlikely(qc->err_mask)) |
| 4658 | qc->flags |= ATA_QCFLAG_FAILED; |
| 4659 | |
Tejun Heo | f08dc1a | 2010-12-09 15:59:32 +0100 | [diff] [blame] | 4660 | /* |
| 4661 | * Finish internal commands without any further processing |
| 4662 | * and always with the result TF filled. |
| 4663 | */ |
| 4664 | if (unlikely(ata_tag_internal(qc->tag))) { |
Tejun Heo | f4b31db9 | 2009-10-16 13:00:51 +0900 | [diff] [blame] | 4665 | fill_result_tf(qc); |
Hannes Reinecke | 255c03d | 2015-03-27 16:46:38 +0100 | [diff] [blame] | 4666 | trace_ata_qc_complete_internal(qc); |
Tejun Heo | f08dc1a | 2010-12-09 15:59:32 +0100 | [diff] [blame] | 4667 | __ata_qc_complete(qc); |
| 4668 | return; |
| 4669 | } |
Tejun Heo | f4b31db9 | 2009-10-16 13:00:51 +0900 | [diff] [blame] | 4670 | |
Tejun Heo | f08dc1a | 2010-12-09 15:59:32 +0100 | [diff] [blame] | 4671 | /* |
| 4672 | * Non-internal qc has failed. Fill the result TF and |
| 4673 | * summon EH. |
| 4674 | */ |
| 4675 | if (unlikely(qc->flags & ATA_QCFLAG_FAILED)) { |
| 4676 | fill_result_tf(qc); |
Hannes Reinecke | 255c03d | 2015-03-27 16:46:38 +0100 | [diff] [blame] | 4677 | trace_ata_qc_complete_failed(qc); |
Tejun Heo | f08dc1a | 2010-12-09 15:59:32 +0100 | [diff] [blame] | 4678 | ata_qc_schedule_eh(qc); |
Tejun Heo | f4b31db9 | 2009-10-16 13:00:51 +0900 | [diff] [blame] | 4679 | return; |
Tejun Heo | f686bcb | 2006-05-15 20:58:05 +0900 | [diff] [blame] | 4680 | } |
| 4681 | |
Tejun Heo | 4dc738e | 2009-09-17 18:45:27 +0900 | [diff] [blame] | 4682 | WARN_ON_ONCE(ap->pflags & ATA_PFLAG_FROZEN); |
| 4683 | |
Tejun Heo | f686bcb | 2006-05-15 20:58:05 +0900 | [diff] [blame] | 4684 | /* read result TF if requested */ |
| 4685 | if (qc->flags & ATA_QCFLAG_RESULT_TF) |
Tejun Heo | 39599a5 | 2006-11-14 22:37:35 +0900 | [diff] [blame] | 4686 | fill_result_tf(qc); |
Tejun Heo | f686bcb | 2006-05-15 20:58:05 +0900 | [diff] [blame] | 4687 | |
Hannes Reinecke | 255c03d | 2015-03-27 16:46:38 +0100 | [diff] [blame] | 4688 | trace_ata_qc_complete_done(qc); |
Tejun Heo | 4dbfa39 | 2007-10-25 18:22:44 +0900 | [diff] [blame] | 4689 | /* Some commands need post-processing after successful |
| 4690 | * completion. |
| 4691 | */ |
| 4692 | switch (qc->tf.command) { |
| 4693 | case ATA_CMD_SET_FEATURES: |
| 4694 | if (qc->tf.feature != SETFEATURES_WC_ON && |
Tom Yan | 0c12735 | 2016-07-12 21:37:02 +0800 | [diff] [blame] | 4695 | qc->tf.feature != SETFEATURES_WC_OFF && |
| 4696 | qc->tf.feature != SETFEATURES_RA_ON && |
| 4697 | qc->tf.feature != SETFEATURES_RA_OFF) |
Tejun Heo | 4dbfa39 | 2007-10-25 18:22:44 +0900 | [diff] [blame] | 4698 | break; |
Gustavo A. R. Silva | df561f66 | 2020-08-23 17:36:59 -0500 | [diff] [blame] | 4699 | fallthrough; |
Tejun Heo | 4dbfa39 | 2007-10-25 18:22:44 +0900 | [diff] [blame] | 4700 | case ATA_CMD_INIT_DEV_PARAMS: /* CHS translation changed */ |
| 4701 | case ATA_CMD_SET_MULTI: /* multi_count changed */ |
| 4702 | /* revalidate device */ |
| 4703 | ehi->dev_action[dev->devno] |= ATA_EH_REVALIDATE; |
| 4704 | ata_port_schedule_eh(ap); |
| 4705 | break; |
Tejun Heo | 054a5fb | 2007-10-25 18:30:36 +0900 | [diff] [blame] | 4706 | |
| 4707 | case ATA_CMD_SLEEP: |
| 4708 | dev->flags |= ATA_DFLAG_SLEEPING; |
| 4709 | break; |
Tejun Heo | 4dbfa39 | 2007-10-25 18:22:44 +0900 | [diff] [blame] | 4710 | } |
| 4711 | |
Tejun Heo | 00115e0 | 2007-11-27 19:28:58 +0900 | [diff] [blame] | 4712 | if (unlikely(dev->flags & ATA_DFLAG_DUBIOUS_XFER)) |
| 4713 | ata_verify_xfer(qc); |
| 4714 | |
Tejun Heo | f686bcb | 2006-05-15 20:58:05 +0900 | [diff] [blame] | 4715 | __ata_qc_complete(qc); |
| 4716 | } else { |
| 4717 | if (qc->flags & ATA_QCFLAG_EH_SCHEDULED) |
| 4718 | return; |
| 4719 | |
| 4720 | /* read result TF if failed or requested */ |
| 4721 | if (qc->err_mask || qc->flags & ATA_QCFLAG_RESULT_TF) |
Tejun Heo | 39599a5 | 2006-11-14 22:37:35 +0900 | [diff] [blame] | 4722 | fill_result_tf(qc); |
Tejun Heo | f686bcb | 2006-05-15 20:58:05 +0900 | [diff] [blame] | 4723 | |
| 4724 | __ata_qc_complete(qc); |
| 4725 | } |
| 4726 | } |
Bartlomiej Zolnierkiewicz | a52fbcf | 2020-03-26 16:58:04 +0100 | [diff] [blame] | 4727 | EXPORT_SYMBOL_GPL(ata_qc_complete); |
Tejun Heo | f686bcb | 2006-05-15 20:58:05 +0900 | [diff] [blame] | 4728 | |
Tejun Heo | dedaf2b | 2006-05-15 21:03:43 +0900 | [diff] [blame] | 4729 | /** |
Sascha Hauer | 8385d75 | 2019-12-13 09:04:08 +0100 | [diff] [blame] | 4730 | * ata_qc_get_active - get bitmask of active qcs |
| 4731 | * @ap: port in question |
| 4732 | * |
| 4733 | * LOCKING: |
| 4734 | * spin_lock_irqsave(host lock) |
| 4735 | * |
| 4736 | * RETURNS: |
| 4737 | * Bitmask of active qcs |
| 4738 | */ |
| 4739 | u64 ata_qc_get_active(struct ata_port *ap) |
| 4740 | { |
| 4741 | u64 qc_active = ap->qc_active; |
| 4742 | |
| 4743 | /* ATA_TAG_INTERNAL is sent to hw as tag 0 */ |
| 4744 | if (qc_active & (1ULL << ATA_TAG_INTERNAL)) { |
| 4745 | qc_active |= (1 << 0); |
| 4746 | qc_active &= ~(1ULL << ATA_TAG_INTERNAL); |
| 4747 | } |
| 4748 | |
| 4749 | return qc_active; |
| 4750 | } |
| 4751 | EXPORT_SYMBOL_GPL(ata_qc_get_active); |
| 4752 | |
| 4753 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4754 | * ata_qc_issue - issue taskfile to device |
| 4755 | * @qc: command to issue to device |
| 4756 | * |
| 4757 | * Prepare an ATA command to submission to device. |
| 4758 | * This includes mapping the data into a DMA-able |
| 4759 | * area, filling in the S/G table, and finally |
| 4760 | * writing the taskfile to hardware, starting the command. |
| 4761 | * |
| 4762 | * LOCKING: |
Jeff Garzik | cca3974 | 2006-08-24 03:19:22 -0400 | [diff] [blame] | 4763 | * spin_lock_irqsave(host lock) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4764 | */ |
Tejun Heo | 8e0e694 | 2006-03-31 20:41:11 +0900 | [diff] [blame] | 4765 | void ata_qc_issue(struct ata_queued_cmd *qc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4766 | { |
| 4767 | struct ata_port *ap = qc->ap; |
Tejun Heo | 9af5c9c | 2007-08-06 18:36:22 +0900 | [diff] [blame] | 4768 | struct ata_link *link = qc->dev->link; |
Tejun Heo | 405e66b | 2007-11-27 19:28:53 +0900 | [diff] [blame] | 4769 | u8 prot = qc->tf.protocol; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4770 | |
Tejun Heo | dedaf2b | 2006-05-15 21:03:43 +0900 | [diff] [blame] | 4771 | /* Make sure only one non-NCQ command is outstanding. The |
| 4772 | * check is skipped for old EH because it reuses active qc to |
| 4773 | * request ATAPI sense. |
| 4774 | */ |
Tejun Heo | efcb3cf | 2009-01-09 19:19:14 +0900 | [diff] [blame] | 4775 | 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] | 4776 | |
Tejun Heo | 1973a02 | 2007-12-05 10:36:13 +0900 | [diff] [blame] | 4777 | if (ata_is_ncq(prot)) { |
Jens Axboe | 4e5b626 | 2018-05-11 12:51:04 -0600 | [diff] [blame] | 4778 | WARN_ON_ONCE(link->sactive & (1 << qc->hw_tag)); |
Tejun Heo | da917d6 | 2007-09-23 13:14:12 +0900 | [diff] [blame] | 4779 | |
| 4780 | if (!link->sactive) |
| 4781 | ap->nr_active_links++; |
Jens Axboe | 4e5b626 | 2018-05-11 12:51:04 -0600 | [diff] [blame] | 4782 | link->sactive |= 1 << qc->hw_tag; |
Tejun Heo | dedaf2b | 2006-05-15 21:03:43 +0900 | [diff] [blame] | 4783 | } else { |
Tejun Heo | efcb3cf | 2009-01-09 19:19:14 +0900 | [diff] [blame] | 4784 | WARN_ON_ONCE(link->sactive); |
Tejun Heo | da917d6 | 2007-09-23 13:14:12 +0900 | [diff] [blame] | 4785 | |
| 4786 | ap->nr_active_links++; |
Tejun Heo | 9af5c9c | 2007-08-06 18:36:22 +0900 | [diff] [blame] | 4787 | link->active_tag = qc->tag; |
Tejun Heo | dedaf2b | 2006-05-15 21:03:43 +0900 | [diff] [blame] | 4788 | } |
| 4789 | |
Tejun Heo | e4a70e7 | 2006-03-31 20:36:47 +0900 | [diff] [blame] | 4790 | qc->flags |= ATA_QCFLAG_ACTIVE; |
Jens Axboe | e3ed8939 | 2018-05-11 12:51:05 -0600 | [diff] [blame] | 4791 | ap->qc_active |= 1ULL << qc->tag; |
Tejun Heo | e4a70e7 | 2006-03-31 20:36:47 +0900 | [diff] [blame] | 4792 | |
Tejun Heo | 60f5d6e | 2010-08-23 11:27:27 +0200 | [diff] [blame] | 4793 | /* |
| 4794 | * We guarantee to LLDs that they will have at least one |
Tejun Heo | f92a263 | 2007-12-05 16:43:10 +0900 | [diff] [blame] | 4795 | * non-zero sg if the command is a data command. |
| 4796 | */ |
Eric Biggers | 9173e5e | 2018-02-03 20:33:27 -0800 | [diff] [blame] | 4797 | if (ata_is_data(prot) && (!qc->sg || !qc->n_elem || !qc->nbytes)) |
Tejun Heo | 60f5d6e | 2010-08-23 11:27:27 +0200 | [diff] [blame] | 4798 | goto sys_err; |
Tejun Heo | f92a263 | 2007-12-05 16:43:10 +0900 | [diff] [blame] | 4799 | |
Tejun Heo | 405e66b | 2007-11-27 19:28:53 +0900 | [diff] [blame] | 4800 | if (ata_is_dma(prot) || (ata_is_pio(prot) && |
Tejun Heo | f92a263 | 2007-12-05 16:43:10 +0900 | [diff] [blame] | 4801 | (ap->flags & ATA_FLAG_PIO_DMA))) |
Tejun Heo | 001102d | 2007-12-05 16:43:09 +0900 | [diff] [blame] | 4802 | if (ata_sg_setup(qc)) |
Tejun Heo | 60f5d6e | 2010-08-23 11:27:27 +0200 | [diff] [blame] | 4803 | goto sys_err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4804 | |
Tejun Heo | cf48062 | 2008-01-24 00:05:14 +0900 | [diff] [blame] | 4805 | /* if device is sleeping, schedule reset and abort the link */ |
Tejun Heo | 054a5fb | 2007-10-25 18:30:36 +0900 | [diff] [blame] | 4806 | if (unlikely(qc->dev->flags & ATA_DFLAG_SLEEPING)) { |
Tejun Heo | cf48062 | 2008-01-24 00:05:14 +0900 | [diff] [blame] | 4807 | link->eh_info.action |= ATA_EH_RESET; |
Tejun Heo | 054a5fb | 2007-10-25 18:30:36 +0900 | [diff] [blame] | 4808 | ata_ehi_push_desc(&link->eh_info, "waking up from sleep"); |
| 4809 | ata_link_abort(link); |
| 4810 | return; |
| 4811 | } |
| 4812 | |
Jiri Slaby | 95364f3 | 2019-10-31 10:59:45 +0100 | [diff] [blame] | 4813 | qc->err_mask |= ap->ops->qc_prep(qc); |
| 4814 | if (unlikely(qc->err_mask)) |
| 4815 | goto err; |
Hannes Reinecke | 255c03d | 2015-03-27 16:46:38 +0100 | [diff] [blame] | 4816 | trace_ata_qc_issue(qc); |
Tejun Heo | 8e0e694 | 2006-03-31 20:41:11 +0900 | [diff] [blame] | 4817 | qc->err_mask |= ap->ops->qc_issue(qc); |
| 4818 | if (unlikely(qc->err_mask)) |
| 4819 | goto err; |
| 4820 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4821 | |
Tejun Heo | 60f5d6e | 2010-08-23 11:27:27 +0200 | [diff] [blame] | 4822 | sys_err: |
Tejun Heo | 8e0e694 | 2006-03-31 20:41:11 +0900 | [diff] [blame] | 4823 | qc->err_mask |= AC_ERR_SYSTEM; |
| 4824 | err: |
| 4825 | ata_qc_complete(qc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4826 | } |
| 4827 | |
| 4828 | /** |
Tejun Heo | b1c7291 | 2008-07-31 17:02:43 +0900 | [diff] [blame] | 4829 | * ata_phys_link_online - test whether the given link is online |
Tejun Heo | 936fd73 | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 4830 | * @link: ATA link to test |
Tejun Heo | 34bf217 | 2006-05-15 20:57:46 +0900 | [diff] [blame] | 4831 | * |
Tejun Heo | 936fd73 | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 4832 | * Test whether @link is online. Note that this function returns |
| 4833 | * 0 if online status of @link cannot be obtained, so |
| 4834 | * ata_link_online(link) != !ata_link_offline(link). |
Tejun Heo | 34bf217 | 2006-05-15 20:57:46 +0900 | [diff] [blame] | 4835 | * |
| 4836 | * LOCKING: |
| 4837 | * None. |
| 4838 | * |
| 4839 | * RETURNS: |
Tejun Heo | b5b3fa3 | 2008-07-31 17:02:42 +0900 | [diff] [blame] | 4840 | * True if the port online status is available and online. |
Tejun Heo | 34bf217 | 2006-05-15 20:57:46 +0900 | [diff] [blame] | 4841 | */ |
Tejun Heo | b1c7291 | 2008-07-31 17:02:43 +0900 | [diff] [blame] | 4842 | bool ata_phys_link_online(struct ata_link *link) |
Tejun Heo | 34bf217 | 2006-05-15 20:57:46 +0900 | [diff] [blame] | 4843 | { |
| 4844 | u32 sstatus; |
| 4845 | |
Tejun Heo | 936fd73 | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 4846 | if (sata_scr_read(link, SCR_STATUS, &sstatus) == 0 && |
Tejun Heo | 9913ff8a | 2009-01-29 20:31:31 +0900 | [diff] [blame] | 4847 | ata_sstatus_online(sstatus)) |
Tejun Heo | b5b3fa3 | 2008-07-31 17:02:42 +0900 | [diff] [blame] | 4848 | return true; |
| 4849 | return false; |
Tejun Heo | 34bf217 | 2006-05-15 20:57:46 +0900 | [diff] [blame] | 4850 | } |
| 4851 | |
| 4852 | /** |
Tejun Heo | b1c7291 | 2008-07-31 17:02:43 +0900 | [diff] [blame] | 4853 | * ata_phys_link_offline - test whether the given link is offline |
Tejun Heo | 936fd73 | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 4854 | * @link: ATA link to test |
Tejun Heo | 34bf217 | 2006-05-15 20:57:46 +0900 | [diff] [blame] | 4855 | * |
Tejun Heo | 936fd73 | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 4856 | * Test whether @link is offline. Note that this function |
| 4857 | * returns 0 if offline status of @link cannot be obtained, so |
| 4858 | * ata_link_online(link) != !ata_link_offline(link). |
Tejun Heo | 34bf217 | 2006-05-15 20:57:46 +0900 | [diff] [blame] | 4859 | * |
| 4860 | * LOCKING: |
| 4861 | * None. |
| 4862 | * |
| 4863 | * RETURNS: |
Tejun Heo | b5b3fa3 | 2008-07-31 17:02:42 +0900 | [diff] [blame] | 4864 | * True if the port offline status is available and offline. |
Tejun Heo | 34bf217 | 2006-05-15 20:57:46 +0900 | [diff] [blame] | 4865 | */ |
Tejun Heo | b1c7291 | 2008-07-31 17:02:43 +0900 | [diff] [blame] | 4866 | bool ata_phys_link_offline(struct ata_link *link) |
Tejun Heo | 34bf217 | 2006-05-15 20:57:46 +0900 | [diff] [blame] | 4867 | { |
| 4868 | u32 sstatus; |
| 4869 | |
Tejun Heo | 936fd73 | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 4870 | if (sata_scr_read(link, SCR_STATUS, &sstatus) == 0 && |
Tejun Heo | 9913ff8a | 2009-01-29 20:31:31 +0900 | [diff] [blame] | 4871 | !ata_sstatus_online(sstatus)) |
Tejun Heo | b5b3fa3 | 2008-07-31 17:02:42 +0900 | [diff] [blame] | 4872 | return true; |
| 4873 | return false; |
Tejun Heo | 34bf217 | 2006-05-15 20:57:46 +0900 | [diff] [blame] | 4874 | } |
Edward Falk | 0baab86 | 2005-06-02 18:17:13 -0400 | [diff] [blame] | 4875 | |
Tejun Heo | b1c7291 | 2008-07-31 17:02:43 +0900 | [diff] [blame] | 4876 | /** |
| 4877 | * ata_link_online - test whether the given link is online |
| 4878 | * @link: ATA link to test |
| 4879 | * |
| 4880 | * Test whether @link is online. This is identical to |
| 4881 | * ata_phys_link_online() when there's no slave link. When |
| 4882 | * there's a slave link, this function should only be called on |
| 4883 | * the master link and will return true if any of M/S links is |
| 4884 | * online. |
| 4885 | * |
| 4886 | * LOCKING: |
| 4887 | * None. |
| 4888 | * |
| 4889 | * RETURNS: |
| 4890 | * True if the port online status is available and online. |
| 4891 | */ |
| 4892 | bool ata_link_online(struct ata_link *link) |
| 4893 | { |
| 4894 | struct ata_link *slave = link->ap->slave_link; |
| 4895 | |
| 4896 | WARN_ON(link == slave); /* shouldn't be called on slave link */ |
| 4897 | |
| 4898 | return ata_phys_link_online(link) || |
| 4899 | (slave && ata_phys_link_online(slave)); |
| 4900 | } |
Bartlomiej Zolnierkiewicz | a52fbcf | 2020-03-26 16:58:04 +0100 | [diff] [blame] | 4901 | EXPORT_SYMBOL_GPL(ata_link_online); |
Tejun Heo | b1c7291 | 2008-07-31 17:02:43 +0900 | [diff] [blame] | 4902 | |
| 4903 | /** |
| 4904 | * ata_link_offline - test whether the given link is offline |
| 4905 | * @link: ATA link to test |
| 4906 | * |
| 4907 | * Test whether @link is offline. This is identical to |
| 4908 | * ata_phys_link_offline() when there's no slave link. When |
| 4909 | * there's a slave link, this function should only be called on |
| 4910 | * the master link and will return true if both M/S links are |
| 4911 | * offline. |
| 4912 | * |
| 4913 | * LOCKING: |
| 4914 | * None. |
| 4915 | * |
| 4916 | * RETURNS: |
| 4917 | * True if the port offline status is available and offline. |
| 4918 | */ |
| 4919 | bool ata_link_offline(struct ata_link *link) |
| 4920 | { |
| 4921 | struct ata_link *slave = link->ap->slave_link; |
| 4922 | |
| 4923 | WARN_ON(link == slave); /* shouldn't be called on slave link */ |
| 4924 | |
| 4925 | return ata_phys_link_offline(link) && |
| 4926 | (!slave || ata_phys_link_offline(slave)); |
| 4927 | } |
Bartlomiej Zolnierkiewicz | a52fbcf | 2020-03-26 16:58:04 +0100 | [diff] [blame] | 4928 | EXPORT_SYMBOL_GPL(ata_link_offline); |
Tejun Heo | b1c7291 | 2008-07-31 17:02:43 +0900 | [diff] [blame] | 4929 | |
Tejun Heo | 6ffa01d | 2007-03-02 17:32:47 +0900 | [diff] [blame] | 4930 | #ifdef CONFIG_PM |
Dan Williams | bc6e7c4 | 2014-03-14 13:52:48 -0700 | [diff] [blame] | 4931 | static void ata_port_request_pm(struct ata_port *ap, pm_message_t mesg, |
| 4932 | unsigned int action, unsigned int ehi_flags, |
| 4933 | bool async) |
Tejun Heo | 500530f | 2006-07-03 16:07:27 +0900 | [diff] [blame] | 4934 | { |
Lin Ming | 5ef4108 | 2011-12-05 09:20:27 +0800 | [diff] [blame] | 4935 | struct ata_link *link; |
Tejun Heo | 500530f | 2006-07-03 16:07:27 +0900 | [diff] [blame] | 4936 | unsigned long flags; |
Tejun Heo | 500530f | 2006-07-03 16:07:27 +0900 | [diff] [blame] | 4937 | |
Lin Ming | 5ef4108 | 2011-12-05 09:20:27 +0800 | [diff] [blame] | 4938 | /* Previous resume operation might still be in |
| 4939 | * progress. Wait for PM_PENDING to clear. |
| 4940 | */ |
| 4941 | if (ap->pflags & ATA_PFLAG_PM_PENDING) { |
| 4942 | ata_port_wait_eh(ap); |
| 4943 | WARN_ON(ap->pflags & ATA_PFLAG_PM_PENDING); |
Tejun Heo | 500530f | 2006-07-03 16:07:27 +0900 | [diff] [blame] | 4944 | } |
| 4945 | |
Lin Ming | 5ef4108 | 2011-12-05 09:20:27 +0800 | [diff] [blame] | 4946 | /* request PM ops to EH */ |
| 4947 | spin_lock_irqsave(ap->lock, flags); |
| 4948 | |
| 4949 | ap->pm_mesg = mesg; |
Lin Ming | 5ef4108 | 2011-12-05 09:20:27 +0800 | [diff] [blame] | 4950 | ap->pflags |= ATA_PFLAG_PM_PENDING; |
| 4951 | ata_for_each_link(link, ap, HOST_FIRST) { |
| 4952 | link->eh_info.action |= action; |
| 4953 | link->eh_info.flags |= ehi_flags; |
| 4954 | } |
| 4955 | |
| 4956 | ata_port_schedule_eh(ap); |
| 4957 | |
| 4958 | spin_unlock_irqrestore(ap->lock, flags); |
| 4959 | |
Dan Williams | 2fcbdcb | 2012-06-21 23:41:46 -0700 | [diff] [blame] | 4960 | if (!async) { |
Lin Ming | 5ef4108 | 2011-12-05 09:20:27 +0800 | [diff] [blame] | 4961 | ata_port_wait_eh(ap); |
| 4962 | WARN_ON(ap->pflags & ATA_PFLAG_PM_PENDING); |
| 4963 | } |
Tejun Heo | 500530f | 2006-07-03 16:07:27 +0900 | [diff] [blame] | 4964 | } |
| 4965 | |
Dan Williams | bc6e7c4 | 2014-03-14 13:52:48 -0700 | [diff] [blame] | 4966 | /* |
| 4967 | * On some hardware, device fails to respond after spun down for suspend. As |
| 4968 | * the device won't be used before being resumed, we don't need to touch the |
| 4969 | * device. Ask EH to skip the usual stuff and proceed directly to suspend. |
| 4970 | * |
| 4971 | * http://thread.gmane.org/gmane.linux.ide/46764 |
| 4972 | */ |
| 4973 | static const unsigned int ata_port_suspend_ehi = ATA_EHI_QUIET |
| 4974 | | ATA_EHI_NO_AUTOPSY |
| 4975 | | ATA_EHI_NO_RECOVERY; |
| 4976 | |
| 4977 | 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] | 4978 | { |
Dan Williams | bc6e7c4 | 2014-03-14 13:52:48 -0700 | [diff] [blame] | 4979 | ata_port_request_pm(ap, mesg, 0, ata_port_suspend_ehi, false); |
Lin Ming | 5ef4108 | 2011-12-05 09:20:27 +0800 | [diff] [blame] | 4980 | } |
| 4981 | |
Dan Williams | bc6e7c4 | 2014-03-14 13:52:48 -0700 | [diff] [blame] | 4982 | static void ata_port_suspend_async(struct ata_port *ap, pm_message_t mesg) |
| 4983 | { |
| 4984 | ata_port_request_pm(ap, mesg, 0, ata_port_suspend_ehi, true); |
| 4985 | } |
| 4986 | |
| 4987 | static int ata_port_pm_suspend(struct device *dev) |
Dan Williams | 2fcbdcb | 2012-06-21 23:41:46 -0700 | [diff] [blame] | 4988 | { |
| 4989 | struct ata_port *ap = to_ata_port(dev); |
| 4990 | |
Lin Ming | 5ef4108 | 2011-12-05 09:20:27 +0800 | [diff] [blame] | 4991 | if (pm_runtime_suspended(dev)) |
| 4992 | return 0; |
| 4993 | |
Dan Williams | bc6e7c4 | 2014-03-14 13:52:48 -0700 | [diff] [blame] | 4994 | ata_port_suspend(ap, PMSG_SUSPEND); |
| 4995 | return 0; |
Lin Ming | 33574d6 | 2011-12-22 14:50:49 +0800 | [diff] [blame] | 4996 | } |
| 4997 | |
Dan Williams | bc6e7c4 | 2014-03-14 13:52:48 -0700 | [diff] [blame] | 4998 | static int ata_port_pm_freeze(struct device *dev) |
Dan Williams | 2fcbdcb | 2012-06-21 23:41:46 -0700 | [diff] [blame] | 4999 | { |
| 5000 | struct ata_port *ap = to_ata_port(dev); |
| 5001 | |
Dan Williams | bc6e7c4 | 2014-03-14 13:52:48 -0700 | [diff] [blame] | 5002 | if (pm_runtime_suspended(dev)) |
| 5003 | return 0; |
| 5004 | |
| 5005 | ata_port_suspend(ap, PMSG_FREEZE); |
| 5006 | return 0; |
Dan Williams | 2fcbdcb | 2012-06-21 23:41:46 -0700 | [diff] [blame] | 5007 | } |
| 5008 | |
Dan Williams | bc6e7c4 | 2014-03-14 13:52:48 -0700 | [diff] [blame] | 5009 | static int ata_port_pm_poweroff(struct device *dev) |
Lin Ming | e90b1e5 | 2011-12-22 14:50:48 +0800 | [diff] [blame] | 5010 | { |
Dan Williams | bc6e7c4 | 2014-03-14 13:52:48 -0700 | [diff] [blame] | 5011 | ata_port_suspend(to_ata_port(dev), PMSG_HIBERNATE); |
| 5012 | return 0; |
| 5013 | } |
Lin Ming | e90b1e5 | 2011-12-22 14:50:48 +0800 | [diff] [blame] | 5014 | |
Dan Williams | bc6e7c4 | 2014-03-14 13:52:48 -0700 | [diff] [blame] | 5015 | static const unsigned int ata_port_resume_ehi = ATA_EHI_NO_AUTOPSY |
| 5016 | | ATA_EHI_QUIET; |
Lin Ming | e90b1e5 | 2011-12-22 14:50:48 +0800 | [diff] [blame] | 5017 | |
Dan Williams | bc6e7c4 | 2014-03-14 13:52:48 -0700 | [diff] [blame] | 5018 | static void ata_port_resume(struct ata_port *ap, pm_message_t mesg) |
| 5019 | { |
| 5020 | ata_port_request_pm(ap, mesg, ATA_EH_RESET, ata_port_resume_ehi, false); |
| 5021 | } |
| 5022 | |
| 5023 | static void ata_port_resume_async(struct ata_port *ap, pm_message_t mesg) |
| 5024 | { |
| 5025 | ata_port_request_pm(ap, mesg, ATA_EH_RESET, ata_port_resume_ehi, true); |
| 5026 | } |
| 5027 | |
| 5028 | static int ata_port_pm_resume(struct device *dev) |
| 5029 | { |
Todd Brandt | 200421a | 2014-03-14 13:52:54 -0700 | [diff] [blame] | 5030 | ata_port_resume_async(to_ata_port(dev), PMSG_RESUME); |
Dan Williams | bc6e7c4 | 2014-03-14 13:52:48 -0700 | [diff] [blame] | 5031 | pm_runtime_disable(dev); |
| 5032 | pm_runtime_set_active(dev); |
| 5033 | pm_runtime_enable(dev); |
| 5034 | return 0; |
Lin Ming | e90b1e5 | 2011-12-22 14:50:48 +0800 | [diff] [blame] | 5035 | } |
| 5036 | |
Aaron Lu | 7e15e9b | 2013-01-15 17:21:04 +0800 | [diff] [blame] | 5037 | /* |
| 5038 | * For ODDs, the upper layer will poll for media change every few seconds, |
| 5039 | * which will make it enter and leave suspend state every few seconds. And |
| 5040 | * as each suspend will cause a hard/soft reset, the gain of runtime suspend |
| 5041 | * is very little and the ODD may malfunction after constantly being reset. |
| 5042 | * So the idle callback here will not proceed to suspend if a non-ZPODD capable |
| 5043 | * ODD is attached to the port. |
| 5044 | */ |
Lin Ming | 9ee4f39 | 2011-12-05 09:20:28 +0800 | [diff] [blame] | 5045 | static int ata_port_runtime_idle(struct device *dev) |
| 5046 | { |
Aaron Lu | 7e15e9b | 2013-01-15 17:21:04 +0800 | [diff] [blame] | 5047 | struct ata_port *ap = to_ata_port(dev); |
| 5048 | struct ata_link *link; |
| 5049 | struct ata_device *adev; |
| 5050 | |
| 5051 | ata_for_each_link(link, ap, HOST_FIRST) { |
| 5052 | ata_for_each_dev(adev, link, ENABLED) |
| 5053 | if (adev->class == ATA_DEV_ATAPI && |
| 5054 | !zpodd_dev_enabled(adev)) |
| 5055 | return -EBUSY; |
| 5056 | } |
| 5057 | |
Rafael J. Wysocki | 45f0a85 | 2013-06-03 21:49:52 +0200 | [diff] [blame] | 5058 | return 0; |
Lin Ming | 9ee4f39 | 2011-12-05 09:20:28 +0800 | [diff] [blame] | 5059 | } |
| 5060 | |
Aaron Lu | a7ff60d | 2013-01-25 14:29:35 +0800 | [diff] [blame] | 5061 | static int ata_port_runtime_suspend(struct device *dev) |
| 5062 | { |
Dan Williams | bc6e7c4 | 2014-03-14 13:52:48 -0700 | [diff] [blame] | 5063 | ata_port_suspend(to_ata_port(dev), PMSG_AUTO_SUSPEND); |
| 5064 | return 0; |
Aaron Lu | a7ff60d | 2013-01-25 14:29:35 +0800 | [diff] [blame] | 5065 | } |
| 5066 | |
| 5067 | static int ata_port_runtime_resume(struct device *dev) |
| 5068 | { |
Dan Williams | bc6e7c4 | 2014-03-14 13:52:48 -0700 | [diff] [blame] | 5069 | ata_port_resume(to_ata_port(dev), PMSG_AUTO_RESUME); |
| 5070 | return 0; |
Aaron Lu | a7ff60d | 2013-01-25 14:29:35 +0800 | [diff] [blame] | 5071 | } |
| 5072 | |
Lin Ming | 5ef4108 | 2011-12-05 09:20:27 +0800 | [diff] [blame] | 5073 | static const struct dev_pm_ops ata_port_pm_ops = { |
Dan Williams | bc6e7c4 | 2014-03-14 13:52:48 -0700 | [diff] [blame] | 5074 | .suspend = ata_port_pm_suspend, |
| 5075 | .resume = ata_port_pm_resume, |
| 5076 | .freeze = ata_port_pm_freeze, |
| 5077 | .thaw = ata_port_pm_resume, |
| 5078 | .poweroff = ata_port_pm_poweroff, |
| 5079 | .restore = ata_port_pm_resume, |
Lin Ming | 9ee4f39 | 2011-12-05 09:20:28 +0800 | [diff] [blame] | 5080 | |
Aaron Lu | a7ff60d | 2013-01-25 14:29:35 +0800 | [diff] [blame] | 5081 | .runtime_suspend = ata_port_runtime_suspend, |
| 5082 | .runtime_resume = ata_port_runtime_resume, |
Lin Ming | 9ee4f39 | 2011-12-05 09:20:28 +0800 | [diff] [blame] | 5083 | .runtime_idle = ata_port_runtime_idle, |
Lin Ming | 5ef4108 | 2011-12-05 09:20:27 +0800 | [diff] [blame] | 5084 | }; |
| 5085 | |
Dan Williams | 2fcbdcb | 2012-06-21 23:41:46 -0700 | [diff] [blame] | 5086 | /* sas ports don't participate in pm runtime management of ata_ports, |
| 5087 | * and need to resume ata devices at the domain level, not the per-port |
| 5088 | * level. sas suspend/resume is async to allow parallel port recovery |
| 5089 | * since sas has multiple ata_port instances per Scsi_Host. |
| 5090 | */ |
Dan Williams | bc6e7c4 | 2014-03-14 13:52:48 -0700 | [diff] [blame] | 5091 | void ata_sas_port_suspend(struct ata_port *ap) |
Dan Williams | 2fcbdcb | 2012-06-21 23:41:46 -0700 | [diff] [blame] | 5092 | { |
Dan Williams | bc6e7c4 | 2014-03-14 13:52:48 -0700 | [diff] [blame] | 5093 | ata_port_suspend_async(ap, PMSG_SUSPEND); |
Dan Williams | 2fcbdcb | 2012-06-21 23:41:46 -0700 | [diff] [blame] | 5094 | } |
Dan Williams | bc6e7c4 | 2014-03-14 13:52:48 -0700 | [diff] [blame] | 5095 | EXPORT_SYMBOL_GPL(ata_sas_port_suspend); |
Dan Williams | 2fcbdcb | 2012-06-21 23:41:46 -0700 | [diff] [blame] | 5096 | |
Dan Williams | bc6e7c4 | 2014-03-14 13:52:48 -0700 | [diff] [blame] | 5097 | void ata_sas_port_resume(struct ata_port *ap) |
Dan Williams | 2fcbdcb | 2012-06-21 23:41:46 -0700 | [diff] [blame] | 5098 | { |
Dan Williams | bc6e7c4 | 2014-03-14 13:52:48 -0700 | [diff] [blame] | 5099 | ata_port_resume_async(ap, PMSG_RESUME); |
Dan Williams | 2fcbdcb | 2012-06-21 23:41:46 -0700 | [diff] [blame] | 5100 | } |
Dan Williams | bc6e7c4 | 2014-03-14 13:52:48 -0700 | [diff] [blame] | 5101 | EXPORT_SYMBOL_GPL(ata_sas_port_resume); |
Dan Williams | 2fcbdcb | 2012-06-21 23:41:46 -0700 | [diff] [blame] | 5102 | |
Tejun Heo | 500530f | 2006-07-03 16:07:27 +0900 | [diff] [blame] | 5103 | /** |
Jeff Garzik | cca3974 | 2006-08-24 03:19:22 -0400 | [diff] [blame] | 5104 | * ata_host_suspend - suspend host |
| 5105 | * @host: host to suspend |
Tejun Heo | 500530f | 2006-07-03 16:07:27 +0900 | [diff] [blame] | 5106 | * @mesg: PM message |
| 5107 | * |
Lin Ming | 5ef4108 | 2011-12-05 09:20:27 +0800 | [diff] [blame] | 5108 | * Suspend @host. Actual operation is performed by port suspend. |
Tejun Heo | 500530f | 2006-07-03 16:07:27 +0900 | [diff] [blame] | 5109 | */ |
Jeff Garzik | cca3974 | 2006-08-24 03:19:22 -0400 | [diff] [blame] | 5110 | int ata_host_suspend(struct ata_host *host, pm_message_t mesg) |
Tejun Heo | 500530f | 2006-07-03 16:07:27 +0900 | [diff] [blame] | 5111 | { |
Lin Ming | 5ef4108 | 2011-12-05 09:20:27 +0800 | [diff] [blame] | 5112 | host->dev->power.power_state = mesg; |
| 5113 | return 0; |
Tejun Heo | 500530f | 2006-07-03 16:07:27 +0900 | [diff] [blame] | 5114 | } |
Bartlomiej Zolnierkiewicz | a52fbcf | 2020-03-26 16:58:04 +0100 | [diff] [blame] | 5115 | EXPORT_SYMBOL_GPL(ata_host_suspend); |
Tejun Heo | 500530f | 2006-07-03 16:07:27 +0900 | [diff] [blame] | 5116 | |
| 5117 | /** |
Jeff Garzik | cca3974 | 2006-08-24 03:19:22 -0400 | [diff] [blame] | 5118 | * ata_host_resume - resume host |
| 5119 | * @host: host to resume |
Tejun Heo | 500530f | 2006-07-03 16:07:27 +0900 | [diff] [blame] | 5120 | * |
Lin Ming | 5ef4108 | 2011-12-05 09:20:27 +0800 | [diff] [blame] | 5121 | * Resume @host. Actual operation is performed by port resume. |
Tejun Heo | 500530f | 2006-07-03 16:07:27 +0900 | [diff] [blame] | 5122 | */ |
Jeff Garzik | cca3974 | 2006-08-24 03:19:22 -0400 | [diff] [blame] | 5123 | void ata_host_resume(struct ata_host *host) |
Tejun Heo | 500530f | 2006-07-03 16:07:27 +0900 | [diff] [blame] | 5124 | { |
Jeff Garzik | 72ad6ec | 2008-02-25 17:31:10 -0500 | [diff] [blame] | 5125 | host->dev->power.power_state = PMSG_ON; |
Tejun Heo | 500530f | 2006-07-03 16:07:27 +0900 | [diff] [blame] | 5126 | } |
Bartlomiej Zolnierkiewicz | a52fbcf | 2020-03-26 16:58:04 +0100 | [diff] [blame] | 5127 | EXPORT_SYMBOL_GPL(ata_host_resume); |
Tejun Heo | 6ffa01d | 2007-03-02 17:32:47 +0900 | [diff] [blame] | 5128 | #endif |
Tejun Heo | 500530f | 2006-07-03 16:07:27 +0900 | [diff] [blame] | 5129 | |
Bhumika Goyal | 8df82c1 | 2017-09-30 22:10:40 +0530 | [diff] [blame] | 5130 | const struct device_type ata_port_type = { |
Lin Ming | 5ef4108 | 2011-12-05 09:20:27 +0800 | [diff] [blame] | 5131 | .name = "ata_port", |
| 5132 | #ifdef CONFIG_PM |
| 5133 | .pm = &ata_port_pm_ops, |
| 5134 | #endif |
| 5135 | }; |
| 5136 | |
Randy Dunlap | c893a3a | 2006-01-28 13:15:32 -0500 | [diff] [blame] | 5137 | /** |
Tejun Heo | 3ef3b43 | 2006-05-31 18:27:30 +0900 | [diff] [blame] | 5138 | * ata_dev_init - Initialize an ata_device structure |
| 5139 | * @dev: Device structure to initialize |
| 5140 | * |
| 5141 | * Initialize @dev in preparation for probing. |
| 5142 | * |
| 5143 | * LOCKING: |
| 5144 | * Inherited from caller. |
| 5145 | */ |
| 5146 | void ata_dev_init(struct ata_device *dev) |
| 5147 | { |
Tejun Heo | b1c7291 | 2008-07-31 17:02:43 +0900 | [diff] [blame] | 5148 | struct ata_link *link = ata_dev_phys_link(dev); |
Tejun Heo | 9af5c9c | 2007-08-06 18:36:22 +0900 | [diff] [blame] | 5149 | struct ata_port *ap = link->ap; |
Tejun Heo | 72fa4b7 | 2006-05-31 18:27:32 +0900 | [diff] [blame] | 5150 | unsigned long flags; |
Tejun Heo | 3ef3b43 | 2006-05-31 18:27:30 +0900 | [diff] [blame] | 5151 | |
Tejun Heo | b1c7291 | 2008-07-31 17:02:43 +0900 | [diff] [blame] | 5152 | /* SATA spd limit is bound to the attached device, reset together */ |
Tejun Heo | 9af5c9c | 2007-08-06 18:36:22 +0900 | [diff] [blame] | 5153 | link->sata_spd_limit = link->hw_sata_spd_limit; |
| 5154 | link->sata_spd = 0; |
Tejun Heo | 5a04bf4 | 2006-05-31 18:27:38 +0900 | [diff] [blame] | 5155 | |
Tejun Heo | 72fa4b7 | 2006-05-31 18:27:32 +0900 | [diff] [blame] | 5156 | /* High bits of dev->flags are used to record warm plug |
| 5157 | * requests which occur asynchronously. Synchronize using |
Jeff Garzik | cca3974 | 2006-08-24 03:19:22 -0400 | [diff] [blame] | 5158 | * host lock. |
Tejun Heo | 72fa4b7 | 2006-05-31 18:27:32 +0900 | [diff] [blame] | 5159 | */ |
Jeff Garzik | ba6a130 | 2006-06-22 23:46:10 -0400 | [diff] [blame] | 5160 | spin_lock_irqsave(ap->lock, flags); |
Tejun Heo | 72fa4b7 | 2006-05-31 18:27:32 +0900 | [diff] [blame] | 5161 | dev->flags &= ~ATA_DFLAG_INIT_MASK; |
Tejun Heo | 3dcc323 | 2007-09-03 12:20:11 +0900 | [diff] [blame] | 5162 | dev->horkage = 0; |
Jeff Garzik | ba6a130 | 2006-06-22 23:46:10 -0400 | [diff] [blame] | 5163 | spin_unlock_irqrestore(ap->lock, flags); |
Tejun Heo | 72fa4b7 | 2006-05-31 18:27:32 +0900 | [diff] [blame] | 5164 | |
Tejun Heo | 99cf610 | 2009-01-29 20:31:32 +0900 | [diff] [blame] | 5165 | memset((void *)dev + ATA_DEVICE_CLEAR_BEGIN, 0, |
| 5166 | ATA_DEVICE_CLEAR_END - ATA_DEVICE_CLEAR_BEGIN); |
Tejun Heo | 3ef3b43 | 2006-05-31 18:27:30 +0900 | [diff] [blame] | 5167 | dev->pio_mask = UINT_MAX; |
| 5168 | dev->mwdma_mask = UINT_MAX; |
| 5169 | dev->udma_mask = UINT_MAX; |
| 5170 | } |
| 5171 | |
| 5172 | /** |
Tejun Heo | 4fb37a2 | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 5173 | * ata_link_init - Initialize an ata_link structure |
| 5174 | * @ap: ATA port link is attached to |
| 5175 | * @link: Link structure to initialize |
Tejun Heo | 8989805 | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 5176 | * @pmp: Port multiplier port number |
Tejun Heo | 4fb37a2 | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 5177 | * |
| 5178 | * Initialize @link. |
| 5179 | * |
| 5180 | * LOCKING: |
| 5181 | * Kernel thread context (may sleep) |
| 5182 | */ |
Tejun Heo | fb7fd61 | 2007-09-23 13:14:12 +0900 | [diff] [blame] | 5183 | 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] | 5184 | { |
| 5185 | int i; |
| 5186 | |
| 5187 | /* clear everything except for devices */ |
Gwendal Grignou | d902747 | 2010-05-25 12:31:38 -0700 | [diff] [blame] | 5188 | memset((void *)link + ATA_LINK_CLEAR_BEGIN, 0, |
| 5189 | ATA_LINK_CLEAR_END - ATA_LINK_CLEAR_BEGIN); |
Tejun Heo | 4fb37a2 | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 5190 | |
| 5191 | link->ap = ap; |
Tejun Heo | 8989805 | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 5192 | link->pmp = pmp; |
Tejun Heo | 4fb37a2 | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 5193 | link->active_tag = ATA_TAG_POISON; |
| 5194 | link->hw_sata_spd_limit = UINT_MAX; |
| 5195 | |
| 5196 | /* can't use iterator, ap isn't initialized yet */ |
| 5197 | for (i = 0; i < ATA_MAX_DEVICES; i++) { |
| 5198 | struct ata_device *dev = &link->device[i]; |
| 5199 | |
| 5200 | dev->link = link; |
| 5201 | dev->devno = dev - link->device; |
Tejun Heo | 110f66d | 2009-09-16 04:17:28 +0900 | [diff] [blame] | 5202 | #ifdef CONFIG_ATA_ACPI |
| 5203 | dev->gtf_filter = ata_acpi_gtf_filter; |
| 5204 | #endif |
Tejun Heo | 4fb37a2 | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 5205 | ata_dev_init(dev); |
| 5206 | } |
| 5207 | } |
| 5208 | |
| 5209 | /** |
| 5210 | * sata_link_init_spd - Initialize link->sata_spd_limit |
| 5211 | * @link: Link to configure sata_spd_limit for |
| 5212 | * |
Mauro Carvalho Chehab | a31a699 | 2020-04-14 18:48:55 +0200 | [diff] [blame] | 5213 | * Initialize ``link->[hw_]sata_spd_limit`` to the currently |
Tejun Heo | 4fb37a2 | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 5214 | * configured value. |
| 5215 | * |
| 5216 | * LOCKING: |
| 5217 | * Kernel thread context (may sleep). |
| 5218 | * |
| 5219 | * RETURNS: |
| 5220 | * 0 on success, -errno on failure. |
| 5221 | */ |
Tejun Heo | fb7fd61 | 2007-09-23 13:14:12 +0900 | [diff] [blame] | 5222 | int sata_link_init_spd(struct ata_link *link) |
Tejun Heo | 4fb37a2 | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 5223 | { |
Tejun Heo | 3326732 | 2008-02-13 09:15:09 +0900 | [diff] [blame] | 5224 | u8 spd; |
Tejun Heo | 4fb37a2 | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 5225 | int rc; |
| 5226 | |
Tejun Heo | d127ea7 | 2008-07-31 16:09:34 +0900 | [diff] [blame] | 5227 | rc = sata_scr_read(link, SCR_CONTROL, &link->saved_scontrol); |
Tejun Heo | 4fb37a2 | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 5228 | if (rc) |
| 5229 | return rc; |
| 5230 | |
Tejun Heo | d127ea7 | 2008-07-31 16:09:34 +0900 | [diff] [blame] | 5231 | spd = (link->saved_scontrol >> 4) & 0xf; |
Tejun Heo | 4fb37a2 | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 5232 | if (spd) |
| 5233 | link->hw_sata_spd_limit &= (1 << spd) - 1; |
| 5234 | |
Tejun Heo | 05944bd | 2008-08-13 20:19:09 +0900 | [diff] [blame] | 5235 | ata_force_link_limits(link); |
Tejun Heo | 3326732 | 2008-02-13 09:15:09 +0900 | [diff] [blame] | 5236 | |
Tejun Heo | 4fb37a2 | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 5237 | link->sata_spd_limit = link->hw_sata_spd_limit; |
| 5238 | |
| 5239 | return 0; |
| 5240 | } |
| 5241 | |
| 5242 | /** |
Tejun Heo | f318719 | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 5243 | * ata_port_alloc - allocate and initialize basic ATA port resources |
| 5244 | * @host: ATA host this allocated port belongs to |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5245 | * |
Tejun Heo | f318719 | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 5246 | * Allocate and initialize basic ATA port resources. |
| 5247 | * |
| 5248 | * RETURNS: |
| 5249 | * Allocate ATA port on success, NULL on failure. |
Jeff Garzik | 0cba632 | 2005-05-30 19:49:12 -0400 | [diff] [blame] | 5250 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5251 | * LOCKING: |
Tejun Heo | f318719 | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 5252 | * Inherited from calling layer (may sleep). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5253 | */ |
Tejun Heo | f318719 | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 5254 | struct ata_port *ata_port_alloc(struct ata_host *host) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5255 | { |
Tejun Heo | f318719 | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 5256 | struct ata_port *ap; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5257 | |
Tejun Heo | f318719 | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 5258 | DPRINTK("ENTER\n"); |
| 5259 | |
| 5260 | ap = kzalloc(sizeof(*ap), GFP_KERNEL); |
| 5261 | if (!ap) |
| 5262 | return NULL; |
Jeff Garzik | 4fca377 | 2011-02-15 01:13:24 -0500 | [diff] [blame] | 5263 | |
Maxime Bizon | 7b3a24c5 | 2011-03-16 14:58:32 +0100 | [diff] [blame] | 5264 | ap->pflags |= ATA_PFLAG_INITIALIZING | ATA_PFLAG_FROZEN; |
Jeff Garzik | cca3974 | 2006-08-24 03:19:22 -0400 | [diff] [blame] | 5265 | ap->lock = &host->lock; |
Tejun Heo | f318719 | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 5266 | ap->print_id = -1; |
David Milburn | e628dc9 | 2013-05-14 13:48:40 -0500 | [diff] [blame] | 5267 | ap->local_port_no = -1; |
Jeff Garzik | cca3974 | 2006-08-24 03:19:22 -0400 | [diff] [blame] | 5268 | ap->host = host; |
Tejun Heo | f318719 | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 5269 | ap->dev = host->dev; |
Borislav Petkov | bd5d825 | 2006-06-11 23:17:01 -0400 | [diff] [blame] | 5270 | |
| 5271 | #if defined(ATA_VERBOSE_DEBUG) |
| 5272 | /* turn on all debugging levels */ |
| 5273 | ap->msg_enable = 0x00FF; |
| 5274 | #elif defined(ATA_DEBUG) |
| 5275 | ap->msg_enable = ATA_MSG_DRV | ATA_MSG_INFO | ATA_MSG_CTL | ATA_MSG_WARN | ATA_MSG_ERR; |
Tejun Heo | 8857455 | 2006-06-25 20:00:35 +0900 | [diff] [blame] | 5276 | #else |
Borislav Petkov | 0dd4b21 | 2006-06-23 02:29:08 -0400 | [diff] [blame] | 5277 | ap->msg_enable = ATA_MSG_DRV | ATA_MSG_ERR | ATA_MSG_WARN; |
Borislav Petkov | bd5d825 | 2006-06-11 23:17:01 -0400 | [diff] [blame] | 5278 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5279 | |
Tejun Heo | ad72cf9 | 2010-07-02 10:03:52 +0200 | [diff] [blame] | 5280 | mutex_init(&ap->scsi_scan_mutex); |
David Howells | 65f27f3 | 2006-11-22 14:55:48 +0000 | [diff] [blame] | 5281 | INIT_DELAYED_WORK(&ap->hotplug_task, ata_scsi_hotplug); |
| 5282 | INIT_WORK(&ap->scsi_rescan_task, ata_scsi_dev_rescan); |
Tejun Heo | a72ec4c | 2006-01-23 13:09:37 +0900 | [diff] [blame] | 5283 | INIT_LIST_HEAD(&ap->eh_done_q); |
Tejun Heo | c6cf9e9 | 2006-05-31 18:27:27 +0900 | [diff] [blame] | 5284 | init_waitqueue_head(&ap->eh_wait_q); |
Elias Oltmanns | 45fabbb | 2008-09-21 11:54:08 +0200 | [diff] [blame] | 5285 | init_completion(&ap->park_req_pending); |
Kees Cook | b93ab33 | 2017-10-16 14:56:42 -0700 | [diff] [blame] | 5286 | timer_setup(&ap->fastdrain_timer, ata_eh_fastdrain_timerfn, |
| 5287 | TIMER_DEFERRABLE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5288 | |
Tejun Heo | 838df62 | 2006-05-15 20:57:44 +0900 | [diff] [blame] | 5289 | ap->cbl = ATA_CBL_NONE; |
Tejun Heo | 838df62 | 2006-05-15 20:57:44 +0900 | [diff] [blame] | 5290 | |
Tejun Heo | 8989805 | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 5291 | ata_link_init(ap, &ap->link, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5292 | |
| 5293 | #ifdef ATA_IRQ_TRAP |
| 5294 | ap->stats.unhandled_irq = 1; |
| 5295 | ap->stats.idle_irq = 1; |
| 5296 | #endif |
Tejun Heo | 270390e | 2010-05-10 21:41:35 +0200 | [diff] [blame] | 5297 | ata_sff_port_init(ap); |
| 5298 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5299 | return ap; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5300 | } |
| 5301 | |
Taras Kondratiuk | 2623c7a | 2018-03-09 08:34:41 +0000 | [diff] [blame] | 5302 | static void ata_devres_release(struct device *gendev, void *res) |
Tejun Heo | f0d36ef | 2007-01-20 16:00:28 +0900 | [diff] [blame] | 5303 | { |
| 5304 | struct ata_host *host = dev_get_drvdata(gendev); |
| 5305 | int i; |
| 5306 | |
| 5307 | for (i = 0; i < host->n_ports; i++) { |
| 5308 | struct ata_port *ap = host->ports[i]; |
| 5309 | |
Tejun Heo | ecef725 | 2007-04-17 23:44:06 +0900 | [diff] [blame] | 5310 | if (!ap) |
| 5311 | continue; |
| 5312 | |
Tejun Heo | 4911487 | 2007-04-17 23:44:06 +0900 | [diff] [blame] | 5313 | if (ap->scsi_host) |
Tejun Heo | 1aa506e4 | 2007-03-09 19:36:12 +0900 | [diff] [blame] | 5314 | scsi_host_put(ap->scsi_host); |
| 5315 | |
Taras Kondratiuk | 2623c7a | 2018-03-09 08:34:41 +0000 | [diff] [blame] | 5316 | } |
| 5317 | |
| 5318 | dev_set_drvdata(gendev, NULL); |
| 5319 | ata_host_put(host); |
| 5320 | } |
| 5321 | |
| 5322 | static void ata_host_release(struct kref *kref) |
| 5323 | { |
| 5324 | struct ata_host *host = container_of(kref, struct ata_host, kref); |
| 5325 | int i; |
| 5326 | |
| 5327 | for (i = 0; i < host->n_ports; i++) { |
| 5328 | struct ata_port *ap = host->ports[i]; |
| 5329 | |
Tejun Heo | 633273a | 2007-09-23 13:19:54 +0900 | [diff] [blame] | 5330 | kfree(ap->pmp_link); |
Tejun Heo | b1c7291 | 2008-07-31 17:02:43 +0900 | [diff] [blame] | 5331 | kfree(ap->slave_link); |
Tejun Heo | 4911487 | 2007-04-17 23:44:06 +0900 | [diff] [blame] | 5332 | kfree(ap); |
Tejun Heo | 1aa506e4 | 2007-03-09 19:36:12 +0900 | [diff] [blame] | 5333 | host->ports[i] = NULL; |
| 5334 | } |
Taras Kondratiuk | 2623c7a | 2018-03-09 08:34:41 +0000 | [diff] [blame] | 5335 | kfree(host); |
| 5336 | } |
Tejun Heo | 1aa506e4 | 2007-03-09 19:36:12 +0900 | [diff] [blame] | 5337 | |
Taras Kondratiuk | 2623c7a | 2018-03-09 08:34:41 +0000 | [diff] [blame] | 5338 | void ata_host_get(struct ata_host *host) |
| 5339 | { |
| 5340 | kref_get(&host->kref); |
| 5341 | } |
| 5342 | |
| 5343 | void ata_host_put(struct ata_host *host) |
| 5344 | { |
| 5345 | kref_put(&host->kref, ata_host_release); |
Tejun Heo | f0d36ef | 2007-01-20 16:00:28 +0900 | [diff] [blame] | 5346 | } |
Bartlomiej Zolnierkiewicz | a52fbcf | 2020-03-26 16:58:04 +0100 | [diff] [blame] | 5347 | EXPORT_SYMBOL_GPL(ata_host_put); |
Tejun Heo | f0d36ef | 2007-01-20 16:00:28 +0900 | [diff] [blame] | 5348 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5349 | /** |
Tejun Heo | f318719 | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 5350 | * ata_host_alloc - allocate and init basic ATA host resources |
| 5351 | * @dev: generic device this host is associated with |
| 5352 | * @max_ports: maximum number of ATA ports associated with this host |
| 5353 | * |
| 5354 | * Allocate and initialize basic ATA host resources. LLD calls |
| 5355 | * this function to allocate a host, initializes it fully and |
| 5356 | * attaches it using ata_host_register(). |
| 5357 | * |
| 5358 | * @max_ports ports are allocated and host->n_ports is |
| 5359 | * initialized to @max_ports. The caller is allowed to decrease |
| 5360 | * host->n_ports before calling ata_host_register(). The unused |
| 5361 | * ports will be automatically freed on registration. |
| 5362 | * |
| 5363 | * RETURNS: |
| 5364 | * Allocate ATA host on success, NULL on failure. |
| 5365 | * |
| 5366 | * LOCKING: |
| 5367 | * Inherited from calling layer (may sleep). |
| 5368 | */ |
| 5369 | struct ata_host *ata_host_alloc(struct device *dev, int max_ports) |
| 5370 | { |
| 5371 | struct ata_host *host; |
| 5372 | size_t sz; |
| 5373 | int i; |
Taras Kondratiuk | 2623c7a | 2018-03-09 08:34:41 +0000 | [diff] [blame] | 5374 | void *dr; |
Tejun Heo | f318719 | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 5375 | |
| 5376 | DPRINTK("ENTER\n"); |
| 5377 | |
Tejun Heo | f318719 | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 5378 | /* alloc a container for our list of ATA ports (buses) */ |
| 5379 | sz = sizeof(struct ata_host) + (max_ports + 1) * sizeof(void *); |
Taras Kondratiuk | 2623c7a | 2018-03-09 08:34:41 +0000 | [diff] [blame] | 5380 | host = kzalloc(sz, GFP_KERNEL); |
Tejun Heo | f318719 | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 5381 | if (!host) |
Taras Kondratiuk | 2623c7a | 2018-03-09 08:34:41 +0000 | [diff] [blame] | 5382 | return NULL; |
| 5383 | |
Tejun Heo | f318719 | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 5384 | if (!devres_open_group(dev, NULL, GFP_KERNEL)) |
Colin Ian King | dafd6c4 | 2018-03-27 14:26:01 +0100 | [diff] [blame] | 5385 | goto err_free; |
Tejun Heo | f318719 | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 5386 | |
Taras Kondratiuk | 2623c7a | 2018-03-09 08:34:41 +0000 | [diff] [blame] | 5387 | dr = devres_alloc(ata_devres_release, 0, GFP_KERNEL); |
| 5388 | if (!dr) |
Tejun Heo | f318719 | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 5389 | goto err_out; |
| 5390 | |
Taras Kondratiuk | 2623c7a | 2018-03-09 08:34:41 +0000 | [diff] [blame] | 5391 | devres_add(dev, dr); |
Tejun Heo | f318719 | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 5392 | dev_set_drvdata(dev, host); |
| 5393 | |
| 5394 | spin_lock_init(&host->lock); |
Tejun Heo | c0c362b | 2010-09-06 17:57:14 +0200 | [diff] [blame] | 5395 | mutex_init(&host->eh_mutex); |
Tejun Heo | f318719 | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 5396 | host->dev = dev; |
| 5397 | host->n_ports = max_ports; |
Taras Kondratiuk | 2623c7a | 2018-03-09 08:34:41 +0000 | [diff] [blame] | 5398 | kref_init(&host->kref); |
Tejun Heo | f318719 | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 5399 | |
| 5400 | /* allocate ports bound to this host */ |
| 5401 | for (i = 0; i < max_ports; i++) { |
| 5402 | struct ata_port *ap; |
| 5403 | |
| 5404 | ap = ata_port_alloc(host); |
| 5405 | if (!ap) |
| 5406 | goto err_out; |
| 5407 | |
| 5408 | ap->port_no = i; |
| 5409 | host->ports[i] = ap; |
| 5410 | } |
| 5411 | |
| 5412 | devres_remove_group(dev, NULL); |
| 5413 | return host; |
| 5414 | |
| 5415 | err_out: |
| 5416 | devres_release_group(dev, NULL); |
Colin Ian King | dafd6c4 | 2018-03-27 14:26:01 +0100 | [diff] [blame] | 5417 | err_free: |
| 5418 | kfree(host); |
Tejun Heo | f318719 | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 5419 | return NULL; |
| 5420 | } |
Bartlomiej Zolnierkiewicz | a52fbcf | 2020-03-26 16:58:04 +0100 | [diff] [blame] | 5421 | EXPORT_SYMBOL_GPL(ata_host_alloc); |
Tejun Heo | f318719 | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 5422 | |
| 5423 | /** |
Tejun Heo | f5cda25 | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 5424 | * ata_host_alloc_pinfo - alloc host and init with port_info array |
| 5425 | * @dev: generic device this host is associated with |
| 5426 | * @ppi: array of ATA port_info to initialize host with |
| 5427 | * @n_ports: number of ATA ports attached to this host |
| 5428 | * |
| 5429 | * Allocate ATA host and initialize with info from @ppi. If NULL |
| 5430 | * terminated, @ppi may contain fewer entries than @n_ports. The |
| 5431 | * last entry will be used for the remaining ports. |
| 5432 | * |
| 5433 | * RETURNS: |
| 5434 | * Allocate ATA host on success, NULL on failure. |
| 5435 | * |
| 5436 | * LOCKING: |
| 5437 | * Inherited from calling layer (may sleep). |
| 5438 | */ |
| 5439 | struct ata_host *ata_host_alloc_pinfo(struct device *dev, |
| 5440 | const struct ata_port_info * const * ppi, |
| 5441 | int n_ports) |
| 5442 | { |
| 5443 | const struct ata_port_info *pi; |
| 5444 | struct ata_host *host; |
| 5445 | int i, j; |
| 5446 | |
| 5447 | host = ata_host_alloc(dev, n_ports); |
| 5448 | if (!host) |
| 5449 | return NULL; |
| 5450 | |
| 5451 | for (i = 0, j = 0, pi = NULL; i < host->n_ports; i++) { |
| 5452 | struct ata_port *ap = host->ports[i]; |
| 5453 | |
| 5454 | if (ppi[j]) |
| 5455 | pi = ppi[j++]; |
| 5456 | |
| 5457 | ap->pio_mask = pi->pio_mask; |
| 5458 | ap->mwdma_mask = pi->mwdma_mask; |
| 5459 | ap->udma_mask = pi->udma_mask; |
| 5460 | ap->flags |= pi->flags; |
Tejun Heo | 0c88758 | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 5461 | ap->link.flags |= pi->link_flags; |
Tejun Heo | f5cda25 | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 5462 | ap->ops = pi->port_ops; |
| 5463 | |
| 5464 | if (!host->ops && (pi->port_ops != &ata_dummy_port_ops)) |
| 5465 | host->ops = pi->port_ops; |
Tejun Heo | f5cda25 | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 5466 | } |
| 5467 | |
| 5468 | return host; |
| 5469 | } |
Bartlomiej Zolnierkiewicz | a52fbcf | 2020-03-26 16:58:04 +0100 | [diff] [blame] | 5470 | EXPORT_SYMBOL_GPL(ata_host_alloc_pinfo); |
Tejun Heo | f5cda25 | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 5471 | |
Tejun Heo | 32ebbc0 | 2007-11-08 13:09:00 +0900 | [diff] [blame] | 5472 | static void ata_host_stop(struct device *gendev, void *res) |
| 5473 | { |
| 5474 | struct ata_host *host = dev_get_drvdata(gendev); |
| 5475 | int i; |
| 5476 | |
| 5477 | WARN_ON(!(host->flags & ATA_HOST_STARTED)); |
| 5478 | |
| 5479 | for (i = 0; i < host->n_ports; i++) { |
| 5480 | struct ata_port *ap = host->ports[i]; |
| 5481 | |
| 5482 | if (ap->ops->port_stop) |
| 5483 | ap->ops->port_stop(ap); |
| 5484 | } |
| 5485 | |
| 5486 | if (host->ops->host_stop) |
| 5487 | host->ops->host_stop(host); |
| 5488 | } |
| 5489 | |
Tejun Heo | f5cda25 | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 5490 | /** |
Tejun Heo | 029cfd6 | 2008-03-25 12:22:49 +0900 | [diff] [blame] | 5491 | * ata_finalize_port_ops - finalize ata_port_operations |
| 5492 | * @ops: ata_port_operations to finalize |
| 5493 | * |
| 5494 | * An ata_port_operations can inherit from another ops and that |
| 5495 | * ops can again inherit from another. This can go on as many |
| 5496 | * times as necessary as long as there is no loop in the |
| 5497 | * inheritance chain. |
| 5498 | * |
| 5499 | * Ops tables are finalized when the host is started. NULL or |
| 5500 | * unspecified entries are inherited from the closet ancestor |
| 5501 | * which has the method and the entry is populated with it. |
| 5502 | * After finalization, the ops table directly points to all the |
| 5503 | * methods and ->inherits is no longer necessary and cleared. |
| 5504 | * |
| 5505 | * Using ATA_OP_NULL, inheriting ops can force a method to NULL. |
| 5506 | * |
| 5507 | * LOCKING: |
| 5508 | * None. |
| 5509 | */ |
| 5510 | static void ata_finalize_port_ops(struct ata_port_operations *ops) |
| 5511 | { |
Pradeep Singh Rautela | 2da6765 | 2008-05-29 23:28:14 +0530 | [diff] [blame] | 5512 | static DEFINE_SPINLOCK(lock); |
Tejun Heo | 029cfd6 | 2008-03-25 12:22:49 +0900 | [diff] [blame] | 5513 | const struct ata_port_operations *cur; |
| 5514 | void **begin = (void **)ops; |
| 5515 | void **end = (void **)&ops->inherits; |
| 5516 | void **pp; |
| 5517 | |
| 5518 | if (!ops || !ops->inherits) |
| 5519 | return; |
| 5520 | |
| 5521 | spin_lock(&lock); |
| 5522 | |
| 5523 | for (cur = ops->inherits; cur; cur = cur->inherits) { |
| 5524 | void **inherit = (void **)cur; |
| 5525 | |
| 5526 | for (pp = begin; pp < end; pp++, inherit++) |
| 5527 | if (!*pp) |
| 5528 | *pp = *inherit; |
| 5529 | } |
| 5530 | |
| 5531 | for (pp = begin; pp < end; pp++) |
| 5532 | if (IS_ERR(*pp)) |
| 5533 | *pp = NULL; |
| 5534 | |
| 5535 | ops->inherits = NULL; |
| 5536 | |
| 5537 | spin_unlock(&lock); |
| 5538 | } |
| 5539 | |
| 5540 | /** |
Tejun Heo | ecef725 | 2007-04-17 23:44:06 +0900 | [diff] [blame] | 5541 | * ata_host_start - start and freeze ports of an ATA host |
| 5542 | * @host: ATA host to start ports for |
| 5543 | * |
| 5544 | * Start and then freeze ports of @host. Started status is |
| 5545 | * recorded in host->flags, so this function can be called |
| 5546 | * multiple times. Ports are guaranteed to get started only |
Tejun Heo | f318719 | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 5547 | * once. If host->ops isn't initialized yet, its set to the |
| 5548 | * first non-dummy port ops. |
Tejun Heo | ecef725 | 2007-04-17 23:44:06 +0900 | [diff] [blame] | 5549 | * |
| 5550 | * LOCKING: |
| 5551 | * Inherited from calling layer (may sleep). |
| 5552 | * |
| 5553 | * RETURNS: |
| 5554 | * 0 if all ports are started successfully, -errno otherwise. |
| 5555 | */ |
| 5556 | int ata_host_start(struct ata_host *host) |
| 5557 | { |
Tejun Heo | 32ebbc0 | 2007-11-08 13:09:00 +0900 | [diff] [blame] | 5558 | int have_stop = 0; |
| 5559 | void *start_dr = NULL; |
Tejun Heo | ecef725 | 2007-04-17 23:44:06 +0900 | [diff] [blame] | 5560 | int i, rc; |
| 5561 | |
| 5562 | if (host->flags & ATA_HOST_STARTED) |
| 5563 | return 0; |
| 5564 | |
Tejun Heo | 029cfd6 | 2008-03-25 12:22:49 +0900 | [diff] [blame] | 5565 | ata_finalize_port_ops(host->ops); |
| 5566 | |
Tejun Heo | ecef725 | 2007-04-17 23:44:06 +0900 | [diff] [blame] | 5567 | for (i = 0; i < host->n_ports; i++) { |
| 5568 | struct ata_port *ap = host->ports[i]; |
| 5569 | |
Tejun Heo | 029cfd6 | 2008-03-25 12:22:49 +0900 | [diff] [blame] | 5570 | ata_finalize_port_ops(ap->ops); |
| 5571 | |
Tejun Heo | f318719 | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 5572 | if (!host->ops && !ata_port_is_dummy(ap)) |
| 5573 | host->ops = ap->ops; |
| 5574 | |
Tejun Heo | 32ebbc0 | 2007-11-08 13:09:00 +0900 | [diff] [blame] | 5575 | if (ap->ops->port_stop) |
| 5576 | have_stop = 1; |
| 5577 | } |
| 5578 | |
Damien Le Moal | 355a803 | 2021-08-16 10:44:47 +0900 | [diff] [blame] | 5579 | if (host->ops && host->ops->host_stop) |
Tejun Heo | 32ebbc0 | 2007-11-08 13:09:00 +0900 | [diff] [blame] | 5580 | have_stop = 1; |
| 5581 | |
| 5582 | if (have_stop) { |
| 5583 | start_dr = devres_alloc(ata_host_stop, 0, GFP_KERNEL); |
| 5584 | if (!start_dr) |
| 5585 | return -ENOMEM; |
| 5586 | } |
| 5587 | |
| 5588 | for (i = 0; i < host->n_ports; i++) { |
| 5589 | struct ata_port *ap = host->ports[i]; |
| 5590 | |
Tejun Heo | ecef725 | 2007-04-17 23:44:06 +0900 | [diff] [blame] | 5591 | if (ap->ops->port_start) { |
| 5592 | rc = ap->ops->port_start(ap); |
| 5593 | if (rc) { |
Alan Cox | 0f9fe9b | 2007-11-30 15:23:16 +0000 | [diff] [blame] | 5594 | if (rc != -ENODEV) |
Joe Perches | a44fec1 | 2011-04-15 15:51:58 -0700 | [diff] [blame] | 5595 | dev_err(host->dev, |
| 5596 | "failed to start port %d (errno=%d)\n", |
| 5597 | i, rc); |
Tejun Heo | ecef725 | 2007-04-17 23:44:06 +0900 | [diff] [blame] | 5598 | goto err_out; |
| 5599 | } |
| 5600 | } |
Tejun Heo | ecef725 | 2007-04-17 23:44:06 +0900 | [diff] [blame] | 5601 | ata_eh_freeze_port(ap); |
| 5602 | } |
| 5603 | |
Tejun Heo | 32ebbc0 | 2007-11-08 13:09:00 +0900 | [diff] [blame] | 5604 | if (start_dr) |
| 5605 | devres_add(host->dev, start_dr); |
Tejun Heo | ecef725 | 2007-04-17 23:44:06 +0900 | [diff] [blame] | 5606 | host->flags |= ATA_HOST_STARTED; |
| 5607 | return 0; |
| 5608 | |
| 5609 | err_out: |
| 5610 | while (--i >= 0) { |
| 5611 | struct ata_port *ap = host->ports[i]; |
| 5612 | |
| 5613 | if (ap->ops->port_stop) |
| 5614 | ap->ops->port_stop(ap); |
| 5615 | } |
Tejun Heo | 32ebbc0 | 2007-11-08 13:09:00 +0900 | [diff] [blame] | 5616 | devres_free(start_dr); |
Tejun Heo | ecef725 | 2007-04-17 23:44:06 +0900 | [diff] [blame] | 5617 | return rc; |
| 5618 | } |
Bartlomiej Zolnierkiewicz | a52fbcf | 2020-03-26 16:58:04 +0100 | [diff] [blame] | 5619 | EXPORT_SYMBOL_GPL(ata_host_start); |
Tejun Heo | ecef725 | 2007-04-17 23:44:06 +0900 | [diff] [blame] | 5620 | |
| 5621 | /** |
Mauro Carvalho Chehab | 94bd571 | 2020-10-23 18:32:55 +0200 | [diff] [blame] | 5622 | * ata_host_init - Initialize a host struct for sas (ipr, libsas) |
Jeff Garzik | cca3974 | 2006-08-24 03:19:22 -0400 | [diff] [blame] | 5623 | * @host: host to initialize |
| 5624 | * @dev: device host is attached to |
Jeff Garzik | cca3974 | 2006-08-24 03:19:22 -0400 | [diff] [blame] | 5625 | * @ops: port_ops |
Brian King | b03732f | 2006-08-07 14:27:10 -0500 | [diff] [blame] | 5626 | * |
Brian King | b03732f | 2006-08-07 14:27:10 -0500 | [diff] [blame] | 5627 | */ |
Jeff Garzik | cca3974 | 2006-08-24 03:19:22 -0400 | [diff] [blame] | 5628 | void ata_host_init(struct ata_host *host, struct device *dev, |
Dan Williams | 8d8e7d1 | 2012-07-09 21:06:08 -0700 | [diff] [blame] | 5629 | struct ata_port_operations *ops) |
Brian King | b03732f | 2006-08-07 14:27:10 -0500 | [diff] [blame] | 5630 | { |
Jeff Garzik | cca3974 | 2006-08-24 03:19:22 -0400 | [diff] [blame] | 5631 | spin_lock_init(&host->lock); |
Tejun Heo | c0c362b | 2010-09-06 17:57:14 +0200 | [diff] [blame] | 5632 | mutex_init(&host->eh_mutex); |
Jens Axboe | 69278f7 | 2018-05-11 12:51:10 -0600 | [diff] [blame] | 5633 | host->n_tags = ATA_MAX_QUEUE; |
Jeff Garzik | cca3974 | 2006-08-24 03:19:22 -0400 | [diff] [blame] | 5634 | host->dev = dev; |
Jeff Garzik | cca3974 | 2006-08-24 03:19:22 -0400 | [diff] [blame] | 5635 | host->ops = ops; |
Jason Yan | 2fa4a32 | 2018-05-10 11:05:16 +0800 | [diff] [blame] | 5636 | kref_init(&host->kref); |
Brian King | b03732f | 2006-08-07 14:27:10 -0500 | [diff] [blame] | 5637 | } |
Bartlomiej Zolnierkiewicz | a52fbcf | 2020-03-26 16:58:04 +0100 | [diff] [blame] | 5638 | EXPORT_SYMBOL_GPL(ata_host_init); |
Brian King | b03732f | 2006-08-07 14:27:10 -0500 | [diff] [blame] | 5639 | |
Dan Williams | 9508a66 | 2012-01-18 20:47:01 -0800 | [diff] [blame] | 5640 | void __ata_port_probe(struct ata_port *ap) |
| 5641 | { |
| 5642 | struct ata_eh_info *ehi = &ap->link.eh_info; |
| 5643 | unsigned long flags; |
| 5644 | |
| 5645 | /* kick EH for boot probing */ |
| 5646 | spin_lock_irqsave(ap->lock, flags); |
| 5647 | |
| 5648 | ehi->probe_mask |= ATA_ALL_DEVICES; |
| 5649 | ehi->action |= ATA_EH_RESET; |
| 5650 | ehi->flags |= ATA_EHI_NO_AUTOPSY | ATA_EHI_QUIET; |
| 5651 | |
| 5652 | ap->pflags &= ~ATA_PFLAG_INITIALIZING; |
| 5653 | ap->pflags |= ATA_PFLAG_LOADING; |
| 5654 | ata_port_schedule_eh(ap); |
| 5655 | |
| 5656 | spin_unlock_irqrestore(ap->lock, flags); |
| 5657 | } |
| 5658 | |
James Bottomley | 238c9cf | 2011-01-23 08:28:33 -0600 | [diff] [blame] | 5659 | int ata_port_probe(struct ata_port *ap) |
Arjan van de Ven | 7931805 | 2009-01-04 05:32:28 -0800 | [diff] [blame] | 5660 | { |
James Bottomley | 238c9cf | 2011-01-23 08:28:33 -0600 | [diff] [blame] | 5661 | int rc = 0; |
Arjan van de Ven | 886ad09 | 2009-01-09 15:54:07 -0800 | [diff] [blame] | 5662 | |
Arjan van de Ven | 7931805 | 2009-01-04 05:32:28 -0800 | [diff] [blame] | 5663 | if (ap->ops->error_handler) { |
Dan Williams | 9508a66 | 2012-01-18 20:47:01 -0800 | [diff] [blame] | 5664 | __ata_port_probe(ap); |
Arjan van de Ven | 7931805 | 2009-01-04 05:32:28 -0800 | [diff] [blame] | 5665 | ata_port_wait_eh(ap); |
| 5666 | } else { |
| 5667 | DPRINTK("ata%u: bus probe begin\n", ap->print_id); |
| 5668 | rc = ata_bus_probe(ap); |
| 5669 | DPRINTK("ata%u: bus probe end\n", ap->print_id); |
Arjan van de Ven | 7931805 | 2009-01-04 05:32:28 -0800 | [diff] [blame] | 5670 | } |
James Bottomley | 238c9cf | 2011-01-23 08:28:33 -0600 | [diff] [blame] | 5671 | return rc; |
| 5672 | } |
| 5673 | |
| 5674 | |
| 5675 | static void async_port_probe(void *data, async_cookie_t cookie) |
| 5676 | { |
| 5677 | struct ata_port *ap = data; |
Jeff Garzik | 4fca377 | 2011-02-15 01:13:24 -0500 | [diff] [blame] | 5678 | |
James Bottomley | 238c9cf | 2011-01-23 08:28:33 -0600 | [diff] [blame] | 5679 | /* |
| 5680 | * If we're not allowed to scan this host in parallel, |
| 5681 | * we need to wait until all previous scans have completed |
| 5682 | * before going further. |
| 5683 | * Jeff Garzik says this is only within a controller, so we |
| 5684 | * don't need to wait for port 0, only for later ports. |
| 5685 | */ |
| 5686 | if (!(ap->host->flags & ATA_HOST_PARALLEL_SCAN) && ap->port_no != 0) |
| 5687 | async_synchronize_cookie(cookie); |
| 5688 | |
| 5689 | (void)ata_port_probe(ap); |
Arjan van de Ven | f29d3b2 | 2009-01-05 15:07:07 -0800 | [diff] [blame] | 5690 | |
| 5691 | /* in order to keep device order, we need to synchronize at this point */ |
| 5692 | async_synchronize_cookie(cookie); |
| 5693 | |
| 5694 | ata_scsi_scan_host(ap, 1); |
Arjan van de Ven | 7931805 | 2009-01-04 05:32:28 -0800 | [diff] [blame] | 5695 | } |
James Bottomley | 238c9cf | 2011-01-23 08:28:33 -0600 | [diff] [blame] | 5696 | |
Brian King | b03732f | 2006-08-07 14:27:10 -0500 | [diff] [blame] | 5697 | /** |
Tejun Heo | f318719 | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 5698 | * ata_host_register - register initialized ATA host |
| 5699 | * @host: ATA host to register |
| 5700 | * @sht: template for SCSI host |
Jeff Garzik | 0cba632 | 2005-05-30 19:49:12 -0400 | [diff] [blame] | 5701 | * |
Tejun Heo | f318719 | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 5702 | * Register initialized ATA host. @host is allocated using |
| 5703 | * ata_host_alloc() and fully initialized by LLD. This function |
| 5704 | * starts ports, registers @host with ATA and SCSI layers and |
| 5705 | * probe registered devices. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5706 | * |
| 5707 | * LOCKING: |
Tejun Heo | f318719 | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 5708 | * Inherited from calling layer (may sleep). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5709 | * |
| 5710 | * RETURNS: |
Tejun Heo | f318719 | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 5711 | * 0 on success, -errno otherwise. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5712 | */ |
Tejun Heo | f318719 | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 5713 | 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] | 5714 | { |
Tejun Heo | f318719 | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 5715 | int i, rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5716 | |
Jens Axboe | 69278f7 | 2018-05-11 12:51:10 -0600 | [diff] [blame] | 5717 | host->n_tags = clamp(sht->can_queue, 1, ATA_MAX_QUEUE); |
Kevin Hao | 1871ee1 | 2014-07-12 12:08:24 +0800 | [diff] [blame] | 5718 | |
Tejun Heo | f318719 | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 5719 | /* host must have been started */ |
| 5720 | if (!(host->flags & ATA_HOST_STARTED)) { |
Joe Perches | a44fec1 | 2011-04-15 15:51:58 -0700 | [diff] [blame] | 5721 | dev_err(host->dev, "BUG: trying to register unstarted host\n"); |
Tejun Heo | f318719 | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 5722 | WARN_ON(1); |
| 5723 | return -EINVAL; |
Alan Cox | 02f076a | 2006-09-26 17:35:32 +0100 | [diff] [blame] | 5724 | } |
Tejun Heo | f0d36ef | 2007-01-20 16:00:28 +0900 | [diff] [blame] | 5725 | |
Tejun Heo | f318719 | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 5726 | /* Blow away unused ports. This happens when LLD can't |
| 5727 | * determine the exact number of ports to allocate at |
| 5728 | * allocation time. |
| 5729 | */ |
| 5730 | for (i = host->n_ports; host->ports[i]; i++) |
| 5731 | kfree(host->ports[i]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5732 | |
Tejun Heo | f318719 | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 5733 | /* give ports names and add SCSI hosts */ |
David Milburn | e628dc9 | 2013-05-14 13:48:40 -0500 | [diff] [blame] | 5734 | for (i = 0; i < host->n_ports; i++) { |
Dan Williams | 85d6725 | 2012-03-10 23:28:46 -0800 | [diff] [blame] | 5735 | host->ports[i]->print_id = atomic_inc_return(&ata_print_id); |
David Milburn | e628dc9 | 2013-05-14 13:48:40 -0500 | [diff] [blame] | 5736 | host->ports[i]->local_port_no = i + 1; |
| 5737 | } |
Jeff Garzik | 4fca377 | 2011-02-15 01:13:24 -0500 | [diff] [blame] | 5738 | |
Gwendal Grignou | d902747 | 2010-05-25 12:31:38 -0700 | [diff] [blame] | 5739 | /* Create associated sysfs transport objects */ |
| 5740 | for (i = 0; i < host->n_ports; i++) { |
| 5741 | rc = ata_tport_add(host->dev,host->ports[i]); |
| 5742 | if (rc) { |
| 5743 | goto err_tadd; |
| 5744 | } |
| 5745 | } |
| 5746 | |
Tejun Heo | f318719 | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 5747 | rc = ata_scsi_add_hosts(host, sht); |
Tejun Heo | ecef725 | 2007-04-17 23:44:06 +0900 | [diff] [blame] | 5748 | if (rc) |
Gwendal Grignou | d902747 | 2010-05-25 12:31:38 -0700 | [diff] [blame] | 5749 | goto err_tadd; |
Tejun Heo | ecef725 | 2007-04-17 23:44:06 +0900 | [diff] [blame] | 5750 | |
Tejun Heo | f318719 | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 5751 | /* set cable, sata_spd_limit and report */ |
Jeff Garzik | cca3974 | 2006-08-24 03:19:22 -0400 | [diff] [blame] | 5752 | for (i = 0; i < host->n_ports; i++) { |
| 5753 | struct ata_port *ap = host->ports[i]; |
Tejun Heo | f318719 | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 5754 | unsigned long xfer_mask; |
| 5755 | |
| 5756 | /* set SATA cable type if still unset */ |
| 5757 | if (ap->cbl == ATA_CBL_NONE && (ap->flags & ATA_FLAG_SATA)) |
| 5758 | ap->cbl = ATA_CBL_SATA; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5759 | |
Tejun Heo | 5a04bf4 | 2006-05-31 18:27:38 +0900 | [diff] [blame] | 5760 | /* init sata_spd_limit to the current value */ |
Tejun Heo | 4fb37a2 | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 5761 | sata_link_init_spd(&ap->link); |
Tejun Heo | b1c7291 | 2008-07-31 17:02:43 +0900 | [diff] [blame] | 5762 | if (ap->slave_link) |
| 5763 | sata_link_init_spd(ap->slave_link); |
Tejun Heo | 5a04bf4 | 2006-05-31 18:27:38 +0900 | [diff] [blame] | 5764 | |
Tejun Heo | cbcdd87 | 2007-08-18 13:14:55 +0900 | [diff] [blame] | 5765 | /* print per-port info to dmesg */ |
Tejun Heo | f318719 | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 5766 | xfer_mask = ata_pack_xfermask(ap->pio_mask, ap->mwdma_mask, |
| 5767 | ap->udma_mask); |
| 5768 | |
Tejun Heo | abf6e8e | 2007-10-09 14:57:25 +0900 | [diff] [blame] | 5769 | if (!ata_port_is_dummy(ap)) { |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 5770 | ata_port_info(ap, "%cATA max %s %s\n", |
| 5771 | (ap->flags & ATA_FLAG_SATA) ? 'S' : 'P', |
| 5772 | ata_mode_string(xfer_mask), |
| 5773 | ap->link.eh_info.desc); |
Tejun Heo | abf6e8e | 2007-10-09 14:57:25 +0900 | [diff] [blame] | 5774 | ata_ehi_clear_desc(&ap->link.eh_info); |
| 5775 | } else |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 5776 | ata_port_info(ap, "DUMMY\n"); |
Tejun Heo | f318719 | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 5777 | } |
| 5778 | |
Vegard Nossum | f600535 | 2009-04-08 18:19:39 +0200 | [diff] [blame] | 5779 | /* perform each probe asynchronously */ |
Tejun Heo | f318719 | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 5780 | for (i = 0; i < host->n_ports; i++) { |
| 5781 | struct ata_port *ap = host->ports[i]; |
Kai-Heng Feng | b529211 | 2020-06-03 15:48:19 +0800 | [diff] [blame] | 5782 | ap->cookie = async_schedule(async_port_probe, ap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5783 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5784 | |
Tejun Heo | f318719 | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 5785 | return 0; |
Gwendal Grignou | d902747 | 2010-05-25 12:31:38 -0700 | [diff] [blame] | 5786 | |
| 5787 | err_tadd: |
| 5788 | while (--i >= 0) { |
| 5789 | ata_tport_delete(host->ports[i]); |
| 5790 | } |
| 5791 | return rc; |
| 5792 | |
Tejun Heo | f318719 | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 5793 | } |
Bartlomiej Zolnierkiewicz | a52fbcf | 2020-03-26 16:58:04 +0100 | [diff] [blame] | 5794 | EXPORT_SYMBOL_GPL(ata_host_register); |
Tejun Heo | f318719 | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 5795 | |
| 5796 | /** |
Tejun Heo | f5cda25 | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 5797 | * ata_host_activate - start host, request IRQ and register it |
| 5798 | * @host: target ATA host |
| 5799 | * @irq: IRQ to request |
| 5800 | * @irq_handler: irq_handler used when requesting IRQ |
| 5801 | * @irq_flags: irq_flags used when requesting IRQ |
| 5802 | * @sht: scsi_host_template to use when registering the host |
| 5803 | * |
| 5804 | * After allocating an ATA host and initializing it, most libata |
| 5805 | * LLDs perform three steps to activate the host - start host, |
Masanari Iida | c9b5560 | 2016-04-13 23:36:27 +0900 | [diff] [blame] | 5806 | * request IRQ and register it. This helper takes necessary |
Tejun Heo | f5cda25 | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 5807 | * arguments and performs the three steps in one go. |
| 5808 | * |
Paul Mundt | 3d46b2e | 2007-11-08 11:14:56 +0900 | [diff] [blame] | 5809 | * An invalid IRQ skips the IRQ registration and expects the host to |
| 5810 | * have set polling mode on the port. In this case, @irq_handler |
| 5811 | * should be NULL. |
| 5812 | * |
Tejun Heo | f5cda25 | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 5813 | * LOCKING: |
| 5814 | * Inherited from calling layer (may sleep). |
| 5815 | * |
| 5816 | * RETURNS: |
| 5817 | * 0 on success, -errno otherwise. |
| 5818 | */ |
| 5819 | int ata_host_activate(struct ata_host *host, int irq, |
| 5820 | irq_handler_t irq_handler, unsigned long irq_flags, |
| 5821 | struct scsi_host_template *sht) |
| 5822 | { |
Tejun Heo | cbcdd87 | 2007-08-18 13:14:55 +0900 | [diff] [blame] | 5823 | int i, rc; |
Heiner Kallweit | 7e22c00 | 2015-12-06 21:56:33 +0100 | [diff] [blame] | 5824 | char *irq_desc; |
Tejun Heo | f5cda25 | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 5825 | |
| 5826 | rc = ata_host_start(host); |
| 5827 | if (rc) |
| 5828 | return rc; |
| 5829 | |
Paul Mundt | 3d46b2e | 2007-11-08 11:14:56 +0900 | [diff] [blame] | 5830 | /* Special case for polling mode */ |
| 5831 | if (!irq) { |
| 5832 | WARN_ON(irq_handler); |
| 5833 | return ata_host_register(host, sht); |
| 5834 | } |
| 5835 | |
Heiner Kallweit | 7e22c00 | 2015-12-06 21:56:33 +0100 | [diff] [blame] | 5836 | irq_desc = devm_kasprintf(host->dev, GFP_KERNEL, "%s[%s]", |
| 5837 | dev_driver_string(host->dev), |
| 5838 | dev_name(host->dev)); |
| 5839 | if (!irq_desc) |
| 5840 | return -ENOMEM; |
| 5841 | |
Tejun Heo | f5cda25 | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 5842 | rc = devm_request_irq(host->dev, irq, irq_handler, irq_flags, |
Heiner Kallweit | 7e22c00 | 2015-12-06 21:56:33 +0100 | [diff] [blame] | 5843 | irq_desc, host); |
Tejun Heo | f5cda25 | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 5844 | if (rc) |
| 5845 | return rc; |
| 5846 | |
Tejun Heo | cbcdd87 | 2007-08-18 13:14:55 +0900 | [diff] [blame] | 5847 | for (i = 0; i < host->n_ports; i++) |
| 5848 | ata_port_desc(host->ports[i], "irq %d", irq); |
Tejun Heo | 4031826 | 2007-07-03 01:38:47 +0900 | [diff] [blame] | 5849 | |
Tejun Heo | f5cda25 | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 5850 | rc = ata_host_register(host, sht); |
| 5851 | /* if failed, just free the IRQ and leave ports alone */ |
| 5852 | if (rc) |
| 5853 | devm_free_irq(host->dev, irq, host); |
| 5854 | |
| 5855 | return rc; |
| 5856 | } |
Bartlomiej Zolnierkiewicz | a52fbcf | 2020-03-26 16:58:04 +0100 | [diff] [blame] | 5857 | EXPORT_SYMBOL_GPL(ata_host_activate); |
Tejun Heo | f5cda25 | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 5858 | |
| 5859 | /** |
Masanari Iida | c9b5560 | 2016-04-13 23:36:27 +0900 | [diff] [blame] | 5860 | * ata_port_detach - Detach ATA port in preparation of device removal |
Tejun Heo | 720ba12 | 2006-05-31 18:28:13 +0900 | [diff] [blame] | 5861 | * @ap: ATA port to be detached |
| 5862 | * |
| 5863 | * Detach all ATA devices and the associated SCSI devices of @ap; |
| 5864 | * then, remove the associated SCSI host. @ap is guaranteed to |
| 5865 | * be quiescent on return from this function. |
| 5866 | * |
| 5867 | * LOCKING: |
| 5868 | * Kernel thread context (may sleep). |
| 5869 | */ |
Adrian Bunk | 741b776 | 2007-10-24 18:23:06 +0200 | [diff] [blame] | 5870 | static void ata_port_detach(struct ata_port *ap) |
Tejun Heo | 720ba12 | 2006-05-31 18:28:13 +0900 | [diff] [blame] | 5871 | { |
| 5872 | unsigned long flags; |
Levente Kurusa | a6f9bf4 | 2014-05-06 15:57:48 +0200 | [diff] [blame] | 5873 | struct ata_link *link; |
| 5874 | struct ata_device *dev; |
Tejun Heo | 720ba12 | 2006-05-31 18:28:13 +0900 | [diff] [blame] | 5875 | |
| 5876 | if (!ap->ops->error_handler) |
Tejun Heo | c3cf30a | 2006-08-05 03:59:11 +0900 | [diff] [blame] | 5877 | goto skip_eh; |
Tejun Heo | 720ba12 | 2006-05-31 18:28:13 +0900 | [diff] [blame] | 5878 | |
| 5879 | /* tell EH we're leaving & flush EH */ |
Jeff Garzik | ba6a130 | 2006-06-22 23:46:10 -0400 | [diff] [blame] | 5880 | spin_lock_irqsave(ap->lock, flags); |
Tejun Heo | b51e9e5 | 2006-06-29 01:29:30 +0900 | [diff] [blame] | 5881 | ap->pflags |= ATA_PFLAG_UNLOADING; |
Tejun Heo | ece180d | 2008-11-03 20:04:37 +0900 | [diff] [blame] | 5882 | ata_port_schedule_eh(ap); |
Jeff Garzik | ba6a130 | 2006-06-22 23:46:10 -0400 | [diff] [blame] | 5883 | spin_unlock_irqrestore(ap->lock, flags); |
Tejun Heo | 720ba12 | 2006-05-31 18:28:13 +0900 | [diff] [blame] | 5884 | |
Tejun Heo | ece180d | 2008-11-03 20:04:37 +0900 | [diff] [blame] | 5885 | /* wait till EH commits suicide */ |
Tejun Heo | 720ba12 | 2006-05-31 18:28:13 +0900 | [diff] [blame] | 5886 | ata_port_wait_eh(ap); |
| 5887 | |
Tejun Heo | ece180d | 2008-11-03 20:04:37 +0900 | [diff] [blame] | 5888 | /* it better be dead now */ |
| 5889 | WARN_ON(!(ap->pflags & ATA_PFLAG_UNLOADED)); |
Tejun Heo | 720ba12 | 2006-05-31 18:28:13 +0900 | [diff] [blame] | 5890 | |
Tejun Heo | afe2c51 | 2010-12-14 16:21:17 +0100 | [diff] [blame] | 5891 | cancel_delayed_work_sync(&ap->hotplug_task); |
Tejun Heo | 720ba12 | 2006-05-31 18:28:13 +0900 | [diff] [blame] | 5892 | |
Tejun Heo | c3cf30a | 2006-08-05 03:59:11 +0900 | [diff] [blame] | 5893 | skip_eh: |
Levente Kurusa | a6f9bf4 | 2014-05-06 15:57:48 +0200 | [diff] [blame] | 5894 | /* clean up zpodd on port removal */ |
| 5895 | ata_for_each_link(link, ap, HOST_FIRST) { |
| 5896 | ata_for_each_dev(dev, link, ALL) { |
| 5897 | if (zpodd_dev_enabled(dev)) |
| 5898 | zpodd_exit(dev); |
| 5899 | } |
| 5900 | } |
Gwendal Grignou | d902747 | 2010-05-25 12:31:38 -0700 | [diff] [blame] | 5901 | if (ap->pmp_link) { |
| 5902 | int i; |
| 5903 | for (i = 0; i < SATA_PMP_MAX_PORTS; i++) |
| 5904 | ata_tlink_delete(&ap->pmp_link[i]); |
| 5905 | } |
Tejun Heo | 720ba12 | 2006-05-31 18:28:13 +0900 | [diff] [blame] | 5906 | /* remove the associated SCSI host */ |
Jeff Garzik | cca3974 | 2006-08-24 03:19:22 -0400 | [diff] [blame] | 5907 | scsi_remove_host(ap->scsi_host); |
Rafael J. Wysocki | c570076 | 2013-11-25 13:19:01 +0100 | [diff] [blame] | 5908 | ata_tport_delete(ap); |
Tejun Heo | 720ba12 | 2006-05-31 18:28:13 +0900 | [diff] [blame] | 5909 | } |
| 5910 | |
| 5911 | /** |
Tejun Heo | 0529c159 | 2007-01-20 16:00:26 +0900 | [diff] [blame] | 5912 | * ata_host_detach - Detach all ports of an ATA host |
| 5913 | * @host: Host to detach |
| 5914 | * |
| 5915 | * Detach all ports of @host. |
| 5916 | * |
| 5917 | * LOCKING: |
| 5918 | * Kernel thread context (may sleep). |
| 5919 | */ |
| 5920 | void ata_host_detach(struct ata_host *host) |
| 5921 | { |
| 5922 | int i; |
| 5923 | |
Kai-Heng Feng | b529211 | 2020-06-03 15:48:19 +0800 | [diff] [blame] | 5924 | for (i = 0; i < host->n_ports; i++) { |
| 5925 | /* Ensure ata_port probe has completed */ |
| 5926 | async_synchronize_cookie(host->ports[i]->cookie + 1); |
Tejun Heo | 0529c159 | 2007-01-20 16:00:26 +0900 | [diff] [blame] | 5927 | ata_port_detach(host->ports[i]); |
Kai-Heng Feng | b529211 | 2020-06-03 15:48:19 +0800 | [diff] [blame] | 5928 | } |
Tejun Heo | 562f0c2 | 2007-12-15 15:05:01 +0900 | [diff] [blame] | 5929 | |
| 5930 | /* the host is dead now, dissociate ACPI */ |
| 5931 | ata_acpi_dissociate(host); |
Tejun Heo | 0529c159 | 2007-01-20 16:00:26 +0900 | [diff] [blame] | 5932 | } |
Bartlomiej Zolnierkiewicz | a52fbcf | 2020-03-26 16:58:04 +0100 | [diff] [blame] | 5933 | EXPORT_SYMBOL_GPL(ata_host_detach); |
Tejun Heo | 0529c159 | 2007-01-20 16:00:26 +0900 | [diff] [blame] | 5934 | |
Jeff Garzik | 374b187 | 2005-08-30 05:42:52 -0400 | [diff] [blame] | 5935 | #ifdef CONFIG_PCI |
| 5936 | |
Edward Falk | 0baab86 | 2005-06-02 18:17:13 -0400 | [diff] [blame] | 5937 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5938 | * ata_pci_remove_one - PCI layer callback for device removal |
| 5939 | * @pdev: PCI device that was removed |
| 5940 | * |
Tejun Heo | b878ca5 | 2007-01-20 16:00:28 +0900 | [diff] [blame] | 5941 | * PCI layer indicates to libata via this hook that hot-unplug or |
| 5942 | * module unload event has occurred. Detach all ports. Resource |
| 5943 | * release is handled via devres. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5944 | * |
| 5945 | * LOCKING: |
| 5946 | * Inherited from PCI layer (may sleep). |
| 5947 | */ |
Tejun Heo | f0d36ef | 2007-01-20 16:00:28 +0900 | [diff] [blame] | 5948 | void ata_pci_remove_one(struct pci_dev *pdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5949 | { |
Brian Norris | 04a3f5b | 2012-12-03 10:34:41 -0800 | [diff] [blame] | 5950 | struct ata_host *host = pci_get_drvdata(pdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5951 | |
Tejun Heo | b878ca5 | 2007-01-20 16:00:28 +0900 | [diff] [blame] | 5952 | ata_host_detach(host); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5953 | } |
Bartlomiej Zolnierkiewicz | a52fbcf | 2020-03-26 16:58:04 +0100 | [diff] [blame] | 5954 | EXPORT_SYMBOL_GPL(ata_pci_remove_one); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5955 | |
Prabhakar Kushwaha | 10a663a | 2020-01-25 03:37:29 +0000 | [diff] [blame] | 5956 | void ata_pci_shutdown_one(struct pci_dev *pdev) |
| 5957 | { |
| 5958 | struct ata_host *host = pci_get_drvdata(pdev); |
| 5959 | int i; |
| 5960 | |
| 5961 | for (i = 0; i < host->n_ports; i++) { |
| 5962 | struct ata_port *ap = host->ports[i]; |
| 5963 | |
| 5964 | ap->pflags |= ATA_PFLAG_FROZEN; |
| 5965 | |
| 5966 | /* Disable port interrupts */ |
| 5967 | if (ap->ops->freeze) |
| 5968 | ap->ops->freeze(ap); |
| 5969 | |
| 5970 | /* Stop the port DMA engines */ |
| 5971 | if (ap->ops->port_stop) |
| 5972 | ap->ops->port_stop(ap); |
| 5973 | } |
| 5974 | } |
Bartlomiej Zolnierkiewicz | a52fbcf | 2020-03-26 16:58:04 +0100 | [diff] [blame] | 5975 | EXPORT_SYMBOL_GPL(ata_pci_shutdown_one); |
Prabhakar Kushwaha | 10a663a | 2020-01-25 03:37:29 +0000 | [diff] [blame] | 5976 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5977 | /* move to PCI subsystem */ |
Jeff Garzik | 057ace5 | 2005-10-22 14:27:05 -0400 | [diff] [blame] | 5978 | 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] | 5979 | { |
| 5980 | unsigned long tmp = 0; |
| 5981 | |
| 5982 | switch (bits->width) { |
| 5983 | case 1: { |
| 5984 | u8 tmp8 = 0; |
| 5985 | pci_read_config_byte(pdev, bits->reg, &tmp8); |
| 5986 | tmp = tmp8; |
| 5987 | break; |
| 5988 | } |
| 5989 | case 2: { |
| 5990 | u16 tmp16 = 0; |
| 5991 | pci_read_config_word(pdev, bits->reg, &tmp16); |
| 5992 | tmp = tmp16; |
| 5993 | break; |
| 5994 | } |
| 5995 | case 4: { |
| 5996 | u32 tmp32 = 0; |
| 5997 | pci_read_config_dword(pdev, bits->reg, &tmp32); |
| 5998 | tmp = tmp32; |
| 5999 | break; |
| 6000 | } |
| 6001 | |
| 6002 | default: |
| 6003 | return -EINVAL; |
| 6004 | } |
| 6005 | |
| 6006 | tmp &= bits->mask; |
| 6007 | |
| 6008 | return (tmp == bits->val) ? 1 : 0; |
| 6009 | } |
Bartlomiej Zolnierkiewicz | a52fbcf | 2020-03-26 16:58:04 +0100 | [diff] [blame] | 6010 | EXPORT_SYMBOL_GPL(pci_test_config_bits); |
Jens Axboe | 9b84754 | 2006-01-06 09:28:07 +0100 | [diff] [blame] | 6011 | |
Tejun Heo | 6ffa01d | 2007-03-02 17:32:47 +0900 | [diff] [blame] | 6012 | #ifdef CONFIG_PM |
Tejun Heo | 3c5100c | 2006-07-26 16:58:33 +0900 | [diff] [blame] | 6013 | 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] | 6014 | { |
| 6015 | pci_save_state(pdev); |
Tejun Heo | 4c90d97 | 2007-02-20 18:14:48 +0900 | [diff] [blame] | 6016 | pci_disable_device(pdev); |
Tejun Heo | 500530f | 2006-07-03 16:07:27 +0900 | [diff] [blame] | 6017 | |
Rafael J. Wysocki | 3a2d5b7 | 2008-02-23 19:13:25 +0100 | [diff] [blame] | 6018 | if (mesg.event & PM_EVENT_SLEEP) |
Tejun Heo | 500530f | 2006-07-03 16:07:27 +0900 | [diff] [blame] | 6019 | pci_set_power_state(pdev, PCI_D3hot); |
Jens Axboe | 9b84754 | 2006-01-06 09:28:07 +0100 | [diff] [blame] | 6020 | } |
Bartlomiej Zolnierkiewicz | a52fbcf | 2020-03-26 16:58:04 +0100 | [diff] [blame] | 6021 | EXPORT_SYMBOL_GPL(ata_pci_device_do_suspend); |
Jens Axboe | 9b84754 | 2006-01-06 09:28:07 +0100 | [diff] [blame] | 6022 | |
Tejun Heo | 553c4aa | 2006-12-26 19:39:50 +0900 | [diff] [blame] | 6023 | int ata_pci_device_do_resume(struct pci_dev *pdev) |
Jens Axboe | 9b84754 | 2006-01-06 09:28:07 +0100 | [diff] [blame] | 6024 | { |
Tejun Heo | 553c4aa | 2006-12-26 19:39:50 +0900 | [diff] [blame] | 6025 | int rc; |
| 6026 | |
Jens Axboe | 9b84754 | 2006-01-06 09:28:07 +0100 | [diff] [blame] | 6027 | pci_set_power_state(pdev, PCI_D0); |
| 6028 | pci_restore_state(pdev); |
Tejun Heo | 553c4aa | 2006-12-26 19:39:50 +0900 | [diff] [blame] | 6029 | |
Tejun Heo | b878ca5 | 2007-01-20 16:00:28 +0900 | [diff] [blame] | 6030 | rc = pcim_enable_device(pdev); |
Tejun Heo | 553c4aa | 2006-12-26 19:39:50 +0900 | [diff] [blame] | 6031 | if (rc) { |
Joe Perches | a44fec1 | 2011-04-15 15:51:58 -0700 | [diff] [blame] | 6032 | dev_err(&pdev->dev, |
| 6033 | "failed to enable device after resume (%d)\n", rc); |
Tejun Heo | 553c4aa | 2006-12-26 19:39:50 +0900 | [diff] [blame] | 6034 | return rc; |
| 6035 | } |
| 6036 | |
Jens Axboe | 9b84754 | 2006-01-06 09:28:07 +0100 | [diff] [blame] | 6037 | pci_set_master(pdev); |
Tejun Heo | 553c4aa | 2006-12-26 19:39:50 +0900 | [diff] [blame] | 6038 | return 0; |
Tejun Heo | 500530f | 2006-07-03 16:07:27 +0900 | [diff] [blame] | 6039 | } |
Bartlomiej Zolnierkiewicz | a52fbcf | 2020-03-26 16:58:04 +0100 | [diff] [blame] | 6040 | EXPORT_SYMBOL_GPL(ata_pci_device_do_resume); |
Tejun Heo | 500530f | 2006-07-03 16:07:27 +0900 | [diff] [blame] | 6041 | |
Tejun Heo | 3c5100c | 2006-07-26 16:58:33 +0900 | [diff] [blame] | 6042 | 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] | 6043 | { |
Brian Norris | 04a3f5b | 2012-12-03 10:34:41 -0800 | [diff] [blame] | 6044 | struct ata_host *host = pci_get_drvdata(pdev); |
Tejun Heo | 500530f | 2006-07-03 16:07:27 +0900 | [diff] [blame] | 6045 | int rc = 0; |
| 6046 | |
Jeff Garzik | cca3974 | 2006-08-24 03:19:22 -0400 | [diff] [blame] | 6047 | rc = ata_host_suspend(host, mesg); |
Tejun Heo | 500530f | 2006-07-03 16:07:27 +0900 | [diff] [blame] | 6048 | if (rc) |
| 6049 | return rc; |
| 6050 | |
Tejun Heo | 3c5100c | 2006-07-26 16:58:33 +0900 | [diff] [blame] | 6051 | ata_pci_device_do_suspend(pdev, mesg); |
Tejun Heo | 500530f | 2006-07-03 16:07:27 +0900 | [diff] [blame] | 6052 | |
| 6053 | return 0; |
| 6054 | } |
Bartlomiej Zolnierkiewicz | a52fbcf | 2020-03-26 16:58:04 +0100 | [diff] [blame] | 6055 | EXPORT_SYMBOL_GPL(ata_pci_device_suspend); |
Tejun Heo | 500530f | 2006-07-03 16:07:27 +0900 | [diff] [blame] | 6056 | |
| 6057 | int ata_pci_device_resume(struct pci_dev *pdev) |
| 6058 | { |
Brian Norris | 04a3f5b | 2012-12-03 10:34:41 -0800 | [diff] [blame] | 6059 | struct ata_host *host = pci_get_drvdata(pdev); |
Tejun Heo | 553c4aa | 2006-12-26 19:39:50 +0900 | [diff] [blame] | 6060 | int rc; |
Tejun Heo | 500530f | 2006-07-03 16:07:27 +0900 | [diff] [blame] | 6061 | |
Tejun Heo | 553c4aa | 2006-12-26 19:39:50 +0900 | [diff] [blame] | 6062 | rc = ata_pci_device_do_resume(pdev); |
| 6063 | if (rc == 0) |
| 6064 | ata_host_resume(host); |
| 6065 | return rc; |
Jens Axboe | 9b84754 | 2006-01-06 09:28:07 +0100 | [diff] [blame] | 6066 | } |
Bartlomiej Zolnierkiewicz | a52fbcf | 2020-03-26 16:58:04 +0100 | [diff] [blame] | 6067 | EXPORT_SYMBOL_GPL(ata_pci_device_resume); |
Tejun Heo | 6ffa01d | 2007-03-02 17:32:47 +0900 | [diff] [blame] | 6068 | #endif /* CONFIG_PM */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6069 | #endif /* CONFIG_PCI */ |
| 6070 | |
Brian Norris | b7db04d | 2012-11-02 12:29:32 -0700 | [diff] [blame] | 6071 | /** |
| 6072 | * ata_platform_remove_one - Platform layer callback for device removal |
| 6073 | * @pdev: Platform device that was removed |
| 6074 | * |
| 6075 | * Platform layer indicates to libata via this hook that hot-unplug or |
| 6076 | * module unload event has occurred. Detach all ports. Resource |
| 6077 | * release is handled via devres. |
| 6078 | * |
| 6079 | * LOCKING: |
| 6080 | * Inherited from platform layer (may sleep). |
| 6081 | */ |
| 6082 | int ata_platform_remove_one(struct platform_device *pdev) |
| 6083 | { |
| 6084 | struct ata_host *host = platform_get_drvdata(pdev); |
| 6085 | |
| 6086 | ata_host_detach(host); |
| 6087 | |
| 6088 | return 0; |
| 6089 | } |
Bartlomiej Zolnierkiewicz | a52fbcf | 2020-03-26 16:58:04 +0100 | [diff] [blame] | 6090 | EXPORT_SYMBOL_GPL(ata_platform_remove_one); |
Brian Norris | b7db04d | 2012-11-02 12:29:32 -0700 | [diff] [blame] | 6091 | |
Bartlomiej Zolnierkiewicz | bf89b0b | 2020-03-26 16:58:22 +0100 | [diff] [blame] | 6092 | #ifdef CONFIG_ATA_FORCE |
Tejun Heo | 3326732 | 2008-02-13 09:15:09 +0900 | [diff] [blame] | 6093 | static int __init ata_parse_force_one(char **cur, |
| 6094 | struct ata_force_ent *force_ent, |
| 6095 | const char **reason) |
| 6096 | { |
Rasmus Villemoes | 0f5f264 | 2015-06-09 15:33:19 +0200 | [diff] [blame] | 6097 | static const struct ata_force_param force_tbl[] __initconst = { |
Tejun Heo | 3326732 | 2008-02-13 09:15:09 +0900 | [diff] [blame] | 6098 | { "40c", .cbl = ATA_CBL_PATA40 }, |
| 6099 | { "80c", .cbl = ATA_CBL_PATA80 }, |
| 6100 | { "short40c", .cbl = ATA_CBL_PATA40_SHORT }, |
| 6101 | { "unk", .cbl = ATA_CBL_PATA_UNK }, |
| 6102 | { "ign", .cbl = ATA_CBL_PATA_IGN }, |
| 6103 | { "sata", .cbl = ATA_CBL_SATA }, |
| 6104 | { "1.5Gbps", .spd_limit = 1 }, |
| 6105 | { "3.0Gbps", .spd_limit = 2 }, |
| 6106 | { "noncq", .horkage_on = ATA_HORKAGE_NONCQ }, |
| 6107 | { "ncq", .horkage_off = ATA_HORKAGE_NONCQ }, |
Martin K. Petersen | d7b16e4 | 2015-05-04 21:54:18 -0400 | [diff] [blame] | 6108 | { "noncqtrim", .horkage_on = ATA_HORKAGE_NO_NCQ_TRIM }, |
| 6109 | { "ncqtrim", .horkage_off = ATA_HORKAGE_NO_NCQ_TRIM }, |
Tejun Heo | 43c9c59 | 2010-05-23 12:59:11 +0200 | [diff] [blame] | 6110 | { "dump_id", .horkage_on = ATA_HORKAGE_DUMP_ID }, |
Tejun Heo | 3326732 | 2008-02-13 09:15:09 +0900 | [diff] [blame] | 6111 | { "pio0", .xfer_mask = 1 << (ATA_SHIFT_PIO + 0) }, |
| 6112 | { "pio1", .xfer_mask = 1 << (ATA_SHIFT_PIO + 1) }, |
| 6113 | { "pio2", .xfer_mask = 1 << (ATA_SHIFT_PIO + 2) }, |
| 6114 | { "pio3", .xfer_mask = 1 << (ATA_SHIFT_PIO + 3) }, |
| 6115 | { "pio4", .xfer_mask = 1 << (ATA_SHIFT_PIO + 4) }, |
| 6116 | { "pio5", .xfer_mask = 1 << (ATA_SHIFT_PIO + 5) }, |
| 6117 | { "pio6", .xfer_mask = 1 << (ATA_SHIFT_PIO + 6) }, |
| 6118 | { "mwdma0", .xfer_mask = 1 << (ATA_SHIFT_MWDMA + 0) }, |
| 6119 | { "mwdma1", .xfer_mask = 1 << (ATA_SHIFT_MWDMA + 1) }, |
| 6120 | { "mwdma2", .xfer_mask = 1 << (ATA_SHIFT_MWDMA + 2) }, |
| 6121 | { "mwdma3", .xfer_mask = 1 << (ATA_SHIFT_MWDMA + 3) }, |
| 6122 | { "mwdma4", .xfer_mask = 1 << (ATA_SHIFT_MWDMA + 4) }, |
| 6123 | { "udma0", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 0) }, |
| 6124 | { "udma16", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 0) }, |
| 6125 | { "udma/16", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 0) }, |
| 6126 | { "udma1", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 1) }, |
| 6127 | { "udma25", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 1) }, |
| 6128 | { "udma/25", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 1) }, |
| 6129 | { "udma2", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 2) }, |
| 6130 | { "udma33", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 2) }, |
| 6131 | { "udma/33", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 2) }, |
| 6132 | { "udma3", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 3) }, |
| 6133 | { "udma44", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 3) }, |
| 6134 | { "udma/44", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 3) }, |
| 6135 | { "udma4", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 4) }, |
| 6136 | { "udma66", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 4) }, |
| 6137 | { "udma/66", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 4) }, |
| 6138 | { "udma5", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 5) }, |
| 6139 | { "udma100", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 5) }, |
| 6140 | { "udma/100", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 5) }, |
| 6141 | { "udma6", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 6) }, |
| 6142 | { "udma133", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 6) }, |
| 6143 | { "udma/133", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 6) }, |
| 6144 | { "udma7", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 7) }, |
Tejun Heo | 05944bd | 2008-08-13 20:19:09 +0900 | [diff] [blame] | 6145 | { "nohrst", .lflags = ATA_LFLAG_NO_HRST }, |
| 6146 | { "nosrst", .lflags = ATA_LFLAG_NO_SRST }, |
| 6147 | { "norst", .lflags = ATA_LFLAG_NO_HRST | ATA_LFLAG_NO_SRST }, |
Dan Williams | ca6d43b | 2012-06-21 23:41:41 -0700 | [diff] [blame] | 6148 | { "rstonce", .lflags = ATA_LFLAG_RST_ONCE }, |
Vincent Pelletier | 966fbe1 | 2013-05-21 22:30:58 +0200 | [diff] [blame] | 6149 | { "atapi_dmadir", .horkage_on = ATA_HORKAGE_ATAPI_DMADIR }, |
Robin H. Johnson | b8bd6dc | 2013-12-16 09:31:19 -0800 | [diff] [blame] | 6150 | { "disable", .horkage_on = ATA_HORKAGE_DISABLE }, |
Tejun Heo | 3326732 | 2008-02-13 09:15:09 +0900 | [diff] [blame] | 6151 | }; |
| 6152 | char *start = *cur, *p = *cur; |
| 6153 | char *id, *val, *endp; |
| 6154 | const struct ata_force_param *match_fp = NULL; |
| 6155 | int nr_matches = 0, i; |
| 6156 | |
| 6157 | /* find where this param ends and update *cur */ |
| 6158 | while (*p != '\0' && *p != ',') |
| 6159 | p++; |
| 6160 | |
| 6161 | if (*p == '\0') |
| 6162 | *cur = p; |
| 6163 | else |
| 6164 | *cur = p + 1; |
| 6165 | |
| 6166 | *p = '\0'; |
| 6167 | |
| 6168 | /* parse */ |
| 6169 | p = strchr(start, ':'); |
| 6170 | if (!p) { |
| 6171 | val = strstrip(start); |
| 6172 | goto parse_val; |
| 6173 | } |
| 6174 | *p = '\0'; |
| 6175 | |
| 6176 | id = strstrip(start); |
| 6177 | val = strstrip(p + 1); |
| 6178 | |
| 6179 | /* parse id */ |
| 6180 | p = strchr(id, '.'); |
| 6181 | if (p) { |
| 6182 | *p++ = '\0'; |
| 6183 | force_ent->device = simple_strtoul(p, &endp, 10); |
| 6184 | if (p == endp || *endp != '\0') { |
| 6185 | *reason = "invalid device"; |
| 6186 | return -EINVAL; |
| 6187 | } |
| 6188 | } |
| 6189 | |
| 6190 | force_ent->port = simple_strtoul(id, &endp, 10); |
Tejun Heo | f7cf69a | 2017-05-31 14:26:26 -0400 | [diff] [blame] | 6191 | if (id == endp || *endp != '\0') { |
Tejun Heo | 3326732 | 2008-02-13 09:15:09 +0900 | [diff] [blame] | 6192 | *reason = "invalid port/link"; |
| 6193 | return -EINVAL; |
| 6194 | } |
| 6195 | |
| 6196 | parse_val: |
| 6197 | /* parse val, allow shortcuts so that both 1.5 and 1.5Gbps work */ |
| 6198 | for (i = 0; i < ARRAY_SIZE(force_tbl); i++) { |
| 6199 | const struct ata_force_param *fp = &force_tbl[i]; |
| 6200 | |
| 6201 | if (strncasecmp(val, fp->name, strlen(val))) |
| 6202 | continue; |
| 6203 | |
| 6204 | nr_matches++; |
| 6205 | match_fp = fp; |
| 6206 | |
| 6207 | if (strcasecmp(val, fp->name) == 0) { |
| 6208 | nr_matches = 1; |
| 6209 | break; |
| 6210 | } |
| 6211 | } |
| 6212 | |
| 6213 | if (!nr_matches) { |
| 6214 | *reason = "unknown value"; |
| 6215 | return -EINVAL; |
| 6216 | } |
| 6217 | if (nr_matches > 1) { |
Arvind Yadav | 9de5535 | 2017-10-28 23:51:47 +0530 | [diff] [blame] | 6218 | *reason = "ambiguous value"; |
Tejun Heo | 3326732 | 2008-02-13 09:15:09 +0900 | [diff] [blame] | 6219 | return -EINVAL; |
| 6220 | } |
| 6221 | |
| 6222 | force_ent->param = *match_fp; |
| 6223 | |
| 6224 | return 0; |
| 6225 | } |
| 6226 | |
| 6227 | static void __init ata_parse_force_param(void) |
| 6228 | { |
| 6229 | int idx = 0, size = 1; |
| 6230 | int last_port = -1, last_device = -1; |
| 6231 | char *p, *cur, *next; |
| 6232 | |
| 6233 | /* calculate maximum number of params and allocate force_tbl */ |
| 6234 | for (p = ata_force_param_buf; *p; p++) |
| 6235 | if (*p == ',') |
| 6236 | size++; |
| 6237 | |
Kees Cook | 6396bb2 | 2018-06-12 14:03:40 -0700 | [diff] [blame] | 6238 | ata_force_tbl = kcalloc(size, sizeof(ata_force_tbl[0]), GFP_KERNEL); |
Tejun Heo | 3326732 | 2008-02-13 09:15:09 +0900 | [diff] [blame] | 6239 | if (!ata_force_tbl) { |
| 6240 | printk(KERN_WARNING "ata: failed to extend force table, " |
| 6241 | "libata.force ignored\n"); |
| 6242 | return; |
| 6243 | } |
| 6244 | |
| 6245 | /* parse and populate the table */ |
| 6246 | for (cur = ata_force_param_buf; *cur != '\0'; cur = next) { |
| 6247 | const char *reason = ""; |
| 6248 | struct ata_force_ent te = { .port = -1, .device = -1 }; |
| 6249 | |
| 6250 | next = cur; |
| 6251 | if (ata_parse_force_one(&next, &te, &reason)) { |
| 6252 | printk(KERN_WARNING "ata: failed to parse force " |
| 6253 | "parameter \"%s\" (%s)\n", |
| 6254 | cur, reason); |
| 6255 | continue; |
| 6256 | } |
| 6257 | |
| 6258 | if (te.port == -1) { |
| 6259 | te.port = last_port; |
| 6260 | te.device = last_device; |
| 6261 | } |
| 6262 | |
| 6263 | ata_force_tbl[idx++] = te; |
| 6264 | |
| 6265 | last_port = te.port; |
| 6266 | last_device = te.device; |
| 6267 | } |
| 6268 | |
| 6269 | ata_force_tbl_size = idx; |
| 6270 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6271 | |
Bartlomiej Zolnierkiewicz | bf89b0b | 2020-03-26 16:58:22 +0100 | [diff] [blame] | 6272 | static void ata_free_force_param(void) |
| 6273 | { |
| 6274 | kfree(ata_force_tbl); |
| 6275 | } |
| 6276 | #else |
| 6277 | static inline void ata_parse_force_param(void) { } |
| 6278 | static inline void ata_free_force_param(void) { } |
| 6279 | #endif |
| 6280 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6281 | static int __init ata_init(void) |
| 6282 | { |
Gwendal Grignou | d902747 | 2010-05-25 12:31:38 -0700 | [diff] [blame] | 6283 | int rc; |
Tejun Heo | 270390e | 2010-05-10 21:41:35 +0200 | [diff] [blame] | 6284 | |
Tejun Heo | 3326732 | 2008-02-13 09:15:09 +0900 | [diff] [blame] | 6285 | ata_parse_force_param(); |
| 6286 | |
Tejun Heo | 270390e | 2010-05-10 21:41:35 +0200 | [diff] [blame] | 6287 | rc = ata_sff_init(); |
Tejun Heo | ad72cf9 | 2010-07-02 10:03:52 +0200 | [diff] [blame] | 6288 | if (rc) { |
Bartlomiej Zolnierkiewicz | bf89b0b | 2020-03-26 16:58:22 +0100 | [diff] [blame] | 6289 | ata_free_force_param(); |
Tejun Heo | ad72cf9 | 2010-07-02 10:03:52 +0200 | [diff] [blame] | 6290 | return rc; |
| 6291 | } |
Tejun Heo | 453b07a | 2006-05-31 18:27:42 +0900 | [diff] [blame] | 6292 | |
Gwendal Grignou | d902747 | 2010-05-25 12:31:38 -0700 | [diff] [blame] | 6293 | libata_transport_init(); |
| 6294 | ata_scsi_transport_template = ata_attach_transport(); |
| 6295 | if (!ata_scsi_transport_template) { |
| 6296 | ata_sff_exit(); |
| 6297 | rc = -ENOMEM; |
| 6298 | goto err_out; |
Jeff Garzik | 4fca377 | 2011-02-15 01:13:24 -0500 | [diff] [blame] | 6299 | } |
Gwendal Grignou | d902747 | 2010-05-25 12:31:38 -0700 | [diff] [blame] | 6300 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6301 | printk(KERN_DEBUG "libata version " DRV_VERSION " loaded.\n"); |
| 6302 | return 0; |
Gwendal Grignou | d902747 | 2010-05-25 12:31:38 -0700 | [diff] [blame] | 6303 | |
| 6304 | err_out: |
| 6305 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6306 | } |
| 6307 | |
| 6308 | static void __exit ata_exit(void) |
| 6309 | { |
Gwendal Grignou | d902747 | 2010-05-25 12:31:38 -0700 | [diff] [blame] | 6310 | ata_release_transport(ata_scsi_transport_template); |
| 6311 | libata_transport_exit(); |
Tejun Heo | 270390e | 2010-05-10 21:41:35 +0200 | [diff] [blame] | 6312 | ata_sff_exit(); |
Bartlomiej Zolnierkiewicz | bf89b0b | 2020-03-26 16:58:22 +0100 | [diff] [blame] | 6313 | ata_free_force_param(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6314 | } |
| 6315 | |
Brian King | a462508 | 2006-11-13 16:32:36 -0600 | [diff] [blame] | 6316 | subsys_initcall(ata_init); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6317 | module_exit(ata_exit); |
| 6318 | |
Akinobu Mita | 9990b6f | 2010-04-12 21:11:41 +0900 | [diff] [blame] | 6319 | static DEFINE_RATELIMIT_STATE(ratelimit, HZ / 5, 1); |
Jeff Garzik | 67846b3 | 2005-10-05 02:58:32 -0400 | [diff] [blame] | 6320 | |
| 6321 | int ata_ratelimit(void) |
| 6322 | { |
Akinobu Mita | 9990b6f | 2010-04-12 21:11:41 +0900 | [diff] [blame] | 6323 | return __ratelimit(&ratelimit); |
Jeff Garzik | 67846b3 | 2005-10-05 02:58:32 -0400 | [diff] [blame] | 6324 | } |
Bartlomiej Zolnierkiewicz | a52fbcf | 2020-03-26 16:58:04 +0100 | [diff] [blame] | 6325 | EXPORT_SYMBOL_GPL(ata_ratelimit); |
Jeff Garzik | 67846b3 | 2005-10-05 02:58:32 -0400 | [diff] [blame] | 6326 | |
Tejun Heo | c0c362b | 2010-09-06 17:57:14 +0200 | [diff] [blame] | 6327 | /** |
| 6328 | * ata_msleep - ATA EH owner aware msleep |
| 6329 | * @ap: ATA port to attribute the sleep to |
| 6330 | * @msecs: duration to sleep in milliseconds |
| 6331 | * |
| 6332 | * Sleeps @msecs. If the current task is owner of @ap's EH, the |
| 6333 | * ownership is released before going to sleep and reacquired |
| 6334 | * after the sleep is complete. IOW, other ports sharing the |
| 6335 | * @ap->host will be allowed to own the EH while this task is |
| 6336 | * sleeping. |
| 6337 | * |
| 6338 | * LOCKING: |
| 6339 | * Might sleep. |
| 6340 | */ |
Tejun Heo | 97750ce | 2010-09-06 17:56:29 +0200 | [diff] [blame] | 6341 | void ata_msleep(struct ata_port *ap, unsigned int msecs) |
| 6342 | { |
Tejun Heo | c0c362b | 2010-09-06 17:57:14 +0200 | [diff] [blame] | 6343 | bool owns_eh = ap && ap->host->eh_owner == current; |
| 6344 | |
| 6345 | if (owns_eh) |
| 6346 | ata_eh_release(ap); |
| 6347 | |
Anil Veliyankara Madam | 848c392 | 2016-01-07 21:18:52 -0800 | [diff] [blame] | 6348 | if (msecs < 20) { |
| 6349 | unsigned long usecs = msecs * USEC_PER_MSEC; |
| 6350 | usleep_range(usecs, usecs + 50); |
| 6351 | } else { |
| 6352 | msleep(msecs); |
| 6353 | } |
Tejun Heo | c0c362b | 2010-09-06 17:57:14 +0200 | [diff] [blame] | 6354 | |
| 6355 | if (owns_eh) |
| 6356 | ata_eh_acquire(ap); |
Tejun Heo | 97750ce | 2010-09-06 17:56:29 +0200 | [diff] [blame] | 6357 | } |
Bartlomiej Zolnierkiewicz | a52fbcf | 2020-03-26 16:58:04 +0100 | [diff] [blame] | 6358 | EXPORT_SYMBOL_GPL(ata_msleep); |
Tejun Heo | 97750ce | 2010-09-06 17:56:29 +0200 | [diff] [blame] | 6359 | |
Tejun Heo | c22daff | 2006-04-11 22:22:29 +0900 | [diff] [blame] | 6360 | /** |
| 6361 | * ata_wait_register - wait until register value changes |
Tejun Heo | 97750ce | 2010-09-06 17:56:29 +0200 | [diff] [blame] | 6362 | * @ap: ATA port to wait register for, can be NULL |
Tejun Heo | c22daff | 2006-04-11 22:22:29 +0900 | [diff] [blame] | 6363 | * @reg: IO-mapped register |
| 6364 | * @mask: Mask to apply to read register value |
| 6365 | * @val: Wait condition |
Tejun Heo | 341c2c9 | 2008-05-20 02:17:51 +0900 | [diff] [blame] | 6366 | * @interval: polling interval in milliseconds |
| 6367 | * @timeout: timeout in milliseconds |
Tejun Heo | c22daff | 2006-04-11 22:22:29 +0900 | [diff] [blame] | 6368 | * |
| 6369 | * Waiting for some bits of register to change is a common |
| 6370 | * operation for ATA controllers. This function reads 32bit LE |
| 6371 | * IO-mapped register @reg and tests for the following condition. |
| 6372 | * |
| 6373 | * (*@reg & mask) != val |
| 6374 | * |
| 6375 | * If the condition is met, it returns; otherwise, the process is |
| 6376 | * repeated after @interval_msec until timeout. |
| 6377 | * |
| 6378 | * LOCKING: |
| 6379 | * Kernel thread context (may sleep) |
| 6380 | * |
| 6381 | * RETURNS: |
| 6382 | * The final register value. |
| 6383 | */ |
Tejun Heo | 97750ce | 2010-09-06 17:56:29 +0200 | [diff] [blame] | 6384 | 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] | 6385 | unsigned long interval, unsigned long timeout) |
Tejun Heo | c22daff | 2006-04-11 22:22:29 +0900 | [diff] [blame] | 6386 | { |
Tejun Heo | 341c2c9 | 2008-05-20 02:17:51 +0900 | [diff] [blame] | 6387 | unsigned long deadline; |
Tejun Heo | c22daff | 2006-04-11 22:22:29 +0900 | [diff] [blame] | 6388 | u32 tmp; |
| 6389 | |
| 6390 | tmp = ioread32(reg); |
| 6391 | |
| 6392 | /* Calculate timeout _after_ the first read to make sure |
| 6393 | * preceding writes reach the controller before starting to |
| 6394 | * eat away the timeout. |
| 6395 | */ |
Tejun Heo | 341c2c9 | 2008-05-20 02:17:51 +0900 | [diff] [blame] | 6396 | deadline = ata_deadline(jiffies, timeout); |
Tejun Heo | c22daff | 2006-04-11 22:22:29 +0900 | [diff] [blame] | 6397 | |
Tejun Heo | 341c2c9 | 2008-05-20 02:17:51 +0900 | [diff] [blame] | 6398 | while ((tmp & mask) == val && time_before(jiffies, deadline)) { |
Tejun Heo | 97750ce | 2010-09-06 17:56:29 +0200 | [diff] [blame] | 6399 | ata_msleep(ap, interval); |
Tejun Heo | c22daff | 2006-04-11 22:22:29 +0900 | [diff] [blame] | 6400 | tmp = ioread32(reg); |
| 6401 | } |
| 6402 | |
| 6403 | return tmp; |
| 6404 | } |
Bartlomiej Zolnierkiewicz | a52fbcf | 2020-03-26 16:58:04 +0100 | [diff] [blame] | 6405 | EXPORT_SYMBOL_GPL(ata_wait_register); |
Tejun Heo | c22daff | 2006-04-11 22:22:29 +0900 | [diff] [blame] | 6406 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6407 | /* |
Tejun Heo | dd5b06c | 2006-08-10 16:59:12 +0900 | [diff] [blame] | 6408 | * Dummy port_ops |
| 6409 | */ |
Tejun Heo | dd5b06c | 2006-08-10 16:59:12 +0900 | [diff] [blame] | 6410 | static unsigned int ata_dummy_qc_issue(struct ata_queued_cmd *qc) |
| 6411 | { |
| 6412 | return AC_ERR_SYSTEM; |
| 6413 | } |
| 6414 | |
Tejun Heo | 182d7bb | 2008-04-07 22:47:21 +0900 | [diff] [blame] | 6415 | static void ata_dummy_error_handler(struct ata_port *ap) |
| 6416 | { |
| 6417 | /* truly dummy */ |
| 6418 | } |
| 6419 | |
Tejun Heo | 029cfd6 | 2008-03-25 12:22:49 +0900 | [diff] [blame] | 6420 | struct ata_port_operations ata_dummy_port_ops = { |
Tejun Heo | dd5b06c | 2006-08-10 16:59:12 +0900 | [diff] [blame] | 6421 | .qc_prep = ata_noop_qc_prep, |
| 6422 | .qc_issue = ata_dummy_qc_issue, |
Tejun Heo | 182d7bb | 2008-04-07 22:47:21 +0900 | [diff] [blame] | 6423 | .error_handler = ata_dummy_error_handler, |
Dan Williams | e4a9c37 | 2012-06-21 23:25:27 -0700 | [diff] [blame] | 6424 | .sched_eh = ata_std_sched_eh, |
| 6425 | .end_eh = ata_std_end_eh, |
Tejun Heo | dd5b06c | 2006-08-10 16:59:12 +0900 | [diff] [blame] | 6426 | }; |
Bartlomiej Zolnierkiewicz | a52fbcf | 2020-03-26 16:58:04 +0100 | [diff] [blame] | 6427 | EXPORT_SYMBOL_GPL(ata_dummy_port_ops); |
Tejun Heo | dd5b06c | 2006-08-10 16:59:12 +0900 | [diff] [blame] | 6428 | |
Tejun Heo | 21b0ad4 | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 6429 | const struct ata_port_info ata_dummy_port_info = { |
| 6430 | .port_ops = &ata_dummy_port_ops, |
| 6431 | }; |
Bartlomiej Zolnierkiewicz | a52fbcf | 2020-03-26 16:58:04 +0100 | [diff] [blame] | 6432 | EXPORT_SYMBOL_GPL(ata_dummy_port_info); |
Tejun Heo | 21b0ad4 | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 6433 | |
Tejun Heo | dd5b06c | 2006-08-10 16:59:12 +0900 | [diff] [blame] | 6434 | /* |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 6435 | * Utility print functions |
| 6436 | */ |
Joe Perches | d7bead1 | 2014-09-22 09:52:18 -0700 | [diff] [blame] | 6437 | void ata_port_printk(const struct ata_port *ap, const char *level, |
| 6438 | const char *fmt, ...) |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 6439 | { |
| 6440 | struct va_format vaf; |
| 6441 | va_list args; |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 6442 | |
| 6443 | va_start(args, fmt); |
| 6444 | |
| 6445 | vaf.fmt = fmt; |
| 6446 | vaf.va = &args; |
| 6447 | |
Joe Perches | d7bead1 | 2014-09-22 09:52:18 -0700 | [diff] [blame] | 6448 | printk("%sata%u: %pV", level, ap->print_id, &vaf); |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 6449 | |
| 6450 | va_end(args); |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 6451 | } |
| 6452 | EXPORT_SYMBOL(ata_port_printk); |
| 6453 | |
Joe Perches | d7bead1 | 2014-09-22 09:52:18 -0700 | [diff] [blame] | 6454 | void ata_link_printk(const struct ata_link *link, const char *level, |
| 6455 | const char *fmt, ...) |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 6456 | { |
| 6457 | struct va_format vaf; |
| 6458 | va_list args; |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 6459 | |
| 6460 | va_start(args, fmt); |
| 6461 | |
| 6462 | vaf.fmt = fmt; |
| 6463 | vaf.va = &args; |
| 6464 | |
| 6465 | if (sata_pmp_attached(link->ap) || link->ap->slave_link) |
Joe Perches | d7bead1 | 2014-09-22 09:52:18 -0700 | [diff] [blame] | 6466 | printk("%sata%u.%02u: %pV", |
| 6467 | level, link->ap->print_id, link->pmp, &vaf); |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 6468 | else |
Joe Perches | d7bead1 | 2014-09-22 09:52:18 -0700 | [diff] [blame] | 6469 | printk("%sata%u: %pV", |
| 6470 | level, link->ap->print_id, &vaf); |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 6471 | |
| 6472 | va_end(args); |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 6473 | } |
| 6474 | EXPORT_SYMBOL(ata_link_printk); |
| 6475 | |
Joe Perches | d7bead1 | 2014-09-22 09:52:18 -0700 | [diff] [blame] | 6476 | void ata_dev_printk(const struct ata_device *dev, const char *level, |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 6477 | const char *fmt, ...) |
| 6478 | { |
| 6479 | struct va_format vaf; |
| 6480 | va_list args; |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 6481 | |
| 6482 | va_start(args, fmt); |
| 6483 | |
| 6484 | vaf.fmt = fmt; |
| 6485 | vaf.va = &args; |
| 6486 | |
Joe Perches | d7bead1 | 2014-09-22 09:52:18 -0700 | [diff] [blame] | 6487 | printk("%sata%u.%02u: %pV", |
| 6488 | level, dev->link->ap->print_id, dev->link->pmp + dev->devno, |
| 6489 | &vaf); |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 6490 | |
| 6491 | va_end(args); |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 6492 | } |
| 6493 | EXPORT_SYMBOL(ata_dev_printk); |
| 6494 | |
Joe Perches | 06296a1 | 2011-04-15 15:52:00 -0700 | [diff] [blame] | 6495 | void ata_print_version(const struct device *dev, const char *version) |
| 6496 | { |
| 6497 | dev_printk(KERN_DEBUG, dev, "version %s\n", version); |
| 6498 | } |
| 6499 | EXPORT_SYMBOL(ata_print_version); |